byteflow/bytecode/opcode.rs
1//! The Byteflow instruction set architecture (ISA v0).
2//!
3//! Byteflow is register-based (à la Lua 5.x / Dalvik) rather than stack-based
4//! (à la JVM/CPython). Register machines need roughly 40-50% fewer dispatched
5//! instructions than an equivalent stack machine because they avoid PUSH/POP
6//! traffic for every intermediate value, at the cost of slightly larger
7//! instruction words. For an interpreter whose steady-state cost is dominated
8//! by dispatch (branch prediction + icache misses), fewer instructions per
9//! logical operation wins.
10//!
11//! Every opcode fits in a single byte so a `Vec<Instruction>` is dense and
12//! the dispatch table (see `byteflow-vm::interp`) can be a flat jump table
13//! indexed directly by discriminant, with no bounds check in release builds
14//! (enforced instead at decode/verification time, see [`crate::verify`]).
15
16/// A single Byteflow opcode.
17///
18/// Numeric values are part of the stable on-disk ABI (`byteflow-bytecode`
19/// module format, see [`super::chunk::MAGIC`]) — never renumber an existing
20/// variant, only append.
21#[repr(u8)]
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
23pub enum Opcode {
24 /// Stop the current Flow's VM loop. Terminal state.
25 Halt = 0x00,
26
27 // ---- data movement ----------------------------------------------------
28 /// `LoadConst ra, imm` → `r[a] = constants[imm]`
29 LoadConst = 0x01,
30 /// `Move ra, rb` → `r[a] = r[b]`
31 Move = 0x02,
32 /// `LoadImm ra, imm` → `r[a] = imm as i64` (fast path, skips const pool)
33 LoadImm = 0x03,
34
35 // ---- arithmetic (integer; float variants share the same encoding
36 // ---- but operate on Value::Float, selected by the operand's runtime tag)
37 Add = 0x10,
38 Sub = 0x11,
39 Mul = 0x12,
40 Div = 0x13,
41 Mod = 0x14,
42 Neg = 0x15,
43
44 // ---- comparison → writes a Value::Bool into ra
45 Eq = 0x18,
46 Lt = 0x19,
47 Le = 0x1A,
48
49 // ---- control flow -------------------------------------------------
50 /// `Jump imm` → unconditional relative jump (imm = signed offset in
51 /// instructions from the *next* pc).
52 Jump = 0x20,
53 /// `Branch ra, imm` → jump by `imm` iff `r[a]` is falsy (Bool(false),
54 /// Unit, or Int(0)). This is the only conditional branch; `if/else` and
55 /// loops both lower to Branch + Jump, keeping the interpreter's branch
56 /// predictor state small.
57 Branch = 0x21,
58
59 // ---- procedure calls (native Rust functions registered via FFI, or
60 // ---- other bytecode functions in the same chunk) ------------------
61 /// `Call ra, fb, nc` → call function `fb` with `nc` arguments taken from
62 /// `r[a..a+nc]`, result written back into `r[a]`.
63 Call = 0x30,
64 /// `Return ra` → return `r[a]` to the caller frame (or complete the
65 /// Flow if this is the outermost frame).
66 Return = 0x31,
67 /// `CallNative ra, fb, nc` → like `Call` but `fb` indexes the native
68 /// function table instead of the bytecode function table.
69 CallNative = 0x32,
70
71 // ---- Flow model --------------------------------------------------
72 /// `Spawn ra, fb, nc` → create a new virtual Flow starting at
73 /// function `fb`, passing `nc` arguments taken from `r[a+1..a+1+nc]`
74 /// (deliberately *not* overlapping `r[a]` itself, which is where the
75 /// scheduler writes a **Cap** to the child once created — FlowCap;
76 /// see [`crate::VmResult::Spawn`]).
77 Spawn = 0x40,
78 /// `Yield` → cooperative yield. Control returns to the scheduler, the
79 /// Flow is re-enqueued as `Ready` and may resume on any worker.
80 Yield = 0x41,
81 /// `Sleep ra` → suspend until `r[a]` (interpreted as milliseconds,
82 /// Value::Int) has elapsed. Registered on the timer wheel.
83 Sleep = 0x42,
84 /// `Exit ra` → terminate the Flow, `r[a]` is delivered to `.join()`.
85 Exit = 0x43,
86 /// `SelfPid ra` → `r[a] =` a **self Cap**
87 /// ([`CapRights::ADDRESSING`](crate::CapRights::ADDRESSING) =
88 /// `SEND|ASK|LINK|MONITOR`) for this flow.
89 /// (Opcode name kept for ABI; the value is `Value::Cap`, not Pid.)
90 /// The VM does not store its own id (it has no scheduler state); this
91 /// is a scheduler effect, same class as `Spawn`/`Receive`.
92 SelfPid = 0x44,
93
94 // ---- messaging --------------------------------------------------------
95 /// `Send ra, rb` → Atomic Hop: deliver `r[b]` (`Message`) to the Cap in
96 /// `r[a]`. With a full `Reject` inbox the sender parks (`WAITING_SEND`);
97 /// otherwise the hop is queued / handed off without blocking the worker
98 /// (see [`crate::docs::mailbox`]).
99 ///
100 /// VM requires Cap + Message; worker resolves Cap (SEND), stamps sender
101 /// + `reply_cap`, then pushes to the resolved mailbox.
102 Send = 0x50,
103 /// `Receive ra` → pop the next message into `r[a]`; if the mailbox is
104 /// empty, suspends the Flow in `Waiting` state until a message
105 /// arrives.
106 Receive = 0x51,
107 /// `ReceiveTimeout ra, rb` → like `Receive` but gives up after `r[b]`
108 /// milliseconds, writing `Value::Unit` into `r[a]` on timeout.
109 ReceiveTimeout = 0x52,
110 /// `ReceiveMatch ra, rb` → **Atomic Hop selective receive**: block until
111 /// a [`crate::Value::Message`] with `tag == r[b]` (as `u16`) is available.
112 /// Non-matching hops stay in the mailbox in FIFO order (skip, don't drop).
113 ReceiveMatch = 0x53,
114 /// `ReceiveMatchImm ra, imm` → like `ReceiveMatch` with an immediate tag
115 /// (`imm` must fit in `u16`).
116 ReceiveMatchImm = 0x54,
117 /// `Ask ra, rb, rc` → **atomic request/reply hop**.
118 ///
119 /// 1. Validate `r[b]` as Cap and `r[c]` as Message (VM).
120 /// 2. Scheduler resolves Cap (ASK), stamps `sender` + mints `reply_cap`.
121 /// 3. Deliver the request to the resolved FlowId (like `Send`).
122 /// 4. Suspend until a reply hop matches
123 /// `request_id == request.request_id && sender == resolved_FlowId`.
124 /// 5. Write the reply `Message` into `r[a]`.
125 ///
126 /// Append-only ABI slot (`0x55`).
127 Ask = 0x55,
128 /// `Monitor ra, rb` → install a one-way watch on Cap `r[b]`; write
129 /// [`crate::MonitorRef`] as `Int` into `r[a]`.
130 Monitor = 0x56,
131 /// `Demonitor ra` → drop the monitor in `r[a]` (`Int` ref).
132 Demonitor = 0x57,
133 /// `Link ra, rb` → bidirectional link with Cap `r[b]`; write
134 /// [`crate::LinkId`] as `Int` into `r[a]`.
135 Link = 0x58,
136 /// `Unlink ra` → drop the link in `r[a]` (`Int` id).
137 Unlink = 0x59,
138 /// `AskTimeout ra, rb, rc, imm` → like `Ask`, but give up after
139 /// `r[imm]` milliseconds and write `Value::Unit` into `ra`.
140 ///
141 /// Encoding: `a=dest`, `b=cap`, `c=msg`, `imm=millis_reg`.
142 /// Append-only ABI slot (`0x5A`); `Trap` stays `0x60`.
143 AskTimeout = 0x5A,
144 /// `Delegate ra, rb, rights, rc?` → write an attenuated Cap into `r[a]`.
145 ///
146 /// `r[b]` is the source Cap; `imm` is the requested rights mask;
147 /// `c = 255` means no extra native-mask narrowing. The scheduler
148 /// calls `Cap::attenuate` — the only derivation path.
149 /// Append-only ABI slot (`0x5B`); `Trap` stays `0x60`.
150 Delegate = 0x5B,
151 /// `FreshRequestId ra` → `r[a] =` next per-flow correlation id (`Int`).
152 ///
153 /// Starts at 1; `0` is reserved as “unset” so `Send` / `Ask` can mint.
154 /// Not a capability — uniqueness, not unpredictability.
155 /// Append-only ABI slot (`0x5C`); `Trap` stays `0x60`.
156 FreshRequestId = 0x5C,
157 /// `ReceiveMatchCorr ra, rb, rc` → wait for a hop with
158 /// `tag == r[b]` and `request_id == r[c]`. Non-matching hops stay queued.
159 /// Append-only ABI slot (`0x5D`); `Trap` stays `0x60`.
160 ReceiveMatchCorr = 0x5D,
161 /// `ReceiveMatchCorrImm ra, rb, imm` → like [`Self::ReceiveMatchCorr`]
162 /// with immediate tag (`imm` as `u16`) and `request_id` from `r[b]`.
163 /// Append-only ABI slot (`0x5E`); `Trap` stays `0x60`.
164 ReceiveMatchCorrImm = 0x5E,
165 /// `SetTrapExit ra` → BEAM `process_flag(trap_exit, r[a])`.
166 ///
167 /// Truthy `r[a]` (same rules as [`Opcode::Branch`]) enables trapping:
168 /// this flow receives [`crate::TAG_SYS_EXIT`] when a linked peer exits
169 /// (including `Normal`), instead of being killed / silently dropping the
170 /// link.
171 /// Append-only ABI slot (`0x5F`); `Trap` stays `0x60`.
172 SetTrapExit = 0x5F,
173 /// `RegisterName ra` → publish `r[a]` (`Str`) as this flow's name.
174 /// Only the calling flow is registered. Requires `SEND` on self-authority
175 /// (confined spawn cannot squat names). Append-only (`0x62`).
176 RegisterName = 0x62,
177 /// `Whereis ra, rb` → look up `r[b]` (`Str`); write a **SEND** Cap for
178 /// the caller, or `Unit` if missing. Never a raw FlowId. (`0x63`)
179 Whereis = 0x63,
180 /// `ReceiveMatchKind ra, imm` → block until a [`crate::Value::Message`]
181 /// whose `payload.wire_tag() == imm` (BFV0 tag `0..=10`) is available.
182 /// Non-matching hops stay queued (FIFO skip). Append-only (`0x64`);
183 /// `Trap` stays `0x60`.
184 ReceiveMatchKind = 0x64,
185 /// `SetRestartPolicy imm` → set this flow's supervisor restart policy:
186 /// `0` = Always, `1` = OnFailure, `2` = Never. Invalid `imm` → Trap.
187 /// Append-only (`0x65`); `Trap` stays `0x60`.
188 SetRestartPolicy = 0x65,
189 /// `HostAwait ra, rb, imm` → register a host operation `(op=imm, args=r[b])`
190 /// and write its ticket id (`Int`) into `r[a]`. The flow keeps running.
191 /// The host completes with a [`crate::TAG_SYS_AWAIT`] hop (`request_id`
192 /// = ticket) that bytecode reads with `Receive`. Requires a bridge on
193 /// [`crate::RuntimeConfig`]; otherwise the flow fails.
194 /// Append-only (`0x66`); not a `CallNative` / std-native slot.
195 HostAwait = 0x66,
196 /// `ReceiveMatchTimeout ra, rb, rc` → like [`Self::ReceiveMatch`] but
197 /// give up after `r[c]` milliseconds and write `Value::Unit` (BEAM
198 /// `receive … after`). Non-matching hops stay queued. Append-only (`0x67`).
199 ReceiveMatchTimeout = 0x67,
200 /// `ReceiveMatchImmTimeout ra, rb, imm` → like [`Self::ReceiveMatchImm`]
201 /// with timeout register `r[b]` (imm = tag). Writes `Unit` on expiry.
202 /// Append-only (`0x68`).
203 ReceiveMatchImmTimeout = 0x68,
204 /// `IsAlive ra, rb` → `r[a] = Bool`: the Cap in `r[b]` is held by this
205 /// flow and still names a live flow (BEAM `is_process_alive/1`).
206 /// Unknown / not-held / revoked / non-flow target → `false` (no trap).
207 /// Append-only (`0x69`).
208 IsAlive = 0x69,
209 /// `ExitSignal ra, rb` → BEAM `exit/2`: signal the flow addressed by
210 /// Cap `r[a]` (needs `LINK`) with reason `r[b]` (`FlowExitReason` as `Int`).
211 /// `Normal` is ignored. `Killed` cannot be trapped. Other reasons become
212 /// a [`crate::TAG_SYS_EXIT`] hop when the target has `trap_exit`.
213 /// Append-only (`0x6A`).
214 ExitSignal = 0x6A,
215 /// `DictPut ra, rb, rc` → process dictionary: `r[a] = old`, key `r[b]`
216 /// (`Int`), value `r[c]` (scalar, `Str`, or `Bytes`). `Str` / `Bytes`
217 /// are charged on the flow quota for as long as the dictionary holds
218 /// them. Missing old → `Unit`. Append-only (`0x6B`).
219 DictPut = 0x6B,
220 /// `DictGet ra, rb` → `r[a] = dict[r[b]]` or `Unit`. Append-only (`0x6C`).
221 DictGet = 0x6C,
222 /// `DictErase ra, rb` → remove key `r[b]`, write the old value or `Unit`
223 /// into `r[a]`. Append-only (`0x6D`).
224 DictErase = 0x6D,
225 /// `ReceiveMatchEq ra, rb, rc` → selective receive: `tag == r[b]` and
226 /// payload `Int == r[c]`. Non-matching hops stay queued. Append-only (`0x6E`).
227 ReceiveMatchEq = 0x6E,
228 /// `StartChild ra, policy, fn` → spawn function `imm` as a supervised
229 /// child (`b` = restart policy `0..=2`). Writes an addressing Cap into
230 /// `r[a]`. Attaches to the supervisor this flow started with
231 /// [`Self::StartSupervisor`], otherwise to the host supervisor that
232 /// started this flow. With neither, the flow fails. Append-only (`0x6F`).
233 StartChild = 0x6F,
234 /// `ReceiveMatchValue ra, rb, rc` → selective receive: `tag == r[b]` and
235 /// payload structurally equal to `r[c]` (any [`crate::Value`], not only
236 /// `Int`). Non-matching hops stay queued. Append-only (`0x70`).
237 ReceiveMatchValue = 0x70,
238 /// `SendRemote ra, rb, rc` → deliver hop `r[c]` to node `r[a]` (`Int`)
239 /// service `r[b]` (`Str`). The host drains [`crate::DistributionBridge`].
240 /// Byteflow does not open a socket. Append-only (`0x71`).
241 SendRemote = 0x71,
242 /// `StartSupervisor imm` → start a supervisor owned by this flow.
243 /// `imm`: `0` one-for-one, `1` one-for-all, `2` rest-for-one.
244 /// Later `StartChild` attaches to it. Append-only (`0x72`).
245 StartSupervisor = 0x72,
246 /// `AddImm ra, rb, imm` → `r[a] = r[b] + imm` (`imm` sign-extended to
247 /// `i64`). Integer add wraps; a float in `r[b]` adds in `f64`. One
248 /// instruction instead of `LoadImm` plus `Add`, and no scratch register.
249 /// Append-only (`0x73`).
250 AddImm = 0x73,
251 /// `CallNativeFrom ra, rb, native` → `r[a] = native_1(r[b])`.
252 ///
253 /// Same FFI slot as [`Self::CallNative`] (`imm` is the native index) but
254 /// the argument is `r[b]` and the result lands in `r[a]`. `r[b]` survives
255 /// when `a != b`, so unpacking a `Message` does not need a preceding
256 /// `Move`. Append-only (`0x74`).
257 CallNativeFrom = 0x74,
258 /// `SubImm ra, rb, imm` → `r[a] = r[b] - imm` (`imm` sign-extended to
259 /// `i64`). Integer subtraction wraps; a float in `r[b]` subtracts in
260 /// `f64`. Append-only (`0x75`).
261 SubImm = 0x75,
262 /// `LtImm ra, rb, imm` → `r[a] = r[b] < imm` as `Bool`. Integers compare
263 /// as signed `i64`. Floats compare as `f64` (NaN is not less). `Bool`
264 /// is not a number here. Append-only (`0x76`).
265 LtImm = 0x76,
266 /// `ListPack ra, rb, imm` → `r[a]` becomes a list of `imm` values
267 /// starting at `r[b]`. `imm` is `0..=32`. Append-only (`0x77`).
268 ListPack = 0x77,
269 /// `ListGet ra, rb, imm` → `r[a] = r[b][imm]`. `r[b]` must be a list
270 /// and `imm` a valid index. Append-only (`0x78`).
271 ListGet = 0x78,
272 /// `ListLen ra, rb` → `r[a] = len(r[b])` as `Int`. Append-only (`0x79`).
273 ListLen = 0x79,
274 /// `MulImm ra, rb, imm` → `r[a] = r[b] * imm` (`imm` sign-extended to
275 /// `i64`). Integer multiply wraps; a float in `r[b]` multiplies in
276 /// `f64`. Append-only (`0x7A`).
277 MulImm = 0x7A,
278 /// `EqImm ra, rb, imm` → `r[a] = Bool(r[b] == Int(imm))`.
279 ///
280 /// The immediate is an integer. A float, bool, or string in `r[b]`
281 /// compares unequal, the same as [`Self::Eq`] against a `LoadImm`.
282 /// Append-only (`0x7B`).
283 EqImm = 0x7B,
284 /// `LeImm ra, rb, imm` → `r[a] = r[b] <= imm` as `Bool`. Integers
285 /// compare as signed `i64`. Floats compare as `f64` (NaN is not less
286 /// or equal). `Bool` is not a number here. Append-only (`0x7C`).
287 LeImm = 0x7C,
288 /// `BitAnd ra, rb, rc` → `r[a] = r[b] & r[c]`. Both operands must be
289 /// `Int`. Append-only (`0x7D`).
290 BitAnd = 0x7D,
291 /// `BitOr ra, rb, rc` → `r[a] = r[b] | r[c]`. Both operands must be
292 /// `Int`. Append-only (`0x7E`).
293 BitOr = 0x7E,
294 /// `BitXor ra, rb, rc` → `r[a] = r[b] ^ r[c]`. Both operands must be
295 /// `Int`. Append-only (`0x7F`).
296 BitXor = 0x7F,
297 /// `BitNot ra, rb` → `r[a] = !r[b]`. `r[b]` must be `Int`.
298 /// Append-only (`0x80`).
299 BitNot = 0x80,
300 /// `Shl ra, rb, rc` → `r[a] = r[b] << (r[c] & 63)`. Both operands must
301 /// be `Int`. The shift count keeps its low 6 bits, so a negative count
302 /// still names a shift in `0..64`. Append-only (`0x81`).
303 Shl = 0x81,
304 /// `Shr ra, rb, rc` → arithmetic `r[a] = r[b] >> (r[c] & 63)`. Both
305 /// operands must be `Int`. The sign bit fills the vacated bits.
306 /// Append-only (`0x82`).
307 Shr = 0x82,
308 /// `ReceiveMatchCmp ra, rb, rc, imm` → wait for tag `r[b]` and an `Int`
309 /// payload compared with `r[c]` by `imm` (`0` eq, `1` lt, `2` le).
310 /// A hop that fails the comparison stays queued. Append-only (`0x83`).
311 ReceiveMatchCmp = 0x83,
312 /// `MapPack ra, rb, imm` → `r[a]` becomes a map of `imm` pairs starting
313 /// at `r[b]` (`key`, `value`, `key`, `value`, …). `imm` is `0..=32`.
314 /// Keys are `Int` or `Str`. A duplicate key traps. Append-only (`0x84`).
315 MapPack = 0x84,
316 /// `MapGet ra, rb, rc` → `r[a] = r[b][r[c]]`. Missing key writes `Unit`.
317 /// Append-only (`0x85`).
318 MapGet = 0x85,
319 /// `MapLen ra, rb` → `r[a] = len(r[b])` as `Int`. Append-only (`0x86`).
320 MapLen = 0x86,
321 /// `TableOpen ra, rb` → this flow's table, named by the `Str` in `r[b]`.
322 /// Writes a Cap (`SEND|RECV`, target = that table) into `r[a]`.
323 /// A second open traps. Append-only (`0x87`).
324 TableOpen = 0x87,
325 /// `TableGet ra, rb, rc` → `r[a]` is the row of cap `r[b]` at key `r[c]`.
326 /// Requires `RECV`. Missing key writes `Unit`. Append-only (`0x88`).
327 TableGet = 0x88,
328 /// `TablePut ra, rb, rc` → store `r[c]` in cap `r[a]` at key `r[b]`.
329 /// Requires `SEND`. Append-only (`0x89`).
330 TablePut = 0x89,
331
332 // ---- diagnostics / safety ------------------------------------------
333 /// `Trap imm` → deliberate fault (assertion failure, div-by-zero, bad
334 /// opcode encountered by a corrupt/foreign module, capability
335 /// violation). Propagates to the Flow supervisor as [`crate::FlowOutcome::Failed`].
336 Trap = 0x60,
337 /// `Nop` → no-op, used by the assembler to pad jump targets.
338 Nop = 0x61,
339}
340
341impl Opcode {
342 /// Decode a raw byte into an `Opcode`, used when loading foreign/untrusted
343 /// modules. Rejects anything outside the currently defined ISA rather
344 /// than transmuting garbage into a jump-table index (which is exactly
345 /// the class of bug that turns a VM into a code-execution primitive).
346 #[inline]
347 pub fn from_u8(byte: u8) -> Option<Opcode> {
348 use Opcode::*;
349 Some(match byte {
350 0x00 => Halt,
351 0x01 => LoadConst,
352 0x02 => Move,
353 0x03 => LoadImm,
354 0x10 => Add,
355 0x11 => Sub,
356 0x12 => Mul,
357 0x13 => Div,
358 0x14 => Mod,
359 0x15 => Neg,
360 0x18 => Eq,
361 0x19 => Lt,
362 0x1A => Le,
363 0x20 => Jump,
364 0x21 => Branch,
365 0x30 => Call,
366 0x31 => Return,
367 0x32 => CallNative,
368 0x40 => Spawn,
369 0x41 => Yield,
370 0x42 => Sleep,
371 0x43 => Exit,
372 0x44 => SelfPid,
373 0x50 => Send,
374 0x51 => Receive,
375 0x52 => ReceiveTimeout,
376 0x53 => ReceiveMatch,
377 0x54 => ReceiveMatchImm,
378 0x55 => Ask,
379 0x56 => Monitor,
380 0x57 => Demonitor,
381 0x58 => Link,
382 0x59 => Unlink,
383 0x5A => AskTimeout,
384 0x5B => Delegate,
385 0x5C => FreshRequestId,
386 0x5D => ReceiveMatchCorr,
387 0x5E => ReceiveMatchCorrImm,
388 0x5F => SetTrapExit,
389 0x60 => Trap,
390 0x61 => Nop,
391 0x62 => RegisterName,
392 0x63 => Whereis,
393 0x64 => ReceiveMatchKind,
394 0x65 => SetRestartPolicy,
395 0x66 => HostAwait,
396 0x67 => ReceiveMatchTimeout,
397 0x68 => ReceiveMatchImmTimeout,
398 0x69 => IsAlive,
399 0x6A => ExitSignal,
400 0x6B => DictPut,
401 0x6C => DictGet,
402 0x6D => DictErase,
403 0x6E => ReceiveMatchEq,
404 0x6F => StartChild,
405 0x70 => ReceiveMatchValue,
406 0x71 => SendRemote,
407 0x72 => StartSupervisor,
408 0x73 => AddImm,
409 0x74 => CallNativeFrom,
410 0x75 => SubImm,
411 0x76 => LtImm,
412 0x77 => ListPack,
413 0x78 => ListGet,
414 0x79 => ListLen,
415 0x7A => MulImm,
416 0x7B => EqImm,
417 0x7C => LeImm,
418 0x7D => BitAnd,
419 0x7E => BitOr,
420 0x7F => BitXor,
421 0x80 => BitNot,
422 0x81 => Shl,
423 0x82 => Shr,
424 0x83 => ReceiveMatchCmp,
425 0x84 => MapPack,
426 0x85 => MapGet,
427 0x86 => MapLen,
428 0x87 => TableOpen,
429 0x88 => TableGet,
430 0x89 => TablePut,
431 _ => return None,
432 })
433 }
434
435 #[inline]
436 pub fn as_u8(self) -> u8 {
437 self as u8
438 }
439}
440
441impl std::fmt::Display for Opcode {
442 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
443 let name = match self {
444 Opcode::Halt => "Halt",
445 Opcode::LoadConst => "LoadConst",
446 Opcode::Move => "Move",
447 Opcode::LoadImm => "LoadImm",
448 Opcode::Add => "Add",
449 Opcode::AddImm => "AddImm",
450 Opcode::SubImm => "SubImm",
451 Opcode::LtImm => "LtImm",
452 Opcode::ListPack => "ListPack",
453 Opcode::ListGet => "ListGet",
454 Opcode::ListLen => "ListLen",
455 Opcode::MulImm => "MulImm",
456 Opcode::EqImm => "EqImm",
457 Opcode::LeImm => "LeImm",
458 Opcode::BitAnd => "BitAnd",
459 Opcode::BitOr => "BitOr",
460 Opcode::BitXor => "BitXor",
461 Opcode::BitNot => "BitNot",
462 Opcode::Shl => "Shl",
463 Opcode::Shr => "Shr",
464 Opcode::ReceiveMatchCmp => "ReceiveMatchCmp",
465 Opcode::MapPack => "MapPack",
466 Opcode::MapGet => "MapGet",
467 Opcode::MapLen => "MapLen",
468 Opcode::TableOpen => "TableOpen",
469 Opcode::TableGet => "TableGet",
470 Opcode::TablePut => "TablePut",
471 Opcode::Sub => "Sub",
472 Opcode::Mul => "Mul",
473 Opcode::Div => "Div",
474 Opcode::Mod => "Mod",
475 Opcode::Neg => "Neg",
476 Opcode::Eq => "Eq",
477 Opcode::Lt => "Lt",
478 Opcode::Le => "Le",
479 Opcode::Jump => "Jump",
480 Opcode::Branch => "Branch",
481 Opcode::Call => "Call",
482 Opcode::Return => "Return",
483 Opcode::CallNative => "CallNative",
484 Opcode::CallNativeFrom => "CallNativeFrom",
485 Opcode::Spawn => "Spawn",
486 Opcode::Yield => "Yield",
487 Opcode::Sleep => "Sleep",
488 Opcode::Exit => "Exit",
489 Opcode::SelfPid => "SelfPid",
490 Opcode::Send => "Send",
491 Opcode::Receive => "Receive",
492 Opcode::ReceiveTimeout => "ReceiveTimeout",
493 Opcode::ReceiveMatch => "ReceiveMatch",
494 Opcode::ReceiveMatchImm => "ReceiveMatchImm",
495 Opcode::Ask => "Ask",
496 Opcode::Monitor => "Monitor",
497 Opcode::Demonitor => "Demonitor",
498 Opcode::Link => "Link",
499 Opcode::Unlink => "Unlink",
500 Opcode::AskTimeout => "AskTimeout",
501 Opcode::Delegate => "Delegate",
502 Opcode::FreshRequestId => "FreshRequestId",
503 Opcode::ReceiveMatchCorr => "ReceiveMatchCorr",
504 Opcode::ReceiveMatchCorrImm => "ReceiveMatchCorrImm",
505 Opcode::SetTrapExit => "SetTrapExit",
506 Opcode::RegisterName => "RegisterName",
507 Opcode::Whereis => "Whereis",
508 Opcode::ReceiveMatchKind => "ReceiveMatchKind",
509 Opcode::SetRestartPolicy => "SetRestartPolicy",
510 Opcode::HostAwait => "HostAwait",
511 Opcode::ReceiveMatchTimeout => "ReceiveMatchTimeout",
512 Opcode::ReceiveMatchImmTimeout => "ReceiveMatchImmTimeout",
513 Opcode::IsAlive => "IsAlive",
514 Opcode::ExitSignal => "ExitSignal",
515 Opcode::DictPut => "DictPut",
516 Opcode::DictGet => "DictGet",
517 Opcode::DictErase => "DictErase",
518 Opcode::ReceiveMatchEq => "ReceiveMatchEq",
519 Opcode::StartChild => "StartChild",
520 Opcode::ReceiveMatchValue => "ReceiveMatchValue",
521 Opcode::SendRemote => "SendRemote",
522 Opcode::StartSupervisor => "StartSupervisor",
523 Opcode::Trap => "Trap",
524 Opcode::Nop => "Nop",
525 };
526 f.write_str(name)
527 }
528}
529
530/// Integer comparison used by [`Opcode::ReceiveMatchCmp`].
531#[derive(Clone, Copy, Debug, PartialEq, Eq)]
532pub enum IntCmp {
533 Eq,
534 Lt,
535 Le,
536}
537
538impl IntCmp {
539 pub const fn from_imm(imm: i32) -> Option<Self> {
540 match imm {
541 0 => Some(Self::Eq),
542 1 => Some(Self::Lt),
543 2 => Some(Self::Le),
544 _ => None,
545 }
546 }
547
548 pub const fn as_imm(self) -> i32 {
549 match self {
550 Self::Eq => 0,
551 Self::Lt => 1,
552 Self::Le => 2,
553 }
554 }
555
556 pub fn holds(self, payload: i64, bound: i64) -> bool {
557 match self {
558 Self::Eq => payload == bound,
559 Self::Lt => payload < bound,
560 Self::Le => payload <= bound,
561 }
562 }
563}