1 USING: generalizations accessors arrays compiler.test kernel
2 kernel.private math hashtables.private math.private namespaces
3 sequences tools.test namespaces.private slots.private
4 sequences.private byte-arrays alien alien.accessors layouts
5 words definitions compiler.units io combinators vectors grouping
6 make alien.c-types alien.data combinators.short-circuit math.order
7 math.libm math.parser math.functions alien.syntax memory
10 QUALIFIED: namespaces.private
11 IN: compiler.tests.codegen
13 ! Originally, this file did black box testing of templating
14 ! optimization. We now have a different codegen, but the tests
15 ! in here are still useful.
18 [ 5000 ] [ [ 5000 ] compile-call ] unit-test
19 [ "hi" ] [ [ "hi" ] compile-call ] unit-test
21 [ 1 2 3 4 ] [ [ 1 2 3 4 ] compile-call ] unit-test
23 [ 1 1 ] [ 1 [ dup ] compile-call ] unit-test
24 [ 0 ] [ 3 [ tag ] compile-call ] unit-test
25 [ 0 3 ] [ 3 [ [ tag ] keep ] compile-call ] unit-test
27 [ 2 3 ] [ 3 [ 2 swap ] compile-call ] unit-test
29 [ 2 1 3 4 ] [ 1 2 [ swap 3 4 ] compile-call ] unit-test
31 [ 2 3 4 ] [ 3 [ 2 swap 4 ] compile-call ] unit-test
33 [ { 1 2 3 } { 1 4 3 } 2 2 ]
34 [ { 1 2 3 } { 1 4 3 } [ over tag over tag ] compile-call ]
37 ! Test literals in either side of a shuffle
38 [ 4 1 ] [ 1 [ [ 3 fixnum+ ] keep ] compile-call ] unit-test
40 [ 2 ] [ 1 2 [ swap fixnum/i ] compile-call ] unit-test
45 [ 1.2 [ tag [ foo ] keep ] compile-call ]
49 [ { 1 2 } [ dup 2 slot swap 3 slot [ foo ] keep ] compile-call ]
54 global [ 3 \ foo set ] bind
55 \ foo [ global >n get namespaces.private:ndrop ] compile-call
58 : blech ( x -- ) drop ;
62 global [ 3 \ foo set ] bind
63 \ foo [ global [ get ] swap blech call ] compile-call
68 global [ 3 \ foo set ] bind
69 \ foo [ global [ get ] swap >n call namespaces.private:ndrop ] compile-call
74 global [ 3 \ foo set ] bind
75 \ foo [ global [ get ] bind ] compile-call
79 -12 -13 [ [ 0 swap fixnum-fast ] bi@ ] compile-call
82 [ -1 2 ] [ 1 2 [ [ 0 swap fixnum- ] dip ] compile-call ] unit-test
85 -12 -13 [ [ 0 swap fixnum- ] bi@ ] compile-call
89 SBUF" " [ 1 slot 1 [ slot ] keep ] compile-call nip
93 [ 3 1 2 3 4 5 6 7 8 9 ] [
95 [ [ [ [ [ [ [ [ [ [ 3 ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ]
99 [ 2 2 2 2 2 2 2 2 2 2 1 ] [
101 [ swap [ dup dup dup dup dup dup dup dup dup ] dip ] compile-call
104 [ ] [ [ 9 [ ] times ] compile-call ] unit-test
108 [ 200 dup [ 200 3array ] curry map drop ] times
109 ] [ ( n -- ) define-temp ] with-compilation-unit drop
112 ! Test how dispatch handles the end of a basic block
113 : try-breaking-dispatch ( n a b -- x str )
114 float+ swap { [ "hey" ] [ "bye" ] } dispatch ;
116 : try-breaking-dispatch-2 ( -- ? )
117 1 1.0 2.5 try-breaking-dispatch "bye" = [ 3.5 = ] dip and ;
120 10000000 [ drop try-breaking-dispatch-2 ] all-integers?
124 : (broken) ( x -- y ) ;
126 [ 2.0 { 2.0 0.0 } ] [
128 [ float/f 0.0 [ drop (broken) ] 2keep 2array ] compile-call
132 : hellish-bug-1 ( a b -- ) 2drop ;
134 : hellish-bug-2 ( i array x -- x )
135 2dup 1 slot eq? [ 2drop ] [
136 2dup array-nth tombstone? [
138 [ array-nth ] 2keep [ 1 fixnum+fast ] dip array-nth
139 pick 2dup hellish-bug-1 3drop
141 ] unless [ 2 fixnum+fast ] dip hellish-bug-2
142 ] if ; inline recursive
144 : hellish-bug-3 ( hash array -- )
145 0 swap hellish-bug-2 drop ;
148 H{ { 1 2 } { 3 4 } } dup array>>
149 [ 0 swap hellish-bug-2 drop ] compile-call
153 : foox ( obj -- obj )
155 [ drop 3 ] [ dup tuple? [ drop 4 ] [ drop 5 ] if ] if ;
157 [ 3 ] [ f foox ] unit-test
161 [ 4 ] [ T{ my-tuple } foox ] unit-test
163 [ 5 ] [ "hi" foox ] unit-test
165 ! Making sure we don't needlessly unbox/rebox
166 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ [ eq? ] dip ] compile-call ] unit-test
168 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ ] compile-call [ eq? ] dip ] unit-test
170 [ t ] [ 1.0 dup [ [ 2.0 float+ ] keep ] compile-call nip eq? ] unit-test
174 { byte-array } declare
175 [ 0 alien-unsigned-1 ] keep
181 [ 2dup fixnum< [ [ die ] dip ] when ] compile-call
185 : a-dummy ( a -- ) drop "hi" print ;
189 dup 0 2 3dup pick >= [ >= ] [ 2drop f ] if [
195 ] [ 2drop no-case ] if
200 : dispatch-alignment-regression ( -- c )
201 { tuple vector } 3 slot { word } declare
202 dup 1 slot 0 fixnum-bitand { [ ] } dispatch ;
204 [ t ] [ \ dispatch-alignment-regression optimized? ] unit-test
206 [ vector ] [ dispatch-alignment-regression ] unit-test
209 : bad-value-bug ( a -- b ) [ 3 ] [ 3 ] if f <array> ;
211 [ { f f f } ] [ t bad-value-bug ] unit-test
216 : (gc-check-bug) ( a b -- c )
217 { [ id boa ] [ id boa ] } dispatch ;
219 : gc-check-bug ( -- )
220 10000000 [ "hi" 0 (gc-check-bug) drop ] times ;
222 [ ] [ gc-check-bug ] unit-test
225 : test-1 ( a -- b ) 8 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
227 [ "a" ] [ 8 test-1 ] unit-test
228 [ "b" ] [ 9 test-1 ] unit-test
230 : test-2 ( a -- b ) 1 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
232 [ "a" ] [ 1 test-2 ] unit-test
233 [ "b" ] [ 2 test-2 ] unit-test
235 ! I accidentally fixnum/i-fast on PowerPC
236 [ { { 1 2 } { 3 4 } } ] [
239 [ { array } declare 2 <groups> [ , ] each ] compile-call
245 [ { array } declare 2 <groups> length ] compile-call
248 ! Oops with new intrinsics
249 : fixnum-overflow-control-flow-test ( a b -- c )
250 [ 1 fixnum- ] [ 2 fixnum- ] if 3 fixnum+fast ;
252 [ 3 ] [ 1 t fixnum-overflow-control-flow-test ] unit-test
253 [ 2 ] [ 1 f fixnum-overflow-control-flow-test ] unit-test
257 { float } declare dup 0 =
260 [ 2 double "libm" "pow" { double double } alien-invoke ]
261 [ -0.5 double "libm" "pow" { double double } alien-invoke ]
265 [ 4.0 ] [ 2.0 blah ] unit-test
267 [ 4 ] [ 2 [ dup fixnum* ] compile-call ] unit-test
268 [ 7 ] [ 2 [ dup fixnum* 3 fixnum+fast ] compile-call ] unit-test
272 M: cucumber equal? "The cucumber has no equal" throw ;
274 [ t ] [ [ cucumber ] compile-call cucumber eq? ] unit-test
276 [ 4294967295 B{ 255 255 255 255 } -1 ]
280 [ [ 0 alien-unsigned-4 swap ] [ 0 alien-signed-2 ] bi ]
284 ! Regression found while working on global register allocation
286 : linear-scan-regression-1 ( a b c -- ) 3array , ;
287 : linear-scan-regression-2 ( a b -- ) 2array , ;
289 : linear-scan-regression ( a b c -- )
290 [ linear-scan-regression-2 ]
291 [ linear-scan-regression-1 ]
292 bi-curry bi-curry interleave ;
303 [ { 1 2 3 } "x" "y" linear-scan-regression ] { } make
306 ! Regression from Doug's value numbering changes
307 [ t ] [ 2 [ 1 swap fixnum< ] compile-call ] unit-test
308 [ 3 ] [ 2 [ 1 swap fixnum< [ 3 ] [ 4 ] if ] compile-call ] unit-test
311 [ 0 ] [ 101 [ dup fixnum-fast 1 fixnum+fast 20 fixnum-shift-fast 20 fixnum-shift-fast ] compile-call ] unit-test
314 ! Regression from Slava's value numbering changes
315 [ 1 ] [ 31337 [ dup fixnum<= [ 1 ] [ 2 ] if ] compile-call ] unit-test
317 ! Bug with ##return node construction
318 : return-recursive-bug ( nodes -- ? )
319 { fixnum } declare iota [
320 dup 3 bitand 1 = [ drop t ] [
325 ] any? ; inline recursive
327 [ t ] [ 3 [ return-recursive-bug ] compile-call ] unit-test
329 ! Coalescing reductions
330 [ f ] [ V{ } 0 [ [ vector? ] both? ] compile-call ] unit-test
331 [ f ] [ 0 V{ } [ [ vector? ] both? ] compile-call ] unit-test
335 [ dup [ \ vector eq? ] [ drop f ] if ] dip
336 dup [ \ vector eq? ] [ drop f ] if
337 over rot [ drop ] [ nip ] if
341 ! Coalesing bug reduced from sequence-parser:take-sequence
342 : coalescing-bug-1 ( a b c d -- a b c d )
345 [ [ drop ] 2dip length > ]
347 } 3|| [ 3drop f ] [ slice boa ] if swap [ [ length ] bi@ ] 2keep ;
349 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } -10 3 "hello" coalescing-bug-1 ] unit-test
350 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 0 7 "hello" coalescing-bug-1 ] unit-test
351 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 3 2 "hello" coalescing-bug-1 ] unit-test
352 [ 2 3 T{ slice f 1 3 "hello" } { 1 2 3 } ] [ { 1 2 3 } 1 3 "hello" coalescing-bug-1 ] unit-test
354 ! Another one, found by Dan
355 : coalescing-bug-2 ( a -- b )
356 dup dup 10 fixnum< [ 1 fixnum+fast ] when
357 fixnum+fast 2 fixnum*fast 2 fixnum-fast 2 fixnum*fast 2 fixnum+fast ;
359 [ 10 ] [ 1 coalescing-bug-2 ] unit-test
360 [ 86 ] [ 11 coalescing-bug-2 ] unit-test
362 ! Regression in suffix-arrays code
363 : coalescing-bug-3 ( from/f to/f seq -- slice )
365 [ drop 0 or ] [ length or ] bi-curry bi*
369 [ T{ slice f 0 5 "hello" } ] [ f f "hello" coalescing-bug-3 ] unit-test
370 [ T{ slice f 1 5 "hello" } ] [ 1 f "hello" coalescing-bug-3 ] unit-test
371 [ T{ slice f 0 3 "hello" } ] [ f 3 "hello" coalescing-bug-3 ] unit-test
372 [ T{ slice f 1 3 "hello" } ] [ 1 3 "hello" coalescing-bug-3 ] unit-test
373 [ T{ slice f 3 3 "hello" } ] [ 4 3 "hello" coalescing-bug-3 ] unit-test
374 [ T{ slice f 5 5 "hello" } ] [ 6 f "hello" coalescing-bug-3 ] unit-test
377 : coalescing-bug-4 ( a b c -- a b c )
378 [ [ min ] keep ] dip vector? [ 1 ] [ 2 ] if ;
380 [ 2 3 2 ] [ 2 3 "" coalescing-bug-4 ] unit-test
381 [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
382 [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
383 [ 2 3 1 ] [ 2 3 V{ } coalescing-bug-4 ] unit-test
384 [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
385 [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
387 ! Global stack analysis dataflow equations are wrong
388 : some-word ( a -- b ) 2 + ;
389 : global-dcn-bug-1 ( a b -- c d )
390 dup [ [ drop 1 ] dip ] [ [ some-word ] dip ] if
391 dup [ [ 1 fixnum+fast ] dip ] [ [ drop 1 ] dip ] if ;
393 [ 2 t ] [ 0 t global-dcn-bug-1 ] unit-test
394 [ 1 f ] [ 0 f global-dcn-bug-1 ] unit-test
397 : missing-gc-check-1 ( a -- b ) { fixnum } declare <alien> ;
398 : missing-gc-check-2 ( -- ) 10000000 [ missing-gc-check-1 drop ] each-integer ;
400 [ ] [ missing-gc-check-2 ] unit-test
402 [ 1 "0.169967142900241" ] [ 1.4 [ 1 swap fcos ] compile-call number>string ] unit-test
403 [ 1 "0.169967142900241" ] [ 1.4 1 [ swap fcos ] compile-call number>string ] unit-test
404 [ "0.169967142900241" "0.9854497299884601" ] [ 1.4 [ [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
405 [ 1 "0.169967142900241" "0.9854497299884601" ] [ 1.4 1 [ swap >float [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
407 [ 6.0 ] [ 1.0 [ >float 3.0 + [ B{ 0 0 0 0 } 0 set-alien-float ] [ 2.0 + ] bi ] compile-call ] unit-test
409 ! Bug in linearization
410 [ 283686952174081 ] [
412 { byte-array } declare
417 swap 1 < [ [ ] dip ] [ [ ] dip ] if
424 ! Bug in CSSA construction
425 TUPLE: myseq { underlying1 byte-array read-only } { underlying2 byte-array read-only } ;
429 T{ myseq f B{ 1 0 0 0 } B{ 1 0 0 0 } }
430 T{ myseq f B{ 0 0 0 1 } B{ 0 0 0 1 } } ?
436 over 1 < [ underlying1>> ] [ [ 1 - ] dip underlying2>> ] if
437 swap 4 * >fixnum alien-signed-4
439 ] 2curry each-integer
443 ! Bug in linear scan's partial sync point logic
445 [ 1.0 100 [ fsin ] times 1.0 float+ ] compile-call
446 1.168852488727981 1.e-9 ~
450 [ 2.0 4 [ 2.0 fpow ] times 1.0 float+ ] compile-call
453 ! ##box-displaced-alien is a def-is-use instruction
457 100 [ 10 swap <displaced-alien> ] times
458 1 swap <displaced-alien>
462 ! Forgot to two-operand shifts
465 [ [ 0xf bitand ] bi@ [ shift ] [ drop -3 shift ] 2bi ] compile-call
471 10 10 <byte-array> [ <displaced-alien> underlying>> ] keep eq?
475 ! GC root offsets were computed wrong on x86
476 : gc-root-messup ( a -- b )
478 1024 (byte-array) 2array
479 10 void* "libc" "malloc" { ulong } alien-invoke
480 void "libc" "free" { void* } alien-invoke
483 [ ] [ 2000 [ "hello" clone dup gc-root-messup first eq? t assert= ] times ] unit-test
485 ! Write barrier elimination was being done before scheduling and
486 ! GC check insertion, and didn't take subroutine calls into
488 : write-barrier-elim-in-wrong-place ( -- obj )
489 ! A callback used below
490 void { } cdecl [ compact-gc ] alien-callback
491 ! Allocate an object A in the nursery
493 ! Subroutine call promotes the object to tenured
494 swap void { } cdecl alien-indirect
495 ! Allocate another object B in the nursery, store it into
497 1 f <array> over set-first
498 ! Now object A's card should be marked and minor GC should
502 [ 100 [ ] times ] infer.
505 [ { { f } } ] [ write-barrier-elim-in-wrong-place ] unit-test
507 ! GC maps must support derived pointers
508 : (derived-pointer-test-1) ( -- byte-array )
511 : derived-pointer-test-1 ( -- byte-array )
512 ! A callback used below
513 void { } cdecl [ compact-gc ] alien-callback
514 ! Put the construction in a word since instruction selection
515 ! eliminates the untagged pointer entirely if the value is a
517 (derived-pointer-test-1) { c-ptr } declare
518 ! Store into an array, an untagged pointer to the payload
519 ! is now an available expression
520 123 over 0 set-alien-unsigned-1
521 ! GC, moving the array and derived pointer
522 swap void { } cdecl alien-indirect
523 ! Store into the array again
524 231 over 1 set-alien-unsigned-1 ;
526 [ B{ 123 231 } ] [ derived-pointer-test-1 ] unit-test