Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/peephole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<O: OutputStream> PeepholeOptimizer<O> {
}

fn optimize(&mut self) {
apply_optimizations!(self, operator, unpack);
apply_optimizations!(self, operator /* , unpack */);
}
}

Expand Down
47 changes: 24 additions & 23 deletions compiler/src/peephole/optimizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,27 @@ pub fn operator(buf: &mut impl OptimizationBuffer) {
}
}

pub fn unpack(buf: &mut impl OptimizationBuffer) {
let (instruction, meta) = buf.pop();
if let Instruction::UnpackSequence { size } = instruction {
let (arg, arg_meta) = buf.pop();
match arg {
Instruction::BuildTuple {
size: tup_size,
unpack,
} if !unpack && tup_size == size => {
buf.emit(
Instruction::Reverse { amount: size },
vec![arg_meta, meta].into(),
);
}
arg => {
buf.emit(arg, arg_meta);
buf.emit(instruction, meta);
}
}
} else {
buf.emit(instruction, meta)
}
}
// TODO: make a version of this that doesn't miscompile `a, b = (1, 2) if True else (3, 4)`
// pub fn unpack(buf: &mut impl OptimizationBuffer) {
// let (instruction, meta) = buf.pop();
// if let Instruction::UnpackSequence { size } = instruction {
// let (arg, arg_meta) = buf.pop();
// match arg {
// Instruction::BuildTuple {
// size: tup_size,
// unpack,
// } if !unpack && tup_size == size => {
// buf.emit(
// Instruction::Reverse { amount: size },
// vec![arg_meta, meta].into(),
// );
// }
// arg => {
// buf.emit(arg, arg_meta);
// buf.emit(instruction, meta);
// }
// }
// } else {
// buf.emit(instruction, meta)
// }
// }
4 changes: 4 additions & 0 deletions tests/snippets/if_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ def func2():
return 20

assert ret(func1() or func2()) == 20

a, b = (1, 2) if True else (3, 4)
assert a == 1
assert b == 2