Skip to main content

harn_vm/vm/ops/
mod.rs

1//! VM opcode dispatch.
2//!
3//! The `Op` enum, the byte-to-variant mapping, the sync and async
4//! dispatch tables, the disassembly renderer, and the per-opcode
5//! classification helpers (`op_reads_outer_name`, `is_adaptive_binary_op`)
6//! are all emitted by `harn_opcode_macros::define_opcodes!` from the
7//! single declarative table below. Adding or renaming an opcode is now a
8//! one-line edit instead of paired edits to four hand-maintained match
9//! tables — see issue #2584 for the migration that collapsed them.
10//!
11//! Each entry follows the shape
12//!
13//! ```ignore
14//! VariantName { <kind>(<expr>...), disasm: <helper>("<LABEL>") [, flags: [...]] };
15//! ```
16//!
17//! where `<kind>` selects the dispatch shape (`sync`, `sync_void`,
18//! `sync_return`, `split`, `async_op`) and the `<helper>` resolves to a
19//! `disasm_<helper>` free fn in `crate::chunk`.
20
21mod arithmetic;
22mod call;
23mod collections;
24mod comparison;
25mod control_flow;
26mod exception;
27mod imports;
28mod iter;
29mod logical;
30mod misc;
31mod parallel;
32mod stack;
33
34use crate::value::{VmError, VmValue};
35
36harn_opcode_macros::define_opcodes! {
37    // === Constants & nil ===
38    Constant { sync(self.execute_constant()), disasm: const_pool_u16("CONSTANT") };
39    Nil { sync_void(self.execute_nil()), disasm: bare("NIL") };
40    True { sync_void(self.execute_true()), disasm: bare("TRUE") };
41    False { sync_void(self.execute_false()), disasm: bare("FALSE") };
42
43    // === Variables ===
44    GetVar { sync(self.execute_get_var()), disasm: const_pool_u16("GET_VAR"), flags: [reads_outer_name] };
45    DefLet { sync(self.execute_def_let()), disasm: const_pool_u16("DEF_LET") };
46    DefVar { sync(self.execute_def_var()), disasm: const_pool_u16("DEF_VAR") };
47    SetVar { sync(self.execute_set_var()), disasm: const_pool_u16("SET_VAR"), flags: [reads_outer_name] };
48    PushScope { sync_void(self.execute_push_scope()), disasm: bare("PUSH_SCOPE") };
49    PopScope { sync_void(self.execute_pop_scope()), disasm: bare("POP_SCOPE") };
50
51    // === Generic arithmetic (adaptive binary IC) ===
52    Add { sync(self.execute_add()), disasm: bare("ADD"), flags: [adaptive_binary] };
53    Sub { sync(self.execute_sub()), disasm: bare("SUB"), flags: [adaptive_binary] };
54    Mul { sync(self.execute_mul()), disasm: bare("MUL"), flags: [adaptive_binary] };
55    Div { sync(self.execute_div()), disasm: bare("DIV"), flags: [adaptive_binary] };
56    Mod { sync(self.execute_mod()), disasm: bare("MOD"), flags: [adaptive_binary] };
57    Pow { sync(self.execute_pow()), disasm: bare("POW") };
58    Negate { sync(self.execute_negate()), disasm: bare("NEGATE") };
59
60    // === Generic comparison (adaptive binary IC) ===
61    Equal { sync(self.execute_equal()), disasm: bare("EQUAL"), flags: [adaptive_binary] };
62    NotEqual { sync(self.execute_not_equal()), disasm: bare("NOT_EQUAL"), flags: [adaptive_binary] };
63    Less { sync(self.execute_less()), disasm: bare("LESS"), flags: [adaptive_binary] };
64    Greater { sync(self.execute_greater()), disasm: bare("GREATER"), flags: [adaptive_binary] };
65    LessEqual { sync(self.execute_less_equal()), disasm: bare("LESS_EQUAL"), flags: [adaptive_binary] };
66    GreaterEqual { sync(self.execute_greater_equal()), disasm: bare("GREATER_EQUAL"), flags: [adaptive_binary] };
67
68    // === Logical ===
69    Not { sync(self.execute_not()), disasm: bare("NOT") };
70
71    // === Control flow ===
72    Jump { sync_void(self.execute_jump()), disasm: u16("JUMP") };
73    JumpIfFalse { sync(self.execute_jump_if_false()), disasm: u16("JUMP_IF_FALSE") };
74    JumpIfTrue { sync(self.execute_jump_if_true()), disasm: u16("JUMP_IF_TRUE") };
75    Pop { sync(self.execute_pop()), disasm: bare("POP") };
76
77    // === Functions ===
78    Call { split(self.execute_call_sync(), self.execute_call_async().await), disasm: u8("CALL"), flags: [reads_outer_name] };
79    TailCall { split(self.execute_tail_call_sync(), self.execute_tail_call_async().await), disasm: u8("TAIL_CALL"), flags: [reads_outer_name] };
80    Return { sync_return(self.execute_return()), disasm: bare("RETURN") };
81    Closure { sync_void(self.execute_closure()), disasm: u16("CLOSURE") };
82
83    // === Collections ===
84    BuildList { sync_void(self.execute_build_list()), disasm: u16("BUILD_LIST") };
85    BuildDict { sync_void(self.execute_build_dict()), disasm: u16("BUILD_DICT") };
86    Subscript { sync(self.execute_subscript(false)), disasm: bare("SUBSCRIPT") };
87    SubscriptOpt { sync(self.execute_subscript(true)), disasm: bare("SUBSCRIPT_OPT") };
88    Slice { sync(self.execute_slice()), disasm: bare("SLICE") };
89
90    // === Object operations ===
91    GetProperty { sync(self.execute_get_property(false)), disasm: const_pool_u16("GET_PROPERTY") };
92    GetPropertyOpt { sync(self.execute_get_property(true)), disasm: const_pool_u16("GET_PROPERTY_OPT") };
93    SetProperty { sync(self.execute_set_property()), disasm: const_pool_u16("SET_PROPERTY") };
94    SetSubscript { sync(self.execute_set_subscript()), disasm: const_pool_u16("SET_SUBSCRIPT") };
95    MethodCall { split(self.execute_method_call_sync(false), self.execute_method_call(false).await), disasm: method_call("METHOD_CALL") };
96    MethodCallOpt { split(self.execute_method_call_sync(true), self.execute_method_call(true).await), disasm: method_call("METHOD_CALL_OPT") };
97
98    // === Strings ===
99    Concat { sync_void(self.execute_concat()), disasm: u16("CONCAT") };
100
101    // === Iteration ===
102    IterInit { sync(self.execute_iter_init()), disasm: bare("ITER_INIT") };
103    IterNext { split(self.execute_iter_next_sync(), self.execute_iter_next_async().await), disasm: u16("ITER_NEXT") };
104
105    // === Pipe (async) ===
106    Pipe { async_op(self.execute_pipe().await), disasm: bare("PIPE"), flags: [reads_outer_name] };
107
108    // === Error handling ===
109    Throw { sync(self.execute_throw()), disasm: bare("THROW") };
110    TryCatchSetup { sync_void(self.execute_try_catch_setup()), disasm: u16("TRY_CATCH_SETUP") };
111    PopHandler { sync_void(self.execute_pop_handler()), disasm: bare("POP_HANDLER") };
112
113    // === Concurrency ===
114    Parallel { async_op(self.execute_parallel().await), disasm: bare("PARALLEL") };
115    ParallelMap { async_op(self.execute_parallel_map().await), disasm: bare("PARALLEL_MAP") };
116    ParallelMapStream { async_op(self.execute_parallel_map_stream().await), disasm: bare("PARALLEL_MAP_STREAM") };
117    ParallelSettle { async_op(self.execute_parallel_settle().await), disasm: bare("PARALLEL_SETTLE") };
118    Spawn { sync(self.execute_spawn()), disasm: bare("SPAWN") };
119    SyncMutexEnter { async_op(self.execute_sync_mutex_enter().await), disasm: const_pool_u16("SYNC_MUTEX_ENTER") };
120
121    // === Imports (async) ===
122    Import { async_op(self.execute_import_op().await), disasm: const_pool_u16("IMPORT") };
123    SelectiveImport { async_op(self.execute_selective_import().await), disasm: selective_import("SELECTIVE_IMPORT") };
124
125    // === Deadline ===
126    DeadlineSetup { sync(self.execute_deadline_setup()), disasm: bare("DEADLINE_SETUP") };
127    DeadlineEnd { sync_void(self.execute_deadline_end()), disasm: bare("DEADLINE_END") };
128
129    // === Enum ===
130    BuildEnum { sync(self.execute_build_enum()), disasm: build_enum("BUILD_ENUM") };
131    MatchEnum { sync(self.execute_match_enum()), disasm: match_enum("MATCH_ENUM") };
132
133    // === Loop control ===
134    PopIterator { sync_void(self.execute_pop_iterator()), disasm: bare("POP_ITERATOR") };
135
136    // === Defaults ===
137    GetArgc { sync_void(self.execute_get_argc()), disasm: bare("GET_ARGC") };
138
139    // === Type checking ===
140    CheckType { sync(self.execute_check_type()), disasm: check_type("CHECK_TYPE"), flags: [reads_outer_name] };
141
142    // === Result try operator ===
143    TryUnwrap { sync(self.execute_try_unwrap()), disasm: bare("TRY_UNWRAP") };
144    TryWrapOk { sync(self.execute_try_wrap_ok()), disasm: bare("TRY_WRAP_OK") };
145
146    // === Spread calls ===
147    CallSpread { async_op(self.execute_call_spread().await), disasm: bare("CALL_SPREAD"), flags: [reads_outer_name] };
148    CallBuiltin { split(self.execute_call_builtin_sync(), self.execute_call_builtin_async().await), disasm: call_builtin("CALL_BUILTIN"), flags: [reads_outer_name] };
149    CallBuiltinSpread { async_op(self.execute_call_builtin_spread().await), disasm: call_builtin_spread("CALL_BUILTIN_SPREAD"), flags: [reads_outer_name] };
150    MethodCallSpread { async_op(self.execute_method_call_spread().await), disasm: method_call_spread("METHOD_CALL_SPREAD") };
151
152    // === Misc ===
153    Dup { sync(self.execute_dup()), disasm: bare("DUP") };
154    Swap { sync_void(self.execute_swap()), disasm: bare("SWAP") };
155    Contains { sync(self.execute_contains()), disasm: bare("CONTAINS") };
156
157    // === Typed arithmetic fast paths ===
158    AddInt { sync(self.execute_add_int()), disasm: bare("ADD_INT") };
159    SubInt { sync(self.execute_sub_int()), disasm: bare("SUB_INT") };
160    MulInt { sync(self.execute_mul_int()), disasm: bare("MUL_INT") };
161    DivInt { sync(self.execute_div_int()), disasm: bare("DIV_INT") };
162    ModInt { sync(self.execute_mod_int()), disasm: bare("MOD_INT") };
163    AddFloat { sync(self.execute_add_float()), disasm: bare("ADD_FLOAT") };
164    SubFloat { sync(self.execute_sub_float()), disasm: bare("SUB_FLOAT") };
165    MulFloat { sync(self.execute_mul_float()), disasm: bare("MUL_FLOAT") };
166    DivFloat { sync(self.execute_div_float()), disasm: bare("DIV_FLOAT") };
167    ModFloat { sync(self.execute_mod_float()), disasm: bare("MOD_FLOAT") };
168
169    // === Typed comparison fast paths ===
170    EqualInt { sync(self.execute_equal_int()), disasm: bare("EQUAL_INT") };
171    NotEqualInt { sync(self.execute_not_equal_int()), disasm: bare("NOT_EQUAL_INT") };
172    LessInt { sync(self.execute_less_int()), disasm: bare("LESS_INT") };
173    GreaterInt { sync(self.execute_greater_int()), disasm: bare("GREATER_INT") };
174    LessEqualInt { sync(self.execute_less_equal_int()), disasm: bare("LESS_EQUAL_INT") };
175    GreaterEqualInt { sync(self.execute_greater_equal_int()), disasm: bare("GREATER_EQUAL_INT") };
176    EqualFloat { sync(self.execute_equal_float()), disasm: bare("EQUAL_FLOAT") };
177    NotEqualFloat { sync(self.execute_not_equal_float()), disasm: bare("NOT_EQUAL_FLOAT") };
178    LessFloat { sync(self.execute_less_float()), disasm: bare("LESS_FLOAT") };
179    GreaterFloat { sync(self.execute_greater_float()), disasm: bare("GREATER_FLOAT") };
180    LessEqualFloat { sync(self.execute_less_equal_float()), disasm: bare("LESS_EQUAL_FLOAT") };
181    GreaterEqualFloat { sync(self.execute_greater_equal_float()), disasm: bare("GREATER_EQUAL_FLOAT") };
182    EqualBool { sync(self.execute_equal_bool()), disasm: bare("EQUAL_BOOL") };
183    NotEqualBool { sync(self.execute_not_equal_bool()), disasm: bare("NOT_EQUAL_BOOL") };
184    EqualString { sync(self.execute_equal_string()), disasm: bare("EQUAL_STRING") };
185    NotEqualString { sync(self.execute_not_equal_string()), disasm: bare("NOT_EQUAL_STRING") };
186
187    // === Generators (async) ===
188    Yield { async_op(self.execute_yield().await), disasm: bare("YIELD") };
189
190    // === Slot-indexed locals ===
191    GetLocalSlot { sync(self.execute_get_local_slot()), disasm: local_slot_u16("GET_LOCAL_SLOT") };
192    DefLocalSlot { sync(self.execute_def_local_slot()), disasm: local_slot_u16("DEF_LOCAL_SLOT") };
193    SetLocalSlot { sync(self.execute_set_local_slot()), disasm: local_slot_u16("SET_LOCAL_SLOT") };
194}
195
196impl super::Vm {
197    /// Execute a single opcode. Used by the scope-interrupt wrapper that
198    /// drives cancellable / deadlined execution; the hot interpreter loop
199    /// in `run_chunk_ref` bypasses this and calls `execute_op_sync` /
200    /// `execute_op_async` directly when no interrupt machinery is armed.
201    pub(super) async fn execute_op(&mut self, op_byte: u8) -> Result<Option<VmValue>, VmError> {
202        let op = Op::from_byte(op_byte).ok_or(VmError::InvalidInstruction(op_byte))?;
203        if let Some(result) = self.execute_op_sync(op) {
204            result?;
205            return Ok(None);
206        }
207        self.execute_op_async(op).await?;
208        Ok(None)
209    }
210}