forked from bytecodealliance/cranelift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Context::compile() function which runs all compiler passes.
This is the main entry point to the code generator. It returns the computed size of the functions code. Also add a 'test compile' command which runs the whole code generation pipeline.
- Loading branch information
Jakob Stoklund Olesen
committed
Jul 12, 2017
1 parent
07a96e6
commit 6cc729a
Showing
5 changed files
with
143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
//! Test command for testing the code generator pipeline | ||
//! | ||
//! The `compile` test command runs each function through the full code generator pipeline | ||
use cretonne::binemit; | ||
use cretonne::ir; | ||
use cretonne; | ||
use cton_reader::TestCommand; | ||
use filetest::subtest::{SubTest, Context, Result}; | ||
use std::borrow::Cow; | ||
use utils::pretty_error; | ||
|
||
struct TestCompile; | ||
|
||
pub fn subtest(parsed: &TestCommand) -> Result<Box<SubTest>> { | ||
assert_eq!(parsed.command, "compile"); | ||
if !parsed.options.is_empty() { | ||
Err(format!("No options allowed on {}", parsed)) | ||
} else { | ||
Ok(Box::new(TestCompile)) | ||
} | ||
} | ||
|
||
impl SubTest for TestCompile { | ||
fn name(&self) -> Cow<str> { | ||
Cow::from("compile") | ||
} | ||
|
||
fn is_mutating(&self) -> bool { | ||
true | ||
} | ||
|
||
fn needs_isa(&self) -> bool { | ||
true | ||
} | ||
|
||
fn run(&self, func: Cow<ir::Function>, context: &Context) -> Result<()> { | ||
let isa = context.isa.expect("compile needs an ISA"); | ||
|
||
// Create a compilation context, and drop in the function. | ||
let mut comp_ctx = cretonne::Context::new(); | ||
comp_ctx.func = func.into_owned(); | ||
|
||
let code_size = comp_ctx | ||
.compile(isa) | ||
.map_err(|e| pretty_error(&comp_ctx.func, context.isa, e))?; | ||
|
||
dbg!("Generated {} bytes of code:\n{}", | ||
code_size, | ||
comp_ctx.func.display(isa)); | ||
|
||
// Finally verify that the returned code size matches the emitted bytes. | ||
let mut sink = SizeSink { offset: 0 }; | ||
for ebb in comp_ctx.func.layout.ebbs() { | ||
assert_eq!(sink.offset, comp_ctx.func.offsets[ebb]); | ||
for inst in comp_ctx.func.layout.ebb_insts(ebb) { | ||
isa.emit_inst(&comp_ctx.func, inst, &mut sink); | ||
} | ||
} | ||
|
||
if sink.offset != code_size { | ||
return Err(format!("Expected code size {}, got {}", code_size, sink.offset)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
// Code sink that simply counts bytes. | ||
struct SizeSink { | ||
offset: binemit::CodeOffset, | ||
} | ||
|
||
impl binemit::CodeSink for SizeSink { | ||
fn offset(&self) -> binemit::CodeOffset { | ||
self.offset | ||
} | ||
|
||
fn put1(&mut self, _: u8) { | ||
self.offset += 1; | ||
} | ||
|
||
fn put2(&mut self, _: u16) { | ||
self.offset += 2; | ||
} | ||
|
||
fn put4(&mut self, _: u32) { | ||
self.offset += 4; | ||
} | ||
|
||
fn put8(&mut self, _: u64) { | ||
self.offset += 8; | ||
} | ||
|
||
fn reloc_ebb(&mut self, _reloc: binemit::Reloc, _ebb: ir::Ebb) {} | ||
fn reloc_func(&mut self, _reloc: binemit::Reloc, _fref: ir::FuncRef) {} | ||
fn reloc_jt(&mut self, _reloc: binemit::Reloc, _jt: ir::JumpTable) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters