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            // #509: same per-function pattern for the blocktype-arity side-table
84            // (value-carrying-branch lowering).
85            let params = config
86                .func_params_i64
87                .get(func.index as usize)
88                .filter(|p| !p.is_empty());
89            // #457: THIS function's DECLARED param count (imports-first full
90            // index), so the backend can cap the access-pattern inference that
91            // mistook a read-before-write local for a param. `None` when the
92            // driver supplied no arg-count table (hand-built modules).
93            let declared_params = config.func_arg_counts.get(func.index as usize).copied();
94            // GI-FPU-002 (#619/#369): THIS function's declared f32-param mask.
95            let params_f32 = config
96                .func_params_f32
97                .get(func.index as usize)
98                .filter(|p| !p.is_empty());
99            // GI-FPU-002 phase 2 (#369): THIS function's declared f64-param
100            // mask (hard-float targets decline f64 params loudly).
101            let params_f64 = config
102                .func_params_f64
103                .get(func.index as usize)
104                .filter(|p| !p.is_empty());
105            // GI-FPU-002 phase 2 (#719/#369): THIS function's declared f32/f64
106            // return flag, so the epilogue soundness guard fires on every driver
107            // path (not only the CLI loops).
108            let ret_f32 = config
109                .func_ret_f32
110                .get(func.index as usize)
111                .copied()
112                .unwrap_or(false);
113            let ret_f64 = config
114                .func_ret_f64
115                .get(func.index as usize)
116                .copied()
117                .unwrap_or(false);
118            // #1214: THIS function's declared-i64-local side-table. Guarded by
119            // `.any(i64)`, not `!is_empty()` — every function with ANY declared
120            // local (i32 included) has a non-empty vector, and that would
121            // widen this memoization guard to nearly every function for no
122            // reason; only a function that actually declares an i64 local
123            // needs the extra clone.
124            let declared_i64_locals = &func.declared_i64_locals;
125            let func_config = if params.is_some()
126                || params_f32.is_some()
127                || params_f64.is_some()
128                || !func.block_arity.is_empty()
129                || declared_params.is_some()
130                || ret_f32
131                || ret_f64
132                || declared_i64_locals.iter().any(|&b| b)
133            {
134                Some(CompileConfig {
135                    current_func_params_i64: params.cloned().unwrap_or_default(),
136                    current_func_params_f32: params_f32.cloned().unwrap_or_default(),
137                    current_func_params_f64: params_f64.cloned().unwrap_or_default(),
138                    current_func_ret_f32: ret_f32,
139                    current_func_ret_f64: ret_f64,
140                    current_func_block_arity: func.block_arity.clone(),
141                    current_func_declared_i64_locals: declared_i64_locals.clone(),
142                    current_func_param_count: declared_params,
143                    ..config.clone()
144                })
145            } else {
146                None
147            };
148            let cfg = func_config.as_ref().unwrap_or(config);
149            let compiled = self.compile_function(&name, &func.ops, cfg)?;
150            functions.push(compiled);
151        }
152
153        Ok(CompilationResult {
154            functions,
155            elf: None,
156            backend_name: self.name().to_string(),
157        })
158    }
159
160    fn compile_function(
161        &self,
162        name: &str,
163        ops: &[WasmOp],
164        config: &CompileConfig,
165    ) -> Result<CompiledFunction, BackendError> {
166        let (code, relocations, line_map, branch_map, final_instrs) =
167            compile_wasm_to_arm(ops, config).map_err(BackendError::CompilationFailed)?;
168
169        // #778: derive the SOUND static WCET intermediate from the final Thumb-2
170        // stream. Only present for the Thumb-2 path; the core class (from the
171        // triple) decides whether the bound is sound (M3/M4) or declined (M7).
172        // Phase 2: any --wcet-hints entry for THIS function is verified (never
173        // trusted) by the loop analyzer. Phase 3: the intermediate carries the
174        // own-body cycles + direct call sites; the module driver composes it across
175        // the call graph. `wcet` here is the SINGLE-FUNCTION view (unresolved direct
176        // calls decline `call`) — a valid standalone answer, overwritten by the
177        // composed result when the driver runs the second pass.
178        let wcet_intermediate = final_instrs.as_ref().map(|instrs| {
179            let hints = config
180                .wcet_hints
181                .as_ref()
182                .and_then(|h| h.functions.get(name));
183            let self_label = config.current_func_index.map(|i| format!("func_{i}"));
184            crate::wcet::function_wcet_intermediate(
185                name,
186                instrs,
187                &config.target.triple,
188                hints,
189                self_label.as_deref(),
190            )
191        });
192        // The SINGLE-FUNCTION standalone view (unresolved direct calls decline
193        // `call`). Kept as a per-function fallback for any consumer that reads a
194        // lone `CompiledFunction` without running the module composer; the CLI
195        // `--emit-wcet` path IGNORES this and composes `wcet_intermediate` across
196        // the whole call graph instead (its result overwrites the report).
197        // RQ-71-STACKDEPTH (#1341): the native-stack profile, from the SAME
198        // final stream. Unconditional and cheap (one linear walk); the CLI
199        // decides whether to report it. It is NOT gated on the WCET decline —
200        // a data-dependent loop is WCET-unbounded and stack-bounded.
201        let stack_frame = final_instrs
202            .as_ref()
203            .map(|instrs| crate::stack_depth::analyze_function(name, instrs));
204        let wcet = final_instrs.map(|instrs| {
205            let hints = config
206                .wcet_hints
207                .as_ref()
208                .and_then(|h| h.functions.get(name));
209            crate::wcet::function_wcet_with_hints(name, &instrs, &config.target.triple, hints)
210        });
211
212        Ok(CompiledFunction {
213            name: name.to_string(),
214            code,
215            wasm_ops: ops.to_vec(),
216            relocations,
217            line_map,
218            branch_map,
219            wcet,
220            wcet_intermediate,
221            stack_frame,
222        })
223    }
224
225    fn is_available(&self) -> bool {
226        true // Always available — it's a library backend
227    }
228}
229
230/// Count the number of function parameters by analyzing LocalGet patterns
231/// RQ-58-MIRRORS (#242): was a private copy of the read-before-write param
232/// heuristic, byte-equivalent to the other two backends'. Now the ONE shared
233/// definition in `synth-core`, so a correction reaches every backend.
234fn count_params(wasm_ops: &[WasmOp]) -> u32 {
235    synth_core::count_params_heuristic(wasm_ops)
236}
237
238/// #457/#970: the parameter count the selector is given.
239///
240/// With a DECLARED count from the driver the bound is
241/// `min(`[`synth_core::referenced_locals`]`(ops), declared)` — the highest
242/// local index the body touches, clamped by the signature. That is exact in
243/// both directions: the clamp stops a read-before-write NON-PARAM local (which
244/// WASM zero-initializes) from being homed in an argument register and reading
245/// caller garbage (#457), and taking the max over ALL accesses — writes as well
246/// as reads — stops a PARAM from being demoted to a local.
247///
248/// The previous rule capped [`count_params`] (a READ-FIRST heuristic) with the
249/// declared count, which got the second direction wrong: a param written on ONE
250/// arm of an `if` is "written first" in linear op order, so it was demoted, and
251/// because the demoted local's first access is a WRITE the #457 zero-init
252/// skipped it too. The arm that does NOT write it then read an UNINITIALISED
253/// frame slot — measured under unicorn with the sub-SP stack poisoned,
254/// `cond_write_param(0, 42)` returned the poison word, i.e. previous-frame
255/// bytes rather than 42 (#970; the aarch64 instance was #851).
256///
257/// `min` rather than a plain `declared` override preserves the leniency for a
258/// body that only touches the first few of many declared params.
259///
260/// `None` (no declared signature: hand-built op streams, direct
261/// `compile_function` callers) keeps the legacy pure inference — see the
262/// residual documented on [`CompileConfig::current_func_param_count`].
263fn effective_num_params(wasm_ops: &[WasmOp], config: &CompileConfig) -> u32 {
264    match config.current_func_param_count {
265        Some(declared) => synth_core::referenced_locals(wasm_ops).min(declared),
266        None => count_params(wasm_ops),
267    }
268}
269
270/// #539: fold the `i32.const 0; memory.grow m` idiom to `memory.size m`.
271/// Moved to `synth_core::rewrite_memory_grow_zero` (#242, VCR-SEL-005) so the
272/// ARM and RISC-V backends share ONE implementation and cannot drift; re-export
273/// here keeps the existing `rewrite_memory_grow_zero(...)` call sites working.
274use synth_core::rewrite_memory_grow_zero;
275
276/// #509: does the op stream contain a `br`/`br_if`/`br_table` that CARRIES a
277/// value — i.e. one targeting a result-typed block/if (forward edge with
278/// results > 0) or a parameterized loop header (backward edge with loop
279/// params > 0)?
280///
281/// The optimized path's wasm→IR lowering drops the carried value on such
282/// edges (the taken arm returns the fall-through result — same class as the
283/// #507 `br_table` drop, observed on `pick_br`/`pick_br_fall`), so — like
284/// #507 — the shape is detected on the raw op stream and routed to the direct
285/// selector, whose #509 designated-result-register lowering lands the value
286/// correctly. `block_arity` is the decoder's ordinal blocktype-arity
287/// side-table; when it is empty (hand-built op streams) every block reads as
288/// void and this never fires, keeping the optimized path byte-identical for
289/// every existing caller. Frozen-safe for the same reason as #507: the frozen
290/// fixtures compile `--relocatable` (already direct), and no optimized-path
291/// fixture branches to a result-typed block.
292fn has_value_carrying_branch(wasm_ops: &[WasmOp], block_arity: &[(u8, u8)]) -> bool {
293    // Open control constructs: (is_loop, params, results), innermost last.
294    let mut open: Vec<(bool, u8, u8)> = Vec::new();
295    let mut ctrl_ord = 0usize;
296    // A branch edge carries a value when its target is a result-typed forward
297    // join (block/if) or a parameterized loop header.
298    let carries = |open: &[(bool, u8, u8)], depth: u32| -> bool {
299        let Some(&(is_loop, params, results)) = open
300            .len()
301            .checked_sub(1 + depth as usize)
302            .and_then(|i| open.get(i))
303        else {
304            return false; // function-level target — handled by Return lowering
305        };
306        if is_loop { params > 0 } else { results > 0 }
307    };
308    for op in wasm_ops {
309        match op {
310            WasmOp::Block | WasmOp::If => {
311                let (p, r) = block_arity.get(ctrl_ord).copied().unwrap_or((0, 0));
312                ctrl_ord += 1;
313                open.push((false, p, r));
314            }
315            WasmOp::Loop => {
316                let (p, r) = block_arity.get(ctrl_ord).copied().unwrap_or((0, 0));
317                ctrl_ord += 1;
318                open.push((true, p, r));
319            }
320            WasmOp::End => {
321                open.pop(); // None only at the function-level end — harmless
322            }
323            WasmOp::Br(d) | WasmOp::BrIf(d) if carries(&open, *d) => return true,
324            WasmOp::BrTable { targets, default }
325                if targets
326                    .iter()
327                    .chain(std::iter::once(default))
328                    .any(|d| carries(&open, *d)) =>
329            {
330                return true;
331            }
332            _ => {}
333        }
334    }
335    false
336}
337
338/// Core compilation: WASM ops → ARM machine code bytes + relocations
339///
340/// Returns (code_bytes, relocations) where relocations record BL instructions
341/// that target external symbols (e.g., `__meld_dispatch_import` for import calls).
342type CompileArmOutput = (
343    Vec<u8>,
344    Vec<CodeRelocation>,
345    LineMap,
346    synth_core::backend::BranchMap,
347    // #778: the SOUND static WCET result over the final Thumb-2 stream, computed
348    // by `compile_function` (which knows the function name); `None` for the A32
349    // path. Purely additive metadata — does not touch `code`.
350    Option<Vec<synth_synthesis::ArmInstruction>>,
351);
352
353fn compile_wasm_to_arm(
354    wasm_ops: &[WasmOp],
355    config: &CompileConfig,
356) -> Result<CompileArmOutput, String> {
357    // #1093: a PARAMETER-taking block type (`if`/`block`/`loop (param ..)`,
358    // wasm multi-value) declines LOUDLY here — the single choke point BOTH
359    // ARM codegen paths pass through, so neither the optimized route nor the
360    // #197 direct route can reach the selectors' frame-entry stack
361    // checkpoints, which cannot represent params consumed below them (the
362    // `split_off` panic with an `else`; a silently-wrong false-path value
363    // without one — see `find_param_block_type`). This is the aarch64
364    // VCR-A64-CF-001 frame-open refusal ported, NOT multi-value support.
365    // Checked on the driver's ORIGINAL stream, which is what the ordinal
366    // side-table was built against. Empty side-table (hand-built op streams)
367    // ⇒ never fires ⇒ byte-identical for every existing caller.
368    //
369    // RQ-64-MVLOWER (#1093): the guard is relaxed PER CONSTRUCT and PER PATH,
370    // from the one policy in synth-core (`arm_param_block_lowering`), on the
371    // configuration the #1097 oracle executes — `--relocatable`. The
372    // self-contained image path keeps the full decline: no oracle leg
373    // executes its images, so there is no evidence to relax on.
374    let lowered = synth_core::arm_param_block_lowering(config.relocatable);
375    if let Some((what, ord, arity)) = synth_core::find_unlowered_param_block_type(
376        wasm_ops,
377        &config.current_func_block_arity,
378        lowered,
379    ) {
380        return Err(synth_core::param_block_decline_msg(
381            if config.relocatable {
382                "the ARM direct selector (--relocatable)"
383            } else {
384                "the ARM selector (self-contained image path — no #1097 oracle \
385                 leg executes it)"
386            },
387            what,
388            ord,
389            arity,
390        ));
391    }
392    // #539: `memory.grow(0)` must return the CURRENT page count, not the
393    // fixed-memory `-1` sentinel — growing by zero pages can never fail (WASM
394    // Core §4.4.7), so a guest doing `if (memory.grow(0) < 0) trap;` wrongly
395    // faulted. Every lowering path emitted a delta-agnostic `-1`. `memory.grow(0)`
396    // is semantically identical to `memory.size`, which the backend already
397    // computes from the runtime memory-size register (R10 >> 16 = pages), so fold
398    // the `i32.const 0; memory.grow` idiom to `memory.size` up front — backend-
399    // and path-agnostic. A non-zero delta keeps `-1` (fixed memory genuinely
400    // cannot grow); a runtime delta that happens to be 0 is the documented
401    // follow-up.
402    let rewritten = rewrite_memory_grow_zero(wasm_ops);
403    // #494 phase 2b: the fact-spec guard-elision marks are keyed by op index
404    // into the stream the DRIVER handed us. The memory.grow(0) fold above can
405    // only shift indices AT OR AFTER a `memory.grow` — an op the fact-spec
406    // walk never crosses (it stops at the first untracked op, so no mark can
407    // follow one). Defense in depth: if the fold fired at all, drop the marks
408    // loudly rather than risk keying a guard elision to the wrong op.
409    //
410    // VCR-MEM-004 (#901): scry's externally-proven bounds-guard marks ride the
411    // SAME defensive gate for the SAME reason — they are op-index keyed, so an
412    // index shift would strip the guard off the wrong access. They are unioned
413    // with #494's certificate-discharged marks here (one consumption point,
414    // two authorities; `CompileConfig` keeps them separate so the attestation
415    // can say which one covered each site).
416    let (fact_div_zero_elide, fact_div_ovf_elide, mem_bounds_elide): (
417        &[usize],
418        &[usize],
419        Vec<usize>,
420    ) = if rewritten.len() == wasm_ops.len() {
421        let mut mem = config.fact_mem_bounds_elide.clone();
422        mem.extend_from_slice(&config.proven_safe_mem_elide);
423        mem.sort_unstable();
424        mem.dedup();
425        (&config.fact_div_zero_elide, &config.fact_div_ovf_elide, mem)
426    } else {
427        if !config.fact_div_zero_elide.is_empty()
428            || !config.fact_div_ovf_elide.is_empty()
429            || !config.fact_mem_bounds_elide.is_empty()
430        {
431            eprintln!(
432                "fact-spec: DECLINE guard elision marks dropped — the                      memory.grow(0) fold shifted op indices (#494 defensive gate);                      general lowering emitted"
433            );
434        }
435        if !config.proven_safe_mem_elide.is_empty() {
436            eprintln!(
437                "proven-safe: DECLINE {} bounds-guard elision mark(s) dropped — the \
438                 memory.grow(0) fold shifted op indices, so the (func, pc) keys no longer \
439                 name the accesses scry proved (VCR-MEM-004 defensive gate, #901); every \
440                 guard is retained",
441                config.proven_safe_mem_elide.len()
442            );
443        }
444        (&[], &[], Vec::new())
445    };
446    let wasm_ops: &[WasmOp] = &rewritten;
447
448    // #457: `count_params` INFERS the param count from access patterns (a local
449    // whose first access is a read is assumed to be a param), so a
450    // read-before-write NON-PARAM local — which WASM zero-initializes — was
451    // indistinguishable from a param: it got homed in a parameter register and
452    // read caller garbage instead of 0. The driver supplies the DECLARED count
453    // (`current_func_param_count`, from the module's type section) to settle it.
454    //
455    // #970 (RQ-57-CONDPARAM): see [`effective_num_params`] for the bound and
456    // why the read-first inference is not it.
457    let inferred_params = count_params(wasm_ops);
458    let num_params = effective_num_params(wasm_ops, config);
459    // A read-before-write non-param local exists iff the ACCESS-PATTERN
460    // inference overshot the declared count — the read-first rule can only
461    // exceed it via a read-first index >= the declared count, which is exactly
462    // such a local. (Unchanged by #970: `referenced >= inferred` always, so
463    // `num_params < inferred_params` still holds iff `inferred > declared`;
464    // stated directly here rather than left to that algebra.)
465    let has_rbw_local = match config.current_func_param_count {
466        Some(declared) => inferred_params > declared,
467        None => false,
468    };
469
470    let bounds_config = match config.effective_safety_bounds() {
471        SafetyBounds::None => BoundsCheckConfig::None,
472        SafetyBounds::Mpu => BoundsCheckConfig::Mpu,
473        SafetyBounds::Software => BoundsCheckConfig::Software,
474        SafetyBounds::Mask => {
475            // #651 (mirroring the RISC-V backend's compile-time decline):
476            // index masking wraps `ea & (size-1)` — a modulo only when the
477            // linear-memory size is a power of two. With a non-power-of-two
478            // size the AND would silently REMAP in-bounds addresses (e.g.
479            // 0x18000 & 0x2FFFF = 0x8000 for a 192 KiB memory). Decline
480            // loudly rather than miscompile.
481            //
482            // RQ-57-SENTINEL (#953 sibling): `bytes == 0` used to be EXEMPT
483            // from this check, because 0 was read as "unknown — plain
484            // per-function path, no module context". But 0 is also exactly
485            // what a module declaring `(memory 0)` produces, and for that
486            // module the emitted guard (`SUB R12, R10, #1; AND addr, R12`,
487            // R10 = 0 baked by the startup) computes `0 - 1 = 0xFFFFFFFF` —
488            // an IDENTITY mask. Every access then executes unmasked at
489            // `[R11 + addr]` for any 32-bit addr: an unbounded OOB read/write
490            // in the mode whose purpose is bounding. Same sentinel/value
491            // collision as #932/#953, third backend-mode instance.
492            //
493            // 0 now means what #953 made it mean everywhere: a zero-byte
494            // memory. No mask can bound an access into a memory with no bytes
495            // (wasm semantics: every access traps), and wrap-not-trap has
496            // nothing to wrap into — refuse. Callers with no module context
497            // must state the size (the #953 contract; the CLI single-function
498            // path now threads the module's declared size for all backends).
499            let bytes = config.linear_memory_bytes;
500            if bytes == 0 {
501                return Err("--safety-bounds mask: the linear memory has ZERO bytes \
502                     (`(memory 0)`, or a driver that did not state the size) — \
503                     every access is out of bounds and a mask cannot express a \
504                     trap. Use --safety-bounds software (traps every access) \
505                     or declare a non-zero memory (RQ-57-SENTINEL, #953)"
506                    .to_string());
507            }
508            if !bytes.is_power_of_two() {
509                return Err(format!(
510                    "--safety-bounds mask requires a power-of-two linear-memory \
511                     size, got {bytes} bytes — switch to --safety-bounds software \
512                     for the deterministic check (#651)"
513                ));
514            }
515            BoundsCheckConfig::Masking
516        }
517    };
518
519    // The non-optimized (direct) instruction-selection path. Handles f32 via
520    // VFP/FPU. Used directly when `--no-optimize` is set, and as the fallback
521    // when the optimized path declines a module (see issue #120 below).
522    //
523    // VCR-RA-001 step 3b-lite (#242): a FRESH selector per attempt, with
524    // `spill_on_exhaustion` set only on the retry — the first pass is the
525    // unmodified default, so every function that compiles today is selected by
526    // exactly the code that compiled it yesterday (bit-identity is structural,
527    // not behavioural).
528    // #1321: the allocator spill areas the direct selector reserved for the
529    // attempt that actually produced the returned stream, as
530    // `(frame_size, areas)`. Written only on a SUCCESSFUL selection, so after
531    // the recovery ladder it holds the rung that won. Left `None` on the
532    // optimized path, which supplies no layout — and `None` means "police every
533    // offset", i.e. exactly today's behaviour.
534    let selected_spill_areas: std::cell::RefCell<Option<(i32, Vec<std::ops::Range<i32>>)>> =
535        std::cell::RefCell::new(None);
536    // RQ-71-VFPALIAS (#881/#1069): the companion record — the PERMANENT frame
537    // homes of frame-resident f32/f64 locals. Kept beside the spill areas rather
538    // than folded into them because it is a different KIND of fact: the areas say
539    // where the allocator's slots live, this says which offsets inside that same
540    // pool are a wasm local's own home and carry no single-value obligation.
541    let selected_vfp_frame_homes: std::cell::RefCell<Option<(i32, Vec<i32>)>> =
542        std::cell::RefCell::new(None);
543    let select_direct_attempt = |spill_on_exhaustion: bool,
544                                 param_backing_on_exhaustion: bool,
545                                 local_promote: bool,
546                                 i64_spill_slots: Option<usize>,
547                                 vfp_spill_on_exhaustion: bool,
548                                 vfp_frame_home_locals: bool,
549                                 vfp_wide_file: bool|
550     -> Result<Vec<ArmInstruction>, synth_core::Error> {
551        let db = RuleDatabase::with_standard_rules();
552        let mut selector =
553            InstructionSelector::with_bounds_check(db.rules().to_vec(), bounds_config);
554        selector.set_target(config.target.fpu, &config.target.triple);
555        if config.num_imports > 0 {
556            selector.set_num_imports(config.num_imports);
557        }
558        // #195: plumb the callee argument-count tables so the direct selector can
559        // marshal call arguments into R0–R3 per AAPCS.
560        selector.set_func_arg_counts(
561            config.func_arg_counts.clone(),
562            config.type_arg_counts.clone(),
563        );
564        // #197: in relocatable host-link mode, emit direct `func_N` BLs for
565        // imports (rewritten to the wasm field name by build_relocatable_elf)
566        // instead of `__meld_dispatch_import`.
567        selector.set_relocatable(config.relocatable);
568        // #642: call_indirect guard inputs (compile-time table size for the
569        // bounds guard + closed-world type verdicts). Without them, every
570        // call_indirect lowering declines loudly.
571        selector.set_call_indirect_guards(config.call_indirect_guards.clone());
572        // #275: on the self-contained image path (NOT --relocatable) the R11
573        // funcref-table dispatch is a silent miscompile — the region is only
574        // populated by an external runtime, which a self-contained ELF does
575        // not have, so the dispatch would read function pointers from
576        // linear-memory data. Two outcomes:
577        //  - the Thumb-2 `--cortex-m` image path (CLI-flagged: the builder
578        //    that emits and patches the flash-resident funcref table will
579        //    run) lowers call_indirect through that table, PC-relative,
580        //    never via R11;
581        //  - every OTHER self-contained configuration (A32/Cortex-R5, the
582        //    simple-ELF builder, imports present) keeps the loud decline.
583        // The host-linked (--relocatable) path keeps the guarded R11
584        // dispatch: there a runtime places the table region at R11.
585        let self_contained_table = config.self_contained_funcref_table
586            && matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb);
587        selector
588            .set_reject_self_contained_call_indirect(!config.relocatable && !self_contained_table);
589        selector.set_self_contained_funcref_table(self_contained_table);
590        // #237: native-pointer ABI — wasm statics become __synth_wasm_data-relative.
591        selector.set_native_pointer_abi(config.native_pointer_abi, config.linear_memory_bytes);
592        // VCR-MEM-002 phase 1 (#406): per-memory initial page counts — enables
593        // the multi-memory arms (memory-0 lowering never reads it; empty ⇒
594        // every multi-memory op declines loudly).
595        selector.set_memory_pages(config.memory_pages.clone());
596        // #311: i64 call results are register PAIRS — tag them.
597        selector.set_result_types(config.func_ret_i64.clone(), config.type_ret_i64.clone());
598        // #1210: per-function result COUNT (0 = void) — `func_ret_i64` alone
599        // cannot tell a void callee from an i32-returning one.
600        selector.set_func_result_counts(config.func_result_counts.clone());
601        // #359: declared param widths of THIS function, so the AAPCS stack-arg
602        // path can refuse 64-bit params (Ok-or-Err). Empty ⇒ assume i32.
603        selector.set_params_i64(config.current_func_params_i64.clone());
604        // GI-FPU-002 (#619/#369): declared f32-param mask — home hard-float f32
605        // args in S0..S15 (AAPCS-VFP) instead of the R0..R3 integer path.
606        selector.set_params_f32(config.current_func_params_f32.clone());
607        // GI-FPU-002 phase 2 (#369): declared f64-param mask — hard-float
608        // targets decline f64-param functions loudly (no D-register homing yet).
609        selector.set_params_f64(config.current_func_params_f64.clone());
610        // GI-FPU-002 phase 2 (#719/#369): THIS function's f32/f64 return flag, so
611        // the epilogue loudly declines a float result reaching it in a core
612        // register (never a silent integer R0 return where a caller reads S0/D0).
613        selector.set_ret_float(config.current_func_ret_f32, config.current_func_ret_f64);
614        // GI-FPU-002 phase 3 (#369): per-callee float-signature tables. `Call`
615        // marshals the AAPCS-VFP boundary from these (float args into S0../D0..,
616        // float results out of S0/D0); `CallIndirect` still declines a
617        // float-returning static type loudly.
618        selector.set_float_call_signatures(
619            config.func_ret_f32.clone(),
620            config.func_ret_f64.clone(),
621            config.type_ret_f32.clone(),
622            config.type_ret_f64.clone(),
623            config.func_params_f32.clone(),
624            config.func_params_f64.clone(),
625        );
626        // #509: blocktype-arity side-table of THIS function, so value-carrying
627        // br/br_if/br_table land the carried value in the target block's
628        // designated result register instead of dropping it. Empty ⇒ legacy
629        // void-block lowering.
630        selector.set_block_arity(config.current_func_block_arity.clone());
631        // #1214: THIS function's declared-i64-local side-table — see
632        // `compute_local_layout`'s use of it. Empty ⇒ exactly the prior
633        // dataflow-only width inference.
634        selector.set_declared_i64_locals(config.current_func_declared_i64_locals.clone());
635        // Stack-pointer promotion is meaningful only under the native-pointer ABI;
636        // gating here keeps every non-native compile (all frozen fixtures) on the
637        // legacy R9 globals-table path, bit-identical.
638        if config.native_pointer_abi
639            && let Some((sp_idx, sp_init)) = config.stack_pointer_global
640        {
641            selector.set_native_pointer_stack(sp_idx, sp_init);
642        }
643        // #643: per-global slot widths — i64/f64 globals occupy 8-byte slots
644        // (register-pair store/load) and shift every later global's offset.
645        // Empty for i32-only modules ⇒ the legacy `idx * 4` layout, unchanged.
646        selector.set_global_widths(config.global_widths.clone());
647        selector.set_spill_on_exhaustion(spill_on_exhaustion);
648        selector.set_param_backing_on_exhaustion(param_backing_on_exhaustion);
649        // #881 (VCR-RA-004): VFP register-file spilling, set ONLY on the retry
650        // after an attempt failed with a GI-FPU-002 exhaustion Err — functions
651        // that compile without it keep byte-identical output by construction.
652        selector.set_vfp_spill_on_exhaustion(vfp_spill_on_exhaustion);
653        // #1069: LAST-resort residence lever — set ONLY by the final VFP
654        // retry below, after the plain #881 rung also exhausted, so every
655        // function that compiles through base path or plain rung is produced
656        // by exactly yesterday's path (byte-identity is structural).
657        selector.set_vfp_frame_home_locals(vfp_frame_home_locals);
658        // RQ-67-VFPREACH (#1267): the AAPCS callee-saved VFP half.
659        selector.set_vfp_wide_file(vfp_wide_file);
660        // #587 pool-grow rung: a larger i64 spill-slot pool, set ONLY on the
661        // retry after an attempt failed with the slot-pool-exhausted Err —
662        // functions that compile with the default pool keep their frame
663        // byte-identical by construction.
664        if let Some(slots) = i64_spill_slots {
665            selector.set_i64_spill_slots(slots);
666        }
667        // VCR-RA local promotion (#390, #242): keep eligible non-param i32 locals
668        // in callee-saved registers instead of frame slots — the structural lever
669        // toward native parity. DEFAULT-ON as of v0.14.0: gale's G474RE DWT gate
670        // cleared it as a net win (gust_mix dissolved 58→50 cyc/call −14%, all 5
671        // stack spill/reloads eliminated, correctness bit-identical over [0,2047],
672        // 2.00×→1.72× vs LLVM). Escape hatch: `SYNTH_NO_LOCAL_PROMOTE=1` restores
673        // the frame-slot path. Leaf-only / i32-only / ARM-only (see
674        // compute_local_promotion); the leaf-only lift + i64 locals are follow-ons.
675        // #474: `local_promote` is now a per-attempt parameter so the retry ladder
676        // can drop promotion as an exhaustion-recovery rung (promotion pins r4-r8,
677        // which on a dense function leaves the spill allocator with nothing to
678        // free → the frame-slot path is the escape that restores compilability).
679        selector.set_local_promote(local_promote);
680        // #494 phase 2b: certificate-discharged div/rem trap-guard elision
681        // marks (empty in every compile without SYNTH_FACT_SPEC + facts).
682        selector
683            .set_fact_div_guard_elisions(fact_div_zero_elide.to_vec(), fact_div_ovf_elide.to_vec());
684        // #494 bounds-elision + VCR-MEM-004 (#901): per-site memory
685        // bounds-guard marks, unioned above. Empty in every compile without
686        // SYNTH_FACT_SPEC + facts or --proven-safe.
687        selector.set_fact_mem_bounds_elisions(mem_bounds_elide.clone());
688        let selected = selector.select_with_stack(wasm_ops, num_params);
689        if selected.is_ok() {
690            // #1321: only a SUCCESSFUL attempt's layout describes the stream we
691            // return; a failed rung's frame is discarded with it.
692            *selected_spill_areas.borrow_mut() = selector.frame_spill_areas().cloned();
693            *selected_vfp_frame_homes.borrow_mut() = selector.vfp_frame_home_halves().cloned();
694        }
695        selected
696    };
697    let select_direct = || -> Result<Vec<ArmInstruction>, String> {
698        const SINGLE_EXHAUSTION: &str = "all allocatable registers are live on the stack";
699        const PAIR_EXHAUSTION: &str = "no consecutive pair of free registers for i64";
700        const SLOT_EXHAUSTION: &str = "i64 spill-slot pool exhausted";
701        // The full exhaustion-recovery ladder, parameterized on whether local
702        // promotion is enabled. Each rung is reached only when the previous one
703        // returned a recoverable register-exhaustion Err, so a function that
704        // compiles on the first attempt is untouched by the later rungs. Returns
705        // the result AND which rung produced it (for the #242 measurement below).
706        let recovery_ladder =
707            |promote: bool,
708             i64_spill_slots: Option<usize>,
709             vfp_spill: bool,
710             vfp_frame: bool,
711             vfp_wide: bool|
712             -> (Result<Vec<ArmInstruction>, synth_core::Error>, &'static str) {
713                let mut attempt = select_direct_attempt(
714                    false,
715                    false,
716                    promote,
717                    i64_spill_slots,
718                    vfp_spill,
719                    vfp_frame,
720                    vfp_wide,
721                );
722                let mut rung = "base";
723                // VCR-RA-001 step 3b-lite (#242): the i32 register-exhaustion
724                // hard-fail is recoverable — retry with spill-on-exhaustion, which
725                // reserves the spill area and spills the deepest stack value when
726                // the pool is full.
727                if let Err(e) = &attempt
728                    && e.to_string().contains(SINGLE_EXHAUSTION)
729                {
730                    attempt = select_direct_attempt(
731                        true,
732                        false,
733                        promote,
734                        i64_spill_slots,
735                        vfp_spill,
736                        vfp_frame,
737                        vfp_wide,
738                    );
739                    rung = "spill";
740                }
741                // VCR-RA-001 acceptance increment (#242): the i64 consecutive-PAIR
742                // exhaustion is recoverable too — not by stack spilling (the pair
743                // allocator already spills stack values, #171) but by frame-backing
744                // the params (#204) so they stop pinning R0-R3, with spill kept on.
745                if let Err(e) = &attempt
746                    && e.to_string().contains(PAIR_EXHAUSTION)
747                {
748                    attempt = select_direct_attempt(
749                        true,
750                        true,
751                        promote,
752                        i64_spill_slots,
753                        vfp_spill,
754                        vfp_frame,
755                        vfp_wide,
756                    );
757                    rung = "param-backing";
758                }
759                (attempt, rung)
760            };
761        // #474: local promotion (default-on since v0.14.0) is an OPTIMIZATION — it
762        // must never be the reason a function fails to compile. Run the full ladder
763        // with promotion first (so every function that compiles today is
764        // bit-identical), and if it still ends in register exhaustion, fall back to
765        // the promotion-off ladder (the v0.12.0 frame-slot lowering — exactly what
766        // the `SYNTH_NO_LOCAL_PROMOTE=1` workaround does, now automatic). Promotion
767        // pins r4-r8 for the locals; on a dense function that leaves the allocator
768        // with nothing to free, so dropping it restores compilability. The fallback
769        // is reached ONLY by functions that exhaust WITH promotion, so promotion-on
770        // output is untouched by construction (frozen byte gate stays green).
771        let promote = std::env::var("SYNTH_NO_LOCAL_PROMOTE").is_err();
772        // RQ-69-FALCON (#1318): per-rung reporting, because the summary line
773        // below CANNOT distinguish the two failures it is asked about. `rung`
774        // is only assigned when an attempt SUCCEEDS, so a function that climbs
775        // the whole ladder and exhausts every rung prints `rung=base
776        // result=exhausted` — identical to one that never climbed at all. Worse,
777        // the Err the caller finally reports is stage 2's, so a stage-3
778        // (wide-file) failure is invisible even in the user-facing message:
779        // #1318's `step` reports the #1069 slot-pool message while the wide-file
780        // rung it also failed says nothing. Measured on the reporter's module
781        // before this was written; reading the source was the only way to tell
782        // which of the two had happened.
783        //
784        // Logging only, behind the same env var, so emitted bytes are unchanged
785        // and the frozen byte gate is unaffected.
786        let stats = std::env::var("SYNTH_RECOVERY_STATS").is_ok();
787        // Every rung's Err, in attempt order (#1318). The ladder keeps the
788        // EARLIER attempt's Err whenever a later rung also fails — stage 3
789        // falls back to stage 2's `fh`, each pool-grow falls back to its
790        // ungrown attempt — so the reporter of #1318 was told "spill-slot pool
791        // exhausted" while the wall three of the four rungs actually hit was
792        // the VSTR/VLDR [sp,#imm] window. Reporting the LAST rung's Err is no
793        // better: the last rung attempted is the wide-file one, whose Err is
794        // the pool message again. So report what was actually tried, deduped.
795        // Control flow is untouched — every rung still runs exactly when it
796        // ran before, and the substrings other code matches on are all still
797        // present in the joined message.
798        let tried: std::cell::RefCell<Vec<(String, String)>> = std::cell::RefCell::new(Vec::new());
799        let note = |name: &str, r: &Result<Vec<ArmInstruction>, synth_core::Error>| {
800            if let Err(e) = r {
801                let m = e.to_string();
802                let first = m.lines().next().unwrap_or("").to_string();
803                tried.borrow_mut().push((name.to_string(), first));
804            }
805            if stats {
806                match r {
807                    Ok(_) => eprintln!("[recovery-stats]   tried rung={name} -> ok"),
808                    Err(e) => {
809                        let m = e.to_string();
810                        let first = m
811                            .lines()
812                            .next()
813                            .unwrap_or("")
814                            .chars()
815                            .take(140)
816                            .collect::<String>();
817                        eprintln!("[recovery-stats]   tried rung={name} -> exhausted: {first}");
818                    }
819                }
820            }
821        };
822        // The full pre-#587 recovery sequence (promotion-on ladder, then the
823        // #474 promotion-off fallback), parameterized on the pool size so the
824        // pool-grow retry below reruns it verbatim.
825        let full_sequence = |slots: Option<usize>,
826                             vfp_spill: bool,
827                             vfp_frame: bool,
828                             vfp_wide: bool|
829         -> (
830            Result<Vec<ArmInstruction>, synth_core::Error>,
831            &'static str,
832            bool,
833        ) {
834            let (mut attempt, mut rung) =
835                recovery_ladder(promote, slots, vfp_spill, vfp_frame, vfp_wide);
836            let mut promotion_dropped = false;
837            if promote
838                && attempt
839                    .as_ref()
840                    .err()
841                    .is_some_and(|e| e.to_string().contains("register exhaustion"))
842            {
843                let (rescued, off_rung) =
844                    recovery_ladder(false, slots, vfp_spill, vfp_frame, vfp_wide);
845                if rescued.is_ok() {
846                    attempt = rescued;
847                    rung = off_rung;
848                    promotion_dropped = true;
849                }
850            }
851            (attempt, rung, promotion_dropped)
852        };
853        let (mut attempt, mut rung, mut promotion_dropped) =
854            full_sequence(None, false, false, false);
855        // #587 pool-grow retry (the falcon func_60/func_73 remainder): the fixed
856        // 8-slot i64 spill pool can exhaust while spilling is otherwise working —
857        // an i64-dense function simply has more values simultaneously live than
858        // the pool holds. Rerun the ENTIRE sequence (every rung, both promotion
859        // modes) with the pool sized from a conservative operand-stack-depth
860        // bound: the number of simultaneously spilled values can never exceed
861        // the operand-stack depth, plus a few transient slots (the arg-move
862        // cycle resolver and call-result parking each borrow one). The selector
863        // clamps the request to its 12-bit-friendly cap; a function that still
864        // exhausts stays an honest loud skip. Deliberately LAST — after the #474
865        // promotion-off fallback — so any function that compiled yesterday
866        // (through any rung or fallback) is produced by exactly yesterday's
867        // path, byte-identical; the grown pool only ever fires for functions
868        // whose every existing escape ended in the slot-pool Err.
869        if attempt
870            .as_ref()
871            .err()
872            .is_some_and(|e| e.to_string().contains(SLOT_EXHAUSTION))
873        {
874            let depth = synth_core::wasm_stack_check::max_depth_bound(wasm_ops) as usize;
875            let (grown, _, grown_dropped) =
876                full_sequence(Some(depth.saturating_add(4)), false, false, false);
877            note("pool-grow", &grown);
878            if grown.is_ok() {
879                attempt = grown;
880                rung = "pool-grow";
881                promotion_dropped = grown_dropped;
882            }
883        }
884        // #881 (VCR-RA-004): the GI-FPU-002 VFP register-file exhaustion is
885        // recoverable too — retry the ENTIRE sequence with VFP spilling
886        // enabled (the pre-op pressure guard spills the deepest segment-local
887        // f32/f64 stack value into the shared spill area and reloads spilled
888        // operands before their consumers). Deliberately LAST, after every
889        // integer rung, so any function that compiled yesterday is produced
890        // by exactly yesterday's path; the VFP rung only ever fires for
891        // functions whose every existing escape ended in a GI-FPU-002
892        // exhaustion Err (previously an unconditional loud skip). A VFP-
893        // spilling function can in turn exhaust the shared slot pool — the
894        // #587 pool-grow retry composes inside the rung.
895        const VFP_S_EXHAUSTION: &str = "VFP register file exhausted";
896        const VFP_D_EXHAUSTION: &str = "VFP D-register file exhausted";
897        if attempt.as_ref().err().is_some_and(|e| {
898            let msg = e.to_string();
899            msg.contains(VFP_S_EXHAUSTION) || msg.contains(VFP_D_EXHAUSTION)
900        }) {
901            // Stage 1 — the plain #881 rung, exactly yesterday's path
902            // (sequence, pool sizing and all): any function it rescues is
903            // byte-identical to what it shipped yesterday, by construction.
904            let (vfp, vfp_rung, vfp_dropped) = full_sequence(None, true, false, false);
905            note("vfp-spill", &vfp);
906            let (vfp, vfp_rung, vfp_dropped) = if vfp.as_ref().err().is_some_and(|e| {
907                let msg = e.to_string();
908                msg.contains(SLOT_EXHAUSTION) || msg.contains("spilling the VFP register file")
909            }) {
910                let depth = synth_core::wasm_stack_check::max_depth_bound(wasm_ops) as usize;
911                let (grown, grown_rung, grown_dropped) =
912                    full_sequence(Some(depth.saturating_add(4)), true, false, false);
913                note("vfp-spill+pool-grow", &grown);
914                if grown.is_ok() {
915                    (grown, grown_rung, grown_dropped)
916                } else {
917                    (vfp, vfp_rung, vfp_dropped)
918                }
919            } else {
920                (vfp, vfp_rung, vfp_dropped)
921            };
922            if vfp.is_ok() {
923                attempt = vfp;
924                rung = match vfp_rung {
925                    "base" => "vfp-spill",
926                    _ => "vfp-spill+int",
927                };
928                promotion_dropped = vfp_dropped;
929            } else {
930                // Stage 2 (#1069, RQ-60-VFPPRESSURE increment 2) — LAST
931                // resort: the plain rung ALSO failed, i.e. the pressure is
932                // not (only) operand-stack values but PINNED LOCAL HOMES,
933                // which the #881 victim search rightly never touches (a home
934                // lives for the function's extent). Rerun the entire
935                // sequence with frame-homed overflow locals: a fresh
936                // f32/f64 local whose home grant would pin above the S7/D3
937                // cap lives in the frame from its first def. Reached ONLY by
938                // functions that failed every prior escape, so nothing that
939                // compiles today moves a byte.
940                let (fh, fh_rung, fh_dropped) = full_sequence(None, true, true, false);
941                note("vfp-frame-locals", &fh);
942                let (fh, fh_rung, fh_dropped) = if fh.as_ref().err().is_some_and(|e| {
943                    let msg = e.to_string();
944                    // The frame-homed-local slot demand (a PERMANENT slot per
945                    // frame-resident float local) is a third way the shared
946                    // pool exhausts — its trigger substring is the selector's
947                    // own pub const, not a second copy that could drift (the
948                    // #881 substring-is-control-flow lesson, pinned red-first
949                    // by the live24 fixture test).
950                    msg.contains(SLOT_EXHAUSTION)
951                        || msg.contains("spilling the VFP register file")
952                        || msg.contains(
953                            synth_synthesis::instruction_selector::VFP_FRAME_HOME_SLOT_EXHAUSTION,
954                        )
955                }) {
956                    let depth = synth_core::wasm_stack_check::max_depth_bound(wasm_ops) as usize;
957                    // Frame-homed locals hold their slots for the function's
958                    // extent, OUTSIDE the operand-stack depth bound — size
959                    // the grown pool for both. Distinct `local.set`/
960                    // `local.tee` targets over-approximate the frame-homed
961                    // local count; the selector clamps the request to its
962                    // cap, and a function that still exhausts stays an
963                    // honest loud skip.
964                    let local_targets: std::collections::HashSet<u32> = wasm_ops
965                        .iter()
966                        .filter_map(|op| match op {
967                            synth_synthesis::WasmOp::LocalSet(i)
968                            | synth_synthesis::WasmOp::LocalTee(i) => Some(*i),
969                            _ => None,
970                        })
971                        .collect();
972                    let (grown, grown_rung, grown_dropped) = full_sequence(
973                        Some(depth.saturating_add(local_targets.len()).saturating_add(4)),
974                        true,
975                        true,
976                        false,
977                    );
978                    note(
979                        &format!(
980                            "vfp-frame-locals+pool-grow({})",
981                            depth.saturating_add(local_targets.len()).saturating_add(4)
982                        ),
983                        &grown,
984                    );
985                    if grown.is_ok() {
986                        (grown, grown_rung, grown_dropped)
987                    } else {
988                        (fh, fh_rung, fh_dropped)
989                    }
990                } else {
991                    (fh, fh_rung, fh_dropped)
992                };
993                if fh.is_ok() {
994                    attempt = fh;
995                    rung = match fh_rung {
996                        "base" => "vfp-frame-locals",
997                        _ => "vfp-frame-locals+int",
998                    };
999                    promotion_dropped = fh_dropped;
1000                } else {
1001                    // Stage 3 (#1267, RQ-67-VFPREACH) — THE LAST RESORT, and the
1002                    // reach half of #1204's root shape. Every escape above has
1003                    // now ended in GI-FPU-002, so this function DECLINES today.
1004                    //
1005                    // It declines against HALF the register file: synth
1006                    // allocates S0-S15/D0-D7 because S16-S31/D8-D15 are AAPCS
1007                    // CALLEE-saved and, until this release, nothing could save
1008                    // them — VPUSH/VPOP did not exist in the encoder. Every FPU
1009                    // target synth supports actually has D0-D15 = S0-S31.
1010                    //
1011                    // Reached ONLY here, so a function that compiles today is
1012                    // produced by exactly today's path and cannot move a byte —
1013                    // the #881 rung discipline, measured over the corpus rather
1014                    // than asserted. The selector emits the VPUSH/VPOP pair and
1015                    // REFUSES functions with stack-passed params (#1273), so a
1016                    // failure here stays the honest loud decline it already was.
1017                    let (wide, wide_rung, wide_dropped) = full_sequence(None, true, true, true);
1018                    note("vfp-wide-file", &wide);
1019                    if wide.is_ok() {
1020                        attempt = wide;
1021                        rung = match wide_rung {
1022                            "base" => "vfp-wide-file",
1023                            _ => "vfp-wide-file+int",
1024                        };
1025                        promotion_dropped = wide_dropped;
1026                    } else {
1027                        attempt = fh;
1028                        promotion_dropped = fh_dropped;
1029                    }
1030                }
1031            }
1032        }
1033        // VCR-RA measurement (#242): log which recovery rung produced the result,
1034        // so the per-rung distribution across a corpus can be measured — the size
1035        // of the failure surface a verified allocator must subsume (see
1036        // scripts/repro/register_exhaustion_recovery_ladder.md). Logging only:
1037        // emitted bytes are unchanged, so the frozen byte gate is unaffected.
1038        if stats {
1039            // `rung` holds the last SUCCESSFUL rung; on total exhaustion it is
1040            // still "base", which said nothing about how far the ladder climbed
1041            // (#1318). Report it as `none` and let the per-rung lines above
1042            // carry the detail.
1043            let rung = if attempt.is_ok() { rung } else { "none" };
1044            eprintln!(
1045                "[recovery-stats] rung={rung}{} result={}",
1046                if promotion_dropped {
1047                    " promotion-off"
1048                } else {
1049                    ""
1050                },
1051                if attempt.is_ok() { "ok" } else { "exhausted" },
1052            );
1053        }
1054        attempt.map_err(|e| {
1055            // The kept Err first (so every substring any caller or test matches
1056            // on is exactly where it was), then the rungs that were tried and
1057            // what each one hit. Deduped by message: three rungs reporting one
1058            // wall should say so once, and name the three (#1318).
1059            let base = format!("instruction selection failed: {}", e);
1060            let rungs = tried.borrow();
1061            if rungs.len() < 2 {
1062                return base;
1063            }
1064            let mut by_msg: Vec<(String, Vec<String>)> = Vec::new();
1065            for (rung, msg) in rungs.iter() {
1066                match by_msg.iter_mut().find(|(m, _)| m == msg) {
1067                    Some((_, names)) => names.push(rung.clone()),
1068                    None => by_msg.push((msg.clone(), vec![rung.clone()])),
1069                }
1070            }
1071            let detail = by_msg
1072                .iter()
1073                .map(|(msg, names)| format!("{} -> {}", names.join(" + "), msg))
1074                .collect::<Vec<_>>()
1075                .join(" | ");
1076            format!("{base} [every recovery rung failed: {detail}]")
1077        })
1078    };
1079
1080    // Instruction selection: optimized or direct.
1081    //
1082    // #197: `--relocatable` (host-link ET_REL) forces the direct selector. The
1083    // optimized path materializes an absolute linmem base (0x20000100) and does
1084    // not preserve caller-saved registers across calls — both wrong for a
1085    // host-linked object, where the linmem base arrives via `fp` at runtime and
1086    // callees follow AAPCS. `select_with_stack` (now i64-spill capable after
1087    // #171) handles fp-relative memory + caller-saved preservation correctly.
1088    //
1089    // #507: `br_table` is DROPPED during the optimized path's wasm→IR lowering
1090    // (`optimize_full`), so `ir_to_arm` never sees the dispatch — it emits the
1091    // arm bodies in fall-through sequence with no `cmp`/branch on the selector, a
1092    // SILENT miscompile (every input hits the last arm). The selector value isn't
1093    // even loaded. Because the drop happens before `ir_to_arm`, there's no `Err`
1094    // to fall back on; detect it on the raw wasm op stream here and force the
1095    // direct selector (`select_with_stack` lowers `br_table` correctly as a
1096    // cmp-chain — confirmed on the `--relocatable` path). Same honest-degradation
1097    // contract as the issue-#120 f32 decline: the function still compiles
1098    // correctly, just without IR-level optimization. Frozen-safe: the frozen
1099    // fixtures compile `--relocatable` (already direct), and no optimized-path
1100    // fixture (control_step, flight_algo) contains `br_table`.
1101    let has_br_table = wasm_ops
1102        .iter()
1103        .any(|op| matches!(op, WasmOp::BrTable { .. }));
1104    // #509: the optimized path also drops the value carried by a `br`/`br_if`
1105    // to a result-typed block (the taken edge returns the wrong arm's value —
1106    // same silent-miscompile class as the #507 br_table drop). Route the shape
1107    // to the direct selector, whose designated-result-register lowering (#509)
1108    // lands the carried value at the join. Never fires for void-block control
1109    // flow (all frozen/optimized fixtures), so those stay byte-identical.
1110    let has_value_carry = has_value_carrying_branch(wasm_ops, &config.current_func_block_arity);
1111    // #503-i64/#518: route any signature with a 64-bit (i64/f64) param to the
1112    // direct selector. The optimized path's param homing is width-naive — its
1113    // #518 decline covers only functions that READ an i64 param (an `I64Load`
1114    // from a param index), so a function that reads an i32 param whose AAPCS
1115    // home a preceding wide param SHIFTED (e.g. p1 of `(i64 i32)` lives in R2,
1116    // not R1; p3 of `(i64 i32 i32 i32)` lives on the stack, not in R3) was
1117    // silently miscompiled rather than falling back. The direct selector's
1118    // `aapcs_param_layout` homing handles every such shape (i64-param READS
1119    // already fell back to it via the ir_to_arm Err, so those functions emit
1120    // the same bytes as before). `num_params` counts read-first locals, so a
1121    // function that never touches any param keeps the optimized path.
1122    let has_wide_param = config
1123        .current_func_params_i64
1124        .iter()
1125        .take(num_params as usize)
1126        .any(|&w| w);
1127    // #782(b): a HARD-float (FPU) target passes f32 args in VFP S-registers
1128    // and returns floats in S0/D0 (AAPCS-VFP) — but the optimized path's
1129    // param/return homing is float-naive (integer R0..R3 args, R0 return). A
1130    // function whose ops ALL lower on the optimized path but whose SIGNATURE
1131    // carries a float — e.g. the pure value-pick
1132    // `(param f32 f32 i32) (result f32) select`, no float OP to trip the
1133    // issue-#120 ir_to_arm fallback — was silently compiled with the integer
1134    // ABI: callers marshal S0/S1, the body reads R0/R1. Route every
1135    // float-signature function to the direct selector (AAPCS-VFP homing, or
1136    // an honest decline). Soft-float targets (no FPU) keep the optimized
1137    // path: the integer treatment IS the ABI there — byte-identical. (f64
1138    // params already route direct via `has_wide_param`; this adds f32 params
1139    // and f32/f64 returns.)
1140    let has_float_sig = config.target.fpu.is_some()
1141        && (config.current_func_ret_f32
1142            || config.current_func_ret_f64
1143            || config
1144                .current_func_params_f32
1145                .iter()
1146                .take(num_params as usize)
1147                .any(|&f| f)
1148            || config
1149                .current_func_params_f64
1150                .iter()
1151                .take(num_params as usize)
1152                .any(|&f| f));
1153    // #494 phase 2b: div/rem guard-elision marks are consumed by the DIRECT
1154    // selector only — the optimized path's IR passes (const-fold/CSE/DCE)
1155    // renumber instructions, so an op-index-keyed mark cannot soundly survive
1156    // them. Route marked functions direct (the #507/#509 honest-degradation
1157    // pattern). Never fires without SYNTH_FACT_SPEC + facts + a discharged
1158    // obligation, so every existing compile keeps its path byte-identical.
1159    let has_fact_div_elide = !fact_div_zero_elide.is_empty()
1160        || !fact_div_ovf_elide.is_empty()
1161        // #494 bounds-elision + VCR-MEM-004 (#901): memory bounds-guard marks
1162        // are direct-selector keyed for the same reason (IR passes renumber
1163        // instructions). This is ALSO why the optimized path's
1164        // `push_software_bounds_guard` sites never need mark plumbing: a
1165        // marked function is routed away from that path entirely.
1166        || !mem_bounds_elide.is_empty();
1167    // #643: the optimized path's global lowering is width-naive — `GlobalGet`/
1168    // `GlobalSet` are single-word `[R9, idx*4]` accesses, which (a) silently
1169    // dropped the high word of every i64 global and (b) mis-address every
1170    // global whose offset an earlier wide (i64/f64) slot shifted. When the
1171    // module has any wide global, route every global-touching function to the
1172    // direct selector, whose type-aware summed layout pairs the access (or
1173    // declines loudly). Modules with only 4-byte globals — every existing
1174    // fixture — keep the optimized path byte-identical.
1175    let has_wide_global_module = config.global_widths.iter().any(|&w| w > 4);
1176    let has_global_access = has_wide_global_module
1177        && wasm_ops
1178            .iter()
1179            .any(|op| matches!(op, WasmOp::GlobalGet(_) | WasmOp::GlobalSet(_)));
1180    // VCR-VER-001 (#242): `post_exhaust` scopes the post-exhaustion cleanup
1181    // extensions to functions whose bytes the #580 spill-on-exhaustion
1182    // machinery actually shaped (bridge-reported). Everything else — the
1183    // direct path, non-exhausted optimized functions — stays byte-identical
1184    // flag-on (the `vcr_ver_001_gate_242` lock's contract).
1185    let (arm_instrs, post_exhaust) = if config.no_optimize
1186        || config.relocatable
1187        || has_br_table
1188        || has_value_carry
1189        || has_wide_param
1190        || has_float_sig
1191        || has_global_access
1192        || has_fact_div_elide
1193        // #457: route read-before-write non-param locals to the direct
1194        // selector, whose prologue zero-init lands the wasm-mandated 0.
1195        || has_rbw_local
1196    {
1197        if std::env::var("SYNTH_PATH_DEBUG").is_ok() {
1198            // RQ-65-PARITY (#197): name WHICH pre-gate predicate diverted the
1199            // function and which function it was, so the selector-parity
1200            // oracle can record every "only one path accepted this" case with
1201            // its reason instead of a bare count. The leading text is kept
1202            // verbatim — the #377/#1021 harnesses match on it.
1203            let reasons: Vec<&str> = [
1204                (config.no_optimize, "no-optimize"),
1205                (config.relocatable, "relocatable"),
1206                (has_br_table, "br-table"),
1207                (has_value_carry, "value-carry"),
1208                (has_wide_param, "wide-param"),
1209                (has_float_sig, "float-sig"),
1210                (has_global_access, "wide-global-access"),
1211                (has_fact_div_elide, "fact-elide"),
1212                (has_rbw_local, "rbw-local"),
1213            ]
1214            .iter()
1215            .filter(|(on, _)| *on)
1216            .map(|(_, name)| *name)
1217            .collect();
1218            eprintln!(
1219                "[path-debug] direct (pre-gate) func={} reasons={}",
1220                config
1221                    .current_func_index
1222                    .map_or("?".to_string(), |i| i.to_string()),
1223                reasons.join(",")
1224            );
1225        }
1226        (select_direct()?, false)
1227    } else {
1228        let opt_config = if config.loom_compat {
1229            OptimizationConfig::loom_compat()
1230        } else {
1231            OptimizationConfig::all()
1232        };
1233
1234        let mut bridge = OptimizerBridge::with_config(opt_config);
1235        // #188: tell the bridge how many imports there are so it declines only
1236        // LOCAL calls (and leaves import calls on the optimized path, keeping
1237        // the #173 field-name relocation rewrite intact).
1238        bridge.set_num_imports(config.num_imports);
1239        // #543 Phase 2: thread the integrator-marked volatile DMA-window ranges
1240        // (`--volatile-segment <base>:<len>`) to the bridge's address-caching
1241        // levers — base-CSE (#468) excludes any access inside a marked range
1242        // from its fold set, and the bridge-level const-CSE declines wholesale
1243        // while any range is marked. Empty (the default) ⇒ byte-identical.
1244        bridge.set_volatile_segments(config.volatile_segments.clone());
1245        // #377: thread `--safety-bounds` to the bridge. Pre-fix the optimized
1246        // path ignored it — `software`/`mask` were SILENT NO-OPS on the path
1247        // that lowers the bulk of a flight loop's i32 loads/stores (byte-
1248        // identical to `none`, while the safety manifest claimed otherwise).
1249        // `Software` now emits the inline guard per access; `Masking` declines
1250        // memory-accessing functions to the direct selector; `None`/`Mpu` are
1251        // byte-identical to before.
1252        bridge.set_bounds_check(bounds_config);
1253        // #687: thread the absolute linear-memory base the optimized path
1254        // materializes. Defaults to 0x2000_0100 (byte-identical);
1255        // `--stack-layout=low` shifts it up by the reserved stack size so
1256        // const-address accesses follow the moved linear memory.
1257        bridge.set_linmem_base(config.linmem_base);
1258        // `ir_to_arm` now returns `Result` — an `Err` means the optimized path
1259        // hit an unmapped vreg (issue-#93-class). Treat it identically to an
1260        // `optimize_full` failure: fall back to the direct selector rather
1261        // than propagating, so the function still compiles correctly.
1262        match bridge
1263            .optimize_full(wasm_ops)
1264            .and_then(|(opt_ir, _cfg, _stats)| bridge.ir_to_arm(&opt_ir, num_params as usize))
1265        {
1266            Ok(arm_ops) => {
1267                if std::env::var("SYNTH_PATH_DEBUG").is_ok() {
1268                    eprintln!(
1269                        "[path-debug] optimized (ir_to_arm ok) func={}",
1270                        config
1271                            .current_func_index
1272                            .map_or("?".to_string(), |i| i.to_string())
1273                    );
1274                }
1275                (
1276                    arm_ops
1277                        .into_iter()
1278                        .map(|op| ArmInstruction {
1279                            op,
1280                            source_line: None,
1281                        })
1282                        .collect(),
1283                    bridge.spill_on_exhaust_fired(),
1284                )
1285            }
1286            // Issue #120: the optimized path declines modules it cannot lower
1287            // (notably scalar f32/f64 ops — the IR has no float opcodes). Fall
1288            // back to the direct instruction selector, which handles f32 via
1289            // VFP/FPU. This is honest degradation: the function still compiles
1290            // correctly, just without IR-level optimization.
1291            Err(e) => {
1292                if std::env::var("SYNTH_PATH_DEBUG").is_ok() {
1293                    eprintln!(
1294                        "[path-debug] direct (fallback: {e}) func={}",
1295                        config
1296                            .current_func_index
1297                            .map_or("?".to_string(), |i| i.to_string())
1298                    );
1299                }
1300                (select_direct()?, false)
1301            }
1302        }
1303    };
1304
1305    // #257/#277: `mul`+`add`→`mla` fusion is intentionally NOT wired here.
1306    // The transform is correct and ready (`synth_synthesis::liveness::fuse_mul_add`,
1307    // fully tested), but it is **register-allocation-coupled**: over the current
1308    // greedy single-pass selector, folding `mul rM,..; add rD,rM,rX` → `mla`
1309    // extends the live ranges of the mul inputs to the mla point, and the added
1310    // pressure (extra moves/spills) costs more than the single-cycle MLA saves —
1311    // gale measured a +2 cyc on-target REGRESSION (flat_flight 255→257, G474RE)
1312    // even though it removes 2 instructions and the seam stays 0x07FDF307. So the
1313    // fusion stays unwired until the spill-aware allocator (VCR-RA-001) chooses
1314    // registers, at which point it becomes net-positive (per #272's plan and the
1315    // wiring design note). Lesson (#277): a register-pressure-affecting transform
1316    // needs an on-target/allocator-aware gate, not a byte-count gate, before it
1317    // can default on.
1318
1319    // VCR-RA-001 const-CSE / rematerialization-avoidance (#209): moved to run
1320    // LAST, after the immediate-folds — see the apply_const_cse call below
1321    // (#242). Earlier it ran here (before range-realloc and the folds), which is
1322    // what let it grow gale's --relocatable `gust_mix` 90→92 B (#242 burndown,
1323    // 2026-06-26): retargeting a read defeated a *downstream* immediate-fold that
1324    // would otherwise have absorbed the constant. Running CSE-last makes those
1325    // foldable consts already-folded-and-gone, so CSE only ever touches genuinely
1326    // redundant materializations.
1327
1328    // VCR-RA-001 RANGE RE-ALLOCATION (#209/#242, wiring step 3a) — the first
1329    // CONSEQUENTIAL allocator pass: re-colour each maximal straight-line
1330    // segment over the R0-R8 pool with value ranges as the allocation unit
1331    // (segment inputs + per-register live-outs pinned to their original
1332    // registers, reserved R9-R12/SP identity-assigned — each segment is
1333    // independently sound, no cross-segment liveness assumed). Renames
1334    // registers only: never adds, removes, or reorders instructions, so
1335    // labels/branch offsets are unaffected.
1336    //
1337    // DEFAULT-ON since v0.11.36: gale cleared the gate on-target (G474RE,
1338    // #209 2026-06-10) — flag-on output byte-identical to flag-off on
1339    // flat_flight/controller/control_step, fires on the filter family with
1340    // zero cycle delta and a small size win, all selfchecks green on silicon.
1341    // Opt out with `SYNTH_RANGE_REALLOC=0`; per-function stats with
1342    // `SYNTH_REALLOC_STATS=1`.
1343    //
1344    // The companion dead callee-saved-save elimination (gale's "next
1345    // consequential lever", same issue comment) then shrinks the prologue
1346    // `push {r4-r8,lr}` / epilogue `pop {r4-r8,pc}` to the callee-saved
1347    // registers the re-allocated body still touches (leaf-only,
1348    // SP-untouched, even-count-padded — see shrink_callee_saved_saves):
1349    // ~12 cycles of pure save/restore overhead removed on small leaves.
1350    let realloc_on = std::env::var("SYNTH_RANGE_REALLOC").map_or(true, |v| v != "0");
1351    let (arm_instrs, ran_realloc) = if realloc_on {
1352        use synth_synthesis::rules::Reg;
1353        const POOL: [Reg; 9] = synth_synthesis::reg_contract::ALLOCATABLE;
1354        // VCR-DEC-001 (epic #242, the North Star's first foothold): the
1355        // SYNTH_GRAPH_ALLOC graph-colouring allocator SPIKE. When enabled it
1356        // replaces STEP 1 of the re-allocation (the segment-based
1357        // `reallocate_function`) with a whole-function Chaitin/Briggs colouring
1358        // (`graph_alloc::reallocate`) built against the SAME acceptance oracle
1359        // (`validate_segment_rewrite` trace-equality); the later dead-frame /
1360        // callee-saved-prologue / shrink passes still run on its output, so a
1361        // value it homes in R4-R8 still gets its callee-saved push (the
1362        // invariant the unconditional VCR-RA-003 validator guards). It is
1363        // BOUNDED to whole straight-line functions and DECLINES (returns None)
1364        // to the shipping `reallocate_function` on any control flow, spill, or
1365        // unmodeled op — never a hard-fail. Flag-OFF (`SYNTH_GRAPH_ALLOC` unset)
1366        // never enters this branch, so the shipping bytes are byte-identical
1367        // (the GOLDEN trick — frozen fixtures unchanged). NO default flip: the
1368        // spike ships flag-off; the flip is a later, evidence-gated step.
1369        //
1370        // VCR-VER-001 (#242): on a function the spill-on-exhaustion machinery
1371        // shaped, the terminal segment gets relaxed live-out pinning (only
1372        // R0/R1 are observable past `bx lr` at this pre-prologue position) so
1373        // the colourer can lower R4-R8-homed tails into caller-saved R0-R3 —
1374        // shrinking the `push {r4-r8,lr}` the #580 exhaustion shapes pay for.
1375        // `post_exhaust == false` selects the shipping pass bit for bit.
1376        let (out, stats) = if synth_synthesis::graph_alloc::enabled() {
1377            // RQ-60-RACOST increment 2 (#242): the colourer prices every
1378            // colour choice in REAL-ENCODER bytes. The sizer is the same
1379            // encoder family the emit loop below constructs — Thumb-2 (with
1380            // the target's FPU) or A32 — asked per candidate instruction, so
1381            // there is no hand size table to drift (#936). On the fixed-width
1382            // A32 ISA every candidate ties and the cost model degenerates to
1383            // the identity hint (zero churn).
1384            let sizing_encoder =
1385                if matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb) {
1386                    ArmEncoder::new_thumb2_with_fpu(config.target.fpu)
1387                } else {
1388                    ArmEncoder::new_arm32()
1389                };
1390            let enc = |op: &synth_synthesis::rules::ArmOp| {
1391                sizing_encoder.encode(op).ok().map(|b| b.len())
1392            };
1393            let stats_on = std::env::var("SYNTH_GRAPH_ALLOC_STATS").is_ok();
1394            match synth_synthesis::graph_alloc::reallocate(&arm_instrs, &POOL, &enc) {
1395                // SYNTH_GRAPH_ALLOC_FORCE (test seam, RQ-60-RACOST
1396                // increment 2): ship every validated candidate WITHOUT the
1397                // final-byte arbiter — the pre-arbiter behaviour. Used by
1398                // `vcr_dec_001_join_alloc_execution_differential.py` so the
1399                // unicorn-vs-wasmtime oracle executes the colourer's
1400                // proposals on EVERY reachable shape (call, i64-pair,
1401                // shift-expansion), not only the ones the arbiter lets ship —
1402                // an arbiter-declined candidate is still a candidate a future
1403                // change could promote, and the execution teeth must stay
1404                // ahead of that. Never set in production; the arbiter is the
1405                // shipping behaviour.
1406                Some(candidate)
1407                    if std::env::var("SYNTH_GRAPH_ALLOC_FORCE").is_ok_and(|v| v != "0") =>
1408                {
1409                    if stats_on {
1410                        eprintln!(
1411                            "[graph-alloc] whole-function colouring APPLIED \
1412                             (validated; FORCED — arbiter bypassed)"
1413                        );
1414                    }
1415                    (
1416                        candidate,
1417                        synth_synthesis::liveness::ReallocStats::default(),
1418                    )
1419                }
1420                Some(candidate) => {
1421                    // FINAL-BYTE ARBITER (RQ-60-RACOST increment 2). A
1422                    // colour-time cost model — however faithful its byte sizes
1423                    // — cannot price DOWNSTREAM PASS INTERACTIONS: measured on
1424                    // const_cse.wat::spill12, an identity-shaped colouring
1425                    // that merely PRESERVED the greedy allocator's register
1426                    // rotation defeated const-CSE's canonicalization and grew
1427                    // the function 148 -> 244 B (+96), with not one occurrence
1428                    // priced differently at colour time. So the candidate is
1429                    // sized through the REAL downstream pipeline
1430                    // (`finish_allocated_stream` — the exact passes the
1431                    // shipped stream runs, not a mirror) plus label resolution
1432                    // and the REAL encoder, against the shipping allocator's
1433                    // stream sized identically, and it ships only when it is
1434                    // STRICTLY smaller. A tie or a refusal keeps the shipping
1435                    // bytes — growth on an applied function is structurally
1436                    // impossible, which is exactly the wired
1437                    // vcr_dec_001_graph_alloc_differential no-growth
1438                    // assertion, promoted from a gate into a construction.
1439                    let ship = synth_synthesis::liveness::reallocate_function_post_exhaust(
1440                        &arm_instrs,
1441                        &POOL,
1442                        post_exhaust,
1443                    );
1444                    let size_of = |stream: &[synth_synthesis::ArmInstruction]| -> Option<usize> {
1445                        let finished = match finish_allocated_stream(
1446                            stream.to_vec(),
1447                            config,
1448                            post_exhaust,
1449                            true,
1450                            selected_spill_areas.borrow().as_ref(),
1451                            selected_vfp_frame_homes.borrow().as_ref(),
1452                        ) {
1453                            Ok(f) => f,
1454                            Err(e) => {
1455                                if stats_on {
1456                                    // The differential greps for the RA-003
1457                                    // hard-error string to detect a SHIPPED
1458                                    // violation; a candidate refused during
1459                                    // sizing is a decline, not a shipped
1460                                    // violation, so that marker is rewritten.
1461                                    let e = e.replace(
1462                                        "register-allocation validation FAILED",
1463                                        "register-allocation validation refused the candidate",
1464                                    );
1465                                    eprintln!(
1466                                        "[graph-alloc] arbiter: stream refused by the \
1467                                         pipeline/validators: {e}"
1468                                    );
1469                                }
1470                                return None;
1471                            }
1472                        };
1473                        let finished = if matches!(
1474                            config.target.isa,
1475                            IsaVariant::Thumb2 | IsaVariant::Thumb
1476                        ) {
1477                            resolve_label_branches(finished, &sizing_encoder).ok()?
1478                        } else {
1479                            finished
1480                        };
1481                        let mut total = 0usize;
1482                        let mut literals = 0usize;
1483                        for ins in &finished {
1484                            total += sizing_encoder.encode(&ins.op).ok()?.len();
1485                            if matches!(ins.op, synth_synthesis::rules::ArmOp::LdrSym { .. }) {
1486                                literals += 1;
1487                            }
1488                        }
1489                        if literals > 0 {
1490                            // The emit loop 4-aligns the literal pool and
1491                            // appends one word per LdrSym (no dedup — each
1492                            // site carries its own addend).
1493                            total += (4 - total % 4) % 4 + 4 * literals;
1494                        }
1495                        Some(total)
1496                    };
1497                    match (size_of(&candidate), size_of(&ship.0)) {
1498                        (Some(cand), Some(base)) if cand < base => {
1499                            if stats_on {
1500                                eprintln!(
1501                                    "[graph-alloc] whole-function colouring APPLIED \
1502                                     (validated; arbiter: {cand} B < shipping {base} B)"
1503                                );
1504                            }
1505                            (
1506                                candidate,
1507                                synth_synthesis::liveness::ReallocStats::default(),
1508                            )
1509                        }
1510                        (cand, base) => {
1511                            if stats_on {
1512                                eprintln!(
1513                                    "[graph-alloc] arbiter kept shipping bytes \
1514                                     (candidate {cand:?} B vs shipping {base:?} B) → \
1515                                     shipping reallocate_function"
1516                                );
1517                            }
1518                            ship
1519                        }
1520                    }
1521                }
1522                None => {
1523                    if stats_on {
1524                        eprintln!("[graph-alloc] DECLINED → shipping reallocate_function");
1525                    }
1526                    synth_synthesis::liveness::reallocate_function_post_exhaust(
1527                        &arm_instrs,
1528                        &POOL,
1529                        post_exhaust,
1530                    )
1531                }
1532            }
1533        } else {
1534            synth_synthesis::liveness::reallocate_function_post_exhaust(
1535                &arm_instrs,
1536                &POOL,
1537                post_exhaust,
1538            )
1539        };
1540        if std::env::var("SYNTH_REALLOC_STATS").is_ok() {
1541            eprintln!(
1542                "[range-realloc] {} segments: {} reallocated, {} declined ({} validator-rejected), {} need spill (step 4)",
1543                stats.segments,
1544                stats.reallocated,
1545                stats.declined,
1546                stats.validator_rejects,
1547                stats.needs_spill
1548            );
1549        }
1550        // VCR-VER-004 AUDIT (#242) — report-only, opt-in, never gating.
1551        //
1552        // The ABI observable-contract validator is a GATE on the flag-off
1553        // graph-colouring spike. This hook asks the same question of the
1554        // SHIPPING allocator's rewrite, so the answer is a MEASUREMENT rather
1555        // than a claim: how much of the shipping path can a value-level,
1556        // ABI-anchored check actually see today? Report-only DELIBERATELY —
1557        // making it gate here would risk a false rejection on the default
1558        // path, and the honest sequence is measure first, flip on evidence.
1559        // `SYNTH_ABI_CONTRACT_AUDIT=1` prints one verdict per function.
1560        if std::env::var_os("SYNTH_ABI_CONTRACT_AUDIT").is_some() {
1561            eprintln!(
1562                "[abi-contract-audit] {:?}",
1563                synth_synthesis::abi_contract::validate_abi_contract(&arm_instrs, &out)
1564            );
1565        }
1566        (out, true)
1567    } else {
1568        (arm_instrs, false)
1569    };
1570    // RQ-60-RACOST increment 2 (#242): every rewrite pass between the chosen
1571    // allocation and the encoder now lives in `finish_allocated_stream`, so
1572    // the SYNTH_GRAPH_ALLOC final-byte arbiter can size a candidate through
1573    // the REAL pipeline. Flag-off this is the exact pre-extraction sequence,
1574    // called once, in the same place.
1575    let arm_instrs = finish_allocated_stream(
1576        arm_instrs,
1577        config,
1578        post_exhaust,
1579        ran_realloc,
1580        selected_spill_areas.borrow().as_ref(),
1581        selected_vfp_frame_homes.borrow().as_ref(),
1582    )?;
1583
1584    // Encode to binary — use Thumb-2 for Cortex-M targets
1585    let use_thumb2 = matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb);
1586
1587    let encoder = if use_thumb2 {
1588        ArmEncoder::new_thumb2_with_fpu(config.target.fpu)
1589    } else {
1590        ArmEncoder::new_arm32()
1591    };
1592
1593    // #202: resolve local label branches (Bcc/B/Bhs/Blo) to byte-accurate
1594    // offsets before encoding. `select_with_stack` emits them as label
1595    // placeholders and never resolves them — without this they encode as
1596    // `bne.n #0` and land mid-instruction whenever a 32-bit Thumb-2 instruction
1597    // sits between the branch and its target (UsageFault on real hardware).
1598    // Only meaningful for Thumb-2 (the offset units are halfword/PC+4).
1599    let arm_instrs = if use_thumb2 {
1600        let resolved = resolve_label_branches(arm_instrs, &encoder)?;
1601        // SC-5 (#740/#930): hard-gate every branch target onto the
1602        // instruction-start set of the final stream — both codegen paths
1603        // funnel through here. See `validate_branch_targets`.
1604        validate_branch_targets(&resolved, &encoder)?;
1605        resolved
1606    } else {
1607        arm_instrs
1608    };
1609
1610    // #778: capture the FINAL Thumb-2 instruction stream (post label-resolution,
1611    // the exact list the encode loop below consumes) so `compile_function` can
1612    // derive the sound WCET bound. Cheap clone; frozen-safe (the WCET walk is a
1613    // pure observation and never touches `code`). Only the Thumb-2 path — the A32
1614    // (Cortex-R5) cycle model is a follow-up.
1615    let final_instrs_for_wcet: Option<Vec<synth_synthesis::ArmInstruction>> = if use_thumb2 {
1616        Some(arm_instrs.clone())
1617    } else {
1618        None
1619    };
1620
1621    let mut code = Vec::new();
1622    let mut relocations = Vec::new();
1623
1624    // #345: literal-pool address loads. Each `LdrSym` was encoded as a placeholder
1625    // `LDR.W rd,[pc,#0]`; record where its instruction sits and what it loads so
1626    // we can append a pooled word (carrying the symbol address via R_ARM_ABS32)
1627    // and patch the PC-relative offset once the pool position is known.
1628    struct PendingLiteral {
1629        ldr_offset: u32,
1630        symbol: String,
1631        addend: i32,
1632    }
1633    let mut pending_literals: Vec<PendingLiteral> = Vec::new();
1634
1635    // VCR-DBG-001: per-instruction source map for DWARF `.debug_line`. Captured
1636    // here because `code.len()` immediately before `encode()` is the final
1637    // machine offset of the instruction within this function's `.text` — nothing
1638    // after the loop shifts earlier instructions (the literal pool is appended at
1639    // the end; the LDR patch below is in-place/length-preserving). Purely
1640    // additive: it does not touch `code`, so `.text` is byte-identical.
1641    let mut line_map: LineMap = Vec::new();
1642    // VCR-DEC-003 (#396): object-branch class per emitted instruction, parallel
1643    // to `line_map`. Cheap, additive, does not touch `code`.
1644    let mut branch_map: synth_core::backend::BranchMap = Vec::new();
1645
1646    for instr in &arm_instrs {
1647        // Record a relocation for every BL: the encoder emits `bl #0` and
1648        // relies on a relocation to patch the target. This covers BOTH import
1649        // dispatch stubs (`__meld_*`, undefined externals) AND internal calls
1650        // (`func_N`, defined in this object). Previously only `__meld_*` was
1651        // recorded, so internal `BL func_N` calls were left as unpatched
1652        // `bl #0` placeholders branching to a garbage address (#167).
1653        if let ArmOp::Bl { label } = &instr.op {
1654            // #1040: the relocation type is ISA-STATE-dependent and this is the
1655            // only site that knows the state. An A32 (Cortex-R) `bl` is
1656            // R_ARM_CALL (28); a Thumb `bl` is R_ARM_THM_CALL (10). Emitting
1657            // the Thumb type for an A32 word made any consumer that trusts the
1658            // declared type patch Thumb halfwords into an ARM-state
1659            // instruction. Decided here rather than in the ELF emitter, where
1660            // `config.target` is no longer in scope and the ISA would have to
1661            // be re-derived — the shape that produced the bug.
1662            let kind = if config.target.isa == synth_core::target::IsaVariant::Arm32 {
1663                synth_core::backend::RelocKind::ArmCall
1664            } else {
1665                synth_core::backend::RelocKind::ThmCall
1666            };
1667            relocations.push(CodeRelocation {
1668                offset: code.len() as u32,
1669                symbol: label.clone(),
1670                kind,
1671            });
1672        }
1673        // #237: symbol-relative MOVW/MOVT (the `--native-pointer-abi` static-data
1674        // addressing). The encoder writes the addend in place; record the matching
1675        // R_ARM_MOVW_ABS_NC / R_ARM_MOVT_ABS so the linker adds the symbol address.
1676        if let ArmOp::MovwSym { symbol, .. } = &instr.op {
1677            relocations.push(CodeRelocation {
1678                offset: code.len() as u32,
1679                symbol: symbol.clone(),
1680                kind: synth_core::backend::RelocKind::MovwAbs,
1681            });
1682        }
1683        if let ArmOp::MovtSym { symbol, .. } = &instr.op {
1684            relocations.push(CodeRelocation {
1685                offset: code.len() as u32,
1686                symbol: symbol.clone(),
1687                kind: synth_core::backend::RelocKind::MovtAbs,
1688            });
1689        }
1690        // #345: defer the literal-pool word + reloc + offset patch to the
1691        // post-loop pass (the pool address is not yet known).
1692        if let ArmOp::LdrSym { symbol, addend, .. } = &instr.op {
1693            pending_literals.push(PendingLiteral {
1694                ldr_offset: code.len() as u32,
1695                symbol: symbol.clone(),
1696                addend: *addend,
1697            });
1698        }
1699
1700        // The machine offset of this instruction is the current code length,
1701        // captured before the bytes are appended.
1702        line_map.push((code.len() as u32, instr.source_line));
1703        branch_map.push((code.len() as u32, classify_arm_branch(&instr.op)));
1704
1705        let encoded = encoder
1706            .encode(&instr.op)
1707            .map_err(|e| format!("ARM encoding failed: {}", e))?;
1708        code.extend_from_slice(&encoded);
1709    }
1710
1711    // #345: place the literal pool at the end of this function's `.text`. Gated on
1712    // there being at least one `LdrSym` — functions without one are byte-identical
1713    // to before (no trailing padding, so downstream `func_offsets` are unchanged
1714    // and the frozen differential fixtures stay bit-for-bit equal).
1715    if !pending_literals.is_empty() {
1716        if !use_thumb2 {
1717            return Err("LdrSym literal-pool addressing requires Thumb-2".to_string());
1718        }
1719        // 4-byte align the pool start (Thumb-2 word loads require it, and
1720        // `Align(PC,4)` in the LDR-literal semantics assumes a word-aligned pool).
1721        while code.len() % 4 != 0 {
1722            code.push(0x00);
1723        }
1724        // One distinct pooled word per LdrSym (no dedup: different sites carry
1725        // different addends, and the REL addend lives in the word).
1726        for lit in &pending_literals {
1727            let word_offset = code.len() as u32;
1728
1729            // REL semantics: the linker computes `S + A`, where A is the in-place
1730            // value of the relocated word. Initialize the word to the addend so
1731            // the final loaded address is `symbol + addend`.
1732            code.extend_from_slice(&(lit.addend as u32).to_le_bytes());
1733            relocations.push(CodeRelocation {
1734                offset: word_offset,
1735                symbol: lit.symbol.clone(),
1736                kind: synth_core::backend::RelocKind::Abs32,
1737            });
1738
1739            // Patch the placeholder `LDR.W rd,[pc,#imm12]`. Thumb-2 LDR (literal):
1740            // address = Align(PC,4) + imm12, with PC = ldr_offset + 4. The pool is
1741            // always after the LDR, so U=1 (already set in hw1 = 0xF8DF).
1742            let pc = lit.ldr_offset + 4;
1743            let aligned_pc = pc & !3u32;
1744            let imm12 = word_offset - aligned_pc;
1745            if imm12 > 0xFFF {
1746                // Wide LDR-literal range is ±4 KB; these function bodies are far
1747                // smaller, but fail cleanly rather than miscompile if exceeded.
1748                return Err(format!(
1749                    "LdrSym literal pool out of range (#345): imm12={} > 4095 \
1750                     for symbol {}",
1751                    imm12, lit.symbol
1752                ));
1753            }
1754            let hw2_off = (lit.ldr_offset + 2) as usize;
1755            let mut hw2 = u16::from_le_bytes([code[hw2_off], code[hw2_off + 1]]);
1756            hw2 = (hw2 & 0xF000) | (imm12 as u16); // keep Rt, set imm12
1757            let hw2_bytes = hw2.to_le_bytes();
1758            code[hw2_off] = hw2_bytes[0];
1759            code[hw2_off + 1] = hw2_bytes[1];
1760        }
1761    }
1762
1763    Ok((
1764        code,
1765        relocations,
1766        line_map,
1767        branch_map,
1768        final_instrs_for_wcet,
1769    ))
1770}
1771
1772/// RQ-60-RACOST increment 2 (#242): every rewrite pass between a CHOSEN
1773/// register allocation and the encoder, as ONE reusable sequence.
1774///
1775/// Extracted VERBATIM from `compile_wasm_to_arm` (pure code motion) so the
1776/// `SYNTH_GRAPH_ALLOC` final-byte arbiter can size a candidate allocation
1777/// through the REAL downstream pipeline — the same passes, in the same order,
1778/// reading the same flags — rather than through a hand-mirrored predicate of
1779/// them (the #936 drift class: a hand mirror of the encoder's offset-fold
1780/// threshold was UNSOUND at authoring; a hand mirror of eleven rewrite passes
1781/// would be worse). Flag-off behaviour is byte-identical: the shipping path
1782/// calls this exactly once, on the same stream, in the same place it always
1783/// ran.
1784///
1785/// `ran_realloc` selects the historical branch: the dead-frame /
1786/// callee-saved-prologue / shrink trio runs only downstream of the range
1787/// re-allocation lever (`SYNTH_RANGE_REALLOC` on), exactly as before the
1788/// extraction. Diagnostics inside (SYNTH_FUSE_STATS, SYNTH_SHADOW_ALLOC,
1789/// SYNTH_SPILL_REPORT) print once per CALL, so an arbiter sizing run repeats
1790/// them — measurement noise under two opt-in flags, never a byte change.
1791fn finish_allocated_stream(
1792    arm_instrs: Vec<synth_synthesis::ArmInstruction>,
1793    config: &CompileConfig,
1794    post_exhaust: bool,
1795    ran_realloc: bool,
1796    // #1321: `(frame_size, areas)` recorded by the direct selector for the
1797    // attempt that produced this stream — the frame regions that are genuinely
1798    // allocator SPILL slots. Used to scope VCR-RA-003's #331 check, and ONLY
1799    // after `frame_size` is re-proven against this stream (see the use site).
1800    // `None` on the optimized path and whenever no area was reserved.
1801    recorded_spill_areas: Option<&(i32, Vec<std::ops::Range<i32>>)>,
1802    // RQ-71-VFPALIAS (#881/#1069): the permanent VFP frame homes from that same
1803    // attempt, proven against this stream by the identical `frame_size` test.
1804    // `None` on the optimized path and whenever selection granted no home.
1805    recorded_vfp_frame_homes: Option<&(i32, Vec<i32>)>,
1806) -> Result<Vec<synth_synthesis::ArmInstruction>, String> {
1807    let arm_instrs = if ran_realloc {
1808        let out = arm_instrs;
1809        // VCR-RA-002 (#390, epic #242): eliminate a provably-dead stack frame
1810        // (`sub sp,#N`/`add sp,#N` reserved by `compute_local_layout` for locals
1811        // that promotion homed in registers, never accessed). Removing it saves
1812        // the two instructions AND restores the SP-untouched precondition that
1813        // `shrink_callee_saved_saves` requires — so it must run FIRST.
1814        // DEFAULT-ON (#242 flag audit flip-wave, #592 audit item): evidence
1815        // basis was the 2-path × repro-corpus sweep — 0 functions grow, 58
1816        // shrink (flight_seam controller_step 250→242 −8 / filter_step 180→168
1817        // −12, native_pointer frame_roundtrip 46→34 −12), locked by the
1818        // `dead_frame_elim_no_grow_corpus_242` cargo gate; execution
1819        // differentials re-run green on the new default bytes BEFORE the
1820        // frozen ARM anchors were re-pinned (leaf_dead_frame, flight_seam,
1821        // frame_slot_dce — see the flip PR). Escape hatch:
1822        // `SYNTH_DEAD_FRAME_ELIM=0` opts out and restores the pre-flip bytes
1823        // (CI-gated in `frozen_codegen_bytes.rs`).
1824        let out = if !std::env::var("SYNTH_DEAD_FRAME_ELIM").is_ok_and(|v| v == "0") {
1825            synth_synthesis::liveness::elide_dead_frame(&out).unwrap_or(out)
1826        } else {
1827            out
1828        };
1829        // #490 (epic #242): the optimized selector uses r4-r8 as scratch /
1830        // promoted locals but emits no prologue, silently clobbering a caller's
1831        // callee-saved registers. Add the missing `push {r4-r8,lr}` /
1832        // `pop {r4-r8,pc}` HERE — on the post-realloc body, where realloc has
1833        // lowered low-pressure r4-r8 scratch back to r0-r3, so a save is added
1834        // only for registers genuinely clobbered. `shrink_callee_saved_saves`
1835        // (next) then trims it to the used set. No-op on the direct path (it
1836        // already has its own prologue) and on callee-saved-free leaves.
1837        let out = synth_synthesis::liveness::ensure_callee_saved_prologue(&out);
1838        synth_synthesis::liveness::shrink_callee_saved_saves(&out).unwrap_or(out)
1839    } else {
1840        // Range-realloc off (`SYNTH_RANGE_REALLOC=0`): the optimized path still
1841        // must preserve the callee-saved registers it clobbers (#490). No shrink
1842        // (it is coupled to the realloc lever), so the conservative full save
1843        // stays — correct, just not minimised in this debug configuration.
1844        synth_synthesis::liveness::ensure_callee_saved_prologue(&arm_instrs)
1845    };
1846    // VCR-RA-001 SHADOW ALLOCATION (#209/#242): run the register allocator on
1847    // the selected stream and LOG what it finds — without changing a single
1848    // emitted byte. This is the measure-only bridge between the built analysis
1849    // layer and the eventual virtual-register wiring: it shows, per real
1850    // function, whether the allocator can colour it within the R0–R8 pool and
1851    // how much const-CSE / rematerialization headroom exists (#209). Enable with
1852    // `SYNTH_SHADOW_ALLOC=1`; off by default and side-effect-free either way.
1853    if std::env::var("SYNTH_SHADOW_ALLOC").is_ok() {
1854        use synth_synthesis::liveness::{
1855            AllocationOutcome, allocate_function, function_peak_pressure,
1856        };
1857        // R9 globals / R10 mem-size / R11 mem-base / R12 IP-scratch are reserved;
1858        // pin them above the 0..9 allocatable pool so the colourer keeps R0–R8.
1859        let precolored = std::collections::BTreeMap::from([
1860            (synth_synthesis::rules::Reg::R9, 9usize),
1861            (synth_synthesis::rules::Reg::R10, 10),
1862            (synth_synthesis::rules::Reg::R11, 11),
1863            (synth_synthesis::rules::Reg::R12, 12),
1864        ]);
1865        // True VALUE pressure (one node per value, not per reused physical reg):
1866        // a NeedsSpill with peak ≤ 9 is a SPURIOUS physical-register spill — the
1867        // function fits once virtually allocated.
1868        let peak = function_peak_pressure(&arm_instrs);
1869        match allocate_function(&arm_instrs, 9, &precolored) {
1870            AllocationOutcome::Allocated {
1871                remat_opportunities,
1872                coloring,
1873            } => eprintln!(
1874                "[shadow-alloc] OK: {} pregs coloured within R0-R8 pool, peak value-pressure {}, {} const-CSE/remat opportunities",
1875                coloring.len(),
1876                peak,
1877                remat_opportunities
1878            ),
1879            AllocationOutcome::NeedsSpill(s) => eprintln!(
1880                "[shadow-alloc] physical-graph would spill {:?}, but peak value-pressure is {} (≤9 ⇒ spurious; fits once virtually allocated)",
1881                s, peak
1882            ),
1883            AllocationOutcome::Declined => {
1884                eprintln!(
1885                    "[shadow-alloc] declined (unmodeled construct — calls/i64/fp/offset-branch)"
1886                )
1887            }
1888        }
1889    }
1890
1891    // VCR-SEL-004 cmp→select → IT-block predication fusion (#242). The selector
1892    // lowers a `select` whose condition is a comparison to a *materialize then
1893    // re-test* sequence (`cmp a,b; SetCond D,c; cmp D,#0; movne dst,v1; moveq
1894    // dst,v2`); this collapses it onto the comparison's own flags — deleting the
1895    // `SetCond` and the `cmp D,#0` and retargeting the predicated moves to `c` /
1896    // `invert(c)` — yielding the textbook predicated clamp (`cmp a,b; movc dst,v1;
1897    // mov{!c} dst,v2`). −2 instructions per fused select. gale #428 measured this
1898    // as the #1 hot-path size/cycle lever on the gust_mix clamp chain.
1899    //
1900    // Run LATE: after range re-allocation (so the dead-D proof sees final register
1901    // identities) and before encode. Removal-only + rename-only ⇒ no spill
1902    // regression and labels/branch offsets are unaffected. Each fusion is proven
1903    // sound (flags reused only when nothing clobbers them in the window; the
1904    // boolean deleted only when provably dead) — see `fuse_cmp_select`.
1905    //
1906    // DEFAULT-ON as of v0.13.0 (#428): cmp→select fusion ships by default. The
1907    // byte-changing flip is validated by (a) the unicorn execution oracle that runs
1908    // the two-move `mov{invert(c)}` arm (cmp_select_two_move_differential.py), (b)
1909    // gale's gale_decider_diff 10,596-case sweep across all 8 verified primitives
1910    // (native ≡ flag-off ≡ flag-on = 0x88e73178d232bcf5), and (c) the named-anchor
1911    // differentials re-run with fusion ON — control_step still 0x00210A55, flat+
1912    // inlined flight_algo still 0x07FDF307 (results preserved; bytes deliberately
1913    // changed, re-frozen on this commit). Escape hatch: `SYNTH_NO_CMP_SELECT_FUSE=1`
1914    // reverts to the pre-fusion lowering. The on-silicon G474RE DWT no-regression
1915    // check is a tracked post-ship follow-up (gale owns it).
1916    let arm_instrs = if std::env::var("SYNTH_NO_CMP_SELECT_FUSE").is_err() {
1917        // The rewritten stream is identical to `fuse_cmp_select`'s 2-tuple form;
1918        // the extra `two_move` count is diagnostic only (the fusion census /
1919        // blast-radius datum — #7 made that arm reachable).
1920        let (out, fused, two_move) =
1921            synth_synthesis::liveness::fuse_cmp_select_with_stats(&arm_instrs);
1922        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
1923            let in_place = fused - two_move;
1924            eprintln!(
1925                "[cmp-select-fuse] {fused} select(s) fused to predicated moves \
1926                 ({two_move} two-move, {in_place} in-place)"
1927            );
1928        }
1929        out
1930    } else {
1931        arm_instrs
1932    };
1933
1934    // Perf lever 1 toward native parity (#390): redundant stack-reload elimination.
1935    // synth lowers every wasm local to a frame slot, so `local.set; local.get` emits
1936    // `str rX,[sp,#N]; … ; ldr rY,[sp,#N]`; when rX still holds the value the reload
1937    // (a ~2-cycle M4 load) becomes `mov rY,rX`. Removal-of-a-load + rename only ⇒ no
1938    // new instruction form and no label/offset change. DEFAULT-ON (#242 feature
1939    // loop): validated bit-identical RESULTS on every frozen anchor (control_step
1940    // 0x00210A55 13/13, flat+inlined flight_algo 0x07FDF307) with .text reduced on
1941    // the shipped --relocatable path, plus 8 unit tests + the frame_slot_dce
1942    // execution differential — the same gated path cmp→select took to default-on in
1943    // v0.13.0 (G474RE silicon confirms perf post-ship). Escape hatch:
1944    // `SYNTH_NO_STACK_FWD=1` restores the frame-resident bytes (frozen-old goldens).
1945    let stack_fwd = std::env::var("SYNTH_NO_STACK_FWD").is_err();
1946    let arm_instrs = if stack_fwd {
1947        let (out, fwd) = synth_synthesis::liveness::forward_stack_reloads(&arm_instrs);
1948        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
1949            eprintln!("[stack-fwd] {fwd} stack reload(s) forwarded to register moves");
1950        }
1951        out
1952    } else {
1953        arm_instrs
1954    };
1955
1956    // VCR-RA frame-slot DCE (#242): once `forward_stack_reloads` has turned the
1957    // reloads of a spill slot into register moves, the `str rX,[sp,#N]` that fed
1958    // them is a dead store — its slot is never loaded again. Remove it. Pairs
1959    // with (and only pays after) stack-reload forwarding, so it shares the flag.
1960    let arm_instrs = if stack_fwd {
1961        let (out, n) = synth_synthesis::liveness::eliminate_dead_frame_stores(&arm_instrs);
1962        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
1963            eprintln!("[frame-slot-dce] {n} dead frame store(s) removed");
1964        }
1965        out
1966    } else {
1967        arm_instrs
1968    };
1969
1970    // VCR-RA-001 spill re-choice (#242), two stages behind one flag.
1971    // Stage 1 (the #569 spike): slot-value forwarding BETWEEN reloads.
1972    // `forward_stack_reloads` (above) forwards only from a spill store's
1973    // SOURCE register, so when register pressure clobbers that source its
1974    // reloads survive; this stage tracks which registers provably still hold
1975    // a frame slot's value (through earlier reloads and reg-reg moves) and
1976    // turns reload #2..#n into a 1-cycle `mov` (or deletes it when the target
1977    // already holds the value). Stage 2 (the Belady re-choice): where NO
1978    // register still holds the value — the genuine-spill case, flat_flight's
1979    // peak-11 hot segment — the value was usually evicted while a dead
1980    // register existed; the clobbering def(s) are renamed onto a provably-dead
1981    // register (`spill_rechoice_segment`) so the value stays resident and the
1982    // reload dissolves outright. A dissolved reload can leave the feeding
1983    // store dead, so the frame-slot DCE sweep runs once more behind the same
1984    // flag. Per-segment commit gates: executable same-value-flow trace
1985    // equality, strict shrink, pool-pressure fit, sub-word/unknown-slot
1986    // conservatism (see `apply_spill_realloc` / `spill_rechoice_segment`).
1987    // Stage 3 (whole-function slot liveness): the segment-local DCE keeps a
1988    // store whose slot reaches function end ("reach-end ≠ dead" — it cannot
1989    // see other segments); `eliminate_unread_frame_stores` walks the whole
1990    // function (labels/branches/loops, SP-displacement tracked) and drops a
1991    // store whose slot NO reachable instruction can read — flat_flight's two
1992    // surviving stores (#576), completing Belady's 0-load side with a 0-store
1993    // side. Same flag: the three stages are one lever, flipped together.
1994    // DEFAULT-ON (#242 feature loop, the v0.14.0 local-promotion pattern):
1995    // Belady spilling ships by default. Evidence basis for the flip: three
1996    // landed flag-off increments (#569 forwarding, #576 Belady re-choice,
1997    // #579 whole-fn slot liveness), 40+ functions shrink / 0 grow across the
1998    // 68-fixture × 2-path sweep, per-segment executable value-trace equality
1999    // guards, and the unicorn-vs-wasmtime execution differentials re-run
2000    // green on the new default bytes (flat+inlined flight_algo 0x07FDF307,
2001    // const_cse, frame_slot_dce, spill_rung_581, r12_spill_496 — which covers
2002    // control_step_decide vs wasmtime; control_step's .text is byte-identical
2003    // under the flip) BEFORE the frozen goldens were re-pinned. Escape hatch:
2004    // `SYNTH_SPILL_REALLOC=0` is the OPT-OUT — it disables all three stages
2005    // and restores the pre-flip bytes (CI-gated by
2006    // `frozen_fixtures_spill_realloc_escape_hatch_restores_old_bytes`). Any
2007    // other value (or unset) runs the pass.
2008    // VCR-VER-001 post-exhaustion extensions (#242, the PR #659 verdict): with
2009    // `SYNTH_SPILL_ON_EXHAUST` active the #580 allocation-time Belady spill
2010    // keeps exhausted functions on the optimized path, and its slots present
2011    // shapes the shipping pass structurally cannot fire on (fresh-monotonic
2012    // slots defeat the overwrite-only DCE; the eviction store's source is
2013    // redefined immediately, defeating store→reload forwarding; R2/R3 are
2014    // never touched again, so the rename-target deadness proof declines them).
2015    // `post_exhaust` (bridge-scoped, see above) enables const
2016    // rematerialization of spilled constants, R2/R3 exit-dead rename targets,
2017    // and per-pair pressure commit — see `apply_spill_realloc_post_exhaust`.
2018    // Flag off (the default): `false` selects the shipping behavior bit for
2019    // bit.
2020    let arm_instrs = if !std::env::var("SYNTH_SPILL_REALLOC").is_ok_and(|v| v == "0") {
2021        let (out, n) =
2022            synth_synthesis::liveness::apply_spill_realloc_post_exhaust(&arm_instrs, post_exhaust);
2023        let (out, d) = synth_synthesis::liveness::eliminate_dead_frame_stores(&out);
2024        let (mut out, u) = synth_synthesis::liveness::eliminate_unread_frame_stores(&out);
2025        let (mut tn, mut td, mut tu) = (n, d, u);
2026        // Post-exhaustion only: iterate the triple to a bounded fixpoint. Each
2027        // dissolved spill pair frees registers and removes stores, exposing
2028        // rename windows and holder chains the previous iteration could not
2029        // prove — the allocation-time Belady slots (#580) routinely need two
2030        // or three rounds where the shipping single round suffices for the
2031        // default path's slots. Every iteration is individually gate-proven
2032        // (value-trace equality, pool pressure, strict shrink), so iterating
2033        // composes soundly; the bound keeps compile time deterministic.
2034        if post_exhaust {
2035            let mut progress = n + d + u > 0;
2036            for _ in 0..3 {
2037                if !progress {
2038                    break;
2039                }
2040                let (o, n) =
2041                    synth_synthesis::liveness::apply_spill_realloc_post_exhaust(&out, true);
2042                let (o, d) = synth_synthesis::liveness::eliminate_dead_frame_stores(&o);
2043                let (o, u) = synth_synthesis::liveness::eliminate_unread_frame_stores(&o);
2044                progress = n + d + u > 0;
2045                (tn, td, tu) = (tn + n, td + d, tu + u);
2046                out = o;
2047            }
2048            // The cleanup can leave the spill frame with zero surviving
2049            // accesses (every reload rematerialized/dissolved, every store
2050            // swept) — the balanced `sub sp,#K`/`add sp,#K` is then pure
2051            // overhead. `elide_dead_frame` proves that and removes the pair;
2052            // its early run (post-realloc) could not, because the spill
2053            // traffic was still in the stream at that point.
2054            out = synth_synthesis::liveness::elide_dead_frame(&out).unwrap_or(out);
2055        }
2056        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
2057            eprintln!(
2058                "[spill-realloc] {tn} reload(s) forwarded/eliminated, {td} newly-dead frame store(s) removed, {tu} unread-slot store(s) removed"
2059            );
2060        }
2061        out
2062    } else {
2063        arm_instrs
2064    };
2065
2066    // VCR-RA immediate-shift folding (#390, #242): a constant shift amount the
2067    // stack selector materialized into a scratch register (`movw rM,#C; lsl rD,rN,rM`)
2068    // folds to the immediate form (`lsl rD,rN,#C`), removing the dead `movw` — −1
2069    // instruction, −1 live register. Removal-only (offset-neutral before branch
2070    // resolution, like the dead-store pass). DEFAULT-ON as of v0.15.0: validated
2071    // bit-identical results + a net cycle win on the dissolved hot path (−2
2072    // cyc/call, .text 100→90 B on gust_mix). Escape hatch: `SYNTH_NO_IMM_SHIFT_FOLD=1`.
2073    let arm_instrs = if std::env::var("SYNTH_NO_IMM_SHIFT_FOLD").is_err() {
2074        let (out, folds) = synth_synthesis::liveness::fold_immediate_shifts(&arm_instrs);
2075        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
2076            eprintln!(
2077                "[imm-shift-fold] {folds} register shift(s) folded to immediate, movw dropped"
2078            );
2079        }
2080        out
2081    } else {
2082        arm_instrs
2083    };
2084
2085    // #686: elide the #682 mod-32 shift-amount mask (`and r12,rK,#31` before
2086    // every register-controlled i32 shl/shr) when the amount is STATICALLY
2087    // provable < 32 — a const amount folds to the immediate-shift form
2088    // (reduced mod 32, so >= 32 shrinks too), and an already-masked amount
2089    // (`rK = rX & c`, c < 32) drops the redundant re-mask. gale measured the
2090    // unconditional mask at ~12% cyc/call (+14 B) on gust_mix, whose Q8
2091    // fixed-point shifts are all constants (#686). The mask stays wherever
2092    // the bound is unproven — elision is an optimization, the mask is the
2093    // sound default (`liveness::elide_shift_masks` has the proof
2094    // obligations). Runs after `fold_immediate_shifts` (whose movw→shift
2095    // window the #682 mask intercepts, so it declines every masked const
2096    // shift) and before branch resolution (removal/rewrite-only ⇒
2097    // offset-neutral).
2098    //
2099    // DEFAULT-ON since v0.50.1 (opt-out via `SYNTH_SHIFT_MASK_ELIDE=0`; #846).
2100    // gale's gpio-thin driver regressed +44 B / +9% on synth 0.49 — its pin
2101    // bit-arithmetic (`pin & 31` then a register shift) emits the source
2102    // `and rN,#0x1f` IMMEDIATELY followed by the #682 mod-32 re-mask
2103    // `and r12,rN,#0x1f`; the second is provably redundant (Pattern B: an
2104    // operand produced by `and X,#c`, c<32, is already in [0,31]), so the
2105    // pass drops it. Flipping default-on is a deliberate byte-changing
2106    // refreeze: the elision also moves the frozen anchors (const-amount
2107    // shifts fold back to the immediate form) — control_step −20 B,
2108    // flight_seam −166 B, flight_seam_flat −168 B — all size DECREASES with
2109    // the mask soundly kept for every unproven amount. All differentials were
2110    // re-run on the new bytes and the goldens re-pinned (see #846 PR /
2111    // `frozen_codegen_bytes.rs`). `SYNTH_SHIFT_MASK_ELIDE=0` restores the
2112    // pre-flip bytes (opt-out gate in `shift_mask_elide_686.rs`).
2113    let arm_instrs = if std::env::var("SYNTH_SHIFT_MASK_ELIDE").is_ok_and(|v| v == "0") {
2114        arm_instrs
2115    } else {
2116        let (out, elisions) = synth_synthesis::liveness::elide_shift_masks(&arm_instrs);
2117        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
2118            eprintln!(
2119                "[shift-mask-elide] {elisions} provably-<32 shift-amount mask(s) elided (#686)"
2120            );
2121        }
2122        out
2123    };
2124
2125    // VCR-RA uxth/uxtb fold (#428, #242): `movw rM,#0xffff; and rD,rN,rM` →
2126    // `uxth rD,rN` (and the 0xff/uxtb form), removing the dead `movw` — −1
2127    // instruction, −1 live register per 16/8-bit mask. 0xffff/0xff are not Thumb-2
2128    // modified immediates so the selector materializes them into a register; the
2129    // dedicated zero-extend expresses the same masking inline. Removal-only +
2130    // rewrite-in-place (offset-neutral). DEFAULT-ON (#242 flag audit flip-wave,
2131    // #592 audit item): evidence basis was the 2-path × repro-corpus sweep —
2132    // 0 functions grow, 13 shrink (control_step 300→294 −6, gust_mix 38→32 −6,
2133    // uxth_fold pack 36→24 −12), locked by the `uxth_fold_no_grow_corpus_242`
2134    // cargo gate; execution differentials re-run green on the new default
2135    // bytes BEFORE the frozen ARM anchors were re-pinned (uxth_fold,
2136    // control_step — see the flip PR). Escape hatch: `SYNTH_UXTH_FOLD=0` opts
2137    // out and restores the pre-flip bytes (CI-gated in
2138    // `frozen_codegen_bytes.rs`).
2139    let arm_instrs = if !std::env::var("SYNTH_UXTH_FOLD").is_ok_and(|v| v == "0") {
2140        let (out, folds) = synth_synthesis::liveness::fold_uxth(&arm_instrs);
2141        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
2142            eprintln!("[uxth-fold] {folds} mask-and folded to uxth/uxtb, movw dropped");
2143        }
2144        out
2145    } else {
2146        arm_instrs
2147    };
2148
2149    // VCR-RA-001 const-CSE / rematerialization-avoidance (#209, #242). Drops a
2150    // `movw`/`mov #imm` that re-materializes a constant already resident in
2151    // another register and retargets the reads — every rewrite proven by the
2152    // liveness analysis. Runs LAST, after every immediate-fold (shift, uxth) and
2153    // range-realloc, but BEFORE branch resolution/encoding (it removes
2154    // instructions, shifting byte offsets). CSE-last is the #242 no-regression
2155    // fix: the folds have already absorbed every foldable constant, so CSE can no
2156    // longer defeat one (the gust_mix 90→92 mechanism). The pass additionally
2157    // size-guards each segment via the byte-estimator — it commits a segment's
2158    // rewrites only if they do not grow its estimated size — so a retarget that
2159    // would flip a 16-bit encoding to 32-bit (higher base register) is declined.
2160    // DEFAULT-ON (#242 flip-wave, the SYNTH_SPILL_REALLOC/SYNTH_BASE_CSE
2161    // template): const-CSE ships by default. The flip prerequisites recorded in
2162    // `const_cse_reduction_242.rs` were retired first — the bridge-level INLINE
2163    // aliasing (the alias-eviction spill-bijection hazard) was DELETED from
2164    // `optimizer_bridge::ir_to_arm`, so this post-hoc, liveness-proven pass is
2165    // the flag's ONLY effect. Evidence basis: 152 fixture×path corpus sweep — 0
2166    // functions grow (size-guarded per segment), 40 shrink (const_cse::spill12
2167    // 236→148 B), total −536 B — and the execution differentials re-run green
2168    // on the new default bytes BEFORE the frozen goldens were re-pinned
2169    // (const_cse, frame_slot_dce, flight_seam 0x07FDF307, spill_rung_581,
2170    // volatile_segment_543, control_step 0x00210A55). Escape hatch:
2171    // `SYNTH_CONST_CSE=0` is the OPT-OUT — it restores the pre-flip bytes
2172    // (CI-gated by `const_cse_escape_hatch_restores_old_bytes_242` and the
2173    // frozen-anchor escape-hatch gate). Any other value (or unset) runs the pass.
2174    //
2175    // #543 Phase 2: const-CSE declines WHOLESALE while any volatile DMA range
2176    // (`--volatile-segment`) is marked. At the ArmOp level a cached constant
2177    // cannot be classified as address-vs-data (a retargeted read may be a
2178    // memory-access base carrying a per-use immediate offset), so the
2179    // conservative stance for statically-unknown addressing is to decline every
2180    // aliasing rewrite — each constant is re-materialized at each occurrence,
2181    // the documented volatile contract (`CompileConfig::volatile_segments`).
2182    let arm_instrs = if !std::env::var("SYNTH_CONST_CSE").is_ok_and(|v| v == "0")
2183        && config.volatile_segments.is_empty()
2184    {
2185        let (out, removed) = synth_synthesis::liveness::apply_const_cse(&arm_instrs);
2186        if std::env::var("SYNTH_FUSE_STATS").is_ok() {
2187            eprintln!("[const-cse] {removed} redundant constant materialization(s) removed");
2188        }
2189        out
2190    } else {
2191        arm_instrs
2192    };
2193
2194    // VCR-RA-001 spill-choice REPORT (#242): measure-only, like SYNTH_SHADOW_ALLOC.
2195    // Per straight-line segment, the frame-slot traffic actually emitted vs the
2196    // reload/store count a farthest-next-use (Belady) allocation over the R0-R8
2197    // pool would need — the measured headroom for the full spill-choice rewrite.
2198    // Printed on the FINAL stream (post all rewrite passes), so a flag-off run
2199    // reports the greedy baseline and a flag-on run reports what remains.
2200    if std::env::var("SYNTH_SPILL_REPORT").is_ok() {
2201        for seg in synth_synthesis::liveness::spill_choice_report(&arm_instrs, 9) {
2202            if seg.actual_reloads + seg.actual_spill_stores > 0 || seg.peak_pressure > 9 {
2203                eprintln!(
2204                    "[spill-report] seg@{} len={} peak={} actual={}ld+{}st belady(k=9)={}ld+{}st",
2205                    seg.start,
2206                    seg.len,
2207                    seg.peak_pressure,
2208                    seg.actual_reloads,
2209                    seg.actual_spill_stores,
2210                    seg.belady_reloads,
2211                    seg.belady_spill_stores
2212                );
2213            }
2214        }
2215    }
2216
2217    // ISA feature gate: validate that all generated instructions are supported
2218    // by the target. This catches FPU instructions on no-FPU targets, double-precision
2219    // instructions on single-precision targets, etc.
2220    validate_instructions(&arm_instrs, config.target.fpu, &config.target.triple)
2221        .map_err(|e| format!("ISA validation failed: {}", e))?;
2222
2223    // VCR-RA-003 (epic #242): UNCONDITIONAL per-compilation register-allocation
2224    // validation. The register allocator is the last major unverified codegen
2225    // component; this whole-function checker proves — by construction, on the
2226    // EXACT emitted stream about to be encoded — that the allocation preserves
2227    // FOUR invariants whose reference lives in the stream (or the ABI): (1)
2228    // callee-saved preservation (#490), (2) spill-slot non-aliasing (#331), and
2229    // — PHASE 2 (#49), extending past straight-line — (3) caller-saved
2230    // preservation across calls (a value in R2/R3/R12 live across a `bl` the
2231    // AAPCS boundary destroys), and (4) value availability across control-flow
2232    // joins (a live-in to a join must be defined on every incoming edge). It runs
2233    // on every ARM compile in the DEFAULT shipping build (NOT behind
2234    // `--features verify`; a verify-gated check would be dormant in exactly the
2235    // build that ships — the #757 / VCR-VER-003 lesson) and hard-errors the
2236    // compile on a VIOLATION. A `NotAttempted` verdict (the join check declines
2237    // on an unmodeled-CF function: numeric branch, `BrTable`, etc.) is NON-FATAL
2238    // — the compile proceeds; the other three invariants were still checked and
2239    // held. This is the decline>guess doctrine applied to the checker itself: it
2240    // never claims join coherence it cannot prove, but it also never blocks a
2241    // correct compile for a construct it simply doesn't model yet. Frozen-safe:
2242    // it emits nothing, so `.text` is byte-identical (proven by the frozen suite).
2243    // #1321: scope the #331 spill-slot check to the frame regions the allocator
2244    // actually hands out as spill slots — but ONLY while the record provably
2245    // still describes THIS stream. Between selection and here the stream passes
2246    // through re-allocation, dead-frame elimination and prologue shrink, any of
2247    // which can re-lay or remove the frame. A stale area list would narrow the
2248    // check over the wrong offsets — the silent weakening this lane exists to
2249    // avoid — so the areas are used only if the final stream still allocates
2250    // EXACTLY the frame they were computed for. On any mismatch (re-laid frame,
2251    // eliminated frame, optimized path, no record) we pass `None` and every
2252    // `[sp,#N]` offset is policed, bit for bit as before. The fix can go stale;
2253    // it cannot go wrong.
2254    // The staleness proof, shared by both records: this stream still allocates
2255    // EXACTLY the frame they were computed for. Factored into one closure so the
2256    // spill areas and the VFP frame homes cannot drift apart — they come from
2257    // the same selection and must stand or fall together.
2258    let frame_still_matches = |frame_size: &i32| {
2259        arm_instrs.iter().any(|ins| {
2260            matches!(
2261                &ins.op,
2262                synth_synthesis::rules::ArmOp::Sub {
2263                    rd: synth_synthesis::rules::Reg::SP,
2264                    rn: synth_synthesis::rules::Reg::SP,
2265                    op2: synth_synthesis::rules::Operand2::Imm(n),
2266                } if n == frame_size
2267            )
2268        })
2269    };
2270    let spill_areas: Option<&[std::ops::Range<i32>]> = match recorded_spill_areas {
2271        Some((frame_size, areas)) if frame_still_matches(frame_size) => Some(areas.as_slice()),
2272        _ => None,
2273    };
2274    // RQ-71-VFPALIAS (#881/#1069): same gate, same failure direction. A stale or
2275    // absent home set means the #881 twin polices every VFP `[sp,#N]` offset,
2276    // which is what it did before this narrowing existed.
2277    let vfp_frame_homes: Option<&[i32]> = match recorded_vfp_frame_homes {
2278        Some((frame_size, halves)) if frame_still_matches(frame_size) => Some(halves.as_slice()),
2279        _ => None,
2280    };
2281    match synth_synthesis::liveness::validate_final_allocation_in_areas_with_vfp_homes(
2282        &arm_instrs,
2283        spill_areas,
2284        vfp_frame_homes,
2285    ) {
2286        synth_synthesis::liveness::RaFinalVerdict::Violation(v) => {
2287            return Err(format!(
2288                "VCR-RA-003: register-allocation validation FAILED — {v:?}. \
2289                 The emitted stream violates a register-allocation invariant \
2290                 (callee-saved preservation #490 / spill-slot non-aliasing #331 \
2291                 / caller-saved-across-call / join-value-availability / the #881 \
2292                 VFP twins); this is a \
2293                 compiler bug, not a program error. Refusing to emit a \
2294                 miscompiled object."
2295            ));
2296        }
2297        // Loud honest decline (join reasoning skipped for an unmodeled-CF
2298        // function). Non-fatal — the straight-line / callee-saved / across-call
2299        // invariants still ran and held; only the across-JOIN availability
2300        // reasoning is skipped. Since the #819 redo the optimized path's
2301        // pre-resolved NUMERIC branches are modeled too (build_join_cfg_numeric
2302        // + the PRESERVED entry-availability discriminator), so this fires only
2303        // on genuinely unmodeled shapes: BrTable, computed Bx, mixed
2304        // label+numeric streams, off-boundary numeric targets.
2305        // Surfaced only under `SYNTH_RA003_VERBOSE` so a production compile stays
2306        // quiet: emitting it unconditionally would print on every branchy
2307        // optimized-path compile (new stderr noise phase 1 never produced), yet
2308        // it must remain observable on demand for the honest-scope audit.
2309        synth_synthesis::liveness::RaFinalVerdict::NotAttempted { reason } => {
2310            if std::env::var_os("SYNTH_RA003_VERBOSE").is_some() {
2311                eprintln!(
2312                    "VCR-RA-003: across-join validation NOT ATTEMPTED ({reason}) — \
2313                     straight-line / callee-saved / across-call invariants held; \
2314                     join-availability reasoning declined on this control-flow shape."
2315                );
2316            }
2317        }
2318        synth_synthesis::liveness::RaFinalVerdict::Consistent => {
2319            if std::env::var_os("SYNTH_RA003_VERBOSE").is_some() {
2320                eprintln!("VCR-RA-003: Consistent");
2321            }
2322        }
2323    }
2324    Ok(arm_instrs)
2325}
2326
2327/// VCR-DEC-003 (#396): classify one emitted `ArmOp` into its object-level
2328/// control-flow role for the `synth-provenance-v1` map. Conditional branches are
2329/// the object decision points MC/DC must reconcile; `SelectMove` is the folded
2330/// (IT-block) predicated form the cmp→select fuse produces — a decision with no
2331/// branch.
2332fn classify_arm_branch(op: &ArmOp) -> synth_core::backend::BranchClass {
2333    use synth_core::backend::BranchClass;
2334    match op {
2335        ArmOp::Bcc { .. } | ArmOp::Bhs { .. } | ArmOp::Blo { .. } | ArmOp::BCondOffset { .. } => {
2336            BranchClass::CondBranch
2337        }
2338        ArmOp::B { .. } | ArmOp::BOffset { .. } => BranchClass::UncondBranch,
2339        ArmOp::SelectMove { .. } => BranchClass::Predicated,
2340        _ => BranchClass::Other,
2341    }
2342}
2343
2344/// Resolve local label branches to byte-accurate offsets (#202).
2345///
2346/// `select_with_stack` emits conditional/unconditional branches as label
2347/// placeholders (`Bcc`/`B`/`Bhs`/`Blo` + `Label`) and never resolves them; the
2348/// encoder then emits a `0xD000`/`0xE000` placeholder with offset 0. Before #197
2349/// this path only ran for `--no-optimize`/declined functions, so the latent bug
2350/// stayed hidden — routing relocatable code through it surfaced branches that
2351/// land mid-instruction (a Cortex-M UsageFault) whenever a 32-bit Thumb-2
2352/// instruction sits between the branch and its target.
2353///
2354/// This pass encodes each instruction to learn its real byte length (so 16- vs
2355/// 32-bit forms and multi-instruction expansions are exact), maps each `Label`
2356/// to its byte position, and rewrites every label branch to the displacement
2357/// the encoder consumes: `(target - branch - 4) / 2` halfwords. A bounded
2358/// fixed-point handles an offset growing a branch from 16- to 32-bit (which
2359/// shifts later positions). `BCondOffset`/`BOffset` already produced inline by
2360/// the optimized path carry no label and are left untouched.
2361fn resolve_label_branches(
2362    arm_instrs: Vec<ArmInstruction>,
2363    encoder: &ArmEncoder,
2364) -> Result<Vec<ArmInstruction>, String> {
2365    use std::collections::HashMap;
2366    use synth_synthesis::Condition;
2367
2368    enum BKind {
2369        Cond(Condition),
2370        Uncond,
2371    }
2372    // Record each label branch ONCE — indices are stable across iterations.
2373    let mut branches: Vec<(usize, BKind, String)> = Vec::new();
2374    for (i, instr) in arm_instrs.iter().enumerate() {
2375        match &instr.op {
2376            ArmOp::Bcc { cond, label } => branches.push((i, BKind::Cond(*cond), label.clone())),
2377            ArmOp::Bhs { label } => branches.push((i, BKind::Cond(Condition::HS), label.clone())),
2378            ArmOp::Blo { label } => branches.push((i, BKind::Cond(Condition::LO), label.clone())),
2379            ArmOp::B { label } => branches.push((i, BKind::Uncond, label.clone())),
2380            _ => {}
2381        }
2382    }
2383    if branches.is_empty() {
2384        return Ok(arm_instrs);
2385    }
2386
2387    let mut resolved = arm_instrs;
2388    // Sizes only grow (16→32-bit), so this converges quickly; cap for safety.
2389    for _ in 0..16 {
2390        // 1. Byte position of each instruction (Label encodes to 0 bytes).
2391        let mut positions = Vec::with_capacity(resolved.len());
2392        let mut pos: i64 = 0;
2393        for instr in &resolved {
2394            positions.push(pos);
2395            pos += encoder
2396                .encode(&instr.op)
2397                .map_err(|e| format!("branch-resolve size probe failed: {}", e))?
2398                .len() as i64;
2399        }
2400        // 2. Label name -> byte position (owned keys so the borrow ends here).
2401        let mut labels: HashMap<String, i64> = HashMap::new();
2402        for (i, instr) in resolved.iter().enumerate() {
2403            if let ArmOp::Label { name } = &instr.op {
2404                labels.insert(name.clone(), positions[i]);
2405            }
2406        }
2407        // 3. Rewrite each branch to its byte-accurate offset.
2408        let mut changed = false;
2409        for (idx, kind, label) in &branches {
2410            // A label not defined locally is an EXTERNAL target (e.g.
2411            // `Trap_Handler` resolved by a relocation / the vector table). Leave
2412            // such branches as their placeholder for the existing relocation
2413            // path — only local control-flow labels are byte-resolved here.
2414            let Some(&target) = labels.get(label) else {
2415                continue;
2416            };
2417            // Encoder consumes the field as (target - branch - 4) / 2 halfwords.
2418            // Positions are always even, so this division is exact.
2419            let halfword_offset = ((target - positions[*idx] - 4) / 2) as i32;
2420            let new_op = match kind {
2421                BKind::Cond(c) => ArmOp::BCondOffset {
2422                    cond: *c,
2423                    offset: halfword_offset,
2424                },
2425                BKind::Uncond => ArmOp::BOffset {
2426                    offset: halfword_offset,
2427                },
2428            };
2429            if resolved[*idx].op != new_op {
2430                resolved[*idx].op = new_op;
2431                changed = true;
2432            }
2433        }
2434        if !changed {
2435            break;
2436        }
2437    }
2438    Ok(resolved)
2439}
2440
2441/// SC-5 branch-target boundary gate (#740, #930): every emitted branch target
2442/// must be a member of the instruction-start set of the final stream.
2443///
2444/// "Branch offset calculation shall account for Thumb instruction alignment
2445/// and variable instruction widths" (`safety/stpa/system-constraints.yaml`
2446/// SC-5). Thumb-2 mixes 16- and 32-bit encodings, so an off-by-one-halfword
2447/// target lands on the SECOND halfword of a wide instruction and the CPU
2448/// executes a halfword that was never an instruction — silent garbage, exit 0
2449/// (#930: the skipped `movw` left the `br_if` condition register at its reset
2450/// value). Both known escapes of that sentence were of this class: #740
2451/// (`B<cond>.W` T3 offset halved) and #930 (inner-block end label never
2452/// emitted, `b #0` placeholder). The encoder knows where every instruction
2453/// starts, so a target outside that set is a hard error here rather than
2454/// silent garbage on target.
2455///
2456/// Runs on the FINAL Thumb-2 stream for BOTH codegen paths — the direct
2457/// (`select_with_stack`, label branches byte-resolved above) and the optimized
2458/// (`optimizer_bridge`, numeric `BOffset`/`BCondOffset` pre-resolved inline) —
2459/// since both funnel through this encode pipeline. Two checks, jointly total
2460/// over local control flow:
2461///
2462/// 1. every numeric branch target is in the instruction-start set;
2463/// 2. no LOCAL (`.L`-prefixed) label branch survives unresolved — the resolver
2464///    deliberately skips labels it cannot find because an external target
2465///    (`Trap_Handler`, `func_N`) is legitimately absent and patched by
2466///    relocation, but a `.L` label is only ever defined in this same stream,
2467///    so an unresolved one is a dropped label (#930), not an external.
2468///
2469/// A32 (Cortex-R5) is fixed-width, so the mid-instruction class needs no gate
2470/// there (and its branches do not flow through the Thumb-2 resolver).
2471fn validate_branch_targets(instrs: &[ArmInstruction], encoder: &ArmEncoder) -> Result<(), String> {
2472    use std::collections::HashSet;
2473
2474    // Byte position of each element (`Label` encodes to 0 bytes, so a label's
2475    // position is exactly the start of the instruction that follows it).
2476    let mut positions = Vec::with_capacity(instrs.len());
2477    let mut pos: i64 = 0;
2478    for instr in instrs {
2479        positions.push(pos);
2480        pos += encoder
2481            .encode(&instr.op)
2482            .map_err(|e| format!("SC-5 branch-target gate: size probe failed: {}", e))?
2483            .len() as i64;
2484    }
2485    let starts: HashSet<i64> = positions.iter().copied().collect();
2486
2487    for (i, instr) in instrs.iter().enumerate() {
2488        let offset = match &instr.op {
2489            ArmOp::BOffset { offset } => *offset,
2490            ArmOp::BCondOffset { offset, .. } => *offset,
2491            ArmOp::B { label }
2492            | ArmOp::Bcc { label, .. }
2493            | ArmOp::Bhs { label }
2494            | ArmOp::Blo { label }
2495                if label.starts_with(".L") =>
2496            {
2497                return Err(format!(
2498                    "SC-5 branch-target gate: local branch label '{}' is not \
2499                     defined anywhere in the emitted stream (branch at byte \
2500                     offset 0x{:x}). A `.L` label is only ever defined locally, \
2501                     so this is a dropped label (the #930 class) — the branch \
2502                     would encode as a `b #0` placeholder and land \
2503                     mid-instruction. Refusing to emit a miscompiled object.",
2504                    label, positions[i]
2505                ));
2506            }
2507            _ => continue,
2508        };
2509        // Thumb branch semantics: target = branch_pc + 4 + 2*offset.
2510        let target = positions[i] + 4 + 2 * offset as i64;
2511        if !starts.contains(&target) {
2512            return Err(format!(
2513                "SC-5 branch-target gate: branch at byte offset 0x{:x} targets \
2514                 0x{:x}, which is not an instruction boundary (instruction-start \
2515                 set violation — the target lands mid-instruction, the \
2516                 #740/#930 class). Refusing to emit a miscompiled object.",
2517                positions[i], target
2518            ));
2519        }
2520    }
2521    Ok(())
2522}
2523
2524#[cfg(test)]
2525mod tests {
2526    use super::*;
2527    use synth_synthesis::{Operand2, Reg};
2528
2529    /// #539: `i32.const 0; memory.grow m` folds to `memory.size m`; other deltas
2530    /// (const non-zero, runtime) are left as `memory.grow` (→ the sound fixed-
2531    /// memory -1). Non-grow ops are untouched, so functions without the idiom are
2532    /// byte-identical.
2533    #[test]
2534    fn test_rewrite_memory_grow_zero_539() {
2535        // the idiom -> memory.size
2536        assert_eq!(
2537            rewrite_memory_grow_zero(&[WasmOp::I32Const(0), WasmOp::MemoryGrow(0)]),
2538            vec![WasmOp::MemorySize(0)]
2539        );
2540        // const non-zero delta: NOT folded
2541        assert_eq!(
2542            rewrite_memory_grow_zero(&[WasmOp::I32Const(2), WasmOp::MemoryGrow(0)]),
2543            vec![WasmOp::I32Const(2), WasmOp::MemoryGrow(0)]
2544        );
2545        // runtime delta (no preceding const): NOT folded
2546        assert_eq!(
2547            rewrite_memory_grow_zero(&[WasmOp::LocalGet(0), WasmOp::MemoryGrow(0)]),
2548            vec![WasmOp::LocalGet(0), WasmOp::MemoryGrow(0)]
2549        );
2550        // a bare const-0 not feeding a grow is untouched
2551        assert_eq!(
2552            rewrite_memory_grow_zero(&[WasmOp::I32Const(0), WasmOp::I32Add]),
2553            vec![WasmOp::I32Const(0), WasmOp::I32Add]
2554        );
2555        // fold is local: surrounding ops preserved, indices past the fold intact
2556        assert_eq!(
2557            rewrite_memory_grow_zero(&[
2558                WasmOp::LocalGet(0),
2559                WasmOp::I32Const(0),
2560                WasmOp::MemoryGrow(0),
2561                WasmOp::I32Add,
2562            ]),
2563            vec![WasmOp::LocalGet(0), WasmOp::MemorySize(0), WasmOp::I32Add]
2564        );
2565    }
2566
2567    /// SC-5 (#740/#930): the branch-target boundary gate. Every emitted branch
2568    /// target must be a member of the instruction-start set of the final
2569    /// Thumb-2 stream; a `.L`-local label branch surviving unresolved is a
2570    /// dropped label. Red-first: both rejection arms were written against the
2571    /// exact #930 stream shape (a `b #0` placeholder whose pc+4 target falls
2572    /// on the second halfword of a 32-bit `movw`) and fail without the gate.
2573    #[test]
2574    fn test_validate_branch_targets_sc5_930() {
2575        let enc = ArmEncoder::new_thumb2();
2576        let ins = |op: ArmOp| ArmInstruction {
2577            op,
2578            source_line: None,
2579        };
2580
2581        // 1. Boundary-valid stream: b over a wide movw onto the mov — OK.
2582        //    positions: 0 BOffset(2B), 2 Movw(4B), 6 Mov(2B)
2583        //    target = 0 + 4 + 2*1 = 6 = start of Mov.
2584        let good = vec![
2585            ins(ArmOp::BOffset { offset: 1 }),
2586            ins(ArmOp::Movw {
2587                rd: Reg::R3,
2588                imm16: 1,
2589            }),
2590            ins(ArmOp::Mov {
2591                rd: Reg::R0,
2592                op2: Operand2::Reg(Reg::R3),
2593            }),
2594        ];
2595        assert!(validate_branch_targets(&good, &enc).is_ok());
2596
2597        // 2. The exact #930 shape: `b #0` (offset 0) -> target = pc+4 = 4,
2598        //    the SECOND halfword of the 4-byte movw spanning 2..6. Hard error.
2599        let mid = vec![
2600            ins(ArmOp::BOffset { offset: 0 }),
2601            ins(ArmOp::Movw {
2602                rd: Reg::R3,
2603                imm16: 1,
2604            }),
2605            ins(ArmOp::Mov {
2606                rd: Reg::R0,
2607                op2: Operand2::Reg(Reg::R3),
2608            }),
2609        ];
2610        let err = validate_branch_targets(&mid, &enc).unwrap_err();
2611        assert!(err.contains("SC-5"), "boundary violation names SC-5: {err}");
2612        assert!(err.contains("not an instruction boundary"), "{err}");
2613
2614        // 3. Conditional form of the same violation.
2615        let mid_cond = vec![
2616            ins(ArmOp::BCondOffset {
2617                cond: synth_synthesis::Condition::NE,
2618                offset: 0,
2619            }),
2620            ins(ArmOp::Movw {
2621                rd: Reg::R3,
2622                imm16: 1,
2623            }),
2624            ins(ArmOp::Mov {
2625                rd: Reg::R0,
2626                op2: Operand2::Reg(Reg::R3),
2627            }),
2628        ];
2629        assert!(validate_branch_targets(&mid_cond, &enc).is_err());
2630
2631        // 4. A `.L`-local label branch that was never resolved (the dropped
2632        //    end label, #930's mechanism) is a hard error even though it
2633        //    would encode as a well-formed placeholder.
2634        let dropped = vec![
2635            ins(ArmOp::B {
2636                label: ".Lblock_end_3".to_string(),
2637            }),
2638            ins(ArmOp::Movw {
2639                rd: Reg::R3,
2640                imm16: 1,
2641            }),
2642        ];
2643        let err = validate_branch_targets(&dropped, &enc).unwrap_err();
2644        assert!(err.contains(".Lblock_end_3"), "{err}");
2645        assert!(err.contains("dropped label"), "{err}");
2646
2647        // 5. An EXTERNAL label branch (no `.L` prefix) is legitimately absent
2648        //    (patched via relocation / vector table) and must NOT trip the
2649        //    gate — the carve-out that used to swallow #930 stays for real
2650        //    externals only.
2651        let external = vec![
2652            ins(ArmOp::B {
2653                label: "Trap_Handler".to_string(),
2654            }),
2655            ins(ArmOp::Movw {
2656                rd: Reg::R3,
2657                imm16: 1,
2658            }),
2659        ];
2660        assert!(validate_branch_targets(&external, &enc).is_ok());
2661
2662        // 6. Labels are zero-width: a target on a Label position is the start
2663        //    of the instruction that follows it — OK.
2664        let labeled = vec![
2665            ins(ArmOp::BOffset { offset: 1 }),
2666            ins(ArmOp::Movw {
2667                rd: Reg::R3,
2668                imm16: 1,
2669            }),
2670            ins(ArmOp::Label {
2671                name: ".Lend".to_string(),
2672            }),
2673            ins(ArmOp::Mov {
2674                rd: Reg::R0,
2675                op2: Operand2::Reg(Reg::R3),
2676            }),
2677        ];
2678        assert!(validate_branch_targets(&labeled, &enc).is_ok());
2679    }
2680
2681    /// SC-5 (#930) end-to-end at the backend seam: the labels.wast `br_if2`
2682    /// shape — a `br_if` exiting an enclosing block from inside an `if`, its
2683    /// value operand a block-that-branches — must COMPILE (the pre-fix
2684    /// selector dropped the inner block's end label, which the SC-5 gate now
2685    /// turns into a hard error, so compile success proves the label was
2686    /// emitted) and every branch in the emitted bytes must land on an
2687    /// instruction boundary (re-derived from the encoded halfwords, not from
2688    /// the resolver's own bookkeeping).
2689    #[test]
2690    fn test_930_brif2_shape_compiles_and_targets_boundaries() {
2691        let backend = ArmBackend::new();
2692        let ops = vec![
2693            WasmOp::Block, // $l0 (result i32)
2694            WasmOp::I32Const(1),
2695            WasmOp::If,
2696            WasmOp::Block, // $l1 (result i32)
2697            WasmOp::I32Const(1),
2698            WasmOp::Br(0), // br $l1
2699            WasmOp::End,
2700            WasmOp::I32Const(1),
2701            WasmOp::BrIf(1), // br_if $l0
2702            WasmOp::Drop,
2703            WasmOp::End, // end if
2704            WasmOp::I32Const(0),
2705            WasmOp::End, // end $l0
2706            WasmOp::End,
2707        ];
2708        let config = CompileConfig::default();
2709        let func = backend
2710            .compile_function("t", &ops, &config)
2711            .expect("#930 shape must compile (SC-5 gate passes)");
2712
2713        // Walk the encoded halfwords: collect instruction starts, then check
2714        // every narrow/wide B / B<cond> target is a member.
2715        let code = &func.code;
2716        let mut starts = std::collections::HashSet::new();
2717        let mut widths = Vec::new();
2718        let mut off = 0usize;
2719        while off + 2 <= code.len() {
2720            starts.insert(off as i64);
2721            let hw = u16::from_le_bytes([code[off], code[off + 1]]);
2722            let wide = (hw & 0xF800) >= 0xE800; // 0b11101/0b11110/0b11111
2723            widths.push((off, hw, wide));
2724            off += if wide { 4 } else { 2 };
2725        }
2726        for (off, hw, wide) in widths {
2727            let target = if !wide && (hw & 0xF800) == 0xE000 {
2728                // T2 B: imm11, halfwords
2729                let imm = ((hw & 0x7FF) as i32) << 21 >> 21;
2730                Some(off as i64 + 4 + 2 * imm as i64)
2731            } else if !wide && (hw & 0xF000) == 0xD000 && (hw & 0x0F00) < 0x0E00 {
2732                // T1 B<cond>: imm8, halfwords
2733                let imm = ((hw & 0xFF) as i32) << 24 >> 24;
2734                Some(off as i64 + 4 + 2 * imm as i64)
2735            } else {
2736                None
2737            };
2738            if let Some(t) = target {
2739                assert!(
2740                    starts.contains(&t),
2741                    "branch at 0x{off:x} (hw 0x{hw:04x}) targets 0x{t:x}, \
2742                     not an instruction boundary — the #930 miscompile shape"
2743                );
2744            }
2745        }
2746    }
2747
2748    #[test]
2749    fn test_arm_backend_name() {
2750        let backend = ArmBackend::new();
2751        assert_eq!(backend.name(), "arm");
2752        assert!(backend.is_available());
2753    }
2754
2755    #[test]
2756    fn test_arm_backend_capabilities() {
2757        let backend = ArmBackend::new();
2758        let caps = backend.capabilities();
2759        assert!(!caps.produces_elf);
2760        assert!(caps.supports_rule_verification);
2761        assert!(!caps.is_external);
2762    }
2763
2764    #[test]
2765    fn test_compile_add_function() {
2766        let backend = ArmBackend::new();
2767        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
2768        let config = CompileConfig::default();
2769
2770        let result = backend.compile_function("add", &ops, &config);
2771        assert!(result.is_ok());
2772
2773        let func = result.unwrap();
2774        assert_eq!(func.name, "add");
2775        assert!(!func.code.is_empty());
2776        assert_eq!(func.wasm_ops, ops);
2777    }
2778
2779    /// VCR-DBG-001: the per-instruction source map must cover the function with
2780    /// monotonic, in-bounds machine offsets, and must not perturb the emitted
2781    /// code (it is captured at encode time, never serialized here).
2782    #[test]
2783    fn test_line_map_is_wellformed_dbg001() {
2784        let backend = ArmBackend::new();
2785        let ops = vec![
2786            WasmOp::LocalGet(0),
2787            WasmOp::LocalGet(1),
2788            WasmOp::I32Add,
2789            WasmOp::End,
2790        ];
2791        let config = CompileConfig::default();
2792        let func = backend.compile_function("add", &ops, &config).unwrap();
2793
2794        // Non-empty, and the first instruction starts at machine offset 0.
2795        assert!(
2796            !func.line_map.is_empty(),
2797            "a non-trivial function captures a source map"
2798        );
2799        assert_eq!(func.line_map[0].0, 0, "first instruction at offset 0");
2800
2801        // Offsets strictly increase by at least one ARM/Thumb instruction (>= 2
2802        // bytes) and every mapped offset lies inside the emitted `.text`.
2803        for w in func.line_map.windows(2) {
2804            assert!(w[1].0 > w[0].0, "instruction offsets strictly increase");
2805            assert!(
2806                w[1].0 - w[0].0 >= 2,
2807                "each ARM/Thumb instruction is >= 2 bytes"
2808            );
2809        }
2810        let last = func.line_map.last().unwrap().0 as usize;
2811        assert!(
2812            last < func.code.len(),
2813            "every mapped offset lies inside .text"
2814        );
2815
2816        // The side-table is additive: recompiling is deterministic and the map is
2817        // consistent with that exact code (capturing it does not alter output).
2818        let again = backend.compile_function("add", &ops, &config).unwrap();
2819        assert_eq!(
2820            again.code, func.code,
2821            "compilation deterministic; map is additive"
2822        );
2823        assert_eq!(again.line_map, func.line_map);
2824    }
2825
2826    #[test]
2827    fn test_count_params() {
2828        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
2829        assert_eq!(count_params(&ops), 2);
2830
2831        let no_params = vec![WasmOp::I32Const(5), WasmOp::I32Const(3), WasmOp::I32Add];
2832        assert_eq!(count_params(&no_params), 0);
2833    }
2834
2835    /// #457: the declared param count caps the access-pattern inference. The
2836    /// repro shape `(param i32)(local i32) → p0 + local1` reads local 1 before
2837    /// any write, so `count_params` infers 2 — with the declared count (1) the
2838    /// local is reclassified onto the zero-inited frame path instead of being
2839    /// read from R1 (caller garbage).
2840    #[test]
2841    fn declared_param_count_caps_inference_457() {
2842        let ops = vec![
2843            WasmOp::LocalGet(0),
2844            WasmOp::LocalGet(1),
2845            WasmOp::I32Add,
2846            WasmOp::End,
2847        ];
2848        // The inference alone still says 2 (the misclassification this caps).
2849        assert_eq!(count_params(&ops), 2);
2850
2851        let backend = ArmBackend::new();
2852        let inferred = backend
2853            .compile_function("rbw", &ops, &CompileConfig::default())
2854            .unwrap();
2855        let declared = backend
2856            .compile_function(
2857                "rbw",
2858                &ops,
2859                &CompileConfig {
2860                    current_func_param_count: Some(1),
2861                    ..CompileConfig::default()
2862                },
2863            )
2864            .unwrap();
2865        // The cap is consumed: the declared-count compile reclassifies local 1
2866        // and must emit different code than the param-misclassified one.
2867        assert_ne!(
2868            inferred.code, declared.code,
2869            "declared param count must reach the selector"
2870        );
2871        // The zero-init is present: a 16-bit Thumb `movs rN, #0`
2872        // (0x2000 | rd<<8 → LE bytes [0x00, 0x20+rd]) somewhere in the body.
2873        let has_movs_zero = declared
2874            .code
2875            .as_chunks::<2>()
2876            .0
2877            .iter()
2878            .any(|h| h[0] == 0x00 && (0x20..=0x27).contains(&h[1]));
2879        assert!(
2880            has_movs_zero,
2881            "declared-count compile must zero-init the read-before-write local; code: {:02x?}",
2882            declared.code
2883        );
2884        // A declared count that matches (or exceeds) the inference changes
2885        // nothing — byte-identity for every function without rbw locals.
2886        let matching = backend
2887            .compile_function(
2888                "rbw",
2889                &ops,
2890                &CompileConfig {
2891                    current_func_param_count: Some(2),
2892                    ..CompileConfig::default()
2893                },
2894            )
2895            .unwrap();
2896        assert_eq!(
2897            matching.code, inferred.code,
2898            "declared >= inferred must stay byte-identical"
2899        );
2900    }
2901
2902    /// #970: a CONDITIONALLY-written param must stay a param.
2903    ///
2904    /// The read-first heuristic sees `LocalSet(1)` before any `LocalGet(1)` in
2905    /// LINEAR op order and demotes index 1 — even though the `if` means the
2906    /// write may not execute at all. `min(referenced, declared)` keeps it.
2907    /// The RED symptom this pins is not a wrong constant: the demoted local's
2908    /// first access is a WRITE, so the #457 zero-init skips it and the
2909    /// fall-through arm reads an UNINITIALISED frame slot (executed evidence:
2910    /// `scripts/repro/cond_write_param_970_arm_differential.py`).
2911    #[test]
2912    fn conditionally_written_param_stays_a_param_970() {
2913        // (param i32 i32): if (local.get 0) { local.set 1 = 5 }; local.get 1
2914        let ops = vec![
2915            WasmOp::LocalGet(0),
2916            WasmOp::If,
2917            WasmOp::I32Const(5),
2918            WasmOp::LocalSet(1),
2919            WasmOp::End,
2920            WasmOp::LocalGet(1),
2921            WasmOp::End,
2922        ];
2923        let declared = CompileConfig {
2924            current_func_param_count: Some(2),
2925            ..CompileConfig::default()
2926        };
2927        // The old rule: index 1 is written before it is read, so it is not
2928        // counted — this is the undercount that produced the miscompile.
2929        assert_eq!(
2930            count_params(&ops),
2931            1,
2932            "the read-first heuristic must still undercount (this is the defect)"
2933        );
2934        assert_eq!(
2935            effective_num_params(&ops, &declared),
2936            2,
2937            "a conditionally-written param must be counted as a param"
2938        );
2939        // The #457 direction is untouched: a genuine non-param local is still
2940        // clamped away by the declared count.
2941        let rbw = vec![
2942            WasmOp::LocalGet(0),
2943            WasmOp::LocalGet(1),
2944            WasmOp::I32Add,
2945            WasmOp::End,
2946        ];
2947        assert_eq!(
2948            effective_num_params(
2949                &rbw,
2950                &CompileConfig {
2951                    current_func_param_count: Some(1),
2952                    ..CompileConfig::default()
2953                }
2954            ),
2955            1,
2956            "#457: a read-before-write non-param local must NOT become a param"
2957        );
2958        // Leniency: a body touching only the first few of many declared params
2959        // still lowers with the small count (the selector homes at most 4 in
2960        // registers; `min` is what keeps this from becoming `declared`).
2961        assert_eq!(
2962            effective_num_params(
2963                &rbw,
2964                &CompileConfig {
2965                    current_func_param_count: Some(12),
2966                    ..CompileConfig::default()
2967                }
2968            ),
2969            2,
2970            "declared >> referenced must stay at the referenced count"
2971        );
2972        // No declared count: the legacy inference, unchanged (honest residual).
2973        assert_eq!(
2974            effective_num_params(&ops, &CompileConfig::default()),
2975            count_params(&ops)
2976        );
2977    }
2978
2979    #[test]
2980    fn test_arm_backend_register() {
2981        let mut registry = synth_core::BackendRegistry::new();
2982        registry.register(Box::new(ArmBackend::new()));
2983        assert!(registry.get("arm").is_some());
2984        assert_eq!(registry.available().len(), 1);
2985    }
2986
2987    #[test]
2988    fn test_compile_import_call_produces_relocations() {
2989        let backend = ArmBackend::new();
2990        // Simulate a WASM module where func index 0 is an import.
2991        // Call(0) should generate MOV R0, #0; BL __meld_dispatch_import
2992        let ops = vec![WasmOp::Call(0)];
2993        let config = CompileConfig {
2994            num_imports: 1,
2995            no_optimize: true, // Direct instruction selection to preserve Call semantics
2996            ..CompileConfig::default()
2997        };
2998
2999        let result = backend.compile_function("caller", &ops, &config);
3000        assert!(result.is_ok());
3001
3002        let func = result.unwrap();
3003        assert!(!func.code.is_empty());
3004        assert_eq!(func.relocations.len(), 1);
3005        assert_eq!(func.relocations[0].symbol, "__meld_dispatch_import");
3006        // The BL is the second instruction (after MOV R0, #0), so offset should be > 0
3007        assert!(func.relocations[0].offset > 0);
3008    }
3009
3010    /// Regression test for #197: in `relocatable` mode, an import call must
3011    /// relocate against the direct `func_N` symbol (rewritten to the wasm field
3012    /// name by `build_relocatable_elf`), NOT `__meld_dispatch_import`. This is
3013    /// the ABI half of the #197 fix — without it, a host linker cannot resolve
3014    /// the call to the real kernel symbol (e.g. `k_spin_lock`).
3015    #[test]
3016    fn test_compile_relocatable_import_uses_direct_func_symbol_197() {
3017        let backend = ArmBackend::new();
3018        let ops = vec![WasmOp::Call(0)]; // func 0 is an import
3019        let config = CompileConfig {
3020            num_imports: 1,
3021            relocatable: true,
3022            ..CompileConfig::default()
3023        };
3024
3025        let func = backend
3026            .compile_function("caller", &ops, &config)
3027            .expect("relocatable import call compiles");
3028
3029        assert_eq!(func.relocations.len(), 1);
3030        assert_eq!(
3031            func.relocations[0].symbol, "func_0",
3032            "#197: relocatable import must relocate against func_0 (→ field name), not Meld dispatch"
3033        );
3034    }
3035
3036    #[test]
3037    fn test_compile_no_imports_no_relocations() {
3038        let backend = ArmBackend::new();
3039        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
3040        let config = CompileConfig::default();
3041
3042        let func = backend.compile_function("add", &ops, &config).unwrap();
3043        assert!(func.relocations.is_empty());
3044    }
3045
3046    /// Regression test for #167: a call to an INTERNAL function
3047    /// (index `>= num_imports`) must record a relocation against `func_{index}`.
3048    /// Before the fix, only `__meld_*` (import) BLs were relocated, so
3049    /// internal `BL func_N` was emitted as an unpatched `bl #0` branching
3050    /// to a garbage address — making the object non-linkable. This test
3051    /// would have caught that regression.
3052    #[test]
3053    fn test_compile_internal_call_produces_relocation_167() {
3054        let backend = ArmBackend::new();
3055        // num_imports = 1, so Call(2) is an INTERNAL call → `BL func_2`.
3056        let ops = vec![WasmOp::Call(2)];
3057        let config = CompileConfig {
3058            num_imports: 1,
3059            no_optimize: true,
3060            ..CompileConfig::default()
3061        };
3062
3063        let func = backend
3064            .compile_function("caller", &ops, &config)
3065            .expect("internal call compiles");
3066
3067        assert_eq!(
3068            func.relocations.len(),
3069            1,
3070            "an internal call must emit exactly one relocation (#167)"
3071        );
3072        assert_eq!(
3073            func.relocations[0].symbol, "func_2",
3074            "internal call must relocate against the callee's func_{{index}} symbol (#167)"
3075        );
3076    }
3077
3078    // ─── Phase 1 safety-bounds plumbing for ARM ──────────────────────────
3079
3080    #[test]
3081    fn arm_safety_bounds_mpu_emits_same_code_as_none() {
3082        // Mpu mode must not introduce any inline check on ARM — the MPU
3083        // handles faults via hardware. The encoded bytes for an i32.load
3084        // should be identical between None and Mpu.
3085        let backend = ArmBackend::new();
3086        let ops = vec![
3087            WasmOp::LocalGet(0),
3088            WasmOp::I32Load {
3089                offset: 0,
3090                align: 2,
3091            },
3092        ];
3093        let cfg_none = CompileConfig {
3094            no_optimize: true,
3095            ..Default::default()
3096        };
3097        let cfg_mpu = CompileConfig {
3098            no_optimize: true,
3099            safety_bounds: SafetyBounds::Mpu,
3100            ..Default::default()
3101        };
3102        let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
3103        let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
3104        assert_eq!(
3105            n.code, m.code,
3106            "Mpu and None should produce identical ARM bytes (Mpu relies on hardware)"
3107        );
3108    }
3109
3110    #[test]
3111    fn arm_legacy_bounds_check_still_emits_software_check() {
3112        // Legacy CLI users with `--bounds-check` should keep getting the
3113        // software path even though the new SafetyBounds field defaults to None.
3114        let backend = ArmBackend::new();
3115        let ops = vec![
3116            WasmOp::LocalGet(0),
3117            WasmOp::I32Load {
3118                offset: 0,
3119                align: 2,
3120            },
3121        ];
3122        let cfg_legacy = CompileConfig {
3123            no_optimize: true,
3124            bounds_check: true,
3125            ..Default::default()
3126        };
3127        let cfg_software = CompileConfig {
3128            no_optimize: true,
3129            safety_bounds: SafetyBounds::Software,
3130            ..Default::default()
3131        };
3132        let l = backend.compile_function("ld", &ops, &cfg_legacy).unwrap();
3133        let s = backend.compile_function("ld", &ops, &cfg_software).unwrap();
3134        assert_eq!(
3135            l.code, s.code,
3136            "--bounds-check should produce the same bytes as --safety-bounds=software"
3137        );
3138    }
3139
3140    /// #377: `--safety-bounds software` must be enforced on the OPTIMIZED path
3141    /// too. Pre-fix, `software` was byte-identical to `none` there (a silent
3142    /// no-op while the safety manifest claimed enforcement). The compiled
3143    /// bytes must now (a) differ from `none` and (b) contain the inline
3144    /// `CMP ip, sl` + `UDF` guard.
3145    #[test]
3146    fn arm_safety_bounds_software_enforced_on_optimized_path_377() {
3147        let backend = ArmBackend::new();
3148        // Dynamic-address store+load: the optimized path accepts this shape
3149        // (no calls, no i64 params, ≤4 params).
3150        let ops = vec![
3151            WasmOp::LocalGet(0),
3152            WasmOp::LocalGet(1),
3153            WasmOp::I32Store {
3154                offset: 4,
3155                align: 2,
3156            },
3157            WasmOp::LocalGet(0),
3158            WasmOp::I32Load {
3159                offset: 0,
3160                align: 2,
3161            },
3162        ];
3163        // no_optimize NOT set — this exercises the optimized path.
3164        let cfg_none = CompileConfig::default();
3165        let cfg_sw = CompileConfig {
3166            safety_bounds: SafetyBounds::Software,
3167            ..Default::default()
3168        };
3169        let n = backend.compile_function("st", &ops, &cfg_none).unwrap();
3170        let s = backend.compile_function("st", &ops, &cfg_sw).unwrap();
3171        assert_ne!(
3172            n.code, s.code,
3173            "#377: software bounds must CHANGE optimized-path codegen (was a silent no-op)"
3174        );
3175        // Thumb-2 `UDF #0` is 0xDE00 (LE bytes: 00 DE); the #752
3176        // wraparound-safe guard's borrow check `CMP sl, ip` (16-bit
3177        // high-reg form) is 0x45E2 (LE: E2 45). Both must appear — one
3178        // guard per access, traps inline.
3179        let has_udf = s.code.windows(2).any(|w| w == [0x00, 0xDE]);
3180        let has_cmp_sl_ip = s.code.windows(2).any(|w| w == [0xE2, 0x45]);
3181        assert!(has_udf, "#377: inline UDF trap missing from optimized path");
3182        assert!(
3183            has_cmp_sl_ip,
3184            "#377/#752: CMP sl, ip bounds borrow-check missing from optimized path"
3185        );
3186        // And `none` must contain NO UDF (the function has no other trap).
3187        assert!(
3188            !n.code.windows(2).any(|w| w == [0x00, 0xDE]),
3189            "none must not contain a UDF for this function"
3190        );
3191    }
3192
3193    /// #377: `mpu` on the optimized path is codegen-passthrough — identical
3194    /// bytes to `none` on BOTH paths (hardware enforcement is target-level;
3195    /// synth does not emit MPU region programming — tracked separately in
3196    /// #377's fix-direction discussion). This pins path-parity for `mpu`.
3197    #[test]
3198    fn arm_safety_bounds_mpu_optimized_path_parity_377() {
3199        let backend = ArmBackend::new();
3200        let ops = vec![
3201            WasmOp::LocalGet(0),
3202            WasmOp::I32Load {
3203                offset: 0,
3204                align: 2,
3205            },
3206        ];
3207        let cfg_none = CompileConfig::default();
3208        let cfg_mpu = CompileConfig {
3209            safety_bounds: SafetyBounds::Mpu,
3210            ..Default::default()
3211        };
3212        let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
3213        let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
3214        assert_eq!(
3215            n.code, m.code,
3216            "Mpu and None must produce identical bytes on the optimized path too"
3217        );
3218    }
3219
3220    /// #377: `mask` on the optimized path declines to the direct selector
3221    /// (honest degradation) — the compiled function must equal the
3222    /// `--no-optimize` masking bytes, i.e. the flag is honored, never dropped.
3223    #[test]
3224    fn arm_safety_bounds_mask_optimized_path_declines_to_direct_377() {
3225        let backend = ArmBackend::new();
3226        let ops = vec![
3227            WasmOp::LocalGet(0),
3228            WasmOp::LocalGet(1),
3229            WasmOp::I32Store {
3230                offset: 0,
3231                align: 2,
3232            },
3233        ];
3234        // RQ-57-SENTINEL: mask now requires a STATED non-zero power-of-two
3235        // size (0 = zero-byte memory = refused), so the test states one —
3236        // exactly what the #953 fix required of the rv32 driver test.
3237        let cfg_mask_opt = CompileConfig {
3238            safety_bounds: SafetyBounds::Mask,
3239            linear_memory_bytes: 64 * 1024,
3240            ..Default::default()
3241        };
3242        let cfg_mask_direct = CompileConfig {
3243            no_optimize: true,
3244            safety_bounds: SafetyBounds::Mask,
3245            linear_memory_bytes: 64 * 1024,
3246            ..Default::default()
3247        };
3248        let o = backend.compile_function("st", &ops, &cfg_mask_opt).unwrap();
3249        let d = backend
3250            .compile_function("st", &ops, &cfg_mask_direct)
3251            .unwrap();
3252        assert_eq!(
3253            o.code, d.code,
3254            "#377: mask on the optimized path must fall back to the direct selector's masking"
3255        );
3256    }
3257
3258    /// RQ-57-SENTINEL (#953 sibling): `--safety-bounds mask` with a ZERO-byte
3259    /// linear memory must REFUSE at compile time. Before this fix, `bytes == 0`
3260    /// was exempt from the power-of-two gate ("0 means unknown"), and the
3261    /// emitted `SUB R12, R10, #1; AND` guard degenerated to an IDENTITY mask
3262    /// at runtime (R10 = 0 baked by the startup for a `(memory 0)` module):
3263    /// an unbounded OOB access in the mode whose purpose is bounding.
3264    /// Red-first: pre-fix this compile SUCCEEDED (verified on the v0.56.1
3265    /// tree: exit 0, `movw r10, #0x0` in the reset handler, AND-masked body).
3266    #[test]
3267    fn arm_safety_bounds_mask_zero_size_refused_rq57() {
3268        let backend = ArmBackend::new();
3269        let ops = vec![
3270            WasmOp::LocalGet(0),
3271            WasmOp::I32Load {
3272                offset: 0,
3273                align: 2,
3274            },
3275        ];
3276        let cfg = CompileConfig {
3277            safety_bounds: SafetyBounds::Mask,
3278            linear_memory_bytes: 0,
3279            ..Default::default()
3280        };
3281        let err = backend
3282            .compile_function("ld", &ops, &cfg)
3283            .expect_err("mask over a zero-byte memory must refuse, not emit an identity mask");
3284        let msg = format!("{err}");
3285        assert!(
3286            msg.contains("ZERO bytes"),
3287            "refusal must name the zero-byte cause, got: {msg}"
3288        );
3289    }
3290
3291    // ========================================================================
3292    // ISA feature gate tests — ensure the compiler never emits unsupported
3293    // instructions for a given target
3294    // ========================================================================
3295
3296    #[test]
3297    fn test_f32_rejected_on_cortex_m3_no_fpu() {
3298        let backend = ArmBackend::new();
3299        let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
3300        let config = CompileConfig {
3301            target: TargetSpec::cortex_m3(),
3302            no_optimize: true,
3303            ..CompileConfig::default()
3304        };
3305
3306        let result = backend.compile_function("fadd", &ops, &config);
3307        assert!(
3308            result.is_err(),
3309            "f32 operations should fail on Cortex-M3 (no FPU)"
3310        );
3311    }
3312
3313    #[test]
3314    fn test_f32_accepted_on_cortex_m4f() {
3315        let backend = ArmBackend::new();
3316        let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
3317        let config = CompileConfig {
3318            target: TargetSpec::cortex_m4f(),
3319            no_optimize: true,
3320            ..CompileConfig::default()
3321        };
3322
3323        let result = backend.compile_function("fadd", &ops, &config);
3324        assert!(
3325            result.is_ok(),
3326            "f32 operations should succeed on Cortex-M4F, got: {:?}",
3327            result.unwrap_err()
3328        );
3329    }
3330
3331    #[test]
3332    fn test_i32_works_on_all_targets() {
3333        let backend = ArmBackend::new();
3334        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
3335
3336        // Cortex-M3 (no FPU)
3337        let config_m3 = CompileConfig {
3338            target: TargetSpec::cortex_m3(),
3339            no_optimize: true,
3340            ..CompileConfig::default()
3341        };
3342        assert!(
3343            backend.compile_function("add", &ops, &config_m3).is_ok(),
3344            "i32 ops should work on Cortex-M3"
3345        );
3346
3347        // Cortex-M4F (single FPU)
3348        let config_m4f = CompileConfig {
3349            target: TargetSpec::cortex_m4f(),
3350            no_optimize: true,
3351            ..CompileConfig::default()
3352        };
3353        assert!(
3354            backend.compile_function("add", &ops, &config_m4f).is_ok(),
3355            "i32 ops should work on Cortex-M4F"
3356        );
3357
3358        // Cortex-M7DP (double FPU)
3359        let config_m7dp = CompileConfig {
3360            target: TargetSpec::cortex_m7dp(),
3361            no_optimize: true,
3362            ..CompileConfig::default()
3363        };
3364        assert!(
3365            backend.compile_function("add", &ops, &config_m7dp).is_ok(),
3366            "i32 ops should work on Cortex-M7DP"
3367        );
3368    }
3369
3370    #[test]
3371    fn test_f32_rejected_on_cortex_m4_no_fpu() {
3372        // Cortex-M4 (without F suffix) has no FPU
3373        let backend = ArmBackend::new();
3374        let ops = vec![WasmOp::F32Const(1.5), WasmOp::F32Const(2.5), WasmOp::F32Mul];
3375        let config = CompileConfig {
3376            target: TargetSpec::cortex_m4(),
3377            no_optimize: true,
3378            ..CompileConfig::default()
3379        };
3380
3381        let result = backend.compile_function("fmul", &ops, &config);
3382        assert!(
3383            result.is_err(),
3384            "f32 operations should fail on Cortex-M4 (no FPU)"
3385        );
3386    }
3387
3388    // ========================================================================
3389    // Issue #120 — f32 ops in the optimized lowering path
3390    //
3391    // `OptimizerBridge::wasm_to_ir` has no handlers for f32/f64 ops, so a
3392    // value-producing float op fell through to `Opcode::Nop`, leaving a
3393    // downstream consumer with an unmapped vreg and tripping the PR #101
3394    // defensive panic in `ir_to_arm`. Customer reproducer: `compiler_builtins
3395    // float::div` and `gale_compute_ipi_mask` in the `falcon-rate-component`
3396    // module.
3397    //
3398    // Fix: `optimize_full` declines float modules with a typed `Err`;
3399    // `compile_wasm_to_arm` falls back to the non-optimized `select_with_stack`
3400    // path, which handles f32 via VFP/FPU. These tests use the *default*
3401    // (optimized) config — `no_optimize` is NOT set — which is the exact
3402    // configuration that panicked pre-fix.
3403    // ========================================================================
3404
3405    /// RQ-65-DECLINE (#1208 / #1213 / #1205): a shape the optimized path
3406    /// used to lower WRONG must now reach the direct selector through the
3407    /// fallback, so the DEFAULT configuration is byte-identical to
3408    /// `--no-optimize` on it — the selector-parity property
3409    /// (`selector_parity_197_differential.py`), asserted at the unit level
3410    /// for one instance of each declined class.
3411    #[test]
3412    fn test_rq65_declined_shapes_default_equals_no_optimize() {
3413        let backend = ArmBackend::new();
3414        let cases: Vec<(&str, Vec<WasmOp>)> = vec![
3415            (
3416                "ld8u",
3417                vec![
3418                    WasmOp::LocalGet(0),
3419                    WasmOp::I64Load8U {
3420                        offset: 0,
3421                        align: 0,
3422                    },
3423                ],
3424            ),
3425            (
3426                "st16",
3427                vec![
3428                    WasmOp::LocalGet(0),
3429                    WasmOp::I64Const(7),
3430                    WasmOp::I64Store16 {
3431                        offset: 0,
3432                        align: 1,
3433                    },
3434                ],
3435            ),
3436            (
3437                "sel64",
3438                vec![
3439                    WasmOp::I64Const(1),
3440                    WasmOp::I64Const(0),
3441                    WasmOp::LocalGet(0),
3442                    WasmOp::Select,
3443                    WasmOp::I32WrapI64,
3444                ],
3445            ),
3446            (
3447                "vif",
3448                vec![
3449                    WasmOp::LocalGet(0),
3450                    WasmOp::LocalGet(1),
3451                    WasmOp::I32GtS,
3452                    WasmOp::If,
3453                    WasmOp::I32Const(7),
3454                    WasmOp::Else,
3455                    WasmOp::I32Const(9),
3456                    WasmOp::End,
3457                ],
3458            ),
3459        ];
3460        for (name, ops) in cases {
3461            let default = backend
3462                .compile_function(name, &ops, &CompileConfig::default())
3463                .unwrap_or_else(|e| {
3464                    panic!("{name}: default config must compile via the fallback: {e}")
3465                });
3466            let direct = backend
3467                .compile_function(
3468                    name,
3469                    &ops,
3470                    &CompileConfig {
3471                        no_optimize: true,
3472                        ..CompileConfig::default()
3473                    },
3474                )
3475                .unwrap_or_else(|e| panic!("{name}: --no-optimize must compile: {e}"));
3476            assert_eq!(
3477                default.code, direct.code,
3478                "{name}: the declined shape must be the direct selector's bytes"
3479            );
3480            assert!(default.code.len() > 2, "{name}: not the #1208 empty body");
3481        }
3482    }
3483
3484    /// Pre-fix: this panicked with "vreg vN has no assigned ARM register and
3485    /// no spill slot" inside `ir_to_arm`. Post-fix: the optimized path declines
3486    /// the module and the backend falls back to direct selection, producing a
3487    /// non-empty f32.div lowering on a Cortex-M4F.
3488    #[test]
3489    fn test_issue120_f32_div_compiles_via_optimized_default() {
3490        let backend = ArmBackend::new();
3491        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
3492        let config = CompileConfig {
3493            target: TargetSpec::cortex_m4f(),
3494            // no_optimize NOT set — this exercises the optimized path that
3495            // panicked in issue #120, then the fallback to direct selection.
3496            // GI-FPU-002: the f32 params must be declared so the direct
3497            // selector homes them in S0/S1 (AAPCS-VFP) rather than declining.
3498            current_func_params_f32: vec![true, true],
3499            ..CompileConfig::default()
3500        };
3501
3502        let result = backend.compile_function("fdiv", &ops, &config);
3503        assert!(
3504            result.is_ok(),
3505            "f32.div must compile on Cortex-M4F via the optimized->direct \
3506             fallback (issue #120), got: {:?}",
3507            result.as_ref().err()
3508        );
3509        assert!(
3510            !result.unwrap().code.is_empty(),
3511            "f32.div must produce non-empty machine code"
3512        );
3513    }
3514
3515    /// A spread of f32 ops, all through the optimized (default) config, must
3516    /// compile via the fallback on an FPU target without panicking.
3517    #[test]
3518    fn test_issue120_assorted_f32_ops_compile_via_optimized_default() {
3519        let backend = ArmBackend::new();
3520        let config = CompileConfig {
3521            target: TargetSpec::cortex_m4f(),
3522            // GI-FPU-002: declare the two f32 params for AAPCS-VFP homing.
3523            current_func_params_f32: vec![true, true],
3524            ..CompileConfig::default()
3525        };
3526
3527        let cases: Vec<(&str, Vec<WasmOp>)> = vec![
3528            (
3529                "fadd",
3530                vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Add],
3531            ),
3532            (
3533                "fmul",
3534                vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Mul],
3535            ),
3536            (
3537                "fsub",
3538                vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Sub],
3539            ),
3540        ];
3541
3542        for (name, ops) in cases {
3543            let result = backend.compile_function(name, &ops, &config);
3544            assert!(
3545                result.is_ok(),
3546                "{name} must compile via the optimized->direct fallback \
3547                 (issue #120), got: {:?}",
3548                result.as_ref().err()
3549            );
3550            assert!(
3551                !result.unwrap().code.is_empty(),
3552                "{name} must produce non-empty machine code"
3553            );
3554        }
3555    }
3556
3557    /// The fallback must still honor the ISA feature gate: f32 on a no-FPU
3558    /// target must fail cleanly (not panic) even on the optimized path.
3559    #[test]
3560    fn test_issue120_f32_div_rejected_on_no_fpu_via_optimized() {
3561        let backend = ArmBackend::new();
3562        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
3563        let config = CompileConfig {
3564            target: TargetSpec::cortex_m3(),
3565            ..CompileConfig::default()
3566        };
3567
3568        let result = backend.compile_function("fdiv", &ops, &config);
3569        assert!(
3570            result.is_err(),
3571            "f32.div must be rejected on Cortex-M3 (no FPU), not panic"
3572        );
3573    }
3574
3575    /// #507: a `br_table` function compiled via the DEFAULT (optimized) config
3576    /// must produce the SAME bytes as the direct (`no_optimize`) selector —
3577    /// i.e. the optimized path declined it to direct, lowering the dispatch as a
3578    /// real cmp-chain instead of silently dropping it (which left all arms in
3579    /// fall-through). Pre-fix the two outputs differed (the optimized one had no
3580    /// selector compare). Execution correctness is gated by
3581    /// `scripts/repro/br_table_507_differential.py`.
3582    #[test]
3583    fn test_507_br_table_declines_to_direct() {
3584        let backend = ArmBackend::new();
3585        // dispatch(sel): br_table over 3 blocks, each storing a marker to mem[0].
3586        let ops = vec![
3587            WasmOp::Block,
3588            WasmOp::Block,
3589            WasmOp::Block,
3590            WasmOp::LocalGet(0),
3591            WasmOp::BrTable {
3592                targets: vec![0, 1, 2],
3593                default: 2,
3594            },
3595            WasmOp::End,
3596            WasmOp::I32Const(0),
3597            WasmOp::I32Const(10),
3598            WasmOp::I32Store {
3599                offset: 0,
3600                align: 2,
3601            },
3602            WasmOp::Return,
3603            WasmOp::End,
3604            WasmOp::I32Const(0),
3605            WasmOp::I32Const(20),
3606            WasmOp::I32Store {
3607                offset: 0,
3608                align: 2,
3609            },
3610            WasmOp::Return,
3611            WasmOp::End,
3612            WasmOp::I32Const(0),
3613            WasmOp::I32Const(30),
3614            WasmOp::I32Store {
3615                offset: 0,
3616                align: 2,
3617            },
3618        ];
3619        let opt = CompileConfig {
3620            target: TargetSpec::cortex_m4(),
3621            ..CompileConfig::default()
3622        };
3623        let direct = CompileConfig {
3624            target: TargetSpec::cortex_m4(),
3625            no_optimize: true,
3626            ..CompileConfig::default()
3627        };
3628        let a = backend
3629            .compile_function("dispatch", &ops, &opt)
3630            .expect("optimized-default must compile br_table (via decline)");
3631        let b = backend
3632            .compile_function("dispatch", &ops, &direct)
3633            .expect("direct must compile br_table");
3634        assert_eq!(
3635            a.code, b.code,
3636            "#507: optimized-default br_table output must be byte-identical to the \
3637             direct selector (i.e. declined to direct), not a dropped dispatch"
3638        );
3639    }
3640
3641    /// Issue #94: end-to-end byte-size check for the canonical u64-packed
3642    /// FFI-return hi32 extract pattern. Compiles two near-identical
3643    /// functions — one with the optimized shift-by-32, one with a generic
3644    /// shift-by-7 — and asserts the optimized form is meaningfully smaller.
3645    #[test]
3646    fn test_issue94_hi32_extract_is_smaller_than_generic_shift() {
3647        let backend = ArmBackend::new();
3648        let config = CompileConfig {
3649            target: TargetSpec::cortex_m4f(),
3650            ..CompileConfig::default()
3651        };
3652
3653        // #518: the i64 value must NOT come from an i64 PARAM — the optimized
3654        // path now declines i64-param functions to the direct selector (it homed
3655        // an i64 param in R4:R5 instead of R0:R1, a silent miscompile this test's
3656        // byte-size-only assertion masked). The canonical #94 case is a u64 from
3657        // an FFI return, not a param, anyway. Source the i64 from a sign-extended
3658        // i32 param (`extend_i32_s`): a runtime, non-constant-foldable i64 that
3659        // stays on the optimized path, so the shift-by-32 hi-extract peephole is
3660        // still exercised on CORRECT code.
3661        // Optimized path: `(i64.extend_i32_s (local.get 0)) >>> 32; wrap_i64`
3662        let ops_hi32 = vec![
3663            WasmOp::LocalGet(0), // i32 param in R0
3664            WasmOp::I64ExtendI32S,
3665            WasmOp::I64Const(32),
3666            WasmOp::I64ShrU,
3667            WasmOp::I32WrapI64,
3668        ];
3669        let func_hi32 = backend
3670            .compile_function("hi32_extract", &ops_hi32, &config)
3671            .unwrap();
3672
3673        // Generic path: `... >>> 7; wrap_i64` — same shape, but the shift amount
3674        // is not a multiple of 32, so it falls through to the runtime shift.
3675        let ops_generic = vec![
3676            WasmOp::LocalGet(0),
3677            WasmOp::I64ExtendI32S,
3678            WasmOp::I64Const(7),
3679            WasmOp::I64ShrU,
3680            WasmOp::I32WrapI64,
3681        ];
3682        let func_generic = backend
3683            .compile_function("generic_shr", &ops_generic, &config)
3684            .unwrap();
3685
3686        let bytes_hi32 = func_hi32.code.len();
3687        let bytes_generic = func_generic.code.len();
3688        println!(
3689            "\n[issue #94] hi32 extract: {} bytes (vs generic shift: {} bytes; saved {})",
3690            bytes_hi32,
3691            bytes_generic,
3692            bytes_generic.saturating_sub(bytes_hi32)
3693        );
3694        let hex: String = func_hi32
3695            .code
3696            .iter()
3697            .map(|b| format!("{:02x}", b))
3698            .collect::<Vec<_>>()
3699            .join(" ");
3700        println!("[issue #94] hi32 bytes: {}", hex);
3701        // We expect the optimized form to be at least 30 bytes smaller than
3702        // the generic 64-bit shift sequence. (Empirically: 14 vs 50 bytes.)
3703        assert!(
3704            bytes_hi32 + 30 <= bytes_generic,
3705            "issue #94: hi32 extract = {} bytes, generic shift = {} bytes; \
3706             expected optimized form to be at least 30 bytes smaller",
3707            bytes_hi32,
3708            bytes_generic,
3709        );
3710    }
3711}