From 2ee48bc5aa0b6b3d35f1abbd24ed11ba69402bce Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Fri, 23 Jan 2026 14:25:03 +0200 Subject: [PATCH] Seperate between scope exit & unconditional jump opcodes --- crates/codegen/src/ir.rs | 4 +-- .../compiler-core/src/bytecode/instruction.rs | 35 ++++++++++--------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/crates/codegen/src/ir.rs b/crates/codegen/src/ir.rs index 93cc784bd71..30077e49d30 100644 --- a/crates/codegen/src/ir.rs +++ b/crates/codegen/src/ir.rs @@ -439,7 +439,7 @@ impl CodeInfo { for block in &mut self.blocks { let mut last_instr = None; for (i, ins) in block.instructions.iter().enumerate() { - if ins.instr.unconditional_branch() { + if ins.instr.is_scope_exit() || ins.instr.is_unconditional_jump() { last_instr = Some(i); break; } @@ -545,7 +545,7 @@ impl CodeInfo { ); } depth = new_depth; - if instr.unconditional_branch() { + if instr.is_scope_exit() || instr.is_unconditional_jump() { continue 'process_blocks; } } diff --git a/crates/compiler-core/src/bytecode/instruction.rs b/crates/compiler-core/src/bytecode/instruction.rs index 00cd4ecdb91..dc03a0e6785 100644 --- a/crates/compiler-core/src/bytecode/instruction.rs +++ b/crates/compiler-core/src/bytecode/instruction.rs @@ -432,15 +432,19 @@ impl InstructionMetadata for Instruction { } } - fn unconditional_branch(&self) -> bool { + fn is_unconditional_jump(&self) -> bool { matches!( self, Self::JumpForward { .. } | Self::JumpBackward { .. } | Self::JumpBackwardNoInterrupt { .. } - | Self::ReturnValue - | Self::RaiseVarargs { .. } - | Self::Reraise { .. } + ) + } + + fn is_scope_exit(&self) -> bool { + matches!( + self, + Self::ReturnValue | Self::RaiseVarargs { .. } | Self::Reraise { .. } ) } @@ -997,7 +1001,11 @@ impl InstructionMetadata for PseudoInstruction { } } - fn unconditional_branch(&self) -> bool { + fn is_scope_exit(&self) -> bool { + false + } + + fn is_unconditional_jump(&self) -> bool { matches!(self, Self::Jump { .. } | Self::JumpNoInterrupt { .. }) } @@ -1085,7 +1093,9 @@ macro_rules! inst_either { impl InstructionMetadata for AnyInstruction { inst_either!(fn label_arg(&self) -> Option>); - inst_either!(fn unconditional_branch(&self) -> bool); + inst_either!(fn is_unconditional_jump(&self) -> bool); + + inst_either!(fn is_scope_exit(&self) -> bool); inst_either!(fn stack_effect(&self, arg: OpArg) -> i32); @@ -1142,16 +1152,9 @@ pub trait InstructionMetadata { /// Gets the label stored inside this instruction, if it exists. fn label_arg(&self) -> Option>; - /// Whether this is an unconditional branching. - /// - /// # Examples - /// - /// ``` - /// use rustpython_compiler_core::bytecode::{Arg, Instruction, InstructionMetadata}; - /// let jump_inst = Instruction::JumpForward { target: Arg::marker() }; - /// assert!(jump_inst.unconditional_branch()) - /// ``` - fn unconditional_branch(&self) -> bool; + fn is_scope_exit(&self) -> bool; + + fn is_unconditional_jump(&self) -> bool; /// What effect this instruction has on the stack ///