2  * Takes a interference graph and generates an ordering for coloring
 
   3  * Author: Joshua Wise <jwise@andrew.cmu.edu>
 
   4  * Author: Chris Lu <czl@aundrew.cmu.edu>
 
   9   structure OperSet : ORD_SET
 
  10     where type Key.ord_key = x86.basicop
 
  11   structure LiveMap : ORD_MAP
 
  12     where type Key.ord_key = int
 
  13   structure TempMap : ORD_MAP
 
  14     where type Key.ord_key = Temp.temp
 
  16   type igraph = OperSet.set TempMap.map
 
  17   type ordering = Temp.temp list
 
  19   val colororder : Igraph.graph * Temp.temp list -> ordering
 
  22 structure ColorOrder :> COLORORDER =
 
  27   structure OperSet = Igraph.OperSet
 
  28   structure LiveMap = Igraph.LiveMap
 
  29   structure TempMap = Igraph.TempMap
 
  31   type igraph = OperSet.set TempMap.map
 
  32   type ordering = Temp.temp list
 
  34   fun colororder (graph,temps) =
 
  36       val initialWeights = TempMap.mapi (fn (t, _) => (t, 0)) graph
 
  38       (* Chooses one temporary to pick, and updates the weights. *)
 
  39       fun orderOne (weights : (Temp.temp * int) list) : Temp.temp * (Temp.temp * int) list =
 
  43               (fn ((t1, w1), (t2, w2)) =>
 
  47               (Temp.new "emarnus" Temp.Word, ~9999)
 
  50           fun ditchOne f nil = nil      (* Special case of filter, which bails out after it removes one. *)
 
  54               else h::(ditchOne f l)
 
  55           val remaining = ditchOne (fn (t, w) => Temp.eq (t, chosen)) weights
 
  57           val neighbors =                       (* Grab all the neighbors for some given temp. *)
 
  59               (valOf (TempMap.find (graph, chosen))))
 
  65                         (fn X.TEMP t' => (T.eq (t, t'))
 
  76       (* Recursively order until we run out of things to order. *)
 
  77       fun keepOrdering (nil : (Temp.temp * int) list) : Temp.temp list = nil
 
  78         | keepOrdering (weights) =
 
  80             val (chosen, newWeights) = orderOne weights
 
  82             chosen :: (keepOrdering newWeights)
 
  85       val ordered = keepOrdering (TempMap.listItems initialWeights)
 
  87       ordered @ (List.filter (fn a => not (List.exists (fn b => Temp.eq (a,b)) ordered)) temps)