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 )
166 dup inlined-dependency depends-on
167 [ all-slots [ initial>> literalize ] map ]
168 [ tuple-layout '[ _ <tuple-boa> ] ]
172 \ new [ inline-new ] 1 define-partial-eval
176 [ "predicate" word-prop ] [ drop f ] if
177 ] 1 define-partial-eval
180 : nths-quot ( indices -- quot )
181 [ [ '[ _ swap nth ] ] map ] [ length ] bi
182 '[ _ cleave _ narray ] ;
185 shuffle-mapping nths-quot
186 ] 1 define-partial-eval
192 dup length iota zip >hashtable '[ _ at ]
195 ] 1 define-partial-eval
197 : member-eq-quot ( seq -- newquot )
198 [ [ dupd eq? ] curry [ drop t ] ] { } map>assoc
199 [ drop f ] suffix [ cond ] curry ;
202 dup sequence? [ member-eq-quot ] [ drop f ] if
203 ] 1 define-partial-eval
206 : member-quot ( seq -- newquot )
209 [ literalize [ t ] ] { } map>assoc linear-case-quot
211 unique [ key? ] curry
215 dup sequence? [ member-quot ] [ drop f ] if
216 ] 1 define-partial-eval
218 ! Fast at for integer maps
219 CONSTANT: lookup-table-at-max 256
221 : lookup-table-at? ( assoc -- ? )
222 #! Can we use a fast byte array test here?
226 [ keys [ integer? ] all? ]
227 [ keys [ 0 lookup-table-at-max between? ] all? ]
230 : lookup-table-seq ( assoc -- table )
231 [ keys supremum 1 + iota ] keep '[ _ at ] { } map-as ;
233 : lookup-table-quot ( seq -- newquot )
238 nth-unsafe dup >boolean
243 : fast-lookup-table-at? ( assoc -- ? )
245 [ [ integer? ] all? ]
246 [ [ 0 254 between? ] all? ]
249 : fast-lookup-table-seq ( assoc -- table )
250 lookup-table-seq [ 255 or ] B{ } map-as ;
252 : fast-lookup-table-quot ( seq -- newquot )
253 fast-lookup-table-seq
257 nth-unsafe dup 255 eq? [ drop f f ] [ t ] if
262 : at-quot ( assoc -- quot )
264 dup lookup-table-at? [
265 dup fast-lookup-table-at? [
266 fast-lookup-table-quot
273 \ at* [ at-quot ] 1 define-partial-eval
275 : diff-quot ( seq -- quot: ( seq' -- seq'' ) )
276 tester '[ [ @ not ] filter ] ;
278 \ diff [ diff-quot ] 1 define-partial-eval
280 : intersect-quot ( seq -- quot: ( seq' -- seq'' ) )
281 tester '[ _ filter ] ;
283 \ intersect [ intersect-quot ] 1 define-partial-eval
285 ! Speeds up sum-file, sort and reverse-complement benchmarks by
286 ! compiling decoder-readln better
288 in-d>> second value-info class>> growable class<=
289 [ \ push def>> ] [ f ] if
290 ] "custom-inlining" set-word-prop
292 ! We want to constant-fold calls to heap-size, and recompile those
293 ! calls when a C type is redefined
296 [ inlined-dependency depends-on ] [ heap-size '[ _ ] ] bi
298 ] 1 define-partial-eval