Commit 4c4b0fc9 authored by qinsoon's avatar qinsoon

code emission. Currently it assumes the use of asm backend (need to be fixed later)

parent 850d7f80
target/*
emit/*
.project
Cargo.lock
......@@ -89,6 +89,17 @@ impl MachineCode for ASMCode {
}
}
fn emit(&self) -> Vec<u8> {
let mut ret = vec![];
for inst in self.code.iter() {
ret.append(&mut inst.code.clone().into_bytes());
ret.append(&mut "\n".to_string().into_bytes());
}
ret
}
fn print(&self) {
println!("");
......
#![allow(dead_code)]
use compiler::CompilerPass;
use compiler::PassExecutionResult;
use compiler;
use ast::ir::*;
use vm::context::VMContext;
pub struct CodeEmission {
name: &'static str
}
impl CodeEmission {
pub fn new() -> CodeEmission {
CodeEmission {
name: "Code Emission"
}
}
}
impl CompilerPass for CodeEmission {
fn name(&self) -> &'static str {
self.name
}
fn visit_function(&mut self, vm_context: &VMContext, func: &mut MuFunction) {
use std::io::prelude::*;
use std::fs::File;
use std::fs;
let compiled_funcs = vm_context.compiled_funcs().read().unwrap();
let mut cf = compiled_funcs.get(func.fn_name).unwrap().borrow();
let code = cf.mc.emit();
// FIXME: this is only for asm backend
const EMIT_DIR : &'static str = "emit";
// match fs::remove_dir_all(EMIT_DIR) {
// Ok(dir) => {},
// Err(_) => {}
// }
match fs::create_dir(EMIT_DIR) {
Ok(dir) => {},
Err(_) => {}
}
let file_name = EMIT_DIR.to_string() + "/" + func.fn_name + ".s";
let mut file = match File::create(file_name.clone()) {
Err(why) => panic!("couldn't create emission file {}: {}", file_name, why),
Ok(file) => file
};
match file.write_all(code.as_slice()) {
Err(why) => panic!("couldn'd write to file {}: {}", file_name, why),
Ok(_) => println!("emit code to {}", file_name)
}
}
}
\ No newline at end of file
pub mod inst_sel;
pub mod reg_alloc;
pub mod code_emission;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RegGroup {GPR, FPR}
......
......@@ -22,6 +22,7 @@ impl RegisterAllocation {
}
#[allow(unused_variables)]
// returns true if we spill registers (which requires another instruction selection)
fn coloring(&mut self, vm_context: &VMContext, func: &mut MuFunction) -> bool {
let compiled_funcs = vm_context.compiled_funcs().read().unwrap();
let mut cf = compiled_funcs.get(func.fn_name).unwrap().borrow_mut();
......
......@@ -7,6 +7,7 @@ pub struct CompiledFunction {
pub trait MachineCode {
fn print(&self);
fn emit(&self) -> Vec<u8>;
fn number_of_insts(&self) -> usize;
......
......@@ -19,7 +19,8 @@ fn test_regalloc_fac() {
Box::new(passes::ControlFlowAnalysis::new()),
Box::new(passes::TraceGen::new()),
Box::new(backend::inst_sel::InstructionSelection::new()),
Box::new(backend::reg_alloc::RegisterAllocation::new())
Box::new(backend::reg_alloc::RegisterAllocation::new()),
Box::new(backend::code_emission::CodeEmission::new())
]), vm_context.clone());
let funcs = vm_context.funcs().read().unwrap();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment