2 * Assembly code generator for fake x86 assembly
3 * Author: Joshua Wise <jwise@andrew.cmu.edu>
4 * Author: Chris Lu <czl@andrew.cmu.edu>
9 val codegen : Tree.stm list -> x86.insn list
12 structure Codegen :> CODEGEN =
17 (* effect : T.exp -> bool
18 * true iff the given expression has an effect.
20 fun effect (T.BINOP(T.DIV, _, _)) = true
21 | effect (T.BINOP(T.MOD, _, _)) = true
22 | effect (T.CALL _) = true
23 | effect (T.BINOP(_, a, b)) = (effect a) orelse (effect b)
24 | effect (T.UNOP (_, a)) = effect a
25 | effect (T.MEMORY m) = true
26 | effect (T.ALLOC(_)) = true
29 (* hasfixed : T.exp -> bool
30 * true iff the given expression has an hasfixed. Somewhat like effect, hmm?
32 fun hasfixed (T.BINOP(T.DIV, _, _)) = true
33 | hasfixed (T.BINOP(T.MOD, _, _)) = true
34 | hasfixed (T.BINOP(T.LSH, _, _)) = true
35 | hasfixed (T.BINOP(T.RSH, _, _)) = true
36 | hasfixed (T.CALL _) = true
37 | hasfixed (T.BINOP(_, a, b)) = (hasfixed a) orelse (hasfixed b)
38 | hasfixed (T.UNOP (_, a)) = hasfixed a
39 | hasfixed (T.ALLOC(_)) = true
40 | hasfixed (T.MEMORY m) = hasfixed m
43 (* munch_exp : prex86oper -> T.exp -> prex86insn list *)
45 * generates instructions to achieve d <- e
46 * d must be TEMP(t) or REG(r)
48 and munch_exp d (T.CONST(n)) = [X.MOV(d, X.CONST n)]
49 | munch_exp d (T.TEMP(t)) = [X.MOV(d, X.TEMP t)]
50 | munch_exp d (T.ARG(0, sz)) = [X.MOV(d, X.OSIZE (X.sts sz, X.REG X.EDI))]
51 | munch_exp d (T.ARG(1, sz)) = [X.MOV(d, X.OSIZE (X.sts sz, X.REG X.ESI))]
52 | munch_exp d (T.ARG(2, sz)) = [X.MOV(d, X.OSIZE (X.sts sz, X.REG X.EDX))]
53 | munch_exp d (T.ARG(3, sz)) = [X.MOV(d, X.OSIZE (X.sts sz, X.REG X.ECX))]
54 | munch_exp d (T.ARG(4, sz)) = [X.MOV(d, X.OSIZE (X.sts sz, X.REG X.R8D))]
55 | munch_exp d (T.ARG(5, sz)) = [X.MOV(d, X.OSIZE (X.sts sz, X.REG X.R9D))]
56 | munch_exp d (T.ARG(t, sz)) = [X.MOV(d, X.OSIZE (X.sts sz, X.STACKARG (t - 6)))]
57 | munch_exp d (T.CALL(name, l, rsz)) = (* Scary demons live here. *)
60 val nstack = if (nargs <= 6)
63 val stackb = nstack * 8
64 fun argdest 1 = X.REG X.EDI
65 | argdest 2 = X.REG X.ESI
66 | argdest 3 = X.REG X.EDX
67 | argdest 4 = X.REG X.ECX
68 | argdest 5 = X.REG X.R8D
69 | argdest 6 = X.REG X.R9D
70 | argdest n = X.REL (X.REG X.RSP, X.CONST (Word32.fromInt (~(stackb - 8 * (n - 7)))) )
72 val dests = List.tabulate (nargs, fn x => argdest (x+1))
73 val (exps,_) = ListPair.unzip l
74 val hf = List.map hasfixed exps
75 val (d_hf, l_hf) = ListPair.unzip (ListPair.foldr
76 (fn (a,b,c) => if b then a::c else c)
78 (ListPair.zip (dests,l), hf)
80 val (d_nohf, l_nohf) = ListPair.unzip (ListPair.foldr
81 (fn (a,b,c) => if b then c else a::c)
83 (ListPair.zip (dests,l), hf)
85 val temps = List.map (fn (_, sz) => Temp.new ("arg") sz (* xxx? *)) l_hf
86 val argevals_hf = List.map
87 (fn (t,(exp,_)) => munch_exp (X.TEMP t) exp)
88 (ListPair.zip (temps, l_hf))
89 val argpushes = List.map
90 (fn (dest, t) => [(X.MOV (X.OSIZE(X.sts (Temp.size t), dest), X.TEMP t))])
91 (ListPair.zip (d_hf, temps))
92 val argevals_nohf = List.map
93 (fn (d,(exp,sz)) => munch_exp (X.OSIZE (X.sts sz, d)) exp)
94 (ListPair.zip (d_nohf, l_nohf))
96 List.concat argevals_hf @
97 List.concat argpushes @
98 List.concat argevals_nohf @
99 [ X.SUB (X.OSIZE (X.Qword, X.REG X.RSP), X.CONST (Word32.fromInt stackb)),
100 X.CALL (name, nargs),
101 X.ADD (X.OSIZE (X.Qword, X.REG X.RSP), X.CONST (Word32.fromInt stackb)),
102 X.MOV (d, X.OSIZE (X.sts rsz, X.REG X.EAX)) ] (* Finally! *)
104 (* | munch_exp d (T.BINOP(T.ADD, e1, T.CONST 0w0)) = munch_exp d e1
105 | munch_exp d (T.BINOP(T.ADD, T.CONST 0w0, e1)) = munch_exp d e1
106 | munch_exp d (T.BINOP(T.ADD, e1, T.CONST n)) = (munch_exp d e1) @ [X.ADD(d, X.CONST n)]
107 | munch_exp d (T.BINOP(T.ADD, T.CONST n, e1)) = (munch_exp d e1) @ [X.ADD(d, X.CONST n)]
108 | munch_exp d (T.BINOP(T.ADD, e1, T.TEMP t)) = (munch_exp d e1) @ [X.ADD(d, X.TEMP t)]
109 | munch_exp d (T.BINOP(T.ADD, T.TEMP t, e2)) = (munch_exp d e2) @ [X.ADD(d, X.TEMP t)] *)
110 | munch_exp d (T.BINOP(T.ADD, e1, e2)) =
112 val t1 = X.TEMP (Temp.new ("add") 4)
114 (munch_exp d e1) @ (munch_exp t1 e2) @ [X.ADD(d, t1)]
116 (* | munch_exp d (T.BINOP(T.SUB, T.CONST 0w0, e1)) = (munch_exp d e1) @ [X.NEG d]
117 | munch_exp d (T.BINOP(T.SUB, e1, T.CONST 0w0)) = munch_exp d e1
118 | munch_exp d (T.BINOP(T.SUB, e1, T.CONST(n))) = (munch_exp d e1) @ [X.SUB(d, X.CONST n)]
119 | munch_exp d (T.BINOP(T.SUB, e1, T.TEMP t)) = (munch_exp d e1) @ [X.SUB(d, X.TEMP t)] *)
120 | munch_exp d (T.BINOP(T.SUB, e1, e2)) =
122 val t1 = X.TEMP (Temp.new ("sub") 4)
124 (munch_exp d e1) @ (munch_exp t1 e2) @ [X.SUB(d, t1)]
126 | munch_exp d (T.BINOP(T.MUL, T.TEMP t, T.CONST n)) = [X.IMUL3(d, X.TEMP t, n)]
127 | munch_exp d (T.BINOP(T.MUL, T.CONST n, T.TEMP t)) = [X.IMUL3(d, X.TEMP t, n)]
129 | munch_exp d (T.BINOP(T.MUL, e1, T.CONST 0w1)) = munch_exp d e1
130 | munch_exp d (T.BINOP(T.MUL, T.CONST 0w1, e1)) = munch_exp d e1
131 | munch_exp d (T.BINOP(T.MUL, e1, T.CONST n)) = (munch_exp d e1) @ [X.IMUL(d, X.CONST n)]
132 | munch_exp d (T.BINOP(T.MUL, T.CONST n, e1)) = (munch_exp d e1) @ [X.IMUL(d, X.CONST n)] *)
133 | munch_exp d (T.BINOP(T.MUL, e1, e2)) =
135 val t1 = X.TEMP (Temp.new ("mul") 4)
137 (munch_exp d e1) @ (munch_exp t1 e2) @ [X.IMUL(d, t1)]
139 | munch_exp d (T.BINOP(T.DIV, e1, e2)) =
141 val t1 = X.TEMP (Temp.new ("div") 4)
143 (munch_exp t1 e1) @ (munch_exp d e2) @
144 [X.MOV (X.REG X.EAX, t1), X.CLTD, X.IDIV d, X.MOV (d, X.REG X.EAX)]
146 | munch_exp d (T.BINOP(T.MOD, e1, e2)) =
148 val t1 = X.TEMP (Temp.new ("mod") 4)
150 (munch_exp t1 e1) @ (munch_exp d e2) @
151 [X.MOV (X.REG X.EAX, t1), X.CLTD, X.IDIV d, X.MOV (d, X.REG X.EDX)]
153 | munch_exp d (T.BINOP(T.LSH, e1, T.CONST n)) = (munch_exp d e1) @ [X.SAL (d, X.CONST (n mod 0w32))]
154 | munch_exp d (T.BINOP(T.LSH, e1, T.TEMP t)) = (munch_exp d e1) @ [X.MOV (X.REG X.ECX, X.TEMP t), X.SAL (d, X.REG X.ECX)]
155 | munch_exp d (T.BINOP(T.LSH, e1, e2)) =
157 val t = X.TEMP (Temp.new ("lsh") 4)
159 (munch_exp d e1) @ (munch_exp t e2) @ [X.MOV (X.REG X.ECX, t), X.SAL (d, X.REG X.ECX)]
161 | munch_exp d (T.BINOP(T.RSH, e1, T.CONST n)) = (munch_exp d e1) @ [X.SAR (d, X.CONST (n mod 0w32))]
162 | munch_exp d (T.BINOP(T.RSH, e1, T.TEMP t)) = (munch_exp d e1) @ [X.MOV (X.REG X.ECX, X.TEMP t), X.SAR (d, X.REG X.ECX)]
163 | munch_exp d (T.BINOP(T.RSH, e1, e2)) =
165 val t = X.TEMP (Temp.new ("rsh") 4)
167 (munch_exp d e1) @ (munch_exp t e2) @ [X.MOV (X.REG X.ECX, t), X.SAR (d, X.REG X.ECX)]
169 | munch_exp d (T.BINOP(T.BITAND, T.CONST n, e1)) = (munch_exp d e1) @ [X.AND (d, X.CONST n)]
170 | munch_exp d (T.BINOP(T.BITAND, e1, T.CONST n)) = (munch_exp d e1) @ [X.AND (d, X.CONST n)]
171 | munch_exp d (T.BINOP(T.BITAND, T.TEMP t, e1)) = (munch_exp d e1) @ [X.AND (d, X.TEMP t)]
172 | munch_exp d (T.BINOP(T.BITAND, e1, T.TEMP t)) = (munch_exp d e1) @ [X.AND (d, X.TEMP t)]
173 | munch_exp d (T.BINOP(T.BITAND, e1, e2)) =
175 val t1 = X.TEMP (Temp.new ("bitand") 4)
177 (munch_exp d e1) @ (munch_exp t1 e2) @ [X.AND(d, t1)]
179 | munch_exp d (T.BINOP(T.BITOR, T.CONST n, e1)) = (munch_exp d e1) @ [X.OR (d, X.CONST n)]
180 | munch_exp d (T.BINOP(T.BITOR, e1, T.CONST n)) = (munch_exp d e1) @ [X.OR (d, X.CONST n)]
181 | munch_exp d (T.BINOP(T.BITOR, T.TEMP t, e1)) = (munch_exp d e1) @ [X.OR (d, X.TEMP t)]
182 | munch_exp d (T.BINOP(T.BITOR, e1, T.TEMP t)) = (munch_exp d e1) @ [X.OR (d, X.TEMP t)]
183 | munch_exp d (T.BINOP(T.BITOR, e1, e2)) =
185 val t1 = X.TEMP (Temp.new ("bitor") 4)
187 (munch_exp d e1) @ (munch_exp t1 e2) @ [X.OR(d, t1)]
189 | munch_exp d (T.BINOP(T.BITXOR, T.CONST n, e1)) = (munch_exp d e1) @ [X.XOR (d, X.CONST n)]
190 | munch_exp d (T.BINOP(T.BITXOR, e1, T.CONST n)) = (munch_exp d e1) @ [X.XOR (d, X.CONST n)]
191 | munch_exp d (T.BINOP(T.BITXOR, T.TEMP t, e1)) = (munch_exp d e1) @ [X.XOR (d, X.TEMP t)]
192 | munch_exp d (T.BINOP(T.BITXOR, e1, T.TEMP t)) = (munch_exp d e1) @ [X.XOR (d, X.TEMP t)]
193 | munch_exp d (T.BINOP(T.BITXOR, e1, e2)) =
195 val t1 = X.TEMP (Temp.new ("bitxor") 4)
197 (munch_exp d e1) @ (munch_exp t1 e2) @ [X.XOR(d, t1)]
199 | munch_exp d (a as T.BINOP(T.LOGAND, e1, e2)) =
201 val (insn1, pos1, neg1) = munch_cond e1
202 val (insn2, pos2, neg2) = munch_cond e2
203 val t1 = X.TEMP (Temp.new("logand 1") 4)
204 val t2 = X.TEMP (Temp.new("logand 2") 4)
207 if (effect e2 orelse (length insn2 > 10))
209 [X.SETcc(pos1, t1), X.Jcc (neg1, l)] @
211 [X.SETcc(pos2, t1), X.LABEL l, X.MOVZB(d, t1)]
212 else insn1 @ [X.SETcc (pos1, t1)] @ insn2 @ [X.SETcc (pos2, t2), X.AND(X.OSIZE (X.Byte, t1), X.OSIZE (X.Byte, t2)), X.MOVZB(d, t1)]
214 | munch_exp d (a as T.BINOP(T.LOGOR, e1, e2)) =
216 val (insn1, pos1, neg1) = munch_cond e1
217 val (insn2, pos2, neg2) = munch_cond e2
218 val t1 = X.TEMP (Temp.new("logor 1") 4)
219 val t2 = X.TEMP (Temp.new("logor 2") 4)
222 if (effect e2 orelse (length insn2 > 10))
224 [X.SETcc(pos1, t1), X.Jcc (pos1, l)] @
226 [X.SETcc(pos2, t1), X.LABEL l, X.MOVZB(d, t1)]
227 else insn1 @ [X.SETcc (pos1, t1)] @ insn2 @ [X.SETcc (pos2, t2), X.OR(X.OSIZE (X.Byte, t1), X.OSIZE (X.Byte, t2)), X.MOVZB(d, t1)]
229 | munch_exp d (a as T.BINOP(T.EQ, _, _)) =
230 let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
231 | munch_exp d (a as T.BINOP(T.NEQ, _, _)) =
232 let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
233 | munch_exp d (a as T.BINOP(T.LE, _, _)) =
234 let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
235 | munch_exp d (a as T.BINOP(T.LT, _, _)) =
236 let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
237 | munch_exp d (a as T.BINOP(T.GE, _, _)) =
238 let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
239 | munch_exp d (a as T.BINOP(T.GT, _, _)) =
240 let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
241 | munch_exp d (T.UNOP(T.NEG, T.CONST n)) = [X.MOV (d, X.CONST (~n))]
242 | munch_exp d (T.UNOP(T.NEG, e1)) = (munch_exp d e1) @ [X.NEG d]
243 | munch_exp d (T.UNOP(T.BITNOT, T.CONST n)) = [X.MOV (d, X.CONST (Word32.notb n))]
244 | munch_exp d (T.UNOP(T.BITNOT, e1)) = (munch_exp d e1) @ [X.NOT d]
245 | munch_exp d (T.UNOP(T.BANG, T.CONST n)) = if (n = 0w0) then [X.MOV (d, X.CONST 0w1)] else [X.MOV (d, X.CONST 0w0)]
246 | munch_exp d (T.UNOP(T.BANG, e)) =
248 val (insns, pos, neg) = munch_cond e
250 insns @ [X.SETcc (neg, d), X.MOVZB(d, d)]
252 | munch_exp d (T.MEMORY e1) =
254 val a = X.TEMP (Temp.new "addr" 8)
256 munch_exp a e1 @ [X.MOV (d, X.REL (a, X.CONST 0w0))]
258 | munch_exp d (T.ALLOC(exp)) = (munch_exp d (T.CALL (Symbol.symbol "calloc", [(exp, 4), (T.CONST 0w1, 4)], 8)))
259 @ [X.MOV (X.REL (d, X.CONST 0w0), X.CONST 0w0)]
261 (* munch_cond : T.exp -> X.insn list * X.cond * X.cond
262 * munch_cond stm generates code to set flags, and then returns a conditional
263 * to test if the expression was true and for if it was false.
265 and munch_cond (T.UNOP (T.BANG, e)) =
267 val (insns, pos, neg) = munch_cond e
271 | munch_cond (T.BINOP(T.NEQ, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.NE, X.E)
272 | munch_cond (T.BINOP(T.NEQ, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.NE, X.E)
273 | munch_cond (T.BINOP(T.NEQ, T.CONST n, e1)) =
274 let val t = X.TEMP (Temp.new ("const neq") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.NE, X.E) end
275 | munch_cond (T.BINOP(T.NEQ, e1, T.CONST n)) =
276 let val t = X.TEMP (Temp.new ("const neq") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.NE, X.E) end
277 | munch_cond (T.BINOP(T.NEQ, T.TEMP t, e1)) =
278 let val t1 = X.TEMP (Temp.new ("const neq") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.NE, X.E) end
279 | munch_cond (T.BINOP(T.NEQ, e1, T.TEMP t)) =
280 let val t1 = X.TEMP (Temp.new ("const neq") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.NE, X.E) end
281 | munch_cond (T.BINOP(T.NEQ, e1, e2)) =
283 val t1 = X.TEMP (Temp.new ("var neq 1") 4)
284 val t2 = X.TEMP (Temp.new ("var neq 2") 4)
286 (munch_exp t1 e1 @ munch_exp t2 e2 @
287 [X.CMP(t1, t2)], X.NE, X.E)
289 | munch_cond (T.BINOP(T.EQ, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.E, X.NE)
290 | munch_cond (T.BINOP(T.EQ, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.E, X.NE)
291 | munch_cond (T.BINOP(T.EQ, T.CONST n, e1)) =
292 let val t = X.TEMP (Temp.new ("const eq") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.E, X.NE) end
293 | munch_cond (T.BINOP(T.EQ, e1, T.CONST n)) =
294 let val t = X.TEMP (Temp.new ("const eq") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.E, X.NE) end
295 | munch_cond (T.BINOP(T.EQ, T.TEMP t, e1)) =
296 let val t1 = X.TEMP (Temp.new ("const eq") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.E, X.NE) end
297 | munch_cond (T.BINOP(T.EQ, e1, T.TEMP t)) =
298 let val t1 = X.TEMP (Temp.new ("const eq") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.E, X.NE) end
299 | munch_cond (T.BINOP(T.EQ, e1, e2)) =
301 val t1 = X.TEMP (Temp.new ("var eq 1") 4)
302 val t2 = X.TEMP (Temp.new ("var eq 2") 4)
304 (munch_exp t1 e1 @ munch_exp t2 e2 @
305 [X.CMP(t1, t2)], X.E, X.NE)
307 | munch_cond (T.BINOP(T.LE, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.LE, X.G)
308 | munch_cond (T.BINOP(T.LE, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.GE, X.L)
309 | munch_cond (T.BINOP(T.LE, T.CONST n, e1)) =
310 let val t = X.TEMP (Temp.new ("const le") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.GE, X.L) end
311 | munch_cond (T.BINOP(T.LE, e1, T.CONST n)) =
312 let val t = X.TEMP (Temp.new ("const le") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.LE, X.G) end
313 | munch_cond (T.BINOP(T.LE, T.TEMP t, e1)) =
314 let val t1 = X.TEMP (Temp.new ("const le") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.GE, X.L) end
315 | munch_cond (T.BINOP(T.LE, e1, T.TEMP t)) =
316 let val t1 = X.TEMP (Temp.new ("const le") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.LE, X.G) end
317 | munch_cond (T.BINOP(T.LE, e1, e2)) =
319 val t1 = X.TEMP (Temp.new ("var le 1") 4)
320 val t2 = X.TEMP (Temp.new ("var le 2") 4)
322 (munch_exp t1 e1 @ munch_exp t2 e2 @
323 [X.CMP(t1, t2)], X.LE, X.G)
325 | munch_cond (T.BINOP(T.LT, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.L, X.GE)
326 | munch_cond (T.BINOP(T.LT, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.G, X.LE)
327 | munch_cond (T.BINOP(T.LT, T.CONST n, e1)) =
328 let val t = X.TEMP (Temp.new ("const lt") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.G, X.LE) end
329 | munch_cond (T.BINOP(T.LT, e1, T.CONST n)) =
330 let val t = X.TEMP (Temp.new ("const lt") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.L, X.GE) end
331 | munch_cond (T.BINOP(T.LT, T.TEMP t, e1)) =
332 let val t1 = X.TEMP (Temp.new ("const lt") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.G, X.LE) end
333 | munch_cond (T.BINOP(T.LT, e1, T.TEMP t)) =
334 let val t1 = X.TEMP (Temp.new ("const lt") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.L, X.GE) end
335 | munch_cond (T.BINOP(T.LT, e1, e2)) =
337 val t1 = X.TEMP (Temp.new ("var lt 1") 4)
338 val t2 = X.TEMP (Temp.new ("var lt 2") 4)
340 (munch_exp t1 e1 @ munch_exp t2 e2 @
341 [X.CMP(t1, t2)], X.L, X.GE)
343 | munch_cond (T.BINOP(T.GT, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.G, X.LE)
344 | munch_cond (T.BINOP(T.GT, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.L, X.GE)
345 | munch_cond (T.BINOP(T.GT, e1, T.CONST n)) =
346 let val t = X.TEMP (Temp.new ("const gt") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.G, X.LE) end
347 | munch_cond (T.BINOP(T.GT, T.CONST n, e1)) =
348 let val t = X.TEMP (Temp.new ("const gt") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.L, X.GE) end
349 | munch_cond (T.BINOP(T.GT, e1, T.TEMP t)) =
350 let val t1 = X.TEMP (Temp.new ("const gt") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.G, X.LE) end
351 | munch_cond (T.BINOP(T.GT, T.TEMP t, e1)) =
352 let val t1 = X.TEMP (Temp.new ("const gt") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.L, X.GE) end
353 | munch_cond (T.BINOP(T.GT, e1, e2)) =
355 val t1 = X.TEMP (Temp.new ("var gt 1") 4)
356 val t2 = X.TEMP (Temp.new ("var gt 2") 4)
358 (munch_exp t1 e1 @ munch_exp t2 e2 @
359 [X.CMP(t1, t2)], X.G, X.LE)
361 | munch_cond (T.BINOP(T.GE, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.GE, X.L)
362 | munch_cond (T.BINOP(T.GE, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.LE, X.G)
363 | munch_cond (T.BINOP(T.GE, e1, T.CONST n)) =
364 let val t = X.TEMP (Temp.new ("const ge") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.GE, X.L) end
365 | munch_cond (T.BINOP(T.GE, T.CONST n, e1)) =
366 let val t = X.TEMP (Temp.new ("const ge") 4) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.LE, X.G) end
367 | munch_cond (T.BINOP(T.GE, e1, T.TEMP t)) =
368 let val t1 = X.TEMP (Temp.new ("const ge") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.GE, X.L) end
369 | munch_cond (T.BINOP(T.GE, T.TEMP t, e1)) =
370 let val t1 = X.TEMP (Temp.new ("const ge") 4) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.LE, X.G) end
371 | munch_cond (T.BINOP(T.GE, e1, e2)) =
373 val t1 = X.TEMP (Temp.new ("var ge 1") 4)
374 val t2 = X.TEMP (Temp.new ("var ge 2") 4)
376 (munch_exp t1 e1 @ munch_exp t2 e2 @
377 [X.CMP(t1, t2)], X.GE, X.L)
379 | munch_cond (T.BINOP(T.LOGOR, e1, e2)) =
381 val (insn1, pos1, neg1) = munch_cond e1
382 val (insn2, pos2, neg2) = munch_cond e2
383 val t1 = X.TEMP (Temp.new("logor c 1") 4)
384 val t2 = X.TEMP (Temp.new("logor c 2") 4)
387 if (effect e2 orelse (length insn2 > 10))
389 [X.SETcc (pos1, t1), X.Jcc (pos1, l)] @
391 [X.SETcc (pos2, t1), X.LABEL l, X.TEST(X.OSIZE (X.Byte, t1), X.OSIZE (X.Byte, t1))],
393 else (insn1 @ [X.SETcc (pos1, t1)] @ insn2 @ [X.SETcc (pos2, t2), X.OR(X.OSIZE (X.Byte, t1), X.OSIZE (X.Byte, t2))], X.NE, X.E)
395 | munch_cond (T.BINOP(T.LOGAND, e1, e2)) =
397 val (insn1, pos1, neg1) = munch_cond e1
398 val (insn2, pos2, neg2) = munch_cond e2
399 val t1 = X.TEMP (Temp.new("logand c 1") 4)
400 val t2 = X.TEMP (Temp.new("logand c 2") 4)
403 if (effect e2 orelse (length insn2 > 10))
405 [X.SETcc (pos1, t1), X.Jcc (neg1, l)] @
407 [X.SETcc (pos2, t1), X.LABEL l, X.TEST(X.OSIZE (X.Byte, t1), X.OSIZE (X.Byte, t1))],
409 else (insn1 @ [X.SETcc (pos1, t1)] @ insn2 @ [X.SETcc (pos2, t2), X.AND(X.OSIZE (X.Byte, t1), X.OSIZE (X.Byte, t2))], X.NE, X.E)
413 val t = X.TEMP (Temp.new ("munch c") 4)
415 (munch_exp t e @ [ X.TEST (t,t) ], X.NE, X.E)
418 (* munch_lval : T.exp -> (X.insn list * X.operand)
419 * Takes an expression that has been typechecked as being a valid lvalue, and then returns an instruction list and an operand to store your shit in.
421 fun munch_lval (T.TEMP t) = ([], X.TEMP t)
422 | munch_lval (T.MEMORY m) =
424 val t = Temp.new "lv addr" 8
426 (munch_exp (X.TEMP t) m, X.REL (X.TEMP t, X.CONST 0w0))
428 | munch_lval _ = raise ErrorMsg.InternalError "That wasn't really a valid lvalue..."
430 (* munch_stm : T.stm -> X.insn list *)
431 (* munch_stm stm generates code to execute stm *)
432 fun munch_stm (T.MOVE (T.TEMP t, a as T.TEMP _, _)) = munch_exp (X.TEMP t) a
433 | munch_stm (T.MOVE (T.TEMP t, a as T.CONST _, _)) = munch_exp (X.TEMP t) a
434 | munch_stm (T.MOVE (T.TEMP t, a as T.ARG (an, sz), _)) = munch_exp (X.TEMP t) a
435 | munch_stm (T.MOVE (T.TEMP t, a as T.CALL _, _)) = munch_exp (X.TEMP t) a
436 | munch_stm (T.MOVE (a, e2, sz)) =
438 val t = Temp.new ("assign") sz
439 val (m, r) = munch_lval a
441 m @ munch_exp (X.TEMP t) e2
442 @ [X.MOV(X.OSIZE (X.sts sz, r), X.TEMP t)]
444 | munch_stm (T.RETURN(e, sz)) =
446 val t = Temp.new ("retval") sz
448 munch_exp (X.TEMP t) e
449 @ [X.MOV(X.OSIZE (X.sts sz, X.REG X.EAX), X.TEMP t), X.RET]
451 | munch_stm (T.LABEL(l)) = [X.LABEL l]
452 | munch_stm (T.JUMP(l)) = [X.JMP l]
453 | munch_stm (T.JUMPIFN(e, l)) =
455 val (insns, pos, neg) = munch_cond e
457 insns @ [X.Jcc (neg, l)]
459 | munch_stm (T.EFFECT(exp, sz)) = let val t = X.TEMP (Temp.new "throwaway" sz) in munch_exp t exp end
461 fun codegen nil = nil
462 | codegen (stm::stms) = munch_stm stm @ codegen stms