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
166 .select_with_stack(wasm_ops, num_params)
167 .map_err(|e| format!("instruction selection failed: {}", e))
168 };
169
170 let arm_instrs = if config.no_optimize {
172 select_direct()?
173 } else {
174 let opt_config = if config.loom_compat {
175 OptimizationConfig::loom_compat()
176 } else {
177 OptimizationConfig::all()
178 };
179
180 let mut bridge = OptimizerBridge::with_config(opt_config);
181 bridge.set_num_imports(config.num_imports);
185 match bridge
190 .optimize_full(wasm_ops)
191 .and_then(|(opt_ir, _cfg, _stats)| bridge.ir_to_arm(&opt_ir, num_params as usize))
192 {
193 Ok(arm_ops) => arm_ops
194 .into_iter()
195 .map(|op| ArmInstruction {
196 op,
197 source_line: None,
198 })
199 .collect(),
200 Err(_) => select_direct()?,
206 }
207 };
208
209 validate_instructions(&arm_instrs, config.target.fpu, &config.target.triple)
213 .map_err(|e| format!("ISA validation failed: {}", e))?;
214
215 let use_thumb2 = matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb);
217
218 let encoder = if use_thumb2 {
219 ArmEncoder::new_thumb2_with_fpu(config.target.fpu)
220 } else {
221 ArmEncoder::new_arm32()
222 };
223
224 let mut code = Vec::new();
225 let mut relocations = Vec::new();
226
227 for instr in &arm_instrs {
228 if let ArmOp::Bl { label } = &instr.op {
235 relocations.push(CodeRelocation {
236 offset: code.len() as u32,
237 symbol: label.clone(),
238 });
239 }
240
241 let encoded = encoder
242 .encode(&instr.op)
243 .map_err(|e| format!("ARM encoding failed: {}", e))?;
244 code.extend_from_slice(&encoded);
245 }
246
247 Ok((code, relocations))
248}
249
250#[cfg(test)]
251mod tests {
252 use super::*;
253
254 #[test]
255 fn test_arm_backend_name() {
256 let backend = ArmBackend::new();
257 assert_eq!(backend.name(), "arm");
258 assert!(backend.is_available());
259 }
260
261 #[test]
262 fn test_arm_backend_capabilities() {
263 let backend = ArmBackend::new();
264 let caps = backend.capabilities();
265 assert!(!caps.produces_elf);
266 assert!(caps.supports_rule_verification);
267 assert!(!caps.is_external);
268 }
269
270 #[test]
271 fn test_compile_add_function() {
272 let backend = ArmBackend::new();
273 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
274 let config = CompileConfig::default();
275
276 let result = backend.compile_function("add", &ops, &config);
277 assert!(result.is_ok());
278
279 let func = result.unwrap();
280 assert_eq!(func.name, "add");
281 assert!(!func.code.is_empty());
282 assert_eq!(func.wasm_ops, ops);
283 }
284
285 #[test]
286 fn test_count_params() {
287 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
288 assert_eq!(count_params(&ops), 2);
289
290 let no_params = vec![WasmOp::I32Const(5), WasmOp::I32Const(3), WasmOp::I32Add];
291 assert_eq!(count_params(&no_params), 0);
292 }
293
294 #[test]
295 fn test_arm_backend_register() {
296 let mut registry = synth_core::BackendRegistry::new();
297 registry.register(Box::new(ArmBackend::new()));
298 assert!(registry.get("arm").is_some());
299 assert_eq!(registry.available().len(), 1);
300 }
301
302 #[test]
303 fn test_compile_import_call_produces_relocations() {
304 let backend = ArmBackend::new();
305 let ops = vec![WasmOp::Call(0)];
308 let config = CompileConfig {
309 num_imports: 1,
310 no_optimize: true, ..CompileConfig::default()
312 };
313
314 let result = backend.compile_function("caller", &ops, &config);
315 assert!(result.is_ok());
316
317 let func = result.unwrap();
318 assert!(!func.code.is_empty());
319 assert_eq!(func.relocations.len(), 1);
320 assert_eq!(func.relocations[0].symbol, "__meld_dispatch_import");
321 assert!(func.relocations[0].offset > 0);
323 }
324
325 #[test]
326 fn test_compile_no_imports_no_relocations() {
327 let backend = ArmBackend::new();
328 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
329 let config = CompileConfig::default();
330
331 let func = backend.compile_function("add", &ops, &config).unwrap();
332 assert!(func.relocations.is_empty());
333 }
334
335 #[test]
342 fn test_compile_internal_call_produces_relocation_167() {
343 let backend = ArmBackend::new();
344 let ops = vec![WasmOp::Call(2)];
346 let config = CompileConfig {
347 num_imports: 1,
348 no_optimize: true,
349 ..CompileConfig::default()
350 };
351
352 let func = backend
353 .compile_function("caller", &ops, &config)
354 .expect("internal call compiles");
355
356 assert_eq!(
357 func.relocations.len(),
358 1,
359 "an internal call must emit exactly one relocation (#167)"
360 );
361 assert_eq!(
362 func.relocations[0].symbol, "func_2",
363 "internal call must relocate against the callee's func_{{index}} symbol (#167)"
364 );
365 }
366
367 #[test]
370 fn arm_safety_bounds_mpu_emits_same_code_as_none() {
371 let backend = ArmBackend::new();
375 let ops = vec![
376 WasmOp::LocalGet(0),
377 WasmOp::I32Load {
378 offset: 0,
379 align: 2,
380 },
381 ];
382 let cfg_none = CompileConfig {
383 no_optimize: true,
384 ..Default::default()
385 };
386 let cfg_mpu = CompileConfig {
387 no_optimize: true,
388 safety_bounds: SafetyBounds::Mpu,
389 ..Default::default()
390 };
391 let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
392 let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
393 assert_eq!(
394 n.code, m.code,
395 "Mpu and None should produce identical ARM bytes (Mpu relies on hardware)"
396 );
397 }
398
399 #[test]
400 fn arm_legacy_bounds_check_still_emits_software_check() {
401 let backend = ArmBackend::new();
404 let ops = vec![
405 WasmOp::LocalGet(0),
406 WasmOp::I32Load {
407 offset: 0,
408 align: 2,
409 },
410 ];
411 let cfg_legacy = CompileConfig {
412 no_optimize: true,
413 bounds_check: true,
414 ..Default::default()
415 };
416 let cfg_software = CompileConfig {
417 no_optimize: true,
418 safety_bounds: SafetyBounds::Software,
419 ..Default::default()
420 };
421 let l = backend.compile_function("ld", &ops, &cfg_legacy).unwrap();
422 let s = backend.compile_function("ld", &ops, &cfg_software).unwrap();
423 assert_eq!(
424 l.code, s.code,
425 "--bounds-check should produce the same bytes as --safety-bounds=software"
426 );
427 }
428
429 #[test]
435 fn test_f32_rejected_on_cortex_m3_no_fpu() {
436 let backend = ArmBackend::new();
437 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
438 let config = CompileConfig {
439 target: TargetSpec::cortex_m3(),
440 no_optimize: true,
441 ..CompileConfig::default()
442 };
443
444 let result = backend.compile_function("fadd", &ops, &config);
445 assert!(
446 result.is_err(),
447 "f32 operations should fail on Cortex-M3 (no FPU)"
448 );
449 }
450
451 #[test]
452 fn test_f32_accepted_on_cortex_m4f() {
453 let backend = ArmBackend::new();
454 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
455 let config = CompileConfig {
456 target: TargetSpec::cortex_m4f(),
457 no_optimize: true,
458 ..CompileConfig::default()
459 };
460
461 let result = backend.compile_function("fadd", &ops, &config);
462 assert!(
463 result.is_ok(),
464 "f32 operations should succeed on Cortex-M4F, got: {:?}",
465 result.unwrap_err()
466 );
467 }
468
469 #[test]
470 fn test_i32_works_on_all_targets() {
471 let backend = ArmBackend::new();
472 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
473
474 let config_m3 = CompileConfig {
476 target: TargetSpec::cortex_m3(),
477 no_optimize: true,
478 ..CompileConfig::default()
479 };
480 assert!(
481 backend.compile_function("add", &ops, &config_m3).is_ok(),
482 "i32 ops should work on Cortex-M3"
483 );
484
485 let config_m4f = CompileConfig {
487 target: TargetSpec::cortex_m4f(),
488 no_optimize: true,
489 ..CompileConfig::default()
490 };
491 assert!(
492 backend.compile_function("add", &ops, &config_m4f).is_ok(),
493 "i32 ops should work on Cortex-M4F"
494 );
495
496 let config_m7dp = CompileConfig {
498 target: TargetSpec::cortex_m7dp(),
499 no_optimize: true,
500 ..CompileConfig::default()
501 };
502 assert!(
503 backend.compile_function("add", &ops, &config_m7dp).is_ok(),
504 "i32 ops should work on Cortex-M7DP"
505 );
506 }
507
508 #[test]
509 fn test_f32_rejected_on_cortex_m4_no_fpu() {
510 let backend = ArmBackend::new();
512 let ops = vec![WasmOp::F32Const(1.5), WasmOp::F32Const(2.5), WasmOp::F32Mul];
513 let config = CompileConfig {
514 target: TargetSpec::cortex_m4(),
515 no_optimize: true,
516 ..CompileConfig::default()
517 };
518
519 let result = backend.compile_function("fmul", &ops, &config);
520 assert!(
521 result.is_err(),
522 "f32 operations should fail on Cortex-M4 (no FPU)"
523 );
524 }
525
526 #[test]
548 fn test_issue120_f32_div_compiles_via_optimized_default() {
549 let backend = ArmBackend::new();
550 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
551 let config = CompileConfig {
552 target: TargetSpec::cortex_m4f(),
553 ..CompileConfig::default()
556 };
557
558 let result = backend.compile_function("fdiv", &ops, &config);
559 assert!(
560 result.is_ok(),
561 "f32.div must compile on Cortex-M4F via the optimized->direct \
562 fallback (issue #120), got: {:?}",
563 result.as_ref().err()
564 );
565 assert!(
566 !result.unwrap().code.is_empty(),
567 "f32.div must produce non-empty machine code"
568 );
569 }
570
571 #[test]
574 fn test_issue120_assorted_f32_ops_compile_via_optimized_default() {
575 let backend = ArmBackend::new();
576 let config = CompileConfig {
577 target: TargetSpec::cortex_m4f(),
578 ..CompileConfig::default()
579 };
580
581 let cases: Vec<(&str, Vec<WasmOp>)> = vec![
582 (
583 "fadd",
584 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Add],
585 ),
586 (
587 "fmul",
588 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Mul],
589 ),
590 (
591 "fsub",
592 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Sub],
593 ),
594 ];
595
596 for (name, ops) in cases {
597 let result = backend.compile_function(name, &ops, &config);
598 assert!(
599 result.is_ok(),
600 "{name} must compile via the optimized->direct fallback \
601 (issue #120), got: {:?}",
602 result.as_ref().err()
603 );
604 assert!(
605 !result.unwrap().code.is_empty(),
606 "{name} must produce non-empty machine code"
607 );
608 }
609 }
610
611 #[test]
614 fn test_issue120_f32_div_rejected_on_no_fpu_via_optimized() {
615 let backend = ArmBackend::new();
616 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
617 let config = CompileConfig {
618 target: TargetSpec::cortex_m3(),
619 ..CompileConfig::default()
620 };
621
622 let result = backend.compile_function("fdiv", &ops, &config);
623 assert!(
624 result.is_err(),
625 "f32.div must be rejected on Cortex-M3 (no FPU), not panic"
626 );
627 }
628
629 #[test]
634 fn test_issue94_hi32_extract_is_smaller_than_generic_shift() {
635 let backend = ArmBackend::new();
636 let config = CompileConfig {
637 target: TargetSpec::cortex_m4f(),
638 ..CompileConfig::default()
639 };
640
641 let ops_hi32 = vec![
643 WasmOp::LocalGet(0), WasmOp::I64Const(32),
645 WasmOp::I64ShrU,
646 WasmOp::I32WrapI64,
647 ];
648 let func_hi32 = backend
649 .compile_function("hi32_extract", &ops_hi32, &config)
650 .unwrap();
651
652 let ops_generic = vec![
656 WasmOp::LocalGet(0),
657 WasmOp::I64Const(7),
658 WasmOp::I64ShrU,
659 WasmOp::I32WrapI64,
660 ];
661 let func_generic = backend
662 .compile_function("generic_shr", &ops_generic, &config)
663 .unwrap();
664
665 let bytes_hi32 = func_hi32.code.len();
666 let bytes_generic = func_generic.code.len();
667 println!(
668 "\n[issue #94] hi32 extract: {} bytes (vs generic shift: {} bytes; saved {})",
669 bytes_hi32,
670 bytes_generic,
671 bytes_generic.saturating_sub(bytes_hi32)
672 );
673 let hex: String = func_hi32
674 .code
675 .iter()
676 .map(|b| format!("{:02x}", b))
677 .collect::<Vec<_>>()
678 .join(" ");
679 println!("[issue #94] hi32 bytes: {}", hex);
680 assert!(
683 bytes_hi32 + 30 <= bytes_generic,
684 "issue #94: hi32 extract = {} bytes, generic shift = {} bytes; \
685 expected optimized form to be at least 30 bytes smaller",
686 bytes_hi32,
687 bytes_generic,
688 );
689 }
690}