From c0367d26e054519265e72cb35bf1507405adebc0 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 26 Aug 2025 15:38:25 +0900 Subject: [PATCH 1/2] New Instruction ToBool --- compiler/core/src/bytecode.rs | 6 +++--- vm/src/frame.rs | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/compiler/core/src/bytecode.rs b/compiler/core/src/bytecode.rs index 6400dec34b4..36077c3a328 100644 --- a/compiler/core/src/bytecode.rs +++ b/compiler/core/src/bytecode.rs @@ -525,7 +525,7 @@ pub enum Instruction { Swap { index: Arg, }, - // ToBool, + ToBool, Rotate2, Rotate3, Duplicate, @@ -1330,7 +1330,7 @@ impl Instruction { CopyItem { .. } => 1, Pop => -1, Swap { .. } => 0, - // ToBool => 0, + ToBool => 0, Rotate2 | Rotate3 => 0, Duplicate => 1, Duplicate2 => 2, @@ -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), diff --git a/vm/src/frame.rs b/vm/src/frame.rs index cfd0a16a8ac..b4b8a46198a 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -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)?; From 75a138afe4746b933f889981dad8282e8e5b6f19 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 26 Aug 2025 15:44:22 +0900 Subject: [PATCH 2/2] Rename JustIf{True,False} => PopJumpIf{...} --- compiler/codegen/src/compile.rs | 8 ++++---- ...stpython_codegen__compile__tests__if_ands.snap | 6 +++--- ...tpython_codegen__compile__tests__if_mixed.snap | 8 ++++---- ...ustpython_codegen__compile__tests__if_ors.snap | 6 +++--- ..._compile__tests__nested_double_async_with.snap | 2 +- ...on_compiler_core__compile__tests__if_ands.snap | 6 +++--- ...n_compiler_core__compile__tests__if_mixed.snap | 8 ++++---- ...hon_compiler_core__compile__tests__if_ors.snap | 6 +++--- ..._compile__tests__nested_double_async_with.snap | 2 +- compiler/core/src/bytecode.rs | 14 +++++++------- jit/src/instructions.rs | 4 ++-- vm/src/frame.rs | 15 +++++++++++---- 12 files changed, 46 insertions(+), 39 deletions(-) diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index b2a0796fa24..67a93b4650a 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -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, } ); diff --git a/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap b/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap index cf6223be1fb..8b2907ef6ff 100644 --- a/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap +++ b/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap @@ -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) diff --git a/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap b/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap index 332d0fa985c..fc91a74283b 100644 --- a/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap +++ b/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap @@ -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) diff --git a/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap b/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap index d4a4d8f2c5a..9be7c2af7bd 100644 --- a/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap +++ b/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap @@ -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) diff --git a/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap b/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap index 9165a6cfbf1..82c02d5ea34 100644 --- a/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap +++ b/compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap @@ -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) diff --git a/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ands.snap b/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ands.snap index d80f10dfee5..bc88cf2349c 100644 --- a/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ands.snap +++ b/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ands.snap @@ -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 diff --git a/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_mixed.snap b/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_mixed.snap index 0a9175bb12e..b19cbb119d9 100644 --- a/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_mixed.snap +++ b/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_mixed.snap @@ -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 diff --git a/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ors.snap b/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ors.snap index 4b812639b2c..3d1f5a1d6f0 100644 --- a/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ors.snap +++ b/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ors.snap @@ -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 diff --git a/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__nested_double_async_with.snap b/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__nested_double_async_with.snap index 79a1a86ad1b..589f3210cfa 100644 --- a/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__nested_double_async_with.snap +++ b/compiler/codegen/src/snapshots/rustpython_compiler_core__compile__tests__nested_double_async_with.snap @@ -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) diff --git a/compiler/core/src/bytecode.rs b/compiler/core/src/bytecode.rs index 36077c3a328..b38c5995085 100644 --- a/compiler/core/src/bytecode.rs +++ b/compiler/core/src/bytecode.rs @@ -548,11 +548,11 @@ pub enum Instruction { target: Arg