-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate.sml
341 lines (287 loc) · 11.3 KB
/
translate.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
signature TRANSLATE =
sig
structure Frame : FRAME
type level
type access
val outermost : level
val newLevel : {parent: level,
name: Temp.label,
formals: bool list} -> level
val formals : level -> access list
val allocLocal : level -> bool -> access
type compilation
val newCompilation : unit -> compilation
val getResult : compilation -> Frame.frag list
type exp
val simpleVar : access * level -> exp
val fieldVar : exp * int -> exp
val subscriptVar : exp * exp -> exp
val nilExp : exp
val intExp : int -> exp
val stringExp : compilation * string -> exp
val proc : compilation * level * exp -> unit
val callExp : Temp.label * level * level * exp list -> exp
val opExp : Absyn.oper * Types.ty -> exp * exp -> exp
val recordExp : exp list -> exp
val arrayExp : exp * exp -> exp
val seqExp : exp list -> exp
val assignExp : exp * exp -> exp
val ifThenExp : exp * exp -> exp
val ifExp : exp * exp * exp -> exp
val whileExp : exp * exp * Temp.label -> exp
val forExp : access * exp * exp * exp * Temp.label -> exp
val breakExp : Temp.label -> exp
end
structure Translate : TRANSLATE =
struct
structure A = Absyn
structure Frame = MIPSFrame
structure T = Tree
structure Ty = Types
val wordSize = Frame.wordSize
val impossible = ErrorMsg.impossible
val unimplemented = ErrorMsg.unimplemented
datatype level = Outermost
| Inner of level * Frame.frame * unit ref
type access = level * Frame.access
val outermost = Outermost
(* adds additional formal parameter for static link *)
fun newLevel {parent, name, formals} =
Inner(parent, Frame.newFrame {name=name, formals=true::formals}, ref())
fun sameLevel (Outermost, Outermost) = true
| sameLevel (Inner(_,_,ref1), Inner(_,_,ref2)) = ref1 = ref2
| sameLevel _ = false
fun staticLink Outermost = impossible "staticLink Outermost"
| staticLink (Inner(_,frame,_)) =
(case Frame.formals frame
of [] => impossible "missing static link"
| x::_ => x)
fun formals Outermost = impossible "formals Outermost"
| formals (l as Inner(_,frame,_)) =
(case Frame.formals frame
of [] => impossible "missing static link"
| _::formals => map (fn a => (l,a)) formals)
fun allocLocal Outermost _ = impossible "allocLocal Outermost"
| allocLocal (l as Inner(_,frame,_)) escape =
(l, Frame.allocLocal frame escape)
(* fragment lists *)
type compilation = Frame.frag list ref
fun newCompilation() : compilation = ref []
fun addFrag (comp, frag) = comp := frag :: !comp
fun getResult comp = rev(!comp)
(* helpers for building IR trees *)
fun seq [] = impossible "empty seq"
| seq [x] = x
| seq (x::xs) = T.SEQ(x, seq xs)
fun memOffset (base, offset) = T.MEM(T.BINOP(T.PLUS, base, offset))
(* expression conversion functions *)
datatype exp = Ex of T.exp
| Nx of T.stm
| Cx of Temp.label * Temp.label -> T.stm
fun unEx (Ex e) = e
| unEx (Nx(T.EXP e)) = e
| unEx (Nx s) = T.ESEQ(s, T.CONST 0)
| unEx (Cx genstm) = let
val r = T.TEMP(Temp.newtemp())
val t = Temp.newlabel()
val f = Temp.newlabel()
in T.ESEQ(seq [T.MOVE(r, T.CONST 1),
genstm(t, f),
T.LABEL f,
T.MOVE(r, T.CONST 0),
T.LABEL t],
r)
end
fun unNx (Nx s) = s
| unNx (Ex e) = T.EXP e
| unNx (Cx genstm) = let
val l = Temp.newlabel()
in seq [genstm(l, l), T.LABEL l]
end
fun unCx (Cx genstm) = genstm
| unCx (Ex(T.CONST 0)) = (fn (_, f) => T.JUMP(T.NAME f, [f]))
| unCx (Ex(T.CONST _)) = (fn (t, _) => T.JUMP(T.NAME t, [t]))
| unCx (Ex e) = (fn (t, f) => T.CJUMP(T.EQ, e, T.CONST 0, f, t))
| unCx (Nx _) = impossible "unCx(Nx _)"
(* building IR expressions *)
(* Returns a Tree.exp that computes the address of the frame at level declvl
* when the current level is curlvl.
*)
fun framePtr (Outermost, _) = impossible "framePtr(Outermost, _)"
| framePtr (_, Outermost) = impossible "framePtr(_, Outermost)"
| framePtr (declvl, curlvl) = let
fun fptr (Outermost, _) = impossible "could not find declared level"
| fptr (l as Inner(parent,_,_), exp) =
if sameLevel(l, declvl) then exp
else fptr(parent, Frame.expOfAccess (staticLink l) exp)
in fptr (curlvl, T.TEMP Frame.FP)
end
fun simpleVar (_, Outermost) = impossible "simpleVar(_, Outermost)"
| simpleVar ((declvl, access), curlvl) =
Ex(Frame.expOfAccess access (framePtr(declvl, curlvl)))
fun fieldVar (exp, i) =
Ex(memOffset(unEx exp, T.CONST(i * wordSize)))
fun subscriptVar (exp, iexp) = let
val offset = T.BINOP(T.MUL, unEx iexp, T.CONST wordSize)
in Ex(memOffset(unEx exp, offset))
end
val nilExp = Ex(T.CONST 0)
fun intExp i = Ex(T.CONST i)
fun stringExp (comp, s) = let
val l = Temp.newlabel()
in addFrag(comp, Frame.STRING(l, s));
Ex(T.NAME l)
end
fun proc (_, Outermost, _) = impossible "proc(_, Outermost, _)"
| proc (comp, Inner(_,frame,_), body) = let
val body = T.MOVE(T.TEMP Frame.RV, unEx body)
val body = Frame.procEntryExit1(frame, body)
in addFrag(comp, Frame.PROC{frame=frame, body=body})
end
fun callExp (f, Outermost, _, args) =
Ex(T.CALL(T.NAME f, map unEx args))
| callExp (f, Inner(declvl,_,_), curlvl, args) = let
val sl = framePtr(declvl, curlvl)
in Ex(T.CALL(T.NAME f, sl :: map unEx args))
end
fun arith oper (lt, rt) = Ex(T.BINOP(oper, unEx lt, unEx rt))
fun cmpString oper (lt, rt) = let
fun id (x, y) = (x, y)
fun sw (x, y) = (y, x)
val test = Frame.externalCall("stringEqual", [unEx lt, unEx rt])
val labsw =
case oper
of T.EQ => id
| T.NE => sw
| T.LT => unimplemented()
| T.LE => unimplemented()
| T.GT => unimplemented()
| T.GE => unimplemented()
| _ => impossible "bad comparison operator"
fun genstm (t, f) = T.CJUMP(T.EQ, test, T.CONST 0, f, t)
in Cx(genstm o labsw)
end
fun cmpScalar oper (lt, rt) =
Cx(fn (t, f) => T.CJUMP(oper, unEx lt, unEx rt, t, f))
fun opExp (A.PlusOp, _) = arith T.PLUS
| opExp (A.MinusOp, _) = arith T.MINUS
| opExp (A.TimesOp, _) = arith T.MUL
| opExp (A.DivideOp, _) = arith T.DIV
| opExp (A.EqOp, Ty.STRING) = cmpString T.EQ
| opExp (A.NeqOp, Ty.STRING) = cmpString T.NE
| opExp (A.LtOp, Ty.STRING) = cmpString T.LT
| opExp (A.LeOp, Ty.STRING) = cmpString T.LE
| opExp (A.GtOp, Ty.STRING) = cmpString T.GT
| opExp (A.GeOp, Ty.STRING) = cmpString T.GE
| opExp (A.EqOp, _) = cmpScalar T.EQ
| opExp (A.NeqOp, _) = cmpScalar T.NE
| opExp (A.LtOp, _) = cmpScalar T.LT
| opExp (A.LeOp, _) = cmpScalar T.LE
| opExp (A.GtOp, _) = cmpScalar T.GT
| opExp (A.GeOp, _) = cmpScalar T.GE
fun recordExp fields = let
val size = length fields * wordSize
val base = Frame.externalCall("allocRecord", [T.CONST size])
val r = T.TEMP(Temp.newtemp())
fun moveField (field, (moves, offset)) = let
val move = T.MOVE(memOffset(r, T.CONST offset), unEx field)
in (move::moves, offset + wordSize)
end
val (moves, _) = foldl moveField ([], 0) fields
in Ex(T.ESEQ(seq (T.MOVE(r, base) :: moves), r))
end
fun arrayExp (size, init) =
Ex(Frame.externalCall("initArray", [unEx size, unEx init]))
fun seqExp [] = Nx(T.EXP(T.CONST 0))
| seqExp [x] = x
| seqExp (x::xs) = Ex(T.ESEQ(unNx x, unEx(seqExp xs)))
fun assignExp (var, exp) = Nx(T.MOVE(unEx var, unEx exp))
fun ifThenExp (test, texp) = let
val t = Temp.newlabel()
val f = Temp.newlabel()
val testgen = unCx test
val tstm = unNx texp
in Nx(seq [testgen(t, f), T.LABEL t, tstm, T.LABEL f])
end
fun ifCondBool (test, tgen, fb) = let
val z = Temp.newlabel()
val testgen = unCx test
in Cx(fn (t, f) => seq [testgen(z, if fb then t else f),
T.LABEL z,
tgen(t, f)])
end
fun ifBoolCond (test, tb, fgen) = let
val z = Temp.newlabel()
val testgen = unCx test
in Cx(fn (t, f) => seq [testgen(if tb then t else f, z),
T.LABEL z,
fgen(t, f)])
end
fun ifStm (test, tstm, fstm) = let
val t = Temp.newlabel()
val f = Temp.newlabel()
val j = Temp.newlabel()
val testgen = unCx test
in Nx(seq [testgen(t, f),
T.LABEL t,
tstm,
T.JUMP(T.NAME j, [j]),
T.LABEL f,
fstm,
T.LABEL j])
end
fun ifExp (test, Cx tgen, Ex(T.CONST 0)) = ifCondBool(test, tgen, false)
| ifExp (test, Cx tgen, Ex(T.CONST 1)) = ifCondBool(test, tgen, true)
| ifExp (test, Ex(T.CONST 0), Cx fgen) = ifBoolCond(test, false, fgen)
| ifExp (test, Ex(T.CONST 1), Cx fgen) = ifBoolCond(test, true, fgen)
| ifExp (test, Cx tgen, Cx fgen) = let
val y = Temp.newlabel()
val z = Temp.newlabel()
val testgen = unCx test
in Cx(fn (t, f) => seq [testgen(y, z),
T.LABEL y, tgen(t, f),
T.LABEL z, fgen(t, f)])
end
| ifExp (test, Nx tstm, fexp) = ifStm(test, tstm, unNx fexp)
| ifExp (test, texp, Nx fstm) = ifStm(test, unNx texp, fstm)
| ifExp (test, texp, fexp) = let
val r = T.TEMP(Temp.newtemp())
val t = Temp.newlabel()
val f = Temp.newlabel()
val j = Temp.newlabel()
val testgen = unCx test
in Ex(T.ESEQ(seq [testgen(t, f),
T.LABEL t,
T.MOVE(r, unEx texp),
T.JUMP(T.NAME j, [j]),
T.LABEL f,
T.MOVE(r, unEx fexp),
T.LABEL j],
r))
end
fun whileExp (test, body, donelab) = let
val testlab = Temp.newlabel()
val bodylab = Temp.newlabel()
val testgen = unCx test
in Nx(seq [T.LABEL testlab,
testgen(bodylab, donelab),
T.LABEL bodylab,
unNx body,
T.JUMP(T.NAME testlab, [testlab]),
T.LABEL donelab])
end
fun forExp ((_, access), lo, hi, body, donelab) = let
val var = Frame.expOfAccess access (T.TEMP Frame.FP)
val testlab = Temp.newlabel()
val looplab = Temp.newlabel()
in Nx(seq [T.MOVE(var, unEx lo),
T.JUMP(T.NAME testlab, [testlab]),
T.LABEL looplab,
unNx body,
T.MOVE(var, T.BINOP(T.PLUS, var, T.CONST 1)),
T.LABEL testlab,
T.CJUMP(T.LT, var, unEx hi, looplab, donelab),
T.LABEL donelab])
end
fun breakExp donelab = Nx(T.JUMP(T.NAME donelab, [donelab]))
end