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

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

qinsoon's avatar
qinsoon committed
10 11
use compiler::backend::init_machine_regs_for_func;

12
mod graph_coloring;
13

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

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

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