mod.rs 2.91 KB
Newer Older
1
use ast::ir::*;
qinsoon's avatar
qinsoon committed
2 3
use vm::context::VMContext;

4
use std::cell::RefCell;
5
use std::sync::Arc;
6 7

pub mod passes;
8
pub mod backend;
9 10

pub struct Compiler {
11 12
    policy: RefCell<CompilerPolicy>,
    vm: Arc<VMContext>
13 14 15
}

impl Compiler {
16 17 18 19 20
    pub fn new(policy: CompilerPolicy, vm: Arc<VMContext>) -> Compiler {
        Compiler{
            policy: RefCell::new(policy),
            vm: vm
        }
21 22
    }
    
23
    pub fn compile(&self, func: &mut MuFunction) {
24
        for pass in self.policy.borrow_mut().passes.iter_mut() {
25
            pass.execute(&self.vm, func);
26 27 28 29 30 31 32 33 34 35 36
        }
    }
}

pub struct CompilerPolicy {
    passes: Vec<Box<CompilerPass>>
}

impl CompilerPolicy {
    pub fn default() -> CompilerPolicy {
        let mut passes : Vec<Box<CompilerPass>> = vec![];
qinsoon's avatar
qinsoon committed
37 38
        passes.push(Box::new(passes::DefUse::new()));
        passes.push(Box::new(passes::TreeGen::new()));
39
        passes.push(Box::new(passes::ControlFlowAnalysis::new()));
40
        passes.push(Box::new(passes::TraceGen::new()));
41
        passes.push(Box::new(backend::inst_sel::InstructionSelection::new()));
42 43 44
        
        CompilerPolicy{passes: passes}
    }
qinsoon's avatar
qinsoon committed
45 46 47 48
    
    pub fn new(passes: Vec<Box<CompilerPass>>) -> CompilerPolicy {
        CompilerPolicy{passes: passes}
    }
49 50
}

qinsoon's avatar
qinsoon committed
51
#[allow(unused_variables)]
52
pub trait CompilerPass {
qinsoon's avatar
qinsoon committed
53 54 55 56 57
    fn name(&self) -> &'static str;
    
    fn execute(&mut self, vm_context: &VMContext, func: &mut MuFunction) {
        debug!("---CompilerPass {} for {}---", self.name(), func.fn_name);
        
58
        self.start_function(vm_context, func);
qinsoon's avatar
qinsoon committed
59
        self.visit_function(vm_context, func);
60
        self.finish_function(vm_context, func);        
qinsoon's avatar
qinsoon committed
61
        
62 63 64 65 66
        debug!("---finish---");
    }
    
    fn visit_function(&mut self, vm_context: &VMContext, func: &mut MuFunction) {
        for (label, ref mut block) in func.content.as_mut().unwrap().blocks.iter_mut() {
qinsoon's avatar
qinsoon committed
67 68
            debug!("block: {}", label);
            
69
            self.start_block(vm_context, &mut func.context, block);
qinsoon's avatar
qinsoon committed
70 71
            self.visit_block(vm_context, &mut func.context, block);
            self.finish_block(vm_context, &mut func.context, block);
qinsoon's avatar
qinsoon committed
72 73 74
        }
    }
    
75 76 77 78 79 80 81 82 83
    fn visit_block(&mut self, vm_context: &VMContext, func_context: &mut FunctionContext, block: &mut Block) {
        for inst in block.content.as_mut().unwrap().body.iter_mut() {
            debug!("{}", inst);
            
            self.visit_inst(vm_context, func_context, inst);
        }    
    }
    
    fn start_function(&mut self, vm_context: &VMContext, func: &mut MuFunction) {}
qinsoon's avatar
qinsoon committed
84 85
    fn finish_function(&mut self, vm_context: &VMContext, func: &mut MuFunction) {}
    
86
    fn start_block(&mut self, vm_context: &VMContext, func_context: &mut FunctionContext, block: &mut Block) {}
qinsoon's avatar
qinsoon committed
87 88 89
    fn finish_block(&mut self, vm_context: &VMContext, func_context: &mut FunctionContext, block: &mut Block) {}
    
    fn visit_inst(&mut self, vm_context: &VMContext, func_context: &mut FunctionContext, node: &mut TreeNode) {}
90
}