Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
35 changes: 19 additions & 16 deletions crates/compiler-core/src/bytecode/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. }
)
}

Expand Down Expand Up @@ -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 { .. })
}

Expand Down Expand Up @@ -1085,7 +1093,9 @@ macro_rules! inst_either {
impl InstructionMetadata for AnyInstruction {
inst_either!(fn label_arg(&self) -> Option<Arg<Label>>);

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);

Expand Down Expand Up @@ -1142,16 +1152,9 @@ pub trait InstructionMetadata {
/// Gets the label stored inside this instruction, if it exists.
fn label_arg(&self) -> Option<Arg<Label>>;

/// 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
///
Expand Down
Loading