Skip to content

Commit 847482e

Browse files
committed
New Instruction ToBool,PopJumpIfFalse
1 parent 711b1a6 commit 847482e

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

compiler/core/src/bytecode.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ pub enum Instruction {
525525
Swap {
526526
index: Arg<u32>,
527527
},
528-
// ToBool,
528+
ToBool,
529529
Rotate2,
530530
Rotate3,
531531
Duplicate,
@@ -555,6 +555,10 @@ pub enum Instruction {
555555
JumpIfFalse {
556556
target: Arg<Label>,
557557
},
558+
/// Pop the top of the stack, then pop the next value and jump if it is false.
559+
PopJumpIfFalse {
560+
target: Arg<Label>,
561+
},
558562
/// Peek at the top of the stack, and jump if this value is true.
559563
/// Otherwise, pop top of stack.
560564
JumpIfTrueOrPop {
@@ -1257,6 +1261,7 @@ impl Instruction {
12571261
Jump { target: l }
12581262
| JumpIfTrue { target: l }
12591263
| JumpIfFalse { target: l }
1264+
| PopJumpIfFalse { target: l }
12601265
| JumpIfTrueOrPop { target: l }
12611266
| JumpIfFalseOrPop { target: l }
12621267
| ForIter { target: l }
@@ -1330,7 +1335,7 @@ impl Instruction {
13301335
CopyItem { .. } => 1,
13311336
Pop => -1,
13321337
Swap { .. } => 0,
1333-
// ToBool => 0,
1338+
ToBool => 0,
13341339
Rotate2 | Rotate3 => 0,
13351340
Duplicate => 1,
13361341
Duplicate2 => 2,
@@ -1341,7 +1346,7 @@ impl Instruction {
13411346
Continue { .. } => 0,
13421347
Break { .. } => 0,
13431348
Jump { .. } => 0,
1344-
JumpIfTrue { .. } | JumpIfFalse { .. } => -1,
1349+
JumpIfTrue { .. } | JumpIfFalse { .. } | PopJumpIfFalse { .. } => -1,
13451350
JumpIfTrueOrPop { .. } | JumpIfFalseOrPop { .. } => {
13461351
if jump {
13471352
0
@@ -1533,7 +1538,7 @@ impl Instruction {
15331538
CopyItem { index } => w!(CopyItem, index),
15341539
Pop => w!(Pop),
15351540
Swap { index } => w!(Swap, index),
1536-
// ToBool => w!(ToBool),
1541+
ToBool => w!(ToBool),
15371542
Rotate2 => w!(Rotate2),
15381543
Rotate3 => w!(Rotate3),
15391544
Duplicate => w!(Duplicate),
@@ -1548,6 +1553,7 @@ impl Instruction {
15481553
Jump { target } => w!(Jump, target),
15491554
JumpIfTrue { target } => w!(JumpIfTrue, target),
15501555
JumpIfFalse { target } => w!(JumpIfFalse, target),
1556+
PopJumpIfFalse { target } => w!(PopJumpIfFalse, target),
15511557
JumpIfTrueOrPop { target } => w!(JumpIfTrueOrPop, target),
15521558
JumpIfFalseOrPop { target } => w!(JumpIfFalseOrPop, target),
15531559
MakeFunction => w!(MakeFunction),

vm/src/frame.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,12 @@ impl ExecutingFrame<'_> {
10831083
self.push_value(vm.ctx.new_int(len).into());
10841084
Ok(None)
10851085
}
1086+
bytecode::Instruction::ToBool => {
1087+
let obj = self.pop_value();
1088+
let bool_val = obj.try_to_bool(vm)?;
1089+
self.push_value(vm.ctx.new_bool(bool_val).into());
1090+
Ok(None)
1091+
}
10861092
bytecode::Instruction::CallIntrinsic1 { func } => {
10871093
let value = self.pop_value();
10881094
let result = self.call_intrinsic_1(func.get(arg), value, vm)?;
@@ -1231,6 +1237,9 @@ impl ExecutingFrame<'_> {
12311237
bytecode::Instruction::JumpIfFalse { target } => {
12321238
self.jump_if(vm, target.get(arg), false)
12331239
}
1240+
bytecode::Instruction::PopJumpIfFalse { target } => {
1241+
self.pop_jump_if(vm, target.get(arg), false)
1242+
}
12341243
bytecode::Instruction::JumpIfTrueOrPop { target } => {
12351244
self.jump_if_or_pop(vm, target.get(arg), true)
12361245
}
@@ -1865,6 +1874,24 @@ impl ExecutingFrame<'_> {
18651874
Ok(None)
18661875
}
18671876

1877+
#[inline]
1878+
fn pop_jump_if(
1879+
&mut self,
1880+
vm: &VirtualMachine,
1881+
target: bytecode::Label,
1882+
flag: bool,
1883+
) -> FrameResult {
1884+
// PopJumpIf{True,False}
1885+
// Pop TOS, convert to bool, jump if matches flag
1886+
let obj = self.pop_value();
1887+
let value = obj.try_to_bool(vm)?;
1888+
if value == flag {
1889+
self.jump(target);
1890+
}
1891+
// If not jumping, the value is already popped
1892+
Ok(None)
1893+
}
1894+
18681895
/// The top of stack contains the iterator, lets push it forward
18691896
fn execute_for_iter(&mut self, vm: &VirtualMachine, target: bytecode::Label) -> FrameResult {
18701897
let top_of_stack = PyIter::new(self.top_value());

0 commit comments

Comments
 (0)