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 let func_config = if params.is_some()
100 || params_f32.is_some()
101 || !func.block_arity.is_empty()
102 || declared_params.is_some()
103 {
104 Some(CompileConfig {
105 current_func_params_i64: params.cloned().unwrap_or_default(),
106 current_func_params_f32: params_f32.cloned().unwrap_or_default(),
107 current_func_block_arity: func.block_arity.clone(),
108 current_func_param_count: declared_params,
109 ..config.clone()
110 })
111 } else {
112 None
113 };
114 let cfg = func_config.as_ref().unwrap_or(config);
115 let compiled = self.compile_function(&name, &func.ops, cfg)?;
116 functions.push(compiled);
117 }
118
119 Ok(CompilationResult {
120 functions,
121 elf: None,
122 backend_name: self.name().to_string(),
123 })
124 }
125
126 fn compile_function(
127 &self,
128 name: &str,
129 ops: &[WasmOp],
130 config: &CompileConfig,
131 ) -> Result<CompiledFunction, BackendError> {
132 let (code, relocations, line_map) =
133 compile_wasm_to_arm(ops, config).map_err(BackendError::CompilationFailed)?;
134
135 Ok(CompiledFunction {
136 name: name.to_string(),
137 code,
138 wasm_ops: ops.to_vec(),
139 relocations,
140 line_map,
141 })
142 }
143
144 fn is_available(&self) -> bool {
145 true // Always available — it's a library backend
146 }
147}
148
149/// Count the number of function parameters by analyzing LocalGet patterns
150fn count_params(wasm_ops: &[WasmOp]) -> u32 {
151 let mut first_access: std::collections::HashMap<u32, bool> = std::collections::HashMap::new();
152 for op in wasm_ops {
153 match op {
154 WasmOp::LocalGet(idx) => {
155 first_access.entry(*idx).or_insert(true);
156 }
157 WasmOp::LocalSet(idx) | WasmOp::LocalTee(idx) => {
158 first_access.entry(*idx).or_insert(false);
159 }
160 _ => {}
161 }
162 }
163
164 first_access
165 .iter()
166 .filter_map(
167 |(&idx, &is_read_first)| {
168 if is_read_first { Some(idx + 1) } else { None }
169 },
170 )
171 .max()
172 .unwrap_or(0)
173}
174
175/// #539: fold the `i32.const 0; memory.grow m` idiom to `memory.size m`.
176/// `memory.grow(0)` always succeeds and returns the current page count (WASM
177/// Core §4.4.7), which is exactly `memory.size`; the fixed-memory backend
178/// otherwise emits a constant `-1` for every `memory.grow`, so the legal
179/// `memory.grow(0)` "read/validate current size" idiom wrongly reported failure.
180/// Only the ADJACENT const-0 delta is folded (a non-zero delta keeps the sound
181/// `-1` — fixed memory genuinely cannot grow; a runtime-computed 0 is a
182/// documented follow-up). Backend- and path-agnostic: `memory.size` reads the
183/// runtime memory-size register on every selector, so this fixes the optimized
184/// and direct paths at once.
185fn rewrite_memory_grow_zero(wasm_ops: &[WasmOp]) -> Vec<WasmOp> {
186 let mut out = Vec::with_capacity(wasm_ops.len());
187 let mut i = 0;
188 while i < wasm_ops.len() {
189 if matches!(wasm_ops[i], WasmOp::I32Const(0))
190 && let Some(WasmOp::MemoryGrow(m)) = wasm_ops.get(i + 1)
191 {
192 out.push(WasmOp::MemorySize(*m));
193 i += 2;
194 } else {
195 out.push(wasm_ops[i].clone());
196 i += 1;
197 }
198 }
199 out
200}
201
202/// #509: does the op stream contain a `br`/`br_if`/`br_table` that CARRIES a
203/// value — i.e. one targeting a result-typed block/if (forward edge with
204/// results > 0) or a parameterized loop header (backward edge with loop
205/// params > 0)?
206///
207/// The optimized path's wasm→IR lowering drops the carried value on such
208/// edges (the taken arm returns the fall-through result — same class as the
209/// #507 `br_table` drop, observed on `pick_br`/`pick_br_fall`), so — like
210/// #507 — the shape is detected on the raw op stream and routed to the direct
211/// selector, whose #509 designated-result-register lowering lands the value
212/// correctly. `block_arity` is the decoder's ordinal blocktype-arity
213/// side-table; when it is empty (hand-built op streams) every block reads as
214/// void and this never fires, keeping the optimized path byte-identical for
215/// every existing caller. Frozen-safe for the same reason as #507: the frozen
216/// fixtures compile `--relocatable` (already direct), and no optimized-path
217/// fixture branches to a result-typed block.
218fn has_value_carrying_branch(wasm_ops: &[WasmOp], block_arity: &[(u8, u8)]) -> bool {
219 // Open control constructs: (is_loop, params, results), innermost last.
220 let mut open: Vec<(bool, u8, u8)> = Vec::new();
221 let mut ctrl_ord = 0usize;
222 // A branch edge carries a value when its target is a result-typed forward
223 // join (block/if) or a parameterized loop header.
224 let carries = |open: &[(bool, u8, u8)], depth: u32| -> bool {
225 let Some(&(is_loop, params, results)) = open
226 .len()
227 .checked_sub(1 + depth as usize)
228 .and_then(|i| open.get(i))
229 else {
230 return false; // function-level target — handled by Return lowering
231 };
232 if is_loop { params > 0 } else { results > 0 }
233 };
234 for op in wasm_ops {
235 match op {
236 WasmOp::Block | WasmOp::If => {
237 let (p, r) = block_arity.get(ctrl_ord).copied().unwrap_or((0, 0));
238 ctrl_ord += 1;
239 open.push((false, p, r));
240 }
241 WasmOp::Loop => {
242 let (p, r) = block_arity.get(ctrl_ord).copied().unwrap_or((0, 0));
243 ctrl_ord += 1;
244 open.push((true, p, r));
245 }
246 WasmOp::End => {
247 open.pop(); // None only at the function-level end — harmless
248 }
249 WasmOp::Br(d) | WasmOp::BrIf(d) if carries(&open, *d) => return true,
250 WasmOp::BrTable { targets, default }
251 if targets
252 .iter()
253 .chain(std::iter::once(default))
254 .any(|d| carries(&open, *d)) =>
255 {
256 return true;
257 }
258 _ => {}
259 }
260 }
261 false
262}
263
264/// Core compilation: WASM ops → ARM machine code bytes + relocations
265///
266/// Returns (code_bytes, relocations) where relocations record BL instructions
267/// that target external symbols (e.g., `__meld_dispatch_import` for import calls).
268fn compile_wasm_to_arm(
269 wasm_ops: &[WasmOp],
270 config: &CompileConfig,
271) -> Result<(Vec<u8>, Vec<CodeRelocation>, LineMap), String> {
272 // #539: `memory.grow(0)` must return the CURRENT page count, not the
273 // fixed-memory `-1` sentinel — growing by zero pages can never fail (WASM
274 // Core §4.4.7), so a guest doing `if (memory.grow(0) < 0) trap;` wrongly
275 // faulted. Every lowering path emitted a delta-agnostic `-1`. `memory.grow(0)`
276 // is semantically identical to `memory.size`, which the backend already
277 // computes from the runtime memory-size register (R10 >> 16 = pages), so fold
278 // the `i32.const 0; memory.grow` idiom to `memory.size` up front — backend-
279 // and path-agnostic. A non-zero delta keeps `-1` (fixed memory genuinely
280 // cannot grow); a runtime delta that happens to be 0 is the documented
281 // follow-up.
282 let rewritten = rewrite_memory_grow_zero(wasm_ops);
283 // #494 phase 2b: the fact-spec guard-elision marks are keyed by op index
284 // into the stream the DRIVER handed us. The memory.grow(0) fold above can
285 // only shift indices AT OR AFTER a `memory.grow` — an op the fact-spec
286 // walk never crosses (it stops at the first untracked op, so no mark can
287 // follow one). Defense in depth: if the fold fired at all, drop the marks
288 // loudly rather than risk keying a guard elision to the wrong op.
289 let (fact_div_zero_elide, fact_div_ovf_elide): (&[usize], &[usize]) = if rewritten.len()
290 == wasm_ops.len()
291 {
292 (&config.fact_div_zero_elide, &config.fact_div_ovf_elide)
293 } else {
294 if !config.fact_div_zero_elide.is_empty() || !config.fact_div_ovf_elide.is_empty() {
295 eprintln!(
296 "fact-spec: DECLINE div-guard elision marks dropped — the memory.grow(0) fold shifted op indices (#494 defensive gate); general lowering emitted"
297 );
298 }
299 (&[], &[])
300 };
301 let wasm_ops: &[WasmOp] = &rewritten;
302
303 // #457: `count_params` INFERS the param count from access patterns (a local
304 // whose first access is a read is assumed to be a param), so a
305 // read-before-write NON-PARAM local — which WASM zero-initializes — was
306 // indistinguishable from a param: it got homed in a parameter register and
307 // read caller garbage instead of 0. When the driver supplied the DECLARED
308 // count (`current_func_param_count`, from the module's type section), cap
309 // the inference with it. `min` (not a plain override) keeps every function
310 // whose inference is <= declared byte-identical: the inferred count can only
311 // EXCEED the declared one via a read-first local index >= the declared count
312 // — i.e. exactly the read-before-write locals this issue is about.
313 let inferred_params = count_params(wasm_ops);
314 let num_params = match config.current_func_param_count {
315 Some(declared) => inferred_params.min(declared),
316 None => inferred_params,
317 };
318 // A read-before-write non-param local exists iff the capped count dropped.
319 // Such locals need the wasm-mandated zero-init, which only the direct
320 // selector emits — the optimized path's `ir_to_arm` maps a non-param
321 // local's vreg onto an r4+ temp with no initialization (caller garbage).
322 let has_rbw_local = num_params < inferred_params;
323
324 let bounds_config = match config.effective_safety_bounds() {
325 SafetyBounds::None => BoundsCheckConfig::None,
326 SafetyBounds::Mpu => BoundsCheckConfig::Mpu,
327 SafetyBounds::Software => BoundsCheckConfig::Software,
328 SafetyBounds::Mask => {
329 // #651 (mirroring the RISC-V backend's compile-time decline):
330 // index masking wraps `ea & (size-1)` — a modulo only when the
331 // linear-memory size is a power of two. With a non-power-of-two
332 // size the AND would silently REMAP in-bounds addresses (e.g.
333 // 0x18000 & 0x2FFFF = 0x8000 for a 192 KiB memory). Decline
334 // loudly rather than miscompile. `linear_memory_bytes == 0`
335 // means "unknown" (plain per-function path, no module context)
336 // — the startup default of one 64 KiB page is a power of two.
337 let bytes = config.linear_memory_bytes;
338 if bytes != 0 && !bytes.is_power_of_two() {
339 return Err(format!(
340 "--safety-bounds mask requires a power-of-two linear-memory \
341 size, got {bytes} bytes — switch to --safety-bounds software \
342 for the deterministic check (#651)"
343 ));
344 }
345 BoundsCheckConfig::Masking
346 }
347 };
348
349 // The non-optimized (direct) instruction-selection path. Handles f32 via
350 // VFP/FPU. Used directly when `--no-optimize` is set, and as the fallback
351 // when the optimized path declines a module (see issue #120 below).
352 //
353 // VCR-RA-001 step 3b-lite (#242): a FRESH selector per attempt, with
354 // `spill_on_exhaustion` set only on the retry — the first pass is the
355 // unmodified default, so every function that compiles today is selected by
356 // exactly the code that compiled it yesterday (bit-identity is structural,
357 // not behavioural).
358 let select_direct_attempt = |spill_on_exhaustion: bool,
359 param_backing_on_exhaustion: bool,
360 local_promote: bool,
361 i64_spill_slots: Option<usize>|
362 -> Result<Vec<ArmInstruction>, synth_core::Error> {
363 let db = RuleDatabase::with_standard_rules();
364 let mut selector =
365 InstructionSelector::with_bounds_check(db.rules().to_vec(), bounds_config);
366 selector.set_target(config.target.fpu, &config.target.triple);
367 if config.num_imports > 0 {
368 selector.set_num_imports(config.num_imports);
369 }
370 // #195: plumb the callee argument-count tables so the direct selector can
371 // marshal call arguments into R0–R3 per AAPCS.
372 selector.set_func_arg_counts(
373 config.func_arg_counts.clone(),
374 config.type_arg_counts.clone(),
375 );
376 // #197: in relocatable host-link mode, emit direct `func_N` BLs for
377 // imports (rewritten to the wasm field name by build_relocatable_elf)
378 // instead of `__meld_dispatch_import`.
379 selector.set_relocatable(config.relocatable);
380 // #642: call_indirect guard inputs (compile-time table size for the
381 // bounds guard + closed-world type verdicts). Without them, every
382 // call_indirect lowering declines loudly.
383 selector.set_call_indirect_guards(config.call_indirect_guards.clone());
384 // #275: on the self-contained image path (NOT --relocatable) decline
385 // call_indirect loudly — the R11 funcref-table region is only populated
386 // by an external runtime, which a self-contained ELF does not have, so
387 // the dispatch would read function pointers from linear-memory data (a
388 // silent miscompile). The host-linked (--relocatable) path keeps the
389 // guarded dispatch: there a runtime places the table region at R11.
390 selector.set_reject_self_contained_call_indirect(!config.relocatable);
391 // #237: native-pointer ABI — wasm statics become __synth_wasm_data-relative.
392 selector.set_native_pointer_abi(config.native_pointer_abi, config.linear_memory_bytes);
393 // #311: i64 call results are register PAIRS — tag them.
394 selector.set_result_types(config.func_ret_i64.clone(), config.type_ret_i64.clone());
395 // #359: declared param widths of THIS function, so the AAPCS stack-arg
396 // path can refuse 64-bit params (Ok-or-Err). Empty ⇒ assume i32.
397 selector.set_params_i64(config.current_func_params_i64.clone());
398 // GI-FPU-002 (#619/#369): declared f32-param mask — home hard-float f32
399 // args in S0..S15 (AAPCS-VFP) instead of the R0..R3 integer path.
400 selector.set_params_f32(config.current_func_params_f32.clone());
401 // #509: blocktype-arity side-table of THIS function, so value-carrying
402 // br/br_if/br_table land the carried value in the target block's
403 // designated result register instead of dropping it. Empty ⇒ legacy
404 // void-block lowering.
405 selector.set_block_arity(config.current_func_block_arity.clone());
406 // Stack-pointer promotion is meaningful only under the native-pointer ABI;
407 // gating here keeps every non-native compile (all frozen fixtures) on the
408 // legacy R9 globals-table path, bit-identical.
409 if config.native_pointer_abi
410 && let Some((sp_idx, sp_init)) = config.stack_pointer_global
411 {
412 selector.set_native_pointer_stack(sp_idx, sp_init);
413 }
414 // #643: per-global slot widths — i64/f64 globals occupy 8-byte slots
415 // (register-pair store/load) and shift every later global's offset.
416 // Empty for i32-only modules ⇒ the legacy `idx * 4` layout, unchanged.
417 selector.set_global_widths(config.global_widths.clone());
418 selector.set_spill_on_exhaustion(spill_on_exhaustion);
419 selector.set_param_backing_on_exhaustion(param_backing_on_exhaustion);
420 // #587 pool-grow rung: a larger i64 spill-slot pool, set ONLY on the
421 // retry after an attempt failed with the slot-pool-exhausted Err —
422 // functions that compile with the default pool keep their frame
423 // byte-identical by construction.
424 if let Some(slots) = i64_spill_slots {
425 selector.set_i64_spill_slots(slots);
426 }
427 // VCR-RA local promotion (#390, #242): keep eligible non-param i32 locals
428 // in callee-saved registers instead of frame slots — the structural lever
429 // toward native parity. DEFAULT-ON as of v0.14.0: gale's G474RE DWT gate
430 // cleared it as a net win (gust_mix dissolved 58→50 cyc/call −14%, all 5
431 // stack spill/reloads eliminated, correctness bit-identical over [0,2047],
432 // 2.00×→1.72× vs LLVM). Escape hatch: `SYNTH_NO_LOCAL_PROMOTE=1` restores
433 // the frame-slot path. Leaf-only / i32-only / ARM-only (see
434 // compute_local_promotion); the leaf-only lift + i64 locals are follow-ons.
435 // #474: `local_promote` is now a per-attempt parameter so the retry ladder
436 // can drop promotion as an exhaustion-recovery rung (promotion pins r4-r8,
437 // which on a dense function leaves the spill allocator with nothing to
438 // free → the frame-slot path is the escape that restores compilability).
439 selector.set_local_promote(local_promote);
440 // #494 phase 2b: certificate-discharged div/rem trap-guard elision
441 // marks (empty in every compile without SYNTH_FACT_SPEC + facts).
442 selector
443 .set_fact_div_guard_elisions(fact_div_zero_elide.to_vec(), fact_div_ovf_elide.to_vec());
444 selector.select_with_stack(wasm_ops, num_params)
445 };
446 let select_direct = || -> Result<Vec<ArmInstruction>, String> {
447 const SINGLE_EXHAUSTION: &str = "all allocatable registers are live on the stack";
448 const PAIR_EXHAUSTION: &str = "no consecutive pair of free registers for i64";
449 const SLOT_EXHAUSTION: &str = "i64 spill-slot pool exhausted";
450 // The full exhaustion-recovery ladder, parameterized on whether local
451 // promotion is enabled. Each rung is reached only when the previous one
452 // returned a recoverable register-exhaustion Err, so a function that
453 // compiles on the first attempt is untouched by the later rungs. Returns
454 // the result AND which rung produced it (for the #242 measurement below).
455 let recovery_ladder =
456 |promote: bool,
457 i64_spill_slots: Option<usize>|
458 -> (Result<Vec<ArmInstruction>, synth_core::Error>, &'static str) {
459 let mut attempt = select_direct_attempt(false, false, promote, i64_spill_slots);
460 let mut rung = "base";
461 // VCR-RA-001 step 3b-lite (#242): the i32 register-exhaustion
462 // hard-fail is recoverable — retry with spill-on-exhaustion, which
463 // reserves the spill area and spills the deepest stack value when
464 // the pool is full.
465 if let Err(e) = &attempt
466 && e.to_string().contains(SINGLE_EXHAUSTION)
467 {
468 attempt = select_direct_attempt(true, false, promote, i64_spill_slots);
469 rung = "spill";
470 }
471 // VCR-RA-001 acceptance increment (#242): the i64 consecutive-PAIR
472 // exhaustion is recoverable too — not by stack spilling (the pair
473 // allocator already spills stack values, #171) but by frame-backing
474 // the params (#204) so they stop pinning R0-R3, with spill kept on.
475 if let Err(e) = &attempt
476 && e.to_string().contains(PAIR_EXHAUSTION)
477 {
478 attempt = select_direct_attempt(true, true, promote, i64_spill_slots);
479 rung = "param-backing";
480 }
481 (attempt, rung)
482 };
483 // #474: local promotion (default-on since v0.14.0) is an OPTIMIZATION — it
484 // must never be the reason a function fails to compile. Run the full ladder
485 // with promotion first (so every function that compiles today is
486 // bit-identical), and if it still ends in register exhaustion, fall back to
487 // the promotion-off ladder (the v0.12.0 frame-slot lowering — exactly what
488 // the `SYNTH_NO_LOCAL_PROMOTE=1` workaround does, now automatic). Promotion
489 // pins r4-r8 for the locals; on a dense function that leaves the allocator
490 // with nothing to free, so dropping it restores compilability. The fallback
491 // is reached ONLY by functions that exhaust WITH promotion, so promotion-on
492 // output is untouched by construction (frozen byte gate stays green).
493 let promote = std::env::var("SYNTH_NO_LOCAL_PROMOTE").is_err();
494 // The full pre-#587 recovery sequence (promotion-on ladder, then the
495 // #474 promotion-off fallback), parameterized on the pool size so the
496 // pool-grow retry below reruns it verbatim.
497 let full_sequence = |slots: Option<usize>| -> (
498 Result<Vec<ArmInstruction>, synth_core::Error>,
499 &'static str,
500 bool,
501 ) {
502 let (mut attempt, mut rung) = recovery_ladder(promote, slots);
503 let mut promotion_dropped = false;
504 if promote
505 && attempt
506 .as_ref()
507 .err()
508 .is_some_and(|e| e.to_string().contains("register exhaustion"))
509 {
510 let (rescued, off_rung) = recovery_ladder(false, slots);
511 if rescued.is_ok() {
512 attempt = rescued;
513 rung = off_rung;
514 promotion_dropped = true;
515 }
516 }
517 (attempt, rung, promotion_dropped)
518 };
519 let (mut attempt, mut rung, mut promotion_dropped) = full_sequence(None);
520 // #587 pool-grow retry (the falcon func_60/func_73 remainder): the fixed
521 // 8-slot i64 spill pool can exhaust while spilling is otherwise working —
522 // an i64-dense function simply has more values simultaneously live than
523 // the pool holds. Rerun the ENTIRE sequence (every rung, both promotion
524 // modes) with the pool sized from a conservative operand-stack-depth
525 // bound: the number of simultaneously spilled values can never exceed
526 // the operand-stack depth, plus a few transient slots (the arg-move
527 // cycle resolver and call-result parking each borrow one). The selector
528 // clamps the request to its 12-bit-friendly cap; a function that still
529 // exhausts stays an honest loud skip. Deliberately LAST — after the #474
530 // promotion-off fallback — so any function that compiled yesterday
531 // (through any rung or fallback) is produced by exactly yesterday's
532 // path, byte-identical; the grown pool only ever fires for functions
533 // whose every existing escape ended in the slot-pool Err.
534 if attempt
535 .as_ref()
536 .err()
537 .is_some_and(|e| e.to_string().contains(SLOT_EXHAUSTION))
538 {
539 let depth = synth_core::wasm_stack_check::max_depth_bound(wasm_ops) as usize;
540 let (grown, _, grown_dropped) = full_sequence(Some(depth.saturating_add(4)));
541 if grown.is_ok() {
542 attempt = grown;
543 rung = "pool-grow";
544 promotion_dropped = grown_dropped;
545 }
546 }
547 // VCR-RA measurement (#242): log which recovery rung produced the result,
548 // so the per-rung distribution across a corpus can be measured — the size
549 // of the failure surface a verified allocator must subsume (see
550 // scripts/repro/register_exhaustion_recovery_ladder.md). Logging only:
551 // emitted bytes are unchanged, so the frozen byte gate is unaffected.
552 if std::env::var("SYNTH_RECOVERY_STATS").is_ok() {
553 eprintln!(
554 "[recovery-stats] rung={rung}{} result={}",
555 if promotion_dropped {
556 " promotion-off"
557 } else {
558 ""
559 },
560 if attempt.is_ok() { "ok" } else { "exhausted" },
561 );
562 }
563 attempt.map_err(|e| format!("instruction selection failed: {}", e))
564 };
565
566 // Instruction selection: optimized or direct.
567 //
568 // #197: `--relocatable` (host-link ET_REL) forces the direct selector. The
569 // optimized path materializes an absolute linmem base (0x20000100) and does
570 // not preserve caller-saved registers across calls — both wrong for a
571 // host-linked object, where the linmem base arrives via `fp` at runtime and
572 // callees follow AAPCS. `select_with_stack` (now i64-spill capable after
573 // #171) handles fp-relative memory + caller-saved preservation correctly.
574 //
575 // #507: `br_table` is DROPPED during the optimized path's wasm→IR lowering
576 // (`optimize_full`), so `ir_to_arm` never sees the dispatch — it emits the
577 // arm bodies in fall-through sequence with no `cmp`/branch on the selector, a
578 // SILENT miscompile (every input hits the last arm). The selector value isn't
579 // even loaded. Because the drop happens before `ir_to_arm`, there's no `Err`
580 // to fall back on; detect it on the raw wasm op stream here and force the
581 // direct selector (`select_with_stack` lowers `br_table` correctly as a
582 // cmp-chain — confirmed on the `--relocatable` path). Same honest-degradation
583 // contract as the issue-#120 f32 decline: the function still compiles
584 // correctly, just without IR-level optimization. Frozen-safe: the frozen
585 // fixtures compile `--relocatable` (already direct), and no optimized-path
586 // fixture (control_step, flight_algo) contains `br_table`.
587 let has_br_table = wasm_ops
588 .iter()
589 .any(|op| matches!(op, WasmOp::BrTable { .. }));
590 // #509: the optimized path also drops the value carried by a `br`/`br_if`
591 // to a result-typed block (the taken edge returns the wrong arm's value —
592 // same silent-miscompile class as the #507 br_table drop). Route the shape
593 // to the direct selector, whose designated-result-register lowering (#509)
594 // lands the carried value at the join. Never fires for void-block control
595 // flow (all frozen/optimized fixtures), so those stay byte-identical.
596 let has_value_carry = has_value_carrying_branch(wasm_ops, &config.current_func_block_arity);
597 // #503-i64/#518: route any signature with a 64-bit (i64/f64) param to the
598 // direct selector. The optimized path's param homing is width-naive — its
599 // #518 decline covers only functions that READ an i64 param (an `I64Load`
600 // from a param index), so a function that reads an i32 param whose AAPCS
601 // home a preceding wide param SHIFTED (e.g. p1 of `(i64 i32)` lives in R2,
602 // not R1; p3 of `(i64 i32 i32 i32)` lives on the stack, not in R3) was
603 // silently miscompiled rather than falling back. The direct selector's
604 // `aapcs_param_layout` homing handles every such shape (i64-param READS
605 // already fell back to it via the ir_to_arm Err, so those functions emit
606 // the same bytes as before). `num_params` counts read-first locals, so a
607 // function that never touches any param keeps the optimized path.
608 let has_wide_param = config
609 .current_func_params_i64
610 .iter()
611 .take(num_params as usize)
612 .any(|&w| w);
613 // #494 phase 2b: div/rem guard-elision marks are consumed by the DIRECT
614 // selector only — the optimized path's IR passes (const-fold/CSE/DCE)
615 // renumber instructions, so an op-index-keyed mark cannot soundly survive
616 // them. Route marked functions direct (the #507/#509 honest-degradation
617 // pattern). Never fires without SYNTH_FACT_SPEC + facts + a discharged
618 // obligation, so every existing compile keeps its path byte-identical.
619 let has_fact_div_elide = !fact_div_zero_elide.is_empty() || !fact_div_ovf_elide.is_empty();
620 // #643: the optimized path's global lowering is width-naive — `GlobalGet`/
621 // `GlobalSet` are single-word `[R9, idx*4]` accesses, which (a) silently
622 // dropped the high word of every i64 global and (b) mis-address every
623 // global whose offset an earlier wide (i64/f64) slot shifted. When the
624 // module has any wide global, route every global-touching function to the
625 // direct selector, whose type-aware summed layout pairs the access (or
626 // declines loudly). Modules with only 4-byte globals — every existing
627 // fixture — keep the optimized path byte-identical.
628 let has_wide_global_module = config.global_widths.iter().any(|&w| w > 4);
629 let has_global_access = has_wide_global_module
630 && wasm_ops
631 .iter()
632 .any(|op| matches!(op, WasmOp::GlobalGet(_) | WasmOp::GlobalSet(_)));
633 // VCR-VER-001 (#242): `post_exhaust` scopes the post-exhaustion cleanup
634 // extensions to functions whose bytes the #580 spill-on-exhaustion
635 // machinery actually shaped (bridge-reported). Everything else — the
636 // direct path, non-exhausted optimized functions — stays byte-identical
637 // flag-on (the `vcr_ver_001_gate_242` lock's contract).
638 let (arm_instrs, post_exhaust) = if config.no_optimize
639 || config.relocatable
640 || has_br_table
641 || has_value_carry
642 || has_wide_param
643 || has_global_access
644 || has_fact_div_elide
645 // #457: route read-before-write non-param locals to the direct
646 // selector, whose prologue zero-init lands the wasm-mandated 0.
647 || has_rbw_local
648 {
649 if std::env::var("SYNTH_PATH_DEBUG").is_ok() {
650 eprintln!("[path-debug] direct (pre-gate)");
651 }
652 (select_direct()?, false)
653 } else {
654 let opt_config = if config.loom_compat {
655 OptimizationConfig::loom_compat()
656 } else {
657 OptimizationConfig::all()
658 };
659
660 let mut bridge = OptimizerBridge::with_config(opt_config);
661 // #188: tell the bridge how many imports there are so it declines only
662 // LOCAL calls (and leaves import calls on the optimized path, keeping
663 // the #173 field-name relocation rewrite intact).
664 bridge.set_num_imports(config.num_imports);
665 // #543 Phase 2: thread the integrator-marked volatile DMA-window ranges
666 // (`--volatile-segment <base>:<len>`) to the bridge's address-caching
667 // levers — base-CSE (#468) excludes any access inside a marked range
668 // from its fold set, and the bridge-level const-CSE declines wholesale
669 // while any range is marked. Empty (the default) ⇒ byte-identical.
670 bridge.set_volatile_segments(config.volatile_segments.clone());
671 // #377: thread `--safety-bounds` to the bridge. Pre-fix the optimized
672 // path ignored it — `software`/`mask` were SILENT NO-OPS on the path
673 // that lowers the bulk of a flight loop's i32 loads/stores (byte-
674 // identical to `none`, while the safety manifest claimed otherwise).
675 // `Software` now emits the inline guard per access; `Masking` declines
676 // memory-accessing functions to the direct selector; `None`/`Mpu` are
677 // byte-identical to before.
678 bridge.set_bounds_check(bounds_config);
679 // #687: thread the absolute linear-memory base the optimized path
680 // materializes. Defaults to 0x2000_0100 (byte-identical);
681 // `--stack-layout=low` shifts it up by the reserved stack size so
682 // const-address accesses follow the moved linear memory.
683 bridge.set_linmem_base(config.linmem_base);
684 // `ir_to_arm` now returns `Result` — an `Err` means the optimized path
685 // hit an unmapped vreg (issue-#93-class). Treat it identically to an
686 // `optimize_full` failure: fall back to the direct selector rather
687 // than propagating, so the function still compiles correctly.
688 match bridge
689 .optimize_full(wasm_ops)
690 .and_then(|(opt_ir, _cfg, _stats)| bridge.ir_to_arm(&opt_ir, num_params as usize))
691 {
692 Ok(arm_ops) => {
693 if std::env::var("SYNTH_PATH_DEBUG").is_ok() {
694 eprintln!("[path-debug] optimized (ir_to_arm ok)");
695 }
696 (
697 arm_ops
698 .into_iter()
699 .map(|op| ArmInstruction {
700 op,
701 source_line: None,
702 })
703 .collect(),
704 bridge.spill_on_exhaust_fired(),
705 )
706 }
707 // Issue #120: the optimized path declines modules it cannot lower
708 // (notably scalar f32/f64 ops — the IR has no float opcodes). Fall
709 // back to the direct instruction selector, which handles f32 via
710 // VFP/FPU. This is honest degradation: the function still compiles
711 // correctly, just without IR-level optimization.
712 Err(e) => {
713 if std::env::var("SYNTH_PATH_DEBUG").is_ok() {
714 eprintln!("[path-debug] direct (fallback: {e})");
715 }
716 (select_direct()?, false)
717 }
718 }
719 };
720
721 // #257/#277: `mul`+`add`→`mla` fusion is intentionally NOT wired here.
722 // The transform is correct and ready (`synth_synthesis::liveness::fuse_mul_add`,
723 // fully tested), but it is **register-allocation-coupled**: over the current
724 // greedy single-pass selector, folding `mul rM,..; add rD,rM,rX` → `mla`
725 // extends the live ranges of the mul inputs to the mla point, and the added
726 // pressure (extra moves/spills) costs more than the single-cycle MLA saves —
727 // gale measured a +2 cyc on-target REGRESSION (flat_flight 255→257, G474RE)
728 // even though it removes 2 instructions and the seam stays 0x07FDF307. So the
729 // fusion stays unwired until the spill-aware allocator (VCR-RA-001) chooses
730 // registers, at which point it becomes net-positive (per #272's plan and the
731 // wiring design note). Lesson (#277): a register-pressure-affecting transform
732 // needs an on-target/allocator-aware gate, not a byte-count gate, before it
733 // can default on.
734
735 // VCR-RA-001 const-CSE / rematerialization-avoidance (#209): moved to run
736 // LAST, after the immediate-folds — see the apply_const_cse call below
737 // (#242). Earlier it ran here (before range-realloc and the folds), which is
738 // what let it grow gale's --relocatable `gust_mix` 90→92 B (#242 burndown,
739 // 2026-06-26): retargeting a read defeated a *downstream* immediate-fold that
740 // would otherwise have absorbed the constant. Running CSE-last makes those
741 // foldable consts already-folded-and-gone, so CSE only ever touches genuinely
742 // redundant materializations.
743
744 // VCR-RA-001 RANGE RE-ALLOCATION (#209/#242, wiring step 3a) — the first
745 // CONSEQUENTIAL allocator pass: re-colour each maximal straight-line
746 // segment over the R0-R8 pool with value ranges as the allocation unit
747 // (segment inputs + per-register live-outs pinned to their original
748 // registers, reserved R9-R12/SP identity-assigned — each segment is
749 // independently sound, no cross-segment liveness assumed). Renames
750 // registers only: never adds, removes, or reorders instructions, so
751 // labels/branch offsets are unaffected.
752 //
753 // DEFAULT-ON since v0.11.36: gale cleared the gate on-target (G474RE,
754 // #209 2026-06-10) — flag-on output byte-identical to flag-off on
755 // flat_flight/controller/control_step, fires on the filter family with
756 // zero cycle delta and a small size win, all selfchecks green on silicon.
757 // Opt out with `SYNTH_RANGE_REALLOC=0`; per-function stats with
758 // `SYNTH_REALLOC_STATS=1`.
759 //
760 // The companion dead callee-saved-save elimination (gale's "next
761 // consequential lever", same issue comment) then shrinks the prologue
762 // `push {r4-r8,lr}` / epilogue `pop {r4-r8,pc}` to the callee-saved
763 // registers the re-allocated body still touches (leaf-only,
764 // SP-untouched, even-count-padded — see shrink_callee_saved_saves):
765 // ~12 cycles of pure save/restore overhead removed on small leaves.
766 let realloc_on = std::env::var("SYNTH_RANGE_REALLOC").map_or(true, |v| v != "0");
767 let arm_instrs = if realloc_on {
768 use synth_synthesis::rules::Reg;
769 const POOL: [Reg; 9] = [
770 Reg::R0,
771 Reg::R1,
772 Reg::R2,
773 Reg::R3,
774 Reg::R4,
775 Reg::R5,
776 Reg::R6,
777 Reg::R7,
778 Reg::R8,
779 ];
780 // VCR-VER-001 (#242): on a function the spill-on-exhaustion machinery
781 // shaped, the terminal segment gets relaxed live-out pinning (only
782 // R0/R1 are observable past `bx lr` at this pre-prologue position) so
783 // the colourer can lower R4-R8-homed tails into caller-saved R0-R3 —
784 // shrinking the `push {r4-r8,lr}` the #580 exhaustion shapes pay for.
785 // `post_exhaust == false` selects the shipping pass bit for bit.
786 let (out, stats) = synth_synthesis::liveness::reallocate_function_post_exhaust(
787 &arm_instrs,
788 &POOL,
789 post_exhaust,
790 );
791 if std::env::var("SYNTH_REALLOC_STATS").is_ok() {
792 eprintln!(
793 "[range-realloc] {} segments: {} reallocated, {} declined ({} validator-rejected), {} need spill (step 4)",
794 stats.segments,
795 stats.reallocated,
796 stats.declined,
797 stats.validator_rejects,
798 stats.needs_spill
799 );
800 }
801 // VCR-RA-002 (#390, epic #242): eliminate a provably-dead stack frame
802 // (`sub sp,#N`/`add sp,#N` reserved by `compute_local_layout` for locals
803 // that promotion homed in registers, never accessed). Removing it saves
804 // the two instructions AND restores the SP-untouched precondition that
805 // `shrink_callee_saved_saves` requires — so it must run FIRST.
806 // DEFAULT-ON (#242 flag audit flip-wave, #592 audit item): evidence
807 // basis was the 2-path × repro-corpus sweep — 0 functions grow, 58
808 // shrink (flight_seam controller_step 250→242 −8 / filter_step 180→168
809 // −12, native_pointer frame_roundtrip 46→34 −12), locked by the
810 // `dead_frame_elim_no_grow_corpus_242` cargo gate; execution
811 // differentials re-run green on the new default bytes BEFORE the
812 // frozen ARM anchors were re-pinned (leaf_dead_frame, flight_seam,
813 // frame_slot_dce — see the flip PR). Escape hatch:
814 // `SYNTH_DEAD_FRAME_ELIM=0` opts out and restores the pre-flip bytes
815 // (CI-gated in `frozen_codegen_bytes.rs`).
816 let out = if !std::env::var("SYNTH_DEAD_FRAME_ELIM").is_ok_and(|v| v == "0") {
817 synth_synthesis::liveness::elide_dead_frame(&out).unwrap_or(out)
818 } else {
819 out
820 };
821 // #490 (epic #242): the optimized selector uses r4-r8 as scratch /
822 // promoted locals but emits no prologue, silently clobbering a caller's
823 // callee-saved registers. Add the missing `push {r4-r8,lr}` /
824 // `pop {r4-r8,pc}` HERE — on the post-realloc body, where realloc has
825 // lowered low-pressure r4-r8 scratch back to r0-r3, so a save is added
826 // only for registers genuinely clobbered. `shrink_callee_saved_saves`
827 // (next) then trims it to the used set. No-op on the direct path (it
828 // already has its own prologue) and on callee-saved-free leaves.
829 let out = synth_synthesis::liveness::ensure_callee_saved_prologue(&out);
830 synth_synthesis::liveness::shrink_callee_saved_saves(&out).unwrap_or(out)
831 } else {
832 // Range-realloc off (`SYNTH_RANGE_REALLOC=0`): the optimized path still
833 // must preserve the callee-saved registers it clobbers (#490). No shrink
834 // (it is coupled to the realloc lever), so the conservative full save
835 // stays — correct, just not minimised in this debug configuration.
836 synth_synthesis::liveness::ensure_callee_saved_prologue(&arm_instrs)
837 };
838
839 // VCR-RA-001 SHADOW ALLOCATION (#209/#242): run the register allocator on
840 // the selected stream and LOG what it finds — without changing a single
841 // emitted byte. This is the measure-only bridge between the built analysis
842 // layer and the eventual virtual-register wiring: it shows, per real
843 // function, whether the allocator can colour it within the R0–R8 pool and
844 // how much const-CSE / rematerialization headroom exists (#209). Enable with
845 // `SYNTH_SHADOW_ALLOC=1`; off by default and side-effect-free either way.
846 if std::env::var("SYNTH_SHADOW_ALLOC").is_ok() {
847 use synth_synthesis::liveness::{
848 AllocationOutcome, allocate_function, function_peak_pressure,
849 };
850 // R9 globals / R10 mem-size / R11 mem-base / R12 IP-scratch are reserved;
851 // pin them above the 0..9 allocatable pool so the colourer keeps R0–R8.
852 let precolored = std::collections::BTreeMap::from([
853 (synth_synthesis::rules::Reg::R9, 9usize),
854 (synth_synthesis::rules::Reg::R10, 10),
855 (synth_synthesis::rules::Reg::R11, 11),
856 (synth_synthesis::rules::Reg::R12, 12),
857 ]);
858 // True VALUE pressure (one node per value, not per reused physical reg):
859 // a NeedsSpill with peak ≤ 9 is a SPURIOUS physical-register spill — the
860 // function fits once virtually allocated.
861 let peak = function_peak_pressure(&arm_instrs);
862 match allocate_function(&arm_instrs, 9, &precolored) {
863 AllocationOutcome::Allocated {
864 remat_opportunities,
865 coloring,
866 } => eprintln!(
867 "[shadow-alloc] OK: {} pregs coloured within R0-R8 pool, peak value-pressure {}, {} const-CSE/remat opportunities",
868 coloring.len(),
869 peak,
870 remat_opportunities
871 ),
872 AllocationOutcome::NeedsSpill(s) => eprintln!(
873 "[shadow-alloc] physical-graph would spill {:?}, but peak value-pressure is {} (≤9 ⇒ spurious; fits once virtually allocated)",
874 s, peak
875 ),
876 AllocationOutcome::Declined => {
877 eprintln!(
878 "[shadow-alloc] declined (unmodeled construct — calls/i64/fp/offset-branch)"
879 )
880 }
881 }
882 }
883
884 // VCR-SEL-004 cmp→select → IT-block predication fusion (#242). The selector
885 // lowers a `select` whose condition is a comparison to a *materialize then
886 // re-test* sequence (`cmp a,b; SetCond D,c; cmp D,#0; movne dst,v1; moveq
887 // dst,v2`); this collapses it onto the comparison's own flags — deleting the
888 // `SetCond` and the `cmp D,#0` and retargeting the predicated moves to `c` /
889 // `invert(c)` — yielding the textbook predicated clamp (`cmp a,b; movc dst,v1;
890 // mov{!c} dst,v2`). −2 instructions per fused select. gale #428 measured this
891 // as the #1 hot-path size/cycle lever on the gust_mix clamp chain.
892 //
893 // Run LATE: after range re-allocation (so the dead-D proof sees final register
894 // identities) and before encode. Removal-only + rename-only ⇒ no spill
895 // regression and labels/branch offsets are unaffected. Each fusion is proven
896 // sound (flags reused only when nothing clobbers them in the window; the
897 // boolean deleted only when provably dead) — see `fuse_cmp_select`.
898 //
899 // DEFAULT-ON as of v0.13.0 (#428): cmp→select fusion ships by default. The
900 // byte-changing flip is validated by (a) the unicorn execution oracle that runs
901 // the two-move `mov{invert(c)}` arm (cmp_select_two_move_differential.py), (b)
902 // gale's gale_decider_diff 10,596-case sweep across all 8 verified primitives
903 // (native ≡ flag-off ≡ flag-on = 0x88e73178d232bcf5), and (c) the named-anchor
904 // differentials re-run with fusion ON — control_step still 0x00210A55, flat+
905 // inlined flight_algo still 0x07FDF307 (results preserved; bytes deliberately
906 // changed, re-frozen on this commit). Escape hatch: `SYNTH_NO_CMP_SELECT_FUSE=1`
907 // reverts to the pre-fusion lowering. The on-silicon G474RE DWT no-regression
908 // check is a tracked post-ship follow-up (gale owns it).
909 let arm_instrs = if std::env::var("SYNTH_NO_CMP_SELECT_FUSE").is_err() {
910 // The rewritten stream is identical to `fuse_cmp_select`'s 2-tuple form;
911 // the extra `two_move` count is diagnostic only (the fusion census /
912 // blast-radius datum — #7 made that arm reachable).
913 let (out, fused, two_move) =
914 synth_synthesis::liveness::fuse_cmp_select_with_stats(&arm_instrs);
915 if std::env::var("SYNTH_FUSE_STATS").is_ok() {
916 let in_place = fused - two_move;
917 eprintln!(
918 "[cmp-select-fuse] {fused} select(s) fused to predicated moves \
919 ({two_move} two-move, {in_place} in-place)"
920 );
921 }
922 out
923 } else {
924 arm_instrs
925 };
926
927 // Perf lever 1 toward native parity (#390): redundant stack-reload elimination.
928 // synth lowers every wasm local to a frame slot, so `local.set; local.get` emits
929 // `str rX,[sp,#N]; … ; ldr rY,[sp,#N]`; when rX still holds the value the reload
930 // (a ~2-cycle M4 load) becomes `mov rY,rX`. Removal-of-a-load + rename only ⇒ no
931 // new instruction form and no label/offset change. DEFAULT-ON (#242 feature
932 // loop): validated bit-identical RESULTS on every frozen anchor (control_step
933 // 0x00210A55 13/13, flat+inlined flight_algo 0x07FDF307) with .text reduced on
934 // the shipped --relocatable path, plus 8 unit tests + the frame_slot_dce
935 // execution differential — the same gated path cmp→select took to default-on in
936 // v0.13.0 (G474RE silicon confirms perf post-ship). Escape hatch:
937 // `SYNTH_NO_STACK_FWD=1` restores the frame-resident bytes (frozen-old goldens).
938 let stack_fwd = std::env::var("SYNTH_NO_STACK_FWD").is_err();
939 let arm_instrs = if stack_fwd {
940 let (out, fwd) = synth_synthesis::liveness::forward_stack_reloads(&arm_instrs);
941 if std::env::var("SYNTH_FUSE_STATS").is_ok() {
942 eprintln!("[stack-fwd] {fwd} stack reload(s) forwarded to register moves");
943 }
944 out
945 } else {
946 arm_instrs
947 };
948
949 // VCR-RA frame-slot DCE (#242): once `forward_stack_reloads` has turned the
950 // reloads of a spill slot into register moves, the `str rX,[sp,#N]` that fed
951 // them is a dead store — its slot is never loaded again. Remove it. Pairs
952 // with (and only pays after) stack-reload forwarding, so it shares the flag.
953 let arm_instrs = if stack_fwd {
954 let (out, n) = synth_synthesis::liveness::eliminate_dead_frame_stores(&arm_instrs);
955 if std::env::var("SYNTH_FUSE_STATS").is_ok() {
956 eprintln!("[frame-slot-dce] {n} dead frame store(s) removed");
957 }
958 out
959 } else {
960 arm_instrs
961 };
962
963 // VCR-RA-001 spill re-choice (#242), two stages behind one flag.
964 // Stage 1 (the #569 spike): slot-value forwarding BETWEEN reloads.
965 // `forward_stack_reloads` (above) forwards only from a spill store's
966 // SOURCE register, so when register pressure clobbers that source its
967 // reloads survive; this stage tracks which registers provably still hold
968 // a frame slot's value (through earlier reloads and reg-reg moves) and
969 // turns reload #2..#n into a 1-cycle `mov` (or deletes it when the target
970 // already holds the value). Stage 2 (the Belady re-choice): where NO
971 // register still holds the value — the genuine-spill case, flat_flight's
972 // peak-11 hot segment — the value was usually evicted while a dead
973 // register existed; the clobbering def(s) are renamed onto a provably-dead
974 // register (`spill_rechoice_segment`) so the value stays resident and the
975 // reload dissolves outright. A dissolved reload can leave the feeding
976 // store dead, so the frame-slot DCE sweep runs once more behind the same
977 // flag. Per-segment commit gates: executable same-value-flow trace
978 // equality, strict shrink, pool-pressure fit, sub-word/unknown-slot
979 // conservatism (see `apply_spill_realloc` / `spill_rechoice_segment`).
980 // Stage 3 (whole-function slot liveness): the segment-local DCE keeps a
981 // store whose slot reaches function end ("reach-end ≠ dead" — it cannot
982 // see other segments); `eliminate_unread_frame_stores` walks the whole
983 // function (labels/branches/loops, SP-displacement tracked) and drops a
984 // store whose slot NO reachable instruction can read — flat_flight's two
985 // surviving stores (#576), completing Belady's 0-load side with a 0-store
986 // side. Same flag: the three stages are one lever, flipped together.
987 // DEFAULT-ON (#242 feature loop, the v0.14.0 local-promotion pattern):
988 // Belady spilling ships by default. Evidence basis for the flip: three
989 // landed flag-off increments (#569 forwarding, #576 Belady re-choice,
990 // #579 whole-fn slot liveness), 40+ functions shrink / 0 grow across the
991 // 68-fixture × 2-path sweep, per-segment executable value-trace equality
992 // guards, and the unicorn-vs-wasmtime execution differentials re-run
993 // green on the new default bytes (flat+inlined flight_algo 0x07FDF307,
994 // const_cse, frame_slot_dce, spill_rung_581, r12_spill_496 — which covers
995 // control_step_decide vs wasmtime; control_step's .text is byte-identical
996 // under the flip) BEFORE the frozen goldens were re-pinned. Escape hatch:
997 // `SYNTH_SPILL_REALLOC=0` is the OPT-OUT — it disables all three stages
998 // and restores the pre-flip bytes (CI-gated by
999 // `frozen_fixtures_spill_realloc_escape_hatch_restores_old_bytes`). Any
1000 // other value (or unset) runs the pass.
1001 // VCR-VER-001 post-exhaustion extensions (#242, the PR #659 verdict): with
1002 // `SYNTH_SPILL_ON_EXHAUST` active the #580 allocation-time Belady spill
1003 // keeps exhausted functions on the optimized path, and its slots present
1004 // shapes the shipping pass structurally cannot fire on (fresh-monotonic
1005 // slots defeat the overwrite-only DCE; the eviction store's source is
1006 // redefined immediately, defeating store→reload forwarding; R2/R3 are
1007 // never touched again, so the rename-target deadness proof declines them).
1008 // `post_exhaust` (bridge-scoped, see above) enables const
1009 // rematerialization of spilled constants, R2/R3 exit-dead rename targets,
1010 // and per-pair pressure commit — see `apply_spill_realloc_post_exhaust`.
1011 // Flag off (the default): `false` selects the shipping behavior bit for
1012 // bit.
1013 let arm_instrs = if !std::env::var("SYNTH_SPILL_REALLOC").is_ok_and(|v| v == "0") {
1014 let (out, n) =
1015 synth_synthesis::liveness::apply_spill_realloc_post_exhaust(&arm_instrs, post_exhaust);
1016 let (out, d) = synth_synthesis::liveness::eliminate_dead_frame_stores(&out);
1017 let (mut out, u) = synth_synthesis::liveness::eliminate_unread_frame_stores(&out);
1018 let (mut tn, mut td, mut tu) = (n, d, u);
1019 // Post-exhaustion only: iterate the triple to a bounded fixpoint. Each
1020 // dissolved spill pair frees registers and removes stores, exposing
1021 // rename windows and holder chains the previous iteration could not
1022 // prove — the allocation-time Belady slots (#580) routinely need two
1023 // or three rounds where the shipping single round suffices for the
1024 // default path's slots. Every iteration is individually gate-proven
1025 // (value-trace equality, pool pressure, strict shrink), so iterating
1026 // composes soundly; the bound keeps compile time deterministic.
1027 if post_exhaust {
1028 let mut progress = n + d + u > 0;
1029 for _ in 0..3 {
1030 if !progress {
1031 break;
1032 }
1033 let (o, n) =
1034 synth_synthesis::liveness::apply_spill_realloc_post_exhaust(&out, true);
1035 let (o, d) = synth_synthesis::liveness::eliminate_dead_frame_stores(&o);
1036 let (o, u) = synth_synthesis::liveness::eliminate_unread_frame_stores(&o);
1037 progress = n + d + u > 0;
1038 (tn, td, tu) = (tn + n, td + d, tu + u);
1039 out = o;
1040 }
1041 // The cleanup can leave the spill frame with zero surviving
1042 // accesses (every reload rematerialized/dissolved, every store
1043 // swept) — the balanced `sub sp,#K`/`add sp,#K` is then pure
1044 // overhead. `elide_dead_frame` proves that and removes the pair;
1045 // its early run (post-realloc) could not, because the spill
1046 // traffic was still in the stream at that point.
1047 out = synth_synthesis::liveness::elide_dead_frame(&out).unwrap_or(out);
1048 }
1049 if std::env::var("SYNTH_FUSE_STATS").is_ok() {
1050 eprintln!(
1051 "[spill-realloc] {tn} reload(s) forwarded/eliminated, {td} newly-dead frame store(s) removed, {tu} unread-slot store(s) removed"
1052 );
1053 }
1054 out
1055 } else {
1056 arm_instrs
1057 };
1058
1059 // VCR-RA immediate-shift folding (#390, #242): a constant shift amount the
1060 // stack selector materialized into a scratch register (`movw rM,#C; lsl rD,rN,rM`)
1061 // folds to the immediate form (`lsl rD,rN,#C`), removing the dead `movw` — −1
1062 // instruction, −1 live register. Removal-only (offset-neutral before branch
1063 // resolution, like the dead-store pass). DEFAULT-ON as of v0.15.0: validated
1064 // bit-identical results + a net cycle win on the dissolved hot path (−2
1065 // cyc/call, .text 100→90 B on gust_mix). Escape hatch: `SYNTH_NO_IMM_SHIFT_FOLD=1`.
1066 let arm_instrs = if std::env::var("SYNTH_NO_IMM_SHIFT_FOLD").is_err() {
1067 let (out, folds) = synth_synthesis::liveness::fold_immediate_shifts(&arm_instrs);
1068 if std::env::var("SYNTH_FUSE_STATS").is_ok() {
1069 eprintln!(
1070 "[imm-shift-fold] {folds} register shift(s) folded to immediate, movw dropped"
1071 );
1072 }
1073 out
1074 } else {
1075 arm_instrs
1076 };
1077
1078 // #686: elide the #682 mod-32 shift-amount mask (`and r12,rK,#31` before
1079 // every register-controlled i32 shl/shr) when the amount is STATICALLY
1080 // provable < 32 — a const amount folds to the immediate-shift form
1081 // (reduced mod 32, so >= 32 shrinks too), and an already-masked amount
1082 // (`rK = rX & c`, c < 32) drops the redundant re-mask. gale measured the
1083 // unconditional mask at ~12% cyc/call (+14 B) on gust_mix, whose Q8
1084 // fixed-point shifts are all constants (#686). The mask stays wherever
1085 // the bound is unproven — elision is an optimization, the mask is the
1086 // sound default (`liveness::elide_shift_masks` has the proof
1087 // obligations). Runs after `fold_immediate_shifts` (whose movw→shift
1088 // window the #682 mask intercepts, so it declines every masked const
1089 // shift) and before branch resolution (removal/rewrite-only ⇒
1090 // offset-neutral).
1091 //
1092 // FLAG-OFF (opt-in via `SYNTH_SHIFT_MASK_ELIDE=1`) because the elision
1093 // moves the frozen anchors: const-amount shifts in control_step (−20 B),
1094 // flight_seam (−164 B) and flight_seam_flat (−168 B) fold back to the
1095 // immediate form — byte-shapes the corpus had BEFORE the #682 mask, now
1096 // with the mask soundly kept for every unproven amount. Flipping
1097 // default-on is a deliberate byte-changing refreeze (all differentials
1098 // re-run on the new bytes, goldens re-pinned) owned by the maintainer.
1099 let arm_instrs = if std::env::var("SYNTH_SHIFT_MASK_ELIDE").is_ok_and(|v| v != "0") {
1100 let (out, elisions) = synth_synthesis::liveness::elide_shift_masks(&arm_instrs);
1101 if std::env::var("SYNTH_FUSE_STATS").is_ok() {
1102 eprintln!(
1103 "[shift-mask-elide] {elisions} provably-<32 shift-amount mask(s) elided (#686)"
1104 );
1105 }
1106 out
1107 } else {
1108 arm_instrs
1109 };
1110
1111 // VCR-RA uxth/uxtb fold (#428, #242): `movw rM,#0xffff; and rD,rN,rM` →
1112 // `uxth rD,rN` (and the 0xff/uxtb form), removing the dead `movw` — −1
1113 // instruction, −1 live register per 16/8-bit mask. 0xffff/0xff are not Thumb-2
1114 // modified immediates so the selector materializes them into a register; the
1115 // dedicated zero-extend expresses the same masking inline. Removal-only +
1116 // rewrite-in-place (offset-neutral). DEFAULT-ON (#242 flag audit flip-wave,
1117 // #592 audit item): evidence basis was the 2-path × repro-corpus sweep —
1118 // 0 functions grow, 13 shrink (control_step 300→294 −6, gust_mix 38→32 −6,
1119 // uxth_fold pack 36→24 −12), locked by the `uxth_fold_no_grow_corpus_242`
1120 // cargo gate; execution differentials re-run green on the new default
1121 // bytes BEFORE the frozen ARM anchors were re-pinned (uxth_fold,
1122 // control_step — see the flip PR). Escape hatch: `SYNTH_UXTH_FOLD=0` opts
1123 // out and restores the pre-flip bytes (CI-gated in
1124 // `frozen_codegen_bytes.rs`).
1125 let arm_instrs = if !std::env::var("SYNTH_UXTH_FOLD").is_ok_and(|v| v == "0") {
1126 let (out, folds) = synth_synthesis::liveness::fold_uxth(&arm_instrs);
1127 if std::env::var("SYNTH_FUSE_STATS").is_ok() {
1128 eprintln!("[uxth-fold] {folds} mask-and folded to uxth/uxtb, movw dropped");
1129 }
1130 out
1131 } else {
1132 arm_instrs
1133 };
1134
1135 // VCR-RA-001 const-CSE / rematerialization-avoidance (#209, #242). Drops a
1136 // `movw`/`mov #imm` that re-materializes a constant already resident in
1137 // another register and retargets the reads — every rewrite proven by the
1138 // liveness analysis. Runs LAST, after every immediate-fold (shift, uxth) and
1139 // range-realloc, but BEFORE branch resolution/encoding (it removes
1140 // instructions, shifting byte offsets). CSE-last is the #242 no-regression
1141 // fix: the folds have already absorbed every foldable constant, so CSE can no
1142 // longer defeat one (the gust_mix 90→92 mechanism). The pass additionally
1143 // size-guards each segment via the byte-estimator — it commits a segment's
1144 // rewrites only if they do not grow its estimated size — so a retarget that
1145 // would flip a 16-bit encoding to 32-bit (higher base register) is declined.
1146 // DEFAULT-ON (#242 flip-wave, the SYNTH_SPILL_REALLOC/SYNTH_BASE_CSE
1147 // template): const-CSE ships by default. The flip prerequisites recorded in
1148 // `const_cse_reduction_242.rs` were retired first — the bridge-level INLINE
1149 // aliasing (the alias-eviction spill-bijection hazard) was DELETED from
1150 // `optimizer_bridge::ir_to_arm`, so this post-hoc, liveness-proven pass is
1151 // the flag's ONLY effect. Evidence basis: 152 fixture×path corpus sweep — 0
1152 // functions grow (size-guarded per segment), 40 shrink (const_cse::spill12
1153 // 236→148 B), total −536 B — and the execution differentials re-run green
1154 // on the new default bytes BEFORE the frozen goldens were re-pinned
1155 // (const_cse, frame_slot_dce, flight_seam 0x07FDF307, spill_rung_581,
1156 // volatile_segment_543, control_step 0x00210A55). Escape hatch:
1157 // `SYNTH_CONST_CSE=0` is the OPT-OUT — it restores the pre-flip bytes
1158 // (CI-gated by `const_cse_escape_hatch_restores_old_bytes_242` and the
1159 // frozen-anchor escape-hatch gate). Any other value (or unset) runs the pass.
1160 //
1161 // #543 Phase 2: const-CSE declines WHOLESALE while any volatile DMA range
1162 // (`--volatile-segment`) is marked. At the ArmOp level a cached constant
1163 // cannot be classified as address-vs-data (a retargeted read may be a
1164 // memory-access base carrying a per-use immediate offset), so the
1165 // conservative stance for statically-unknown addressing is to decline every
1166 // aliasing rewrite — each constant is re-materialized at each occurrence,
1167 // the documented volatile contract (`CompileConfig::volatile_segments`).
1168 let arm_instrs = if !std::env::var("SYNTH_CONST_CSE").is_ok_and(|v| v == "0")
1169 && config.volatile_segments.is_empty()
1170 {
1171 let (out, removed) = synth_synthesis::liveness::apply_const_cse(&arm_instrs);
1172 if std::env::var("SYNTH_FUSE_STATS").is_ok() {
1173 eprintln!("[const-cse] {removed} redundant constant materialization(s) removed");
1174 }
1175 out
1176 } else {
1177 arm_instrs
1178 };
1179
1180 // VCR-RA-001 spill-choice REPORT (#242): measure-only, like SYNTH_SHADOW_ALLOC.
1181 // Per straight-line segment, the frame-slot traffic actually emitted vs the
1182 // reload/store count a farthest-next-use (Belady) allocation over the R0-R8
1183 // pool would need — the measured headroom for the full spill-choice rewrite.
1184 // Printed on the FINAL stream (post all rewrite passes), so a flag-off run
1185 // reports the greedy baseline and a flag-on run reports what remains.
1186 if std::env::var("SYNTH_SPILL_REPORT").is_ok() {
1187 for seg in synth_synthesis::liveness::spill_choice_report(&arm_instrs, 9) {
1188 if seg.actual_reloads + seg.actual_spill_stores > 0 || seg.peak_pressure > 9 {
1189 eprintln!(
1190 "[spill-report] seg@{} len={} peak={} actual={}ld+{}st belady(k=9)={}ld+{}st",
1191 seg.start,
1192 seg.len,
1193 seg.peak_pressure,
1194 seg.actual_reloads,
1195 seg.actual_spill_stores,
1196 seg.belady_reloads,
1197 seg.belady_spill_stores
1198 );
1199 }
1200 }
1201 }
1202
1203 // ISA feature gate: validate that all generated instructions are supported
1204 // by the target. This catches FPU instructions on no-FPU targets, double-precision
1205 // instructions on single-precision targets, etc.
1206 validate_instructions(&arm_instrs, config.target.fpu, &config.target.triple)
1207 .map_err(|e| format!("ISA validation failed: {}", e))?;
1208
1209 // Encode to binary — use Thumb-2 for Cortex-M targets
1210 let use_thumb2 = matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb);
1211
1212 let encoder = if use_thumb2 {
1213 ArmEncoder::new_thumb2_with_fpu(config.target.fpu)
1214 } else {
1215 ArmEncoder::new_arm32()
1216 };
1217
1218 // #202: resolve local label branches (Bcc/B/Bhs/Blo) to byte-accurate
1219 // offsets before encoding. `select_with_stack` emits them as label
1220 // placeholders and never resolves them — without this they encode as
1221 // `bne.n #0` and land mid-instruction whenever a 32-bit Thumb-2 instruction
1222 // sits between the branch and its target (UsageFault on real hardware).
1223 // Only meaningful for Thumb-2 (the offset units are halfword/PC+4).
1224 let arm_instrs = if use_thumb2 {
1225 resolve_label_branches(arm_instrs, &encoder)?
1226 } else {
1227 arm_instrs
1228 };
1229
1230 let mut code = Vec::new();
1231 let mut relocations = Vec::new();
1232
1233 // #345: literal-pool address loads. Each `LdrSym` was encoded as a placeholder
1234 // `LDR.W rd,[pc,#0]`; record where its instruction sits and what it loads so
1235 // we can append a pooled word (carrying the symbol address via R_ARM_ABS32)
1236 // and patch the PC-relative offset once the pool position is known.
1237 struct PendingLiteral {
1238 ldr_offset: u32,
1239 symbol: String,
1240 addend: i32,
1241 }
1242 let mut pending_literals: Vec<PendingLiteral> = Vec::new();
1243
1244 // VCR-DBG-001: per-instruction source map for DWARF `.debug_line`. Captured
1245 // here because `code.len()` immediately before `encode()` is the final
1246 // machine offset of the instruction within this function's `.text` — nothing
1247 // after the loop shifts earlier instructions (the literal pool is appended at
1248 // the end; the LDR patch below is in-place/length-preserving). Purely
1249 // additive: it does not touch `code`, so `.text` is byte-identical.
1250 let mut line_map: LineMap = Vec::new();
1251
1252 for instr in &arm_instrs {
1253 // Record a relocation for every BL: the encoder emits `bl #0` and
1254 // relies on a relocation to patch the target. This covers BOTH import
1255 // dispatch stubs (`__meld_*`, undefined externals) AND internal calls
1256 // (`func_N`, defined in this object). Previously only `__meld_*` was
1257 // recorded, so internal `BL func_N` calls were left as unpatched
1258 // `bl #0` placeholders branching to a garbage address (#167).
1259 if let ArmOp::Bl { label } = &instr.op {
1260 relocations.push(CodeRelocation {
1261 offset: code.len() as u32,
1262 symbol: label.clone(),
1263 kind: synth_core::backend::RelocKind::ThmCall,
1264 });
1265 }
1266 // #237: symbol-relative MOVW/MOVT (the `--native-pointer-abi` static-data
1267 // addressing). The encoder writes the addend in place; record the matching
1268 // R_ARM_MOVW_ABS_NC / R_ARM_MOVT_ABS so the linker adds the symbol address.
1269 if let ArmOp::MovwSym { symbol, .. } = &instr.op {
1270 relocations.push(CodeRelocation {
1271 offset: code.len() as u32,
1272 symbol: symbol.clone(),
1273 kind: synth_core::backend::RelocKind::MovwAbs,
1274 });
1275 }
1276 if let ArmOp::MovtSym { symbol, .. } = &instr.op {
1277 relocations.push(CodeRelocation {
1278 offset: code.len() as u32,
1279 symbol: symbol.clone(),
1280 kind: synth_core::backend::RelocKind::MovtAbs,
1281 });
1282 }
1283 // #345: defer the literal-pool word + reloc + offset patch to the
1284 // post-loop pass (the pool address is not yet known).
1285 if let ArmOp::LdrSym { symbol, addend, .. } = &instr.op {
1286 pending_literals.push(PendingLiteral {
1287 ldr_offset: code.len() as u32,
1288 symbol: symbol.clone(),
1289 addend: *addend,
1290 });
1291 }
1292
1293 // The machine offset of this instruction is the current code length,
1294 // captured before the bytes are appended.
1295 line_map.push((code.len() as u32, instr.source_line));
1296
1297 let encoded = encoder
1298 .encode(&instr.op)
1299 .map_err(|e| format!("ARM encoding failed: {}", e))?;
1300 code.extend_from_slice(&encoded);
1301 }
1302
1303 // #345: place the literal pool at the end of this function's `.text`. Gated on
1304 // there being at least one `LdrSym` — functions without one are byte-identical
1305 // to before (no trailing padding, so downstream `func_offsets` are unchanged
1306 // and the frozen differential fixtures stay bit-for-bit equal).
1307 if !pending_literals.is_empty() {
1308 if !use_thumb2 {
1309 return Err("LdrSym literal-pool addressing requires Thumb-2".to_string());
1310 }
1311 // 4-byte align the pool start (Thumb-2 word loads require it, and
1312 // `Align(PC,4)` in the LDR-literal semantics assumes a word-aligned pool).
1313 while code.len() % 4 != 0 {
1314 code.push(0x00);
1315 }
1316 // One distinct pooled word per LdrSym (no dedup: different sites carry
1317 // different addends, and the REL addend lives in the word).
1318 for lit in &pending_literals {
1319 let word_offset = code.len() as u32;
1320
1321 // REL semantics: the linker computes `S + A`, where A is the in-place
1322 // value of the relocated word. Initialize the word to the addend so
1323 // the final loaded address is `symbol + addend`.
1324 code.extend_from_slice(&(lit.addend as u32).to_le_bytes());
1325 relocations.push(CodeRelocation {
1326 offset: word_offset,
1327 symbol: lit.symbol.clone(),
1328 kind: synth_core::backend::RelocKind::Abs32,
1329 });
1330
1331 // Patch the placeholder `LDR.W rd,[pc,#imm12]`. Thumb-2 LDR (literal):
1332 // address = Align(PC,4) + imm12, with PC = ldr_offset + 4. The pool is
1333 // always after the LDR, so U=1 (already set in hw1 = 0xF8DF).
1334 let pc = lit.ldr_offset + 4;
1335 let aligned_pc = pc & !3u32;
1336 let imm12 = word_offset - aligned_pc;
1337 if imm12 > 0xFFF {
1338 // Wide LDR-literal range is ±4 KB; these function bodies are far
1339 // smaller, but fail cleanly rather than miscompile if exceeded.
1340 return Err(format!(
1341 "LdrSym literal pool out of range (#345): imm12={} > 4095 \
1342 for symbol {}",
1343 imm12, lit.symbol
1344 ));
1345 }
1346 let hw2_off = (lit.ldr_offset + 2) as usize;
1347 let mut hw2 = u16::from_le_bytes([code[hw2_off], code[hw2_off + 1]]);
1348 hw2 = (hw2 & 0xF000) | (imm12 as u16); // keep Rt, set imm12
1349 let hw2_bytes = hw2.to_le_bytes();
1350 code[hw2_off] = hw2_bytes[0];
1351 code[hw2_off + 1] = hw2_bytes[1];
1352 }
1353 }
1354
1355 Ok((code, relocations, line_map))
1356}
1357
1358/// Resolve local label branches to byte-accurate offsets (#202).
1359///
1360/// `select_with_stack` emits conditional/unconditional branches as label
1361/// placeholders (`Bcc`/`B`/`Bhs`/`Blo` + `Label`) and never resolves them; the
1362/// encoder then emits a `0xD000`/`0xE000` placeholder with offset 0. Before #197
1363/// this path only ran for `--no-optimize`/declined functions, so the latent bug
1364/// stayed hidden — routing relocatable code through it surfaced branches that
1365/// land mid-instruction (a Cortex-M UsageFault) whenever a 32-bit Thumb-2
1366/// instruction sits between the branch and its target.
1367///
1368/// This pass encodes each instruction to learn its real byte length (so 16- vs
1369/// 32-bit forms and multi-instruction expansions are exact), maps each `Label`
1370/// to its byte position, and rewrites every label branch to the displacement
1371/// the encoder consumes: `(target - branch - 4) / 2` halfwords. A bounded
1372/// fixed-point handles an offset growing a branch from 16- to 32-bit (which
1373/// shifts later positions). `BCondOffset`/`BOffset` already produced inline by
1374/// the optimized path carry no label and are left untouched.
1375fn resolve_label_branches(
1376 arm_instrs: Vec<ArmInstruction>,
1377 encoder: &ArmEncoder,
1378) -> Result<Vec<ArmInstruction>, String> {
1379 use std::collections::HashMap;
1380 use synth_synthesis::Condition;
1381
1382 enum BKind {
1383 Cond(Condition),
1384 Uncond,
1385 }
1386 // Record each label branch ONCE — indices are stable across iterations.
1387 let mut branches: Vec<(usize, BKind, String)> = Vec::new();
1388 for (i, instr) in arm_instrs.iter().enumerate() {
1389 match &instr.op {
1390 ArmOp::Bcc { cond, label } => branches.push((i, BKind::Cond(*cond), label.clone())),
1391 ArmOp::Bhs { label } => branches.push((i, BKind::Cond(Condition::HS), label.clone())),
1392 ArmOp::Blo { label } => branches.push((i, BKind::Cond(Condition::LO), label.clone())),
1393 ArmOp::B { label } => branches.push((i, BKind::Uncond, label.clone())),
1394 _ => {}
1395 }
1396 }
1397 if branches.is_empty() {
1398 return Ok(arm_instrs);
1399 }
1400
1401 let mut resolved = arm_instrs;
1402 // Sizes only grow (16→32-bit), so this converges quickly; cap for safety.
1403 for _ in 0..16 {
1404 // 1. Byte position of each instruction (Label encodes to 0 bytes).
1405 let mut positions = Vec::with_capacity(resolved.len());
1406 let mut pos: i64 = 0;
1407 for instr in &resolved {
1408 positions.push(pos);
1409 pos += encoder
1410 .encode(&instr.op)
1411 .map_err(|e| format!("branch-resolve size probe failed: {}", e))?
1412 .len() as i64;
1413 }
1414 // 2. Label name -> byte position (owned keys so the borrow ends here).
1415 let mut labels: HashMap<String, i64> = HashMap::new();
1416 for (i, instr) in resolved.iter().enumerate() {
1417 if let ArmOp::Label { name } = &instr.op {
1418 labels.insert(name.clone(), positions[i]);
1419 }
1420 }
1421 // 3. Rewrite each branch to its byte-accurate offset.
1422 let mut changed = false;
1423 for (idx, kind, label) in &branches {
1424 // A label not defined locally is an EXTERNAL target (e.g.
1425 // `Trap_Handler` resolved by a relocation / the vector table). Leave
1426 // such branches as their placeholder for the existing relocation
1427 // path — only local control-flow labels are byte-resolved here.
1428 let Some(&target) = labels.get(label) else {
1429 continue;
1430 };
1431 // Encoder consumes the field as (target - branch - 4) / 2 halfwords.
1432 // Positions are always even, so this division is exact.
1433 let halfword_offset = ((target - positions[*idx] - 4) / 2) as i32;
1434 let new_op = match kind {
1435 BKind::Cond(c) => ArmOp::BCondOffset {
1436 cond: *c,
1437 offset: halfword_offset,
1438 },
1439 BKind::Uncond => ArmOp::BOffset {
1440 offset: halfword_offset,
1441 },
1442 };
1443 if resolved[*idx].op != new_op {
1444 resolved[*idx].op = new_op;
1445 changed = true;
1446 }
1447 }
1448 if !changed {
1449 break;
1450 }
1451 }
1452 Ok(resolved)
1453}
1454
1455#[cfg(test)]
1456mod tests {
1457 use super::*;
1458
1459 /// #539: `i32.const 0; memory.grow m` folds to `memory.size m`; other deltas
1460 /// (const non-zero, runtime) are left as `memory.grow` (→ the sound fixed-
1461 /// memory -1). Non-grow ops are untouched, so functions without the idiom are
1462 /// byte-identical.
1463 #[test]
1464 fn test_rewrite_memory_grow_zero_539() {
1465 // the idiom -> memory.size
1466 assert_eq!(
1467 rewrite_memory_grow_zero(&[WasmOp::I32Const(0), WasmOp::MemoryGrow(0)]),
1468 vec![WasmOp::MemorySize(0)]
1469 );
1470 // const non-zero delta: NOT folded
1471 assert_eq!(
1472 rewrite_memory_grow_zero(&[WasmOp::I32Const(2), WasmOp::MemoryGrow(0)]),
1473 vec![WasmOp::I32Const(2), WasmOp::MemoryGrow(0)]
1474 );
1475 // runtime delta (no preceding const): NOT folded
1476 assert_eq!(
1477 rewrite_memory_grow_zero(&[WasmOp::LocalGet(0), WasmOp::MemoryGrow(0)]),
1478 vec![WasmOp::LocalGet(0), WasmOp::MemoryGrow(0)]
1479 );
1480 // a bare const-0 not feeding a grow is untouched
1481 assert_eq!(
1482 rewrite_memory_grow_zero(&[WasmOp::I32Const(0), WasmOp::I32Add]),
1483 vec![WasmOp::I32Const(0), WasmOp::I32Add]
1484 );
1485 // fold is local: surrounding ops preserved, indices past the fold intact
1486 assert_eq!(
1487 rewrite_memory_grow_zero(&[
1488 WasmOp::LocalGet(0),
1489 WasmOp::I32Const(0),
1490 WasmOp::MemoryGrow(0),
1491 WasmOp::I32Add,
1492 ]),
1493 vec![WasmOp::LocalGet(0), WasmOp::MemorySize(0), WasmOp::I32Add]
1494 );
1495 }
1496
1497 #[test]
1498 fn test_arm_backend_name() {
1499 let backend = ArmBackend::new();
1500 assert_eq!(backend.name(), "arm");
1501 assert!(backend.is_available());
1502 }
1503
1504 #[test]
1505 fn test_arm_backend_capabilities() {
1506 let backend = ArmBackend::new();
1507 let caps = backend.capabilities();
1508 assert!(!caps.produces_elf);
1509 assert!(caps.supports_rule_verification);
1510 assert!(!caps.is_external);
1511 }
1512
1513 #[test]
1514 fn test_compile_add_function() {
1515 let backend = ArmBackend::new();
1516 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
1517 let config = CompileConfig::default();
1518
1519 let result = backend.compile_function("add", &ops, &config);
1520 assert!(result.is_ok());
1521
1522 let func = result.unwrap();
1523 assert_eq!(func.name, "add");
1524 assert!(!func.code.is_empty());
1525 assert_eq!(func.wasm_ops, ops);
1526 }
1527
1528 /// VCR-DBG-001: the per-instruction source map must cover the function with
1529 /// monotonic, in-bounds machine offsets, and must not perturb the emitted
1530 /// code (it is captured at encode time, never serialized here).
1531 #[test]
1532 fn test_line_map_is_wellformed_dbg001() {
1533 let backend = ArmBackend::new();
1534 let ops = vec![
1535 WasmOp::LocalGet(0),
1536 WasmOp::LocalGet(1),
1537 WasmOp::I32Add,
1538 WasmOp::End,
1539 ];
1540 let config = CompileConfig::default();
1541 let func = backend.compile_function("add", &ops, &config).unwrap();
1542
1543 // Non-empty, and the first instruction starts at machine offset 0.
1544 assert!(
1545 !func.line_map.is_empty(),
1546 "a non-trivial function captures a source map"
1547 );
1548 assert_eq!(func.line_map[0].0, 0, "first instruction at offset 0");
1549
1550 // Offsets strictly increase by at least one ARM/Thumb instruction (>= 2
1551 // bytes) and every mapped offset lies inside the emitted `.text`.
1552 for w in func.line_map.windows(2) {
1553 assert!(w[1].0 > w[0].0, "instruction offsets strictly increase");
1554 assert!(
1555 w[1].0 - w[0].0 >= 2,
1556 "each ARM/Thumb instruction is >= 2 bytes"
1557 );
1558 }
1559 let last = func.line_map.last().unwrap().0 as usize;
1560 assert!(
1561 last < func.code.len(),
1562 "every mapped offset lies inside .text"
1563 );
1564
1565 // The side-table is additive: recompiling is deterministic and the map is
1566 // consistent with that exact code (capturing it does not alter output).
1567 let again = backend.compile_function("add", &ops, &config).unwrap();
1568 assert_eq!(
1569 again.code, func.code,
1570 "compilation deterministic; map is additive"
1571 );
1572 assert_eq!(again.line_map, func.line_map);
1573 }
1574
1575 #[test]
1576 fn test_count_params() {
1577 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
1578 assert_eq!(count_params(&ops), 2);
1579
1580 let no_params = vec![WasmOp::I32Const(5), WasmOp::I32Const(3), WasmOp::I32Add];
1581 assert_eq!(count_params(&no_params), 0);
1582 }
1583
1584 /// #457: the declared param count caps the access-pattern inference. The
1585 /// repro shape `(param i32)(local i32) → p0 + local1` reads local 1 before
1586 /// any write, so `count_params` infers 2 — with the declared count (1) the
1587 /// local is reclassified onto the zero-inited frame path instead of being
1588 /// read from R1 (caller garbage).
1589 #[test]
1590 fn declared_param_count_caps_inference_457() {
1591 let ops = vec![
1592 WasmOp::LocalGet(0),
1593 WasmOp::LocalGet(1),
1594 WasmOp::I32Add,
1595 WasmOp::End,
1596 ];
1597 // The inference alone still says 2 (the misclassification this caps).
1598 assert_eq!(count_params(&ops), 2);
1599
1600 let backend = ArmBackend::new();
1601 let inferred = backend
1602 .compile_function("rbw", &ops, &CompileConfig::default())
1603 .unwrap();
1604 let declared = backend
1605 .compile_function(
1606 "rbw",
1607 &ops,
1608 &CompileConfig {
1609 current_func_param_count: Some(1),
1610 ..CompileConfig::default()
1611 },
1612 )
1613 .unwrap();
1614 // The cap is consumed: the declared-count compile reclassifies local 1
1615 // and must emit different code than the param-misclassified one.
1616 assert_ne!(
1617 inferred.code, declared.code,
1618 "declared param count must reach the selector"
1619 );
1620 // The zero-init is present: a 16-bit Thumb `movs rN, #0`
1621 // (0x2000 | rd<<8 → LE bytes [0x00, 0x20+rd]) somewhere in the body.
1622 let has_movs_zero = declared
1623 .code
1624 .chunks_exact(2)
1625 .any(|h| h[0] == 0x00 && (0x20..=0x27).contains(&h[1]));
1626 assert!(
1627 has_movs_zero,
1628 "declared-count compile must zero-init the read-before-write local; code: {:02x?}",
1629 declared.code
1630 );
1631 // A declared count that matches (or exceeds) the inference changes
1632 // nothing — byte-identity for every function without rbw locals.
1633 let matching = backend
1634 .compile_function(
1635 "rbw",
1636 &ops,
1637 &CompileConfig {
1638 current_func_param_count: Some(2),
1639 ..CompileConfig::default()
1640 },
1641 )
1642 .unwrap();
1643 assert_eq!(
1644 matching.code, inferred.code,
1645 "declared >= inferred must stay byte-identical"
1646 );
1647 }
1648
1649 #[test]
1650 fn test_arm_backend_register() {
1651 let mut registry = synth_core::BackendRegistry::new();
1652 registry.register(Box::new(ArmBackend::new()));
1653 assert!(registry.get("arm").is_some());
1654 assert_eq!(registry.available().len(), 1);
1655 }
1656
1657 #[test]
1658 fn test_compile_import_call_produces_relocations() {
1659 let backend = ArmBackend::new();
1660 // Simulate a WASM module where func index 0 is an import.
1661 // Call(0) should generate MOV R0, #0; BL __meld_dispatch_import
1662 let ops = vec![WasmOp::Call(0)];
1663 let config = CompileConfig {
1664 num_imports: 1,
1665 no_optimize: true, // Direct instruction selection to preserve Call semantics
1666 ..CompileConfig::default()
1667 };
1668
1669 let result = backend.compile_function("caller", &ops, &config);
1670 assert!(result.is_ok());
1671
1672 let func = result.unwrap();
1673 assert!(!func.code.is_empty());
1674 assert_eq!(func.relocations.len(), 1);
1675 assert_eq!(func.relocations[0].symbol, "__meld_dispatch_import");
1676 // The BL is the second instruction (after MOV R0, #0), so offset should be > 0
1677 assert!(func.relocations[0].offset > 0);
1678 }
1679
1680 /// Regression test for #197: in `relocatable` mode, an import call must
1681 /// relocate against the direct `func_N` symbol (rewritten to the wasm field
1682 /// name by `build_relocatable_elf`), NOT `__meld_dispatch_import`. This is
1683 /// the ABI half of the #197 fix — without it, a host linker cannot resolve
1684 /// the call to the real kernel symbol (e.g. `k_spin_lock`).
1685 #[test]
1686 fn test_compile_relocatable_import_uses_direct_func_symbol_197() {
1687 let backend = ArmBackend::new();
1688 let ops = vec![WasmOp::Call(0)]; // func 0 is an import
1689 let config = CompileConfig {
1690 num_imports: 1,
1691 relocatable: true,
1692 ..CompileConfig::default()
1693 };
1694
1695 let func = backend
1696 .compile_function("caller", &ops, &config)
1697 .expect("relocatable import call compiles");
1698
1699 assert_eq!(func.relocations.len(), 1);
1700 assert_eq!(
1701 func.relocations[0].symbol, "func_0",
1702 "#197: relocatable import must relocate against func_0 (→ field name), not Meld dispatch"
1703 );
1704 }
1705
1706 #[test]
1707 fn test_compile_no_imports_no_relocations() {
1708 let backend = ArmBackend::new();
1709 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
1710 let config = CompileConfig::default();
1711
1712 let func = backend.compile_function("add", &ops, &config).unwrap();
1713 assert!(func.relocations.is_empty());
1714 }
1715
1716 /// Regression test for #167: a call to an INTERNAL function
1717 /// (index `>= num_imports`) must record a relocation against `func_{index}`.
1718 /// Before the fix, only `__meld_*` (import) BLs were relocated, so
1719 /// internal `BL func_N` was emitted as an unpatched `bl #0` branching
1720 /// to a garbage address — making the object non-linkable. This test
1721 /// would have caught that regression.
1722 #[test]
1723 fn test_compile_internal_call_produces_relocation_167() {
1724 let backend = ArmBackend::new();
1725 // num_imports = 1, so Call(2) is an INTERNAL call → `BL func_2`.
1726 let ops = vec![WasmOp::Call(2)];
1727 let config = CompileConfig {
1728 num_imports: 1,
1729 no_optimize: true,
1730 ..CompileConfig::default()
1731 };
1732
1733 let func = backend
1734 .compile_function("caller", &ops, &config)
1735 .expect("internal call compiles");
1736
1737 assert_eq!(
1738 func.relocations.len(),
1739 1,
1740 "an internal call must emit exactly one relocation (#167)"
1741 );
1742 assert_eq!(
1743 func.relocations[0].symbol, "func_2",
1744 "internal call must relocate against the callee's func_{{index}} symbol (#167)"
1745 );
1746 }
1747
1748 // ─── Phase 1 safety-bounds plumbing for ARM ──────────────────────────
1749
1750 #[test]
1751 fn arm_safety_bounds_mpu_emits_same_code_as_none() {
1752 // Mpu mode must not introduce any inline check on ARM — the MPU
1753 // handles faults via hardware. The encoded bytes for an i32.load
1754 // should be identical between None and Mpu.
1755 let backend = ArmBackend::new();
1756 let ops = vec![
1757 WasmOp::LocalGet(0),
1758 WasmOp::I32Load {
1759 offset: 0,
1760 align: 2,
1761 },
1762 ];
1763 let cfg_none = CompileConfig {
1764 no_optimize: true,
1765 ..Default::default()
1766 };
1767 let cfg_mpu = CompileConfig {
1768 no_optimize: true,
1769 safety_bounds: SafetyBounds::Mpu,
1770 ..Default::default()
1771 };
1772 let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
1773 let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
1774 assert_eq!(
1775 n.code, m.code,
1776 "Mpu and None should produce identical ARM bytes (Mpu relies on hardware)"
1777 );
1778 }
1779
1780 #[test]
1781 fn arm_legacy_bounds_check_still_emits_software_check() {
1782 // Legacy CLI users with `--bounds-check` should keep getting the
1783 // software path even though the new SafetyBounds field defaults to None.
1784 let backend = ArmBackend::new();
1785 let ops = vec![
1786 WasmOp::LocalGet(0),
1787 WasmOp::I32Load {
1788 offset: 0,
1789 align: 2,
1790 },
1791 ];
1792 let cfg_legacy = CompileConfig {
1793 no_optimize: true,
1794 bounds_check: true,
1795 ..Default::default()
1796 };
1797 let cfg_software = CompileConfig {
1798 no_optimize: true,
1799 safety_bounds: SafetyBounds::Software,
1800 ..Default::default()
1801 };
1802 let l = backend.compile_function("ld", &ops, &cfg_legacy).unwrap();
1803 let s = backend.compile_function("ld", &ops, &cfg_software).unwrap();
1804 assert_eq!(
1805 l.code, s.code,
1806 "--bounds-check should produce the same bytes as --safety-bounds=software"
1807 );
1808 }
1809
1810 /// #377: `--safety-bounds software` must be enforced on the OPTIMIZED path
1811 /// too. Pre-fix, `software` was byte-identical to `none` there (a silent
1812 /// no-op while the safety manifest claimed enforcement). The compiled
1813 /// bytes must now (a) differ from `none` and (b) contain the inline
1814 /// `CMP ip, sl` + `UDF` guard.
1815 #[test]
1816 fn arm_safety_bounds_software_enforced_on_optimized_path_377() {
1817 let backend = ArmBackend::new();
1818 // Dynamic-address store+load: the optimized path accepts this shape
1819 // (no calls, no i64 params, ≤4 params).
1820 let ops = vec![
1821 WasmOp::LocalGet(0),
1822 WasmOp::LocalGet(1),
1823 WasmOp::I32Store {
1824 offset: 4,
1825 align: 2,
1826 },
1827 WasmOp::LocalGet(0),
1828 WasmOp::I32Load {
1829 offset: 0,
1830 align: 2,
1831 },
1832 ];
1833 // no_optimize NOT set — this exercises the optimized path.
1834 let cfg_none = CompileConfig::default();
1835 let cfg_sw = CompileConfig {
1836 safety_bounds: SafetyBounds::Software,
1837 ..Default::default()
1838 };
1839 let n = backend.compile_function("st", &ops, &cfg_none).unwrap();
1840 let s = backend.compile_function("st", &ops, &cfg_sw).unwrap();
1841 assert_ne!(
1842 n.code, s.code,
1843 "#377: software bounds must CHANGE optimized-path codegen (was a silent no-op)"
1844 );
1845 // Thumb-2 `UDF #0` is 0xDE00 (LE bytes: 00 DE); `CMP ip, sl` (T2
1846 // high-reg) is 0x45D4 (LE: D4 45). Both must appear — one guard per
1847 // access, trap inline.
1848 let has_udf = s.code.windows(2).any(|w| w == [0x00, 0xDE]);
1849 let has_cmp_ip_sl = s.code.windows(2).any(|w| w == [0xD4, 0x45]);
1850 assert!(has_udf, "#377: inline UDF trap missing from optimized path");
1851 assert!(
1852 has_cmp_ip_sl,
1853 "#377: CMP ip, sl bounds compare missing from optimized path"
1854 );
1855 // And `none` must contain NO UDF (the function has no other trap).
1856 assert!(
1857 !n.code.windows(2).any(|w| w == [0x00, 0xDE]),
1858 "none must not contain a UDF for this function"
1859 );
1860 }
1861
1862 /// #377: `mpu` on the optimized path is codegen-passthrough — identical
1863 /// bytes to `none` on BOTH paths (hardware enforcement is target-level;
1864 /// synth does not emit MPU region programming — tracked separately in
1865 /// #377's fix-direction discussion). This pins path-parity for `mpu`.
1866 #[test]
1867 fn arm_safety_bounds_mpu_optimized_path_parity_377() {
1868 let backend = ArmBackend::new();
1869 let ops = vec![
1870 WasmOp::LocalGet(0),
1871 WasmOp::I32Load {
1872 offset: 0,
1873 align: 2,
1874 },
1875 ];
1876 let cfg_none = CompileConfig::default();
1877 let cfg_mpu = CompileConfig {
1878 safety_bounds: SafetyBounds::Mpu,
1879 ..Default::default()
1880 };
1881 let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
1882 let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
1883 assert_eq!(
1884 n.code, m.code,
1885 "Mpu and None must produce identical bytes on the optimized path too"
1886 );
1887 }
1888
1889 /// #377: `mask` on the optimized path declines to the direct selector
1890 /// (honest degradation) — the compiled function must equal the
1891 /// `--no-optimize` masking bytes, i.e. the flag is honored, never dropped.
1892 #[test]
1893 fn arm_safety_bounds_mask_optimized_path_declines_to_direct_377() {
1894 let backend = ArmBackend::new();
1895 let ops = vec![
1896 WasmOp::LocalGet(0),
1897 WasmOp::LocalGet(1),
1898 WasmOp::I32Store {
1899 offset: 0,
1900 align: 2,
1901 },
1902 ];
1903 let cfg_mask_opt = CompileConfig {
1904 safety_bounds: SafetyBounds::Mask,
1905 ..Default::default()
1906 };
1907 let cfg_mask_direct = CompileConfig {
1908 no_optimize: true,
1909 safety_bounds: SafetyBounds::Mask,
1910 ..Default::default()
1911 };
1912 let o = backend.compile_function("st", &ops, &cfg_mask_opt).unwrap();
1913 let d = backend
1914 .compile_function("st", &ops, &cfg_mask_direct)
1915 .unwrap();
1916 assert_eq!(
1917 o.code, d.code,
1918 "#377: mask on the optimized path must fall back to the direct selector's masking"
1919 );
1920 }
1921
1922 // ========================================================================
1923 // ISA feature gate tests — ensure the compiler never emits unsupported
1924 // instructions for a given target
1925 // ========================================================================
1926
1927 #[test]
1928 fn test_f32_rejected_on_cortex_m3_no_fpu() {
1929 let backend = ArmBackend::new();
1930 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
1931 let config = CompileConfig {
1932 target: TargetSpec::cortex_m3(),
1933 no_optimize: true,
1934 ..CompileConfig::default()
1935 };
1936
1937 let result = backend.compile_function("fadd", &ops, &config);
1938 assert!(
1939 result.is_err(),
1940 "f32 operations should fail on Cortex-M3 (no FPU)"
1941 );
1942 }
1943
1944 #[test]
1945 fn test_f32_accepted_on_cortex_m4f() {
1946 let backend = ArmBackend::new();
1947 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
1948 let config = CompileConfig {
1949 target: TargetSpec::cortex_m4f(),
1950 no_optimize: true,
1951 ..CompileConfig::default()
1952 };
1953
1954 let result = backend.compile_function("fadd", &ops, &config);
1955 assert!(
1956 result.is_ok(),
1957 "f32 operations should succeed on Cortex-M4F, got: {:?}",
1958 result.unwrap_err()
1959 );
1960 }
1961
1962 #[test]
1963 fn test_i32_works_on_all_targets() {
1964 let backend = ArmBackend::new();
1965 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
1966
1967 // Cortex-M3 (no FPU)
1968 let config_m3 = CompileConfig {
1969 target: TargetSpec::cortex_m3(),
1970 no_optimize: true,
1971 ..CompileConfig::default()
1972 };
1973 assert!(
1974 backend.compile_function("add", &ops, &config_m3).is_ok(),
1975 "i32 ops should work on Cortex-M3"
1976 );
1977
1978 // Cortex-M4F (single FPU)
1979 let config_m4f = CompileConfig {
1980 target: TargetSpec::cortex_m4f(),
1981 no_optimize: true,
1982 ..CompileConfig::default()
1983 };
1984 assert!(
1985 backend.compile_function("add", &ops, &config_m4f).is_ok(),
1986 "i32 ops should work on Cortex-M4F"
1987 );
1988
1989 // Cortex-M7DP (double FPU)
1990 let config_m7dp = CompileConfig {
1991 target: TargetSpec::cortex_m7dp(),
1992 no_optimize: true,
1993 ..CompileConfig::default()
1994 };
1995 assert!(
1996 backend.compile_function("add", &ops, &config_m7dp).is_ok(),
1997 "i32 ops should work on Cortex-M7DP"
1998 );
1999 }
2000
2001 #[test]
2002 fn test_f32_rejected_on_cortex_m4_no_fpu() {
2003 // Cortex-M4 (without F suffix) has no FPU
2004 let backend = ArmBackend::new();
2005 let ops = vec![WasmOp::F32Const(1.5), WasmOp::F32Const(2.5), WasmOp::F32Mul];
2006 let config = CompileConfig {
2007 target: TargetSpec::cortex_m4(),
2008 no_optimize: true,
2009 ..CompileConfig::default()
2010 };
2011
2012 let result = backend.compile_function("fmul", &ops, &config);
2013 assert!(
2014 result.is_err(),
2015 "f32 operations should fail on Cortex-M4 (no FPU)"
2016 );
2017 }
2018
2019 // ========================================================================
2020 // Issue #120 — f32 ops in the optimized lowering path
2021 //
2022 // `OptimizerBridge::wasm_to_ir` has no handlers for f32/f64 ops, so a
2023 // value-producing float op fell through to `Opcode::Nop`, leaving a
2024 // downstream consumer with an unmapped vreg and tripping the PR #101
2025 // defensive panic in `ir_to_arm`. Customer reproducer: `compiler_builtins
2026 // float::div` and `gale_compute_ipi_mask` in the `falcon-rate-component`
2027 // module.
2028 //
2029 // Fix: `optimize_full` declines float modules with a typed `Err`;
2030 // `compile_wasm_to_arm` falls back to the non-optimized `select_with_stack`
2031 // path, which handles f32 via VFP/FPU. These tests use the *default*
2032 // (optimized) config — `no_optimize` is NOT set — which is the exact
2033 // configuration that panicked pre-fix.
2034 // ========================================================================
2035
2036 /// Pre-fix: this panicked with "vreg vN has no assigned ARM register and
2037 /// no spill slot" inside `ir_to_arm`. Post-fix: the optimized path declines
2038 /// the module and the backend falls back to direct selection, producing a
2039 /// non-empty f32.div lowering on a Cortex-M4F.
2040 #[test]
2041 fn test_issue120_f32_div_compiles_via_optimized_default() {
2042 let backend = ArmBackend::new();
2043 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
2044 let config = CompileConfig {
2045 target: TargetSpec::cortex_m4f(),
2046 // no_optimize NOT set — this exercises the optimized path that
2047 // panicked in issue #120, then the fallback to direct selection.
2048 // GI-FPU-002: the f32 params must be declared so the direct
2049 // selector homes them in S0/S1 (AAPCS-VFP) rather than declining.
2050 current_func_params_f32: vec![true, true],
2051 ..CompileConfig::default()
2052 };
2053
2054 let result = backend.compile_function("fdiv", &ops, &config);
2055 assert!(
2056 result.is_ok(),
2057 "f32.div must compile on Cortex-M4F via the optimized->direct \
2058 fallback (issue #120), got: {:?}",
2059 result.as_ref().err()
2060 );
2061 assert!(
2062 !result.unwrap().code.is_empty(),
2063 "f32.div must produce non-empty machine code"
2064 );
2065 }
2066
2067 /// A spread of f32 ops, all through the optimized (default) config, must
2068 /// compile via the fallback on an FPU target without panicking.
2069 #[test]
2070 fn test_issue120_assorted_f32_ops_compile_via_optimized_default() {
2071 let backend = ArmBackend::new();
2072 let config = CompileConfig {
2073 target: TargetSpec::cortex_m4f(),
2074 // GI-FPU-002: declare the two f32 params for AAPCS-VFP homing.
2075 current_func_params_f32: vec![true, true],
2076 ..CompileConfig::default()
2077 };
2078
2079 let cases: Vec<(&str, Vec<WasmOp>)> = vec![
2080 (
2081 "fadd",
2082 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Add],
2083 ),
2084 (
2085 "fmul",
2086 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Mul],
2087 ),
2088 (
2089 "fsub",
2090 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Sub],
2091 ),
2092 ];
2093
2094 for (name, ops) in cases {
2095 let result = backend.compile_function(name, &ops, &config);
2096 assert!(
2097 result.is_ok(),
2098 "{name} must compile via the optimized->direct fallback \
2099 (issue #120), got: {:?}",
2100 result.as_ref().err()
2101 );
2102 assert!(
2103 !result.unwrap().code.is_empty(),
2104 "{name} must produce non-empty machine code"
2105 );
2106 }
2107 }
2108
2109 /// The fallback must still honor the ISA feature gate: f32 on a no-FPU
2110 /// target must fail cleanly (not panic) even on the optimized path.
2111 #[test]
2112 fn test_issue120_f32_div_rejected_on_no_fpu_via_optimized() {
2113 let backend = ArmBackend::new();
2114 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
2115 let config = CompileConfig {
2116 target: TargetSpec::cortex_m3(),
2117 ..CompileConfig::default()
2118 };
2119
2120 let result = backend.compile_function("fdiv", &ops, &config);
2121 assert!(
2122 result.is_err(),
2123 "f32.div must be rejected on Cortex-M3 (no FPU), not panic"
2124 );
2125 }
2126
2127 /// #507: a `br_table` function compiled via the DEFAULT (optimized) config
2128 /// must produce the SAME bytes as the direct (`no_optimize`) selector —
2129 /// i.e. the optimized path declined it to direct, lowering the dispatch as a
2130 /// real cmp-chain instead of silently dropping it (which left all arms in
2131 /// fall-through). Pre-fix the two outputs differed (the optimized one had no
2132 /// selector compare). Execution correctness is gated by
2133 /// `scripts/repro/br_table_507_differential.py`.
2134 #[test]
2135 fn test_507_br_table_declines_to_direct() {
2136 let backend = ArmBackend::new();
2137 // dispatch(sel): br_table over 3 blocks, each storing a marker to mem[0].
2138 let ops = vec![
2139 WasmOp::Block,
2140 WasmOp::Block,
2141 WasmOp::Block,
2142 WasmOp::LocalGet(0),
2143 WasmOp::BrTable {
2144 targets: vec![0, 1, 2],
2145 default: 2,
2146 },
2147 WasmOp::End,
2148 WasmOp::I32Const(0),
2149 WasmOp::I32Const(10),
2150 WasmOp::I32Store {
2151 offset: 0,
2152 align: 2,
2153 },
2154 WasmOp::Return,
2155 WasmOp::End,
2156 WasmOp::I32Const(0),
2157 WasmOp::I32Const(20),
2158 WasmOp::I32Store {
2159 offset: 0,
2160 align: 2,
2161 },
2162 WasmOp::Return,
2163 WasmOp::End,
2164 WasmOp::I32Const(0),
2165 WasmOp::I32Const(30),
2166 WasmOp::I32Store {
2167 offset: 0,
2168 align: 2,
2169 },
2170 ];
2171 let opt = CompileConfig {
2172 target: TargetSpec::cortex_m4(),
2173 ..CompileConfig::default()
2174 };
2175 let direct = CompileConfig {
2176 target: TargetSpec::cortex_m4(),
2177 no_optimize: true,
2178 ..CompileConfig::default()
2179 };
2180 let a = backend
2181 .compile_function("dispatch", &ops, &opt)
2182 .expect("optimized-default must compile br_table (via decline)");
2183 let b = backend
2184 .compile_function("dispatch", &ops, &direct)
2185 .expect("direct must compile br_table");
2186 assert_eq!(
2187 a.code, b.code,
2188 "#507: optimized-default br_table output must be byte-identical to the \
2189 direct selector (i.e. declined to direct), not a dropped dispatch"
2190 );
2191 }
2192
2193 /// Issue #94: end-to-end byte-size check for the canonical u64-packed
2194 /// FFI-return hi32 extract pattern. Compiles two near-identical
2195 /// functions — one with the optimized shift-by-32, one with a generic
2196 /// shift-by-7 — and asserts the optimized form is meaningfully smaller.
2197 #[test]
2198 fn test_issue94_hi32_extract_is_smaller_than_generic_shift() {
2199 let backend = ArmBackend::new();
2200 let config = CompileConfig {
2201 target: TargetSpec::cortex_m4f(),
2202 ..CompileConfig::default()
2203 };
2204
2205 // #518: the i64 value must NOT come from an i64 PARAM — the optimized
2206 // path now declines i64-param functions to the direct selector (it homed
2207 // an i64 param in R4:R5 instead of R0:R1, a silent miscompile this test's
2208 // byte-size-only assertion masked). The canonical #94 case is a u64 from
2209 // an FFI return, not a param, anyway. Source the i64 from a sign-extended
2210 // i32 param (`extend_i32_s`): a runtime, non-constant-foldable i64 that
2211 // stays on the optimized path, so the shift-by-32 hi-extract peephole is
2212 // still exercised on CORRECT code.
2213 // Optimized path: `(i64.extend_i32_s (local.get 0)) >>> 32; wrap_i64`
2214 let ops_hi32 = vec![
2215 WasmOp::LocalGet(0), // i32 param in R0
2216 WasmOp::I64ExtendI32S,
2217 WasmOp::I64Const(32),
2218 WasmOp::I64ShrU,
2219 WasmOp::I32WrapI64,
2220 ];
2221 let func_hi32 = backend
2222 .compile_function("hi32_extract", &ops_hi32, &config)
2223 .unwrap();
2224
2225 // Generic path: `... >>> 7; wrap_i64` — same shape, but the shift amount
2226 // is not a multiple of 32, so it falls through to the runtime shift.
2227 let ops_generic = vec![
2228 WasmOp::LocalGet(0),
2229 WasmOp::I64ExtendI32S,
2230 WasmOp::I64Const(7),
2231 WasmOp::I64ShrU,
2232 WasmOp::I32WrapI64,
2233 ];
2234 let func_generic = backend
2235 .compile_function("generic_shr", &ops_generic, &config)
2236 .unwrap();
2237
2238 let bytes_hi32 = func_hi32.code.len();
2239 let bytes_generic = func_generic.code.len();
2240 println!(
2241 "\n[issue #94] hi32 extract: {} bytes (vs generic shift: {} bytes; saved {})",
2242 bytes_hi32,
2243 bytes_generic,
2244 bytes_generic.saturating_sub(bytes_hi32)
2245 );
2246 let hex: String = func_hi32
2247 .code
2248 .iter()
2249 .map(|b| format!("{:02x}", b))
2250 .collect::<Vec<_>>()
2251 .join(" ");
2252 println!("[issue #94] hi32 bytes: {}", hex);
2253 // We expect the optimized form to be at least 30 bytes smaller than
2254 // the generic 64-bit shift sequence. (Empirically: 14 vs 50 bytes.)
2255 assert!(
2256 bytes_hi32 + 30 <= bytes_generic,
2257 "issue #94: hi32 extract = {} bytes, generic shift = {} bytes; \
2258 expected optimized form to be at least 30 bytes smaller",
2259 bytes_hi32,
2260 bytes_generic,
2261 );
2262 }
2263}