| 1 |
(* X86Spill.sml |
| 2 |
* |
| 3 |
* X86 spilling is complicated business. |
| 4 |
* Allen: and it just got more complicated; now we have to recognize the regmap. |
| 5 |
* I've also improved the spilling code so that more instructions are |
| 6 |
* recognized. Addressing modes are now folded into the existing instruction |
| 7 |
* whenever possible. This eliminates some redundant temporaries which were |
| 8 |
* introduced before. |
| 9 |
*) |
| 10 |
functor X86SpillInstr(structure Instr: X86INSTR |
| 11 |
structure Props: INSN_PROPERTIES (* where I = Instr *) |
| 12 |
where type I.addressing_mode = Instr.addressing_mode |
| 13 |
and type I.ea = Instr.ea |
| 14 |
and type I.instr = Instr.instr |
| 15 |
and type I.instruction = Instr.instruction |
| 16 |
and type I.operand = Instr.operand |
| 17 |
) : ARCH_SPILL_INSTR = struct |
| 18 |
|
| 19 |
structure I = Instr |
| 20 |
structure C = I.C |
| 21 |
structure CB = CellsBasis |
| 22 |
|
| 23 |
fun error msg = MLRiscErrorMsg.impossible("X86Spill: "^ msg) |
| 24 |
|
| 25 |
fun immed(I.Immed _) = true |
| 26 |
| immed(I.ImmedLabel _) = true |
| 27 |
| immed _ = false |
| 28 |
|
| 29 |
fun immedOrReg(I.Direct r) = true |
| 30 |
| immedOrReg(I.Immed _) = true |
| 31 |
| immedOrReg(I.ImmedLabel _) = true |
| 32 |
| immedOrReg _ = false |
| 33 |
|
| 34 |
fun isMemory(I.MemReg _) = true |
| 35 |
| isMemory(I.Displace _) = true |
| 36 |
| isMemory(I.Indexed _) = true |
| 37 |
| isMemory(I.LabelEA _) = true |
| 38 |
| isMemory _ = false |
| 39 |
|
| 40 |
(* Annotate instruction *) |
| 41 |
fun annotate(instr,[]) = instr |
| 42 |
| annotate(instr,a::an) = annotate(I.ANNOTATION{i=instr,a=a},an) |
| 43 |
|
| 44 |
fun mark(instr, an) = annotate(I.INSTR instr, an) |
| 45 |
|
| 46 |
fun liveKill(add, rmv) ({regs, spilled}, reg) = |
| 47 |
{regs=rmv(reg, regs), spilled=add(reg, spilled)} |
| 48 |
|
| 49 |
val fLiveKill = liveKill (C.addFreg, C.rmvFreg) |
| 50 |
val rLiveKill = liveKill (C.addReg, C.rmvReg) |
| 51 |
|
| 52 |
val newReg = C.newReg |
| 53 |
|
| 54 |
|
| 55 |
fun spillR(instr, reg, spillLoc) = let |
| 56 |
fun x86Spill(instr, an) = let |
| 57 |
fun done(instr, an) = {code=[mark(instr, an)], proh=[], newReg=NONE} |
| 58 |
in |
| 59 |
case instr of |
| 60 |
I.CALL{opnd=addr, defs, uses, return, cutsTo, mem, pops} => |
| 61 |
done(I.CALL{opnd=addr, defs=C.rmvReg(reg,defs), |
| 62 |
return=return, uses=uses, |
| 63 |
cutsTo=cutsTo, mem=mem, pops=pops}, an) |
| 64 |
| I.MOVE{mvOp as (I.MOVZBL|I.MOVSBL|I.MOVZWL|I.MOVSWL), src, dst} => |
| 65 |
let val tmpR = newReg() val tmp = I.Direct tmpR |
| 66 |
in {proh=[tmpR], newReg=SOME tmpR, |
| 67 |
code=[mark(I.MOVE{mvOp=mvOp, src=src, dst=tmp}, an), |
| 68 |
I.move{mvOp=I.MOVL, src=tmp, dst=spillLoc}] |
| 69 |
} |
| 70 |
end |
| 71 |
| I.MOVE{mvOp, src as I.Direct rs, dst} => |
| 72 |
if CB.sameColor(rs,reg) then {code=[], proh=[], newReg=NONE} |
| 73 |
else done(I.MOVE{mvOp=mvOp, src=src, dst=spillLoc}, an) |
| 74 |
| I.MOVE{mvOp, src, dst=I.Direct _} => |
| 75 |
if Props.eqOpn(src, spillLoc) then {code=[], proh=[], newReg=NONE} |
| 76 |
else if immed src then |
| 77 |
done(I.MOVE{mvOp=mvOp, src=src, dst=spillLoc}, an) |
| 78 |
else |
| 79 |
let val tmpR = newReg() |
| 80 |
val tmp = I.Direct tmpR |
| 81 |
in {proh=[tmpR], |
| 82 |
newReg=SOME tmpR, |
| 83 |
code=[mark(I.MOVE{mvOp=mvOp, src=src, dst=tmp}, an), |
| 84 |
I.move{mvOp=mvOp, src=tmp, dst=spillLoc}] |
| 85 |
} |
| 86 |
end |
| 87 |
| I.LEA{addr, r32} => |
| 88 |
let val tmpR = newReg() |
| 89 |
in {proh=[tmpR], |
| 90 |
newReg=SOME tmpR, |
| 91 |
code=[mark(I.LEA{addr=addr, r32=tmpR}, an), |
| 92 |
I.move{mvOp=I.MOVL, src=I.Direct tmpR, dst=spillLoc}] |
| 93 |
} |
| 94 |
end |
| 95 |
| I.BINARY{binOp=I.XORL, src as I.Direct rs, dst=I.Direct rd} => |
| 96 |
if CB.sameColor(rs,rd) then |
| 97 |
{proh=[], |
| 98 |
code=[mark(I.MOVE{mvOp=I.MOVL, src=I.Immed 0, dst=spillLoc}, an)], |
| 99 |
newReg=NONE |
| 100 |
} |
| 101 |
else |
| 102 |
{proh=[], |
| 103 |
code=[mark(I.BINARY{binOp=I.XORL, src=src, dst=spillLoc}, an)], |
| 104 |
newReg=NONE |
| 105 |
} |
| 106 |
| I.BINARY{binOp, src, dst} => let (* note: dst = reg *) |
| 107 |
fun multBinOp(I.IMULL|I.IMULW|I.IMULB) = true |
| 108 |
| multBinOp _ = false |
| 109 |
in |
| 110 |
if multBinOp binOp then let |
| 111 |
(* destination must remain a register *) |
| 112 |
val tmpR = newReg() |
| 113 |
val tmp = I.Direct tmpR |
| 114 |
in |
| 115 |
{proh=[tmpR], |
| 116 |
code= [I.move{mvOp=I.MOVL, src=spillLoc, dst=tmp}, |
| 117 |
I.binary{binOp=binOp, src=src, dst=tmp}, |
| 118 |
I.move{mvOp=I.MOVL, src=tmp, dst=spillLoc}], |
| 119 |
newReg=SOME tmpR |
| 120 |
} |
| 121 |
end |
| 122 |
else if immedOrReg src then |
| 123 |
(* can replace the destination directly *) |
| 124 |
done(I.BINARY{binOp=binOp, src=src, dst=spillLoc}, an) |
| 125 |
else let |
| 126 |
(* a memory src and non multBinOp |
| 127 |
* --- cannot have two memory operands |
| 128 |
*) |
| 129 |
val tmpR = newReg() |
| 130 |
val tmp = I.Direct tmpR |
| 131 |
in |
| 132 |
{ proh=[tmpR], |
| 133 |
code=[I.move{mvOp=I.MOVL, src=src, dst=tmp}, |
| 134 |
I.binary{binOp=binOp, src=tmp, dst=spillLoc}], |
| 135 |
newReg=NONE |
| 136 |
} |
| 137 |
end |
| 138 |
end |
| 139 |
| I.SHIFT{shiftOp, count, src, dst} => error "go and implement SHIFT" |
| 140 |
| I.CMOV{cond, src, dst} => |
| 141 |
(* note: dst must be a register *) |
| 142 |
(case spillLoc of |
| 143 |
I.Direct r => |
| 144 |
{proh=[], |
| 145 |
newReg=NONE, |
| 146 |
code=[mark(I.CMOV{cond=cond,src=src,dst=r},an)] |
| 147 |
} |
| 148 |
| _ => |
| 149 |
let val tmpR = newReg() |
| 150 |
val tmp = I.Direct tmpR |
| 151 |
in {proh=[tmpR], |
| 152 |
newReg=SOME tmpR, |
| 153 |
code=[I.move{mvOp=I.MOVL, src=spillLoc, dst=tmp}, |
| 154 |
mark(I.CMOV{cond=cond,src=src,dst=tmpR},an), |
| 155 |
I.move{mvOp=I.MOVL, src=tmp, dst=spillLoc}] |
| 156 |
} |
| 157 |
end |
| 158 |
) |
| 159 |
|
| 160 |
| I.CMPXCHG{lock,sz,src,dst} => |
| 161 |
if immedOrReg src then |
| 162 |
{proh=[], |
| 163 |
code=[mark(I.CMPXCHG{lock=lock,sz=sz,src=src,dst=spillLoc},an)], |
| 164 |
newReg=NONE |
| 165 |
} |
| 166 |
else |
| 167 |
let val tmpR = newReg() |
| 168 |
val tmp = I.Direct tmpR |
| 169 |
in {proh=[], |
| 170 |
code=[I.move{mvOp=I.MOVL, src=src, dst=tmp}, |
| 171 |
mark(I.CMPXCHG{lock=lock,sz=sz,src=tmp,dst=spillLoc},an)], |
| 172 |
newReg=NONE |
| 173 |
} |
| 174 |
end |
| 175 |
| I.MULTDIV _ => error "spill: MULTDIV" |
| 176 |
| I.MUL3{src1, src2, dst} => |
| 177 |
let val tmpR = newReg() |
| 178 |
in {proh=[tmpR], newReg=SOME tmpR, |
| 179 |
code=[mark(I.MUL3{src1=src1, src2=src2, dst=tmpR}, an), |
| 180 |
I.move{mvOp=I.MOVL, src=I.Direct tmpR, dst=spillLoc}] |
| 181 |
} |
| 182 |
end |
| 183 |
| I.UNARY{unOp, opnd} => done(I.UNARY{unOp=unOp, opnd=spillLoc}, an) |
| 184 |
| I.SET{cond, opnd} => done(I.SET{cond=cond, opnd=spillLoc}, an) |
| 185 |
| I.POP _ => done(I.POP spillLoc, an) |
| 186 |
| I.FNSTSW => error "spill: FNSTSW" |
| 187 |
| _ => error "spill" |
| 188 |
end (* x86Spill *) |
| 189 |
|
| 190 |
fun f(I.INSTR instr, an) = x86Spill(instr, an) |
| 191 |
| f(I.ANNOTATION{a, i}, an) = f(i, a::an) |
| 192 |
| f(I.KILL lk, an) = |
| 193 |
{code=[annotate(I.KILL(rLiveKill (lk, reg)), an)], |
| 194 |
proh=[], |
| 195 |
newReg=NONE} |
| 196 |
| f _ = error "spill:f" |
| 197 |
in f(instr, []) |
| 198 |
end |
| 199 |
|
| 200 |
fun reloadR(instr, reg, spillLoc) = let |
| 201 |
fun x86Reload(instr, reg, spillLoc, an) = let |
| 202 |
fun operand(rt, opnd) = |
| 203 |
(case opnd |
| 204 |
of I.Direct r => if CB.sameColor(r,reg) then I.Direct rt else opnd |
| 205 |
| I.Displace{base, disp, mem} => |
| 206 |
if CB.sameColor(base,reg) |
| 207 |
then I.Displace{base=rt, disp=disp, mem=mem} |
| 208 |
else opnd |
| 209 |
| I.Indexed{base=NONE, index, scale, disp, mem=mem} => |
| 210 |
if CB.sameColor(index,reg) then |
| 211 |
I.Indexed{base=NONE, index=rt, scale=scale, disp=disp, mem=mem} |
| 212 |
else opnd |
| 213 |
| I.Indexed{base as SOME b, index, scale, disp, mem=mem} => |
| 214 |
if CB.sameColor(b,reg) then |
| 215 |
operand(rt, I.Indexed{base=SOME rt, index=index, |
| 216 |
scale=scale, disp=disp, mem=mem}) |
| 217 |
else if CB.sameColor(index,reg) then |
| 218 |
I.Indexed{base=base, index=rt, scale=scale, disp=disp, mem=mem} |
| 219 |
else opnd |
| 220 |
| opnd => opnd |
| 221 |
(*esac*)) |
| 222 |
|
| 223 |
fun done(instr, an) = {code=[mark(instr, an)], proh=[], newReg=NONE} |
| 224 |
|
| 225 |
fun isReloading (I.Direct r) = CB.sameColor(r,reg) |
| 226 |
| isReloading _ = false |
| 227 |
|
| 228 |
(* This version assumes that the value of tmpR is killed *) |
| 229 |
fun withTmp(f, an) = |
| 230 |
case spillLoc of |
| 231 |
I.Direct tmpR => |
| 232 |
{newReg=NONE, |
| 233 |
proh=[], |
| 234 |
code=[mark(f tmpR, an)] |
| 235 |
} |
| 236 |
| _ => |
| 237 |
let val tmpR = newReg() |
| 238 |
in {newReg=NONE, |
| 239 |
proh=[tmpR], |
| 240 |
code=[I.move{mvOp=I.MOVL, src=spillLoc, dst=I.Direct tmpR}, |
| 241 |
mark(f tmpR, an) |
| 242 |
] |
| 243 |
} |
| 244 |
end |
| 245 |
|
| 246 |
(* This version assumes that the value of tmpR is available afterwards *) |
| 247 |
fun withTmpAvail(f, an) = |
| 248 |
case spillLoc of |
| 249 |
I.Direct tmpR => |
| 250 |
{newReg=SOME tmpR, |
| 251 |
proh=[tmpR], |
| 252 |
code=[mark(f tmpR, an)] |
| 253 |
} |
| 254 |
| _ => |
| 255 |
let val tmpR = newReg() |
| 256 |
val tmp = I.Direct tmpR |
| 257 |
in {newReg=SOME tmpR, |
| 258 |
proh=[tmpR], |
| 259 |
code=[I.move{mvOp=I.MOVL, src=spillLoc, dst=I.Direct tmpR}, |
| 260 |
mark(f tmpR, an) |
| 261 |
] |
| 262 |
} |
| 263 |
end |
| 264 |
|
| 265 |
fun replace(opn as I.Direct r) = |
| 266 |
if CB.sameColor(r,reg) then spillLoc else opn |
| 267 |
| replace opn = opn |
| 268 |
|
| 269 |
(* Fold in a memory operand if possible. Makes sure that both operands |
| 270 |
* are not in memory. lsrc cannot be immediate. |
| 271 |
*) |
| 272 |
fun reloadCmp(cmp, lsrc, rsrc, an) = |
| 273 |
let fun reloadIt() = |
| 274 |
withTmp(fn tmpR => |
| 275 |
cmp{lsrc=operand(tmpR, lsrc), rsrc=operand(tmpR, rsrc)}, an) |
| 276 |
in if immedOrReg lsrc andalso immedOrReg rsrc then |
| 277 |
let val lsrc' = replace lsrc |
| 278 |
val rsrc' = replace rsrc |
| 279 |
in if isMemory lsrc' andalso isMemory rsrc' then |
| 280 |
reloadIt() |
| 281 |
else |
| 282 |
done(cmp{lsrc=lsrc', rsrc=rsrc'}, an) |
| 283 |
end |
| 284 |
else reloadIt() |
| 285 |
end |
| 286 |
|
| 287 |
fun reloadBT(bitOp, lsrc, rsrc, an) = |
| 288 |
reloadCmp(fn {lsrc,rsrc} => I.BITOP{bitOp=bitOp,lsrc=lsrc,rsrc=rsrc}, |
| 289 |
lsrc, rsrc, an) |
| 290 |
|
| 291 |
(* Fold in a memory operand if possible. Makes sure that the right |
| 292 |
* operand is not in memory and left operand is not an immediate. |
| 293 |
* lsrc rsrc |
| 294 |
* AL, imm8 opc1 A8 |
| 295 |
* EAX, imm32 opc1 A9 |
| 296 |
* r/m8, imm8 opc2 F6/0 ib |
| 297 |
* r/m32, imm32 opc2 F7/0 id |
| 298 |
* r/m32, r32 opc3 85/r |
| 299 |
*) |
| 300 |
fun reloadTest(test, lsrc, rsrc, an) = |
| 301 |
let fun reloadIt() = |
| 302 |
withTmp(fn tmpR => |
| 303 |
test{lsrc=operand(tmpR, lsrc), rsrc=operand(tmpR, rsrc)}, an) |
| 304 |
in if immedOrReg lsrc andalso immedOrReg rsrc then |
| 305 |
let val lsrc = replace lsrc |
| 306 |
val rsrc = replace rsrc |
| 307 |
in if isMemory rsrc then |
| 308 |
if isMemory lsrc then reloadIt() |
| 309 |
else (* it is commutative! *) |
| 310 |
done(test{lsrc=rsrc, rsrc=lsrc}, an) |
| 311 |
else |
| 312 |
done(test{lsrc=lsrc, rsrc=rsrc}, an) |
| 313 |
end |
| 314 |
else reloadIt() |
| 315 |
end |
| 316 |
|
| 317 |
fun reloadPush(push, arg as I.Direct _, an) = |
| 318 |
done(push(replace arg), an) |
| 319 |
| reloadPush(push, arg, an) = |
| 320 |
withTmpAvail(fn tmpR => push(operand(tmpR, arg)), an) |
| 321 |
|
| 322 |
fun reloadReal(realOp, opnd, an) = |
| 323 |
withTmpAvail(fn tmpR => realOp(operand(tmpR, opnd)), an) |
| 324 |
in |
| 325 |
case instr |
| 326 |
of I.JMP(I.Direct _, labs) => done(I.JMP(spillLoc, labs), an) |
| 327 |
| I.JMP(opnd, labs) => withTmp(fn t => I.JMP(operand(t, opnd), labs), an) |
| 328 |
| I.JCC{opnd=I.Direct _, cond} => done(I.JCC{opnd=spillLoc, cond=cond}, an) |
| 329 |
| I.JCC{opnd, cond} => |
| 330 |
withTmp(fn t => I.JCC{opnd=operand(t,opnd), cond=cond}, an) |
| 331 |
| I.CALL{opnd, defs, uses, return, cutsTo, mem, pops} => |
| 332 |
withTmp(fn t => |
| 333 |
I.CALL{opnd=operand(t, opnd), defs=defs, return=return,pops=pops, |
| 334 |
uses=C.rmvReg(reg, uses), cutsTo=cutsTo, mem=mem}, an) |
| 335 |
| I.MOVE{mvOp, src as I.Direct _, dst as I.Direct _} => |
| 336 |
done(I.MOVE{mvOp=mvOp, src=replace src, dst=dst},an) |
| 337 |
| I.MOVE{mvOp, src, dst as I.Direct _} => |
| 338 |
withTmpAvail(fn t =>I.MOVE{mvOp=mvOp, src=operand(t, src), dst=dst},an) |
| 339 |
| I.MOVE{mvOp, src as I.Direct _, dst} => |
| 340 |
if Props.eqOpn(dst, spillLoc) then {code=[], proh=[], newReg=NONE} |
| 341 |
else withTmpAvail (* dst is not the spill reg *) |
| 342 |
(fn t => |
| 343 |
I.MOVE{mvOp=mvOp, src=operand(t, src), dst=operand(t, dst)}, an) |
| 344 |
| I.MOVE{mvOp, src, dst} => |
| 345 |
withTmpAvail (* dst is not the spill reg *) |
| 346 |
(fn t => |
| 347 |
I.MOVE{mvOp=mvOp, src=operand(t, src), dst=operand(t, dst)}, an) |
| 348 |
| I.LEA{r32, addr} => |
| 349 |
withTmpAvail(fn tmpR => I.LEA{r32=r32, addr=operand(tmpR, addr)}, an) |
| 350 |
| I.CMPL{lsrc, rsrc} => reloadCmp(I.CMPL, lsrc, rsrc, an) |
| 351 |
| I.CMPW{lsrc, rsrc} => reloadCmp(I.CMPW, lsrc, rsrc, an) |
| 352 |
| I.CMPB{lsrc, rsrc} => reloadCmp(I.CMPB, lsrc, rsrc, an) |
| 353 |
| I.TESTL{lsrc, rsrc} => reloadTest(I.TESTL, lsrc, rsrc, an) |
| 354 |
| I.TESTW{lsrc, rsrc} => reloadTest(I.TESTW, lsrc, rsrc, an) |
| 355 |
| I.TESTB{lsrc, rsrc} => reloadTest(I.TESTB, lsrc, rsrc, an) |
| 356 |
| I.BITOP{bitOp,lsrc, rsrc} => reloadBT(bitOp, lsrc, rsrc, an) |
| 357 |
| I.BINARY{binOp, src, dst as I.Direct _} => |
| 358 |
(case src of |
| 359 |
I.Direct _ => |
| 360 |
done(I.BINARY{binOp=binOp, src=replace src, dst=dst},an) |
| 361 |
| _ => withTmp(fn tmpR => |
| 362 |
I.BINARY{binOp=binOp, src=operand(tmpR, src), dst=dst}, an) |
| 363 |
) |
| 364 |
| I.BINARY{binOp, src, dst} => |
| 365 |
withTmp(fn tmpR => I.BINARY{binOp=binOp, src=operand(tmpR, src), |
| 366 |
dst=operand(tmpR, dst)}, an) |
| 367 |
| I.CMOV{cond, src, dst} => |
| 368 |
if CB.sameColor(dst,reg) then |
| 369 |
error "CMOV" |
| 370 |
else |
| 371 |
done(I.CMOV{cond=cond, src=spillLoc, dst=dst}, an) |
| 372 |
| I.SHIFT{shiftOp, count, src, dst} => error "go and implement SHIFT" |
| 373 |
| I.CMPXCHG{lock,sz,src,dst} => |
| 374 |
withTmp(fn tmpR => I.CMPXCHG{lock=lock, sz=sz, |
| 375 |
src=operand(tmpR, src), |
| 376 |
dst=operand(tmpR, dst)},an) |
| 377 |
| I.MULTDIV{multDivOp, src as I.Direct _} => |
| 378 |
done(I.MULTDIV{multDivOp=multDivOp, src=replace src}, an) |
| 379 |
| I.MULTDIV{multDivOp, src} => |
| 380 |
withTmp(fn tmpR => |
| 381 |
I.MULTDIV{multDivOp=multDivOp, src=operand(tmpR, src)}, an) |
| 382 |
| I.MUL3{src1, src2, dst} => |
| 383 |
withTmp(fn tmpR => |
| 384 |
I.MUL3{src1=operand(tmpR, src1), src2=src2, |
| 385 |
dst=if CB.sameColor(dst,reg) |
| 386 |
then error "reload:MUL3" else dst}, an) |
| 387 |
| I.UNARY{unOp, opnd} => |
| 388 |
withTmpAvail |
| 389 |
(fn tmpR => I.UNARY{unOp=unOp, opnd=operand(tmpR, opnd)}, an) |
| 390 |
| I.SET{cond, opnd} => |
| 391 |
withTmpAvail(fn tmpR => I.SET{cond=cond, opnd=operand(tmpR, opnd)}, an) |
| 392 |
| I.PUSHL arg => reloadPush(I.PUSHL, arg, an) |
| 393 |
| I.PUSHW arg => reloadPush(I.PUSHW, arg, an) |
| 394 |
| I.PUSHB arg => reloadPush(I.PUSHB, arg, an) |
| 395 |
| I.FILD opnd => reloadReal(I.FILD, opnd, an) |
| 396 |
| I.FILDL opnd => reloadReal(I.FILDL, opnd, an) |
| 397 |
| I.FILDLL opnd => reloadReal(I.FILDLL, opnd, an) |
| 398 |
| I.FLDT opnd => reloadReal(I.FLDT, opnd, an) |
| 399 |
| I.FLDL opnd => reloadReal(I.FLDL, opnd, an) |
| 400 |
| I.FLDS opnd => reloadReal(I.FLDS, opnd, an) |
| 401 |
| I.FSTPT opnd => reloadReal(I.FSTPT, opnd, an) |
| 402 |
| I.FSTPL opnd => reloadReal(I.FSTPL, opnd, an) |
| 403 |
| I.FSTPS opnd => reloadReal(I.FSTPS, opnd, an) |
| 404 |
| I.FSTL opnd => reloadReal(I.FSTL, opnd, an) |
| 405 |
| I.FSTS opnd => reloadReal(I.FSTS, opnd, an) |
| 406 |
| I.FUCOM opnd => reloadReal(I.FUCOM, opnd, an) |
| 407 |
| I.FUCOMP opnd => reloadReal(I.FUCOMP, opnd, an) |
| 408 |
| I.FCOMI opnd => reloadReal(I.FCOMI, opnd, an) |
| 409 |
| I.FCOMIP opnd => reloadReal(I.FCOMIP, opnd, an) |
| 410 |
| I.FUCOMI opnd => reloadReal(I.FUCOMI, opnd, an) |
| 411 |
| I.FUCOMIP opnd => reloadReal(I.FUCOMIP, opnd, an) |
| 412 |
| I.FENV{fenvOp, opnd} => reloadReal(fn opnd => |
| 413 |
I.FENV{fenvOp=fenvOp,opnd=opnd}, opnd, an) |
| 414 |
| I.FBINARY{binOp, src, dst} => |
| 415 |
withTmpAvail(fn tmpR => |
| 416 |
I.FBINARY{binOp=binOp, src=operand(tmpR, src), dst=dst}, an) |
| 417 |
| I.FIBINARY{binOp, src} => |
| 418 |
withTmpAvail |
| 419 |
(fn tmpR => I.FIBINARY{binOp=binOp, src=operand(tmpR, src)}, an) |
| 420 |
|
| 421 |
(* Pseudo fp instrctions *) |
| 422 |
| I.FMOVE{fsize,src,dst} => |
| 423 |
withTmpAvail |
| 424 |
(fn tmpR => I.FMOVE{fsize=fsize, src=operand(tmpR, src), |
| 425 |
dst=operand(tmpR, dst)}, an) |
| 426 |
| I.FILOAD{isize,ea,dst} => |
| 427 |
withTmpAvail |
| 428 |
(fn tmpR => I.FILOAD{isize=isize, ea=operand(tmpR, ea), |
| 429 |
dst=operand(tmpR, dst)}, an) |
| 430 |
| I.FBINOP{fsize,binOp,lsrc,rsrc,dst} => |
| 431 |
withTmpAvail(fn tmpR => |
| 432 |
I.FBINOP{fsize=fsize, binOp=binOp, lsrc=operand(tmpR, lsrc), |
| 433 |
rsrc=operand(tmpR, rsrc), dst=operand(tmpR, dst)}, an) |
| 434 |
| I.FIBINOP{isize,binOp,lsrc,rsrc,dst} => |
| 435 |
withTmpAvail(fn tmpR => |
| 436 |
I.FIBINOP{isize=isize, binOp=binOp, lsrc=operand(tmpR, lsrc), |
| 437 |
rsrc=operand(tmpR, rsrc), dst=operand(tmpR, dst)}, an) |
| 438 |
| I.FUNOP{fsize,unOp,src,dst} => |
| 439 |
withTmpAvail(fn tmpR => |
| 440 |
I.FUNOP{fsize=fsize, unOp=unOp, src=operand(tmpR, src), |
| 441 |
dst=operand(tmpR, dst)}, an) |
| 442 |
| I.FCMP{i,fsize,lsrc,rsrc} => |
| 443 |
withTmpAvail(fn tmpR => |
| 444 |
I.FCMP{i=i,fsize=fsize, |
| 445 |
lsrc=operand(tmpR, lsrc), rsrc=operand(tmpR, rsrc) |
| 446 |
}, an) |
| 447 |
|
| 448 |
| _ => error "reload" |
| 449 |
end (*x86Reload*) |
| 450 |
|
| 451 |
fun f(I.ANNOTATION{a, i}, an) = f(i, a::an) |
| 452 |
| f(I.INSTR i, an) = x86Reload(i, reg, spillLoc, an) |
| 453 |
| f(I.LIVE lk, an) = |
| 454 |
{code=[annotate(I.LIVE(rLiveKill (lk, reg)), an)], |
| 455 |
proh=[], |
| 456 |
newReg=NONE} |
| 457 |
| f _ = error "reload: f" |
| 458 |
in f(instr, []) |
| 459 |
end (* reload *) |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
fun spillF(instr, reg, spillLoc) = let |
| 465 |
fun x86Fspill(instr, reg, spillLoc, an) = let |
| 466 |
fun withTmp(f, fsize, an) = let |
| 467 |
val tmpR = C.newFreg() |
| 468 |
val tmp = I.FPR tmpR |
| 469 |
in |
| 470 |
{ proh=[tmpR], |
| 471 |
code=[mark(f tmp, an), |
| 472 |
I.fmove{fsize=fsize, src=tmp, dst=spillLoc}], |
| 473 |
newReg=SOME tmpR (* XXX Should we propagate the definition? *) |
| 474 |
} |
| 475 |
end |
| 476 |
in |
| 477 |
case instr |
| 478 |
of I.FSTPL _ => {proh=[], code=[mark(I.FSTPL spillLoc, an)], newReg=NONE} |
| 479 |
| I.FSTPS _ => {proh=[], code=[mark(I.FSTPS spillLoc, an)], newReg=NONE} |
| 480 |
| I.FSTPT _ => {proh=[], code=[mark(I.FSTPT spillLoc, an)], newReg=NONE} |
| 481 |
| I.FSTL _ => {proh=[], code=[mark(I.FSTL spillLoc, an)], newReg=NONE} |
| 482 |
| I.FSTS _ => {proh=[], code=[mark(I.FSTS spillLoc, an)], newReg=NONE} |
| 483 |
| I.CALL{opnd, defs, uses, return, cutsTo, mem, pops} => |
| 484 |
{proh=[], |
| 485 |
code=[mark(I.CALL{opnd=opnd, defs=C.rmvFreg(reg,defs), |
| 486 |
return=return, uses=uses, |
| 487 |
cutsTo=cutsTo, mem=mem, pops=pops}, an)], |
| 488 |
newReg=NONE} |
| 489 |
|
| 490 |
(* Pseudo fp instrctions *) |
| 491 |
| I.FMOVE{fsize,src,dst} => |
| 492 |
if Props.eqOpn(src,spillLoc) then |
| 493 |
{proh=[], code=[], newReg=NONE} |
| 494 |
else |
| 495 |
{proh=[],code=[mark(I.FMOVE{fsize=fsize,src=src,dst=spillLoc},an)], |
| 496 |
newReg=NONE} |
| 497 |
| I.FILOAD{isize,ea,dst} => |
| 498 |
{proh=[],code=[mark(I.FILOAD{isize=isize,ea=ea,dst=spillLoc},an)], |
| 499 |
newReg=NONE} (* XXX bad for single precision *) |
| 500 |
| I.FBINOP{fsize as I.FP64,binOp,lsrc,rsrc,dst} => |
| 501 |
{proh=[],code=[mark(I.FBINOP{fsize=fsize,binOp=binOp, |
| 502 |
lsrc=lsrc, rsrc=rsrc, |
| 503 |
dst=spillLoc},an)], |
| 504 |
newReg=NONE} |
| 505 |
| I.FBINOP{fsize,binOp,lsrc,rsrc,dst} => |
| 506 |
withTmp(fn tmpR => |
| 507 |
I.FBINOP{fsize=fsize, binOp=binOp, |
| 508 |
lsrc=lsrc, rsrc=rsrc, dst=tmpR}, |
| 509 |
fsize, an) |
| 510 |
| I.FIBINOP{isize,binOp,lsrc,rsrc,dst} => |
| 511 |
withTmp(fn tmpR => |
| 512 |
I.FIBINOP{isize=isize, binOp=binOp, |
| 513 |
lsrc=lsrc, rsrc=rsrc, dst=tmpR}, |
| 514 |
I.FP64, an) (* XXX *) |
| 515 |
| I.FUNOP{fsize,unOp,src,dst} => |
| 516 |
{proh=[],code=[mark(I.FUNOP{fsize=fsize,unOp=unOp, |
| 517 |
src=src,dst=spillLoc},an)], |
| 518 |
newReg=NONE} |
| 519 |
| _ => error "fspill" |
| 520 |
(*esac*) |
| 521 |
end (* x86Fspill *) |
| 522 |
fun f(I.ANNOTATION{a,i}, an) = f(i, a::an) |
| 523 |
| f(I.INSTR(i), an) = x86Fspill(i, reg, spillLoc, an) |
| 524 |
| f(I.KILL lk, an) = |
| 525 |
{code=[annotate(I.KILL(fLiveKill (lk, reg)), an)], |
| 526 |
proh=[], |
| 527 |
newReg=NONE} |
| 528 |
| f _ = error "fspill:f" |
| 529 |
in f(instr, []) |
| 530 |
end |
| 531 |
|
| 532 |
|
| 533 |
fun reloadF(instr, reg, spillLoc) = let |
| 534 |
fun x86Freload(instr, reg, spillLoc, an) = let |
| 535 |
fun rename(src as I.FDirect f) = |
| 536 |
if CB.sameColor(f,reg) then spillLoc else src |
| 537 |
| rename(src as I.FPR f) = |
| 538 |
if CB.sameColor(f,reg) then spillLoc else src |
| 539 |
| rename src = src |
| 540 |
|
| 541 |
fun withTmp(fsize, f, an) = |
| 542 |
case spillLoc of |
| 543 |
I.FDirect _ => {newReg=NONE, proh=[], code=[mark(f spillLoc, an)]} |
| 544 |
| I.FPR _ => {newReg=NONE, proh=[], code=[mark(f spillLoc, an)]} |
| 545 |
| _ => |
| 546 |
let val ftmpR = C.newFreg() |
| 547 |
val ftmp = I.FPR(ftmpR) |
| 548 |
in {newReg=NONE, |
| 549 |
proh=[ftmpR], |
| 550 |
code=[I.fmove{fsize=fsize, src=spillLoc, dst=ftmp}, |
| 551 |
mark(f ftmp, an) |
| 552 |
] |
| 553 |
} |
| 554 |
end |
| 555 |
in |
| 556 |
(case instr of |
| 557 |
I.FLDT opnd => {code=[mark(I.FLDT spillLoc, an)], proh=[], newReg=NONE} |
| 558 |
| I.FLDL opnd => {code=[mark(I.FLDL spillLoc, an)], proh=[], newReg=NONE} |
| 559 |
| I.FLDS opnd => {code=[mark(I.FLDS spillLoc, an)], proh=[], newReg=NONE} |
| 560 |
| I.FUCOM opnd => {code=[mark(I.FUCOM spillLoc, an)],proh=[],newReg=NONE} |
| 561 |
| I.FUCOMP opnd => {code=[mark(I.FUCOMP spillLoc, an)],proh=[],newReg=NONE} |
| 562 |
| I.FCOMI opnd => {code=[mark(I.FCOMI spillLoc, an)],proh=[],newReg=NONE} |
| 563 |
| I.FCOMIP opnd => {code=[mark(I.FCOMIP spillLoc, an)],proh=[],newReg=NONE} |
| 564 |
| I.FUCOMI opnd => {code=[mark(I.FUCOMI spillLoc, an)],proh=[],newReg=NONE} |
| 565 |
| I.FUCOMIP opnd => {code=[mark(I.FUCOMIP spillLoc, an)],proh=[],newReg=NONE} |
| 566 |
| I.FBINARY{binOp, src=I.FDirect f, dst} => |
| 567 |
if CB.sameColor(f,reg) then |
| 568 |
{code=[mark(I.FBINARY{binOp=binOp, src=spillLoc, dst=dst}, an)], |
| 569 |
proh=[], |
| 570 |
newReg=NONE} |
| 571 |
else error "reloadF:FBINARY" |
| 572 |
|
| 573 |
(* Pseudo fp instructions. |
| 574 |
*) |
| 575 |
| I.FMOVE{fsize,src,dst} => |
| 576 |
if Props.eqOpn(dst,spillLoc) then |
| 577 |
{code=[], proh=[], newReg=NONE} |
| 578 |
else |
| 579 |
{code=[mark(I.FMOVE{fsize=fsize,src=spillLoc,dst=dst},an)], |
| 580 |
proh=[], newReg=NONE} |
| 581 |
| I.FBINOP{fsize,binOp,lsrc,rsrc,dst} => |
| 582 |
{code=[mark(I.FBINOP{fsize=fsize,binOp=binOp, |
| 583 |
lsrc=rename lsrc, rsrc=rename rsrc,dst=dst},an)], |
| 584 |
proh=[], newReg=NONE} |
| 585 |
| I.FIBINOP{isize,binOp,lsrc,rsrc,dst} => |
| 586 |
{code=[mark(I.FIBINOP{isize=isize,binOp=binOp, |
| 587 |
lsrc=rename lsrc,rsrc=rename rsrc,dst=dst},an)], |
| 588 |
proh=[], newReg=NONE} |
| 589 |
| I.FUNOP{fsize,unOp,src,dst} => |
| 590 |
{code=[mark(I.FUNOP{fsize=fsize,unOp=unOp, |
| 591 |
src=rename src, dst=dst},an)], |
| 592 |
proh=[], newReg=NONE} |
| 593 |
| I.FCMP{i,fsize,lsrc,rsrc} => |
| 594 |
(* Make sure that both the lsrc and rsrc cannot be in memory *) |
| 595 |
(case (lsrc, rsrc) of |
| 596 |
(I.FPR fs1, I.FPR fs2) => |
| 597 |
(case (CB.sameColor(fs1,reg), CB.sameColor(fs2,reg)) of |
| 598 |
(true, true) => |
| 599 |
withTmp(fsize, |
| 600 |
fn tmp => I.FCMP{i=i,fsize=fsize,lsrc=tmp, rsrc=tmp}, an) |
| 601 |
| (true, false) => |
| 602 |
{code=[mark(I.FCMP{i=i,fsize=fsize,lsrc=spillLoc,rsrc=rsrc},an)], |
| 603 |
proh=[], newReg=NONE} |
| 604 |
| (false, true) => |
| 605 |
{code=[mark(I.FCMP{i=i,fsize=fsize,lsrc=lsrc,rsrc=spillLoc},an)], |
| 606 |
proh=[], newReg=NONE} |
| 607 |
| _ => error "fcmp.1" |
| 608 |
) |
| 609 |
| (I.FPR _, _) => |
| 610 |
withTmp(fsize, |
| 611 |
fn tmp => I.FCMP{i=i,fsize=fsize,lsrc=tmp, rsrc=rsrc}, an) |
| 612 |
| (_, I.FPR _) => |
| 613 |
withTmp(fsize, |
| 614 |
fn tmp => I.FCMP{i=i,fsize=fsize,lsrc=lsrc, rsrc=tmp}, an) |
| 615 |
| _ => error "fcmp.2" |
| 616 |
) |
| 617 |
| I.CALL{opnd, defs, uses, return, cutsTo, mem, pops} => |
| 618 |
{proh=[], |
| 619 |
code=[mark(I.CALL{opnd=opnd, defs=C.rmvFreg(reg,defs), |
| 620 |
return=return, pops=pops, |
| 621 |
uses=uses, cutsTo=cutsTo, mem=mem}, an)], |
| 622 |
newReg=NONE} |
| 623 |
| _ => error "reloadF" |
| 624 |
(*esac*)) |
| 625 |
end (* x86Freload *) |
| 626 |
|
| 627 |
fun f(I.ANNOTATION{a, i}, an) = f(i, a::an) |
| 628 |
| f(I.INSTR i, an) = x86Freload(i, reg, spillLoc, an) |
| 629 |
| f(I.LIVE lk, an) = |
| 630 |
{code=[annotate(I.LIVE(fLiveKill (lk, reg)), an)], |
| 631 |
proh=[], |
| 632 |
newReg=NONE} |
| 633 |
| f _ = error "freload.f" |
| 634 |
|
| 635 |
in f(instr, []) |
| 636 |
end |
| 637 |
|
| 638 |
fun spillToEA CB.GP (reg, ea) = let |
| 639 |
fun returnMove() = |
| 640 |
{code=[I.move{mvOp=I.MOVL, src=I.Direct reg, dst=ea}], |
| 641 |
proh=[], newReg=NONE} |
| 642 |
in |
| 643 |
case ea |
| 644 |
of I.MemReg _ => returnMove() |
| 645 |
| I.Displace _ => returnMove() |
| 646 |
| I.Indexed _ => returnMove() |
| 647 |
| _ => error "spillToEA: GP" |
| 648 |
end |
| 649 |
| spillToEA CB.FP (freg, ea) = error "spillToEA: FP" |
| 650 |
| spillToEA _ _ = error "spillToEA" |
| 651 |
|
| 652 |
fun reloadFromEA CB.GP (reg, ea) = let |
| 653 |
fun returnMove() = |
| 654 |
{code=[I.move{mvOp=I.MOVL, dst=I.Direct reg, src=ea}], |
| 655 |
proh=[], |
| 656 |
newReg=NONE} |
| 657 |
in |
| 658 |
case ea |
| 659 |
of I.MemReg _ => returnMove() |
| 660 |
| I.Displace _ => returnMove() |
| 661 |
| I.Indexed _ => returnMove() |
| 662 |
| _ => error "reloadFromEA: GP" |
| 663 |
end |
| 664 |
| reloadFromEA CB.FP (freg, ea) = error "spillToEA: FP" |
| 665 |
| reloadFromEA _ _ = error "spillToEA" |
| 666 |
|
| 667 |
|
| 668 |
fun reload CB.GP = reloadR |
| 669 |
| reload CB.FP = reloadF |
| 670 |
| reload _ = error "reload" |
| 671 |
|
| 672 |
fun spill CB.GP = spillR |
| 673 |
| spill CB.FP = spillF |
| 674 |
| spill _ = error "spill" |
| 675 |
end |