| 1 |
(* |
| 2 |
* This is a simple module for viewing a cluster graph graphically. |
| 3 |
* This is meant to be used only by those of you who don't want to |
| 4 |
* migrate to the CFG data structure. |
| 5 |
* |
| 6 |
* -- Allen |
| 7 |
*) |
| 8 |
functor ClusterViewer |
| 9 |
(structure ClusterGraph : CLUSTER_GRAPH |
| 10 |
structure GraphViewer : GRAPH_VIEWER |
| 11 |
structure Asm : INSTRUCTION_EMITTER |
| 12 |
sharing ClusterGraph.F.I = Asm.I |
| 13 |
) : CLUSTER_VIEWER = |
| 14 |
struct |
| 15 |
|
| 16 |
structure ClusterGraph = ClusterGraph |
| 17 |
structure F = ClusterGraph.F |
| 18 |
structure W = ClusterGraph.W |
| 19 |
structure L = GraphLayout |
| 20 |
structure FMT = FormatInstruction(Asm) |
| 21 |
structure G = Graph |
| 22 |
|
| 23 |
val outline = MLRiscControl.getFlag "view-outline" |
| 24 |
|
| 25 |
fun view(clusterGraph as G.GRAPH cfg) = |
| 26 |
let val F.CLUSTER{annotations,...} = ClusterGraph.cluster clusterGraph |
| 27 |
val toString = FMT.toString (!annotations) |
| 28 |
fun graph _ = [] |
| 29 |
|
| 30 |
val red = L.COLOR "red" |
| 31 |
val yellow = L.COLOR "yellow" |
| 32 |
val green = L.COLOR "green" |
| 33 |
val ENTRY = hd(#entries cfg ()) |
| 34 |
val EXIT = hd(#exits cfg ()) |
| 35 |
|
| 36 |
fun edge(i,j,ref w) = |
| 37 |
let val label = L.LABEL(W.toString w) |
| 38 |
val color = |
| 39 |
if i = ENTRY orelse j = EXIT then green (* special edge *) |
| 40 |
else if i+1 = j then yellow (* fallsthru *) |
| 41 |
else red |
| 42 |
in [label, color] end |
| 43 |
|
| 44 |
fun title(blknum,ref freq) = |
| 45 |
" "^Int.toString blknum^" ("^W.toString freq^")" |
| 46 |
|
| 47 |
fun ann(annotations) = |
| 48 |
List.foldl(fn (a,l) => "/* "^Annotations.toString a^" */\n"^l) "" |
| 49 |
(!annotations) |
| 50 |
|
| 51 |
fun node(_,F.ENTRY{blknum,freq,...}) = |
| 52 |
[L.LABEL("entry"^title(blknum,freq)^"\n"^ann(annotations))] |
| 53 |
| node(_,F.EXIT{blknum,freq,...}) = |
| 54 |
[L.LABEL("exit"^title(blknum,freq))] |
| 55 |
| node(_,F.BBLOCK{annotations,blknum,freq,insns,...}) = |
| 56 |
[L.LABEL(title(blknum,freq)^"\n"^ |
| 57 |
ann(annotations)^ |
| 58 |
(if !outline then "" else |
| 59 |
List.foldl (fn (i,t) => |
| 60 |
let val text = toString i |
| 61 |
in if text = "" then t else text^"\n"^t end |
| 62 |
) "" (!insns)))] |
| 63 |
| node (_,_) = [L.LABEL "???"] |
| 64 |
in GraphViewer.view |
| 65 |
(L.makeLayout{graph=graph, edge=edge, node=node} clusterGraph) |
| 66 |
end |
| 67 |
|
| 68 |
end |