1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs classes classes.algebra classes.tuple
4 classes.tuple.private classes.singleton kernel accessors math
5 math.intervals namespaces sequences sequences.private words
6 combinators memoize combinators.short-circuit byte-arrays
7 strings arrays layouts cpu.architecture
8 compiler.tree.propagation.copy ;
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 ] bi@ = ] [ 2drop f ] if ;
23 M: complex eql? over complex? [ = ] [ 2drop f ] if ;
25 ! Value info represents a set of objects. Don't mutate value infos
26 ! you receive, always construct new ones. We don't declare the
27 ! slots read-only to allow cloning followed by writing, and to
28 ! simplify constructors.
36 CONSTANT: null-info T{ value-info f null empty-interval }
38 CONSTANT: object-info T{ value-info f object full-interval }
40 : interval>literal ( class interval -- literal literal? )
41 #! If interval has zero length and the class is sufficiently
42 #! precise, we can turn it into a literal
43 dup special-interval? [
47 { [ over interval-length 0 > ] [ 3drop f f ] }
48 { [ pick bignum class<= ] [ 2nip >bignum t ] }
49 { [ pick integer class<= ] [ 2nip >fixnum t ] }
50 { [ pick float class<= ] [ 2nip [ f f ] [ >float t ] if-zero ] }
55 : <value-info> ( -- info ) \ value-info new ;
59 : tuple-slot-infos ( tuple -- slots )
60 [ tuple-slots ] [ class-of all-slots ] bi
61 [ read-only>> [ <literal-info> ] [ drop f ] if ] 2map
64 UNION: fixed-length array byte-array string ;
66 : literal-class ( obj -- class )
67 #! Handle forgotten tuples and singleton classes properly
68 dup singleton-class? [
74 : (slots-with-length) ( length class -- slots )
75 "slots" word-prop length 1 - f <array> swap prefix ;
77 : slots-with-length ( seq -- slots )
78 [ length <literal-info> ] [ class-of ] bi (slots-with-length) ;
80 : init-literal-info ( info -- info )
81 empty-interval >>interval
82 dup literal>> literal-class >>class
84 { [ dup real? ] [ [a,a] >>interval ] }
85 { [ dup tuple? ] [ tuple-slot-infos >>slots ] }
86 { [ dup fixed-length? ] [ slots-with-length >>slots ] }
90 : empty-set? ( info -- ? )
92 [ class>> null-class? ]
93 [ [ interval>> empty-interval eq? ] [ class>> real class<= ] bi and ]
96 : min-value ( class -- n )
98 { fixnum [ most-negative-fixnum ] }
99 { array-capacity [ 0 ] }
103 : max-value ( class -- n )
105 { fixnum [ most-positive-fixnum ] }
106 { array-capacity [ max-array-capacity ] }
110 : class-interval ( class -- i )
112 { fixnum [ fixnum-interval ] }
113 { array-capacity [ array-capacity-interval ] }
114 [ drop full-interval ]
117 : wrap-interval ( interval class -- interval' )
119 { [ over empty-interval eq? ] [ drop ] }
120 { [ over full-interval eq? ] [ nip class-interval ] }
121 { [ 2dup class-interval interval-subset? not ] [ nip class-interval ] }
125 : init-interval ( info -- info )
126 dup [ interval>> full-interval or ] [ class>> ] bi wrap-interval >>interval
127 dup class>> integer class<= [ [ integral-closure ] change-interval ] when ; inline
129 : init-value-info ( info -- info )
135 empty-interval >>interval
138 dup [ class>> ] [ interval>> ] bi interval>literal
139 [ >>literal ] [ >>literal? ] bi*
143 : <class/interval-info> ( class interval -- info )
147 init-value-info ; foldable
149 : <class-info> ( class -- info )
150 f <class/interval-info> ; foldable
152 : <interval-info> ( interval -- info )
156 init-value-info ; foldable
158 : <literal-info> ( literal -- info )
162 init-value-info ; foldable
164 : <sequence-info> ( length class -- info )
167 [ (slots-with-length) ] dip swap >>slots
170 : <tuple-info> ( slots class -- info )
176 : >literal< ( info -- literal literal? )
177 [ literal>> ] [ literal?>> ] bi ;
179 : intersect-literals ( info1 info2 -- literal literal? )
181 { [ dup literal?>> not ] [ drop >literal< ] }
182 { [ over literal?>> not ] [ nip >literal< ] }
183 { [ 2dup [ literal>> ] bi@ eql? not ] [ 2drop f f ] }
187 DEFER: value-info-intersect
189 DEFER: (value-info-intersect)
191 : intersect-slot ( info1 info2 -- info )
193 { [ dup not ] [ nip ] }
194 { [ over not ] [ drop ] }
195 [ (value-info-intersect) ]
198 : intersect-slots ( info1 info2 -- slots )
200 { [ dup not ] [ drop ] }
201 { [ over not ] [ nip ] }
203 2dup [ length ] bi@ =
204 [ [ intersect-slot ] 2map ] [ 2drop f ] if
208 : (value-info-intersect) ( info1 info2 -- info )
209 [ <value-info> ] 2dip
211 [ [ class>> ] bi@ class-and >>class ]
212 [ [ interval>> ] bi@ interval-intersect >>interval ]
213 [ intersect-literals [ >>literal ] [ >>literal? ] bi* ]
214 [ intersect-slots >>slots ]
218 : value-info-intersect ( info1 info2 -- info )
220 { [ dup class>> null-class? ] [ nip ] }
221 { [ over class>> null-class? ] [ drop ] }
222 [ (value-info-intersect) ]
225 : union-literals ( info1 info2 -- literal literal? )
226 2dup [ literal?>> ] both? [
227 [ literal>> ] bi@ 2dup eql? [ drop t ] [ 2drop f f ] if
230 DEFER: value-info-union
232 DEFER: (value-info-union)
234 : union-slot ( info1 info2 -- info )
236 { [ dup not ] [ nip ] }
237 { [ over not ] [ drop ] }
238 [ (value-info-union) ]
241 : union-slots ( info1 info2 -- slots )
243 2dup [ length ] bi@ =
244 [ [ union-slot ] 2map ] [ 2drop f ] if ;
246 : (value-info-union) ( info1 info2 -- info )
247 [ <value-info> ] 2dip
249 [ [ class>> ] bi@ class-or >>class ]
250 [ [ interval>> ] bi@ interval-union >>interval ]
251 [ union-literals [ >>literal ] [ >>literal? ] bi* ]
252 [ union-slots >>slots ]
256 : value-info-union ( info1 info2 -- info )
258 { [ dup class>> null-class? ] [ drop ] }
259 { [ over class>> null-class? ] [ nip ] }
260 [ (value-info-union) ]
263 : value-infos-union ( infos -- info )
265 [ [ ] [ value-info-union ] map-reduce ] if-empty ;
267 : literals<= ( info1 info2 -- ? )
269 { [ dup literal?>> not ] [ 2drop t ] }
270 { [ over literal?>> not ] [ drop class>> null-class? ] }
271 [ [ literal>> ] bi@ eql? ]
274 : value-info<= ( info1 info2 -- ? )
276 { [ dup not ] [ 2drop t ] }
277 { [ over not ] [ 2drop f ] }
280 { [ 2dup [ class>> ] bi@ class<= not ] [ f ] }
281 { [ 2dup [ interval>> ] bi@ interval-subset? not ] [ f ] }
282 { [ 2dup literals<= not ] [ f ] }
283 { [ 2dup [ slots>> ] bi@ [ value-info<= ] 2all? not ] [ f ] }
289 ! Assoc stack of current value --> info mapping
292 : value-info* ( value -- info ? )
293 resolve-copy value-infos get assoc-stack [ null-info or ] [ >boolean ] bi ; inline
295 : value-info ( value -- info )
298 : set-value-info ( info value -- )
299 resolve-copy value-infos get last set-at ;
301 : refine-value-info ( info value -- )
302 resolve-copy value-infos get
303 [ assoc-stack [ value-info-intersect ] when* ] 2keep
306 : value-literal ( value -- obj ? )
307 value-info >literal< ;
309 : possible-boolean-values ( info -- values )
311 { [ dup null-class? ] [ { } ] }
312 { [ dup true-class? ] [ { t } ] }
313 { [ dup false-class? ] [ { f } ] }
317 : node-value-info ( node value -- info )
318 swap info>> at* [ drop null-info ] unless ;
320 : node-input-infos ( node -- seq )
321 dup in-d>> [ node-value-info ] with map ;
323 : node-output-infos ( node -- seq )
324 dup out-d>> [ node-value-info ] with map ;
326 : first-literal ( #call -- obj )
327 dup in-d>> first node-value-info literal>> ;
329 : last-literal ( #call -- obj )
330 dup out-d>> last node-value-info literal>> ;
332 : immutable-tuple-boa? ( #call -- ? )
333 dup word>> \ <tuple-boa> eq? [
334 dup in-d>> last node-value-info
335 literal>> first immutable-tuple-class?