WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

mod.rs 2.76 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(backend::inst_sel::InstructionSelection::new()));
36 37 38
        
        CompilerPolicy{passes: passes}
    }
qinsoon's avatar
qinsoon committed
39 40 41 42
    
    pub fn new(passes: Vec<Box<CompilerPass>>) -> CompilerPolicy {
        CompilerPolicy{passes: passes}
    }
43 44
}

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