2 * Gathers tiberium, fires rockets
3 * Takes a list of mappings of temporaries to colors and a pseudoasm listing,
4 * then produces x86 code.
5 * Author: Joshua Wise <jwise@andrew.cmu.edu>
10 type colorings = (Temp.temp * int) list
11 type asm = x86.insn list
13 val solidify : colorings -> asm -> asm
16 structure Solidify :> SOLIDIFY =
21 type colorings = (Temp.temp * int) list
22 type asm = x86.insn list
26 fun solidify (regmap : colorings) (instrs : asm) : asm =
28 (* r14d and r15d is reserved for spilling *)
29 val maxreg = X.regtonum X.R13D
35 fun temptonum (t: T.temp) : int =
37 (List.map (fn (_, n) => n)
38 (List.filter (fn (a, _) => (Temp.compare (a, t) = EQUAL)) regmap)))
40 fun temptoreg (t: T.temp) : x86.reg =
41 numtoreg (temptonum t)
44 val () = print (" Uncolored temp "^(Temp.name t)^" -- dead code?\n")
46 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.*)
49 val spillreg1 = X.R14D
50 val spillreg2 = X.R15D
52 val prologue = [X.DIRECTIVE "\tpush %rbx\n\tpush %r12\n\tpush %r13\n\tpush %r14\n\tpush %r15"] (* Could be done better. *)
53 val epilogue = [X.DIRECTIVE "\tpop %r15\n\tpop %r14\n\tpop %r13\n\tpop %r12\n\tpop %rbx"]
55 val numreg = foldr (Int.max) 0 (map (fn (_, n) => n) regmap) (* Number of registers used. *)
56 val nspilled = Int.max (numreg - maxreg, 0) (* Number of spilled registers. *)
57 fun isspilled (X.TEMP temp) = (((temptonum temp) > maxreg) handle Empty => false) (* Whether a register is spilled *)
59 fun stackpos (reg: int) = (reg - maxreg) * ~4 (* Stack position of some register number *)
61 fun spill (X.TEMP temp, xreg: x86.reg) = (* Spill a register if need be. *)
62 if (isspilled (X.TEMP temp))
63 then [X.MOVL (X.REL (X.RSP, stackpos (temptonum temp)), X.REG xreg)]
65 | spill _ = nil (* Nothing else can be spilled. *)
66 fun unspill (X.TEMP temp, xreg: x86.reg) = (* Unspill a register if need be. *)
67 if (isspilled (X.TEMP temp))
68 then [X.MOVL (X.REG xreg, X.REL (X.RSP, stackpos (temptonum temp)))]
72 fun realoper (X.TEMP temp) = X.REG (temptoreg temp) (* Makes a operand 'real'. *)
74 fun stackoper (X.TEMP temp) =
75 if not (isspilled (X.TEMP temp)) then raise ErrorMsg.InternalError "stackoper on unspilled temp?"
76 else X.REL (X.RSP, stackpos (temptonum temp))
77 | stackoper _ = raise ErrorMsg.InternalError "stackoper on not temp?"
79 fun transform (X.DIRECTIVE s) = [X.DIRECTIVE s]
80 | transform (X.COMMENT s) = [X.COMMENT s]
81 | transform (X.MOVL (dest, src)) =
84 unspill (src, spillreg1) @
86 realoper dest handle Spilled => stackoper dest,
87 realoper src handle Spilled => X.REG spillreg1)]
90 realoper dest handle Spilled => raise ErrorMsg.InternalError "But we said that wasn't spilled?",
91 realoper src handle Spilled => stackoper src)]
92 | transform (X.SUBL (dest, src)) =
93 unspill (src, spillreg1) @
94 unspill (dest, spillreg2) @
96 realoper dest handle Spilled => X.REG spillreg2,
97 realoper src handle Spilled => X.REG spillreg1)] @
98 spill (dest, spillreg2)
99 | transform (X.IMUL (dest, src)) =
100 unspill (dest, spillreg1) @
102 realoper dest handle Spilled => X.REG spillreg1,
103 realoper src handle Spilled => stackoper src)] @
104 spill (dest, spillreg1)
105 | transform (X.IMUL3 (dest, src, const)) =
107 realoper dest handle Spilled => X.REG spillreg1,
108 realoper src handle Spilled => stackoper src,
110 spill (dest, spillreg1)
111 | transform (X.ADDL (dest, src)) = (* You can have either operand spilled, but not both. Pick one. *)
114 unspill (src, spillreg1) @
116 realoper dest handle Spilled => stackoper dest,
117 realoper src handle Spilled => X.REG spillreg1)]
120 realoper dest handle Spilled => raise ErrorMsg.InternalError "But we said that wasn't spilled?",
121 realoper src handle Spilled => stackoper src)]
122 | transform (X.LEAL (dest, src1, src2)) =
123 unspill (src1, spillreg1) @
124 unspill (src2, spillreg2) @
126 realoper dest handle Spilled => X.REG spillreg1,
127 realoper src1 handle Spilled => X.REG spillreg1,
128 realoper src2 handle Spilled => X.REG spillreg2)] @
129 spill (dest, spillreg1)
130 | transform (X.IDIVL (src)) = [ X.IDIVL(realoper src handle Spilled => stackoper src)]
131 | transform (X.NEG (src)) = [ X.NEG(realoper src handle Spilled => stackoper src)]
132 | transform (X.CLTD) = [ X.CLTD ]
133 | transform (X.RET) = epilogue @ [X.RET]
134 (* | transform _ = raise ErrorMsg.InternalError ("Unimplemented transform")*)
136 List.concat (prologue :: (map transform instrs))