Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

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

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

revision 179, Tue Jul 27 20:43:23 2010 UTC revision 511, Tue Feb 8 17:01:43 2011 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)
5   *   *
6   * Simplify the AST representation.   * Simplify the AST representation.
# Line 22  Line 22
22      end      end
23
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 = let      fun mkBlock stms = S.Block(List.rev stms)
fun flatten [] = []
| flatten (S.S_Block stms :: r) = stms @ flatten r
| flatten (stm :: r) = stm :: flatten r
in
S.S_Block(flatten (List.rev stms))
end
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 strands = ref []
31            fun simplifyDecl dcl = (case dcl            fun simplifyDecl dcl = (case dcl
32                   of AST.D_Input(x, NONE) => globals := x :: !globals                   of AST.D_Input(x, NONE) => let
33                          val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, NONE)
34                          in
35                            globals := x :: !globals;
36                            globalInit := S.S_Assign(x, e') :: !globalInit
37                          end
38                    | AST.D_Input(x, SOME e) => let                    | AST.D_Input(x, SOME e) => let
39                        val (stms, x') = simplifyExpToVar (e, [])                        val (stms, x') = simplifyExpToVar (e, [])
40                        val t = newTemp Ty.T_String                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, SOME x')
val stm = S.S_Assign(t, S.E_Lit(Literal.String(Var.nameOf x)))
val ty = Var.monoTypeOf x
val e' = S.E_Apply(BasisVars.input,
[Ty.TYPE(MetaVar.newFromType ty)], [t, x'], ty)
41                        in                        in
42                          globals := x :: !globals;                          globals := x :: !globals;
43                          globalInit := S.S_Assign(x, e') :: stm :: (stms @ !globalInit)                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
44                        end                        end
45                    | AST.D_Var(AST.VD_Decl(x, e)) => let                    | AST.D_Var(AST.VD_Decl(x, e)) => let
46                        val (stms, e') = simplifyExp (e, [])                        val (stms, e') = simplifyExp (e, [])
# Line 53  Line 48
48                          globals := x :: !globals;                          globals := x :: !globals;
49                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
50                        end                        end
51                    | AST.D_Actor info => actors := simplifyActor info :: !actors                    | AST.D_Strand info => strands := simplifyStrand info :: !strands
52                    | AST.D_InitialArray(e, iters) => () (* FIXME *)                    | AST.D_InitialArray(e, iters) => () (* FIXME *)
53                    | AST.D_InitialCollection(e, iters) => () (* FIXME *)                    | AST.D_InitialCollection(e, iters) => () (* FIXME *)
54                  (* end case *))                  (* end case *))
# Line 62  Line 57
57              S.Program{              S.Program{
58                  globals = List.rev(!globals),                  globals = List.rev(!globals),
59                  globalInit = mkBlock (!globalInit),                  globalInit = mkBlock (!globalInit),
60                  actors = List.rev(!actors)                  strands = List.rev(!strands)
61                }                }
62            end            end
63
64      and simplifyActor {name, params, state, methods} = let      and simplifyStrand {name, params, state, methods} = let
65            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
66              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
67                  val (stms, e') = simplifyExp (e, stms)                  val (stms, e') = simplifyExp (e, stms)
# Line 75  Line 70
70                  end                  end
71            val (xs, stm) = simplifyState (state, [], [])            val (xs, stm) = simplifyState (state, [], [])
72            in            in
73              S.Actor{              S.Strand{
74                  name = name,                  name = name,
75                  params = params,                  params = params,
76                  state = xs, stateInit = stm,                  state = xs, stateInit = stm,
# Line 84  Line 79
79            end            end
80
81      and simplifyMethod (AST.M_Method(name, body)) =      and simplifyMethod (AST.M_Method(name, body)) =
82            S.M_Method(name, simplifyBlock body)            S.Method(name, simplifyBlock body)
83
84    (* 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 into more
85     * than one new statement.     * than one new statement.
# Line 126  Line 121
121
122      and simplifyExp (exp, stms) = (      and simplifyExp (exp, stms) = (
123            case exp            case exp
124             of AST.E_Var x => (stms, S.E_Var x)             of AST.E_Var x => (case Var.kindOf x
125                     of Var.BasisVar => let
126                          val ty = Var.monoTypeOf x
127                          val x' = newTemp ty
128                          val stm = S.S_Assign(x', S.E_Apply(x, [], [], ty))
129                          in
130                            (stm::stms, S.E_Var x')
131                          end
132                      | _ => (stms, S.E_Var x)
133                    (* end case *))
134              | AST.E_Lit lit => (stms, S.E_Lit lit)              | AST.E_Lit lit => (stms, S.E_Lit lit)
135              | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"              | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
136              | AST.E_Apply(f, tyArgs, args, ty) => let              | AST.E_Apply(f, tyArgs, args, ty) => let
# Line 139  Line 143
143                  in                  in
144                    (stms, S.E_Cons xs)                    (stms, S.E_Cons xs)
145                  end                  end
146              | AST.E_Cond(e1, e2, e3) => let              | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
147                    val (stms, x) = simplifyExpToVar (e, stms)
148                    fun f ([], ys, stms) = (stms, List.rev ys)
149                      | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
150                      | f (SOME e::es, ys, stms) = let
151                          val (stms, y) = simplifyExpToVar (e, stms)
152                          in
153                            f (es, SOME y::ys, stms)
154                          end
155                    val (stms, indices) = f (indices, [], stms)
156                    in
157                      (stms, S.E_Slice(x, indices, ty))
158                    end
159                | AST.E_Cond(e1, e2, e3, ty) => let
160                (* a conditional expression gets turned into an if-then-else statememt *)                (* a conditional expression gets turned into an if-then-else statememt *)
161                  val result = newTemp Ty.T_Bool                  val result = newTemp ty
162                  val (stms, x) = simplifyExpToVar (e1, stms)                  val (stms, x) = simplifyExpToVar (e1, stms)
163                  fun simplifyBranch e = let                  fun simplifyBranch e = let
164                        val (stms, e) = simplifyExp (e, [])                        val (stms, e) = simplifyExp (e, [])
165                        in                        in
166                          mkBlock (S.S_Assign(result, e)::stms)                          mkBlock (S.S_Assign(result, e)::stms)
167                        end                        end
168                  val s1 = simplifyBranch e1                  val s1 = simplifyBranch e2
169                  val s2 = simplifyBranch e2                  val s2 = simplifyBranch e3
170                  in                  in
171                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
172                  end                  end
# Line 179  Line 196
196              f (exps, [], stms)              f (exps, [], stms)
197            end            end
198
199        fun transform ast = let
200              val simple = simplifyProgram ast
201              val _ = SimplePP.output (Log.logFile(), simple)       (* DEBUG *)
202              val simple = Lift.transform simple
203              in
204                simple
205              end
206
207    end    end

Legend:
 Removed from v.179 changed lines Added in v.511