| 1 |
(* printFlowgraph.sml -- print flowgraph of target machine instructions. |
| 2 |
* |
| 3 |
* Copyright (c) 1997 Bell Laboratories. |
| 4 |
*) |
| 5 |
signature PRINT_FLOWGRAPH = |
| 6 |
sig |
| 7 |
structure Asm : INSTRUCTION_EMITTER |
| 8 |
structure CFG : CONTROL_FLOW_GRAPH (* where I = Asm.I and P = Asm.S.P *) |
| 9 |
where type I.addressing_mode = Asm.I.addressing_mode |
| 10 |
and type I.ea = Asm.I.ea |
| 11 |
and type I.instr = Asm.I.instr |
| 12 |
and type I.instruction = Asm.I.instruction |
| 13 |
and type I.operand = Asm.I.operand |
| 14 |
where type P.Client.pseudo_op = Asm.S.P.Client.pseudo_op |
| 15 |
and type P.T.Basis.cond = Asm.S.P.T.Basis.cond |
| 16 |
and type P.T.Basis.div_rounding_mode = Asm.S.P.T.Basis.div_rounding_mode |
| 17 |
and type P.T.Basis.ext = Asm.S.P.T.Basis.ext |
| 18 |
and type P.T.Basis.fcond = Asm.S.P.T.Basis.fcond |
| 19 |
and type P.T.Basis.rounding_mode = Asm.S.P.T.Basis.rounding_mode |
| 20 |
and type P.T.Constant.const = Asm.S.P.T.Constant.const |
| 21 |
and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) Asm.S.P.T.Extension.ccx |
| 22 |
and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) Asm.S.P.T.Extension.fx |
| 23 |
and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) Asm.S.P.T.Extension.rx |
| 24 |
and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) Asm.S.P.T.Extension.sx |
| 25 |
and type P.T.I.div_rounding_mode = Asm.S.P.T.I.div_rounding_mode |
| 26 |
and type P.T.Region.region = Asm.S.P.T.Region.region |
| 27 |
and type P.T.ccexp = Asm.S.P.T.ccexp |
| 28 |
and type P.T.fexp = Asm.S.P.T.fexp |
| 29 |
(* and type P.T.labexp = Asm.S.P.T.labexp *) |
| 30 |
and type P.T.mlrisc = Asm.S.P.T.mlrisc |
| 31 |
and type P.T.oper = Asm.S.P.T.oper |
| 32 |
and type P.T.rep = Asm.S.P.T.rep |
| 33 |
and type P.T.rexp = Asm.S.P.T.rexp |
| 34 |
and type P.T.stm = Asm.S.P.T.stm |
| 35 |
|
| 36 |
val printCFG : TextIO.outstream -> string -> CFG.cfg -> unit |
| 37 |
end |
| 38 |
|
| 39 |
|
| 40 |
functor PrintFlowgraph |
| 41 |
(structure Asm : INSTRUCTION_EMITTER |
| 42 |
structure CFG : CONTROL_FLOW_GRAPH (* where I = Asm.I and P = Asm.S.P *) |
| 43 |
where type I.addressing_mode = Asm.I.addressing_mode |
| 44 |
and type I.ea = Asm.I.ea |
| 45 |
and type I.instr = Asm.I.instr |
| 46 |
and type I.instruction = Asm.I.instruction |
| 47 |
and type I.operand = Asm.I.operand |
| 48 |
where type P.Client.pseudo_op = Asm.S.P.Client.pseudo_op |
| 49 |
and type P.T.Basis.cond = Asm.S.P.T.Basis.cond |
| 50 |
and type P.T.Basis.div_rounding_mode = Asm.S.P.T.Basis.div_rounding_mode |
| 51 |
and type P.T.Basis.ext = Asm.S.P.T.Basis.ext |
| 52 |
and type P.T.Basis.fcond = Asm.S.P.T.Basis.fcond |
| 53 |
and type P.T.Basis.rounding_mode = Asm.S.P.T.Basis.rounding_mode |
| 54 |
and type P.T.Constant.const = Asm.S.P.T.Constant.const |
| 55 |
and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) Asm.S.P.T.Extension.ccx |
| 56 |
and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) Asm.S.P.T.Extension.fx |
| 57 |
and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) Asm.S.P.T.Extension.rx |
| 58 |
and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) Asm.S.P.T.Extension.sx |
| 59 |
and type P.T.I.div_rounding_mode = Asm.S.P.T.I.div_rounding_mode |
| 60 |
and type P.T.Region.region = Asm.S.P.T.Region.region |
| 61 |
and type P.T.ccexp = Asm.S.P.T.ccexp |
| 62 |
and type P.T.fexp = Asm.S.P.T.fexp |
| 63 |
(* and type P.T.labexp = Asm.S.P.T.labexp *) |
| 64 |
and type P.T.mlrisc = Asm.S.P.T.mlrisc |
| 65 |
and type P.T.oper = Asm.S.P.T.oper |
| 66 |
and type P.T.rep = Asm.S.P.T.rep |
| 67 |
and type P.T.rexp = Asm.S.P.T.rexp |
| 68 |
and type P.T.stm = Asm.S.P.T.stm |
| 69 |
) : PRINT_FLOWGRAPH = |
| 70 |
struct |
| 71 |
structure Asm = Asm |
| 72 |
structure CFG = CFG |
| 73 |
structure C = CFG.I.C |
| 74 |
structure Fmt = Format |
| 75 |
|
| 76 |
val i2s = Int.toString |
| 77 |
|
| 78 |
fun printList stream list = let |
| 79 |
fun pr str = TextIO.output(stream, str) |
| 80 |
fun iter [] = () |
| 81 |
| iter [i] = pr i |
| 82 |
| iter (h::t) = (pr (h ^ ", "); iter t) |
| 83 |
in iter list |
| 84 |
end |
| 85 |
|
| 86 |
fun printCFG stream title (Cfg as Graph.GRAPH cfg) = |
| 87 |
let fun pr str = TextIO.output(stream, str) |
| 88 |
val prList = printList stream |
| 89 |
val annotations = !(CFG.annotations Cfg) |
| 90 |
val Asm.S.STREAM{emit,pseudoOp,defineLabel,annotation,...} = |
| 91 |
AsmStream.withStream stream Asm.makeStream annotations |
| 92 |
|
| 93 |
fun showFreq(ref w) = Fmt.format "[%f]" [Fmt.REAL w] |
| 94 |
fun showEdge(blknum,e) = |
| 95 |
Fmt.format "%d:%s" [Fmt.INT blknum, Fmt.STR(CFG.show_edge e)] |
| 96 |
fun showSucc(_, x, e) = showEdge(x,e) |
| 97 |
fun showPred(x, _, e) = showEdge(x,e) |
| 98 |
fun showSuccs b = |
| 99 |
(pr "\tsucc: "; |
| 100 |
prList (map showSucc (#out_edges cfg b)); |
| 101 |
pr "\n") |
| 102 |
fun showPreds b = |
| 103 |
(pr "\tpred: "; |
| 104 |
prList (map showPred (#in_edges cfg b)); |
| 105 |
pr "\n") |
| 106 |
|
| 107 |
fun printBlock(_, CFG.BLOCK{kind=CFG.START, id, freq, ...}) = |
| 108 |
(pr (Fmt.format "ENTRY %d %s\n" [Fmt.INT id, Fmt.STR(showFreq freq)]); |
| 109 |
showSuccs id) |
| 110 |
| printBlock(_, CFG.BLOCK{kind=CFG.STOP, id, freq, ...}) = |
| 111 |
(pr (Fmt.format "EXIT %d %s\n" [Fmt.INT id, Fmt.STR(showFreq freq)]); |
| 112 |
showPreds id) |
| 113 |
| printBlock(_, CFG.BLOCK{id, align, freq, insns, annotations, |
| 114 |
labels, ...}) = |
| 115 |
(pr (Fmt.format "BLOCK %d %s\n" [Fmt.INT id, Fmt.STR(showFreq freq)]); |
| 116 |
case !align of NONE => () | SOME p => (pr (CFG.P.toString p ^ "\n")); |
| 117 |
app annotation (!annotations); |
| 118 |
app defineLabel (!labels); |
| 119 |
(* |
| 120 |
pr ("\tlive in: " ^ CellsBasis.CellSet.toString (!liveIn) ^ "\n"); |
| 121 |
pr ("\tlive out: " ^ CellsBasis.CellSet.toString (!liveOut) ^ "\n"); |
| 122 |
*) |
| 123 |
showSuccs id; |
| 124 |
showPreds id; |
| 125 |
app emit (rev (!insns))) |
| 126 |
|
| 127 |
fun printData() = let |
| 128 |
val CFG.INFO{data, ...} = #graph_info cfg |
| 129 |
in List.app (pr o CFG.P.toString) (rev(!data)) |
| 130 |
end |
| 131 |
in |
| 132 |
pr(Fmt.format "[ %s ]\n" [Fmt.STR title]); |
| 133 |
app annotation annotations; |
| 134 |
(* printBlock entry; *) |
| 135 |
AsmStream.withStream stream (#forall_nodes cfg) printBlock; |
| 136 |
(* printBlock exit; *) |
| 137 |
AsmStream.withStream stream printData (); |
| 138 |
TextIO.flushOut stream |
| 139 |
end |
| 140 |
end |
| 141 |
|