Skip to main content

aver/vm/
opcode.rs

1// Aver VM bytecode opcodes.
2//
3// Stack-based: operands are pushed/popped from the operand stack.
4// Variable-width encoding: opcode (1 byte) + operands (0-3 bytes).
5
6// -- Stack / locals ----------------------------------------------------------
7
8/// No-op, used as padding after superinstruction fusion.
9pub const NOP: u8 = 0x00;
10
11/// Push `stack[bp + slot]` onto the operand stack.
12pub const LOAD_LOCAL: u8 = 0x01; // slot:u8
13
14/// Pop top and store into `stack[bp + slot]`.
15pub const STORE_LOCAL: u8 = 0x02; // slot:u8
16
17/// Push `constants[idx]` onto the operand stack.
18pub const LOAD_CONST: u8 = 0x03; // idx:u16
19
20/// Push `globals[idx]` onto the operand stack.
21pub const LOAD_GLOBAL: u8 = 0x04; // idx:u16
22
23/// Pop top and store into `globals[idx]`.
24pub const STORE_GLOBAL: u8 = 0x0A; // idx:u16
25
26/// Discard the top value.
27pub const POP: u8 = 0x05;
28
29/// Duplicate the top value.
30pub const DUP: u8 = 0x06;
31
32/// Push `NanValue::UNIT`.
33pub const LOAD_UNIT: u8 = 0x07;
34
35/// Push `NanValue::TRUE`.
36pub const LOAD_TRUE: u8 = 0x08;
37
38/// Push `NanValue::FALSE`.
39pub const LOAD_FALSE: u8 = 0x09;
40
41// -- Arithmetic --------------------------------------------------------------
42
43/// Pop b, pop a, push a + b.
44pub const ADD: u8 = 0x10;
45
46/// Pop b, pop a, push a - b.
47pub const SUB: u8 = 0x11;
48
49/// Pop b, pop a, push a * b.
50pub const MUL: u8 = 0x12;
51
52/// Pop b, pop a, push a / b.
53pub const DIV: u8 = 0x13;
54
55/// Pop b, pop a, push a % b.
56pub const MOD: u8 = 0x14;
57
58/// Pop a, push -a.
59pub const NEG: u8 = 0x15;
60
61/// Pop a, push !a (boolean not).
62pub const NOT: u8 = 0x16;
63
64// -- Comparison --------------------------------------------------------------
65
66/// Pop b, pop a, push a == b.
67pub const EQ: u8 = 0x20;
68
69/// Pop b, pop a, push a < b.
70pub const LT: u8 = 0x21;
71
72/// Pop b, pop a, push a > b.
73pub const GT: u8 = 0x22;
74
75// -- String ------------------------------------------------------------------
76
77/// Pop b, pop a, push str(a) ++ str(b).
78pub const CONCAT: u8 = 0x28;
79
80// -- Control flow ------------------------------------------------------------
81
82/// Unconditional relative jump: ip += offset.
83pub const JUMP: u8 = 0x30; // offset:i16
84
85/// Pop top, if falsy: ip += offset.
86pub const JUMP_IF_FALSE: u8 = 0x31; // offset:i16
87
88// -- Calls -------------------------------------------------------------------
89
90/// Call a known function by id. Args already on stack.
91pub const CALL_KNOWN: u8 = 0x40; // fn_id:u16, argc:u8
92
93/// Call a function value on the stack (under args).
94pub const CALL_VALUE: u8 = 0x41; // argc:u8
95
96/// Call a builtin service function.
97pub const CALL_BUILTIN: u8 = 0x42; // symbol_id:u32, argc:u8
98
99/// Self tail-call: reuse current frame with new args.
100pub const TAIL_CALL_SELF: u8 = 0x43; // argc:u8
101
102/// Mutual tail-call to a known function: reuse frame, switch target.
103pub const TAIL_CALL_KNOWN: u8 = 0x44; // fn_id:u16, argc:u8
104
105/// Return top of stack to caller.
106pub const RETURN: u8 = 0x50;
107
108// -- Structured values -------------------------------------------------------
109
110/// Push Nil (empty cons list).
111pub const LIST_NIL: u8 = 0x60;
112
113/// Pop tail, pop head, push Cons(head, tail).
114pub const LIST_CONS: u8 = 0x61;
115
116/// Pop `count` items, build cons list from them (first item = head), push list.
117pub const LIST_NEW: u8 = 0x62; // count:u8
118
119/// Pop `count` field values, push a new record with `type_id`.
120pub const RECORD_NEW: u8 = 0x63; // type_id:u16, count:u8
121
122/// Pop record, push `fields[field_idx]` (compile-time resolved index).
123pub const RECORD_GET: u8 = 0x64; // field_idx:u8
124
125/// Pop record, lookup field by interned field symbol, push value.
126pub const RECORD_GET_NAMED: u8 = 0x67; // field_symbol_id:u32
127
128/// Pop `count` field values, push a new variant.
129pub const VARIANT_NEW: u8 = 0x65; // type_id:u16, variant_id:u16, count:u8
130
131/// Pop value, push wrapped value. kind: 0=Ok, 1=Err, 2=Some.
132pub const WRAP: u8 = 0x66; // kind:u8
133
134/// Pop `count` items, build a tuple from them, push tuple.
135pub const TUPLE_NEW: u8 = 0x68; // count:u8
136
137/// Parallel function calls for independent products (?! / !).
138/// Pops N callable values plus their args from the stack, dispatches them via
139/// the same callable resolution rules as CALL_VALUE, then builds the result tuple.
140/// Enters/exits replay group around parallel dispatch.
141///
142/// Encoding: CALL_PAR count:u8 unwrap:u8 [argc:u8 × count]
143/// unwrap=1 (?!): unwrap each Result, propagate first Err.
144/// unwrap=0 (!): return raw tuple.
145pub const CALL_PAR: u8 = 0x86;
146
147/// Update selected fields on a record, preserving the rest from the base value.
148/// Stack: [..., base_record, update_0, ..., update_n-1] -> [..., updated_record]
149pub const RECORD_UPDATE: u8 = 0x69; // type_id:u16, count:u8, field_idx[count]:u8
150
151/// Propagate `Result.Err` to caller or unwrap `Result.Ok` in place.
152pub const PROPAGATE_ERR: u8 = 0x6A;
153
154/// Pop list, push its length as Int.
155pub const LIST_LEN: u8 = 0x6B;
156
157// 0x6C and 0x6D were LIST_GET and LIST_APPEND — removed.
158
159/// Pop list, pop value, push prepended list.
160pub const LIST_PREPEND: u8 = 0x6E;
161
162// 0x6F was LIST_GET_MATCH — removed.
163
164// -- Pattern matching --------------------------------------------------------
165
166/// Peek top: if NaN tag != expected, ip += fail_offset.
167pub const MATCH_TAG: u8 = 0x70; // expected_tag:u8, fail_offset:i16
168
169/// Peek top (must be variant): if variant_id != expected, ip += fail_offset.
170pub const MATCH_VARIANT: u8 = 0x71; // ctor_id:u16, fail_offset:i16
171
172/// Peek top: if not wrapper of `kind`, ip += fail_offset.
173/// If matches, replace top with inner value (unwrap in-place).
174/// kind: 0=Ok, 1=Err, 2=Some.
175pub const MATCH_UNWRAP: u8 = 0x72; // kind:u8, fail_offset:i16
176
177/// Peek top: if not Nil, ip += fail_offset.
178pub const MATCH_NIL: u8 = 0x73; // fail_offset:i16
179
180/// Peek top: if Nil (not a cons), ip += fail_offset.
181pub const MATCH_CONS: u8 = 0x74; // fail_offset:i16
182
183/// Pop cons cell, push tail then push head.
184pub const LIST_HEAD_TAIL: u8 = 0x75;
185
186/// Peek top (record/variant), push `fields[field_idx]` (non-destructive).
187pub const EXTRACT_FIELD: u8 = 0x76; // field_idx:u8
188
189/// Peek top: if not a tuple of `count` items, ip += fail_offset.
190pub const MATCH_TUPLE: u8 = 0x78; // count:u8, fail_offset:i16
191
192/// Peek top tuple, push `items[item_idx]` (non-destructive).
193pub const EXTRACT_TUPLE_ITEM: u8 = 0x79; // item_idx:u8
194
195/// Non-exhaustive match error at source line.
196pub const MATCH_FAIL: u8 = 0x77; // line:u16
197
198/// Unified prefix/exact dispatch on NanValue bits.
199///
200/// Encoding:
201///   MATCH_DISPATCH count:u8 default_offset:i16
202///     [(kind:u8, expected:u64, offset:i16) × count]
203///
204/// kind=0: exact match  — `val.bits() == expected`
205/// kind=1: tag match    — `(val.bits() & TAG_MASK_FULL) == expected`
206///   where TAG_MASK_FULL = 0xFFFF_C000_0000_0000 (QNAN 14 bits + tag 4 bits)
207///
208/// Pops subject. Scans entries in order; first match wins → ip += offset.
209/// No match → ip += default_offset.
210/// All offsets are relative to the end of the full instruction.
211pub const MATCH_DISPATCH: u8 = 0x7A;
212
213/// Like MATCH_DISPATCH but every entry carries an inline result instead
214/// of a jump offset.  When an entry matches, the result is pushed directly
215/// onto the stack and the match body is skipped entirely.
216///
217/// Encoding:
218///   MATCH_DISPATCH_CONST count:u8 default_offset:i16
219///     [(kind:u8, expected:u64, result:u64) × count]
220///
221/// Hit → pop subject, push result NanValue.
222/// Miss → pop subject, ip += default_offset (execute default arm body).
223///
224/// Emitted when ALL dispatchable arms have constant bodies (literals).
225pub const MATCH_DISPATCH_CONST: u8 = 0x7B;
226
227/// Tail-call self for thin frames: no arena finalization needed.
228/// The compiler emits this instead of TAIL_CALL_SELF when the function
229/// is known to be "thin" (no heap allocations within the frame).
230/// Skips finalize_frame_locals_for_tail_call entirely — just copies
231/// args in-place and resets ip.
232pub const TAIL_CALL_SELF_THIN: u8 = 0x45; // argc:u8
233
234/// Inline Option.withDefault: pop default, pop option → push inner or default.
235/// Stack: [option, default] → [result]
236/// If option is Some → push unwrapped inner value.
237/// If option is None → push default.
238pub const UNWRAP_OR: u8 = 0x7C;
239
240/// Inline Result.withDefault: pop default, pop result → push inner or default.
241/// Stack: [result, default] → [value]
242/// If result is Ok → push unwrapped inner value.
243/// If result is Err → push default.
244pub const UNWRAP_RESULT_OR: u8 = 0x7D;
245
246/// Frameless call to a leaf+thin+args-only function.
247/// No CallFrame is pushed — just saves (fn_id, ip) in the dispatch loop,
248/// sets bp to the args already on stack, and jumps to the target.
249/// On RETURN, restores the caller's state directly.
250/// Format: fn_id:u16, argc:u8 (same as CALL_KNOWN).
251pub const CALL_LEAF: u8 = 0x7E;
252
253// ─── Superinstructions ──────────────────────────────────────
254
255/// Push two locals in one dispatch. Format: slot_a:u8, slot_b:u8.
256pub const LOAD_LOCAL_2: u8 = 0x80;
257
258/// Push one local + one constant in one dispatch. Format: slot:u8, const_idx:u16.
259pub const LOAD_LOCAL_CONST: u8 = 0x81;
260
261/// Inline Vector.get: pop index, pop vector → push Option (Some/None).
262/// Stack: [vector, index] → [option]
263pub const VECTOR_GET: u8 = 0x82;
264
265/// Fused Vector.get + Option.withDefault: pop default, pop index, pop vector → push value.
266/// Stack: [vector, index, default] → [value]
267/// Combines CALL_BUILTIN(Vector.get) + LOAD_CONST + UNWRAP_OR into one opcode.
268pub const VECTOR_GET_OR: u8 = 0x83;
269
270/// Inline Vector.set: pop value, pop index, pop vector → push Option<Vector>.
271/// Stack: [vector, index, value] → [option_vector]
272pub const VECTOR_SET: u8 = 0x84;
273
274/// Fused Vector.set + Option.withDefault(vec): pop value, pop index, pop vector → push vector.
275/// Stack: [vector, index, value] → [vector]
276pub const VECTOR_SET_OR_KEEP: u8 = 0x85;
277
278/// Opcode name for debug/disassembly.
279pub fn opcode_name(op: u8) -> &'static str {
280    match op {
281        LOAD_LOCAL => "LOAD_LOCAL",
282        STORE_LOCAL => "STORE_LOCAL",
283        LOAD_CONST => "LOAD_CONST",
284        LOAD_GLOBAL => "LOAD_GLOBAL",
285        POP => "POP",
286        DUP => "DUP",
287        LOAD_UNIT => "LOAD_UNIT",
288        LOAD_TRUE => "LOAD_TRUE",
289        LOAD_FALSE => "LOAD_FALSE",
290        ADD => "ADD",
291        SUB => "SUB",
292        MUL => "MUL",
293        DIV => "DIV",
294        MOD => "MOD",
295        NEG => "NEG",
296        NOT => "NOT",
297        EQ => "EQ",
298        LT => "LT",
299        GT => "GT",
300        CONCAT => "CONCAT",
301        JUMP => "JUMP",
302        JUMP_IF_FALSE => "JUMP_IF_FALSE",
303        CALL_KNOWN => "CALL_KNOWN",
304        CALL_VALUE => "CALL_VALUE",
305        CALL_BUILTIN => "CALL_BUILTIN",
306        TAIL_CALL_SELF => "TAIL_CALL_SELF",
307        TAIL_CALL_KNOWN => "TAIL_CALL_KNOWN",
308        RETURN => "RETURN",
309        LIST_NIL => "LIST_NIL",
310        LIST_CONS => "LIST_CONS",
311        LIST_NEW => "LIST_NEW",
312        RECORD_NEW => "RECORD_NEW",
313        STORE_GLOBAL => "STORE_GLOBAL",
314        RECORD_GET => "RECORD_GET",
315        RECORD_GET_NAMED => "RECORD_GET_NAMED",
316        VARIANT_NEW => "VARIANT_NEW",
317        WRAP => "WRAP",
318        TUPLE_NEW => "TUPLE_NEW",
319        RECORD_UPDATE => "RECORD_UPDATE",
320        PROPAGATE_ERR => "PROPAGATE_ERR",
321        LIST_LEN => "LIST_LEN",
322        LIST_PREPEND => "LIST_PREPEND",
323        MATCH_TAG => "MATCH_TAG",
324        MATCH_VARIANT => "MATCH_VARIANT",
325        MATCH_UNWRAP => "MATCH_UNWRAP",
326        MATCH_NIL => "MATCH_NIL",
327        MATCH_CONS => "MATCH_CONS",
328        LIST_HEAD_TAIL => "LIST_HEAD_TAIL",
329        EXTRACT_FIELD => "EXTRACT_FIELD",
330        MATCH_TUPLE => "MATCH_TUPLE",
331        EXTRACT_TUPLE_ITEM => "EXTRACT_TUPLE_ITEM",
332        MATCH_FAIL => "MATCH_FAIL",
333        MATCH_DISPATCH => "MATCH_DISPATCH",
334        MATCH_DISPATCH_CONST => "MATCH_DISPATCH_CONST",
335        TAIL_CALL_SELF_THIN => "TAIL_CALL_SELF_THIN",
336        UNWRAP_OR => "UNWRAP_OR",
337        UNWRAP_RESULT_OR => "UNWRAP_RESULT_OR",
338        CALL_LEAF => "CALL_LEAF",
339        LOAD_LOCAL_2 => "LOAD_LOCAL_2",
340        LOAD_LOCAL_CONST => "LOAD_LOCAL_CONST",
341        VECTOR_GET => "VECTOR_GET",
342        VECTOR_GET_OR => "VECTOR_GET_OR",
343        VECTOR_SET => "VECTOR_SET",
344        VECTOR_SET_OR_KEEP => "VECTOR_SET_OR_KEEP",
345        CALL_PAR => "CALL_PAR",
346        NOP => "NOP",
347        _ => "UNKNOWN",
348    }
349}
350
351/// Operand byte width after the opcode byte. Single source of truth —
352/// all bytecode traversal functions must use this.
353pub fn opcode_operand_width(op: u8, code: &[u8], ip: usize) -> usize {
354    match op {
355        // 0-byte (stack-only)
356        POP | DUP | LOAD_UNIT | LOAD_TRUE | LOAD_FALSE | ADD | SUB | MUL | DIV | MOD | NEG
357        | NOT | EQ | LT | GT | RETURN | PROPAGATE_ERR | LIST_HEAD_TAIL | LIST_NIL | LIST_CONS
358        | LIST_LEN | LIST_PREPEND | UNWRAP_OR | UNWRAP_RESULT_OR | CONCAT | VECTOR_GET
359        | VECTOR_SET | VECTOR_SET_OR_KEEP | NOP => 0,
360
361        // 1-byte
362        LOAD_LOCAL | STORE_LOCAL | CALL_VALUE | RECORD_GET | EXTRACT_FIELD | EXTRACT_TUPLE_ITEM
363        | LIST_NEW | WRAP | TUPLE_NEW | TAIL_CALL_SELF | TAIL_CALL_SELF_THIN => 1,
364
365        // 2-byte (u16 or u8+u8)
366        LOAD_CONST | LOAD_GLOBAL | STORE_GLOBAL | JUMP | JUMP_IF_FALSE | MATCH_FAIL | MATCH_NIL
367        | MATCH_CONS | LOAD_LOCAL_2 | VECTOR_GET_OR => 2,
368
369        // 3-byte
370        CALL_KNOWN | CALL_LEAF | MATCH_TAG | MATCH_UNWRAP | MATCH_TUPLE | RECORD_NEW
371        | LOAD_LOCAL_CONST => 3,
372
373        // 4-byte
374        MATCH_VARIANT | RECORD_GET_NAMED => 4,
375
376        // 5-byte
377        CALL_BUILTIN | VARIANT_NEW => 5,
378
379        // Variable-length
380        MATCH_DISPATCH | MATCH_DISPATCH_CONST => {
381            if ip < code.len() {
382                let count = code[ip] as usize;
383                let entry_size = if op == MATCH_DISPATCH { 11 } else { 17 };
384                3 + count * entry_size
385            } else {
386                0
387            }
388        }
389        RECORD_UPDATE => {
390            if ip + 2 < code.len() {
391                3 + code[ip + 2] as usize
392            } else {
393                0
394            }
395        }
396        // CALL_PAR count:u8 unwrap:u8 [argc:u8 × count]
397        CALL_PAR => {
398            if ip < code.len() {
399                let count = code[ip] as usize;
400                2 + count
401            } else {
402                0
403            }
404        }
405        _ => 0,
406    }
407}