pub struct Program {
pub chunks: Vec<Chunk>,
pub main_index: usize,
pub globals: Vec<String>,
pub fn_names: Vec<String>,
pub enum_variant_names: Vec<(String, String)>,
pub structs: Vec<StructInfo>,
}Expand description
a whole compiled program: one chunk per function, with the entry-point index and a globals table.
function indexes in CALL opcodes are indexes into Program::chunks;
matching name lookups for the disassembler are in Program::fn_names.
enum variant ids in MAKE_ENUM_VARIANT and MATCH_VARIANT are indexes into
Program::enum_variant_names. struct ids in MAKE_STRUCT are indexes
into Program::structs.
Fields§
§chunks: Vec<Chunk>one chunk per function, indexed by function id (u16 in CALL operands).
main_index: usizethe chunks index where execution starts. Phase 4 codegen sets this
to the index of the main function.
globals: Vec<String>global names indexed by GET_GLOBAL / SET_GLOBAL u16 operands. v1 has
no top-level let so this is empty in practice – kept for forward
compatibility.
fn_names: Vec<String>function names parallel to chunks – the disassembler renders
CALL #fn_id(name).
enum_variant_names: Vec<(String, String)>(enum_name, variant_name) pairs indexed by MAKE_ENUM_VARIANT / MATCH_VARIANT u16 operands. the disassembler renders #variant_id(Enum::Variant).
structs: Vec<StructInfo>one StructInfo per declared struct, indexed by MAKE_STRUCT u16
operands. records each struct’s name and field count so the VM can
build a heap struct with the right type_name and pop the right
number of fields. Program::default() gives an empty vec.
Implementations§
Source§impl Program
impl Program
Sourcepub fn new() -> Self
pub fn new() -> Self
construct an empty program. all vectors start empty; main_index
defaults to 0.
Sourcepub fn disassemble(&self) -> String
pub fn disassemble(&self) -> String
produce the human-readable listing for the whole program: one
header line per chunk followed by the chunk’s body. byte-
deterministic across runs by construction (iterates chunks in
index order; no HashMap iteration). the format matches the locked
playground bytecode-panel contract.
Sourcepub fn optimize(&mut self)
pub fn optimize(&mut self)
run the peephole optimizer over every chunk in this program. invokes
crate::optimizer::peephole. idempotent: a second call does no
work, because the first pass already collapsed every matchable
instruction window.
optimization is opt-in – the disassembler can show un-optimized bytecode by skipping this call, which supports the playground’s “show me what the compiler did” use case.