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

SCM Repository

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

Annotation of /trunk/src/compiler/translate/translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 435 - (view) (download)

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

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