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.82 KB
Newer Older
1
use ast::ir::*;
qinsoon's avatar
qinsoon committed
2 3
use vm::context::VMContext;

4 5 6
use std::cell::RefCell;

pub mod passes;
7
pub mod backend;
8 9 10 11 12 13 14 15 16 17

pub struct Compiler {
    policy: RefCell<CompilerPolicy>
}

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

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
32 33
        passes.push(Box::new(passes::DefUse::new()));
        passes.push(Box::new(passes::TreeGen::new()));
34
        passes.push(Box::new(passes::ControlFlowAnalysis::new()));
35
        passes.push(Box::new(passes::TraceGen::new()));
36
        passes.push(Box::new(backend::inst_sel::InstructionSelection::new()));
37 38 39
        
        CompilerPolicy{passes: passes}
    }
qinsoon's avatar
qinsoon committed
40 41 42 43
    
    pub fn new(passes: Vec<Box<CompilerPass>>) -> CompilerPolicy {
        CompilerPolicy{passes: passes}
    }
44 45
}

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