1 USING: generalizations accessors arrays compiler 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 combinators.short-circuit math.order
7 math.libm math.parser math.functions alien.syntax ;
9 QUALIFIED: namespaces.private
10 IN: compiler.tests.codegen
12 ! Originally, this file did black box testing of templating
13 ! optimization. We now have a different codegen, but the tests
14 ! in here are still useful.
17 [ 5000 ] [ [ 5000 ] compile-call ] unit-test
18 [ "hi" ] [ [ "hi" ] compile-call ] unit-test
20 [ 1 2 3 4 ] [ [ 1 2 3 4 ] compile-call ] unit-test
22 [ 1 1 ] [ 1 [ dup ] compile-call ] unit-test
23 [ 0 ] [ 3 [ tag ] compile-call ] unit-test
24 [ 0 3 ] [ 3 [ [ tag ] keep ] compile-call ] unit-test
26 [ 2 3 ] [ 3 [ 2 swap ] compile-call ] unit-test
28 [ 2 1 3 4 ] [ 1 2 [ swap 3 4 ] compile-call ] unit-test
30 [ 2 3 4 ] [ 3 [ 2 swap 4 ] compile-call ] unit-test
32 [ { 1 2 3 } { 1 4 3 } 2 2 ]
33 [ { 1 2 3 } { 1 4 3 } [ over tag over tag ] compile-call ]
36 ! Test literals in either side of a shuffle
37 [ 4 1 ] [ 1 [ [ 3 fixnum+ ] keep ] compile-call ] unit-test
39 [ 2 ] [ 1 2 [ swap fixnum/i ] compile-call ] unit-test
44 [ 1.2 [ tag [ foo ] keep ] compile-call ]
48 [ { 1 2 } [ dup 2 slot swap 3 slot [ foo ] keep ] compile-call ]
53 global [ 3 \ foo set ] bind
54 \ foo [ global >n get namespaces.private:ndrop ] compile-call
57 : blech ( x -- ) drop ;
61 global [ 3 \ foo set ] bind
62 \ foo [ global [ get ] swap blech call ] compile-call
67 global [ 3 \ foo set ] bind
68 \ foo [ global [ get ] swap >n call namespaces.private:ndrop ] compile-call
73 global [ 3 \ foo set ] bind
74 \ foo [ global [ get ] bind ] compile-call
78 -12 -13 [ [ 0 swap fixnum-fast ] bi@ ] compile-call
81 [ -1 2 ] [ 1 2 [ [ 0 swap fixnum- ] dip ] compile-call ] unit-test
84 -12 -13 [ [ 0 swap fixnum- ] bi@ ] compile-call
88 SBUF" " [ 1 slot 1 [ slot ] keep ] compile-call nip
92 [ 3 1 2 3 4 5 6 7 8 9 ] [
94 [ [ [ [ [ [ [ [ [ [ 3 ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ]
98 [ 2 2 2 2 2 2 2 2 2 2 1 ] [
100 [ swap [ dup dup dup dup dup dup dup dup dup ] dip ] compile-call
103 [ ] [ [ 9 [ ] times ] compile-call ] unit-test
107 [ 200 dup [ 200 3array ] curry map drop ] times
108 ] [ (( n -- )) define-temp ] with-compilation-unit drop
111 ! Test how dispatch handles the end of a basic block
112 : try-breaking-dispatch ( n a b -- x str )
113 float+ swap { [ "hey" ] [ "bye" ] } dispatch ;
115 : try-breaking-dispatch-2 ( -- ? )
116 1 1.0 2.5 try-breaking-dispatch "bye" = [ 3.5 = ] dip and ;
119 10000000 [ drop try-breaking-dispatch-2 ] all?
123 : (broken) ( x -- y ) ;
125 [ 2.0 { 2.0 0.0 } ] [
127 [ float/f 0.0 [ drop (broken) ] 2keep 2array ] compile-call
131 : hellish-bug-1 ( a b -- ) 2drop ;
133 : hellish-bug-2 ( i array x -- x )
134 2dup 1 slot eq? [ 2drop ] [
135 2dup array-nth tombstone? [
137 [ array-nth ] 2keep [ 1 fixnum+fast ] dip array-nth
138 pick 2dup hellish-bug-1 3drop
140 ] unless [ 2 fixnum+fast ] dip hellish-bug-2
141 ] if ; inline recursive
143 : hellish-bug-3 ( hash array -- )
144 0 swap hellish-bug-2 drop ;
147 H{ { 1 2 } { 3 4 } } dup array>>
148 [ 0 swap hellish-bug-2 drop ] compile-call
152 : foox ( obj -- obj )
154 [ drop 3 ] [ dup tuple? [ drop 4 ] [ drop 5 ] if ] if ;
156 [ 3 ] [ f foox ] unit-test
160 [ 4 ] [ T{ my-tuple } foox ] unit-test
162 [ 5 ] [ "hi" foox ] unit-test
164 ! Making sure we don't needlessly unbox/rebox
165 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ [ eq? ] dip ] compile-call ] unit-test
167 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ ] compile-call [ eq? ] dip ] unit-test
169 [ t ] [ 1.0 dup [ [ 2.0 float+ ] keep ] compile-call nip eq? ] unit-test
173 { byte-array } declare
174 [ 0 alien-unsigned-1 ] keep
180 [ 2dup fixnum< [ [ die ] dip ] when ] compile-call
184 : a-dummy ( a -- ) drop "hi" print ;
188 dup 0 2 3dup pick >= [ >= ] [ 2drop f ] if [
194 ] [ 2drop no-case ] if
199 : dispatch-alignment-regression ( -- c )
200 { tuple vector } 3 slot { word } declare
201 dup 1 slot 0 fixnum-bitand { [ ] } dispatch ;
203 [ t ] [ \ dispatch-alignment-regression optimized? ] unit-test
205 [ vector ] [ dispatch-alignment-regression ] unit-test
208 : bad-value-bug ( a -- b ) [ 3 ] [ 3 ] if f <array> ;
210 [ { f f f } ] [ t bad-value-bug ] unit-test
215 : (gc-check-bug) ( a b -- c )
216 { [ id boa ] [ id boa ] } dispatch ;
218 : gc-check-bug ( -- )
219 10000000 [ "hi" 0 (gc-check-bug) drop ] times ;
221 [ ] [ gc-check-bug ] unit-test
224 : test-1 ( a -- b ) 8 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
226 [ "a" ] [ 8 test-1 ] unit-test
227 [ "b" ] [ 9 test-1 ] unit-test
229 : test-2 ( a -- b ) 1 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
231 [ "a" ] [ 1 test-2 ] unit-test
232 [ "b" ] [ 2 test-2 ] unit-test
234 ! I accidentally fixnum/i-fast on PowerPC
235 [ { { 1 2 } { 3 4 } } ] [
238 [ { array } declare 2 <groups> [ , ] each ] compile-call
244 [ { array } declare 2 <groups> length ] compile-call
247 ! Oops with new intrinsics
248 : fixnum-overflow-control-flow-test ( a b -- c )
249 [ 1 fixnum- ] [ 2 fixnum- ] if 3 fixnum+fast ;
251 [ 3 ] [ 1 t fixnum-overflow-control-flow-test ] unit-test
252 [ 2 ] [ 1 f fixnum-overflow-control-flow-test ] unit-test
256 { float } declare dup 0 =
259 [ 2 double "libm" "pow" { double double } alien-invoke ]
260 [ -0.5 double "libm" "pow" { double double } alien-invoke ]
264 [ 4.0 ] [ 2.0 blah ] unit-test
266 [ 4 ] [ 2 [ dup fixnum* ] compile-call ] unit-test
267 [ 7 ] [ 2 [ dup fixnum* 3 fixnum+fast ] compile-call ] unit-test
271 M: cucumber equal? "The cucumber has no equal" throw ;
273 [ t ] [ [ cucumber ] compile-call cucumber eq? ] unit-test
275 [ 4294967295 B{ 255 255 255 255 } -1 ]
278 [ [ 0 alien-unsigned-4 swap ] [ 0 alien-signed-2 ] bi ]
282 ! Regression found while working on global register allocation
284 : linear-scan-regression-1 ( a b c -- ) 3array , ;
285 : linear-scan-regression-2 ( a b -- ) 2array , ;
287 : linear-scan-regression ( a b c -- )
288 [ linear-scan-regression-2 ]
289 [ linear-scan-regression-1 ]
290 bi-curry bi-curry interleave ;
301 [ { 1 2 3 } "x" "y" linear-scan-regression ] { } make
304 ! Regression from Doug's value numbering changes
305 [ t ] [ 2 [ 1 swap fixnum< ] compile-call ] unit-test
306 [ 3 ] [ 2 [ 1 swap fixnum< [ 3 ] [ 4 ] if ] compile-call ] unit-test
309 [ 0 ] [ 101 [ dup fixnum-fast 1 fixnum+fast 20 fixnum-shift-fast 20 fixnum-shift-fast ] compile-call ] unit-test
312 ! Regression from Slava's value numbering changes
313 [ 1 ] [ 31337 [ dup fixnum<= [ 1 ] [ 2 ] if ] compile-call ] unit-test
315 ! Bug with ##return node construction
316 : return-recursive-bug ( nodes -- ? )
318 dup 3 bitand 1 = [ drop t ] [
323 ] any? ; inline recursive
325 [ t ] [ 3 [ return-recursive-bug ] compile-call ] unit-test
327 ! Coalescing reductions
328 [ f ] [ V{ } 0 [ [ vector? ] both? ] compile-call ] unit-test
329 [ f ] [ 0 V{ } [ [ vector? ] both? ] compile-call ] unit-test
333 [ dup [ \ vector eq? ] [ drop f ] if ] dip
334 dup [ \ vector eq? ] [ drop f ] if
335 over rot [ drop ] [ nip ] if
339 ! Coalesing bug reduced from sequence-parser:take-sequence
340 : coalescing-bug-1 ( a b c d -- a b c d )
343 [ [ drop ] 2dip length > ]
345 } 3|| [ 3drop f ] [ slice boa ] if swap [ [ length ] bi@ ] 2keep ;
347 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } -10 3 "hello" coalescing-bug-1 ] unit-test
348 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 0 7 "hello" coalescing-bug-1 ] unit-test
349 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 3 2 "hello" coalescing-bug-1 ] unit-test
350 [ 2 3 T{ slice f 1 3 "hello" } { 1 2 3 } ] [ { 1 2 3 } 1 3 "hello" coalescing-bug-1 ] unit-test
352 ! Another one, found by Dan
353 : coalescing-bug-2 ( a -- b )
354 dup dup 10 fixnum< [ 1 fixnum+fast ] when
355 fixnum+fast 2 fixnum*fast 2 fixnum-fast 2 fixnum*fast 2 fixnum+fast ;
357 [ 10 ] [ 1 coalescing-bug-2 ] unit-test
358 [ 86 ] [ 11 coalescing-bug-2 ] unit-test
360 ! Regression in suffix-arrays code
361 : coalescing-bug-3 ( from/f to/f seq -- slice )
363 [ drop 0 or ] [ length or ] bi-curry bi*
367 [ T{ slice f 0 5 "hello" } ] [ f f "hello" coalescing-bug-3 ] unit-test
368 [ T{ slice f 1 5 "hello" } ] [ 1 f "hello" coalescing-bug-3 ] unit-test
369 [ T{ slice f 0 3 "hello" } ] [ f 3 "hello" coalescing-bug-3 ] unit-test
370 [ T{ slice f 1 3 "hello" } ] [ 1 3 "hello" coalescing-bug-3 ] unit-test
371 [ T{ slice f 3 3 "hello" } ] [ 4 3 "hello" coalescing-bug-3 ] unit-test
372 [ T{ slice f 5 5 "hello" } ] [ 6 f "hello" coalescing-bug-3 ] unit-test
375 : coalescing-bug-4 ( a b c -- a b c )
376 [ [ min ] keep ] dip vector? [ 1 ] [ 2 ] if ;
378 [ 2 3 2 ] [ 2 3 "" coalescing-bug-4 ] unit-test
379 [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
380 [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
381 [ 2 3 1 ] [ 2 3 V{ } coalescing-bug-4 ] unit-test
382 [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
383 [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
385 ! Global stack analysis dataflow equations are wrong
386 : some-word ( a -- b ) 2 + ;
387 : global-dcn-bug-1 ( a b -- c d )
388 dup [ [ drop 1 ] dip ] [ [ some-word ] dip ] if
389 dup [ [ 1 fixnum+fast ] dip ] [ [ drop 1 ] dip ] if ;
391 [ 2 t ] [ 0 t global-dcn-bug-1 ] unit-test
392 [ 1 f ] [ 0 f global-dcn-bug-1 ] unit-test
395 : missing-gc-check-1 ( a -- b ) { fixnum } declare <alien> ;
396 : missing-gc-check-2 ( -- ) 10000000 [ missing-gc-check-1 drop ] each-integer ;
398 [ ] [ missing-gc-check-2 ] unit-test
400 [ 1 "0.169967142900241" ] [ 1.4 [ 1 swap fcos ] compile-call number>string ] unit-test
401 [ 1 "0.169967142900241" ] [ 1.4 1 [ swap fcos ] compile-call number>string ] unit-test
402 [ "0.169967142900241" "0.9854497299884601" ] [ 1.4 [ [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
403 [ 1 "0.169967142900241" "0.9854497299884601" ] [ 1.4 1 [ swap >float [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
405 [ 6.0 ] [ 1.0 [ >float 3.0 + [ B{ 0 0 0 0 } 0 set-alien-float ] [ 2.0 + ] bi ] compile-call ] unit-test
407 ! Bug in linearization
408 [ 283686952174081 ] [
410 { byte-array } declare
415 swap 1 < [ [ ] dip ] [ [ ] dip ] if
422 ! Bug in CSSA construction
423 TUPLE: myseq { underlying1 byte-array read-only } { underlying2 byte-array read-only } ;
427 T{ myseq f B{ 1 0 0 0 } B{ 1 0 0 0 } }
428 T{ myseq f B{ 0 0 0 1 } B{ 0 0 0 1 } } ?
434 over 1 < [ underlying1>> ] [ [ 1 - ] dip underlying2>> ] if
435 swap 4 * >fixnum alien-signed-4
437 ] 2curry each-integer
441 ! Bug in linear scan's partial sync point logic
443 [ 1.0 100 [ fsin ] times 1.0 float+ ] compile-call
444 1.168852488727981 1.e-9 ~
448 [ 2.0 4 [ 2.0 fpow ] times 1.0 float+ ] compile-call
451 ! ##box-displaced-alien is a def-is-use instruction
455 100 [ 10 swap <displaced-alien> ] times
456 1 swap <displaced-alien>
460 ! Forgot to two-operand shifts
463 [ [ HEX: f bitand ] bi@ [ shift ] [ drop -3 shift ] 2bi ] compile-call