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

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/compiler/IL/census-fn.sml
ViewVC logotype

Annotation of /branches/pure-cfg/src/compiler/IL/census-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1017 - (view) (download)

1 : jhr 338 (* census-fn.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 338 * All rights reserved.
5 :     *
6 : jhr 368 * Compute use counts for IL variables and initialize their bindings.
7 : jhr 338 *)
8 :    
9 :     functor CensusFn (IL : SSA) : sig
10 :    
11 :     structure IL : SSA
12 :    
13 :     val init : IL.program -> unit
14 :    
15 : jhr 341 val inc : IL.var -> unit
16 :    
17 : jhr 338 end = struct
18 :    
19 :     structure IL = IL
20 :    
21 : jhr 341 fun inc (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
22 : jhr 368 fun setBinding (IL.V{bind, ...}, vb) = bind := vb
23 : jhr 338
24 : jhr 1017 fun init (IL.Program{globalInit, initially, strands}) = let
25 : jhr 341 fun clearVar (IL.V{useCnt, ...}) = useCnt := 0
26 :     (* clear the counts of the variables defined in a node *)
27 :     fun clearNode (IL.ND{kind, ...}) = (case kind
28 :     of IL.JOIN{phis, ...} => List.app (fn (x, _) => clearVar x) (!phis)
29 : jhr 491 | IL.ASSIGN{stm=(x, _), ...} => clearVar x
30 : jhr 341 | _ => ()
31 :     (* end case *))
32 : jhr 613 (* clear the counts of the initially code *)
33 : jhr 621 fun clearInitially (IL.Initially{rangeInit, iters, create, ...}) = let
34 :     fun clearIter (param, lo, hi) = clearVar param
35 : jhr 613 in
36 : jhr 621 IL.CFG.apply clearNode rangeInit;
37 : jhr 613 List.app clearIter iters;
38 :     IL.CFG.apply clearNode (#1 create)
39 :     end
40 : jhr 499 (* clear the counts of the variables defined in an strand *)
41 :     fun clearStrand (IL.Strand{params, state, stateInit, methods, ...}) = let
42 : jhr 341 fun clearMethod (IL.Method{stateIn, body, ...}) = (
43 :     List.app clearVar stateIn;
44 : jhr 491 IL.CFG.apply clearNode body)
45 : jhr 341 in
46 :     List.app clearVar params;
47 : jhr 654 List.app (clearVar o #2) state;
48 : jhr 491 IL.CFG.apply clearNode stateInit;
49 : jhr 341 List.app clearMethod methods
50 :     end
51 :     (* increment the use counts of a list of variables *)
52 :     val incList = List.app inc
53 :     (* increment the counts of the variables used in a node *)
54 :     fun incNode (IL.ND{kind, ...}) = (case kind
55 : jhr 368 of IL.JOIN{phis, ...} => let
56 :     fun f (y, xs) = (
57 :     setBinding (y, IL.VB_PHI xs);
58 :     incList xs)
59 :     in
60 :     List.app f (!phis)
61 :     end
62 : jhr 341 | IL.COND{cond, ...} => inc cond
63 : jhr 491 | IL.ASSIGN{stm = (y, rhs), ...} => (
64 :     setBinding (y, IL.VB_RHS rhs);
65 :     case rhs
66 :     of (IL.VAR x) => inc x
67 :     | (IL.LIT _) => ()
68 :     | (IL.OP(_, args)) => incList args
69 : jhr 695 | (IL.APPLY(_, args)) => incList args
70 : jhr 736 | (IL.CONS(_, args)) => incList args
71 : jhr 491 (* end case *))
72 : jhr 341 | IL.NEW{args, ...} => incList args
73 : jhr 1017 | IL.EXIT{kind, live, ...} => (case kind
74 :     of ExitKind.RETURN => () (* don't count the live variables as uses *)
75 :     | ExitKind.DIE => ()
76 :     | _ => incList live
77 :     (* end case *))
78 : jhr 341 | _ => ()
79 :     (* end case *))
80 : jhr 613 (* increment the counts of variables used in the initially code *)
81 : jhr 621 fun incInitially (IL.Initially{create, rangeInit, iters, ...}) = let
82 :     fun incIter (param, lo, hi) = (
83 : jhr 613 setBinding (param, IL.VB_PARAM); (* QUESTION: should there be a special kind for this? *)
84 :     inc lo; inc hi)
85 :     in
86 : jhr 621 IL.CFG.apply incNode rangeInit;
87 : jhr 613 List.app incIter iters;
88 :     IL.CFG.apply incNode (#1 create);
89 :     List.app inc (#3 create)
90 :     end
91 : jhr 501 (* increment the counts of the variables used in a strand *)
92 : jhr 499 fun incStrand (IL.Strand{params, state, stateInit, methods, ...}) = let
93 : jhr 1017 fun incMethod (IL.Method{stateIn, body, ...}) = (
94 : jhr 794 ListPair.app
95 :     (fn (x, (_, x')) => IL.Var.setBinding(x, IL.VB_RHS(IL.VAR x')))
96 :     (stateIn, state);
97 : jhr 491 IL.CFG.apply incNode body)
98 : jhr 341 in
99 : jhr 368 List.app (fn x => setBinding(x, IL.VB_PARAM)) params;
100 : jhr 654 List.app (inc o #2) state; (* state variables are implicitly used *)
101 : jhr 491 IL.CFG.apply incNode stateInit;
102 : jhr 341 List.app incMethod methods
103 :     end
104 :     in
105 :     (* first clear the counts of all variables *)
106 : jhr 491 IL.CFG.apply clearNode globalInit;
107 : jhr 613 clearInitially initially;
108 : jhr 499 List.app clearStrand strands;
109 : jhr 341 (* then count uses *)
110 : jhr 491 IL.CFG.apply incNode globalInit;
111 : jhr 613 incInitially initially;
112 : jhr 499 List.app incStrand strands
113 : jhr 341 end
114 :    
115 : jhr 338 end

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