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

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

qinsoon's avatar
qinsoon committed
12 13
use std::collections::HashMap;

14
mod graph_coloring;
15

16
pub enum RegAllocFailure {
17 18 19
    FailedForSpilling,
}

20
pub struct RegisterAllocation {
21
    name: &'static str,
22 23 24
}

impl RegisterAllocation {
25
    pub fn new() -> RegisterAllocation {
26
        RegisterAllocation {
27
            name: "Register Allcoation",
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() {
qinsoon's avatar
qinsoon committed
55 56 57 58 59 60 61 62 63
            let mut spilled_mem = HashMap::new();

            // allocating frame slots for every spilled temp
            for reg_id in spills.iter() {
                let ssa_entry = match func.context.get_value(*reg_id) {
                    Some(entry) => entry,
                    None => panic!("The spilled register {} is not in func context", reg_id)
                };
                let mem = cf.frame.alloc_slot_for_spilling(ssa_entry.value().clone(), vm);
64

qinsoon's avatar
qinsoon committed
65 66 67 68 69 70
                spilled_mem.insert(*reg_id, mem);
            }

            self.spill_rewrite(&spilled_mem, func, &mut cf, vm);

            return Err(RegAllocFailure::FailedForSpilling);
71 72 73 74 75 76 77 78
        }
        
        // 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
79
            if temp < MACHINE_ID_END {
80 81 82 83 84 85
                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
86
                cf.mc_mut().replace_reg(temp, machine_reg);
87 88
                
                cf.temps.insert(temp, machine_reg);
89 90 91
            }
        }
        
qinsoon's avatar
qinsoon committed
92
        cf.mc().trace_mc();
93
        
94
        Ok(())
qinsoon's avatar
qinsoon committed
95 96 97 98 99 100 101 102 103 104 105 106
    }

    #[cfg(feature = "aot")]
    fn spill_rewrite(
        &mut self,
        spills: &HashMap<MuID, P<Value>>,
        func: &mut MuFunctionVersion,
        compiled_func: &mut CompiledFunction,
        vm: &VM)
    {
        unimplemented!()
    }
107 108 109 110 111 112 113
}

impl CompilerPass for RegisterAllocation {
    fn name(&self) -> &'static str {
        self.name
    }
    
qinsoon's avatar
qinsoon committed
114
    fn execute(&mut self, vm: &VM, func: &mut MuFunctionVersion) -> PassExecutionResult {
qinsoon's avatar
qinsoon committed
115
        debug!("---CompilerPass {} for {}---", self.name(), func);
116 117 118 119 120 121
        
        match self.coloring(vm, func) {
            // skip slow path
            Ok(_) => PassExecutionResult::ProceedTo(compiler::PASS_PEEPHOLE),

            // go back to instruction selection for spilled operands
122
            Err(RegAllocFailure::FailedForSpilling) => PassExecutionResult::GoBackTo(compiler::PASS_INST_SEL),
123
        }
124
    }
125
}