-(* L1 Compiler
- * Gathers tiberium, fires rockets
+(* L3 Compiler
* Takes a list of mappings of temporaries to colors and a pseudoasm listing,
* then produces x86 code.
+ * Author: Chris Lu <czl@andrew.cmu.edu>
* Author: Joshua Wise <jwise@andrew.cmu.edu>
*)
fun solidify (regmap : colorings) (instrs : asm) : asm =
let
(* r14d and r15d is reserved for spilling *)
- val maxreg = X.regtonum X.R13D
+ val maxreg = X.regtonum X.R14D
fun numtoreg n =
if (n > maxreg)
then raise Spilled
fun temptonum (t: T.temp) : int =
(List.hd
(List.map (fn (_, n) => n)
- (List.filter (fn (a, _) => (Temp.compare (a, t) = EQUAL)) regmap)))
+ (List.filter (fn (a, _) => (Temp.eq (a, t))) regmap)))
fun temptoreg (t: T.temp) : x86.reg =
numtoreg (temptonum t)
- handle Empty =>
- (let
- val () = print (" Uncolored temp "^(Temp.name t)^" -- dead code?\n")
- in
- X.R15D (*If we don't care about the output, then it is cool to explode this; R15D is guaranteed not to be used across builtin blocks.*)
- end)
-
- val spillreg1 = X.R14D
- val spillreg2 = X.R15D
-
- val prologue = [X.DIRECTIVE "\tpush %rbx\n\tpush %r12\n\tpush %r13\n\tpush %r14\n\tpush %r15"] (* Could be done better. *)
- val epilogue = [X.DIRECTIVE "\tpop %r15\n\tpop %r14\n\tpop %r13\n\tpop %r12\n\tpop %rbx"]
-
+ handle Empty => raise ErrorMsg.InternalError ("Uncolored temp "^(Temp.name t)^", agh!")
+
+ val spillreg1 = X.R15D
+
+ (* Determine which need to be saved. *)
+ val opsused = map (fn (_, n) => X.REG (numtoreg n handle Spilled => X.R15D)) regmap
+ val saveregs = X.OperSet.intersection (
+ X.OperSet.addList (X.OperSet.empty, opsused),
+ X.OperSet.addList (
+ X.OperSet.empty,
+ [X.REG X.EBX,
+ X.REG X.EBP,
+ X.REG X.R12D,
+ X.REG X.R13D,
+ X.REG X.R14D,
+ X.REG X.R15D]))
+ val savelist = X.OperSet.listItems saveregs
+ val nsave = length savelist
+
val numreg = foldr (Int.max) 0 (map (fn (_, n) => n) regmap) (* Number of registers used. *)
val nspilled = Int.max (numreg - maxreg, 0) (* Number of spilled registers. *)
fun isspilled (X.TEMP temp) = (((temptonum temp) > maxreg) handle Empty => false) (* Whether a register is spilled *)
+ | isspilled (X.STACKARG _) = true
+ | isspilled (X.REL _) = true
| isspilled _ = false
- fun stackpos (reg: int) = (reg - maxreg) * ~4 (* Stack position of some register number *)
-
+ val stacksz = (nspilled + nsave) * 8
+ fun stackpos (reg: int) = stacksz - (reg - maxreg + nsave) * 8 (* Stack position of some register number *)
+
+ val prologue =
+ (X.SIZE (X.Qword, X.SUB (X.REG X.RSP, X.CONST (Word32.fromInt stacksz)))) ::
+ (ListPair.map
+ (fn (num, reg) =>
+ X.SIZE (X.Qword, X.MOV (X.REL (X.RSP, stacksz - 8*(num+1)), reg)))
+ (List.tabulate (nsave, fn x => x), savelist))
+ val epilogue =
+ (ListPair.map
+ (fn (num, reg) =>
+ X.SIZE (X.Qword, X.MOV (reg, X.REL (X.RSP, stacksz - 8*(num+1)))))
+ (List.tabulate (nsave, fn x => x), savelist)) @
+ [X.SIZE (X.Qword, X.ADD (X.REG X.RSP, X.CONST (Word32.fromInt stacksz)))]
+ val endlbl = Label.new()
+
fun spill (X.TEMP temp, xreg: x86.reg) = (* Spill a register if need be. *)
if (isspilled (X.TEMP temp))
- then [X.MOVL (X.REL (X.RSP, stackpos (temptonum temp)), X.REG xreg)]
+ then [X.MOV (X.REL (X.RSP, stackpos (temptonum temp)), X.REG xreg)]
else nil
+ | spill (X.STACKARG _, _) = raise ErrorMsg.InternalError "Cannot spill to a stack arg"
+ | spill (a as X.REL _, xreg) = [X.MOV (a, X.REG xreg)]
| spill _ = nil (* Nothing else can be spilled. *)
fun unspill (X.TEMP temp, xreg: x86.reg) = (* Unspill a register if need be. *)
if (isspilled (X.TEMP temp))
- then [X.MOVL (X.REG xreg, X.REL (X.RSP, stackpos (temptonum temp)))]
+ then [X.MOV (X.REG xreg, X.REL (X.RSP, stackpos (temptonum temp)))]
else nil
+ | unspill (X.STACKARG arg, xreg) = [X.MOV (X.REG xreg, X.REL (X.RSP, stacksz + 8 + (arg * 8)))]
+ | unspill (a as X.REL _, xreg) = [X.MOV (X.REG xreg, a)]
| unspill _ = nil
fun realoper (X.TEMP temp) = X.REG (temptoreg temp) (* Makes a operand 'real'. *)
+ | realoper (X.STACKARG arg) = raise Spilled
+ | realoper (X.REL _) = raise Spilled
| realoper r = r
+
fun stackoper (X.TEMP temp) =
if not (isspilled (X.TEMP temp)) then raise ErrorMsg.InternalError "stackoper on unspilled temp?"
else X.REL (X.RSP, stackpos (temptonum temp))
+ | stackoper (X.STACKARG arg) = X.REL (X.RSP, stacksz + 8 + (arg * 8))
+ | stackoper (a as X.REL _) = a
| stackoper _ = raise ErrorMsg.InternalError "stackoper on not temp?"
fun transform (X.DIRECTIVE s) = [X.DIRECTIVE s]
| transform (X.COMMENT s) = [X.COMMENT s]
- | transform (X.MOVL (dest, src)) =
+ | transform (X.LIVEIGN a) = transform a
+ | transform (X.SIZE (s, i)) = map (fn i' => (X.SIZE (s, i'))) (transform i)
+ | transform (X.MOV (dest, src)) =
if (isspilled dest)
then
unspill (src, spillreg1) @
- [ X.MOVL(
+ [X.MOV(
realoper dest handle Spilled => stackoper dest,
realoper src handle Spilled => X.REG spillreg1)]
else
- [ X.MOVL(
+ [X.MOV(
realoper dest handle Spilled => raise ErrorMsg.InternalError "But we said that wasn't spilled?",
realoper src handle Spilled => stackoper src)]
- | transform (X.SUBL (dest, src)) =
+ | transform (X.SUB (dest, src)) =
unspill (src, spillreg1) @
- unspill (dest, spillreg2) @
- [ X.SUBL(
- realoper dest handle Spilled => X.REG spillreg2,
- realoper src handle Spilled => X.REG spillreg1)] @
- spill (dest, spillreg2)
+ [ X.SUB(
+ realoper dest handle Spilled => stackoper dest,
+ realoper src handle Spilled => X.REG spillreg1)]
| transform (X.IMUL (dest, src)) =
unspill (dest, spillreg1) @
[ X.IMUL(
realoper src handle Spilled => stackoper src,
const)] @
spill (dest, spillreg1)
- | transform (X.ADDL (dest, src)) = (* You can have either operand spilled, but not both. Pick one. *)
+ | transform (X.ADD (dest, src)) = (* You can have either operand spilled, but not both. Pick one. *)
if (isspilled dest)
then
unspill (src, spillreg1) @
- [ X.ADDL(
+ [ X.ADD(
realoper dest handle Spilled => stackoper dest,
realoper src handle Spilled => X.REG spillreg1)]
else
- [ X.ADDL(
+ [ X.ADD(
realoper dest handle Spilled => raise ErrorMsg.InternalError "But we said that wasn't spilled?",
realoper src handle Spilled => stackoper src)]
- | transform (X.LEAL (dest, src1, src2)) =
- unspill (src1, spillreg1) @
- unspill (src2, spillreg2) @
- [ X.LEAL(
- realoper dest handle Spilled => X.REG spillreg1,
- realoper src1 handle Spilled => X.REG spillreg1,
- realoper src2 handle Spilled => X.REG spillreg2)] @
- spill (dest, spillreg1)
- | transform (X.IDIVL (src)) = [ X.IDIVL(realoper src handle Spilled => stackoper src)]
+ | transform (X.IDIV (src)) = [ X.IDIV(realoper src handle Spilled => stackoper src)]
| transform (X.NEG (src)) = [ X.NEG(realoper src handle Spilled => stackoper src)]
+ | transform (X.NOT (src)) = [ X.NOT(realoper src handle Spilled => stackoper src)]
+ | transform (X.SAL (dest, shft)) =
+ [ X.SAL (
+ realoper dest handle Spilled => stackoper dest,
+ shft)]
+ | transform (X.SAR (dest, shft)) =
+ [ X.SAR (
+ realoper dest handle Spilled => stackoper dest,
+ shft)]
| transform (X.CLTD) = [ X.CLTD ]
- | transform (X.RET) = epilogue @ [X.RET]
-(* | transform _ = raise ErrorMsg.InternalError ("Unimplemented transform")*)
+ | transform (X.AND (dest, src)) =
+ unspill (src, spillreg1) @
+ [ X.AND(
+ realoper dest handle Spilled => stackoper dest,
+ realoper src handle Spilled => X.REG spillreg1)]
+ | transform (X.OR (dest, src)) =
+ unspill (src, spillreg1) @
+ [ X.OR(
+ realoper dest handle Spilled => stackoper dest,
+ realoper src handle Spilled => X.REG spillreg1)]
+ | transform (X.XOR (dest, src)) =
+ unspill (src, spillreg1) @
+ [ X.XOR(
+ realoper dest handle Spilled => stackoper dest,
+ realoper src handle Spilled => X.REG spillreg1)]
+ | transform (X.CMP (op1, op2)) =
+ unspill (op2, spillreg1) @
+ [ X.CMP(
+ realoper op1 handle Spilled => stackoper op1,
+ realoper op2 handle Spilled => X.REG spillreg1)]
+ | transform (X.TEST (op1, op2)) =
+ unspill (op2, spillreg1) @
+ [ X.TEST(
+ realoper op1 handle Spilled => stackoper op1,
+ realoper op2 handle Spilled => X.REG spillreg1)]
+ | transform (X.SETcc (c,src)) = [ X.SETcc(c, realoper src handle Spilled => stackoper src)]
+ | transform (X.CALL l) = [ X.CALL l ]
+ | transform (X.MOVZB (dest, src)) =
+ [ X.MOVZB(
+ realoper dest handle Spilled => X.REG spillreg1,
+ realoper src handle Spilled => stackoper src)]
+ @ spill (dest, spillreg1)
+ | transform (X.RET) = if nsave < 2 then (epilogue @ [X.RET]) else [X.JMP endlbl]
+ | transform (X.LABEL l) = [ X.LABEL l ]
+ | transform (X.JMP l) = [ X.JMP l ]
+ | transform (X.Jcc (c,l)) = [X.Jcc (c,l)]
in
- List.concat (prologue :: (map transform instrs))
-
+ if (nsave < 2) then
+ List.concat (prologue :: (map transform instrs))
+ else
+ List.concat (prologue :: ((map transform instrs) @ [[X.LABEL endlbl], epilogue, [X.RET]]))
end
end