mod.rs 2.47 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2
#![allow(dead_code)]

3
use compiler::CompilerPass;
4
use compiler::PassExecutionResult;
5
use ast::ir::*;
qinsoon's avatar
qinsoon committed
6
use vm::VM;
7

qinsoon's avatar
qinsoon committed
8 9
use compiler::backend::init_machine_regs_for_func;

10
mod graph_coloring;
11

12 13 14 15 16 17 18 19 20 21 22 23
pub struct RegisterAllocation {
    name: &'static str
}

impl RegisterAllocation {
    pub fn new() -> RegisterAllocation {
        RegisterAllocation {
            name: "Register Allcoation"
        }
    }
    
    #[allow(unused_variables)]
24
    // returns true if we spill registers (which requires another instruction selection)
qinsoon's avatar
qinsoon committed
25 26
    fn coloring(&mut self, vm: &VM, func: &mut MuFunctionVersion) -> bool {
        let compiled_funcs = vm.compiled_funcs().read().unwrap();
qinsoon's avatar
qinsoon committed
27
        let mut cf = compiled_funcs.get(&func.id()).unwrap().write().unwrap();
28
        
qinsoon's avatar
qinsoon committed
29
        cf.mc().trace_mc();
30
        
qinsoon's avatar
qinsoon committed
31 32 33
        // initialize machine registers for the function context
        init_machine_regs_for_func(&mut func.context);
        
34
        let liveness = graph_coloring::build_inteference_graph(&mut cf, func);
35
        liveness.print();
qinsoon's avatar
qinsoon committed
36
        
37
        let coloring = graph_coloring::GraphColoring::start(liveness);
38
        let spills = coloring.spills();
39 40
        
        if !spills.is_empty() {
41
            return false;
42 43 44 45 46 47 48 49
        }
        
        // replace regs
        trace!("Replacing Registers...");
        for node in coloring.ig.nodes() {
            let temp = coloring.ig.get_temp_of(node);
            
            // skip machine registers
qinsoon's avatar
qinsoon committed
50
            if temp < MACHINE_ID_END {
51 52 53 54 55 56
                continue;
            } else {
                let alias = coloring.get_alias(node);
                let machine_reg = coloring.ig.get_color_of(alias).unwrap();
                
                trace!("replacing {} with {}", temp, machine_reg);
qinsoon's avatar
qinsoon committed
57
                cf.mc_mut().replace_reg(temp, machine_reg);
58 59
                
                cf.temps.insert(temp, machine_reg);
60 61 62
            }
        }
        
qinsoon's avatar
qinsoon committed
63
        cf.mc().trace_mc();
64 65 66 67 68 69 70 71 72 73
        
        true
    }    
}

impl CompilerPass for RegisterAllocation {
    fn name(&self) -> &'static str {
        self.name
    }
    
qinsoon's avatar
qinsoon committed
74
    fn execute(&mut self, vm: &VM, func: &mut MuFunctionVersion) -> PassExecutionResult {
qinsoon's avatar
qinsoon committed
75
        debug!("---CompilerPass {} for {}---", self.name(), func);
76
        
qinsoon's avatar
qinsoon committed
77
        if self.coloring(vm, func) {
78 79 80 81
            debug!("---finish---");
            
            PassExecutionResult::ProceedToNext
        } else {
82 83 84
            // PassExecutionResult::GoBackTo(compiler::PASS_INST_SEL)
                        
            unimplemented!()
85
        }
86
    }
87
}