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:exit :code 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 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))
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))
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))
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)))))
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)))))
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)))))
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)))))
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))
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))
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)))))
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)))))
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)))))
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)))))
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))
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)))
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)))))
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)))))
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)))))
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)))))
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)))
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)))
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)))))
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)))))
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)))))
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)))))
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)))
422 (declaim (inline make-foo1 make-foo2 make-foo3))
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)))))
431 (x 0.0 :type single-float)
432 (y 0.0d0 :type double-float)
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)))))
445 ;;; Check that constants work out as argument for all relevant
450 (c 2 :type sb-vm:word)
451 (d 3.0 :type single-float)
452 (e 4.0d0 :type double-float))
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)))))
465 (defun-with-dx nested-dx-lists ()
466 (let ((dx (list (list 1 2) (list 3 4))))
467 (declare (dynamic-extent dx))
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))
477 (defun-with-dx nested-dx-not-used (x)
479 (let ((l (setf (car x) (list x x x))))
480 (declare (dynamic-extent l))
485 (defun-with-dx nested-evil-dx-used (x)
487 (let ((l (list x x x)))
488 (declare (dynamic-extent l))
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)))
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))
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*)))
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)))))))
519 ;;; multiple uses for dx lvar
521 (defun-with-dx multiple-dx-uses ()
522 (let ((dx (if (true t)
525 (declare (dynamic-extent dx))
529 ;;; handler-case and handler-bind should use DX internally
531 (defun dx-handler-bind (x)
532 (handler-bind ((error
534 (break "OOPS: ~S caused ~S" x c)))
535 ((and serious-condition (not error))
537 (break "OOPS2: ~S did ~S" x c))))
540 (defun dx-handler-case (x)
541 (assert (zerop (handler-case (/ 2 x)
543 (break "OOPS: ~S caused ~S" x c)
548 (defvar *a-cons* (cons nil nil))
550 (with-test (:name (:no-consing :dx-closures) :skipped-on '(not :stack-allocatable-closures))
551 (assert-no-consing (dxclosure 42)))
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)))
566 (with-test (:name (:no-consing :dx-value-cell))
567 (assert-no-consing (dx-value-cell 13)))
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)))
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)))
586 (with-test (:name (:no-consing :specialized-dx-vectors)
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)))
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))))
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)))
604 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
606 (defvar *table* (make-hash-table))
608 (defun test-hash-table ()
609 (setf (gethash 5 *table*) 13)
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)))
618 ;;; with-mutex should use DX and not cons
620 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
623 (sb-thread:with-mutex (*mutex*)
626 (with-test (:name (:no-consing :mutex) :fails-on :ppc :skipped-on '(not :sb-thread))
627 (assert-no-consing (test-mutex)))
630 ;;; Bugs found by Paul F. Dietz
632 (with-test (:name (:dx-bug-misc :pfdietz))
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))
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)))
654 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
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))))
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))))
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))
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))
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)))))
689 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
690 (let* ((a (list x y z))
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))))))
700 ;;; workaround for bug 419 -- real issue remains, but check that the
702 (defun-with-dx bug419 (x)
703 (multiple-value-call #'list
704 (eval '(values 1 2 3))
706 (declare (dynamic-extent x))
711 (declare (dynamic-extent #'mget #'mset))
712 ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
714 (with-test (:name (:dx-bug-misc :bug419))
715 (assert (equal (bug419 42) '(1 2 3 4 5 6))))
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))))
730 (macrolet ((def (n f1 f2 f3)
731 (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
733 (defun-with-dx ,name (s)
735 (declare (dynamic-extent x))
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)))
750 ;;; Test that unknown-values coming after a DX value won't mess up the
752 (defun test-update-uvl-live-sets (x y z)
753 (declare (optimize speed (safety 0)))
755 (declare (dynamic-extent a))
756 (eval `(list (length ',a) ',b))))
758 (bar (list x y z) ; dx push
760 (multiple-value-call 'list
761 (eval '(values 1 2 3)) ; uv push
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))))
771 (with-test (:name :regression-1.0.23.38)
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))
782 (compile nil '(lambda ()
784 (let ((res (cons x x)))
787 (declaim (inline make))
788 (let ((z (make 1 2)))
789 (declare (dynamic-extent z))
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))))
805 (with-test (:name :handler-case-bogus-compiler-note :fails-on :ppc)
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
812 `(lambda (files fasl-dir load)
813 (let ((needs-recompile nil))
815 (let ((dest (binary-pathname src fasl-dir)))
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))
825 (load dest :verbose t)))
826 (serious-condition (c)
827 (handle-loadtime-error c dest))))))))))
829 (declaim (inline foovector barvector))
830 (defun foovector (x y z)
831 (let ((v (make-array 3)))
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)
841 (handler-bind ((compiler-note (lambda (c)
846 (error "Wanted ~S notes, got ~S for~% ~S"
848 ;; These ones should complain.
849 (assert-notes 1 `(lambda (x)
850 (let ((v (make-array x)))
851 (declare (dynamic-extent v))
853 (assert-notes 2 `(lambda (x)
854 (let ((y (if (plusp x)
857 (declare (dynamic-extent y))
860 (assert-notes 1 `(lambda (x)
861 (let ((y (foovector x x x)))
862 (declare (sb-int:truly-dynamic-extent y))
865 ;; These ones should not complain.
866 (assert-notes 0 `(lambda (name)
868 ((posix-getenv (function c-string c-string)
871 (alien-funcall posix-getenv name)))))
872 (assert-notes 0 `(lambda (x)
873 (let ((y (barvector x x x)))
874 (declare (dynamic-extent y))
877 (assert-notes 0 `(lambda (list)
878 (declare (optimize (space 0)))
879 (sort list (lambda (x y) ; shut unrelated notes up
880 (< (truly-the fixnum x)
881 (truly-the fixnum y))))))
882 (assert-notes 0 `(lambda (other)
884 (ignore-errors (funcall other s c n)))))))
886 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
887 ;;; in an unfortunate loop.
888 (defun handler-case-eating-stack ()
890 (do ((n 0 (logand most-positive-fixnum (1+ n))))
892 (multiple-value-bind (value error) (ignore-errors)
893 (when (and value error) nil))
895 (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
896 (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
897 (with-test (:name :handler-case-eating-stack :fails-on :ppc)
898 (assert-no-consing (handler-case-eating-stack)))
900 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
901 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
902 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
904 `(simple-array single-float (3)))
905 (declaim (ftype (function (t t t) vec) vec))
906 (declaim (inline vec))
908 (make-array 3 :element-type 'single-float :initial-contents (list a b c)))
910 (declare (type vec vec))
912 (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
913 (declare (dynamic-extent vec))
915 (with-test (:name :recheck-nested-dx-bug :fails-on :ppc)
916 (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
917 (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
918 (flet ((foo (x) (declare (ignore x))))
919 (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
920 (assert-no-consing (funcall bad-boy #'foo)))))
922 (with-test (:name :bug-497321)
923 (flet ((test (lambda type)
925 (handler-bind ((condition (lambda (c)
927 (unless (typep c type)
928 (error "wanted ~S for~% ~S~%got ~S"
929 type lambda (type-of c))))))
930 (compile nil lambda))
932 (test `(lambda () (declare (dynamic-extent #'bar)))
934 (test `(lambda () (declare (dynamic-extent bar)))
936 (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
937 'sb-ext:compiler-note)
940 (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
941 'sb-ext:compiler-note)))
943 (with-test (:name :bug-586105 :fails-on '(not (and :stack-allocatable-vectors
944 :stack-allocatable-lists)))
946 (let ((vec (make-array 1 :initial-contents (list (list x)))))
947 (declare (dynamic-extent vec))
948 (assert (eql x (car (aref vec 0)))))))
949 (assert-no-consing (test 42))))
952 (declare (optimize speed))
955 (declare (dynamic-extent #'bar))
956 (do () ((list) (bar))
959 (with-test (:name :bug-681092)
960 (assert (= 10 (bug-681092))))
962 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
963 ;;;; but required by sanity.
965 (declaim (inline rest-stops-dx))
966 (defun-with-dx rest-stops-dx (&rest args)
967 (declare (dynamic-extent args))
968 (apply #'opaque-identity args))
970 (defun-with-dx rest-stops-dx-ok ()
971 (equal '(:foo) (rest-stops-dx (list :foo))))
973 (with-test (:name :rest-stops-dynamic-extent)
974 (assert (rest-stops-dx-ok)))
976 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
978 (with-test (:name :rest-to-more-conversion)
979 (let ((f1 (compile nil `(lambda (f &rest args)
981 (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
982 (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
983 (values (apply f1 args) (apply f2 args))))))
984 (assert-no-consing (multiple-value-bind (a b)
985 (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
987 (assert (and (eql 6 a) (eql -4 b))))))
988 (let ((f3 (compile nil `(lambda (f &optional x &rest args)
990 (apply f x args))))))
991 (assert-no-consing (assert (eql 42 (funcall f3
992 (lambda (a b c) (+ a b c))
996 (let ((f4 (compile nil `(lambda (f &optional x &rest args &key y &allow-other-keys)
997 (apply f y x args)))))
998 (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
1006 (let ((f5 (compile nil `(lambda (a b c &rest args)
1007 (apply #'list* a b c args)))))
1008 (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
1009 (let ((f6 (compile nil `(lambda (x y)
1010 (declare (optimize speed))
1011 (concatenate 'string x y)))))
1012 (assert (equal "foobar" (funcall f6 "foo" "bar"))))
1013 (let ((f7 (compile nil `(lambda (&rest args)
1016 (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
1017 (let ((f8 (compile nil `(lambda (&rest args)
1021 (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
1022 (let ((f9 (compile nil `(lambda (f &rest args)
1025 (declare (dynamic-extent #'foo))
1026 (funcall f #'foo))))))
1027 (assert (equal '(a b c d e f)
1028 (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1029 (let ((f10 (compile nil `(lambda (f &rest args)
1032 (funcall f #'foo))))))
1033 (assert (equal '(a b c d e f)
1034 (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1035 (let ((f11 (compile nil `(lambda (x y z)
1037 (labels ((foo (x &rest rest)
1038 (apply (lambda (&rest rest2)
1039 (return-from out (values-list rest2)))
1044 (multiple-value-bind (a b c) (funcall f11 1 2 3)
1047 (assert (eql c 3)))))
1049 (defun opaque-funcall (function &rest arguments)
1050 (apply function arguments))
1052 (with-test (:name :implicit-value-cells)
1053 (flet ((test-it (type input output)
1054 (let ((f (compile nil `(lambda (x)
1055 (declare (type ,type x))
1058 (declare (dynamic-extent #'inc))
1059 (list (opaque-funcall #'inc) x))))))
1060 (assert (equal (funcall f input)
1061 (list output output))))))
1062 (let ((width sb-vm:n-word-bits))
1063 (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1064 (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1065 (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1066 (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1067 (test-it 'single-float 3f0 4f0)
1068 (test-it 'double-float 3d0 4d0)
1069 (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1070 (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1072 (with-test (:name :sap-implicit-value-cells)
1073 (let ((f (compile nil `(lambda (x)
1074 (declare (type system-area-pointer x))
1076 (setf x (sb-sys:sap+ x 16))))
1077 (declare (dynamic-extent #'inc))
1078 (list (opaque-funcall #'inc) x)))))
1079 (width sb-vm:n-machine-word-bits))
1080 (assert (every (lambda (x)
1081 (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1082 (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))