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 alien.c-types ;
8 QUALIFIED: namespaces.private
9 IN: compiler.tests.codegen
11 ! Originally, this file did black box testing of templating
12 ! optimization. We now have a different codegen, but the tests
13 ! in here are still useful.
16 [ 5000 ] [ [ 5000 ] compile-call ] unit-test
17 [ "hi" ] [ [ "hi" ] compile-call ] unit-test
19 [ 1 2 3 4 ] [ [ 1 2 3 4 ] compile-call ] unit-test
21 [ 1 1 ] [ 1 [ dup ] compile-call ] unit-test
22 [ 0 ] [ 3 [ tag ] compile-call ] unit-test
23 [ 0 3 ] [ 3 [ [ tag ] keep ] compile-call ] unit-test
25 [ 2 3 ] [ 3 [ 2 swap ] compile-call ] unit-test
27 [ 2 1 3 4 ] [ 1 2 [ swap 3 4 ] compile-call ] unit-test
29 [ 2 3 4 ] [ 3 [ 2 swap 4 ] compile-call ] unit-test
31 [ { 1 2 3 } { 1 4 3 } 2 2 ]
32 [ { 1 2 3 } { 1 4 3 } [ over tag over tag ] compile-call ]
35 ! Test literals in either side of a shuffle
36 [ 4 1 ] [ 1 [ [ 3 fixnum+ ] keep ] compile-call ] unit-test
38 [ 2 ] [ 1 2 [ swap fixnum/i ] compile-call ] unit-test
43 [ 1.2 [ tag [ foo ] keep ] compile-call ]
47 [ { 1 2 } [ dup 2 slot swap 3 slot [ foo ] keep ] compile-call ]
52 global [ 3 \ foo set ] bind
53 \ foo [ global >n get namespaces.private:ndrop ] compile-call
56 : blech ( x -- ) drop ;
60 global [ 3 \ foo set ] bind
61 \ foo [ global [ get ] swap blech call ] compile-call
66 global [ 3 \ foo set ] bind
67 \ foo [ global [ get ] swap >n call namespaces.private:ndrop ] compile-call
72 global [ 3 \ foo set ] bind
73 \ foo [ global [ get ] bind ] compile-call
77 -12 -13 [ [ 0 swap fixnum-fast ] bi@ ] compile-call
80 [ -1 2 ] [ 1 2 [ [ 0 swap fixnum- ] dip ] compile-call ] unit-test
83 -12 -13 [ [ 0 swap fixnum- ] bi@ ] compile-call
87 SBUF" " [ 1 slot 1 [ slot ] keep ] compile-call nip
91 [ 3 1 2 3 4 5 6 7 8 9 ] [
93 [ [ [ [ [ [ [ [ [ [ 3 ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ]
97 [ 2 2 2 2 2 2 2 2 2 2 1 ] [
99 [ swap [ dup dup dup dup dup dup dup dup dup ] dip ] compile-call
102 [ ] [ [ 9 [ ] times ] compile-call ] unit-test
106 [ 200 dup [ 200 3array ] curry map drop ] times
107 ] [ (( n -- )) define-temp ] with-compilation-unit drop
110 ! Test how dispatch handles the end of a basic block
111 : try-breaking-dispatch ( n a b -- x str )
112 float+ swap { [ "hey" ] [ "bye" ] } dispatch ;
114 : try-breaking-dispatch-2 ( -- ? )
115 1 1.0 2.5 try-breaking-dispatch "bye" = [ 3.5 = ] dip and ;
118 10000000 [ drop try-breaking-dispatch-2 ] all?
122 : (broken) ( x -- y ) ;
124 [ 2.0 { 2.0 0.0 } ] [
126 [ float/f 0.0 [ drop (broken) ] 2keep 2array ] compile-call
130 : hellish-bug-1 ( a b -- ) 2drop ;
132 : hellish-bug-2 ( i array x -- x )
133 2dup 1 slot eq? [ 2drop ] [
134 2dup array-nth tombstone? [
136 [ array-nth ] 2keep [ 1 fixnum+fast ] dip array-nth
137 pick 2dup hellish-bug-1 3drop
139 ] unless [ 2 fixnum+fast ] dip hellish-bug-2
140 ] if ; inline recursive
142 : hellish-bug-3 ( hash array -- )
143 0 swap hellish-bug-2 drop ;
146 H{ { 1 2 } { 3 4 } } dup array>>
147 [ 0 swap hellish-bug-2 drop ] compile-call
151 : foox ( obj -- obj )
153 [ drop 3 ] [ dup tuple? [ drop 4 ] [ drop 5 ] if ] if ;
155 [ 3 ] [ f foox ] unit-test
159 [ 4 ] [ T{ my-tuple } foox ] unit-test
161 [ 5 ] [ "hi" foox ] unit-test
163 ! Making sure we don't needlessly unbox/rebox
164 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ [ eq? ] dip ] compile-call ] unit-test
166 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ ] compile-call [ eq? ] dip ] unit-test
168 [ t ] [ 1.0 dup [ [ 2.0 float+ ] keep ] compile-call nip eq? ] unit-test
172 { byte-array } declare
173 [ 0 alien-unsigned-1 ] keep
180 [ 0 alien-unsigned-1 ] keep hi-tag
181 ] compile-call byte-array type-number =
188 ] compile-call alien type-number =
193 [ 2dup fixnum< [ [ die ] dip ] when ] compile-call
197 : a-dummy ( a -- ) drop "hi" print ;
201 dup 0 2 3dup pick >= [ >= ] [ 2drop f ] if [
207 ] [ 2drop no-case ] if
212 : dispatch-alignment-regression ( -- c )
213 { tuple vector } 3 slot { word } declare
214 dup 1 slot 0 fixnum-bitand { [ ] } dispatch ;
216 [ t ] [ \ dispatch-alignment-regression optimized? ] unit-test
218 [ vector ] [ dispatch-alignment-regression ] unit-test
221 : bad-value-bug ( a -- b ) [ 3 ] [ 3 ] if f <array> ;
223 [ { f f f } ] [ t bad-value-bug ] unit-test
228 : (gc-check-bug) ( a b -- c )
229 { [ id boa ] [ id boa ] } dispatch ;
231 : gc-check-bug ( -- )
232 10000000 [ "hi" 0 (gc-check-bug) drop ] times ;
234 [ ] [ gc-check-bug ] unit-test
237 : test-1 ( a -- b ) 8 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
239 [ "a" ] [ 8 test-1 ] unit-test
240 [ "b" ] [ 9 test-1 ] unit-test
242 : test-2 ( a -- b ) 1 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
244 [ "a" ] [ 1 test-2 ] unit-test
245 [ "b" ] [ 2 test-2 ] unit-test
247 ! I accidentally fixnum/i-fast on PowerPC
248 [ { { 1 2 } { 3 4 } } ] [
251 [ { array } declare 2 <groups> [ , ] each ] compile-call
257 [ { array } declare 2 <groups> length ] compile-call
260 ! Oops with new intrinsics
261 : fixnum-overflow-control-flow-test ( a b -- c )
262 [ 1 fixnum- ] [ 2 fixnum- ] if 3 fixnum+fast ;
264 [ 3 ] [ 1 t fixnum-overflow-control-flow-test ] unit-test
265 [ 2 ] [ 1 f fixnum-overflow-control-flow-test ] unit-test
269 { float } declare dup 0 =
272 [ 2 "double" "libm" "pow" { "double" "double" } alien-invoke ]
273 [ -0.5 "double" "libm" "pow" { "double" "double" } alien-invoke ]
277 [ 4.0 ] [ 2.0 blah ] unit-test
279 [ 4 ] [ 2 [ dup fixnum* ] compile-call ] unit-test
280 [ 7 ] [ 2 [ dup fixnum* 3 fixnum+fast ] compile-call ] unit-test
284 M: cucumber equal? "The cucumber has no equal" throw ;
286 [ t ] [ [ cucumber ] compile-call cucumber eq? ] unit-test
288 [ 4294967295 B{ 255 255 255 255 } -1 ]
291 [ [ 0 alien-unsigned-4 swap ] [ 0 alien-signed-2 ] bi ]
295 ! Regression found while working on global register allocation
297 : linear-scan-regression-1 ( a b c -- ) 3array , ;
298 : linear-scan-regression-2 ( a b -- ) 2array , ;
300 : linear-scan-regression ( a b c -- )
301 [ linear-scan-regression-2 ]
302 [ linear-scan-regression-1 ]
303 bi-curry bi-curry interleave ;
314 [ { 1 2 3 } "x" "y" linear-scan-regression ] { } make
317 ! Regression from Doug's value numbering changes
318 [ t ] [ 2 [ 1 swap fixnum< ] compile-call ] unit-test
319 [ 3 ] [ 2 [ 1 swap fixnum< [ 3 ] [ 4 ] if ] compile-call ] unit-test
322 [ 0 ] [ 101 [ dup fixnum-fast 1 fixnum+fast 20 fixnum-shift-fast 20 fixnum-shift-fast ] compile-call ] unit-test
325 ! Regression from Slava's value numbering changes
326 [ 1 ] [ 31337 [ dup fixnum<= [ 1 ] [ 2 ] if ] compile-call ] unit-test
328 ! Bug with ##return node construction
329 : return-recursive-bug ( nodes -- ? )
331 dup 3 bitand 1 = [ drop t ] [
336 ] any? ; inline recursive
338 [ t ] [ 3 [ return-recursive-bug ] compile-call ] unit-test
340 ! Coalescing reductions
341 [ f ] [ V{ } 0 [ [ vector? ] both? ] compile-call ] unit-test
342 [ f ] [ 0 V{ } [ [ vector? ] both? ] compile-call ] unit-test
346 [ dup [ \ vector eq? ] [ drop f ] if ] dip
347 dup [ \ vector eq? ] [ drop f ] if
348 over rot [ drop ] [ nip ] if
352 ! Coalesing bug reduced from sequence-parser:take-sequence
353 : coalescing-bug-1 ( a b c d -- a b c d )
356 [ [ drop ] 2dip length > ]
358 } 3|| [ 3drop f ] [ slice boa ] if swap [ [ length ] bi@ ] 2keep ;
360 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } -10 3 "hello" coalescing-bug-1 ] unit-test
361 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 0 7 "hello" coalescing-bug-1 ] unit-test
362 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 3 2 "hello" coalescing-bug-1 ] unit-test
363 [ 2 3 T{ slice f 1 3 "hello" } { 1 2 3 } ] [ { 1 2 3 } 1 3 "hello" coalescing-bug-1 ] unit-test
365 ! Another one, found by Dan
366 : coalescing-bug-2 ( a -- b )
367 dup dup 10 fixnum< [ 1 fixnum+fast ] when
368 fixnum+fast 2 fixnum*fast 2 fixnum-fast 2 fixnum*fast 2 fixnum+fast ;
370 [ 10 ] [ 1 coalescing-bug-2 ] unit-test
371 [ 86 ] [ 11 coalescing-bug-2 ] unit-test
373 ! Regression in suffix-arrays code
374 : coalescing-bug-3 ( from/f to/f seq -- slice )
376 [ drop 0 or ] [ length or ] bi-curry bi*
380 [ T{ slice f 0 5 "hello" } ] [ f f "hello" coalescing-bug-3 ] unit-test
381 [ T{ slice f 1 5 "hello" } ] [ 1 f "hello" coalescing-bug-3 ] unit-test
382 [ T{ slice f 0 3 "hello" } ] [ f 3 "hello" coalescing-bug-3 ] unit-test
383 [ T{ slice f 1 3 "hello" } ] [ 1 3 "hello" coalescing-bug-3 ] unit-test
384 [ T{ slice f 3 3 "hello" } ] [ 4 3 "hello" coalescing-bug-3 ] unit-test
385 [ T{ slice f 5 5 "hello" } ] [ 6 f "hello" coalescing-bug-3 ] unit-test
388 : coalescing-bug-4 ( a b c -- a b c )
389 [ [ min ] keep ] dip vector? [ 1 ] [ 2 ] if ;
391 [ 2 3 2 ] [ 2 3 "" coalescing-bug-4 ] unit-test
392 [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
393 [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
394 [ 2 3 1 ] [ 2 3 V{ } coalescing-bug-4 ] unit-test
395 [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
396 [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
398 ! Global stack analysis dataflow equations are wrong
399 : some-word ( a -- b ) 2 + ;
400 : global-dcn-bug-1 ( a b -- c d )
401 dup [ [ drop 1 ] dip ] [ [ some-word ] dip ] if
402 dup [ [ 1 fixnum+fast ] dip ] [ [ drop 1 ] dip ] if ;
404 [ 2 t ] [ 0 t global-dcn-bug-1 ] unit-test
405 [ 1 f ] [ 0 f global-dcn-bug-1 ] unit-test
408 : missing-gc-check-1 ( a -- b ) { fixnum } declare <alien> ;
409 : missing-gc-check-2 ( -- ) 10000000 [ missing-gc-check-1 drop ] each-integer ;
411 [ ] [ missing-gc-check-2 ] unit-test
413 [ 1 "0.169967142900241" ] [ 1.4 [ 1 swap fcos ] compile-call number>string ] unit-test
414 [ 1 "0.169967142900241" ] [ 1.4 1 [ swap fcos ] compile-call number>string ] unit-test
415 [ "0.169967142900241" "0.9854497299884601" ] [ 1.4 [ [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
416 [ 1 "0.169967142900241" "0.9854497299884601" ] [ 1.4 1 [ swap >float [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
418 [ 6.0 ] [ 1.0 [ >float 3.0 + [ B{ 0 0 0 0 } 0 set-alien-float ] [ 2.0 + ] bi ] compile-call ] unit-test
420 ! Bug in linearization
421 [ 283686952174081 ] [
423 { byte-array } declare
428 swap 1 < [ [ ] dip ] [ [ ] dip ] if
435 TUPLE: myseq { underlying1 byte-array read-only } { underlying2 byte-array read-only } ;
439 T{ myseq f B{ 1 0 0 0 } B{ 1 0 0 0 } }
440 T{ myseq f B{ 0 0 0 1 } B{ 0 0 0 1 } } ?
446 over 1 < [ underlying1>> ] [ [ 1 - ] dip underlying2>> ] if
447 swap 4 * >fixnum alien-signed-4
449 ] 2curry each-integer