| 1 |
(* vlBackPatch.sml -- variable length back patching. |
| 2 |
* |
| 3 |
* Copyright 1999 by Bell Laboratories |
| 4 |
*) |
| 5 |
|
| 6 |
(* NOTE on maxIter: |
| 7 |
* |
| 8 |
* maxIter -- is the number of times a span-dependent instruction |
| 9 |
* is allowed to expand in a non-monotonic way. |
| 10 |
* |
| 11 |
* This table shows the number of span-dependent instructions |
| 12 |
* whose size was over-estimated as a function of maxIter, for the |
| 13 |
* file Parse/parse/ml.grm.sml: |
| 14 |
* |
| 15 |
* maxIter # of instructions: |
| 16 |
* 10 687 |
| 17 |
* 20 438 |
| 18 |
* 30 198 |
| 19 |
* 40 0 |
| 20 |
* |
| 21 |
* In compiling the compiler, there is no significant difference in |
| 22 |
* compilation speed between maxIter=10 and maxIter=40. |
| 23 |
* Indeed 96% of the files in the compiler reach a fix point within |
| 24 |
* 13 iterations. |
| 25 |
*) |
| 26 |
functor BackPatch |
| 27 |
(structure CodeString : CODE_STRING |
| 28 |
structure Jumps : SDI_JUMPS |
| 29 |
structure Props : INSN_PROPERTIES (* where I = Jumps.I *) |
| 30 |
where type I.addressing_mode = Jumps.I.addressing_mode |
| 31 |
and type I.ea = Jumps.I.ea |
| 32 |
and type I.instr = Jumps.I.instr |
| 33 |
and type I.instruction = Jumps.I.instruction |
| 34 |
and type I.operand = Jumps.I.operand |
| 35 |
structure Emitter : MC_EMIT (* where I = Props.I *) |
| 36 |
where type I.addressing_mode = Props.I.addressing_mode |
| 37 |
and type I.ea = Props.I.ea |
| 38 |
and type I.instr = Props.I.instr |
| 39 |
and type I.instruction = Props.I.instruction |
| 40 |
and type I.operand = Props.I.operand |
| 41 |
structure CFG : CONTROL_FLOW_GRAPH (* where I = Emitter.I *) |
| 42 |
where type I.addressing_mode = Emitter.I.addressing_mode |
| 43 |
and type I.ea = Emitter.I.ea |
| 44 |
and type I.instr = Emitter.I.instr |
| 45 |
and type I.instruction = Emitter.I.instruction |
| 46 |
and type I.operand = Emitter.I.operand |
| 47 |
structure Asm : INSTRUCTION_EMITTER (* where I = CFG.I *) |
| 48 |
where type I.addressing_mode = CFG.I.addressing_mode |
| 49 |
and type I.ea = CFG.I.ea |
| 50 |
and type I.instr = CFG.I.instr |
| 51 |
and type I.instruction = CFG.I.instruction |
| 52 |
and type I.operand = CFG.I.operand |
| 53 |
) = |
| 54 |
struct |
| 55 |
structure I = Jumps.I |
| 56 |
structure C = I.C |
| 57 |
structure CFG = CFG |
| 58 |
structure P = CFG.P |
| 59 |
structure G = Graph |
| 60 |
structure W8V = Word8Vector |
| 61 |
|
| 62 |
datatype desc = |
| 63 |
BYTES of W8V.vector * desc |
| 64 |
| PSEUDO of P.pseudo_op * desc |
| 65 |
| SDI of I.instruction * int ref * desc |
| 66 |
| LABEL of Label.label * desc |
| 67 |
| NIL |
| 68 |
|
| 69 |
datatype cluster = CLUSTER of {cluster: desc} |
| 70 |
|
| 71 |
val maxIter = MLRiscControl.mkInt |
| 72 |
("variable-length-backpatch-iterations", |
| 73 |
"number of variable-length backpath iterations") |
| 74 |
|
| 75 |
val _ = maxIter := 40 |
| 76 |
|
| 77 |
fun error msg = MLRiscErrorMsg.error("vlBackPatch",msg) |
| 78 |
|
| 79 |
val clusterList : cluster list ref = ref [] |
| 80 |
val dataList : P.pseudo_op list ref = ref [] |
| 81 |
fun cleanUp() = (clusterList := []; dataList := []) |
| 82 |
|
| 83 |
fun bbsched(G.GRAPH{graph_info=CFG.INFO{data, ...}, ...}, blocks) = let |
| 84 |
fun bytes([], p) = p |
| 85 |
| bytes([s], p) = BYTES(s, p) |
| 86 |
| bytes(s, p) = BYTES(W8V.concat s, p) |
| 87 |
|
| 88 |
fun f((_, CFG.BLOCK{align, labels, insns, ...})::rest) = let |
| 89 |
fun instrs([], b) = bytes(rev b, f rest) |
| 90 |
| instrs(i::rest, b) = |
| 91 |
if Jumps.isSdi i then |
| 92 |
bytes(rev b, SDI(i, ref(Jumps.minSize i), instrs(rest, []))) |
| 93 |
else |
| 94 |
instrs(rest, Emitter.emitInstr(i)::b) |
| 95 |
fun doLabels(lab::rest) = LABEL(lab, doLabels rest) |
| 96 |
| doLabels [] = instrs(rev(!insns), []) |
| 97 |
fun alignIt(NONE) = doLabels(!labels) |
| 98 |
| alignIt(SOME p) = PSEUDO(p, alignIt(NONE)) |
| 99 |
in |
| 100 |
alignIt(!align) |
| 101 |
end |
| 102 |
| f [] = NIL |
| 103 |
in |
| 104 |
clusterList := |
| 105 |
CLUSTER{cluster=f blocks}:: !clusterList; |
| 106 |
dataList := !data @ !dataList |
| 107 |
end |
| 108 |
|
| 109 |
|
| 110 |
fun finish () = let |
| 111 |
val iter = ref 0 (* iteration count *) |
| 112 |
fun labels (BYTES(s,rest), pos, chgd) = labels(rest, pos+W8V.length s, chgd) |
| 113 |
| labels (SDI(instr, r as ref size, rest), pos, chgd) = |
| 114 |
let val s = Jumps.sdiSize(instr, Label.addrOf, pos) |
| 115 |
(* Allow contraction in the first two passes; |
| 116 |
* after which only allows expansion to ensure termination. |
| 117 |
*) |
| 118 |
in |
| 119 |
if (!iter <= !maxIter andalso s <> size) orelse s > size then |
| 120 |
(r := s; labels(rest, pos + s, true)) |
| 121 |
else labels(rest, pos + size, chgd) |
| 122 |
end |
| 123 |
| labels (LABEL(l,rest), pos, changed) = |
| 124 |
if Label.addrOf(l) = pos then labels(rest, pos, changed) |
| 125 |
else (Label.setAddr(l, pos); labels(rest, pos, true)) |
| 126 |
| labels (PSEUDO(pOp, rest), pos, chgd) = let |
| 127 |
val oldSz = P.sizeOf(pOp, pos) |
| 128 |
val newSz = (P.adjustLabels(pOp, pos); P.sizeOf(pOp, pos)) |
| 129 |
in labels(rest, pos + newSz, chgd orelse newSz<>oldSz) (* XXXX???? *) |
| 130 |
end |
| 131 |
| labels (NIL, pos, chgd) = (pos, chgd) |
| 132 |
|
| 133 |
fun clusterLabels clusters = let |
| 134 |
fun f (CLUSTER{cluster, ...}, (pos,chgd)) = labels(cluster, pos, chgd) |
| 135 |
in List.foldl f (0, false) clusters |
| 136 |
end |
| 137 |
|
| 138 |
val nop = Props.nop() |
| 139 |
|
| 140 |
val loc = ref 0 |
| 141 |
|
| 142 |
fun output v = |
| 143 |
W8V.app (fn v => (CodeString.update(!loc, v); loc:= !loc+1)) v |
| 144 |
|
| 145 |
|
| 146 |
fun chunk(pos, []) = () |
| 147 |
| chunk(pos, CLUSTER{cluster}::rest) = let |
| 148 |
fun outputInstr i = output (Emitter.emitInstr(nop)) |
| 149 |
fun nops 0 = () |
| 150 |
| nops n = (outputInstr(nop); nops(n-1)) |
| 151 |
fun f(pos, BYTES(s,r)) = (output s; f(pos+W8V.length s,r)) |
| 152 |
| f(pos, SDI(instr, ref size, r)) = let |
| 153 |
val emitInstrs = map (fn i => Emitter.emitInstr(i)) |
| 154 |
val instrs = emitInstrs (Jumps.expand(instr, size, pos)) |
| 155 |
val sum = List.foldl (fn (a,b) => (W8V.length a + b)) 0 |
| 156 |
val n = size - sum instrs |
| 157 |
in |
| 158 |
(* |
| 159 |
if n > 0 then |
| 160 |
(print ("\t\t\t Inserting " ^ Int.toString n ^ "nops\n"); |
| 161 |
emit instr) |
| 162 |
else (); |
| 163 |
*) |
| 164 |
app output instrs; |
| 165 |
nops(n); |
| 166 |
f(pos+size, r) |
| 167 |
end |
| 168 |
| f(pos, LABEL(lab, rest)) = |
| 169 |
if pos = Label.addrOf lab then f(pos,rest) |
| 170 |
else error "chunk: LABEL" |
| 171 |
| f(pos, PSEUDO(pOp, rest)) = let |
| 172 |
val s : Word8.word list ref = ref [] |
| 173 |
in |
| 174 |
P.emitValue{pOp=pOp, loc=pos, |
| 175 |
emit=(fn w => s := w :: (!s))}; |
| 176 |
output(W8V.fromList(rev(!s))); |
| 177 |
f(pos + P.sizeOf(pOp, pos), rest) |
| 178 |
end |
| 179 |
| f(pos, NIL) = chunk(pos, rest) |
| 180 |
|
| 181 |
in f(pos, cluster) |
| 182 |
end |
| 183 |
|
| 184 |
fun fix clusters = let |
| 185 |
val (pos, changed) = clusterLabels clusters |
| 186 |
in |
| 187 |
if changed then (iter := !iter + 1; fix clusters) else pos |
| 188 |
end |
| 189 |
|
| 190 |
(* |
| 191 |
* Initialize labels so that they have |
| 192 |
* some reasonable value to begin with. |
| 193 |
*) |
| 194 |
fun initLabels (clusters) = let |
| 195 |
fun init(BYTES(bytes, rest), loc) = init(rest, loc + W8V.length bytes) |
| 196 |
| init(PSEUDO(pOp, rest), loc) = |
| 197 |
(P.adjustLabels(pOp, loc); init(rest, loc + P.sizeOf(pOp, loc))) |
| 198 |
| init(SDI(sdi, size, rest), loc) = init(rest, loc + !size) |
| 199 |
| init(LABEL(lab, rest), loc) = |
| 200 |
(Label.setAddr(lab, loc); init(rest, loc)) |
| 201 |
| init(NIL, loc) = loc |
| 202 |
in |
| 203 |
List.foldl |
| 204 |
(fn (CLUSTER{cluster, ...}, loc) => init(cluster, loc)) 0 clusters |
| 205 |
end |
| 206 |
|
| 207 |
(* The dataList is in reverse order, and the entries in each |
| 208 |
* are also in reverse |
| 209 |
*) |
| 210 |
fun compUnit(d::dl, cl, acc) = compUnit(dl, cl, PSEUDO(d, acc)) |
| 211 |
| compUnit([], cl, acc) = let |
| 212 |
fun revCl(c::cl, acc) = revCl(cl, c::acc) |
| 213 |
| revCl([], acc) = acc |
| 214 |
in revCl(cl, [CLUSTER{cluster=acc}]) |
| 215 |
end |
| 216 |
|
| 217 |
val compressed = compUnit(!dataList, !clusterList, NIL) before cleanUp() |
| 218 |
in |
| 219 |
initLabels(compressed); |
| 220 |
CodeString.init(fix compressed); |
| 221 |
loc := 0; chunk(0, compressed) |
| 222 |
end (* finish *) |
| 223 |
|
| 224 |
end (* functor BackPatch *) |