mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-15 09:05:25 +00:00
Remove useless_const optimization
There could be jump depending on the Pop being there, and then it breaks.
This commit is contained in:
parent
b8726f03df
commit
bff58fd626
3 changed files with 11 additions and 23 deletions
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue