2 * Takes a list of mappings of temporaries to colors and a pseudoasm listing,
3 * then produces x86 code.
4 * Author: Chris Lu <czl@andrew.cmu.edu>
5 * Author: Joshua Wise <jwise@andrew.cmu.edu>
10 type colorings = (Temp.temp * int) list
11 type asm = Blarg.insn list
13 val solidify : colorings -> asm -> asm
16 structure Solidify :> SOLIDIFY =
21 type colorings = (Temp.temp * int) list
22 type asm = Blarg.insn list
26 structure TempMap = SplayMapFn(struct
27 type ord_key = Temp.temp
28 val compare = Temp.compare
32 fun solidify (regmap : colorings) (instrs : asm) : asm =
34 (* r11 and r12 is reserved for spilling *)
35 val maxreg = X.regtonum X.R10
41 val tempnums = List.foldr (fn ((t,n),b) => TempMap.insert(b,t,n)) (TempMap.empty) regmap
42 fun temptonum (t: T.temp) : int = valOf (TempMap.find (tempnums, t))
44 fun temptoreg (t: T.temp) : Blarg.reg =
45 numtoreg (temptonum t)
46 handle Option => raise ErrorMsg.InternalError ("Uncolored temp "^(Temp.name t)^", agh!")
51 (* Determine which need to be saved. *)
52 val opsused = (map (fn (_, n) => X.REG (numtoreg n handle Spilled => X.R12)) regmap) @ [X.REG X.R11]
53 val saveregs = X.OperSet.intersection (
54 X.OperSet.addList (X.OperSet.empty, opsused),
64 val savelist = X.OperSet.listItems saveregs
65 val nsave = length savelist
67 val numreg = foldr (Int.max) 0 (map (fn (_, n) => n) regmap) (* Number of registers used. *)
68 val nspilled = Int.max (numreg - maxreg, 0) (* Number of spilled registers. *)
69 fun isspilled (X.TEMP temp) = (temptonum temp) > maxreg (* Whether a register is spilled *)
70 | isspilled (X.STACKARG _) = true
73 val stacksz = (nspilled + nsave) * 1
74 fun stackpos (reg: int) = stacksz - (reg - maxreg + nsave) * 1 (* Stack position of some register number *)
77 [X.INSN (X.AL, X.MOVLIT (X.REG X.R4, Word.fromInt stacksz)),
78 X.INSN (X.AL, X.SUB (X.REG X.SP, X.REG X.R4))] @
82 [X.INSN (X.AL, X.MOVLIT (X.REG X.R4, Word.fromInt (stacksz - 1*(num+1)))),
83 X.INSN (X.AL, X.ADD (X.REG X.R4, X.REG X.SP)),
84 X.INSN (X.AL, X.STO (X.REG X.R4, reg))])
85 (List.tabulate (nsave, fn x => x), savelist)
92 [X.INSN (X.AL, X.MOVLIT (X.REG X.R4, Word.fromInt (stacksz - 1*(num+1)))),
93 X.INSN (X.AL, X.ADD (X.REG X.R4, X.REG X.SP)),
94 X.INSN (X.AL, X.LDR (reg, X.REG X.R4))])
95 (List.tabulate (nsave, fn x => x), savelist)
98 [X.INSN (X.AL, X.MOVLIT (X.REG X.R4, Word.fromInt stacksz)),
99 X.INSN (X.AL, X.ADD (X.REG X.SP, X.REG X.R4))]
100 val endlbl = Label.new()
102 fun spill (X.TEMP temp, xreg: Blarg.reg) = (* Spill a register if need be. *)
104 val base = X.REG X.SP
105 val offs = Word.fromInt (stackpos (temptonum temp))
107 if (isspilled (X.TEMP temp))
108 then raise ErrorMsg.InternalError "unspill not supported" (*[X.MOV ((X.REL (base, offs, 0w1), Tm.Quad), (X.REG xreg, Tm.Quad))]*)
111 | spill (X.STACKARG _, _) = raise ErrorMsg.InternalError "Cannot spill to a stack arg"
112 | spill _ = nil (* Nothing else can be spilled. *)
113 fun unspill (X.TEMP temp, xreg: Blarg.reg) = (* Unspill a register if need be. *)
115 val base = X.REG X.SP
116 val offs = Word.fromInt (stackpos (temptonum temp))
118 if (isspilled (X.TEMP temp))
119 then raise ErrorMsg.InternalError "unspill not supported" (*[X.MOV ((X.REG xreg, Tm.Quad), (X.REL (base, offs, 0w1), Tm.Quad))]*)
122 | unspill (X.STACKARG arg, xreg) =
124 val base = X.REG X.SP
125 val offs = Word.fromInt (stacksz + 8 + (arg * 8))
127 (*[X.MOV ((X.REG xreg, s), (X.REL (base, offs, 0w1), s))]*)
128 raise ErrorMsg.InternalError "unspill from stack not supported"
132 fun unspill_ops (op1, op2) =
133 case (isspilled op1, isspilled op2)
134 of (false, false) => []
135 | (true, false) => unspill (op1, spillreg1)
136 | (false, true) => unspill (op2, spillreg2)
137 | (true, true) => unspill (op1, spillreg1) @ unspill (op2, spillreg2)
139 fun respill_ops (op1, op2) =
140 case (isspilled op1, isspilled op2) (* no instruction writes back to op2 *)
142 | (true, _) => spill (op1, spillreg1)
146 of (X.TEMP temp) => if isspilled op1
147 then (X.REG spillreg1)
148 else X.REG (temptoreg temp)
149 | (X.STACKARG arg) => X.REG spillreg1
152 fun real_ops (op1, op2) =
154 of (X.TEMP temp) => if isspilled op1
155 then (X.REG spillreg1)
156 else X.REG (temptoreg temp)
157 | (X.STACKARG arg) => X.REG spillreg1
160 of (X.TEMP temp) => if isspilled op2
161 then (X.REG spillreg2)
162 else X.REG (temptoreg temp)
163 | (X.STACKARG arg) => X.REG spillreg2
166 fun whack_insn (pred, insn, op1, op2) =
167 (map (fn i => X.INSN (pred, i)) (unspill_ops (op1, op2))) @
168 [ X.INSN (pred, insn (real_ops (op1, op2))) ] @
169 (map (fn i => X.INSN (pred, i)) (respill_ops (op1, op2)))
171 fun transform (X.DIRECTIVE s) = [X.DIRECTIVE s]
172 | transform (X.COMMENT s) = [X.COMMENT s]
173 | transform (X.LABEL l) = [X.LABEL l]
174 | transform (X.LIVEIGN a) = transform a
176 (* god the special cases are going to suck *)
177 | transform (X.INSN (pred, X.MOVLIT (op1, w))) =
178 [ X.INSN (pred, X.MOVLIT (real_op1 op1, w)) ] @
180 then spill (op1, spillreg1)
182 | transform (X.INSN (pred, X.MOVSYM (op1, w))) =
183 [ X.INSN (pred, X.MOVSYM (real_op1 op1, w)) ] @
185 then spill (op1, spillreg1)
187 | transform (X.INSN (pred, X.MOVSTR (op1, w))) =
188 [ X.INSN (pred, X.MOVSTR (real_op1 op1, w)) ] @
190 then spill (op1, spillreg1)
192 | transform (X.INSN (pred, X.MOVLBL (op1, w))) =
193 [ X.INSN (pred, X.MOVLBL (real_op1 op1, w)) ] @
195 then spill (op1, spillreg1)
198 (* and here comes the boilerplate *)
199 | transform (X.INSN (pred, X.LDR (op1, op2))) = whack_insn (pred, X.LDR, op1, op2)
200 | transform (X.INSN (pred, X.STO (op1, op2))) = whack_insn (pred, X.STO, op1, op2)
201 | transform (X.INSN (pred, X.MOV (op1, op2))) = whack_insn (pred, X.MOV, op1, op2)
202 | transform (X.INSN (pred, X.MOVS (op1, op2))) = whack_insn (pred, X.MOVS, op1, op2)
203 | transform (X.INSN (pred, X.ADD (op1, op2))) = whack_insn (pred, X.ADD, op1, op2)
204 | transform (X.INSN (pred, X.ADDS (op1, op2))) = whack_insn (pred, X.ADDS, op1, op2)
205 | transform (X.INSN (pred, X.SUB (op1, op2))) = whack_insn (pred, X.SUB, op1, op2)
206 | transform (X.INSN (pred, X.SUBS (op1, op2))) = whack_insn (pred, X.SUBS, op1, op2)
207 | transform (X.INSN (pred, X.AND (op1, op2))) = whack_insn (pred, X.AND, op1, op2)
208 | transform (X.INSN (pred, X.ANDS (op1, op2))) = whack_insn (pred, X.ANDS, op1, op2)
209 | transform (X.INSN (pred, X.NOT (op1, op2))) = whack_insn (pred, X.NOT, op1, op2)
210 | transform (X.INSN (pred, X.NOTS (op1, op2))) = whack_insn (pred, X.NOTS, op1, op2)
211 | transform (X.INSN (pred, X.PUSH (op1, op2))) = if isspilled op2
212 then raise ErrorMsg.InternalError "PUSH on spilled op2 is not possible"
213 else [ X.INSN (pred, X.PUSH (real_ops (op1, op2))) ]
214 | transform (X.INSN (pred, X.POP (X.REG X.SP, X.REG X.PC))) = [ X.INSN (pred, X.MOVLBL (X.REG X.PC, endlbl)) ] (* optimize epilogue? *)
215 | transform (X.INSN (pred, X.POP (op1, op2))) = if isspilled op2
216 then raise ErrorMsg.InternalError "POP on spilled op2 is not possible"
217 else [ X.INSN (pred, X.POP (real_ops (op1, op2))) ]
218 | transform (X.INSN (pred, X.CALL (op1, op2, i))) = if isspilled op2
219 then raise ErrorMsg.InternalError "CALL on spilled op2 is not possible"
220 else [ X.INSN (pred, X.CALL ((fn (x, y) => (x, y, i)) (real_ops (op1, op2)))) ]
221 | transform (X.INSN (pred, X.SHR (op1, op2))) = whack_insn (pred, X.SHR, op1, op2)
222 | transform (X.INSN (pred, X.SHL (op1, op2))) = whack_insn (pred, X.SHL, op1, op2)
223 (*| transform _ = raise ErrorMsg.InternalError "unimplemented"*)
225 (*if (nsave < 2) then
226 List.concat (prologue :: (map transform instrs))
229 List.concat (prologue ::
230 ((map transform instrs) @
233 [X.INSN (X.AL, X.POP (X.REG X.SP, X.REG X.PC))]