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 ;
6 QUALIFIED: namespaces.private
7 IN: compiler.tests.codegen
9 ! Originally, this file did black box testing of templating
10 ! optimization. We now have a different codegen, but the tests
11 ! in here are still useful.
14 [ 5000 ] [ [ 5000 ] compile-call ] unit-test
15 [ "hi" ] [ [ "hi" ] compile-call ] unit-test
17 [ 1 2 3 4 ] [ [ 1 2 3 4 ] compile-call ] unit-test
19 [ 1 1 ] [ 1 [ dup ] compile-call ] unit-test
20 [ 0 ] [ 3 [ tag ] compile-call ] unit-test
21 [ 0 3 ] [ 3 [ [ tag ] keep ] compile-call ] unit-test
23 [ 2 3 ] [ 3 [ 2 swap ] compile-call ] unit-test
25 [ 2 1 3 4 ] [ 1 2 [ swap 3 4 ] compile-call ] unit-test
27 [ 2 3 4 ] [ 3 [ 2 swap 4 ] compile-call ] unit-test
29 [ { 1 2 3 } { 1 4 3 } 2 2 ]
30 [ { 1 2 3 } { 1 4 3 } [ over tag over tag ] compile-call ]
33 ! Test literals in either side of a shuffle
34 [ 4 1 ] [ 1 [ [ 3 fixnum+ ] keep ] compile-call ] unit-test
36 [ 2 ] [ 1 2 [ swap fixnum/i ] compile-call ] unit-test
41 [ 1.2 [ tag [ foo ] keep ] compile-call ]
45 [ { 1 2 } [ dup 2 slot swap 3 slot [ foo ] keep ] compile-call ]
50 global [ 3 \ foo set ] bind
51 \ foo [ global >n get namespaces.private:ndrop ] compile-call
54 : blech ( x -- ) drop ;
58 global [ 3 \ foo set ] bind
59 \ foo [ global [ get ] swap blech call ] compile-call
64 global [ 3 \ foo set ] bind
65 \ foo [ global [ get ] swap >n call namespaces.private:ndrop ] compile-call
70 global [ 3 \ foo set ] bind
71 \ foo [ global [ get ] bind ] compile-call
75 -12 -13 [ [ 0 swap fixnum-fast ] bi@ ] compile-call
78 [ -1 2 ] [ 1 2 [ [ 0 swap fixnum- ] dip ] compile-call ] unit-test
81 -12 -13 [ [ 0 swap fixnum- ] bi@ ] compile-call
85 SBUF" " [ 1 slot 1 [ slot ] keep ] compile-call nip
89 [ 3 1 2 3 4 5 6 7 8 9 ] [
91 [ [ [ [ [ [ [ [ [ [ 3 ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ]
95 [ 2 2 2 2 2 2 2 2 2 2 1 ] [
97 [ swap [ dup dup dup dup dup dup dup dup dup ] dip ] compile-call
100 [ ] [ [ 9 [ ] times ] compile-call ] unit-test
104 [ 200 dup [ 200 3array ] curry map drop ] times
105 ] [ (( n -- )) define-temp ] with-compilation-unit drop
108 ! Test how dispatch handles the end of a basic block
109 : try-breaking-dispatch ( n a b -- x str )
110 float+ swap { [ "hey" ] [ "bye" ] } dispatch ;
112 : try-breaking-dispatch-2 ( -- ? )
113 1 1.0 2.5 try-breaking-dispatch "bye" = [ 3.5 = ] dip and ;
116 10000000 [ drop try-breaking-dispatch-2 ] all?
120 : (broken) ( x -- y ) ;
122 [ 2.0 { 2.0 0.0 } ] [
124 [ float/f 0.0 [ drop (broken) ] 2keep 2array ] compile-call
128 : hellish-bug-1 ( a b -- ) 2drop ;
130 : hellish-bug-2 ( i array x -- x )
131 2dup 1 slot eq? [ 2drop ] [
132 2dup array-nth tombstone? [
134 [ array-nth ] 2keep [ 1 fixnum+fast ] dip array-nth
135 pick 2dup hellish-bug-1 3drop
137 ] unless [ 2 fixnum+fast ] dip hellish-bug-2
138 ] if ; inline recursive
140 : hellish-bug-3 ( hash array -- )
141 0 swap hellish-bug-2 drop ;
144 H{ { 1 2 } { 3 4 } } dup array>>
145 [ 0 swap hellish-bug-2 drop ] compile-call
149 : foox ( obj -- obj )
151 [ drop 3 ] [ dup tuple? [ drop 4 ] [ drop 5 ] if ] if ;
153 [ 3 ] [ f foox ] unit-test
157 [ 4 ] [ T{ my-tuple } foox ] unit-test
159 [ 5 ] [ "hi" foox ] unit-test
161 ! Making sure we don't needlessly unbox/rebox
162 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ [ eq? ] dip ] compile-call ] unit-test
164 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ ] compile-call [ eq? ] dip ] unit-test
166 [ t ] [ 1.0 dup [ [ 2.0 float+ ] keep ] compile-call nip eq? ] unit-test
170 { byte-array } declare
171 [ 0 alien-unsigned-1 ] keep
178 [ 0 alien-unsigned-1 ] keep hi-tag
179 ] compile-call byte-array type-number =
186 ] compile-call alien type-number =
191 [ 2dup fixnum< [ [ die ] dip ] when ] compile-call
195 : a-dummy ( a -- ) drop "hi" print ;
199 dup 0 2 3dup pick >= [ >= ] [ 2drop f ] if [
205 ] [ 2drop no-case ] if
210 : dispatch-alignment-regression ( -- c )
211 { tuple vector } 3 slot { word } declare
212 dup 1 slot 0 fixnum-bitand { [ ] } dispatch ;
214 [ t ] [ \ dispatch-alignment-regression optimized? ] unit-test
216 [ vector ] [ dispatch-alignment-regression ] unit-test
219 : bad-value-bug ( a -- b ) [ 3 ] [ 3 ] if f <array> ;
221 [ { f f f } ] [ t bad-value-bug ] unit-test
226 : (gc-check-bug) ( a b -- c )
227 { [ id boa ] [ id boa ] } dispatch ;
229 : gc-check-bug ( -- )
230 10000000 [ "hi" 0 (gc-check-bug) drop ] times ;
232 [ ] [ gc-check-bug ] unit-test
235 : test-1 ( a -- b ) 8 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
237 [ "a" ] [ 8 test-1 ] unit-test
238 [ "b" ] [ 9 test-1 ] unit-test
240 : test-2 ( a -- b ) 1 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
242 [ "a" ] [ 1 test-2 ] unit-test
243 [ "b" ] [ 2 test-2 ] unit-test
245 ! I accidentally fixnum/i-fast on PowerPC
246 [ { { 1 2 } { 3 4 } } ] [
249 [ { array } declare 2 <groups> [ , ] each ] compile-call
255 [ { array } declare 2 <groups> length ] compile-call
258 ! Oops with new intrinsics
259 : fixnum-overflow-control-flow-test ( a b -- c )
260 [ 1 fixnum- ] [ 2 fixnum- ] if 3 fixnum+fast ;
262 [ 3 ] [ 1 t fixnum-overflow-control-flow-test ] unit-test
263 [ 2 ] [ 1 f fixnum-overflow-control-flow-test ] unit-test
267 { float } declare dup 0 =
270 [ 2 "double" "libm" "pow" { "double" "double" } alien-invoke ]
271 [ -0.5 "double" "libm" "pow" { "double" "double" } alien-invoke ]
275 [ 4.0 ] [ 2.0 blah ] unit-test
277 [ 4 ] [ 2 [ dup fixnum* ] compile-call ] unit-test
278 [ 7 ] [ 2 [ dup fixnum* 3 fixnum+fast ] compile-call ] unit-test
282 M: cucumber equal? "The cucumber has no equal" throw ;
284 [ t ] [ [ cucumber ] compile-call cucumber eq? ] unit-test
286 [ 4294967295 B{ 255 255 255 255 } -1 ]
289 [ [ 0 alien-unsigned-4 swap ] [ 0 alien-signed-2 ] bi ]
293 ! Regression found while working on global register allocation
295 : linear-scan-regression-1 ( a b c -- ) 3array , ;
296 : linear-scan-regression-2 ( a b -- ) 2array , ;
298 : linear-scan-regression ( a b c -- )
299 [ linear-scan-regression-2 ]
300 [ linear-scan-regression-1 ]
301 bi-curry bi-curry interleave ;
312 [ { 1 2 3 } "x" "y" linear-scan-regression ] { } make
315 ! Regression from Doug's value numbering changes
316 [ t ] [ 2 [ 1 swap fixnum< ] compile-call ] unit-test
317 [ 3 ] [ 2 [ 1 swap fixnum< [ 3 ] [ 4 ] if ] compile-call ] unit-test
320 [ 0 ] [ 101 [ dup fixnum-fast 1 fixnum+fast 20 fixnum-shift-fast 20 fixnum-shift-fast ] compile-call ] unit-test
323 ! Regression from Slava's value numbering changes
324 [ 1 ] [ 31337 [ dup fixnum<= [ 1 ] [ 2 ] if ] compile-call ] unit-test
326 ! Bug with ##return node construction
327 : return-recursive-bug ( nodes -- ? )
329 dup 3 bitand 1 = [ drop t ] [
334 ] any? ; inline recursive
336 [ t ] [ 3 [ return-recursive-bug ] compile-call ] unit-test
338 ! Coalescing reductions
339 [ f ] [ V{ } 0 [ [ vector? ] both? ] compile-call ] unit-test
340 [ f ] [ 0 V{ } [ [ vector? ] both? ] compile-call ] unit-test
344 [ dup [ \ vector eq? ] [ drop f ] if ] dip
345 dup [ \ vector eq? ] [ drop f ] if
346 over rot [ drop ] [ nip ] if