Skip to content
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
fad76c6
save
ShaharNaveh Jan 11, 2026
af70361
Merge remote-tracking branch 'upstream/main' into bytecode-pseudo-opc…
ShaharNaveh Jan 11, 2026
ea53cec
save
ShaharNaveh Jan 11, 2026
1ca20a9
Merge remote-tracking branch 'upstream/main' into bytecode-pseudo-opc…
ShaharNaveh Jan 12, 2026
6f09ebe
Base compiler-core
ShaharNaveh Jan 12, 2026
6805175
save
ShaharNaveh Jan 12, 2026
046e17a
Codegen compile
ShaharNaveh Jan 12, 2026
d627623
Move LoadCloure back to RealInstruction
ShaharNaveh Jan 12, 2026
697fe41
Fix opcode.rs
ShaharNaveh Jan 12, 2026
11fc63f
Merge remote-tracking branch 'upstream/main' into bytecode-pseudo-opc…
ShaharNaveh Jan 13, 2026
33b4554
Fix `TryFrom<u8>` for RealInstruction
ShaharNaveh Jan 13, 2026
6140186
Fix script
ShaharNaveh Jan 13, 2026
7acdc1a
Fix jit
ShaharNaveh Jan 13, 2026
0fad43f
Fix typo
ShaharNaveh Jan 13, 2026
24f5161
Fix typo
ShaharNaveh Jan 13, 2026
6e91410
Remove popblock
ShaharNaveh Jan 13, 2026
8751953
Fix docs
ShaharNaveh Jan 13, 2026
8b73441
Fix more docs
ShaharNaveh Jan 13, 2026
d6e46f8
ok word `argty`
ShaharNaveh Jan 13, 2026
0872cb6
Revert "ok word `argty`"
ShaharNaveh Jan 13, 2026
0c7880d
Rename argty -> arg_ty
ShaharNaveh Jan 13, 2026
1a5c72b
Merge remote-tracking branch 'upstream/main' into bytecode-pseudo-opc…
ShaharNaveh Jan 13, 2026
761fa4a
Simplify `emit` macro
ShaharNaveh Jan 13, 2026
4eed137
Trigger CI
ShaharNaveh Jan 13, 2026
8f777d2
Trigger CI
ShaharNaveh Jan 13, 2026
bff25eb
Trigger CI
ShaharNaveh Jan 13, 2026
91c01e6
Merge remote-tracking branch 'upstream/main' into bytecode-pseudo-opc…
ShaharNaveh Jan 14, 2026
f6a0b26
Merge remote-tracking branch 'upstream/main' into bytecode-pseudo-opc…
ShaharNaveh Jan 14, 2026
118ecf3
Move docs
ShaharNaveh Jan 14, 2026
c517585
Fix oparg docs
ShaharNaveh Jan 14, 2026
78958aa
Revert "Move docs"
ShaharNaveh Jan 14, 2026
a405cca
Merge remote-tracking branch 'upstream/main' into bytecode-pseudo-opc…
ShaharNaveh Jan 14, 2026
8ae98b2
Remove `Eq` & `ParitalEq` for RealInstruction
ShaharNaveh Jan 14, 2026
1453726
Simplify `match` arms
ShaharNaveh Jan 14, 2026
f207284
Trigger CI
ShaharNaveh Jan 14, 2026
a1555dc
Trigger CI
ShaharNaveh Jan 14, 2026
c443f8e
Remove `repr(u16)` for `Instruction`
ShaharNaveh Jan 14, 2026
453212e
Fix doc
ShaharNaveh Jan 14, 2026
1f8722a
Rename the enums
ShaharNaveh Jan 14, 2026
0096a65
Restore fmt_dis for LoadClousure
ShaharNaveh Jan 14, 2026
045d576
Fix script
ShaharNaveh Jan 14, 2026
b00f81f
Fix commet
ShaharNaveh Jan 14, 2026
4823d17
Merge remote-tracking branch 'upstream/main' into bytecode-pseudo-opc…
ShaharNaveh Jan 14, 2026
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
18 changes: 12 additions & 6 deletions Lib/_opcode_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,20 @@
'JUMP_IF_NOT_EXC_MATCH': 131,
'SET_EXC_INFO': 134,
'SUBSCRIPT': 135,
'LOAD_SUPER_METHOD': 136,
'LOAD_ZERO_SUPER_ATTR': 137,
'LOAD_ZERO_SUPER_METHOD': 138,
'RESUME': 149,
'JUMP': 252,
'LOAD_CLOSURE': 253,
'LOAD_ATTR_METHOD': 254,
'POP_BLOCK': 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,
}

# CPython 3.13 compatible: opcodes < 44 have no argument
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test__opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def check_bool_function_result(self, func, ops, expected):
self.assertIsInstance(func(op), bool)
self.assertEqual(func(op), expected)

@unittest.expectedFailure # TODO: RUSTPYTHON; Only supporting u8 ATM
def test_invalid_opcodes(self):
invalid = [-100, -1, 255, 512, 513, 1000]
self.check_bool_function_result(_opcode.is_valid, invalid, False)
Expand Down
962 changes: 495 additions & 467 deletions crates/codegen/src/compile.rs

Large diffs are not rendered by default.

90 changes: 60 additions & 30 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use core::ops;

use crate::{IndexMap, IndexSet, error::InternalError};

use rustpython_compiler_core::{
OneIndexed, SourceLocation,
bytecode::{
Arg, CodeFlags, CodeObject, CodeUnit, CodeUnits, ConstantData, ExceptionTableEntry,
InstrDisplayContext, Instruction, Label, OpArg, PyCodeLocationInfoKind,
encode_exception_table, encode_load_attr_arg, encode_load_super_attr_arg,
InstrDisplayContext, Instruction, InstructionMetadata, Label, OpArg, PseudoInstruction,
PyCodeLocationInfoKind, RealInstruction, encode_exception_table, encode_load_attr_arg,
encode_load_super_attr_arg,
},
varint::{write_signed_varint, write_varint},
};
Expand Down Expand Up @@ -195,48 +197,74 @@ impl CodeInfo {
.filter(|b| b.next != BlockIdx::NULL || !b.instructions.is_empty())
{
for info in &mut block.instructions {
match info.instr {
// Special case for:
// - `RealInstruction::LoadAttr`
// - `RealInstruction::LoadSuperAttr`

if let Some(instr) = info.instr.real() {
match instr {
// LOAD_ATTR → encode with method flag=0
RealInstruction::LoadAttr { idx } => {
let encoded = encode_load_attr_arg(idx.get(info.arg), false);
info.arg = OpArg(encoded);
info.instr = RealInstruction::LoadAttr { idx: Arg::marker() }.into();
}
// LOAD_SUPER_ATTR → encode with flags=0b10 (method=0, class=1)
RealInstruction::LoadSuperAttr { arg: idx } => {
let encoded =
encode_load_super_attr_arg(idx.get(info.arg), false, true);
info.arg = OpArg(encoded);
info.instr =
RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
}
_ => {}
}

continue;
}

let instr = info.instr.expect_pseudo();

match instr {
// LOAD_ATTR_METHOD pseudo → LOAD_ATTR (with method flag=1)
Instruction::LoadAttrMethod { idx } => {
PseudoInstruction::LoadAttrMethod { idx } => {
let encoded = encode_load_attr_arg(idx.get(info.arg), true);
info.arg = OpArg(encoded);
info.instr = Instruction::LoadAttr { idx: Arg::marker() };
}
// LOAD_ATTR → encode with method flag=0
Instruction::LoadAttr { idx } => {
let encoded = encode_load_attr_arg(idx.get(info.arg), false);
info.arg = OpArg(encoded);
info.instr = Instruction::LoadAttr { idx: Arg::marker() };
info.instr = RealInstruction::LoadAttr { idx: Arg::marker() }.into();
}
// POP_BLOCK pseudo → NOP
Instruction::PopBlock => {
info.instr = Instruction::Nop;
PseudoInstruction::PopBlock => {
info.instr = RealInstruction::Nop.into();
}
// LOAD_SUPER_METHOD pseudo → LOAD_SUPER_ATTR (flags=0b11: method=1, class=1)
Instruction::LoadSuperMethod { idx } => {
PseudoInstruction::LoadSuperMethod { idx } => {
let encoded = encode_load_super_attr_arg(idx.get(info.arg), true, true);
info.arg = OpArg(encoded);
info.instr = Instruction::LoadSuperAttr { arg: Arg::marker() };
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
}
// LOAD_ZERO_SUPER_ATTR pseudo → LOAD_SUPER_ATTR (flags=0b00: method=0, class=0)
Instruction::LoadZeroSuperAttr { idx } => {
PseudoInstruction::LoadZeroSuperAttr { idx } => {
let encoded = encode_load_super_attr_arg(idx.get(info.arg), false, false);
info.arg = OpArg(encoded);
info.instr = Instruction::LoadSuperAttr { arg: Arg::marker() };
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
}
// LOAD_ZERO_SUPER_METHOD pseudo → LOAD_SUPER_ATTR (flags=0b01: method=1, class=0)
Instruction::LoadZeroSuperMethod { idx } => {
PseudoInstruction::LoadZeroSuperMethod { idx } => {
let encoded = encode_load_super_attr_arg(idx.get(info.arg), true, false);
info.arg = OpArg(encoded);
info.instr = Instruction::LoadSuperAttr { arg: Arg::marker() };
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
}
// LOAD_SUPER_ATTR → encode with flags=0b10 (method=0, class=1)
Instruction::LoadSuperAttr { arg: idx } => {
let encoded = encode_load_super_attr_arg(idx.get(info.arg), false, true);
info.arg = OpArg(encoded);
info.instr = Instruction::LoadSuperAttr { arg: Arg::marker() };
PseudoInstruction::Jump { .. } => {
// PseudoInstruction::Jump instructions are handled later
}
PseudoInstruction::JumpNoInterrupt { .. }
| PseudoInstruction::Reserved258
| PseudoInstruction::SetupCleanup
| PseudoInstruction::SetupFinally
| PseudoInstruction::SetupWith
| PseudoInstruction::StoreFastMaybeNull => {
unimplemented!("Got a placeholder pseudo instruction ({instr:?})")
}
Comment on lines +259 to 266
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.

⚠️ Potential issue | 🟠 Major

JumpNoInterrupt and other placeholders will panic at runtime.

If any of these placeholder pseudo instructions are emitted during compilation, the code will panic with unimplemented!(). Notably, JumpNoInterrupt is defined with a target field but is not handled for conversion.

Consider either:

  1. Implementing conversion for JumpNoInterrupt (similar to Jump at lines 307-323)
  2. Ensuring these instructions are never emitted by the compiler
🐛 Proposed fix for JumpNoInterrupt

Add handling in the match at lines 257-267 and in the JUMP conversion block:

                     PseudoInstruction::Jump { .. } => {
                         // PseudoInstruction::Jump instructions are handled later
                     }
-                    PseudoInstruction::JumpNoInterrupt { .. }
+                    PseudoInstruction::JumpNoInterrupt { .. } => {
+                        // PseudoInstruction::JumpNoInterrupt instructions are handled later
+                    }
                     | PseudoInstruction::Reserved258

And update the conversion block (lines 307-323) to handle JumpNoInterrupt:

                     let op = match info.instr {
                         Instruction::Pseudo(PseudoInstruction::Jump { .. })
+                            | Instruction::Pseudo(PseudoInstruction::JumpNoInterrupt { .. })
                             if target != BlockIdx::NULL =>
                         {
                             let target_offset = block_to_offset[target.idx()].0;
                             if target_offset > current_offset {
-                                RealInstruction::JumpForward {
+                                // For JumpNoInterrupt, use JumpBackwardNoInterrupt
+                                if matches!(info.instr, Instruction::Pseudo(PseudoInstruction::JumpNoInterrupt { .. })) {
+                                    // JumpNoInterrupt should only jump backward
+                                    RealInstruction::JumpBackwardNoInterrupt { target: Arg::marker() }
+                                } else {
+                                    RealInstruction::JumpForward { target: Arg::marker() }
+                                }
🤖 Prompt for AI Agents
In `@crates/codegen/src/ir.rs` around lines 260 - 267, The match arm that
currently groups PseudoInstruction::JumpNoInterrupt with placeholders and calls
unimplemented! must be fixed: add an explicit PseudoInstruction::JumpNoInterrupt
{ target } arm and implement its conversion similar to PseudoInstruction::Jump
(the conversion block around the existing Jump handling at lines ~307-323),
using the target field to produce the equivalent bytecode/IR for a
non-interrupting jump; update the Jump conversion logic to handle both
PseudoInstruction::Jump and PseudoInstruction::JumpNoInterrupt (e.g., by
branching on which enum variant and mapping to the same jump emission code path
but preserving the non-interrupt semantics), or alternatively ensure elsewhere
in the emitter that JumpNoInterrupt is never produced — but prefer adding the
explicit conversion for JumpNoInterrupt in the match and conversion block so it
no longer panics.

_ => {}
}
}
}
Expand Down Expand Up @@ -277,19 +305,21 @@ impl CodeInfo {

// Convert JUMP pseudo to real instructions (direction depends on offset)
let op = match info.instr {
Instruction::Jump { .. } if target != BlockIdx::NULL => {
Instruction::Pseudo(PseudoInstruction::Jump { .. })
if target != BlockIdx::NULL =>
{
let target_offset = block_to_offset[target.idx()].0;
if target_offset > current_offset {
Instruction::JumpForward {
RealInstruction::JumpForward {
target: Arg::marker(),
}
} else {
Instruction::JumpBackward {
RealInstruction::JumpBackward {
target: Arg::marker(),
}
}
}
other => other,
other => other.expect_real(),
};

let (extras, lo_arg) = info.arg.split();
Expand All @@ -299,7 +329,7 @@ impl CodeInfo {
));
instructions.extend(
extras
.map(|byte| CodeUnit::new(Instruction::ExtendedArg, byte))
.map(|byte| CodeUnit::new(RealInstruction::ExtendedArg, byte))
.chain([CodeUnit { op, arg: lo_arg }]),
);
current_offset += info.arg.instr_size() as u32;
Expand Down
24 changes: 7 additions & 17 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ use num_complex::Complex64;
use rustpython_wtf8::{Wtf8, Wtf8Buf};

pub use crate::bytecode::{
instruction::{Arg, Instruction, decode_load_super_attr_arg, encode_load_super_attr_arg},
instruction::{
Arg, Instruction, InstructionMetadata, PseudoInstruction, RealInstruction,
decode_load_attr_arg, decode_load_super_attr_arg, encode_load_attr_arg,
encode_load_super_attr_arg,
},
oparg::{
BinaryOperator, BuildSliceArgCount, ComparisonOperator, ConvertValueOparg,
IntrinsicFunction1, IntrinsicFunction2, Invert, Label, MakeFunctionFlags, NameIdx, OpArg,
Expand Down Expand Up @@ -96,20 +100,6 @@ pub fn find_exception_handler(table: &[u8], offset: u32) -> Option<ExceptionTabl
None
}

/// Encode LOAD_ATTR oparg: bit 0 = method flag, bits 1+ = name index.
#[inline]
pub const fn encode_load_attr_arg(name_idx: u32, is_method: bool) -> u32 {
(name_idx << 1) | (is_method as u32)
}

/// Decode LOAD_ATTR oparg: returns (name_idx, is_method).
#[inline]
pub const fn decode_load_attr_arg(oparg: u32) -> (u32, bool) {
let is_method = (oparg & 1) == 1;
let name_idx = oparg >> 1;
(name_idx, is_method)
}

/// CPython 3.11+ linetable location info codes
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
Expand Down Expand Up @@ -313,14 +303,14 @@ bitflags! {
#[derive(Copy, Clone)]
#[repr(C)]
pub struct CodeUnit {
pub op: Instruction,
pub op: RealInstruction,
pub arg: OpArgByte,
}

const _: () = assert!(mem::size_of::<CodeUnit>() == 2);

impl CodeUnit {
pub const fn new(op: Instruction, arg: OpArgByte) -> Self {
pub const fn new(op: RealInstruction, arg: OpArgByte) -> Self {
Self { op, arg }
}
}
Expand Down
Loading
Loading