3 * Author: Kaustuv Chaudhuri <kaustuv+@cs.cmu.edu>
4 * Modified by: Alex Vaynberg <alv@andrew.cmu.edu>
5 * Modified: Frank Pfenning <fp@cs.cmu.edu>
6 * Modified: Chris Lu <czl@andrew.cmu.edu>
7 * Modified: Joshua Wise <jwise@andrew.cmu.edu>
12 (* translate abstract syntax tree to IR tree *)
13 val translate : Ast.program -> Tree.func list
16 structure Trans :> TRANS =
20 structure AU = AstUtils
23 fun trans_oper A.PLUS = T.ADD
24 | trans_oper A.MINUS = T.SUB
25 | trans_oper A.TIMES = T.MUL
26 | trans_oper A.DIVIDEDBY = T.DIV
27 | trans_oper A.MODULO = T.MOD
28 | trans_oper A.LSH = T.LSH
29 | trans_oper A.RSH = T.RSH
30 | trans_oper A.LOGOR = T.LOGOR
31 | trans_oper A.LOGAND = T.LOGAND
32 | trans_oper A.BITOR = T.BITOR
33 | trans_oper A.BITXOR = T.BITXOR
34 | trans_oper A.BITAND = T.BITAND
35 | trans_oper A.NEQ = T.NEQ
36 | trans_oper A.EQ = T.EQ
37 | trans_oper A.LT = T.LT
38 | trans_oper A.LE = T.LE
39 | trans_oper A.GE = T.GE
40 | trans_oper A.GT = T.GT
41 | trans_oper _ = raise ErrorMsg.InternalError "expected AST binop, got AST unop"
43 fun translate (defs, funcs) =
45 val funclist = Symbol.elemsi funcs
47 val alignments = ref Symbol.empty (* Ref for memoization. *)
48 fun alignment A.Int = 4
49 | alignment (A.Typedef(id)) =
50 (case Symbol.look (!alignments) id
53 val r = alignment_s (Symbol.look' defs id)
54 val _ = (alignments := (Symbol.bind (!alignments) (id, r)))
59 | alignment (A.Pointer(_)) = 8
60 | alignment (A.Array(_)) = 8
61 | alignment (A.TNull) = raise ErrorMsg.InternalError "alignmentof TNull?"
62 and alignment_s (A.Struct(members)) =
64 (fn ((_,t),al) => Int.max (al, alignment t))
67 | alignment_s (A.MarkedTypedef(a)) = alignment_s (Mark.data a)
73 if (curpos mod al) = 0
75 else curpos + al - (curpos mod al)
78 val sizes = ref Symbol.empty
79 fun sizeof_v A.Int = 4
80 | sizeof_v (A.Typedef(id)) =
81 (case Symbol.look (!sizes) id
84 val r = sizeof_s (Symbol.look' defs id)
85 val _ = (sizes := (Symbol.bind (!sizes) (id, r)))
90 | sizeof_v (A.Pointer(_)) = 8
91 | sizeof_v (A.Array(_)) = 8
92 | sizeof_v (A.TNull) = raise ErrorMsg.InternalError "sizeof TNull?"
93 and sizeof_s (A.Struct(l)) =
95 (fn ((_,t),curpos) => align t curpos + sizeof_v t)
98 | sizeof_s (A.MarkedTypedef(a)) = sizeof_s (Mark.data a)
100 fun offset_s id (A.Typedef(id')) =
102 val shit = Symbol.look' defs id'
103 fun eat (A.Struct(l)) = l
104 | eat (A.MarkedTypedef(a)) = eat (Mark.data a)
105 fun offset_s' ((id1,t)::l') curofs =
107 val a = align t curofs
109 if Symbol.compare(id,id1) = EQUAL
111 else offset_s' l' (a + sizeof_v t)
113 | offset_s' nil _ = raise ErrorMsg.InternalError "looking for offset of something that isn't in the structure"
115 offset_s' (eat shit) 0
117 | offset_s _ _ = raise ErrorMsg.InternalError "cannot find offset into non-typedef"
119 fun type_s id (A.Typedef id') =
122 case AU.Typedef.data (Symbol.look' defs id')
124 | _ => raise ErrorMsg.InternalError "data didn't return struct"
125 fun type_s' ((id',t)::l) =
126 if (Symbol.compare (id, id') = EQUAL)
129 | type_s' nil = raise ErrorMsg.InternalError "struct member not found in type_s"
133 | type_s id _ = raise ErrorMsg.InternalError "cannot find internal type non-typedef"
135 fun deref (A.Pointer i) = i
136 | deref (A.Array i) = i
137 | deref _ = raise ErrorMsg.InternalError "cannot deref non-pointer"
139 fun trans_unop A.NEGATIVE = T.NEG
140 | trans_unop A.BITNOT = T.BITNOT
141 | trans_unop A.BANG = T.BANG
142 | trans_unop _ = raise ErrorMsg.InternalError "expected AST unop, got AST binop"
144 fun typeof' vartypes exp = TypeChecker.typeof (defs, funcs) vartypes NONE exp
146 fun trans_exp env vartypes (A.Var(id)) =
147 (* after type-checking, id must be declared; do not guard lookup *)
148 T.TEMP (Symbol.look' env id)
149 | trans_exp env vartypes (A.ConstExp c) = T.CONST(c)
150 | trans_exp env vartypes (A.OpExp(oper, [e1, e2])) =
151 T.BINOP(trans_oper oper, trans_exp env vartypes e1, trans_exp env vartypes e2)
152 | trans_exp env vartypes (A.OpExp(oper, [e])) =
153 T.UNOP(trans_unop oper, trans_exp env vartypes e)
154 | trans_exp env vartypes (A.OpExp(oper, _)) =
155 raise ErrorMsg.InternalError "expected one or two operands, got it in the oven"
156 | trans_exp env vartypes (A.Marked(marked_exp)) =
157 trans_exp env vartypes (Mark.data marked_exp)
158 | trans_exp env vartypes (A.FuncCall(func, stms)) =
161 (fn exp => (trans_exp env vartypes exp, AU.Type.size (typeof' vartypes exp)))
163 AU.Type.size (AU.Function.returntype (Symbol.look' funcs func)) )
164 | trans_exp env vartypes (A.Member (exp, id)) =
166 val apk = T.BINOP (T.ADD, trans_exp env vartypes exp, T.CONST (Word32.fromInt (offset_s id (typeof' vartypes exp))))
168 if (AU.Type.issmall (type_s id (typeof' vartypes exp)))
172 | trans_exp env vartypes (A.DerefMember (exp, id)) =
173 trans_exp env vartypes (A.Member (A.Dereference (exp), id))
174 | trans_exp env vartypes (A.Dereference(exp)) =
175 if (AU.Type.issmall (deref (typeof' vartypes exp)))
176 then T.MEMORY(trans_exp env vartypes exp)
177 else trans_exp env vartypes exp
178 | trans_exp env vartypes (A.ArrIndex(exp1, exp2)) =
180 val asubk = T.BINOP(T.ADD, trans_exp env vartypes exp1,
181 T.BINOP(T.MUL, trans_exp env vartypes exp2,
182 T.CONST(Word32.fromInt(sizeof_v (deref (typeof' vartypes exp1))))))
184 if (AU.Type.issmall (deref (typeof' vartypes exp1)))
188 | trans_exp env vartypes (A.New(tipo)) =
189 T.ALLOC(T.CONST (Word32.fromInt(sizeof_v tipo)))
190 | trans_exp env vartypes (A.NewArr(tipo, exp)) =
191 T.ALLOC(T.BINOP(T.MUL, trans_exp env vartypes exp, T.CONST(Word32.fromInt(sizeof_v tipo))))
192 | trans_exp env vartypes (A.Null) = T.CONST(0w0)
194 (* anything else should be impossible *)
196 (* trans_stms : Temp.temp Symbol.table -> (Label.label * Label.label) option -> A.stm list -> Tree.stm list
197 * translates a statement to the corresponding IR
198 * we pass around the environment and the current loop context, if any
199 * (usually called ls, which contains a continue label and a break label)
201 fun trans_stms vars vartypes ls (A.Assign(e1,e2)::stms) = T.MOVE(trans_exp vars vartypes e1, trans_exp vars vartypes e2, AU.Type.size (typeof' vartypes e2))::(trans_stms vars vartypes ls stms)
202 | trans_stms vars vartypes ls (A.AsnOp(oop,e1,e2)::stms) =
204 val te1 = trans_exp vars vartypes e1
205 val te2 = trans_exp vars vartypes e2
206 val t1 = T.TEMP (Temp.new "memory deref cache" 8)
207 val size = AU.Type.size (typeof' vartypes e2)
210 of T.MEMORY(m) => T.MOVE(t1, m, 8) :: T.MOVE (T.MEMORY(t1), T.BINOP(trans_oper oop, T.MEMORY(t1), te2), size) :: (trans_stms vars vartypes ls stms)
211 | _ => T.MOVE(te1, T.BINOP(trans_oper oop, te1, te2), size) :: (trans_stms vars vartypes ls stms)
213 | trans_stms vars vartypes ls (A.Return e::stms) =
215 val remainder = trans_stms vars vartypes ls stms
217 T.RETURN (trans_exp vars vartypes e, AU.Type.size (typeof' vartypes e))
220 | trans_stms vars vartypes ls (A.If(e, s, NONE)::stms) =
223 val strans = trans_stms vars vartypes ls s
224 val remainder = trans_stms vars vartypes ls stms
226 (T.JUMPIFN(trans_exp vars vartypes e, l)
231 | trans_stms vars vartypes ls (A.If(e, s, SOME s2)::stms) =
234 val l2 = Label.new ()
235 val s1trans = trans_stms vars vartypes ls s
236 val s2trans = trans_stms vars vartypes ls s2
237 val remainder = trans_stms vars vartypes ls stms
239 (T.JUMPIFN(trans_exp vars vartypes e, l)
241 @ [T.JUMP (l2), T.LABEL (l)]
246 | trans_stms vars vartypes ls (A.For(s1, e, s2, s)::stms) =
248 val head = Label.new ()
249 val tail = Label.new ()
250 val loop = Label.new ()
251 val stm1 = if isSome s1 then trans_stms vars vartypes NONE [valOf s1] else nil
252 val strans = trans_stms vars vartypes (SOME(loop,tail)) s
253 val stm2 = if isSome s2 then trans_stms vars vartypes NONE [valOf s2] else nil
254 val remainder = trans_stms vars vartypes ls stms
257 @ [T.LABEL head, T.JUMPIFN(trans_exp vars vartypes e, tail)]
261 @ [T.JUMP head, T.LABEL tail]
264 | trans_stms vars vartypes ls (A.While(e, s)::stms) =
266 val head = Label.new ()
267 val tail = Label.new ()
268 val strans = trans_stms vars vartypes (SOME(head,tail)) s
269 val remainder = trans_stms vars vartypes ls stms
272 :: T.JUMPIFN(trans_exp vars vartypes e, tail)
274 @ [T.JUMP head, T.LABEL tail]
277 | trans_stms vars vartypes ls (A.Effect(e)::stms) = (T.EFFECT (trans_exp vars vartypes e, AU.Type.size (typeof' vartypes e))) :: (trans_stms vars vartypes ls stms)
278 | trans_stms vars vartypes (SOME(b,e)) (A.Break::stms) =
280 val remainder = trans_stms vars vartypes (SOME(b,e)) stms
282 ((T.JUMP e) :: remainder)
284 | trans_stms vars vartypes NONE (A.Break::_) = raise ErrorMsg.InternalError "Break from invalid location... should have been caught in typechecker"
285 | trans_stms vars vartypes (SOME(b,e)) (A.Continue::stms) =
287 val remainder = trans_stms vars vartypes (SOME(b,e)) stms
289 ((T.JUMP b) :: remainder)
291 | trans_stms vars vartypes NONE (A.Continue::_) = raise ErrorMsg.InternalError "Continue from invalid location... should have been caught in typechecker"
292 | trans_stms vars vartypes ls (A.Nop::stms) = trans_stms vars vartypes ls stms
293 | trans_stms vars vartypes ls (A.MarkedStm m :: stms) = trans_stms vars vartypes ls ((Mark.data m) :: stms)
294 | trans_stms vars vartypes _ nil = nil
296 fun trans_funcs ((id, A.Extern(_, _))::l) = trans_funcs l
297 | trans_funcs ((id, A.MarkedFunction a)::l) = trans_funcs ((id, Mark.data a)::l)
298 | trans_funcs ((id, A.Function(t, args, vars, body))::l) =
302 Symbol.bind b (name, Temp.new (Symbol.name(name)) (AU.Type.size t)))
305 val vartypes = foldr (fn ((i, t), b) => Symbol.bind b (i, t)) Symbol.empty (args @ vars)
306 val b = trans_stms allvars vartypes NONE body
307 val (argn,_) = ListPair.unzip args
308 val numberedargs = ListPair.zip (List.tabulate (length argn, fn x => x), argn)
310 (fn (n, argname) => T.MOVE(T.TEMP (Symbol.look' allvars argname), T.ARG (n, Temp.size (Symbol.look' allvars argname)), Temp.size (Symbol.look' allvars argname)))
313 (T.FUNCTION(id, argmv @ b)) :: (trans_funcs l)
315 | trans_funcs nil = nil