-(* L2 Compiler
+(* 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>
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 =>
- (ErrorMsg.warn NONE ("Uncolored temp "^(Temp.name t)^" -- dead code?") ;
- 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.*)
-
- val spillreg1 = X.R14D
-
- 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) @
- [ X.SUBL(
+ [ X.SUB(
realoper dest handle Spilled => stackoper dest,
realoper src handle Spilled => X.REG spillreg1)]
| transform (X.IMUL (dest, src)) =
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.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.NOTL (src)) = [ X.NOTL(realoper src handle Spilled => stackoper src)]
- | transform (X.SALL (dest, shft)) =
- [ X.SALL (
+ | 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.SARL (dest, shft)) =
- [ X.SARL (
+ | transform (X.SAR (dest, shft)) =
+ [ X.SAR (
realoper dest handle Spilled => stackoper dest,
shft)]
| transform (X.CLTD) = [ X.CLTD ]
- | transform (X.ANDL (dest, src)) =
+ | transform (X.AND (dest, src)) =
unspill (src, spillreg1) @
- [ X.ANDL(
+ [ X.AND(
realoper dest handle Spilled => stackoper dest,
realoper src handle Spilled => X.REG spillreg1)]
- | transform (X.ORL (dest, src)) =
+ | transform (X.OR (dest, src)) =
unspill (src, spillreg1) @
- [ X.ORL(
+ [ X.OR(
realoper dest handle Spilled => stackoper dest,
realoper src handle Spilled => X.REG spillreg1)]
- | transform (X.XORL (dest, src)) =
+ | transform (X.XOR (dest, src)) =
unspill (src, spillreg1) @
- [ X.XORL(
+ [ X.XOR(
realoper dest handle Spilled => stackoper dest,
realoper src handle Spilled => X.REG spillreg1)]
- | transform (X.CMPL (op1, op2)) =
+ | transform (X.CMP (op1, op2)) =
unspill (op2, spillreg1) @
- [ X.CMPL(
+ [ X.CMP(
realoper op1 handle Spilled => stackoper op1,
realoper op2 handle Spilled => X.REG spillreg1)]
| transform (X.TEST (op1, op2)) =
[ X.TEST(
realoper op1 handle Spilled => stackoper op1,
realoper op2 handle Spilled => X.REG spillreg1)]
- | transform (X.SETNE (src)) = [ X.SETNE(realoper src handle Spilled => stackoper src)]
- | transform (X.SETE (src)) = [ X.SETE(realoper src handle Spilled => stackoper src)]
- | transform (X.SETLE (src)) = [ X.SETLE(realoper src handle Spilled => stackoper src)]
- | transform (X.SETL (src)) = [ X.SETL(realoper src handle Spilled => stackoper src)]
- | transform (X.SETGE (src)) = [ X.SETGE(realoper src handle Spilled => stackoper src)]
- | transform (X.SETG (src)) = [ X.SETG(realoper src handle Spilled => stackoper src)]
- | transform (X.MOVZBL (dest, src)) =
- [ X.MOVZBL(
+ | 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) = epilogue @ [X.RET]
+ | 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.JE l) = [ X.JE l]
- | transform (X.JNE l) = [ X.JNE l]
-(* | transform _ = raise ErrorMsg.InternalError ("Unimplemented transform")*)
+ | 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