| 1 |
(* check-placement-fn.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2002 Bell Labs, Lucent Technologies |
| 4 |
* |
| 5 |
* This functor implements code to check that a block placement is |
| 6 |
* correct. |
| 7 |
*) |
| 8 |
|
| 9 |
functor CheckPlacementFn ( |
| 10 |
|
| 11 |
structure CFG : CONTROL_FLOW_GRAPH |
| 12 |
structure InsnProps : INSN_PROPERTIES (* where I = CFG.I *) |
| 13 |
where type I.addressing_mode = CFG.I.addressing_mode |
| 14 |
and type I.ea = CFG.I.ea |
| 15 |
and type I.instr = CFG.I.instr |
| 16 |
and type I.instruction = CFG.I.instruction |
| 17 |
and type I.operand = CFG.I.operand |
| 18 |
|
| 19 |
) : sig |
| 20 |
|
| 21 |
structure CFG : CONTROL_FLOW_GRAPH |
| 22 |
|
| 23 |
val check : (CFG.cfg * CFG.node list) -> unit |
| 24 |
|
| 25 |
end = struct |
| 26 |
|
| 27 |
structure CFG=CFG |
| 28 |
structure IP = InsnProps |
| 29 |
structure G = Graph |
| 30 |
|
| 31 |
val dumpStrm = MLRiscControl.debug_stream |
| 32 |
|
| 33 |
fun blockToString (id', CFG.BLOCK{id, ...}) = |
| 34 |
concat["<", Int.toString id', ":", Int.toString id, ">"] |
| 35 |
|
| 36 |
fun check (cfg as G.GRAPH graph, blocks) = let |
| 37 |
(* an array that maps from block id to position in the placement (starting |
| 38 |
* from 1). Nodes that have no placement have index ~1. |
| 39 |
*) |
| 40 |
val order = let |
| 41 |
val arr = Array.array(#capacity graph (), ~1) |
| 42 |
fun init ((id, _), i) = (Array.update(arr, id, i); i+1) |
| 43 |
in |
| 44 |
ignore (List.foldl init 1 blocks); |
| 45 |
arr |
| 46 |
end |
| 47 |
fun adjacentNodes (a, b) = (Array.sub(order, a) + 1 = Array.sub(order, b)) |
| 48 |
val anyErrors = ref false |
| 49 |
(* report an error and dump the cfg *) |
| 50 |
fun reportError msg = let |
| 51 |
fun say s = TextIO.output(!dumpStrm, s) |
| 52 |
in |
| 53 |
if !anyErrors |
| 54 |
then () |
| 55 |
else ( |
| 56 |
anyErrors := true; |
| 57 |
say "********** Bogus block placement **********\n"); |
| 58 |
say(concat("** "::msg)) |
| 59 |
end |
| 60 |
fun reportNotAdjacent (src, dst) = let |
| 61 |
fun b2s id = concat[ |
| 62 |
Int.toString id, "@", Int.toString(Array.sub(order, id)) |
| 63 |
] |
| 64 |
in |
| 65 |
reportError [ |
| 66 |
"Blocks ", b2s src, " and ", b2s dst, |
| 67 |
" are not adjacent\n" |
| 68 |
] |
| 69 |
end |
| 70 |
(* return true if the edge must connect adjacent nodes *) |
| 71 |
fun adjEdge (CFG.EDGE{k=CFG.FALLSTHRU, ...}) = true |
| 72 |
| adjEdge (CFG.EDGE{k=CFG.BRANCH false, ...}) = true |
| 73 |
| adjEdge _ = false |
| 74 |
(* entry and exit nodes *) |
| 75 |
val entryId = CFG.entryId cfg |
| 76 |
val exitId = CFG.exitId cfg |
| 77 |
(* get the jump targets from the last instruction in a block *) |
| 78 |
fun getJumpTargets id = (case #node_info graph id |
| 79 |
of CFG.BLOCK{insns=ref(i::_), ...} => ( |
| 80 |
case IP.instrKind i |
| 81 |
of IP.IK_JUMP => IP.branchTargets i |
| 82 |
| _ => [] |
| 83 |
(* end case *)) |
| 84 |
| _ => [] |
| 85 |
(* end case *)) |
| 86 |
(* check that FALLSTHRU and BRANCH false edges connect adjacent nodes *) |
| 87 |
fun chkEdge (src, dst, CFG.EDGE{k, ...}) = (case k |
| 88 |
of (CFG.FALLSTHRU | CFG.BRANCH false) => |
| 89 |
if adjacentNodes(src, dst) |
| 90 |
then () |
| 91 |
else reportNotAdjacent(src, dst) |
| 92 |
| CFG.BRANCH true => (case getJumpTargets src |
| 93 |
of [IP.FALLTHROUGH, IP.LABELLED _] => () |
| 94 |
| [IP.LABELLED _, IP.FALLTHROUGH] => () |
| 95 |
| _ => reportError[ |
| 96 |
"Block ", Int.toString src, |
| 97 |
" doesn't end in conditiona branch\n" |
| 98 |
] |
| 99 |
(* end case *)) |
| 100 |
| CFG.JUMP => (case getJumpTargets src |
| 101 |
of [IP.LABELLED _] => () |
| 102 |
| _ => reportError[ |
| 103 |
"Block ", Int.toString src, " doesn't end in jump\n" |
| 104 |
] |
| 105 |
(* end case *)) |
| 106 |
| CFG.ENTRY => if (src <> entryId) |
| 107 |
then reportError[ |
| 108 |
"Block ", Int.toString src, " is not ENTRY\n" |
| 109 |
] |
| 110 |
else () |
| 111 |
| CFG.EXIT => if (dst <> exitId) |
| 112 |
then reportError[ |
| 113 |
"Block ", Int.toString dst, " is not EXIT\n" |
| 114 |
] |
| 115 |
else (case getJumpTargets src |
| 116 |
of [IP.ESCAPES] => () |
| 117 |
| _ => reportError [ |
| 118 |
"Block ", Int.toString src, |
| 119 |
"doesn't end in an escaping jump\n" |
| 120 |
] |
| 121 |
(* end case *)) |
| 122 |
| _ => () (* no checking for SWITCH or FLOWSTO *) |
| 123 |
(* end case *)) |
| 124 |
in |
| 125 |
#forall_edges graph chkEdge; |
| 126 |
if (!anyErrors) |
| 127 |
then let |
| 128 |
fun say s = TextIO.output(!dumpStrm, s) |
| 129 |
val prBlock = CFG.dumpBlock (!dumpStrm, cfg) |
| 130 |
in |
| 131 |
say "Block placement order:\n"; |
| 132 |
List.app |
| 133 |
(fn b => say(concat[" ", blockToString b, "\n"])) |
| 134 |
blocks; |
| 135 |
TextIO.output(!dumpStrm, "[ control-flow-graph ]\n"); |
| 136 |
List.app prBlock blocks; |
| 137 |
say "**********\n"; |
| 138 |
MLRiscErrorMsg.error ("CheckPlacementFn", "bogus placement") |
| 139 |
end |
| 140 |
else () |
| 141 |
end |
| 142 |
|
| 143 |
end |