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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/simplify/lift.sml
ViewVC logotype

Annotation of /trunk/src/compiler/simplify/lift.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 227 - (view) (download)

1 : jhr 181 (* lift.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Lift field operations to global scope.
7 :     *)
8 :    
9 :     structure Lift : sig
10 :    
11 :     val transform : Simple.program -> Simple.program
12 :    
13 :     end = struct
14 :    
15 :     structure BV = BasisVars
16 : jhr 227 structure S = Simple
17 :     structure VSet = Var.Set
18 :     structure VMap = Var.Map
19 : jhr 181
20 :     (*
21 :     BV.op_at
22 :     BV.op_D
23 :     BV.fn_convolve
24 :     BV.fn_load
25 :     *)
26 :    
27 : jhr 227 (* the kinds of things a variable in Simple AST can be bound to *)
28 :     datatype var_binding
29 :     = RHS of S.exp
30 :     | Param
31 : jhr 181
32 : jhr 227 (* identify the image load operations and lift them and their antecedents; in terms of BTA,
33 :     * this phase is essentially determining what must be static in order to get the image
34 :     * info needed for the rest of the compile.
35 :     *)
36 :     fun liftLoads block = let
37 :     (* analysis to compute the set of static variables *)
38 :     fun mkStatic (env, statics, x) = if VSet.member(statics, x)
39 :     then statics
40 :     else let
41 :     val statics = VSet.add(statics, x)
42 :     in
43 :     case VMap.find(env, x)
44 :     of SOME(S.E_Var y) => mkStatic (env, statics, y)
45 :     | SOME(S.E_Tuple ys) => mkStatics (env, statics, ys)
46 :     | SOME(S.E_Apply(_, _, ys, _)) => mkStatics (env, statics, ys)
47 :     | SOME(S.E_Cons ys) => mkStatics (env, statics, ys)
48 :     | SOME(S.E_Input(_, SOME y)) => mkStatic (env, statics, y)
49 :     | SOME _ => statics
50 :     | NONE => raise Fail(concat["variable ", Var.uniqueNameOf x, " has no binding"])
51 :     (* end case *)
52 :     end
53 :     and mkStatics (env, statics, xs) =
54 :     List.foldl (fn (x, statics) => mkStatic(env, statics, x)) statics xs
55 :     fun doBlock (env, statics, S.Block stms) = let
56 :     fun doStmts (env, statics, []) = statics
57 :     | doStmts (env, statics, stm::stms) = let
58 :     val (env, statics) = doStmt (env, statics, stm)
59 :     in
60 :     doStmts (env, statics, stms)
61 :     end
62 :     in
63 :     doStmts (env, statics, stms)
64 :     end
65 :     and doStmt (env, statics, stm) = (case stm
66 :     of S.S_Assign(x, e) => let
67 :     val env = VMap.insert(env, x, e)
68 :     in
69 :     case e
70 :     of S.E_Apply(f, _, xs, _) =>
71 :     if Var.same(f, BV.fn_load)
72 :     then (env, mkStatic(env, statics, x))
73 :     else (env, statics)
74 :     | _ => (env, statics)
75 :     (* end case *)
76 :     end
77 :     | S.S_IfThenElse(x, b1, b2) => let
78 :     val statics1 = doBlock (env, statics, b1)
79 :     val statics2 = doBlock (env, statics, b2)
80 :     val n = VSet.numItems statics
81 :     in
82 :     if ((n <> VSet.numItems statics1) orelse (n <> VSet.numItems statics2))
83 :     then (env, mkStatic(env, statics, x))
84 :     else (env, statics)
85 :     end
86 :     | _ => (env, statics)
87 :     (* end case *))
88 :     val statics = doBlock (VMap.empty, VSet.empty, block)
89 :     (* lift out the static code *)
90 :     fun doBlock (S.Block stms) = let
91 :     fun doStmts ([], staticStms) = S.Block(List.rev staticStms)
92 :     | doStmts (stm::stms, staticStms) = (case doStmt stm
93 :     of SOME stm => doStmts (stms, stm::staticStms)
94 :     | NONE => doStmts (stms, staticStms)
95 :     (* end case *))
96 :     in
97 :     doStmts (stms, [])
98 :     end
99 :     and doStmt stm = (case stm
100 :     of S.S_Assign(x, e) => if VSet.member(statics, x)
101 :     then SOME stm
102 :     else NONE
103 :     | S.S_IfThenElse(x, b1, b2) => if VSet.member(statics, x)
104 :     then SOME(S.S_IfThenElse(x, doBlock b1, doBlock b2))
105 :     else NONE
106 :     | _ => NONE
107 :     (* end case *))
108 :     val staticBlock = doBlock block
109 :     in
110 :     print "**** static variables: ";
111 :     VSet.app (fn x => print(" "^Var.uniqueNameOf x)) statics;
112 :     print "\n";
113 :     staticBlock
114 :     end
115 :    
116 :     fun transform (prog as S.Program{globals, staticInit, globalInit, actors}) = let
117 :     val staticInit = liftLoads globalInit
118 :     in
119 :     S.Program{
120 :     globals = globals,
121 :     staticInit = staticInit,
122 :     globalInit = globalInit,
123 :     actors = actors
124 :     }
125 :     end
126 :    
127 : jhr 181 end

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