Opcode metadata auto generation by ShaharNaveh · Pull Request #6174 · RustPython/RustPython
Repository navigation
Opcode metadata auto generation - #6174
Closed
ShaharNaveh wants to merge 44 commits into RustPython:main from ShaharNaveh:instruction-autogen
Closed
Opcode metadata auto generation #6174 ShaharNaveh wants to merge 44 commits into RustPython:main from ShaharNaveh:instruction-autogen
Conversation
ShaharNaveh
commented
Sep 23, 2025
·
edited by coderabbitai
Bot
Loading
Copy link Copy Markdown
Contributor
This PR's original goal was to auto generate an enum that will replace:
RustPython/compiler/core/src/bytecode.rs
Lines 539 to 795 in 3a6fda4
| /// A Single bytecode instruction. | |
| # [ derive ( Debug , Copy , Clone , PartialEq , Eq ) ] | |
| # [ repr ( u8 ) ] | |
| pub enum Instruction { | |
| Nop , | |
| /// Importing by name | |
| ImportName { | |
| idx : Arg < NameIdx > , | |
| } , | |
| /// Importing without name | |
| ImportNameless , | |
| /// from ... import ... | |
| ImportFrom { | |
| idx : Arg < NameIdx > , | |
| } , | |
| LoadFast ( Arg < NameIdx > ) , | |
| LoadNameAny ( Arg < NameIdx > ) , | |
| LoadGlobal ( Arg < NameIdx > ) , | |
| LoadDeref ( Arg < NameIdx > ) , | |
| LoadClassDeref ( Arg < NameIdx > ) , | |
| StoreFast ( Arg < NameIdx > ) , | |
| StoreLocal ( Arg < NameIdx > ) , | |
| StoreGlobal ( Arg < NameIdx > ) , | |
| StoreDeref ( Arg < NameIdx > ) , | |
| DeleteFast ( Arg < NameIdx > ) , | |
| DeleteLocal ( Arg < NameIdx > ) , | |
| DeleteGlobal ( Arg < NameIdx > ) , | |
| DeleteDeref ( Arg < NameIdx > ) , | |
| LoadClosure ( Arg < NameIdx > ) , | |
| Subscript , | |
| StoreSubscript , | |
| DeleteSubscript , | |
| StoreAttr { | |
| idx : Arg < NameIdx > , | |
| } , | |
| DeleteAttr { | |
| idx : Arg < NameIdx > , | |
| } , | |
| LoadConst { | |
| /// index into constants vec | |
| idx : Arg < u32 > , | |
| } , | |
| UnaryOperation { | |
| op : Arg < UnaryOperator > , | |
| } , | |
| BinaryOperation { | |
| op : Arg < BinaryOperator > , | |
| } , | |
| BinaryOperationInplace { | |
| op : Arg < BinaryOperator > , | |
| } , | |
| BinarySubscript , | |
| LoadAttr { | |
| idx : Arg < NameIdx > , | |
| } , | |
| TestOperation { | |
| op : Arg < TestOperator > , | |
| } , | |
| CompareOperation { | |
| op : Arg < ComparisonOperator > , | |
| } , | |
| CopyItem { | |
| index : Arg < u32 > , | |
| } , | |
| Pop , | |
| Swap { | |
| index : Arg < u32 > , | |
| } , | |
| ToBool , | |
| Rotate2 , | |
| Rotate3 , | |
| Duplicate , | |
| Duplicate2 , | |
| GetIter , | |
| GetLen , | |
| CallIntrinsic1 { | |
| func : Arg < IntrinsicFunction1 > , | |
| } , | |
| CallIntrinsic2 { | |
| func : Arg < IntrinsicFunction2 > , | |
| } , | |
| Continue { | |
| target : Arg < Label > , | |
| } , | |
| Break { | |
| target : Arg < Label > , | |
| } , | |
| Jump { | |
| target : Arg < Label > , | |
| } , | |
| /// Pop the top of the stack, and jump if this value is true. | |
| PopJumpIfTrue { | |
| target : Arg < Label > , | |
| } , | |
| /// Pop the top of the stack, and jump if this value is false. | |
| PopJumpIfFalse { | |
| target : Arg < Label > , | |
| } , | |
| /// Peek at the top of the stack, and jump if this value is true. | |
| /// Otherwise, pop top of stack. | |
| JumpIfTrueOrPop { | |
| target : Arg < Label > , | |
| } , | |
| /// Peek at the top of the stack, and jump if this value is false. | |
| /// Otherwise, pop top of stack. | |
| JumpIfFalseOrPop { | |
| target : Arg < Label > , | |
| } , | |
| MakeFunction , | |
| SetFunctionAttribute { | |
| attr : Arg < MakeFunctionFlags > , | |
| } , | |
| CallFunctionPositional { | |
| nargs : Arg < u32 > , | |
| } , | |
| CallFunctionKeyword { | |
| nargs : Arg < u32 > , | |
| } , | |
| CallFunctionEx { | |
| has_kwargs : Arg < bool > , | |
| } , | |
| LoadMethod { | |
| idx : Arg < NameIdx > , | |
| } , | |
| CallMethodPositional { | |
| nargs : Arg < u32 > , | |
| } , | |
| CallMethodKeyword { | |
| nargs : Arg < u32 > , | |
| } , | |
| CallMethodEx { | |
| has_kwargs : Arg < bool > , | |
| } , | |
| ForIter { | |
| target : Arg < Label > , | |
| } , | |
| ReturnValue , | |
| ReturnConst { | |
| idx : Arg < u32 > , | |
| } , | |
| YieldValue , | |
| YieldFrom , | |
| /// Resume execution (e.g., at function start, after yield, etc.) | |
| Resume { | |
| arg : Arg < u32 > , | |
| } , | |
| SetupAnnotation , | |
| SetupLoop , | |
| /// Setup a finally handler, which will be called whenever one of this events occurs: | |
| /// - the block is popped | |
| /// - the function returns | |
| /// - an exception is returned | |
| SetupFinally { | |
| handler : Arg < Label > , | |
| } , | |
| /// Enter a finally block, without returning, excepting, just because we are there. | |
| EnterFinally , | |
| /// Marker bytecode for the end of a finally sequence. | |
| /// When this bytecode is executed, the eval loop does one of those things: | |
| /// - Continue at a certain bytecode position | |
| /// - Propagate the exception | |
| /// - Return from a function | |
| /// - Do nothing at all, just continue | |
| EndFinally , | |
| SetupExcept { | |
| handler : Arg < Label > , | |
| } , | |
| SetupWith { | |
| end : Arg < Label > , | |
| } , | |
| WithCleanupStart , | |
| WithCleanupFinish , | |
| PopBlock , | |
| Raise { | |
| kind : Arg < RaiseKind > , | |
| } , | |
| BuildString { | |
| size : Arg < u32 > , | |
| } , | |
| BuildTuple { | |
| size : Arg < u32 > , | |
| } , | |
| BuildTupleFromTuples { | |
| size : Arg < u32 > , | |
| } , | |
| BuildTupleFromIter , | |
| BuildList { | |
| size : Arg < u32 > , | |
| } , | |
| BuildListFromTuples { | |
| size : Arg < u32 > , | |
| } , | |
| BuildSet { | |
| size : Arg < u32 > , | |
| } , | |
| BuildSetFromTuples { | |
| size : Arg < u32 > , | |
| } , | |
| BuildMap { | |
| size : Arg < u32 > , | |
| } , | |
| BuildMapForCall { | |
| size : Arg < u32 > , | |
| } , | |
| DictUpdate { | |
| index : Arg < u32 > , | |
| } , | |
| BuildSlice { | |
| /// whether build a slice with a third step argument | |
| step : Arg < bool > , | |
| } , | |
| ListAppend { | |
| i : Arg < u32 > , | |
| } , | |
| SetAdd { | |
| i : Arg < u32 > , | |
| } , | |
| MapAdd { | |
| i : Arg < u32 > , | |
| } , | |
| PrintExpr , | |
| LoadBuildClass , | |
| UnpackSequence { | |
| size : Arg < u32 > , | |
| } , | |
| UnpackEx { | |
| args : Arg < UnpackExArgs > , | |
| } , | |
| FormatValue { | |
| conversion : Arg < ConversionFlag > , | |
| } , | |
| PopException , | |
| Reverse { | |
| amount : Arg < u32 > , | |
| } , | |
| GetAwaitable , | |
| BeforeAsyncWith , | |
| SetupAsyncWith { | |
| end : Arg < Label > , | |
| } , | |
| GetAIter , | |
| GetANext , | |
| EndAsyncFor , | |
| MatchMapping , | |
| MatchSequence , | |
| MatchKeys , | |
| MatchClass ( Arg < u32 > ) , | |
| ExtendedArg , | |
| // If you add a new instruction here, be sure to keep LAST_INSTRUCTION updated | |
| } |
RustPython/compiler/core/src/bytecode.rs
Lines 1335 to 1697 in 3a6fda4
| impl Instruction { | |
| /// Gets the label stored inside this instruction, if it exists | |
| # [ inline ] | |
| pub const fn label_arg ( & self ) -> Option < Arg < Label > > { | |
| match self { | |
| Jump { target : l } | |
| | PopJumpIfTrue { target : l } | |
| | PopJumpIfFalse { target : l } | |
| | JumpIfTrueOrPop { target : l } | |
| | JumpIfFalseOrPop { target : l } | |
| | ForIter { target : l } | |
| | SetupFinally { handler : l } | |
| | SetupExcept { handler : l } | |
| | SetupWith { end : l } | |
| | SetupAsyncWith { end : l } | |
| | Break { target : l } | |
| | Continue { target : l } => Some ( * l ) , | |
| _ => None , | |
| } | |
| } | |
| /// Whether this is an unconditional branching | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use rustpython_compiler_core::bytecode::{Arg, Instruction}; | |
| /// let jump_inst = Instruction::Jump { target: Arg::marker() }; | |
| /// assert!(jump_inst.unconditional_branch()) | |
| /// ``` | |
| pub const fn unconditional_branch ( & self ) -> bool { | |
| matches ! ( | |
| self , | |
| Jump { .. } | |
| | Continue { .. } | |
| | Break { .. } | |
| | ReturnValue | |
| | ReturnConst { .. } | |
| | Raise { .. } | |
| ) | |
| } | |
| /// What effect this instruction has on the stack | |
| /// | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// use rustpython_compiler_core::bytecode::{Arg, Instruction, Label, UnaryOperator}; | |
| /// let (target, jump_arg) = Arg::new(Label(0xF)); | |
| /// let jump_instruction = Instruction::Jump { target }; | |
| /// let (op, invert_arg) = Arg::new(UnaryOperator::Invert); | |
| /// let invert_instruction = Instruction::UnaryOperation { op }; | |
| /// assert_eq!(jump_instruction.stack_effect(jump_arg, true), 0); | |
| /// assert_eq!(invert_instruction.stack_effect(invert_arg, false), 0); | |
| /// ``` | |
| /// | |
| pub fn stack_effect ( & self , arg : OpArg , jump : bool ) -> i32 { | |
| match self { | |
| Nop => 0 , | |
| ImportName { .. } | ImportNameless => - 1 , | |
| ImportFrom { .. } => 1 , | |
| LoadFast ( _ ) | LoadNameAny ( _ ) | LoadGlobal ( _ ) | LoadDeref ( _ ) | LoadClassDeref ( _ ) => 1 , | |
| StoreFast ( _ ) | StoreLocal ( _ ) | StoreGlobal ( _ ) | StoreDeref ( _ ) => - 1 , | |
| DeleteFast ( _ ) | DeleteLocal ( _ ) | DeleteGlobal ( _ ) | DeleteDeref ( _ ) => 0 , | |
| LoadClosure ( _ ) => 1 , | |
| Subscript => - 1 , | |
| StoreSubscript => - 3 , | |
| DeleteSubscript => - 2 , | |
| LoadAttr { .. } => 0 , | |
| StoreAttr { .. } => - 2 , | |
| DeleteAttr { .. } => - 1 , | |
| LoadConst { .. } => 1 , | |
| UnaryOperation { .. } => 0 , | |
| BinaryOperation { .. } | |
| | BinaryOperationInplace { .. } | |
| | TestOperation { .. } | |
| | CompareOperation { .. } => - 1 , | |
| BinarySubscript => - 1 , | |
| CopyItem { .. } => 1 , | |
| Pop => - 1 , | |
| Swap { .. } => 0 , | |
| ToBool => 0 , | |
| Rotate2 | Rotate3 => 0 , | |
| Duplicate => 1 , | |
| Duplicate2 => 2 , | |
| GetIter => 0 , | |
| GetLen => 1 , | |
| CallIntrinsic1 { .. } => 0 , // Takes 1, pushes 1 | |
| CallIntrinsic2 { .. } => - 1 , // Takes 2, pushes 1 | |
| Continue { .. } => 0 , | |
| Break { .. } => 0 , | |
| Jump { .. } => 0 , | |
| PopJumpIfTrue { .. } | PopJumpIfFalse { .. } => - 1 , | |
| JumpIfTrueOrPop { .. } | JumpIfFalseOrPop { .. } => { | |
| if jump { | |
| 0 | |
| } else { | |
| - 1 | |
| } | |
| } | |
| MakeFunction => { | |
| // CPython 3.13 style: MakeFunction only pops code object | |
| - 1 + 1 // pop code, push function | |
| } | |
| SetFunctionAttribute { .. } => { | |
| // pops attribute value and function, pushes function back | |
| - 2 + 1 | |
| } | |
| CallFunctionPositional { nargs } => - ( nargs . get ( arg ) as i32 ) - 1 + 1 , | |
| CallMethodPositional { nargs } => - ( nargs . get ( arg ) as i32 ) - 3 + 1 , | |
| CallFunctionKeyword { nargs } => - 1 - ( nargs . get ( arg ) as i32 ) - 1 + 1 , | |
| CallMethodKeyword { nargs } => - 1 - ( nargs . get ( arg ) as i32 ) - 3 + 1 , | |
| CallFunctionEx { has_kwargs } => - 1 - ( has_kwargs . get ( arg ) as i32 ) - 1 + 1 , | |
| CallMethodEx { has_kwargs } => - 1 - ( has_kwargs . get ( arg ) as i32 ) - 3 + 1 , | |
| LoadMethod { .. } => - 1 + 3 , | |
| ForIter { .. } => { | |
| if jump { | |
| - 1 | |
| } else { | |
| 1 | |
| } | |
| } | |
| ReturnValue => - 1 , | |
| ReturnConst { .. } => 0 , | |
| Resume { .. } => 0 , | |
| YieldValue => 0 , | |
| YieldFrom => - 1 , | |
| SetupAnnotation | SetupLoop | SetupFinally { .. } | EnterFinally | EndFinally => 0 , | |
| SetupExcept { .. } => jump as i32 , | |
| SetupWith { .. } => ( !jump ) as i32 , | |
| WithCleanupStart => 0 , | |
| WithCleanupFinish => - 1 , | |
| PopBlock => 0 , | |
| Raise { kind } => - ( kind . get ( arg ) as u8 as i32 ) , | |
| BuildString { size } | |
| | BuildTuple { size , .. } | |
| | BuildTupleFromTuples { size , .. } | |
| | BuildList { size , .. } | |
| | BuildListFromTuples { size , .. } | |
| | BuildSet { size , .. } | |
| | BuildSetFromTuples { size , .. } => - ( size . get ( arg ) as i32 ) + 1 , | |
| BuildTupleFromIter => 0 , | |
| BuildMap { size } => { | |
| let nargs = size . get ( arg ) * 2 ; | |
| - ( nargs as i32 ) + 1 | |
| } | |
| BuildMapForCall { size } => { | |
| let nargs = size . get ( arg ) ; | |
| - ( nargs as i32 ) + 1 | |
| } | |
| DictUpdate { .. } => - 1 , | |
| BuildSlice { step } => - 2 - ( step . get ( arg ) as i32 ) + 1 , | |
| ListAppend { .. } | SetAdd { .. } => - 1 , | |
| MapAdd { .. } => - 2 , | |
| PrintExpr => - 1 , | |
| LoadBuildClass => 1 , | |
| UnpackSequence { size } => - 1 + size . get ( arg ) as i32 , | |
| UnpackEx { args } => { | |
| let UnpackExArgs { before , after } = args . get ( arg ) ; | |
| - 1 + before as i32 + 1 + after as i32 | |
| } | |
| FormatValue { .. } => - 1 , | |
| PopException => 0 , | |
| Reverse { .. } => 0 , | |
| GetAwaitable => 0 , | |
| BeforeAsyncWith => 1 , | |
| SetupAsyncWith { .. } => { | |
| if jump { | |
| - 1 | |
| } else { | |
| 0 | |
| } | |
| } | |
| GetAIter => 0 , | |
| GetANext => 1 , | |
| EndAsyncFor => - 2 , | |
| MatchMapping | MatchSequence => 1 , // Push bool result | |
| MatchKeys => 1 , // Pop 2 (subject, keys), push 3 (subject, keys_or_none, values_or_none) | |
| MatchClass ( _ ) => - 2 , | |
| ExtendedArg => 0 , | |
| } | |
| } | |
| pub fn display < ' a > ( | |
| & ' a self , | |
| arg : OpArg , | |
| ctx : & ' a impl InstrDisplayContext , | |
| ) -> impl fmt :: Display + ' a { | |
| struct FmtFn < F > ( F ) ; | |
| impl < F : Fn ( & mut fmt :: Formatter < ' _ > ) -> fmt :: Result > fmt :: Display for FmtFn < F > { | |
| fn fmt ( & self , f : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result { | |
| ( self . 0 ) ( f ) | |
| } | |
| } | |
| FmtFn ( move | f : & mut fmt :: Formatter < ' _ > | self . fmt_dis ( arg , f , ctx , false , 0 , 0 ) ) | |
| } | |
| # [ allow ( clippy :: too_many_arguments ) ] | |
| fn fmt_dis ( | |
| & self , | |
| arg : OpArg , | |
| f : & mut fmt :: Formatter < ' _ > , | |
| ctx : & impl InstrDisplayContext , | |
| expand_code_objects : bool , | |
| pad : usize , | |
| level : usize , | |
| ) -> fmt :: Result { | |
| macro_rules! w { | |
| ( $variant : ident ) => { | |
| write! ( f , stringify! ( $variant ) ) | |
| } ; | |
| ( $variant : ident , $map : ident = $arg_marker : expr ) => { { | |
| let arg = $arg_marker . get ( arg ) ; | |
| write! ( f , "{:pad$}({}, {})" , stringify! ( $variant ) , arg , $map ( arg ) ) | |
| } } ; | |
| ( $variant : ident , $arg_marker : expr ) => { | |
| write! ( f , "{:pad$}({})" , stringify! ( $variant ) , $arg_marker . get ( arg ) ) | |
| } ; | |
| ( $variant : ident , ?$arg_marker : expr ) => { | |
| write! ( | |
| f , | |
| "{:pad$}({:?})" , | |
| stringify! ( $variant ) , | |
| $arg_marker . get ( arg ) | |
| ) | |
| } ; | |
| } | |
| let varname = | i : u32 | ctx . get_varname ( i as usize ) ; | |
| let name = | i : u32 | ctx . get_name ( i as usize ) ; | |
| let cell_name = | i : u32 | ctx . get_cell_name ( i as usize ) ; | |
| let fmt_const = | |
| | op : & str , arg : OpArg , f : & mut fmt :: Formatter < ' _ > , idx : & Arg < u32 > | -> fmt :: Result { | |
| let value = ctx . get_constant ( idx . get ( arg ) as usize ) ; | |
| match value . borrow_constant ( ) { | |
| BorrowedConstant :: Code { code } if expand_code_objects => { | |
| write ! ( f , "{op:pad$}({code:?}):" ) ? ; | |
| code . display_inner ( f , true , level + 1 ) ? ; | |
| Ok ( ( ) ) | |
| } | |
| c => { | |
| write ! ( f , "{op:pad$}(" ) ? ; | |
| c . fmt_display ( f ) ? ; | |
| write ! ( f , ")" ) | |
| } | |
| } | |
| } ; | |
| match self { | |
| Nop => w ! ( Nop ) , | |
| ImportName { idx } => w ! ( ImportName , name = idx ) , | |
| ImportNameless => w ! ( ImportNameless ) , | |
| ImportFrom { idx } => w ! ( ImportFrom , name = idx ) , | |
| LoadFast ( idx ) => w ! ( LoadFast , varname = idx ) , | |
| LoadNameAny ( idx ) => w ! ( LoadNameAny , name = idx ) , | |
| LoadGlobal ( idx ) => w ! ( LoadGlobal , name = idx ) , | |
| LoadDeref ( idx ) => w ! ( LoadDeref , cell_name = idx ) , | |
| LoadClassDeref ( idx ) => w ! ( LoadClassDeref , cell_name = idx ) , | |
| StoreFast ( idx ) => w ! ( StoreFast , varname = idx ) , | |
| StoreLocal ( idx ) => w ! ( StoreLocal , name = idx ) , | |
| StoreGlobal ( idx ) => w ! ( StoreGlobal , name = idx ) , | |
| StoreDeref ( idx ) => w ! ( StoreDeref , cell_name = idx ) , | |
| DeleteFast ( idx ) => w ! ( DeleteFast , varname = idx ) , | |
| DeleteLocal ( idx ) => w ! ( DeleteLocal , name = idx ) , | |
| DeleteGlobal ( idx ) => w ! ( DeleteGlobal , name = idx ) , | |
| DeleteDeref ( idx ) => w ! ( DeleteDeref , cell_name = idx ) , | |
| LoadClosure ( i ) => w ! ( LoadClosure , cell_name = i ) , | |
| Subscript => w ! ( Subscript ) , | |
| StoreSubscript => w ! ( StoreSubscript ) , | |
| DeleteSubscript => w ! ( DeleteSubscript ) , | |
| StoreAttr { idx } => w ! ( StoreAttr , name = idx ) , | |
| DeleteAttr { idx } => w ! ( DeleteAttr , name = idx ) , | |
| LoadConst { idx } => fmt_const ( "LoadConst" , arg , f , idx ) , | |
| UnaryOperation { op } => w ! ( UnaryOperation , ?op ) , | |
| BinaryOperation { op } => w ! ( BinaryOperation , ?op ) , | |
| BinaryOperationInplace { op } => w ! ( BinaryOperationInplace , ?op ) , | |
| BinarySubscript => w ! ( BinarySubscript ) , | |
| LoadAttr { idx } => w ! ( LoadAttr , name = idx ) , | |
| TestOperation { op } => w ! ( TestOperation , ?op ) , | |
| CompareOperation { op } => w ! ( CompareOperation , ?op ) , | |
| CopyItem { index } => w ! ( CopyItem , index ) , | |
| Pop => w ! ( Pop ) , | |
| Swap { index } => w ! ( Swap , index ) , | |
| ToBool => w ! ( ToBool ) , | |
| Rotate2 => w ! ( Rotate2 ) , | |
| Rotate3 => w ! ( Rotate3 ) , | |
| Duplicate => w ! ( Duplicate ) , | |
| Duplicate2 => w ! ( Duplicate2 ) , | |
| GetIter => w ! ( GetIter ) , | |
| // GET_LEN | |
| GetLen => w ! ( GetLen ) , | |
| CallIntrinsic1 { func } => w ! ( CallIntrinsic1 , ?func ) , | |
| CallIntrinsic2 { func } => w ! ( CallIntrinsic2 , ?func ) , | |
| Continue { target } => w ! ( Continue , target ) , | |
| Break { target } => w ! ( Break , target ) , | |
| Jump { target } => w ! ( Jump , target ) , | |
| PopJumpIfTrue { target } => w ! ( PopJumpIfTrue , target ) , | |
| PopJumpIfFalse { target } => w ! ( PopJumpIfFalse , target ) , | |
| JumpIfTrueOrPop { target } => w ! ( JumpIfTrueOrPop , target ) , | |
| JumpIfFalseOrPop { target } => w ! ( JumpIfFalseOrPop , target ) , | |
| MakeFunction => w ! ( MakeFunction ) , | |
| SetFunctionAttribute { attr } => w ! ( SetFunctionAttribute , ?attr ) , | |
| CallFunctionPositional { nargs } => w ! ( CallFunctionPositional , nargs ) , | |
| CallFunctionKeyword { nargs } => w ! ( CallFunctionKeyword , nargs ) , | |
| CallFunctionEx { has_kwargs } => w ! ( CallFunctionEx , has_kwargs ) , | |
| LoadMethod { idx } => w ! ( LoadMethod , name = idx ) , | |
| CallMethodPositional { nargs } => w ! ( CallMethodPositional , nargs ) , | |
| CallMethodKeyword { nargs } => w ! ( CallMethodKeyword , nargs ) , | |
| CallMethodEx { has_kwargs } => w ! ( CallMethodEx , has_kwargs ) , | |
| ForIter { target } => w ! ( ForIter , target ) , | |
| ReturnValue => w ! ( ReturnValue ) , | |
| ReturnConst { idx } => fmt_const ( "ReturnConst" , arg , f , idx ) , | |
| Resume { arg } => w ! ( Resume , arg ) , | |
| YieldValue => w ! ( YieldValue ) , | |
| YieldFrom => w ! ( YieldFrom ) , | |
| SetupAnnotation => w ! ( SetupAnnotation ) , | |
| SetupLoop => w ! ( SetupLoop ) , | |
| SetupExcept { handler } => w ! ( SetupExcept , handler ) , | |
| SetupFinally { handler } => w ! ( SetupFinally , handler ) , | |
| EnterFinally => w ! ( EnterFinally ) , | |
| EndFinally => w ! ( EndFinally ) , | |
| SetupWith { end } => w ! ( SetupWith , end ) , | |
| WithCleanupStart => w ! ( WithCleanupStart ) , | |
| WithCleanupFinish => w ! ( WithCleanupFinish ) , | |
| BeforeAsyncWith => w ! ( BeforeAsyncWith ) , | |
| SetupAsyncWith { end } => w ! ( SetupAsyncWith , end ) , | |
| PopBlock => w ! ( PopBlock ) , | |
| Raise { kind } => w ! ( Raise , ?kind ) , | |
| BuildString { size } => w ! ( BuildString , size ) , | |
| BuildTuple { size } => w ! ( BuildTuple , size ) , | |
| BuildTupleFromTuples { size } => w ! ( BuildTupleFromTuples , size ) , | |
| BuildTupleFromIter => w ! ( BuildTupleFromIter ) , | |
| BuildList { size } => w ! ( BuildList , size ) , | |
| BuildListFromTuples { size } => w ! ( BuildListFromTuples , size ) , | |
| BuildSet { size } => w ! ( BuildSet , size ) , | |
| BuildSetFromTuples { size } => w ! ( BuildSetFromTuples , size ) , | |
| BuildMap { size } => w ! ( BuildMap , size ) , | |
| BuildMapForCall { size } => w ! ( BuildMapForCall , size ) , | |
| DictUpdate { index } => w ! ( DictUpdate , index ) , | |
| BuildSlice { step } => w ! ( BuildSlice , step ) , | |
| ListAppend { i } => w ! ( ListAppend , i ) , | |
| SetAdd { i } => w ! ( SetAdd , i ) , | |
| MapAdd { i } => w ! ( MapAdd , i ) , | |
| PrintExpr => w ! ( PrintExpr ) , | |
| LoadBuildClass => w ! ( LoadBuildClass ) , | |
| UnpackSequence { size } => w ! ( UnpackSequence , size ) , | |
| UnpackEx { args } => w ! ( UnpackEx , args ) , | |
| FormatValue { conversion } => w ! ( FormatValue , ?conversion ) , | |
| PopException => w ! ( PopException ) , | |
| Reverse { amount } => w ! ( Reverse , amount ) , | |
| GetAwaitable => w ! ( GetAwaitable ) , | |
| GetAIter => w ! ( GetAIter ) , | |
| GetANext => w ! ( GetANext ) , | |
| EndAsyncFor => w ! ( EndAsyncFor ) , | |
| MatchMapping => w ! ( MatchMapping ) , | |
| MatchSequence => w ! ( MatchSequence ) , | |
| MatchKeys => w ! ( MatchKeys ) , | |
| MatchClass ( arg ) => w ! ( MatchClass , arg ) , | |
| ExtendedArg => w ! ( ExtendedArg , Arg :: < u32 > :: marker ( ) ) , | |
| } | |
| } | |
| } |
But turned out to be more work than originally anticipated.
From what I see ATM our largest gaps are that we have opcodes that doesn't exist in Cpython (3.13.7) such as
RustPython/compiler/core/src/bytecode.rs
Lines 620 to 625 in 3a6fda4
| Continue { | |
| target : Arg < Label > , | |
| } , | |
| Break { | |
| target : Arg < Label > , | |
| } , |
So, I have converted this PR to address #6156 (review)
cc: @youknowone @coolreader18
Followup for #6156 (comment)
Summary by CodeRabbit
-
New Features
- Public, strongly-typed opcode API with Real and Pseudo opcode types, unified conversions, and rich inspection utilities exposed throughout the runtime.
-
Bug Fixes
- Improved opcode validation and more accurate stack-effect calculations; added special-case handling and adjusted a related configuration constant.
-
Chores
- Added an opcode generation script, marked generated sources for tooling, updated spell dictionary, and re-exported opcode APIs for easier access.
coderabbitai
Bot
commented
Sep 23, 2025
·
edited
Loading
Copy link Copy Markdown
Contributor
WalkthroughAdds generated opcode enums (RealOpcode, PseudoOpcode) and an Opcode wrapper to compiler/core, a Python generator script to emit the generated file, stdlib and VM updates to use the new opcode types, marks the generated file in .gitattributes, adds dependencies, and updates a spelling dictionary token. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Caller
participant Std as stdlib::opcode::stack_effect
participant Core as compiler_core::opcode::Opcode
participant Real as compiler_core::opcodes::RealOpcode
participant Pseudo as compiler_core::opcodes::PseudoOpcode
Caller->>Std: stack_effect(raw_id, oparg)
Std->>Core: Opcode::try_from(raw_id)
alt Pseudo (u16)
Core->>Pseudo: PseudoOpcode::try_from(raw)
Pseudo-->>Std: PseudoOpcode
Std->>Std: apply pseudo-specific stack logic
Std-->>Caller: stack delta
else Real (u8)
Core->>Real: RealOpcode::try_from(raw_u8)
Real-->>Std: RealOpcode
Std->>Std: use RealOpcode::num_popped/num_pushed/deopt
Std-->>Caller: stack delta
else Invalid
Std-->>Caller: invalid / error
end
Loading
sequenceDiagram autonumber actor Dev participant Script as scripts/gen_opcodes.py participant Output as compiler/core/src/opcodes.rs participant Repo as Git Dev->>Script: run generator (analyze CPython) Script->>Output: emit enums, predicates, TryFrom impls, helpers Script->>Repo: write file & run cargo fmt Repo-->>Output: file added (marked in .gitattributes)Loading Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
📜 Recent review details Configuration used : Path: .coderabbit.yml Review profile : CHILL Plan : Pro 📥 CommitsReviewing files that changed from the base of the PR and between 3daec5a and 9436354 . 📒 Files selected for processing (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. ❤️ ShareComment |
opcode from 3.13.7
#6156
Merged
ShaharNaveh
changed the title
POC: Instruction auto generation
POC: Opcode metadata auto generation
Oct 5, 2025
ShaharNaveh commented Oct 5, 2025
Copy link Copy Markdown
Contributor
Author
|
I have updated the main PR comment |
Bot
reviewed Oct 5, 2025
View reviewed changes
coderabbitai
Bot
left a comment
Copy link Copy Markdown
Contributor
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
Actionable comments posted: 2
🧹 Nitpick comments (3)compiler/core/src/opcode.rs (3)📜 Review details
7-8: Consider adding accessor and additional trait implementations.The
OpcodeIdnewtype lacks a public accessor for the inneru16value. Additionally, consider implementingHash,Display, and potentiallyFromStrfor better ergonomics.Apply this diff to add an accessor:
#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct OpcodeId(u16); impl OpcodeId { + /// Returns the raw opcode ID value. + #[must_use] + pub const fn get(self) -> u16 { + self.0 + } +For
Hash, update the derives:-#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] pub struct OpcodeId(u16);
235-243: Enhance safety documentation.The safety documentation for
new_uncheckedshould reference theis_validmethod to clarify what constitutes a valid opcode ID.Apply this diff:
/// Creates a new opcode ID without checking the value is a valid opcode ID. /// /// # Safety /// - /// The value should be a valid opcode ID. + /// The caller must ensure that `id` satisfies `Self::is_valid(id)`. #[must_use] pub const unsafe fn new_unchecked(id: u16) -> Self {
545-560: Consider a more descriptive error type for TryFrom.The
TryFromimplementations use()as the error type, providing no information about why a conversion failed. While this is acceptable, a custom error type could improve debuggability.Consider defining an error type:
/// Error type for invalid opcode ID conversions. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct InvalidOpcodeId; impl std::fmt::Display for InvalidOpcodeId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "invalid opcode ID") } } impl std::error::Error for InvalidOpcodeId {}Then update the macro:
macro_rules! opcode_id_try_from_impl { ($t:ty) => { impl TryFrom<$t> for OpcodeId { - type Error = (); + type Error = InvalidOpcodeId; fn try_from(value: $t) -> Result<Self, Self::Error> { - let id = value.try_into().map_err(|_| ())?; + let id = value.try_into().map_err(|_| InvalidOpcodeId)?; if Self::is_valid(id) { Ok(Self(id)) } else { - Err(()) + Err(InvalidOpcodeId) } } }
Configuration used : Path: .coderabbit.yml
Review profile : CHILL
Plan : Pro
📥 CommitsReviewing files that changed from the base of the PR and between 3a6fda4 and db2775a .
⛔ Files ignored due to path filters (1)Cargo.lockis excluded by!**/*.lock
.gitattributes(1 hunks)compiler/core/src/lib.rs(1 hunks)compiler/core/src/opcode.rs(1 hunks)scripts/gen_opcodes.py(1 hunks)stdlib/src/opcode.rs(3 hunks)vm/src/lib.rs(1 hunks)
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Format Rust code with the default rustfmt style (runcargo fmt)
Run clippy and fix any warnings or lints introduced by your changes
Follow Rust best practices for error handling and memory management
Files:
vm/src/lib.rscompiler/core/src/lib.rscompiler/core/src/opcode.rsstdlib/src/opcode.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use RustPython macros (
pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
vm/src/lib.rsstdlib/src/opcode.rs
compiler/core/src/opcode.rs (9)compiler/core/src/opcode.rs (1)stdlib/src/opcode.rs (8)
is_pseudo(253-269)is_valid(247-249)has_arg(273-426)has_const(430-435)has_name(439-465)has_jump(469-487)has_free(491-500)has_local(504-519)has_exc(523-542)
is_valid(70-72)has_arg(75-77)has_const(80-82)has_name(85-87)has_jump(90-92)has_free(95-97)has_local(100-102)has_exc(105-107)
stdlib/src/opcode.rs (8)stdlib/src/opcode.rs (3)
is_valid(70-72)has_arg(75-77)has_const(80-82)has_name(85-87)has_jump(90-92)has_free(95-97)has_local(100-102)has_exc(105-107)
scripts/gen_opcodes.py (1)🪛 Flake8 (7.3.0) scripts/gen_opcodes.pycompiler/core/src/bytecode.rs (1)
Instruction(81-119)compiler/core/src/opcode.rs (8)
try_from(814-820)
is_valid(247-249)has_arg(273-426)has_const(430-435)has_name(439-465)has_jump(469-487)has_free(491-500)has_local(504-519)has_exc(523-542)
[error] 256-256: indentation contains mixed spaces and tabs
(E101)
[error] 257-257: indentation contains mixed spaces and tabs
(E101)
[error] 258-258: indentation contains mixed spaces and tabs
(E101)
[error] 259-259: indentation contains mixed spaces and tabs
(E101)
[error] 260-260: indentation contains mixed spaces and tabs
(E101)
[error] 261-261: indentation contains mixed spaces and tabs
(E101)
[error] 262-262: indentation contains mixed spaces and tabs
(E101)
[error] 263-263: indentation contains mixed spaces and tabs
(E101)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)- GitHub Check: Run snippets and cpython tests (windows-2025)
scripts/gen_opcodes.py (2)Comment thread compiler/core/src/opcode.rscompiler/core/src/opcode.rs (1)
75-78: ...
75-78: ...
11-233: Refine opcode verification scope
Adaptive and instrumented opcodes (e.g. CALL_ALLOC_AND_ENTER_INIT, INSTRUMENTED_*, etc.) are RustPython-specific and must not be removed; only verify that the standard CPython 3.13.7 opcodes (those without ADAPTIVE_ or INSTRUMENTED_ prefixes) match their CPython values.Likely an incorrect or invalid review comment.
Outdated
Comment on lines +521 to +523
| /// Whether opcode ID have 'HAS_PURE_FLAG' set. | ||
| #[must_use] | ||
| pub const fn has_exc(&self) -> bool { |
Bot
reviewed Oct 5, 2025
View reviewed changes
coderabbitai
Bot
left a comment
Copy link Copy Markdown
Contributor
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
Actionable comments posted: 1
🧹 Nitpick comments (1)stdlib/src/opcode.rs (1)📜 Review details
26-26: Track the TODO for auto-generated Instruction enum.This TODO references future work to leverage the auto-generated
Instructionenum. Consider tracking this as a follow-up task.Do you want me to open a new issue to track this task?
Configuration used : Path: .coderabbit.yml
Review profile : CHILL
Plan : Pro
📥 CommitsReviewing files that changed from the base of the PR and between db2775a and 7fa003b .
📒 Files selected for processing (1)stdlib/src/opcode.rs(3 hunks)
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Format Rust code with the default rustfmt style (runcargo fmt)
Run clippy and fix any warnings or lints introduced by your changes
Follow Rust best practices for error handling and memory management
Files:
stdlib/src/opcode.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use RustPython macros (
pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
stdlib/src/opcode.rs
scripts/gen_opcodes.py (1)⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)compiler/core/src/bytecode.rs (1)
Instruction(81-119)compiler/core/src/opcode.rs (8)
try_from(814-820)
is_valid(247-249)has_arg(273-426)has_const(430-435)has_name(439-465)has_jump(469-487)has_free(491-500)has_local(504-519)has_exc(523-542)
- GitHub Check: Check Rust code with rustfmt and clippy
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests (windows-2025)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Ensure compilation on various targets
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Run rust tests (windows-2025)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Check the WASM package and demo
stdlib/src/opcode.rs (3)Comment thread stdlib/src/opcode.rs
57-59: LGTM! TypeError preservation implemented correctly.The error handling now correctly preserves
TypeErrorwhen the opcode argument is not an integer, while only convertingInstruction::try_fromfailures toValueError. This directly addresses the feedback from the previous review.
14-14: LGTM! Correct type for the flag.Changing
ENABLE_SPECIALIZATIONfromi8tou8is appropriate for a boolean-like flag constant.
71-101: LGTM! Consistent pattern across all predicates.The
has_*functions follow a consistent pattern usingOpcodeId::try_from(opcode).is_ok_and(|oid| !oid.is_pseudo() && oid.has_*()), which correctly filters out pseudo-opcodes and delegates to the generated predicates.
Outdated
| #[pyfunction] | ||
| fn is_valid(opcode: i32) -> bool { | ||
| Opcode::is_valid (opcode) | ||
| OpcodeId::try_from (opcode ).is_ok( ) |
POC: automated instruction gen
21d9632
Add generated file
f6ab8d2
Add note about pseudos opcode
b8ebe9b
Use properties.oparg instead
8510815
Sort by id. impl TryFrom
bcea0bf
Have is_valid as a seperate method
c7ffa2a
Include in tree
f1948c6
Impl has_X methods
84c7f45
is_pseudo
9adf908
Set Self::
99ed360
refactor
56c0dc8
impl num_{pushed,popped}
3fdc479
Resolve some errors
5cf1888
Sort like cpython does it
eaee21e
Make more things compile
acd74c7
Don't use the new Instruction yet
f71ad1b
pivot to only generating opcodeid atm
b860f65
Remove enum related code
ff4fb09
Use OpcodeId in _opcode
493da32
add auto generated file to .gitattributes
657660f
clippy
eacab03
Apply coderabbit suggestion
7b36086
ShaharNaveh
added 3 commits
October 6, 2025 18:54
More docs
465528e
unused import
e437232
nits
1ae0c7e
ShaharNaveh
requested review from
coolreader18 and
youknowone
October 6, 2025 16:09
Trigger CI
28bab70
youknowone
reviewed
Oct 13, 2025
View reviewed changes
youknowone left a comment
Copy link Copy Markdown
Member
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
@coolreader18 What will be the best way to solve it?
@ShaharNaveh Do we need to have different type for Instruction and RealOpCode? Otherwise, just in case, can it be defined like:
struct RealOpCode(bytecode::Instruction);
Comment thread
compiler/core/src/opcodes.rs
Outdated
Comment on lines +1001 to +1166
| impl TryFrom<i8> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: i8) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<i16> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: i16) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<i32> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: i32) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<i64> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: i64) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<i128> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: i128) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<isize> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: isize) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<u8> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(id: u8) -> Result<Self, Self::Error> { | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<u16> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: u16) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<u32> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: u32) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<u64> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: u64) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<u128> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: u128) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TryFrom<usize> for RealOpcode { | ||
| type Error = (); | ||
|
|
||
| fn try_from(raw: usize) -> Result<Self, Self::Error> { | ||
| let id = raw.try_into().map_err(|_| ())?; | ||
| if Self::is_valid(id) { | ||
| // SAFETY: We just validated that we have a valid opcode id. | ||
| Ok(unsafe { Self::new_unchecked(id) }) | ||
| } else { | ||
| Err(()) | ||
| } | ||
| } | ||
| } |
ShaharNaveh commented Oct 13, 2025
Copy link Copy Markdown
Contributor
Author
Yes, because imo opcode should be it's own enum. An instruction should contain a where
For impl<T: OpargType> Instruction<T> {
pub fn new(opcode: RealOpcode, raw_oparg: u32) -> Result<Self, MarshalError> {
if !opcode.has_arg() {
return Ok(Self {opcode, oparg: None });
}
let oparg = match opcode {
RealOpcode::Resume => ResumeOpcode::try_from(raw_oparg)?,
// More typed oparg checks for specific opcodes
_ => Oparg(raw_oparg)
};
Ok(Self { opcode, oparg: Some(oparg) })
}
}
|
Merge remote-tracking branch 'upstream/main' into instruction-autogen
fe3f94a
Use num_enum for opcode enums
aca4508
coderabbitai
Bot
reviewed Oct 13, 2025
View reviewed changes
coderabbitai
Bot
left a comment
Copy link Copy Markdown
Contributor
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
Actionable comments posted: 1
♻️ Duplicate comments (2)stdlib/src/opcode.rs (1)📜 Review detailsscripts/gen_opcodes.py (1)
131-133: Filter pseudo-opcodes inis_validto match CPython semantics.Past review comments correctly noted that
is_validshould reject pseudo-opcodes, as they are not valid for runtime bytecode execution and CPython'sopcode.is_validexcludes them. Currently,Opcode::try_from(opcode).is_ok()returnstruefor pseudo-opcodes.Apply this diff to filter pseudo-opcodes:
#[pyfunction] fn is_valid(opcode: i32) -> bool { - Opcode::try_from(opcode).is_ok() + matches!(Opcode::try_from(opcode), Ok(Opcode::Real(_))) }
53-78: Guard against empty input ingroup_ranges.The function unconditionally accesses
nums[0]on line 73, raisingIndexErrorif the input iterable is empty. While CPython's opcode metadata likely never produces empty ranges in practice, defensive coding would prevent potential future issues.Apply this diff to add an early return:
def group_ranges(it: "Iterable[int]") -> "Iterator[range]": """ Group consecutive numbers into ranges. Parameters ---------- it : Iterable[int] Numbers to group into ranges. Notes ----- Numbers in `it` must be sorted in ascending order. Examples -------- >>> nums = [0, 1, 2, 3, 17, 18, 42, 50, 51] >>> list(group_ranges(nums)) [range(0, 4), range(17, 19), range(42, 43), range(50, 52)] """ nums = list(it) + if not nums: + return start = prev = nums[0]
Configuration used : Path: .coderabbit.yml
Review profile : CHILL
Plan : Pro
📥 CommitsReviewing files that changed from the base of the PR and between 1ae0c7e and aca4508 .
⛔ Files ignored due to path filters (1)Cargo.lockis excluded by!**/*.lock
compiler/core/Cargo.toml(1 hunks)compiler/core/src/opcode.rs(1 hunks)compiler/core/src/opcodes.rs(1 hunks)scripts/gen_opcodes.py(1 hunks)stdlib/src/opcode.rs(3 hunks)
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Format Rust code with the default rustfmt style (runcargo fmt)
Run clippy and fix any warnings or lints introduced by your changes
Follow Rust best practices for error handling and memory management
Files:
stdlib/src/opcode.rscompiler/core/src/opcode.rscompiler/core/src/opcodes.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use RustPython macros (
pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
stdlib/src/opcode.rs
compiler/core/src/bytecode.rs (2)scripts/gen_opcodes.py (1)compiler/core/src/opcode.rs (2)
stack_effect(1391-1516)try_from(814-820)compiler/core/src/opcodes.rs (14)
try_from(13-20)try_from(26-29)
has_arg(303-445)has_arg(1026-1038)has_const(449-454)has_const(1042-1044)has_name(527-549)has_name(1079-1087)has_jump(490-506)has_jump(1067-1069)has_free(477-486)has_free(1061-1063)has_local(510-523)has_local(1073-1075)has_exc(458-473)has_exc(1048-1057)
compiler/core/src/opcodes.rs (1)compiler/core/src/opcodes.rs (1)
deopt(228-299)
stdlib/src/opcode.rs (8)⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
has_arg(136-138)has_const(141-143)has_exc(166-168)has_free(156-158)has_jump(151-153)has_local(161-163)has_name(146-148)is_valid(131-133)
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Ensure compilation on various targets
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Check the WASM package and demo
- GitHub Check: Check Rust code with rustfmt and clippy
compiler/core/Cargo.toml (1)stdlib/src/opcode.rs (3)
20-21: LGTM! Dependencies align with the new opcode system.The addition of
num_enumandnum-traitsappropriately supports the generated opcode infrastructure.num_enumprovides theTryFromPrimitivederive macro used inopcodes.rs, andnum-traitssupplies thePrimInttrait bound for the error constructor.scripts/gen_opcodes.py (1)
7-11: LGTM! Updated imports and type change.The imports now correctly reference the new
Opcode,PseudoOpcode, andRealOpcodetypes from thevm::opcodemodule. The change ofENABLE_SPECIALIZATIONfromi8tou8aligns with CPython's unsigned value and removes unnecessary signedness.
23-117: Stack effect implementation aligns well with CPython.The refactored
stack_effectcorrectly handlesRealOpcodeandPseudoOpcodeseparately, mirrors CPython's special-case treatment ofExitInitCheck, rejects specialized opcodes viadeopt(), and properly computes stack effects for pseudo-opcodes with jump-dependent logic.
119-168: LGTM! Macro-based real opcode checks.The
real_opcode_check!macro correctly filters out pseudo-opcodes by attempting conversion toRealOpcodeviau8::try_fromand delegates to the appropriatehas_*predicate. This ensures allhas_arg,has_const,has_name,has_jump,has_free,has_local, andhas_excfunctions only operate on real opcodes, consistent with CPython semantics.compiler/core/src/opcodes.rs (1)
112-343: Well-structured generator design.The abstraction via
InstructionsMeta,RealInstructions, andPseudoInstructionscleanly separates concerns and generates comprehensive Rust opcode scaffolding including predicates, stack effects, deoptimization mappings, and TryFrom conversions. The CPython variant-to-Rust identifier substitution and automaticcargo fmtinvocation are nice touches for maintainability.compiler/core/src/opcode.rs (2)
1-1109: Generated opcode definitions are comprehensive and correct.The auto-generated
RealOpcode(repr(u8), 223 variants) andPseudoOpcode(repr(u16), 12 variants) enums provide exhaustive CPython 3.13.7 opcode coverage. The implementation includes:
deopt()mappings for specialized opcodes- Predicate methods (
has_arg,has_const,has_exc,has_free,has_jump,has_local,has_name)- Stack effect calculations (
num_popped,num_pushed) with oparg-dependent logicis_valid()range checks- TryFromPrimitive conversions with custom MarshalError constructor
The generated code follows Rust best practices (const methods where possible, exhaustive matching, proper repr annotations).
1-8: LGTM! Clean opcode wrapper design.The
Opcodeenum unifiesRealOpcodeandPseudoOpcodeunder a single type, enabling unified handling while preserving type safety. The public re-export ofRealOpcodeandPseudoOpcodemaintains flexibility for code that needs direct access to the underlying types.
32-48: LGTM! Macro reduces TryFrom boilerplate.The
impl_try_from!macro generates consistentTryFromimplementations for signed and unsigned integer types by first converting tou16and delegating to the existingTryFrom<u16>implementation. This reduces code duplication and ensures uniform error handling.
Remove unused code
3daec5a
coderabbitai
Bot
reviewed Oct 13, 2025
View reviewed changes
coderabbitai
Bot
left a comment
Copy link Copy Markdown
Contributor
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
Actionable comments posted: 2
♻️ Duplicate comments (1)scripts/gen_opcodes.py (1)🧹 Nitpick comments (4)
49-50: Avoid assigning lambdas to methods (E731); use named defs.Define helpers and assign them to
StackOffsetfor clarity and lint compliance. (Similar to prior feedback.)-StackOffset.pop = lambda self, item: self.popped.append(_var_size(item)) -StackOffset.push = lambda self, item: self.pushed.append(_var_size(item)) +def _stackoffset_pop(self, item): + self.popped.append(_var_size(item)) + +def _stackoffset_push(self, item): + self.pushed.append(_var_size(item)) + +StackOffset.pop = _stackoffset_pop +StackOffset.push = _stackoffset_push
scripts/gen_opcodes.py (3)📜 Review detailscompiler/core/src/opcodes.rs (1)
14-21: Make CPython path configurable and fail with a clear message.Hardcoding
CPYTHON_PATHrisks opaque failures. Allow an env override and validate the path.-CPYTHON_PATH = ( - pathlib.Path(__file__).parents[2] / "cpython" -) +CPYTHON_PATH = pathlib.Path( + sys.environ.get("RUSTPYTHON_CPYTHON_PATH", "") +) or (pathlib.Path(__file__).parents[2] / "cpython") + +if not CPYTHON_PATH.exists(): + sys.exit(f"cpython checkout not found at: {CPYTHON_PATH}. " + "Set RUSTPYTHON_CPYTHON_PATH to override.")
253-256: Escape and order tokens in the regex replacement.Unescaped alternation can mis-match; ordering prevents short-name shadowing.
- replacements = {name: enum_variant_name(name) for name in analysis.opmap} - inner_pattern = "|".join(replacements) + replacements = {name: enum_variant_name(name) for name in analysis.opmap} + # Longest-first to avoid partial matches (e.g., LOAD vs LOAD_CONST) + names = sorted(replacements.keys(), key=len, reverse=True) + inner_pattern = "|".join(re.escape(n) for n in names) pattern = re.compile(rf"\b({inner_pattern})\b") out = pattern.sub(lambda m: replacements[m.group(0)], out)
258-259: Surface formatting failures and keep output consistent.Use
check=Trueto fail fast; ifcargois missing, at least warn.- print("Running `cargo fmt`") - subprocess.run(["cargo", "fmt"], cwd=ROOT) + print("Running `cargo fmt`") + try: + subprocess.run(["cargo", "fmt"], cwd=ROOT, check=True) + except Exception as e: + print(f"Warning: `cargo fmt` failed: {e}", file=sys.stderr)
227-299: Optional: simplify deopt for readability.Current
Some(match ... _ => return None)works; consider a directmatchto avoid early return inside the expression.- pub const fn deopt(&self) -> Option<Self> { - Some(match *self { + pub const fn deopt(&self) -> Option<Self> { + match *self { /* arms unchanged */ - _ => return None, - }) + _ => return None, + } }
Configuration used : Path: .coderabbit.yml
Review profile : CHILL
Plan : Pro
📥 CommitsReviewing files that changed from the base of the PR and between aca4508 and 3daec5a .
📒 Files selected for processing (2)compiler/core/src/opcodes.rs(1 hunks)scripts/gen_opcodes.py(1 hunks)
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Format Rust code with the default rustfmt style (runcargo fmt)
Run clippy and fix any warnings or lints introduced by your changes
Follow Rust best practices for error handling and memory management
Files:
compiler/core/src/opcodes.rs
stdlib/src/opcode.rs (7)scripts/gen_opcodes.py (2)
has_arg(136-138)has_const(141-143)has_exc(166-168)has_free(156-158)has_jump(151-153)has_local(161-163)has_name(146-148)
compiler/core/src/marshal.rs (1)🪛 Flake8 (7.3.0) scripts/gen_opcodes.pycompiler/core/src/opcodes.rs (1)
instructions(188-195)
deopt(228-299)
[error] 12-12: 'collections.abc.Iterable' imported but unused
(F401)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Ensure compilation on various targets
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Check the WASM package and demo
- GitHub Check: Check Rust code with rustfmt and clippy
compiler/core/src/opcodes.rs (2)Comment thread scripts/gen_opcodes.py
9-12: Good use of num_enum for fallible conversions.Derive + error_type hook is clean and const-friendly. LGTM.
988-1005: Approve PseudoOpcode derivation; num_enum dependency confirmed
repr(u16) + TryFromPrimitive matches design and num_enum is declared in Cargo.toml.
| import typing | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from collections.abc import Iterable, Iterator |
| const fn new_invalid_bytecode<T: num_traits::int::PrimInt>(_: T) -> MarshalError {{ | ||
| MarshalError::InvalidBytecode | ||
| }} |
typo
9436354
youknowone commented Oct 14, 2025
Copy link Copy Markdown
Member
|
Somehow, our Instruction is a u8 backed type because Arg is a zero-sized type. This is a trick to type the argument but not actually owning a value. |
ShaharNaveh commented Oct 14, 2025 · edited Loading
Copy link Copy Markdown
Contributor
Author
So, if I understand it correctly we must keep |
youknowone commented Oct 15, 2025
Copy link Copy Markdown
Member
|
I am currently not perfectly understanding how they are different in Python. |
ShaharNaveh commented Oct 15, 2025
Copy link Copy Markdown
Contributor
Author
Some opcodes that we have does not exist in Cpython & Some were changed. I thought it would be better if we would use CPython
Completely agree. Using Rust safety features is better than forcing rust to behave like C. For now, I'll convert this PR to draft and try to see if I'm able to auto-generate the |
youknowone commented Oct 15, 2025
Copy link Copy Markdown
Member
|
I agree it will be great if dis.py perfectly work.
I agree here too. But it may not be always possible especially when CPython is updated. Because CPython doesn't guarantee bytecode compatibility, every python version can have different bytecode. We can patch bytecode to be aligned to CPython. But if we have incompatibility, |
ShaharNaveh commented Oct 15, 2025
Copy link Copy Markdown
Contributor
Author
Sounds good. I'll invest more time to see how we can auto generate the |
youknowone commented Oct 23, 2025
Copy link Copy Markdown
Member
|
Now Python has exception table for code object, which is related to the newer except instructions. |
ShaharNaveh commented Oct 23, 2025
Copy link Copy Markdown
Contributor
Author
Are we aiming now for Python 3.14? what are the steps to achieve it? Updating the Instructions can't be only thing needed, right? |
youknowone commented Oct 23, 2025
Copy link Copy Markdown
Member
|
If you are interested in 3.14 support, #5484 is the guideline |
youknowone commented Oct 24, 2025
Copy link Copy Markdown
Member
|
Oh, to prevent miscommunication, the exception table is not related to 3.14. It is probably a 3.11 feature which we didn't implement yet. |
ShaharNaveh commented Oct 24, 2025
Copy link Copy Markdown
Contributor
Author
No worries. I think that this PR will stay open for a while as the changes required to use the updated instructions is very large. meanwhile I'll open small PRs to get us there slowly but surely |
Instruction variants + Ensure each holds a singular oparg type
#6312
Closed
youknowone commented Aug 2, 2026
Copy link Copy Markdown
Member
|
@ShaharNaveh is this replaced by your recent refactorings? |
ShaharNaveh commented Aug 2, 2026
Copy link Copy Markdown
Contributor
Author
yup! |