1 ! Copyright (C) 2008, 2010 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types kernel sequences words fry generic accessors
4 classes.tuple classes classes.algebra definitions
5 stack-checker.dependencies quotations classes.tuple.private math
6 math.partial-dispatch math.private math.intervals sets.private
7 math.floats.private math.integers.private layouts math.order
8 vectors hashtables combinators effects generalizations assocs
9 sets combinators.short-circuit sequences.private locals growable
10 stack-checker namespaces compiler.tree.propagation.info ;
12 IN: compiler.tree.propagation.transforms
15 ! If first input has a known type and second input is an
16 ! object, we convert this to [ swap equal? ].
17 in-d>> first2 value-info class>> object class= [
18 value-info class>> \ equal? method-for-class
21 ] "custom-inlining" set-word-prop
23 : rem-custom-inlining ( #call -- quot/f )
24 second value-info literal>> dup integer?
25 [ power-of-2? [ 1 - bitand ] f ? ] [ drop f ] if ;
34 in-d>> dup first value-info interval>> [0,inf] interval-subset?
35 [ rem-custom-inlining ] [ drop f ] if
36 ] "custom-inlining" set-word-prop
40 in-d>> rem-custom-inlining
41 ] "custom-inlining" set-word-prop
43 : positive-fixnum? ( obj -- ? )
44 { [ fixnum? ] [ 0 >= ] } 1&& ;
46 : simplify-bitand? ( value1 value2 -- ? )
47 [ literal>> positive-fixnum? ]
48 [ class>> fixnum swap class<= ]
51 : all-ones? ( n -- ? ) dup 1 + bitand zero? ; inline
53 : redundant-bitand? ( value1 value2 -- ? )
54 [ interval>> ] [ literal>> ] bi* {
57 [ 0 swap [a,b] interval-subset? ]
60 : zero-bitand? ( value1 value2 -- ? )
61 [ interval>> ] [ literal>> ] bi* {
63 [ nip bitnot all-ones? ]
64 [ 0 swap bitnot [a,b] interval-subset? ]
68 bitand-integer-integer
74 in-d>> first2 [ value-info ] bi@ {
80 [ 2dup swap zero-bitand? ]
84 [ 2dup redundant-bitand? ]
88 [ 2dup swap redundant-bitand? ]
92 [ 2dup simplify-bitand? ]
93 [ 2drop [ >fixnum fixnum-bitand ] ]
96 [ 2dup swap simplify-bitand? ]
97 [ 2drop [ [ >fixnum ] dip fixnum-bitand ] ]
101 ] "custom-inlining" set-word-prop
106 in-d>> first2 [ value-info ] bi@
107 [ { [ literal>> 1 = ] [ class>> fixnum class<= ] } 1&& ]
108 [ class>> fixnum class<= ]
113 cell-bits tag-bits get - 1 -
115 >fixnum dup 0 < [ 2drop 0 ] [
116 dup _ < [ fixnum-shift ] [
122 ] "custom-inlining" set-word-prop
124 { /i fixnum/i fixnum/i-fast bignum/i } [
126 in-d>> first2 [ value-info ] bi@ {
127 [ drop class>> integer class<= ]
128 [ drop interval>> 0 [a,a] interval>= ]
129 [ nip literal>> integer? ]
130 [ nip literal>> power-of-2? ]
131 } 2&& [ [ log2 neg shift ] ] [ f ] if
132 ] "custom-inlining" set-word-prop
135 ! Generate more efficient code for common idiom
137 in-d>> first value-info literal>> {
138 { V{ } [ [ drop { } 0 vector boa ] ] }
139 { H{ } [ [ drop 0 <hashtable> ] ] }
142 ] "custom-inlining" set-word-prop
144 ERROR: bad-partial-eval quot word ;
146 : check-effect ( quot word -- )
147 2dup [ infer ] [ stack-effect ] bi* effect<=
148 [ 2drop ] [ bad-partial-eval ] if ;
150 :: define-partial-eval ( word quot n -- )
154 dup [ literal?>> ] all? [
159 dup word check-effect
162 ] "custom-inlining" set-word-prop ;
164 : inline-new ( class -- quot/f )
167 [ depends-on-tuple-layout ]
168 [ drop all-slots [ initial>> literalize ] [ ] map-as ]
174 \ new [ inline-new ] 1 define-partial-eval
178 [ "predicate" word-prop ] [ drop f ] if
179 ] 1 define-partial-eval
182 : nths-quot ( indices -- quot )
183 [ [ '[ _ swap nth ] ] map ] [ length ] bi
184 '[ _ cleave _ narray ] ;
187 shuffle-mapping nths-quot
188 ] 1 define-partial-eval
194 dup length iota zip >hashtable '[ _ at ]
197 ] 1 define-partial-eval
199 : member-eq-quot ( seq -- newquot )
200 [ [ dupd eq? ] curry [ drop t ] ] { } map>assoc
201 [ drop f ] suffix [ cond ] curry ;
204 dup sequence? [ member-eq-quot ] [ drop f ] if
205 ] 1 define-partial-eval
208 : member-quot ( seq -- newquot )
211 [ literalize [ t ] ] { } map>assoc linear-case-quot
213 unique [ key? ] curry
217 dup sequence? [ member-quot ] [ drop f ] if
218 ] 1 define-partial-eval
220 ! Fast at for integer maps
221 CONSTANT: lookup-table-at-max 256
223 : lookup-table-at? ( assoc -- ? )
224 #! Can we use a fast byte array test here?
228 [ keys [ integer? ] all? ]
229 [ keys [ 0 lookup-table-at-max between? ] all? ]
232 : lookup-table-seq ( assoc -- table )
233 [ keys supremum 1 + iota ] keep '[ _ at ] { } map-as ;
235 : lookup-table-quot ( seq -- newquot )
240 nth-unsafe dup >boolean
245 : fast-lookup-table-at? ( assoc -- ? )
247 [ [ integer? ] all? ]
248 [ [ 0 254 between? ] all? ]
251 : fast-lookup-table-seq ( assoc -- table )
252 lookup-table-seq [ 255 or ] B{ } map-as ;
254 : fast-lookup-table-quot ( seq -- newquot )
255 fast-lookup-table-seq
259 nth-unsafe dup 255 eq? [ drop f f ] [ t ] if
264 : at-quot ( assoc -- quot )
266 dup lookup-table-at? [
267 dup fast-lookup-table-at? [
268 fast-lookup-table-quot
275 \ at* [ at-quot ] 1 define-partial-eval
277 : diff-quot ( seq -- quot: ( seq' -- seq'' ) )
278 tester '[ [ @ not ] filter ] ;
280 \ diff [ diff-quot ] 1 define-partial-eval
282 : intersect-quot ( seq -- quot: ( seq' -- seq'' ) )
283 tester '[ _ filter ] ;
285 \ intersect [ intersect-quot ] 1 define-partial-eval
287 : fixnum-bits ( -- n )
288 cell-bits tag-bits get - ;
290 : bit-quot ( #call -- quot/f )
291 in-d>> second value-info interval>> 0 fixnum-bits [a,b] interval-subset?
292 [ [ >fixnum ] dip fixnum-bit? ] f ? ;
294 \ bit? [ bit-quot ] "custom-inlining" set-word-prop
296 ! Speeds up sum-file, sort and reverse-complement benchmarks by
297 ! compiling decoder-readln better
299 in-d>> second value-info class>> growable class<=
300 [ \ push def>> ] [ f ] if
301 ] "custom-inlining" set-word-prop
303 ! We want to constant-fold calls to heap-size, and recompile those
304 ! calls when a C type is redefined
307 [ depends-on-definition ] [ heap-size '[ _ ] ] bi
309 ] 1 define-partial-eval