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

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