@@ -12,11 +12,15 @@ use crate::{
1212 marshal:: MarshalError ,
1313} ;
1414
15- /// A Single bytecode instruction.
16- /// All instructions above 44 have an argument.
15+ /// A Single bytecode instruction that are executed by the VM.
16+ ///
17+ /// Currently aligned with CPython 3.13.
18+ ///
19+ /// ## See also
20+ /// - [CPython opcode IDs](https://github.com/python/cpython/blob/627894459a84be3488a1789919679c997056a03c/Include/opcode_ids.h)
1721#[ derive( Clone , Copy , Debug ) ]
1822#[ repr( u8 ) ]
19- pub enum RealInstruction {
23+ pub enum Instruction {
2024 // ==================== No-argument instructions (opcode < 44) ====================
2125 Cache = 0 , // Placeholder
2226 BeforeAsyncWith = 1 ,
@@ -275,20 +279,21 @@ pub enum RealInstruction {
275279 JumpIfNotExcMatch ( Arg < Label > ) = 131 ,
276280 SetExcInfo = 134 ,
277281 Subscript = 135 ,
282+ // Pseudos (needs to be moved to `PseudoInstruction` enum.
278283 LoadClosure ( Arg < NameIdx > ) = 253 , // TODO: Move to pseudos
279284}
280285
281- const _: ( ) = assert ! ( mem:: size_of:: <RealInstruction >( ) == 1 ) ;
286+ const _: ( ) = assert ! ( mem:: size_of:: <Instruction >( ) == 1 ) ;
282287
283- impl From < RealInstruction > for u8 {
288+ impl From < Instruction > for u8 {
284289 #[ inline]
285- fn from ( ins : RealInstruction ) -> Self {
290+ fn from ( ins : Instruction ) -> Self {
286291 // SAFETY: there's no padding bits
287- unsafe { core :: mem:: transmute :: < RealInstruction , Self > ( ins) }
292+ unsafe { mem:: transmute :: < Instruction , Self > ( ins) }
288293 }
289294}
290295
291- impl TryFrom < u8 > for RealInstruction {
296+ impl TryFrom < u8 > for Instruction {
292297 type Error = MarshalError ;
293298
294299 #[ inline]
@@ -341,14 +346,14 @@ impl TryFrom<u8> for RealInstruction {
341346 || value == load_closure
342347 || custom_ops. contains ( & value)
343348 {
344- Ok ( unsafe { core :: mem:: transmute :: < u8 , Self > ( value) } )
349+ Ok ( unsafe { mem:: transmute :: < u8 , Self > ( value) } )
345350 } else {
346351 Err ( Self :: Error :: InvalidBytecode )
347352 }
348353 }
349354}
350355
351- impl InstructionMetadata for RealInstruction {
356+ impl InstructionMetadata for Instruction {
352357 #[ inline]
353358 fn label_arg ( & self ) -> Option < Arg < Label > > {
354359 match self {
@@ -783,6 +788,7 @@ impl InstructionMetadata for RealInstruction {
783788 }
784789}
785790
791+ /// Instructions used by the compiler. They are not executed by the VM.
786792#[ derive( Clone , Copy , Debug ) ]
787793#[ repr( u16 ) ]
788794pub enum PseudoInstruction {
@@ -821,7 +827,7 @@ impl From<PseudoInstruction> for u16 {
821827 #[ inline]
822828 fn from ( ins : PseudoInstruction ) -> Self {
823829 // SAFETY: there's no padding bits
824- unsafe { core :: mem:: transmute :: < PseudoInstruction , Self > ( ins) }
830+ unsafe { mem:: transmute :: < PseudoInstruction , Self > ( ins) }
825831 }
826832}
827833
@@ -836,7 +842,7 @@ impl TryFrom<u16> for PseudoInstruction {
836842 let end = u16:: from ( Self :: StoreFastMaybeNull ) ;
837843
838844 if ( start..=end) . contains ( & value) {
839- Ok ( unsafe { core :: mem:: transmute :: < u16 , Self > ( value) } )
845+ Ok ( unsafe { mem:: transmute :: < u16 , Self > ( value) } )
840846 } else {
841847 Err ( Self :: Error :: InvalidBytecode )
842848 }
@@ -886,32 +892,32 @@ impl InstructionMetadata for PseudoInstruction {
886892}
887893
888894#[ derive( Clone , Copy , Debug ) ]
889- pub enum Instruction {
890- Real ( RealInstruction ) ,
895+ pub enum AnyInstruction {
896+ Real ( Instruction ) ,
891897 Pseudo ( PseudoInstruction ) ,
892898}
893899
894- impl From < RealInstruction > for Instruction {
895- fn from ( value : RealInstruction ) -> Self {
900+ impl From < Instruction > for AnyInstruction {
901+ fn from ( value : Instruction ) -> Self {
896902 Self :: Real ( value)
897903 }
898904}
899905
900- impl From < PseudoInstruction > for Instruction {
906+ impl From < PseudoInstruction > for AnyInstruction {
901907 fn from ( value : PseudoInstruction ) -> Self {
902908 Self :: Pseudo ( value)
903909 }
904910}
905911
906- impl TryFrom < u8 > for Instruction {
912+ impl TryFrom < u8 > for AnyInstruction {
907913 type Error = MarshalError ;
908914
909915 fn try_from ( value : u8 ) -> Result < Self , Self :: Error > {
910- Ok ( RealInstruction :: try_from ( value) ?. into ( ) )
916+ Ok ( Instruction :: try_from ( value) ?. into ( ) )
911917 }
912918}
913919
914- impl TryFrom < u16 > for Instruction {
920+ impl TryFrom < u16 > for AnyInstruction {
915921 type Error = MarshalError ;
916922
917923 fn try_from ( value : u16 ) -> Result < Self , Self :: Error > {
@@ -933,7 +939,7 @@ macro_rules! inst_either {
933939 } ;
934940}
935941
936- impl InstructionMetadata for Instruction {
942+ impl InstructionMetadata for AnyInstruction {
937943 inst_either ! ( fn label_arg( & self ) -> Option <Arg <Label >>) ;
938944
939945 inst_either ! ( fn unconditional_branch( & self ) -> bool ) ;
@@ -951,9 +957,9 @@ impl InstructionMetadata for Instruction {
951957 ) -> fmt:: Result ) ;
952958}
953959
954- impl Instruction {
960+ impl AnyInstruction {
955961 /// Gets the inner value of [`Self::Real`].
956- pub const fn real ( self ) -> Option < RealInstruction > {
962+ pub const fn real ( self ) -> Option < Instruction > {
957963 match self {
958964 Self :: Real ( ins) => Some ( ins) ,
959965 _ => None ,
@@ -973,7 +979,7 @@ impl Instruction {
973979 /// # Panics
974980 ///
975981 /// If was called on something else other than [`Self::Real`].
976- pub const fn expect_real ( self ) -> RealInstruction {
982+ pub const fn expect_real ( self ) -> Instruction {
977983 self . real ( )
978984 . expect ( "Expected Instruction::Real, found Instruction::Pseudo" )
979985 }
@@ -998,8 +1004,8 @@ pub trait InstructionMetadata {
9981004 /// # Examples
9991005 ///
10001006 /// ```
1001- /// use rustpython_compiler_core::bytecode::{Arg, RealInstruction , InstructionMetadata};
1002- /// let jump_inst = RealInstruction ::JumpForward { target: Arg::marker() };
1007+ /// use rustpython_compiler_core::bytecode::{Arg, Instruction , InstructionMetadata};
1008+ /// let jump_inst = Instruction ::JumpForward { target: Arg::marker() };
10031009 /// assert!(jump_inst.unconditional_branch())
10041010 /// ```
10051011 fn unconditional_branch ( & self ) -> bool ;
@@ -1009,9 +1015,9 @@ pub trait InstructionMetadata {
10091015 /// # Examples
10101016 ///
10111017 /// ```
1012- /// use rustpython_compiler_core::bytecode::{Arg, RealInstruction , Label, InstructionMetadata};
1018+ /// use rustpython_compiler_core::bytecode::{Arg, Instruction , Label, InstructionMetadata};
10131019 /// let (target, jump_arg) = Arg::new(Label(0xF));
1014- /// let jump_instruction = RealInstruction ::JumpForward { target };
1020+ /// let jump_instruction = Instruction ::JumpForward { target };
10151021 /// assert_eq!(jump_instruction.stack_effect(jump_arg, true), 0);
10161022 /// ```
10171023 fn stack_effect ( & self , arg : OpArg , jump : bool ) -> i32 ;
0 commit comments