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

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