1 ;;;; tests that dynamic-extent functionality works.
3 ;;;; This software is part of the SBCL system. See the README file for
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
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.
14 (when (eq sb-ext:*evaluator-mode* :interpret)
15 (sb-ext:quit :unix-status 104))
17 (load "compiler-test-util.lisp")
20 (setq sb-c::*check-consistency* t
21 sb-ext:*stack-allocate-dynamic-extent* t)
23 (defmacro defun-with-dx (name arglist &body body)
24 `(defun ,name ,arglist
27 (declaim (notinline opaque-identity))
28 (defun opaque-identity (x)
33 (defun-with-dx dxlength (&rest rest)
34 (declare (dynamic-extent rest))
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)))
43 (destructuring-bind (a b c d e f &rest g) list
44 (+ a b c d e f (length g))))
46 (defun-with-dx dxcaller (&rest rest)
47 (declare (dynamic-extent rest))
50 (with-test (:name (:dx-&rest :pass-down-to-callee :tail-call))
51 (assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
53 (defun-with-dx dxcaller-align-1 (x &rest rest)
54 (declare (dynamic-extent rest))
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)))
63 (defun-with-dx test-nip-values ()
64 (flet ((bar (x &rest y)
65 (declare (dynamic-extent y))
69 (multiple-value-call #'values
73 (with-test (:name (:nip-values))
74 (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a))))
76 ;;; LET-variable substitution
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))
85 (with-test (:name (:let-variable-substitution))
86 (assert (eql (test-let-var-subst1 17) 2)))
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)))
95 (with-test (:name (:let-variable-substitution-2))
96 (assert (eql (test-let-var-subst2 17) 2)))
99 ;;; DX propagation through LET-return.
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)
108 (with-test (:name (:dx-propagation-through-let-return))
109 (assert (eql (test-lvar-subst 11) 10)))
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)
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)))))
129 (with-test (:name (:dx-list :alignment))
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))))))
138 (declaim (notinline true))
143 (defun-with-dx dxclosure (x)
146 (declare (dynamic-extent #'f))
149 (with-test (:name (:dx-closure))
150 (assert (eq t (dxclosure 13))))
154 (defun-with-dx dx-value-cell (x)
155 ;; Not implemented everywhere, yet.
156 #+(or x86 x86-64 mips hppa)
158 (declare (sb-int:truly-dynamic-extent cell))
161 (declare (dynamic-extent #'f))
166 (defun-with-dx cons-on-stack (x)
167 (let ((cons (cons x x)))
168 (declare (dynamic-extent cons))
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))
182 (defun-with-dx make-array-on-stack-1 ()
183 (let ((v (make-array '(42) :element-type 'single-float)))
184 (declare (dynamic-extent v))
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))
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))
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))
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))
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))
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))
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))
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))
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))
255 (defun-with-dx vector-on-stack (x y)
256 (let ((v (vector 1 x 2 y 3)))
257 (declare (sb-int:truly-dynamic-extent v))
263 (declaim (inline make-fp-struct-1))
264 (defstruct fp-struct-1
265 (s 0.0 :type single-float)
266 (d 0.0d0 :type double-float))
268 (defun-with-dx test-fp-struct-1.1 (s d)
269 (let ((fp (make-fp-struct-1 :s s)))
270 (declare (dynamic-extent fp))
271 (assert (eql s (fp-struct-1-s fp)))
272 (assert (eql 0.0d0 (fp-struct-1-d fp)))))
274 (defun-with-dx test-fp-struct-1.2 (s d)
275 (let ((fp (make-fp-struct-1 :d d)))
276 (declare (dynamic-extent fp))
277 (assert (eql 0.0 (fp-struct-1-s fp)))
278 (assert (eql d (fp-struct-1-d fp)))))
280 (defun-with-dx test-fp-struct-1.3 (s d)
281 (let ((fp (make-fp-struct-1 :d d :s s)))
282 (declare (dynamic-extent fp))
283 (assert (eql s (fp-struct-1-s fp)))
284 (assert (eql d (fp-struct-1-d fp)))))
286 (defun-with-dx test-fp-struct-1.4 (s d)
287 (let ((fp (make-fp-struct-1 :s s :d d)))
288 (declare (dynamic-extent fp))
289 (assert (eql s (fp-struct-1-s fp)))
290 (assert (eql d (fp-struct-1-d fp)))))
292 (with-test (:name (:test-fp-struct-1.1))
293 (test-fp-struct-1.1 123.456 876.243d0))
294 (with-test (:name (:test-fp-struct-1.2))
295 (test-fp-struct-1.2 123.456 876.243d0))
296 (with-test (:name (:test-fp-struct-1.3))
297 (test-fp-struct-1.3 123.456 876.243d0))
298 (with-test (:name (:test-fp-struct-1.4))
299 (test-fp-struct-1.4 123.456 876.243d0))
301 (declaim (inline make-fp-struct-2))
302 (defstruct fp-struct-2
303 (d 0.0d0 :type double-float)
304 (s 0.0 :type single-float))
306 (defun-with-dx test-fp-struct-2.1 (s d)
307 (let ((fp (make-fp-struct-2 :s s)))
308 (declare (dynamic-extent fp))
309 (assert (eql s (fp-struct-2-s fp)))
310 (assert (eql 0.0d0 (fp-struct-2-d fp)))))
312 (defun-with-dx test-fp-struct-2.2 (s d)
313 (let ((fp (make-fp-struct-2 :d d)))
314 (declare (dynamic-extent fp))
315 (assert (eql 0.0 (fp-struct-2-s fp)))
316 (assert (eql d (fp-struct-2-d fp)))))
318 (defun-with-dx test-fp-struct-2.3 (s d)
319 (let ((fp (make-fp-struct-2 :d d :s s)))
320 (declare (dynamic-extent fp))
321 (assert (eql s (fp-struct-2-s fp)))
322 (assert (eql d (fp-struct-2-d fp)))))
324 (defun-with-dx test-fp-struct-2.4 (s d)
325 (let ((fp (make-fp-struct-2 :s s :d d)))
326 (declare (dynamic-extent fp))
327 (assert (eql s (fp-struct-2-s fp)))
328 (assert (eql d (fp-struct-2-d fp)))))
330 (with-test (:name (:test-fp-struct-2.1))
331 (test-fp-struct-2.1 123.456 876.243d0))
332 (with-test (:name (:test-fp-struct-2.2))
333 (test-fp-struct-2.2 123.456 876.243d0))
334 (with-test (:name (:test-fp-struct-2.3))
335 (test-fp-struct-2.3 123.456 876.243d0))
336 (with-test (:name (:test-fp-struct-2.4))
337 (test-fp-struct-2.4 123.456 876.243d0))
339 (declaim (inline make-cfp-struct-1))
340 (defstruct cfp-struct-1
341 (s (complex 0.0) :type (complex single-float))
342 (d (complex 0.0d0) :type (complex double-float)))
344 (defun-with-dx test-cfp-struct-1.1 (s d)
345 (let ((cfp (make-cfp-struct-1 :s s)))
346 (declare (dynamic-extent cfp))
347 (assert (eql s (cfp-struct-1-s cfp)))
348 (assert (eql (complex 0.0d0) (cfp-struct-1-d cfp)))))
350 (defun-with-dx test-cfp-struct-1.2 (s d)
351 (let ((cfp (make-cfp-struct-1 :d d)))
352 (declare (dynamic-extent cfp))
353 (assert (eql (complex 0.0) (cfp-struct-1-s cfp)))
354 (assert (eql d (cfp-struct-1-d cfp)))))
356 (defun-with-dx test-cfp-struct-1.3 (s d)
357 (let ((cfp (make-cfp-struct-1 :d d :s s)))
358 (declare (dynamic-extent cfp))
359 (assert (eql s (cfp-struct-1-s cfp)))
360 (assert (eql d (cfp-struct-1-d cfp)))))
362 (defun-with-dx test-cfp-struct-1.4 (s d)
363 (let ((cfp (make-cfp-struct-1 :s s :d d)))
364 (declare (dynamic-extent cfp))
365 (assert (eql s (cfp-struct-1-s cfp)))
366 (assert (eql d (cfp-struct-1-d cfp)))))
368 (with-test (:name (:test-cfp-struct-1.1))
369 (test-cfp-struct-1.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
370 (with-test (:name (:test-cfp-struct-1.2))
371 (test-cfp-struct-1.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
372 (with-test (:name (:test-cfp-struct-1.3))
373 (test-cfp-struct-1.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
374 (with-test (:name (:test-cfp-struct-1.4))
375 (test-cfp-struct-1.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
377 (declaim (inline make-cfp-struct-2))
378 (defstruct cfp-struct-2
379 (d (complex 0.0d0) :type (complex double-float))
380 (s (complex 0.0) :type (complex single-float)))
382 (defun-with-dx test-cfp-struct-2.1 (s d)
383 (let ((cfp (make-cfp-struct-2 :s s)))
384 (declare (dynamic-extent cfp))
385 (assert (eql s (cfp-struct-2-s cfp)))
386 (assert (eql (complex 0.0d0) (cfp-struct-2-d cfp)))))
388 (defun-with-dx test-cfp-struct-2.2 (s d)
389 (let ((cfp (make-cfp-struct-2 :d d)))
390 (declare (dynamic-extent cfp))
391 (assert (eql (complex 0.0) (cfp-struct-2-s cfp)))
392 (assert (eql d (cfp-struct-2-d cfp)))))
394 (defun-with-dx test-cfp-struct-2.3 (s d)
395 (let ((cfp (make-cfp-struct-2 :d d :s s)))
396 (declare (dynamic-extent cfp))
397 (assert (eql s (cfp-struct-2-s cfp)))
398 (assert (eql d (cfp-struct-2-d cfp)))))
400 (defun-with-dx test-cfp-struct-2.4 (s d)
401 (let ((cfp (make-cfp-struct-2 :s s :d d)))
402 (declare (dynamic-extent cfp))
403 (assert (eql s (cfp-struct-2-s cfp)))
404 (assert (eql d (cfp-struct-2-d cfp)))))
406 (with-test (:name (:test-cfp-struct-2.1))
407 (test-cfp-struct-2.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
408 (with-test (:name (:test-cfp-struct-2.2))
409 (test-cfp-struct-2.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
410 (with-test (:name (:test-cfp-struct-2.3))
411 (test-cfp-struct-2.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
412 (with-test (:name (:test-cfp-struct-2.4))
413 (test-cfp-struct-2.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
415 (declaim (inline make-foo1 make-foo2 make-foo3))
418 (defun-with-dx make-foo1-on-stack (x)
419 (let ((foo (make-foo1 :x x)))
420 (declare (dynamic-extent foo))
421 (assert (eql x (foo1-x foo)))))
424 (x 0.0 :type single-float)
425 (y 0.0d0 :type double-float)
430 (defun-with-dx make-foo2-on-stack (x y)
431 (let ((foo (make-foo2 :y y :c 'c)))
432 (declare (dynamic-extent foo))
433 (assert (eql 0.0 (foo2-x foo)))
434 (assert (eql y (foo2-y foo)))
435 (assert (eql 'c (foo2-c foo)))
436 (assert (eql nil (foo2-b foo)))))
438 ;;; Check that constants work out as argument for all relevant
443 (c 2 :type sb-vm:word)
444 (d 3.0 :type single-float)
445 (e 4.0d0 :type double-float))
447 (defun-with-dx make-foo3-on-stack ()
448 (let ((foo (make-foo3)))
449 (declare (dynamic-extent foo))
450 (assert (eql 0 (foo3-a foo)))
451 (assert (eql 1 (foo3-b foo)))
452 (assert (eql 2 (foo3-c foo)))
453 (assert (eql 3.0 (foo3-d foo)))
454 (assert (eql 4.0d0 (foo3-e foo)))))
458 (defun-with-dx nested-dx-lists ()
459 (let ((dx (list (list 1 2) (list 3 4))))
460 (declare (dynamic-extent dx))
464 (defun-with-dx nested-dx-conses ()
465 (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t) nil))))))
466 (declare (dynamic-extent dx))
470 (defun-with-dx nested-dx-not-used (x)
472 (let ((l (setf (car x) (list x x x))))
473 (declare (dynamic-extent l))
478 (defun-with-dx nested-evil-dx-used (x)
480 (let ((l (list x x x)))
481 (declare (dynamic-extent l))
489 (defparameter *bar* nil)
490 (declaim (inline make-nested-bad make-nested-good))
491 (defstruct (nested (:constructor make-nested-bad (&key bar &aux (bar (setf *bar* bar))))
492 (:constructor make-nested-good (&key bar)))
495 (defun-with-dx nested-good (y)
496 (let ((x (list (list (make-nested-good :bar (list (list (make-nested-good :bar (list y)))))))))
497 (declare (dynamic-extent x))
500 (defun-with-dx nested-bad (y)
501 (let ((x (list (list (make-nested-bad :bar (list (list (make-nested-bad :bar (list y)))))))))
502 (declare (dynamic-extent x))
503 (unless (equalp (caar x) (make-nested-good :bar *bar*))
504 (error "got ~S, wanted ~S" (caar x) (make-nested-good :bar *bar*)))
507 (with-test (:name :conservative-nested-dx)
508 ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
509 (assert (equalp (nested-bad 42) (make-nested-good :bar *bar*)))
510 (assert (equalp *bar* (list (list (make-nested-bad :bar (list 42)))))))
512 ;;; multiple uses for dx lvar
514 (defun-with-dx multiple-dx-uses ()
515 (let ((dx (if (true t)
518 (declare (dynamic-extent dx))
522 ;;; handler-case and handler-bind should use DX internally
524 (defun dx-handler-bind (x)
525 (handler-bind ((error
527 (break "OOPS: ~S caused ~S" x c)))
528 ((and serious-condition (not error))
530 (break "OOPS2: ~S did ~S" x c))))
533 (defun dx-handler-case (x)
534 (assert (zerop (handler-case (/ 2 x)
536 (break "OOPS: ~S caused ~S" x c)
541 (defvar *a-cons* (cons nil nil))
543 (with-test (:name (:no-consing :dx-closures) :skipped-on '(not :stack-allocatable-closures))
544 (assert-no-consing (dxclosure 42)))
546 (with-test (:name (:no-consing :dx-lists) :skipped-on '(not :stack-allocatable-lists))
547 (assert-no-consing (dxlength 1 2 3))
548 (assert-no-consing (dxlength t t t t t t))
549 (assert-no-consing (dxlength))
550 (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
551 (assert-no-consing (test-nip-values))
552 (assert-no-consing (test-let-var-subst2 17))
553 (assert-no-consing (test-lvar-subst 11))
554 (assert-no-consing (nested-dx-lists))
555 (assert-consing (nested-dx-not-used *a-cons*))
556 (assert-no-consing (nested-evil-dx-used *a-cons*))
557 (assert-no-consing (multiple-dx-uses)))
559 (with-test (:name (:no-consing :dx-value-cell))
560 (assert-no-consing (dx-value-cell 13)))
562 (with-test (:name (:no-consing :dx-fixed-objects) :skipped-on '(not :stack-allocatable-fixed-objects))
563 (assert-no-consing (cons-on-stack 42))
564 (assert-no-consing (make-foo1-on-stack 123))
565 (assert-no-consing (nested-good 42))
566 (assert-no-consing (nested-dx-conses))
567 (assert-no-consing (dx-handler-bind 2))
568 (assert-no-consing (dx-handler-case 2)))
570 (with-test (:name (:no-consing :dx-vectors) :skipped-on '(not :stack-allocatable-vectors))
571 (assert-no-consing (force-make-array-on-stack 128))
572 (assert-no-consing (make-array-on-stack-1))
573 (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
574 (assert-no-consing (make-array-on-stack-3 9 8 7))
575 (assert-no-consing (make-array-on-stack-4))
576 (assert-no-consing (make-array-on-stack-5))
577 (assert-no-consing (vector-on-stack :x :y)))
579 (with-test (:name (:no-consing :specialized-dx-vectors)
580 :skipped-on `(not (and :stack-allocatable-vectors
581 :c-stack-is-control-stack)))
582 (assert-no-consing (make-array-on-stack-6))
583 (assert-no-consing (make-array-on-stack-7))
584 (assert-no-consing (make-array-on-stack-8))
585 (assert-no-consing (make-array-on-stack-9))
586 (assert-no-consing (make-array-on-stack-10)))
588 (with-test (:name (:no-consing :dx-raw-instances) :fails-on :ppc :skipped-on '(not :raw-instance-init-vops))
590 (setf a 1.24 b 1.23d0)
591 (assert-no-consing (make-foo2-on-stack a b)))
592 (assert-no-consing (make-foo3-on-stack)))
594 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
596 (defvar *table* (make-hash-table))
598 (defun test-hash-table ()
599 (setf (gethash 5 *table*) 13)
602 ;; This fails on threaded PPC because the hash-table implementation
603 ;; uses recursive system locks, which cons (see below for test
604 ;; (:no-consing :lock), which also fails on threaded PPC).
605 (with-test (:name (:no-consing :hash-tables) :fails-on '(and :ppc :sb-thread))
606 (assert-no-consing (test-hash-table)))
608 ;;; with-mutex should use DX and not cons
610 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
613 (sb-thread:with-mutex (*mutex*)
616 (with-test (:name (:no-consing :mutex) :fails-on :ppc :skipped-on '(not :sb-thread))
617 (assert-no-consing (test-mutex)))
620 ;;; Bugs found by Paul F. Dietz
622 (with-test (:name (:dx-bug-misc :pfdietz))
629 (declare (optimize (speed 2) (space 0) (safety 0)
630 (debug 1) (compilation-speed 3)))
631 (let* ((v5 (cons b b)))
632 (declare (dynamic-extent v5))
637 ;;; bug reported by Svein Ove Aas
638 (defun svein-2005-ii-07 (x y)
639 (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
640 (let ((args (list* y 1 2 x)))
641 (declare (dynamic-extent args))
642 (apply #'aref args)))
644 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
648 #3A(((1 1 1) (1 1 1) (1 1 1))
649 ((1 1 1) (1 1 1) (4 1 1))
650 ((1 1 1) (1 1 1) (1 1 1))))
653 ;;; bug reported by Brian Downing: stack-allocated arrays were not
654 ;;; filled with zeroes.
655 (defun-with-dx bdowning-2005-iv-16 ()
656 (let ((a (make-array 11 :initial-element 0)))
657 (declare (dynamic-extent a))
658 (assert (every (lambda (x) (eql x 0)) a))))
660 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
661 #+(or hppa mips x86 x86-64)
662 (assert-no-consing (bdowning-2005-iv-16))
663 (bdowning-2005-iv-16))
665 (declaim (inline my-nconc))
666 (defun-with-dx my-nconc (&rest lists)
667 (declare (dynamic-extent lists))
668 (apply #'nconc lists))
669 (defun-with-dx my-nconc-caller (a b c)
670 (let ((l1 (list a b c))
673 (with-test (:name :rest-stops-the-buck)
674 (let ((list1 (my-nconc-caller 1 2 3))
675 (list2 (my-nconc-caller 9 8 7)))
676 (assert (equal list1 '(1 2 3 1 2 3)))
677 (assert (equal list2 '(9 8 7 9 8 7)))))
679 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
680 (let* ((a (list x y z))
683 (declare (dynamic-extent c))
684 (values (first c) (second c))))
685 (with-test (:name :let-converted-vars-dx-allocated-bug)
686 (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
687 (assert (and (equal i j)
688 (equal i (list 1 2 3))))))
690 ;;; workaround for bug 419 -- real issue remains, but check that the
692 (defun-with-dx bug419 (x)
693 (multiple-value-call #'list
694 (eval '(values 1 2 3))
696 (declare (dynamic-extent x))
701 (declare (dynamic-extent #'mget #'mset))
702 ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
704 (with-test (:name (:dx-bug-misc :bug419))
705 (assert (equal (bug419 42) '(1 2 3 4 5 6))))
707 ;;; Multiple DX arguments in a local function call
708 (defun test-dx-flet-test (fun n f1 f2 f3)
709 (let ((res (with-output-to-string (s)
710 (assert (eql n (ignore-errors (funcall fun s)))))))
711 (multiple-value-bind (x pos) (read-from-string res nil)
712 (assert (equalp f1 x))
713 (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
714 (assert (equalp f2 y))
715 (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
716 #+(or hppa mips x86 x86-64)
717 (assert-no-consing (assert (eql n (funcall fun nil))))
718 (assert (eql n (funcall fun nil))))
720 (macrolet ((def (n f1 f2 f3)
721 (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
723 (defun-with-dx ,name (s)
725 (declare (dynamic-extent x))
734 (with-test (:name (:dx-flet-test ,n))
735 (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
736 (def 0 (list :one) (list :two) (list :three))
737 (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
738 (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
740 ;;; Test that unknown-values coming after a DX value won't mess up the
742 (defun test-update-uvl-live-sets (x y z)
743 (declare (optimize speed (safety 0)))
745 (declare (dynamic-extent a))
746 (eval `(list (length ',a) ',b))))
748 (bar (list x y z) ; dx push
750 (multiple-value-call 'list
751 (eval '(values 1 2 3)) ; uv push
757 (with-test (:name (:update-uvl-live-sets))
758 (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
759 (test-update-uvl-live-sets #() 4 5))))
761 (with-test (:name :regression-1.0.23.38)
762 (compile nil '(lambda ()
764 (let ((res (cons x x)))
767 (declaim (inline make))
768 (let ((z (make 1 2)))
769 (declare (dynamic-extent z))
772 (compile nil '(lambda ()
774 (let ((res (cons x x)))
777 (declaim (inline make))
778 (let ((z (make 1 2)))
779 (declare (dynamic-extent z))
783 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
784 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
785 ;;; location, leading to all manner of badness. ...reproducing this
786 ;;; reliably is hard, but this it at least used to break on x86-64.
787 (defun length-and-words-packed-in-same-tn (m)
788 (declare (optimize speed (safety 0) (debug 0) (space 0)))
789 (let ((array (make-array (max 1 m) :element-type 'fixnum)))
790 (declare (dynamic-extent array))
791 (array-total-size array)))
792 (with-test (:name :length-and-words-packed-in-same-tn)
793 (assert (= 1 (length-and-words-packed-in-same-tn -3))))
795 (with-test (:name :handler-case-bogus-compiler-note :fails-on :ppc)
797 ((compiler-note (lambda (note)
798 (error "compiler issued note ~S during test" note))))
799 ;; Taken from SWANK, used to signal a bogus stack allocation
802 `(lambda (files fasl-dir load)
803 (let ((needs-recompile nil))
805 (let ((dest (binary-pathname src fasl-dir)))
808 (when (or needs-recompile
809 (not (probe-file dest))
810 (file-newer-p src dest))
811 (setq needs-recompile t)
812 (ensure-directories-exist dest)
813 (compile-file src :output-file dest :print nil :verbose t))
815 (load dest :verbose t)))
816 (serious-condition (c)
817 (handle-loadtime-error c dest))))))))))
819 (declaim (inline foovector barvector))
820 (defun foovector (x y z)
821 (let ((v (make-array 3)))
826 (defun barvector (x y z)
827 (make-array 3 :initial-contents (list x y z)))
828 (with-test (:name :dx-compiler-notes :fails-on :ppc)
829 (flet ((assert-notes (j lambda)
831 (handler-bind ((compiler-note (lambda (c)
836 (error "Wanted ~S notes, got ~S for~% ~S"
838 ;; These ones should complain.
839 (assert-notes 1 `(lambda (x)
840 (let ((v (make-array x)))
841 (declare (dynamic-extent v))
843 (assert-notes 2 `(lambda (x)
844 (let ((y (if (plusp x)
847 (declare (dynamic-extent y))
850 (assert-notes 1 `(lambda (x)
851 (let ((y (foovector x x x)))
852 (declare (sb-int:truly-dynamic-extent y))
855 ;; These ones should not complain.
856 (assert-notes 0 `(lambda (name)
858 ((posix-getenv (function c-string c-string)
861 (alien-funcall posix-getenv name)))))
862 (assert-notes 0 `(lambda (x)
863 (let ((y (barvector x x x)))
864 (declare (dynamic-extent y))
867 (assert-notes 0 `(lambda (list)
868 (declare (optimize (space 0)))
870 (assert-notes 0 `(lambda (other)
872 (ignore-errors (funcall other s c n)))))))
874 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
875 ;;; in an unfortunate loop.
876 (defun handler-case-eating-stack ()
878 (do ((n 0 (logand most-positive-fixnum (1+ n))))
880 (multiple-value-bind (value error) (ignore-errors)
881 (when (and value error) nil))
883 (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
884 (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
885 (with-test (:name :handler-case-eating-stack :fails-on :ppc)
886 (assert-no-consing (handler-case-eating-stack)))
888 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
889 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
890 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
892 `(simple-array single-float (3)))
893 (declaim (ftype (function (t t t) vec) vec))
894 (declaim (inline vec))
896 (make-array 3 :element-type 'single-float :initial-contents (list a b c)))
898 (declare (type vec vec))
900 (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
901 (declare (dynamic-extent vec))
903 (with-test (:name :recheck-nested-dx-bug :fails-on :ppc)
904 (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
905 (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
906 (flet ((foo (x) (declare (ignore x))))
907 (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
908 (assert-no-consing (funcall bad-boy #'foo)))))
910 (with-test (:name :bug-497321)
911 (flet ((test (lambda type)
913 (handler-bind ((condition (lambda (c)
915 (unless (typep c type)
916 (error "wanted ~S for~% ~S~%got ~S"
917 type lambda (type-of c))))))
918 (compile nil lambda))
920 (test `(lambda () (declare (dynamic-extent #'bar)))
922 (test `(lambda () (declare (dynamic-extent bar)))
924 (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
925 'sb-ext:compiler-note)
928 (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
929 'sb-ext:compiler-note)))
931 (with-test (:name :bug-586105 :fails-on '(not (and :stack-allocatable-vectors
932 :stack-allocatable-lists)))
934 (let ((vec (make-array 1 :initial-contents (list (list x)))))
935 (declare (dynamic-extent vec))
936 (assert (eql x (car (aref vec 0)))))))
937 (assert-no-consing (test 42))))
940 (declare (optimize speed))
943 (declare (dynamic-extent #'bar))
944 (do () ((list) (bar))
947 (with-test (:name :bug-681092)
948 (assert (= 10 (bug-681092))))
950 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
951 ;;;; but required by sanity.
953 (declaim (inline rest-stops-dx))
954 (defun-with-dx rest-stops-dx (&rest args)
955 (declare (dynamic-extent args))
956 (apply #'opaque-identity args))
958 (defun-with-dx rest-stops-dx-ok ()
959 (equal '(:foo) (rest-stops-dx (list :foo))))
961 (with-test (:name :rest-stops-dynamic-extent)
962 (assert (rest-stops-dx-ok)))
964 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
966 (with-test (:name :rest-to-more-conversion)
967 (let ((f1 (compile nil `(lambda (f &rest args)
969 (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
970 (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
971 (values (apply f1 args) (apply f2 args))))))
972 (assert-no-consing (multiple-value-bind (a b)
973 (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
975 (assert (and (eql 6 a) (eql -4 b))))))
976 (let ((f3 (compile nil `(lambda (f &optional x &rest args)
978 (apply f x args))))))
979 (assert-no-consing (assert (eql 42 (funcall f3
980 (lambda (a b c) (+ a b c))
984 (let ((f4 (compile nil `(lambda (f &optional x &rest args &key y &allow-other-keys)
985 (apply f y x args)))))
986 (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
994 (let ((f5 (compile nil `(lambda (a b c &rest args)
995 (apply #'list* a b c args)))))
996 (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
997 (let ((f6 (compile nil `(lambda (x y)
998 (declare (optimize speed))
999 (concatenate 'string x y)))))
1000 (assert (equal "foobar" (funcall f6 "foo" "bar"))))
1001 (let ((f7 (compile nil `(lambda (&rest args)
1004 (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
1005 (let ((f8 (compile nil `(lambda (&rest args)
1009 (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
1010 (let ((f9 (compile nil `(lambda (f &rest args)
1013 (declare (dynamic-extent #'foo))
1014 (funcall f #'foo))))))
1015 (assert (equal '(a b c d e f)
1016 (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1017 (let ((f10 (compile nil `(lambda (f &rest args)
1020 (funcall f #'foo))))))
1021 (assert (equal '(a b c d e f)
1022 (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1023 (let ((f11 (compile nil `(lambda (x y z)
1025 (labels ((foo (x &rest rest)
1026 (apply (lambda (&rest rest2)
1027 (return-from out (values-list rest2)))
1032 (multiple-value-bind (a b c) (funcall f11 1 2 3)
1035 (assert (eql c 3)))))
1037 (defun opaque-funcall (function &rest arguments)
1038 (apply function arguments))
1040 (with-test (:name :implicit-value-cells)
1041 (flet ((test-it (type input output)
1042 (let ((f (compile nil `(lambda (x)
1043 (declare (type ,type x))
1046 (declare (dynamic-extent #'inc))
1047 (list (opaque-funcall #'inc) x))))))
1048 (assert (equal (funcall f input)
1049 (list output output))))))
1050 (let ((width sb-vm:n-word-bits))
1051 (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1052 (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1053 (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1054 (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1055 (test-it 'single-float 3f0 4f0)
1056 (test-it 'double-float 3d0 4d0)
1057 (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1058 (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1060 (with-test (:name :sap-implicit-value-cells)
1061 (let ((f (compile nil `(lambda (x)
1062 (declare (type system-area-pointer x))
1064 (setf x (sb-sys:sap+ x 16))))
1065 (declare (dynamic-extent #'inc))
1066 (list (opaque-funcall #'inc) x)))))
1067 (width sb-vm:n-machine-word-bits))
1068 (assert (every (lambda (x)
1069 (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1070 (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))