Skip to main content

synth_backend/
arm_backend.rs

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