X-Git-Url: http://git.joshuawise.com/snipe.git/blobdiff_plain/0a24e44d4e9f82f8d3d83de8e58c83c8cf2868b6..5c90fbb8681e975ccd0a1bc407b31daa1daef38a:/codegen/colororder.sml diff --git a/codegen/colororder.sml b/codegen/colororder.sml index f533cea..f1a724b 100644 --- a/codegen/colororder.sml +++ b/codegen/colororder.sml @@ -1,4 +1,4 @@ -(* L2 Compiler +(* L3 Compiler * Takes a interference graph and generates an ordering for coloring * Author: Joshua Wise * Author: Chris Lu @@ -6,43 +6,63 @@ signature COLORORDER = sig - type igraph = (Temp.temp * x86.oper list) list + structure OperSet : ORD_SET + where type Key.ord_key = Blarg.oper + structure LiveMap : ORD_MAP + where type Key.ord_key = int + structure TempMap : ORD_MAP + where type Key.ord_key = Temp.temp + + type igraph = OperSet.set TempMap.map type ordering = Temp.temp list - val colororder : igraph -> ordering + val colororder : Igraph.graph * Temp.temp list -> ordering end structure ColorOrder :> COLORORDER = struct structure T = Temp - structure X = x86 + structure X = Blarg + + structure OperSet = Igraph.OperSet + structure LiveMap = Igraph.LiveMap + structure TempMap = Igraph.TempMap - type igraph = (Temp.temp * x86.oper list) list + type igraph = OperSet.set TempMap.map type ordering = Temp.temp list - fun colororder graph = + fun colororder (graph,temps) = let - val initialWeights = map (fn (t, _) => (t, 0)) graph + val initialWeights = TempMap.mapi (fn (t, _) => (t, 0)) graph - fun sortWeights weights = (* Sort the weights such that the largest is at left, ready to be grabbed. *) - ListMergeSort.sort (fn ((_, a), (_, b)) => a < b) weights - (* Chooses one temporary to pick, and updates the weights. *) fun orderOne (weights : (Temp.temp * int) list) : Temp.temp * (Temp.temp * int) list = let - val sorted = sortWeights weights - val (chosen, w) = List.hd sorted (* Grab the temp with the highest weight. *) - val remaining = List.tl sorted + val (chosen, w) = + foldr + (fn ((t1, w1), (t2, w2)) => + if (w2 > w1) + then (t2, w2) + else (t1, w1)) + (Temp.new "emarnus", ~9999) + weights + + fun ditchOne f nil = nil (* Special case of filter, which bails out after it removes one. *) + | ditchOne f (h::l) = + if f h + then l + else h::(ditchOne f l) + val remaining = ditchOne (fn (t, w) => Temp.eq (t, chosen)) weights + val neighbors = (* Grab all the neighbors for some given temp. *) - List.hd - (List.map (fn (_, neighbors) => neighbors) - (List.filter (fn (t, _) => T.compare (t, chosen) = EQUAL) graph)) + (OperSet.listItems + (valOf (TempMap.find (graph, chosen)))) val newWeights = List.map (fn (t, wt) => (t, if (List.exists - (fn X.TEMP t' => (T.compare (t, t') = EQUAL) + (fn X.TEMP t' => (T.eq (t, t')) | _ => false) neighbors) then (wt + 1) @@ -61,7 +81,9 @@ struct in chosen :: (keepOrdering newWeights) end + + val ordered = keepOrdering (TempMap.listItems initialWeights) in - (keepOrdering initialWeights) + ordered @ (List.filter (fn a => not (List.exists (fn b => Temp.eq (a,b)) ordered)) temps) end end