mod.rs 4.23 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
        drop(_p);
44 45

        hprof_print_timing(hprof::profiler().root());
qinsoon's avatar
qinsoon committed
46 47

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

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

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

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

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

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

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

84 85
        CompilerPolicy{passes: passes}
    }
86
}
87

88
// rewrite parts of the hprof crates to print via log (instead of print!())
89 90 91 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
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.)
        }
    }
}