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 199 - (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 176 (* end case *))
42 : jhr 168
43 : jhr 192 (* convert a Simple AST block to an IL statement. We return the statement that represents the
44 :     * block, plus the environment mapping Simple AST variables to their current SSA representations
45 :     * and the set of Simple AST variables that were assigned to in the block.
46 : jhr 189 *)
47 : jhr 192 fun cvtBlock (env, S.Block stms) = let
48 :     fun toBlock (env, assigned, [], assignments) =
49 :     (IL.mkBLOCK{succ=IL.dummy, body=List.rev assignments}, env, assigned)
50 :     | toBlock (env, assigned, S.S_Assign(x, e)::rest, assignments) = 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 : jhr 197 toBlock (env, assigned, rest, stms@assignments)
57 : jhr 189 end
58 : jhr 192 | toBlock (env, assigned, stms, assignments) = let
59 :     val (succ, env, assigned) = toStmt (env, assigned, stms)
60 :     val blk = IL.mkBLOCK{succ=succ, body=List.rev assignments}
61 :     in
62 :     IL.addPred (succ, blk);
63 :     (blk, env, assigned)
64 :     end
65 :     and toStmt (env, assigned, []) =
66 :     (IL.mkBLOCK{succ=IL.dummy, body=[]}, env, assigned)
67 : jhr 197 | toStmt (env, assigned, stms as stm::rest) = (case stm
68 :     of S.S_Assign _ => toBlock (env, assigned, stms, [])
69 : jhr 192 | S.S_IfThenElse(x, b1, b2) => let
70 :     val x' = lookup env x
71 : jhr 197 val (s1, env1, assigned1) = cvtBlock(env, b1)
72 :     val (s2, env2, assigned2) = cvtBlock(env, b2)
73 : jhr 192 val assigned = VSet.union(assigned1, assigned2)
74 :     val (env, phis) = let
75 : jhr 194 fun mkPhi (x, (env, phis)) = let
76 : jhr 197 val x1 = lookup env1 x
77 :     val x2 = lookup env2 x
78 : jhr 192 val x' = newVar x
79 :     in
80 :     (VMap.insert(env, x, x'), (x', [x1, x2])::phis)
81 :     end
82 : jhr 190 in
83 : jhr 192 VSet.foldl mkPhi (env, []) assigned
84 : jhr 190 end
85 : jhr 192 val stm = IL.mkIF{cond=x', thenBranch=s1, elseBranch=s2}
86 : jhr 190 in
87 : jhr 192 case rest
88 :     of [] => (stm, env, assigned)
89 :     | _ => let
90 :     val (join, env, assigned) = toStmt (env, assigned, rest)
91 :     in
92 :     IL.addPred (join, stm);
93 :     IL.setSucc (stm, join);
94 :     (stm, env, assigned)
95 :     end
96 :     (* end case *)
97 : jhr 190 end
98 : jhr 192 | S.S_New(name, xs) => let
99 :     val xs' = List.map (lookup env) xs
100 :     in
101 :     case rest
102 :     of [] => (IL.mkNEW{actor=name, args=xs', succ=IL.dummy}, env, assigned)
103 :     | _ => let
104 :     val (succ, env, assigned) = toStmt (env, assigned, rest)
105 :     val stm = IL.mkNEW{actor=name, args=xs', succ=succ}
106 :     in
107 :     IL.addPred (succ, stm);
108 :     (stm, env, assigned)
109 :     end
110 : jhr 194 end
111 : jhr 197 | S.S_Die => (IL.mkDIE(), env, assigned)
112 :     | S.S_Stabilize => (IL.mkSTABILIZE(), env, assigned)
113 : jhr 192 (* end case *))
114 :     in
115 :     toStmt (env, VSet.empty, stms)
116 :     end
117 : jhr 168
118 : jhr 197 fun translate (S.Program{globals, globalInit, actors}) = ??
119 : jhr 176
120 : jhr 137 end

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