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

SCM Repository

[diderot] Diff of /branches/vis12/src/compiler/simplify/simplify.sml
ViewVC logotype

Diff of /branches/vis12/src/compiler/simplify/simplify.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/compiler/simplify/simplify.sml revision 395, Thu Oct 14 16:52:15 2010 UTC branches/vis12/src/compiler/simplify/simplify.sml revision 1996, Thu Oct 4 10:42:57 2012 UTC
# Line 1  Line 1 
1  (* simplify.sml  (* simplify.sml
2   *   *
3   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4   * All rights reserved.   * All rights reserved.
5   *   *
6   * Simplify the AST representation.   * Simplify the AST representation.
# Line 8  Line 8 
8    
9  structure Simplify : sig  structure Simplify : sig
10    
11      val transform : AST.program -> Simple.program      val transform : Error.err_stream * AST.program -> Simple.program
12    
13    end = struct    end = struct
14    
# Line 27  Line 27 
27      fun simplifyProgram (AST.Program dcls) = let      fun simplifyProgram (AST.Program dcls) = let
28            val globals = ref []            val globals = ref []
29            val globalInit = ref []            val globalInit = ref []
30            val actors = ref []            val initially = ref NONE
31              val strands = ref []
32              fun setInitially init = (case !initially
33                     of NONE => initially := SOME init
34    (* FIXME: the check for multiple initially decls should happen in type checking *)
35                      | SOME _ => raise Fail "multiple initially declarations"
36                    (* end case *))
37            fun simplifyDecl dcl = (case dcl            fun simplifyDecl dcl = (case dcl
38                   of AST.D_Input(x, NONE) => let                   of AST.D_Input(x, desc, NONE) => let
39                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, NONE)                        val rhs = (case Var.monoTypeOf x
40                                 of ty as Ty.T_DynSequence _ => S.E_InputNrrd(ty, Var.nameOf x, desc, NONE)
41                                  | ty as Ty.T_Image _ => S.E_InputNrrd(ty, Var.nameOf x, desc, NONE)
42                                  | ty => S.E_Input(ty, Var.nameOf x, desc, NONE)
43                                (* end case *))
44                          in
45                            globals := x :: !globals;
46                            globalInit := S.S_Assign(x, rhs) :: !globalInit
47                          end
48                      | AST.D_Input(x, desc, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))) => let
49                        (* load the nrrd proxy here *)
50                          val info = NrrdInfo.getInfo nrrd
51    (* FIXME: what do we do with the proxy info? *)
52                          val rhs = (case TypeUtil.pruneHead ty
53                                 of ty as Ty.T_DynSequence _ =>
54                                      S.E_InputNrrd(ty, Var.nameOf x, desc, SOME nrrd)
55                                  | Ty.T_Image _ =>
56                                      S.E_InputNrrd(ty, Var.nameOf x, desc, SOME nrrd)
57                                  | _ => raise Fail "impossible"
58                                (* end case *))
59                          val stm = S.S_Assign(x, rhs)
60                        in                        in
61                          globals := x :: !globals;                          globals := x :: !globals;
62                          globalInit := S.S_Assign(x, e') :: !globalInit                          globalInit := stm :: !globalInit
63                        end                        end
64                    | AST.D_Input(x, SOME e) => let                    | AST.D_Input(x, desc, SOME e) => let
65                        val (stms, x') = simplifyExpToVar (e, [])                        val (stms, x') = simplifyExpToVar (e, [])
66                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, SOME x')                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, desc, SOME x')
67                        in                        in
68                          globals := x :: !globals;                          globals := x :: !globals;
69                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
# Line 48  Line 74 
74                          globals := x :: !globals;                          globals := x :: !globals;
75                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
76                        end                        end
77                    | AST.D_Actor info => actors := simplifyActor info :: !actors                    | AST.D_Strand info => strands := simplifyStrand info :: !strands
78                    | AST.D_InitialArray(e, iters) => () (* FIXME *)                    | AST.D_InitialArray(creat, iters) =>
79                    | AST.D_InitialCollection(e, iters) => () (* FIXME *)                        setInitially (simplifyInit(true, creat, iters))
80                      | AST.D_InitialCollection(creat, iters) =>
81                          setInitially (simplifyInit(false, creat, iters))
82                  (* end case *))                  (* end case *))
83            in            in
84              List.app simplifyDecl dcls;              List.app simplifyDecl dcls;
85              S.Program{              S.Program{
86                  globals = List.rev(!globals),                  globals = List.rev(!globals),
87                  globalInit = mkBlock (!globalInit),                  globalInit = mkBlock (!globalInit),
88                  actors = List.rev(!actors)                  init = (case !initially
89    (* FIXME: the check for the initially block should really happen in typechecking *)
90                       of NONE => raise Fail "missing initially declaration"
91                        | SOME blk => blk
92                      (* end case *)),
93                    strands = List.rev(!strands)
94                }                }
95            end            end
96    
97      and simplifyActor {name, params, state, methods} = let      and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
98              val (stms, xs) = simplifyExpsToVars (exps, [])
99              val creat = S.C_Create{
100                      argInit = mkBlock stms,
101                      name = strand,
102                      args = xs
103                    }
104              fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
105                    val (stms, lo) = simplifyExpToVar (e1, stms)
106                    val (stms, hi) = simplifyExpToVar (e2, stms)
107                    in
108                      ({param=x, lo=lo, hi=hi}::iters, stms)
109                    end
110              val (iters, stms) = List.foldl simplifyIter ([], []) iters
111              in
112                S.Initially{
113                    isArray = isArray,
114                    rangeInit = mkBlock stms,
115                    iters = List.rev iters,
116                    create = creat
117                  }
118              end
119    
120        and simplifyStrand {name, params, state, methods} = let
121            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
122              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
123                  val (stms, e') = simplifyExp (e, stms)                  val (stms, e') = simplifyExp (e, stms)
# Line 70  Line 126 
126                  end                  end
127            val (xs, stm) = simplifyState (state, [], [])            val (xs, stm) = simplifyState (state, [], [])
128            in            in
129              S.Actor{              S.Strand{
130                  name = name,                  name = name,
131                  params = params,                  params = params,
132                  state = xs, stateInit = stm,                  state = xs, stateInit = stm,
# Line 81  Line 137 
137      and simplifyMethod (AST.M_Method(name, body)) =      and simplifyMethod (AST.M_Method(name, body)) =
138            S.Method(name, simplifyBlock body)            S.Method(name, simplifyBlock body)
139    
140    (* simplify a statement into a single statement (i.e., a block if it expands into more    (* simplify a statement into a single statement (i.e., a block if it expands
141     * than one new statement.     * into more than one new statement).
142     *)     *)
143      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
144    
# Line 117  Line 173 
173                  end                  end
174              | AST.S_Die => S.S_Die :: stms              | AST.S_Die => S.S_Die :: stms
175              | AST.S_Stabilize => S.S_Stabilize :: stms              | AST.S_Stabilize => S.S_Stabilize :: stms
176                | AST.S_Print args => let
177                    val (stms, xs) = simplifyExpsToVars (args, stms)
178                    in
179                      S.S_Print xs :: stms
180                    end
181            (* end case *))            (* end case *))
182    
183      and simplifyExp (exp, stms) = (      and simplifyExp (exp, stms) = (
# Line 143  Line 204 
204                  in                  in
205                    (stms, S.E_Cons xs)                    (stms, S.E_Cons xs)
206                  end                  end
207              | AST.E_Slice(e, indices) => let (* tensor slicing *)              | AST.E_Seq es => let
208                    val (stms, xs) = simplifyExpsToVars (es, stms)
209                    in
210                      (stms, S.E_Seq xs)
211                    end
212                | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
213                  val (stms, x) = simplifyExpToVar (e, stms)                  val (stms, x) = simplifyExpToVar (e, stms)
214                    fun f ([], ys, stms) = (stms, List.rev ys)
215                      | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
216                      | f (SOME e::es, ys, stms) = let
217                          val (stms, y) = simplifyExpToVar (e, stms)
218                  in                  in
219                    raise Fail "FIXME"                          f (es, SOME y::ys, stms)
220                  end                  end
221              | AST.E_Cond(e1, e2, e3) => let                  val (stms, indices) = f (indices, [], stms)
222                    in
223                      (stms, S.E_Slice(x, indices, ty))
224                    end
225                | AST.E_Cond(e1, e2, e3, ty) => let
226                (* a conditional expression gets turned into an if-then-else statememt *)                (* a conditional expression gets turned into an if-then-else statememt *)
227                  val result = newTemp Ty.T_Bool                  val result = newTemp ty
228                  val (stms, x) = simplifyExpToVar (e1, stms)                  val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
229                  fun simplifyBranch e = let                  fun simplifyBranch e = let
230                        val (stms, e) = simplifyExp (e, [])                        val (stms, e) = simplifyExp (e, [])
231                        in                        in
232                          mkBlock (S.S_Assign(result, e)::stms)                          mkBlock (S.S_Assign(result, e)::stms)
233                        end                        end
234                  val s1 = simplifyBranch e1                  val s1 = simplifyBranch e2
235                  val s2 = simplifyBranch e2                  val s2 = simplifyBranch e3
236                  in                  in
237                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
238                  end                  end
239                | AST.E_LoadNrrd _ => raise Fail "unexpected E_LoadNrrd" (* should be handled by simplifyDecl *)
240                | AST.E_Coerce{srcTy, dstTy, e} => let
241                    val (stms, x) = simplifyExpToVar (e, stms)
242                    val result = newTemp dstTy
243                    in
244                      (S.S_Assign(result, S.E_Coerce{srcTy=srcTy, dstTy=dstTy, x=x})::stms, S.E_Var result)
245                    end
246            (* end case *))            (* end case *))
247    
248      and simplifyExpToVar (exp, stms) = let      and simplifyExpToVar (exp, stms) = let
# Line 188  Line 269 
269              f (exps, [], stms)              f (exps, [], stms)
270            end            end
271    
272      fun transform ast = let      fun transform (errStrm, ast) = let
273            val simple = simplifyProgram ast            val simple = simplifyProgram ast
274            val _ = SimplePP.output (Log.logFile(), simple)       (* DEBUG *)            val _ = SimplePP.output (Log.logFile(), simple)       (* DEBUG *)
275    (*
276            val simple = Lift.transform simple            val simple = Lift.transform simple
277                    handle Eval.Error msg => (Error.error(errStrm, msg); simple)
278    *)
279            in            in
280              simple              simple
281            end            end

Legend:
Removed from v.395  
changed lines
  Added in v.1996

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