3 * colors a graph and returns a list of nodes with associated colors
4 * Author: Joshua Wise <jwise@andrew.cmu.edu>
5 * Author: Chris Lu <czl@andrew.cmu.edu>
10 type temps = Temp.temp list
11 type colorlist = (Temp.temp * int) list
12 type igraph = (Temp.temp * x86.oper list) list
14 val colorize : temps -> igraph -> colorlist
17 structure Colorizer :> COLORIZER =
19 type temps = Temp.temp list
20 type colorlist = (Temp.temp * int) list
21 type igraph = (Temp.temp * x86.oper list) list
25 (* val color_single : igraph -> Temp.temp * colorlist -> colorlist
26 * color_single graph (temp, regs) => takes an interference graph, the temp to be colored, and the
27 * already-colored nodes, colors the temp, and adds it to the list
28 * this is a helper function for the foldr in colorize
30 fun color_single (graph: igraph) (temp, regs) =
32 (* Grab the list of interfering operands from the graph *)
33 val interfere = case List.find (fn (temp',_) => Temp.compare (temp', temp) = EQUAL) graph
35 | NONE => raise ErrorMsg.InternalError "Temporary not found in graph"
37 (* Grab the subset of those that are already colorized *)
42 (fn X.TEMP t' => Temp.compare (t, t') = EQUAL
47 (* Grab the subset of those that are fixed colors *)
54 (* Grab the register number of the already-colorized ones *)
63 | _ => raise ErrorMsg.InternalError "Bad kind of specreg")
65 (* Greedy-colorize -- pick the lowest number that isn't used by a neighbor *)
67 if (List.exists (fn a => a = i) l)
71 val newcolor = greedy 0 ints
72 (* val () = print (" Assigned color "^(Int.toString newcolor)^" to temp "^(Temp.name temp)^"\n") *)
74 (temp, (greedy 0 ints)) :: regs
77 (* val colorize : temps -> igraph -> colorlist
78 * colorizes a graph given the graph representation and the order in which to color
79 * nodes, returns a list of nodes numbered with their respective color *)
80 fun colorize order graph = foldl (color_single graph) nil order