12 |
|
|
13 |
val init : IL.program -> unit |
val init : IL.program -> unit |
14 |
|
|
15 |
|
val inc : IL.var -> unit |
16 |
|
|
17 |
end = struct |
end = struct |
18 |
|
|
19 |
structure IL = IL |
structure IL = IL |
20 |
|
|
21 |
fun init (IL.Program{globals, globalInit, actors}) = () (* FIXME *) |
fun inc (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1) |
22 |
|
|
23 |
|
fun init (IL.Program{globals, globalInit, actors}) = let |
24 |
|
fun clearVar (IL.V{useCnt, ...}) = useCnt := 0 |
25 |
|
(* clear the counts of the variables defined in a node *) |
26 |
|
fun clearNode (IL.ND{kind, ...}) = (case kind |
27 |
|
of IL.JOIN{phis, ...} => List.app (fn (x, _) => clearVar x) (!phis) |
28 |
|
| IL.BLOCK{body, ...} => List.app (fn (x, _) => clearVar x) (!body) |
29 |
|
| _ => () |
30 |
|
(* end case *)) |
31 |
|
(* clear the counts of the variables defined in an actor *) |
32 |
|
fun clearActor (IL.Actor{params, state, stateInit, methods, ...}) = let |
33 |
|
fun clearMethod (IL.Method{stateIn, body, ...}) = ( |
34 |
|
List.app clearVar stateIn; |
35 |
|
IL.applyToNodes clearNode body) |
36 |
|
in |
37 |
|
List.app clearVar params; |
38 |
|
List.app clearVar state; |
39 |
|
IL.applyToNodes clearNode stateInit; |
40 |
|
List.app clearMethod methods |
41 |
|
end |
42 |
|
(* increment the use counts of a list of variables *) |
43 |
|
val incList = List.app inc |
44 |
|
(* increment the counts of the variables used in a node *) |
45 |
|
fun incNode (IL.ND{kind, ...}) = (case kind |
46 |
|
of IL.JOIN{phis, ...} => List.app (fn (_, xs) => incList xs) (!phis) |
47 |
|
| IL.COND{cond, ...} => inc cond |
48 |
|
| IL.BLOCK{body, ...} => let |
49 |
|
fun incRHS (IL.VAR x) = inc x |
50 |
|
| incRHS (IL.LIT _) = () |
51 |
|
| incRHS (IL.OP(_, args)) = incList args |
52 |
|
| incRHS (IL.CONS args) = incList args |
53 |
|
in |
54 |
|
List.app (fn (_, rhs) => incRHS rhs) (!body) |
55 |
|
end |
56 |
|
| IL.NEW{args, ...} => incList args |
57 |
|
| _ => () |
58 |
|
(* end case *)) |
59 |
|
(* increment the counts of the variables used in an actor *) |
60 |
|
fun incActor (IL.Actor{params, state, stateInit, methods, ...}) = let |
61 |
|
fun incMethod (IL.Method{stateOut, body, ...}) = ( |
62 |
|
incList stateOut; |
63 |
|
IL.applyToNodes incNode body) |
64 |
|
in |
65 |
|
IL.applyToNodes incNode stateInit; |
66 |
|
List.app incMethod methods |
67 |
|
end |
68 |
|
in |
69 |
|
(* first clear the counts of all variables *) |
70 |
|
List.app clearVar globals; |
71 |
|
IL.applyToNodes clearNode globalInit; |
72 |
|
List.app clearActor actors; |
73 |
|
(* then count uses *) |
74 |
|
IL.applyToNodes incNode globalInit; |
75 |
|
List.app incActor actors |
76 |
|
end |
77 |
|
|
78 |
end |
end |