1 USING: generalizations accessors arrays compiler kernel kernel.private
2 math hashtables.private math.private namespaces sequences tools.test
3 namespaces.private slots.private sequences.private byte-arrays alien
4 alien.accessors layouts words definitions compiler.units io
5 combinators vectors grouping make alien.c-types combinators.short-circuit
6 math.order math.libm math.parser ;
7 QUALIFIED: namespaces.private
8 IN: compiler.tests.codegen
10 ! Originally, this file did black box testing of templating
11 ! optimization. We now have a different codegen, but the tests
12 ! in here are still useful.
15 [ 5000 ] [ [ 5000 ] compile-call ] unit-test
16 [ "hi" ] [ [ "hi" ] compile-call ] unit-test
18 [ 1 2 3 4 ] [ [ 1 2 3 4 ] compile-call ] unit-test
20 [ 1 1 ] [ 1 [ dup ] compile-call ] unit-test
21 [ 0 ] [ 3 [ tag ] compile-call ] unit-test
22 [ 0 3 ] [ 3 [ [ tag ] keep ] compile-call ] unit-test
24 [ 2 3 ] [ 3 [ 2 swap ] compile-call ] unit-test
26 [ 2 1 3 4 ] [ 1 2 [ swap 3 4 ] compile-call ] unit-test
28 [ 2 3 4 ] [ 3 [ 2 swap 4 ] compile-call ] unit-test
30 [ { 1 2 3 } { 1 4 3 } 2 2 ]
31 [ { 1 2 3 } { 1 4 3 } [ over tag over tag ] compile-call ]
34 ! Test literals in either side of a shuffle
35 [ 4 1 ] [ 1 [ [ 3 fixnum+ ] keep ] compile-call ] unit-test
37 [ 2 ] [ 1 2 [ swap fixnum/i ] compile-call ] unit-test
42 [ 1.2 [ tag [ foo ] keep ] compile-call ]
46 [ { 1 2 } [ dup 2 slot swap 3 slot [ foo ] keep ] compile-call ]
51 global [ 3 \ foo set ] bind
52 \ foo [ global >n get namespaces.private:ndrop ] compile-call
55 : blech ( x -- ) drop ;
59 global [ 3 \ foo set ] bind
60 \ foo [ global [ get ] swap blech call ] compile-call
65 global [ 3 \ foo set ] bind
66 \ foo [ global [ get ] swap >n call namespaces.private:ndrop ] compile-call
71 global [ 3 \ foo set ] bind
72 \ foo [ global [ get ] bind ] compile-call
76 -12 -13 [ [ 0 swap fixnum-fast ] bi@ ] compile-call
79 [ -1 2 ] [ 1 2 [ [ 0 swap fixnum- ] dip ] compile-call ] unit-test
82 -12 -13 [ [ 0 swap fixnum- ] bi@ ] compile-call
86 SBUF" " [ 1 slot 1 [ slot ] keep ] compile-call nip
90 [ 3 1 2 3 4 5 6 7 8 9 ] [
92 [ [ [ [ [ [ [ [ [ [ 3 ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ]
96 [ 2 2 2 2 2 2 2 2 2 2 1 ] [
98 [ swap [ dup dup dup dup dup dup dup dup dup ] dip ] compile-call
101 [ ] [ [ 9 [ ] times ] compile-call ] unit-test
105 [ 200 dup [ 200 3array ] curry map drop ] times
106 ] [ (( n -- )) define-temp ] with-compilation-unit drop
109 ! Test how dispatch handles the end of a basic block
110 : try-breaking-dispatch ( n a b -- x str )
111 float+ swap { [ "hey" ] [ "bye" ] } dispatch ;
113 : try-breaking-dispatch-2 ( -- ? )
114 1 1.0 2.5 try-breaking-dispatch "bye" = [ 3.5 = ] dip and ;
117 10000000 [ drop try-breaking-dispatch-2 ] all?
121 : (broken) ( x -- y ) ;
123 [ 2.0 { 2.0 0.0 } ] [
125 [ float/f 0.0 [ drop (broken) ] 2keep 2array ] compile-call
129 : hellish-bug-1 ( a b -- ) 2drop ;
131 : hellish-bug-2 ( i array x -- x )
132 2dup 1 slot eq? [ 2drop ] [
133 2dup array-nth tombstone? [
135 [ array-nth ] 2keep [ 1 fixnum+fast ] dip array-nth
136 pick 2dup hellish-bug-1 3drop
138 ] unless [ 2 fixnum+fast ] dip hellish-bug-2
139 ] if ; inline recursive
141 : hellish-bug-3 ( hash array -- )
142 0 swap hellish-bug-2 drop ;
145 H{ { 1 2 } { 3 4 } } dup array>>
146 [ 0 swap hellish-bug-2 drop ] compile-call
150 : foox ( obj -- obj )
152 [ drop 3 ] [ dup tuple? [ drop 4 ] [ drop 5 ] if ] if ;
154 [ 3 ] [ f foox ] unit-test
158 [ 4 ] [ T{ my-tuple } foox ] unit-test
160 [ 5 ] [ "hi" foox ] unit-test
162 ! Making sure we don't needlessly unbox/rebox
163 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ [ eq? ] dip ] compile-call ] unit-test
165 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ ] compile-call [ eq? ] dip ] unit-test
167 [ t ] [ 1.0 dup [ [ 2.0 float+ ] keep ] compile-call nip eq? ] unit-test
171 { byte-array } declare
172 [ 0 alien-unsigned-1 ] keep
179 [ 0 alien-unsigned-1 ] keep hi-tag
180 ] compile-call byte-array type-number =
187 ] compile-call alien type-number =
192 [ 2dup fixnum< [ [ die ] dip ] when ] compile-call
196 : a-dummy ( a -- ) drop "hi" print ;
200 dup 0 2 3dup pick >= [ >= ] [ 2drop f ] if [
206 ] [ 2drop no-case ] if
211 : dispatch-alignment-regression ( -- c )
212 { tuple vector } 3 slot { word } declare
213 dup 1 slot 0 fixnum-bitand { [ ] } dispatch ;
215 [ t ] [ \ dispatch-alignment-regression optimized? ] unit-test
217 [ vector ] [ dispatch-alignment-regression ] unit-test
220 : bad-value-bug ( a -- b ) [ 3 ] [ 3 ] if f <array> ;
222 [ { f f f } ] [ t bad-value-bug ] unit-test
227 : (gc-check-bug) ( a b -- c )
228 { [ id boa ] [ id boa ] } dispatch ;
230 : gc-check-bug ( -- )
231 10000000 [ "hi" 0 (gc-check-bug) drop ] times ;
233 [ ] [ gc-check-bug ] unit-test
236 : test-1 ( a -- b ) 8 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
238 [ "a" ] [ 8 test-1 ] unit-test
239 [ "b" ] [ 9 test-1 ] unit-test
241 : test-2 ( a -- b ) 1 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
243 [ "a" ] [ 1 test-2 ] unit-test
244 [ "b" ] [ 2 test-2 ] unit-test
246 ! I accidentally fixnum/i-fast on PowerPC
247 [ { { 1 2 } { 3 4 } } ] [
250 [ { array } declare 2 <groups> [ , ] each ] compile-call
256 [ { array } declare 2 <groups> length ] compile-call
259 ! Oops with new intrinsics
260 : fixnum-overflow-control-flow-test ( a b -- c )
261 [ 1 fixnum- ] [ 2 fixnum- ] if 3 fixnum+fast ;
263 [ 3 ] [ 1 t fixnum-overflow-control-flow-test ] unit-test
264 [ 2 ] [ 1 f fixnum-overflow-control-flow-test ] unit-test
268 { float } declare dup 0 =
271 [ 2 "double" "libm" "pow" { "double" "double" } alien-invoke ]
272 [ -0.5 "double" "libm" "pow" { "double" "double" } alien-invoke ]
276 [ 4.0 ] [ 2.0 blah ] unit-test
278 [ 4 ] [ 2 [ dup fixnum* ] compile-call ] unit-test
279 [ 7 ] [ 2 [ dup fixnum* 3 fixnum+fast ] compile-call ] unit-test
283 M: cucumber equal? "The cucumber has no equal" throw ;
285 [ t ] [ [ cucumber ] compile-call cucumber eq? ] unit-test
287 [ 4294967295 B{ 255 255 255 255 } -1 ]
290 [ [ 0 alien-unsigned-4 swap ] [ 0 alien-signed-2 ] bi ]
294 ! Regression found while working on global register allocation
296 : linear-scan-regression-1 ( a b c -- ) 3array , ;
297 : linear-scan-regression-2 ( a b -- ) 2array , ;
299 : linear-scan-regression ( a b c -- )
300 [ linear-scan-regression-2 ]
301 [ linear-scan-regression-1 ]
302 bi-curry bi-curry interleave ;
313 [ { 1 2 3 } "x" "y" linear-scan-regression ] { } make
316 ! Regression from Doug's value numbering changes
317 [ t ] [ 2 [ 1 swap fixnum< ] compile-call ] unit-test
318 [ 3 ] [ 2 [ 1 swap fixnum< [ 3 ] [ 4 ] if ] compile-call ] unit-test
321 [ 0 ] [ 101 [ dup fixnum-fast 1 fixnum+fast 20 fixnum-shift-fast 20 fixnum-shift-fast ] compile-call ] unit-test
324 ! Regression from Slava's value numbering changes
325 [ 1 ] [ 31337 [ dup fixnum<= [ 1 ] [ 2 ] if ] compile-call ] unit-test
327 ! Bug with ##return node construction
328 : return-recursive-bug ( nodes -- ? )
330 dup 3 bitand 1 = [ drop t ] [
335 ] any? ; inline recursive
337 [ t ] [ 3 [ return-recursive-bug ] compile-call ] unit-test
339 ! Coalescing reductions
340 [ f ] [ V{ } 0 [ [ vector? ] both? ] compile-call ] unit-test
341 [ f ] [ 0 V{ } [ [ vector? ] both? ] compile-call ] unit-test
345 [ dup [ \ vector eq? ] [ drop f ] if ] dip
346 dup [ \ vector eq? ] [ drop f ] if
347 over rot [ drop ] [ nip ] if
351 ! Coalesing bug reduced from sequence-parser:take-sequence
352 : coalescing-bug-1 ( a b c d -- a b c d )
355 [ [ drop ] 2dip length > ]
357 } 3|| [ 3drop f ] [ slice boa ] if swap [ [ length ] bi@ ] 2keep ;
359 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } -10 3 "hello" coalescing-bug-1 ] unit-test
360 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 0 7 "hello" coalescing-bug-1 ] unit-test
361 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 3 2 "hello" coalescing-bug-1 ] unit-test
362 [ 2 3 T{ slice f 1 3 "hello" } { 1 2 3 } ] [ { 1 2 3 } 1 3 "hello" coalescing-bug-1 ] unit-test
364 ! Another one, found by Dan
365 : coalescing-bug-2 ( a -- b )
366 dup dup 10 fixnum< [ 1 fixnum+fast ] when
367 fixnum+fast 2 fixnum*fast 2 fixnum-fast 2 fixnum*fast 2 fixnum+fast ;
369 [ 10 ] [ 1 coalescing-bug-2 ] unit-test
370 [ 86 ] [ 11 coalescing-bug-2 ] unit-test
372 ! Regression in suffix-arrays code
373 : coalescing-bug-3 ( from/f to/f seq -- slice )
375 [ drop 0 or ] [ length or ] bi-curry bi*
379 [ T{ slice f 0 5 "hello" } ] [ f f "hello" coalescing-bug-3 ] unit-test
380 [ T{ slice f 1 5 "hello" } ] [ 1 f "hello" coalescing-bug-3 ] unit-test
381 [ T{ slice f 0 3 "hello" } ] [ f 3 "hello" coalescing-bug-3 ] unit-test
382 [ T{ slice f 1 3 "hello" } ] [ 1 3 "hello" coalescing-bug-3 ] unit-test
383 [ T{ slice f 3 3 "hello" } ] [ 4 3 "hello" coalescing-bug-3 ] unit-test
384 [ T{ slice f 5 5 "hello" } ] [ 6 f "hello" coalescing-bug-3 ] unit-test
387 : coalescing-bug-4 ( a b c -- a b c )
388 [ [ min ] keep ] dip vector? [ 1 ] [ 2 ] if ;
390 [ 2 3 2 ] [ 2 3 "" coalescing-bug-4 ] unit-test
391 [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
392 [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
393 [ 2 3 1 ] [ 2 3 V{ } coalescing-bug-4 ] unit-test
394 [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
395 [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
397 ! Global stack analysis dataflow equations are wrong
398 : some-word ( a -- b ) 2 + ;
399 : global-dcn-bug-1 ( a b -- c d )
400 dup [ [ drop 1 ] dip ] [ [ some-word ] dip ] if
401 dup [ [ 1 fixnum+fast ] dip ] [ [ drop 1 ] dip ] if ;
403 [ 2 t ] [ 0 t global-dcn-bug-1 ] unit-test
404 [ 1 f ] [ 0 f global-dcn-bug-1 ] unit-test
407 : missing-gc-check-1 ( a -- b ) { fixnum } declare <alien> ;
408 : missing-gc-check-2 ( -- ) 10000000 [ missing-gc-check-1 drop ] each-integer ;
410 [ ] [ missing-gc-check-2 ] unit-test
412 [ 1 "0.169967142900241" ] [ 1.4 [ 1 swap fcos ] compile-call number>string ] unit-test
413 [ 1 "0.169967142900241" ] [ 1.4 1 [ swap fcos ] compile-call number>string ] unit-test
414 [ "0.169967142900241" "0.9854497299884601" ] [ 1.4 [ [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
415 [ 1 "0.169967142900241" "0.9854497299884601" ] [ 1.4 1 [ swap >float [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test