Skip to content

Commit 48f3772

Browse files
committed
Restore PopJumpIf instructions
1 parent a1399bc commit 48f3772

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

crates/compiler-core/src/bytecode.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,20 @@ pub enum Instruction {
736736
},
737737
/// Performs `is` comparison, or `is not` if `invert` is 1.
738738
IsOp(Arg<Invert>),
739+
/// Peek at the top of the stack, and jump if this value is false.
740+
/// Otherwise, pop top of stack.
741+
JumpIfFalseOrPop {
742+
target: Arg<Label>,
743+
},
739744
/// Performs exception matching for except.
740745
/// Tests whether the STACK[-2] is an exception matching STACK[-1].
741746
/// Pops STACK[-1] and pushes the boolean result of the test.
742747
JumpIfNotExcMatch(Arg<Label>),
748+
/// Peek at the top of the stack, and jump if this value is true.
749+
/// Otherwise, pop top of stack.
750+
JumpIfTrueOrPop {
751+
target: Arg<Label>,
752+
},
743753
Jump {
744754
target: Arg<Label>,
745755
},
@@ -1611,6 +1621,8 @@ impl Instruction {
16111621
| JumpIfNotExcMatch(l)
16121622
| PopJumpIfTrue { target: l }
16131623
| PopJumpIfFalse { target: l }
1624+
| JumpIfTrueOrPop { target: l }
1625+
| JumpIfFalseOrPop { target: l }
16141626
| ForIter { target: l }
16151627
| SetupFinally { handler: l }
16161628
| SetupExcept { handler: l }
@@ -1688,6 +1700,13 @@ impl Instruction {
16881700
Break { .. } => 0,
16891701
Jump { .. } => 0,
16901702
PopJumpIfTrue { .. } | PopJumpIfFalse { .. } => -1,
1703+
JumpIfTrueOrPop { .. } | JumpIfFalseOrPop { .. } => {
1704+
if jump {
1705+
0
1706+
} else {
1707+
-1
1708+
}
1709+
}
16911710
MakeFunction => {
16921711
// CPython 3.13 style: MakeFunction only pops code object
16931712
-1 + 1 // pop code, push function
@@ -1895,7 +1914,9 @@ impl Instruction {
18951914
ImportFrom { idx } => w!(IMPORT_FROM, name = idx),
18961915
ImportName { idx } => w!(IMPORT_NAME, name = idx),
18971916
IsOp(inv) => w!(IS_OP, ?inv),
1917+
JumpIfFalseOrPop { target } => w!(JUMP_IF_FALSE_OR_POP, target),
18981918
JumpIfNotExcMatch(target) => w!(JUMP_IF_NOT_EXC_MATCH, target),
1919+
JumpIfTrueOrPop { target } => w!(JUMP_IF_TRUE_OR_POP, target),
18991920
Jump { target } => w!(JUMP, target),
19001921
ListAppend { i } => w!(LIST_APPEND, i),
19011922
LoadAttr { idx } => w!(LOAD_ATTR, name = idx),

crates/vm/src/frame.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,9 @@ impl ExecutingFrame<'_> {
981981
self.push_value(vm.ctx.new_bool(value).into());
982982
Ok(None)
983983
}
984+
bytecode::Instruction::JumpIfFalseOrPop { target } => {
985+
self.jump_if_or_pop(vm, target.get(arg), false)
986+
}
984987
bytecode::Instruction::JumpIfNotExcMatch(target) => {
985988
let b = self.pop_value();
986989
let a = self.pop_value();
@@ -1004,6 +1007,9 @@ impl ExecutingFrame<'_> {
10041007
self.push_value(vm.ctx.new_bool(value).into());
10051008
self.pop_jump_if(vm, target.get(arg), false)
10061009
}
1010+
bytecode::Instruction::JumpIfTrueOrPop { target } => {
1011+
self.jump_if_or_pop(vm, target.get(arg), true)
1012+
}
10071013
bytecode::Instruction::Jump { target } => {
10081014
self.jump(target.get(arg));
10091015
Ok(None)
@@ -2041,6 +2047,23 @@ impl ExecutingFrame<'_> {
20412047
Ok(None)
20422048
}
20432049

2050+
#[inline]
2051+
fn jump_if_or_pop(
2052+
&mut self,
2053+
vm: &VirtualMachine,
2054+
target: bytecode::Label,
2055+
flag: bool,
2056+
) -> FrameResult {
2057+
let obj = self.top_value();
2058+
let value = obj.to_owned().try_to_bool(vm)?;
2059+
if value == flag {
2060+
self.jump(target);
2061+
} else {
2062+
self.pop_value();
2063+
}
2064+
Ok(None)
2065+
}
2066+
20442067
/// The top of stack contains the iterator, lets push it forward
20452068
fn execute_for_iter(&mut self, vm: &VirtualMachine, target: bytecode::Label) -> FrameResult {
20462069
let top_of_stack = PyIter::new(self.top_value());

0 commit comments

Comments
 (0)