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