1 ! Copyright (C) 2008, 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences words fry generic accessors classes.tuple
4 classes classes.algebra definitions stack-checker.state quotations
5 classes.tuple.private math math.partial-dispatch math.private
6 math.intervals layouts math.order vectors hashtables
7 combinators effects generalizations assocs sets
8 combinators.short-circuit sequences.private locals
9 stack-checker namespaces compiler.tree.propagation.info ;
10 IN: compiler.tree.propagation.transforms
13 ! If first input has a known type and second input is an
14 ! object, we convert this to [ swap equal? ].
15 in-d>> first2 value-info class>> object class= [
16 value-info class>> \ equal? specific-method
19 ] "custom-inlining" set-word-prop
21 : rem-custom-inlining ( #call -- quot/f )
22 second value-info literal>> dup integer?
23 [ power-of-2? [ 1 - bitand ] f ? ] [ drop f ] if ;
32 in-d>> dup first value-info interval>> [0,inf] interval-subset?
33 [ rem-custom-inlining ] [ drop f ] if
34 ] "custom-inlining" set-word-prop
38 in-d>> rem-custom-inlining
39 ] "custom-inlining" set-word-prop
42 bitand-integer-integer
48 in-d>> second value-info >literal< [
49 0 most-positive-fixnum between?
50 [ [ >fixnum ] bi@ fixnum-bitand ] f ?
52 ] "custom-inlining" set-word-prop
57 in-d>> first value-info literal>> 1 = [
58 cell-bits tag-bits get - 1 -
60 >fixnum dup 0 < [ 2drop 0 ] [
61 dup _ < [ fixnum-shift ] [
67 ] "custom-inlining" set-word-prop
69 ! Generate more efficient code for common idiom
71 in-d>> first value-info literal>> {
72 { V{ } [ [ drop { } 0 vector boa ] ] }
73 { H{ } [ [ drop 0 <hashtable> ] ] }
76 ] "custom-inlining" set-word-prop
78 ERROR: bad-partial-eval quot word ;
80 : check-effect ( quot word -- )
81 2dup [ infer ] [ stack-effect ] bi* effect<=
82 [ 2drop ] [ bad-partial-eval ] if ;
84 :: define-partial-eval ( word quot n -- )
88 dup [ literal?>> ] all? [
96 ] "custom-inlining" set-word-prop ;
98 : inline-new ( class -- quot/f )
100 dup inlined-dependency depends-on
101 [ all-slots [ initial>> literalize ] map ]
102 [ tuple-layout '[ _ <tuple-boa> ] ]
106 \ new [ inline-new ] 1 define-partial-eval
110 [ "predicate" word-prop ] [ drop f ] if
111 ] 1 define-partial-eval
114 : nths-quot ( indices -- quot )
115 [ [ '[ _ swap nth ] ] map ] [ length ] bi
116 '[ _ cleave _ narray ] ;
119 shuffle-mapping nths-quot
120 ] 1 define-partial-eval
126 dup length zip >hashtable '[ _ at ]
129 ] 1 define-partial-eval
131 : memq-quot ( seq -- newquot )
132 [ [ dupd eq? ] curry [ drop t ] ] { } map>assoc
133 [ drop f ] suffix [ cond ] curry ;
136 dup sequence? [ memq-quot ] [ drop f ] if
137 ] 1 define-partial-eval
140 : member-quot ( seq -- newquot )
143 [ literalize [ t ] ] { } map>assoc linear-case-quot
145 unique [ key? ] curry
149 dup sequence? [ member-quot ] [ drop f ] if
150 ] 1 define-partial-eval
152 ! Fast at for integer maps
153 CONSTANT: lookup-table-at-max 256
155 : lookup-table-at? ( assoc -- ? )
156 #! Can we use a fast byte array test here?
160 [ keys [ integer? ] all? ]
161 [ keys [ 0 lookup-table-at-max between? ] all? ]
164 : lookup-table-seq ( assoc -- table )
165 [ keys supremum 1 + ] keep '[ _ at ] { } map-as ;
167 : lookup-table-quot ( seq -- newquot )
172 nth-unsafe dup >boolean
177 : fast-lookup-table-at? ( assoc -- ? )
179 [ [ integer? ] all? ]
180 [ [ 0 254 between? ] all? ]
183 : fast-lookup-table-seq ( assoc -- table )
184 lookup-table-seq [ 255 or ] B{ } map-as ;
186 : fast-lookup-table-quot ( seq -- newquot )
187 fast-lookup-table-seq
191 nth-unsafe dup 255 eq? [ drop f f ] [ t ] if
196 : at-quot ( assoc -- quot )
197 dup lookup-table-at? [
198 dup fast-lookup-table-at? [
199 fast-lookup-table-quot
205 \ at* [ at-quot ] 1 define-partial-eval