]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/propagation/branches/branches.factor
merge project-euler.factor
[factor.git] / basis / compiler / tree / propagation / branches / branches.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: fry kernel sequences assocs accessors namespaces
4 math.intervals arrays classes.algebra combinators columns
5 stack-checker.branches locals math
6 compiler.utilities
7 compiler.tree
8 compiler.tree.combinators
9 compiler.tree.propagation.info
10 compiler.tree.propagation.nodes
11 compiler.tree.propagation.simple
12 compiler.tree.propagation.constraints ;
13 IN: compiler.tree.propagation.branches
14
15 ! For conditionals, an assoc of child node # --> constraint
16 GENERIC: child-constraints ( node -- seq )
17
18 M: #if child-constraints
19     in-d>> first [ =t ] [ =f ] bi 2array ;
20
21 M: #dispatch child-constraints
22     children>> length f <repetition> ;
23
24 ! There is an important invariant here, either no flags are set
25 ! in live-branches, exactly one is set, or all are set.
26
27 GENERIC: live-branches ( #branch -- indices )
28
29 M: #if live-branches
30     in-d>> first value-info class>> {
31         { [ dup null-class? ] [ { f f } ] }
32         { [ dup true-class? ] [ { t f } ] }
33         { [ dup false-class? ] [ { f t } ] }
34         [ { t t } ]
35     } cond nip ;
36
37 M: #dispatch live-branches
38     [ children>> ] [ in-d>> first value-info ] bi {
39         { [ dup class>> null-class? ] [ drop length f <array> ] }
40         { [ dup literal>> integer? not ] [ drop length t <array> ] }
41         { [ 2dup literal>> swap bounds-check? not ] [ drop length t <array> ] }
42         [ literal>> swap length f <array> [ [ t ] 2dip set-nth ] keep ]
43     } cond ;
44
45 : live-children ( #branch -- children )
46     [ children>> ] [ live-branches>> ] bi select-children ;
47
48 SYMBOL: infer-children-data
49
50 : copy-value-info ( -- )
51     value-infos [ H{ } clone suffix ] change
52     constraints [ H{ } clone suffix ] change ;
53
54 : no-value-info ( -- )
55     value-infos off
56     constraints off ;
57
58 : infer-children ( node -- )
59     [ live-children ] [ child-constraints ] bi [
60         [
61             over
62             [ copy-value-info assume (propagate) ]
63             [ 2drop no-value-info ]
64             if
65         ] H{ } make-assoc
66     ] 2map infer-children-data set ;
67
68 : compute-phi-input-infos ( phi-in -- phi-info )
69     infer-children-data get
70     [
71         '[
72             _ [
73                 dup +bottom+ eq?
74                 [ drop null-info ] [ value-info ] if
75             ] bind
76         ] map
77     ] 2map ;
78
79 : annotate-phi-inputs ( #phi -- )
80     dup phi-in-d>> compute-phi-input-infos >>phi-info-d drop ;
81
82 : merge-value-infos ( infos outputs -- )
83     [ [ value-infos-union ] map ] dip set-value-infos ;
84
85 SYMBOL: condition-value
86
87 M: #phi propagate-before ( #phi -- )
88     [ annotate-phi-inputs ]
89     [ [ phi-info-d>> flip ] [ out-d>> ] bi merge-value-infos ]
90     bi ;
91
92 :: update-constraints ( new old -- )
93     new [| key value | key old [ value append ] change-at ] assoc-each ;
94
95 : include-child-constraints ( i -- )
96     infer-children-data get nth constraints swap at last
97     constraints get last update-constraints ;
98
99 : branch-phi-constraints ( output values booleans -- )
100      {
101         {
102             { { t } { f } }
103             [
104                 drop condition-value get
105                 [ [ =t ] [ =t ] bi* <--> ]
106                 [ [ =f ] [ =f ] bi* <--> ] 2bi /\
107             ]
108         }
109         {
110             { { f } { t } }
111             [
112                 drop condition-value get
113                 [ [ =t ] [ =f ] bi* <--> ]
114                 [ [ =f ] [ =t ] bi* <--> ] 2bi /\
115             ]
116         }
117         {
118             { { t f } { f } }
119             [
120                 first =t
121                 condition-value get =t /\
122                 swap t-->
123             ]
124         }
125         {
126             { { f } { t f } }
127             [
128                 second =t
129                 condition-value get =f /\
130                 swap t-->
131             ]
132         }
133         {
134             { { t f } { } }
135             [
136                 first
137                 [ [ =t ] bi@ <--> ]
138                 [ [ =f ] bi@ <--> ] 2bi /\
139                 0 include-child-constraints
140             ]
141         }
142         {
143             { { } { t f } }
144             [
145                 second
146                 [ [ =t ] bi@ <--> ]
147                 [ [ =f ] bi@ <--> ] 2bi /\
148                 1 include-child-constraints
149             ]
150         }
151         [ 3drop f ]
152     } case assume ;
153
154 M: #phi propagate-after ( #phi -- )
155     condition-value get [
156         [ out-d>> ]
157         [ phi-in-d>> flip ]
158         [ phi-info-d>> flip ] tri
159         [
160             [ possible-boolean-values ] map
161             branch-phi-constraints
162         ] 3each
163     ] [ drop ] if ;
164
165 M: #branch propagate-around
166     dup live-branches >>live-branches
167     [ infer-children ] [ annotate-node ] bi ;
168
169 M: #if propagate-around
170     [ in-d>> first condition-value set ] [ call-next-method ] bi ;
171
172 M: #dispatch propagate-around
173     condition-value off call-next-method ;