1 : |
george |
1087 |
functor CFGViewer
|
2 : |
|
|
(structure CFG : CONTROL_FLOW_GRAPH
|
3 : |
|
|
structure GraphViewer : GRAPH_VIEWER
|
4 : |
|
|
structure Asm : INSTRUCTION_EMITTER where I = CFG.I)
|
5 : |
|
|
: sig
|
6 : |
|
|
val view : CFG.cfg -> unit
|
7 : |
|
|
end =
|
8 : |
|
|
struct
|
9 : |
|
|
structure CFG = CFG
|
10 : |
|
|
structure L = GraphLayout
|
11 : |
|
|
structure FMT = FormatInstruction(Asm)
|
12 : |
|
|
structure G = Graph
|
13 : |
|
|
|
14 : |
|
|
val outline = MLRiscControl.getFlag "view-outline"
|
15 : |
|
|
|
16 : |
|
|
fun view(cfg as G.GRAPH g) = let
|
17 : |
|
|
val CFG.INFO{annotations, ...} = #graph_info g
|
18 : |
|
|
val toString = FMT.toString (!annotations)
|
19 : |
|
|
fun graph _ = []
|
20 : |
george |
1173 |
val colorScale =
|
21 : |
|
|
Array.fromList
|
22 : |
|
|
["#ccffff", "#99ffff", "#66ccff", "#54a9ff", "#ccff99",
|
23 : |
|
|
"#ffff99", "#ffcc66", "#ff9966", "#cc6666", "#d14949",
|
24 : |
|
|
"#d14949"]
|
25 : |
george |
1087 |
|
26 : |
george |
1173 |
fun weightRange([], min, max) = (min, max-min)
|
27 : |
|
|
| weightRange((_,_,CFG.EDGE{w, ...})::rest, min, max) = let
|
28 : |
|
|
val wt = !w
|
29 : |
|
|
in
|
30 : |
|
|
if wt > max then weightRange(rest, min, wt)
|
31 : |
|
|
else if wt < min then weightRange(rest, wt, max)
|
32 : |
|
|
else weightRange(rest, min, max)
|
33 : |
|
|
end
|
34 : |
george |
1087 |
|
35 : |
george |
1173 |
val (loWt, range) = weightRange( #edges g (), ~1.0, ~1.0)
|
36 : |
|
|
|
37 : |
george |
1258 |
fun color w = let
|
38 : |
|
|
val pos =
|
39 : |
|
|
if range < 100.0
|
40 : |
|
|
then floor(((w-loWt) * 10.0) / range)
|
41 : |
|
|
else floor (Math.log10(w-loWt) * 10.0 / Math.log10 range)
|
42 : |
|
|
in
|
43 : |
|
|
Array.sub(colorScale, pos)
|
44 : |
|
|
end
|
45 : |
|
|
|
46 : |
george |
1087 |
val ENTRY = hd(#entries g ())
|
47 : |
|
|
val EXIT = hd(#exits g ())
|
48 : |
|
|
|
49 : |
george |
1173 |
val red = L.COLOR "#ff0000"
|
50 : |
|
|
val yellow = L.COLOR "yellow"
|
51 : |
|
|
val green = L.COLOR "green"
|
52 : |
|
|
|
53 : |
george |
1087 |
fun edge(i,j,CFG.EDGE{w, ...}) =
|
54 : |
jhr |
1125 |
let val label = L.LABEL(Real.toString (!w))
|
55 : |
george |
1258 |
in [label, L.COLOR(color(!w))]
|
56 : |
|
|
end
|
57 : |
george |
1087 |
|
58 : |
|
|
fun title(blknum,ref freq) =
|
59 : |
george |
1222 |
" "^Int.toString blknum ^ " freq="^Real.toString freq
|
60 : |
george |
1087 |
|
61 : |
|
|
fun ann(annotations) =
|
62 : |
|
|
List.foldl(fn (a,l) => "/* "^Annotations.toString a^" */\n"^l) ""
|
63 : |
|
|
(!annotations)
|
64 : |
|
|
|
65 : |
george |
1222 |
fun node(_, CFG.BLOCK{kind, labels, id, freq, insns, annotations, ...}) =
|
66 : |
george |
1087 |
(case kind
|
67 : |
|
|
of CFG.START =>
|
68 : |
|
|
[L.LABEL("entry"^title(id,freq)^"\n"^ann(annotations))]
|
69 : |
|
|
| CFG.STOP =>
|
70 : |
|
|
[L.LABEL("exit"^title(id,freq))]
|
71 : |
|
|
| _ =>
|
72 : |
george |
1222 |
[L.LABEL("BLK"^title(id,freq)^"\n"^
|
73 : |
|
|
(case !labels
|
74 : |
|
|
of [] => ""
|
75 : |
|
|
| labs =>
|
76 : |
|
|
String.concatWith ":\n" (map Label.toString labs) ^ ":\n"
|
77 : |
|
|
(*easc*)) ^
|
78 : |
george |
1087 |
ann(annotations)^
|
79 : |
|
|
(if !outline then "" else
|
80 : |
george |
1222 |
List.foldl
|
81 : |
|
|
(fn (i,t) => let val text = toString i
|
82 : |
|
|
in if text = "" then t else text^"\n"^t
|
83 : |
|
|
end)
|
84 : |
|
|
""
|
85 : |
|
|
(!insns)))]
|
86 : |
george |
1087 |
(*esac*))
|
87 : |
|
|
|
88 : |
|
|
in
|
89 : |
|
|
GraphViewer.view
|
90 : |
|
|
(L.makeLayout{graph=graph, edge=edge, node=node} cfg)
|
91 : |
|
|
end
|
92 : |
|
|
|
93 : |
|
|
end
|