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

SCM Repository

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

Annotation of /branches/lamont_dev/src/compiler/translate/translate.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 190 - (view) (download)
Original Path: trunk/src/compiler/translate/translate.sml

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 176 fun lookup env x = (case VMap.find x
21 :     of SOME x' => x'
22 :     | NONE => raise Fail(concat[
23 :     "no binding for ", Var.toString x, " in environment"
24 :     ])
25 :     (* end case *))
26 :    
27 : jhr 189 (* create a new instance of a variable *)
28 :     fun newVar x = IL.newVar (Var.nameOf x)
29 :    
30 : jhr 168 (* expression translation *)
31 : jhr 188 fun cvtExpr (env, lhs, exp) = (case exp
32 :     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 176 (* end case *))
42 : jhr 168
43 : jhr 190 fun cvtBlock (env, S.Block stms) =
44 :    
45 : jhr 189 (* convert a statement, where env is the mapping from Simple AST variables to
46 :     * their current SSA name, assigned is the set of AST variables assigned to
47 :     * in the current context, and stm is the statement to convert.
48 :     *)
49 :     and cvtStmt (env, assigned, stm, preStms, k) = (case stm
50 : jhr 190 of S.S_Assign(x, e) => let
51 : jhr 189 val x' = newVar x
52 :     val stms = cvtExp(env, x', e)
53 :     val assigned = VSet.add(assigned, x)
54 :     val env = VMap.insert(env, x, x')
55 :     in
56 :     k (env, assigned, stm::preStms)
57 :     end
58 : jhr 190 | S.S_IfThenElse(x, b1, b2) => let
59 :     val x' = lookup env x
60 :     val (b1, env1, assigned1) = block(env, b1)
61 :     val (b2, env2, assigned2) = block(env, b2)
62 :     val assigned = VSet.union(assigned1, assigned2)
63 :     val (env, phis) = let
64 :     fun mkPhi (x, (env, phis) = let
65 :     val x1 = lookup(env1, x)
66 :     val x2 = lookup(env2, x)
67 :     val x' = newVar x
68 :     in
69 :     (VMap.insert(env, x, x'), (x', [x1, x2])::phis)
70 :     end
71 :     in
72 :     VSet.foldl mkPhi (env, []) assigned
73 :     end
74 :     in
75 :     end
76 : jhr 176 | S.S_New(name, xs) =>
77 :     | S.S_Die =>
78 :     | S.S_Stabilize =>
79 : jhr 168 (* end case *))
80 :    
81 : jhr 190 fun newBlock (??, stm) =
82 :    
83 :     and nextStmt (env, assigned, stm, ??) =
84 :    
85 :     and join (env
86 : jhr 176 fun translate (S.Program{globals, globaInit, actors}) = ??
87 :    
88 : jhr 137 end

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