1 ! Copyright (C) 2008 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
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 GENERIC: live-branches ( #branch -- indices )
27 in-d>> first value-info class>> {
28 { [ dup null-class? ] [ { f f } ] }
29 { [ dup true-class? ] [ { t f } ] }
30 { [ dup false-class? ] [ { f t } ] }
34 M: #dispatch live-branches
35 [ children>> length ] [ in-d>> first value-info interval>> ] bi
36 '[ _ interval-contains? ] map ;
38 : live-children ( #branch -- children )
39 [ children>> ] [ live-branches>> ] bi select-children ;
41 SYMBOL: infer-children-data
43 : copy-value-info ( -- )
44 value-infos [ H{ } clone suffix ] change
45 constraints [ H{ } clone suffix ] change ;
47 : no-value-info ( -- )
51 : infer-children ( node -- )
52 [ live-children ] [ child-constraints ] bi [
55 [ copy-value-info assume (propagate) ]
56 [ 2drop no-value-info ]
59 ] 2map infer-children-data set ;
61 : compute-phi-input-infos ( phi-in -- phi-info )
62 infer-children-data get
67 [ drop null-info ] [ value-info ] if
72 : annotate-phi-inputs ( #phi -- )
73 dup phi-in-d>> compute-phi-input-infos >>phi-info-d drop ;
75 : merge-value-infos ( infos outputs -- )
76 [ [ value-infos-union ] map ] dip set-value-infos ;
78 SYMBOL: condition-value
80 M: #phi propagate-before ( #phi -- )
81 [ annotate-phi-inputs ]
82 [ [ phi-info-d>> flip ] [ out-d>> ] bi merge-value-infos ]
85 :: update-constraints ( new old -- )
86 new [| key value | key old [ value append ] change-at ] assoc-each ;
88 : include-child-constraints ( i -- )
89 infer-children-data get nth constraints swap at last
90 constraints get last update-constraints ;
92 : branch-phi-constraints ( output values booleans -- )
97 drop condition-value get
98 [ [ =t ] [ =t ] bi* <--> ]
99 [ [ =f ] [ =f ] bi* <--> ] 2bi /\
105 drop condition-value get
106 [ [ =t ] [ =f ] bi* <--> ]
107 [ [ =f ] [ =t ] bi* <--> ] 2bi /\
114 condition-value get =t /\
122 condition-value get =f /\
131 [ [ =f ] bi@ <--> ] 2bi /\
132 0 include-child-constraints
140 [ [ =f ] bi@ <--> ] 2bi /\
141 1 include-child-constraints
147 M: #phi propagate-after ( #phi -- )
148 condition-value get [
151 [ phi-info-d>> flip ] tri
153 [ possible-boolean-values ] map
154 branch-phi-constraints
158 M: #phi propagate-around ( #phi -- )
160 [ propagate-before ] [ propagate-after ] bi ;
162 M: #branch propagate-around
163 dup live-branches >>live-branches
164 [ infer-children ] [ annotate-node ] bi ;
166 M: #if propagate-around
167 [ in-d>> first condition-value set ] [ call-next-method ] bi ;
169 M: #dispatch propagate-around
170 condition-value off call-next-method ;