redesign exiting SBCL
[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 (with-test (:name (:no-consing :hash-tables) :fails-on '(and :ppc :sb-thread))
616   (assert-no-consing (test-hash-table)))
617
618 ;;; with-mutex should use DX and not cons
619
620 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
621
622 (defun test-mutex ()
623   (sb-thread:with-mutex (*mutex*)
624     (true *mutex*)))
625
626 (with-test (:name (:no-consing :mutex) :fails-on :ppc :skipped-on '(not :sb-thread))
627   (assert-no-consing (test-mutex)))
628 \f
629
630 ;;; Bugs found by Paul F. Dietz
631
632 (with-test (:name (:dx-bug-misc :pfdietz))
633   (assert
634    (eq
635     (funcall
636      (compile
637       nil
638       '(lambda (a b)
639         (declare (optimize (speed 2) (space 0) (safety 0)
640                   (debug 1) (compilation-speed 3)))
641         (let* ((v5 (cons b b)))
642           (declare (dynamic-extent v5))
643           a)))
644      'x 'y)
645     'x)))
646
647 ;;; bug reported by Svein Ove Aas
648 (defun svein-2005-ii-07 (x y)
649   (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
650   (let ((args (list* y 1 2 x)))
651     (declare (dynamic-extent args))
652     (apply #'aref args)))
653
654 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
655   (assert (eql
656            (svein-2005-ii-07
657             '(0)
658             #3A(((1 1 1) (1 1 1) (1 1 1))
659                 ((1 1 1) (1 1 1) (4 1 1))
660                 ((1 1 1) (1 1 1) (1 1 1))))
661            4)))
662
663 ;;; bug reported by Brian Downing: stack-allocated arrays were not
664 ;;; filled with zeroes.
665 (defun-with-dx bdowning-2005-iv-16 ()
666   (let ((a (make-array 11 :initial-element 0)))
667     (declare (dynamic-extent a))
668     (assert (every (lambda (x) (eql x 0)) a))))
669
670 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
671   #+(or hppa mips x86 x86-64)
672   (assert-no-consing (bdowning-2005-iv-16))
673   (bdowning-2005-iv-16))
674
675 (declaim (inline my-nconc))
676 (defun-with-dx my-nconc (&rest lists)
677   (declare (dynamic-extent lists))
678   (apply #'nconc lists))
679 (defun-with-dx my-nconc-caller (a b c)
680   (let ((l1 (list a b c))
681         (l2 (list a b c)))
682     (my-nconc l1 l2)))
683 (with-test (:name :rest-stops-the-buck)
684   (let ((list1 (my-nconc-caller 1 2 3))
685         (list2 (my-nconc-caller 9 8 7)))
686     (assert (equal list1 '(1 2 3 1 2 3)))
687     (assert (equal list2 '(9 8 7 9 8 7)))))
688
689 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
690   (let* ((a (list x y z))
691          (b (list x y z))
692          (c (list a b)))
693     (declare (dynamic-extent c))
694     (values (first c) (second c))))
695 (with-test (:name :let-converted-vars-dx-allocated-bug)
696   (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
697     (assert (and (equal i j)
698                  (equal i (list 1 2 3))))))
699
700 ;;; workaround for bug 419 -- real issue remains, but check that the
701 ;;; bandaid holds.
702 (defun-with-dx bug419 (x)
703   (multiple-value-call #'list
704     (eval '(values 1 2 3))
705     (let ((x x))
706       (declare (dynamic-extent x))
707       (flet ((mget (y)
708                (+ x y))
709              (mset (z)
710                (incf x z)))
711         (declare (dynamic-extent #'mget #'mset))
712         ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
713
714 (with-test (:name (:dx-bug-misc :bug419))
715   (assert (equal (bug419 42) '(1 2 3 4 5 6))))
716
717 ;;; Multiple DX arguments in a local function call
718 (defun test-dx-flet-test (fun n f1 f2 f3)
719   (let ((res (with-output-to-string (s)
720                (assert (eql n (ignore-errors (funcall fun s)))))))
721     (multiple-value-bind (x pos) (read-from-string res nil)
722       (assert (equalp f1 x))
723       (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
724         (assert (equalp f2 y))
725         (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
726   #+(or hppa mips x86 x86-64)
727   (assert-no-consing (assert (eql n (funcall fun nil))))
728   (assert (eql n (funcall fun nil))))
729
730 (macrolet ((def (n f1 f2 f3)
731              (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
732                `(progn
733                   (defun-with-dx ,name (s)
734                     (flet ((f (x)
735                              (declare (dynamic-extent x))
736                              (when s
737                                (print x s)
738                                (finish-output s))
739                              nil))
740                       (f ,f1)
741                       (f ,f2)
742                       (f ,f3)
743                       ,n))
744                   (with-test (:name (:dx-flet-test ,n))
745                     (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
746   (def 0 (list :one) (list :two) (list :three))
747   (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
748   (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
749
750 ;;; Test that unknown-values coming after a DX value won't mess up the
751 ;;; stack analysis
752 (defun test-update-uvl-live-sets (x y z)
753  (declare (optimize speed (safety 0)))
754  (flet ((bar (a b)
755           (declare (dynamic-extent a))
756           (eval `(list (length ',a) ',b))))
757    (list (bar x y)
758          (bar (list x y z)                  ; dx push
759               (list
760                (multiple-value-call 'list
761                  (eval '(values 1 2 3))     ; uv push
762                  (max y z)
763                )                            ; uv pop
764                14)
765          ))))
766
767 (with-test (:name (:update-uvl-live-sets))
768   (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
769                  (test-update-uvl-live-sets #() 4 5))))
770
771 (with-test (:name :regression-1.0.23.38)
772   (compile nil '(lambda ()
773                  (flet ((make (x y)
774                           (let ((res (cons x x)))
775                             (setf (cdr res) y)
776                             res)))
777                    (declaim (inline make))
778                    (let ((z (make 1 2)))
779                      (declare (dynamic-extent z))
780                      (print z)
781                      t))))
782   (compile nil '(lambda ()
783                  (flet ((make (x y)
784                           (let ((res (cons x x)))
785                             (setf (cdr res) y)
786                             (if x res y))))
787                    (declaim (inline make))
788                    (let ((z (make 1 2)))
789                      (declare (dynamic-extent z))
790                      (print z)
791                      t)))))
792
793 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
794 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
795 ;;; location, leading to all manner of badness. ...reproducing this
796 ;;; reliably is hard, but this it at least used to break on x86-64.
797 (defun length-and-words-packed-in-same-tn (m)
798   (declare (optimize speed (safety 0) (debug 0) (space 0)))
799   (let ((array (make-array (max 1 m) :element-type 'fixnum)))
800     (declare (dynamic-extent array))
801     (array-total-size array)))
802 (with-test (:name :length-and-words-packed-in-same-tn)
803   (assert (= 1 (length-and-words-packed-in-same-tn -3))))
804
805 (with-test (:name :handler-case-bogus-compiler-note :fails-on :ppc)
806   (handler-bind
807       ((compiler-note (lambda (note)
808                         (error "compiler issued note ~S during test" note))))
809     ;; Taken from SWANK, used to signal a bogus stack allocation
810     ;; failure note.
811     (compile nil
812              `(lambda (files fasl-dir load)
813                 (let ((needs-recompile nil))
814                   (dolist (src files)
815                     (let ((dest (binary-pathname src fasl-dir)))
816                       (handler-case
817                           (progn
818                             (when (or needs-recompile
819                                       (not (probe-file dest))
820                                       (file-newer-p src dest))
821                               (setq needs-recompile t)
822                               (ensure-directories-exist dest)
823                               (compile-file src :output-file dest :print nil :verbose t))
824                             (when load
825                               (load dest :verbose t)))
826                         (serious-condition (c)
827                           (handle-loadtime-error c dest))))))))))
828
829 (declaim (inline foovector barvector))
830 (defun foovector (x y z)
831   (let ((v (make-array 3)))
832     (setf (aref v 0) x
833           (aref v 1) y
834           (aref v 2) z)
835     v))
836 (defun barvector (x y z)
837   (make-array 3 :initial-contents (list x y z)))
838 (with-test (:name :dx-compiler-notes :fails-on :ppc)
839   (flet ((assert-notes (j lambda)
840            (let ((n 0))
841              (handler-bind ((compiler-note (lambda (c)
842                                              (declare (ignore c))
843                                              (incf n))))
844                (compile nil lambda)
845                (unless (= j n)
846                  (error "Wanted ~S notes, got ~S for~%   ~S"
847                         j n lambda))))))
848     ;; These ones should complain.
849     (assert-notes 1 `(lambda (x)
850                        (let ((v (make-array x)))
851                          (declare (dynamic-extent v))
852                          (length v))))
853     (assert-notes 2 `(lambda (x)
854                        (let ((y (if (plusp x)
855                                     (true x)
856                                     (true (- x)))))
857                          (declare (dynamic-extent y))
858                          (print y)
859                          nil)))
860     (assert-notes 1 `(lambda (x)
861                        (let ((y (foovector x x x)))
862                          (declare (sb-int:truly-dynamic-extent y))
863                          (print y)
864                          nil)))
865     ;; These ones should not complain.
866     (assert-notes 0 `(lambda (name)
867                        (with-alien
868                            ((posix-getenv (function c-string c-string)
869                                           :EXTERN "getenv"))
870                          (values
871                           (alien-funcall posix-getenv name)))))
872     (assert-notes 0 `(lambda (x)
873                        (let ((y (barvector x x x)))
874                          (declare (dynamic-extent y))
875                          (print y)
876                          nil)))
877     (assert-notes 0 `(lambda (list)
878                        (declare (optimize (space 0)))
879                        (sort list #'<)))
880     (assert-notes 0 `(lambda (other)
881                        #'(lambda (s c n)
882                            (ignore-errors (funcall other s c n)))))))
883
884 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
885 ;;; in an unfortunate loop.
886 (defun handler-case-eating-stack ()
887   (let ((sp nil))
888     (do ((n 0 (logand most-positive-fixnum (1+ n))))
889         ((>= n 1024))
890      (multiple-value-bind (value error) (ignore-errors)
891        (when (and value error) nil))
892       (if sp
893           (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
894           (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
895 (with-test (:name :handler-case-eating-stack :fails-on :ppc)
896   (assert-no-consing (handler-case-eating-stack)))
897
898 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
899 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
900 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
901 (deftype vec ()
902   `(simple-array single-float (3)))
903 (declaim (ftype (function (t t t) vec) vec))
904 (declaim (inline vec))
905 (defun vec (a b c)
906   (make-array 3 :element-type 'single-float :initial-contents (list a b c)))
907 (defun bad-boy (vec)
908   (declare (type vec vec))
909   (lambda (fun)
910     (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
911       (declare (dynamic-extent vec))
912       (funcall fun vec))))
913 (with-test (:name :recheck-nested-dx-bug :fails-on :ppc)
914   (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
915                    (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
916   (flet ((foo (x) (declare (ignore x))))
917     (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
918       (assert-no-consing (funcall bad-boy #'foo)))))
919
920 (with-test (:name :bug-497321)
921   (flet ((test (lambda type)
922            (let ((n 0))
923              (handler-bind ((condition (lambda (c)
924                                          (incf n)
925                                          (unless (typep c type)
926                                            (error "wanted ~S for~%  ~S~%got ~S"
927                                                   type lambda (type-of c))))))
928                (compile nil lambda))
929              (assert (= n 1)))))
930     (test `(lambda () (declare (dynamic-extent #'bar)))
931           'style-warning)
932     (test `(lambda () (declare (dynamic-extent bar)))
933           'style-warning)
934     (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
935           'sb-ext:compiler-note)
936     (test `(lambda ()
937              (flet ((bar () t))
938                (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
939           'sb-ext:compiler-note)))
940
941 (with-test (:name :bug-586105 :fails-on '(not (and :stack-allocatable-vectors
942                                                    :stack-allocatable-lists)))
943   (flet ((test (x)
944            (let ((vec (make-array 1 :initial-contents (list (list x)))))
945              (declare (dynamic-extent vec))
946              (assert (eql x (car (aref vec 0)))))))
947     (assert-no-consing (test 42))))
948 \f
949 (defun bug-681092 ()
950   (declare (optimize speed))
951   (let ((c 0))
952     (flet ((bar () c))
953       (declare (dynamic-extent #'bar))
954       (do () ((list) (bar))
955         (setf c 10)
956         (return (bar))))))
957 (with-test (:name :bug-681092)
958   (assert (= 10 (bug-681092))))
959
960 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
961 ;;;; but required by sanity.
962
963 (declaim (inline rest-stops-dx))
964 (defun-with-dx rest-stops-dx (&rest args)
965   (declare (dynamic-extent args))
966   (apply #'opaque-identity args))
967
968 (defun-with-dx rest-stops-dx-ok ()
969   (equal '(:foo) (rest-stops-dx (list :foo))))
970
971 (with-test (:name :rest-stops-dynamic-extent)
972   (assert (rest-stops-dx-ok)))
973
974 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
975 ;;;; conversion.
976 (with-test (:name :rest-to-more-conversion)
977   (let ((f1 (compile nil `(lambda (f &rest args)
978                             (apply f args)))))
979     (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
980   (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
981                             (values (apply f1 args) (apply f2 args))))))
982     (assert-no-consing (multiple-value-bind (a b)
983                            (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
984                                     1 2 3)
985                          (assert (and (eql 6 a) (eql -4 b))))))
986   (let ((f3 (compile nil `(lambda (f &optional x &rest args)
987                             (when x
988                               (apply f x args))))))
989     (assert-no-consing (assert (eql 42 (funcall f3
990                                                 (lambda (a b c) (+ a b c))
991                                                 11
992                                                 10
993                                                 21)))))
994   (let ((f4 (compile nil `(lambda (f &optional x &rest args &key y &allow-other-keys)
995                             (apply f y x args)))))
996     (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
997                                      (assert (eq y 'y))
998                                      (assert (= x 2))
999                                      (assert (eq :y yk))
1000                                      (assert (eq y2 'y))
1001                                      (assert (eq b 'b))
1002                                      (assert (eq c 'c)))
1003                                 2 :y 'y 'b 'c)))
1004   (let ((f5 (compile nil `(lambda (a b c &rest args)
1005                             (apply #'list* a b c args)))))
1006     (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
1007   (let ((f6 (compile nil `(lambda (x y)
1008                             (declare (optimize speed))
1009                             (concatenate 'string x y)))))
1010     (assert (equal "foobar" (funcall f6 "foo" "bar"))))
1011   (let ((f7 (compile nil `(lambda (&rest args)
1012                             (lambda (f)
1013                               (apply f args))))))
1014     (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
1015   (let ((f8 (compile nil `(lambda (&rest args)
1016                             (flet ((foo (f)
1017                                      (apply f args)))
1018                               #'foo)))))
1019     (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
1020   (let ((f9 (compile nil `(lambda (f &rest args)
1021                             (flet ((foo (g)
1022                                      (apply g args)))
1023                               (declare (dynamic-extent #'foo))
1024                               (funcall f #'foo))))))
1025     (assert (equal '(a b c d e f)
1026                    (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1027   (let ((f10 (compile nil `(lambda (f &rest args)
1028                             (flet ((foo (g)
1029                                      (apply g args)))
1030                               (funcall f #'foo))))))
1031     (assert (equal '(a b c d e f)
1032                    (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1033   (let ((f11 (compile nil `(lambda (x y z)
1034                              (block out
1035                                (labels ((foo (x &rest rest)
1036                                           (apply (lambda (&rest rest2)
1037                                                    (return-from out (values-list rest2)))
1038                                                  x rest)))
1039                                 (if x
1040                                     (foo x y z)
1041                                     (foo y z x))))))))
1042     (multiple-value-bind (a b c) (funcall f11 1 2 3)
1043       (assert (eql a 1))
1044       (assert (eql b 2))
1045       (assert (eql c 3)))))
1046
1047 (defun opaque-funcall (function &rest arguments)
1048   (apply function arguments))
1049
1050 (with-test (:name :implicit-value-cells)
1051   (flet ((test-it (type input output)
1052            (let ((f (compile nil `(lambda (x)
1053                                     (declare (type ,type x))
1054                                     (flet ((inc ()
1055                                              (incf x)))
1056                                       (declare (dynamic-extent #'inc))
1057                                       (list (opaque-funcall #'inc) x))))))
1058              (assert (equal (funcall f input)
1059                             (list output output))))))
1060     (let ((width sb-vm:n-word-bits))
1061       (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1062       (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1063       (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1064       (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1065       (test-it 'single-float 3f0 4f0)
1066       (test-it 'double-float 3d0 4d0)
1067       (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1068       (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1069
1070 (with-test (:name :sap-implicit-value-cells)
1071   (let ((f (compile nil `(lambda (x)
1072                            (declare (type system-area-pointer x))
1073                            (flet ((inc ()
1074                                     (setf x (sb-sys:sap+ x 16))))
1075                              (declare (dynamic-extent #'inc))
1076                              (list (opaque-funcall #'inc) x)))))
1077         (width sb-vm:n-machine-word-bits))
1078     (assert (every (lambda (x)
1079                      (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1080                    (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))