GitLab will be upgraded to the 12.10.14-ce.0 on 28 Sept 2020 at 2.00pm (AEDT) to 2.30pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

mod.rs 2.67 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![];
qinsoon's avatar
qinsoon committed
31 32
        passes.push(Box::new(passes::DefUse::new()));
        passes.push(Box::new(passes::TreeGen::new()));
33
        passes.push(Box::new(passes::ControlFlowAnalysis::new()));
34 35 36
        
        CompilerPolicy{passes: passes}
    }
qinsoon's avatar
qinsoon committed
37 38 39 40
    
    pub fn new(passes: Vec<Box<CompilerPass>>) -> CompilerPolicy {
        CompilerPolicy{passes: passes}
    }
41 42
}

qinsoon's avatar
qinsoon committed
43
#[allow(unused_variables)]
44
pub trait CompilerPass {
qinsoon's avatar
qinsoon committed
45 46 47 48 49
    fn name(&self) -> &'static str;
    
    fn execute(&mut self, vm_context: &VMContext, func: &mut MuFunction) {
        debug!("---CompilerPass {} for {}---", self.name(), func.fn_name);
        
50
        self.start_function(vm_context, func);
qinsoon's avatar
qinsoon committed
51
        self.visit_function(vm_context, func);
52
        self.finish_function(vm_context, func);        
qinsoon's avatar
qinsoon committed
53
        
54 55 56 57 58
        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
59 60
            debug!("block: {}", label);
            
61
            self.start_block(vm_context, &mut func.context, block);
qinsoon's avatar
qinsoon committed
62 63
            self.visit_block(vm_context, &mut func.context, block);
            self.finish_block(vm_context, &mut func.context, block);
qinsoon's avatar
qinsoon committed
64 65 66
        }
    }
    
67 68 69 70 71 72 73 74 75
    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
76 77
    fn finish_function(&mut self, vm_context: &VMContext, func: &mut MuFunction) {}
    
78
    fn start_block(&mut self, vm_context: &VMContext, func_context: &mut FunctionContext, block: &mut Block) {}
qinsoon's avatar
qinsoon committed
79 80 81
    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) {}
82
}