Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Simplify match arms
  • Loading branch information
ShaharNaveh committed Jan 14, 2026
commit 1453726d4ff12281633588bf4e3397b46349abc8
62 changes: 44 additions & 18 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,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::Pseudo(PseudoInstruction::LoadAttrMethod { idx }) => {
PseudoInstruction::LoadAttrMethod { idx } => {
let encoded = encode_load_attr_arg(idx.get(info.arg), true);
info.arg = OpArg(encoded);
info.instr = RealInstruction::LoadAttr { idx: Arg::marker() }.into();
}
// LOAD_ATTR → encode with method flag=0
Instruction::Real(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();
}
// POP_BLOCK pseudo → NOP
Instruction::Pseudo(PseudoInstruction::PopBlock) => {
PseudoInstruction::PopBlock => {
info.instr = RealInstruction::Nop.into();
}
// LOAD_SUPER_METHOD pseudo → LOAD_SUPER_ATTR (flags=0b11: method=1, class=1)
Instruction::Pseudo(PseudoInstruction::LoadSuperMethod { idx }) => {
PseudoInstruction::LoadSuperMethod { idx } => {
let encoded = encode_load_super_attr_arg(idx.get(info.arg), true, true);
info.arg = OpArg(encoded);
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
}
// LOAD_ZERO_SUPER_ATTR pseudo → LOAD_SUPER_ATTR (flags=0b00: method=0, class=0)
Instruction::Pseudo(PseudoInstruction::LoadZeroSuperAttr { idx }) => {
PseudoInstruction::LoadZeroSuperAttr { idx } => {
let encoded = encode_load_super_attr_arg(idx.get(info.arg), false, false);
info.arg = OpArg(encoded);
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
}
// LOAD_ZERO_SUPER_METHOD pseudo → LOAD_SUPER_ATTR (flags=0b01: method=1, class=0)
Instruction::Pseudo(PseudoInstruction::LoadZeroSuperMethod { idx }) => {
PseudoInstruction::LoadZeroSuperMethod { idx } => {
let encoded = encode_load_super_attr_arg(idx.get(info.arg), true, false);
info.arg = OpArg(encoded);
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
}
// LOAD_SUPER_ATTR → encode with flags=0b10 (method=0, class=1)
Instruction::Real(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();
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
28 changes: 26 additions & 2 deletions crates/compiler-core/src/bytecode/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,17 +954,41 @@ impl InstructionMetadata for Instruction {
}

impl Instruction {
pub const fn as_real(self) -> Option<RealInstruction> {
/// Gets the inner value of [`Self::Real`].
pub const fn real(self) -> Option<RealInstruction> {
match self {
Self::Real(ins) => Some(ins),
_ => None,
}
}

/// Gets the inner value of [`Self::Pseudo`].
pub const fn pseudo(self) -> Option<PseudoInstruction> {
match self {
Self::Pseudo(ins) => Some(ins),
_ => None,
}
}

/// Same as [`Self::real`] but panics if wasn't called on [`Self::Real`].
///
/// # Panics
///
/// If was called on something else other than [`Self::Real`].
pub const fn expect_real(self) -> RealInstruction {
self.as_real()
self.real()
.expect("Expected Instruction::Real, found Instruction::Pseudo")
}

/// Same as [`Self::pseudo`] but panics if wasn't called on [`Self::Pseudo`].
///
/// # Panics
///
/// If was called on something else other than [`Self::Pseudo`].
pub const fn expect_pseudo(self) -> PseudoInstruction {
self.pseudo()
.expect("Expected Instruction::Pseudo, found Instruction::Real")
}
Comment on lines +977 to +995
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 | 🟡 Minor

Panic messages refer to the wrong type (Instruction::Real/Pseudo).

These are AnyInstruction::Real/Pseudo; the current messages will confuse crash reports.

Suggested fix
     pub const fn expect_real(self) -> Instruction {
         self.real()
-            .expect("Expected Instruction::Real, found Instruction::Pseudo")
+            .expect("Expected AnyInstruction::Real, found AnyInstruction::Pseudo")
     }

     pub const fn expect_pseudo(self) -> PseudoInstruction {
         self.pseudo()
-            .expect("Expected Instruction::Pseudo, found Instruction::Real")
+            .expect("Expected AnyInstruction::Pseudo, found AnyInstruction::Real")
     }
🤖 Prompt for AI Agents
In `@crates/compiler-core/src/bytecode/instruction.rs` around lines 977 - 995, The
panic messages in expect_real and expect_pseudo reference the wrong enum name;
update the expect calls in the const fns expect_real and expect_pseudo to
mention AnyInstruction::Real and AnyInstruction::Pseudo respectively (e.g.,
replace "Expected Instruction::Real, found Instruction::Pseudo" with "Expected
AnyInstruction::Real, found AnyInstruction::Pseudo" and similarly for the other)
so crash reports correctly identify the type variant.

}

pub trait InstructionMetadata {
Expand Down
Loading