1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs byte-arrays classes
4 classes.algebra classes.singleton classes.tuple
5 classes.tuple.private combinators combinators.short-circuit
6 compiler.tree.propagation.copy kernel layouts math
7 math.intervals namespaces sequences sequences.private strings
9 IN: compiler.tree.propagation.info
11 : false-class? ( class -- ? ) \ f class<= ;
13 : true-class? ( class -- ? ) \ f class-not class<= ;
15 : null-class? ( class -- ? ) null class<= ;
17 GENERIC: eql? ( obj1 obj2 -- ? )
20 M: bignum eql? over bignum? [ = ] [ 2drop f ] if ;
21 M: ratio eql? over ratio? [ = ] [ 2drop f ] if ;
22 M: float eql? over float? [ [ double>bits ] same? ] [ 2drop f ] if ;
23 M: complex eql? over complex? [ = ] [ 2drop f ] if ;
25 TUPLE: value-info-state
32 CONSTANT: null-info T{ value-info-state f null empty-interval }
34 CONSTANT: object-info T{ value-info-state f object full-interval }
36 : interval>literal ( class interval -- literal literal? )
37 #! If interval has zero length and the class is sufficiently
38 #! precise, we can turn it into a literal
39 dup special-interval? [
43 { [ over interval-length 0 > ] [ 3drop f f ] }
44 { [ pick bignum class<= ] [ 2nip >bignum t ] }
45 { [ pick integer class<= ] [ 2nip >fixnum t ] }
46 { [ pick float class<= ] [ 2nip [ f f ] [ >float t ] if-zero ] }
51 : <value-info> ( -- info ) \ value-info-state new ; inline
55 : tuple-slot-infos ( tuple -- slots )
56 [ tuple-slots ] [ class-of all-slots ] bi
57 [ read-only>> [ <literal-info> ] [ drop f ] if ] 2map
60 UNION: fixed-length array byte-array string ;
62 : literal-class ( obj -- class )
63 #! Handle forgotten tuples and singleton classes properly
64 dup singleton-class? [
70 : (slots-with-length) ( length class -- slots )
71 "slots" word-prop length 1 - f <array> swap prefix ;
73 : slots-with-length ( seq -- slots )
74 [ length <literal-info> ] [ class-of ] bi (slots-with-length) ;
76 : init-literal-info ( info -- info )
77 empty-interval >>interval
78 dup literal>> literal-class >>class
80 { [ dup real? ] [ [a,a] >>interval ] }
81 { [ dup tuple? ] [ tuple-slot-infos >>slots ] }
82 { [ dup fixed-length? ] [ slots-with-length >>slots ] }
86 : empty-set? ( info -- ? )
88 [ class>> null-class? ]
89 [ [ interval>> empty-interval eq? ] [ class>> real class<= ] bi and ]
92 : min-value ( class -- n )
94 { fixnum [ most-negative-fixnum ] }
95 { array-capacity [ 0 ] }
99 : max-value ( class -- n )
101 { fixnum [ most-positive-fixnum ] }
102 { array-capacity [ max-array-capacity ] }
106 : class-interval ( class -- i )
108 { fixnum [ fixnum-interval ] }
109 { array-capacity [ array-capacity-interval ] }
110 [ drop full-interval ]
113 : wrap-interval ( interval class -- interval' )
115 { [ over empty-interval eq? ] [ drop ] }
116 { [ over full-interval eq? ] [ nip class-interval ] }
117 { [ 2dup class-interval interval-subset? not ] [ nip class-interval ] }
121 : init-interval ( info -- info )
122 dup [ interval>> full-interval or ] [ class>> ] bi wrap-interval >>interval
123 dup class>> integer class<= [ [ integral-closure ] change-interval ] when ; inline
125 : init-value-info ( info -- info )
131 empty-interval >>interval
134 dup [ class>> ] [ interval>> ] bi interval>literal
135 [ >>literal ] [ >>literal? ] bi*
139 : <class/interval-info> ( class interval -- info )
143 init-value-info ; foldable
145 : <class-info> ( class -- info )
146 f <class/interval-info> ; foldable
148 : <interval-info> ( interval -- info )
152 init-value-info ; foldable
154 : <literal-info> ( literal -- info )
158 init-value-info ; foldable
160 : <sequence-info> ( length class -- info )
163 [ (slots-with-length) ] dip swap >>slots
166 : <tuple-info> ( slots class -- info )
172 : >literal< ( info -- literal literal? )
173 [ literal>> ] [ literal?>> ] bi ;
175 : intersect-literals ( info1 info2 -- literal literal? )
177 { [ dup literal?>> not ] [ drop >literal< ] }
178 { [ over literal?>> not ] [ nip >literal< ] }
179 { [ 2dup [ literal>> ] bi@ eql? not ] [ 2drop f f ] }
183 DEFER: value-info-intersect
185 DEFER: (value-info-intersect)
187 : intersect-slot ( info1 info2 -- info )
189 { [ dup not ] [ nip ] }
190 { [ over not ] [ drop ] }
191 [ (value-info-intersect) ]
194 : intersect-slots ( info1 info2 -- slots )
196 { [ dup not ] [ drop ] }
197 { [ over not ] [ nip ] }
199 2dup [ length ] same?
200 [ [ intersect-slot ] 2map ] [ 2drop f ] if
204 : (value-info-intersect) ( info1 info2 -- info )
205 [ <value-info> ] 2dip
207 [ [ class>> ] bi@ class-and >>class ]
208 [ [ interval>> ] bi@ interval-intersect >>interval ]
209 [ intersect-literals [ >>literal ] [ >>literal? ] bi* ]
210 [ intersect-slots >>slots ]
214 : value-info-intersect ( info1 info2 -- info )
216 { [ dup class>> null-class? ] [ nip ] }
217 { [ over class>> null-class? ] [ drop ] }
218 [ (value-info-intersect) ]
221 : union-literals ( info1 info2 -- literal literal? )
222 2dup [ literal?>> ] both? [
223 [ literal>> ] bi@ 2dup eql? [ drop t ] [ 2drop f f ] if
226 DEFER: value-info-union
228 DEFER: (value-info-union)
230 : union-slot ( info1 info2 -- info )
232 { [ dup not ] [ nip ] }
233 { [ over not ] [ drop ] }
234 [ (value-info-union) ]
237 : union-slots ( info1 info2 -- slots )
239 2dup [ length ] same?
240 [ [ union-slot ] 2map ] [ 2drop f ] if ;
242 : (value-info-union) ( info1 info2 -- info )
243 [ <value-info> ] 2dip
245 [ [ class>> ] bi@ class-or >>class ]
246 [ [ interval>> ] bi@ interval-union >>interval ]
247 [ union-literals [ >>literal ] [ >>literal? ] bi* ]
248 [ union-slots >>slots ]
252 : value-info-union ( info1 info2 -- info )
254 { [ dup class>> null-class? ] [ drop ] }
255 { [ over class>> null-class? ] [ nip ] }
256 [ (value-info-union) ]
259 : value-infos-union ( infos -- info )
261 [ [ ] [ value-info-union ] map-reduce ] if-empty ;
263 : literals<= ( info1 info2 -- ? )
265 { [ dup literal?>> not ] [ 2drop t ] }
266 { [ over literal?>> not ] [ drop class>> null-class? ] }
267 [ [ literal>> ] bi@ eql? ]
270 : value-info<= ( info1 info2 -- ? )
272 { [ dup not ] [ 2drop t ] }
273 { [ over not ] [ 2drop f ] }
276 { [ 2dup [ class>> ] bi@ class<= not ] [ f ] }
277 { [ 2dup [ interval>> ] bi@ interval-subset? not ] [ f ] }
278 { [ 2dup literals<= not ] [ f ] }
279 { [ 2dup [ slots>> ] bi@ [ value-info<= ] 2all? not ] [ f ] }
287 : value-info* ( value -- info ? )
288 resolve-copy value-infos get assoc-stack
289 [ null-info or ] [ >boolean ] bi ; inline
291 : value-info ( value -- info )
294 : set-value-info ( info value -- )
295 resolve-copy value-infos get last set-at ;
297 : refine-value-info ( info value -- )
298 resolve-copy value-infos get
299 [ assoc-stack [ value-info-intersect ] when* ] 2keep
302 : value-literal ( value -- obj ? )
303 value-info >literal< ;
305 : possible-boolean-values ( info -- values )
307 { [ dup null-class? ] [ { } ] }
308 { [ dup true-class? ] [ { t } ] }
309 { [ dup false-class? ] [ { f } ] }
313 : node-value-info ( node value -- info )
314 swap info>> at* [ drop null-info ] unless ;
316 : node-input-infos ( node -- seq )
317 dup in-d>> [ node-value-info ] with map ;
319 : node-output-infos ( node -- seq )
320 dup out-d>> [ node-value-info ] with map ;
322 : first-literal ( #call -- obj )
323 dup in-d>> first node-value-info literal>> ;
325 : last-literal ( #call -- obj )
326 dup out-d>> last node-value-info literal>> ;
328 : immutable-tuple-boa? ( #call -- ? )
329 dup word>> \ <tuple-boa> eq? [
330 dup in-d>> last node-value-info
331 literal>> first immutable-tuple-class?