| 1 |
(* |
| 2 |
* This module takes a stream of instructions and build a CFG. |
| 3 |
* The building can be incremental. |
| 4 |
* |
| 5 |
* -- Allen |
| 6 |
*) |
| 7 |
functor ControlFlowGraphGen |
| 8 |
(structure CFG : CONTROL_FLOW_GRAPH |
| 9 |
structure Stream : INSTRUCTION_STREAM |
| 10 |
structure InsnProps : INSN_PROPERTIES |
| 11 |
sharing CFG.I = InsnProps.I |
| 12 |
sharing CFG.P = Stream.P |
| 13 |
) : CONTROL_FLOW_GRAPH_GEN = |
| 14 |
struct |
| 15 |
|
| 16 |
structure CFG = CFG |
| 17 |
structure I = CFG.I |
| 18 |
structure P = CFG.P |
| 19 |
structure G = Graph |
| 20 |
structure W = CFG.W |
| 21 |
structure S = Stream |
| 22 |
|
| 23 |
fun error msg = MLRiscErrorMsg.error("ControlFlowGraphGen",msg) |
| 24 |
|
| 25 |
fun builder(CFG) = |
| 26 |
let val NOBLOCK = CFG.newBlock(~1,ref 0) |
| 27 |
val currentBlock = ref NOBLOCK |
| 28 |
val newBlocks = ref [] : CFG.block list ref |
| 29 |
val blockNames = ref [] : Annotations.annotations ref |
| 30 |
val entryLabels = ref [] : Label.label list ref |
| 31 |
fun can'tUse _ = error "unimplemented" |
| 32 |
exception NotFound |
| 33 |
val labelMap = IntHashTable.mkTable(32,NotFound) |
| 34 |
val newLabel = IntHashTable.insert labelMap |
| 35 |
val lookupLabel = IntHashTable.lookup labelMap |
| 36 |
val CFG = ref CFG |
| 37 |
|
| 38 |
(* Initialization *) |
| 39 |
fun init _ = |
| 40 |
let val G.GRAPH cfg = !CFG |
| 41 |
in IntHashTable.clear labelMap; |
| 42 |
#forall_nodes cfg |
| 43 |
(fn (blockId,CFG.BLOCK{labels, ...}) => |
| 44 |
app (fn Label.Label{id, ...} => newLabel(id,blockId)) |
| 45 |
(!labels)); |
| 46 |
currentBlock := NOBLOCK; |
| 47 |
newBlocks := []; |
| 48 |
blockNames := []; |
| 49 |
entryLabels := [] |
| 50 |
end |
| 51 |
|
| 52 |
val _ = init() |
| 53 |
|
| 54 |
fun next cfg = CFG := cfg |
| 55 |
|
| 56 |
fun newBlock() = |
| 57 |
let val G.GRAPH cfg = !CFG |
| 58 |
val id = #new_id cfg () |
| 59 |
val b as CFG.BLOCK{annotations,...} = CFG.newBlock(id,ref 0) |
| 60 |
in currentBlock := b; |
| 61 |
annotations := !blockNames; |
| 62 |
newBlocks := b :: !newBlocks; |
| 63 |
#add_node cfg (id,b); |
| 64 |
b |
| 65 |
end |
| 66 |
|
| 67 |
fun getBlock() = |
| 68 |
case !currentBlock of |
| 69 |
CFG.BLOCK{id= ~1,...} => newBlock() |
| 70 |
| b => b |
| 71 |
|
| 72 |
fun newPseudoOpBlock() = |
| 73 |
(case !currentBlock of |
| 74 |
CFG.BLOCK{id= ~1,...} => newBlock() |
| 75 |
| b as CFG.BLOCK{insns=ref [],...} => b |
| 76 |
| _ => newBlock() |
| 77 |
) |
| 78 |
|
| 79 |
fun insertOp p = |
| 80 |
let val CFG.BLOCK{data,...} = newPseudoOpBlock() |
| 81 |
in data := !data @ [p] end |
| 82 |
|
| 83 |
(* Add a new label *) |
| 84 |
fun defineLabel(l as Label.Label{id=labelId,...}) = |
| 85 |
let val id = lookupLabel labelId |
| 86 |
val G.GRAPH cfg = !CFG |
| 87 |
val blk as CFG.BLOCK{insns, ...} = #node_info cfg id |
| 88 |
in currentBlock := blk; |
| 89 |
newBlocks := blk :: !newBlocks; |
| 90 |
insns := []; (* clear instructions *) |
| 91 |
#set_out_edges cfg (id,[]) (* clear edges *) |
| 92 |
end handle _ => |
| 93 |
let val CFG.BLOCK{id,labels,...} = newPseudoOpBlock() |
| 94 |
in labels := l :: !labels; |
| 95 |
newLabel(labelId, id) |
| 96 |
end |
| 97 |
|
| 98 |
(* Add a new entry label *) |
| 99 |
fun entryLabel l = (defineLabel l; entryLabels := l :: !entryLabels) |
| 100 |
|
| 101 |
(* Add a new pseudo op *) |
| 102 |
fun pseudoOp p = insertOp(CFG.PSEUDO p) |
| 103 |
|
| 104 |
fun nextBlock() = |
| 105 |
case !currentBlock of |
| 106 |
CFG.BLOCK{id= ~1,...} => () |
| 107 |
| b => currentBlock := NOBLOCK |
| 108 |
|
| 109 |
(* Add a new annotation *) |
| 110 |
fun annotation a = |
| 111 |
case a of |
| 112 |
MLRiscAnnotations.BLOCKNAMES names => |
| 113 |
(blockNames := names; |
| 114 |
nextBlock() |
| 115 |
) |
| 116 |
| MLRiscAnnotations.EMPTYBLOCK => nextBlock() |
| 117 |
| a => |
| 118 |
let val CFG.BLOCK{annotations,...} = getBlock() |
| 119 |
in annotations := a :: !annotations |
| 120 |
end |
| 121 |
|
| 122 |
(* Mark current block as exit *) |
| 123 |
fun exitBlock liveOut = |
| 124 |
let fun setLiveOut(CFG.BLOCK{annotations,...}) = |
| 125 |
annotations := #create CFG.LIVEOUT liveOut :: !annotations |
| 126 |
in case !currentBlock of |
| 127 |
CFG.BLOCK{id= ~1,...} => |
| 128 |
(case !newBlocks of |
| 129 |
[] => error "exitBlock" |
| 130 |
| b::_ => setLiveOut b |
| 131 |
) |
| 132 |
| b => setLiveOut b |
| 133 |
end |
| 134 |
|
| 135 |
(* Add a new comment *) |
| 136 |
fun comment msg = annotation(#create MLRiscAnnotations.COMMENT msg) |
| 137 |
|
| 138 |
(* Emit an instruction *) |
| 139 |
fun emit i = |
| 140 |
let val CFG.BLOCK{insns,...} = getBlock() |
| 141 |
in insns := i :: !insns; |
| 142 |
case InsnProps.instrKind i of |
| 143 |
(InsnProps.IK_JUMP | InsnProps.IK_CALL_WITH_CUTS) => |
| 144 |
currentBlock := NOBLOCK |
| 145 |
| _ => () |
| 146 |
end |
| 147 |
|
| 148 |
(* End current cluster *) |
| 149 |
fun endCluster(annotations) = |
| 150 |
let val G.GRAPH cfg = !CFG |
| 151 |
val _ = CFG.init(!CFG) (* create entry/exit *) |
| 152 |
|
| 153 |
val ENTRY = hd(#entries cfg ()) |
| 154 |
val EXIT = hd(#exits cfg ()) |
| 155 |
|
| 156 |
fun next(CFG.BLOCK{id,data=ref [],...}::_) = id |
| 157 |
| next _ = error "next" |
| 158 |
|
| 159 |
val lookupLabelMap = IntHashTable.find labelMap |
| 160 |
val lookupLabelMap = |
| 161 |
fn l => case lookupLabelMap l of SOME b => b | NONE => EXIT |
| 162 |
val TRUE = CFG.BRANCH true |
| 163 |
val FALSE = CFG.BRANCH false |
| 164 |
val addEdge = #add_edge cfg |
| 165 |
|
| 166 |
fun target(Label.Label{id,...}) = lookupLabelMap id |
| 167 |
|
| 168 |
fun addEdges [] = () |
| 169 |
| addEdges(CFG.BLOCK{id,insns,...}::blocks) = |
| 170 |
(case !insns of |
| 171 |
[] => fallsThru(id,blocks) |
| 172 |
| instr::_ => |
| 173 |
(case InsnProps.instrKind instr of |
| 174 |
(InsnProps.IK_JUMP | InsnProps.IK_CALL_WITH_CUTS) => |
| 175 |
jump(id,InsnProps.branchTargets instr,blocks) |
| 176 |
| _ => fallsThru(id,blocks) |
| 177 |
); |
| 178 |
addEdges blocks |
| 179 |
) |
| 180 |
and fallsThru(i,CFG.BLOCK{id=j,data,...}::_) = |
| 181 |
(case !data of |
| 182 |
[] => () |
| 183 |
| _ => error("falls thru into pseudo ops: "^ |
| 184 |
Int.toString i^" -> "^Int.toString j) |
| 185 |
; |
| 186 |
addEdge(i,j,CFG.EDGE{k=CFG.FALLSTHRU,w=ref 0, a=ref []}) |
| 187 |
) |
| 188 |
| fallsThru(i,[]) = |
| 189 |
(* error("missing return in block "^Int.toString i) *) |
| 190 |
addEdge(i,EXIT,CFG.EDGE{k=CFG.EXIT,w=ref 0,a=ref []}) |
| 191 |
and jump(i,[InsnProps.ESCAPES],_) = |
| 192 |
addEdge(i,EXIT,CFG.EDGE{k=CFG.EXIT,w=ref 0,a=ref []}) |
| 193 |
| jump(i,[InsnProps.LABELLED L],_) = |
| 194 |
addEdge(i,target L,CFG.EDGE{k=CFG.JUMP,w=ref 0,a=ref []}) |
| 195 |
| jump(i,[InsnProps.LABELLED L,InsnProps.FALLTHROUGH],bs) = |
| 196 |
(addEdge(i,target L,CFG.EDGE{k=TRUE,w=ref 0,a=ref[]}); |
| 197 |
addEdge(i,next bs,CFG.EDGE{k=FALSE,w=ref 0,a=ref []}) |
| 198 |
) |
| 199 |
| jump(i,[InsnProps.FALLTHROUGH,InsnProps.LABELLED L],bs) = |
| 200 |
(addEdge(i,target L,CFG.EDGE{k=TRUE,w=ref 0,a=ref []}); |
| 201 |
addEdge(i,next bs,CFG.EDGE{k=FALSE,w=ref 0,a=ref []}) |
| 202 |
) |
| 203 |
| jump(i,targets,_) = |
| 204 |
let fun loop(n,[]) = () |
| 205 |
| loop(n,InsnProps.LABELLED L::targets) = |
| 206 |
(addEdge(i,target L, |
| 207 |
CFG.EDGE{k=CFG.SWITCH n,w=ref 0,a=ref []}); |
| 208 |
loop(n+1,targets)) |
| 209 |
| loop _ = error "jump" |
| 210 |
in loop(0,targets) end |
| 211 |
in addEdges(rev(!newBlocks)); |
| 212 |
app (fn l => addEdge(ENTRY,target l, |
| 213 |
CFG.EDGE{k=CFG.ENTRY,a=ref [],w=ref 0})) |
| 214 |
(!entryLabels); |
| 215 |
let val an = CFG.annotations(!CFG); |
| 216 |
in an := annotations @ (!an) end; |
| 217 |
init() |
| 218 |
end |
| 219 |
|
| 220 |
(* Start a new cluster *) |
| 221 |
fun beginCluster _ = init() |
| 222 |
|
| 223 |
fun getAnnotations() = CFG.annotations(!CFG) |
| 224 |
|
| 225 |
in {stream=S.STREAM |
| 226 |
{ beginCluster = beginCluster, |
| 227 |
endCluster = endCluster, |
| 228 |
defineLabel = defineLabel, |
| 229 |
entryLabel = entryLabel, |
| 230 |
pseudoOp = pseudoOp, |
| 231 |
emit = emit, |
| 232 |
exitBlock = exitBlock, |
| 233 |
comment = comment, |
| 234 |
annotation = annotation, |
| 235 |
getAnnotations= getAnnotations |
| 236 |
}, |
| 237 |
next = next |
| 238 |
} |
| 239 |
end |
| 240 |
|
| 241 |
end |