| 1 |
(* mltree-gen.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2002 Bell Labs, Lucent Technologies |
| 4 |
* |
| 5 |
* This is a generic module for transforming MLTREE expressions: |
| 6 |
* (1) expressions involving non-standard type widths are promoted when |
| 7 |
* necessary. |
| 8 |
* (2) operators that cannot be directly handled are expanded into |
| 9 |
* more complex instruction sequences when necessary. |
| 10 |
* |
| 11 |
* -- Allen |
| 12 |
*) |
| 13 |
|
| 14 |
functor MLTreeGen ( |
| 15 |
structure T : MLTREE |
| 16 |
structure Cells : CELLS |
| 17 |
val intTy : T.ty (* size of integer word *) |
| 18 |
|
| 19 |
(* This is a list of possible data widths to promote to. |
| 20 |
* The list must be in increasing sizes. |
| 21 |
* We'll try to promote to the next largest size. |
| 22 |
*) |
| 23 |
val naturalWidths : T.ty list |
| 24 |
|
| 25 |
(* |
| 26 |
* Are integers of widths less than the size of integer word. |
| 27 |
* automatically sign extended, zero extended, or neither. |
| 28 |
* When in doubt, choose neither since it is conservative. |
| 29 |
*) |
| 30 |
datatype rep = SE | ZE | NEITHER |
| 31 |
val rep : rep |
| 32 |
|
| 33 |
) : MLTREEGEN = struct |
| 34 |
|
| 35 |
structure T = T |
| 36 |
structure Size = MLTreeSize(structure T = T val intTy = intTy) |
| 37 |
structure C = CellsBasis |
| 38 |
|
| 39 |
fun error msg = MLRiscErrorMsg.error("MLTreeGen",msg) |
| 40 |
fun unsupported what = error ("unsupported: " ^ what) |
| 41 |
|
| 42 |
val zeroT = T.LI 0 |
| 43 |
fun LI i = T.LI(T.I.fromInt(intTy, i)) |
| 44 |
|
| 45 |
fun condOf(T.CC(cc,_)) = cc |
| 46 |
| condOf(T.CMP(_,cc,_,_)) = cc |
| 47 |
| condOf(T.CCMARK(cc,_)) = condOf cc |
| 48 |
| condOf _ = error "condOf" |
| 49 |
|
| 50 |
fun fcondOf(T.FCC(fcc,_)) = fcc |
| 51 |
| fcondOf(T.FCMP(_,fcc,_,_)) = fcc |
| 52 |
| fcondOf(T.CCMARK(cc,_)) = fcondOf cc |
| 53 |
| fcondOf _ = error "fcondOf" |
| 54 |
|
| 55 |
val W = intTy |
| 56 |
|
| 57 |
(* To compute f.ty(a,b) |
| 58 |
* |
| 59 |
* let r1 <- a << (intTy - ty) |
| 60 |
* r2 <- b << (intTy - ty) |
| 61 |
* r3 <- f(a,b) |
| 62 |
* in r3 ~>> (intTy - ty) end |
| 63 |
* |
| 64 |
* Lal showed me this neat trick! |
| 65 |
*) |
| 66 |
fun arith(rightShift,f,ty,a,b) = |
| 67 |
let val shift = LI(W-ty) |
| 68 |
in rightShift(W,f(W,T.SLL(W,a,shift),T.SLL(W,b,shift)),shift) |
| 69 |
end |
| 70 |
|
| 71 |
fun promoteTy(ty) = |
| 72 |
let fun loop([]) = |
| 73 |
unsupported("can't promote integer width "^Int.toString ty) |
| 74 |
| loop(t::ts) = if t > ty then t else loop ts |
| 75 |
in loop(naturalWidths) end |
| 76 |
|
| 77 |
fun promotable rightShift (e, f, ty, a, b) = |
| 78 |
case naturalWidths of |
| 79 |
[] => arith(rightShift,f,ty,a,b) |
| 80 |
| _ => f(promoteTy(ty), a, b) |
| 81 |
|
| 82 |
fun isNatural w = let |
| 83 |
fun loop [] = false |
| 84 |
| loop (h :: t) = h = w orelse w > h andalso loop t |
| 85 |
in |
| 86 |
loop naturalWidths |
| 87 |
end |
| 88 |
|
| 89 |
(* Implement division with round-to-negative-infinity in terms |
| 90 |
* of division with round-to-zero. |
| 91 |
* The logic is as follows: |
| 92 |
* - if q > 0, then we are done since any rounding was |
| 93 |
* at the same time TO_ZERO and TO_NEGINF |
| 94 |
* (This is the fast path that does not require calculating the remainder.) |
| 95 |
* - otherwise we calculate r and see if it is zero; if so, no adjustment |
| 96 |
* - finally if r and b have the same sign (i.e., r XOR b >= 0) |
| 97 |
* we still don't need adjustment |
| 98 |
* - otherwise adjust |
| 99 |
* |
| 100 |
* Instruction selection for machines (e.g., x86) where the hardware returns both |
| 101 |
* q and r anyway should implement this logic directly. |
| 102 |
*) |
| 103 |
fun divinf (xdiv, ty, aexp, bexp) = let |
| 104 |
val a = Cells.newReg () |
| 105 |
val b = Cells.newReg () |
| 106 |
val q = Cells.newReg () |
| 107 |
val r = Cells.newReg () |
| 108 |
val zero = T.LI 0 |
| 109 |
val one = T.LI 1 |
| 110 |
in |
| 111 |
T.LET |
| 112 |
(T.SEQ |
| 113 |
[T.MV (ty, a, aexp), |
| 114 |
T.MV (ty, b, bexp), |
| 115 |
T.MV (ty, q, xdiv (T.DIV_TO_ZERO, ty, T.REG (ty, a), T.REG (ty, b))), |
| 116 |
T.IF (T.CMP (ty, T.Basis.GT, T.REG (ty, q), zero), |
| 117 |
T.SEQ [], |
| 118 |
T.SEQ |
| 119 |
[T.MV (ty, r, T.SUB (ty, T.REG (ty, a), |
| 120 |
T.MULS (ty, T.REG (ty, b), |
| 121 |
T.REG (ty, q)))), |
| 122 |
T.IF (T.CMP (ty, T.Basis.EQ, T.REG (ty, r), zero), |
| 123 |
T.SEQ [], |
| 124 |
T.IF (T.CMP (ty, T.Basis.GE, |
| 125 |
T.XORB (ty, T.REG (ty, b), T.REG (ty, r)), |
| 126 |
zero), |
| 127 |
T.SEQ [], |
| 128 |
T.MV (ty, q, T.SUB (ty, T.REG (ty, q), |
| 129 |
one))))])], |
| 130 |
T.REG (ty, q)) |
| 131 |
end |
| 132 |
|
| 133 |
(* Same for rem when rounding to negative infinity. |
| 134 |
* Since we have to return (and therefore calculate) the remainder anyway, |
| 135 |
* we can skip the q > 0 test because it will be caught by the samesign(r,b) |
| 136 |
* test. |
| 137 |
* |
| 138 |
* The odd case is when a = MININT and b = -1 in which case the DIVS op |
| 139 |
* will overflow and trap on some machines. On others the result |
| 140 |
* will be bogus. Should we fix that? *) |
| 141 |
fun reminf (ty, aexp, bexp) = let |
| 142 |
val a = Cells.newReg () |
| 143 |
val b = Cells.newReg () |
| 144 |
val q = Cells.newReg () |
| 145 |
val r = Cells.newReg () |
| 146 |
val zero = T.LI 0 |
| 147 |
in |
| 148 |
T.LET |
| 149 |
(T.SEQ |
| 150 |
[T.MV (ty, a, aexp), |
| 151 |
T.MV (ty, b, bexp), |
| 152 |
T.MV (ty, q, T.DIVS (T.DIV_TO_ZERO, ty, T.REG (ty, a), |
| 153 |
T.REG (ty, b))), |
| 154 |
T.MV (ty, r, T.SUB (ty, T.REG (ty, a), |
| 155 |
T.MULS (ty, T.REG (ty, q), |
| 156 |
T.REG (ty, b)))), |
| 157 |
T.IF (T.CMP (ty, T.Basis.EQ, T.REG (ty, r), zero), |
| 158 |
T.SEQ [], |
| 159 |
T.IF (T.CMP (ty, T.Basis.GE, |
| 160 |
T.XORB (ty, T.REG (ty, b), T.REG (ty, r)), |
| 161 |
zero), |
| 162 |
T.SEQ [], |
| 163 |
T.MV (ty, r, T.ADD (ty, T.REG (ty, r), T.REG (ty, b)))))], |
| 164 |
T.REG (ty, r)) |
| 165 |
end |
| 166 |
|
| 167 |
(* Same for rem when rounding to zero. *) |
| 168 |
fun remzero (xdiv, xmul, ty, aexp, bexp) = let |
| 169 |
val a = Cells.newReg () |
| 170 |
val b = Cells.newReg () |
| 171 |
in |
| 172 |
T.LET (T.SEQ [T.MV (ty, a, aexp), |
| 173 |
T.MV (ty, b, bexp)], |
| 174 |
T.SUB (ty, T.REG (ty, a), |
| 175 |
xmul (ty, T.REG (ty, b), |
| 176 |
xdiv (T.DIV_TO_ZERO, ty, T.REG (ty, a), |
| 177 |
T.REG (ty, b))))) |
| 178 |
end |
| 179 |
|
| 180 |
(* |
| 181 |
* Translate integer expressions of unknown types into the appropriate |
| 182 |
* term. |
| 183 |
*) |
| 184 |
|
| 185 |
fun DIVREMz d (ty, a, b) = d (T.DIV_TO_ZERO, ty, a, b) |
| 186 |
|
| 187 |
fun compileRexp(exp) = |
| 188 |
case exp of |
| 189 |
T.CONST c => T.LABEXP exp |
| 190 |
|
| 191 |
(* non overflow trapping ops *) |
| 192 |
| T.NEG(ty,a) => T.SUB(ty, zeroT, a) |
| 193 |
| T.ADD(ty,a,b) => promotable T.SRA (exp,T.ADD,ty,a,b) |
| 194 |
| T.SUB(ty,a,b) => promotable T.SRA (exp,T.SUB,ty,a,b) |
| 195 |
| T.MULS(ty,a,b) => promotable T.SRA (exp,T.MULS,ty,a,b) |
| 196 |
| T.DIVS(T.DIV_TO_ZERO,ty,a,b) => |
| 197 |
promotable T.SRA (exp,DIVREMz T.DIVS,ty,a,b) |
| 198 |
| T.DIVS(T.DIV_TO_NEGINF,ty,a,b) => divinf (T.DIVS,ty,a,b) |
| 199 |
| T.REMS(T.DIV_TO_ZERO,ty,a,b) => |
| 200 |
if isNatural ty then remzero (T.DIVS,T.MULS,ty,a,b) |
| 201 |
else promotable T.SRA (exp,DIVREMz T.REMS,ty,a,b) |
| 202 |
| T.REMS(T.DIV_TO_NEGINF,ty,a,b) => reminf (ty,a,b) |
| 203 |
| T.MULU(ty,a,b) => promotable T.SRL (exp,T.MULU,ty,a,b) |
| 204 |
| T.DIVU(ty,a,b) => promotable T.SRL (exp,T.DIVU,ty,a,b) |
| 205 |
| T.REMU(ty,a,b) => |
| 206 |
if isNatural ty then |
| 207 |
remzero (fn (_,ty,a,b) => T.DIVU (ty,a,b),T.MULU,ty,a,b) |
| 208 |
else promotable T.SRL (exp,T.REMU,ty,a,b) |
| 209 |
|
| 210 |
(* for overflow trapping ops; we have to do the simulation *) |
| 211 |
| T.NEGT(ty,a) => T.SUBT(ty,zeroT,a) |
| 212 |
| T.ADDT(ty,a,b) => arith (T.SRA,T.ADDT,ty,a,b) |
| 213 |
| T.SUBT(ty,a,b) => arith (T.SRA,T.SUBT,ty,a,b) |
| 214 |
| T.MULT(ty,a,b) => arith (T.SRA,T.MULT,ty,a,b) |
| 215 |
| T.DIVT(T.DIV_TO_ZERO,ty,a,b) => arith (T.SRA,DIVREMz T.DIVT,ty,a,b) |
| 216 |
| T.DIVT(T.DIV_TO_NEGINF,ty,a,b) => divinf (T.DIVT,ty,a,b) |
| 217 |
|
| 218 |
(* conditional evaluation rules *) |
| 219 |
(*** XXX: Seems wrong. |
| 220 |
| T.COND(ty,T.CC(cond,r),x,y) => |
| 221 |
T.COND(ty,T.CMP(ty,cond,T.REG(ty,r),zeroT),x,y) |
| 222 |
***) |
| 223 |
| T.COND(ty,T.CCMARK(cc,a),x,y) => T.MARK(T.COND(ty,cc,x,y),a) |
| 224 |
(*** XXX: TODO |
| 225 |
| T.COND(ty,T.CMP(t,cc,e1,e2),x as (T.LI 0 | T.LI32 0w0),y) => |
| 226 |
T.COND(ty,T.CMP(t,T.Basis.negateCond cc,e1,e2),y,T.LI 0) |
| 227 |
(* we'll let others strength reduce the multiply *) |
| 228 |
***) |
| 229 |
| T.COND(ty,cc as T.FCMP _, yes, no) => let |
| 230 |
val tmp = Cells.newReg() |
| 231 |
in |
| 232 |
T.LET( |
| 233 |
T.SEQ[T.MV(ty, tmp, no), T.IF(cc, T.MV(ty, tmp, yes), T.SEQ [])], |
| 234 |
T.REG(ty,tmp)) |
| 235 |
end |
| 236 |
(*** XXX: TODO |
| 237 |
| T.COND(ty,cc,e1,(T.LI 0 | T.LI32 0w0)) => |
| 238 |
T.MULU(ty,T.COND(ty,cc,T.LI 1,T.LI 0),e1) |
| 239 |
| T.COND(ty,cc,T.LI m,T.LI n) => |
| 240 |
T.ADD(ty,T.MULU(ty,T.COND(ty,cc,T.LI 1,T.LI 0),T.LI(m-n)),T.LI n) |
| 241 |
***) |
| 242 |
|
| 243 |
| T.COND(ty,cc,e1,e2) => |
| 244 |
T.ADD(ty,T.MULU(ty,T.COND(ty,cc,T.LI 1,zeroT),T.SUB(ty,e1,e2)),e2) |
| 245 |
|
| 246 |
(* ones-complement. |
| 247 |
* WARNING: we are assuming two's complement architectures here. |
| 248 |
* Are there any architectures in use nowadays that doesn't use |
| 249 |
* two's complement for integer arithmetic? |
| 250 |
*) |
| 251 |
| T.NOTB(ty,e) => T.XORB(ty,e,T.LI ~1) |
| 252 |
|
| 253 |
(* |
| 254 |
* Default ways of converting integers to integers |
| 255 |
*) |
| 256 |
| T.SX(ty,fromTy,e) => |
| 257 |
if fromTy = ty then e |
| 258 |
else if rep = SE andalso fromTy < ty andalso |
| 259 |
fromTy >= hd naturalWidths then e |
| 260 |
else |
| 261 |
let val shift = T.LI(T.I.fromInt(intTy, W - fromTy)) |
| 262 |
in T.SRA(W,T.SLL(W,e,shift),shift) |
| 263 |
end |
| 264 |
| T.ZX(toTy, fromTy, e) => let |
| 265 |
(* NOTE: we cannot assume that the high bits of the source are zero! *) |
| 266 |
fun mask 8 = T.ANDB(toTy, e, T.LI 0xff) |
| 267 |
| mask 16 = T.ANDB(toTy,e,T.LI 0xffff) |
| 268 |
| mask 32 = T.ANDB(toTy,e,T.LI 0xffffffff) |
| 269 |
| mask 64 = e |
| 270 |
| mask _ = unsupported "unknown expression" |
| 271 |
in |
| 272 |
if (fromTy = toTy) then e |
| 273 |
else if (fromTy < toTy) then mask fromTy |
| 274 |
else mask toTy |
| 275 |
end |
| 276 |
|
| 277 |
(* |
| 278 |
* Converting floating point to integers. |
| 279 |
* The following rule handles the case when ty is not |
| 280 |
* one of the naturally supported widths on the machine. |
| 281 |
*) |
| 282 |
| T.CVTF2I(ty,round,fty,e) => |
| 283 |
let val ty' = promoteTy(ty) |
| 284 |
in T.SX(ty,ty',T.CVTF2I(ty',round,fty,e)) |
| 285 |
end |
| 286 |
|
| 287 |
(* Promote to higher width and zero high bits *) |
| 288 |
| T.SLL(ty, data, shift) => |
| 289 |
let val ty' = promoteTy(ty) |
| 290 |
in T.ZX(ty, ty', T.SLL(ty', data, shift)) end |
| 291 |
|
| 292 |
| exp => unsupported("unknown expression") |
| 293 |
|
| 294 |
fun compileFexp fexp = unsupported("unknown expression") |
| 295 |
|
| 296 |
fun mark(s,[]) = s |
| 297 |
| mark(s,a::an) = mark(T.ANNOTATION(s,a),an) |
| 298 |
|
| 299 |
fun compileStm (T.SEQ s) = s |
| 300 |
| compileStm (T.IF(cond,T.JMP(T.LABEL L,_),T.SEQ [])) = |
| 301 |
[T.BCC(cond,L)] |
| 302 |
| compileStm (T.IF(cond,yes,no)) = |
| 303 |
let val L1 = Label.anon() |
| 304 |
val L2 = Label.anon() |
| 305 |
in [T.BCC(cond,L1), |
| 306 |
no, |
| 307 |
T.JMP(T.LABEL L2,[]), |
| 308 |
T.DEFINE L1, |
| 309 |
yes, |
| 310 |
T.DEFINE L2 |
| 311 |
] |
| 312 |
end |
| 313 |
| compileStm stm = error "compileStm" |
| 314 |
|
| 315 |
(* |
| 316 |
* This function translations conditional expressions into a |
| 317 |
* branch sequence. |
| 318 |
* Note: we'll actually take advantage of the fact that |
| 319 |
* e1 and e2 are allowed to be eagerly evaluated. |
| 320 |
*) |
| 321 |
fun compileCond{exp=(ty,ccexp,e1,e2),rd,an} = |
| 322 |
let val L1 = Label.anon() |
| 323 |
in [T.MV(ty,rd,e1), |
| 324 |
mark(T.BCC(ccexp,L1),an), |
| 325 |
T.MV(ty,rd,e2), |
| 326 |
T.DEFINE L1 |
| 327 |
] |
| 328 |
end |
| 329 |
fun compileFcond{exp=(fty,ccexp,e1,e2),fd,an} = |
| 330 |
let val L1 = Label.anon() |
| 331 |
in [T.FMV(fty,fd,e1), |
| 332 |
mark(T.BCC(ccexp,L1),an), |
| 333 |
T.FMV(fty,fd,e2), |
| 334 |
T.DEFINE L1 |
| 335 |
] |
| 336 |
end |
| 337 |
|
| 338 |
end |