Skip to main content

Module opcode

Module opcode 

Source
Expand description

the bytecode opcode enum: every byte the codegen emits is one of these variants encoded as Opcode::Foo as u8. dense discriminants 0..=45 for the real opcodes plus Opcode::Halt at 0xFF as a sentinel for the decoder so an out-of-bounds byte is detectable rather than silently reinterpretable.

the layout is part of the bytecode format. adding a new opcode means three places: the enum variant (with an explicit discriminant), the Opcode::from_u8 arm, the Opcode::name arm. Opcode::operand_bytes adds a fourth if the new opcode carries operands. the unit tests in this module loop over every variant and over every byte 0..=255, so a missing arm or a wrong discriminant fails loudly.

the disassembler (crate::chunk::Chunk::disassemble, plan 04-03) reads the byte stream back via Opcode::from_u8 and renders via Opcode::name; the peephole optimizer (crate::optimizer::peephole, plan 04-05) uses Opcode::operand_bytes as its step function for walking past whole instructions. these three methods are the public contract this file owns.

no transmute-based decode (anti-pattern per Phase 4 research): the match-based reverse lookup compiles to a branch table and is safe on every byte. zero-cost safety; idiomatic Rust pattern.

Enums§

Opcode
the bytecode opcode set.

Constants§

STDLIB_FN_BASE
the first fn-id reserved for the native standard library.