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