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
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
15 ! For conditionals, an assoc of child node # --> constraint
16 GENERIC: child-constraints ( node -- seq )
18 M: #if child-constraints
19 in-d>> first [ =t ] [ =f ] bi 2array ;
21 M: #dispatch child-constraints
22 children>> length f <repetition> ;
24 ! There is an important invariant here, either no flags are set
25 ! in live-branches, exactly one is set, or all are set.
27 GENERIC: live-branches ( #branch -- indices )
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 } ] }
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 ]
45 : live-children ( #branch -- children )
46 [ children>> ] [ live-branches>> ] bi select-children ;
48 SYMBOL: infer-children-data
50 : copy-value-info ( -- )
51 value-infos [ H{ } clone suffix ] change
52 constraints [ H{ } clone suffix ] change ;
54 : no-value-info ( -- )
58 : infer-children ( node -- )
59 [ live-children ] [ child-constraints ] bi [
62 [ copy-value-info assume (propagate) ]
63 [ 2drop no-value-info ]
66 ] 2map infer-children-data set ;
68 : compute-phi-input-infos ( phi-in -- phi-info )
69 infer-children-data get
74 [ drop null-info ] [ value-info ] if
79 : annotate-phi-inputs ( #phi -- )
80 dup phi-in-d>> compute-phi-input-infos >>phi-info-d drop ;
82 : merge-value-infos ( infos outputs -- )
83 [ [ value-infos-union ] map ] dip set-value-infos ;
85 SYMBOL: condition-value
87 M: #phi propagate-before ( #phi -- )
88 [ annotate-phi-inputs ]
89 [ [ phi-info-d>> flip ] [ out-d>> ] bi merge-value-infos ]
92 :: update-constraints ( new old -- )
93 new [| key value | key old [ value append ] change-at ] assoc-each ;
95 : include-child-constraints ( i -- )
96 infer-children-data get nth constraints swap at last
97 constraints get last update-constraints ;
99 : branch-phi-constraints ( output values booleans -- )
104 drop condition-value get
105 [ [ =t ] [ =t ] bi* <--> ]
106 [ [ =f ] [ =f ] bi* <--> ] 2bi /\
112 drop condition-value get
113 [ [ =t ] [ =f ] bi* <--> ]
114 [ [ =f ] [ =t ] bi* <--> ] 2bi /\
121 condition-value get =t /\
129 condition-value get =f /\
137 condition-value get =t /\
145 condition-value get =f /\
154 [ [ =f ] bi@ <--> ] 2bi /\
155 0 include-child-constraints
163 [ [ =f ] bi@ <--> ] 2bi /\
164 1 include-child-constraints
170 M: #phi propagate-after ( #phi -- )
171 condition-value get [
174 [ phi-info-d>> flip ] tri
176 [ possible-boolean-values ] map
177 branch-phi-constraints
181 M: #branch propagate-around
182 dup live-branches >>live-branches
183 [ infer-children ] [ annotate-node ] bi ;
185 M: #if propagate-around
186 [ in-d>> first condition-value set ] [ call-next-method ] bi ;
188 M: #dispatch propagate-around
189 condition-value off call-next-method ;