Skip to content
Merged
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
Fix opcode.rs
  • Loading branch information
ShaharNaveh committed Jan 12, 2026
commit 697fe41c70b3d73d6eca271cc4b7d971e5ec351e
136 changes: 77 additions & 59 deletions crates/stdlib/src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ mod opcode {
use crate::vm::{
AsObject, PyObjectRef, PyResult, VirtualMachine,
builtins::{PyInt, PyIntRef},
bytecode::Instruction,
bytecode::{Instruction, InstructionMetadata, PseudoInstruction, RealInstruction},
};
use core::ops::Deref;

#[derive(Clone, Copy)]
struct Opcode(Instruction);

impl Deref for Opcode {
Expand All @@ -19,13 +20,26 @@ mod opcode {
}
}

impl TryFrom<i32> for Opcode {
type Error = ();

fn try_from(value: i32) -> Result<Self, Self::Error> {
Ok(Self(
u16::try_from(value)
.map_err(|_| ())?
.try_into()
.map_err(|_| ())?,
))
}
}

impl Opcode {
// https://github.com/python/cpython/blob/bcee1c322115c581da27600f2ae55e5439c027eb/Include/opcode_ids.h#L238
const HAVE_ARGUMENT: i32 = 44;

pub fn try_from_pyint(raw: PyIntRef, vm: &VirtualMachine) -> PyResult<Self> {
let instruction = raw
.try_to_primitive::<u8>(vm)
.try_to_primitive::<u16>(vm)
.and_then(|v| {
Instruction::try_from(v).map_err(|_| {
vm.new_exception_empty(vm.ctx.exceptions.value_error.to_owned())
Expand All @@ -36,13 +50,14 @@ mod opcode {
Ok(Self(instruction))
}

const fn inner(self) -> Instruction {
self.0
}

/// Check if opcode is valid (can be converted to an Instruction)
#[must_use]
pub fn is_valid(opcode: i32) -> bool {
if !(0..=255).contains(&opcode) {
return false;
}
Instruction::try_from(opcode as u8).is_ok()
Self::try_from(opcode).is_ok()
}

/// Check if instruction has an argument
Expand All @@ -54,79 +69,82 @@ mod opcode {
/// Check if instruction uses co_consts
#[must_use]
pub fn has_const(opcode: i32) -> bool {
Self::is_valid(opcode)
&& matches!(
Instruction::try_from(opcode as u8),
Ok(Instruction::LoadConst { .. } | Instruction::ReturnConst { .. })
)
matches!(
Self::try_from(opcode).map(|op| op.inner()),
Ok(Instruction::Real(
RealInstruction::LoadConst { .. } | RealInstruction::ReturnConst { .. }
))
)
}

/// Check if instruction uses co_names
#[must_use]
pub fn has_name(opcode: i32) -> bool {
Self::is_valid(opcode)
&& matches!(
Instruction::try_from(opcode as u8),
Ok(Instruction::DeleteAttr { .. }
| Instruction::DeleteGlobal(_)
| Instruction::DeleteName(_)
| Instruction::ImportFrom { .. }
| Instruction::ImportName { .. }
| Instruction::LoadAttr { .. }
| Instruction::LoadGlobal(_)
| Instruction::LoadAttrMethod { .. }
| Instruction::LoadName(_)
| Instruction::StoreAttr { .. }
| Instruction::StoreGlobal(_)
| Instruction::StoreName(_))
)
matches!(
Self::try_from(opcode).map(|op| op.inner()),
Ok(Instruction::Real(
RealInstruction::DeleteAttr { .. }
| RealInstruction::DeleteGlobal(_)
| RealInstruction::DeleteName(_)
| RealInstruction::ImportFrom { .. }
| RealInstruction::ImportName { .. }
| RealInstruction::LoadAttr { .. }
| RealInstruction::LoadGlobal(_)
| RealInstruction::LoadName(_)
| RealInstruction::StoreAttr { .. }
| RealInstruction::StoreGlobal(_)
| RealInstruction::StoreName(_)
) | Instruction::Pseudo(PseudoInstruction::LoadAttrMethod { .. }))
)
}
Comment on lines 82 to 99
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

Fix opcode classification gaps: add missing real jump ops (and likely LoadSuperAttr) to predicates.

  • has_jump (Line 103-118) should include RealInstruction::JumpForward/JumpBackward/JumpBackwardNoInterrupt.
  • has_name (Line 82-99) likely needs RealInstruction::LoadSuperAttr { .. } since it indexes into co_names via packed oparg.
Proposed fix (extend match arms)
         pub fn has_name(opcode: i32) -> bool {
             matches!(
                 Self::try_from(opcode).map(|op| op.inner()),
                 Ok(Instruction::Real(
                     RealInstruction::DeleteAttr { .. }
                         | RealInstruction::DeleteGlobal(_)
                         | RealInstruction::DeleteName(_)
                         | RealInstruction::ImportFrom { .. }
                         | RealInstruction::ImportName { .. }
                         | RealInstruction::LoadAttr { .. }
                         | RealInstruction::LoadGlobal(_)
                         | RealInstruction::LoadName(_)
+                        | RealInstruction::LoadSuperAttr { .. }
                         | RealInstruction::StoreAttr { .. }
                         | RealInstruction::StoreGlobal(_)
                         | RealInstruction::StoreName(_)
                 ) | Instruction::Pseudo(PseudoInstruction::LoadAttrMethod { .. }))
             )
         }

         pub fn has_jump(opcode: i32) -> bool {
             matches!(
                 Self::try_from(opcode).map(|op| op.inner()),
                 Ok(Instruction::Real(
                     RealInstruction::Break { .. }
                         | RealInstruction::Continue { .. }
                         | RealInstruction::ForIter { .. }
+                        | RealInstruction::JumpForward { .. }
+                        | RealInstruction::JumpBackward { .. }
+                        | RealInstruction::JumpBackwardNoInterrupt { .. }
                         | RealInstruction::JumpIfFalseOrPop { .. }
                         | RealInstruction::JumpIfNotExcMatch(_)
                         | RealInstruction::JumpIfTrueOrPop { .. }
                         | RealInstruction::PopJumpIfFalse { .. }
                         | RealInstruction::PopJumpIfTrue { .. }
                         | RealInstruction::Send { .. }
                 ) | Instruction::Pseudo(PseudoInstruction::Jump { .. }))
             )
         }

Also applies to: 103-118

🤖 Prompt for AI Agents
In @crates/stdlib/src/opcode.rs around lines 82 - 99, The predicates misclassify
some opcodes: update has_name to also treat RealInstruction::LoadSuperAttr { ..
} as a name-indexing opcode (it indexes co_names via packed oparg) by adding it
to the match arm alongside other RealInstruction::Load*/Store*/Delete*/Import*
variants, and update has_jump to include RealInstruction::JumpForward,
RealInstruction::JumpBackward, and RealInstruction::JumpBackwardNoInterrupt in
its jump match arm; modify the match expressions inside both functions (the
pattern matching on Self::try_from(opcode).map(|op| op.inner())) to include
these missing RealInstruction variants so those predicates accurately reflect
all name and jump opcodes.


/// Check if instruction is a jump
#[must_use]
pub fn has_jump(opcode: i32) -> bool {
Self::is_valid(opcode)
&& matches!(
Instruction::try_from(opcode as u8),
Ok(Instruction::Break { .. }
| Instruction::Continue { .. }
| Instruction::ForIter { .. }
| Instruction::JumpIfFalseOrPop { .. }
| Instruction::JumpIfNotExcMatch(_)
| Instruction::JumpIfTrueOrPop { .. }
| Instruction::Jump { .. }
| Instruction::PopJumpIfFalse { .. }
| Instruction::PopJumpIfTrue { .. }
| Instruction::Send { .. })
)
matches!(
Self::try_from(opcode).map(|op| op.inner()),
Ok(Instruction::Real(
RealInstruction::Break { .. }
| RealInstruction::Continue { .. }
| RealInstruction::ForIter { .. }
| RealInstruction::JumpIfFalseOrPop { .. }
| RealInstruction::JumpIfNotExcMatch(_)
| RealInstruction::JumpIfTrueOrPop { .. }
| RealInstruction::PopJumpIfFalse { .. }
| RealInstruction::PopJumpIfTrue { .. }
| RealInstruction::Send { .. }
) | Instruction::Pseudo(PseudoInstruction::Jump { .. }))
)
}

/// Check if instruction uses co_freevars/co_cellvars
#[must_use]
pub fn has_free(opcode: i32) -> bool {
Self::is_valid(opcode)
&& matches!(
Instruction::try_from(opcode as u8),
Ok(Instruction::DeleteDeref(_)
| Instruction::LoadFromDictOrDeref(_)
| Instruction::LoadClosure(_)
| Instruction::LoadDeref(_)
| Instruction::StoreDeref(_))
)
matches!(
Self::try_from(opcode).map(|op| op.inner()),
Ok(Instruction::Real(
RealInstruction::DeleteDeref(_)
| RealInstruction::LoadFromDictOrDeref(_)
| RealInstruction::LoadClosure(_)
| RealInstruction::LoadDeref(_)
| RealInstruction::StoreDeref(_)
))
)
}

/// Check if instruction uses co_varnames (local variables)
#[must_use]
pub fn has_local(opcode: i32) -> bool {
Self::is_valid(opcode)
&& matches!(
Instruction::try_from(opcode as u8),
Ok(Instruction::DeleteFast(_)
| Instruction::LoadFast(_)
| Instruction::LoadFastAndClear(_)
| Instruction::StoreFast(_)
| Instruction::StoreFastLoadFast { .. })
)
matches!(
Self::try_from(opcode).map(|op| op.inner()),
Ok(Instruction::Real(
RealInstruction::DeleteFast(_)
| RealInstruction::LoadFast(_)
| RealInstruction::LoadFastAndClear(_)
| RealInstruction::StoreFast(_)
| RealInstruction::StoreFastLoadFast { .. }
))
)
}

/// Check if instruction has exception info
Expand Down
Loading