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
176 .select_with_stack(wasm_ops, num_params)
177 .map_err(|e| format!("instruction selection failed: {}", e))
178 };
179
180 let arm_instrs = if config.no_optimize || config.relocatable {
189 select_direct()?
190 } else {
191 let opt_config = if config.loom_compat {
192 OptimizationConfig::loom_compat()
193 } else {
194 OptimizationConfig::all()
195 };
196
197 let mut bridge = OptimizerBridge::with_config(opt_config);
198 bridge.set_num_imports(config.num_imports);
202 match bridge
207 .optimize_full(wasm_ops)
208 .and_then(|(opt_ir, _cfg, _stats)| bridge.ir_to_arm(&opt_ir, num_params as usize))
209 {
210 Ok(arm_ops) => arm_ops
211 .into_iter()
212 .map(|op| ArmInstruction {
213 op,
214 source_line: None,
215 })
216 .collect(),
217 Err(_) => select_direct()?,
223 }
224 };
225
226 validate_instructions(&arm_instrs, config.target.fpu, &config.target.triple)
230 .map_err(|e| format!("ISA validation failed: {}", e))?;
231
232 let use_thumb2 = matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb);
234
235 let encoder = if use_thumb2 {
236 ArmEncoder::new_thumb2_with_fpu(config.target.fpu)
237 } else {
238 ArmEncoder::new_arm32()
239 };
240
241 let arm_instrs = if use_thumb2 {
248 resolve_label_branches(arm_instrs, &encoder)?
249 } else {
250 arm_instrs
251 };
252
253 let mut code = Vec::new();
254 let mut relocations = Vec::new();
255
256 for instr in &arm_instrs {
257 if let ArmOp::Bl { label } = &instr.op {
264 relocations.push(CodeRelocation {
265 offset: code.len() as u32,
266 symbol: label.clone(),
267 });
268 }
269
270 let encoded = encoder
271 .encode(&instr.op)
272 .map_err(|e| format!("ARM encoding failed: {}", e))?;
273 code.extend_from_slice(&encoded);
274 }
275
276 Ok((code, relocations))
277}
278
279fn resolve_label_branches(
297 arm_instrs: Vec<ArmInstruction>,
298 encoder: &ArmEncoder,
299) -> Result<Vec<ArmInstruction>, String> {
300 use std::collections::HashMap;
301 use synth_synthesis::Condition;
302
303 enum BKind {
304 Cond(Condition),
305 Uncond,
306 }
307 let mut branches: Vec<(usize, BKind, String)> = Vec::new();
309 for (i, instr) in arm_instrs.iter().enumerate() {
310 match &instr.op {
311 ArmOp::Bcc { cond, label } => branches.push((i, BKind::Cond(*cond), label.clone())),
312 ArmOp::Bhs { label } => branches.push((i, BKind::Cond(Condition::HS), label.clone())),
313 ArmOp::Blo { label } => branches.push((i, BKind::Cond(Condition::LO), label.clone())),
314 ArmOp::B { label } => branches.push((i, BKind::Uncond, label.clone())),
315 _ => {}
316 }
317 }
318 if branches.is_empty() {
319 return Ok(arm_instrs);
320 }
321
322 let mut resolved = arm_instrs;
323 for _ in 0..16 {
325 let mut positions = Vec::with_capacity(resolved.len());
327 let mut pos: i64 = 0;
328 for instr in &resolved {
329 positions.push(pos);
330 pos += encoder
331 .encode(&instr.op)
332 .map_err(|e| format!("branch-resolve size probe failed: {}", e))?
333 .len() as i64;
334 }
335 let mut labels: HashMap<String, i64> = HashMap::new();
337 for (i, instr) in resolved.iter().enumerate() {
338 if let ArmOp::Label { name } = &instr.op {
339 labels.insert(name.clone(), positions[i]);
340 }
341 }
342 let mut changed = false;
344 for (idx, kind, label) in &branches {
345 let Some(&target) = labels.get(label) else {
350 continue;
351 };
352 let halfword_offset = ((target - positions[*idx] - 4) / 2) as i32;
355 let new_op = match kind {
356 BKind::Cond(c) => ArmOp::BCondOffset {
357 cond: *c,
358 offset: halfword_offset,
359 },
360 BKind::Uncond => ArmOp::BOffset {
361 offset: halfword_offset,
362 },
363 };
364 if resolved[*idx].op != new_op {
365 resolved[*idx].op = new_op;
366 changed = true;
367 }
368 }
369 if !changed {
370 break;
371 }
372 }
373 Ok(resolved)
374}
375
376#[cfg(test)]
377mod tests {
378 use super::*;
379
380 #[test]
381 fn test_arm_backend_name() {
382 let backend = ArmBackend::new();
383 assert_eq!(backend.name(), "arm");
384 assert!(backend.is_available());
385 }
386
387 #[test]
388 fn test_arm_backend_capabilities() {
389 let backend = ArmBackend::new();
390 let caps = backend.capabilities();
391 assert!(!caps.produces_elf);
392 assert!(caps.supports_rule_verification);
393 assert!(!caps.is_external);
394 }
395
396 #[test]
397 fn test_compile_add_function() {
398 let backend = ArmBackend::new();
399 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
400 let config = CompileConfig::default();
401
402 let result = backend.compile_function("add", &ops, &config);
403 assert!(result.is_ok());
404
405 let func = result.unwrap();
406 assert_eq!(func.name, "add");
407 assert!(!func.code.is_empty());
408 assert_eq!(func.wasm_ops, ops);
409 }
410
411 #[test]
412 fn test_count_params() {
413 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
414 assert_eq!(count_params(&ops), 2);
415
416 let no_params = vec![WasmOp::I32Const(5), WasmOp::I32Const(3), WasmOp::I32Add];
417 assert_eq!(count_params(&no_params), 0);
418 }
419
420 #[test]
421 fn test_arm_backend_register() {
422 let mut registry = synth_core::BackendRegistry::new();
423 registry.register(Box::new(ArmBackend::new()));
424 assert!(registry.get("arm").is_some());
425 assert_eq!(registry.available().len(), 1);
426 }
427
428 #[test]
429 fn test_compile_import_call_produces_relocations() {
430 let backend = ArmBackend::new();
431 let ops = vec![WasmOp::Call(0)];
434 let config = CompileConfig {
435 num_imports: 1,
436 no_optimize: true, ..CompileConfig::default()
438 };
439
440 let result = backend.compile_function("caller", &ops, &config);
441 assert!(result.is_ok());
442
443 let func = result.unwrap();
444 assert!(!func.code.is_empty());
445 assert_eq!(func.relocations.len(), 1);
446 assert_eq!(func.relocations[0].symbol, "__meld_dispatch_import");
447 assert!(func.relocations[0].offset > 0);
449 }
450
451 #[test]
457 fn test_compile_relocatable_import_uses_direct_func_symbol_197() {
458 let backend = ArmBackend::new();
459 let ops = vec![WasmOp::Call(0)]; let config = CompileConfig {
461 num_imports: 1,
462 relocatable: true,
463 ..CompileConfig::default()
464 };
465
466 let func = backend
467 .compile_function("caller", &ops, &config)
468 .expect("relocatable import call compiles");
469
470 assert_eq!(func.relocations.len(), 1);
471 assert_eq!(
472 func.relocations[0].symbol, "func_0",
473 "#197: relocatable import must relocate against func_0 (→ field name), not Meld dispatch"
474 );
475 }
476
477 #[test]
478 fn test_compile_no_imports_no_relocations() {
479 let backend = ArmBackend::new();
480 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
481 let config = CompileConfig::default();
482
483 let func = backend.compile_function("add", &ops, &config).unwrap();
484 assert!(func.relocations.is_empty());
485 }
486
487 #[test]
494 fn test_compile_internal_call_produces_relocation_167() {
495 let backend = ArmBackend::new();
496 let ops = vec![WasmOp::Call(2)];
498 let config = CompileConfig {
499 num_imports: 1,
500 no_optimize: true,
501 ..CompileConfig::default()
502 };
503
504 let func = backend
505 .compile_function("caller", &ops, &config)
506 .expect("internal call compiles");
507
508 assert_eq!(
509 func.relocations.len(),
510 1,
511 "an internal call must emit exactly one relocation (#167)"
512 );
513 assert_eq!(
514 func.relocations[0].symbol, "func_2",
515 "internal call must relocate against the callee's func_{{index}} symbol (#167)"
516 );
517 }
518
519 #[test]
522 fn arm_safety_bounds_mpu_emits_same_code_as_none() {
523 let backend = ArmBackend::new();
527 let ops = vec![
528 WasmOp::LocalGet(0),
529 WasmOp::I32Load {
530 offset: 0,
531 align: 2,
532 },
533 ];
534 let cfg_none = CompileConfig {
535 no_optimize: true,
536 ..Default::default()
537 };
538 let cfg_mpu = CompileConfig {
539 no_optimize: true,
540 safety_bounds: SafetyBounds::Mpu,
541 ..Default::default()
542 };
543 let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
544 let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
545 assert_eq!(
546 n.code, m.code,
547 "Mpu and None should produce identical ARM bytes (Mpu relies on hardware)"
548 );
549 }
550
551 #[test]
552 fn arm_legacy_bounds_check_still_emits_software_check() {
553 let backend = ArmBackend::new();
556 let ops = vec![
557 WasmOp::LocalGet(0),
558 WasmOp::I32Load {
559 offset: 0,
560 align: 2,
561 },
562 ];
563 let cfg_legacy = CompileConfig {
564 no_optimize: true,
565 bounds_check: true,
566 ..Default::default()
567 };
568 let cfg_software = CompileConfig {
569 no_optimize: true,
570 safety_bounds: SafetyBounds::Software,
571 ..Default::default()
572 };
573 let l = backend.compile_function("ld", &ops, &cfg_legacy).unwrap();
574 let s = backend.compile_function("ld", &ops, &cfg_software).unwrap();
575 assert_eq!(
576 l.code, s.code,
577 "--bounds-check should produce the same bytes as --safety-bounds=software"
578 );
579 }
580
581 #[test]
587 fn test_f32_rejected_on_cortex_m3_no_fpu() {
588 let backend = ArmBackend::new();
589 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
590 let config = CompileConfig {
591 target: TargetSpec::cortex_m3(),
592 no_optimize: true,
593 ..CompileConfig::default()
594 };
595
596 let result = backend.compile_function("fadd", &ops, &config);
597 assert!(
598 result.is_err(),
599 "f32 operations should fail on Cortex-M3 (no FPU)"
600 );
601 }
602
603 #[test]
604 fn test_f32_accepted_on_cortex_m4f() {
605 let backend = ArmBackend::new();
606 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
607 let config = CompileConfig {
608 target: TargetSpec::cortex_m4f(),
609 no_optimize: true,
610 ..CompileConfig::default()
611 };
612
613 let result = backend.compile_function("fadd", &ops, &config);
614 assert!(
615 result.is_ok(),
616 "f32 operations should succeed on Cortex-M4F, got: {:?}",
617 result.unwrap_err()
618 );
619 }
620
621 #[test]
622 fn test_i32_works_on_all_targets() {
623 let backend = ArmBackend::new();
624 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
625
626 let config_m3 = CompileConfig {
628 target: TargetSpec::cortex_m3(),
629 no_optimize: true,
630 ..CompileConfig::default()
631 };
632 assert!(
633 backend.compile_function("add", &ops, &config_m3).is_ok(),
634 "i32 ops should work on Cortex-M3"
635 );
636
637 let config_m4f = CompileConfig {
639 target: TargetSpec::cortex_m4f(),
640 no_optimize: true,
641 ..CompileConfig::default()
642 };
643 assert!(
644 backend.compile_function("add", &ops, &config_m4f).is_ok(),
645 "i32 ops should work on Cortex-M4F"
646 );
647
648 let config_m7dp = CompileConfig {
650 target: TargetSpec::cortex_m7dp(),
651 no_optimize: true,
652 ..CompileConfig::default()
653 };
654 assert!(
655 backend.compile_function("add", &ops, &config_m7dp).is_ok(),
656 "i32 ops should work on Cortex-M7DP"
657 );
658 }
659
660 #[test]
661 fn test_f32_rejected_on_cortex_m4_no_fpu() {
662 let backend = ArmBackend::new();
664 let ops = vec![WasmOp::F32Const(1.5), WasmOp::F32Const(2.5), WasmOp::F32Mul];
665 let config = CompileConfig {
666 target: TargetSpec::cortex_m4(),
667 no_optimize: true,
668 ..CompileConfig::default()
669 };
670
671 let result = backend.compile_function("fmul", &ops, &config);
672 assert!(
673 result.is_err(),
674 "f32 operations should fail on Cortex-M4 (no FPU)"
675 );
676 }
677
678 #[test]
700 fn test_issue120_f32_div_compiles_via_optimized_default() {
701 let backend = ArmBackend::new();
702 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
703 let config = CompileConfig {
704 target: TargetSpec::cortex_m4f(),
705 ..CompileConfig::default()
708 };
709
710 let result = backend.compile_function("fdiv", &ops, &config);
711 assert!(
712 result.is_ok(),
713 "f32.div must compile on Cortex-M4F via the optimized->direct \
714 fallback (issue #120), got: {:?}",
715 result.as_ref().err()
716 );
717 assert!(
718 !result.unwrap().code.is_empty(),
719 "f32.div must produce non-empty machine code"
720 );
721 }
722
723 #[test]
726 fn test_issue120_assorted_f32_ops_compile_via_optimized_default() {
727 let backend = ArmBackend::new();
728 let config = CompileConfig {
729 target: TargetSpec::cortex_m4f(),
730 ..CompileConfig::default()
731 };
732
733 let cases: Vec<(&str, Vec<WasmOp>)> = vec![
734 (
735 "fadd",
736 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Add],
737 ),
738 (
739 "fmul",
740 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Mul],
741 ),
742 (
743 "fsub",
744 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Sub],
745 ),
746 ];
747
748 for (name, ops) in cases {
749 let result = backend.compile_function(name, &ops, &config);
750 assert!(
751 result.is_ok(),
752 "{name} must compile via the optimized->direct fallback \
753 (issue #120), got: {:?}",
754 result.as_ref().err()
755 );
756 assert!(
757 !result.unwrap().code.is_empty(),
758 "{name} must produce non-empty machine code"
759 );
760 }
761 }
762
763 #[test]
766 fn test_issue120_f32_div_rejected_on_no_fpu_via_optimized() {
767 let backend = ArmBackend::new();
768 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
769 let config = CompileConfig {
770 target: TargetSpec::cortex_m3(),
771 ..CompileConfig::default()
772 };
773
774 let result = backend.compile_function("fdiv", &ops, &config);
775 assert!(
776 result.is_err(),
777 "f32.div must be rejected on Cortex-M3 (no FPU), not panic"
778 );
779 }
780
781 #[test]
786 fn test_issue94_hi32_extract_is_smaller_than_generic_shift() {
787 let backend = ArmBackend::new();
788 let config = CompileConfig {
789 target: TargetSpec::cortex_m4f(),
790 ..CompileConfig::default()
791 };
792
793 let ops_hi32 = vec![
795 WasmOp::LocalGet(0), WasmOp::I64Const(32),
797 WasmOp::I64ShrU,
798 WasmOp::I32WrapI64,
799 ];
800 let func_hi32 = backend
801 .compile_function("hi32_extract", &ops_hi32, &config)
802 .unwrap();
803
804 let ops_generic = vec![
808 WasmOp::LocalGet(0),
809 WasmOp::I64Const(7),
810 WasmOp::I64ShrU,
811 WasmOp::I32WrapI64,
812 ];
813 let func_generic = backend
814 .compile_function("generic_shr", &ops_generic, &config)
815 .unwrap();
816
817 let bytes_hi32 = func_hi32.code.len();
818 let bytes_generic = func_generic.code.len();
819 println!(
820 "\n[issue #94] hi32 extract: {} bytes (vs generic shift: {} bytes; saved {})",
821 bytes_hi32,
822 bytes_generic,
823 bytes_generic.saturating_sub(bytes_hi32)
824 );
825 let hex: String = func_hi32
826 .code
827 .iter()
828 .map(|b| format!("{:02x}", b))
829 .collect::<Vec<_>>()
830 .join(" ");
831 println!("[issue #94] hi32 bytes: {}", hex);
832 assert!(
835 bytes_hi32 + 30 <= bytes_generic,
836 "issue #94: hi32 extract = {} bytes, generic shift = {} bytes; \
837 expected optimized form to be at least 30 bytes smaller",
838 bytes_hi32,
839 bytes_generic,
840 );
841 }
842}