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

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