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

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

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

11
mod graph_coloring;
12

13
pub enum RegAllocFailure {
14 15 16 17
    FailedForSpilling,
    FailedForUsingCallerSaved
}

18
pub struct RegisterAllocation {
19 20
    name: &'static str,
    is_fastpath: bool
21 22 23
}

impl RegisterAllocation {
24
    pub fn new(is_fastpath: bool) -> RegisterAllocation {
25
        RegisterAllocation {
26 27
            name: "Register Allcoation",
            is_fastpath: is_fastpath
28 29 30 31
        }
    }
    
    #[allow(unused_variables)]
32
    // returns true if we spill registers (which requires another instruction selection)
33
    fn coloring(&mut self, vm: &VM, func: &mut MuFunctionVersion) -> Result<(), RegAllocFailure> {
qinsoon's avatar
qinsoon committed
34
        let compiled_funcs = vm.compiled_funcs().read().unwrap();
qinsoon's avatar
qinsoon committed
35
        let mut cf = compiled_funcs.get(&func.id()).unwrap().write().unwrap();
36
        
qinsoon's avatar
qinsoon committed
37
        cf.mc().trace_mc();
38
        
qinsoon's avatar
qinsoon committed
39 40 41
        // initialize machine registers for the function context
        init_machine_regs_for_func(&mut func.context);
        
42
        let liveness = graph_coloring::build_inteference_graph(&mut cf, func);
43
        liveness.print();
44 45 46 47 48 49 50 51

        let coloring = match graph_coloring::GraphColoring::start(liveness) {
            Ok(coloring) => coloring,
            Err(err) => {
                return Err(err);
            }
        };

52
        let spills = coloring.spills();
53 54
        
        if !spills.is_empty() {
55
            return Err(RegAllocFailure::FailedForSpilling);
56 57 58 59 60 61 62 63
        }
        
        // 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
64
            if temp < MACHINE_ID_END {
65 66 67 68 69 70
                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
71
                cf.mc_mut().replace_reg(temp, machine_reg);
72 73
                
                cf.temps.insert(temp, machine_reg);
74 75 76
            }
        }
        
qinsoon's avatar
qinsoon committed
77
        cf.mc().trace_mc();
78
        
79
        Ok(())
80 81 82 83 84 85 86 87
    }    
}

impl CompilerPass for RegisterAllocation {
    fn name(&self) -> &'static str {
        self.name
    }
    
qinsoon's avatar
qinsoon committed
88
    fn execute(&mut self, vm: &VM, func: &mut MuFunctionVersion) -> PassExecutionResult {
qinsoon's avatar
qinsoon committed
89
        debug!("---CompilerPass {} for {}---", self.name(), func);
90 91

        if !self.is_fastpath {
92
            unimplemented!()
93
        }
94 95 96 97 98 99 100 101 102 103 104
        
        match self.coloring(vm, func) {
            // skip slow path
            Ok(_) => PassExecutionResult::ProceedTo(compiler::PASS_PEEPHOLE),

            // go back to instruction selection for spilled operands
            Err(RegAllocFailure::FailedForSpilling) => PassExecutionResult::GoBackTo(compiler::PASS_FAST_INST_SEL),

            // proceed to slow path
            Err(RegAllocFailure::FailedForUsingCallerSaved) => PassExecutionResult::ProceedToNext
        }
105
    }
106
}