To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

mod.rs 2.82 KB
Newer Older
qinsoon's avatar
qinsoon committed
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

qinsoon's avatar
qinsoon committed
14
15
pub use compiler::passes::CompilerPass;
pub use compiler::passes::PassExecutionResult;
16
17
18
19
20
pub use compiler::passes::PASS_IR_CHECK;
pub use compiler::passes::PASS_DEF_USE;
pub use compiler::passes::PASS_TREE_GEN;
pub use compiler::passes::PASS_CFA;
pub use compiler::passes::PASS_TRACE_GEN;
21
22
pub use compiler::passes::PASS_INST_SEL;
pub use compiler::passes::PASS_REG_ALLOC;
23
24
pub use compiler::passes::PASS_PEEPHOLE;
pub use compiler::passes::PASS_CODE_EMIT;
qinsoon's avatar
qinsoon committed
25

26
pub struct Compiler {
27
    policy: RefCell<CompilerPolicy>,
qinsoon's avatar
qinsoon committed
28
    vm: Arc<VM>
29
30
31
}

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

39
    pub fn compile(&self, func: &mut MuFunctionVersion) {
40
41
        trace!("{:?}", func);
        
qinsoon's avatar
qinsoon committed
42
43
        // FIXME: should use function name here (however hprof::enter only accept &'static str)
        let _p = hprof::enter("Function Compilation");
qinsoon's avatar
qinsoon committed
44

45
46
        let mut cur_pass = 0;
        let n_passes = self.policy.borrow().passes.len();
qinsoon's avatar
qinsoon committed
47

48
        let ref mut passes = self.policy.borrow_mut().passes;
qinsoon's avatar
qinsoon committed
49

50
        while cur_pass < n_passes {
qinsoon's avatar
qinsoon committed
51
            let _p = hprof::enter(passes[cur_pass].name());
52
            let result = passes[cur_pass].execute(&self.vm, func);
qinsoon's avatar
qinsoon committed
53

54
55
            match result {
                PassExecutionResult::ProceedToNext => cur_pass += 1,
56
57
                PassExecutionResult::ProceedTo(next)
                | PassExecutionResult::GoBackTo(next) => cur_pass = next.get()
58
            }
qinsoon's avatar
qinsoon committed
59
60

            drop(_p);
61
        }
qinsoon's avatar
qinsoon committed
62

63
64
        drop(_p);
        hprof::profiler().print_timing();
65
    }
qinsoon's avatar
qinsoon committed
66
67
68
69

    pub fn get_policy(&self) -> &RefCell<CompilerPolicy> {
        &self.policy
    }
70
71
72
}

pub struct CompilerPolicy {
73
    pub passes: Vec<Box<CompilerPass>>
74
75
76
}

impl CompilerPolicy {
77
78
79
80
81
82
83
    pub fn new(passes: Vec<Box<CompilerPass>>) -> CompilerPolicy {
        CompilerPolicy{passes: passes}
    }
}

impl Default for CompilerPolicy {
    fn default() -> Self {
84
        let mut passes : Vec<Box<CompilerPass>> = vec![];
85
        // ir level passes
qinsoon's avatar
qinsoon committed
86
87
        passes.push(Box::new(passes::DefUse::new()));
        passes.push(Box::new(passes::TreeGen::new()));
88
        passes.push(Box::new(passes::ControlFlowAnalysis::new()));
89
        passes.push(Box::new(passes::TraceGen::new()));
90

91
92
93
        // compilation
        passes.push(Box::new(backend::inst_sel::InstructionSelection::new()));
        passes.push(Box::new(backend::reg_alloc::RegisterAllocation::new()));
94
95

        // machine code level passes
96
97
        passes.push(Box::new(backend::peephole_opt::PeepholeOptimization::new()));
        passes.push(Box::new(backend::code_emission::CodeEmission::new()));
qinsoon's avatar
qinsoon committed
98

99
100
        CompilerPolicy{passes: passes}
    }
101
}