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 221, Tue Aug 3 20:13:55 2010 UTC branches/vis12/src/compiler/simplify/simplify.sml revision 1685, Sun Jan 22 15:23:36 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 24  Line 24 
24    (* make a block out of a list of statements that are in reverse order *)    (* make a block out of a list of statements that are in reverse order *)
25      fun mkBlock stms = S.Block(List.rev stms)      fun mkBlock stms = S.Block(List.rev stms)
26    
27      fun transform (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.nameOf x, NONE)                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, desc, NONE)
40                        in                        in
41                          globals := x :: !globals;                          globals := x :: !globals;
42                          globalInit := S.S_Assign(x, e') :: !globalInit                          globalInit := S.S_Assign(x, e') :: !globalInit
43                        end                        end
44                    | AST.D_Input(x, SOME e) => let                    | AST.D_Input(x, desc, SOME e) => let
45                        val (stms, x') = simplifyExpToVar (e, [])                        val (stms, x') = simplifyExpToVar (e, [])
46                        val e' = S.E_Input(Var.nameOf x, SOME x')                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, desc, SOME x')
47                        in                        in
48                          globals := x :: !globals;                          globals := x :: !globals;
49                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
# Line 48  Line 54 
54                          globals := x :: !globals;                          globals := x :: !globals;
55                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
56                        end                        end
57                    | AST.D_Actor info => actors := simplifyActor info :: !actors                    | AST.D_Strand info => strands := simplifyStrand info :: !strands
58                    | AST.D_InitialArray(e, iters) => () (* FIXME *)                    | AST.D_InitialArray(creat, iters) =>
59                    | AST.D_InitialCollection(e, iters) => () (* FIXME *)                        setInitially (simplifyInit(true, creat, iters))
60                      | AST.D_InitialCollection(creat, iters) =>
61                          setInitially (simplifyInit(false, creat, iters))
62                  (* end case *))                  (* end case *))
63            in            in
64              List.app simplifyDecl dcls;              List.app simplifyDecl dcls;
65              S.Program{              S.Program{
66                  globals = List.rev(!globals),                  globals = List.rev(!globals),
67                  globalInit = mkBlock (!globalInit),                  globalInit = mkBlock (!globalInit),
68                  actors = List.rev(!actors)                  init = (case !initially
69    (* FIXME: the check for the initially block should really happen in typechecking *)
70                       of NONE => raise Fail "missing initially declaration"
71                        | SOME blk => blk
72                      (* end case *)),
73                    strands = List.rev(!strands)
74                  }
75              end
76    
77        and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
78              val (stms, xs) = simplifyExpsToVars (exps, [])
79              val creat = S.C_Create{
80                      argInit = mkBlock stms,
81                      name = strand,
82                      args = xs
83                    }
84              fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
85                    val (stms, lo) = simplifyExpToVar (e1, stms)
86                    val (stms, hi) = simplifyExpToVar (e2, stms)
87                    in
88                      ({param=x, lo=lo, hi=hi}::iters, stms)
89                    end
90              val (iters, stms) = List.foldl simplifyIter ([], []) iters
91              in
92                S.Initially{
93                    isArray = isArray,
94                    rangeInit = mkBlock stms,
95                    iters = List.rev iters,
96                    create = creat
97                }                }
98            end            end
99    
100      and simplifyActor {name, params, state, methods} = let      and simplifyStrand {name, params, state, methods} = let
101            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
102              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
103                  val (stms, e') = simplifyExp (e, stms)                  val (stms, e') = simplifyExp (e, stms)
# Line 70  Line 106 
106                  end                  end
107            val (xs, stm) = simplifyState (state, [], [])            val (xs, stm) = simplifyState (state, [], [])
108            in            in
109              S.Actor{              S.Strand{
110                  name = name,                  name = name,
111                  params = params,                  params = params,
112                  state = xs, stateInit = stm,                  state = xs, stateInit = stm,
# Line 81  Line 117 
117      and simplifyMethod (AST.M_Method(name, body)) =      and simplifyMethod (AST.M_Method(name, body)) =
118            S.Method(name, simplifyBlock body)            S.Method(name, simplifyBlock body)
119    
120    (* 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
121     * than one new statement.     * into more than one new statement).
122     *)     *)
123      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
124    
# Line 117  Line 153 
153                  end                  end
154              | AST.S_Die => S.S_Die :: stms              | AST.S_Die => S.S_Die :: stms
155              | AST.S_Stabilize => S.S_Stabilize :: stms              | AST.S_Stabilize => S.S_Stabilize :: stms
156                | AST.S_Print args => let
157                    val (stms, xs) = simplifyExpsToVars (args, stms)
158                    in
159                      S.S_Print xs :: stms
160                    end
161            (* end case *))            (* end case *))
162    
163      and simplifyExp (exp, stms) = (      and simplifyExp (exp, stms) = (
# Line 143  Line 184 
184                  in                  in
185                    (stms, S.E_Cons xs)                    (stms, S.E_Cons xs)
186                  end                  end
187              | AST.E_Cond(e1, e2, e3) => let              | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
188                    val (stms, x) = simplifyExpToVar (e, stms)
189                    fun f ([], ys, stms) = (stms, List.rev ys)
190                      | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
191                      | f (SOME e::es, ys, stms) = let
192                          val (stms, y) = simplifyExpToVar (e, stms)
193                          in
194                            f (es, SOME y::ys, stms)
195                          end
196                    val (stms, indices) = f (indices, [], stms)
197                    in
198                      (stms, S.E_Slice(x, indices, ty))
199                    end
200                | AST.E_Cond(e1, e2, e3, ty) => let
201                (* a conditional expression gets turned into an if-then-else statememt *)                (* a conditional expression gets turned into an if-then-else statememt *)
202                  val result = newTemp Ty.T_Bool                  val result = newTemp ty
203                  val (stms, x) = simplifyExpToVar (e1, stms)                  val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
204                  fun simplifyBranch e = let                  fun simplifyBranch e = let
205                        val (stms, e) = simplifyExp (e, [])                        val (stms, e) = simplifyExp (e, [])
206                        in                        in
207                          mkBlock (S.S_Assign(result, e)::stms)                          mkBlock (S.S_Assign(result, e)::stms)
208                        end                        end
209                  val s1 = simplifyBranch e1                  val s1 = simplifyBranch e2
210                  val s2 = simplifyBranch e2                  val s2 = simplifyBranch e3
211                  in                  in
212                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
213                  end                  end
# Line 183  Line 237 
237              f (exps, [], stms)              f (exps, [], stms)
238            end            end
239    
240        fun transform (errStrm, ast) = let
241              val simple = simplifyProgram ast
242              val _ = SimplePP.output (Log.logFile(), simple)       (* DEBUG *)
243              val simple = Lift.transform simple
244                    handle Eval.Error msg => (Error.error(errStrm, msg); simple)
245              in
246                simple
247              end
248    
249    end    end

Legend:
Removed from v.221  
changed lines
  Added in v.1685

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