Remove useless_const optimization

There could be jump depending on the Pop being there, and then it
breaks.
This commit is contained in:
coolreader18 2019-09-15 18:34:48 -05:00 committed by Windel Bouwman
parent b8726f03df
commit bff58fd626
3 changed files with 11 additions and 23 deletions

View file

@ -848,7 +848,9 @@ impl<O: OutputStream> Compiler<O> {
// Emit None at end: // Emit None at end:
match body.last().map(|s| &s.node) { 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 { self.emit(Instruction::LoadConst {
value: bytecode::Constant::None, value: bytecode::Constant::None,

View file

@ -92,7 +92,7 @@ impl<O: OutputStream> PeepholeOptimizer<O> {
} }
fn optimize(&mut self) { 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 { pub trait OptimizationBuffer {
fn emit(&mut self, instruction: Instruction, meta: InstructionMetadata); fn emit(&mut self, instruction: Instruction, meta: InstructionMetadata);
fn pop(&mut self) -> (Instruction, InstructionMetadata); fn pop(&mut self) -> (Instruction, InstructionMetadata);

View file

@ -2,6 +2,11 @@ use rustpython_bytecode::bytecode::{self, Instruction};
use super::{InstructionMetadata, OptimizationBuffer}; use super::{InstructionMetadata, OptimizationBuffer};
macro_rules! metas {
[$($metas:expr),*$(,)?] => {
InstructionMetadata::from(vec![$($metas),*])
};
}
macro_rules! lc { macro_rules! lc {
($name:ident {$($field:tt)*}) => { ($name:ident {$($field:tt)*}) => {
Instruction::LoadConst { Instruction::LoadConst {
@ -13,10 +18,10 @@ macro_rules! lc {
}; };
} }
macro_rules! emitconst { macro_rules! emitconst {
($buf:expr, [$($metas:expr),*], $($arg:tt)*) => { ($buf:expr, [$($metas:expr),*$(,)?], $($arg:tt)*) => {
$buf.emit( $buf.emit(
lc!($($arg)*), lc!($($arg)*),
InstructionMetadata::from(vec![$($metas),*]), metas![$($metas),*],
) )
}; };
} }
@ -91,20 +96,3 @@ pub fn unpack(buf: &mut impl OptimizationBuffer) {
buf.emit(instruction, meta) 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);
}
}