Skip to content
Next Next commit
Remove JumpIfTrueOrPop & JumpIfFalseOrPop opcdes
  • Loading branch information
ShaharNaveh committed Dec 25, 2025
commit 1445698a3ca66e84568cd78db4f365ba30855e72
16 changes: 12 additions & 4 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4030,7 +4030,9 @@ impl Compiler {
if let Some(ref guard) = m.guard {
// Compile guard and jump to end if false
self.compile_expression(guard)?;
emit!(self, Instruction::JumpIfFalseOrPop { target: end });
emit!(self, Instruction::CopyItem { index: 1_u32 });
emit!(self, Instruction::PopJumpIfFalse { target: end });
emit!(self, Instruction::Pop);
}
self.compile_statements(&m.body)?;
}
Expand Down Expand Up @@ -4101,12 +4103,14 @@ impl Compiler {

// if comparison result is false, we break with this value; if true, try the next one.
if let Some((break_block, _)) = end_blocks {
emit!(self, Instruction::CopyItem { index: 1_u32 });
emit!(
self,
Instruction::JumpIfFalseOrPop {
Instruction::PopJumpIfFalse {
target: break_block,
}
);
emit!(self, Instruction::Pop);
}
}

Expand Down Expand Up @@ -4457,27 +4461,31 @@ impl Compiler {
let after_block = self.new_block();

let (last_value, values) = values.split_last().unwrap();

for value in values {
self.compile_expression(value)?;

emit!(self, Instruction::CopyItem { index: 1_u32 });
match op {
BoolOp::And => {
emit!(
self,
Instruction::JumpIfFalseOrPop {
Instruction::PopJumpIfFalse {
target: after_block,
}
);
}
BoolOp::Or => {
emit!(
self,
Instruction::JumpIfTrueOrPop {
Instruction::PopJumpIfTrue {
target: after_block,
}
);
}
}

emit!(self, Instruction::Pop);
}

// If all values did not qualify, take the value of the last value:
Expand Down
21 changes: 0 additions & 21 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,20 +736,10 @@ pub enum Instruction {
},
/// Performs `is` comparison, or `is not` if `invert` is 1.
IsOp(Arg<Invert>),
/// Peek at the top of the stack, and jump if this value is false.
/// Otherwise, pop top of stack.
JumpIfFalseOrPop {
target: Arg<Label>,
},
/// Performs exception matching for except.
/// Tests whether the STACK[-2] is an exception matching STACK[-1].
/// Pops STACK[-1] and pushes the boolean result of the test.
JumpIfNotExcMatch(Arg<Label>),
/// Peek at the top of the stack, and jump if this value is true.
/// Otherwise, pop top of stack.
JumpIfTrueOrPop {
target: Arg<Label>,
},
Jump {
target: Arg<Label>,
},
Expand Down Expand Up @@ -1617,8 +1607,6 @@ impl Instruction {
| JumpIfNotExcMatch(l)
| PopJumpIfTrue { target: l }
| PopJumpIfFalse { target: l }
| JumpIfTrueOrPop { target: l }
| JumpIfFalseOrPop { target: l }
| ForIter { target: l }
| SetupFinally { handler: l }
| SetupExcept { handler: l }
Expand Down Expand Up @@ -1696,13 +1684,6 @@ impl Instruction {
Break { .. } => 0,
Jump { .. } => 0,
PopJumpIfTrue { .. } | PopJumpIfFalse { .. } => -1,
JumpIfTrueOrPop { .. } | JumpIfFalseOrPop { .. } => {
if jump {
0
} else {
-1
}
}
MakeFunction => {
// CPython 3.13 style: MakeFunction only pops code object
-1 + 1 // pop code, push function
Expand Down Expand Up @@ -1910,9 +1891,7 @@ impl Instruction {
ImportFrom { idx } => w!(ImportFrom, name = idx),
ImportName { idx } => w!(ImportName, name = idx),
IsOp(inv) => w!(IS_OP, ?inv),
JumpIfFalseOrPop { target } => w!(JumpIfFalseOrPop, target),
JumpIfNotExcMatch(target) => w!(JUMP_IF_NOT_EXC_MATCH, target),
JumpIfTrueOrPop { target } => w!(JumpIfTrueOrPop, target),
Jump { target } => w!(Jump, target),
ListAppend { i } => w!(ListAppend, i),
LoadAttr { idx } => w!(LoadAttr, name = idx),
Expand Down
23 changes: 0 additions & 23 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,9 +981,6 @@ impl ExecutingFrame<'_> {
self.push_value(vm.ctx.new_bool(value).into());
Ok(None)
}
bytecode::Instruction::JumpIfFalseOrPop { target } => {
self.jump_if_or_pop(vm, target.get(arg), false)
}
bytecode::Instruction::JumpIfNotExcMatch(target) => {
let b = self.pop_value();
let a = self.pop_value();
Expand All @@ -1007,9 +1004,6 @@ impl ExecutingFrame<'_> {
self.push_value(vm.ctx.new_bool(value).into());
self.pop_jump_if(vm, target.get(arg), false)
}
bytecode::Instruction::JumpIfTrueOrPop { target } => {
self.jump_if_or_pop(vm, target.get(arg), true)
}
bytecode::Instruction::Jump { target } => {
self.jump(target.get(arg));
Ok(None)
Expand Down Expand Up @@ -2047,23 +2041,6 @@ impl ExecutingFrame<'_> {
Ok(None)
}

#[inline]
fn jump_if_or_pop(
&mut self,
vm: &VirtualMachine,
target: bytecode::Label,
flag: bool,
) -> FrameResult {
let obj = self.top_value();
let value = obj.to_owned().try_to_bool(vm)?;
if value == flag {
self.jump(target);
} else {
self.pop_value();
}
Ok(None)
}

/// The top of stack contains the iterator, lets push it forward
fn execute_for_iter(&mut self, vm: &VirtualMachine, target: bytecode::Label) -> FrameResult {
let top_of_stack = PyIter::new(self.top_value());
Expand Down
Loading