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

revision 2822, Sun Nov 9 02:36:50 2014 UTC revision 2823, Sun Nov 9 03:57:35 2014 UTC
# Line 49  Line 49 
49              | SOME info => InP.Proxy(nrrd, info)              | SOME info => InP.Proxy(nrrd, info)
50            (* end case *))            (* end case *))
51    
52    (* is the given statement's continuation the syntactically following statement? *)      datatype 'a ctl_flow_info
53      fun contIsNext (AST.S_Block stms) = List.all contIsNext stms        = EXIT                    (* stm sequence always exits; no pruning so far *)
54        | contIsNext (AST.S_IfThenElse(_, s1, s2)) = contIsNext s1 orelse contIsNext s2        | PRUNE of 'a             (* stm sequence always exits at last stm in argument, which
55        | contIsNext AST.S_Die = false                                   * is either a block of stm list *)
56        | contIsNext AST.S_Stabilize = false        | CONT                    (* stm sequence falls through *)
57        | contIsNext (AST.S_Return _) = false        | EDIT of 'a              (* pruned code that has non-exiting paths *)
58        | contIsNext _ = true  
59        fun pruneUnreachableCode (blk as S.Block stms) = let
60              fun isExit S.S_Die = true
61                | isExit S.S_Stabilize = true
62                | isExit (S.S_Return _) = true
63                | isExit _ = false
64              fun pruneStms [] = CONT
65                | pruneStms [S.S_IfThenElse(x, blk1, blk2)] = (
66                    case pruneIf(x, blk1, blk2)
67                     of EXIT => EXIT
68                      | PRUNE stm => PRUNE[stm]
69                      | CONT => CONT
70                      | EDIT stm => EDIT[stm]
71                    (* end case *))
72                | pruneStms [stm] = if isExit stm then EXIT else CONT
73                | pruneStms ((stm as S.S_IfThenElse(x, blk1, blk2))::stms) = (
74                    case pruneIf(x, blk1, blk2)
75                     of EXIT => PRUNE[stm]
76                      | PRUNE stm => PRUNE[stm]
77                      | CONT => (case pruneStms stms
78                           of PRUNE stms => PRUNE(stm::stms)
79                            | EDIT stms => EDIT(stm::stms)
80                            | EXIT => EXIT (* different instances of ctl_flow_info *)
81                            | CONT => CONT
82                          (* end case *))
83                      | EDIT stm => (case pruneStms stms
84                           of PRUNE stms => PRUNE(stm::stms)
85                            | EDIT stms => EDIT(stm::stms)
86                            | _ => EDIT(stm::stms)
87                          (* end case *))
88                    (* end case *))
89                | pruneStms (stm::stms) = if isExit stm
90                    then PRUNE[stm]
91                    else (case pruneStms stms
92                       of PRUNE stms => PRUNE(stm::stms)
93                        | EDIT stms => EDIT(stm::stms)
94                        | info => info
95                      (* end case *))
96              and pruneIf (x, blk1, blk2) = (case (pruneBlk blk1, pruneBlk blk2)
97                     of (EXIT,       EXIT      ) => EXIT
98                      | (CONT,       CONT      ) => CONT
99                      | (CONT,       EXIT      ) => CONT
100                      | (EXIT,       CONT      ) => CONT
101                      | (CONT,       EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
102                      | (EDIT blk1,  CONT      ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
103                      | (CONT,       PRUNE blk2) => EDIT(S.S_IfThenElse(x, blk1, blk2))
104                      | (PRUNE blk1, CONT      ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
105                      | (EXIT,       EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
106                      | (EDIT blk1,  EXIT      ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
107                      | (EDIT blk1,  EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
108                      | (EDIT blk1,  PRUNE blk2) => EDIT(S.S_IfThenElse(x, blk1, blk2))
109                      | (PRUNE blk1, EDIT blk2 ) => EDIT(S.S_IfThenElse(x, blk1, blk2))
110                      | (EXIT,       PRUNE blk2) => PRUNE(S.S_IfThenElse(x, blk1, blk2))
111                      | (PRUNE blk1, EXIT      ) => PRUNE(S.S_IfThenElse(x, blk1, blk2))
112                      | (PRUNE blk1, PRUNE blk2) => PRUNE(S.S_IfThenElse(x, blk1, blk2))
113                    (* end case *))
114              and pruneBlk (S.Block stms) = (case pruneStms stms
115                     of PRUNE stms => PRUNE(S.Block stms)
116                      | EDIT stms => EDIT(S.Block stms)
117                      | EXIT => EXIT (* different instances of ctl_flow_info *)
118                      | CONT => CONT
119                    (* end case *))
120              in
121                case pruneBlk blk
122                 of PRUNE blk => blk
123                  | EDIT blk => blk
124                  | _=> blk
125                (* end case *)
126              end
127    
128      fun simplifyProgram (AST.Program{props, decls}) = let      fun simplifyProgram (AST.Program{props, decls}) = let
129            val inputs = ref []            val inputs = ref []
# Line 135  Line 203 
203                    | AST.D_Func(f, params, body) => let                    | AST.D_Func(f, params, body) => let
204                        val (f', env) = cvtVar(env, f)                        val (f', env) = cvtVar(env, f)
205                        val (params', env) = cvtVars (env, params)                        val (params', env) = cvtVars (env, params)
206                        val body' = simplifyBlock(env, body)                        val body' = pruneUnreachableCode (simplifyBlock(env, body))
207                        in                        in
208                          funcs := S.Func{f=f', params=params', body=body'} :: !funcs;                          funcs := S.Func{f=f', params=params', body=body'} :: !funcs;
209                          env                          env
# Line 212  Line 280 
280            end            end
281    
282      and simplifyMethod env (AST.M_Method(name, body)) =      and simplifyMethod env (AST.M_Method(name, body)) =
283            S.Method(name, simplifyBlock(env, body))            S.Method(name, pruneUnreachableCode (simplifyBlock(env, body)))
284    
285    (* simplify a statement into a single statement (i.e., a block if it expands    (* simplify a statement into a single statement (i.e., a block if it expands
286     * into more than one new statement).     * into more than one new statement).
# Line 230  Line 298 
298                    | simplify (env', stm::r, stms) = let                    | simplify (env', stm::r, stms) = let
299                        val (stms, env') = simplifyStmt (env', stm, stms)                        val (stms, env') = simplifyStmt (env', stm, stms)
300                        in                        in
301                          if contIsNext stm                          simplify (env', r, stms)
                           then simplify (env', r, stms)  
                           else stms  (* prune the unreachable statements "r" *)  
302                        end                        end
303                  in                  in
304                    (simplify (env, body, stms), env)                    (simplify (env, body, stms), env)

Legend:
Removed from v.2822  
changed lines
  Added in v.2823

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