1 //! Test command for testing the code generator pipeline
2 //!
3 //! The `compile` test command runs each function through the full code generator pipeline
4 
5 use crate::subtest::{run_filecheck, Context, SubTest, SubtestResult};
6 use cranelift_codegen;
7 use cranelift_codegen::binemit::{self, CodeInfo};
8 use cranelift_codegen::ir;
9 use cranelift_codegen::isa;
10 use cranelift_codegen::print_errors::pretty_error;
11 use cranelift_reader::TestCommand;
12 use log::info;
13 use std::borrow::Cow;
14 
15 struct TestCompile;
16 
subtest(parsed: &TestCommand) -> SubtestResult<Box<dyn SubTest>>17 pub fn subtest(parsed: &TestCommand) -> SubtestResult<Box<dyn SubTest>> {
18     assert_eq!(parsed.command, "compile");
19     if !parsed.options.is_empty() {
20         Err(format!("No options allowed on {}", parsed))
21     } else {
22         Ok(Box::new(TestCompile))
23     }
24 }
25 
26 impl SubTest for TestCompile {
name(&self) -> &'static str27     fn name(&self) -> &'static str {
28         "compile"
29     }
30 
is_mutating(&self) -> bool31     fn is_mutating(&self) -> bool {
32         true
33     }
34 
needs_isa(&self) -> bool35     fn needs_isa(&self) -> bool {
36         true
37     }
38 
run(&self, func: Cow<ir::Function>, context: &Context) -> SubtestResult<()>39     fn run(&self, func: Cow<ir::Function>, context: &Context) -> SubtestResult<()> {
40         let isa = context.isa.expect("compile needs an ISA");
41         let mut comp_ctx = cranelift_codegen::Context::for_function(func.into_owned());
42 
43         if isa.get_mach_backend().is_some() {
44             // With `MachBackend`s, we need to explicitly request dissassembly results.
45             comp_ctx.set_disasm(true);
46         }
47 
48         let CodeInfo { total_size, .. } = comp_ctx
49             .compile(isa)
50             .map_err(|e| pretty_error(&comp_ctx.func, context.isa, e))?;
51 
52         info!(
53             "Generated {} bytes of code:\n{}",
54             total_size,
55             comp_ctx.func.display(isa)
56         );
57 
58         if !isa.get_mach_backend().is_some() {
59             // Verify that the returned code size matches the emitted bytes.
60             let mut sink = SizeSink { offset: 0 };
61             binemit::emit_function(
62                 &comp_ctx.func,
63                 |func, inst, div, sink, isa| isa.emit_inst(func, inst, div, sink),
64                 &mut sink,
65                 isa,
66             );
67 
68             if sink.offset != total_size {
69                 return Err(format!(
70                     "Expected code size {}, got {}",
71                     total_size, sink.offset
72                 ));
73             }
74 
75             // Run final code through filecheck.
76             let text = comp_ctx.func.display(Some(isa)).to_string();
77             run_filecheck(&text, context)
78         } else {
79             let disasm = comp_ctx
80                 .mach_compile_result
81                 .as_ref()
82                 .unwrap()
83                 .disasm
84                 .as_ref()
85                 .unwrap();
86             run_filecheck(&disasm, context)
87         }
88     }
89 }
90 
91 /// Code sink that simply counts bytes.
92 struct SizeSink {
93     offset: binemit::CodeOffset,
94 }
95 
96 impl binemit::CodeSink for SizeSink {
offset(&self) -> binemit::CodeOffset97     fn offset(&self) -> binemit::CodeOffset {
98         self.offset
99     }
100 
put1(&mut self, _: u8)101     fn put1(&mut self, _: u8) {
102         self.offset += 1;
103     }
104 
put2(&mut self, _: u16)105     fn put2(&mut self, _: u16) {
106         self.offset += 2;
107     }
108 
put4(&mut self, _: u32)109     fn put4(&mut self, _: u32) {
110         self.offset += 4;
111     }
112 
put8(&mut self, _: u64)113     fn put8(&mut self, _: u64) {
114         self.offset += 8;
115     }
116 
reloc_block(&mut self, _reloc: binemit::Reloc, _block_offset: binemit::CodeOffset)117     fn reloc_block(&mut self, _reloc: binemit::Reloc, _block_offset: binemit::CodeOffset) {}
reloc_external( &mut self, _srcloc: ir::SourceLoc, _reloc: binemit::Reloc, _name: &ir::ExternalName, _addend: binemit::Addend, )118     fn reloc_external(
119         &mut self,
120         _srcloc: ir::SourceLoc,
121         _reloc: binemit::Reloc,
122         _name: &ir::ExternalName,
123         _addend: binemit::Addend,
124     ) {
125     }
reloc_constant(&mut self, _: binemit::Reloc, _: ir::ConstantOffset)126     fn reloc_constant(&mut self, _: binemit::Reloc, _: ir::ConstantOffset) {}
reloc_jt(&mut self, _reloc: binemit::Reloc, _jt: ir::JumpTable)127     fn reloc_jt(&mut self, _reloc: binemit::Reloc, _jt: ir::JumpTable) {}
trap(&mut self, _code: ir::TrapCode, _srcloc: ir::SourceLoc)128     fn trap(&mut self, _code: ir::TrapCode, _srcloc: ir::SourceLoc) {}
begin_jumptables(&mut self)129     fn begin_jumptables(&mut self) {}
begin_rodata(&mut self)130     fn begin_rodata(&mut self) {}
end_codegen(&mut self)131     fn end_codegen(&mut self) {}
add_stackmap( &mut self, _: &[ir::entities::Value], _: &ir::Function, _: &dyn isa::TargetIsa, )132     fn add_stackmap(
133         &mut self,
134         _: &[ir::entities::Value],
135         _: &ir::Function,
136         _: &dyn isa::TargetIsa,
137     ) {
138     }
139 }
140