Skip to main content

synth_backend/
arm_backend.rs

1//! ARM Backend — wraps the instruction selector + optimizer + encoder as a Backend
2//!
3//! This is Synth's custom ARM compiler targeting Cortex-M (Thumb-2).
4//! It's the only backend that supports per-rule formal verification (ASIL D path).
5
6use crate::ArmEncoder;
7use synth_core::backend::{
8    Backend, BackendCapabilities, BackendError, CodeRelocation, CompilationResult, CompileConfig,
9    CompiledFunction, LineMap, SafetyBounds,
10};
11use synth_core::target::{IsaVariant, TargetSpec};
12use synth_core::wasm_decoder::DecodedModule;
13use synth_core::wasm_op::WasmOp;
14use synth_synthesis::{
15    ArmInstruction, ArmOp, BoundsCheckConfig, InstructionSelector, OptimizationConfig,
16    OptimizerBridge, RuleDatabase, validate_instructions,
17};
18
19/// ARM Cortex-M backend using Synth's custom compiler pipeline
20pub struct ArmBackend;
21
22impl ArmBackend {
23    pub fn new() -> Self {
24        Self
25    }
26}
27
28impl Default for ArmBackend {
29    fn default() -> Self {
30        Self::new()
31    }
32}
33
34impl Backend for ArmBackend {
35    fn name(&self) -> &str {
36        "arm"
37    }
38
39    fn capabilities(&self) -> BackendCapabilities {
40        BackendCapabilities {
41            produces_elf: false,
42            supports_rule_verification: true,
43            supports_binary_verification: true,
44            is_external: false,
45        }
46    }
47
48    fn supported_targets(&self) -> Vec<TargetSpec> {
49        vec![
50            TargetSpec::cortex_m3(),
51            TargetSpec::cortex_m4(),
52            TargetSpec::cortex_m4f(),
53            TargetSpec::cortex_m7(),
54            TargetSpec::cortex_m7dp(),
55        ]
56    }
57
58    fn compile_module(
59        &self,
60        module: &DecodedModule,
61        config: &CompileConfig,
62    ) -> Result<CompilationResult, BackendError> {
63        let exports: Vec<_> = module
64            .functions
65            .iter()
66            .filter(|f| f.export_name.is_some())
67            .collect();
68
69        if exports.is_empty() {
70            return Err(BackendError::CompilationFailed(
71                "no exported functions found".into(),
72            ));
73        }
74
75        let mut functions = Vec::new();
76        for func in &exports {
77            let name = func.export_name.clone().unwrap();
78            // #359: copy THIS function's declared param widths into the config so
79            // `compile_function` (which carries no function index) can refuse a
80            // 64-bit param on the AAPCS stack-argument path. Cheap clone only when
81            // a signature table is present and this function has a width entry —
82            // otherwise reuse the shared config (every existing module unchanged).
83            let func_config = match config.func_params_i64.get(func.index as usize) {
84                Some(p) if !p.is_empty() => Some(CompileConfig {
85                    current_func_params_i64: p.clone(),
86                    ..config.clone()
87                }),
88                _ => None,
89            };
90            let cfg = func_config.as_ref().unwrap_or(config);
91            let compiled = self.compile_function(&name, &func.ops, cfg)?;
92            functions.push(compiled);
93        }
94
95        Ok(CompilationResult {
96            functions,
97            elf: None,
98            backend_name: self.name().to_string(),
99        })
100    }
101
102    fn compile_function(
103        &self,
104        name: &str,
105        ops: &[WasmOp],
106        config: &CompileConfig,
107    ) -> Result<CompiledFunction, BackendError> {
108        let (code, relocations, line_map) =
109            compile_wasm_to_arm(ops, config).map_err(BackendError::CompilationFailed)?;
110
111        Ok(CompiledFunction {
112            name: name.to_string(),
113            code,
114            wasm_ops: ops.to_vec(),
115            relocations,
116            line_map,
117        })
118    }
119
120    fn is_available(&self) -> bool {
121        true // Always available — it's a library backend
122    }
123}
124
125/// Count the number of function parameters by analyzing LocalGet patterns
126fn count_params(wasm_ops: &[WasmOp]) -> u32 {
127    let mut first_access: std::collections::HashMap<u32, bool> = std::collections::HashMap::new();
128    for op in wasm_ops {
129        match op {
130            WasmOp::LocalGet(idx) => {
131                first_access.entry(*idx).or_insert(true);
132            }
133            WasmOp::LocalSet(idx) | WasmOp::LocalTee(idx) => {
134                first_access.entry(*idx).or_insert(false);
135            }
136            _ => {}
137        }
138    }
139
140    first_access
141        .iter()
142        .filter_map(
143            |(&idx, &is_read_first)| {
144                if is_read_first { Some(idx + 1) } else { None }
145            },
146        )
147        .max()
148        .unwrap_or(0)
149}
150
151/// #539: fold the `i32.const 0; memory.grow m` idiom to `memory.size m`.
152/// `memory.grow(0)` always succeeds and returns the current page count (WASM
153/// Core §4.4.7), which is exactly `memory.size`; the fixed-memory backend
154/// otherwise emits a constant `-1` for every `memory.grow`, so the legal
155/// `memory.grow(0)` "read/validate current size" idiom wrongly reported failure.
156/// Only the ADJACENT const-0 delta is folded (a non-zero delta keeps the sound
157/// `-1` — fixed memory genuinely cannot grow; a runtime-computed 0 is a
158/// documented follow-up). Backend- and path-agnostic: `memory.size` reads the
159/// runtime memory-size register on every selector, so this fixes the optimized
160/// and direct paths at once.
161fn rewrite_memory_grow_zero(wasm_ops: &[WasmOp]) -> Vec<WasmOp> {
162    let mut out = Vec::with_capacity(wasm_ops.len());
163    let mut i = 0;
164    while i < wasm_ops.len() {
165        if matches!(wasm_ops[i], WasmOp::I32Const(0))
166            && let Some(WasmOp::MemoryGrow(m)) = wasm_ops.get(i + 1)
167        {
168            out.push(WasmOp::MemorySize(*m));
169            i += 2;
170        } else {
171            out.push(wasm_ops[i].clone());
172            i += 1;
173        }
174    }
175    out
176}
177
178/// Core compilation: WASM ops → ARM machine code bytes + relocations
179///
180/// Returns (code_bytes, relocations) where relocations record BL instructions
181/// that target external symbols (e.g., `__meld_dispatch_import` for import calls).
182fn compile_wasm_to_arm(
183    wasm_ops: &[WasmOp],
184    config: &CompileConfig,
185) -> Result<(Vec<u8>, Vec<CodeRelocation>, LineMap), String> {
186    // #539: `memory.grow(0)` must return the CURRENT page count, not the
187    // fixed-memory `-1` sentinel — growing by zero pages can never fail (WASM
188    // Core §4.4.7), so a guest doing `if (memory.grow(0) < 0) trap;` wrongly
189    // faulted. Every lowering path emitted a delta-agnostic `-1`. `memory.grow(0)`
190    // is semantically identical to `memory.size`, which the backend already
191    // computes from the runtime memory-size register (R10 >> 16 = pages), so fold
192    // the `i32.const 0; memory.grow` idiom to `memory.size` up front — backend-
193    // and path-agnostic. A non-zero delta keeps `-1` (fixed memory genuinely
194    // cannot grow); a runtime delta that happens to be 0 is the documented
195    // follow-up.
196    let rewritten = rewrite_memory_grow_zero(wasm_ops);
197    let wasm_ops: &[WasmOp] = &rewritten;
198
199    let num_params = count_params(wasm_ops);
200
201    let bounds_config = match config.effective_safety_bounds() {
202        SafetyBounds::None => BoundsCheckConfig::None,
203        SafetyBounds::Mpu => BoundsCheckConfig::Mpu,
204        SafetyBounds::Software => BoundsCheckConfig::Software,
205        SafetyBounds::Mask => BoundsCheckConfig::Masking,
206    };
207
208    // The non-optimized (direct) instruction-selection path. Handles f32 via
209    // VFP/FPU. Used directly when `--no-optimize` is set, and as the fallback
210    // when the optimized path declines a module (see issue #120 below).
211    //
212    // VCR-RA-001 step 3b-lite (#242): a FRESH selector per attempt, with
213    // `spill_on_exhaustion` set only on the retry — the first pass is the
214    // unmodified default, so every function that compiles today is selected by
215    // exactly the code that compiled it yesterday (bit-identity is structural,
216    // not behavioural).
217    let select_direct_attempt = |spill_on_exhaustion: bool,
218                                 param_backing_on_exhaustion: bool,
219                                 local_promote: bool|
220     -> Result<Vec<ArmInstruction>, synth_core::Error> {
221        let db = RuleDatabase::with_standard_rules();
222        let mut selector =
223            InstructionSelector::with_bounds_check(db.rules().to_vec(), bounds_config);
224        selector.set_target(config.target.fpu, &config.target.triple);
225        if config.num_imports > 0 {
226            selector.set_num_imports(config.num_imports);
227        }
228        // #195: plumb the callee argument-count tables so the direct selector can
229        // marshal call arguments into R0–R3 per AAPCS.
230        selector.set_func_arg_counts(
231            config.func_arg_counts.clone(),
232            config.type_arg_counts.clone(),
233        );
234        // #197: in relocatable host-link mode, emit direct `func_N` BLs for
235        // imports (rewritten to the wasm field name by build_relocatable_elf)
236        // instead of `__meld_dispatch_import`.
237        selector.set_relocatable(config.relocatable);
238        // #237: native-pointer ABI — wasm statics become __synth_wasm_data-relative.
239        selector.set_native_pointer_abi(config.native_pointer_abi, config.linear_memory_bytes);
240        // #311: i64 call results are register PAIRS — tag them.
241        selector.set_result_types(config.func_ret_i64.clone(), config.type_ret_i64.clone());
242        // #359: declared param widths of THIS function, so the AAPCS stack-arg
243        // path can refuse 64-bit params (Ok-or-Err). Empty ⇒ assume i32.
244        selector.set_params_i64(config.current_func_params_i64.clone());
245        // Stack-pointer promotion is meaningful only under the native-pointer ABI;
246        // gating here keeps every non-native compile (all frozen fixtures) on the
247        // legacy R9 globals-table path, bit-identical.
248        if config.native_pointer_abi
249            && let Some((sp_idx, sp_init)) = config.stack_pointer_global
250        {
251            selector.set_native_pointer_stack(sp_idx, sp_init);
252        }
253        selector.set_spill_on_exhaustion(spill_on_exhaustion);
254        selector.set_param_backing_on_exhaustion(param_backing_on_exhaustion);
255        // VCR-RA local promotion (#390, #242): keep eligible non-param i32 locals
256        // in callee-saved registers instead of frame slots — the structural lever
257        // toward native parity. DEFAULT-ON as of v0.14.0: gale's G474RE DWT gate
258        // cleared it as a net win (gust_mix dissolved 58→50 cyc/call −14%, all 5
259        // stack spill/reloads eliminated, correctness bit-identical over [0,2047],
260        // 2.00×→1.72× vs LLVM). Escape hatch: `SYNTH_NO_LOCAL_PROMOTE=1` restores
261        // the frame-slot path. Leaf-only / i32-only / ARM-only (see
262        // compute_local_promotion); the leaf-only lift + i64 locals are follow-ons.
263        // #474: `local_promote` is now a per-attempt parameter so the retry ladder
264        // can drop promotion as an exhaustion-recovery rung (promotion pins r4-r8,
265        // which on a dense function leaves the spill allocator with nothing to
266        // free → the frame-slot path is the escape that restores compilability).
267        selector.set_local_promote(local_promote);
268        selector.select_with_stack(wasm_ops, num_params)
269    };
270    let select_direct = || -> Result<Vec<ArmInstruction>, String> {
271        const SINGLE_EXHAUSTION: &str = "all allocatable registers are live on the stack";
272        const PAIR_EXHAUSTION: &str = "no consecutive pair of free registers for i64";
273        // The full exhaustion-recovery ladder, parameterized on whether local
274        // promotion is enabled. Each rung is reached only when the previous one
275        // returned a recoverable register-exhaustion Err, so a function that
276        // compiles on the first attempt is untouched by the later rungs. Returns
277        // the result AND which rung produced it (for the #242 measurement below).
278        let recovery_ladder =
279            |promote: bool| -> (Result<Vec<ArmInstruction>, synth_core::Error>, &'static str) {
280                let mut attempt = select_direct_attempt(false, false, promote);
281                let mut rung = "base";
282                // VCR-RA-001 step 3b-lite (#242): the i32 register-exhaustion
283                // hard-fail is recoverable — retry with spill-on-exhaustion, which
284                // reserves the spill area and spills the deepest stack value when
285                // the pool is full.
286                if let Err(e) = &attempt
287                    && e.to_string().contains(SINGLE_EXHAUSTION)
288                {
289                    attempt = select_direct_attempt(true, false, promote);
290                    rung = "spill";
291                }
292                // VCR-RA-001 acceptance increment (#242): the i64 consecutive-PAIR
293                // exhaustion is recoverable too — not by stack spilling (the pair
294                // allocator already spills stack values, #171) but by frame-backing
295                // the params (#204) so they stop pinning R0-R3, with spill kept on.
296                if let Err(e) = &attempt
297                    && e.to_string().contains(PAIR_EXHAUSTION)
298                {
299                    attempt = select_direct_attempt(true, true, promote);
300                    rung = "param-backing";
301                }
302                (attempt, rung)
303            };
304        // #474: local promotion (default-on since v0.14.0) is an OPTIMIZATION — it
305        // must never be the reason a function fails to compile. Run the full ladder
306        // with promotion first (so every function that compiles today is
307        // bit-identical), and if it still ends in register exhaustion, fall back to
308        // the promotion-off ladder (the v0.12.0 frame-slot lowering — exactly what
309        // the `SYNTH_NO_LOCAL_PROMOTE=1` workaround does, now automatic). Promotion
310        // pins r4-r8 for the locals; on a dense function that leaves the allocator
311        // with nothing to free, so dropping it restores compilability. The fallback
312        // is reached ONLY by functions that exhaust WITH promotion, so promotion-on
313        // output is untouched by construction (frozen byte gate stays green).
314        let promote = std::env::var("SYNTH_NO_LOCAL_PROMOTE").is_err();
315        let (mut attempt, mut rung) = recovery_ladder(promote);
316        let mut promotion_dropped = false;
317        if promote
318            && attempt
319                .as_ref()
320                .err()
321                .is_some_and(|e| e.to_string().contains("register exhaustion"))
322        {
323            let (rescued, off_rung) = recovery_ladder(false);
324            if rescued.is_ok() {
325                attempt = rescued;
326                rung = off_rung;
327                promotion_dropped = true;
328            }
329        }
330        // VCR-RA measurement (#242): log which recovery rung produced the result,
331        // so the per-rung distribution across a corpus can be measured — the size
332        // of the failure surface a verified allocator must subsume (see
333        // scripts/repro/register_exhaustion_recovery_ladder.md). Logging only:
334        // emitted bytes are unchanged, so the frozen byte gate is unaffected.
335        if std::env::var("SYNTH_RECOVERY_STATS").is_ok() {
336            eprintln!(
337                "[recovery-stats] rung={rung}{} result={}",
338                if promotion_dropped {
339                    " promotion-off"
340                } else {
341                    ""
342                },
343                if attempt.is_ok() { "ok" } else { "exhausted" },
344            );
345        }
346        attempt.map_err(|e| format!("instruction selection failed: {}", e))
347    };
348
349    // Instruction selection: optimized or direct.
350    //
351    // #197: `--relocatable` (host-link ET_REL) forces the direct selector. The
352    // optimized path materializes an absolute linmem base (0x20000100) and does
353    // not preserve caller-saved registers across calls — both wrong for a
354    // host-linked object, where the linmem base arrives via `fp` at runtime and
355    // callees follow AAPCS. `select_with_stack` (now i64-spill capable after
356    // #171) handles fp-relative memory + caller-saved preservation correctly.
357    //
358    // #507: `br_table` is DROPPED during the optimized path's wasm→IR lowering
359    // (`optimize_full`), so `ir_to_arm` never sees the dispatch — it emits the
360    // arm bodies in fall-through sequence with no `cmp`/branch on the selector, a
361    // SILENT miscompile (every input hits the last arm). The selector value isn't
362    // even loaded. Because the drop happens before `ir_to_arm`, there's no `Err`
363    // to fall back on; detect it on the raw wasm op stream here and force the
364    // direct selector (`select_with_stack` lowers `br_table` correctly as a
365    // cmp-chain — confirmed on the `--relocatable` path). Same honest-degradation
366    // contract as the issue-#120 f32 decline: the function still compiles
367    // correctly, just without IR-level optimization. Frozen-safe: the frozen
368    // fixtures compile `--relocatable` (already direct), and no optimized-path
369    // fixture (control_step, flight_algo) contains `br_table`.
370    let has_br_table = wasm_ops
371        .iter()
372        .any(|op| matches!(op, WasmOp::BrTable { .. }));
373    let arm_instrs = if config.no_optimize || config.relocatable || has_br_table {
374        select_direct()?
375    } else {
376        let opt_config = if config.loom_compat {
377            OptimizationConfig::loom_compat()
378        } else {
379            OptimizationConfig::all()
380        };
381
382        let mut bridge = OptimizerBridge::with_config(opt_config);
383        // #188: tell the bridge how many imports there are so it declines only
384        // LOCAL calls (and leaves import calls on the optimized path, keeping
385        // the #173 field-name relocation rewrite intact).
386        bridge.set_num_imports(config.num_imports);
387        // `ir_to_arm` now returns `Result` — an `Err` means the optimized path
388        // hit an unmapped vreg (issue-#93-class). Treat it identically to an
389        // `optimize_full` failure: fall back to the direct selector rather
390        // than propagating, so the function still compiles correctly.
391        match bridge
392            .optimize_full(wasm_ops)
393            .and_then(|(opt_ir, _cfg, _stats)| bridge.ir_to_arm(&opt_ir, num_params as usize))
394        {
395            Ok(arm_ops) => arm_ops
396                .into_iter()
397                .map(|op| ArmInstruction {
398                    op,
399                    source_line: None,
400                })
401                .collect(),
402            // Issue #120: the optimized path declines modules it cannot lower
403            // (notably scalar f32/f64 ops — the IR has no float opcodes). Fall
404            // back to the direct instruction selector, which handles f32 via
405            // VFP/FPU. This is honest degradation: the function still compiles
406            // correctly, just without IR-level optimization.
407            Err(_) => select_direct()?,
408        }
409    };
410
411    // #257/#277: `mul`+`add`→`mla` fusion is intentionally NOT wired here.
412    // The transform is correct and ready (`synth_synthesis::liveness::fuse_mul_add`,
413    // fully tested), but it is **register-allocation-coupled**: over the current
414    // greedy single-pass selector, folding `mul rM,..; add rD,rM,rX` → `mla`
415    // extends the live ranges of the mul inputs to the mla point, and the added
416    // pressure (extra moves/spills) costs more than the single-cycle MLA saves —
417    // gale measured a +2 cyc on-target REGRESSION (flat_flight 255→257, G474RE)
418    // even though it removes 2 instructions and the seam stays 0x07FDF307. So the
419    // fusion stays unwired until the spill-aware allocator (VCR-RA-001) chooses
420    // registers, at which point it becomes net-positive (per #272's plan and the
421    // wiring design note). Lesson (#277): a register-pressure-affecting transform
422    // needs an on-target/allocator-aware gate, not a byte-count gate, before it
423    // can default on.
424
425    // VCR-RA-001 const-CSE / rematerialization-avoidance (#209): moved to run
426    // LAST, after the immediate-folds — see the apply_const_cse call below
427    // (#242). Earlier it ran here (before range-realloc and the folds), which is
428    // what let it grow gale's --relocatable `gust_mix` 90→92 B (#242 burndown,
429    // 2026-06-26): retargeting a read defeated a *downstream* immediate-fold that
430    // would otherwise have absorbed the constant. Running CSE-last makes those
431    // foldable consts already-folded-and-gone, so CSE only ever touches genuinely
432    // redundant materializations.
433
434    // VCR-RA-001 RANGE RE-ALLOCATION (#209/#242, wiring step 3a) — the first
435    // CONSEQUENTIAL allocator pass: re-colour each maximal straight-line
436    // segment over the R0-R8 pool with value ranges as the allocation unit
437    // (segment inputs + per-register live-outs pinned to their original
438    // registers, reserved R9-R12/SP identity-assigned — each segment is
439    // independently sound, no cross-segment liveness assumed). Renames
440    // registers only: never adds, removes, or reorders instructions, so
441    // labels/branch offsets are unaffected.
442    //
443    // DEFAULT-ON since v0.11.36: gale cleared the gate on-target (G474RE,
444    // #209 2026-06-10) — flag-on output byte-identical to flag-off on
445    // flat_flight/controller/control_step, fires on the filter family with
446    // zero cycle delta and a small size win, all selfchecks green on silicon.
447    // Opt out with `SYNTH_RANGE_REALLOC=0`; per-function stats with
448    // `SYNTH_REALLOC_STATS=1`.
449    //
450    // The companion dead callee-saved-save elimination (gale's "next
451    // consequential lever", same issue comment) then shrinks the prologue
452    // `push {r4-r8,lr}` / epilogue `pop {r4-r8,pc}` to the callee-saved
453    // registers the re-allocated body still touches (leaf-only,
454    // SP-untouched, even-count-padded — see shrink_callee_saved_saves):
455    // ~12 cycles of pure save/restore overhead removed on small leaves.
456    let realloc_on = std::env::var("SYNTH_RANGE_REALLOC").map_or(true, |v| v != "0");
457    let arm_instrs = if realloc_on {
458        use synth_synthesis::rules::Reg;
459        const POOL: [Reg; 9] = [
460            Reg::R0,
461            Reg::R1,
462            Reg::R2,
463            Reg::R3,
464            Reg::R4,
465            Reg::R5,
466            Reg::R6,
467            Reg::R7,
468            Reg::R8,
469        ];
470        let (out, stats) = synth_synthesis::liveness::reallocate_function(&arm_instrs, &POOL);
471        if std::env::var("SYNTH_REALLOC_STATS").is_ok() {
472            eprintln!(
473                "[range-realloc] {} segments: {} reallocated, {} declined ({} validator-rejected), {} need spill (step 4)",
474                stats.segments,
475                stats.reallocated,
476                stats.declined,
477                stats.validator_rejects,
478                stats.needs_spill
479            );
480        }
481        // VCR-RA-002 (#390, epic #242): eliminate a provably-dead stack frame
482        // (`sub sp,#N`/`add sp,#N` reserved by `compute_local_layout` for locals
483        // that promotion homed in registers, never accessed). Removing it saves
484        // the two instructions AND restores the SP-untouched precondition that
485        // `shrink_callee_saved_saves` requires — so it must run FIRST. Flag-off
486        // (opt-in `SYNTH_DEAD_FRAME_ELIM=1`); off ⇒ byte-identical. Default-on
487        // flip held for on-silicon validation, like the realloc/shrink levers.
488        let out = if std::env::var("SYNTH_DEAD_FRAME_ELIM").is_ok() {
489            synth_synthesis::liveness::elide_dead_frame(&out).unwrap_or(out)
490        } else {
491            out
492        };
493        // #490 (epic #242): the optimized selector uses r4-r8 as scratch /
494        // promoted locals but emits no prologue, silently clobbering a caller's
495        // callee-saved registers. Add the missing `push {r4-r8,lr}` /
496        // `pop {r4-r8,pc}` HERE — on the post-realloc body, where realloc has
497        // lowered low-pressure r4-r8 scratch back to r0-r3, so a save is added
498        // only for registers genuinely clobbered. `shrink_callee_saved_saves`
499        // (next) then trims it to the used set. No-op on the direct path (it
500        // already has its own prologue) and on callee-saved-free leaves.
501        let out = synth_synthesis::liveness::ensure_callee_saved_prologue(&out);
502        synth_synthesis::liveness::shrink_callee_saved_saves(&out).unwrap_or(out)
503    } else {
504        // Range-realloc off (`SYNTH_RANGE_REALLOC=0`): the optimized path still
505        // must preserve the callee-saved registers it clobbers (#490). No shrink
506        // (it is coupled to the realloc lever), so the conservative full save
507        // stays — correct, just not minimised in this debug configuration.
508        synth_synthesis::liveness::ensure_callee_saved_prologue(&arm_instrs)
509    };
510
511    // VCR-RA-001 SHADOW ALLOCATION (#209/#242): run the register allocator on
512    // the selected stream and LOG what it finds — without changing a single
513    // emitted byte. This is the measure-only bridge between the built analysis
514    // layer and the eventual virtual-register wiring: it shows, per real
515    // function, whether the allocator can colour it within the R0–R8 pool and
516    // how much const-CSE / rematerialization headroom exists (#209). Enable with
517    // `SYNTH_SHADOW_ALLOC=1`; off by default and side-effect-free either way.
518    if std::env::var("SYNTH_SHADOW_ALLOC").is_ok() {
519        use synth_synthesis::liveness::{
520            AllocationOutcome, allocate_function, function_peak_pressure,
521        };
522        // R9 globals / R10 mem-size / R11 mem-base / R12 IP-scratch are reserved;
523        // pin them above the 0..9 allocatable pool so the colourer keeps R0–R8.
524        let precolored = std::collections::BTreeMap::from([
525            (synth_synthesis::rules::Reg::R9, 9usize),
526            (synth_synthesis::rules::Reg::R10, 10),
527            (synth_synthesis::rules::Reg::R11, 11),
528            (synth_synthesis::rules::Reg::R12, 12),
529        ]);
530        // True VALUE pressure (one node per value, not per reused physical reg):
531        // a NeedsSpill with peak ≤ 9 is a SPURIOUS physical-register spill — the
532        // function fits once virtually allocated.
533        let peak = function_peak_pressure(&arm_instrs);
534        match allocate_function(&arm_instrs, 9, &precolored) {
535            AllocationOutcome::Allocated {
536                remat_opportunities,
537                coloring,
538            } => eprintln!(
539                "[shadow-alloc] OK: {} pregs coloured within R0-R8 pool, peak value-pressure {}, {} const-CSE/remat opportunities",
540                coloring.len(),
541                peak,
542                remat_opportunities
543            ),
544            AllocationOutcome::NeedsSpill(s) => eprintln!(
545                "[shadow-alloc] physical-graph would spill {:?}, but peak value-pressure is {} (≤9 ⇒ spurious; fits once virtually allocated)",
546                s, peak
547            ),
548            AllocationOutcome::Declined => {
549                eprintln!(
550                    "[shadow-alloc] declined (unmodeled construct — calls/i64/fp/offset-branch)"
551                )
552            }
553        }
554    }
555
556    // VCR-SEL-004 cmp→select → IT-block predication fusion (#242). The selector
557    // lowers a `select` whose condition is a comparison to a *materialize then
558    // re-test* sequence (`cmp a,b; SetCond D,c; cmp D,#0; movne dst,v1; moveq
559    // dst,v2`); this collapses it onto the comparison's own flags — deleting the
560    // `SetCond` and the `cmp D,#0` and retargeting the predicated moves to `c` /
561    // `invert(c)` — yielding the textbook predicated clamp (`cmp a,b; movc dst,v1;
562    // mov{!c} dst,v2`). −2 instructions per fused select. gale #428 measured this
563    // as the #1 hot-path size/cycle lever on the gust_mix clamp chain.
564    //
565    // Run LATE: after range re-allocation (so the dead-D proof sees final register
566    // identities) and before encode. Removal-only + rename-only ⇒ no spill
567    // regression and labels/branch offsets are unaffected. Each fusion is proven
568    // sound (flags reused only when nothing clobbers them in the window; the
569    // boolean deleted only when provably dead) — see `fuse_cmp_select`.
570    //
571    // DEFAULT-ON as of v0.13.0 (#428): cmp→select fusion ships by default. The
572    // byte-changing flip is validated by (a) the unicorn execution oracle that runs
573    // the two-move `mov{invert(c)}` arm (cmp_select_two_move_differential.py), (b)
574    // gale's gale_decider_diff 10,596-case sweep across all 8 verified primitives
575    // (native ≡ flag-off ≡ flag-on = 0x88e73178d232bcf5), and (c) the named-anchor
576    // differentials re-run with fusion ON — control_step still 0x00210A55, flat+
577    // inlined flight_algo still 0x07FDF307 (results preserved; bytes deliberately
578    // changed, re-frozen on this commit). Escape hatch: `SYNTH_NO_CMP_SELECT_FUSE=1`
579    // reverts to the pre-fusion lowering. The on-silicon G474RE DWT no-regression
580    // check is a tracked post-ship follow-up (gale owns it).
581    let arm_instrs = if std::env::var("SYNTH_NO_CMP_SELECT_FUSE").is_err() {
582        // The rewritten stream is identical to `fuse_cmp_select`'s 2-tuple form;
583        // the extra `two_move` count is diagnostic only (the fusion census /
584        // blast-radius datum — #7 made that arm reachable).
585        let (out, fused, two_move) =
586            synth_synthesis::liveness::fuse_cmp_select_with_stats(&arm_instrs);
587        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
588            let in_place = fused - two_move;
589            eprintln!(
590                "[cmp-select-fuse] {fused} select(s) fused to predicated moves \
591                 ({two_move} two-move, {in_place} in-place)"
592            );
593        }
594        out
595    } else {
596        arm_instrs
597    };
598
599    // Perf lever 1 toward native parity (#390): redundant stack-reload elimination.
600    // synth lowers every wasm local to a frame slot, so `local.set; local.get` emits
601    // `str rX,[sp,#N]; … ; ldr rY,[sp,#N]`; when rX still holds the value the reload
602    // (a ~2-cycle M4 load) becomes `mov rY,rX`. Removal-of-a-load + rename only ⇒ no
603    // new instruction form and no label/offset change. DEFAULT-ON (#242 feature
604    // loop): validated bit-identical RESULTS on every frozen anchor (control_step
605    // 0x00210A55 13/13, flat+inlined flight_algo 0x07FDF307) with .text reduced on
606    // the shipped --relocatable path, plus 8 unit tests + the frame_slot_dce
607    // execution differential — the same gated path cmp→select took to default-on in
608    // v0.13.0 (G474RE silicon confirms perf post-ship). Escape hatch:
609    // `SYNTH_NO_STACK_FWD=1` restores the frame-resident bytes (frozen-old goldens).
610    let stack_fwd = std::env::var("SYNTH_NO_STACK_FWD").is_err();
611    let arm_instrs = if stack_fwd {
612        let (out, fwd) = synth_synthesis::liveness::forward_stack_reloads(&arm_instrs);
613        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
614            eprintln!("[stack-fwd] {fwd} stack reload(s) forwarded to register moves");
615        }
616        out
617    } else {
618        arm_instrs
619    };
620
621    // VCR-RA frame-slot DCE (#242): once `forward_stack_reloads` has turned the
622    // reloads of a spill slot into register moves, the `str rX,[sp,#N]` that fed
623    // them is a dead store — its slot is never loaded again. Remove it. Pairs
624    // with (and only pays after) stack-reload forwarding, so it shares the flag.
625    let arm_instrs = if stack_fwd {
626        let (out, n) = synth_synthesis::liveness::eliminate_dead_frame_stores(&arm_instrs);
627        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
628            eprintln!("[frame-slot-dce] {n} dead frame store(s) removed");
629        }
630        out
631    } else {
632        arm_instrs
633    };
634
635    // VCR-RA-001 spill re-choice spike (#242): slot-value forwarding BETWEEN
636    // reloads. `forward_stack_reloads` (above) forwards only from a spill
637    // store's SOURCE register, so when register pressure clobbers that source
638    // — the genuine-spill case, flat_flight's peak-11 hot segment — its
639    // reloads survive. This pass tracks which registers provably still hold a
640    // frame slot's value (through earlier reloads and reg-reg moves) and turns
641    // reload #2..#n into a 1-cycle `mov` (or deletes it when the target
642    // already holds the value). A forwarded reload can leave the feeding
643    // store dead, so the frame-slot DCE sweep runs once more behind the same
644    // flag. Per-segment commit gates: never grows, and post-transform peak
645    // value pressure must fit the R0–R8 pool or not exceed the pre-transform
646    // peak (see `apply_spill_realloc`). Flag-off (`SYNTH_SPILL_REALLOC=1`)
647    // while differential-validated; off ⇒ byte-identical.
648    let arm_instrs = if std::env::var("SYNTH_SPILL_REALLOC").is_ok() {
649        let (out, n) = synth_synthesis::liveness::apply_spill_realloc(&arm_instrs);
650        let (out, d) = synth_synthesis::liveness::eliminate_dead_frame_stores(&out);
651        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
652            eprintln!(
653                "[spill-realloc] {n} reload(s) forwarded/eliminated, {d} newly-dead frame store(s) removed"
654            );
655        }
656        out
657    } else {
658        arm_instrs
659    };
660
661    // VCR-RA immediate-shift folding (#390, #242): a constant shift amount the
662    // stack selector materialized into a scratch register (`movw rM,#C; lsl rD,rN,rM`)
663    // folds to the immediate form (`lsl rD,rN,#C`), removing the dead `movw` — −1
664    // instruction, −1 live register. Removal-only (offset-neutral before branch
665    // resolution, like the dead-store pass). DEFAULT-ON as of v0.15.0: validated
666    // bit-identical results + a net cycle win on the dissolved hot path (−2
667    // cyc/call, .text 100→90 B on gust_mix). Escape hatch: `SYNTH_NO_IMM_SHIFT_FOLD=1`.
668    let arm_instrs = if std::env::var("SYNTH_NO_IMM_SHIFT_FOLD").is_err() {
669        let (out, folds) = synth_synthesis::liveness::fold_immediate_shifts(&arm_instrs);
670        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
671            eprintln!(
672                "[imm-shift-fold] {folds} register shift(s) folded to immediate, movw dropped"
673            );
674        }
675        out
676    } else {
677        arm_instrs
678    };
679
680    // VCR-RA uxth/uxtb fold (#428, #242): `movw rM,#0xffff; and rD,rN,rM` →
681    // `uxth rD,rN` (and the 0xff/uxtb form), removing the dead `movw` — −1
682    // instruction, −1 live register per 16/8-bit mask. 0xffff/0xff are not Thumb-2
683    // modified immediates so the selector materializes them into a register; the
684    // dedicated zero-extend expresses the same masking inline. Removal-only +
685    // rewrite-in-place (offset-neutral). FLAG-OFF by default (opt-in
686    // `SYNTH_UXTH_FOLD=1`) ⇒ bit-identical (frozen gate green); the byte-changing
687    // default-on flip is the separate on-target-gated step, like the prior levers.
688    let arm_instrs = if std::env::var("SYNTH_UXTH_FOLD").is_ok() {
689        let (out, folds) = synth_synthesis::liveness::fold_uxth(&arm_instrs);
690        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
691            eprintln!("[uxth-fold] {folds} mask-and folded to uxth/uxtb, movw dropped");
692        }
693        out
694    } else {
695        arm_instrs
696    };
697
698    // VCR-RA-001 const-CSE / rematerialization-avoidance (#209, #242). Drops a
699    // `movw`/`mov #imm` that re-materializes a constant already resident in
700    // another register and retargets the reads — every rewrite proven by the
701    // liveness analysis. Runs LAST, after every immediate-fold (shift, uxth) and
702    // range-realloc, but BEFORE branch resolution/encoding (it removes
703    // instructions, shifting byte offsets). CSE-last is the #242 no-regression
704    // fix: the folds have already absorbed every foldable constant, so CSE can no
705    // longer defeat one (the gust_mix 90→92 mechanism). The pass additionally
706    // size-guards each segment via the byte-estimator — it commits a segment's
707    // rewrites only if they do not grow its estimated size — so a retarget that
708    // would flip a 16-bit encoding to 32-bit (higher base register) is declined.
709    // Behind `SYNTH_CONST_CSE=1` while validated against the differential oracle;
710    // off by default keeps every fixture bit-identical.
711    let arm_instrs = if std::env::var("SYNTH_CONST_CSE").is_ok() {
712        let (out, removed) = synth_synthesis::liveness::apply_const_cse(&arm_instrs);
713        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
714            eprintln!("[const-cse] {removed} redundant constant materialization(s) removed");
715        }
716        out
717    } else {
718        arm_instrs
719    };
720
721    // VCR-RA-001 spill-choice REPORT (#242): measure-only, like SYNTH_SHADOW_ALLOC.
722    // Per straight-line segment, the frame-slot traffic actually emitted vs the
723    // reload/store count a farthest-next-use (Belady) allocation over the R0-R8
724    // pool would need — the measured headroom for the full spill-choice rewrite.
725    // Printed on the FINAL stream (post all rewrite passes), so a flag-off run
726    // reports the greedy baseline and a flag-on run reports what remains.
727    if std::env::var("SYNTH_SPILL_REPORT").is_ok() {
728        for seg in synth_synthesis::liveness::spill_choice_report(&arm_instrs, 9) {
729            if seg.actual_reloads + seg.actual_spill_stores > 0 || seg.peak_pressure > 9 {
730                eprintln!(
731                    "[spill-report] seg@{} len={} peak={} actual={}ld+{}st belady(k=9)={}ld+{}st",
732                    seg.start,
733                    seg.len,
734                    seg.peak_pressure,
735                    seg.actual_reloads,
736                    seg.actual_spill_stores,
737                    seg.belady_reloads,
738                    seg.belady_spill_stores
739                );
740            }
741        }
742    }
743
744    // ISA feature gate: validate that all generated instructions are supported
745    // by the target. This catches FPU instructions on no-FPU targets, double-precision
746    // instructions on single-precision targets, etc.
747    validate_instructions(&arm_instrs, config.target.fpu, &config.target.triple)
748        .map_err(|e| format!("ISA validation failed: {}", e))?;
749
750    // Encode to binary — use Thumb-2 for Cortex-M targets
751    let use_thumb2 = matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb);
752
753    let encoder = if use_thumb2 {
754        ArmEncoder::new_thumb2_with_fpu(config.target.fpu)
755    } else {
756        ArmEncoder::new_arm32()
757    };
758
759    // #202: resolve local label branches (Bcc/B/Bhs/Blo) to byte-accurate
760    // offsets before encoding. `select_with_stack` emits them as label
761    // placeholders and never resolves them — without this they encode as
762    // `bne.n #0` and land mid-instruction whenever a 32-bit Thumb-2 instruction
763    // sits between the branch and its target (UsageFault on real hardware).
764    // Only meaningful for Thumb-2 (the offset units are halfword/PC+4).
765    let arm_instrs = if use_thumb2 {
766        resolve_label_branches(arm_instrs, &encoder)?
767    } else {
768        arm_instrs
769    };
770
771    let mut code = Vec::new();
772    let mut relocations = Vec::new();
773
774    // #345: literal-pool address loads. Each `LdrSym` was encoded as a placeholder
775    // `LDR.W rd,[pc,#0]`; record where its instruction sits and what it loads so
776    // we can append a pooled word (carrying the symbol address via R_ARM_ABS32)
777    // and patch the PC-relative offset once the pool position is known.
778    struct PendingLiteral {
779        ldr_offset: u32,
780        symbol: String,
781        addend: i32,
782    }
783    let mut pending_literals: Vec<PendingLiteral> = Vec::new();
784
785    // VCR-DBG-001: per-instruction source map for DWARF `.debug_line`. Captured
786    // here because `code.len()` immediately before `encode()` is the final
787    // machine offset of the instruction within this function's `.text` — nothing
788    // after the loop shifts earlier instructions (the literal pool is appended at
789    // the end; the LDR patch below is in-place/length-preserving). Purely
790    // additive: it does not touch `code`, so `.text` is byte-identical.
791    let mut line_map: LineMap = Vec::new();
792
793    for instr in &arm_instrs {
794        // Record a relocation for every BL: the encoder emits `bl #0` and
795        // relies on a relocation to patch the target. This covers BOTH import
796        // dispatch stubs (`__meld_*`, undefined externals) AND internal calls
797        // (`func_N`, defined in this object). Previously only `__meld_*` was
798        // recorded, so internal `BL func_N` calls were left as unpatched
799        // `bl #0` placeholders branching to a garbage address (#167).
800        if let ArmOp::Bl { label } = &instr.op {
801            relocations.push(CodeRelocation {
802                offset: code.len() as u32,
803                symbol: label.clone(),
804                kind: synth_core::backend::RelocKind::ThmCall,
805            });
806        }
807        // #237: symbol-relative MOVW/MOVT (the `--native-pointer-abi` static-data
808        // addressing). The encoder writes the addend in place; record the matching
809        // R_ARM_MOVW_ABS_NC / R_ARM_MOVT_ABS so the linker adds the symbol address.
810        if let ArmOp::MovwSym { symbol, .. } = &instr.op {
811            relocations.push(CodeRelocation {
812                offset: code.len() as u32,
813                symbol: symbol.clone(),
814                kind: synth_core::backend::RelocKind::MovwAbs,
815            });
816        }
817        if let ArmOp::MovtSym { symbol, .. } = &instr.op {
818            relocations.push(CodeRelocation {
819                offset: code.len() as u32,
820                symbol: symbol.clone(),
821                kind: synth_core::backend::RelocKind::MovtAbs,
822            });
823        }
824        // #345: defer the literal-pool word + reloc + offset patch to the
825        // post-loop pass (the pool address is not yet known).
826        if let ArmOp::LdrSym { symbol, addend, .. } = &instr.op {
827            pending_literals.push(PendingLiteral {
828                ldr_offset: code.len() as u32,
829                symbol: symbol.clone(),
830                addend: *addend,
831            });
832        }
833
834        // The machine offset of this instruction is the current code length,
835        // captured before the bytes are appended.
836        line_map.push((code.len() as u32, instr.source_line));
837
838        let encoded = encoder
839            .encode(&instr.op)
840            .map_err(|e| format!("ARM encoding failed: {}", e))?;
841        code.extend_from_slice(&encoded);
842    }
843
844    // #345: place the literal pool at the end of this function's `.text`. Gated on
845    // there being at least one `LdrSym` — functions without one are byte-identical
846    // to before (no trailing padding, so downstream `func_offsets` are unchanged
847    // and the frozen differential fixtures stay bit-for-bit equal).
848    if !pending_literals.is_empty() {
849        if !use_thumb2 {
850            return Err("LdrSym literal-pool addressing requires Thumb-2".to_string());
851        }
852        // 4-byte align the pool start (Thumb-2 word loads require it, and
853        // `Align(PC,4)` in the LDR-literal semantics assumes a word-aligned pool).
854        while code.len() % 4 != 0 {
855            code.push(0x00);
856        }
857        // One distinct pooled word per LdrSym (no dedup: different sites carry
858        // different addends, and the REL addend lives in the word).
859        for lit in &pending_literals {
860            let word_offset = code.len() as u32;
861
862            // REL semantics: the linker computes `S + A`, where A is the in-place
863            // value of the relocated word. Initialize the word to the addend so
864            // the final loaded address is `symbol + addend`.
865            code.extend_from_slice(&(lit.addend as u32).to_le_bytes());
866            relocations.push(CodeRelocation {
867                offset: word_offset,
868                symbol: lit.symbol.clone(),
869                kind: synth_core::backend::RelocKind::Abs32,
870            });
871
872            // Patch the placeholder `LDR.W rd,[pc,#imm12]`. Thumb-2 LDR (literal):
873            // address = Align(PC,4) + imm12, with PC = ldr_offset + 4. The pool is
874            // always after the LDR, so U=1 (already set in hw1 = 0xF8DF).
875            let pc = lit.ldr_offset + 4;
876            let aligned_pc = pc & !3u32;
877            let imm12 = word_offset - aligned_pc;
878            if imm12 > 0xFFF {
879                // Wide LDR-literal range is ±4 KB; these function bodies are far
880                // smaller, but fail cleanly rather than miscompile if exceeded.
881                return Err(format!(
882                    "LdrSym literal pool out of range (#345): imm12={} > 4095 \
883                     for symbol {}",
884                    imm12, lit.symbol
885                ));
886            }
887            let hw2_off = (lit.ldr_offset + 2) as usize;
888            let mut hw2 = u16::from_le_bytes([code[hw2_off], code[hw2_off + 1]]);
889            hw2 = (hw2 & 0xF000) | (imm12 as u16); // keep Rt, set imm12
890            let hw2_bytes = hw2.to_le_bytes();
891            code[hw2_off] = hw2_bytes[0];
892            code[hw2_off + 1] = hw2_bytes[1];
893        }
894    }
895
896    Ok((code, relocations, line_map))
897}
898
899/// Resolve local label branches to byte-accurate offsets (#202).
900///
901/// `select_with_stack` emits conditional/unconditional branches as label
902/// placeholders (`Bcc`/`B`/`Bhs`/`Blo` + `Label`) and never resolves them; the
903/// encoder then emits a `0xD000`/`0xE000` placeholder with offset 0. Before #197
904/// this path only ran for `--no-optimize`/declined functions, so the latent bug
905/// stayed hidden — routing relocatable code through it surfaced branches that
906/// land mid-instruction (a Cortex-M UsageFault) whenever a 32-bit Thumb-2
907/// instruction sits between the branch and its target.
908///
909/// This pass encodes each instruction to learn its real byte length (so 16- vs
910/// 32-bit forms and multi-instruction expansions are exact), maps each `Label`
911/// to its byte position, and rewrites every label branch to the displacement
912/// the encoder consumes: `(target - branch - 4) / 2` halfwords. A bounded
913/// fixed-point handles an offset growing a branch from 16- to 32-bit (which
914/// shifts later positions). `BCondOffset`/`BOffset` already produced inline by
915/// the optimized path carry no label and are left untouched.
916fn resolve_label_branches(
917    arm_instrs: Vec<ArmInstruction>,
918    encoder: &ArmEncoder,
919) -> Result<Vec<ArmInstruction>, String> {
920    use std::collections::HashMap;
921    use synth_synthesis::Condition;
922
923    enum BKind {
924        Cond(Condition),
925        Uncond,
926    }
927    // Record each label branch ONCE — indices are stable across iterations.
928    let mut branches: Vec<(usize, BKind, String)> = Vec::new();
929    for (i, instr) in arm_instrs.iter().enumerate() {
930        match &instr.op {
931            ArmOp::Bcc { cond, label } => branches.push((i, BKind::Cond(*cond), label.clone())),
932            ArmOp::Bhs { label } => branches.push((i, BKind::Cond(Condition::HS), label.clone())),
933            ArmOp::Blo { label } => branches.push((i, BKind::Cond(Condition::LO), label.clone())),
934            ArmOp::B { label } => branches.push((i, BKind::Uncond, label.clone())),
935            _ => {}
936        }
937    }
938    if branches.is_empty() {
939        return Ok(arm_instrs);
940    }
941
942    let mut resolved = arm_instrs;
943    // Sizes only grow (16→32-bit), so this converges quickly; cap for safety.
944    for _ in 0..16 {
945        // 1. Byte position of each instruction (Label encodes to 0 bytes).
946        let mut positions = Vec::with_capacity(resolved.len());
947        let mut pos: i64 = 0;
948        for instr in &resolved {
949            positions.push(pos);
950            pos += encoder
951                .encode(&instr.op)
952                .map_err(|e| format!("branch-resolve size probe failed: {}", e))?
953                .len() as i64;
954        }
955        // 2. Label name -> byte position (owned keys so the borrow ends here).
956        let mut labels: HashMap<String, i64> = HashMap::new();
957        for (i, instr) in resolved.iter().enumerate() {
958            if let ArmOp::Label { name } = &instr.op {
959                labels.insert(name.clone(), positions[i]);
960            }
961        }
962        // 3. Rewrite each branch to its byte-accurate offset.
963        let mut changed = false;
964        for (idx, kind, label) in &branches {
965            // A label not defined locally is an EXTERNAL target (e.g.
966            // `Trap_Handler` resolved by a relocation / the vector table). Leave
967            // such branches as their placeholder for the existing relocation
968            // path — only local control-flow labels are byte-resolved here.
969            let Some(&target) = labels.get(label) else {
970                continue;
971            };
972            // Encoder consumes the field as (target - branch - 4) / 2 halfwords.
973            // Positions are always even, so this division is exact.
974            let halfword_offset = ((target - positions[*idx] - 4) / 2) as i32;
975            let new_op = match kind {
976                BKind::Cond(c) => ArmOp::BCondOffset {
977                    cond: *c,
978                    offset: halfword_offset,
979                },
980                BKind::Uncond => ArmOp::BOffset {
981                    offset: halfword_offset,
982                },
983            };
984            if resolved[*idx].op != new_op {
985                resolved[*idx].op = new_op;
986                changed = true;
987            }
988        }
989        if !changed {
990            break;
991        }
992    }
993    Ok(resolved)
994}
995
996#[cfg(test)]
997mod tests {
998    use super::*;
999
1000    /// #539: `i32.const 0; memory.grow m` folds to `memory.size m`; other deltas
1001    /// (const non-zero, runtime) are left as `memory.grow` (→ the sound fixed-
1002    /// memory -1). Non-grow ops are untouched, so functions without the idiom are
1003    /// byte-identical.
1004    #[test]
1005    fn test_rewrite_memory_grow_zero_539() {
1006        // the idiom -> memory.size
1007        assert_eq!(
1008            rewrite_memory_grow_zero(&[WasmOp::I32Const(0), WasmOp::MemoryGrow(0)]),
1009            vec![WasmOp::MemorySize(0)]
1010        );
1011        // const non-zero delta: NOT folded
1012        assert_eq!(
1013            rewrite_memory_grow_zero(&[WasmOp::I32Const(2), WasmOp::MemoryGrow(0)]),
1014            vec![WasmOp::I32Const(2), WasmOp::MemoryGrow(0)]
1015        );
1016        // runtime delta (no preceding const): NOT folded
1017        assert_eq!(
1018            rewrite_memory_grow_zero(&[WasmOp::LocalGet(0), WasmOp::MemoryGrow(0)]),
1019            vec![WasmOp::LocalGet(0), WasmOp::MemoryGrow(0)]
1020        );
1021        // a bare const-0 not feeding a grow is untouched
1022        assert_eq!(
1023            rewrite_memory_grow_zero(&[WasmOp::I32Const(0), WasmOp::I32Add]),
1024            vec![WasmOp::I32Const(0), WasmOp::I32Add]
1025        );
1026        // fold is local: surrounding ops preserved, indices past the fold intact
1027        assert_eq!(
1028            rewrite_memory_grow_zero(&[
1029                WasmOp::LocalGet(0),
1030                WasmOp::I32Const(0),
1031                WasmOp::MemoryGrow(0),
1032                WasmOp::I32Add,
1033            ]),
1034            vec![WasmOp::LocalGet(0), WasmOp::MemorySize(0), WasmOp::I32Add]
1035        );
1036    }
1037
1038    #[test]
1039    fn test_arm_backend_name() {
1040        let backend = ArmBackend::new();
1041        assert_eq!(backend.name(), "arm");
1042        assert!(backend.is_available());
1043    }
1044
1045    #[test]
1046    fn test_arm_backend_capabilities() {
1047        let backend = ArmBackend::new();
1048        let caps = backend.capabilities();
1049        assert!(!caps.produces_elf);
1050        assert!(caps.supports_rule_verification);
1051        assert!(!caps.is_external);
1052    }
1053
1054    #[test]
1055    fn test_compile_add_function() {
1056        let backend = ArmBackend::new();
1057        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
1058        let config = CompileConfig::default();
1059
1060        let result = backend.compile_function("add", &ops, &config);
1061        assert!(result.is_ok());
1062
1063        let func = result.unwrap();
1064        assert_eq!(func.name, "add");
1065        assert!(!func.code.is_empty());
1066        assert_eq!(func.wasm_ops, ops);
1067    }
1068
1069    /// VCR-DBG-001: the per-instruction source map must cover the function with
1070    /// monotonic, in-bounds machine offsets, and must not perturb the emitted
1071    /// code (it is captured at encode time, never serialized here).
1072    #[test]
1073    fn test_line_map_is_wellformed_dbg001() {
1074        let backend = ArmBackend::new();
1075        let ops = vec![
1076            WasmOp::LocalGet(0),
1077            WasmOp::LocalGet(1),
1078            WasmOp::I32Add,
1079            WasmOp::End,
1080        ];
1081        let config = CompileConfig::default();
1082        let func = backend.compile_function("add", &ops, &config).unwrap();
1083
1084        // Non-empty, and the first instruction starts at machine offset 0.
1085        assert!(
1086            !func.line_map.is_empty(),
1087            "a non-trivial function captures a source map"
1088        );
1089        assert_eq!(func.line_map[0].0, 0, "first instruction at offset 0");
1090
1091        // Offsets strictly increase by at least one ARM/Thumb instruction (>= 2
1092        // bytes) and every mapped offset lies inside the emitted `.text`.
1093        for w in func.line_map.windows(2) {
1094            assert!(w[1].0 > w[0].0, "instruction offsets strictly increase");
1095            assert!(
1096                w[1].0 - w[0].0 >= 2,
1097                "each ARM/Thumb instruction is >= 2 bytes"
1098            );
1099        }
1100        let last = func.line_map.last().unwrap().0 as usize;
1101        assert!(
1102            last < func.code.len(),
1103            "every mapped offset lies inside .text"
1104        );
1105
1106        // The side-table is additive: recompiling is deterministic and the map is
1107        // consistent with that exact code (capturing it does not alter output).
1108        let again = backend.compile_function("add", &ops, &config).unwrap();
1109        assert_eq!(
1110            again.code, func.code,
1111            "compilation deterministic; map is additive"
1112        );
1113        assert_eq!(again.line_map, func.line_map);
1114    }
1115
1116    #[test]
1117    fn test_count_params() {
1118        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
1119        assert_eq!(count_params(&ops), 2);
1120
1121        let no_params = vec![WasmOp::I32Const(5), WasmOp::I32Const(3), WasmOp::I32Add];
1122        assert_eq!(count_params(&no_params), 0);
1123    }
1124
1125    #[test]
1126    fn test_arm_backend_register() {
1127        let mut registry = synth_core::BackendRegistry::new();
1128        registry.register(Box::new(ArmBackend::new()));
1129        assert!(registry.get("arm").is_some());
1130        assert_eq!(registry.available().len(), 1);
1131    }
1132
1133    #[test]
1134    fn test_compile_import_call_produces_relocations() {
1135        let backend = ArmBackend::new();
1136        // Simulate a WASM module where func index 0 is an import.
1137        // Call(0) should generate MOV R0, #0; BL __meld_dispatch_import
1138        let ops = vec![WasmOp::Call(0)];
1139        let config = CompileConfig {
1140            num_imports: 1,
1141            no_optimize: true, // Direct instruction selection to preserve Call semantics
1142            ..CompileConfig::default()
1143        };
1144
1145        let result = backend.compile_function("caller", &ops, &config);
1146        assert!(result.is_ok());
1147
1148        let func = result.unwrap();
1149        assert!(!func.code.is_empty());
1150        assert_eq!(func.relocations.len(), 1);
1151        assert_eq!(func.relocations[0].symbol, "__meld_dispatch_import");
1152        // The BL is the second instruction (after MOV R0, #0), so offset should be > 0
1153        assert!(func.relocations[0].offset > 0);
1154    }
1155
1156    /// Regression test for #197: in `relocatable` mode, an import call must
1157    /// relocate against the direct `func_N` symbol (rewritten to the wasm field
1158    /// name by `build_relocatable_elf`), NOT `__meld_dispatch_import`. This is
1159    /// the ABI half of the #197 fix — without it, a host linker cannot resolve
1160    /// the call to the real kernel symbol (e.g. `k_spin_lock`).
1161    #[test]
1162    fn test_compile_relocatable_import_uses_direct_func_symbol_197() {
1163        let backend = ArmBackend::new();
1164        let ops = vec![WasmOp::Call(0)]; // func 0 is an import
1165        let config = CompileConfig {
1166            num_imports: 1,
1167            relocatable: true,
1168            ..CompileConfig::default()
1169        };
1170
1171        let func = backend
1172            .compile_function("caller", &ops, &config)
1173            .expect("relocatable import call compiles");
1174
1175        assert_eq!(func.relocations.len(), 1);
1176        assert_eq!(
1177            func.relocations[0].symbol, "func_0",
1178            "#197: relocatable import must relocate against func_0 (→ field name), not Meld dispatch"
1179        );
1180    }
1181
1182    #[test]
1183    fn test_compile_no_imports_no_relocations() {
1184        let backend = ArmBackend::new();
1185        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
1186        let config = CompileConfig::default();
1187
1188        let func = backend.compile_function("add", &ops, &config).unwrap();
1189        assert!(func.relocations.is_empty());
1190    }
1191
1192    /// Regression test for #167: a call to an INTERNAL function
1193    /// (index `>= num_imports`) must record a relocation against `func_{index}`.
1194    /// Before the fix, only `__meld_*` (import) BLs were relocated, so
1195    /// internal `BL func_N` was emitted as an unpatched `bl #0` branching
1196    /// to a garbage address — making the object non-linkable. This test
1197    /// would have caught that regression.
1198    #[test]
1199    fn test_compile_internal_call_produces_relocation_167() {
1200        let backend = ArmBackend::new();
1201        // num_imports = 1, so Call(2) is an INTERNAL call → `BL func_2`.
1202        let ops = vec![WasmOp::Call(2)];
1203        let config = CompileConfig {
1204            num_imports: 1,
1205            no_optimize: true,
1206            ..CompileConfig::default()
1207        };
1208
1209        let func = backend
1210            .compile_function("caller", &ops, &config)
1211            .expect("internal call compiles");
1212
1213        assert_eq!(
1214            func.relocations.len(),
1215            1,
1216            "an internal call must emit exactly one relocation (#167)"
1217        );
1218        assert_eq!(
1219            func.relocations[0].symbol, "func_2",
1220            "internal call must relocate against the callee's func_{{index}} symbol (#167)"
1221        );
1222    }
1223
1224    // ─── Phase 1 safety-bounds plumbing for ARM ──────────────────────────
1225
1226    #[test]
1227    fn arm_safety_bounds_mpu_emits_same_code_as_none() {
1228        // Mpu mode must not introduce any inline check on ARM — the MPU
1229        // handles faults via hardware. The encoded bytes for an i32.load
1230        // should be identical between None and Mpu.
1231        let backend = ArmBackend::new();
1232        let ops = vec![
1233            WasmOp::LocalGet(0),
1234            WasmOp::I32Load {
1235                offset: 0,
1236                align: 2,
1237            },
1238        ];
1239        let cfg_none = CompileConfig {
1240            no_optimize: true,
1241            ..Default::default()
1242        };
1243        let cfg_mpu = CompileConfig {
1244            no_optimize: true,
1245            safety_bounds: SafetyBounds::Mpu,
1246            ..Default::default()
1247        };
1248        let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
1249        let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
1250        assert_eq!(
1251            n.code, m.code,
1252            "Mpu and None should produce identical ARM bytes (Mpu relies on hardware)"
1253        );
1254    }
1255
1256    #[test]
1257    fn arm_legacy_bounds_check_still_emits_software_check() {
1258        // Legacy CLI users with `--bounds-check` should keep getting the
1259        // software path even though the new SafetyBounds field defaults to None.
1260        let backend = ArmBackend::new();
1261        let ops = vec![
1262            WasmOp::LocalGet(0),
1263            WasmOp::I32Load {
1264                offset: 0,
1265                align: 2,
1266            },
1267        ];
1268        let cfg_legacy = CompileConfig {
1269            no_optimize: true,
1270            bounds_check: true,
1271            ..Default::default()
1272        };
1273        let cfg_software = CompileConfig {
1274            no_optimize: true,
1275            safety_bounds: SafetyBounds::Software,
1276            ..Default::default()
1277        };
1278        let l = backend.compile_function("ld", &ops, &cfg_legacy).unwrap();
1279        let s = backend.compile_function("ld", &ops, &cfg_software).unwrap();
1280        assert_eq!(
1281            l.code, s.code,
1282            "--bounds-check should produce the same bytes as --safety-bounds=software"
1283        );
1284    }
1285
1286    // ========================================================================
1287    // ISA feature gate tests — ensure the compiler never emits unsupported
1288    // instructions for a given target
1289    // ========================================================================
1290
1291    #[test]
1292    fn test_f32_rejected_on_cortex_m3_no_fpu() {
1293        let backend = ArmBackend::new();
1294        let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
1295        let config = CompileConfig {
1296            target: TargetSpec::cortex_m3(),
1297            no_optimize: true,
1298            ..CompileConfig::default()
1299        };
1300
1301        let result = backend.compile_function("fadd", &ops, &config);
1302        assert!(
1303            result.is_err(),
1304            "f32 operations should fail on Cortex-M3 (no FPU)"
1305        );
1306    }
1307
1308    #[test]
1309    fn test_f32_accepted_on_cortex_m4f() {
1310        let backend = ArmBackend::new();
1311        let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
1312        let config = CompileConfig {
1313            target: TargetSpec::cortex_m4f(),
1314            no_optimize: true,
1315            ..CompileConfig::default()
1316        };
1317
1318        let result = backend.compile_function("fadd", &ops, &config);
1319        assert!(
1320            result.is_ok(),
1321            "f32 operations should succeed on Cortex-M4F, got: {:?}",
1322            result.unwrap_err()
1323        );
1324    }
1325
1326    #[test]
1327    fn test_i32_works_on_all_targets() {
1328        let backend = ArmBackend::new();
1329        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
1330
1331        // Cortex-M3 (no FPU)
1332        let config_m3 = CompileConfig {
1333            target: TargetSpec::cortex_m3(),
1334            no_optimize: true,
1335            ..CompileConfig::default()
1336        };
1337        assert!(
1338            backend.compile_function("add", &ops, &config_m3).is_ok(),
1339            "i32 ops should work on Cortex-M3"
1340        );
1341
1342        // Cortex-M4F (single FPU)
1343        let config_m4f = CompileConfig {
1344            target: TargetSpec::cortex_m4f(),
1345            no_optimize: true,
1346            ..CompileConfig::default()
1347        };
1348        assert!(
1349            backend.compile_function("add", &ops, &config_m4f).is_ok(),
1350            "i32 ops should work on Cortex-M4F"
1351        );
1352
1353        // Cortex-M7DP (double FPU)
1354        let config_m7dp = CompileConfig {
1355            target: TargetSpec::cortex_m7dp(),
1356            no_optimize: true,
1357            ..CompileConfig::default()
1358        };
1359        assert!(
1360            backend.compile_function("add", &ops, &config_m7dp).is_ok(),
1361            "i32 ops should work on Cortex-M7DP"
1362        );
1363    }
1364
1365    #[test]
1366    fn test_f32_rejected_on_cortex_m4_no_fpu() {
1367        // Cortex-M4 (without F suffix) has no FPU
1368        let backend = ArmBackend::new();
1369        let ops = vec![WasmOp::F32Const(1.5), WasmOp::F32Const(2.5), WasmOp::F32Mul];
1370        let config = CompileConfig {
1371            target: TargetSpec::cortex_m4(),
1372            no_optimize: true,
1373            ..CompileConfig::default()
1374        };
1375
1376        let result = backend.compile_function("fmul", &ops, &config);
1377        assert!(
1378            result.is_err(),
1379            "f32 operations should fail on Cortex-M4 (no FPU)"
1380        );
1381    }
1382
1383    // ========================================================================
1384    // Issue #120 — f32 ops in the optimized lowering path
1385    //
1386    // `OptimizerBridge::wasm_to_ir` has no handlers for f32/f64 ops, so a
1387    // value-producing float op fell through to `Opcode::Nop`, leaving a
1388    // downstream consumer with an unmapped vreg and tripping the PR #101
1389    // defensive panic in `ir_to_arm`. Customer reproducer: `compiler_builtins
1390    // float::div` and `gale_compute_ipi_mask` in the `falcon-rate-component`
1391    // module.
1392    //
1393    // Fix: `optimize_full` declines float modules with a typed `Err`;
1394    // `compile_wasm_to_arm` falls back to the non-optimized `select_with_stack`
1395    // path, which handles f32 via VFP/FPU. These tests use the *default*
1396    // (optimized) config — `no_optimize` is NOT set — which is the exact
1397    // configuration that panicked pre-fix.
1398    // ========================================================================
1399
1400    /// Pre-fix: this panicked with "vreg vN has no assigned ARM register and
1401    /// no spill slot" inside `ir_to_arm`. Post-fix: the optimized path declines
1402    /// the module and the backend falls back to direct selection, producing a
1403    /// non-empty f32.div lowering on a Cortex-M4F.
1404    #[test]
1405    fn test_issue120_f32_div_compiles_via_optimized_default() {
1406        let backend = ArmBackend::new();
1407        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
1408        let config = CompileConfig {
1409            target: TargetSpec::cortex_m4f(),
1410            // no_optimize NOT set — this exercises the optimized path that
1411            // panicked in issue #120, then the fallback to direct selection.
1412            ..CompileConfig::default()
1413        };
1414
1415        let result = backend.compile_function("fdiv", &ops, &config);
1416        assert!(
1417            result.is_ok(),
1418            "f32.div must compile on Cortex-M4F via the optimized->direct \
1419             fallback (issue #120), got: {:?}",
1420            result.as_ref().err()
1421        );
1422        assert!(
1423            !result.unwrap().code.is_empty(),
1424            "f32.div must produce non-empty machine code"
1425        );
1426    }
1427
1428    /// A spread of f32 ops, all through the optimized (default) config, must
1429    /// compile via the fallback on an FPU target without panicking.
1430    #[test]
1431    fn test_issue120_assorted_f32_ops_compile_via_optimized_default() {
1432        let backend = ArmBackend::new();
1433        let config = CompileConfig {
1434            target: TargetSpec::cortex_m4f(),
1435            ..CompileConfig::default()
1436        };
1437
1438        let cases: Vec<(&str, Vec<WasmOp>)> = vec![
1439            (
1440                "fadd",
1441                vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Add],
1442            ),
1443            (
1444                "fmul",
1445                vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Mul],
1446            ),
1447            (
1448                "fsub",
1449                vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Sub],
1450            ),
1451        ];
1452
1453        for (name, ops) in cases {
1454            let result = backend.compile_function(name, &ops, &config);
1455            assert!(
1456                result.is_ok(),
1457                "{name} must compile via the optimized->direct fallback \
1458                 (issue #120), got: {:?}",
1459                result.as_ref().err()
1460            );
1461            assert!(
1462                !result.unwrap().code.is_empty(),
1463                "{name} must produce non-empty machine code"
1464            );
1465        }
1466    }
1467
1468    /// The fallback must still honor the ISA feature gate: f32 on a no-FPU
1469    /// target must fail cleanly (not panic) even on the optimized path.
1470    #[test]
1471    fn test_issue120_f32_div_rejected_on_no_fpu_via_optimized() {
1472        let backend = ArmBackend::new();
1473        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
1474        let config = CompileConfig {
1475            target: TargetSpec::cortex_m3(),
1476            ..CompileConfig::default()
1477        };
1478
1479        let result = backend.compile_function("fdiv", &ops, &config);
1480        assert!(
1481            result.is_err(),
1482            "f32.div must be rejected on Cortex-M3 (no FPU), not panic"
1483        );
1484    }
1485
1486    /// #507: a `br_table` function compiled via the DEFAULT (optimized) config
1487    /// must produce the SAME bytes as the direct (`no_optimize`) selector —
1488    /// i.e. the optimized path declined it to direct, lowering the dispatch as a
1489    /// real cmp-chain instead of silently dropping it (which left all arms in
1490    /// fall-through). Pre-fix the two outputs differed (the optimized one had no
1491    /// selector compare). Execution correctness is gated by
1492    /// `scripts/repro/br_table_507_differential.py`.
1493    #[test]
1494    fn test_507_br_table_declines_to_direct() {
1495        let backend = ArmBackend::new();
1496        // dispatch(sel): br_table over 3 blocks, each storing a marker to mem[0].
1497        let ops = vec![
1498            WasmOp::Block,
1499            WasmOp::Block,
1500            WasmOp::Block,
1501            WasmOp::LocalGet(0),
1502            WasmOp::BrTable {
1503                targets: vec![0, 1, 2],
1504                default: 2,
1505            },
1506            WasmOp::End,
1507            WasmOp::I32Const(0),
1508            WasmOp::I32Const(10),
1509            WasmOp::I32Store {
1510                offset: 0,
1511                align: 2,
1512            },
1513            WasmOp::Return,
1514            WasmOp::End,
1515            WasmOp::I32Const(0),
1516            WasmOp::I32Const(20),
1517            WasmOp::I32Store {
1518                offset: 0,
1519                align: 2,
1520            },
1521            WasmOp::Return,
1522            WasmOp::End,
1523            WasmOp::I32Const(0),
1524            WasmOp::I32Const(30),
1525            WasmOp::I32Store {
1526                offset: 0,
1527                align: 2,
1528            },
1529        ];
1530        let opt = CompileConfig {
1531            target: TargetSpec::cortex_m4(),
1532            ..CompileConfig::default()
1533        };
1534        let direct = CompileConfig {
1535            target: TargetSpec::cortex_m4(),
1536            no_optimize: true,
1537            ..CompileConfig::default()
1538        };
1539        let a = backend
1540            .compile_function("dispatch", &ops, &opt)
1541            .expect("optimized-default must compile br_table (via decline)");
1542        let b = backend
1543            .compile_function("dispatch", &ops, &direct)
1544            .expect("direct must compile br_table");
1545        assert_eq!(
1546            a.code, b.code,
1547            "#507: optimized-default br_table output must be byte-identical to the \
1548             direct selector (i.e. declined to direct), not a dropped dispatch"
1549        );
1550    }
1551
1552    /// Issue #94: end-to-end byte-size check for the canonical u64-packed
1553    /// FFI-return hi32 extract pattern. Compiles two near-identical
1554    /// functions — one with the optimized shift-by-32, one with a generic
1555    /// shift-by-7 — and asserts the optimized form is meaningfully smaller.
1556    #[test]
1557    fn test_issue94_hi32_extract_is_smaller_than_generic_shift() {
1558        let backend = ArmBackend::new();
1559        let config = CompileConfig {
1560            target: TargetSpec::cortex_m4f(),
1561            ..CompileConfig::default()
1562        };
1563
1564        // #518: the i64 value must NOT come from an i64 PARAM — the optimized
1565        // path now declines i64-param functions to the direct selector (it homed
1566        // an i64 param in R4:R5 instead of R0:R1, a silent miscompile this test's
1567        // byte-size-only assertion masked). The canonical #94 case is a u64 from
1568        // an FFI return, not a param, anyway. Source the i64 from a sign-extended
1569        // i32 param (`extend_i32_s`): a runtime, non-constant-foldable i64 that
1570        // stays on the optimized path, so the shift-by-32 hi-extract peephole is
1571        // still exercised on CORRECT code.
1572        // Optimized path: `(i64.extend_i32_s (local.get 0)) >>> 32; wrap_i64`
1573        let ops_hi32 = vec![
1574            WasmOp::LocalGet(0), // i32 param in R0
1575            WasmOp::I64ExtendI32S,
1576            WasmOp::I64Const(32),
1577            WasmOp::I64ShrU,
1578            WasmOp::I32WrapI64,
1579        ];
1580        let func_hi32 = backend
1581            .compile_function("hi32_extract", &ops_hi32, &config)
1582            .unwrap();
1583
1584        // Generic path: `... >>> 7; wrap_i64` — same shape, but the shift amount
1585        // is not a multiple of 32, so it falls through to the runtime shift.
1586        let ops_generic = vec![
1587            WasmOp::LocalGet(0),
1588            WasmOp::I64ExtendI32S,
1589            WasmOp::I64Const(7),
1590            WasmOp::I64ShrU,
1591            WasmOp::I32WrapI64,
1592        ];
1593        let func_generic = backend
1594            .compile_function("generic_shr", &ops_generic, &config)
1595            .unwrap();
1596
1597        let bytes_hi32 = func_hi32.code.len();
1598        let bytes_generic = func_generic.code.len();
1599        println!(
1600            "\n[issue #94] hi32 extract: {} bytes (vs generic shift: {} bytes; saved {})",
1601            bytes_hi32,
1602            bytes_generic,
1603            bytes_generic.saturating_sub(bytes_hi32)
1604        );
1605        let hex: String = func_hi32
1606            .code
1607            .iter()
1608            .map(|b| format!("{:02x}", b))
1609            .collect::<Vec<_>>()
1610            .join(" ");
1611        println!("[issue #94] hi32 bytes: {}", hex);
1612        // We expect the optimized form to be at least 30 bytes smaller than
1613        // the generic 64-bit shift sequence. (Empirically: 14 vs 50 bytes.)
1614        assert!(
1615            bytes_hi32 + 30 <= bytes_generic,
1616            "issue #94: hi32 extract = {} bytes, generic shift = {} bytes; \
1617             expected optimized form to be at least 30 bytes smaller",
1618            bytes_hi32,
1619            bytes_generic,
1620        );
1621    }
1622}