| 1 |
(* ppc.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2002 Bell Labs, Lucent Technologies |
| 4 |
* |
| 5 |
* I've substantially modified this code generator to support the new MLTREE. |
| 6 |
* Please see the file README.hppa for the ugly details. |
| 7 |
* |
| 8 |
* -- Allen |
| 9 |
*) |
| 10 |
|
| 11 |
functor PPC |
| 12 |
(structure PPCInstr : PPCINSTR |
| 13 |
structure PseudoInstrs : PPC_PSEUDO_INSTR (* where I = PPCInstr *) |
| 14 |
where type I.Constant.const = PPCInstr.Constant.const |
| 15 |
and type I.Region.region = PPCInstr.Region.region |
| 16 |
and type I.T.Basis.cond = PPCInstr.T.Basis.cond |
| 17 |
and type I.T.Basis.div_rounding_mode = PPCInstr.T.Basis.div_rounding_mode |
| 18 |
and type I.T.Basis.ext = PPCInstr.T.Basis.ext |
| 19 |
and type I.T.Basis.fcond = PPCInstr.T.Basis.fcond |
| 20 |
and type I.T.Basis.rounding_mode = PPCInstr.T.Basis.rounding_mode |
| 21 |
and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) PPCInstr.T.Extension.ccx |
| 22 |
and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) PPCInstr.T.Extension.fx |
| 23 |
and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) PPCInstr.T.Extension.rx |
| 24 |
and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) PPCInstr.T.Extension.sx |
| 25 |
and type I.T.I.div_rounding_mode = PPCInstr.T.I.div_rounding_mode |
| 26 |
and type I.T.ccexp = PPCInstr.T.ccexp |
| 27 |
and type I.T.fexp = PPCInstr.T.fexp |
| 28 |
(* and type I.T.labexp = PPCInstr.T.labexp *) |
| 29 |
and type I.T.mlrisc = PPCInstr.T.mlrisc |
| 30 |
and type I.T.oper = PPCInstr.T.oper |
| 31 |
and type I.T.rep = PPCInstr.T.rep |
| 32 |
and type I.T.rexp = PPCInstr.T.rexp |
| 33 |
and type I.T.stm = PPCInstr.T.stm |
| 34 |
and type I.arith = PPCInstr.arith |
| 35 |
and type I.arithi = PPCInstr.arithi |
| 36 |
and type I.bit = PPCInstr.bit |
| 37 |
and type I.bo = PPCInstr.bo |
| 38 |
and type I.ccarith = PPCInstr.ccarith |
| 39 |
and type I.cmp = PPCInstr.cmp |
| 40 |
and type I.ea = PPCInstr.ea |
| 41 |
and type I.farith = PPCInstr.farith |
| 42 |
and type I.farith3 = PPCInstr.farith3 |
| 43 |
and type I.fcmp = PPCInstr.fcmp |
| 44 |
and type I.fload = PPCInstr.fload |
| 45 |
and type I.fstore = PPCInstr.fstore |
| 46 |
and type I.funary = PPCInstr.funary |
| 47 |
and type I.instr = PPCInstr.instr |
| 48 |
and type I.instruction = PPCInstr.instruction |
| 49 |
and type I.load = PPCInstr.load |
| 50 |
and type I.operand = PPCInstr.operand |
| 51 |
and type I.rotate = PPCInstr.rotate |
| 52 |
and type I.rotatei = PPCInstr.rotatei |
| 53 |
and type I.spr = PPCInstr.spr |
| 54 |
and type I.store = PPCInstr.store |
| 55 |
and type I.unary = PPCInstr.unary |
| 56 |
and type I.xerbit = PPCInstr.xerbit |
| 57 |
structure ExtensionComp : MLTREE_EXTENSION_COMP (* where I = PPCInstr and T = PPCInstr.T *) |
| 58 |
where type I.addressing_mode = PPCInstr.addressing_mode |
| 59 |
and type I.ea = PPCInstr.ea |
| 60 |
and type I.instr = PPCInstr.instr |
| 61 |
and type I.instruction = PPCInstr.instruction |
| 62 |
and type I.operand = PPCInstr.operand |
| 63 |
where type T.Basis.cond = PPCInstr.T.Basis.cond |
| 64 |
and type T.Basis.div_rounding_mode = PPCInstr.T.Basis.div_rounding_mode |
| 65 |
and type T.Basis.ext = PPCInstr.T.Basis.ext |
| 66 |
and type T.Basis.fcond = PPCInstr.T.Basis.fcond |
| 67 |
and type T.Basis.rounding_mode = PPCInstr.T.Basis.rounding_mode |
| 68 |
and type T.Constant.const = PPCInstr.T.Constant.const |
| 69 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) PPCInstr.T.Extension.ccx |
| 70 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) PPCInstr.T.Extension.fx |
| 71 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) PPCInstr.T.Extension.rx |
| 72 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) PPCInstr.T.Extension.sx |
| 73 |
and type T.I.div_rounding_mode = PPCInstr.T.I.div_rounding_mode |
| 74 |
and type T.Region.region = PPCInstr.T.Region.region |
| 75 |
and type T.ccexp = PPCInstr.T.ccexp |
| 76 |
and type T.fexp = PPCInstr.T.fexp |
| 77 |
(* and type T.labexp = PPCInstr.T.labexp *) |
| 78 |
and type T.mlrisc = PPCInstr.T.mlrisc |
| 79 |
and type T.oper = PPCInstr.T.oper |
| 80 |
and type T.rep = PPCInstr.T.rep |
| 81 |
and type T.rexp = PPCInstr.T.rexp |
| 82 |
and type T.stm = PPCInstr.T.stm |
| 83 |
|
| 84 |
(* |
| 85 |
* Support 64 bit mode? |
| 86 |
* This should be set to false for SML/NJ |
| 87 |
*) |
| 88 |
val bit64mode : bool |
| 89 |
|
| 90 |
(* |
| 91 |
* Cost of multiplication in cycles |
| 92 |
*) |
| 93 |
val multCost : int ref |
| 94 |
) = |
| 95 |
struct |
| 96 |
structure I = PPCInstr |
| 97 |
structure T = I.T |
| 98 |
structure TS = ExtensionComp.TS |
| 99 |
structure C = PPCInstr.C |
| 100 |
structure CB = CellsBasis |
| 101 |
structure W32 = Word32 |
| 102 |
structure A = MLRiscAnnotations |
| 103 |
structure CFG = ExtensionComp.CFG |
| 104 |
|
| 105 |
fun error msg = MLRiscErrorMsg.error("PPC",msg) |
| 106 |
|
| 107 |
type instrStream = (I.instruction, CB.CellSet.cellset, CFG.cfg) TS.stream |
| 108 |
type mltreeStream = (T.stm, T.mlrisc list, CFG.cfg) TS.stream |
| 109 |
|
| 110 |
|
| 111 |
val (intTy,naturalWidths) = if bit64mode then (64,[32,64]) else (32,[32]) |
| 112 |
structure Gen = MLTreeGen |
| 113 |
(structure T = T |
| 114 |
structure Cells = C |
| 115 |
val intTy = intTy |
| 116 |
val naturalWidths = naturalWidths |
| 117 |
datatype rep = SE | ZE | NEITHER |
| 118 |
val rep = NEITHER |
| 119 |
) |
| 120 |
|
| 121 |
(* |
| 122 |
* Special instructions |
| 123 |
*) |
| 124 |
fun MTLR r = I.MTSPR{rs=r, spr=C.lr} |
| 125 |
fun MFLR r = I.MFSPR{rt=r, spr=C.lr} |
| 126 |
val CR0 = C.Reg CB.CC 0 |
| 127 |
val RET = I.BCLR{bo=I.ALWAYS, bf=CR0, bit=I.LT, LK=false, labels=[]} |
| 128 |
fun SLLI32{r,i,d} = |
| 129 |
I.ROTATEI{oper=I.RLWINM,ra=d,rs=r,sh=I.ImmedOp i,mb=0,me=SOME(31-i)} |
| 130 |
fun SRLI32{r,i,d} = |
| 131 |
I.ROTATEI{oper=I.RLWINM,ra=d,rs=r,sh=I.ImmedOp(Int.mod(32-i,32)),mb=i,me=SOME(31)} |
| 132 |
fun COPY{dst, src, tmp} = |
| 133 |
I.COPY{k=CB.GP, sz=32, dst=dst, src=src, tmp=tmp} |
| 134 |
fun FCOPY{dst, src, tmp} = |
| 135 |
I.COPY{k=CB.FP, sz=64, dst=dst, src=src, tmp=tmp} |
| 136 |
|
| 137 |
(* |
| 138 |
* Integer multiplication |
| 139 |
*) |
| 140 |
structure Mulu32 = MLTreeMult |
| 141 |
(structure I = I |
| 142 |
structure T = T |
| 143 |
structure CB = CellsBasis |
| 144 |
val intTy = 32 |
| 145 |
type arg = {r1:CB.cell,r2:CB.cell,d:CB.cell} |
| 146 |
type argi = {r:CB.cell,i:int,d:CB.cell} |
| 147 |
|
| 148 |
fun mov{r,d} = COPY{dst=[d],src=[r],tmp=NONE} |
| 149 |
fun add{r1,r2,d}= I.arith{oper=I.ADD,ra=r1,rb=r2,rt=d,Rc=false,OE=false} |
| 150 |
fun slli{r,i,d} = [I.INSTR(SLLI32{r=r,i=i,d=d})] |
| 151 |
fun srli{r,i,d} = [I.INSTR(SRLI32{r=r,i=i,d=d})] |
| 152 |
fun srai{r,i,d} = [I.arithi{oper=I.SRAWI,rt=d,ra=r,im=I.ImmedOp i}] |
| 153 |
|
| 154 |
val trapping = false |
| 155 |
val multCost = multCost |
| 156 |
fun addv{r1,r2,d}=[I.arith{oper=I.ADD,ra=r1,rb=r2,rt=d,Rc=false,OE=false}] |
| 157 |
fun subv{r1,r2,d}=[I.arith{oper=I.SUBF,ra=r2,rb=r1,rt=d,Rc=false,OE=false}] |
| 158 |
val sh1addv = NONE |
| 159 |
val sh2addv = NONE |
| 160 |
val sh3addv = NONE |
| 161 |
|
| 162 |
val signed = false) |
| 163 |
|
| 164 |
structure Muls32 = MLTreeMult |
| 165 |
(structure I = I |
| 166 |
structure T = T |
| 167 |
structure CB = CellsBasis |
| 168 |
val intTy = 32 |
| 169 |
type arg = {r1:CB.cell,r2:CB.cell,d:CB.cell} |
| 170 |
type argi = {r:CB.cell,i:int,d:CB.cell} |
| 171 |
|
| 172 |
fun mov{r,d} = COPY{dst=[d],src=[r],tmp=NONE} |
| 173 |
fun add{r1,r2,d}= I.arith{oper=I.ADD,ra=r1,rb=r2,rt=d,Rc=false,OE=false} |
| 174 |
fun slli{r,i,d} = [I.INSTR(SLLI32{r=r,i=i,d=d})] |
| 175 |
fun srli{r,i,d} = [I.INSTR(SRLI32{r=r,i=i,d=d})] |
| 176 |
fun srai{r,i,d} = [I.arithi{oper=I.SRAWI,rt=d,ra=r,im=I.ImmedOp i}] |
| 177 |
|
| 178 |
val trapping = false |
| 179 |
val multCost = multCost |
| 180 |
fun addv{r1,r2,d}=[I.arith{oper=I.ADD,ra=r1,rb=r2,rt=d,Rc=false,OE=false}] |
| 181 |
fun subv{r1,r2,d}=[I.arith{oper=I.SUBF,ra=r2,rb=r1,rt=d,Rc=false,OE=false}] |
| 182 |
val sh1addv = NONE |
| 183 |
val sh2addv = NONE |
| 184 |
val sh3addv = NONE |
| 185 |
|
| 186 |
val signed = true) |
| 187 |
|
| 188 |
structure Mult32 = MLTreeMult |
| 189 |
(structure I = I |
| 190 |
structure T = T |
| 191 |
structure CB = CellsBasis |
| 192 |
val intTy = 32 |
| 193 |
type arg = {r1:CB.cell,r2:CB.cell,d:CB.cell} |
| 194 |
type argi = {r:CB.cell,i:int,d:CB.cell} |
| 195 |
|
| 196 |
fun mov{r,d} = COPY{dst=[d],src=[r],tmp=NONE} |
| 197 |
fun add{r1,r2,d}= I.arith{oper=I.ADD,ra=r1,rb=r2,rt=d,Rc=false,OE=false} |
| 198 |
fun slli{r,i,d} = [I.INSTR(SLLI32{r=r,i=i,d=d})] |
| 199 |
fun srli{r,i,d} = [I.INSTR(SRLI32{r=r,i=i,d=d})] |
| 200 |
fun srai{r,i,d} = [I.arithi{oper=I.SRAWI,rt=d,ra=r,im=I.ImmedOp i}] |
| 201 |
|
| 202 |
val trapping = true |
| 203 |
val multCost = multCost |
| 204 |
fun addv{r1,r2,d} = error "Mult32.addv" |
| 205 |
fun subv{r1,r2,d} = error "Mult32.subv" |
| 206 |
val sh1addv = NONE |
| 207 |
val sh2addv = NONE |
| 208 |
val sh3addv = NONE |
| 209 |
|
| 210 |
val signed = true) |
| 211 |
|
| 212 |
fun selectInstructions |
| 213 |
(instrStream as |
| 214 |
TS.S.STREAM{emit=emitInstruction,comment,getAnnotations, |
| 215 |
defineLabel,entryLabel,pseudoOp,annotation, |
| 216 |
beginCluster,endCluster,exitBlock,...}) = |
| 217 |
let |
| 218 |
val emit = emitInstruction o I.INSTR |
| 219 |
|
| 220 |
(* mark an instruction with annotations *) |
| 221 |
fun annotate(instr,[]) = instr |
| 222 |
| annotate(instr,a::an) = annotate(I.ANNOTATION{i=instr,a=a},an) |
| 223 |
fun mark'(instr, an) = emitInstruction(annotate(instr, an)) |
| 224 |
fun mark(instr,an) = emitInstruction(annotate(I.INSTR instr,an)) |
| 225 |
|
| 226 |
(* Label where trap is generated. |
| 227 |
* For overflow trapping instructions, we generate a branch |
| 228 |
* to this label. |
| 229 |
*) |
| 230 |
val trapLabel : Label.label option ref = ref NONE |
| 231 |
val zeroR = C.r0 |
| 232 |
|
| 233 |
val newReg = C.newReg |
| 234 |
val newFreg = C.newFreg |
| 235 |
val newCCreg = C.newCell CB.CC |
| 236 |
|
| 237 |
|
| 238 |
fun LT (x,y) = T.I.LT(32, x, y) |
| 239 |
fun LE (x,y) = T.I.LE(32, x, y) |
| 240 |
fun toInt mi = T.I.toInt(32, mi) |
| 241 |
fun LI i = T.I.fromInt(32, i) |
| 242 |
|
| 243 |
fun signed16 mi = LE(~0x8000, mi) andalso LT(mi, 0x8000) |
| 244 |
fun signed12 mi = LE(~0x800, mi) andalso LT(mi, 0x800) |
| 245 |
fun unsigned16 mi = LE(0, mi) andalso LT(mi, 0x10000) |
| 246 |
fun unsigned5 mi = LE(0, mi) andalso LT(mi, 32) |
| 247 |
fun unsigned6 mi = LE(0, mi) andalso LT(mi, 64) |
| 248 |
|
| 249 |
fun move(rs,rd,an) = |
| 250 |
if CB.sameColor(rs,rd) then () |
| 251 |
else mark'(COPY{dst=[rd],src=[rs],tmp=NONE},an) |
| 252 |
|
| 253 |
fun fmove(fs,fd,an) = |
| 254 |
if CB.sameColor(fs,fd) then () |
| 255 |
else mark'(FCOPY{dst=[fd],src=[fs],tmp=NONE},an) |
| 256 |
|
| 257 |
fun ccmove(ccs,ccd,an) = |
| 258 |
if CB.sameColor(ccd,ccs) then () else mark(I.MCRF{bf=ccd, bfa=ccs},an) |
| 259 |
|
| 260 |
fun copy(dst, src, an) = |
| 261 |
mark'(COPY{dst=dst, src=src, |
| 262 |
tmp=case dst of [_] => NONE |
| 263 |
| _ => SOME(I.Direct(newReg()))},an) |
| 264 |
fun fcopy(dst, src, an) = |
| 265 |
mark'(FCOPY{dst=dst, src=src, |
| 266 |
tmp=case dst of [_] => NONE |
| 267 |
| _ => SOME(I.FDirect(newFreg()))},an) |
| 268 |
|
| 269 |
fun emitBranch{bo, bf, bit, addr, LK} = |
| 270 |
let val fallThrLab = Label.anon() |
| 271 |
val fallThrOpnd = I.LabelOp(T.LABEL fallThrLab) |
| 272 |
in |
| 273 |
emit(I.BC{bo=bo, bf=bf, bit=bit, addr=addr, LK=LK, fall=fallThrOpnd}); |
| 274 |
defineLabel fallThrLab |
| 275 |
end |
| 276 |
|
| 277 |
fun split n = let |
| 278 |
val wtoi = Word32.toIntX |
| 279 |
val w = T.I.toWord32(32, n) |
| 280 |
val hi = W32.~>>(w, 0w16) |
| 281 |
val lo = W32.andb(w, 0w65535) |
| 282 |
val (high, low) = |
| 283 |
if W32.<(lo,0w32768) then (hi, lo) else (hi+0w1, lo-0w65536) |
| 284 |
in |
| 285 |
(wtoi high, wtoi low) |
| 286 |
end |
| 287 |
|
| 288 |
fun loadImmedHiLo(0, lo, rt, an) = |
| 289 |
mark(I.ARITHI{oper=I.ADDI, rt=rt, ra=zeroR, im=I.ImmedOp lo}, an) |
| 290 |
| loadImmedHiLo(hi, lo, rt, an) = |
| 291 |
(mark(I.ARITHI{oper=I.ADDIS, rt=rt, ra=zeroR, im=I.ImmedOp hi}, an); |
| 292 |
if lo = 0 then () |
| 293 |
else emit(I.ARITHI{oper=I.ADDI, rt=rt, ra=rt, im=I.ImmedOp lo})) |
| 294 |
|
| 295 |
fun loadImmed(n, rt, an) = |
| 296 |
if signed16 n then |
| 297 |
mark(I.ARITHI{oper=I.ADDI, rt=rt, ra=zeroR, im=I.ImmedOp(toInt(n))}, an) |
| 298 |
else let val (hi, lo) = split n |
| 299 |
in loadImmedHiLo(hi, lo, rt, an) |
| 300 |
end |
| 301 |
|
| 302 |
fun loadLabexp(lexp, rt, an) = |
| 303 |
mark(I.ARITHI{oper=I.ADDI, rt=rt, ra=zeroR, im=I.LabelOp lexp}, an) |
| 304 |
|
| 305 |
fun immedOpnd range (e1, e2 as T.LI i) = |
| 306 |
(expr e1, if range i then I.ImmedOp(toInt i) else I.RegOp(expr e2)) |
| 307 |
| immedOpnd _ (e1, x as T.CONST _) = (expr e1, I.LabelOp x) |
| 308 |
| immedOpnd _ (e1, x as T.LABEL _) = (expr e1, I.LabelOp x) |
| 309 |
| immedOpnd _ (e1, T.LABEXP lexp) = (expr e1, I.LabelOp lexp) |
| 310 |
| immedOpnd _ (e1, e2) = (expr e1, I.RegOp(expr e2)) |
| 311 |
|
| 312 |
and commImmedOpnd range (e1 as T.LI _, e2) = |
| 313 |
immedOpnd range (e2, e1) |
| 314 |
| commImmedOpnd range (e1 as T.CONST _, e2) = |
| 315 |
immedOpnd range (e2, e1) |
| 316 |
| commImmedOpnd range (e1 as T.LABEL _, e2) = |
| 317 |
immedOpnd range (e2, e1) |
| 318 |
| commImmedOpnd range (e1 as T.LABEXP _, e2) = |
| 319 |
immedOpnd range (e2, e1) |
| 320 |
| commImmedOpnd range arg = immedOpnd range arg |
| 321 |
|
| 322 |
and eCommImm range (oper, operi, e1, e2, rt, an) = |
| 323 |
(case commImmedOpnd range (e1, e2) |
| 324 |
of (ra, I.RegOp rb) => |
| 325 |
mark(I.ARITH{oper=oper, ra=ra, rb=rb, rt=rt, Rc=false, OE=false},an) |
| 326 |
| (ra, opnd) => |
| 327 |
mark(I.ARITHI{oper=operi, ra=ra, im=opnd, rt=rt},an) |
| 328 |
(*esac*)) |
| 329 |
|
| 330 |
(* |
| 331 |
* Compute a base/displacement effective address |
| 332 |
*) |
| 333 |
and addr(size,T.ADD(_, e, T.LI i)) = |
| 334 |
let val ra = expr e |
| 335 |
in if size i then (ra, I.ImmedOp(toInt i)) else |
| 336 |
let val (hi, lo) = split i |
| 337 |
val tmpR = newReg() |
| 338 |
in emit(I.ARITHI{oper=I.ADDIS, rt=tmpR, ra=ra, im=I.ImmedOp hi}); |
| 339 |
(tmpR, I.ImmedOp lo) |
| 340 |
end |
| 341 |
end |
| 342 |
| addr(size,T.ADD(ty, T.LI i, e)) = addr(size,T.ADD(ty, e, T.LI i)) |
| 343 |
| addr(size,exp as T.SUB(ty, e, T.LI i)) = |
| 344 |
(addr(size,T.ADD(ty, e, T.LI (T.I.NEGT(32, i)))) |
| 345 |
handle Overflow => (expr exp, I.ImmedOp 0)) |
| 346 |
| addr(size,T.ADD(_, e1, e2)) = (expr e1, I.RegOp (expr e2)) |
| 347 |
| addr(size,e) = (expr e, I.ImmedOp 0) |
| 348 |
|
| 349 |
(* convert mlrisc to cellset: *) |
| 350 |
and cellset mlrisc = |
| 351 |
let val addCCReg = CB.CellSet.add |
| 352 |
fun g([],acc) = acc |
| 353 |
| g(T.GPR(T.REG(_,r))::regs,acc) = g(regs,C.addReg(r,acc)) |
| 354 |
| g(T.FPR(T.FREG(_,f))::regs,acc) = g(regs,C.addFreg(f,acc)) |
| 355 |
| g(T.CCR(T.CC(_,cc))::regs,acc) = g(regs,addCCReg(cc,acc)) |
| 356 |
| g(T.CCR(T.FCC(_,cc))::regs,acc) = g(regs,addCCReg(cc,acc)) |
| 357 |
| g(_::regs, acc) = g(regs, acc) |
| 358 |
in g(mlrisc, C.empty) end |
| 359 |
|
| 360 |
(* |
| 361 |
* Translate a statement, and annotate it |
| 362 |
*) |
| 363 |
and stmt(T.MV(_, rd, e),an) = doExpr(e, rd, an) |
| 364 |
| stmt(T.FMV(_, fd, e),an) = doFexpr(e, fd, an) |
| 365 |
| stmt(T.CCMV(ccd, ccexp), an) = doCCexpr(ccexp, ccd, an) |
| 366 |
| stmt(T.COPY(_, dst, src), an) = copy(dst, src, an) |
| 367 |
| stmt(T.FCOPY(_, dst, src), an) = fcopy(dst, src, an) |
| 368 |
| stmt(T.JMP(T.LABEXP lexp, labs),an) = |
| 369 |
mark(I.B{addr=I.LabelOp lexp, LK=false},an) |
| 370 |
| stmt(T.JMP(x as (T.LABEL _ | T.CONST _), labs),an) = |
| 371 |
mark(I.B{addr=I.LabelOp x, LK=false},an) |
| 372 |
| stmt(T.JMP(rexp, labs),an) = |
| 373 |
let val rs = expr(rexp) |
| 374 |
in emit(MTLR(rs)); |
| 375 |
mark(I.BCLR{bo=I.ALWAYS,bf=CR0,bit=I.LT,LK=false,labels=labs},an) |
| 376 |
end |
| 377 |
| stmt(T.CALL{funct, targets, defs, uses, region, pops, ...}, an) = |
| 378 |
call(funct, targets, defs, uses, region, [], an, pops) |
| 379 |
| stmt(T.FLOW_TO(T.CALL{funct, targets, defs, uses, region, pops,...}, |
| 380 |
cutTo), an) = |
| 381 |
call(funct, targets, defs, uses, region, cutTo, an, pops) |
| 382 |
| stmt(T.RET flow,an) = mark(RET,an) |
| 383 |
| stmt(T.STORE(ty,ea,data,mem),an) = store(ty,ea,data,mem,an) |
| 384 |
| stmt(T.FSTORE(ty,ea,data,mem),an) = fstore(ty,ea,data,mem,an) |
| 385 |
| stmt(T.BCC(cc, lab),an) = branch(cc,lab,an) |
| 386 |
| stmt(T.DEFINE l, _) = defineLabel l |
| 387 |
| stmt(T.LIVE S, an) = mark'(I.LIVE{regs=cellset S,spilled=C.empty},an) |
| 388 |
| stmt(T.KILL S, an) = mark'(I.KILL{regs=cellset S,spilled=C.empty},an) |
| 389 |
| stmt(T.ANNOTATION(s,a),an) = stmt(s,a::an) |
| 390 |
| stmt(T.EXT s,an) = ExtensionComp.compileSext(reducer()) {stm=s, an=an} |
| 391 |
| stmt(s, _) = doStmts(Gen.compileStm s) |
| 392 |
|
| 393 |
and call(funct, targets, defs, uses, region, cutsTo, an, 0) = |
| 394 |
let val defs=cellset(defs) |
| 395 |
val uses=cellset(uses) |
| 396 |
in emit(MTLR(expr funct)); |
| 397 |
mark(I.CALL{def=defs, use=uses, cutsTo=cutsTo, mem=region}, an) |
| 398 |
end |
| 399 |
| call _ = error "pops<>0 not implemented" |
| 400 |
|
| 401 |
and branch(T.CMP(_, _, T.LI _, T.LI _), _, _) = error "branch(LI,LI)" |
| 402 |
| branch(T.CMP(ty, cc, e1 as T.LI _, e2), lab, an) = |
| 403 |
let val cc' = T.Basis.swapCond cc |
| 404 |
in branch(T.CMP(ty, cc', e2, e1), lab, an) |
| 405 |
end |
| 406 |
| branch(cmp as T.CMP(ty, cond, e1, e2), lab, an) = let |
| 407 |
val (bo, cf) = |
| 408 |
(case cond |
| 409 |
of T.LT => (I.TRUE, I.LT) |
| 410 |
| T.LE => (I.FALSE, I.GT) |
| 411 |
| T.EQ => (I.TRUE, I.EQ) |
| 412 |
| T.NE => (I.FALSE, I.EQ) |
| 413 |
| T.GT => (I.TRUE, I.GT) |
| 414 |
| T.GE => (I.FALSE, I.LT) |
| 415 |
| T.LTU => (I.TRUE, I.LT) |
| 416 |
| T.LEU => (I.FALSE, I.GT) |
| 417 |
| T.GTU => (I.TRUE, I.GT) |
| 418 |
| T.GEU => (I.FALSE, I.LT) |
| 419 |
| (T.SETCC | T.MISC_COND _) => error "branch(CMP)" |
| 420 |
(*esac*)) |
| 421 |
val ccreg = if true then CR0 else newCCreg() (* XXX *) |
| 422 |
val addr = I.LabelOp(T.LABEL lab) |
| 423 |
fun default() = |
| 424 |
(doCCexpr(cmp, ccreg, []); |
| 425 |
emitBranch{bo=bo, bf=ccreg, bit=cf, addr=addr, LK=false}) |
| 426 |
in |
| 427 |
case (e1, e2) |
| 428 |
of (T.ANDB(_, a1, a2), T.LI z) => |
| 429 |
if z = 0 then |
| 430 |
(case commImmedOpnd unsigned16 (a1, a2) |
| 431 |
of (ra, I.RegOp rb) => |
| 432 |
emit(I.ARITH{oper=I.AND, ra=ra, rb=rb, rt=newReg(), Rc=true, OE=false}) |
| 433 |
| (ra, opnd) => |
| 434 |
emit(I.ARITHI{oper=I.ANDI_Rc, ra=ra, im=opnd, rt=newReg()}) |
| 435 |
(*esac*); |
| 436 |
branch(T.CC(cond, CR0), lab, an)) |
| 437 |
else |
| 438 |
default() |
| 439 |
| _ => |
| 440 |
default() |
| 441 |
end |
| 442 |
| branch(T.CC(cc, cr), lab, an) = |
| 443 |
let val addr=I.LabelOp(T.LABEL lab) |
| 444 |
fun branch(bo, bit) = |
| 445 |
emitBranch{bo=bo, bf=cr, bit=bit, addr=addr, LK=false} |
| 446 |
in case cc of |
| 447 |
T.EQ => branch(I.TRUE, I.EQ) |
| 448 |
| T.NE => branch(I.FALSE, I.EQ) |
| 449 |
| (T.LT | T.LTU) => branch(I.TRUE, I.LT) |
| 450 |
| (T.LE | T.LEU) => branch(I.FALSE, I.GT) |
| 451 |
| (T.GE | T.GEU) => branch(I.FALSE, I.LT) |
| 452 |
| (T.GT | T.GTU) => branch(I.TRUE, I.GT) |
| 453 |
| (T.SETCC | T.MISC_COND _) => error "branch(CC)" |
| 454 |
end |
| 455 |
| branch(cmp as T.FCMP(fty, cond, _, _), lab, an) = |
| 456 |
let val ccreg = if true then CR0 else newCCreg() (* XXX *) |
| 457 |
val labOp = I.LabelOp(T.LABEL lab) |
| 458 |
fun branch(bo, bf, bit) = |
| 459 |
emitBranch{bo=bo, bf=bf, bit=bit, addr=labOp, LK=false} |
| 460 |
fun test2bits(bit1, bit2) = |
| 461 |
let val ba=(ccreg, bit1) |
| 462 |
val bb=(ccreg, bit2) |
| 463 |
val bt=(ccreg, I.FL) |
| 464 |
in emit(I.CCARITH{oper=I.CROR, bt=bt, ba=ba, bb=bb}); |
| 465 |
branch(I.TRUE, ccreg, I.FL) |
| 466 |
end |
| 467 |
in doCCexpr(cmp, ccreg, []); |
| 468 |
case cond of |
| 469 |
T.== => branch(I.TRUE, ccreg, I.FE) |
| 470 |
| T.?<> => branch(I.FALSE, ccreg, I.FE) |
| 471 |
| T.? => branch(I.TRUE, ccreg, I.FU) |
| 472 |
| T.<=> => branch(I.FALSE, ccreg, I.FU) |
| 473 |
| T.> => branch(I.TRUE, ccreg, I.FG) |
| 474 |
| T.>= => test2bits(I.FG, I.FE) |
| 475 |
| T.?> => test2bits(I.FU, I.FG) |
| 476 |
| T.?>= => branch(I.FALSE, ccreg, I.FL) |
| 477 |
| T.< => branch(I.TRUE, ccreg, I.FL) |
| 478 |
| T.<= => test2bits(I.FL, I.FE) |
| 479 |
| T.?< => test2bits(I.FU, I.FL) |
| 480 |
| T.?<= => branch(I.FALSE, ccreg, I.FG) |
| 481 |
| T.<> => test2bits(I.FL, I.FG) |
| 482 |
| T.?= => test2bits(I.FU, I.FE) |
| 483 |
| (T.SETFCC | T.MISC_FCOND _) => error "branch(FCMP)" |
| 484 |
(*esac*) |
| 485 |
end |
| 486 |
| branch _ = error "branch" |
| 487 |
|
| 488 |
and doStmt s = stmt(s,[]) |
| 489 |
|
| 490 |
and doStmts ss = app doStmt ss |
| 491 |
|
| 492 |
(* Emit an integer store *) |
| 493 |
and store(ty, ea, data, mem, an) = |
| 494 |
let val (st,size) = case (ty,Gen.Size.size ea) of |
| 495 |
(8,32) => (I.STB,signed16) |
| 496 |
| (8,64) => (I.STBE,signed12) |
| 497 |
| (16,32) => (I.STH,signed16) |
| 498 |
| (16,64) => (I.STHE,signed12) |
| 499 |
| (32,32) => (I.STW,signed16) |
| 500 |
| (32,64) => (I.STWE,signed12) |
| 501 |
| (64,64) => (I.STDE,signed12) |
| 502 |
| _ => error "store" |
| 503 |
val (r, disp) = addr(size,ea) |
| 504 |
in mark(I.ST{st=st, rs=expr data, ra=r, d=disp, mem=mem}, an) end |
| 505 |
|
| 506 |
(* Emit a floating point store *) |
| 507 |
and fstore(ty, ea, data, mem, an) = |
| 508 |
let val (st,size) = case (ty,Gen.Size.size ea) of |
| 509 |
(32,32) => (I.STFS,signed16) |
| 510 |
| (32,64) => (I.STFSE,signed12) |
| 511 |
| (64,32) => (I.STFD,signed16) |
| 512 |
| (64,64) => (I.STFDE,signed12) |
| 513 |
| _ => error "fstore" |
| 514 |
val (r, disp) = addr(size,ea) |
| 515 |
in mark(I.STF{st=st,fs=fexpr data, ra=r, d=disp, mem=mem},an) end |
| 516 |
|
| 517 |
and subfImmed(i, ra, rt, an) = |
| 518 |
if signed16 i then |
| 519 |
mark(I.ARITHI{oper=I.SUBFIC, rt=rt, ra=ra, im=I.ImmedOp(toInt i)}, an) |
| 520 |
else |
| 521 |
mark(I.ARITH{oper=I.SUBF, rt=rt, ra=ra, rb=expr(T.LI i), |
| 522 |
Rc=false, OE=false}, an) |
| 523 |
|
| 524 |
(* Generate an arithmetic instruction *) |
| 525 |
and arith(oper, e1, e2, rt, an) = |
| 526 |
mark(I.ARITH{oper=oper,ra=expr e1,rb=expr e2,rt=rt,OE=false,Rc=false}, |
| 527 |
an) |
| 528 |
|
| 529 |
(* Generate a trapping instruction *) |
| 530 |
and arithTrapping(oper, e1, e2, rt, an) = |
| 531 |
let val ra = expr e1 val rb = expr e2 |
| 532 |
in mark(I.ARITH{oper=oper,ra=ra,rb=rb,rt=rt,OE=true,Rc=true},an); |
| 533 |
overflowTrap() |
| 534 |
end |
| 535 |
|
| 536 |
(* Generate an overflow trap *) |
| 537 |
and overflowTrap() = |
| 538 |
let val label = case !trapLabel of |
| 539 |
NONE => let val l = Label.anon() |
| 540 |
in trapLabel := SOME l; l end |
| 541 |
| SOME l => l |
| 542 |
in emitBranch{bo=I.TRUE, bf=CR0, bit=I.SO, LK=false, |
| 543 |
addr=I.LabelOp(T.LABEL label)} |
| 544 |
end |
| 545 |
|
| 546 |
(* Generate a load and annotate the instruction *) |
| 547 |
and load(ld32, ld64, ea, mem, rt, an) = |
| 548 |
let val (ld,size) = |
| 549 |
if bit64mode andalso Gen.Size.size ea = 64 |
| 550 |
then (ld64,signed12) |
| 551 |
else (ld32,signed16) |
| 552 |
val (r, disp) = addr(size,ea) |
| 553 |
in mark(I.L{ld=ld, rt=rt, ra=r, d=disp, mem=mem},an) |
| 554 |
end |
| 555 |
|
| 556 |
(* Generate a SRA shift operation and annotate the instruction *) |
| 557 |
and sra(oper, operi, e1, e2, rt, an) = |
| 558 |
case immedOpnd unsigned5 (e1, e2) of |
| 559 |
(ra, I.RegOp rb) => |
| 560 |
mark(I.ARITH{oper=oper,rt=rt,ra=ra,rb=rb,Rc=false,OE=false},an) |
| 561 |
| (ra, rb) => |
| 562 |
mark(I.ARITHI{oper=operi, rt=rt, ra=ra, im=rb},an) |
| 563 |
|
| 564 |
(* Generate a SRL shift operation and annotate the instruction *) |
| 565 |
and srl32(e1, e2, rt, an) = |
| 566 |
case immedOpnd unsigned5 (e1, e2) of |
| 567 |
(ra, I.ImmedOp n) => |
| 568 |
mark(SRLI32{r=ra,i=n,d=rt},an) |
| 569 |
| (ra, rb) => |
| 570 |
mark(I.ARITH{oper=I.SRW,rt=rt,ra=ra,rb=reduceOpn rb, |
| 571 |
Rc=false,OE=false},an) |
| 572 |
|
| 573 |
and sll32(e1, e2, rt, an) = |
| 574 |
case immedOpnd unsigned5 (e1, e2) of |
| 575 |
(ra, rb as I.ImmedOp n) => |
| 576 |
mark(SLLI32{r=ra,i=n,d=rt},an) |
| 577 |
| (ra, rb) => |
| 578 |
mark(I.ARITH{oper=I.SLW,rt=rt,ra=ra,rb=reduceOpn rb, |
| 579 |
Rc=false,OE=false},an) |
| 580 |
|
| 581 |
(* Generate a subtract operation *) |
| 582 |
and subtract(ty, e1, e2 as T.LI i, rt, an) = |
| 583 |
(doExpr(T.ADD(ty, e1, T.LI (T.I.NEGT(32, i))), rt, an) |
| 584 |
handle Overflow => |
| 585 |
mark(I.ARITH{oper=I.SUBF, rt=rt, ra=expr e2, |
| 586 |
rb=expr e1, OE=false, Rc=false}, an) |
| 587 |
) |
| 588 |
| subtract(ty, T.LI i, e2, rt, an) = subfImmed(i, expr e2, rt, an) |
| 589 |
| subtract(ty, x as (T.CONST _ | T.LABEL _), e2, rt, an) = |
| 590 |
mark(I.ARITHI{oper=I.SUBFIC,rt=rt,ra=expr e2, |
| 591 |
im=I.LabelOp x},an) |
| 592 |
| subtract(ty, e1, e2, rt, an) = |
| 593 |
let val rb = expr e1 val ra = expr e2 |
| 594 |
in mark(I.ARITH{oper=I.SUBF,rt=rt,ra=ra,rb=rb,Rc=false,OE=false},an) |
| 595 |
end |
| 596 |
|
| 597 |
(* Generate optimized multiplication code *) |
| 598 |
and multiply(ty,oper,operi,genMult,e1,e2,rt,an) = |
| 599 |
let fun nonconst(e1,e2) = |
| 600 |
[annotate( |
| 601 |
case commImmedOpnd signed16 (e1,e2) of |
| 602 |
(ra,I.RegOp rb) => |
| 603 |
I.arith{oper=oper,ra=ra,rb=rb,rt=rt,OE=false,Rc=false} |
| 604 |
| (ra,im) => I.arithi{oper=operi,ra=ra,im=im,rt=rt}, |
| 605 |
an)] |
| 606 |
fun const(e,i) = |
| 607 |
let val r = expr e |
| 608 |
in genMult{r=r,i=toInt(i),d=rt} |
| 609 |
handle _ => nonconst(T.REG(ty,r),T.LI i) |
| 610 |
end |
| 611 |
val instrs = |
| 612 |
case (e1,e2) of |
| 613 |
(_,T.LI i) => const(e1,i) |
| 614 |
| (T.LI i,_) => const(e2,i) |
| 615 |
| _ => nonconst(e1,e2) |
| 616 |
in app emitInstruction instrs end |
| 617 |
|
| 618 |
and divu32 x = Mulu32.divide{mode=T.TO_ZERO,stm=doStmt} x |
| 619 |
|
| 620 |
and divs32 x = Muls32.divide{mode=T.TO_ZERO,stm=doStmt} x |
| 621 |
|
| 622 |
and divt32 x = Mult32.divide{mode=T.TO_ZERO,stm=doStmt} x |
| 623 |
|
| 624 |
(* Generate optimized division code *) |
| 625 |
and divide(ty,oper,genDiv,e1,e2,rt,overflow,an) = |
| 626 |
let fun nonconst(e1,e2) = |
| 627 |
(mark(I.ARITH{oper=oper,ra=expr e1,rb=expr e2,rt=rt, |
| 628 |
OE=overflow,Rc=overflow},an); |
| 629 |
if overflow then overflowTrap() else () |
| 630 |
) |
| 631 |
fun const(e,i) = |
| 632 |
let val r = expr e |
| 633 |
in app emitInstruction (genDiv{r=r,i=toInt(i),d=rt}) |
| 634 |
handle _ => nonconst(T.REG(ty,r),T.LI i) |
| 635 |
end |
| 636 |
in case (e1,e2) of |
| 637 |
(_,T.LI i) => const(e1,i) |
| 638 |
| _ => nonconst(e1,e2) |
| 639 |
end |
| 640 |
|
| 641 |
(* Reduce an operand into a register *) |
| 642 |
and reduceOpn(I.RegOp r) = r |
| 643 |
| reduceOpn opn = |
| 644 |
let val rt = newReg() |
| 645 |
in emit(I.ARITHI{oper=I.ADDI, rt=rt, ra=zeroR, im=opn}); |
| 646 |
rt |
| 647 |
end |
| 648 |
|
| 649 |
(* Reduce an expression, and returns the register that holds |
| 650 |
* the value. |
| 651 |
*) |
| 652 |
and expr(rexp as T.REG(_,r)) = |
| 653 |
if CB.sameColor(C.lr, r) then |
| 654 |
let val rt = newReg() |
| 655 |
in doExpr(rexp, rt, []); rt end |
| 656 |
else r |
| 657 |
| expr(rexp) = |
| 658 |
let val rt = newReg() |
| 659 |
in doExpr(rexp, rt, []); rt end |
| 660 |
|
| 661 |
(* doExpr(e, rt, an) -- |
| 662 |
* reduce the expression e, assigns it to rd, |
| 663 |
* and annotate the expression with an |
| 664 |
*) |
| 665 |
and doExpr(e, rt, an) = |
| 666 |
if CB.sameColor(rt,C.lr) then |
| 667 |
let val rt = newReg() in doExpr(e,rt,[]); mark(MTLR rt,an) end |
| 668 |
else |
| 669 |
case e of |
| 670 |
T.REG(_,rs) => if CB.sameColor(rs,C.lr) then mark(MFLR rt,an) |
| 671 |
else move(rs,rt,an) |
| 672 |
| T.LI i => loadImmed(i, rt, an) |
| 673 |
| T.LABEXP lexp => loadLabexp(lexp, rt, an) |
| 674 |
| T.CONST _ => loadLabexp(e, rt, an) |
| 675 |
| T.LABEL _ => loadLabexp(e, rt, an) |
| 676 |
|
| 677 |
(* All data widths *) |
| 678 |
| T.ADD(_, e1, e2) => eCommImm signed16 (I.ADD,I.ADDI,e1,e2,rt,an) |
| 679 |
| T.SUB(ty, e1, e2) => subtract(ty, e1, e2, rt, an) |
| 680 |
|
| 681 |
(* Special PPC bit operations *) |
| 682 |
| T.ANDB(_,e1,T.NOTB(_,e2)) => arith(I.ANDC,e1,e2,rt,an) |
| 683 |
| T.ORB(_,e1,T.NOTB(_,e2)) => arith(I.ORC,e1,e2,rt,an) |
| 684 |
| T.XORB(_,e1,T.NOTB(_,e2)) => arith(I.EQV,e1,e2,rt,an) |
| 685 |
| T.EQVB(_,e1,e2) => arith(I.EQV,e1,e2,rt,an) |
| 686 |
| T.ANDB(_,T.NOTB(_,e1),e2) => arith(I.ANDC,e2,e1,rt,an) |
| 687 |
| T.ORB(_,T.NOTB(_,e1),e2) => arith(I.ORC,e2,e1,rt,an) |
| 688 |
| T.XORB(_,T.NOTB(_,e1),e2) => arith(I.EQV,e2,e1,rt,an) |
| 689 |
| T.NOTB(_,T.ANDB(_,e1,e2)) => arith(I.NAND,e1,e2,rt,an) |
| 690 |
| T.NOTB(_,T.ORB(_,e1,e2)) => arith(I.NOR,e1,e2,rt,an) |
| 691 |
| T.NOTB(_,T.XORB(_,e1,e2)) => arith(I.EQV,e1,e2,rt,an) |
| 692 |
|
| 693 |
| T.ANDB(_, e1, e2) => |
| 694 |
eCommImm unsigned16(I.AND,I.ANDI_Rc,e1,e2,rt,an) |
| 695 |
| T.ORB(_, e1, e2) => eCommImm unsigned16(I.OR,I.ORI,e1,e2,rt,an) |
| 696 |
| T.XORB(_, e1, e2) => eCommImm unsigned16(I.XOR,I.XORI,e1,e2,rt,an) |
| 697 |
|
| 698 |
(* 32 bit support *) |
| 699 |
| T.MULU(32, e1, e2) => multiply(32,I.MULLW,I.MULLI, |
| 700 |
Mulu32.multiply,e1,e2,rt,an) |
| 701 |
| T.DIVU(32, e1, e2) => divide(32,I.DIVWU,divu32,e1,e2,rt,false,an) |
| 702 |
|
| 703 |
| T.MULS(32, e1, e2) => multiply(32,I.MULLW,I.MULLI, |
| 704 |
Muls32.multiply,e1,e2,rt,an) |
| 705 |
| T.DIVS(T.DIV_TO_ZERO, 32, e1, e2) => |
| 706 |
(* On the PPC we turn overflow checking on despite this |
| 707 |
* being DIVS. That's because divide-by-zero is also |
| 708 |
* indicated through "overflow" instead of causing a trap. *) |
| 709 |
divide(32,I.DIVW,divs32,e1,e2,rt, |
| 710 |
true (* !! *), |
| 711 |
an) |
| 712 |
|
| 713 |
| T.ADDT(32, e1, e2) => arithTrapping(I.ADD, e1, e2, rt, an) |
| 714 |
| T.SUBT(32, e1, e2) => arithTrapping(I.SUBF, e2, e1, rt, an) |
| 715 |
| T.MULT(32, e1, e2) => arithTrapping(I.MULLW, e1, e2, rt, an) |
| 716 |
| T.DIVT(T.DIV_TO_ZERO, 32, e1, e2) => |
| 717 |
divide(32,I.DIVW,divt32,e1,e2,rt,true,an) |
| 718 |
|
| 719 |
| T.SRA(32, e1, e2) => sra(I.SRAW, I.SRAWI, e1, e2, rt, an) |
| 720 |
| T.SRL(32, e1, e2) => srl32(e1, e2, rt, an) |
| 721 |
| T.SLL(32, e1, e2) => sll32(e1, e2, rt, an) |
| 722 |
|
| 723 |
(* 64 bit support *) |
| 724 |
| T.SRA(64, e1, e2) => sra(I.SRAD, I.SRADI, e1, e2, rt, an) |
| 725 |
(*| T.SRL(64, e1, e2) => srl(32, I.SRD, I.RLDINM, e1, e2, rt, an) |
| 726 |
| T.SLL(64, e1, e2) => sll(32, I.SLD, I.RLDINM, e1, e2, rt, an)*) |
| 727 |
|
| 728 |
(* loads *) |
| 729 |
| T.LOAD(8,ea,mem) => load(I.LBZ,I.LBZE,ea,mem,rt,an) |
| 730 |
| T.LOAD(16,ea, mem) => load(I.LHZ,I.LHZE,ea,mem,rt,an) |
| 731 |
| T.LOAD(32,ea, mem) => load(I.LWZ,I.LWZE,ea,mem,rt,an) |
| 732 |
| T.LOAD(64,ea, mem) => load(I.LDE,I.LDE,ea,mem,rt,an) |
| 733 |
|
| 734 |
(* Conditional expression *) |
| 735 |
| T.COND exp => |
| 736 |
doStmts(Gen.compileCond{exp=exp,an=an,rd=rt}) |
| 737 |
|
| 738 |
(* Misc *) |
| 739 |
| T.LET(s,e) => (doStmt s; doExpr(e, rt, an)) |
| 740 |
| T.MARK(e, A.MARKREG f) => (f rt; doExpr(e,rt,an)) |
| 741 |
| T.MARK(e, a) => doExpr(e,rt,a::an) |
| 742 |
| T.REXT e => ExtensionComp.compileRext (reducer()) {e=e,rd=rt,an=an} |
| 743 |
| e => doExpr(Gen.compileRexp e,rt,an) |
| 744 |
|
| 745 |
(* Generate a floating point load *) |
| 746 |
and fload(ld32, ld64, ea, mem, ft, an) = |
| 747 |
let val (ld,size) = |
| 748 |
if bit64mode andalso Gen.Size.size ea = 64 then (ld64,signed12) |
| 749 |
else (ld32,signed16) |
| 750 |
val (r, disp) = addr(size,ea) |
| 751 |
in mark(I.LF{ld=ld, ft=ft, ra=r, d=disp, mem=mem}, an) end |
| 752 |
|
| 753 |
(* Generate a floating-point binary operation *) |
| 754 |
and fbinary(oper, e1, e2, ft, an) = |
| 755 |
mark(I.FARITH{oper=oper,fa=fexpr e1,fb=fexpr e2,ft=ft,Rc=false}, an) |
| 756 |
|
| 757 |
(* Generate a floating-point 3-operand operation |
| 758 |
* These are of the form |
| 759 |
* +/- e1 * e3 +/- e2 |
| 760 |
*) |
| 761 |
and f3(oper, e1, e2, e3, ft, an) = |
| 762 |
mark(I.FARITH3{oper=oper,fa=fexpr e1,fb=fexpr e2,fc=fexpr e3, |
| 763 |
ft=ft,Rc=false}, an) |
| 764 |
|
| 765 |
(* Generate a floating-point unary operation *) |
| 766 |
and funary(oper, e, ft, an) = |
| 767 |
mark(I.FUNARY{oper=oper, ft=ft, fb=fexpr e, Rc=false}, an) |
| 768 |
|
| 769 |
(* Reduce the expression fexp, return the register that holds |
| 770 |
* the value. |
| 771 |
*) |
| 772 |
and fexpr(T.FREG(_,f)) = f |
| 773 |
| fexpr(e) = |
| 774 |
let val ft = newFreg() |
| 775 |
in doFexpr(e, ft, []); ft end |
| 776 |
|
| 777 |
(* doExpr(fexp, ft, an) -- |
| 778 |
* reduce the expression fexp, and assigns |
| 779 |
* it to ft. Also annotate fexp. |
| 780 |
*) |
| 781 |
and doFexpr(e, ft, an) = |
| 782 |
case e of |
| 783 |
T.FREG(_,fs) => fmove(fs,ft,an) |
| 784 |
|
| 785 |
(* Single precision support *) |
| 786 |
| T.FLOAD(32, ea, mem) => fload(I.LFS,I.LFSE,ea,mem,ft,an) |
| 787 |
|
| 788 |
(* special 3 operand floating point arithmetic *) |
| 789 |
| T.FADD(32,T.FMUL(32,a,c),b) => f3(I.FMADDS,a,b,c,ft,an) |
| 790 |
| T.FADD(32,b,T.FMUL(32,a,c)) => f3(I.FMADDS,a,b,c,ft,an) |
| 791 |
| T.FSUB(32,T.FMUL(32,a,c),b) => f3(I.FMSUBS,a,b,c,ft,an) |
| 792 |
| T.FSUB(32,b,T.FMUL(32,a,c)) => f3(I.FNMSUBS,a,b,c,ft,an) |
| 793 |
| T.FNEG(32,T.FADD(32,T.FMUL(32,a,c),b)) => f3(I.FNMADDS,a,b,c,ft,an) |
| 794 |
| T.FNEG(32,T.FADD(32,b,T.FMUL(32,a,c))) => f3(I.FNMADDS,a,b,c,ft,an) |
| 795 |
| T.FSUB(32,T.FNEG(32,T.FMUL(32,a,c)),b) => f3(I.FNMADDS,a,b,c,ft,an) |
| 796 |
|
| 797 |
| T.FADD(32, e1, e2) => fbinary(I.FADDS, e1, e2, ft, an) |
| 798 |
| T.FSUB(32, e1, e2) => fbinary(I.FSUBS, e1, e2, ft, an) |
| 799 |
| T.FMUL(32, e1, e2) => fbinary(I.FMULS, e1, e2, ft, an) |
| 800 |
| T.FDIV(32, e1, e2) => fbinary(I.FDIVS, e1, e2, ft, an) |
| 801 |
|
| 802 |
(* Double precision support *) |
| 803 |
| T.FLOAD(64, ea, mem) => fload(I.LFD,I.LFDE,ea,mem,ft,an) |
| 804 |
|
| 805 |
(* special 3 operand floating point arithmetic *) |
| 806 |
| T.FADD(64,T.FMUL(64,a,c),b) => f3(I.FMADD,a,b,c,ft,an) |
| 807 |
| T.FADD(64,b,T.FMUL(64,a,c)) => f3(I.FMADD,a,b,c,ft,an) |
| 808 |
| T.FSUB(64,T.FMUL(64,a,c),b) => f3(I.FMSUB,a,b,c,ft,an) |
| 809 |
| T.FSUB(64,b,T.FMUL(64,a,c)) => f3(I.FNMSUB,a,b,c,ft,an) |
| 810 |
| T.FNEG(64,T.FADD(64,T.FMUL(64,a,c),b)) => f3(I.FNMADD,a,b,c,ft,an) |
| 811 |
| T.FNEG(64,T.FADD(64,b,T.FMUL(64,a,c))) => f3(I.FNMADD,a,b,c,ft,an) |
| 812 |
| T.FSUB(64,T.FNEG(64,T.FMUL(64,a,c)),b) => f3(I.FNMADD,a,b,c,ft,an) |
| 813 |
|
| 814 |
| T.FADD(64, e1, e2) => fbinary(I.FADD, e1, e2, ft, an) |
| 815 |
| T.FSUB(64, e1, e2) => fbinary(I.FSUB, e1, e2, ft, an) |
| 816 |
| T.FMUL(64, e1, e2) => fbinary(I.FMUL, e1, e2, ft, an) |
| 817 |
| T.FDIV(64, e1, e2) => fbinary(I.FDIV, e1, e2, ft, an) |
| 818 |
| T.CVTI2F(64,_,e) => |
| 819 |
app emitInstruction (PseudoInstrs.cvti2d{reg=expr e,fd=ft}) |
| 820 |
|
| 821 |
(* Single/double precision support *) |
| 822 |
| T.FABS((32|64), e) => funary(I.FABS, e, ft, an) |
| 823 |
| T.FNEG((32|64), e) => funary(I.FNEG, e, ft, an) |
| 824 |
| T.FSQRT(32, e) => funary(I.FSQRTS, e, ft, an) |
| 825 |
| T.FSQRT(64, e) => funary(I.FSQRT, e, ft, an) |
| 826 |
|
| 827 |
| T.CVTF2F(64,32,e) => doFexpr(e,ft,an) (* 32->64 is a no-op *) |
| 828 |
| T.CVTF2F(32,32,e) => doFexpr(e,ft,an) |
| 829 |
| T.CVTF2F(64,64,e) => doFexpr(e,ft,an) |
| 830 |
| T.CVTF2F(32,64,e) => funary(I.FRSP,e,ft,an) |
| 831 |
|
| 832 |
(* Misc *) |
| 833 |
| T.FMARK(e, A.MARKREG f) => (f ft; doFexpr(e,ft,an)) |
| 834 |
| T.FMARK(e, a) => doFexpr(e,ft,a::an) |
| 835 |
| T.FEXT e => ExtensionComp.compileFext (reducer()) {e=e,fd=ft,an=an} |
| 836 |
| _ => error "doFexpr" |
| 837 |
|
| 838 |
and ccExpr(T.CC(_,cc)) = cc |
| 839 |
| ccExpr(T.FCC(_,cc)) = cc |
| 840 |
| ccExpr(ccexp) = |
| 841 |
let val cc = newCCreg() |
| 842 |
in doCCexpr(ccexp,cc,[]); cc end |
| 843 |
|
| 844 |
(* Reduce an condition expression, and assigns the result to ccd *) |
| 845 |
and doCCexpr(ccexp, ccd, an) = |
| 846 |
case ccexp of |
| 847 |
T.CMP(ty, cc, e1, e2) => |
| 848 |
let val (opnds, cmp) = |
| 849 |
case cc of |
| 850 |
(T.LT | T.LE | T.EQ | T.NE | T.GT | T.GE) => |
| 851 |
(immedOpnd signed16, I.CMP) |
| 852 |
| _ => (immedOpnd unsigned16, I.CMPL) |
| 853 |
val (opndA, opndB) = opnds(e1, e2) |
| 854 |
val l = case ty of |
| 855 |
32 => false |
| 856 |
| 64 => true |
| 857 |
| _ => error "doCCexpr" |
| 858 |
in mark(I.COMPARE{cmp=cmp, l=l, bf=ccd, ra=opndA, rb=opndB},an) |
| 859 |
end |
| 860 |
| T.FCMP(fty, fcc, e1, e2) => |
| 861 |
mark(I.FCOMPARE{cmp=I.FCMPU, bf=ccd, fa=fexpr e1, fb=fexpr e2},an) |
| 862 |
| T.CC(_,cc) => ccmove(cc,ccd,an) |
| 863 |
| T.CCMARK(cc,A.MARKREG f) => (f ccd; doCCexpr(cc,ccd,an)) |
| 864 |
| T.CCMARK(cc,a) => doCCexpr(cc,ccd,a::an) |
| 865 |
| T.CCEXT e => |
| 866 |
ExtensionComp.compileCCext (reducer()) {e=e, ccd=ccd, an=an} |
| 867 |
| _ => error "doCCexpr: Not implemented" |
| 868 |
|
| 869 |
and emitTrap() = emit(I.TW{to=31,ra=zeroR,si=I.ImmedOp 0}) |
| 870 |
|
| 871 |
and beginCluster' _ = |
| 872 |
(trapLabel := NONE; beginCluster 0) |
| 873 |
|
| 874 |
and endCluster' a = |
| 875 |
(case !trapLabel of |
| 876 |
SOME label => |
| 877 |
(defineLabel label; emitTrap(); trapLabel := NONE) |
| 878 |
| NONE => (); |
| 879 |
endCluster a) |
| 880 |
|
| 881 |
and reducer() = |
| 882 |
TS.REDUCER{reduceRexp = expr, |
| 883 |
reduceFexp = fexpr, |
| 884 |
reduceCCexp = ccExpr, |
| 885 |
reduceStm = stmt, |
| 886 |
operand = (fn _ => error "operand"), |
| 887 |
reduceOperand = reduceOpn, |
| 888 |
addressOf = (fn _ => error "addressOf"), |
| 889 |
emit = emitInstruction o annotate, |
| 890 |
instrStream = instrStream, |
| 891 |
mltreeStream = self() |
| 892 |
} |
| 893 |
and self() = |
| 894 |
TS.S.STREAM |
| 895 |
{ beginCluster = beginCluster', |
| 896 |
endCluster = endCluster', |
| 897 |
emit = doStmt, |
| 898 |
pseudoOp = pseudoOp, |
| 899 |
defineLabel = defineLabel, |
| 900 |
entryLabel = entryLabel, |
| 901 |
comment = comment, |
| 902 |
annotation = annotation, |
| 903 |
getAnnotations=getAnnotations, |
| 904 |
exitBlock = fn mlrisc => exitBlock(cellset mlrisc) |
| 905 |
} |
| 906 |
in self() |
| 907 |
end |
| 908 |
|
| 909 |
end |
| 910 |
|