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

4 5 6 7 8 9 10 11 12 13 14 15 16
use std::cell::RefCell;

pub mod passes;

pub struct Compiler {
    policy: RefCell<CompilerPolicy>
}

impl Compiler {
    pub fn new(policy: CompilerPolicy) -> Compiler {
        Compiler{policy: RefCell::new(policy)}
    }
    
qinsoon's avatar
qinsoon committed
17
    pub fn compile(&self, vm: &VMContext, func: &mut MuFunction) {
18
        for pass in self.policy.borrow_mut().passes.iter_mut() {
qinsoon's avatar
qinsoon committed
19
            pass.execute(vm, func);
20 21 22 23 24 25 26 27 28 29 30
        }
    }
}

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

impl CompilerPolicy {
    pub fn default() -> CompilerPolicy {
        let mut passes : Vec<Box<CompilerPass>> = vec![];
31
        passes.push(Box::new(passes::def_use::DefUsePass::new("DefUse")));
qinsoon's avatar
qinsoon committed
32
        passes.push(Box::new(passes::tree_gen::TreeGenerationPass::new("Tree Generation")));
33 34 35 36 37 38
        
        CompilerPolicy{passes: passes}
    }
}

pub trait CompilerPass {
qinsoon's avatar
qinsoon committed
39 40 41 42 43 44 45
    fn name(&self) -> &'static str;
    
    fn execute(&mut self, vm_context: &VMContext, func: &mut MuFunction) {
        debug!("---CompilerPass {} for {}---", self.name(), func.fn_name);
        
        self.visit_function(vm_context, func);
        
46
        for entry in func.content.as_mut().unwrap().blocks.iter_mut() {
qinsoon's avatar
qinsoon committed
47 48 49 50 51
            let label : MuTag = entry.0;
            let ref mut block : &mut Block = &mut entry.1;
            
            debug!("block: {}", label);
            
52 53 54 55
            for inst in block.content.as_mut().unwrap().body.iter_mut() {
                debug!("{:?}", inst);
                
                self.visit_inst(vm_context, inst);
qinsoon's avatar
qinsoon committed
56 57 58 59 60 61 62 63
            }
            
            debug!("---finish---");
        }
    }
    
    fn visit_function(&mut self, vm_context: &VMContext, func: &mut MuFunction) {}
    fn visit_block(&mut self, vm_context: &VMContext, block: &mut Block) {}
64
    fn visit_inst(&mut self, vm_context: &VMContext, node: &mut TreeNode) {}
65
}