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

SCM Repository

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

Diff of /trunk/src/compiler/simplify/simplify.sml

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

revision 1640, Wed Nov 16 02:19:51 2011 UTC revision 2356, Sun Apr 7 14:45:25 2013 UTC
# Line 15  Line 15 
15      structure Ty = Types      structure Ty = Types
16      structure S = Simple      structure S = Simple
17    
18      (* the SimpleAST and AST currently use the same type representation, but
19       * we prune out meta variables.
20       *)
21        val cvtTy = TypeUtil.prune
22    
23      local      local
24        val tempName = Atom.atom "_t"        val tempName = Atom.atom "_t"
25      in      in
26      fun newTemp ty = Var.new (tempName, AST.LocalVar, ty)      fun newTemp ty = Var.new (tempName, AST.LocalVar, cvtTy ty)
27      end      end
28    
29    (* 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 *)
30      fun mkBlock stms = S.Block(List.rev stms)      fun mkBlock stms = S.Block(List.rev stms)
31    
32      (* is the given statement's continuation the syntactically following statement? *)
33        fun contIsNext (AST.S_Block stms) = List.all contIsNext stms
34          | contIsNext (AST.S_IfThenElse(_, s1, s2)) = contIsNext s1 orelse contIsNext s2
35          | contIsNext AST.S_Die = false
36          | contIsNext AST.S_Stabilize = false
37          | contIsNext (AST.S_Return _) = false
38          | contIsNext _ = true
39    
40      fun simplifyProgram (AST.Program dcls) = let      fun simplifyProgram (AST.Program dcls) = let
41            val globals = ref []            val globals = ref []
42            val globalInit = ref []            val globalInit = ref []
43              val funcs = ref []
44            val initially = ref NONE            val initially = ref NONE
45            val strands = ref []            val strands = ref []
46            fun setInitially init = (case !initially            fun setInitially init = (case !initially
# Line 54  Line 68 
68                          globals := x :: !globals;                          globals := x :: !globals;
69                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
70                        end                        end
71                      | AST.D_Func(f, params, body) =>
72                          funcs := S.Func{f=f, params=params, body=simplifyBlock body} :: !funcs
73                    | AST.D_Strand info => strands := simplifyStrand info :: !strands                    | AST.D_Strand info => strands := simplifyStrand info :: !strands
74                    | AST.D_InitialArray(creat, iters) =>                    | AST.D_InitialArray(creat, iters) =>
75                        setInitially (simplifyInit(true, creat, iters))                        setInitially (simplifyInit(true, creat, iters))
# Line 65  Line 81 
81              S.Program{              S.Program{
82                  globals = List.rev(!globals),                  globals = List.rev(!globals),
83                  globalInit = mkBlock (!globalInit),                  globalInit = mkBlock (!globalInit),
84                    funcs = List.rev(!funcs),
85                  init = (case !initially                  init = (case !initially
86  (* FIXME: the check for the initially block should really happen in typechecking *)  (* FIXME: the check for the initially block should really happen in typechecking *)
87                     of NONE => raise Fail "missing initially declaration"                     of NONE => raise Fail "missing initially declaration"
# Line 97  Line 114 
114                }                }
115            end            end
116    
117      and simplifyStrand {name, params, state, methods} = let      and simplifyStrand (AST.Strand{name, params, state, methods}) = let
118            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
119              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
120                  val (stms, e') = simplifyExp (e, stms)                  val (stms, e') = simplifyExp (e, stms)
# Line 122  Line 139 
139     *)     *)
140      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
141    
142      (* simplify the statement stm where stms is a reverse-order list of preceeding simplified
143       * statements.  This function returns a reverse-order list of simplified statements.
144       * Note that error reporting is done in the typechecker, but it does not prune unreachable
145       * code.
146       *)
147      and simplifyStmt (stm, stms) = (case stm      and simplifyStmt (stm, stms) = (case stm
148             of AST.S_Block body => let             of AST.S_Block body => let
149                  fun simplify ([], stms) = stms                  fun simplify ([], stms) = stms
150                    | simplify (stm::r, stms) = simplify (r, simplifyStmt (stm, stms))                    | simplify (stm::r, stms) = if contIsNext stm
151                          then simplify (r, simplifyStmt (stm, stms))
152                          else simplifyStmt (stm, stms)  (* prune unreachable statements *)
153                  in                  in
154                    simplify (body, stms)                    simplify (body, stms)
155                  end                  end
# Line 153  Line 177 
177                  end                  end
178              | AST.S_Die => S.S_Die :: stms              | AST.S_Die => S.S_Die :: stms
179              | AST.S_Stabilize => S.S_Stabilize :: stms              | AST.S_Stabilize => S.S_Stabilize :: stms
180                | AST.S_Return e => let
181                    val (stms, x) = simplifyExpToVar (e, stms)
182                    in
183                      S.S_Return x :: stms
184                    end
185              | AST.S_Print args => let              | AST.S_Print args => let
186                  val (stms, xs) = simplifyExpsToVars (args, stms)                  val (stms, xs) = simplifyExpsToVars (args, stms)
187                  in                  in
# Line 211  Line 240 
240                  in                  in
241                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
242                  end                  end
243                | AST.E_Coerce{srcTy, dstTy, e} => let
244                    val (stms, x) = simplifyExpToVar (e, stms)
245                    val result = newTemp dstTy
246                    val rhs = S.E_Coerce{srcTy = cvtTy srcTy, dstTy = cvtTy dstTy, x = x}
247                    in
248                      (S.S_Assign(result, rhs)::stms, S.E_Var result)
249                    end
250            (* end case *))            (* end case *))
251    
252      and simplifyExpToVar (exp, stms) = let      and simplifyExpToVar (exp, stms) = let
# Line 239  Line 275 
275    
276      fun transform (errStrm, ast) = let      fun transform (errStrm, ast) = let
277            val simple = simplifyProgram ast            val simple = simplifyProgram ast
278            val _ = SimplePP.output (Log.logFile(), simple)       (* DEBUG *)            val _ = SimplePP.output (Log.logFile(), "simplify", simple)   (* DEBUG *)
279              val simple = Inliner.transform simple
280              val _ = SimplePP.output (Log.logFile(), "inlining", simple)   (* DEBUG *)
281            val simple = Lift.transform simple            val simple = Lift.transform simple
282                  handle Eval.Error msg => (Error.error(errStrm, msg); simple)                  handle Eval.Error msg => (Error.error(errStrm, msg); simple)
283              val _ = SimplePP.output (Log.logFile(), "lifting", simple)   (* DEBUG *)
284            in            in
285              simple              simple
286            end            end

Legend:
Removed from v.1640  
changed lines
  Added in v.2356

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