diff --git a/src/compile.rs b/src/compile.rs index b1b5f62..403b148 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -848,7 +848,9 @@ impl Compiler { // 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, diff --git a/src/peephole.rs b/src/peephole.rs index 2eff20c..3a64380 100644 --- a/src/peephole.rs +++ b/src/peephole.rs @@ -92,7 +92,7 @@ impl PeepholeOptimizer { } fn optimize(&mut self) { - apply_optimizations!(self, operator, unpack, useless_const); + apply_optimizations!(self, operator, unpack); } } @@ -123,8 +123,6 @@ impl OptimizationBuffer for PeepholeOptimizer { } } -// OPTIMIZATION - pub trait OptimizationBuffer { fn emit(&mut self, instruction: Instruction, meta: InstructionMetadata); fn pop(&mut self) -> (Instruction, InstructionMetadata); diff --git a/src/peephole/optimizations.rs b/src/peephole/optimizations.rs index ee8f1f0..b0fb753 100644 --- a/src/peephole/optimizations.rs +++ b/src/peephole/optimizations.rs @@ -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); - } -}