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