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