mod.rs 2.42 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
34
35
        
        CompilerPolicy{passes: passes}
    }
qinsoon's avatar
qinsoon committed
36
37
38
39
    
    pub fn new(passes: Vec<Box<CompilerPass>>) -> CompilerPolicy {
        CompilerPolicy{passes: passes}
    }
40
41
}

qinsoon's avatar
qinsoon committed
42
#[allow(unused_variables)]
43
pub trait CompilerPass {
qinsoon's avatar
qinsoon committed
44
45
46
47
48
49
50
    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);
        
51
        for entry in func.content.as_mut().unwrap().blocks.iter_mut() {
qinsoon's avatar
qinsoon committed
52
53
54
55
56
            let label : MuTag = entry.0;
            let ref mut block : &mut Block = &mut entry.1;
            
            debug!("block: {}", label);
            
qinsoon's avatar
qinsoon committed
57
58
            self.visit_block(vm_context, &mut func.context, block);
            
59
            for inst in block.content.as_mut().unwrap().body.iter_mut() {
60
                debug!("{}", inst);
61
                
qinsoon's avatar
qinsoon committed
62
                self.visit_inst(vm_context, &mut func.context, inst);
qinsoon's avatar
qinsoon committed
63
64
            }
            
qinsoon's avatar
qinsoon committed
65
            self.finish_block(vm_context, &mut func.context, block);
qinsoon's avatar
qinsoon committed
66
        }
qinsoon's avatar
qinsoon committed
67
68
69
        
        self.finish_function(vm_context, func);
        debug!("---finish---");
qinsoon's avatar
qinsoon committed
70
71
72
    }
    
    fn visit_function(&mut self, vm_context: &VMContext, func: &mut MuFunction) {}
qinsoon's avatar
qinsoon committed
73
74
75
76
77
78
    fn finish_function(&mut self, vm_context: &VMContext, func: &mut MuFunction) {}
    
    fn visit_block(&mut self, vm_context: &VMContext, func_context: &mut FunctionContext, block: &mut Block) {}
    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) {}
79
}