1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel effects accessors math math.private
4 math.integers.private math.floats.private math.partial-dispatch
5 math.intervals math.parser math.order math.functions math.libm
6 layouts words sequences sequences.private arrays assocs classes
7 classes.algebra combinators generic.math splitting fry locals
8 classes.tuple alien.accessors classes.tuple.private
9 slots.private definitions strings.private vectors hashtables
10 generic quotations alien
12 compiler.tree.comparisons
13 compiler.tree.propagation.info
14 compiler.tree.propagation.nodes
15 compiler.tree.propagation.slots
16 compiler.tree.propagation.simple
17 compiler.tree.propagation.constraints
18 compiler.tree.propagation.call-effect
19 compiler.tree.propagation.transforms
20 compiler.tree.propagation.simd ;
21 FROM: alien.c-types => (signed-interval) (unsigned-interval) ;
22 IN: compiler.tree.propagation.known-words
25 [ { number number } "input-classes" set-word-prop ] each
27 { /f < > <= >= u< u> u<= u>= }
28 [ { real real } "input-classes" set-word-prop ] each
31 [ { rational rational } "input-classes" set-word-prop ] each
33 { bitand bitor bitxor bitnot shift }
34 [ { integer integer } "input-classes" set-word-prop ] each
36 \ bitnot { integer } "input-classes" set-word-prop
38 : math-closure ( class -- newclass )
39 { fixnum bignum integer rational float real number object }
40 [ class<= ] with find nip ;
42 : fits-in-fixnum? ( interval -- ? )
43 fixnum-interval interval-subset? ;
45 : won't-overflow? ( class interval -- ? )
46 [ fixnum class<= ] [ fits-in-fixnum? ] bi* and ;
48 : may-overflow ( class interval -- class' interval' )
51 [ [ integer math-class-max ] dip ] unless
54 : may-be-rational ( class interval -- class' interval' )
56 [ rational math-class-max ] dip
59 : ensure-math-class ( class must-be -- class' )
62 : number-valued ( class interval -- class' interval' )
63 [ number ensure-math-class ] dip ;
65 : fixnum-valued ( class interval -- class' interval' )
70 : integer-valued ( class interval -- class' interval' )
71 [ integer ensure-math-class ] dip ;
73 : real-valued ( class interval -- class' interval' )
74 [ real ensure-math-class ] dip ;
76 : float-valued ( class interval -- class' interval' )
81 : unary-op-class ( info -- newclass )
82 class>> dup null-class? [ drop null ] [ math-closure ] if ;
84 : unary-op-interval ( info quot -- newinterval )
86 dup class>> real classes-intersect?
87 [ interval>> ] [ drop full-interval ] if
90 : unary-op ( word interval-quot post-proc-quot -- )
92 [ unary-op-class ] [ _ unary-op-interval ] bi
95 ] "outputs" set-word-prop ;
97 { bitnot fixnum-bitnot bignum-bitnot } [
98 [ interval-bitnot ] [ integer-valued ] unary-op
101 \ abs [ interval-abs ] [ may-overflow real-valued ] unary-op
103 \ absq [ interval-absq ] [ may-overflow real-valued ] unary-op
105 : binary-op-class ( info1 info2 -- newclass )
107 2dup [ null-class? ] either? [ 2drop null ] [
108 [ math-closure ] bi@ math-class-max
111 : binary-op-interval ( info1 info2 quot -- newinterval )
112 [ [ interval>> ] bi@ ] dip call ; inline
114 : binary-op ( word interval-quot post-proc-quot -- )
116 [ binary-op-class ] [ _ binary-op-interval ] 2bi
118 <class/interval-info>
119 ] "outputs" set-word-prop ;
121 \ + [ [ interval+ ] [ may-overflow number-valued ] binary-op ] each-derived-op
122 \ + [ [ interval+ ] [ number-valued ] binary-op ] each-fast-derived-op
124 \ - [ [ interval- ] [ may-overflow number-valued ] binary-op ] each-derived-op
125 \ - [ [ interval- ] [ number-valued ] binary-op ] each-fast-derived-op
127 \ * [ [ interval* ] [ may-overflow number-valued ] binary-op ] each-derived-op
128 \ * [ [ interval* ] [ number-valued ] binary-op ] each-fast-derived-op
130 \ / [ [ interval/-safe ] [ may-be-rational number-valued ] binary-op ] each-derived-op
131 \ /i [ [ interval/i ] [ may-overflow integer-valued ] binary-op ] each-derived-op
132 \ /f [ [ interval/f ] [ float-valued ] binary-op ] each-derived-op
134 \ mod [ [ interval-mod ] [ real-valued ] binary-op ] each-derived-op
135 \ rem [ [ interval-rem ] [ may-overflow real-valued ] binary-op ] each-derived-op
137 { /mod fixnum/mod } [
139 [ "outputs" word-prop ] bi@
140 '[ _ _ 2bi ] "outputs" set-word-prop
143 \ shift [ [ interval-shift-safe ] [ may-overflow integer-valued ] binary-op ] each-derived-op
144 \ shift [ [ interval-shift-safe ] [ integer-valued ] binary-op ] each-fast-derived-op
146 \ bitand [ [ interval-bitand ] [ integer-valued ] binary-op ] each-derived-op
147 \ bitor [ [ interval-bitor ] [ integer-valued ] binary-op ] each-derived-op
148 \ bitxor [ [ interval-bitxor ] [ integer-valued ] binary-op ] each-derived-op
150 :: (comparison-constraints) ( in1 in2 op -- constraint )
151 [let | i1 [ in1 value-info interval>> ]
152 i2 [ in2 value-info interval>> ] |
153 in1 i1 i2 op assumption is-in-interval
154 in2 i2 i1 op swap-comparison assumption is-in-interval
158 :: comparison-constraints ( in1 in2 out op -- constraint )
159 in1 in2 op (comparison-constraints) out t-->
160 in1 in2 op negate-comparison (comparison-constraints) out f--> /\ ;
162 : define-comparison-constraints ( word op -- )
163 '[ _ comparison-constraints ] "constraints" set-word-prop ;
166 [ dup '[ _ define-comparison-constraints ] each-derived-op ] each
168 ! Remove redundant comparisons
169 : fold-comparison ( info1 info2 word -- info )
170 [ [ interval>> ] bi@ ] dip interval-comparison {
171 { incomparable [ object-info ] }
172 { t [ t <literal-info> ] }
173 { f [ f <literal-info> ] }
178 [ _ fold-comparison ] "outputs" set-word-prop
182 generic-comparison-ops [
183 dup specific-comparison
184 '[ _ fold-comparison ] "outputs" set-word-prop
187 : maybe-or-never ( ? -- info )
188 [ object-info ] [ f <literal-info> ] if ;
190 : info-intervals-intersect? ( info1 info2 -- ? )
191 2dup [ class>> real class<= ] both?
192 [ [ interval>> ] bi@ intervals-intersect? ] [ 2drop t ] if ;
194 { number= bignum= float= } [
196 info-intervals-intersect? maybe-or-never
197 ] "outputs" set-word-prop
200 : info-classes-intersect? ( info1 info2 -- ? )
201 [ class>> ] bi@ classes-intersect? ;
204 over value-info literal>> fixnum? [
205 [ value-info literal>> is-equal-to ] dip t-->
207 ] "constraints" set-word-prop
210 [ info-intervals-intersect? ]
211 [ info-classes-intersect? ]
212 2bi and maybe-or-never
213 ] "outputs" set-word-prop
217 { [ 2dup [ fixnum classes-intersect? not ] either? ] [ f <literal-info> ] }
218 { [ 2dup [ fixnum class<= ] both? ] [ t <literal-info> ] }
221 ] "outputs" set-word-prop
225 { bignum>fixnum fixnum }
228 { fixnum>bignum bignum }
229 { float>bignum bignum }
232 { fixnum>float float }
233 { bignum>float float }
237 '[ _ swap interval>> <class/interval-info> ] "outputs" set-word-prop
240 { numerator denominator }
241 [ [ drop integer <class-info> ] "outputs" set-word-prop ] each
243 { (log2) fixnum-log2 bignum-log2 } [
245 [ class>> ] [ interval>> interval-log2 ] bi <class/interval-info>
246 ] "outputs" set-word-prop
250 2drop fixnum 0 23 2^ [a,b] <class/interval-info>
251 ] "outputs" set-word-prop
264 { [ "alien-signed-" ?head ] [ string>number (signed-interval) ] }
265 { [ "alien-unsigned-" ?head ] [ string>number (unsigned-interval) ] }
267 [ fits-in-fixnum? fixnum integer ? ] keep <class/interval-info>
268 '[ 2drop _ ] "outputs" set-word-prop
272 2drop simple-alien \ f class-or <class-info>
273 ] "outputs" set-word-prop
275 { <tuple> <tuple-boa> } [
277 literal>> dup array? [ first ] [ drop tuple ] if <class-info>
279 ] "outputs" set-word-prop
283 literal>> dup tuple-class? [ drop tuple ] unless <class-info>
284 ] "outputs" set-word-prop
286 ! the output of clone has the same type as the input
287 : cloned-value-info ( value-info -- value-info' )
288 clone f >>literal f >>literal?
289 [ [ dup [ cloned-value-info ] when ] map ] change-slots ;
292 [ cloned-value-info ] "outputs" set-word-prop
297 [ literal>> swap value-info-slot ] [ 2drop object-info ] if
298 ] "outputs" set-word-prop
301 [ value-info ] dip over literal>> class? [
302 [ literal>> ] dip predicate-constraints
304 ] "constraints" set-word-prop
307 ! We need to force the caller word to recompile when the class
308 ! is redefined, since now we're making assumptions but the
309 ! class definition itself.
313 [ inlined-dependency depends-on ]
314 [ predicate-output-infos ]
316 ] [ 2drop object-info ] if
317 ] "outputs" set-word-prop
319 { facos fasin fatan fatan2 fcos fsin ftan fcosh fsinh ftanh fexp
320 flog fpow fsqrt facosh fasinh fatanh } [
321 { float } "default-output-classes" set-word-prop
324 ! Find a less repetitive way of doing this
325 \ float-min { float float } "input-classes" set-word-prop
326 \ float-min [ interval-min ] [ float-valued ] binary-op
328 \ float-max { float float } "input-classes" set-word-prop
329 \ float-max [ interval-max ] [ float-valued ] binary-op
331 \ fixnum-min { fixnum fixnum } "input-classes" set-word-prop
332 \ fixnum-min [ interval-min ] [ fixnum-valued ] binary-op
334 \ fixnum-max { fixnum fixnum } "input-classes" set-word-prop
335 \ fixnum-max [ interval-max ] [ fixnum-valued ] binary-op