@@ -462,7 +462,7 @@ impl TryFrom<u8> for Instruction {
462462impl Instruction {
463463 /// Returns `true` if this is any instrumented opcode
464464 /// (regular INSTRUMENTED_*, INSTRUMENTED_LINE, or INSTRUMENTED_INSTRUCTION).
465- pub fn is_instrumented ( self ) -> bool {
465+ pub const fn is_instrumented ( self ) -> bool {
466466 self . to_base ( ) . is_some ( )
467467 || matches ! ( self , Self :: InstrumentedLine | Self :: InstrumentedInstruction )
468468 }
@@ -509,7 +509,7 @@ impl Instruction {
509509 ///
510510 /// The returned base opcode uses `Arg::marker()` for typed fields —
511511 /// only the opcode byte matters since `replace_op` preserves the arg byte.
512- pub fn to_base ( self ) -> Option < Self > {
512+ pub const fn to_base ( self ) -> Option < Self > {
513513 Some ( match self {
514514 Self :: InstrumentedResume => Self :: Resume {
515515 context : Arg :: marker ( ) ,
@@ -555,11 +555,10 @@ impl Instruction {
555555 _ => return None ,
556556 } )
557557 }
558-
559- /// Map a specialized opcode back to its adaptive (base) variant.
558+ /// Map a specialized or instrumented opcode back to its adaptive (base) variant.
560559 /// `_PyOpcode_Deopt`
561- pub fn deoptimize ( self ) -> Self {
562- match self {
560+ pub const fn deopt ( self ) -> Option < Self > {
561+ Some ( match self {
563562 // RESUME specializations
564563 Self :: ResumeCheck => Self :: Resume {
565564 context : Arg :: marker ( ) ,
@@ -678,17 +677,27 @@ impl Instruction {
678677 Self :: CallKwBoundMethod | Self :: CallKwPy | Self :: CallKwNonPy => Self :: CallKw {
679678 argc : Arg :: marker ( ) ,
680679 } ,
681- // Instrumented opcodes map back to their base
682- _ => match self . to_base ( ) {
683- Some ( base) => base,
684- None => self ,
685- } ,
680+ _ => return None ,
681+ } )
682+ }
683+
684+ /// Map a specialized opcode back to its adaptive (base) variant.
685+ pub const fn deoptimize ( self ) -> Self {
686+ match self . deopt ( ) {
687+ Some ( v) => v,
688+ None => {
689+ // Instrumented opcodes map back to their base
690+ match self . to_base ( ) {
691+ Some ( v) => v,
692+ None => self ,
693+ }
694+ }
686695 }
687696 }
688697
689698 /// Number of CACHE code units that follow this instruction.
690699 /// _PyOpcode_Caches
691- pub fn cache_entries ( self ) -> usize {
700+ pub const fn cache_entries ( self ) -> usize {
692701 match self {
693702 // LOAD_ATTR: 9 cache entries
694703 Self :: LoadAttr { .. }
0 commit comments