GitLab will be upgraded to the 12.10.14-ce.0 on 28 Sept 2020 at 2.00pm (AEDT) to 2.30pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

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

3
use ast::ir::*;
qinsoon's avatar
qinsoon committed
4
use vm::VM;
5 6 7
use std::cell::RefCell;

pub mod passes;
8
pub mod backend;
qinsoon's avatar
qinsoon committed
9 10
pub mod frame;
pub mod machine_code;
11

12 13
pub use compiler::passes::CompilerPass;

qinsoon's avatar
qinsoon committed
14
pub struct Compiler<'vm> {
15
    policy: RefCell<CompilerPolicy>,
qinsoon's avatar
qinsoon committed
16
    vm: &'vm VM
17 18
}

qinsoon's avatar
qinsoon committed
19 20
impl <'vm> Compiler<'vm> {
    pub fn new(policy: CompilerPolicy, vm: &VM) -> Compiler {
21 22 23 24
        Compiler{
            policy: RefCell::new(policy),
            vm: vm
        }
25
    }
26

27
    pub fn compile(&self, func: &mut MuFunctionVersion) {
28 29
        trace!("{:?}", func);
        
qinsoon's avatar
qinsoon committed
30 31
        // FIXME: should use function name here (however hprof::enter only accept &'static str)
        let _p = hprof::enter("Function Compilation");
32

33
        let ref mut passes = self.policy.borrow_mut().passes;
34

35 36
        for pass in passes.iter_mut() {
            let _p = hprof::enter(pass.name());
37

qinsoon's avatar
qinsoon committed
38
            pass.execute(self.vm, func);
39 40

            drop(_p);
41
        }
42

43 44
        drop(_p);
        hprof::profiler().print_timing();
qinsoon's avatar
qinsoon committed
45 46

        func.set_compiled();
47
    }
qinsoon's avatar
qinsoon committed
48 49 50 51

    pub fn get_policy(&self) -> &RefCell<CompilerPolicy> {
        &self.policy
    }
52 53 54
}

pub struct CompilerPolicy {
55
    pub passes: Vec<Box<CompilerPass>>
56 57 58
}

impl CompilerPolicy {
59 60 61 62 63 64 65
    pub fn new(passes: Vec<Box<CompilerPass>>) -> CompilerPolicy {
        CompilerPolicy{passes: passes}
    }
}

impl Default for CompilerPolicy {
    fn default() -> Self {
66
        let mut passes : Vec<Box<CompilerPass>> = vec![];
qinsoon's avatar
qinsoon committed
67
        passes.push(Box::new(passes::Inlining::new()));
68
        // ir level passes
qinsoon's avatar
qinsoon committed
69 70
        passes.push(Box::new(passes::DefUse::new()));
        passes.push(Box::new(passes::TreeGen::new()));
71
        passes.push(Box::new(passes::GenMovPhi::new()));
72
        passes.push(Box::new(passes::ControlFlowAnalysis::new()));
73
        passes.push(Box::new(passes::TraceGen::new()));
74

75 76 77
        // compilation
        passes.push(Box::new(backend::inst_sel::InstructionSelection::new()));
        passes.push(Box::new(backend::reg_alloc::RegisterAllocation::new()));
78 79

        // machine code level passes
80 81
        passes.push(Box::new(backend::peephole_opt::PeepholeOptimization::new()));
        passes.push(Box::new(backend::code_emission::CodeEmission::new()));
82

83 84
        CompilerPolicy{passes: passes}
    }
85
}