New Instruction ToBool,PopJumpIfFalse (#6112)

* New Instruction ToBool

* Rename JustIf{True,False} => PopJumpIf{...}
This commit is contained in:
Jeong, YunWon authored and GitHub committed 2025-08-26 16:12:14 +09:00
1 parent 16cdcfb96f
commit 776cabb883
12 files changed
+55 -42

No files matched your search

+4 -4
View File
@@ -2027,7 +2027,7 @@ impl Compiler {
// We cannot handle this exception type:
emit!(
self,
Instruction::JumpIfFalse {
Instruction::PopJumpIfFalse {
target: next_handler,
}
);
@@ -3117,7 +3117,7 @@ impl Compiler {
JumpOp::PopJumpIfFalse => {
emit!(
self,
Instruction::JumpIfFalse {
Instruction::PopJumpIfFalse {
target: pc.fail_pop[pops]
}
);
@@ -4398,14 +4398,14 @@ impl Compiler {
if condition {
emit!(
self,
Instruction::JumpIfTrue {
Instruction::PopJumpIfTrue {
target: target_block,
}
);
} else {
emit!(
self,
Instruction::JumpIfFalse {
Instruction::PopJumpIfFalse {
target: target_block,
}
);
@@ -3,10 +3,10 @@ source: compiler/codegen/src/compile.rs
expression: "compile_exec(\"\\\nif True and False and False:\n pass\n\")"
---
1 0 LoadConst (True)
1 JumpIfFalse (6)
1 PopJumpIfFalse (6)
2 LoadConst (False)
3 JumpIfFalse (6)
3 PopJumpIfFalse (6)
4 LoadConst (False)
5 JumpIfFalse (6)
5 PopJumpIfFalse (6)
2 >> 6 ReturnConst (None)
@@ -3,12 +3,12 @@ source: compiler/codegen/src/compile.rs
expression: "compile_exec(\"\\\nif (True and False) or (False and True):\n pass\n\")"
---
1 0 LoadConst (True)
1 JumpIfFalse (4)
1 PopJumpIfFalse (4)
2 LoadConst (False)
3 JumpIfTrue (8)
3 PopJumpIfTrue (8)
>> 4 LoadConst (False)
5 JumpIfFalse (8)
5 PopJumpIfFalse (8)
6 LoadConst (True)
7 JumpIfFalse (8)
7 PopJumpIfFalse (8)
2 >> 8 ReturnConst (None)
@@ -3,10 +3,10 @@ source: compiler/codegen/src/compile.rs
expression: "compile_exec(\"\\\nif True or False or False:\n pass\n\")"
---
1 0 LoadConst (True)
1 JumpIfTrue (6)
1 PopJumpIfTrue (6)
2 LoadConst (False)
3 JumpIfTrue (6)
3 PopJumpIfTrue (6)
4 LoadConst (False)
5 JumpIfFalse (6)
5 PopJumpIfFalse (6)
2 >> 6 ReturnConst (None)
@@ -53,7 +53,7 @@ expression: "compile_exec(\"\\\nfor stop_exc in (StopIteration('spam'), StopAsyn
6 43 LoadNameAny (7, Exception)
44 TestOperation (ExceptionMatch)
45 JumpIfFalse (58)
45 PopJumpIfFalse (58)
46 StoreLocal (8, ex)
7 47 LoadNameAny (3, self)
@@ -3,11 +3,11 @@ source: compiler/src/compile.rs
expression: "compile_exec(\"\\\nif True and False and False:\n pass\n\")"
---
1 0 LoadConst (True)
1 JumpIfFalse (6)
1 PopJumpIfFalse (6)
2 LoadConst (False)
3 JumpIfFalse (6)
3 PopJumpIfFalse (6)
4 LoadConst (False)
5 JumpIfFalse (6)
5 PopJumpIfFalse (6)
2 >> 6 LoadConst (None)
7 ReturnValue
@@ -3,13 +3,13 @@ source: compiler/src/compile.rs
expression: "compile_exec(\"\\\nif (True and False) or (False and True):\n pass\n\")"
---
1 0 LoadConst (True)
1 JumpIfFalse (4)
1 PopJumpIfFalse (4)
2 LoadConst (False)
3 JumpIfTrue (8)
3 PopJumpIfTrue (8)
>> 4 LoadConst (False)
5 JumpIfFalse (8)
5 PopJumpIfFalse (8)
6 LoadConst (True)
7 JumpIfFalse (8)
7 PopJumpIfFalse (8)
2 >> 8 LoadConst (None)
9 ReturnValue
@@ -3,11 +3,11 @@ source: compiler/src/compile.rs
expression: "compile_exec(\"\\\nif True or False or False:\n pass\n\")"
---
1 0 LoadConst (True)
1 JumpIfTrue (6)
1 PopJumpIfTrue (6)
2 LoadConst (False)
3 JumpIfTrue (6)
3 PopJumpIfTrue (6)
4 LoadConst (False)
5 JumpIfFalse (6)
5 PopJumpIfFalse (6)
2 >> 6 LoadConst (None)
7 ReturnValue
@@ -51,7 +51,7 @@ expression: "compile_exec(\"\\\nfor stop_exc in (StopIteration('spam'), StopAsyn
6 41 LoadNameAny (7, Exception)
42 TestOperation (ExceptionMatch)
43 JumpIfFalse (53)
43 PopJumpIfFalse (53)
44 StoreLocal (8, ex)
7 45 LoadNameAny (3, self)
+10 -10
View File
@@ -525,7 +525,7 @@ pub enum Instruction {
Swap {
index: Arg<u32>,
},
// ToBool,
ToBool,
Rotate2,
Rotate3,
Duplicate,
@@ -548,11 +548,11 @@ pub enum Instruction {
target: Arg<Label>,
},
/// Pop the top of the stack, and jump if this value is true.
JumpIfTrue {
PopJumpIfTrue {
target: Arg<Label>,
},
/// Pop the top of the stack, and jump if this value is false.
JumpIfFalse {
PopJumpIfFalse {
target: Arg<Label>,
},
/// Peek at the top of the stack, and jump if this value is true.
@@ -1255,8 +1255,8 @@ impl Instruction {
pub const fn label_arg(&self) -> Option<Arg<Label>> {
match self {
Jump { target: l }
| JumpIfTrue { target: l }
| JumpIfFalse { target: l }
| PopJumpIfTrue { target: l }
| PopJumpIfFalse { target: l }
| JumpIfTrueOrPop { target: l }
| JumpIfFalseOrPop { target: l }
| ForIter { target: l }
@@ -1330,7 +1330,7 @@ impl Instruction {
CopyItem { .. } => 1,
Pop => -1,
Swap { .. } => 0,
// ToBool => 0,
ToBool => 0,
Rotate2 | Rotate3 => 0,
Duplicate => 1,
Duplicate2 => 2,
@@ -1341,7 +1341,7 @@ impl Instruction {
Continue { .. } => 0,
Break { .. } => 0,
Jump { .. } => 0,
JumpIfTrue { .. } | JumpIfFalse { .. } => -1,
PopJumpIfTrue { .. } | PopJumpIfFalse { .. } => -1,
JumpIfTrueOrPop { .. } | JumpIfFalseOrPop { .. } => {
if jump {
0
@@ -1533,7 +1533,7 @@ impl Instruction {
CopyItem { index } => w!(CopyItem, index),
Pop => w!(Pop),
Swap { index } => w!(Swap, index),
// ToBool => w!(ToBool),
ToBool => w!(ToBool),
Rotate2 => w!(Rotate2),
Rotate3 => w!(Rotate3),
Duplicate => w!(Duplicate),
@@ -1546,8 +1546,8 @@ impl Instruction {
Continue { target } => w!(Continue, target),
Break { target } => w!(Break, target),
Jump { target } => w!(Jump, target),
JumpIfTrue { target } => w!(JumpIfTrue, target),
JumpIfFalse { target } => w!(JumpIfFalse, target),
PopJumpIfTrue { target } => w!(PopJumpIfTrue, target),
PopJumpIfFalse { target } => w!(PopJumpIfFalse, target),
JumpIfTrueOrPop { target } => w!(JumpIfTrueOrPop, target),
JumpIfFalseOrPop { target } => w!(JumpIfFalseOrPop, target),
MakeFunction => w!(MakeFunction),
+2 -2
View File
@@ -275,7 +275,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
) -> Result<(), JitCompileError> {
match instruction {
Instruction::ExtendedArg => Ok(()),
Instruction::JumpIfFalse { target } => {
Instruction::PopJumpIfFalse { target } => {
let cond = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
let val = self.boolean_val(cond)?;
let then_block = self.get_or_create_block(target.get(arg));
@@ -288,7 +288,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
Ok(())
}
Instruction::JumpIfTrue { target } => {
Instruction::PopJumpIfTrue { target } => {
let cond = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
let val = self.boolean_val(cond)?;
let then_block = self.get_or_create_block(target.get(arg));
+17 -4
View File
@@ -1083,6 +1083,12 @@ impl ExecutingFrame<'_> {
self.push_value(vm.ctx.new_int(len).into());
Ok(None)
}
bytecode::Instruction::ToBool => {
let obj = self.pop_value();
let bool_val = obj.try_to_bool(vm)?;
self.push_value(vm.ctx.new_bool(bool_val).into());
Ok(None)
}
bytecode::Instruction::CallIntrinsic1 { func } => {
let value = self.pop_value();
let result = self.call_intrinsic_1(func.get(arg), value, vm)?;
@@ -1227,9 +1233,11 @@ impl ExecutingFrame<'_> {
self.jump(target.get(arg));
Ok(None)
}
bytecode::Instruction::JumpIfTrue { target } => self.jump_if(vm, target.get(arg), true),
bytecode::Instruction::JumpIfFalse { target } => {
self.jump_if(vm, target.get(arg), false)
bytecode::Instruction::PopJumpIfTrue { target } => {
self.pop_jump_if(vm, target.get(arg), true)
}
bytecode::Instruction::PopJumpIfFalse { target } => {
self.pop_jump_if(vm, target.get(arg), false)
}
bytecode::Instruction::JumpIfTrueOrPop { target } => {
self.jump_if_or_pop(vm, target.get(arg), true)
@@ -1839,7 +1847,12 @@ impl ExecutingFrame<'_> {
}
#[inline]
fn jump_if(&mut self, vm: &VirtualMachine, target: bytecode::Label, flag: bool) -> FrameResult {
fn pop_jump_if(
&mut self,
vm: &VirtualMachine,
target: bytecode::Label,
flag: bool,
) -> FrameResult {
let obj = self.pop_value();
let value = obj.try_to_bool(vm)?;
if value == flag {