stack-allocatable fill-initialized specialized arrays
[sbcl.git] / tests / dynamic-extent.impure.lisp
1 ;;;; tests that dynamic-extent functionality works.
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
13
14 (when (eq sb-ext:*evaluator-mode* :interpret)
15   (sb-ext:quit :unix-status 104))
16
17 (load "compiler-test-util.lisp")
18 (use-package :ctu)
19
20 (setq sb-c::*check-consistency* t
21       sb-ext:*stack-allocate-dynamic-extent* t)
22
23 (defmacro defun-with-dx (name arglist &body body)
24   `(defun ,name ,arglist
25      ,@body))
26
27 (declaim (notinline opaque-identity))
28 (defun opaque-identity (x)
29   x)
30
31 ;;; &REST lists
32
33 (defun-with-dx dxlength (&rest rest)
34   (declare (dynamic-extent rest))
35   (length rest))
36
37 (with-test (:name (:dx-&rest :basics))
38   (assert (= (dxlength 1 2 3) 3))
39   (assert (= (dxlength t t t t t t) 6))
40   (assert (= (dxlength) 0)))
41
42 (defun callee (list)
43   (destructuring-bind (a b c d e f &rest g) list
44     (+ a b c d e f (length g))))
45
46 (defun-with-dx dxcaller (&rest rest)
47   (declare (dynamic-extent rest))
48   (callee rest))
49
50 (with-test (:name (:dx-&rest :pass-down-to-callee :tail-call))
51   (assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
52
53 (defun-with-dx dxcaller-align-1 (x &rest rest)
54   (declare (dynamic-extent rest))
55   (+ x (callee rest)))
56
57 (with-test (:name (:dx-&rest :pass-down-to-callee :non-tail-call))
58   (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
59   (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40)))
60
61 ;;; %NIP-VALUES
62
63 (defun-with-dx test-nip-values ()
64   (flet ((bar (x &rest y)
65            (declare (dynamic-extent y))
66            (if (> x 0)
67                (values x (length y))
68                (values (car y)))))
69     (multiple-value-call #'values
70       (bar 1 2 3 4 5 6)
71       (bar -1 'a 'b))))
72
73 (with-test (:name (:nip-values))
74   (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a))))
75
76 ;;; LET-variable substitution
77
78 (defun-with-dx test-let-var-subst1 (x)
79   (let ((y (list x (1- x))))
80     (opaque-identity :foo)
81     (let ((z (the list y)))
82       (declare (dynamic-extent z))
83       (length z))))
84
85 (with-test (:name (:let-variable-substitution))
86   (assert (eql (test-let-var-subst1 17) 2)))
87
88 (defun-with-dx test-let-var-subst2 (x)
89   (let ((y (list x (1- x))))
90     (declare (dynamic-extent y))
91     (opaque-identity :foo)
92     (let ((z (the list y)))
93       (length z))))
94
95 (with-test (:name (:let-variable-substitution-2))
96   (assert (eql (test-let-var-subst2 17) 2)))
97
98
99 ;;; DX propagation through LET-return.
100
101 (defun-with-dx test-lvar-subst (x)
102   (let ((y (list x (1- x))))
103     (declare (dynamic-extent y))
104     (second (let ((z (the list y)))
105               (opaque-identity :foo)
106               z))))
107
108 (with-test (:name (:dx-propagation-through-let-return))
109   (assert (eql (test-lvar-subst 11) 10)))
110
111 ;;; this code is incorrect, but the compiler should not fail
112 (defun-with-dx test-let-var-subst-incorrect (x)
113   (let ((y (list x (1- x))))
114     (opaque-identity :foo)
115     (let ((z (the list y)))
116       (declare (dynamic-extent z))
117       (opaque-identity :bar)
118       z)))
119 \f
120 ;;; alignment
121
122 (defvar *x*)
123 (defun-with-dx test-alignment-dx-list (form)
124   (multiple-value-prog1 (eval form)
125     (let ((l (list 1 2 3 4)))
126       (declare (dynamic-extent l))
127       (setq *x* (copy-list l)))))
128
129 (with-test (:name (:dx-list :alignment))
130   (dotimes (n 64)
131     (let* ((res (loop for i below n collect i))
132            (form `(values ,@res)))
133       (assert (equal (multiple-value-list (test-alignment-dx-list form)) res))
134       (assert (equal *x* '(1 2 3 4))))))
135
136 ;;; closure
137
138 (declaim (notinline true))
139 (defun true (x)
140   (declare (ignore x))
141   t)
142
143 (defun-with-dx dxclosure (x)
144   (flet ((f (y)
145            (+ y x)))
146     (declare (dynamic-extent #'f))
147     (true #'f)))
148
149 (with-test (:name (:dx-closure))
150   (assert (eq t (dxclosure 13))))
151
152 ;;; value-cells
153
154 (defun-with-dx dx-value-cell (x)
155   ;; Not implemented everywhere, yet.
156   #+(or x86 x86-64 mips hppa)
157   (let ((cell x))
158     (declare (sb-int:truly-dynamic-extent cell))
159     (flet ((f ()
160              (incf cell)))
161       (declare (dynamic-extent #'f))
162       (true #'f))))
163
164 ;;; CONS
165
166 (defun-with-dx cons-on-stack (x)
167   (let ((cons (cons x x)))
168     (declare (dynamic-extent cons))
169     (true cons)
170     nil))
171
172 ;;; MAKE-ARRAY
173
174 (defun force-make-array-on-stack (n)
175   (declare (optimize safety))
176   (let ((v (make-array (min n 1))))
177     (declare (sb-int:truly-dynamic-extent v))
178     (true v)
179     nil))
180
181 (defun-with-dx make-array-on-stack-1 ()
182   (let ((v (make-array '(42) :element-type 'single-float)))
183     (declare (dynamic-extent v))
184     (true v)
185     nil))
186
187 (defun-with-dx make-array-on-stack-2 (n x)
188   (declare (integer n))
189   (let ((v (make-array n :initial-contents x)))
190     (declare (sb-int:truly-dynamic-extent v))
191     (true v)
192     nil))
193
194 (defun-with-dx make-array-on-stack-3 (x y z)
195   (let ((v (make-array 3
196                        :element-type 'fixnum :initial-contents (list x y z)
197                        :element-type t :initial-contents x)))
198     (declare (sb-int:truly-dynamic-extent v))
199     (true v)
200     nil))
201
202 (defun-with-dx make-array-on-stack-4 ()
203   (let ((v (make-array 3 :initial-contents '(1 2 3))))
204     (declare (sb-int:truly-dynamic-extent v))
205     (true v)
206     nil))
207
208 (defun-with-dx make-array-on-stack-5 ()
209   (let ((v (make-array 3 :initial-element 12 :element-type t)))
210     (declare (sb-int:truly-dynamic-extent v))
211     (true v)
212     nil))
213
214 (defun-with-dx make-array-on-stack-6 ()
215   (let ((v (make-array 3 :initial-element 12 :element-type '(unsigned-byte 8))))
216     (declare (sb-int:truly-dynamic-extent v))
217     (true v)
218     nil))
219
220 (defun-with-dx make-array-on-stack-7 ()
221   (let ((v (make-array 3 :initial-element 12 :element-type '(signed-byte 8))))
222     (declare (sb-int:truly-dynamic-extent v))
223     (true v)
224     nil))
225
226 (defun-with-dx make-array-on-stack-8 ()
227   (let ((v (make-array 3 :initial-element 12 :element-type 'word)))
228     (declare (sb-int:truly-dynamic-extent v))
229     (true v)
230     nil))
231
232 (defun-with-dx make-array-on-stack-9 ()
233   (let ((v (make-array 3 :initial-element 12.0 :element-type 'single-float)))
234     (declare (sb-int:truly-dynamic-extent v))
235     (true v)
236     nil))
237
238 (defun-with-dx make-array-on-stack-10 ()
239   (let ((v (make-array 3 :initial-element 12.0d0 :element-type 'double-float)))
240     (declare (sb-int:truly-dynamic-extent v))
241     (true v)
242     nil))
243
244 (defun-with-dx vector-on-stack (x y)
245   (let ((v (vector 1 x 2 y 3)))
246     (declare (sb-int:truly-dynamic-extent v))
247     (true v)
248     nil))
249
250 ;;; MAKE-STRUCTURE
251
252 (declaim (inline make-fp-struct-1))
253 (defstruct fp-struct-1
254   (s 0.0 :type single-float)
255   (d 0.0d0 :type double-float))
256
257 (defun-with-dx test-fp-struct-1.1 (s d)
258   (let ((fp (make-fp-struct-1 :s s)))
259     (declare (dynamic-extent fp))
260     (assert (eql s (fp-struct-1-s fp)))
261     (assert (eql 0.0d0 (fp-struct-1-d fp)))))
262
263 (defun-with-dx test-fp-struct-1.2 (s d)
264   (let ((fp (make-fp-struct-1 :d d)))
265     (declare (dynamic-extent fp))
266     (assert (eql 0.0 (fp-struct-1-s fp)))
267     (assert (eql d (fp-struct-1-d fp)))))
268
269 (defun-with-dx test-fp-struct-1.3 (s d)
270   (let ((fp (make-fp-struct-1 :d d :s s)))
271     (declare (dynamic-extent fp))
272     (assert (eql s (fp-struct-1-s fp)))
273     (assert (eql d (fp-struct-1-d fp)))))
274
275 (defun-with-dx test-fp-struct-1.4 (s d)
276   (let ((fp (make-fp-struct-1 :s s :d d)))
277     (declare (dynamic-extent fp))
278     (assert (eql s (fp-struct-1-s fp)))
279     (assert (eql d (fp-struct-1-d fp)))))
280
281 (with-test (:name (:test-fp-struct-1.1))
282   (test-fp-struct-1.1 123.456 876.243d0))
283 (with-test (:name (:test-fp-struct-1.2))
284   (test-fp-struct-1.2 123.456 876.243d0))
285 (with-test (:name (:test-fp-struct-1.3))
286   (test-fp-struct-1.3 123.456 876.243d0))
287 (with-test (:name (:test-fp-struct-1.4))
288   (test-fp-struct-1.4 123.456 876.243d0))
289
290 (declaim (inline make-fp-struct-2))
291 (defstruct fp-struct-2
292   (d 0.0d0 :type double-float)
293   (s 0.0 :type single-float))
294
295 (defun-with-dx test-fp-struct-2.1 (s d)
296   (let ((fp (make-fp-struct-2 :s s)))
297     (declare (dynamic-extent fp))
298     (assert (eql s (fp-struct-2-s fp)))
299     (assert (eql 0.0d0 (fp-struct-2-d fp)))))
300
301 (defun-with-dx test-fp-struct-2.2 (s d)
302   (let ((fp (make-fp-struct-2 :d d)))
303     (declare (dynamic-extent fp))
304     (assert (eql 0.0 (fp-struct-2-s fp)))
305     (assert (eql d (fp-struct-2-d fp)))))
306
307 (defun-with-dx test-fp-struct-2.3 (s d)
308   (let ((fp (make-fp-struct-2 :d d :s s)))
309     (declare (dynamic-extent fp))
310     (assert (eql s (fp-struct-2-s fp)))
311     (assert (eql d (fp-struct-2-d fp)))))
312
313 (defun-with-dx test-fp-struct-2.4 (s d)
314   (let ((fp (make-fp-struct-2 :s s :d d)))
315     (declare (dynamic-extent fp))
316     (assert (eql s (fp-struct-2-s fp)))
317     (assert (eql d (fp-struct-2-d fp)))))
318
319 (with-test (:name (:test-fp-struct-2.1))
320   (test-fp-struct-2.1 123.456 876.243d0))
321 (with-test (:name (:test-fp-struct-2.2))
322   (test-fp-struct-2.2 123.456 876.243d0))
323 (with-test (:name (:test-fp-struct-2.3))
324   (test-fp-struct-2.3 123.456 876.243d0))
325 (with-test (:name (:test-fp-struct-2.4))
326   (test-fp-struct-2.4 123.456 876.243d0))
327
328 (declaim (inline make-cfp-struct-1))
329 (defstruct cfp-struct-1
330   (s (complex 0.0) :type (complex single-float))
331   (d (complex 0.0d0) :type (complex double-float)))
332
333 (defun-with-dx test-cfp-struct-1.1 (s d)
334   (let ((cfp (make-cfp-struct-1 :s s)))
335     (declare (dynamic-extent cfp))
336     (assert (eql s (cfp-struct-1-s cfp)))
337     (assert (eql (complex 0.0d0) (cfp-struct-1-d cfp)))))
338
339 (defun-with-dx test-cfp-struct-1.2 (s d)
340   (let ((cfp (make-cfp-struct-1 :d d)))
341     (declare (dynamic-extent cfp))
342     (assert (eql (complex 0.0) (cfp-struct-1-s cfp)))
343     (assert (eql d (cfp-struct-1-d cfp)))))
344
345 (defun-with-dx test-cfp-struct-1.3 (s d)
346   (let ((cfp (make-cfp-struct-1 :d d :s s)))
347     (declare (dynamic-extent cfp))
348     (assert (eql s (cfp-struct-1-s cfp)))
349     (assert (eql d (cfp-struct-1-d cfp)))))
350
351 (defun-with-dx test-cfp-struct-1.4 (s d)
352   (let ((cfp (make-cfp-struct-1 :s s :d d)))
353     (declare (dynamic-extent cfp))
354     (assert (eql s (cfp-struct-1-s cfp)))
355     (assert (eql d (cfp-struct-1-d cfp)))))
356
357 (with-test (:name (:test-cfp-struct-1.1))
358   (test-cfp-struct-1.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
359 (with-test (:name (:test-cfp-struct-1.2))
360   (test-cfp-struct-1.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
361 (with-test (:name (:test-cfp-struct-1.3))
362   (test-cfp-struct-1.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
363 (with-test (:name (:test-cfp-struct-1.4))
364   (test-cfp-struct-1.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
365
366 (declaim (inline make-cfp-struct-2))
367 (defstruct cfp-struct-2
368   (d (complex 0.0d0) :type (complex double-float))
369   (s (complex 0.0) :type (complex single-float)))
370
371 (defun-with-dx test-cfp-struct-2.1 (s d)
372   (let ((cfp (make-cfp-struct-2 :s s)))
373     (declare (dynamic-extent cfp))
374     (assert (eql s (cfp-struct-2-s cfp)))
375     (assert (eql (complex 0.0d0) (cfp-struct-2-d cfp)))))
376
377 (defun-with-dx test-cfp-struct-2.2 (s d)
378   (let ((cfp (make-cfp-struct-2 :d d)))
379     (declare (dynamic-extent cfp))
380     (assert (eql (complex 0.0) (cfp-struct-2-s cfp)))
381     (assert (eql d (cfp-struct-2-d cfp)))))
382
383 (defun-with-dx test-cfp-struct-2.3 (s d)
384   (let ((cfp (make-cfp-struct-2 :d d :s s)))
385     (declare (dynamic-extent cfp))
386     (assert (eql s (cfp-struct-2-s cfp)))
387     (assert (eql d (cfp-struct-2-d cfp)))))
388
389 (defun-with-dx test-cfp-struct-2.4 (s d)
390   (let ((cfp (make-cfp-struct-2 :s s :d d)))
391     (declare (dynamic-extent cfp))
392     (assert (eql s (cfp-struct-2-s cfp)))
393     (assert (eql d (cfp-struct-2-d cfp)))))
394
395 (with-test (:name (:test-cfp-struct-2.1))
396   (test-cfp-struct-2.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
397 (with-test (:name (:test-cfp-struct-2.2))
398   (test-cfp-struct-2.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
399 (with-test (:name (:test-cfp-struct-2.3))
400   (test-cfp-struct-2.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
401 (with-test (:name (:test-cfp-struct-2.4))
402   (test-cfp-struct-2.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
403
404 (declaim (inline make-foo1 make-foo2 make-foo3))
405 (defstruct foo1 x)
406
407 (defun-with-dx make-foo1-on-stack (x)
408   (let ((foo (make-foo1 :x x)))
409     (declare (dynamic-extent foo))
410     (assert (eql x (foo1-x foo)))))
411
412 (defstruct foo2
413   (x 0.0 :type single-float)
414   (y 0.0d0 :type double-float)
415   a
416   b
417   c)
418
419 (defun-with-dx make-foo2-on-stack (x y)
420   (let ((foo (make-foo2 :y y :c 'c)))
421     (declare (dynamic-extent foo))
422     (assert (eql 0.0 (foo2-x foo)))
423     (assert (eql y (foo2-y foo)))
424     (assert (eql 'c (foo2-c foo)))
425     (assert (eql nil (foo2-b foo)))))
426
427 ;;; Check that constants work out as argument for all relevant
428 ;;; slot types.
429 (defstruct foo3
430   (a 0 :type t)
431   (b 1 :type fixnum)
432   (c 2 :type sb-vm:word)
433   (d 3.0 :type single-float)
434   (e 4.0d0 :type double-float))
435
436 (defun-with-dx make-foo3-on-stack ()
437   (let ((foo (make-foo3)))
438     (declare (dynamic-extent foo))
439     (assert (eql 0 (foo3-a foo)))
440     (assert (eql 1 (foo3-b foo)))
441     (assert (eql 2 (foo3-c foo)))
442     (assert (eql 3.0 (foo3-d foo)))
443     (assert (eql 4.0d0 (foo3-e foo)))))
444
445 ;;; Nested DX
446
447 (defun-with-dx nested-dx-lists ()
448   (let ((dx (list (list 1 2) (list 3 4))))
449     (declare (dynamic-extent dx))
450     (true dx)
451     nil))
452
453 (defun-with-dx nested-dx-conses ()
454   (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t) nil))))))
455     (declare (dynamic-extent dx))
456     (true dx)
457     nil))
458
459 (defun-with-dx nested-dx-not-used (x)
460   (declare (list x))
461   (let ((l (setf (car x) (list x x x))))
462     (declare (dynamic-extent l))
463     (true l)
464     (true (length l))
465     nil))
466
467 (defun-with-dx nested-evil-dx-used (x)
468   (declare (list x))
469   (let ((l (list x x x)))
470     (declare (dynamic-extent l))
471     (unwind-protect
472          (progn
473            (setf (car x) l)
474            (true l))
475       (setf (car x) nil))
476     nil))
477
478 (defparameter *bar* nil)
479 (declaim (inline make-nested-bad make-nested-good))
480 (defstruct (nested (:constructor make-nested-bad (&key bar &aux (bar (setf *bar* bar))))
481                    (:constructor make-nested-good (&key bar)))
482   bar)
483
484 (defun-with-dx nested-good (y)
485   (let ((x (list (list (make-nested-good :bar (list (list (make-nested-good :bar (list y)))))))))
486     (declare (dynamic-extent x))
487     (true x)))
488
489 (defun-with-dx nested-bad (y)
490   (let ((x (list (list (make-nested-bad :bar (list (list (make-nested-bad :bar (list y)))))))))
491     (declare (dynamic-extent x))
492     (unless (equalp (caar x) (make-nested-good :bar *bar*))
493       (error "got ~S, wanted ~S" (caar x) (make-nested-good :bar *bar*)))
494     (caar x)))
495
496 (with-test (:name :conservative-nested-dx)
497   ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
498   (assert (equalp (nested-bad 42) (make-nested-good :bar *bar*)))
499   (assert (equalp *bar* (list (list (make-nested-bad :bar (list 42)))))))
500
501 ;;; multiple uses for dx lvar
502
503 (defun-with-dx multiple-dx-uses ()
504   (let ((dx (if (true t)
505                 (list 1 2 3)
506                 (list 2 3 4))))
507     (declare (dynamic-extent dx))
508     (true dx)
509     nil))
510
511 ;;; handler-case and handler-bind should use DX internally
512
513 (defun dx-handler-bind (x)
514   (handler-bind ((error
515                   #'(lambda (c)
516                       (break "OOPS: ~S caused ~S" x c)))
517                  ((and serious-condition (not error))
518                   #'(lambda (c)
519                       (break "OOPS2: ~S did ~S" x c))))
520     (/ 2 x)))
521
522 (defun dx-handler-case (x)
523   (assert (zerop (handler-case (/ 2 x)
524                    (error (c)
525                      (break "OOPS: ~S caused ~S" x c)
526                      -1)
527                    (:no-error (res)
528                      (1- res))))))
529
530 (defvar *a-cons* (cons nil nil))
531
532 (with-test (:name (:no-consing :dx-closures) :skipped-on '(not :stack-allocatable-closures))
533   (assert-no-consing (dxclosure 42)))
534
535 (with-test (:name (:no-consing :dx-lists) :skipped-on '(not :stack-allocatable-lists))
536   (assert-no-consing (dxlength 1 2 3))
537   (assert-no-consing (dxlength t t t t t t))
538   (assert-no-consing (dxlength))
539   (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
540   (assert-no-consing (test-nip-values))
541   (assert-no-consing (test-let-var-subst2 17))
542   (assert-no-consing (test-lvar-subst 11))
543   (assert-no-consing (nested-dx-lists))
544   (assert-consing (nested-dx-not-used *a-cons*))
545   (assert-no-consing (nested-evil-dx-used *a-cons*))
546   (assert-no-consing (multiple-dx-uses)))
547
548 (with-test (:name (:no-consing :dx-value-cell))
549   (assert-no-consing (dx-value-cell 13)))
550
551 (with-test (:name (:no-consing :dx-fixed-objects) :skipped-on '(not :stack-allocatable-fixed-objects))
552   (assert-no-consing (cons-on-stack 42))
553   (assert-no-consing (make-foo1-on-stack 123))
554   (assert-no-consing (nested-good 42))
555   (assert-no-consing (nested-dx-conses))
556   (assert-no-consing (dx-handler-bind 2))
557   (assert-no-consing (dx-handler-case 2)))
558
559 (with-test (:name (:no-consing :dx-vectors) :skipped-on '(not :stack-allocatable-vectors))
560   (assert-no-consing (force-make-array-on-stack 128))
561   (assert-no-consing (make-array-on-stack-1))
562   (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
563   (assert-no-consing (make-array-on-stack-3 9 8 7))
564   (assert-no-consing (make-array-on-stack-4))
565   (assert-no-consing (make-array-on-stack-5))
566   (assert-no-consing (vector-on-stack :x :y)))
567
568 (with-test (:name (:no-consing :specialized-dx-vectors)
569             :skipped-on `(not (and :stack-allocatable-vectors
570                                    :c-stack-is-control-stack)))
571   (assert-no-consing (make-array-on-stack-6))
572   (assert-no-consing (make-array-on-stack-7))
573   (assert-no-consing (make-array-on-stack-8))
574   (assert-no-consing (make-array-on-stack-9))
575   (assert-no-consing (make-array-on-stack-10)))
576
577 (with-test (:name (:no-consing :dx-raw-instances) :fails-on :ppc :skipped-on '(not :raw-instance-init-vops))
578   (let (a b)
579     (setf a 1.24 b 1.23d0)
580     (assert-no-consing (make-foo2-on-stack a b)))
581     (assert-no-consing (make-foo3-on-stack)))
582
583 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
584
585 (defvar *table* (make-hash-table))
586
587 (defun test-hash-table ()
588   (setf (gethash 5 *table*) 13)
589   (gethash 5 *table*))
590
591 ;; This fails on threaded PPC because the hash-table implementation
592 ;; uses recursive system locks, which cons (see below for test
593 ;; (:no-consing :lock), which also fails on threaded PPC).
594 (with-test (:name (:no-consing :hash-tables) :fails-on '(and :ppc :sb-thread))
595   (assert-no-consing (test-hash-table)))
596
597 ;;; with-mutex should use DX and not cons
598
599 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
600
601 (defun test-mutex ()
602   (sb-thread:with-mutex (*mutex*)
603     (true *mutex*)))
604
605 (with-test (:name (:no-consing :mutex) :fails-on :ppc :skipped-on '(not :sb-thread))
606   (assert-no-consing (test-mutex)))
607 \f
608
609 ;;; Bugs found by Paul F. Dietz
610
611 (with-test (:name (:dx-bug-misc :pfdietz))
612   (assert
613    (eq
614     (funcall
615      (compile
616       nil
617       '(lambda (a b)
618         (declare (optimize (speed 2) (space 0) (safety 0)
619                   (debug 1) (compilation-speed 3)))
620         (let* ((v5 (cons b b)))
621           (declare (dynamic-extent v5))
622           a)))
623      'x 'y)
624     'x)))
625
626 ;;; bug reported by Svein Ove Aas
627 (defun svein-2005-ii-07 (x y)
628   (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
629   (let ((args (list* y 1 2 x)))
630     (declare (dynamic-extent args))
631     (apply #'aref args)))
632
633 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
634   (assert (eql
635            (svein-2005-ii-07
636             '(0)
637             #3A(((1 1 1) (1 1 1) (1 1 1))
638                 ((1 1 1) (1 1 1) (4 1 1))
639                 ((1 1 1) (1 1 1) (1 1 1))))
640            4)))
641
642 ;;; bug reported by Brian Downing: stack-allocated arrays were not
643 ;;; filled with zeroes.
644 (defun-with-dx bdowning-2005-iv-16 ()
645   (let ((a (make-array 11 :initial-element 0)))
646     (declare (dynamic-extent a))
647     (assert (every (lambda (x) (eql x 0)) a))))
648
649 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
650   #+(or hppa mips x86 x86-64)
651   (assert-no-consing (bdowning-2005-iv-16))
652   (bdowning-2005-iv-16))
653
654 (declaim (inline my-nconc))
655 (defun-with-dx my-nconc (&rest lists)
656   (declare (dynamic-extent lists))
657   (apply #'nconc lists))
658 (defun-with-dx my-nconc-caller (a b c)
659   (let ((l1 (list a b c))
660         (l2 (list a b c)))
661     (my-nconc l1 l2)))
662 (with-test (:name :rest-stops-the-buck)
663   (let ((list1 (my-nconc-caller 1 2 3))
664         (list2 (my-nconc-caller 9 8 7)))
665     (assert (equal list1 '(1 2 3 1 2 3)))
666     (assert (equal list2 '(9 8 7 9 8 7)))))
667
668 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
669   (let* ((a (list x y z))
670          (b (list x y z))
671          (c (list a b)))
672     (declare (dynamic-extent c))
673     (values (first c) (second c))))
674 (with-test (:name :let-converted-vars-dx-allocated-bug)
675   (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
676     (assert (and (equal i j)
677                  (equal i (list 1 2 3))))))
678
679 ;;; workaround for bug 419 -- real issue remains, but check that the
680 ;;; bandaid holds.
681 (defun-with-dx bug419 (x)
682   (multiple-value-call #'list
683     (eval '(values 1 2 3))
684     (let ((x x))
685       (declare (dynamic-extent x))
686       (flet ((mget (y)
687                (+ x y))
688              (mset (z)
689                (incf x z)))
690         (declare (dynamic-extent #'mget #'mset))
691         ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
692
693 (with-test (:name (:dx-bug-misc :bug419))
694   (assert (equal (bug419 42) '(1 2 3 4 5 6))))
695
696 ;;; Multiple DX arguments in a local function call
697 (defun test-dx-flet-test (fun n f1 f2 f3)
698   (let ((res (with-output-to-string (s)
699                (assert (eql n (ignore-errors (funcall fun s)))))))
700     (multiple-value-bind (x pos) (read-from-string res nil)
701       (assert (equalp f1 x))
702       (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
703         (assert (equalp f2 y))
704         (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
705   #+(or hppa mips x86 x86-64)
706   (assert-no-consing (assert (eql n (funcall fun nil))))
707   (assert (eql n (funcall fun nil))))
708
709 (macrolet ((def (n f1 f2 f3)
710              (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
711                `(progn
712                   (defun-with-dx ,name (s)
713                     (flet ((f (x)
714                              (declare (dynamic-extent x))
715                              (when s
716                                (print x s)
717                                (finish-output s))
718                              nil))
719                       (f ,f1)
720                       (f ,f2)
721                       (f ,f3)
722                       ,n))
723                   (with-test (:name (:dx-flet-test ,n))
724                     (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
725   (def 0 (list :one) (list :two) (list :three))
726   (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
727   (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
728
729 ;;; Test that unknown-values coming after a DX value won't mess up the
730 ;;; stack analysis
731 (defun test-update-uvl-live-sets (x y z)
732  (declare (optimize speed (safety 0)))
733  (flet ((bar (a b)
734           (declare (dynamic-extent a))
735           (eval `(list (length ',a) ',b))))
736    (list (bar x y)
737          (bar (list x y z)                  ; dx push
738               (list
739                (multiple-value-call 'list
740                  (eval '(values 1 2 3))     ; uv push
741                  (max y z)
742                )                            ; uv pop
743                14)
744          ))))
745
746 (with-test (:name (:update-uvl-live-sets))
747   (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
748                  (test-update-uvl-live-sets #() 4 5))))
749
750 (with-test (:name :regression-1.0.23.38)
751   (compile nil '(lambda ()
752                  (flet ((make (x y)
753                           (let ((res (cons x x)))
754                             (setf (cdr res) y)
755                             res)))
756                    (declaim (inline make))
757                    (let ((z (make 1 2)))
758                      (declare (dynamic-extent z))
759                      (print z)
760                      t))))
761   (compile nil '(lambda ()
762                  (flet ((make (x y)
763                           (let ((res (cons x x)))
764                             (setf (cdr res) y)
765                             (if x res y))))
766                    (declaim (inline make))
767                    (let ((z (make 1 2)))
768                      (declare (dynamic-extent z))
769                      (print z)
770                      t)))))
771
772 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
773 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
774 ;;; location, leading to all manner of badness. ...reproducing this
775 ;;; reliably is hard, but this it at least used to break on x86-64.
776 (defun length-and-words-packed-in-same-tn (m)
777   (declare (optimize speed (safety 0) (debug 0) (space 0)))
778   (let ((array (make-array (max 1 m) :element-type 'fixnum)))
779     (declare (dynamic-extent array))
780     (array-total-size array)))
781 (with-test (:name :length-and-words-packed-in-same-tn)
782   (assert (= 1 (length-and-words-packed-in-same-tn -3))))
783
784 (with-test (:name :handler-case-bogus-compiler-note :fails-on :ppc)
785   (handler-bind
786       ((compiler-note (lambda (note)
787                         (error "compiler issued note ~S during test" note))))
788     ;; Taken from SWANK, used to signal a bogus stack allocation
789     ;; failure note.
790     (compile nil
791              `(lambda (files fasl-dir load)
792                 (let ((needs-recompile nil))
793                   (dolist (src files)
794                     (let ((dest (binary-pathname src fasl-dir)))
795                       (handler-case
796                           (progn
797                             (when (or needs-recompile
798                                       (not (probe-file dest))
799                                       (file-newer-p src dest))
800                               (setq needs-recompile t)
801                               (ensure-directories-exist dest)
802                               (compile-file src :output-file dest :print nil :verbose t))
803                             (when load
804                               (load dest :verbose t)))
805                         (serious-condition (c)
806                           (handle-loadtime-error c dest))))))))))
807
808 (declaim (inline foovector barvector))
809 (defun foovector (x y z)
810   (let ((v (make-array 3)))
811     (setf (aref v 0) x
812           (aref v 1) y
813           (aref v 2) z)
814     v))
815 (defun barvector (x y z)
816   (make-array 3 :initial-contents (list x y z)))
817 (with-test (:name :dx-compiler-notes :fails-on :ppc)
818   (flet ((assert-notes (j lambda)
819            (let ((n 0))
820              (handler-bind ((compiler-note (lambda (c)
821                                              (declare (ignore c))
822                                              (incf n))))
823                (compile nil lambda)
824                (unless (= j n)
825                  (error "Wanted ~S notes, got ~S for~%   ~S"
826                         j n lambda))))))
827     ;; These ones should complain.
828     (assert-notes 1 `(lambda (x)
829                        (let ((v (make-array x)))
830                          (declare (dynamic-extent v))
831                          (length v))))
832     (assert-notes 2 `(lambda (x)
833                        (let ((y (if (plusp x)
834                                     (true x)
835                                     (true (- x)))))
836                          (declare (dynamic-extent y))
837                          (print y)
838                          nil)))
839     (assert-notes 1 `(lambda (x)
840                        (let ((y (foovector x x x)))
841                          (declare (sb-int:truly-dynamic-extent y))
842                          (print y)
843                          nil)))
844     ;; These ones should not complain.
845     (assert-notes 0 `(lambda (name)
846                        (with-alien
847                            ((posix-getenv (function c-string c-string)
848                                           :EXTERN "getenv"))
849                          (values
850                           (alien-funcall posix-getenv name)))))
851     (assert-notes 0 `(lambda (x)
852                        (let ((y (barvector x x x)))
853                          (declare (dynamic-extent y))
854                          (print y)
855                          nil)))
856     (assert-notes 0 `(lambda (list)
857                        (declare (optimize (space 0)))
858                        (sort list #'<)))
859     (assert-notes 0 `(lambda (other)
860                        #'(lambda (s c n)
861                            (ignore-errors (funcall other s c n)))))))
862
863 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
864 ;;; in an unfortunate loop.
865 (defun handler-case-eating-stack ()
866   (let ((sp nil))
867     (do ((n 0 (logand most-positive-fixnum (1+ n))))
868         ((>= n 1024))
869      (multiple-value-bind (value error) (ignore-errors)
870        (when (and value error) nil))
871       (if sp
872           (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
873           (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
874 (with-test (:name :handler-case-eating-stack :fails-on :ppc)
875   (assert-no-consing (handler-case-eating-stack)))
876
877 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
878 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
879 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
880 (deftype vec ()
881   `(simple-array single-float (3)))
882 (declaim (ftype (function (t t t) vec) vec))
883 (declaim (inline vec))
884 (defun vec (a b c)
885   (make-array 3 :element-type 'single-float :initial-contents (list a b c)))
886 (defun bad-boy (vec)
887   (declare (type vec vec))
888   (lambda (fun)
889     (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
890       (declare (dynamic-extent vec))
891       (funcall fun vec))))
892 (with-test (:name :recheck-nested-dx-bug :fails-on :ppc)
893   (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
894                    (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
895   (flet ((foo (x) (declare (ignore x))))
896     (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
897       (assert-no-consing (funcall bad-boy #'foo)))))
898
899 (with-test (:name :bug-497321)
900   (flet ((test (lambda type)
901            (let ((n 0))
902              (handler-bind ((condition (lambda (c)
903                                          (incf n)
904                                          (unless (typep c type)
905                                            (error "wanted ~S for~%  ~S~%got ~S"
906                                                   type lambda (type-of c))))))
907                (compile nil lambda))
908              (assert (= n 1)))))
909     (test `(lambda () (declare (dynamic-extent #'bar)))
910           'style-warning)
911     (test `(lambda () (declare (dynamic-extent bar)))
912           'style-warning)
913     (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
914           'sb-ext:compiler-note)
915     (test `(lambda ()
916              (flet ((bar () t))
917                (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
918           'sb-ext:compiler-note)))
919
920 (with-test (:name :bug-586105 :fails-on '(not (and :stack-allocatable-vectors
921                                                    :stack-allocatable-lists)))
922   (flet ((test (x)
923            (let ((vec (make-array 1 :initial-contents (list (list x)))))
924              (declare (dynamic-extent vec))
925              (assert (eql x (car (aref vec 0)))))))
926     (assert-no-consing (test 42))))
927 \f
928 (defun bug-681092 ()
929   (declare (optimize speed))
930   (let ((c 0))
931     (flet ((bar () c))
932       (declare (dynamic-extent #'bar))
933       (do () ((list) (bar))
934         (setf c 10)
935         (return (bar))))))
936 (with-test (:name :bug-681092)
937   (assert (= 10 (bug-681092))))
938
939 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
940 ;;;; but required by sanity.
941
942 (declaim (inline rest-stops-dx))
943 (defun-with-dx rest-stops-dx (&rest args)
944   (declare (dynamic-extent args))
945   (apply #'opaque-identity args))
946
947 (defun-with-dx rest-stops-dx-ok ()
948   (equal '(:foo) (rest-stops-dx (list :foo))))
949
950 (with-test (:name :rest-stops-dynamic-extent)
951   (assert (rest-stops-dx-ok)))
952
953 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
954 ;;;; conversion.
955 (with-test (:name :rest-to-more-conversion)
956   (let ((f1 (compile nil `(lambda (f &rest args)
957                             (apply f args)))))
958     (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
959   (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
960                             (values (apply f1 args) (apply f2 args))))))
961     (assert-no-consing (multiple-value-bind (a b)
962                            (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
963                                     1 2 3)
964                          (assert (and (eql 6 a) (eql -4 b))))))
965   (let ((f3 (compile nil `(lambda (f &optional x &rest args)
966                             (when x
967                               (apply f x args))))))
968     (assert-no-consing (assert (eql 42 (funcall f3
969                                                 (lambda (a b c) (+ a b c))
970                                                 11
971                                                 10
972                                                 21)))))
973   (let ((f4 (compile nil `(lambda (f &optional x &rest args &key y &allow-other-keys)
974                             (apply f y x args)))))
975     (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
976                                      (assert (eq y 'y))
977                                      (assert (= x 2))
978                                      (assert (eq :y yk))
979                                      (assert (eq y2 'y))
980                                      (assert (eq b 'b))
981                                      (assert (eq c 'c)))
982                                 2 :y 'y 'b 'c)))
983   (let ((f5 (compile nil `(lambda (a b c &rest args)
984                             (apply #'list* a b c args)))))
985     (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
986   (let ((f6 (compile nil `(lambda (x y)
987                             (declare (optimize speed))
988                             (concatenate 'string x y)))))
989     (assert (equal "foobar" (funcall f6 "foo" "bar"))))
990   (let ((f7 (compile nil `(lambda (&rest args)
991                             (lambda (f)
992                               (apply f args))))))
993     (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
994   (let ((f8 (compile nil `(lambda (&rest args)
995                             (flet ((foo (f)
996                                      (apply f args)))
997                               #'foo)))))
998     (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
999   (let ((f9 (compile nil `(lambda (f &rest args)
1000                             (flet ((foo (g)
1001                                      (apply g args)))
1002                               (declare (dynamic-extent #'foo))
1003                               (funcall f #'foo))))))
1004     (assert (equal '(a b c d e f)
1005                    (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1006   (let ((f10 (compile nil `(lambda (f &rest args)
1007                             (flet ((foo (g)
1008                                      (apply g args)))
1009                               (funcall f #'foo))))))
1010     (assert (equal '(a b c d e f)
1011                    (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1012   (let ((f11 (compile nil `(lambda (x y z)
1013                              (block out
1014                                (labels ((foo (x &rest rest)
1015                                           (apply (lambda (&rest rest2)
1016                                                    (return-from out (values-list rest2)))
1017                                                  x rest)))
1018                                 (if x
1019                                     (foo x y z)
1020                                     (foo y z x))))))))
1021     (multiple-value-bind (a b c) (funcall f11 1 2 3)
1022       (assert (eql a 1))
1023       (assert (eql b 2))
1024       (assert (eql c 3)))))
1025
1026 (defun opaque-funcall (function &rest arguments)
1027   (apply function arguments))
1028
1029 (with-test (:name :implicit-value-cells)
1030   (flet ((test-it (type input output)
1031            (let ((f (compile nil `(lambda (x)
1032                                     (declare (type ,type x))
1033                                     (flet ((inc ()
1034                                              (incf x)))
1035                                       (declare (dynamic-extent #'inc))
1036                                       (list (opaque-funcall #'inc) x))))))
1037              (assert (equal (funcall f input)
1038                             (list output output))))))
1039     (let ((width sb-vm:n-word-bits))
1040       (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1041       (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1042       (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1043       (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1044       (test-it 'single-float 3f0 4f0)
1045       (test-it 'double-float 3d0 4d0)
1046       (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1047       (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1048
1049 (with-test (:name :sap-implicit-value-cells)
1050   (let ((f (compile nil `(lambda (x)
1051                            (declare (type system-area-pointer x))
1052                            (flet ((inc ()
1053                                     (setf x (sb-sys:sap+ x 16))))
1054                              (declare (dynamic-extent #'inc))
1055                              (list (opaque-funcall #'inc) x)))))
1056         (width sb-vm:n-machine-word-bits))
1057     (assert (every (lambda (x)
1058                      (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1059                    (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))