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.
This commit is contained in:
Jakob Stoklund Olesen
2017-07-11 16:26:16 -07:00
parent ae5e440094
commit 9e3b6a6eba
5 changed files with 143 additions and 8 deletions

View File

@@ -12,7 +12,7 @@ use cretonne::ir::entities::AnyEntity;
use cretonne::isa::TargetIsa;
use cton_reader::TestCommand;
use filetest::subtest::{SubTest, Context, Result};
use utils::match_directive;
use utils::{match_directive, pretty_error};
struct TestBinEmit;
@@ -117,7 +117,8 @@ impl SubTest for TestBinEmit {
}
// Relax branches and compute EBB offsets based on the encodings.
binemit::relax_branches(&mut func, isa);
let code_size = binemit::relax_branches(&mut func, isa)
.map_err(|e| pretty_error(&func, context.isa, e))?;
// Collect all of the 'bin:' directives on instructions.
let mut bins = HashMap::new();
@@ -188,6 +189,10 @@ impl SubTest for TestBinEmit {
}
}
if sink.offset != code_size {
return Err(format!("Expected code size {}, got {}", code_size, sink.offset));
}
Ok(())
}
}

View File

@@ -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) {}
}

View File

@@ -14,6 +14,7 @@ use filetest::runner::TestRunner;
pub mod subtest;
mod binemit;
mod compile;
mod concurrent;
mod domtree;
mod legalizer;
@@ -57,15 +58,16 @@ pub fn run(verbose: bool, files: Vec<String>) -> CommandResult {
/// a `.cton` test file.
fn new_subtest(parsed: &TestCommand) -> subtest::Result<Box<subtest::SubTest>> {
match parsed.command {
"binemit" => binemit::subtest(parsed),
"cat" => cat::subtest(parsed),
"print-cfg" => print_cfg::subtest(parsed),
"compile" => compile::subtest(parsed),
"domtree" => domtree::subtest(parsed),
"verifier" => verifier::subtest(parsed),
"legalizer" => legalizer::subtest(parsed),
"licm" => licm::subtest(parsed),
"print-cfg" => print_cfg::subtest(parsed),
"regalloc" => regalloc::subtest(parsed),
"binemit" => binemit::subtest(parsed),
"simple-gvn" => simple_gvn::subtest(parsed),
"verifier" => verifier::subtest(parsed),
_ => Err(format!("unknown test command '{}'", parsed.command)),
}
}