]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tests/codegen.factor
Merge branch 'master' into strong-typing
[factor.git] / basis / compiler / tests / codegen.factor
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 ;
7 QUALIFIED: namespaces.private
8 IN: compiler.tests.codegen
9
10 ! Originally, this file did black box testing of templating
11 ! optimization. We now have a different codegen, but the tests
12 ! in here are still useful.
13
14 ! Oops!
15 [ 5000 ] [ [ 5000 ] compile-call ] unit-test
16 [ "hi" ] [ [ "hi" ] compile-call ] unit-test
17
18 [ 1 2 3 4 ] [ [ 1 2 3 4 ] compile-call ] unit-test
19
20 [ 1 1 ] [ 1 [ dup ] compile-call ] unit-test
21 [ 0 ] [ 3 [ tag ] compile-call ] unit-test
22 [ 0 3 ] [ 3 [ [ tag ] keep ] compile-call ] unit-test
23
24 [ 2 3 ] [ 3 [ 2 swap ] compile-call ] unit-test
25
26 [ 2 1 3 4 ] [ 1 2 [ swap 3 4 ] compile-call ] unit-test
27
28 [ 2 3 4 ] [ 3 [ 2 swap 4 ] compile-call ] unit-test
29
30 [ { 1 2 3 } { 1 4 3 } 2 2 ]
31 [ { 1 2 3 } { 1 4 3 } [ over tag over tag ] compile-call ]
32 unit-test
33
34 ! Test literals in either side of a shuffle
35 [ 4 1 ] [ 1 [ [ 3 fixnum+ ] keep ] compile-call ] unit-test
36
37 [ 2 ] [ 1 2 [ swap fixnum/i ] compile-call ] unit-test
38
39 : foo ( -- ) ;
40
41 [ 3 3 ]
42 [ 1.2 [ tag [ foo ] keep ] compile-call ]
43 unit-test
44
45 [ 1 2 2 ]
46 [ { 1 2 } [ dup 2 slot swap 3 slot [ foo ] keep ] compile-call ]
47 unit-test
48
49 [ 3 ]
50 [
51     global [ 3 \ foo set ] bind
52     \ foo [ global >n get namespaces.private:ndrop ] compile-call
53 ] unit-test
54
55 : blech ( x -- ) drop ;
56
57 [ 3 ]
58 [
59     global [ 3 \ foo set ] bind
60     \ foo [ global [ get ] swap blech call ] compile-call
61 ] unit-test
62
63 [ 3 ]
64 [
65     global [ 3 \ foo set ] bind
66     \ foo [ global [ get ] swap >n call namespaces.private:ndrop ] compile-call
67 ] unit-test
68
69 [ 3 ]
70 [
71     global [ 3 \ foo set ] bind
72     \ foo [ global [ get ] bind ] compile-call
73 ] unit-test
74
75 [ 12 13 ] [
76     -12 -13 [ [ 0 swap fixnum-fast ] bi@ ] compile-call
77 ] unit-test
78
79 [ -1 2 ] [ 1 2 [ [ 0 swap fixnum- ] dip ] compile-call ] unit-test
80
81 [ 12 13 ] [
82     -12 -13 [ [ 0 swap fixnum- ] bi@ ] compile-call
83 ] unit-test
84
85 [ 1 ] [
86     SBUF" " [ 1 slot 1 [ slot ] keep ] compile-call nip
87 ] unit-test
88
89 ! Test slow shuffles
90 [ 3 1 2 3 4 5 6 7 8 9 ] [
91     1 2 3 4 5 6 7 8 9
92     [ [ [ [ [ [ [ [ [ [ 3 ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ] dip ]
93     compile-call
94 ] unit-test
95
96 [ 2 2 2 2 2 2 2 2 2 2 1 ] [
97     1 2
98     [ swap [ dup dup dup dup dup dup dup dup dup ] dip ] compile-call
99 ] unit-test
100
101 [ ] [ [ 9 [ ] times ] compile-call ] unit-test
102
103 [ ] [
104     [
105         [ 200 dup [ 200 3array ] curry map drop ] times
106     ] [ (( n -- )) define-temp ] with-compilation-unit drop
107 ] unit-test
108
109 ! Test how dispatch handles the end of a basic block
110 : try-breaking-dispatch ( n a b -- x str )
111     float+ swap { [ "hey" ] [ "bye" ] } dispatch ;
112
113 : try-breaking-dispatch-2 ( -- ? )
114     1 1.0 2.5 try-breaking-dispatch "bye" = [ 3.5 = ] dip and ;
115
116 [ t ] [
117     10000000 [ drop try-breaking-dispatch-2 ] all?
118 ] unit-test
119
120 ! Regression
121 : (broken) ( x -- y ) ;
122
123 [ 2.0 { 2.0 0.0 } ] [
124     2.0 1.0
125     [ float/f 0.0 [ drop (broken) ] 2keep 2array ] compile-call
126 ] unit-test
127
128 ! Regression
129 : hellish-bug-1 ( a b -- ) 2drop ;
130
131 : hellish-bug-2 ( i array x -- x ) 
132     2dup 1 slot eq? [ 2drop ] [ 
133         2dup array-nth tombstone? [ 
134             [
135                 [ array-nth ] 2keep [ 1 fixnum+fast ] dip array-nth
136                 pick 2dup hellish-bug-1 3drop
137             ] 2keep
138         ] unless [ 2 fixnum+fast ] dip hellish-bug-2
139     ] if ; inline recursive
140
141 : hellish-bug-3 ( hash array -- ) 
142     0 swap hellish-bug-2 drop ;
143
144 [ ] [
145     H{ { 1 2 } { 3 4 } } dup array>>
146     [ 0 swap hellish-bug-2 drop ] compile-call
147 ] unit-test
148
149 ! Regression
150 : foox ( obj -- obj )
151     dup not
152     [ drop 3 ] [ dup tuple? [ drop 4 ] [ drop 5 ] if ] if ;
153
154 [ 3 ] [ f foox ] unit-test
155
156 TUPLE: my-tuple ;
157
158 [ 4 ] [ T{ my-tuple } foox ] unit-test
159
160 [ 5 ] [ "hi" foox ] unit-test
161
162 ! Making sure we don't needlessly unbox/rebox
163 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ [ eq? ] dip ] compile-call ] unit-test
164
165 [ t 3.0 ] [ 1.0 dup [ dup 2.0 float+ ] compile-call [ eq? ] dip ] unit-test
166
167 [ t ] [ 1.0 dup [ [ 2.0 float+ ] keep ] compile-call nip eq? ] unit-test
168
169 [ 1 B{ 1 2 3 4 } ] [
170     B{ 1 2 3 4 } [
171         { byte-array } declare
172         [ 0 alien-unsigned-1 ] keep
173     ] compile-call
174 ] unit-test
175
176 [ 1 t ] [
177     B{ 1 2 3 4 } [
178         { c-ptr } declare
179         [ 0 alien-unsigned-1 ] keep hi-tag
180     ] compile-call byte-array type-number =
181 ] unit-test
182
183 [ t ] [
184     B{ 1 2 3 4 } [
185         { c-ptr } declare
186         0 alien-cell hi-tag
187     ] compile-call alien type-number =
188 ] unit-test
189
190 [ 2 1 ] [
191     2 1
192     [ 2dup fixnum< [ [ die ] dip ] when ] compile-call
193 ] unit-test
194
195 ! Regression
196 : a-dummy ( a -- ) drop "hi" print ;
197
198 [ ] [
199     1 [
200         dup 0 2 3dup pick >= [ >= ] [ 2drop f ] if [
201             drop - >fixnum {
202                 [ a-dummy ]
203                 [ a-dummy ]
204                 [ a-dummy ]
205             } dispatch
206         ] [ 2drop no-case ] if
207     ] compile-call
208 ] unit-test
209
210 ! Regression
211 : dispatch-alignment-regression ( -- c )
212     { tuple vector } 3 slot { word } declare
213     dup 1 slot 0 fixnum-bitand { [ ] } dispatch ;
214
215 [ t ] [ \ dispatch-alignment-regression optimized? ] unit-test
216
217 [ vector ] [ dispatch-alignment-regression ] unit-test
218
219 ! Regression
220 : bad-value-bug ( a -- b ) [ 3 ] [ 3 ] if f <array> ;
221
222 [ { f f f } ] [ t bad-value-bug ] unit-test
223
224 ! PowerPC regression
225 TUPLE: id obj ;
226
227 : (gc-check-bug) ( a b -- c )
228     { [ id boa ] [ id boa ] } dispatch ;
229
230 : gc-check-bug ( -- )
231     10000000 [ "hi" 0 (gc-check-bug) drop ] times ;
232
233 [ ] [ gc-check-bug ] unit-test
234
235 ! New optimization
236 : test-1 ( a -- b ) 8 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
237
238 [ "a" ] [ 8 test-1 ] unit-test
239 [ "b" ] [ 9 test-1 ] unit-test
240
241 : test-2 ( a -- b ) 1 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
242
243 [ "a" ] [ 1 test-2 ] unit-test
244 [ "b" ] [ 2 test-2 ] unit-test
245
246 ! I accidentally fixnum/i-fast on PowerPC
247 [ { { 1 2 } { 3 4 } } ] [
248     { 1 2 3 4 }
249     [
250         [ { array } declare 2 <groups> [ , ] each ] compile-call
251     ] { } make
252 ] unit-test
253
254 [ 2 ] [
255     { 1 2 3 4 }
256     [ { array } declare 2 <groups> length ] compile-call
257 ] unit-test
258
259 ! Oops with new intrinsics
260 : fixnum-overflow-control-flow-test ( a b -- c )
261     [ 1 fixnum- ] [ 2 fixnum- ] if 3 fixnum+fast ;
262
263 [ 3 ] [ 1 t fixnum-overflow-control-flow-test ] unit-test
264 [ 2 ] [ 1 f fixnum-overflow-control-flow-test ] unit-test
265
266 ! LOL
267 : blah ( a -- b )
268     { float } declare dup 0 =
269     [ drop 1 ] [
270         dup 0 >=
271         [ 2 "double" "libm" "pow" { "double" "double" } alien-invoke ]
272         [ -0.5 "double" "libm" "pow" { "double" "double" } alien-invoke ]
273         if
274     ] if ;
275
276 [ 4.0 ] [ 2.0 blah ] unit-test
277
278 [ 4 ] [ 2 [ dup fixnum* ] compile-call ] unit-test
279 [ 7 ] [ 2 [ dup fixnum* 3 fixnum+fast ] compile-call ] unit-test
280
281 TUPLE: cucumber ;
282
283 M: cucumber equal? "The cucumber has no equal" throw ;
284
285 [ t ] [ [ cucumber ] compile-call cucumber eq? ] unit-test
286
287 [ 4294967295 B{ 255 255 255 255 } -1 ]
288 [
289     -1 <int> -1 <int>
290     [ [ 0 alien-unsigned-4 swap ] [ 0 alien-signed-2 ] bi ]
291     compile-call
292 ] unit-test
293
294 ! Regression found while working on global register allocation
295
296 : linear-scan-regression-1 ( a b c -- ) 3array , ;
297 : linear-scan-regression-2 ( a b -- ) 2array , ;
298
299 : linear-scan-regression ( a b c -- )
300     [ linear-scan-regression-2 ]
301     [ linear-scan-regression-1 ]
302     bi-curry bi-curry interleave ;
303
304 [
305     {
306         { 1 "x" "y" }
307         { "x" "y" }
308         { 2 "x" "y" }
309         { "x" "y" }
310         { 3 "x" "y" }
311     }
312 ] [
313     [ { 1 2 3 } "x" "y" linear-scan-regression ] { } make
314 ] unit-test
315
316 ! Regression from Doug's value numbering changes
317 [ t ] [ 2 [ 1 swap fixnum< ] compile-call ] unit-test
318 [ 3 ] [ 2 [ 1 swap fixnum< [ 3 ] [ 4 ] if ] compile-call ] unit-test
319
320 cell 4 = [
321     [ 0 ] [ 101 [ dup fixnum-fast 1 fixnum+fast 20 fixnum-shift-fast 20 fixnum-shift-fast ] compile-call ] unit-test
322 ] when
323
324 ! Regression from Slava's value numbering changes
325 [ 1 ] [ 31337 [ dup fixnum<= [ 1 ] [ 2 ] if ] compile-call ] unit-test
326
327 ! Bug with ##return node construction
328 : return-recursive-bug ( nodes -- ? )
329     { fixnum } declare [
330         dup 3 bitand 1 = [ drop t ] [
331             dup 3 bitand 2 = [
332                 return-recursive-bug
333             ] [ drop f ] if
334         ] if
335     ] any? ; inline recursive
336
337 [ t ] [ 3 [ return-recursive-bug ] compile-call ] unit-test
338
339 ! Coalescing reductions
340 [ f ] [ V{ } 0 [ [ vector? ] both? ] compile-call ] unit-test
341 [ f ] [ 0 V{ } [ [ vector? ] both? ] compile-call ] unit-test
342
343 [ f ] [
344     f vector [
345         [ dup [ \ vector eq? ] [ drop f ] if ] dip
346         dup [ \ vector eq? ] [ drop f ] if
347         over rot [ drop ] [ nip ] if
348     ] compile-call
349 ] unit-test
350
351 ! Coalesing bug reduced from sequence-parser:take-sequence
352 : coalescing-bug-1 ( a b c d -- a b c d )
353     3dup {
354         [ 2drop 0 < ]
355         [ [ drop ] 2dip length > ]
356         [ drop > ]
357     } 3|| [ 3drop f ] [ slice boa ] if swap [ [ length ] bi@ ] 2keep ;
358
359 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } -10 3 "hello" coalescing-bug-1 ] unit-test
360 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 0 7 "hello" coalescing-bug-1 ] unit-test
361 [ 0 3 f { 1 2 3 } ] [ { 1 2 3 } 3 2 "hello" coalescing-bug-1 ] unit-test
362 [ 2 3 T{ slice f 1 3 "hello" } { 1 2 3 } ] [ { 1 2 3 } 1 3 "hello" coalescing-bug-1 ] unit-test
363
364 ! Another one, found by Dan
365 : coalescing-bug-2 ( a -- b )
366     dup dup 10 fixnum< [ 1 fixnum+fast ] when
367     fixnum+fast 2 fixnum*fast 2 fixnum-fast 2 fixnum*fast 2 fixnum+fast ;
368
369 [ 10 ] [ 1 coalescing-bug-2 ] unit-test
370 [ 86 ] [ 11 coalescing-bug-2 ] unit-test
371
372 ! Regression in suffix-arrays code
373 : coalescing-bug-3 ( from/f to/f seq -- slice )
374     [
375         [ drop 0 or ] [ length or ] bi-curry bi*
376         [ min ] keep
377     ] keep <slice> ;
378
379 [ T{ slice f 0 5 "hello" } ] [ f f "hello" coalescing-bug-3 ] unit-test
380 [ T{ slice f 1 5 "hello" } ] [ 1 f "hello" coalescing-bug-3 ] unit-test
381 [ T{ slice f 0 3 "hello" } ] [ f 3 "hello" coalescing-bug-3 ] unit-test
382 [ T{ slice f 1 3 "hello" } ] [ 1 3 "hello" coalescing-bug-3 ] unit-test
383 [ T{ slice f 3 3 "hello" } ] [ 4 3 "hello" coalescing-bug-3 ] unit-test
384 [ T{ slice f 5 5 "hello" } ] [ 6 f "hello" coalescing-bug-3 ] unit-test
385
386 ! Reduction
387 : coalescing-bug-4 ( a b c -- a b c )
388      [ [ min ] keep ] dip vector? [ 1 ] [ 2 ] if ;
389
390  [ 2 3 2 ] [ 2 3 "" coalescing-bug-4 ] unit-test
391  [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
392  [ 3 3 2 ] [ 4 3 "" coalescing-bug-4 ] unit-test
393  [ 2 3 1 ] [ 2 3 V{ } coalescing-bug-4 ] unit-test
394  [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
395  [ 3 3 1 ] [ 4 3 V{ } coalescing-bug-4 ] unit-test
396  
397 ! Global stack analysis dataflow equations are wrong
398 : some-word ( a -- b ) 2 + ;
399 : global-dcn-bug-1 ( a b -- c d )
400     dup [ [ drop 1 ] dip ] [ [ some-word ] dip ] if
401     dup [ [ 1 fixnum+fast ] dip ] [ [ drop 1 ] dip ] if ;
402
403 [ 2 t ] [ 0 t global-dcn-bug-1 ] unit-test
404 [ 1 f ] [ 0 f global-dcn-bug-1 ] unit-test
405
406 ! Forgot a GC check
407 : missing-gc-check-1 ( a -- b ) { fixnum } declare <alien> ;
408 : missing-gc-check-2 ( -- ) 10000000 [ missing-gc-check-1 drop ] each-integer ;
409
410 [ ] [ missing-gc-check-2 ] unit-test
411
412 [ 1 "0.169967142900241" ] [ 1.4 [ 1 swap fcos ] compile-call number>string ] unit-test
413 [ 1 "0.169967142900241" ] [ 1.4 1 [ swap fcos ] compile-call number>string ] unit-test
414 [ "0.169967142900241" "0.9854497299884601" ] [ 1.4 [ [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
415 [ 1 "0.169967142900241" "0.9854497299884601" ] [ 1.4 1 [ swap >float [ fcos ] [ fsin ] bi ] compile-call [ number>string ] bi@ ] unit-test
416
417 [ 6.0 ] [ 1.0 [ >float 3.0 + [ B{ 0 0 0 0 } 0 set-alien-float ] [ 2.0 + ] bi ] compile-call ] unit-test