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, 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            let compiled = self.compile_function(&name, &func.ops, config)?;
79            functions.push(compiled);
80        }
81
82        Ok(CompilationResult {
83            functions,
84            elf: None,
85            backend_name: self.name().to_string(),
86        })
87    }
88
89    fn compile_function(
90        &self,
91        name: &str,
92        ops: &[WasmOp],
93        config: &CompileConfig,
94    ) -> Result<CompiledFunction, BackendError> {
95        let (code, relocations) =
96            compile_wasm_to_arm(ops, config).map_err(BackendError::CompilationFailed)?;
97
98        Ok(CompiledFunction {
99            name: name.to_string(),
100            code,
101            wasm_ops: ops.to_vec(),
102            relocations,
103        })
104    }
105
106    fn is_available(&self) -> bool {
107        true // Always available — it's a library backend
108    }
109}
110
111/// Count the number of function parameters by analyzing LocalGet patterns
112fn count_params(wasm_ops: &[WasmOp]) -> u32 {
113    let mut first_access: std::collections::HashMap<u32, bool> = std::collections::HashMap::new();
114    for op in wasm_ops {
115        match op {
116            WasmOp::LocalGet(idx) => {
117                first_access.entry(*idx).or_insert(true);
118            }
119            WasmOp::LocalSet(idx) | WasmOp::LocalTee(idx) => {
120                first_access.entry(*idx).or_insert(false);
121            }
122            _ => {}
123        }
124    }
125
126    first_access
127        .iter()
128        .filter_map(
129            |(&idx, &is_read_first)| {
130                if is_read_first { Some(idx + 1) } else { None }
131            },
132        )
133        .max()
134        .unwrap_or(0)
135}
136
137/// Core compilation: WASM ops → ARM machine code bytes + relocations
138///
139/// Returns (code_bytes, relocations) where relocations record BL instructions
140/// that target external symbols (e.g., `__meld_dispatch_import` for import calls).
141fn compile_wasm_to_arm(
142    wasm_ops: &[WasmOp],
143    config: &CompileConfig,
144) -> Result<(Vec<u8>, Vec<CodeRelocation>), String> {
145    let num_params = count_params(wasm_ops);
146
147    let bounds_config = match config.effective_safety_bounds() {
148        SafetyBounds::None => BoundsCheckConfig::None,
149        SafetyBounds::Mpu => BoundsCheckConfig::Mpu,
150        SafetyBounds::Software => BoundsCheckConfig::Software,
151        SafetyBounds::Mask => BoundsCheckConfig::Masking,
152    };
153
154    // The non-optimized (direct) instruction-selection path. Handles f32 via
155    // VFP/FPU. Used directly when `--no-optimize` is set, and as the fallback
156    // when the optimized path declines a module (see issue #120 below).
157    let select_direct = || -> Result<Vec<ArmInstruction>, String> {
158        let db = RuleDatabase::with_standard_rules();
159        let mut selector =
160            InstructionSelector::with_bounds_check(db.rules().to_vec(), bounds_config);
161        selector.set_target(config.target.fpu, &config.target.triple);
162        if config.num_imports > 0 {
163            selector.set_num_imports(config.num_imports);
164        }
165        // #195: plumb the callee argument-count tables so the direct selector can
166        // marshal call arguments into R0–R3 per AAPCS.
167        selector.set_func_arg_counts(
168            config.func_arg_counts.clone(),
169            config.type_arg_counts.clone(),
170        );
171        // #197: in relocatable host-link mode, emit direct `func_N` BLs for
172        // imports (rewritten to the wasm field name by build_relocatable_elf)
173        // instead of `__meld_dispatch_import`.
174        selector.set_relocatable(config.relocatable);
175        // #237: native-pointer ABI — wasm statics become __synth_wasm_data-relative.
176        selector.set_native_pointer_abi(config.native_pointer_abi, config.linear_memory_bytes);
177        // #311: i64 call results are register PAIRS — tag them.
178        selector.set_result_types(config.func_ret_i64.clone(), config.type_ret_i64.clone());
179        // Stack-pointer promotion is meaningful only under the native-pointer ABI;
180        // gating here keeps every non-native compile (all frozen fixtures) on the
181        // legacy R9 globals-table path, bit-identical.
182        if config.native_pointer_abi
183            && let Some((sp_idx, sp_init)) = config.stack_pointer_global
184        {
185            selector.set_native_pointer_stack(sp_idx, sp_init);
186        }
187        selector
188            .select_with_stack(wasm_ops, num_params)
189            .map_err(|e| format!("instruction selection failed: {}", e))
190    };
191
192    // Instruction selection: optimized or direct.
193    //
194    // #197: `--relocatable` (host-link ET_REL) forces the direct selector. The
195    // optimized path materializes an absolute linmem base (0x20000100) and does
196    // not preserve caller-saved registers across calls — both wrong for a
197    // host-linked object, where the linmem base arrives via `fp` at runtime and
198    // callees follow AAPCS. `select_with_stack` (now i64-spill capable after
199    // #171) handles fp-relative memory + caller-saved preservation correctly.
200    let arm_instrs = if config.no_optimize || config.relocatable {
201        select_direct()?
202    } else {
203        let opt_config = if config.loom_compat {
204            OptimizationConfig::loom_compat()
205        } else {
206            OptimizationConfig::all()
207        };
208
209        let mut bridge = OptimizerBridge::with_config(opt_config);
210        // #188: tell the bridge how many imports there are so it declines only
211        // LOCAL calls (and leaves import calls on the optimized path, keeping
212        // the #173 field-name relocation rewrite intact).
213        bridge.set_num_imports(config.num_imports);
214        // `ir_to_arm` now returns `Result` — an `Err` means the optimized path
215        // hit an unmapped vreg (issue-#93-class). Treat it identically to an
216        // `optimize_full` failure: fall back to the direct selector rather
217        // than propagating, so the function still compiles correctly.
218        match bridge
219            .optimize_full(wasm_ops)
220            .and_then(|(opt_ir, _cfg, _stats)| bridge.ir_to_arm(&opt_ir, num_params as usize))
221        {
222            Ok(arm_ops) => arm_ops
223                .into_iter()
224                .map(|op| ArmInstruction {
225                    op,
226                    source_line: None,
227                })
228                .collect(),
229            // Issue #120: the optimized path declines modules it cannot lower
230            // (notably scalar f32/f64 ops — the IR has no float opcodes). Fall
231            // back to the direct instruction selector, which handles f32 via
232            // VFP/FPU. This is honest degradation: the function still compiles
233            // correctly, just without IR-level optimization.
234            Err(_) => select_direct()?,
235        }
236    };
237
238    // #257/#277: `mul`+`add`→`mla` fusion is intentionally NOT wired here.
239    // The transform is correct and ready (`synth_synthesis::liveness::fuse_mul_add`,
240    // fully tested), but it is **register-allocation-coupled**: over the current
241    // greedy single-pass selector, folding `mul rM,..; add rD,rM,rX` → `mla`
242    // extends the live ranges of the mul inputs to the mla point, and the added
243    // pressure (extra moves/spills) costs more than the single-cycle MLA saves —
244    // gale measured a +2 cyc on-target REGRESSION (flat_flight 255→257, G474RE)
245    // even though it removes 2 instructions and the seam stays 0x07FDF307. So the
246    // fusion stays unwired until the spill-aware allocator (VCR-RA-001) chooses
247    // registers, at which point it becomes net-positive (per #272's plan and the
248    // wiring design note). Lesson (#277): a register-pressure-affecting transform
249    // needs an on-target/allocator-aware gate, not a byte-count gate, before it
250    // can default on.
251
252    // VCR-RA-001 const-CSE / rematerialization-avoidance (#209), the first
253    // allocator-analysis-driven CODEGEN change. Drops `movw` re-materializations
254    // of a constant already resident in another register and retargets the reads
255    // — every rewrite proven by the liveness analysis, and it ONLY removes
256    // materializations (pressure never rises), so unlike the mla fusion (#277) it
257    // cannot regress on-target. Runs on the selected stream before branch
258    // resolution (it removes instructions, shifting byte offsets). Behind
259    // `SYNTH_CONST_CSE=1` while it is validated against the differential oracle +
260    // gale's five on-target baselines; off by default keeps every fixture
261    // bit-identical.
262    let arm_instrs = if std::env::var("SYNTH_CONST_CSE").is_ok() {
263        synth_synthesis::liveness::apply_const_cse(&arm_instrs).0
264    } else {
265        arm_instrs
266    };
267
268    // VCR-RA-001 RANGE RE-ALLOCATION (#209/#242, wiring step 3a) — the first
269    // CONSEQUENTIAL allocator pass: re-colour each maximal straight-line
270    // segment over the R0-R8 pool with value ranges as the allocation unit
271    // (segment inputs + per-register live-outs pinned to their original
272    // registers, reserved R9-R12/SP identity-assigned — each segment is
273    // independently sound, no cross-segment liveness assumed). Renames
274    // registers only: never adds, removes, or reorders instructions, so
275    // labels/branch offsets are unaffected.
276    //
277    // DEFAULT-ON since v0.11.36: gale cleared the gate on-target (G474RE,
278    // #209 2026-06-10) — flag-on output byte-identical to flag-off on
279    // flat_flight/controller/control_step, fires on the filter family with
280    // zero cycle delta and a small size win, all selfchecks green on silicon.
281    // Opt out with `SYNTH_RANGE_REALLOC=0`; per-function stats with
282    // `SYNTH_REALLOC_STATS=1`.
283    //
284    // The companion dead callee-saved-save elimination (gale's "next
285    // consequential lever", same issue comment) then shrinks the prologue
286    // `push {r4-r8,lr}` / epilogue `pop {r4-r8,pc}` to the callee-saved
287    // registers the re-allocated body still touches (leaf-only,
288    // SP-untouched, even-count-padded — see shrink_callee_saved_saves):
289    // ~12 cycles of pure save/restore overhead removed on small leaves.
290    let realloc_on = std::env::var("SYNTH_RANGE_REALLOC").map_or(true, |v| v != "0");
291    let arm_instrs = if realloc_on {
292        use synth_synthesis::rules::Reg;
293        const POOL: [Reg; 9] = [
294            Reg::R0,
295            Reg::R1,
296            Reg::R2,
297            Reg::R3,
298            Reg::R4,
299            Reg::R5,
300            Reg::R6,
301            Reg::R7,
302            Reg::R8,
303        ];
304        let (out, stats) = synth_synthesis::liveness::reallocate_function(&arm_instrs, &POOL);
305        if std::env::var("SYNTH_REALLOC_STATS").is_ok() {
306            eprintln!(
307                "[range-realloc] {} segments: {} reallocated, {} declined, {} need spill (step 4)",
308                stats.segments, stats.reallocated, stats.declined, stats.needs_spill
309            );
310        }
311        synth_synthesis::liveness::shrink_callee_saved_saves(&out).unwrap_or(out)
312    } else {
313        arm_instrs
314    };
315
316    // VCR-RA-001 SHADOW ALLOCATION (#209/#242): run the register allocator on
317    // the selected stream and LOG what it finds — without changing a single
318    // emitted byte. This is the measure-only bridge between the built analysis
319    // layer and the eventual virtual-register wiring: it shows, per real
320    // function, whether the allocator can colour it within the R0–R8 pool and
321    // how much const-CSE / rematerialization headroom exists (#209). Enable with
322    // `SYNTH_SHADOW_ALLOC=1`; off by default and side-effect-free either way.
323    if std::env::var("SYNTH_SHADOW_ALLOC").is_ok() {
324        use synth_synthesis::liveness::{
325            AllocationOutcome, allocate_function, function_peak_pressure,
326        };
327        // R9 globals / R10 mem-size / R11 mem-base / R12 IP-scratch are reserved;
328        // pin them above the 0..9 allocatable pool so the colourer keeps R0–R8.
329        let precolored = std::collections::BTreeMap::from([
330            (synth_synthesis::rules::Reg::R9, 9usize),
331            (synth_synthesis::rules::Reg::R10, 10),
332            (synth_synthesis::rules::Reg::R11, 11),
333            (synth_synthesis::rules::Reg::R12, 12),
334        ]);
335        // True VALUE pressure (one node per value, not per reused physical reg):
336        // a NeedsSpill with peak ≤ 9 is a SPURIOUS physical-register spill — the
337        // function fits once virtually allocated.
338        let peak = function_peak_pressure(&arm_instrs);
339        match allocate_function(&arm_instrs, 9, &precolored) {
340            AllocationOutcome::Allocated {
341                remat_opportunities,
342                coloring,
343            } => eprintln!(
344                "[shadow-alloc] OK: {} pregs coloured within R0-R8 pool, peak value-pressure {}, {} const-CSE/remat opportunities",
345                coloring.len(),
346                peak,
347                remat_opportunities
348            ),
349            AllocationOutcome::NeedsSpill(s) => eprintln!(
350                "[shadow-alloc] physical-graph would spill {:?}, but peak value-pressure is {} (≤9 ⇒ spurious; fits once virtually allocated)",
351                s, peak
352            ),
353            AllocationOutcome::Declined => {
354                eprintln!(
355                    "[shadow-alloc] declined (unmodeled construct — calls/i64/fp/offset-branch)"
356                )
357            }
358        }
359    }
360
361    // ISA feature gate: validate that all generated instructions are supported
362    // by the target. This catches FPU instructions on no-FPU targets, double-precision
363    // instructions on single-precision targets, etc.
364    validate_instructions(&arm_instrs, config.target.fpu, &config.target.triple)
365        .map_err(|e| format!("ISA validation failed: {}", e))?;
366
367    // Encode to binary — use Thumb-2 for Cortex-M targets
368    let use_thumb2 = matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb);
369
370    let encoder = if use_thumb2 {
371        ArmEncoder::new_thumb2_with_fpu(config.target.fpu)
372    } else {
373        ArmEncoder::new_arm32()
374    };
375
376    // #202: resolve local label branches (Bcc/B/Bhs/Blo) to byte-accurate
377    // offsets before encoding. `select_with_stack` emits them as label
378    // placeholders and never resolves them — without this they encode as
379    // `bne.n #0` and land mid-instruction whenever a 32-bit Thumb-2 instruction
380    // sits between the branch and its target (UsageFault on real hardware).
381    // Only meaningful for Thumb-2 (the offset units are halfword/PC+4).
382    let arm_instrs = if use_thumb2 {
383        resolve_label_branches(arm_instrs, &encoder)?
384    } else {
385        arm_instrs
386    };
387
388    let mut code = Vec::new();
389    let mut relocations = Vec::new();
390
391    for instr in &arm_instrs {
392        // Record a relocation for every BL: the encoder emits `bl #0` and
393        // relies on a relocation to patch the target. This covers BOTH import
394        // dispatch stubs (`__meld_*`, undefined externals) AND internal calls
395        // (`func_N`, defined in this object). Previously only `__meld_*` was
396        // recorded, so internal `BL func_N` calls were left as unpatched
397        // `bl #0` placeholders branching to a garbage address (#167).
398        if let ArmOp::Bl { label } = &instr.op {
399            relocations.push(CodeRelocation {
400                offset: code.len() as u32,
401                symbol: label.clone(),
402                kind: synth_core::backend::RelocKind::ThmCall,
403            });
404        }
405        // #237: symbol-relative MOVW/MOVT (the `--native-pointer-abi` static-data
406        // addressing). The encoder writes the addend in place; record the matching
407        // R_ARM_MOVW_ABS_NC / R_ARM_MOVT_ABS so the linker adds the symbol address.
408        if let ArmOp::MovwSym { symbol, .. } = &instr.op {
409            relocations.push(CodeRelocation {
410                offset: code.len() as u32,
411                symbol: symbol.clone(),
412                kind: synth_core::backend::RelocKind::MovwAbs,
413            });
414        }
415        if let ArmOp::MovtSym { symbol, .. } = &instr.op {
416            relocations.push(CodeRelocation {
417                offset: code.len() as u32,
418                symbol: symbol.clone(),
419                kind: synth_core::backend::RelocKind::MovtAbs,
420            });
421        }
422
423        let encoded = encoder
424            .encode(&instr.op)
425            .map_err(|e| format!("ARM encoding failed: {}", e))?;
426        code.extend_from_slice(&encoded);
427    }
428
429    Ok((code, relocations))
430}
431
432/// Resolve local label branches to byte-accurate offsets (#202).
433///
434/// `select_with_stack` emits conditional/unconditional branches as label
435/// placeholders (`Bcc`/`B`/`Bhs`/`Blo` + `Label`) and never resolves them; the
436/// encoder then emits a `0xD000`/`0xE000` placeholder with offset 0. Before #197
437/// this path only ran for `--no-optimize`/declined functions, so the latent bug
438/// stayed hidden — routing relocatable code through it surfaced branches that
439/// land mid-instruction (a Cortex-M UsageFault) whenever a 32-bit Thumb-2
440/// instruction sits between the branch and its target.
441///
442/// This pass encodes each instruction to learn its real byte length (so 16- vs
443/// 32-bit forms and multi-instruction expansions are exact), maps each `Label`
444/// to its byte position, and rewrites every label branch to the displacement
445/// the encoder consumes: `(target - branch - 4) / 2` halfwords. A bounded
446/// fixed-point handles an offset growing a branch from 16- to 32-bit (which
447/// shifts later positions). `BCondOffset`/`BOffset` already produced inline by
448/// the optimized path carry no label and are left untouched.
449fn resolve_label_branches(
450    arm_instrs: Vec<ArmInstruction>,
451    encoder: &ArmEncoder,
452) -> Result<Vec<ArmInstruction>, String> {
453    use std::collections::HashMap;
454    use synth_synthesis::Condition;
455
456    enum BKind {
457        Cond(Condition),
458        Uncond,
459    }
460    // Record each label branch ONCE — indices are stable across iterations.
461    let mut branches: Vec<(usize, BKind, String)> = Vec::new();
462    for (i, instr) in arm_instrs.iter().enumerate() {
463        match &instr.op {
464            ArmOp::Bcc { cond, label } => branches.push((i, BKind::Cond(*cond), label.clone())),
465            ArmOp::Bhs { label } => branches.push((i, BKind::Cond(Condition::HS), label.clone())),
466            ArmOp::Blo { label } => branches.push((i, BKind::Cond(Condition::LO), label.clone())),
467            ArmOp::B { label } => branches.push((i, BKind::Uncond, label.clone())),
468            _ => {}
469        }
470    }
471    if branches.is_empty() {
472        return Ok(arm_instrs);
473    }
474
475    let mut resolved = arm_instrs;
476    // Sizes only grow (16→32-bit), so this converges quickly; cap for safety.
477    for _ in 0..16 {
478        // 1. Byte position of each instruction (Label encodes to 0 bytes).
479        let mut positions = Vec::with_capacity(resolved.len());
480        let mut pos: i64 = 0;
481        for instr in &resolved {
482            positions.push(pos);
483            pos += encoder
484                .encode(&instr.op)
485                .map_err(|e| format!("branch-resolve size probe failed: {}", e))?
486                .len() as i64;
487        }
488        // 2. Label name -> byte position (owned keys so the borrow ends here).
489        let mut labels: HashMap<String, i64> = HashMap::new();
490        for (i, instr) in resolved.iter().enumerate() {
491            if let ArmOp::Label { name } = &instr.op {
492                labels.insert(name.clone(), positions[i]);
493            }
494        }
495        // 3. Rewrite each branch to its byte-accurate offset.
496        let mut changed = false;
497        for (idx, kind, label) in &branches {
498            // A label not defined locally is an EXTERNAL target (e.g.
499            // `Trap_Handler` resolved by a relocation / the vector table). Leave
500            // such branches as their placeholder for the existing relocation
501            // path — only local control-flow labels are byte-resolved here.
502            let Some(&target) = labels.get(label) else {
503                continue;
504            };
505            // Encoder consumes the field as (target - branch - 4) / 2 halfwords.
506            // Positions are always even, so this division is exact.
507            let halfword_offset = ((target - positions[*idx] - 4) / 2) as i32;
508            let new_op = match kind {
509                BKind::Cond(c) => ArmOp::BCondOffset {
510                    cond: *c,
511                    offset: halfword_offset,
512                },
513                BKind::Uncond => ArmOp::BOffset {
514                    offset: halfword_offset,
515                },
516            };
517            if resolved[*idx].op != new_op {
518                resolved[*idx].op = new_op;
519                changed = true;
520            }
521        }
522        if !changed {
523            break;
524        }
525    }
526    Ok(resolved)
527}
528
529#[cfg(test)]
530mod tests {
531    use super::*;
532
533    #[test]
534    fn test_arm_backend_name() {
535        let backend = ArmBackend::new();
536        assert_eq!(backend.name(), "arm");
537        assert!(backend.is_available());
538    }
539
540    #[test]
541    fn test_arm_backend_capabilities() {
542        let backend = ArmBackend::new();
543        let caps = backend.capabilities();
544        assert!(!caps.produces_elf);
545        assert!(caps.supports_rule_verification);
546        assert!(!caps.is_external);
547    }
548
549    #[test]
550    fn test_compile_add_function() {
551        let backend = ArmBackend::new();
552        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
553        let config = CompileConfig::default();
554
555        let result = backend.compile_function("add", &ops, &config);
556        assert!(result.is_ok());
557
558        let func = result.unwrap();
559        assert_eq!(func.name, "add");
560        assert!(!func.code.is_empty());
561        assert_eq!(func.wasm_ops, ops);
562    }
563
564    #[test]
565    fn test_count_params() {
566        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
567        assert_eq!(count_params(&ops), 2);
568
569        let no_params = vec![WasmOp::I32Const(5), WasmOp::I32Const(3), WasmOp::I32Add];
570        assert_eq!(count_params(&no_params), 0);
571    }
572
573    #[test]
574    fn test_arm_backend_register() {
575        let mut registry = synth_core::BackendRegistry::new();
576        registry.register(Box::new(ArmBackend::new()));
577        assert!(registry.get("arm").is_some());
578        assert_eq!(registry.available().len(), 1);
579    }
580
581    #[test]
582    fn test_compile_import_call_produces_relocations() {
583        let backend = ArmBackend::new();
584        // Simulate a WASM module where func index 0 is an import.
585        // Call(0) should generate MOV R0, #0; BL __meld_dispatch_import
586        let ops = vec![WasmOp::Call(0)];
587        let config = CompileConfig {
588            num_imports: 1,
589            no_optimize: true, // Direct instruction selection to preserve Call semantics
590            ..CompileConfig::default()
591        };
592
593        let result = backend.compile_function("caller", &ops, &config);
594        assert!(result.is_ok());
595
596        let func = result.unwrap();
597        assert!(!func.code.is_empty());
598        assert_eq!(func.relocations.len(), 1);
599        assert_eq!(func.relocations[0].symbol, "__meld_dispatch_import");
600        // The BL is the second instruction (after MOV R0, #0), so offset should be > 0
601        assert!(func.relocations[0].offset > 0);
602    }
603
604    /// Regression test for #197: in `relocatable` mode, an import call must
605    /// relocate against the direct `func_N` symbol (rewritten to the wasm field
606    /// name by `build_relocatable_elf`), NOT `__meld_dispatch_import`. This is
607    /// the ABI half of the #197 fix — without it, a host linker cannot resolve
608    /// the call to the real kernel symbol (e.g. `k_spin_lock`).
609    #[test]
610    fn test_compile_relocatable_import_uses_direct_func_symbol_197() {
611        let backend = ArmBackend::new();
612        let ops = vec![WasmOp::Call(0)]; // func 0 is an import
613        let config = CompileConfig {
614            num_imports: 1,
615            relocatable: true,
616            ..CompileConfig::default()
617        };
618
619        let func = backend
620            .compile_function("caller", &ops, &config)
621            .expect("relocatable import call compiles");
622
623        assert_eq!(func.relocations.len(), 1);
624        assert_eq!(
625            func.relocations[0].symbol, "func_0",
626            "#197: relocatable import must relocate against func_0 (→ field name), not Meld dispatch"
627        );
628    }
629
630    #[test]
631    fn test_compile_no_imports_no_relocations() {
632        let backend = ArmBackend::new();
633        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
634        let config = CompileConfig::default();
635
636        let func = backend.compile_function("add", &ops, &config).unwrap();
637        assert!(func.relocations.is_empty());
638    }
639
640    /// Regression test for #167: a call to an INTERNAL function
641    /// (index `>= num_imports`) must record a relocation against `func_{index}`.
642    /// Before the fix, only `__meld_*` (import) BLs were relocated, so
643    /// internal `BL func_N` was emitted as an unpatched `bl #0` branching
644    /// to a garbage address — making the object non-linkable. This test
645    /// would have caught that regression.
646    #[test]
647    fn test_compile_internal_call_produces_relocation_167() {
648        let backend = ArmBackend::new();
649        // num_imports = 1, so Call(2) is an INTERNAL call → `BL func_2`.
650        let ops = vec![WasmOp::Call(2)];
651        let config = CompileConfig {
652            num_imports: 1,
653            no_optimize: true,
654            ..CompileConfig::default()
655        };
656
657        let func = backend
658            .compile_function("caller", &ops, &config)
659            .expect("internal call compiles");
660
661        assert_eq!(
662            func.relocations.len(),
663            1,
664            "an internal call must emit exactly one relocation (#167)"
665        );
666        assert_eq!(
667            func.relocations[0].symbol, "func_2",
668            "internal call must relocate against the callee's func_{{index}} symbol (#167)"
669        );
670    }
671
672    // ─── Phase 1 safety-bounds plumbing for ARM ──────────────────────────
673
674    #[test]
675    fn arm_safety_bounds_mpu_emits_same_code_as_none() {
676        // Mpu mode must not introduce any inline check on ARM — the MPU
677        // handles faults via hardware. The encoded bytes for an i32.load
678        // should be identical between None and Mpu.
679        let backend = ArmBackend::new();
680        let ops = vec![
681            WasmOp::LocalGet(0),
682            WasmOp::I32Load {
683                offset: 0,
684                align: 2,
685            },
686        ];
687        let cfg_none = CompileConfig {
688            no_optimize: true,
689            ..Default::default()
690        };
691        let cfg_mpu = CompileConfig {
692            no_optimize: true,
693            safety_bounds: SafetyBounds::Mpu,
694            ..Default::default()
695        };
696        let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
697        let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
698        assert_eq!(
699            n.code, m.code,
700            "Mpu and None should produce identical ARM bytes (Mpu relies on hardware)"
701        );
702    }
703
704    #[test]
705    fn arm_legacy_bounds_check_still_emits_software_check() {
706        // Legacy CLI users with `--bounds-check` should keep getting the
707        // software path even though the new SafetyBounds field defaults to None.
708        let backend = ArmBackend::new();
709        let ops = vec![
710            WasmOp::LocalGet(0),
711            WasmOp::I32Load {
712                offset: 0,
713                align: 2,
714            },
715        ];
716        let cfg_legacy = CompileConfig {
717            no_optimize: true,
718            bounds_check: true,
719            ..Default::default()
720        };
721        let cfg_software = CompileConfig {
722            no_optimize: true,
723            safety_bounds: SafetyBounds::Software,
724            ..Default::default()
725        };
726        let l = backend.compile_function("ld", &ops, &cfg_legacy).unwrap();
727        let s = backend.compile_function("ld", &ops, &cfg_software).unwrap();
728        assert_eq!(
729            l.code, s.code,
730            "--bounds-check should produce the same bytes as --safety-bounds=software"
731        );
732    }
733
734    // ========================================================================
735    // ISA feature gate tests — ensure the compiler never emits unsupported
736    // instructions for a given target
737    // ========================================================================
738
739    #[test]
740    fn test_f32_rejected_on_cortex_m3_no_fpu() {
741        let backend = ArmBackend::new();
742        let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
743        let config = CompileConfig {
744            target: TargetSpec::cortex_m3(),
745            no_optimize: true,
746            ..CompileConfig::default()
747        };
748
749        let result = backend.compile_function("fadd", &ops, &config);
750        assert!(
751            result.is_err(),
752            "f32 operations should fail on Cortex-M3 (no FPU)"
753        );
754    }
755
756    #[test]
757    fn test_f32_accepted_on_cortex_m4f() {
758        let backend = ArmBackend::new();
759        let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
760        let config = CompileConfig {
761            target: TargetSpec::cortex_m4f(),
762            no_optimize: true,
763            ..CompileConfig::default()
764        };
765
766        let result = backend.compile_function("fadd", &ops, &config);
767        assert!(
768            result.is_ok(),
769            "f32 operations should succeed on Cortex-M4F, got: {:?}",
770            result.unwrap_err()
771        );
772    }
773
774    #[test]
775    fn test_i32_works_on_all_targets() {
776        let backend = ArmBackend::new();
777        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
778
779        // Cortex-M3 (no FPU)
780        let config_m3 = CompileConfig {
781            target: TargetSpec::cortex_m3(),
782            no_optimize: true,
783            ..CompileConfig::default()
784        };
785        assert!(
786            backend.compile_function("add", &ops, &config_m3).is_ok(),
787            "i32 ops should work on Cortex-M3"
788        );
789
790        // Cortex-M4F (single FPU)
791        let config_m4f = CompileConfig {
792            target: TargetSpec::cortex_m4f(),
793            no_optimize: true,
794            ..CompileConfig::default()
795        };
796        assert!(
797            backend.compile_function("add", &ops, &config_m4f).is_ok(),
798            "i32 ops should work on Cortex-M4F"
799        );
800
801        // Cortex-M7DP (double FPU)
802        let config_m7dp = CompileConfig {
803            target: TargetSpec::cortex_m7dp(),
804            no_optimize: true,
805            ..CompileConfig::default()
806        };
807        assert!(
808            backend.compile_function("add", &ops, &config_m7dp).is_ok(),
809            "i32 ops should work on Cortex-M7DP"
810        );
811    }
812
813    #[test]
814    fn test_f32_rejected_on_cortex_m4_no_fpu() {
815        // Cortex-M4 (without F suffix) has no FPU
816        let backend = ArmBackend::new();
817        let ops = vec![WasmOp::F32Const(1.5), WasmOp::F32Const(2.5), WasmOp::F32Mul];
818        let config = CompileConfig {
819            target: TargetSpec::cortex_m4(),
820            no_optimize: true,
821            ..CompileConfig::default()
822        };
823
824        let result = backend.compile_function("fmul", &ops, &config);
825        assert!(
826            result.is_err(),
827            "f32 operations should fail on Cortex-M4 (no FPU)"
828        );
829    }
830
831    // ========================================================================
832    // Issue #120 — f32 ops in the optimized lowering path
833    //
834    // `OptimizerBridge::wasm_to_ir` has no handlers for f32/f64 ops, so a
835    // value-producing float op fell through to `Opcode::Nop`, leaving a
836    // downstream consumer with an unmapped vreg and tripping the PR #101
837    // defensive panic in `ir_to_arm`. Customer reproducer: `compiler_builtins
838    // float::div` and `gale_compute_ipi_mask` in the `falcon-rate-component`
839    // module.
840    //
841    // Fix: `optimize_full` declines float modules with a typed `Err`;
842    // `compile_wasm_to_arm` falls back to the non-optimized `select_with_stack`
843    // path, which handles f32 via VFP/FPU. These tests use the *default*
844    // (optimized) config — `no_optimize` is NOT set — which is the exact
845    // configuration that panicked pre-fix.
846    // ========================================================================
847
848    /// Pre-fix: this panicked with "vreg vN has no assigned ARM register and
849    /// no spill slot" inside `ir_to_arm`. Post-fix: the optimized path declines
850    /// the module and the backend falls back to direct selection, producing a
851    /// non-empty f32.div lowering on a Cortex-M4F.
852    #[test]
853    fn test_issue120_f32_div_compiles_via_optimized_default() {
854        let backend = ArmBackend::new();
855        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
856        let config = CompileConfig {
857            target: TargetSpec::cortex_m4f(),
858            // no_optimize NOT set — this exercises the optimized path that
859            // panicked in issue #120, then the fallback to direct selection.
860            ..CompileConfig::default()
861        };
862
863        let result = backend.compile_function("fdiv", &ops, &config);
864        assert!(
865            result.is_ok(),
866            "f32.div must compile on Cortex-M4F via the optimized->direct \
867             fallback (issue #120), got: {:?}",
868            result.as_ref().err()
869        );
870        assert!(
871            !result.unwrap().code.is_empty(),
872            "f32.div must produce non-empty machine code"
873        );
874    }
875
876    /// A spread of f32 ops, all through the optimized (default) config, must
877    /// compile via the fallback on an FPU target without panicking.
878    #[test]
879    fn test_issue120_assorted_f32_ops_compile_via_optimized_default() {
880        let backend = ArmBackend::new();
881        let config = CompileConfig {
882            target: TargetSpec::cortex_m4f(),
883            ..CompileConfig::default()
884        };
885
886        let cases: Vec<(&str, Vec<WasmOp>)> = vec![
887            (
888                "fadd",
889                vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Add],
890            ),
891            (
892                "fmul",
893                vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Mul],
894            ),
895            (
896                "fsub",
897                vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Sub],
898            ),
899        ];
900
901        for (name, ops) in cases {
902            let result = backend.compile_function(name, &ops, &config);
903            assert!(
904                result.is_ok(),
905                "{name} must compile via the optimized->direct fallback \
906                 (issue #120), got: {:?}",
907                result.as_ref().err()
908            );
909            assert!(
910                !result.unwrap().code.is_empty(),
911                "{name} must produce non-empty machine code"
912            );
913        }
914    }
915
916    /// The fallback must still honor the ISA feature gate: f32 on a no-FPU
917    /// target must fail cleanly (not panic) even on the optimized path.
918    #[test]
919    fn test_issue120_f32_div_rejected_on_no_fpu_via_optimized() {
920        let backend = ArmBackend::new();
921        let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
922        let config = CompileConfig {
923            target: TargetSpec::cortex_m3(),
924            ..CompileConfig::default()
925        };
926
927        let result = backend.compile_function("fdiv", &ops, &config);
928        assert!(
929            result.is_err(),
930            "f32.div must be rejected on Cortex-M3 (no FPU), not panic"
931        );
932    }
933
934    /// Issue #94: end-to-end byte-size check for the canonical u64-packed
935    /// FFI-return hi32 extract pattern. Compiles two near-identical
936    /// functions — one with the optimized shift-by-32, one with a generic
937    /// shift-by-7 — and asserts the optimized form is meaningfully smaller.
938    #[test]
939    fn test_issue94_hi32_extract_is_smaller_than_generic_shift() {
940        let backend = ArmBackend::new();
941        let config = CompileConfig {
942            target: TargetSpec::cortex_m4f(),
943            ..CompileConfig::default()
944        };
945
946        // Optimized path: `(local.get 0) >>> 32; wrap_i64`
947        let ops_hi32 = vec![
948            WasmOp::LocalGet(0), // i64 param in R0:R1
949            WasmOp::I64Const(32),
950            WasmOp::I64ShrU,
951            WasmOp::I32WrapI64,
952        ];
953        let func_hi32 = backend
954            .compile_function("hi32_extract", &ops_hi32, &config)
955            .unwrap();
956
957        // Generic path: `(local.get 0) >>> 7; wrap_i64` — same shape, but the
958        // shift amount is not a multiple of 32, so it falls through to the
959        // 38-byte runtime shift.
960        let ops_generic = vec![
961            WasmOp::LocalGet(0),
962            WasmOp::I64Const(7),
963            WasmOp::I64ShrU,
964            WasmOp::I32WrapI64,
965        ];
966        let func_generic = backend
967            .compile_function("generic_shr", &ops_generic, &config)
968            .unwrap();
969
970        let bytes_hi32 = func_hi32.code.len();
971        let bytes_generic = func_generic.code.len();
972        println!(
973            "\n[issue #94] hi32 extract: {} bytes (vs generic shift: {} bytes; saved {})",
974            bytes_hi32,
975            bytes_generic,
976            bytes_generic.saturating_sub(bytes_hi32)
977        );
978        let hex: String = func_hi32
979            .code
980            .iter()
981            .map(|b| format!("{:02x}", b))
982            .collect::<Vec<_>>()
983            .join(" ");
984        println!("[issue #94] hi32 bytes: {}", hex);
985        // We expect the optimized form to be at least 30 bytes smaller than
986        // the generic 64-bit shift sequence. (Empirically: 14 vs 50 bytes.)
987        assert!(
988            bytes_hi32 + 30 <= bytes_generic,
989            "issue #94: hi32 extract = {} bytes, generic shift = {} bytes; \
990             expected optimized form to be at least 30 bytes smaller",
991            bytes_hi32,
992            bytes_generic,
993        );
994    }
995}