2 * Gathers tiberium, fires rockets
3 * colors a graph and returns a list of nodes with associated colors
4 * Author: Chris Lu <czl@andrew>
9 type tiberium = Temp.temp list
10 type colorlist = (Temp.temp * int) list
11 type igraph = (Temp.temp * x86.oper list) list
13 val colorize : tiberium -> igraph -> colorlist
16 structure Colorizer :> COLORIZER =
18 type tiberium = Temp.temp list
19 type colorlist = (Temp.temp * int) list
20 type igraph = (Temp.temp * x86.oper list) list
24 (* val color_single : igraph -> Temp.temp * colorlist -> colorlist
25 * color_single graph (temp, regs) => takes an interference graph, the temp to be colored, and the
26 * already-colored nodes, colors the temp, and adds it to the list
27 * this is a helper function for the foldr in colorize
29 fun color_single (graph: igraph) (temp, regs) =
31 (* Grab the list of interfering operands from the graph *)
32 val interfere = case List.find (fn (temp',_) => Temp.compare (temp', temp) = EQUAL) graph
34 | NONE => raise ErrorMsg.InternalError "Temporary not found in graph"
36 (* Grab the subset of those that are already colorized *)
41 (fn X.TEMP t' => Temp.compare (t, t') = EQUAL
46 (* Grab the subset of those that are fixed colors *)
53 (* Grab the register number of the already-colorized ones *)
61 | _ => raise ErrorMsg.InternalError "Bad kind of specreg")
63 (* Greedy-colorize -- pick the lowest number that isn't used by a neighbor *)
65 if (List.exists (fn a => a = i) l)
69 val newcolor = greedy 0 ints
70 val () = print (" Assigned color "^(Int.toString newcolor)^" to temp "^(Temp.name temp)^"\n")
72 (temp, (greedy 0 ints)) :: regs
75 (* val colorize : tiberium -> igraph -> colorlist
76 * colorizes a graph given the graph representation and the order in which to color
77 * nodes, returns a list of nodes numbered with their respective color *)
78 fun colorize order graph = foldl (color_single graph) nil order