1 USING: arrays compiler.units generic hashtables inference kernel
2 kernel.private math optimizer generator prettyprint sequences
3 sbufs strings tools.test vectors words sequences.private
4 quotations optimizer.backend classes classes.algebra
5 inference.dataflow classes.tuple.private continuations growable
6 optimizer.inlining namespaces hints ;
9 [ H{ { 1 5 } { 3 4 } { 2 5 } } ] [
10 H{ { 1 2 } { 3 4 } } H{ { 2 5 } } union*
13 [ H{ { 1 4 } { 2 4 } { 3 4 } } ] [
14 H{ { 1 2 } { 3 4 } } H{ { 2 3 } } union*
17 GENERIC: xyz ( obj -- obj )
20 [ t ] [ \ xyz compiled>> ] unit-test
22 ! Test predicate inlining
25 dup integer? [ "integer" ] [ "nope" ] if
30 [ 1 "integer" ] [ 1 pred-test-1 ] unit-test
36 dup pred-test? [ "pred-test" ] [ "nope" ] if
41 [ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-2 ] unit-test
45 dup tuple? [ "pred-test" ] [ "nope" ] if
50 [ T{ pred-test } "pred-test" ] [ T{ pred-test } pred-test-3 ] unit-test
55 [ t ] [ "nom" inline-test ] unit-test
56 [ f ] [ "shayin" inline-test ] unit-test
57 [ f ] [ 3 inline-test ] unit-test
59 : fixnum-declarations >fixnum 24 shift 1234 bitxor ;
61 [ ] [ 1000000 fixnum-declarations . ] unit-test
65 : literal-not-branch 0 not [ ] [ ] if ;
67 [ ] [ literal-not-branch ] unit-test
71 : bad-kill-1 ( a b -- c d e ) [ 3 f ] [ dup bad-kill-1 ] if ; inline
72 : bad-kill-2 bad-kill-1 drop ;
74 [ 3 ] [ t bad-kill-2 ] unit-test
77 : (the-test) ( x -- y ) dup 0 > [ 1- (the-test) ] when ; inline
78 : the-test ( -- x y ) 2 dup (the-test) ;
80 [ 2 0 ] [ the-test ] unit-test
83 : (double-recursion) ( start end -- )
85 6 1 (double-recursion)
86 3 2 (double-recursion)
89 : double-recursion 0 2 (double-recursion) ;
91 [ ] [ double-recursion ] unit-test
94 : double-label-1 ( a b c -- d )
95 [ f double-label-1 ] [ swap nth-unsafe ] if ; inline
97 : double-label-2 ( a -- b )
98 dup array? [ ] [ ] if 0 t double-label-1 ;
100 [ 0 ] [ 10 double-label-2 ] unit-test
103 GENERIC: void-generic ( obj -- * )
104 : breakage ( -- * ) "hi" void-generic ;
105 [ t ] [ \ breakage compiled>> ] unit-test
106 [ breakage ] must-fail
109 : branch-fold-regression-0 ( m -- n )
110 t [ ] [ 1+ branch-fold-regression-0 ] if ; inline
112 : branch-fold-regression-1 ( -- m )
113 10 branch-fold-regression-0 ;
115 [ 10 ] [ branch-fold-regression-1 ] unit-test
118 : constant-branch-fold-0 "hey" ; foldable
119 : constant-branch-fold-1 ( -- ? ) constant-branch-fold-0 "hey" = ; inline
120 [ 1 ] [ [ constant-branch-fold-1 [ 1 ] [ 2 ] if ] compile-call ] unit-test
124 : bar ( -- ? ) foo 4 4 = and ;
125 [ f ] [ bar ] unit-test
127 ! ensure identities are working in some form
129 [ { number } declare 0 + ] dataflow optimize
130 [ #push? ] node-exists? not
133 ! compiling <tuple> with a non-literal class failed
134 : <tuple>-regression ( class -- tuple ) <tuple> ;
136 [ t ] [ \ <tuple>-regression compiled>> ] unit-test
138 GENERIC: foozul ( a -- b )
143 [ reversed ] [ reversed \ foozul specific-method ] unit-test
146 : constant-fold-2 f ; foldable
147 : constant-fold-3 4 ; foldable
150 [ constant-fold-2 constant-fold-3 4 = ] compile-call
153 : constant-fold-4 f ; foldable
154 : constant-fold-5 f ; foldable
157 [ constant-fold-4 constant-fold-5 or ] compile-call
160 [ 5 ] [ 5 [ 0 + ] compile-call ] unit-test
161 [ 5 ] [ 5 [ 0 swap + ] compile-call ] unit-test
163 [ 5 ] [ 5 [ 0 - ] compile-call ] unit-test
164 [ -5 ] [ 5 [ 0 swap - ] compile-call ] unit-test
165 [ 0 ] [ 5 [ dup - ] compile-call ] unit-test
167 [ 5 ] [ 5 [ 1 * ] compile-call ] unit-test
168 [ 5 ] [ 5 [ 1 swap * ] compile-call ] unit-test
169 [ 0 ] [ 5 [ 0 * ] compile-call ] unit-test
170 [ 0 ] [ 5 [ 0 swap * ] compile-call ] unit-test
171 [ -5 ] [ 5 [ -1 * ] compile-call ] unit-test
172 [ -5 ] [ 5 [ -1 swap * ] compile-call ] unit-test
174 [ 0 ] [ 5 [ 1 mod ] compile-call ] unit-test
175 [ 0 ] [ 5 [ 1 rem ] compile-call ] unit-test
177 [ 5 ] [ 5 [ -1 bitand ] compile-call ] unit-test
178 [ 0 ] [ 5 [ 0 bitand ] compile-call ] unit-test
179 [ 5 ] [ 5 [ -1 swap bitand ] compile-call ] unit-test
180 [ 0 ] [ 5 [ 0 swap bitand ] compile-call ] unit-test
181 [ 5 ] [ 5 [ dup bitand ] compile-call ] unit-test
183 [ 5 ] [ 5 [ 0 bitor ] compile-call ] unit-test
184 [ -1 ] [ 5 [ -1 bitor ] compile-call ] unit-test
185 [ 5 ] [ 5 [ 0 swap bitor ] compile-call ] unit-test
186 [ -1 ] [ 5 [ -1 swap bitor ] compile-call ] unit-test
187 [ 5 ] [ 5 [ dup bitor ] compile-call ] unit-test
189 [ 5 ] [ 5 [ 0 bitxor ] compile-call ] unit-test
190 [ 5 ] [ 5 [ 0 swap bitxor ] compile-call ] unit-test
191 [ -6 ] [ 5 [ -1 bitxor ] compile-call ] unit-test
192 [ -6 ] [ 5 [ -1 swap bitxor ] compile-call ] unit-test
193 [ 0 ] [ 5 [ dup bitxor ] compile-call ] unit-test
195 [ 0 ] [ 5 [ 0 swap shift ] compile-call ] unit-test
196 [ 5 ] [ 5 [ 0 shift ] compile-call ] unit-test
198 [ f ] [ 5 [ dup < ] compile-call ] unit-test
199 [ t ] [ 5 [ dup <= ] compile-call ] unit-test
200 [ f ] [ 5 [ dup > ] compile-call ] unit-test
201 [ t ] [ 5 [ dup >= ] compile-call ] unit-test
203 [ t ] [ 5 [ dup eq? ] compile-call ] unit-test
204 [ t ] [ 5 [ dup = ] compile-call ] unit-test
205 [ t ] [ 5 [ dup number= ] compile-call ] unit-test
206 [ t ] [ \ vector [ \ vector = ] compile-call ] unit-test
208 GENERIC: detect-number ( obj -- obj )
209 M: number detect-number ;
211 [ 10 f [ <array> 0 + detect-number ] compile-call ] must-fail
214 [ 4 [ + ] ] [ 2 2 [ [ + ] [ call ] keep ] compile-call ] unit-test
220 : old-binsearch ( elt quot seq -- elt quot i )
224 [ midpoint swap call ] 3keep roll dup zero?
225 [ drop dup slice-from swap midpoint@ + ]
226 [ partition old-binsearch ] if
230 10 20 >vector <flat-slice>
231 [ [ - ] swap old-binsearch ] compile-call 2nip
235 TUPLE: silly-tuple a b ;
237 [ 1 2 { silly-tuple-a silly-tuple-b } ] [
238 T{ silly-tuple f 1 2 }
240 { silly-tuple-a silly-tuple-b } [ get-slots ] keep
247 : node-successor-f-bug ( x -- * )
248 [ 3 throw ] [ empty-compound ] compose [ 3 throw ] if ;
250 [ t ] [ \ node-successor-f-bug compiled>> ] unit-test
252 [ ] [ [ new ] dataflow optimize drop ] unit-test
254 [ ] [ [ <tuple> ] dataflow optimize drop ] unit-test
256 ! Make sure we have sane heuristics
257 : should-inline? ( generic class -- ? ) method flat-length 10 <= ;
259 [ t ] [ \ fixnum \ shift should-inline? ] unit-test
260 [ f ] [ \ array \ equal? should-inline? ] unit-test
261 [ f ] [ \ sequence \ hashcode* should-inline? ] unit-test
262 [ t ] [ \ array \ nth-unsafe should-inline? ] unit-test
263 [ t ] [ \ growable \ nth-unsafe should-inline? ] unit-test
264 [ t ] [ \ sbuf \ set-nth-unsafe should-inline? ] unit-test
267 : lift-throw-tail-regression ( obj -- obj str )
268 dup integer? [ "an integer" ] [
269 dup string? [ "a string" ] [
274 [ t ] [ \ lift-throw-tail-regression compiled>> ] unit-test
275 [ 3 "an integer" ] [ 3 lift-throw-tail-regression ] unit-test
276 [ "hi" "a string" ] [ "hi" lift-throw-tail-regression ] unit-test
278 : lift-loop-tail-test-1 ( a quot -- )
280 [ >r 3 - r> call ] keep lift-loop-tail-test-1
285 [ >r 2 - r> call ] keep lift-loop-tail-test-1
289 : lift-loop-tail-test-2
290 10 [ ] lift-loop-tail-test-1 1 2 3 ;
292 [ 1 2 3 ] [ lift-loop-tail-test-2 ] unit-test
294 GENERIC: generic-inline-test ( x -- y )
295 M: integer generic-inline-test ;
297 : generic-inline-test-1 ( -- x )
308 generic-inline-test ;
310 ! Inlining all of the above should only take two passes
312 \ generic-inline-test-1 def>> dataflow
313 [ optimize-1 , optimize-1 , drop ] { } make
316 ! Forgot a recursive inline check
317 : recursive-inline-hang ( a -- a )
318 dup array? [ recursive-inline-hang ] when ;
320 HINTS: recursive-inline-hang array ;
322 : recursive-inline-hang-1 ( -- a )
323 { } recursive-inline-hang ;
325 [ t ] [ \ recursive-inline-hang-1 compiled>> ] unit-test
327 DEFER: recursive-inline-hang-3
329 : recursive-inline-hang-2 ( a -- a )
330 dup array? [ recursive-inline-hang-3 ] when ;
332 HINTS: recursive-inline-hang-2 array ;
334 : recursive-inline-hang-3 ( a -- a )
335 dup array? [ recursive-inline-hang-2 ] when ;
337 HINTS: recursive-inline-hang-3 array ;
340 USE: sequences.private
342 [ ] [ { (3append) } compile ] unit-test
345 : counter-example ( a b c d -- a' b' c' d' )
346 dup 0 > [ 1 - >r rot 2 * r> counter-example ] when ; inline
348 : counter-example' ( -- a' b' c' d' )
349 1 2 3.0 3 counter-example ;
351 [ 2 4 6.0 0 ] [ counter-example' ] unit-test
353 : member-test ( obj -- ? ) { + - * / /i } member? ;
355 \ member-test must-infer
356 [ ] [ \ member-test word-dataflow optimize 2drop ] unit-test
357 [ t ] [ \ + member-test ] unit-test
358 [ f ] [ \ append member-test ] unit-test