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
Next Next commit
align psuedo ops
  • Loading branch information
youknowone committed Jan 22, 2026
commit 96a21119db27bd42dc87fdb7b99b5e62ed16716e
40 changes: 21 additions & 19 deletions Lib/_opcode_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@
'BINARY_OP_EXTEND': 132,
'BINARY_OP_MULTIPLY_FLOAT': 133,
'BINARY_OP_MULTIPLY_INT': 134,
'BINARY_SUBSCR_DICT': 135,
'BINARY_SUBSCR_GETITEM': 136,
'BINARY_SUBSCR_LIST_INT': 137,
'BINARY_SUBSCR_LIST_SLICE': 138,
'BINARY_SUBSCR_STR_INT': 139,
'BINARY_SUBSCR_TUPLE_INT': 140,
'BINARY_OP_SUBSCR_DICT': 135,
'BINARY_OP_SUBSCR_GETITEM': 136,
'BINARY_OP_SUBSCR_LIST_INT': 137,
'BINARY_OP_SUBSCR_LIST_SLICE': 138,
'BINARY_OP_SUBSCR_STR_INT': 139,
'BINARY_OP_SUBSCR_TUPLE_INT': 140,
'BINARY_OP_SUBTRACT_FLOAT': 141,
'BINARY_OP_SUBTRACT_INT': 142,
'CALL_ALLOC_AND_ENTER_INIT': 143,
Expand Down Expand Up @@ -234,19 +234,21 @@
'INSTRUMENTED_JUMP_BACKWARD': 253,
'INSTRUMENTED_LINE': 254,
'ENTER_EXECUTOR': 255,
'JUMP': 256,
'JUMP_NO_INTERRUPT': 257,
'RESERVED_258': 258,
'LOAD_ATTR_METHOD': 259,
'LOAD_SUPER_METHOD': 260,
'LOAD_ZERO_SUPER_ATTR': 261,
'LOAD_ZERO_SUPER_METHOD': 262,
'POP_BLOCK': 263,
'SETUP_CLEANUP': 264,
'SETUP_FINALLY': 265,
'SETUP_WITH': 266,
'STORE_FAST_MAYBE_NULL': 267,
'LOAD_CLOSURE': 268,
'ANNOTATIONS_PLACEHOLDER': 256,
'JUMP': 257,
'JUMP_IF_FALSE': 258,
'JUMP_IF_TRUE': 259,
'JUMP_NO_INTERRUPT': 260,
'LOAD_CLOSURE': 261,
'POP_BLOCK': 262,
'SETUP_CLEANUP': 263,
'SETUP_FINALLY': 264,
'SETUP_WITH': 265,
'STORE_FAST_MAYBE_NULL': 266,
'LOAD_ATTR_METHOD': 267,
'LOAD_SUPER_METHOD': 268,
'LOAD_ZERO_SUPER_ATTR': 269,
'LOAD_ZERO_SUPER_METHOD': 270,
}

# CPython 3.13 compatible: opcodes < 44 have no argument
Expand Down
8 changes: 5 additions & 3 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,14 @@ impl CodeInfo {
PseudoInstruction::Jump { .. } => {
// PseudoInstruction::Jump instructions are handled later
}
PseudoInstruction::JumpNoInterrupt { .. }
| PseudoInstruction::Reserved258
PseudoInstruction::AnnotationsPlaceholder
| PseudoInstruction::JumpIfFalse { .. }
| PseudoInstruction::JumpIfTrue { .. }
| PseudoInstruction::JumpNoInterrupt { .. }
| PseudoInstruction::SetupCleanup
| PseudoInstruction::SetupFinally
| PseudoInstruction::SetupWith
| PseudoInstruction::StoreFastMaybeNull => {
| PseudoInstruction::StoreFastMaybeNull(_) => {
unimplemented!("Got a placeholder pseudo instruction ({instr:?})")
}
}
Expand Down
92 changes: 53 additions & 39 deletions crates/compiler-core/src/bytecode/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ pub enum Instruction {
BinaryOpExtend = 132, // Placeholder
BinaryOpMultiplyFloat = 133, // Placeholder
BinaryOpMultiplyInt = 134, // Placeholder
BinarySubscrDict = 135, // Placeholder
BinarySubscrGetitem = 136, // Placeholder
BinarySubscrListInt = 137, // Placeholder
BinarySubscrListSlice = 138, // Placeholder
BinarySubscrStrInt = 139, // Placeholder
BinarySubscrTupleInt = 140, // Placeholder
BinaryOpSubscrDict = 135, // Placeholder
BinaryOpSubscrGetitem = 136, // Placeholder
BinaryOpSubscrListInt = 137, // Placeholder
BinaryOpSubscrListSlice = 138, // Placeholder
BinaryOpSubscrStrInt = 139, // Placeholder
BinaryOpSubscrTupleInt = 140, // Placeholder
BinaryOpSubtractFloat = 141, // Placeholder
BinaryOpSubtractInt = 142, // Placeholder
CallAllocAndEnterInit = 143, // Placeholder
Expand Down Expand Up @@ -571,7 +571,7 @@ impl InstructionMetadata for Instruction {
let UnpackExArgs { before, after } = args.get(arg);
-1 + before as i32 + 1 + after as i32
}
Self::PopExcept => 0,
Self::PopExcept => -1,
Self::PopIter => -1,
Self::GetAwaitable => 0,
Self::GetAIter => 0,
Expand Down Expand Up @@ -624,12 +624,12 @@ impl InstructionMetadata for Instruction {
Self::BinaryOpMultiplyInt => 0,
Self::BinaryOpSubtractFloat => 0,
Self::BinaryOpSubtractInt => 0,
Self::BinarySubscrDict => 0,
Self::BinarySubscrGetitem => 0,
Self::BinarySubscrListInt => 0,
Self::BinarySubscrListSlice => 0,
Self::BinarySubscrStrInt => 0,
Self::BinarySubscrTupleInt => 0,
Self::BinaryOpSubscrDict => 0,
Self::BinaryOpSubscrGetitem => 0,
Self::BinaryOpSubscrListInt => 0,
Self::BinaryOpSubscrListSlice => 0,
Self::BinaryOpSubscrStrInt => 0,
Self::BinaryOpSubscrTupleInt => 0,
Self::CallAllocAndEnterInit => 0,
Self::CallBoundMethodExactArgs => 0,
Self::CallBoundMethodGeneral => 0,
Expand Down Expand Up @@ -917,37 +917,48 @@ impl InstructionMetadata for Instruction {
}

/// Instructions used by the compiler. They are not executed by the VM.
///
/// CPython 3.14.2 aligned (256-266), RustPython-specific variants start at 267.
#[derive(Clone, Copy, Debug)]
#[repr(u16)]
pub enum PseudoInstruction {
// CPython 3.14.2 pseudo instructions (256-266)
AnnotationsPlaceholder = 256,
Jump {
target: Arg<Label>,
} = 256,
} = 257,
JumpIfFalse {
target: Arg<Label>,
} = 258,
JumpIfTrue {
target: Arg<Label>,
} = 259,
JumpNoInterrupt {
target: Arg<Label>,
} = 257, // Placeholder
Reserved258 = 258,
} = 260,
LoadClosure(Arg<NameIdx>) = 261,
PopBlock = 262,
SetupCleanup = 263,
SetupFinally = 264,
SetupWith = 265,
StoreFastMaybeNull(Arg<NameIdx>) = 266,

// RustPython-specific pseudo instructions (267+)
LoadAttrMethod {
idx: Arg<NameIdx>,
} = 259,
} = 267,
// "Zero" variants are for 0-arg super() calls (has_class=false).
// Non-"Zero" variants are for 2-arg super(cls, self) calls (has_class=true).
/// 2-arg super(cls, self).method() - has_class=true, load_method=true
LoadSuperMethod {
idx: Arg<NameIdx>,
} = 260,
} = 268,
LoadZeroSuperAttr {
idx: Arg<NameIdx>,
} = 261,
} = 269,
LoadZeroSuperMethod {
idx: Arg<NameIdx>,
} = 262,
PopBlock = 263,
SetupCleanup = 264, // Placeholder
SetupFinally = 265, // Placeholder
SetupWith = 266, // Placeholder
StoreFastMaybeNull = 267, // Placeholder
LoadClosure(Arg<NameIdx>) = 268,
} = 270,
}

const _: () = assert!(mem::size_of::<PseudoInstruction>() == 2);
Expand All @@ -965,10 +976,8 @@ impl TryFrom<u16> for PseudoInstruction {

#[inline]
fn try_from(value: u16) -> Result<Self, MarshalError> {
let start = u16::from(Self::Jump {
target: Arg::marker(),
});
let end = u16::from(Self::LoadClosure(Arg::marker()));
let start = u16::from(Self::AnnotationsPlaceholder);
let end = u16::from(Self::LoadZeroSuperMethod { idx: Arg::marker() });

if (start..=end).contains(&value) {
Ok(unsafe { mem::transmute::<u16, Self>(value) })
Expand All @@ -981,30 +990,35 @@ impl TryFrom<u16> for PseudoInstruction {
impl InstructionMetadata for PseudoInstruction {
fn label_arg(&self) -> Option<Arg<Label>> {
match self {
Self::Jump { target: l } => Some(*l),
Self::Jump { target: l }
| Self::JumpIfFalse { target: l }
| Self::JumpIfTrue { target: l }
| Self::JumpNoInterrupt { target: l } => Some(*l),
_ => None,
}
}

fn unconditional_branch(&self) -> bool {
matches!(self, Self::Jump { .. },)
matches!(self, Self::Jump { .. } | Self::JumpNoInterrupt { .. })
}

fn stack_effect(&self, _arg: OpArg, _jump: bool) -> i32 {
match self {
Self::AnnotationsPlaceholder => 0,
Self::Jump { .. } => 0,
Self::JumpIfFalse { .. } => 0, // peek, don't pop: COPY + TO_BOOL + POP_JUMP_IF_FALSE
Self::JumpIfTrue { .. } => 0, // peek, don't pop: COPY + TO_BOOL + POP_JUMP_IF_TRUE
Self::JumpNoInterrupt { .. } => 0,
Self::LoadAttrMethod { .. } => 1, // pop obj, push method + self_or_null
Self::LoadSuperMethod { .. } => -3 + 2, // pop 3, push [method, self_or_null]
Self::LoadZeroSuperAttr { .. } => -3 + 1, // pop 3, push [attr]
Self::LoadZeroSuperMethod { .. } => -3 + 2, // pop 3, push [method, self_or_null]
Self::LoadClosure(_) => 1,
Self::PopBlock => 0,
Self::SetupCleanup => 0,
Self::SetupFinally => 0,
Self::SetupWith => 0,
Self::StoreFastMaybeNull => 0,
Self::Reserved258 => 0,
Self::LoadClosure(_) => 1,
Self::StoreFastMaybeNull(_) => -1,
Self::LoadAttrMethod { .. } => 1, // pop obj, push method + self_or_null
Self::LoadSuperMethod { .. } => -3 + 2, // pop 3, push [method, self_or_null]
Self::LoadZeroSuperAttr { .. } => -3 + 1, // pop 3, push [attr]
Self::LoadZeroSuperMethod { .. } => -3 + 2, // pop 3, push [method, self_or_null]
}
}

Expand Down