/
mipsgen.sml
303 lines (252 loc) · 10.4 KB
/
mipsgen.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
signature CODEGEN =
sig
structure Frame: FRAME
val codegen: Frame.frame -> Tree.stm -> Assem.instr list
end
structure MipsCodegen: CODEGEN =
struct
structure Frame = MipsFrame
structure A = Assem
structure T = Tree
(* Temps which become trashed during procedure calls *)
val calldefs = Frame.tempList((Frame.argregs) @ (Frame.callersaves) @ (Frame.specialregs))
fun intToStr i = if (i < 0) then "-" ^ (Int.toString (~i)) else (Int.toString i)
fun codegen(frame)(stm) =
let
val instrs = ref nil
(* Accumulate an instruction list in reverse order *)
fun emit(instr) = instrs := (instr :: !instrs)
(*
Generate a new temp as the destination for the assembler block built by
the given generator
*)
fun result(gen) =
let
val t = Temp.newtemp()
in
(gen t; t)
end
(* Thrown if a tree does not map to a MIPS instruction *)
exception IllegalTree
(* Sequence of stms *)
fun munchStm(T.SEQ(a, b)) = (munchStm a; munchStm b)
(* Labels *)
| munchStm(T.LABEL(label)) =
emit(A.LABEL{assem=(Symbol.name label) ^ ":\n", lab=label})
(* Unconditional jumps *)
| munchStm(T.JUMP(T.NAME(label), labels)) =
emit(A.OPER{assem="j " ^ (Symbol.name label) ^ "\n",
src=[],
dst=[],
jump=SOME(labels)})
(* Conditional jumps *)
| munchStm(T.CJUMP(relop, exp1, exp2, trueLabel, falseLabel)) =
let
val relopAssem = case relop of
T.EQ => "beq"
| T.NE => "bne"
| T.LT => "blt"
| T.GT => "bgt"
| T.LE => "ble"
| T.GE => "bge"
| T.ULT => "bltu"
| T.ULE => "bleu"
| T.UGT => "bgtu"
| T.UGE => "bgeu"
in
emit(A.OPER{assem=(relopAssem ^ " `s0, `s1, " ^ (Symbol.name trueLabel) ^ "\n" ^
"j " ^ (Symbol.name falseLabel) ^ "\n"),
src=[munchExp exp1, munchExp exp2],
dst=[],
jump=SOME([trueLabel, falseLabel])})
end
(* Stores to memory *)
| munchStm(T.MOVE(T.MEM(T.BINOP(T.PLUS, exp1, T.CONST(intVal))), exp2)) =
emit(A.OPER{assem="sw `s1, " ^ (intToStr intVal) ^ "(`s0)\n",
src=[munchExp exp1, munchExp exp2],
dst=[],
jump=NONE})
| munchStm(T.MOVE(T.MEM(T.BINOP(T.PLUS, T.CONST(intVal), exp1)), exp2)) =
emit(A.OPER{assem="sw `s1, " ^ (intToStr intVal) ^ "(`s0)\n",
src=[munchExp exp1, munchExp exp2],
dst=[],
jump=NONE})
| munchStm(T.MOVE(T.MEM(exp1), exp2)) =
emit(A.OPER{assem="sw `s1, `s0\n",
src=[munchExp exp1, munchExp exp2],
dst=[],
jump=NONE})
(* Function calls *)
| munchStm(T.MOVE(T.TEMP(temp), T.CALL(T.NAME(funcName), argExps))) =
(emit(A.OPER{assem="jal " ^ (Symbol.name funcName) ^ "\n",
src=munchArgs(0, argExps),
dst=calldefs,
jump=SOME([funcName])});
emit(A.MOVE{assem="move `d0, `s0\n",
src=Frame.RV,
dst=temp}))
(* Stores to registers *)
| munchStm(T.MOVE(T.TEMP(temp), T.CONST(intVal))) =
emit(A.OPER{assem="li `d0, " ^ (intToStr intVal) ^ "\n",
src=[],
dst=[temp],
jump=NONE})
| munchStm(T.MOVE(T.TEMP(temp), exp)) =
emit(A.MOVE{assem="move `d0, `s0\n",
src=munchExp exp,
dst=temp})
(* Procedure calls *)
| munchStm(T.EXP(T.CALL(T.NAME(funcName), argExps))) =
emit(A.OPER{assem="jal " ^ (Symbol.name funcName) ^ "\n",
src=munchArgs(0, argExps),
dst=calldefs,
jump=SOME([funcName])})
| munchStm(_) = raise IllegalTree
(* Immediate arithmetic operations *)
and munchExp(T.BINOP(T.PLUS, T.CONST(intVal), exp)) =
result(fn r =>
emit(A.OPER{assem="addi `d0, `s0, " ^ (intToStr intVal) ^ "\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
| munchExp(T.BINOP(T.PLUS, exp, T.CONST(intVal))) =
result(fn r =>
emit(A.OPER{assem="addi `d0, `s0, " ^ (intToStr intVal) ^ "\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
| munchExp(T.BINOP(T.MINUS, T.CONST(0), exp)) =
result(fn r =>
emit(A.OPER{assem="neg `d0, `s0\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
| munchExp(T.BINOP(T.MINUS, exp, T.CONST(intVal))) =
result(fn r =>
emit(A.OPER{assem="sub `d0, `s0, " ^ (intToStr intVal) ^ "\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
| munchExp(T.BINOP(T.MUL, T.CONST(intVal), exp)) =
result(fn r =>
emit(A.OPER{assem="mul `d0, `s0, " ^ (intToStr intVal) ^ "\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
| munchExp(T.BINOP(T.MUL, exp, T.CONST(intVal))) =
result(fn r =>
emit(A.OPER{assem="mul `d0, `s0, " ^ (intToStr intVal) ^ "\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
| munchExp(T.BINOP(T.DIV, exp, T.CONST(intVal))) =
result(fn r =>
emit(A.OPER{assem="div `d0, `s0, " ^ (intToStr intVal) ^ "\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
(* Non-immediate arithmetic and logical operations *)
| munchExp(T.BINOP(binop, exp1, exp2)) =
let
val binopAssem = case binop of
T.PLUS => "add"
| T.MINUS => "sub"
| T.MUL => "mul"
| T.DIV => "div"
| T.AND => "and"
| T.OR => "or"
| T.LSHIFT => "sll"
| T.RSHIFT => "srl"
| T.ARSHIFT => "sra"
| T.XOR => "xor"
in
result(fn r =>
emit(A.OPER{assem=(binopAssem ^ " `d0, `s0, `s1\n"),
src=[munchExp exp1, munchExp exp2],
dst=[r],
jump=NONE}))
end
(* Moves from a temp *)
| munchExp(T.TEMP(temp)) =
result(fn r =>
emit(A.MOVE{assem="move `d0, `s0\n",
src=temp,
dst=r}))
(* Loads from memory *)
| munchExp(T.MEM(T.BINOP(T.PLUS, exp, T.CONST(intVal)))) =
result(fn r =>
emit(A.OPER{assem="lw `d0, " ^ (intToStr intVal) ^ "(`s0)\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
| munchExp(T.MEM(T.BINOP(T.PLUS, T.CONST(intVal), exp))) =
result(fn r =>
emit(A.OPER{assem="lw `d0, " ^ (intToStr intVal) ^ "(`s0)\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
| munchExp(T.MEM(T.CONST(intVal))) =
result(fn r =>
emit(A.OPER{assem="lw `d0, " ^ (intToStr intVal) ^ "($0)\n",
src=[],
dst=[r],
jump=NONE}))
| munchExp(T.MEM(exp)) =
result(fn r =>
emit(A.OPER{assem="lw `d0, 0(`s0)\n",
src=[munchExp exp],
dst=[r],
jump=NONE}))
(* Immediate loads *)
| munchExp(T.CONST(intVal)) =
result(fn r =>
emit(A.OPER{assem="li `d0, " ^ (intToStr intVal) ^ "\n",
src=[],
dst=[r],
jump=NONE}))
(* References to string labels *)
| munchExp(T.NAME(strLabel)) =
result(fn r =>
emit(A.OPER{assem="la `d0, " ^ (Symbol.name strLabel) ^ "\n",
src=[],
dst=[r],
jump=NONE}))
(* Procedure calls *)
| munchExp(T.CALL(T.NAME(funcName), argExps)) =
result(fn r =>
emit(A.OPER{assem="jal " ^ (Symbol.name funcName) ^ "\n",
src=munchArgs(0, argExps),
dst=calldefs,
jump=SOME([funcName])}))
| munchExp(tree) = (Printtree.printtree(TextIO.stdOut, T.EXP(tree)); raise IllegalTree)
(*
Move the first 4 arguments into a registers, then allocate the rest of
the arguments on the stack frame
*)
and munchArgs(idx, nil) = nil
| munchArgs(idx, argExp :: rest) =
let val numRegs = length(Frame.argregs) in
if idx < numRegs then
let
val (argTemp, argReg) = List.nth(Frame.argregs, idx)
in
(munchStm(T.MOVE(T.TEMP(argTemp), argExp));
(*
Once we start pushing args to the stack, do so in reverse order
so that they can be read in order by incrementing the address from
the frame pointer.
*)
argTemp :: munchArgs(idx + 1, (if idx = (numRegs - 1) then rev(rest) else rest)))
end
else
(munchStm(T.MOVE(
Frame.exp(Frame.allocLocal(frame)(true))
(T.TEMP(Frame.FP)),
T.TEMP(munchExp(argExp))));
munchArgs(idx + 1, rest))
end
in
(munchStm stm;
rev(!instrs))
end
end