mod.rs 4.31 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) {
qinsoon's avatar
qinsoon committed
28 29 30 31
        info!("");
        info!("Start compiling {}", func);
        info!("");
        debug!("{:?}", func);
32
        
qinsoon's avatar
qinsoon committed
33 34
        // FIXME: should use function name here (however hprof::enter only accept &'static str)
        let _p = hprof::enter("Function Compilation");
35

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

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

qinsoon's avatar
qinsoon committed
41
            pass.execute(self.vm, func);
42 43

            drop(_p);
44
        }
45

46
        drop(_p);
47 48

        hprof_print_timing(hprof::profiler().root());
qinsoon's avatar
qinsoon committed
49 50

        func.set_compiled();
51
    }
qinsoon's avatar
qinsoon committed
52 53 54 55

    pub fn get_policy(&self) -> &RefCell<CompilerPolicy> {
        &self.policy
    }
56 57 58
}

pub struct CompilerPolicy {
59
    pub passes: Vec<Box<CompilerPass>>
60 61 62
}

impl CompilerPolicy {
63 64 65 66 67 68 69
    pub fn new(passes: Vec<Box<CompilerPass>>) -> CompilerPolicy {
        CompilerPolicy{passes: passes}
    }
}

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

79 80 81
        // compilation
        passes.push(Box::new(backend::inst_sel::InstructionSelection::new()));
        passes.push(Box::new(backend::reg_alloc::RegisterAllocation::new()));
82 83

        // machine code level passes
84 85
        passes.push(Box::new(backend::peephole_opt::PeepholeOptimization::new()));
        passes.push(Box::new(backend::code_emission::CodeEmission::new()));
86

87 88
        CompilerPolicy{passes: passes}
    }
89
}
90

91
// rewrite parts of the hprof crates to print via log (instead of print!())
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
use self::hprof::ProfileNode;
use std::rc::Rc;

fn hprof_print_timing(root: Rc<ProfileNode>) {
    info!("Timing information for {}:", root.name);
    for child in &*root.children.borrow() {
        hprof_print_child(child, 2);
    }
}

fn hprof_print_child(this: &ProfileNode, indent: usize) {
    let mut indent_str = "".to_string();
    for _ in 0..indent {
        indent_str += " ";
    }

    let parent_time = this.parent
        .as_ref()
        .map(|p| p.total_time.get())
        .unwrap_or(this.total_time.get()) as f64;
    let percent = 100.0 * (this.total_time.get() as f64 / parent_time);
    if percent.is_infinite() {
        info!("{}{name} - {calls} * {each} = {total} @ {hz:.1}hz",
        indent_str,
        name  = this.name,
        calls = this.calls.get(),
        each = Nanoseconds((this.total_time.get() as f64 / this.calls.get() as f64) as u64),
        total = Nanoseconds(this.total_time.get()),
        hz = this.calls.get() as f64 / this.total_time.get() as f64 * 1e9f64
        );
    } else {
        info!("{}{name} - {calls} * {each} = {total} ({percent:.1}%)",
        indent_str,
        name  = this.name,
        calls = this.calls.get(),
        each = Nanoseconds((this.total_time.get() as f64 / this.calls.get() as f64) as u64),
        total = Nanoseconds(this.total_time.get()),
        percent = percent
        );
    }
    for c in &*this.children.borrow() {
        hprof_print_child(c, indent+2);
    }
}

// used to do a pretty printing of time
struct Nanoseconds(u64);

use std::fmt;
impl fmt::Display for Nanoseconds {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.0 < 1_000 {
            write!(f, "{}ns", self.0)
        } else if self.0 < 1_000_000 {
            write!(f, "{:.1}us", self.0 as f64 / 1_000.)
        } else if self.0 < 1_000_000_000 {
            write!(f, "{:.1}ms", self.0 as f64 / 1_000_000.)
        } else {
            write!(f, "{:.1}s", self.0 as f64 / 1_000_000_000.)
        }
    }
}