1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types arrays assocs classes
4 classes.algebra classes.algebra.private classes.maybe
5 classes.tuple.private combinators combinators.short-circuit
6 compiler.tree compiler.tree.propagation.constraints
7 compiler.tree.propagation.info
8 compiler.tree.propagation.inlining
9 compiler.tree.propagation.nodes compiler.tree.propagation.slots
10 continuations fry kernel sequences stack-checker.dependencies
12 IN: compiler.tree.propagation.simple
14 M: #introduce propagate-before
15 out-d>> [ object-info swap set-value-info ] each ;
17 M: #push propagate-before
18 [ literal>> <literal-info> ] [ out-d>> first ] bi
21 : refine-value-infos ( classes values -- )
22 [ refine-value-info ] 2each ;
24 : class-infos ( classes -- infos )
25 [ <class-info> ] map ;
27 : set-value-infos ( infos values -- )
28 [ set-value-info ] 2each ;
30 GENERIC: add-depends-on-class ( obj -- )
32 M: class add-depends-on-class
33 add-depends-on-conditionally ;
35 M: maybe add-depends-on-class
36 class>> add-depends-on-class ;
38 M: anonymous-union add-depends-on-class
39 members>> [ add-depends-on-class ] each ;
41 M: anonymous-intersection add-depends-on-class
42 participants>> [ add-depends-on-class ] each ;
44 M: #declare propagate-before
45 #! We need to force the caller word to recompile when the
46 #! classes mentioned in the declaration are redefined, since
47 #! now we're making assumptions about their definitions.
49 [ add-depends-on-class ]
50 [ <class-info> swap refine-value-info ]
54 : predicate-constraints ( value class boolean-value -- constraint )
55 [ [ is-instance-of ] dip t--> ]
56 [ [ class-not is-instance-of ] dip f--> ]
59 : custom-constraints ( #call quot -- )
60 [ [ in-d>> ] [ out-d>> ] bi append ] dip
61 with-datastack first assume ;
63 : compute-constraints ( #call word -- )
64 dup "constraints" word-prop [ nip custom-constraints ] [
66 [ [ in-d>> first ] [ out-d>> first ] bi ]
67 [ "predicating" word-prop ] bi*
68 swap predicate-constraints assume
72 ERROR: invalid-outputs #call infos ;
74 : check-outputs ( #call infos -- infos )
75 over out-d>> over [ length ] bi@ =
76 [ nip ] [ throw-invalid-outputs ] if ;
78 : call-outputs-quot ( #call word -- infos )
80 [ in-d>> [ value-info ] map ]
81 [ "outputs" word-prop ] bi*
82 with-datastack check-outputs ;
84 : literal-inputs? ( #call -- ? )
85 in-d>> [ value-info literal?>> ] all? ;
87 : input-classes-match? ( #call word -- ? )
88 [ in-d>> ] [ "input-classes" word-prop ] bi*
89 [ [ value-info literal>> ] dip instance? ] 2all? ;
91 : foldable-call? ( #call word -- ? )
94 [ drop literal-inputs? ]
95 [ input-classes-match? ]
98 : (fold-call) ( #call word -- info )
99 [ [ out-d>> ] [ in-d>> [ value-info literal>> ] map ] bi ] [ '[ _ execute ] ] bi*
100 '[ _ _ with-datastack [ <literal-info> ] map nip ]
101 [ drop length [ object-info ] replicate ]
104 : fold-call ( #call word -- )
105 [ (fold-call) ] [ drop out-d>> ] 2bi set-value-infos ;
107 : predicate-output-infos/literal ( info class -- info )
109 '[ _ _ instance? <literal-info> ]
113 : predicate-output-infos/class ( info class -- info )
114 [ class>> ] dip evaluate-class-predicate
115 dup +incomparable+ eq? [ drop object-info ] [ <literal-info> ] if ;
117 : predicate-output-infos ( info class -- info )
119 [ predicate-output-infos/literal ]
120 [ predicate-output-infos/class ]
123 : propagate-predicate ( #call word -- infos )
124 #! We need to force the caller word to recompile when the class
125 #! is redefined, since now we're making assumptions but the
126 #! class definition itself.
127 [ in-d>> first value-info ]
128 [ "predicating" word-prop ] bi*
129 [ nip add-depends-on-conditionally ]
130 [ predicate-output-infos 1array ] 2bi ;
132 : default-output-value-infos ( #call word -- infos )
133 "default-output-classes" word-prop
134 [ class-infos ] [ out-d>> length object-info <repetition> ] ?if ;
136 : output-value-infos ( #call word -- infos )
138 { [ dup \ <tuple-boa> eq? ] [ drop propagate-<tuple-boa> ] }
139 { [ dup sequence-constructor? ] [ propagate-sequence-constructor ] }
140 { [ dup predicate? ] [ propagate-predicate ] }
141 { [ dup "outputs" word-prop ] [ call-outputs-quot ] }
142 [ default-output-value-infos ]
145 M: #call propagate-before
147 { [ 2dup foldable-call? ] [ fold-call ] }
148 { [ 2dup do-inlining ] [
149 [ output-value-infos ] [ drop out-d>> ] 2bi refine-value-infos
152 [ [ output-value-infos ] [ drop out-d>> ] 2bi set-value-infos ]
153 [ compute-constraints ]
158 M: #call annotate-node
159 dup [ in-d>> ] [ out-d>> ] bi append (annotate-node) ;
161 : propagate-input-classes ( node input-classes -- )
162 class-infos swap in-d>> refine-value-infos ;
164 M: #call propagate-after
165 dup word>> "input-classes" word-prop dup
166 [ propagate-input-classes ] [ 2drop ] if ;
168 : propagate-alien-invoke ( node -- )
169 [ out-d>> ] [ params>> return>> ] bi
170 [ drop ] [ c-type-class <class-info> swap first set-value-info ] if-void ;
172 M: #alien-node propagate-before propagate-alien-invoke ;
174 M: #alien-callback propagate-around child>> (propagate) ;
176 M: #return annotate-node dup in-d>> (annotate-node) ;