1use crate::ArmEncoder;
7use synth_core::backend::{
8 Backend, BackendCapabilities, BackendError, CodeRelocation, CompilationResult, CompileConfig,
9 CompiledFunction, SafetyBounds,
10};
11use synth_core::target::{IsaVariant, TargetSpec};
12use synth_core::wasm_decoder::DecodedModule;
13use synth_core::wasm_op::WasmOp;
14use synth_synthesis::{
15 ArmInstruction, ArmOp, BoundsCheckConfig, InstructionSelector, OptimizationConfig,
16 OptimizerBridge, RuleDatabase, validate_instructions,
17};
18
19pub struct ArmBackend;
21
22impl ArmBackend {
23 pub fn new() -> Self {
24 Self
25 }
26}
27
28impl Default for ArmBackend {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34impl Backend for ArmBackend {
35 fn name(&self) -> &str {
36 "arm"
37 }
38
39 fn capabilities(&self) -> BackendCapabilities {
40 BackendCapabilities {
41 produces_elf: false,
42 supports_rule_verification: true,
43 supports_binary_verification: true,
44 is_external: false,
45 }
46 }
47
48 fn supported_targets(&self) -> Vec<TargetSpec> {
49 vec![
50 TargetSpec::cortex_m3(),
51 TargetSpec::cortex_m4(),
52 TargetSpec::cortex_m4f(),
53 TargetSpec::cortex_m7(),
54 TargetSpec::cortex_m7dp(),
55 ]
56 }
57
58 fn compile_module(
59 &self,
60 module: &DecodedModule,
61 config: &CompileConfig,
62 ) -> Result<CompilationResult, BackendError> {
63 let exports: Vec<_> = module
64 .functions
65 .iter()
66 .filter(|f| f.export_name.is_some())
67 .collect();
68
69 if exports.is_empty() {
70 return Err(BackendError::CompilationFailed(
71 "no exported functions found".into(),
72 ));
73 }
74
75 let mut functions = Vec::new();
76 for func in &exports {
77 let name = func.export_name.clone().unwrap();
78 let compiled = self.compile_function(&name, &func.ops, config)?;
79 functions.push(compiled);
80 }
81
82 Ok(CompilationResult {
83 functions,
84 elf: None,
85 backend_name: self.name().to_string(),
86 })
87 }
88
89 fn compile_function(
90 &self,
91 name: &str,
92 ops: &[WasmOp],
93 config: &CompileConfig,
94 ) -> Result<CompiledFunction, BackendError> {
95 let (code, relocations) =
96 compile_wasm_to_arm(ops, config).map_err(BackendError::CompilationFailed)?;
97
98 Ok(CompiledFunction {
99 name: name.to_string(),
100 code,
101 wasm_ops: ops.to_vec(),
102 relocations,
103 })
104 }
105
106 fn is_available(&self) -> bool {
107 true }
109}
110
111fn count_params(wasm_ops: &[WasmOp]) -> u32 {
113 let mut first_access: std::collections::HashMap<u32, bool> = std::collections::HashMap::new();
114 for op in wasm_ops {
115 match op {
116 WasmOp::LocalGet(idx) => {
117 first_access.entry(*idx).or_insert(true);
118 }
119 WasmOp::LocalSet(idx) | WasmOp::LocalTee(idx) => {
120 first_access.entry(*idx).or_insert(false);
121 }
122 _ => {}
123 }
124 }
125
126 first_access
127 .iter()
128 .filter_map(
129 |(&idx, &is_read_first)| {
130 if is_read_first { Some(idx + 1) } else { None }
131 },
132 )
133 .max()
134 .unwrap_or(0)
135}
136
137fn compile_wasm_to_arm(
142 wasm_ops: &[WasmOp],
143 config: &CompileConfig,
144) -> Result<(Vec<u8>, Vec<CodeRelocation>), String> {
145 let num_params = count_params(wasm_ops);
146
147 let bounds_config = match config.effective_safety_bounds() {
148 SafetyBounds::None => BoundsCheckConfig::None,
149 SafetyBounds::Mpu => BoundsCheckConfig::Mpu,
150 SafetyBounds::Software => BoundsCheckConfig::Software,
151 SafetyBounds::Mask => BoundsCheckConfig::Masking,
152 };
153
154 let select_direct = || -> Result<Vec<ArmInstruction>, String> {
158 let db = RuleDatabase::with_standard_rules();
159 let mut selector =
160 InstructionSelector::with_bounds_check(db.rules().to_vec(), bounds_config);
161 selector.set_target(config.target.fpu, &config.target.triple);
162 if config.num_imports > 0 {
163 selector.set_num_imports(config.num_imports);
164 }
165 selector.set_func_arg_counts(
168 config.func_arg_counts.clone(),
169 config.type_arg_counts.clone(),
170 );
171 selector.set_relocatable(config.relocatable);
175 selector.set_native_pointer_abi(config.native_pointer_abi, config.linear_memory_bytes);
177 selector
178 .select_with_stack(wasm_ops, num_params)
179 .map_err(|e| format!("instruction selection failed: {}", e))
180 };
181
182 let arm_instrs = if config.no_optimize || config.relocatable {
191 select_direct()?
192 } else {
193 let opt_config = if config.loom_compat {
194 OptimizationConfig::loom_compat()
195 } else {
196 OptimizationConfig::all()
197 };
198
199 let mut bridge = OptimizerBridge::with_config(opt_config);
200 bridge.set_num_imports(config.num_imports);
204 match bridge
209 .optimize_full(wasm_ops)
210 .and_then(|(opt_ir, _cfg, _stats)| bridge.ir_to_arm(&opt_ir, num_params as usize))
211 {
212 Ok(arm_ops) => arm_ops
213 .into_iter()
214 .map(|op| ArmInstruction {
215 op,
216 source_line: None,
217 })
218 .collect(),
219 Err(_) => select_direct()?,
225 }
226 };
227
228 validate_instructions(&arm_instrs, config.target.fpu, &config.target.triple)
232 .map_err(|e| format!("ISA validation failed: {}", e))?;
233
234 let use_thumb2 = matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb);
236
237 let encoder = if use_thumb2 {
238 ArmEncoder::new_thumb2_with_fpu(config.target.fpu)
239 } else {
240 ArmEncoder::new_arm32()
241 };
242
243 let arm_instrs = if use_thumb2 {
250 resolve_label_branches(arm_instrs, &encoder)?
251 } else {
252 arm_instrs
253 };
254
255 let mut code = Vec::new();
256 let mut relocations = Vec::new();
257
258 for instr in &arm_instrs {
259 if let ArmOp::Bl { label } = &instr.op {
266 relocations.push(CodeRelocation {
267 offset: code.len() as u32,
268 symbol: label.clone(),
269 kind: synth_core::backend::RelocKind::ThmCall,
270 });
271 }
272 if let ArmOp::MovwSym { symbol, .. } = &instr.op {
276 relocations.push(CodeRelocation {
277 offset: code.len() as u32,
278 symbol: symbol.clone(),
279 kind: synth_core::backend::RelocKind::MovwAbs,
280 });
281 }
282 if let ArmOp::MovtSym { symbol, .. } = &instr.op {
283 relocations.push(CodeRelocation {
284 offset: code.len() as u32,
285 symbol: symbol.clone(),
286 kind: synth_core::backend::RelocKind::MovtAbs,
287 });
288 }
289
290 let encoded = encoder
291 .encode(&instr.op)
292 .map_err(|e| format!("ARM encoding failed: {}", e))?;
293 code.extend_from_slice(&encoded);
294 }
295
296 Ok((code, relocations))
297}
298
299fn resolve_label_branches(
317 arm_instrs: Vec<ArmInstruction>,
318 encoder: &ArmEncoder,
319) -> Result<Vec<ArmInstruction>, String> {
320 use std::collections::HashMap;
321 use synth_synthesis::Condition;
322
323 enum BKind {
324 Cond(Condition),
325 Uncond,
326 }
327 let mut branches: Vec<(usize, BKind, String)> = Vec::new();
329 for (i, instr) in arm_instrs.iter().enumerate() {
330 match &instr.op {
331 ArmOp::Bcc { cond, label } => branches.push((i, BKind::Cond(*cond), label.clone())),
332 ArmOp::Bhs { label } => branches.push((i, BKind::Cond(Condition::HS), label.clone())),
333 ArmOp::Blo { label } => branches.push((i, BKind::Cond(Condition::LO), label.clone())),
334 ArmOp::B { label } => branches.push((i, BKind::Uncond, label.clone())),
335 _ => {}
336 }
337 }
338 if branches.is_empty() {
339 return Ok(arm_instrs);
340 }
341
342 let mut resolved = arm_instrs;
343 for _ in 0..16 {
345 let mut positions = Vec::with_capacity(resolved.len());
347 let mut pos: i64 = 0;
348 for instr in &resolved {
349 positions.push(pos);
350 pos += encoder
351 .encode(&instr.op)
352 .map_err(|e| format!("branch-resolve size probe failed: {}", e))?
353 .len() as i64;
354 }
355 let mut labels: HashMap<String, i64> = HashMap::new();
357 for (i, instr) in resolved.iter().enumerate() {
358 if let ArmOp::Label { name } = &instr.op {
359 labels.insert(name.clone(), positions[i]);
360 }
361 }
362 let mut changed = false;
364 for (idx, kind, label) in &branches {
365 let Some(&target) = labels.get(label) else {
370 continue;
371 };
372 let halfword_offset = ((target - positions[*idx] - 4) / 2) as i32;
375 let new_op = match kind {
376 BKind::Cond(c) => ArmOp::BCondOffset {
377 cond: *c,
378 offset: halfword_offset,
379 },
380 BKind::Uncond => ArmOp::BOffset {
381 offset: halfword_offset,
382 },
383 };
384 if resolved[*idx].op != new_op {
385 resolved[*idx].op = new_op;
386 changed = true;
387 }
388 }
389 if !changed {
390 break;
391 }
392 }
393 Ok(resolved)
394}
395
396#[cfg(test)]
397mod tests {
398 use super::*;
399
400 #[test]
401 fn test_arm_backend_name() {
402 let backend = ArmBackend::new();
403 assert_eq!(backend.name(), "arm");
404 assert!(backend.is_available());
405 }
406
407 #[test]
408 fn test_arm_backend_capabilities() {
409 let backend = ArmBackend::new();
410 let caps = backend.capabilities();
411 assert!(!caps.produces_elf);
412 assert!(caps.supports_rule_verification);
413 assert!(!caps.is_external);
414 }
415
416 #[test]
417 fn test_compile_add_function() {
418 let backend = ArmBackend::new();
419 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
420 let config = CompileConfig::default();
421
422 let result = backend.compile_function("add", &ops, &config);
423 assert!(result.is_ok());
424
425 let func = result.unwrap();
426 assert_eq!(func.name, "add");
427 assert!(!func.code.is_empty());
428 assert_eq!(func.wasm_ops, ops);
429 }
430
431 #[test]
432 fn test_count_params() {
433 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
434 assert_eq!(count_params(&ops), 2);
435
436 let no_params = vec![WasmOp::I32Const(5), WasmOp::I32Const(3), WasmOp::I32Add];
437 assert_eq!(count_params(&no_params), 0);
438 }
439
440 #[test]
441 fn test_arm_backend_register() {
442 let mut registry = synth_core::BackendRegistry::new();
443 registry.register(Box::new(ArmBackend::new()));
444 assert!(registry.get("arm").is_some());
445 assert_eq!(registry.available().len(), 1);
446 }
447
448 #[test]
449 fn test_compile_import_call_produces_relocations() {
450 let backend = ArmBackend::new();
451 let ops = vec![WasmOp::Call(0)];
454 let config = CompileConfig {
455 num_imports: 1,
456 no_optimize: true, ..CompileConfig::default()
458 };
459
460 let result = backend.compile_function("caller", &ops, &config);
461 assert!(result.is_ok());
462
463 let func = result.unwrap();
464 assert!(!func.code.is_empty());
465 assert_eq!(func.relocations.len(), 1);
466 assert_eq!(func.relocations[0].symbol, "__meld_dispatch_import");
467 assert!(func.relocations[0].offset > 0);
469 }
470
471 #[test]
477 fn test_compile_relocatable_import_uses_direct_func_symbol_197() {
478 let backend = ArmBackend::new();
479 let ops = vec![WasmOp::Call(0)]; let config = CompileConfig {
481 num_imports: 1,
482 relocatable: true,
483 ..CompileConfig::default()
484 };
485
486 let func = backend
487 .compile_function("caller", &ops, &config)
488 .expect("relocatable import call compiles");
489
490 assert_eq!(func.relocations.len(), 1);
491 assert_eq!(
492 func.relocations[0].symbol, "func_0",
493 "#197: relocatable import must relocate against func_0 (→ field name), not Meld dispatch"
494 );
495 }
496
497 #[test]
498 fn test_compile_no_imports_no_relocations() {
499 let backend = ArmBackend::new();
500 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
501 let config = CompileConfig::default();
502
503 let func = backend.compile_function("add", &ops, &config).unwrap();
504 assert!(func.relocations.is_empty());
505 }
506
507 #[test]
514 fn test_compile_internal_call_produces_relocation_167() {
515 let backend = ArmBackend::new();
516 let ops = vec![WasmOp::Call(2)];
518 let config = CompileConfig {
519 num_imports: 1,
520 no_optimize: true,
521 ..CompileConfig::default()
522 };
523
524 let func = backend
525 .compile_function("caller", &ops, &config)
526 .expect("internal call compiles");
527
528 assert_eq!(
529 func.relocations.len(),
530 1,
531 "an internal call must emit exactly one relocation (#167)"
532 );
533 assert_eq!(
534 func.relocations[0].symbol, "func_2",
535 "internal call must relocate against the callee's func_{{index}} symbol (#167)"
536 );
537 }
538
539 #[test]
542 fn arm_safety_bounds_mpu_emits_same_code_as_none() {
543 let backend = ArmBackend::new();
547 let ops = vec![
548 WasmOp::LocalGet(0),
549 WasmOp::I32Load {
550 offset: 0,
551 align: 2,
552 },
553 ];
554 let cfg_none = CompileConfig {
555 no_optimize: true,
556 ..Default::default()
557 };
558 let cfg_mpu = CompileConfig {
559 no_optimize: true,
560 safety_bounds: SafetyBounds::Mpu,
561 ..Default::default()
562 };
563 let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
564 let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
565 assert_eq!(
566 n.code, m.code,
567 "Mpu and None should produce identical ARM bytes (Mpu relies on hardware)"
568 );
569 }
570
571 #[test]
572 fn arm_legacy_bounds_check_still_emits_software_check() {
573 let backend = ArmBackend::new();
576 let ops = vec![
577 WasmOp::LocalGet(0),
578 WasmOp::I32Load {
579 offset: 0,
580 align: 2,
581 },
582 ];
583 let cfg_legacy = CompileConfig {
584 no_optimize: true,
585 bounds_check: true,
586 ..Default::default()
587 };
588 let cfg_software = CompileConfig {
589 no_optimize: true,
590 safety_bounds: SafetyBounds::Software,
591 ..Default::default()
592 };
593 let l = backend.compile_function("ld", &ops, &cfg_legacy).unwrap();
594 let s = backend.compile_function("ld", &ops, &cfg_software).unwrap();
595 assert_eq!(
596 l.code, s.code,
597 "--bounds-check should produce the same bytes as --safety-bounds=software"
598 );
599 }
600
601 #[test]
607 fn test_f32_rejected_on_cortex_m3_no_fpu() {
608 let backend = ArmBackend::new();
609 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
610 let config = CompileConfig {
611 target: TargetSpec::cortex_m3(),
612 no_optimize: true,
613 ..CompileConfig::default()
614 };
615
616 let result = backend.compile_function("fadd", &ops, &config);
617 assert!(
618 result.is_err(),
619 "f32 operations should fail on Cortex-M3 (no FPU)"
620 );
621 }
622
623 #[test]
624 fn test_f32_accepted_on_cortex_m4f() {
625 let backend = ArmBackend::new();
626 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
627 let config = CompileConfig {
628 target: TargetSpec::cortex_m4f(),
629 no_optimize: true,
630 ..CompileConfig::default()
631 };
632
633 let result = backend.compile_function("fadd", &ops, &config);
634 assert!(
635 result.is_ok(),
636 "f32 operations should succeed on Cortex-M4F, got: {:?}",
637 result.unwrap_err()
638 );
639 }
640
641 #[test]
642 fn test_i32_works_on_all_targets() {
643 let backend = ArmBackend::new();
644 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
645
646 let config_m3 = CompileConfig {
648 target: TargetSpec::cortex_m3(),
649 no_optimize: true,
650 ..CompileConfig::default()
651 };
652 assert!(
653 backend.compile_function("add", &ops, &config_m3).is_ok(),
654 "i32 ops should work on Cortex-M3"
655 );
656
657 let config_m4f = CompileConfig {
659 target: TargetSpec::cortex_m4f(),
660 no_optimize: true,
661 ..CompileConfig::default()
662 };
663 assert!(
664 backend.compile_function("add", &ops, &config_m4f).is_ok(),
665 "i32 ops should work on Cortex-M4F"
666 );
667
668 let config_m7dp = CompileConfig {
670 target: TargetSpec::cortex_m7dp(),
671 no_optimize: true,
672 ..CompileConfig::default()
673 };
674 assert!(
675 backend.compile_function("add", &ops, &config_m7dp).is_ok(),
676 "i32 ops should work on Cortex-M7DP"
677 );
678 }
679
680 #[test]
681 fn test_f32_rejected_on_cortex_m4_no_fpu() {
682 let backend = ArmBackend::new();
684 let ops = vec![WasmOp::F32Const(1.5), WasmOp::F32Const(2.5), WasmOp::F32Mul];
685 let config = CompileConfig {
686 target: TargetSpec::cortex_m4(),
687 no_optimize: true,
688 ..CompileConfig::default()
689 };
690
691 let result = backend.compile_function("fmul", &ops, &config);
692 assert!(
693 result.is_err(),
694 "f32 operations should fail on Cortex-M4 (no FPU)"
695 );
696 }
697
698 #[test]
720 fn test_issue120_f32_div_compiles_via_optimized_default() {
721 let backend = ArmBackend::new();
722 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
723 let config = CompileConfig {
724 target: TargetSpec::cortex_m4f(),
725 ..CompileConfig::default()
728 };
729
730 let result = backend.compile_function("fdiv", &ops, &config);
731 assert!(
732 result.is_ok(),
733 "f32.div must compile on Cortex-M4F via the optimized->direct \
734 fallback (issue #120), got: {:?}",
735 result.as_ref().err()
736 );
737 assert!(
738 !result.unwrap().code.is_empty(),
739 "f32.div must produce non-empty machine code"
740 );
741 }
742
743 #[test]
746 fn test_issue120_assorted_f32_ops_compile_via_optimized_default() {
747 let backend = ArmBackend::new();
748 let config = CompileConfig {
749 target: TargetSpec::cortex_m4f(),
750 ..CompileConfig::default()
751 };
752
753 let cases: Vec<(&str, Vec<WasmOp>)> = vec![
754 (
755 "fadd",
756 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Add],
757 ),
758 (
759 "fmul",
760 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Mul],
761 ),
762 (
763 "fsub",
764 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Sub],
765 ),
766 ];
767
768 for (name, ops) in cases {
769 let result = backend.compile_function(name, &ops, &config);
770 assert!(
771 result.is_ok(),
772 "{name} must compile via the optimized->direct fallback \
773 (issue #120), got: {:?}",
774 result.as_ref().err()
775 );
776 assert!(
777 !result.unwrap().code.is_empty(),
778 "{name} must produce non-empty machine code"
779 );
780 }
781 }
782
783 #[test]
786 fn test_issue120_f32_div_rejected_on_no_fpu_via_optimized() {
787 let backend = ArmBackend::new();
788 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
789 let config = CompileConfig {
790 target: TargetSpec::cortex_m3(),
791 ..CompileConfig::default()
792 };
793
794 let result = backend.compile_function("fdiv", &ops, &config);
795 assert!(
796 result.is_err(),
797 "f32.div must be rejected on Cortex-M3 (no FPU), not panic"
798 );
799 }
800
801 #[test]
806 fn test_issue94_hi32_extract_is_smaller_than_generic_shift() {
807 let backend = ArmBackend::new();
808 let config = CompileConfig {
809 target: TargetSpec::cortex_m4f(),
810 ..CompileConfig::default()
811 };
812
813 let ops_hi32 = vec![
815 WasmOp::LocalGet(0), WasmOp::I64Const(32),
817 WasmOp::I64ShrU,
818 WasmOp::I32WrapI64,
819 ];
820 let func_hi32 = backend
821 .compile_function("hi32_extract", &ops_hi32, &config)
822 .unwrap();
823
824 let ops_generic = vec![
828 WasmOp::LocalGet(0),
829 WasmOp::I64Const(7),
830 WasmOp::I64ShrU,
831 WasmOp::I32WrapI64,
832 ];
833 let func_generic = backend
834 .compile_function("generic_shr", &ops_generic, &config)
835 .unwrap();
836
837 let bytes_hi32 = func_hi32.code.len();
838 let bytes_generic = func_generic.code.len();
839 println!(
840 "\n[issue #94] hi32 extract: {} bytes (vs generic shift: {} bytes; saved {})",
841 bytes_hi32,
842 bytes_generic,
843 bytes_generic.saturating_sub(bytes_hi32)
844 );
845 let hex: String = func_hi32
846 .code
847 .iter()
848 .map(|b| format!("{:02x}", b))
849 .collect::<Vec<_>>()
850 .join(" ");
851 println!("[issue #94] hi32 bytes: {}", hex);
852 assert!(
855 bytes_hi32 + 30 <= bytes_generic,
856 "issue #94: hi32 extract = {} bytes, generic shift = {} bytes; \
857 expected optimized form to be at least 30 bytes smaller",
858 bytes_hi32,
859 bytes_generic,
860 );
861 }
862}