-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmipsgen.sml
416 lines (333 loc) · 16.8 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
structure MipsGen :> CODEGEN =
struct
structure Frame : FRAME = MipsFrame
structure A = Assem
structure T = Tree
exception TooManyArgs of string
fun codegen (frame) (stm:Tree.stm) : Assem.instr list =
let val ilist = ref (nil: A.instr list)
fun emit x = ilist := x :: !ilist
fun result(gen) = let val t = Temp.newtemp() in gen t; t end
fun int2str n =
if n >= 0 then Int.toString(n) else ("-" ^ Int.toString(~n))
(* calling a function will trash a particular set of registers:
* - argregs: they are defined to pass parameters;
* - callersaves: they may be redefined inside the call;
* - RV: it will be overwritten for function return.
*)
val calldefs = [Frame.RV,Frame.RA]@Frame.argregs
fun munchStm (T.SEQ(a,b)) = (munchStm(a); munchStm(b))
| munchStm (T.LABEL lab) =
emit(A.LABEL{assem=Symbol.name(lab) ^ ":",lab=lab})
(* data movement instructions *)
(* 1, store to memory (sw) *)
(* e1+i <= e2 *)
| munchStm (T.MOVE(T.MEM(T.BINOP(T.PLUS, e1, T.CONST i)), e2)) =
emit(A.OPER{assem="sw `s0, " ^ int2str i ^ "(`s1)",
src=[munchExp e2, munchExp e1],
dst=[],jump=NONE})
| munchStm (T.MOVE(T.MEM(T.BINOP(T.PLUS, T.CONST i, e1)), e2)) =
emit(A.OPER{assem="sw `s0, " ^ int2str i ^ "(`s1)",
src=[munchExp e2, munchExp e1],
dst=[],jump=NONE})
(* e1-i <= e2 *)
| munchStm (T.MOVE(T.MEM(T.BINOP(T.MINUS, e1, T.CONST i)), e2)) =
emit(A.OPER{assem="sw `s0, " ^ int2str (~i) ^ "(`s1)",
src=[munchExp e2, munchExp e1],
dst=[],jump=NONE})
| munchStm (T.MOVE(T.MEM(T.BINOP(T.MINUS, T.CONST i, e1)), e2)) =
emit(A.OPER{assem="sw `s0, " ^ int2str (~i) ^ "(`s1)",
src=[munchExp e2, munchExp e1],
dst=[],jump=NONE})
(* i <= e2 *)
(* | munchStm (T.MOVE(T.MEM(T.CONST i), e2)) = *)
(* emit(A.OPER{assem="sw `s0, " ^ int2str i ^ "($zero)", *)
(* src=[munchExp e2],dst=[],jump=NONE}) *)
| munchStm (T.MOVE(T.MEM(e1), e2)) =
emit(A.OPER{assem="sw `s0, 0(`s1)",
src=[munchExp e2, munchExp e1],
dst=[],jump=NONE})
(* 2, load to register (lw) *)
| munchStm (T.MOVE((T.TEMP i, T.CONST n))) =
emit(A.OPER{assem="li `d0, " ^ int2str n,
src=[],dst=[i],jump=NONE})
| munchStm (T.MOVE(T.TEMP i,
T.MEM(T.BINOP(T.PLUS, e1, T.CONST n)))) =
emit(A.OPER{assem="lw `d0, " ^ int2str n ^ "(`s0)",
src=[munchExp e1],dst=[i],jump=NONE})
| munchStm (T.MOVE(T.TEMP i,
T.MEM(T.BINOP(T.PLUS, T.CONST n, e1)))) =
emit(A.OPER{assem="lw `d0, " ^ int2str n ^ "(`s0)",
src=[munchExp e1],dst=[i],jump=NONE})
| munchStm (T.MOVE(T.TEMP i,
T.MEM(T.BINOP(T.MINUS, e1, T.CONST n)))) =
emit(A.OPER{assem="lw `d0, " ^ int2str (~n) ^ "(`s0)",
src=[munchExp e1],dst=[i],jump=NONE})
| munchStm (T.MOVE(T.TEMP i,
T.MEM(T.BINOP(T.MINUS, T.CONST n, e1)))) =
emit(A.OPER{assem="lw `d0, " ^ int2str (~n) ^ "(`s0)",
src=[munchExp e1],dst=[i],jump=NONE})
(* 3, move from register to register *)
| munchStm (T.MOVE((T.TEMP i, e2))) =
emit(A.MOVE{assem="move `d0, `s0",src=munchExp e2,dst=i})
(* branching *)
| munchStm (T.JUMP(T.NAME lab, _)) =
emit(A.OPER{assem="b `j0",src=[],dst=[],jump=SOME([lab])})
| munchStm (T.JUMP(e, labels)) =
emit(A.OPER{assem="jr `s0",src=[munchExp e],
dst=[],jump=SOME(labels)})
(* when comparing with 0 *)
| munchStm (T.CJUMP(T.GE, e1, T.CONST 0, l1, l2)) =
emit(A.OPER{assem="bgez `s0, `j0\nb `j1",
dst=[],src=[munchExp e1],jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.GT, e1, T.CONST 0, l1, l2)) =
emit(A.OPER{assem="bgtz `s0, `j0\nb `j1",
dst=[],src=[munchExp e1],jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.LE, e1, T.CONST 0, l1, l2)) =
emit(A.OPER{assem="blez `s0, `j0\nb `j1",
dst=[],src=[munchExp e1],jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.LT, e1, T.CONST 0, l1, l2)) =
emit(A.OPER{assem="bltz `s0, `j0\nb `j1",
dst=[],src=[munchExp e1],jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.EQ, e1, T.CONST 0, l1, l2)) =
emit(A.OPER{assem="beqz `s0, `j0\nb `j1",
dst=[],src=[munchExp e1],jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.NE, e1, T.CONST 0, l1, l2)) =
emit(A.OPER{assem="bnez `s0, `j0\nb `j1",
dst=[],src=[munchExp e1],jump=SOME [l1,l2]})
(* more general cases *)
| munchStm (T.CJUMP(T.GE, e1, e2, l1, l2)) =
emit(A.OPER{assem="bge `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.UGE, e1, e2, l1, l2)) =
emit(A.OPER{assem="bgeu `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.GT, e1, e2, l1, l2)) =
emit(A.OPER{assem="bgt `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.UGT, e1, e2, l1, l2)) =
emit(A.OPER{assem="bgtu `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.LT, e1, e2, l1, l2)) =
emit(A.OPER{assem="blt `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.ULT, e1, e2, l1, l2)) =
emit(A.OPER{assem="bltu `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.LE, e1, e2, l1, l2)) =
emit(A.OPER{assem="ble `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.ULE, e1, e2, l1, l2)) =
emit(A.OPER{assem="bleu `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.EQ, e1, e2, l1, l2)) =
emit(A.OPER{assem="beq `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.CJUMP(T.NE, e1, e2, l1, l2)) =
emit(A.OPER{assem="bne `s0, `s1, `j0\nb `j1",
dst=[],src=[munchExp e1, munchExp e2],
jump=SOME [l1,l2]})
| munchStm (T.EXP(T.CALL(e,args))) =
(let
val pairs = map (fn r => (Temp.newtemp (), r)) Frame.callersaves
val srcs = map #1 pairs
fun fetch a r = T.MOVE(T.TEMP r, T.TEMP a)
fun store a r = T.MOVE(T.TEMP a, T.TEMP r)
in
map (fn (a,r) => munchStm(store a r)) pairs;
emit(A.OPER{
assem="jalr `s0",
src=munchExp(e) :: munchArgs(0,args),
dst=calldefs,
jump=NONE});
map (fn (a,r) => munchStm(fetch a r)) (List.rev pairs);
()
end)
| munchStm (T.EXP e) = (munchExp e; ())
(* memory ops *)
and munchExp (T.MEM(T.CONST i)) =
result(fn r => emit(A.OPER{
assem="lw `d0, " ^ int2str i ^ "($zero)",
src=[],dst=[r],jump=NONE}))
| munchExp (T.MEM(T.BINOP(T.PLUS, e1, T.CONST i))) =
result(fn r => emit(A.OPER{
assem="lw `d0, " ^ int2str i ^ "(`s0)",
src=[munchExp e1],dst=[r],jump=NONE}))
| munchExp (T.MEM(T.BINOP(T.PLUS, T.CONST i, e2))) =
result(fn r => emit(A.OPER{
assem="lw `d0, " ^ int2str i ^ "(`s0)",
src=[munchExp e2],dst=[r],jump=NONE}))
| munchExp (T.MEM(T.BINOP(T.MINUS, e1, T.CONST i))) =
result(fn r => emit(A.OPER{
assem="lw `d0, " ^ int2str (~i) ^ "(`s0)",
src=[munchExp e1],dst=[r],jump=NONE}))
| munchExp (T.MEM(T.BINOP(T.MINUS, T.CONST i, e2))) =
result(fn r => emit(A.OPER{
assem="lw `d0, " ^ int2str (~i) ^ "(`s0)",
src=[munchExp e2],dst=[r],jump=NONE}))
(* binary operations *)
(* 1, add/sub immediate *)
| munchExp (T.BINOP(T.PLUS, e1, T.CONST i)) =
result(fn r => emit(A.OPER{
assem="addiu `d0, `s0, " ^ int2str i,
src=[munchExp e1],dst=[r],jump=NONE}))
| munchExp (T.BINOP (T.PLUS, T.CONST i, e1)) =
result(fn r => emit(A.OPER{
assem="addiu `d0, `s0, " ^ int2str i,
src=[munchExp e1],dst=[r],jump=NONE}))
| munchExp (T.BINOP(T.PLUS, e1, e2)) =
result(fn r => emit(A.OPER{
assem="add `d0, `s0, `s1",
src=[munchExp e1,munchExp e2],
dst=[r],jump=NONE}))
| munchExp (T.BINOP(T.MINUS, e1, T.CONST i)) =
result(fn r => emit(A.OPER{
assem="addiu `d0, `s0, " ^ int2str (~i),
src=[munchExp e1],dst=[r],jump=NONE}))
(* div *)
| munchExp (T.BINOP(T.DIV, e1, e2)) =
result(fn r => emit(A.OPER{
assem="div `d0, `s0, `s1",
src=[munchExp e1,munchExp e2],
dst=[r],jump=NONE}))
(* mul *)
| munchExp (T.BINOP(T.MUL, e1, e2)) =
result(fn r => emit(A.OPER{
assem="mul `d0, `s0, `s1",
src=[munchExp e1,munchExp e2],
dst=[r],jump=NONE}))
| munchExp (T.BINOP(T.MINUS, e1, e2)) =
result(fn r => emit(A.OPER{
assem="sub `d0, `s0, `s1",
src=[munchExp e1, munchExp e2],
dst=[r],jump=NONE}))
(* and *)
| munchExp (T.BINOP (T.AND, e1, T.CONST n)) =
result(fn r => emit(A.OPER{
assem="andi `d0, `s0, " ^ int2str n,
src=[munchExp e1],
dst=[r],
jump=NONE}))
| munchExp (T.BINOP (T.AND, T.CONST n, e1)) =
result(fn r => emit(A.OPER{
assem="andi `d0, `s0, " ^ int2str n,
src=[munchExp e1],
dst=[r],
jump=NONE}))
| munchExp (T.BINOP (T.AND, e1, e2)) =
result(fn r => emit(A.OPER{
assem="and `d0, `s0, `s1",
src=[munchExp e1],
dst=[r],
jump=NONE}))
(* or *)
| munchExp (T.BINOP (T.OR, e1, T.CONST n)) =
result(fn r => emit(A.OPER{
assem="ori `d0, `s0, " ^ int2str n,
src=[munchExp e1],dst=[r],jump=NONE}))
| munchExp (T.BINOP (T.OR, T.CONST n, e1)) =
result(fn r => emit(A.OPER{
assem="ori `d0, `s0, " ^ int2str n,
src=[munchExp e1],dst=[r],jump=NONE}))
| munchExp (T.BINOP (T.OR, e1, e2)) =
result(fn r => emit(A.OPER{
assem="or `d0, `s0, `s1",
src=[munchExp e1],dst=[r],jump=NONE}))
(* shift *)
| munchExp (T.BINOP (T.LSHIFT, e, T.CONST n)) =
result (fn r => emit (A.OPER {
assem="sll `d0, `s0, " ^ int2str n,
src=[munchExp e],
dst=[r],
jump=NONE}))
| munchExp (T.BINOP (T.LSHIFT, e1, e2)) =
result (fn r => emit (A.OPER {
assem="sllv `d0, `s0, `s1",
src=[munchExp e1, munchExp e2],
dst=[r],
jump=NONE}))
| munchExp (T.BINOP (T.RSHIFT, e, T.CONST n)) =
result (fn r => emit (A.OPER {
assem="srl `d0, `s0, " ^ int2str n,
src=[munchExp e],
dst=[r],
jump=NONE}))
| munchExp (T.BINOP (T.RSHIFT, e1, e2)) =
result (fn r => emit (A.OPER {
assem="srlv `d0, `s0, `s1",
src=[munchExp e1, munchExp e2],
dst=[r],
jump=NONE}))
| munchExp (T.BINOP (T.ARSHIFT, e, T.CONST n)) =
result (fn r => emit (A.OPER {
assem="sra `d0, `s0, " ^ int2str n,
src=[munchExp e],
dst=[r],
jump=NONE}))
| munchExp (T.BINOP (T.ARSHIFT, e1, e2)) =
result (fn r => emit (A.OPER {
assem="srav `d0, `s0, `s1",
src=[munchExp e1, munchExp e2],
dst=[r],
jump=NONE}))
| munchExp (T.CONST i) =
result(fn r => emit(A.OPER{
assem="li `d0, " ^ int2str i,
src=[],
dst=[r],
jump=NONE}))
| munchExp (T.MEM(e1)) =
result(fn r => emit(A.OPER{
assem="lw `d0, 0(`s0)",
src=[munchExp e1],dst=[r],jump=NONE}))
| munchExp (T.TEMP t) = t
| munchExp (T.NAME label) =
result(fn r => emit(A.OPER{
assem="la `d0, " ^ Symbol.name label,
src=[],
dst=[r],
jump=NONE}))
| munchExp (T.CALL(e,args)) =
(let
val pairs = map (fn r => (Temp.newtemp (), r)) Frame.callersaves
val srcs = map #1 pairs
fun fetch a r = T.MOVE(T.TEMP r, T.TEMP a)
fun store a r = T.MOVE(T.TEMP a, T.TEMP r)
in
map (fn (a,r) => munchStm(store a r)) pairs;
result(fn r => emit(A.OPER{
assem="jalr `s0",
src=munchExp(e) :: munchArgs(0,args),
dst=calldefs,
jump=NONE}));
map (fn (a,r) => munchStm(fetch a r)) (List.rev pairs);
Frame.RV
end)
(* generate code to move all arguments to their correct positions.
* In SPIM MIPS, we use a0-a3 to store first four parameters, and
* others go to frame. The result of this function is a list of
* temporaries that are to be passed to the machine's CALL function. *)
and munchArgs (_, nil) = nil
| munchArgs (i, exp :: rest) =
let val len = List.length Frame.argregs in
if i < len then
let val dst = List.nth(Frame.argregs,i)
val src = munchExp(exp) in
munchStm(T.MOVE(T.TEMP dst,T.TEMP src));
dst :: munchArgs(i+1,rest)
end
else raise TooManyArgs("too many arguments!") (* TODO: spilling *)
end
in munchStm stm;
rev(!ilist)
end
end