Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/lamont_dev/src/compiler/translate/translate.sml
ViewVC logotype

Annotation of /branches/lamont_dev/src/compiler/translate/translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 400 - (view) (download)
Original Path: trunk/src/compiler/translate/translate.sml

1 : jhr 137 (* translate.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 : jhr 176 * Translate Simple-AST code into the IL representation.
7 : jhr 137 *)
8 :    
9 :     structure Translate : sig
10 :    
11 : jhr 176 val translate : Simple.program -> HighIL.program
12 : jhr 137
13 :     end = struct
14 :    
15 : jhr 176 structure S = Simple
16 : jhr 394 structure Ty = Types
17 : jhr 137 structure VMap = Var.Map
18 : jhr 189 structure VSet = Var.Set
19 : jhr 168 structure IL = HighIL
20 : jhr 394 structure DstTy = HighILTypes
21 : jhr 137
22 : jhr 197 fun lookup env x = (case VMap.find (env, x)
23 : jhr 176 of SOME x' => x'
24 :     | NONE => raise Fail(concat[
25 : jhr 197 "no binding for ", Var.uniqueNameOf x, " in environment"
26 : jhr 176 ])
27 :     (* end case *))
28 :    
29 : jhr 394 fun cvtTy ty = (case TypeUtil.prune ty
30 :     of Ty.T_Bool => DstTy.BoolTy
31 :     | Ty.T_Int => DstTy.IntTy
32 :     | Ty.T_String => DstTy.StringTy
33 :     | Ty.T_Kernel _ => DstTy.KernelTy
34 :     | Ty.T_Tensor(Ty.Shape dd) => DstTy.TensorTy(List.map (fn (Ty.DimConst d) => d) dd)
35 :     | Ty.T_Image _ => DstTy.ImageTy
36 :     | Ty.T_Field _ => DstTy.FieldTy
37 :     | ty => raise Fail("cvtTy: unexpected " ^ TypeUtil.toString ty)
38 :     (* end case *))
39 :    
40 : jhr 189 (* create a new instance of a variable *)
41 : jhr 394 fun newVar x = IL.Var.new (Var.nameOf x, cvtTy(Var.monoTypeOf x))
42 : jhr 189
43 : jhr 168 (* expression translation *)
44 : jhr 197 fun cvtExp (env, lhs, exp) = (case exp
45 : jhr 188 of S.E_Var x => [(lhs, IL.VAR(lookup env x))]
46 :     | S.E_Lit lit => [(lhs, IL.LIT lit)]
47 : jhr 176 | S.E_Tuple xs => raise Fail "E_Tuple not implemeted"
48 : jhr 188 | S.E_Apply(f, tyArgs, args, ty) => let
49 :     val args' = List.map (lookup env) args
50 :     in
51 :     TranslateBasis.translate (lhs, f, tyArgs, args')
52 :     end
53 :     | S.E_Cons args => [(lhs, IL.CONS(List.map (lookup env) args))]
54 : jhr 400 | S.E_Slice(x, indices, ty) => let
55 :     val x = lookup env x
56 :     val mask = List.map isSome indices
57 :     fun cvt NONE = NONE
58 :     | cvt (SOME x) = SOME(lookup env x)
59 :     val indices = List.mapPartial cvt indices
60 :     in
61 :     if List.all (fn b => b) mask
62 :     then [(lhs, IL.OP(HighOps.Subscript(IL.Var.ty x), x::indices))]
63 :     else [(lhs, IL.OP(HighOps.Slice(IL.Var.ty lhs, mask), x::indices))]
64 :     end
65 : jhr 229 | S.E_Input(_, name, NONE) => [(lhs, IL.OP(HighOps.Input name, []))]
66 :     | S.E_Input(_, name, SOME dflt) =>
67 : jhr 226 [(lhs, IL.OP(HighOps.InputWithDefault name, [lookup env dflt]))]
68 :     | S.E_Field fld => [(lhs, IL.OP(HighOps.Field fld, []))]
69 :     | S.E_LoadImage info => [(lhs, IL.OP(HighOps.LoadImage info, []))]
70 : jhr 176 (* end case *))
71 : jhr 168
72 : jhr 192 (* convert a Simple AST block to an IL statement. We return the statement that represents the
73 :     * block, plus the environment mapping Simple AST variables to their current SSA representations
74 :     * and the set of Simple AST variables that were assigned to in the block.
75 : jhr 189 *)
76 : jhr 256 fun cvtBlock (env, S.Block stms, optExit) = let
77 :     fun toBlock (env, assigned, [], assignments) = let
78 :     val stm = IL.Stmt.mkBLOCK(List.rev assignments, optExit)
79 :     in
80 :     (stm, IL.Stmt.tail stm, env, assigned)
81 :     end
82 : jhr 192 | toBlock (env, assigned, S.S_Assign(x, e)::rest, assignments) = let
83 : jhr 189 val x' = newVar x
84 :     val stms = cvtExp(env, x', e)
85 :     val assigned = VSet.add(assigned, x)
86 :     val env = VMap.insert(env, x, x')
87 :     in
88 : jhr 197 toBlock (env, assigned, rest, stms@assignments)
89 : jhr 189 end
90 : jhr 192 | toBlock (env, assigned, stms, assignments) = let
91 : jhr 256 val (next, last, env, assigned) = toStmt (env, assigned, stms)
92 :     val blk = IL.Stmt.mkBLOCK(List.rev assignments, SOME next)
93 : jhr 192 in
94 : jhr 256 IL.Node.addEdge (IL.Stmt.tail blk, IL.Stmt.entry next);
95 :     (blk, last, env, assigned)
96 : jhr 192 end
97 : jhr 256 and toStmt (env, assigned, []) = let
98 :     (* this case only occurs for the empty else arm of an if-then-else statement *)
99 :     val stm = IL.Stmt.mkBLOCK([], optExit)
100 :     in
101 :     (stm, IL.Stmt.tail stm, env, assigned)
102 :     end
103 : jhr 197 | toStmt (env, assigned, stms as stm::rest) = (case stm
104 :     of S.S_Assign _ => toBlock (env, assigned, stms, [])
105 : jhr 192 | S.S_IfThenElse(x, b1, b2) => let
106 :     val x' = lookup env x
107 : jhr 256 val (s1, last1, env1, assigned1) = cvtBlock(env, b1, NONE)
108 :     val (s2, last2, env2, assigned2) = cvtBlock(env, b2, NONE)
109 : jhr 192 val assigned = VSet.union(assigned1, assigned2)
110 : jhr 240 (* PROBLEM: what about variables that are assigned for the first time in one branch
111 :     * and not the other? This situation should only occur for variables who's scope is
112 :     * the branch of the if. Short-term solution is to ignore variables that are defined
113 :     * in only one branch.
114 :     *)
115 : jhr 192 val (env, phis) = let
116 : jhr 240 fun mkPhi (x, (env, phis)) = (
117 :     case (VMap.find(env1, x), VMap.find(env2, x))
118 :     of (SOME x1, SOME x2) => let
119 :     val x' = newVar x
120 :     in
121 :     (VMap.insert(env, x, x'), (x', [x1, x2])::phis)
122 :     end
123 :     | _ => (env, phis)
124 :     (* end case *))
125 : jhr 190 in
126 : jhr 192 VSet.foldl mkPhi (env, []) assigned
127 : jhr 190 end
128 :     in
129 : jhr 192 case rest
130 : jhr 256 of [] => let
131 :     val join = IL.Stmt.mkJOIN (phis, optExit)
132 :     val joinNd = IL.Stmt.entry join
133 :     val stm = IL.Stmt.mkIF(x', s1, s2, SOME join)
134 :     in
135 :     IL.Node.addEdge (last2, joinNd);
136 :     IL.Node.addEdge (last1, joinNd);
137 :     (stm, joinNd, env, assigned)
138 :     end
139 : jhr 192 | _ => let
140 : jhr 256 val (next, last, env, assigned) = toStmt (env, assigned, rest)
141 :     val join = IL.Stmt.mkJOIN (phis, SOME next)
142 :     val joinNd = IL.Stmt.entry join
143 :     val stm = IL.Stmt.mkIF(x', s1, s2, SOME join)
144 : jhr 192 in
145 : jhr 256 IL.Node.addEdge (last2, joinNd);
146 :     IL.Node.addEdge (last1, joinNd);
147 :     IL.Node.addEdge (joinNd, IL.Stmt.entry next);
148 :     (stm, last, env, assigned)
149 : jhr 192 end
150 :     (* end case *)
151 : jhr 190 end
152 : jhr 192 | S.S_New(name, xs) => let
153 :     val xs' = List.map (lookup env) xs
154 :     in
155 :     case rest
156 : jhr 256 of [] => let
157 :     val stm = IL.Stmt.mkNEW(name, xs', optExit)
158 :     in
159 :     (stm, IL.Stmt.tail stm, env, assigned)
160 :     end
161 : jhr 192 | _ => let
162 : jhr 256 val (next, last, env, assigned) = toStmt (env, assigned, rest)
163 :     val stm = IL.Stmt.mkNEW(name, xs', SOME next)
164 : jhr 192 in
165 : jhr 256 IL.Node.addEdge (IL.Stmt.tail stm, IL.Stmt.entry next);
166 :     (stm, last, env, assigned)
167 : jhr 192 end
168 : jhr 194 end
169 : jhr 256 | S.S_Die => let
170 :     val stm = IL.Stmt.mkDIE()
171 :     in
172 :     (stm, IL.Stmt.tail stm, env, assigned)
173 :     end
174 :     | S.S_Stabilize => let
175 :     val stm = IL.Stmt.mkSTABILIZE()
176 :     in
177 :     (stm, IL.Stmt.tail stm, env, assigned)
178 :     end
179 : jhr 192 (* end case *))
180 :     in
181 :     toStmt (env, VSet.empty, stms)
182 :     end
183 : jhr 168
184 : jhr 256 fun cvtTopLevelBlock (env, blk) = let
185 :     val exit = IL.Stmt.mkEXIT ()
186 :     val (stm, last, env, assigned) = cvtBlock (env, blk, SOME exit)
187 :     val entry = IL.Stmt.mkENTRY (SOME stm)
188 :     in
189 :     IL.Node.addEdge (IL.Stmt.tail entry, IL.Stmt.entry stm);
190 :     (* NOTE: this could fail if all control paths end in DIE or STABILIZE, so we
191 :     * wrap it in a handler
192 :     *)
193 :     IL.Node.addEdge (last, IL.Stmt.entry exit) handle _ => ();
194 :     (entry, env)
195 :     end
196 :    
197 :     (* generate fresh SSA variables and add them to the environment *)
198 :     fun freshVars (env, xs) = let
199 :     fun cvtVar (x, (env, xs)) = let
200 :     val x' = newVar x
201 :     in
202 :     (VMap.insert(env, x, x'), x'::xs)
203 :     end
204 :     val (env, xs) = List.foldl cvtVar (env, []) xs
205 :     in
206 :     (env, List.rev xs)
207 :     end
208 :    
209 : jhr 240 fun translate (S.Program{globals, globalInit, actors}) = let
210 : jhr 256 val (globalInit, env) = cvtTopLevelBlock (VMap.empty, globalInit)
211 :     (* get the SSA names for the globals and a reduced environment that just defines
212 :     * the globals.
213 :     *)
214 : jhr 200 val (env, globs) = let
215 :     val lookup = lookup env
216 :     fun cvtVar (x, (env, globs)) = let
217 :     val x' = lookup x
218 :     in
219 :     (VMap.insert(env, x, x'), x'::globs)
220 :     end
221 :     val (env, globs) = List.foldl cvtVar (VMap.empty, []) globals
222 :     in
223 :     (env, List.rev globs)
224 :     end
225 :     fun cvtActor (S.Actor{name, params, state, stateInit, methods}) = let
226 :     val (env, params) = let
227 :     fun cvtParam (x, (env, xs)) = let
228 :     val x' = newVar x
229 :     in
230 :     (VMap.insert(env, x, x'), x'::xs)
231 :     end
232 :     val (env, params) = List.foldl cvtParam (env, []) params
233 :     in
234 :     (env, List.rev params)
235 :     end
236 : jhr 256 val (stateInit, env) = cvtTopLevelBlock (env, stateInit)
237 :     val state' = List.map (lookup env) state
238 : jhr 200 fun cvtMethod (S.Method(name, blk)) = let
239 : jhr 256 (* allocate fresh variables for the state variables *)
240 :     val (env, stateIn) = freshVars (env, state)
241 :     val (body, env) = cvtTopLevelBlock (env, blk)
242 :     val stateOut = List.map (lookup env) state
243 : jhr 200 in
244 : jhr 256 IL.Method{name=name, stateIn=stateIn, stateOut=stateOut, body=body}
245 : jhr 200 end
246 :     in
247 :     IL.Actor{
248 :     name = name,
249 :     params = params,
250 : jhr 256 state = state',
251 : jhr 200 stateInit = stateInit,
252 :     methods = List.map cvtMethod methods
253 :     }
254 :     end
255 :     in
256 :     IL.Program{
257 :     globals = globs,
258 :     globalInit = globalInit,
259 :     actors = List.map cvtActor actors
260 :     }
261 :     end
262 : jhr 176
263 : jhr 137 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0