mod.rs 4.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 The Australian National University
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16
extern crate hprof;

17
use ast::ir::*;
qinsoon's avatar
qinsoon committed
18
use vm::VM;
19 20 21
use std::cell::RefCell;

pub mod passes;
22
pub mod backend;
qinsoon's avatar
qinsoon committed
23 24
pub mod frame;
pub mod machine_code;
25

26 27
pub use compiler::passes::CompilerPass;

28
pub struct Compiler<'vm> {
29
    policy: RefCell<CompilerPolicy>,
30
    vm: &'vm VM
31 32
}

33 34
impl <'vm> Compiler<'vm> {
    pub fn new(policy: CompilerPolicy, vm: &VM) -> Compiler {
35 36 37 38
        Compiler{
            policy: RefCell::new(policy),
            vm: vm
        }
39
    }
40

41
    pub fn compile(&self, func: &mut MuFunctionVersion) {
qinsoon's avatar
qinsoon committed
42 43 44 45
        info!("");
        info!("Start compiling {}", func);
        info!("");
        debug!("{:?}", func);
46
        
qinsoon's avatar
qinsoon committed
47 48
        // FIXME: should use function name here (however hprof::enter only accept &'static str)
        let _p = hprof::enter("Function Compilation");
49

50
        let ref mut passes = self.policy.borrow_mut().passes;
51

52 53
        for pass in passes.iter_mut() {
            let _p = hprof::enter(pass.name());
54

55
            pass.execute(self.vm, func);
56 57

            drop(_p);
58
        }
59

60
        drop(_p);
61 62

        hprof_print_timing(hprof::profiler().root());
63 64

        func.set_compiled();
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![];
qinsoon's avatar
qinsoon committed
85
        passes.push(Box::new(passes::Inlining::new()));
86
        // ir level passes
qinsoon's avatar
qinsoon committed
87 88
        passes.push(Box::new(passes::DefUse::new()));
        passes.push(Box::new(passes::TreeGen::new()));
89
        passes.push(Box::new(passes::GenMovPhi::new()));
90
        passes.push(Box::new(passes::ControlFlowAnalysis::new()));
91
        passes.push(Box::new(passes::TraceGen::new()));
92

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

        // machine code level passes
98 99
        passes.push(Box::new(backend::peephole_opt::PeepholeOptimization::new()));
        passes.push(Box::new(backend::code_emission::CodeEmission::new()));
100

101 102
        CompilerPolicy{passes: passes}
    }
103
}
104

105
// rewrite parts of the hprof crates to print via log (instead of print!())
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167
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.)
        }
    }
}