]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tests/codegen.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / compiler / tests / codegen.factor
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 ;
8 FROM: math => float ;
9 QUALIFIED: namespaces.private
10 IN: compiler.tests.codegen
11
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.
15
16 ! Oops!
17 [ 5000 ] [ [ 5000 ] compile-call ] unit-test
18 [ "hi" ] [ [ "hi" ] compile-call ] unit-test
19
20 [ 1 2 3 4 ] [ [ 1 2 3 4 ] compile-call ] unit-test
21
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
25
26 [ 2 3 ] [ 3 [ 2 swap ] compile-call ] unit-test
27
28 [ 2 1 3 4 ] [ 1 2 [ swap 3 4 ] compile-call ] unit-test
29
30 [ 2 3 4 ] [ 3 [ 2 swap 4 ] compile-call ] unit-test
31
32 [ { 1 2 3 } { 1 4 3 } 2 2 ]
33 [ { 1 2 3 } { 1 4 3 } [ over tag over tag ] compile-call ]
34 unit-test
35
36 ! Test literals in either side of a shuffle
37 [ 4 1 ] [ 1 [ [ 3 fixnum+ ] keep ] compile-call ] unit-test
38
39 [ 2 ] [ 1 2 [ swap fixnum/i ] compile-call ] unit-test
40
41 : foo ( -- ) ;
42
43 [ 3 3 ]
44 [ 1.2 [ tag [ foo ] keep ] compile-call ]
45 unit-test
46
47 [ 1 2 2 ]
48 [ { 1 2 } [ dup 2 slot swap 3 slot [ foo ] keep ] compile-call ]
49 unit-test
50
51 [ 3 ]
52 [
53     global [ 3 \ foo set ] bind
54     \ foo [ global >n get namespaces.private:ndrop ] compile-call
55 ] unit-test
56
57 : blech ( x -- ) drop ;
58
59 [ 3 ]
60 [
61     global [ 3 \ foo set ] bind
62     \ foo [ global [ get ] swap blech call ] compile-call
63 ] unit-test
64
65 [ 3 ]
66 [
67     global [ 3 \ foo set ] bind
68     \ foo [ global [ get ] swap >n call namespaces.private:ndrop ] compile-call
69 ] unit-test
70
71 [ 3 ]
72 [
73     global [ 3 \ foo set ] bind
74     \ foo [ global [ get ] bind ] compile-call
75 ] unit-test
76
77 [ 12 13 ] [
78     -12 -13 [ [ 0 swap fixnum-fast ] bi@ ] compile-call
79 ] unit-test
80
81 [ -1 2 ] [ 1 2 [ [ 0 swap fixnum- ] dip ] compile-call ] unit-test
82
83 [ 12 13 ] [
84     -12 -13 [ [ 0 swap fixnum- ] bi@ ] compile-call
85 ] unit-test
86
87 [ 1 ] [
88     SBUF" " [ 1 slot 1 [ slot ] keep ] compile-call nip
89 ] unit-test
90
91 ! Test slow shuffles
92 [ 3 1 2 3 4 5 6 7 8 9 ] [
93     1 2 3 4 5 6 7 8 9
94     [ [ [ [ [ [ [ [ [ [ 3 ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ]
95     compile-call
96 ] unit-test
97
98 [ 2 2 2 2 2 2 2 2 2 2 1 ] [
99     1 2
100     [ swap [ dup dup dup dup dup dup dup dup dup ] dip ] compile-call
101 ] unit-test
102
103 [ ] [ [ 9 [ ] times ] compile-call ] unit-test
104
105 [ ] [
106     [
107         [ 200 dup [ 200 3array ] curry map drop ] times
108     ] [ (( n -- )) define-temp ] with-compilation-unit drop
109 ] unit-test
110
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 ;
114
115 : try-breaking-dispatch-2 ( -- ? )
116     1 1.0 2.5 try-breaking-dispatch "bye" = [ 3.5 = ] dip and ;
117
118 [ t ] [
119     10000000 [ drop try-breaking-dispatch-2 ] all-integers?
120 ] unit-test
121
122 ! Regression
123 : (broken) ( x -- y ) ;
124
125 [ 2.0 { 2.0 0.0 } ] [
126     2.0 1.0
127     [ float/f 0.0 [ drop (broken) ] 2keep 2array ] compile-call
128 ] unit-test
129
130 ! Regression
131 : hellish-bug-1 ( a b -- ) 2drop ;
132
133 : hellish-bug-2 ( i array x -- x ) 
134     2dup 1 slot eq? [ 2drop ] [ 
135         2dup array-nth tombstone? [ 
136             [
137                 [ array-nth ] 2keep [ 1 fixnum+fast ] dip array-nth
138                 pick 2dup hellish-bug-1 3drop
139             ] 2keep
140         ] unless [ 2 fixnum+fast ] dip hellish-bug-2
141     ] if ; inline recursive
142
143 : hellish-bug-3 ( hash array -- ) 
144     0 swap hellish-bug-2 drop ;
145
146 [ ] [
147     H{ { 1 2 } { 3 4 } } dup array>>
148     [ 0 swap hellish-bug-2 drop ] compile-call
149 ] unit-test
150
151 ! Regression
152 : foox ( obj -- obj )
153     dup not
154     [ drop 3 ] [ dup tuple? [ drop 4 ] [ drop 5 ] if ] if ;
155
156 [ 3 ] [ f foox ] unit-test
157
158 TUPLE: my-tuple ;
159
160 [ 4 ] [ T{ my-tuple } foox ] unit-test
161
162 [ 5 ] [ "hi" foox ] unit-test
163
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
166
167 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ ] compile-call [ eq? ] dip ] unit-test
168
169 [ t ] [ 1.0 dup [ [ 2.0 float+ ] keep ] compile-call nip eq? ] unit-test
170
171 [ 1 B{ 1 2 3 4 } ] [
172     B{ 1 2 3 4 } [
173         { byte-array } declare
174         [ 0 alien-unsigned-1 ] keep
175     ] compile-call
176 ] unit-test
177
178 [ 2 1 ] [
179     2 1
180     [ 2dup fixnum< [ [ die ] dip ] when ] compile-call
181 ] unit-test
182
183 ! Regression
184 : a-dummy ( a -- ) drop "hi" print ;
185
186 [ ] [
187     1 [
188         dup 0 2 3dup pick >= [ >= ] [ 2drop f ] if [
189             drop - >fixnum {
190                 [ a-dummy ]
191                 [ a-dummy ]
192                 [ a-dummy ]
193             } dispatch
194         ] [ 2drop no-case ] if
195     ] compile-call
196 ] unit-test
197
198 ! Regression
199 : dispatch-alignment-regression ( -- c )
200     { tuple vector } 3 slot { word } declare
201     dup 1 slot 0 fixnum-bitand { [ ] } dispatch ;
202
203 [ t ] [ \ dispatch-alignment-regression optimized? ] unit-test
204
205 [ vector ] [ dispatch-alignment-regression ] unit-test
206
207 ! Regression
208 : bad-value-bug ( a -- b ) [ 3 ] [ 3 ] if f <array> ;
209
210 [ { f f f } ] [ t bad-value-bug ] unit-test
211
212 ! PowerPC regression
213 TUPLE: id obj ;
214
215 : (gc-check-bug) ( a b -- c )
216     { [ id boa ] [ id boa ] } dispatch ;
217
218 : gc-check-bug ( -- )
219     10000000 [ "hi" 0 (gc-check-bug) drop ] times ;
220
221 [ ] [ gc-check-bug ] unit-test
222
223 ! New optimization
224 : test-1 ( a -- b ) 8 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
225
226 [ "a" ] [ 8 test-1 ] unit-test
227 [ "b" ] [ 9 test-1 ] unit-test
228
229 : test-2 ( a -- b ) 1 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
230
231 [ "a" ] [ 1 test-2 ] unit-test
232 [ "b" ] [ 2 test-2 ] unit-test
233
234 ! I accidentally fixnum/i-fast on PowerPC
235 [ { { 1 2 } { 3 4 } } ] [
236     { 1 2 3 4 }
237     [
238         [ { array } declare 2 <groups> [ , ] each ] compile-call
239     ] { } make
240 ] unit-test
241
242 [ 2 ] [
243     { 1 2 3 4 }
244     [ { array } declare 2 <groups> length ] compile-call
245 ] unit-test
246
247 ! Oops with new intrinsics
248 : fixnum-overflow-control-flow-test ( a b -- c )
249     [ 1 fixnum- ] [ 2 fixnum- ] if 3 fixnum+fast ;
250
251 [ 3 ] [ 1 t fixnum-overflow-control-flow-test ] unit-test
252 [ 2 ] [ 1 f fixnum-overflow-control-flow-test ] unit-test
253
254 ! LOL
255 : blah ( a -- b )
256     { float } declare dup 0 =
257     [ drop 1 ] [
258         dup 0 >=
259         [ 2 double "libm" "pow" { double double } alien-invoke ]
260         [ -0.5 double "libm" "pow" { double double } alien-invoke ]
261         if
262     ] if ;
263
264 [ 4.0 ] [ 2.0 blah ] unit-test
265
266 [ 4 ] [ 2 [ dup fixnum* ] compile-call ] unit-test
267 [ 7 ] [ 2 [ dup fixnum* 3 fixnum+fast ] compile-call ] unit-test
268
269 TUPLE: cucumber ;
270
271 M: cucumber equal? "The cucumber has no equal" throw ;
272
273 [ t ] [ [ cucumber ] compile-call cucumber eq? ] unit-test
274
275 [ 4294967295 B{ 255 255 255 255 } -1 ]
276 [
277     -1 <int> -1 <int>
278     [ [ 0 alien-unsigned-4 swap ] [ 0 alien-signed-2 ] bi ]
279     compile-call
280 ] unit-test
281
282 ! Regression found while working on global register allocation
283
284 : linear-scan-regression-1 ( a b c -- ) 3array , ;
285 : linear-scan-regression-2 ( a b -- ) 2array , ;
286
287 : linear-scan-regression ( a b c -- )
288     [ linear-scan-regression-2 ]
289     [ linear-scan-regression-1 ]
290     bi-curry bi-curry interleave ;
291
292 [
293     {
294         { 1 "x" "y" }
295         { "x" "y" }
296         { 2 "x" "y" }
297         { "x" "y" }
298         { 3 "x" "y" }
299     }
300 ] [
301     [ { 1 2 3 } "x" "y" linear-scan-regression ] { } make
302 ] unit-test
303
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
307
308 cell 4 = [
309     [ 0 ] [ 101 [ dup fixnum-fast 1 fixnum+fast 20 fixnum-shift-fast 20 fixnum-shift-fast ] compile-call ] unit-test
310 ] when
311
312 ! Regression from Slava's value numbering changes
313 [ 1 ] [ 31337 [ dup fixnum<= [ 1 ] [ 2 ] if ] compile-call ] unit-test
314
315 ! Bug with ##return node construction
316 : return-recursive-bug ( nodes -- ? )
317     { fixnum } declare iota [
318         dup 3 bitand 1 = [ drop t ] [
319             dup 3 bitand 2 = [
320                 return-recursive-bug
321             ] [ drop f ] if
322         ] if
323     ] any? ; inline recursive
324
325 [ t ] [ 3 [ return-recursive-bug ] compile-call ] unit-test
326
327 ! Coalescing reductions
328 [ f ] [ V{ } 0 [ [ vector? ] both? ] compile-call ] unit-test
329 [ f ] [ 0 V{ } [ [ vector? ] both? ] compile-call ] unit-test
330
331 [ f ] [
332     f vector [
333         [ dup [ \ vector eq? ] [ drop f ] if ] dip
334         dup [ \ vector eq? ] [ drop f ] if
335         over rot [ drop ] [ nip ] if
336     ] compile-call
337 ] unit-test
338
339 ! Coalesing bug reduced from sequence-parser:take-sequence
340 : coalescing-bug-1 ( a b c d -- a b c d )
341     3dup {
342         [ 2drop 0 < ]
343         [ [ drop ] 2dip length > ]
344         [ drop > ]
345     } 3|| [ 3drop f ] [ slice boa ] if swap [ [ length ] bi@ ] 2keep ;
346
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
351
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 ;
356
357 [ 10 ] [ 1 coalescing-bug-2 ] unit-test
358 [ 86 ] [ 11 coalescing-bug-2 ] unit-test
359
360 ! Regression in suffix-arrays code
361 : coalescing-bug-3 ( from/f to/f seq -- slice )
362     [
363         [ drop 0 or ] [ length or ] bi-curry bi*
364         [ min ] keep
365     ] keep <slice> ;
366
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
373
374 ! Reduction
375 : coalescing-bug-4 ( a b c -- a b c )
376      [ [ min ] keep ] dip vector? [ 1 ] [ 2 ] if ;
377
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
384  
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 ;
390
391 [ 2 t ] [ 0 t global-dcn-bug-1 ] unit-test
392 [ 1 f ] [ 0 f global-dcn-bug-1 ] unit-test
393
394 ! Forgot a GC check
395 : missing-gc-check-1 ( a -- b ) { fixnum } declare <alien> ;
396 : missing-gc-check-2 ( -- ) 10000000 [ missing-gc-check-1 drop ] each-integer ;
397
398 [ ] [ missing-gc-check-2 ] unit-test
399
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
404
405 [ 6.0 ] [ 1.0 [ >float 3.0 + [ B{ 0 0 0 0 } 0 set-alien-float ] [ 2.0 + ] bi ] compile-call ] unit-test
406
407 ! Bug in linearization
408 [ 283686952174081 ] [
409     B{ 1 1 1 1 } [
410         { byte-array } declare
411         [ 0 2 ] dip
412         [
413             [ drop ] 2dip
414             [
415                 swap 1 < [ [ ] dip ] [ [ ] dip ] if
416                 0 alien-signed-4
417             ] curry dup bi *
418         ] curry each-integer
419     ] compile-call
420 ] unit-test
421
422 ! Bug in CSSA construction
423 TUPLE: myseq { underlying1 byte-array read-only } { underlying2 byte-array read-only } ;
424
425 [ 2 ] [
426     little-endian?
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 } } ?
429     [
430         { myseq } declare
431         [ 0 2 ] dip dup
432         [
433             [
434                 over 1 < [ underlying1>> ] [ [ 1 - ] dip underlying2>> ] if
435                 swap 4 * >fixnum alien-signed-4
436             ] bi-curry@ bi * +
437         ] 2curry each-integer
438     ] compile-call
439 ] unit-test
440
441 ! Bug in linear scan's partial sync point logic
442 [ t ] [
443     [ 1.0 100 [ fsin ] times 1.0 float+ ] compile-call
444     1.168852488727981 1.e-9 ~
445 ] unit-test
446
447 [ 65537.0 ] [
448     [ 2.0 4 [ 2.0 fpow ] times 1.0 float+ ] compile-call
449 ] unit-test
450
451 ! ##box-displaced-alien is a def-is-use instruction
452 [ ALIEN: 3e9 ] [
453     [
454         f
455         100 [ 10 swap <displaced-alien> ] times
456         1 swap <displaced-alien>
457     ] compile-call
458 ] unit-test
459
460 ! Forgot to two-operand shifts
461 [ 2 0 ] [
462     1 1
463     [ [ HEX: f bitand ] bi@ [ shift ] [ drop -3 shift ] 2bi ] compile-call
464 ] unit-test