From ba8da19f82137e9385dda1e7a5604856ff786de0 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 9 Apr 2026 18:18:10 +0200 Subject: [PATCH 1/4] Macro for defining opcode & instruction enums --- .../compiler-core/src/bytecode/instruction.rs | 171 +++++++++++++----- 1 file changed, 124 insertions(+), 47 deletions(-) diff --git a/crates/compiler-core/src/bytecode/instruction.rs b/crates/compiler-core/src/bytecode/instruction.rs index 76dc6a7938..6616911733 100644 --- a/crates/compiler-core/src/bytecode/instruction.rs +++ b/crates/compiler-core/src/bytecode/instruction.rs @@ -13,6 +13,107 @@ use crate::{ marshal::MarshalError, }; +macro_rules! define_opcodes { + ( + opcode_enum: $opcode_name:ident, + instruction_enum: $instr_name:ident, + typ: $typ:ty, + ops: [ + $( + { + name: $op_name:ident, + id: $op_id:expr, + display: $op_display:literal + $(, oparg: { name: $arg_name:ident, typ: $arg_type:ty })? + } + ),* $(,)? + ] + ) => { + #[derive(Clone, Copy, Debug)] + pub enum $opcode_name { + $($op_name),* + } + + impl $opcode_name { + #[must_use] + pub const fn as_instruction(&self) -> $instr_name { + match self { + $( + Self::$op_name => $instr_name::$op_name $({ $arg_name: Arg::marker() })?, + )* + } + } + } + + impl From<$opcode_name> for $instr_name { + fn from(opcode: $opcode_name) -> Self { + opcode.as_instruction() + } + } + + + impl TryFrom<$typ> for $opcode_name { + type Error = $crate::marshal::MarshalError; + + fn try_from(value: $typ) -> Result { + match value { + $($op_id => Ok(Self::$op_name),)* + _ => Err(Self::Error::InvalidBytecode), + } + } + } + + impl From<$opcode_name> for $typ { + fn from(opcode: $opcode_name) -> Self { + match opcode { + $($opcode_name::$op_name => $op_id,)* + } + } + } + + impl ::core::fmt::Display for $opcode_name { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + match self { + $(Self::$op_name => write!(f, $op_display),)* + } + } + } + + #[derive(Clone, Copy, Debug)] + pub enum $instr_name { + $( + $op_name $({ $arg_name: Arg<$arg_type> })? + ),* + } + + impl $instr_name { + #[must_use] + pub const fn opcode(&self) -> $opcode_name { + match self { + $( + Self::$op_name $({ $arg_name: _ })? => $opcode_name::$op_name, + )* + } + } + + } + + impl From<$instr_name> for $opcode_name { + fn from(instruction: $instr_name) -> Self { + instruction.opcode() + } + } + + impl TryFrom<$typ> for $instr_name { + type Error = $crate::marshal::MarshalError; + + fn try_from(value: $typ) -> Result { + $opcode_name::try_from(value).map(Into::into) + } + } + }; +} + /// A Single bytecode instruction that are executed by the VM. /// /// Currently aligned with CPython 3.14. @@ -1389,53 +1490,29 @@ impl InstructionMetadata for Instruction { } } -/// Instructions used by the compiler. They are not executed by the VM. -/// -/// CPython 3.14.2 aligned (256-266). -#[derive(Clone, Copy, Debug)] -#[repr(u16)] -pub enum PseudoInstruction { - // CPython 3.14.2 pseudo instructions (256-266) - AnnotationsPlaceholder = 256, - Jump { delta: Arg