lex_bytecode/op.rs
1//! Bytecode instruction set per spec §8.2.
2
3use serde::{Deserialize, Serialize};
4
5/// Constant pool entry.
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
7pub enum Const {
8 Int(i64),
9 Float(f64),
10 Bool(bool),
11 Str(String),
12 Bytes(Vec<u8>),
13 Unit,
14 /// A field name, used by MAKE_RECORD/GET_FIELD.
15 FieldName(String),
16 /// A variant tag, used by MAKE_VARIANT/TEST_VARIANT/GET_VARIANT.
17 VariantName(String),
18 /// An AST NodeId, attached to Call / EffectCall for trace keying (§10.1).
19 NodeId(String),
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
23pub enum Op {
24 // stack manipulation
25 PushConst(u32),
26 Pop,
27 Dup,
28
29 // locals
30 LoadLocal(u16),
31 StoreLocal(u16),
32
33 // constructors / pattern matching
34 /// Builds a record from `count` (name_const_idx, value) pairs on the stack.
35 /// Stack: [name_idx_n, val_n, ..., name_idx_1, val_1] but encoded as
36 /// alternating `<name_idx_const_u32> <value popped from stack>` — for
37 /// simplicity we instead push `count` values and `count` field name
38 /// constants in the same op as a `Vec<u32>` of name indices.
39 MakeRecord { field_name_indices: Vec<u32> },
40 MakeTuple(u16),
41 MakeList(u32),
42 MakeVariant { name_idx: u32, arity: u16 },
43 GetField(u32), // field name const idx
44 GetElem(u16), // tuple element index
45 TestVariant(u32), // pushes Bool: top-of-stack matches variant name?
46 GetVariant(u32), // extracts payload (replaces variant on stack with its args list)
47 GetVariantArg(u16), // pop variant, push its i'th arg
48 GetListLen,
49 GetListElem(u32),
50 /// Pop [list, value]; push list with `value` appended.
51 ListAppend,
52 /// Pop list; push it indexed by the integer on top.
53 /// Stack: [list, idx] → [list[idx]]. (Like GetListElem(u32) but
54 /// the index is dynamic.)
55 GetListElemDyn,
56
57 // control flow
58 Jump(i32),
59 JumpIf(i32), // pops Bool
60 JumpIfNot(i32),
61 Call { fn_id: u32, arity: u16, node_id_idx: u32 },
62 TailCall { fn_id: u32, arity: u16, node_id_idx: u32 },
63 /// Build a Value::Closure: pop `capture_count` values (in order) and
64 /// pair them with `fn_id`.
65 MakeClosure { fn_id: u32, capture_count: u16 },
66 /// Call a closure: pop `arity` args + 1 closure (top of stack), invoke.
67 CallClosure { arity: u16, node_id_idx: u32 },
68 /// EFFECT_CALL `<effect_kind_const_idx>` `<op_name_const_idx>` `<arity>`.
69 /// Pops `arity` args, dispatches to a host effect handler, pushes result.
70 /// `node_id_idx` points to a `Const::NodeId` for trace keying.
71 EffectCall { kind_idx: u32, op_idx: u32, arity: u16, node_id_idx: u32 },
72 Return,
73 Panic(u32), // pushes constant message and aborts
74
75 // arithmetic — typed (per spec §8.2). `NumAdd`/etc. dispatch on operand
76 // type at runtime; emitted when the compiler doesn't have type info.
77 // The post-M5 plan is to lower NumAdd → IntAdd|FloatAdd in a typed pass.
78 IntAdd, IntSub, IntMul, IntDiv, IntMod, IntNeg,
79 IntEq, IntLt, IntLe,
80 FloatAdd, FloatSub, FloatMul, FloatDiv, FloatNeg,
81 FloatEq, FloatLt, FloatLe,
82 NumAdd, NumSub, NumMul, NumDiv, NumMod, NumNeg,
83 NumEq, NumLt, NumLe,
84 BoolAnd, BoolOr, BoolNot,
85
86 // strings
87 StrConcat, StrLen, StrEq,
88 BytesLen, BytesEq,
89}