Example #1
0
        public static List<List<string>> assign(Graph graph)
        {
            List<List<string>> assigned = new List<List<string>>();
            List<string> available = new List<string>();

            Regex literal = new Regex(@"^[0-9]+");
            Regex str = new Regex(@"^"".+""");
            Regex memory = new Regex(@"[MEM.+]");

            //build list of available registers
            for(int i = 1; i <= Allocate.registers; i++){
            string reg = "R" + i;
            if(!livein.Contains(reg)){
                available.Add(reg);
            }
            }

            //assign registers
            for(int i = 0; i < graph.Count; i++){
            Node n = graph.Get(i);
            if(n.isRegister || str.IsMatch(n.id) || memory.IsMatch(n.id)){
                assigned.Add(new List<string>(){n.id,n.id});
            }
            else if(literal.IsMatch(n.id)){
                assigned.Add(new List<string>(){n.id,"="+n.id});
            }
            else{
                assigned.Add(new List<string>(){n.id,available[0]});
                available.Remove(available[0]);
            }
            }
            return assigned;
        }
Example #2
0
 public static void takeInput(List<List<string>> inputIn, List<string> live)
 {
     input = inputIn;
     livein = live;
     graph = new Graph();
     Console.WriteLine("Registers = " + registers);
 }
Example #3
0
        public static List<List<string>> simplify(Graph graph, List<string> spill)
        {
            //conditions to start colouring graph
            if(graph.Count == 1 || graph.isRegisters()){
            return assign(graph);
            }

            List<List<string>> result = new List<List<string>>();
            List<int> degrees = new List<int>();
            Node n;

            for(int i = 0; i < graph.Count; i++){
            if(graph.Get(i).isRegister){
                degrees.Add(-1); //causes nodes that are already registers to be ignored when simplifying
            }
            else{
                degrees.Add(graph.Get(i).getDegree());
            }
            }

            //define degree that node must be at (or less than) to be simplified
            int k = Allocate.registers - 1;
            while(k >= 0){
            if(degrees.Contains(k)){
                int index = degrees.IndexOf(k);
                n = graph.Get(index);
                graph.Remove(n);
                result = simplify(graph, spill);
                if(result == null){
                    return null;
                }
                graph.Add(n);
                return appendToAllocated(result, n);
            }
            k--;
            }
            n = graph.GetHighestDegreeNode();
            graph.Remove(n);
            spill.Add(n.id);
            result = simplify(graph, spill);
            if(result == null){
            return null;
            }
            graph.Add(n);
            result = appendToAllocated(result, n); //you know, let's just try to colour the node anyway
            if(result != null){
            spill.Remove(n.id);
            }
            return result;
        }