Skip to content

Commit a878ffc

Browse files
coolreader18windelbouwman
authored andcommitted
Remove useless_const optimization
There could be jump depending on the Pop being there, and then it breaks.
1 parent b71b4e6 commit a878ffc

4 files changed

Lines changed: 23 additions & 25 deletions

File tree

compiler/src/compile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,9 @@ impl<O: OutputStream> Compiler<O> {
848848

849849
// Emit None at end:
850850
match body.last().map(|s| &s.node) {
851-
Some(ast::StatementType::Return { .. }) => {}
851+
Some(ast::StatementType::Return { .. }) => {
852+
// the last instruction is a ReturnValue already, we don't need to emit it
853+
}
852854
_ => {
853855
self.emit(Instruction::LoadConst {
854856
value: bytecode::Constant::None,

compiler/src/peephole.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<O: OutputStream> PeepholeOptimizer<O> {
9292
}
9393

9494
fn optimize(&mut self) {
95-
apply_optimizations!(self, operator, unpack, useless_const);
95+
apply_optimizations!(self, operator, unpack);
9696
}
9797
}
9898

@@ -123,8 +123,6 @@ impl<O: OutputStream> OptimizationBuffer for PeepholeOptimizer<O> {
123123
}
124124
}
125125

126-
// OPTIMIZATION
127-
128126
pub trait OptimizationBuffer {
129127
fn emit(&mut self, instruction: Instruction, meta: InstructionMetadata);
130128
fn pop(&mut self) -> (Instruction, InstructionMetadata);

compiler/src/peephole/optimizations.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ use rustpython_bytecode::bytecode::{self, Instruction};
22

33
use super::{InstructionMetadata, OptimizationBuffer};
44

5+
macro_rules! metas {
6+
[$($metas:expr),*$(,)?] => {
7+
InstructionMetadata::from(vec![$($metas),*])
8+
};
9+
}
510
macro_rules! lc {
611
($name:ident {$($field:tt)*}) => {
712
Instruction::LoadConst {
@@ -13,10 +18,10 @@ macro_rules! lc {
1318
};
1419
}
1520
macro_rules! emitconst {
16-
($buf:expr, [$($metas:expr),*], $($arg:tt)*) => {
21+
($buf:expr, [$($metas:expr),*$(,)?], $($arg:tt)*) => {
1722
$buf.emit(
1823
lc!($($arg)*),
19-
InstructionMetadata::from(vec![$($metas),*]),
24+
metas![$($metas),*],
2025
)
2126
};
2227
}
@@ -91,20 +96,3 @@ pub fn unpack(buf: &mut impl OptimizationBuffer) {
9196
buf.emit(instruction, meta)
9297
}
9398
}
94-
95-
pub fn useless_const(buf: &mut impl OptimizationBuffer) {
96-
let (instruction, meta) = buf.pop();
97-
if instruction == Instruction::Pop {
98-
let (arg, arg_meta) = buf.pop();
99-
if let Instruction::LoadConst { .. } = arg {
100-
// just ignore it all
101-
drop(arg);
102-
drop(instruction);
103-
} else {
104-
buf.emit(arg, arg_meta);
105-
buf.emit(instruction, meta);
106-
}
107-
} else {
108-
buf.emit(instruction, meta);
109-
}
110-
}

tests/snippets/dismod.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22

33
dis.dis(compile("5 + x + 5 or 2", "", "eval"))
44
print("\n")
5-
dis.dis(compile("def f(x):\n return 1", "", "exec"))
5+
dis.dis(compile("""
6+
def f(x):
7+
return 1
8+
""", "", "exec"))
69
print("\n")
7-
dis.dis(compile("if a:\n 1 or 2\nelif x == 'hello':\n 3\nelse:\n 4", "", "exec"))
10+
dis.dis(compile("""
11+
if a:
12+
1 or 2
13+
elif x == 'hello':
14+
3
15+
else:
16+
4
17+
""", "", "exec"))
818
print("\n")
919
dis.dis(compile("f(x=1, y=2)", "", "eval"))
1020
print("\n")

0 commit comments

Comments
 (0)