]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tests/codegen.factor
4c4e8de94dd6c78bf1e705467ce5a67b6c97c584
[factor.git] / basis / compiler / tests / codegen.factor
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 combinators.short-circuit math.order
7 math.libm math.parser math.functions alien.syntax memory
8 stack-checker ;
9 FROM: math => float ;
10 QUALIFIED: namespaces.private
11 IN: compiler.tests.codegen
12
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.
16
17 ! Oops!
18 [ 5000 ] [ [ 5000 ] compile-call ] unit-test
19 [ "hi" ] [ [ "hi" ] compile-call ] unit-test
20
21 [ 1 2 3 4 ] [ [ 1 2 3 4 ] compile-call ] unit-test
22
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
26
27 [ 2 3 ] [ 3 [ 2 swap ] compile-call ] unit-test
28
29 [ 2 1 3 4 ] [ 1 2 [ swap 3 4 ] compile-call ] unit-test
30
31 [ 2 3 4 ] [ 3 [ 2 swap 4 ] compile-call ] unit-test
32
33 [ { 1 2 3 } { 1 4 3 } 2 2 ]
34 [ { 1 2 3 } { 1 4 3 } [ over tag over tag ] compile-call ]
35 unit-test
36
37 ! Test literals in either side of a shuffle
38 [ 4 1 ] [ 1 [ [ 3 fixnum+ ] keep ] compile-call ] unit-test
39
40 [ 2 ] [ 1 2 [ swap fixnum/i ] compile-call ] unit-test
41
42 : foo ( -- ) ;
43
44 [ 3 3 ]
45 [ 1.2 [ tag [ foo ] keep ] compile-call ]
46 unit-test
47
48 [ 1 2 2 ]
49 [ { 1 2 } [ dup 2 slot swap 3 slot [ foo ] keep ] compile-call ]
50 unit-test
51
52 [ 3 ]
53 [
54     global [ 3 \ foo set ] bind
55     \ foo [ global >n get namespaces.private:ndrop ] compile-call
56 ] unit-test
57
58 : blech ( x -- ) drop ;
59
60 [ 3 ]
61 [
62     global [ 3 \ foo set ] bind
63     \ foo [ global [ get ] swap blech call ] compile-call
64 ] unit-test
65
66 [ 3 ]
67 [
68     global [ 3 \ foo set ] bind
69     \ foo [ global [ get ] swap >n call namespaces.private:ndrop ] compile-call
70 ] unit-test
71
72 [ 3 ]
73 [
74     global [ 3 \ foo set ] bind
75     \ foo [ global [ get ] bind ] compile-call
76 ] unit-test
77
78 [ 12 13 ] [
79     -12 -13 [ [ 0 swap fixnum-fast ] bi@ ] compile-call
80 ] unit-test
81
82 [ -1 2 ] [ 1 2 [ [ 0 swap fixnum- ] dip ] compile-call ] unit-test
83
84 [ 12 13 ] [
85     -12 -13 [ [ 0 swap fixnum- ] bi@ ] compile-call
86 ] unit-test
87
88 [ 1 ] [
89     SBUF" " [ 1 slot 1 [ slot ] keep ] compile-call nip
90 ] unit-test
91
92 ! Test slow shuffles
93 [ 3 1 2 3 4 5 6 7 8 9 ] [
94     1 2 3 4 5 6 7 8 9
95     [ [ [ [ [ [ [ [ [ [ 3 ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ]
96     compile-call
97 ] unit-test
98
99 [ 2 2 2 2 2 2 2 2 2 2 1 ] [
100     1 2
101     [ swap [ dup dup dup dup dup dup dup dup dup ] dip ] compile-call
102 ] unit-test
103
104 [ ] [ [ 9 [ ] times ] compile-call ] unit-test
105
106 [ ] [
107     [
108         [ 200 dup [ 200 3array ] curry map drop ] times
109     ] [ (( n -- )) define-temp ] with-compilation-unit drop
110 ] unit-test
111
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 ;
115
116 : try-breaking-dispatch-2 ( -- ? )
117     1 1.0 2.5 try-breaking-dispatch "bye" = [ 3.5 = ] dip and ;
118
119 [ t ] [
120     10000000 [ drop try-breaking-dispatch-2 ] all-integers?
121 ] unit-test
122
123 ! Regression
124 : (broken) ( x -- y ) ;
125
126 [ 2.0 { 2.0 0.0 } ] [
127     2.0 1.0
128     [ float/f 0.0 [ drop (broken) ] 2keep 2array ] compile-call
129 ] unit-test
130
131 ! Regression
132 : hellish-bug-1 ( a b -- ) 2drop ;
133
134 : hellish-bug-2 ( i array x -- x ) 
135     2dup 1 slot eq? [ 2drop ] [ 
136         2dup array-nth tombstone? [ 
137             [
138                 [ array-nth ] 2keep [ 1 fixnum+fast ] dip array-nth
139                 pick 2dup hellish-bug-1 3drop
140             ] 2keep
141         ] unless [ 2 fixnum+fast ] dip hellish-bug-2
142     ] if ; inline recursive
143
144 : hellish-bug-3 ( hash array -- ) 
145     0 swap hellish-bug-2 drop ;
146
147 [ ] [
148     H{ { 1 2 } { 3 4 } } dup array>>
149     [ 0 swap hellish-bug-2 drop ] compile-call
150 ] unit-test
151
152 ! Regression
153 : foox ( obj -- obj )
154     dup not
155     [ drop 3 ] [ dup tuple? [ drop 4 ] [ drop 5 ] if ] if ;
156
157 [ 3 ] [ f foox ] unit-test
158
159 TUPLE: my-tuple ;
160
161 [ 4 ] [ T{ my-tuple } foox ] unit-test
162
163 [ 5 ] [ "hi" foox ] unit-test
164
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
167
168 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ ] compile-call [ eq? ] dip ] unit-test
169
170 [ t ] [ 1.0 dup [ [ 2.0 float+ ] keep ] compile-call nip eq? ] unit-test
171
172 [ 1 B{ 1 2 3 4 } ] [
173     B{ 1 2 3 4 } [
174         { byte-array } declare
175         [ 0 alien-unsigned-1 ] keep
176     ] compile-call
177 ] unit-test
178
179 [ 2 1 ] [
180     2 1
181     [ 2dup fixnum< [ [ die ] dip ] when ] compile-call
182 ] unit-test
183
184 ! Regression
185 : a-dummy ( a -- ) drop "hi" print ;
186
187 [ ] [
188     1 [
189         dup 0 2 3dup pick >= [ >= ] [ 2drop f ] if [
190             drop - >fixnum {
191                 [ a-dummy ]
192                 [ a-dummy ]
193                 [ a-dummy ]
194             } dispatch
195         ] [ 2drop no-case ] if
196     ] compile-call
197 ] unit-test
198
199 ! Regression
200 : dispatch-alignment-regression ( -- c )
201     { tuple vector } 3 slot { word } declare
202     dup 1 slot 0 fixnum-bitand { [ ] } dispatch ;
203
204 [ t ] [ \ dispatch-alignment-regression optimized? ] unit-test
205
206 [ vector ] [ dispatch-alignment-regression ] unit-test
207
208 ! Regression
209 : bad-value-bug ( a -- b ) [ 3 ] [ 3 ] if f <array> ;
210
211 [ { f f f } ] [ t bad-value-bug ] unit-test
212
213 ! PowerPC regression
214 TUPLE: id obj ;
215
216 : (gc-check-bug) ( a b -- c )
217     { [ id boa ] [ id boa ] } dispatch ;
218
219 : gc-check-bug ( -- )
220     10000000 [ "hi" 0 (gc-check-bug) drop ] times ;
221
222 [ ] [ gc-check-bug ] unit-test
223
224 ! New optimization
225 : test-1 ( a -- b ) 8 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
226
227 [ "a" ] [ 8 test-1 ] unit-test
228 [ "b" ] [ 9 test-1 ] unit-test
229
230 : test-2 ( a -- b ) 1 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
231
232 [ "a" ] [ 1 test-2 ] unit-test
233 [ "b" ] [ 2 test-2 ] unit-test
234
235 ! I accidentally fixnum/i-fast on PowerPC
236 [ { { 1 2 } { 3 4 } } ] [
237     { 1 2 3 4 }
238     [
239         [ { array } declare 2 <groups> [ , ] each ] compile-call
240     ] { } make
241 ] unit-test
242
243 [ 2 ] [
244     { 1 2 3 4 }
245     [ { array } declare 2 <groups> length ] compile-call
246 ] unit-test
247
248 ! Oops with new intrinsics
249 : fixnum-overflow-control-flow-test ( a b -- c )
250     [ 1 fixnum- ] [ 2 fixnum- ] if 3 fixnum+fast ;
251
252 [ 3 ] [ 1 t fixnum-overflow-control-flow-test ] unit-test
253 [ 2 ] [ 1 f fixnum-overflow-control-flow-test ] unit-test
254
255 ! LOL
256 : blah ( a -- b )
257     { float } declare dup 0 =
258     [ drop 1 ] [
259         dup 0 >=
260         [ 2 double "libm" "pow" { double double } alien-invoke ]
261         [ -0.5 double "libm" "pow" { double double } alien-invoke ]
262         if
263     ] if ;
264
265 [ 4.0 ] [ 2.0 blah ] unit-test
266
267 [ 4 ] [ 2 [ dup fixnum* ] compile-call ] unit-test
268 [ 7 ] [ 2 [ dup fixnum* 3 fixnum+fast ] compile-call ] unit-test
269
270 TUPLE: cucumber ;
271
272 M: cucumber equal? "The cucumber has no equal" throw ;
273
274 [ t ] [ [ cucumber ] compile-call cucumber eq? ] unit-test
275
276 [ 4294967295 B{ 255 255 255 255 } -1 ]
277 [
278     -1 <int> -1 <int>
279     [ [ 0 alien-unsigned-4 swap ] [ 0 alien-signed-2 ] bi ]
280     compile-call
281 ] unit-test
282
283 ! Regression found while working on global register allocation
284
285 : linear-scan-regression-1 ( a b c -- ) 3array , ;
286 : linear-scan-regression-2 ( a b -- ) 2array , ;
287
288 : linear-scan-regression ( a b c -- )
289     [ linear-scan-regression-2 ]
290     [ linear-scan-regression-1 ]
291     bi-curry bi-curry interleave ;
292
293 [
294     {
295         { 1 "x" "y" }
296         { "x" "y" }
297         { 2 "x" "y" }
298         { "x" "y" }
299         { 3 "x" "y" }
300     }
301 ] [
302     [ { 1 2 3 } "x" "y" linear-scan-regression ] { } make
303 ] unit-test
304
305 ! Regression from Doug's value numbering changes
306 [ t ] [ 2 [ 1 swap fixnum< ] compile-call ] unit-test
307 [ 3 ] [ 2 [ 1 swap fixnum< [ 3 ] [ 4 ] if ] compile-call ] unit-test
308
309 cell 4 = [
310     [ 0 ] [ 101 [ dup fixnum-fast 1 fixnum+fast 20 fixnum-shift-fast 20 fixnum-shift-fast ] compile-call ] unit-test
311 ] when
312
313 ! Regression from Slava's value numbering changes
314 [ 1 ] [ 31337 [ dup fixnum<= [ 1 ] [ 2 ] if ] compile-call ] unit-test
315
316 ! Bug with ##return node construction
317 : return-recursive-bug ( nodes -- ? )
318     { fixnum } declare iota [
319         dup 3 bitand 1 = [ drop t ] [
320             dup 3 bitand 2 = [
321                 return-recursive-bug
322             ] [ drop f ] if
323         ] if
324     ] any? ; inline recursive
325
326 [ t ] [ 3 [ return-recursive-bug ] compile-call ] unit-test
327
328 ! Coalescing reductions
329 [ f ] [ V{ } 0 [ [ vector? ] both? ] compile-call ] unit-test
330 [ f ] [ 0 V{ } [ [ vector? ] both? ] compile-call ] unit-test
331
332 [ f ] [
333     f vector [
334         [ dup [ \ vector eq? ] [ drop f ] if ] dip
335         dup [ \ vector eq? ] [ drop f ] if
336         over rot [ drop ] [ nip ] if
337     ] compile-call
338 ] unit-test
339
340 ! Coalesing bug reduced from sequence-parser:take-sequence
341 : coalescing-bug-1 ( a b c d -- a b c d )
342     3dup {
343         [ 2drop 0 < ]
344         [ [ drop ] 2dip length > ]
345         [ drop > ]
346     } 3|| [ 3drop f ] [ slice boa ] if swap [ [ length ] bi@ ] 2keep ;
347
348 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } -10 3 "hello" coalescing-bug-1 ] unit-test
349 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 0 7 "hello" coalescing-bug-1 ] unit-test
350 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 3 2 "hello" coalescing-bug-1 ] unit-test
351 [ 2 3 T{ slice f 1 3 "hello" } { 1 2 3 } ] [ { 1 2 3 } 1 3 "hello" coalescing-bug-1 ] unit-test
352
353 ! Another one, found by Dan
354 : coalescing-bug-2 ( a -- b )
355     dup dup 10 fixnum< [ 1 fixnum+fast ] when
356     fixnum+fast 2 fixnum*fast 2 fixnum-fast 2 fixnum*fast 2 fixnum+fast ;
357
358 [ 10 ] [ 1 coalescing-bug-2 ] unit-test
359 [ 86 ] [ 11 coalescing-bug-2 ] unit-test
360
361 ! Regression in suffix-arrays code
362 : coalescing-bug-3 ( from/f to/f seq -- slice )
363     [
364         [ drop 0 or ] [ length or ] bi-curry bi*
365         [ min ] keep
366     ] keep <slice> ;
367
368 [ T{ slice f 0 5 "hello" } ] [ f f "hello" coalescing-bug-3 ] unit-test
369 [ T{ slice f 1 5 "hello" } ] [ 1 f "hello" coalescing-bug-3 ] unit-test
370 [ T{ slice f 0 3 "hello" } ] [ f 3 "hello" coalescing-bug-3 ] unit-test
371 [ T{ slice f 1 3 "hello" } ] [ 1 3 "hello" coalescing-bug-3 ] unit-test
372 [ T{ slice f 3 3 "hello" } ] [ 4 3 "hello" coalescing-bug-3 ] unit-test
373 [ T{ slice f 5 5 "hello" } ] [ 6 f "hello" coalescing-bug-3 ] unit-test
374
375 ! Reduction
376 : coalescing-bug-4 ( a b c -- a b c )
377      [ [ min ] keep ] dip vector? [ 1 ] [ 2 ] if ;
378
379  [ 2 3 2 ] [ 2 3 "" coalescing-bug-4 ] unit-test
380  [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
381  [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
382  [ 2 3 1 ] [ 2 3 V{ } coalescing-bug-4 ] unit-test
383  [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
384  [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
385  
386 ! Global stack analysis dataflow equations are wrong
387 : some-word ( a -- b ) 2 + ;
388 : global-dcn-bug-1 ( a b -- c d )
389     dup [ [ drop 1 ] dip ] [ [ some-word ] dip ] if
390     dup [ [ 1 fixnum+fast ] dip ] [ [ drop 1 ] dip ] if ;
391
392 [ 2 t ] [ 0 t global-dcn-bug-1 ] unit-test
393 [ 1 f ] [ 0 f global-dcn-bug-1 ] unit-test
394
395 ! Forgot a GC check
396 : missing-gc-check-1 ( a -- b ) { fixnum } declare <alien> ;
397 : missing-gc-check-2 ( -- ) 10000000 [ missing-gc-check-1 drop ] each-integer ;
398
399 [ ] [ missing-gc-check-2 ] unit-test
400
401 [ 1 "0.169967142900241" ] [ 1.4 [ 1 swap fcos ] compile-call number>string ] unit-test
402 [ 1 "0.169967142900241" ] [ 1.4 1 [ swap fcos ] compile-call number>string ] unit-test
403 [ "0.169967142900241" "0.9854497299884601" ] [ 1.4 [ [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
404 [ 1 "0.169967142900241" "0.9854497299884601" ] [ 1.4 1 [ swap >float [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
405
406 [ 6.0 ] [ 1.0 [ >float 3.0 + [ B{ 0 0 0 0 } 0 set-alien-float ] [ 2.0 + ] bi ] compile-call ] unit-test
407
408 ! Bug in linearization
409 [ 283686952174081 ] [
410     B{ 1 1 1 1 } [
411         { byte-array } declare
412         [ 0 2 ] dip
413         [
414             [ drop ] 2dip
415             [
416                 swap 1 < [ [ ] dip ] [ [ ] dip ] if
417                 0 alien-signed-4
418             ] curry dup bi *
419         ] curry each-integer
420     ] compile-call
421 ] unit-test
422
423 ! Bug in CSSA construction
424 TUPLE: myseq { underlying1 byte-array read-only } { underlying2 byte-array read-only } ;
425
426 [ 2 ] [
427     little-endian?
428     T{ myseq f B{ 1 0 0 0 } B{ 1 0 0 0 } }
429     T{ myseq f B{ 0 0 0 1 } B{ 0 0 0 1 } } ?
430     [
431         { myseq } declare
432         [ 0 2 ] dip dup
433         [
434             [
435                 over 1 < [ underlying1>> ] [ [ 1 - ] dip underlying2>> ] if
436                 swap 4 * >fixnum alien-signed-4
437             ] bi-curry@ bi * +
438         ] 2curry each-integer
439     ] compile-call
440 ] unit-test
441
442 ! Bug in linear scan's partial sync point logic
443 [ t ] [
444     [ 1.0 100 [ fsin ] times 1.0 float+ ] compile-call
445     1.168852488727981 1.e-9 ~
446 ] unit-test
447
448 [ 65537.0 ] [
449     [ 2.0 4 [ 2.0 fpow ] times 1.0 float+ ] compile-call
450 ] unit-test
451
452 ! ##box-displaced-alien is a def-is-use instruction
453 [ ALIEN: 3e9 ] [
454     [
455         f
456         100 [ 10 swap <displaced-alien> ] times
457         1 swap <displaced-alien>
458     ] compile-call
459 ] unit-test
460
461 ! Forgot to two-operand shifts
462 [ 2 0 ] [
463     1 1
464     [ [ HEX: f bitand ] bi@ [ shift ] [ drop -3 shift ] 2bi ] compile-call
465 ] unit-test
466
467 ! Alias analysis bug
468 [ t ] [
469     [
470         10 10 <byte-array> [ <displaced-alien> underlying>> ] keep eq?
471     ] compile-call
472 ] unit-test
473
474 ! GC root offsets were computed wrong on x86
475 : gc-root-messup ( a -- b )
476     dup [
477         1024 (byte-array) 2array
478         10 void* "libc" "malloc" { ulong } alien-invoke
479         void "libc" "free" { void* } alien-invoke
480     ] when ;
481
482 [ ] [ 2000 [ "hello" clone dup gc-root-messup first eq? t assert= ] times ] unit-test
483
484 ! Write barrier elimination was being done before scheduling and
485 ! GC check insertion, and didn't take subroutine calls into
486 ! account. Oops...
487 : write-barrier-elim-in-wrong-place ( -- obj )
488     ! A callback used below
489     void { } cdecl [ compact-gc ] alien-callback
490     ! Allocate an object A in the nursery
491     1 f <array>
492     ! Subroutine call promotes the object to tenured
493     swap void { } cdecl alien-indirect
494     ! Allocate another object B in the nursery, store it into
495     ! the first
496     1 f <array> over set-first
497     ! Now object A's card should be marked and minor GC should
498     ! promote B to aging
499     minor-gc
500     ! Do stuff
501     [ 100 [ ] times ] infer.
502     ;
503
504 [ { { f } } ] [ write-barrier-elim-in-wrong-place ] unit-test
505
506 ! GC maps must support derived pointers
507 : (derived-pointer-test-1) ( -- byte-array )
508     2 <byte-array> ;
509
510 : derived-pointer-test-1 ( -- byte-array )
511     ! A callback used below
512     void { } cdecl [ compact-gc ] alien-callback
513     ! Put the construction in a word since instruction selection
514     ! eliminates the untagged pointer entirely if the value is a
515     ! byte array
516     (derived-pointer-test-1) { c-ptr } declare
517     ! Store into an array, an untagged pointer to the payload
518     ! is now an available expression
519     123 over 0 set-alien-unsigned-1
520     ! GC, moving the array and derived pointer
521     swap void { } cdecl alien-indirect
522     ! Store into the array again
523     231 over 1 set-alien-unsigned-1 ;
524
525 [ B{ 123 231 } ] [ derived-pointer-test-1 ] unit-test