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.set_result_types(config.func_ret_i64.clone(), config.type_ret_i64.clone());
179 if config.native_pointer_abi
183 && let Some((sp_idx, sp_init)) = config.stack_pointer_global
184 {
185 selector.set_native_pointer_stack(sp_idx, sp_init);
186 }
187 selector
188 .select_with_stack(wasm_ops, num_params)
189 .map_err(|e| format!("instruction selection failed: {}", e))
190 };
191
192 let arm_instrs = if config.no_optimize || config.relocatable {
201 select_direct()?
202 } else {
203 let opt_config = if config.loom_compat {
204 OptimizationConfig::loom_compat()
205 } else {
206 OptimizationConfig::all()
207 };
208
209 let mut bridge = OptimizerBridge::with_config(opt_config);
210 bridge.set_num_imports(config.num_imports);
214 match bridge
219 .optimize_full(wasm_ops)
220 .and_then(|(opt_ir, _cfg, _stats)| bridge.ir_to_arm(&opt_ir, num_params as usize))
221 {
222 Ok(arm_ops) => arm_ops
223 .into_iter()
224 .map(|op| ArmInstruction {
225 op,
226 source_line: None,
227 })
228 .collect(),
229 Err(_) => select_direct()?,
235 }
236 };
237
238 let arm_instrs = if std::env::var("SYNTH_CONST_CSE").is_ok() {
263 synth_synthesis::liveness::apply_const_cse(&arm_instrs).0
264 } else {
265 arm_instrs
266 };
267
268 let realloc_on = std::env::var("SYNTH_RANGE_REALLOC").map_or(true, |v| v != "0");
291 let arm_instrs = if realloc_on {
292 use synth_synthesis::rules::Reg;
293 const POOL: [Reg; 9] = [
294 Reg::R0,
295 Reg::R1,
296 Reg::R2,
297 Reg::R3,
298 Reg::R4,
299 Reg::R5,
300 Reg::R6,
301 Reg::R7,
302 Reg::R8,
303 ];
304 let (out, stats) = synth_synthesis::liveness::reallocate_function(&arm_instrs, &POOL);
305 if std::env::var("SYNTH_REALLOC_STATS").is_ok() {
306 eprintln!(
307 "[range-realloc] {} segments: {} reallocated, {} declined, {} need spill (step 4)",
308 stats.segments, stats.reallocated, stats.declined, stats.needs_spill
309 );
310 }
311 synth_synthesis::liveness::shrink_callee_saved_saves(&out).unwrap_or(out)
312 } else {
313 arm_instrs
314 };
315
316 if std::env::var("SYNTH_SHADOW_ALLOC").is_ok() {
324 use synth_synthesis::liveness::{
325 AllocationOutcome, allocate_function, function_peak_pressure,
326 };
327 let precolored = std::collections::BTreeMap::from([
330 (synth_synthesis::rules::Reg::R9, 9usize),
331 (synth_synthesis::rules::Reg::R10, 10),
332 (synth_synthesis::rules::Reg::R11, 11),
333 (synth_synthesis::rules::Reg::R12, 12),
334 ]);
335 let peak = function_peak_pressure(&arm_instrs);
339 match allocate_function(&arm_instrs, 9, &precolored) {
340 AllocationOutcome::Allocated {
341 remat_opportunities,
342 coloring,
343 } => eprintln!(
344 "[shadow-alloc] OK: {} pregs coloured within R0-R8 pool, peak value-pressure {}, {} const-CSE/remat opportunities",
345 coloring.len(),
346 peak,
347 remat_opportunities
348 ),
349 AllocationOutcome::NeedsSpill(s) => eprintln!(
350 "[shadow-alloc] physical-graph would spill {:?}, but peak value-pressure is {} (≤9 ⇒ spurious; fits once virtually allocated)",
351 s, peak
352 ),
353 AllocationOutcome::Declined => {
354 eprintln!(
355 "[shadow-alloc] declined (unmodeled construct — calls/i64/fp/offset-branch)"
356 )
357 }
358 }
359 }
360
361 validate_instructions(&arm_instrs, config.target.fpu, &config.target.triple)
365 .map_err(|e| format!("ISA validation failed: {}", e))?;
366
367 let use_thumb2 = matches!(config.target.isa, IsaVariant::Thumb2 | IsaVariant::Thumb);
369
370 let encoder = if use_thumb2 {
371 ArmEncoder::new_thumb2_with_fpu(config.target.fpu)
372 } else {
373 ArmEncoder::new_arm32()
374 };
375
376 let arm_instrs = if use_thumb2 {
383 resolve_label_branches(arm_instrs, &encoder)?
384 } else {
385 arm_instrs
386 };
387
388 let mut code = Vec::new();
389 let mut relocations = Vec::new();
390
391 for instr in &arm_instrs {
392 if let ArmOp::Bl { label } = &instr.op {
399 relocations.push(CodeRelocation {
400 offset: code.len() as u32,
401 symbol: label.clone(),
402 kind: synth_core::backend::RelocKind::ThmCall,
403 });
404 }
405 if let ArmOp::MovwSym { symbol, .. } = &instr.op {
409 relocations.push(CodeRelocation {
410 offset: code.len() as u32,
411 symbol: symbol.clone(),
412 kind: synth_core::backend::RelocKind::MovwAbs,
413 });
414 }
415 if let ArmOp::MovtSym { symbol, .. } = &instr.op {
416 relocations.push(CodeRelocation {
417 offset: code.len() as u32,
418 symbol: symbol.clone(),
419 kind: synth_core::backend::RelocKind::MovtAbs,
420 });
421 }
422
423 let encoded = encoder
424 .encode(&instr.op)
425 .map_err(|e| format!("ARM encoding failed: {}", e))?;
426 code.extend_from_slice(&encoded);
427 }
428
429 Ok((code, relocations))
430}
431
432fn resolve_label_branches(
450 arm_instrs: Vec<ArmInstruction>,
451 encoder: &ArmEncoder,
452) -> Result<Vec<ArmInstruction>, String> {
453 use std::collections::HashMap;
454 use synth_synthesis::Condition;
455
456 enum BKind {
457 Cond(Condition),
458 Uncond,
459 }
460 let mut branches: Vec<(usize, BKind, String)> = Vec::new();
462 for (i, instr) in arm_instrs.iter().enumerate() {
463 match &instr.op {
464 ArmOp::Bcc { cond, label } => branches.push((i, BKind::Cond(*cond), label.clone())),
465 ArmOp::Bhs { label } => branches.push((i, BKind::Cond(Condition::HS), label.clone())),
466 ArmOp::Blo { label } => branches.push((i, BKind::Cond(Condition::LO), label.clone())),
467 ArmOp::B { label } => branches.push((i, BKind::Uncond, label.clone())),
468 _ => {}
469 }
470 }
471 if branches.is_empty() {
472 return Ok(arm_instrs);
473 }
474
475 let mut resolved = arm_instrs;
476 for _ in 0..16 {
478 let mut positions = Vec::with_capacity(resolved.len());
480 let mut pos: i64 = 0;
481 for instr in &resolved {
482 positions.push(pos);
483 pos += encoder
484 .encode(&instr.op)
485 .map_err(|e| format!("branch-resolve size probe failed: {}", e))?
486 .len() as i64;
487 }
488 let mut labels: HashMap<String, i64> = HashMap::new();
490 for (i, instr) in resolved.iter().enumerate() {
491 if let ArmOp::Label { name } = &instr.op {
492 labels.insert(name.clone(), positions[i]);
493 }
494 }
495 let mut changed = false;
497 for (idx, kind, label) in &branches {
498 let Some(&target) = labels.get(label) else {
503 continue;
504 };
505 let halfword_offset = ((target - positions[*idx] - 4) / 2) as i32;
508 let new_op = match kind {
509 BKind::Cond(c) => ArmOp::BCondOffset {
510 cond: *c,
511 offset: halfword_offset,
512 },
513 BKind::Uncond => ArmOp::BOffset {
514 offset: halfword_offset,
515 },
516 };
517 if resolved[*idx].op != new_op {
518 resolved[*idx].op = new_op;
519 changed = true;
520 }
521 }
522 if !changed {
523 break;
524 }
525 }
526 Ok(resolved)
527}
528
529#[cfg(test)]
530mod tests {
531 use super::*;
532
533 #[test]
534 fn test_arm_backend_name() {
535 let backend = ArmBackend::new();
536 assert_eq!(backend.name(), "arm");
537 assert!(backend.is_available());
538 }
539
540 #[test]
541 fn test_arm_backend_capabilities() {
542 let backend = ArmBackend::new();
543 let caps = backend.capabilities();
544 assert!(!caps.produces_elf);
545 assert!(caps.supports_rule_verification);
546 assert!(!caps.is_external);
547 }
548
549 #[test]
550 fn test_compile_add_function() {
551 let backend = ArmBackend::new();
552 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
553 let config = CompileConfig::default();
554
555 let result = backend.compile_function("add", &ops, &config);
556 assert!(result.is_ok());
557
558 let func = result.unwrap();
559 assert_eq!(func.name, "add");
560 assert!(!func.code.is_empty());
561 assert_eq!(func.wasm_ops, ops);
562 }
563
564 #[test]
565 fn test_count_params() {
566 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
567 assert_eq!(count_params(&ops), 2);
568
569 let no_params = vec![WasmOp::I32Const(5), WasmOp::I32Const(3), WasmOp::I32Add];
570 assert_eq!(count_params(&no_params), 0);
571 }
572
573 #[test]
574 fn test_arm_backend_register() {
575 let mut registry = synth_core::BackendRegistry::new();
576 registry.register(Box::new(ArmBackend::new()));
577 assert!(registry.get("arm").is_some());
578 assert_eq!(registry.available().len(), 1);
579 }
580
581 #[test]
582 fn test_compile_import_call_produces_relocations() {
583 let backend = ArmBackend::new();
584 let ops = vec![WasmOp::Call(0)];
587 let config = CompileConfig {
588 num_imports: 1,
589 no_optimize: true, ..CompileConfig::default()
591 };
592
593 let result = backend.compile_function("caller", &ops, &config);
594 assert!(result.is_ok());
595
596 let func = result.unwrap();
597 assert!(!func.code.is_empty());
598 assert_eq!(func.relocations.len(), 1);
599 assert_eq!(func.relocations[0].symbol, "__meld_dispatch_import");
600 assert!(func.relocations[0].offset > 0);
602 }
603
604 #[test]
610 fn test_compile_relocatable_import_uses_direct_func_symbol_197() {
611 let backend = ArmBackend::new();
612 let ops = vec![WasmOp::Call(0)]; let config = CompileConfig {
614 num_imports: 1,
615 relocatable: true,
616 ..CompileConfig::default()
617 };
618
619 let func = backend
620 .compile_function("caller", &ops, &config)
621 .expect("relocatable import call compiles");
622
623 assert_eq!(func.relocations.len(), 1);
624 assert_eq!(
625 func.relocations[0].symbol, "func_0",
626 "#197: relocatable import must relocate against func_0 (→ field name), not Meld dispatch"
627 );
628 }
629
630 #[test]
631 fn test_compile_no_imports_no_relocations() {
632 let backend = ArmBackend::new();
633 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
634 let config = CompileConfig::default();
635
636 let func = backend.compile_function("add", &ops, &config).unwrap();
637 assert!(func.relocations.is_empty());
638 }
639
640 #[test]
647 fn test_compile_internal_call_produces_relocation_167() {
648 let backend = ArmBackend::new();
649 let ops = vec![WasmOp::Call(2)];
651 let config = CompileConfig {
652 num_imports: 1,
653 no_optimize: true,
654 ..CompileConfig::default()
655 };
656
657 let func = backend
658 .compile_function("caller", &ops, &config)
659 .expect("internal call compiles");
660
661 assert_eq!(
662 func.relocations.len(),
663 1,
664 "an internal call must emit exactly one relocation (#167)"
665 );
666 assert_eq!(
667 func.relocations[0].symbol, "func_2",
668 "internal call must relocate against the callee's func_{{index}} symbol (#167)"
669 );
670 }
671
672 #[test]
675 fn arm_safety_bounds_mpu_emits_same_code_as_none() {
676 let backend = ArmBackend::new();
680 let ops = vec![
681 WasmOp::LocalGet(0),
682 WasmOp::I32Load {
683 offset: 0,
684 align: 2,
685 },
686 ];
687 let cfg_none = CompileConfig {
688 no_optimize: true,
689 ..Default::default()
690 };
691 let cfg_mpu = CompileConfig {
692 no_optimize: true,
693 safety_bounds: SafetyBounds::Mpu,
694 ..Default::default()
695 };
696 let n = backend.compile_function("ld", &ops, &cfg_none).unwrap();
697 let m = backend.compile_function("ld", &ops, &cfg_mpu).unwrap();
698 assert_eq!(
699 n.code, m.code,
700 "Mpu and None should produce identical ARM bytes (Mpu relies on hardware)"
701 );
702 }
703
704 #[test]
705 fn arm_legacy_bounds_check_still_emits_software_check() {
706 let backend = ArmBackend::new();
709 let ops = vec![
710 WasmOp::LocalGet(0),
711 WasmOp::I32Load {
712 offset: 0,
713 align: 2,
714 },
715 ];
716 let cfg_legacy = CompileConfig {
717 no_optimize: true,
718 bounds_check: true,
719 ..Default::default()
720 };
721 let cfg_software = CompileConfig {
722 no_optimize: true,
723 safety_bounds: SafetyBounds::Software,
724 ..Default::default()
725 };
726 let l = backend.compile_function("ld", &ops, &cfg_legacy).unwrap();
727 let s = backend.compile_function("ld", &ops, &cfg_software).unwrap();
728 assert_eq!(
729 l.code, s.code,
730 "--bounds-check should produce the same bytes as --safety-bounds=software"
731 );
732 }
733
734 #[test]
740 fn test_f32_rejected_on_cortex_m3_no_fpu() {
741 let backend = ArmBackend::new();
742 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
743 let config = CompileConfig {
744 target: TargetSpec::cortex_m3(),
745 no_optimize: true,
746 ..CompileConfig::default()
747 };
748
749 let result = backend.compile_function("fadd", &ops, &config);
750 assert!(
751 result.is_err(),
752 "f32 operations should fail on Cortex-M3 (no FPU)"
753 );
754 }
755
756 #[test]
757 fn test_f32_accepted_on_cortex_m4f() {
758 let backend = ArmBackend::new();
759 let ops = vec![WasmOp::F32Const(1.0), WasmOp::F32Const(2.0), WasmOp::F32Add];
760 let config = CompileConfig {
761 target: TargetSpec::cortex_m4f(),
762 no_optimize: true,
763 ..CompileConfig::default()
764 };
765
766 let result = backend.compile_function("fadd", &ops, &config);
767 assert!(
768 result.is_ok(),
769 "f32 operations should succeed on Cortex-M4F, got: {:?}",
770 result.unwrap_err()
771 );
772 }
773
774 #[test]
775 fn test_i32_works_on_all_targets() {
776 let backend = ArmBackend::new();
777 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::I32Add];
778
779 let config_m3 = CompileConfig {
781 target: TargetSpec::cortex_m3(),
782 no_optimize: true,
783 ..CompileConfig::default()
784 };
785 assert!(
786 backend.compile_function("add", &ops, &config_m3).is_ok(),
787 "i32 ops should work on Cortex-M3"
788 );
789
790 let config_m4f = CompileConfig {
792 target: TargetSpec::cortex_m4f(),
793 no_optimize: true,
794 ..CompileConfig::default()
795 };
796 assert!(
797 backend.compile_function("add", &ops, &config_m4f).is_ok(),
798 "i32 ops should work on Cortex-M4F"
799 );
800
801 let config_m7dp = CompileConfig {
803 target: TargetSpec::cortex_m7dp(),
804 no_optimize: true,
805 ..CompileConfig::default()
806 };
807 assert!(
808 backend.compile_function("add", &ops, &config_m7dp).is_ok(),
809 "i32 ops should work on Cortex-M7DP"
810 );
811 }
812
813 #[test]
814 fn test_f32_rejected_on_cortex_m4_no_fpu() {
815 let backend = ArmBackend::new();
817 let ops = vec![WasmOp::F32Const(1.5), WasmOp::F32Const(2.5), WasmOp::F32Mul];
818 let config = CompileConfig {
819 target: TargetSpec::cortex_m4(),
820 no_optimize: true,
821 ..CompileConfig::default()
822 };
823
824 let result = backend.compile_function("fmul", &ops, &config);
825 assert!(
826 result.is_err(),
827 "f32 operations should fail on Cortex-M4 (no FPU)"
828 );
829 }
830
831 #[test]
853 fn test_issue120_f32_div_compiles_via_optimized_default() {
854 let backend = ArmBackend::new();
855 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
856 let config = CompileConfig {
857 target: TargetSpec::cortex_m4f(),
858 ..CompileConfig::default()
861 };
862
863 let result = backend.compile_function("fdiv", &ops, &config);
864 assert!(
865 result.is_ok(),
866 "f32.div must compile on Cortex-M4F via the optimized->direct \
867 fallback (issue #120), got: {:?}",
868 result.as_ref().err()
869 );
870 assert!(
871 !result.unwrap().code.is_empty(),
872 "f32.div must produce non-empty machine code"
873 );
874 }
875
876 #[test]
879 fn test_issue120_assorted_f32_ops_compile_via_optimized_default() {
880 let backend = ArmBackend::new();
881 let config = CompileConfig {
882 target: TargetSpec::cortex_m4f(),
883 ..CompileConfig::default()
884 };
885
886 let cases: Vec<(&str, Vec<WasmOp>)> = vec![
887 (
888 "fadd",
889 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Add],
890 ),
891 (
892 "fmul",
893 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Mul],
894 ),
895 (
896 "fsub",
897 vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Sub],
898 ),
899 ];
900
901 for (name, ops) in cases {
902 let result = backend.compile_function(name, &ops, &config);
903 assert!(
904 result.is_ok(),
905 "{name} must compile via the optimized->direct fallback \
906 (issue #120), got: {:?}",
907 result.as_ref().err()
908 );
909 assert!(
910 !result.unwrap().code.is_empty(),
911 "{name} must produce non-empty machine code"
912 );
913 }
914 }
915
916 #[test]
919 fn test_issue120_f32_div_rejected_on_no_fpu_via_optimized() {
920 let backend = ArmBackend::new();
921 let ops = vec![WasmOp::LocalGet(0), WasmOp::LocalGet(1), WasmOp::F32Div];
922 let config = CompileConfig {
923 target: TargetSpec::cortex_m3(),
924 ..CompileConfig::default()
925 };
926
927 let result = backend.compile_function("fdiv", &ops, &config);
928 assert!(
929 result.is_err(),
930 "f32.div must be rejected on Cortex-M3 (no FPU), not panic"
931 );
932 }
933
934 #[test]
939 fn test_issue94_hi32_extract_is_smaller_than_generic_shift() {
940 let backend = ArmBackend::new();
941 let config = CompileConfig {
942 target: TargetSpec::cortex_m4f(),
943 ..CompileConfig::default()
944 };
945
946 let ops_hi32 = vec![
948 WasmOp::LocalGet(0), WasmOp::I64Const(32),
950 WasmOp::I64ShrU,
951 WasmOp::I32WrapI64,
952 ];
953 let func_hi32 = backend
954 .compile_function("hi32_extract", &ops_hi32, &config)
955 .unwrap();
956
957 let ops_generic = vec![
961 WasmOp::LocalGet(0),
962 WasmOp::I64Const(7),
963 WasmOp::I64ShrU,
964 WasmOp::I32WrapI64,
965 ];
966 let func_generic = backend
967 .compile_function("generic_shr", &ops_generic, &config)
968 .unwrap();
969
970 let bytes_hi32 = func_hi32.code.len();
971 let bytes_generic = func_generic.code.len();
972 println!(
973 "\n[issue #94] hi32 extract: {} bytes (vs generic shift: {} bytes; saved {})",
974 bytes_hi32,
975 bytes_generic,
976 bytes_generic.saturating_sub(bytes_hi32)
977 );
978 let hex: String = func_hi32
979 .code
980 .iter()
981 .map(|b| format!("{:02x}", b))
982 .collect::<Vec<_>>()
983 .join(" ");
984 println!("[issue #94] hi32 bytes: {}", hex);
985 assert!(
988 bytes_hi32 + 30 <= bytes_generic,
989 "issue #94: hi32 extract = {} bytes, generic shift = {} bytes; \
990 expected optimized form to be at least 30 bytes smaller",
991 bytes_hi32,
992 bytes_generic,
993 );
994 }
995}