Commit f192c70c authored by qinsoon's avatar qinsoon

[wip] code emission is specific to plat/backend

parent 18349852
...@@ -4,6 +4,7 @@ use compiler::backend; ...@@ -4,6 +4,7 @@ use compiler::backend;
use compiler::backend::x86_64; use compiler::backend::x86_64;
use compiler::backend::x86_64::CodeGenerator; use compiler::backend::x86_64::CodeGenerator;
use vm::machine_code::MachineCode; use vm::machine_code::MachineCode;
use vm::context::VMContext;
use utils::string_utils; use utils::string_utils;
...@@ -1060,3 +1061,37 @@ impl CodeGenerator for ASMCodeGen { ...@@ -1060,3 +1061,37 @@ impl CodeGenerator for ASMCodeGen {
) )
} }
} }
pub fn emit_code(func: &mut MuFunctionVersion, vm: &VMContext) {
use std::io::prelude::*;
use std::fs::File;
use std::fs;
let compiled_funcs = vm.compiled_funcs().read().unwrap();
let cf = compiled_funcs.get(func.fn_name).unwrap().borrow();
let code = cf.mc.emit();
const EMIT_DIR : &'static str = "emit";
match fs::create_dir(EMIT_DIR) {
Ok(_) => {},
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)
}
}
pub fn emit_context(vm: &VMContext) {
debug!("---Emit VM Context---");
debug!("---finish---");
}
\ No newline at end of file
...@@ -8,6 +8,8 @@ pub use compiler::backend::x86_64::codegen::CodeGenerator; ...@@ -8,6 +8,8 @@ pub use compiler::backend::x86_64::codegen::CodeGenerator;
mod asm_backend; mod asm_backend;
pub use compiler::backend::x86_64::asm_backend::ASMCodeGen; pub use compiler::backend::x86_64::asm_backend::ASMCodeGen;
pub use compiler::backend::x86_64::asm_backend::emit_code;
pub use compiler::backend::x86_64::asm_backend::emit_context;
use ast::ptr::P; use ast::ptr::P;
use ast::ir::*; use ast::ir::*;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
use compiler::CompilerPass; use compiler::CompilerPass;
use ast::ir::*; use ast::ir::*;
use vm::context::VMContext; use vm::context::VMContext;
use compiler::backend::emit_code;
pub struct CodeEmission { pub struct CodeEmission {
name: &'static str name: &'static str
...@@ -22,35 +23,6 @@ impl CompilerPass for CodeEmission { ...@@ -22,35 +23,6 @@ impl CompilerPass for CodeEmission {
} }
fn visit_function(&mut self, vm_context: &VMContext, func: &mut MuFunctionVersion) { fn visit_function(&mut self, vm_context: &VMContext, func: &mut MuFunctionVersion) {
use std::io::prelude::*; emit_code(func, vm_context);
use std::fs::File;
use std::fs;
let compiled_funcs = vm_context.compiled_funcs().read().unwrap();
let 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(_) => {},
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)
}
} }
} }
...@@ -25,12 +25,11 @@ pub use compiler::backend::x86_64::all_usable_regs; ...@@ -25,12 +25,11 @@ pub use compiler::backend::x86_64::all_usable_regs;
pub use compiler::backend::x86_64::pick_group_for_reg; pub use compiler::backend::x86_64::pick_group_for_reg;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::is_callee_saved; pub use compiler::backend::x86_64::is_callee_saved;
#[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::emit_code;
#[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::emit_context;
#[cfg(target_arch = "arm")] #[cfg(target_arch = "arm")]
#[path = "arch/arm/mod.rs"] #[path = "arch/arm/mod.rs"]
mod arm; mod arm;
#[cfg(target_arch = "arm")]
pub use compiler::backend::arm::GPR_COUNT;
#[cfg(target_arch = "arm")]
pub use compiler::backend::arm::FPR_COUNT;
...@@ -30,4 +30,5 @@ fn test_global_access() { ...@@ -30,4 +30,5 @@ fn test_global_access() {
let mut func_ver = func_vers.get(&(func.fn_name, func.cur_ver.unwrap())).unwrap().borrow_mut(); let mut func_ver = func_vers.get(&(func.fn_name, func.cur_ver.unwrap())).unwrap().borrow_mut();
compiler.compile(&mut func_ver); compiler.compile(&mut func_ver);
backend::emit_context(&vm_context);
} }
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