submilli-wasm 0.2.0

A WebAssembly interpreter with a wasmtime-compatible API, built for fast compilation and startup
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//! The flat opcode dispatch: one `step` per instruction, delegating to the per-category handlers.
//! See ARCHITECTURE §7. The big `match` is the interpreter's single sanctioned long function.

// `f*.eq`/`f*.ne` are exact IEEE comparisons by definition.
#![allow(clippy::float_cmp)]

use super::call::{self, CallKind};
use super::cell::Cell;
use super::outcome::CALL_INTERNAL;
use super::stack::{Bin, Un};
use super::{arith, cell, convert, memory, Execution, StepOutcome, CONTROL};
use crate::instance::Instance;
use crate::module::code::Code;
use crate::module::op::{BranchTarget, CmpKind, Op, Slot};
use crate::store::StoreInner;
use crate::trap::Trap;
use crate::Result;

impl Execution {
    // Flat opcode dispatch; arms are short. `inline(always)` is deliberate: `step` has a single
    // call site (the `run` loop), and inlining it there removes the per-op call/return and the
    // `Result<StepOutcome>` stack round-trip, keeping `ip`/`base` in registers across ops —
    // measured ~1.8× on CoreMark. The op arrives already fetched (`run` gets it via `ops.get`, so
    // there is exactly one bounds check per op); `next` is the fall-through ip. Unoptimized builds
    // keep it out of line: there every arm's temporaries get their own stack slot (~43 KB), which
    // inlined would stay live under each host call, so each host re-entry would cost ~4× the
    // native stack (charged against `max_wasm_stack`, see `exec::native`).
    #[allow(
        clippy::too_many_lines,
        clippy::inline_always,
        clippy::too_many_arguments
    )]
    #[allow(clippy::cognitive_complexity)] // flat bytecode dispatch; arms delegate by category
    #[cfg_attr(not(submilli_unoptimized), inline(always))]
    #[cfg_attr(submilli_unoptimized, inline(never))]
    pub(super) fn step(
        &mut self,
        inner: &mut StoreInner,
        code: &Code,
        op: &Op,
        next: u32,
        base: u32,
        instance: Instance,
    ) -> Result<u32> {
        match op {
            Op::Nop => {}
            Op::Unreachable => return Err(Trap::UnreachableCodeReached.into()),
            Op::I32Const(s, v) => self.set_scalar(slot(base, *s), Cell::from_i32(*v))?,
            Op::I64Const(s, v) => self.set_scalar(slot(base, *s), Cell::from_i64(*v))?,
            Op::F32Const(s, v) => {
                self.set_scalar(slot(base, *s), Cell::of_bytes(v.to_le_bytes()))?;
            }
            Op::F64Const(s, v) => {
                self.set_scalar(slot(base, *s), Cell::of_bytes(v.to_le_bytes()))?;
            }
            Op::Select(s) => self.select_at(slot(base, *s))?,
            Op::Copy(dst, src) => self.copy_slot(slot(base, *src), slot(base, *dst))?,
            Op::GlobalGet(s, g) => {
                let v = inner
                    .global(index_space::global(inner, instance, *g)?)
                    .value;
                self.set_val(slot(base, *s), v)?;
            }
            Op::GlobalSet(s, g) => {
                let handle = index_space::global(inner, instance, *g)?;
                let v = self.take_typed(slot(base, *s), inner.global(handle).ty.content())?;
                inner.global_mut(handle).value = v;
            }
            Op::Br(t, s) => return self.branch_to(*t, slot(base, *s)),
            Op::BrIf(t, s) => {
                let at = slot(base, *s);
                if self.cond_at(at)? != 0 {
                    return self.branch_to(*t, at);
                }
            }
            Op::BrIfNot(t, s) => {
                let at = slot(base, *s);
                if self.cond_at(at)? == 0 {
                    return self.branch_to(*t, at);
                }
            }
            Op::BrIfCmp {
                kind,
                negate,
                ip,
                lhs,
                rhs,
            } => {
                let a = self.scalar_at(slot(base, *lhs))?.unwrap_i32();
                let b = self.scalar_at(slot(base, *rhs))?.unwrap_i32();
                if cmp_i32(*kind, a, b) != *negate {
                    return Ok(*ip);
                }
            }
            Op::BrIfCmpImm {
                kind,
                negate,
                ip,
                lhs,
                imm,
            } => {
                let a = self.scalar_at(slot(base, *lhs))?.unwrap_i32();
                if cmp_i32(*kind, a, *imm) != *negate {
                    return Ok(*ip);
                }
            }
            Op::BrTable(range, s) => {
                // Targets live out-of-line in `code.br_tables`: `len` cases then the default.
                let pool = code.br_tables()?;
                let end = range.base.checked_add(range.len).ok_or(
                    crate::error::InternalError::Compiler("branch table range overflow"),
                )? as usize;
                let cases = pool.get(range.base as usize..end).ok_or(
                    crate::error::InternalError::Compiler("branch table range is invalid"),
                )?;
                let default = pool.get(end).ok_or(crate::error::InternalError::Compiler(
                    "branch table default is missing",
                ))?;
                let at = slot(base, *s);
                let i = self.cond_at(at)? as u32 as usize;
                return self.branch_to(*cases.get(i).unwrap_or(default), at);
            }
            // The run loop makes the call (it re-reads the op): see `CALL_INTERNAL`.
            Op::CallInternal(..) => return Ok(CALL_INTERNAL),
            call_op @ (Op::Call(f, s) | Op::ReturnCall(f, s)) => {
                let callee = index_space::func(inner, instance, *f)?;
                let kind = call_kind(matches!(call_op, Op::ReturnCall(..)), next);
                let resolved = call::resolve(inner, callee)?;
                let outcome = call::call_outcome(inner, resolved, instance, kind, slot(base, *s));
                return Ok(self.control(outcome));
            }
            Op::CallIndirect { type_idx, table, s } => {
                let (kind, at) = (CallKind::Nested(next), slot(base, *s));
                let outcome =
                    self.do_call_indirect(inner, instance, (*type_idx, *table), kind, at)?;
                return Ok(self.control(outcome));
            }
            Op::ReturnCallIndirect { type_idx, table, s } => {
                let (kind, at) = (CallKind::Tail, slot(base, *s));
                let outcome =
                    self.do_call_indirect(inner, instance, (*type_idx, *table), kind, at)?;
                return Ok(self.control(outcome));
            }
            Op::CallRef(_, s) => {
                let at = slot(base, *s);
                let outcome = self.do_call_ref(inner, instance, CallKind::Nested(next), at)?;
                return Ok(self.control(outcome));
            }
            Op::ReturnCallRef(_, s) => {
                let at = slot(base, *s);
                let outcome = self.do_call_ref(inner, instance, CallKind::Tail, at)?;
                return Ok(self.control(outcome));
            }
            Op::BrOnNull(t, s) => {
                let at = slot(base, *s);
                if self.ref_at(at)?.0.is_null() {
                    self.take_branch(*t, at)?; // the reference is consumed
                    return Ok(t.ip);
                }
                // non-null: the reference stays in its slot; fall through
            }
            Op::BrOnNonNull(t, s) => {
                let at = slot(base, *s);
                if !self.ref_at(at)?.0.is_null() {
                    self.take_branch(*t, at + 1)?; // the reference is kept for the target
                    return Ok(t.ip);
                } // null: dropped; fall through
            }
            Op::MemoryGrow(i, s) => {
                // Routed through the driver so the (T-generic) limiter is consulted; it writes the
                // old size (or -1) back over the delta's slot.
                let at = slot(base, *s);
                let memory = index_space::memory(inner, instance, *i)?;
                let delta = self.index_at(at, inner.memory(memory).ty.is_64())?;
                return Ok(self.control(StepOutcome::Grow {
                    memory,
                    delta,
                    at,
                    return_ip: next,
                }));
            }
            Op::TableGrow(t, s) => {
                // Routed through the driver (limiter-consulted), like `memory.grow`; operands
                // `init, delta`, and the old size (or -1) replaces them.
                let at = slot(base, *s);
                let table = index_space::table(inner, instance, *t)?;
                let tt = &inner.table(table).ty;
                let (is_64, kind) = (tt.is_64(), cell::refkind_of_heap(tt.element().heap_type()));
                let init = self.take_ref(at, kind)?.to_ref();
                let delta = self.index_at(at + 1, is_64)?;
                return Ok(self.control(StepOutcome::TableGrow {
                    table,
                    delta,
                    init,
                    at,
                    return_ip: next,
                }));
            }
            // Loads/stores: flat here (a second-level `match` per category cost a second
            // indirect branch per op). The address slot is the op's slot; a load's value replaces it.
            Op::I32Load(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<4>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(slot(base, m.dst), Cell::from_i32(i32::from_le_bytes(b)))?;
            }
            Op::I64Load(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<8>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(slot(base, m.dst), Cell::from_i64(i64::from_le_bytes(b)))?;
            }
            Op::F32Load(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<4>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(slot(base, m.dst), Cell::of_bytes(b))?;
            }
            Op::F64Load(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<8>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(slot(base, m.dst), Cell::of_bytes(b))?;
            }
            Op::I32Load8S(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<1>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(slot(base, m.dst), Cell::from_i32(i32::from(b[0] as i8)))?;
            }
            Op::I32Load8U(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<1>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(slot(base, m.dst), Cell::from_i32(i32::from(b[0])))?;
            }
            Op::I32Load16S(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<2>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(
                    slot(base, m.dst),
                    Cell::from_i32(i32::from(i16::from_le_bytes(b))),
                )?;
            }
            Op::I32Load16U(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<2>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(
                    slot(base, m.dst),
                    Cell::from_i32(i32::from(u16::from_le_bytes(b))),
                )?;
            }
            Op::I64Load8S(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<1>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(slot(base, m.dst), Cell::from_i64(i64::from(b[0] as i8)))?;
            }
            Op::I64Load8U(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<1>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(slot(base, m.dst), Cell::from_i64(i64::from(b[0])))?;
            }
            Op::I64Load16S(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<2>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(
                    slot(base, m.dst),
                    Cell::from_i64(i64::from(i16::from_le_bytes(b))),
                )?;
            }
            Op::I64Load16U(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<2>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(
                    slot(base, m.dst),
                    Cell::from_i64(i64::from(u16::from_le_bytes(b))),
                )?;
            }
            Op::I64Load32S(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<4>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(
                    slot(base, m.dst),
                    Cell::from_i64(i64::from(i32::from_le_bytes(b))),
                )?;
            }
            Op::I64Load32U(m) => {
                let (at, mem) = memory::load_operands(code, m)?;
                let b = self.load_at::<4>(inner, instance, mem, slot(base, at))?;
                self.set_scalar(
                    slot(base, m.dst),
                    Cell::from_i64(i64::from(u32::from_le_bytes(b))),
                )?;
            }
            Op::I32Store(m) => {
                let (addr, val, mem) = memory::store_operands(code, m)?;
                let v = self.scalar_at(slot(base, val))?.unwrap_i32();
                self.store_at(inner, instance, mem, slot(base, addr), v.to_le_bytes())?;
            }
            Op::I64Store(m) => {
                let (addr, val, mem) = memory::store_operands(code, m)?;
                let v = self.scalar_at(slot(base, val))?.unwrap_i64();
                self.store_at(inner, instance, mem, slot(base, addr), v.to_le_bytes())?;
            }
            Op::F32Store(m) => {
                let (addr, val, mem) = memory::store_operands(code, m)?;
                let v = self.scalar_at(slot(base, val))?.unwrap_f32().to_bits();
                self.store_at(inner, instance, mem, slot(base, addr), v.to_le_bytes())?;
            }
            Op::F64Store(m) => {
                let (addr, val, mem) = memory::store_operands(code, m)?;
                let v = self.scalar_at(slot(base, val))?.unwrap_f64().to_bits();
                self.store_at(inner, instance, mem, slot(base, addr), v.to_le_bytes())?;
            }
            Op::I32Store8(m) => {
                let (addr, val, mem) = memory::store_operands(code, m)?;
                let v = self.scalar_at(slot(base, val))?.unwrap_i32() as u8;
                self.store_at(inner, instance, mem, slot(base, addr), [v])?;
            }
            Op::I32Store16(m) => {
                let (addr, val, mem) = memory::store_operands(code, m)?;
                let v = self.scalar_at(slot(base, val))?.unwrap_i32() as u16;
                self.store_at(inner, instance, mem, slot(base, addr), v.to_le_bytes())?;
            }
            Op::I64Store8(m) => {
                let (addr, val, mem) = memory::store_operands(code, m)?;
                let v = self.scalar_at(slot(base, val))?.unwrap_i64() as u8;
                self.store_at(inner, instance, mem, slot(base, addr), [v])?;
            }
            Op::I64Store16(m) => {
                let (addr, val, mem) = memory::store_operands(code, m)?;
                let v = self.scalar_at(slot(base, val))?.unwrap_i64() as u16;
                self.store_at(inner, instance, mem, slot(base, addr), v.to_le_bytes())?;
            }
            Op::I64Store32(m) => {
                let (addr, val, mem) = memory::store_operands(code, m)?;
                let v = self.scalar_at(slot(base, val))?.unwrap_i64() as u32;
                self.store_at(inner, instance, mem, slot(base, addr), v.to_le_bytes())?;
            }
            // The cold memory ops route to their category handler.
            op @ (Op::MemorySize(..)
            | Op::MemoryCopy(..)
            | Op::MemoryFill(..)
            | Op::MemoryInit(..)
            | Op::DataDrop(_)) => {
                self.exec_memory(inner, op, instance, base)?;
            }
            op @ (Op::RefNull(..) | Op::RefFunc(..) | Op::RefIsNull(_) | Op::RefAsNonNull(_)) => {
                self.exec_ref(inner, op, instance, base)?;
            }
            op @ (Op::TableInit { .. }
            | Op::TableCopy { .. }
            | Op::ElemDrop(_)
            | Op::TableGet(..)
            | Op::TableSet(..)
            | Op::TableSize(..)
            | Op::TableFill(..)) => self.exec_table(inner, op, instance, base)?,
            Op::Throw(tag, s) => {
                let outcome = self.throw(inner, instance, *tag, next - 1, slot(base, *s))?;
                return Ok(self.control(outcome));
            }
            Op::ThrowRef(s) => {
                let outcome = self.throw_ref(slot(base, *s))?;
                return Ok(self.control(outcome));
            }
            op @ Op::BrOnCast { .. } => {
                self.enter_window_of(base, op)?;
                if let Some(ip) = self.br_on_cast(inner, code, instance, op, false)? {
                    return Ok(ip);
                }
            }
            op @ Op::BrOnCastFail { .. } => {
                self.enter_window_of(base, op)?;
                if let Some(ip) = self.br_on_cast(inner, code, instance, op, true)? {
                    return Ok(ip);
                }
            }
            #[cfg(feature = "simd")]
            Op::Simd(s, top) => {
                self.enter_window(slot(base, *top))?;
                self.exec_simd(inner, code, s, instance)?;
            }
            // GC aggregate allocation: ensure the limiter-granted reservation covers it first (with
            // operands still on the stack as roots); a reservation grow suspends and re-executes.
            // `array.new_data`/`array.new_elem` route through here too — their charge is clamped to
            // the source segment size (`seg_clamped_charge`), so a would-be-out-of-bounds count
            // reserves only a bounded amount and still traps with the correct error in its handler.
            alloc @ (Op::StructNew(..)
            | Op::StructNewDefault(..)
            | Op::ArrayNew(..)
            | Op::ArrayNewDefault(..)
            | Op::ArrayNewFixed { .. }
            | Op::ArrayNewData { .. }
            | Op::ArrayNewElem { .. }) => {
                self.enter_window_of(base, alloc)?;
                if let Some(charge) = self.gc_alloc_charge(inner, instance, alloc)? {
                    if let Some(out) = self.gc_reserve(inner, charge, next - 1)? {
                        return Ok(self.control(out));
                    }
                }
                self.exec_gc(inner, alloc, instance)?;
            }
            // Numerics: flat for the same reason as loads/stores.
            // --- i32 with an immediate right operand ---
            Op::I32AddImm(d, a, k) => self.i32_unop(un(base, *d, *a), |x| x.wrapping_add(*k))?,
            Op::I32MulImm(d, a, k) => self.i32_unop(un(base, *d, *a), |x| x.wrapping_mul(*k))?,
            Op::I32AndImm(d, a, k) => self.i32_unop(un(base, *d, *a), |x| x & *k)?,
            Op::I32OrImm(d, a, k) => self.i32_unop(un(base, *d, *a), |x| x | *k)?,
            Op::I32XorImm(d, a, k) => self.i32_unop(un(base, *d, *a), |x| x ^ *k)?,
            Op::I32ShlImm(d, a, k) => {
                self.i32_unop(un(base, *d, *a), |x| x.wrapping_shl(*k as u32))?;
            }
            Op::I32ShrSImm(d, a, k) => {
                self.i32_unop(un(base, *d, *a), |x| x.wrapping_shr(*k as u32))?;
            }
            Op::I32ShrUImm(d, a, k) => {
                self.i32_unop(un(base, *d, *a), |x| {
                    (x as u32).wrapping_shr(*k as u32) as i32
                })?;
            }
            Op::I32CmpImm {
                kind,
                dst,
                lhs,
                imm,
            } => self.i32_unop(un(base, *dst, *lhs), |x| i32::from(cmp_i32(*kind, x, *imm)))?,
            // --- i32 ---
            Op::I32Eqz(d, a) => self.i32_unop(un(base, *d, *a), |a| i32::from(a == 0))?,
            Op::I32Eq(d, a, b) => self.i32_relop(bin(base, *d, *a, *b), |a, b| a == b)?,
            Op::I32Ne(d, a, b) => self.i32_relop(bin(base, *d, *a, *b), |a, b| a != b)?,
            Op::I32LtS(d, a, b) => self.i32_relop(bin(base, *d, *a, *b), |a, b| a < b)?,
            Op::I32LtU(d, a, b) => self.u32_relop(bin(base, *d, *a, *b), |a, b| a < b)?,
            Op::I32GtS(d, a, b) => self.i32_relop(bin(base, *d, *a, *b), |a, b| a > b)?,
            Op::I32GtU(d, a, b) => self.u32_relop(bin(base, *d, *a, *b), |a, b| a > b)?,
            Op::I32LeS(d, a, b) => self.i32_relop(bin(base, *d, *a, *b), |a, b| a <= b)?,
            Op::I32LeU(d, a, b) => self.u32_relop(bin(base, *d, *a, *b), |a, b| a <= b)?,
            Op::I32GeS(d, a, b) => self.i32_relop(bin(base, *d, *a, *b), |a, b| a >= b)?,
            Op::I32GeU(d, a, b) => self.u32_relop(bin(base, *d, *a, *b), |a, b| a >= b)?,
            Op::I32Clz(d, a) => self.i32_unop(un(base, *d, *a), |a| a.leading_zeros() as i32)?,
            Op::I32Ctz(d, a) => self.i32_unop(un(base, *d, *a), |a| a.trailing_zeros() as i32)?,
            Op::I32Popcnt(d, a) => self.i32_unop(un(base, *d, *a), |a| a.count_ones() as i32)?,
            Op::I32Add(d, a, b) => self.i32_binop(bin(base, *d, *a, *b), i32::wrapping_add)?,
            Op::I32Sub(d, a, b) => self.i32_binop(bin(base, *d, *a, *b), i32::wrapping_sub)?,
            Op::I32Mul(d, a, b) => self.i32_binop(bin(base, *d, *a, *b), i32::wrapping_mul)?,
            Op::I32DivS(d, a, b) => self.i32_try_binop(bin(base, *d, *a, *b), arith::div_s_i32)?,
            Op::I32DivU(d, a, b) => {
                self.u32_try_binop(bin(base, *d, *a, *b), |a, b| arith::nz(b).map(|()| a / b))?;
            }
            Op::I32RemS(d, a, b) => self.i32_try_binop(bin(base, *d, *a, *b), |a, b| {
                arith::nz(b).map(|()| a.wrapping_rem(b))
            })?,
            Op::I32RemU(d, a, b) => {
                self.u32_try_binop(bin(base, *d, *a, *b), |a, b| arith::nz(b).map(|()| a % b))?;
            }
            Op::I32And(d, a, b) => self.i32_binop(bin(base, *d, *a, *b), |a, b| a & b)?,
            Op::I32Or(d, a, b) => self.i32_binop(bin(base, *d, *a, *b), |a, b| a | b)?,
            Op::I32Xor(d, a, b) => self.i32_binop(bin(base, *d, *a, *b), |a, b| a ^ b)?,
            Op::I32Shl(d, a, b) => {
                self.i32_binop(bin(base, *d, *a, *b), |a, b| a.wrapping_shl(b as u32))?;
            }
            Op::I32ShrS(d, a, b) => {
                self.i32_binop(bin(base, *d, *a, *b), |a, b| a.wrapping_shr(b as u32))?;
            }
            Op::I32ShrU(d, a, b) => self.u32_binop(bin(base, *d, *a, *b), u32::wrapping_shr)?,
            Op::I32Rotl(d, a, b) => {
                self.u32_binop(bin(base, *d, *a, *b), |a, b| a.rotate_left(b % 32))?;
            }
            Op::I32Rotr(d, a, b) => {
                self.u32_binop(bin(base, *d, *a, *b), |a, b| a.rotate_right(b % 32))?;
            }

            // --- i64 ---
            Op::I64Eqz(d, a) => self.map_i64_i32(un(base, *d, *a), |a| i32::from(a == 0))?,
            Op::I64Eq(d, a, b) => self.i64_relop(bin(base, *d, *a, *b), |a, b| a == b)?,
            Op::I64Ne(d, a, b) => self.i64_relop(bin(base, *d, *a, *b), |a, b| a != b)?,
            Op::I64LtS(d, a, b) => self.i64_relop(bin(base, *d, *a, *b), |a, b| a < b)?,
            Op::I64LtU(d, a, b) => self.u64_relop(bin(base, *d, *a, *b), |a, b| a < b)?,
            Op::I64GtS(d, a, b) => self.i64_relop(bin(base, *d, *a, *b), |a, b| a > b)?,
            Op::I64GtU(d, a, b) => self.u64_relop(bin(base, *d, *a, *b), |a, b| a > b)?,
            Op::I64LeS(d, a, b) => self.i64_relop(bin(base, *d, *a, *b), |a, b| a <= b)?,
            Op::I64LeU(d, a, b) => self.u64_relop(bin(base, *d, *a, *b), |a, b| a <= b)?,
            Op::I64GeS(d, a, b) => self.i64_relop(bin(base, *d, *a, *b), |a, b| a >= b)?,
            Op::I64GeU(d, a, b) => self.u64_relop(bin(base, *d, *a, *b), |a, b| a >= b)?,
            Op::I64Clz(d, a) => {
                self.i64_unop(un(base, *d, *a), |a| i64::from(a.leading_zeros()))?;
            }
            Op::I64Ctz(d, a) => {
                self.i64_unop(un(base, *d, *a), |a| i64::from(a.trailing_zeros()))?;
            }
            Op::I64Popcnt(d, a) => {
                self.i64_unop(un(base, *d, *a), |a| i64::from(a.count_ones()))?;
            }
            Op::I64Add(d, a, b) => self.i64_binop(bin(base, *d, *a, *b), i64::wrapping_add)?,
            Op::I64Sub(d, a, b) => self.i64_binop(bin(base, *d, *a, *b), i64::wrapping_sub)?,
            Op::I64Mul(d, a, b) => self.i64_binop(bin(base, *d, *a, *b), i64::wrapping_mul)?,
            Op::I64DivS(d, a, b) => self.i64_try_binop(bin(base, *d, *a, *b), arith::div_s_i64)?,
            Op::I64DivU(d, a, b) => {
                self.u64_try_binop(bin(base, *d, *a, *b), |a, b| arith::nz(b).map(|()| a / b))?;
            }
            Op::I64RemS(d, a, b) => self.i64_try_binop(bin(base, *d, *a, *b), |a, b| {
                arith::nz(b).map(|()| a.wrapping_rem(b))
            })?,
            Op::I64RemU(d, a, b) => {
                self.u64_try_binop(bin(base, *d, *a, *b), |a, b| arith::nz(b).map(|()| a % b))?;
            }
            Op::I64And(d, a, b) => self.i64_binop(bin(base, *d, *a, *b), |a, b| a & b)?,
            Op::I64Or(d, a, b) => self.i64_binop(bin(base, *d, *a, *b), |a, b| a | b)?,
            Op::I64Xor(d, a, b) => self.i64_binop(bin(base, *d, *a, *b), |a, b| a ^ b)?,
            Op::I64Shl(d, a, b) => {
                self.i64_binop(bin(base, *d, *a, *b), |a, b| a.wrapping_shl(b as u32))?;
            }
            Op::I64ShrS(d, a, b) => {
                self.i64_binop(bin(base, *d, *a, *b), |a, b| a.wrapping_shr(b as u32))?;
            }
            Op::I64ShrU(d, a, b) => {
                self.u64_binop(bin(base, *d, *a, *b), |a, b| a.wrapping_shr(b as u32))?;
            }
            Op::I64Rotl(d, a, b) => {
                self.u64_binop(bin(base, *d, *a, *b), |a, b| a.rotate_left((b % 64) as u32))?;
            }
            Op::I64Rotr(d, a, b) => {
                self.u64_binop(bin(base, *d, *a, *b), |a, b| {
                    a.rotate_right((b % 64) as u32)
                })?;
            }

            // --- f32 ---
            Op::F32Eq(d, a, b) => self.f32_relop(bin(base, *d, *a, *b), |a, b| a == b)?,
            Op::F32Ne(d, a, b) => self.f32_relop(bin(base, *d, *a, *b), |a, b| a != b)?,
            Op::F32Lt(d, a, b) => self.f32_relop(bin(base, *d, *a, *b), |a, b| a < b)?,
            Op::F32Gt(d, a, b) => self.f32_relop(bin(base, *d, *a, *b), |a, b| a > b)?,
            Op::F32Le(d, a, b) => self.f32_relop(bin(base, *d, *a, *b), |a, b| a <= b)?,
            Op::F32Ge(d, a, b) => self.f32_relop(bin(base, *d, *a, *b), |a, b| a >= b)?,
            Op::F32Abs(d, a) => self.f32_unop(un(base, *d, *a), f32::abs)?,
            Op::F32Neg(d, a) => self.f32_unop(un(base, *d, *a), |a| -a)?,
            Op::F32Ceil(d, a) => self.f32_unop_canon(un(base, *d, *a), f32::ceil)?,
            Op::F32Floor(d, a) => self.f32_unop_canon(un(base, *d, *a), f32::floor)?,
            Op::F32Trunc(d, a) => self.f32_unop_canon(un(base, *d, *a), f32::trunc)?,
            Op::F32Nearest(d, a) => self.f32_unop_canon(un(base, *d, *a), f32::round_ties_even)?,
            Op::F32Sqrt(d, a) => self.f32_unop_canon(un(base, *d, *a), f32::sqrt)?,
            Op::F32Add(d, a, b) => self.f32_arith(bin(base, *d, *a, *b), |a, b| a + b)?,
            Op::F32Sub(d, a, b) => self.f32_arith(bin(base, *d, *a, *b), |a, b| a - b)?,
            Op::F32Mul(d, a, b) => self.f32_arith(bin(base, *d, *a, *b), |a, b| a * b)?,
            Op::F32Div(d, a, b) => self.f32_arith(bin(base, *d, *a, *b), |a, b| a / b)?,
            Op::F32Min(d, a, b) => self.f32_binop(bin(base, *d, *a, *b), arith::f32_min)?,
            Op::F32Max(d, a, b) => self.f32_binop(bin(base, *d, *a, *b), arith::f32_max)?,
            Op::F32Copysign(d, a, b) => self.f32_binop(bin(base, *d, *a, *b), f32::copysign)?,

            // --- f64 ---
            Op::F64Eq(d, a, b) => self.f64_relop(bin(base, *d, *a, *b), |a, b| a == b)?,
            Op::F64Ne(d, a, b) => self.f64_relop(bin(base, *d, *a, *b), |a, b| a != b)?,
            Op::F64Lt(d, a, b) => self.f64_relop(bin(base, *d, *a, *b), |a, b| a < b)?,
            Op::F64Gt(d, a, b) => self.f64_relop(bin(base, *d, *a, *b), |a, b| a > b)?,
            Op::F64Le(d, a, b) => self.f64_relop(bin(base, *d, *a, *b), |a, b| a <= b)?,
            Op::F64Ge(d, a, b) => self.f64_relop(bin(base, *d, *a, *b), |a, b| a >= b)?,
            Op::F64Abs(d, a) => self.f64_unop(un(base, *d, *a), f64::abs)?,
            Op::F64Neg(d, a) => self.f64_unop(un(base, *d, *a), |a| -a)?,
            Op::F64Ceil(d, a) => self.f64_unop_canon(un(base, *d, *a), f64::ceil)?,
            Op::F64Floor(d, a) => self.f64_unop_canon(un(base, *d, *a), f64::floor)?,
            Op::F64Trunc(d, a) => self.f64_unop_canon(un(base, *d, *a), f64::trunc)?,
            Op::F64Nearest(d, a) => self.f64_unop_canon(un(base, *d, *a), f64::round_ties_even)?,
            Op::F64Sqrt(d, a) => self.f64_unop_canon(un(base, *d, *a), f64::sqrt)?,
            Op::F64Add(d, a, b) => self.f64_arith(bin(base, *d, *a, *b), |a, b| a + b)?,
            Op::F64Sub(d, a, b) => self.f64_arith(bin(base, *d, *a, *b), |a, b| a - b)?,
            Op::F64Mul(d, a, b) => self.f64_arith(bin(base, *d, *a, *b), |a, b| a * b)?,
            Op::F64Div(d, a, b) => self.f64_arith(bin(base, *d, *a, *b), |a, b| a / b)?,
            Op::F64Min(d, a, b) => self.f64_binop(bin(base, *d, *a, *b), arith::f64_min)?,
            Op::F64Max(d, a, b) => self.f64_binop(bin(base, *d, *a, *b), arith::f64_max)?,
            Op::F64Copysign(d, a, b) => self.f64_binop(bin(base, *d, *a, *b), f64::copysign)?,

            // --- conversions ---
            Op::I32WrapI64(d, a) => self.map_i64_i32(un(base, *d, *a), convert::i32_wrap_i64)?,
            Op::I64ExtendI32S(d, a) => {
                self.map_i32_i64(un(base, *d, *a), convert::i64_extend_i32_s)?;
            }
            Op::I64ExtendI32U(d, a) => {
                self.map_i32_i64(un(base, *d, *a), convert::i64_extend_i32_u)?;
            }
            Op::I32TruncF32S(d, a) => {
                self.trunc_to_i32(un(base, *d, *a), convert::i32_trunc_f32_s)?;
            }
            Op::I32TruncF32U(d, a) => {
                self.trunc_to_i32(un(base, *d, *a), convert::i32_trunc_f32_u)?;
            }
            Op::I32TruncF64S(d, a) => {
                self.trunc64_to_i32(un(base, *d, *a), convert::i32_trunc_f64_s)?;
            }
            Op::I32TruncF64U(d, a) => {
                self.trunc64_to_i32(un(base, *d, *a), convert::i32_trunc_f64_u)?;
            }
            Op::I64TruncF32S(d, a) => {
                self.trunc_to_i64(un(base, *d, *a), convert::i64_trunc_f32_s)?;
            }
            Op::I64TruncF32U(d, a) => {
                self.trunc_to_i64(un(base, *d, *a), convert::i64_trunc_f32_u)?;
            }
            Op::I64TruncF64S(d, a) => {
                self.trunc64_to_i64(un(base, *d, *a), convert::i64_trunc_f64_s)?;
            }
            Op::I64TruncF64U(d, a) => {
                self.trunc64_to_i64(un(base, *d, *a), convert::i64_trunc_f64_u)?;
            }
            Op::I32TruncSatF32S(d, a) => {
                self.map_f32_i32(un(base, *d, *a), convert::i32_trunc_sat_f32_s)?;
            }
            Op::I32TruncSatF32U(d, a) => {
                self.map_f32_i32(un(base, *d, *a), convert::i32_trunc_sat_f32_u)?;
            }
            Op::I32TruncSatF64S(d, a) => {
                self.map_f64_i32(un(base, *d, *a), convert::i32_trunc_sat_f64_s)?;
            }
            Op::I32TruncSatF64U(d, a) => {
                self.map_f64_i32(un(base, *d, *a), convert::i32_trunc_sat_f64_u)?;
            }
            Op::I64TruncSatF32S(d, a) => {
                self.map_f32_i64(un(base, *d, *a), convert::i64_trunc_sat_f32_s)?;
            }
            Op::I64TruncSatF32U(d, a) => {
                self.map_f32_i64(un(base, *d, *a), convert::i64_trunc_sat_f32_u)?;
            }
            Op::I64TruncSatF64S(d, a) => {
                self.map_f64_i64(un(base, *d, *a), convert::i64_trunc_sat_f64_s)?;
            }
            Op::I64TruncSatF64U(d, a) => {
                self.map_f64_i64(un(base, *d, *a), convert::i64_trunc_sat_f64_u)?;
            }
            Op::F32ConvertI32S(d, a) => {
                self.map_i32_f32(un(base, *d, *a), convert::f32_convert_i32_s)?;
            }
            Op::F32ConvertI32U(d, a) => {
                self.map_i32_f32(un(base, *d, *a), convert::f32_convert_i32_u)?;
            }
            Op::F32ConvertI64S(d, a) => {
                self.map_i64_f32(un(base, *d, *a), convert::f32_convert_i64_s)?;
            }
            Op::F32ConvertI64U(d, a) => {
                self.map_i64_f32(un(base, *d, *a), convert::f32_convert_i64_u)?;
            }
            Op::F32DemoteF64(d, a) => {
                self.map_f64_f32(un(base, *d, *a), convert::f32_demote_f64)?;
            }
            Op::F64ConvertI32S(d, a) => {
                self.map_i32_f64(un(base, *d, *a), convert::f64_convert_i32_s)?;
            }
            Op::F64ConvertI32U(d, a) => {
                self.map_i32_f64(un(base, *d, *a), convert::f64_convert_i32_u)?;
            }
            Op::F64ConvertI64S(d, a) => {
                self.map_i64_f64(un(base, *d, *a), convert::f64_convert_i64_s)?;
            }
            Op::F64ConvertI64U(d, a) => {
                self.map_i64_f64(un(base, *d, *a), convert::f64_convert_i64_u)?;
            }
            Op::F64PromoteF32(d, a) => {
                self.map_f32_f64(un(base, *d, *a), convert::f64_promote_f32)?;
            }
            Op::I32ReinterpretF32(d, a) => {
                self.map_f32_i32(un(base, *d, *a), |x| x.to_bits() as i32)?;
            }
            Op::I64ReinterpretF64(d, a) => {
                self.map_f64_i64(un(base, *d, *a), |x| x.to_bits() as i64)?;
            }
            Op::F32ReinterpretI32(d, a) => {
                self.map_i32_f32(un(base, *d, *a), |x| f32::from_bits(x as u32))?;
            }
            Op::F64ReinterpretI64(d, a) => {
                self.map_i64_f64(un(base, *d, *a), |x| f64::from_bits(x as u64))?;
            }

            // --- sign-extension ---
            Op::I32Extend8S(d, a) => self.i32_unop(un(base, *d, *a), |a| i32::from(a as i8))?,
            Op::I32Extend16S(d, a) => self.i32_unop(un(base, *d, *a), |a| i32::from(a as i16))?,
            Op::I64Extend8S(d, a) => self.i64_unop(un(base, *d, *a), |a| i64::from(a as i8))?,
            Op::I64Extend16S(d, a) => self.i64_unop(un(base, *d, *a), |a| i64::from(a as i16))?,
            Op::I64Extend32S(d, a) => self.i64_unop(un(base, *d, *a), |a| i64::from(a as i32))?,
            // The colder GC/cast categories fall through: exec_gc → exec_gc_array → exec_cast.
            other => {
                self.enter_window_of(base, other)?;
                self.exec_gc(inner, other, instance)?;
            }
        }
        Ok(next)
    }

    /// Enters a stack-window op's operand window (its handler pops/pushes relative to `top`).
    #[inline]
    fn enter_window_of(&mut self, base: u32, op: &Op) -> Result<()> {
        let top = op
            .window_top()
            .ok_or(crate::error::InternalError::Compiler(
                "op has no operand window",
            ))?;
        self.enter_window(slot(base, top))
    }

    /// Takes branch `t` (its condition/index operand at `at`) and returns the target ip. The usual
    /// keep-nothing branch moves nothing, so it is tested here: left to `take_branch` it cost an
    /// out-of-line call on every loop back-edge (CoreMark/memory loops ~5%, PERF-NOTES §25).
    #[allow(clippy::inline_always)]
    #[inline(always)]
    fn branch_to(&mut self, t: BranchTarget, at: usize) -> Result<u32> {
        if t.keep != 0 {
            self.take_branch(t, at)?;
        }
        Ok(t.ip)
    }

    /// Leaves `outcome` for the run loop and returns the [`CONTROL`] marker.
    #[inline]
    fn control(&mut self, outcome: StepOutcome) -> u32 {
        self.control = Some(outcome);
        CONTROL
    }
}

/// Evaluates a fused compare-and-branch comparison ([`Op::BrIfCmp`]).
#[allow(clippy::inline_always)] // one hot call site, inside the dispatch arm
#[inline(always)]
#[allow(clippy::cast_sign_loss)] // `_U` kinds reinterpret the operand bits as unsigned, per spec
fn cmp_i32(kind: CmpKind, a: i32, b: i32) -> bool {
    match kind {
        CmpKind::Eq => a == b,
        CmpKind::Ne => a != b,
        CmpKind::LtS => a < b,
        CmpKind::LtU => (a as u32) < (b as u32),
        CmpKind::GtS => a > b,
        CmpKind::GtU => (a as u32) > (b as u32),
        CmpKind::LeS => a <= b,
        CmpKind::LeU => (a as u32) <= (b as u32),
        CmpKind::GeS => a >= b,
        CmpKind::GeU => (a as u32) >= (b as u32),
    }
}

/// A tail (`return_call*`) site replaces the current frame; a normal call keeps `next` as the
/// caller's resume ip (#39).
#[inline]
fn call_kind(tail: bool, next: u32) -> CallKind {
    if tail {
        CallKind::Tail
    } else {
        CallKind::Nested(next)
    }
}

/// Lookups into the executing instance's index spaces by an index from the op stream (in range for
/// a validated module; fail-closed for a corrupt artifact — see [`ir_get`](crate::error::ir_get)).
mod index_space {
    use crate::error::ir_get;
    use crate::extern_::{Global, Memory, Table};
    use crate::func::Func;
    use crate::instance::Instance;
    use crate::store::StoreInner;
    use crate::Result;

    #[inline]
    pub(super) fn global(inner: &StoreInner, instance: Instance, index: u32) -> Result<Global> {
        ir_get(&inner.instance(instance).globals, index).copied()
    }

    #[inline]
    pub(super) fn func(inner: &StoreInner, instance: Instance, index: u32) -> Result<Func> {
        ir_get(&inner.instance(instance).funcs, index).copied()
    }

    #[inline]
    pub(super) fn memory(inner: &StoreInner, instance: Instance, index: u32) -> Result<Memory> {
        ir_get(&inner.instance(instance).memories, index).copied()
    }

    #[inline]
    pub(super) fn table(inner: &StoreInner, instance: Instance, index: u32) -> Result<Table> {
        ir_get(&inner.instance(instance).tables, index).copied()
    }
}

/// The absolute operand-stack index of frame slot `s` (`base` = the frame's first local).
/// Saturating: an out-of-range slot fails the access's bounds check instead of wrapping.
#[allow(clippy::inline_always)]
#[inline(always)]
pub(super) fn slot(base: u32, s: crate::module::op::Slot) -> usize {
    (base as usize).saturating_add(s as usize)
}

/// A three-address op's absolute `(dst, lhs, rhs)` slots.
#[allow(clippy::inline_always)]
#[inline(always)]
fn bin(base: u32, d: Slot, a: Slot, b: Slot) -> Bin {
    (slot(base, d), slot(base, a), slot(base, b))
}

/// A two-address op's absolute `(dst, src)` slots.
#[allow(clippy::inline_always)]
#[inline(always)]
fn un(base: u32, d: Slot, a: Slot) -> Un {
    (slot(base, d), slot(base, a))
}