From 074f72b46882a8f203c14dc592d1fcd29db7c0a1 Mon Sep 17 00:00:00 2001 From: yt2b Date: Sun, 4 Dec 2022 13:22:24 +0900 Subject: [PATCH 1/2] Add jump_if function --- vm/src/frame.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/vm/src/frame.rs b/vm/src/frame.rs index bfe3dd6290..f201a8d921 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -983,23 +983,8 @@ impl ExecutingFrame<'_> { self.jump(*target); Ok(None) } - bytecode::Instruction::JumpIfTrue { target } => { - let obj = self.pop_value(); - let value = obj.try_to_bool(vm)?; - if value { - self.jump(*target); - } - Ok(None) - } - - bytecode::Instruction::JumpIfFalse { target } => { - let obj = self.pop_value(); - let value = obj.try_to_bool(vm)?; - if !value { - self.jump(*target); - } - Ok(None) - } + bytecode::Instruction::JumpIfTrue { target } => self.jump_if(vm, *target, true), + bytecode::Instruction::JumpIfFalse { target } => self.jump_if(vm, *target, false), bytecode::Instruction::JumpIfTrueOrPop { target } => { let obj = self.last_value(); @@ -1487,6 +1472,16 @@ impl ExecutingFrame<'_> { self.update_lasti(|i| *i = target_pc); } + #[inline] + fn 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 { + self.jump(target); + } + 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.last_value()); From 49c294dce84a888c3108d9cb3495779979b7d208 Mon Sep 17 00:00:00 2001 From: yt2b Date: Sun, 4 Dec 2022 13:43:09 +0900 Subject: [PATCH 2/2] Add jump_if_or_pop function --- vm/src/frame.rs | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/vm/src/frame.rs b/vm/src/frame.rs index f201a8d921..f74645aa92 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -985,27 +985,11 @@ impl ExecutingFrame<'_> { } bytecode::Instruction::JumpIfTrue { target } => self.jump_if(vm, *target, true), bytecode::Instruction::JumpIfFalse { target } => self.jump_if(vm, *target, false), - bytecode::Instruction::JumpIfTrueOrPop { target } => { - let obj = self.last_value(); - let value = obj.try_to_bool(vm)?; - if value { - self.jump(*target); - } else { - self.pop_value(); - } - Ok(None) + self.jump_if_or_pop(vm, *target, true) } - bytecode::Instruction::JumpIfFalseOrPop { target } => { - let obj = self.last_value(); - let value = obj.try_to_bool(vm)?; - if !value { - self.jump(*target); - } else { - self.pop_value(); - } - Ok(None) + self.jump_if_or_pop(vm, *target, false) } bytecode::Instruction::Raise { kind } => self.execute_raise(vm, *kind), @@ -1482,6 +1466,23 @@ impl ExecutingFrame<'_> { Ok(None) } + #[inline] + fn jump_if_or_pop( + &mut self, + vm: &VirtualMachine, + target: bytecode::Label, + flag: bool, + ) -> FrameResult { + let obj = self.last_value(); + let value = obj.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.last_value());