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 407 - (view) (download)

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 407 | S.E_Input(_, name, NONE) =>
66 :     [(lhs, IL.OP(HighOps.Input(IL.Var.ty lhs, name), []))]
67 : jhr 229 | S.E_Input(_, name, SOME dflt) =>
68 : jhr 407 [(lhs, IL.OP(HighOps.InputWithDefault(IL.Var.ty lhs, name), [lookup env dflt]))]
69 : jhr 226 | S.E_Field fld => [(lhs, IL.OP(HighOps.Field fld, []))]
70 :     | S.E_LoadImage info => [(lhs, IL.OP(HighOps.LoadImage info, []))]
71 : jhr 176 (* end case *))
72 : jhr 168
73 : jhr 192 (* convert a Simple AST block to an IL statement. We return the statement that represents the
74 :     * block, plus the environment mapping Simple AST variables to their current SSA representations
75 :     * and the set of Simple AST variables that were assigned to in the block.
76 : jhr 189 *)
77 : jhr 256 fun cvtBlock (env, S.Block stms, optExit) = let
78 :     fun toBlock (env, assigned, [], assignments) = let
79 :     val stm = IL.Stmt.mkBLOCK(List.rev assignments, optExit)
80 :     in
81 :     (stm, IL.Stmt.tail stm, env, assigned)
82 :     end
83 : jhr 192 | toBlock (env, assigned, S.S_Assign(x, e)::rest, assignments) = let
84 : jhr 189 val x' = newVar x
85 :     val stms = cvtExp(env, x', e)
86 :     val assigned = VSet.add(assigned, x)
87 :     val env = VMap.insert(env, x, x')
88 :     in
89 : jhr 197 toBlock (env, assigned, rest, stms@assignments)
90 : jhr 189 end
91 : jhr 192 | toBlock (env, assigned, stms, assignments) = let
92 : jhr 256 val (next, last, env, assigned) = toStmt (env, assigned, stms)
93 :     val blk = IL.Stmt.mkBLOCK(List.rev assignments, SOME next)
94 : jhr 192 in
95 : jhr 256 IL.Node.addEdge (IL.Stmt.tail blk, IL.Stmt.entry next);
96 :     (blk, last, env, assigned)
97 : jhr 192 end
98 : jhr 256 and toStmt (env, assigned, []) = let
99 :     (* this case only occurs for the empty else arm of an if-then-else statement *)
100 :     val stm = IL.Stmt.mkBLOCK([], optExit)
101 :     in
102 :     (stm, IL.Stmt.tail stm, env, assigned)
103 :     end
104 : jhr 197 | toStmt (env, assigned, stms as stm::rest) = (case stm
105 :     of S.S_Assign _ => toBlock (env, assigned, stms, [])
106 : jhr 192 | S.S_IfThenElse(x, b1, b2) => let
107 :     val x' = lookup env x
108 : jhr 256 val (s1, last1, env1, assigned1) = cvtBlock(env, b1, NONE)
109 :     val (s2, last2, env2, assigned2) = cvtBlock(env, b2, NONE)
110 : jhr 192 val assigned = VSet.union(assigned1, assigned2)
111 : jhr 240 (* PROBLEM: what about variables that are assigned for the first time in one branch
112 :     * and not the other? This situation should only occur for variables who's scope is
113 :     * the branch of the if. Short-term solution is to ignore variables that are defined
114 :     * in only one branch.
115 :     *)
116 : jhr 192 val (env, phis) = let
117 : jhr 240 fun mkPhi (x, (env, phis)) = (
118 :     case (VMap.find(env1, x), VMap.find(env2, x))
119 :     of (SOME x1, SOME x2) => let
120 :     val x' = newVar x
121 :     in
122 :     (VMap.insert(env, x, x'), (x', [x1, x2])::phis)
123 :     end
124 :     | _ => (env, phis)
125 :     (* end case *))
126 : jhr 190 in
127 : jhr 192 VSet.foldl mkPhi (env, []) assigned
128 : jhr 190 end
129 :     in
130 : jhr 192 case rest
131 : jhr 256 of [] => let
132 :     val join = IL.Stmt.mkJOIN (phis, optExit)
133 :     val joinNd = IL.Stmt.entry join
134 :     val stm = IL.Stmt.mkIF(x', s1, s2, SOME join)
135 :     in
136 :     IL.Node.addEdge (last2, joinNd);
137 :     IL.Node.addEdge (last1, joinNd);
138 :     (stm, joinNd, env, assigned)
139 :     end
140 : jhr 192 | _ => let
141 : jhr 256 val (next, last, env, assigned) = toStmt (env, assigned, rest)
142 :     val join = IL.Stmt.mkJOIN (phis, SOME next)
143 :     val joinNd = IL.Stmt.entry join
144 :     val stm = IL.Stmt.mkIF(x', s1, s2, SOME join)
145 : jhr 192 in
146 : jhr 256 IL.Node.addEdge (last2, joinNd);
147 :     IL.Node.addEdge (last1, joinNd);
148 :     IL.Node.addEdge (joinNd, IL.Stmt.entry next);
149 :     (stm, last, env, assigned)
150 : jhr 192 end
151 :     (* end case *)
152 : jhr 190 end
153 : jhr 192 | S.S_New(name, xs) => let
154 :     val xs' = List.map (lookup env) xs
155 :     in
156 :     case rest
157 : jhr 256 of [] => let
158 :     val stm = IL.Stmt.mkNEW(name, xs', optExit)
159 :     in
160 :     (stm, IL.Stmt.tail stm, env, assigned)
161 :     end
162 : jhr 192 | _ => let
163 : jhr 256 val (next, last, env, assigned) = toStmt (env, assigned, rest)
164 :     val stm = IL.Stmt.mkNEW(name, xs', SOME next)
165 : jhr 192 in
166 : jhr 256 IL.Node.addEdge (IL.Stmt.tail stm, IL.Stmt.entry next);
167 :     (stm, last, env, assigned)
168 : jhr 192 end
169 : jhr 194 end
170 : jhr 256 | S.S_Die => let
171 :     val stm = IL.Stmt.mkDIE()
172 :     in
173 :     (stm, IL.Stmt.tail stm, env, assigned)
174 :     end
175 :     | S.S_Stabilize => let
176 :     val stm = IL.Stmt.mkSTABILIZE()
177 :     in
178 :     (stm, IL.Stmt.tail stm, env, assigned)
179 :     end
180 : jhr 192 (* end case *))
181 :     in
182 :     toStmt (env, VSet.empty, stms)
183 :     end
184 : jhr 168
185 : jhr 256 fun cvtTopLevelBlock (env, blk) = let
186 :     val exit = IL.Stmt.mkEXIT ()
187 :     val (stm, last, env, assigned) = cvtBlock (env, blk, SOME exit)
188 :     val entry = IL.Stmt.mkENTRY (SOME stm)
189 :     in
190 :     IL.Node.addEdge (IL.Stmt.tail entry, IL.Stmt.entry stm);
191 :     (* NOTE: this could fail if all control paths end in DIE or STABILIZE, so we
192 :     * wrap it in a handler
193 :     *)
194 :     IL.Node.addEdge (last, IL.Stmt.entry exit) handle _ => ();
195 :     (entry, env)
196 :     end
197 :    
198 :     (* generate fresh SSA variables and add them to the environment *)
199 :     fun freshVars (env, xs) = let
200 :     fun cvtVar (x, (env, xs)) = let
201 :     val x' = newVar x
202 :     in
203 :     (VMap.insert(env, x, x'), x'::xs)
204 :     end
205 :     val (env, xs) = List.foldl cvtVar (env, []) xs
206 :     in
207 :     (env, List.rev xs)
208 :     end
209 :    
210 : jhr 240 fun translate (S.Program{globals, globalInit, actors}) = let
211 : jhr 256 val (globalInit, env) = cvtTopLevelBlock (VMap.empty, globalInit)
212 :     (* get the SSA names for the globals and a reduced environment that just defines
213 :     * the globals.
214 :     *)
215 : jhr 200 val (env, globs) = let
216 :     val lookup = lookup env
217 :     fun cvtVar (x, (env, globs)) = let
218 :     val x' = lookup x
219 :     in
220 :     (VMap.insert(env, x, x'), x'::globs)
221 :     end
222 :     val (env, globs) = List.foldl cvtVar (VMap.empty, []) globals
223 :     in
224 :     (env, List.rev globs)
225 :     end
226 :     fun cvtActor (S.Actor{name, params, state, stateInit, methods}) = let
227 :     val (env, params) = let
228 :     fun cvtParam (x, (env, xs)) = let
229 :     val x' = newVar x
230 :     in
231 :     (VMap.insert(env, x, x'), x'::xs)
232 :     end
233 :     val (env, params) = List.foldl cvtParam (env, []) params
234 :     in
235 :     (env, List.rev params)
236 :     end
237 : jhr 256 val (stateInit, env) = cvtTopLevelBlock (env, stateInit)
238 :     val state' = List.map (lookup env) state
239 : jhr 200 fun cvtMethod (S.Method(name, blk)) = let
240 : jhr 256 (* allocate fresh variables for the state variables *)
241 :     val (env, stateIn) = freshVars (env, state)
242 :     val (body, env) = cvtTopLevelBlock (env, blk)
243 :     val stateOut = List.map (lookup env) state
244 : jhr 200 in
245 : jhr 256 IL.Method{name=name, stateIn=stateIn, stateOut=stateOut, body=body}
246 : jhr 200 end
247 :     in
248 :     IL.Actor{
249 :     name = name,
250 :     params = params,
251 : jhr 256 state = state',
252 : jhr 200 stateInit = stateInit,
253 :     methods = List.map cvtMethod methods
254 :     }
255 :     end
256 :     in
257 :     IL.Program{
258 :     globals = globs,
259 :     globalInit = globalInit,
260 :     actors = List.map cvtActor actors
261 :     }
262 :     end
263 : jhr 176
264 : jhr 137 end

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