Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add AnyOpcode enum
  • Loading branch information
ShaharNaveh committed Apr 15, 2026
commit f6fb58da5484c4c3661e265fc88083edc11e6208
4 changes: 2 additions & 2 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use rustpython_wtf8::{Wtf8, Wtf8Buf};

pub use crate::bytecode::{
instruction::{
AnyInstruction, Arg, Instruction, InstructionMetadata, Opcode, PseudoInstruction,
PseudoOpcode, StackEffect,
AnyInstruction, AnyOpcode, Arg, Instruction, InstructionMetadata, Opcode,
PseudoInstruction, PseudoOpcode, StackEffect,
},
oparg::{
BinaryOperator, BuildSliceArgCount, CommonConstant, ComparisonOperator, ConvertValueOparg,
Expand Down
48 changes: 46 additions & 2 deletions crates/compiler-core/src/bytecode/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ impl AnyInstruction {
/// If was called on something else other than [`Self::Real`].
pub const fn expect_real(self) -> Instruction {
self.real()
.expect("Expected Instruction::Real, found Instruction::Pseudo")
.expect("Expected AnyInstruction::Real, found AnyInstruction::Pseudo")
}

/// Same as [`Self::pseudo`] but panics if wasn't called on [`Self::Pseudo`].
Expand All @@ -1670,7 +1670,7 @@ impl AnyInstruction {
/// If was called on something else other than [`Self::Pseudo`].
pub const fn expect_pseudo(self) -> PseudoInstruction {
self.pseudo()
.expect("Expected Instruction::Pseudo, found Instruction::Real")
.expect("Expected AnyInstruction::Pseudo, found AnyInstruction::Real")
}

/// Returns true if this is a block push pseudo instruction
Expand All @@ -1685,6 +1685,50 @@ impl AnyInstruction {
}
}

#[derive(Clone, Copy, Debug)]
pub enum AnyOpcode {
Real(Opcode),
Pseudo(PseudoOpcode),
}

impl AnyOpcode {
/// Gets the inner value of [`Self::Real`].
pub const fn real(self) -> Option<Opcode> {
match self {
Self::Real(op) => Some(op),
_ => None,
}
}

/// Gets the inner value of [`Self::Pseudo`].
pub const fn pseudo(self) -> Option<PseudoOpcode> {
match self {
Self::Pseudo(op) => Some(op),
_ => None,
}
}

/// Same as [`Self::real`] but panics if wasn't called on [`Self::Real`].
///
/// # Panics
///
/// If was called on something else other than [`Self::Real`].
pub const fn expect_real(self) -> Opcode {
self.real()
.expect("Expected AnyOpcode::Real, found AnyOpcode::Pseudo")
}

/// Same as [`Self::pseudo`] but panics if wasn't called on [`Self::Pseudo`].
///
/// # Panics
///
/// If was called on something else other than [`Self::Pseudo`].
pub const fn expect_pseudo(self) -> PseudoOpcode {
self.pseudo()
.expect("Expected AnyOpcode::Pseudo, found AnyOpcode::Real")
}
}

/// What effect the instruction has on the stack.
#[derive(Clone, Copy)]
pub struct StackEffect {
Expand Down