mod.rs 2.35 KB
Newer Older
1 2
extern crate hprof;

3
use ast::ir::*;
qinsoon's avatar
qinsoon committed
4
use vm::VM;
qinsoon's avatar
qinsoon committed
5

6
use std::cell::RefCell;
7
use std::sync::Arc;
8 9

pub mod passes;
10
pub mod backend;
11

12 13 14 15 16 17 18 19
pub use compiler::passes::CompilerPass;
pub use compiler::passes::PassExecutionResult;
pub use compiler::passes::PASS0_DEF_USE;
pub use compiler::passes::PASS1_TREE_GEN;
pub use compiler::passes::PASS2_CFA;
pub use compiler::passes::PASS3_TRACE_GEN;
pub use compiler::passes::PASS4_INST_SEL;
pub use compiler::passes::PASS5_REG_ALLOC;
20 21
pub use compiler::passes::PASS6_PEEPHOLE;
pub use compiler::passes::PASS7_CODE_EMIT;
22

23
pub struct Compiler {
24
    policy: RefCell<CompilerPolicy>,
qinsoon's avatar
qinsoon committed
25
    vm: Arc<VM>
26 27 28
}

impl Compiler {
qinsoon's avatar
qinsoon committed
29
    pub fn new(policy: CompilerPolicy, vm: Arc<VM>) -> Compiler {
30 31 32 33
        Compiler{
            policy: RefCell::new(policy),
            vm: vm
        }
34
    }
35

36
    pub fn compile(&self, func: &mut MuFunctionVersion) {
37 38
        let _p = hprof::enter(func.fn_name);

39 40
        let mut cur_pass = 0;
        let n_passes = self.policy.borrow().passes.len();
41

42
        let ref mut passes = self.policy.borrow_mut().passes;
43

44
        while cur_pass < n_passes {
45
            let _p = hprof::enter(passes[cur_pass].name());
46
            let result = passes[cur_pass].execute(&self.vm, func);
47

48 49 50 51
            match result {
                PassExecutionResult::ProceedToNext => cur_pass += 1,
                PassExecutionResult::GoBackTo(next) => cur_pass = next
            }
52 53

            drop(_p);
54
        }
55

56 57
        drop(_p);
        hprof::profiler().print_timing();
58 59 60 61
    }
}

pub struct CompilerPolicy {
62
    pub passes: Vec<Box<CompilerPass>>
63 64 65 66 67
}

impl CompilerPolicy {
    pub fn default() -> CompilerPolicy {
        let mut passes : Vec<Box<CompilerPass>> = vec![];
qinsoon's avatar
qinsoon committed
68 69
        passes.push(Box::new(passes::DefUse::new()));
        passes.push(Box::new(passes::TreeGen::new()));
70
        passes.push(Box::new(passes::ControlFlowAnalysis::new()));
71
        passes.push(Box::new(passes::TraceGen::new()));
72
        passes.push(Box::new(backend::inst_sel::InstructionSelection::new()));
73
        passes.push(Box::new(backend::reg_alloc::RegisterAllocation::new()));
74 75
        passes.push(Box::new(backend::peephole_opt::PeepholeOptimization::new()));
        passes.push(Box::new(backend::code_emission::CodeEmission::new()));
76

77 78
        CompilerPolicy{passes: passes}
    }
79

qinsoon's avatar
qinsoon committed
80 81 82
    pub fn new(passes: Vec<Box<CompilerPass>>) -> CompilerPolicy {
        CompilerPolicy{passes: passes}
    }
qinsoon's avatar
qinsoon committed
83
}