Remove useless_const optimization

There could be jump depending on the Pop being there, and then it
breaks.
This commit is contained in:
coolreader18 authored and Windel Bouwman committed 2019-09-17 08:04:11 +02:00
1 parent b71b4e6f14
commit a878ffc678
4 files changed
+23 -25

No files matched your search

+3 -1
View File
@@ -848,7 +848,9 @@ impl<O: OutputStream> Compiler<O> {
// Emit None at end:
match body.last().map(|s| &s.node) {
Some(ast::StatementType::Return { .. }) => {}
Some(ast::StatementType::Return { .. }) => {
// the last instruction is a ReturnValue already, we don't need to emit it
}
_ => {
self.emit(Instruction::LoadConst {
value: bytecode::Constant::None,
+1 -3
View File
@@ -92,7 +92,7 @@ impl<O: OutputStream> PeepholeOptimizer<O> {
}
fn optimize(&mut self) {
apply_optimizations!(self, operator, unpack, useless_const);
apply_optimizations!(self, operator, unpack);
}
}
@@ -123,8 +123,6 @@ impl<O: OutputStream> OptimizationBuffer for PeepholeOptimizer<O> {
}
}
// OPTIMIZATION
pub trait OptimizationBuffer {
fn emit(&mut self, instruction: Instruction, meta: InstructionMetadata);
fn pop(&mut self) -> (Instruction, InstructionMetadata);
+7 -19
View File
@@ -2,6 +2,11 @@ use rustpython_bytecode::bytecode::{self, Instruction};
use super::{InstructionMetadata, OptimizationBuffer};
macro_rules! metas {
[$($metas:expr),*$(,)?] => {
InstructionMetadata::from(vec![$($metas),*])
};
}
macro_rules! lc {
($name:ident {$($field:tt)*}) => {
Instruction::LoadConst {
@@ -13,10 +18,10 @@ macro_rules! lc {
};
}
macro_rules! emitconst {
($buf:expr, [$($metas:expr),*], $($arg:tt)*) => {
($buf:expr, [$($metas:expr),*$(,)?], $($arg:tt)*) => {
$buf.emit(
lc!($($arg)*),
InstructionMetadata::from(vec![$($metas),*]),
metas![$($metas),*],
)
};
}
@@ -91,20 +96,3 @@ pub fn unpack(buf: &mut impl OptimizationBuffer) {
buf.emit(instruction, meta)
}
}
pub fn useless_const(buf: &mut impl OptimizationBuffer) {
let (instruction, meta) = buf.pop();
if instruction == Instruction::Pop {
let (arg, arg_meta) = buf.pop();
if let Instruction::LoadConst { .. } = arg {
// just ignore it all
drop(arg);
drop(instruction);
} else {
buf.emit(arg, arg_meta);
buf.emit(instruction, meta);
}
} else {
buf.emit(instruction, meta);
}
}
+12 -2
View File
@@ -2,9 +2,19 @@ import dis
dis.dis(compile("5 + x + 5 or 2", "", "eval"))
print("\n")
dis.dis(compile("def f(x):\n return 1", "", "exec"))
dis.dis(compile("""
def f(x):
return 1
""", "", "exec"))
print("\n")
dis.dis(compile("if a:\n 1 or 2\nelif x == 'hello':\n 3\nelse:\n 4", "", "exec"))
dis.dis(compile("""
if a:
1 or 2
elif x == 'hello':
3
else:
4
""", "", "exec"))
print("\n")
dis.dis(compile("f(x=1, y=2)", "", "eval"))
print("\n")