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.17 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;
qinsoon's avatar
qinsoon committed
11 12
pub mod frame;
pub mod machine_code;
13

14 15
pub use compiler::passes::CompilerPass;

16
pub struct Compiler {
17
    policy: RefCell<CompilerPolicy>,
qinsoon's avatar
qinsoon committed
18
    vm: Arc<VM>
19 20 21
}

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

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

35
        let ref mut passes = self.policy.borrow_mut().passes;
36

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

40
            pass.execute(&self.vm, func);
41 42

            drop(_p);
43
        }
44

45 46
        drop(_p);
        hprof::profiler().print_timing();
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
}