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))
181 (defun-with-dx make-array-on-stack-1 ()
182 (let ((v (make-array '(42) :element-type 'single-float)))
183 (declare (dynamic-extent v))
187 (defun-with-dx make-array-on-stack-2 (n x)
188 (declare (integer n))
189 (let ((v (make-array n :initial-contents x)))
190 (declare (sb-int:truly-dynamic-extent v))
194 (defun-with-dx make-array-on-stack-3 (x y z)
195 (let ((v (make-array 3
196 :element-type 'fixnum :initial-contents (list x y z)
197 :element-type t :initial-contents x)))
198 (declare (sb-int:truly-dynamic-extent v))
202 (defun-with-dx make-array-on-stack-4 ()
203 (let ((v (make-array 3 :initial-contents '(1 2 3))))
204 (declare (sb-int:truly-dynamic-extent v))
208 (defun-with-dx make-array-on-stack-5 ()
209 (let ((v (make-array 3 :initial-element 12 :element-type t)))
210 (declare (sb-int:truly-dynamic-extent v))
214 (defun-with-dx make-array-on-stack-6 ()
215 (let ((v (make-array 3 :initial-element 12 :element-type '(unsigned-byte 8))))
216 (declare (sb-int:truly-dynamic-extent v))
220 (defun-with-dx make-array-on-stack-7 ()
221 (let ((v (make-array 3 :initial-element 12 :element-type '(signed-byte 8))))
222 (declare (sb-int:truly-dynamic-extent v))
226 (defun-with-dx make-array-on-stack-8 ()
227 (let ((v (make-array 3 :initial-element 12 :element-type 'word)))
228 (declare (sb-int:truly-dynamic-extent v))
232 (defun-with-dx make-array-on-stack-9 ()
233 (let ((v (make-array 3 :initial-element 12.0 :element-type 'single-float)))
234 (declare (sb-int:truly-dynamic-extent v))
238 (defun-with-dx make-array-on-stack-10 ()
239 (let ((v (make-array 3 :initial-element 12.0d0 :element-type 'double-float)))
240 (declare (sb-int:truly-dynamic-extent v))
244 (defun-with-dx vector-on-stack (x y)
245 (let ((v (vector 1 x 2 y 3)))
246 (declare (sb-int:truly-dynamic-extent v))
252 (declaim (inline make-fp-struct-1))
253 (defstruct fp-struct-1
254 (s 0.0 :type single-float)
255 (d 0.0d0 :type double-float))
257 (defun-with-dx test-fp-struct-1.1 (s d)
258 (let ((fp (make-fp-struct-1 :s s)))
259 (declare (dynamic-extent fp))
260 (assert (eql s (fp-struct-1-s fp)))
261 (assert (eql 0.0d0 (fp-struct-1-d fp)))))
263 (defun-with-dx test-fp-struct-1.2 (s d)
264 (let ((fp (make-fp-struct-1 :d d)))
265 (declare (dynamic-extent fp))
266 (assert (eql 0.0 (fp-struct-1-s fp)))
267 (assert (eql d (fp-struct-1-d fp)))))
269 (defun-with-dx test-fp-struct-1.3 (s d)
270 (let ((fp (make-fp-struct-1 :d d :s s)))
271 (declare (dynamic-extent fp))
272 (assert (eql s (fp-struct-1-s fp)))
273 (assert (eql d (fp-struct-1-d fp)))))
275 (defun-with-dx test-fp-struct-1.4 (s d)
276 (let ((fp (make-fp-struct-1 :s s :d d)))
277 (declare (dynamic-extent fp))
278 (assert (eql s (fp-struct-1-s fp)))
279 (assert (eql d (fp-struct-1-d fp)))))
281 (with-test (:name (:test-fp-struct-1.1))
282 (test-fp-struct-1.1 123.456 876.243d0))
283 (with-test (:name (:test-fp-struct-1.2))
284 (test-fp-struct-1.2 123.456 876.243d0))
285 (with-test (:name (:test-fp-struct-1.3))
286 (test-fp-struct-1.3 123.456 876.243d0))
287 (with-test (:name (:test-fp-struct-1.4))
288 (test-fp-struct-1.4 123.456 876.243d0))
290 (declaim (inline make-fp-struct-2))
291 (defstruct fp-struct-2
292 (d 0.0d0 :type double-float)
293 (s 0.0 :type single-float))
295 (defun-with-dx test-fp-struct-2.1 (s d)
296 (let ((fp (make-fp-struct-2 :s s)))
297 (declare (dynamic-extent fp))
298 (assert (eql s (fp-struct-2-s fp)))
299 (assert (eql 0.0d0 (fp-struct-2-d fp)))))
301 (defun-with-dx test-fp-struct-2.2 (s d)
302 (let ((fp (make-fp-struct-2 :d d)))
303 (declare (dynamic-extent fp))
304 (assert (eql 0.0 (fp-struct-2-s fp)))
305 (assert (eql d (fp-struct-2-d fp)))))
307 (defun-with-dx test-fp-struct-2.3 (s d)
308 (let ((fp (make-fp-struct-2 :d d :s s)))
309 (declare (dynamic-extent fp))
310 (assert (eql s (fp-struct-2-s fp)))
311 (assert (eql d (fp-struct-2-d fp)))))
313 (defun-with-dx test-fp-struct-2.4 (s d)
314 (let ((fp (make-fp-struct-2 :s s :d d)))
315 (declare (dynamic-extent fp))
316 (assert (eql s (fp-struct-2-s fp)))
317 (assert (eql d (fp-struct-2-d fp)))))
319 (with-test (:name (:test-fp-struct-2.1))
320 (test-fp-struct-2.1 123.456 876.243d0))
321 (with-test (:name (:test-fp-struct-2.2))
322 (test-fp-struct-2.2 123.456 876.243d0))
323 (with-test (:name (:test-fp-struct-2.3))
324 (test-fp-struct-2.3 123.456 876.243d0))
325 (with-test (:name (:test-fp-struct-2.4))
326 (test-fp-struct-2.4 123.456 876.243d0))
328 (declaim (inline make-cfp-struct-1))
329 (defstruct cfp-struct-1
330 (s (complex 0.0) :type (complex single-float))
331 (d (complex 0.0d0) :type (complex double-float)))
333 (defun-with-dx test-cfp-struct-1.1 (s d)
334 (let ((cfp (make-cfp-struct-1 :s s)))
335 (declare (dynamic-extent cfp))
336 (assert (eql s (cfp-struct-1-s cfp)))
337 (assert (eql (complex 0.0d0) (cfp-struct-1-d cfp)))))
339 (defun-with-dx test-cfp-struct-1.2 (s d)
340 (let ((cfp (make-cfp-struct-1 :d d)))
341 (declare (dynamic-extent cfp))
342 (assert (eql (complex 0.0) (cfp-struct-1-s cfp)))
343 (assert (eql d (cfp-struct-1-d cfp)))))
345 (defun-with-dx test-cfp-struct-1.3 (s d)
346 (let ((cfp (make-cfp-struct-1 :d d :s s)))
347 (declare (dynamic-extent cfp))
348 (assert (eql s (cfp-struct-1-s cfp)))
349 (assert (eql d (cfp-struct-1-d cfp)))))
351 (defun-with-dx test-cfp-struct-1.4 (s d)
352 (let ((cfp (make-cfp-struct-1 :s s :d d)))
353 (declare (dynamic-extent cfp))
354 (assert (eql s (cfp-struct-1-s cfp)))
355 (assert (eql d (cfp-struct-1-d cfp)))))
357 (with-test (:name (:test-cfp-struct-1.1))
358 (test-cfp-struct-1.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
359 (with-test (:name (:test-cfp-struct-1.2))
360 (test-cfp-struct-1.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
361 (with-test (:name (:test-cfp-struct-1.3))
362 (test-cfp-struct-1.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
363 (with-test (:name (:test-cfp-struct-1.4))
364 (test-cfp-struct-1.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
366 (declaim (inline make-cfp-struct-2))
367 (defstruct cfp-struct-2
368 (d (complex 0.0d0) :type (complex double-float))
369 (s (complex 0.0) :type (complex single-float)))
371 (defun-with-dx test-cfp-struct-2.1 (s d)
372 (let ((cfp (make-cfp-struct-2 :s s)))
373 (declare (dynamic-extent cfp))
374 (assert (eql s (cfp-struct-2-s cfp)))
375 (assert (eql (complex 0.0d0) (cfp-struct-2-d cfp)))))
377 (defun-with-dx test-cfp-struct-2.2 (s d)
378 (let ((cfp (make-cfp-struct-2 :d d)))
379 (declare (dynamic-extent cfp))
380 (assert (eql (complex 0.0) (cfp-struct-2-s cfp)))
381 (assert (eql d (cfp-struct-2-d cfp)))))
383 (defun-with-dx test-cfp-struct-2.3 (s d)
384 (let ((cfp (make-cfp-struct-2 :d d :s s)))
385 (declare (dynamic-extent cfp))
386 (assert (eql s (cfp-struct-2-s cfp)))
387 (assert (eql d (cfp-struct-2-d cfp)))))
389 (defun-with-dx test-cfp-struct-2.4 (s d)
390 (let ((cfp (make-cfp-struct-2 :s s :d d)))
391 (declare (dynamic-extent cfp))
392 (assert (eql s (cfp-struct-2-s cfp)))
393 (assert (eql d (cfp-struct-2-d cfp)))))
395 (with-test (:name (:test-cfp-struct-2.1))
396 (test-cfp-struct-2.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
397 (with-test (:name (:test-cfp-struct-2.2))
398 (test-cfp-struct-2.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
399 (with-test (:name (:test-cfp-struct-2.3))
400 (test-cfp-struct-2.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
401 (with-test (:name (:test-cfp-struct-2.4))
402 (test-cfp-struct-2.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
404 (declaim (inline make-foo1 make-foo2 make-foo3))
407 (defun-with-dx make-foo1-on-stack (x)
408 (let ((foo (make-foo1 :x x)))
409 (declare (dynamic-extent foo))
410 (assert (eql x (foo1-x foo)))))
413 (x 0.0 :type single-float)
414 (y 0.0d0 :type double-float)
419 (defun-with-dx make-foo2-on-stack (x y)
420 (let ((foo (make-foo2 :y y :c 'c)))
421 (declare (dynamic-extent foo))
422 (assert (eql 0.0 (foo2-x foo)))
423 (assert (eql y (foo2-y foo)))
424 (assert (eql 'c (foo2-c foo)))
425 (assert (eql nil (foo2-b foo)))))
427 ;;; Check that constants work out as argument for all relevant
432 (c 2 :type sb-vm:word)
433 (d 3.0 :type single-float)
434 (e 4.0d0 :type double-float))
436 (defun-with-dx make-foo3-on-stack ()
437 (let ((foo (make-foo3)))
438 (declare (dynamic-extent foo))
439 (assert (eql 0 (foo3-a foo)))
440 (assert (eql 1 (foo3-b foo)))
441 (assert (eql 2 (foo3-c foo)))
442 (assert (eql 3.0 (foo3-d foo)))
443 (assert (eql 4.0d0 (foo3-e foo)))))
447 (defun-with-dx nested-dx-lists ()
448 (let ((dx (list (list 1 2) (list 3 4))))
449 (declare (dynamic-extent dx))
453 (defun-with-dx nested-dx-conses ()
454 (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t) nil))))))
455 (declare (dynamic-extent dx))
459 (defun-with-dx nested-dx-not-used (x)
461 (let ((l (setf (car x) (list x x x))))
462 (declare (dynamic-extent l))
467 (defun-with-dx nested-evil-dx-used (x)
469 (let ((l (list x x x)))
470 (declare (dynamic-extent l))
478 (defparameter *bar* nil)
479 (declaim (inline make-nested-bad make-nested-good))
480 (defstruct (nested (:constructor make-nested-bad (&key bar &aux (bar (setf *bar* bar))))
481 (:constructor make-nested-good (&key bar)))
484 (defun-with-dx nested-good (y)
485 (let ((x (list (list (make-nested-good :bar (list (list (make-nested-good :bar (list y)))))))))
486 (declare (dynamic-extent x))
489 (defun-with-dx nested-bad (y)
490 (let ((x (list (list (make-nested-bad :bar (list (list (make-nested-bad :bar (list y)))))))))
491 (declare (dynamic-extent x))
492 (unless (equalp (caar x) (make-nested-good :bar *bar*))
493 (error "got ~S, wanted ~S" (caar x) (make-nested-good :bar *bar*)))
496 (with-test (:name :conservative-nested-dx)
497 ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
498 (assert (equalp (nested-bad 42) (make-nested-good :bar *bar*)))
499 (assert (equalp *bar* (list (list (make-nested-bad :bar (list 42)))))))
501 ;;; multiple uses for dx lvar
503 (defun-with-dx multiple-dx-uses ()
504 (let ((dx (if (true t)
507 (declare (dynamic-extent dx))
511 ;;; handler-case and handler-bind should use DX internally
513 (defun dx-handler-bind (x)
514 (handler-bind ((error
516 (break "OOPS: ~S caused ~S" x c)))
517 ((and serious-condition (not error))
519 (break "OOPS2: ~S did ~S" x c))))
522 (defun dx-handler-case (x)
523 (assert (zerop (handler-case (/ 2 x)
525 (break "OOPS: ~S caused ~S" x c)
530 (defvar *a-cons* (cons nil nil))
532 (with-test (:name (:no-consing :dx-closures) :skipped-on '(not :stack-allocatable-closures))
533 (assert-no-consing (dxclosure 42)))
535 (with-test (:name (:no-consing :dx-lists) :skipped-on '(not :stack-allocatable-lists))
536 (assert-no-consing (dxlength 1 2 3))
537 (assert-no-consing (dxlength t t t t t t))
538 (assert-no-consing (dxlength))
539 (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
540 (assert-no-consing (test-nip-values))
541 (assert-no-consing (test-let-var-subst2 17))
542 (assert-no-consing (test-lvar-subst 11))
543 (assert-no-consing (nested-dx-lists))
544 (assert-consing (nested-dx-not-used *a-cons*))
545 (assert-no-consing (nested-evil-dx-used *a-cons*))
546 (assert-no-consing (multiple-dx-uses)))
548 (with-test (:name (:no-consing :dx-value-cell))
549 (assert-no-consing (dx-value-cell 13)))
551 (with-test (:name (:no-consing :dx-fixed-objects) :skipped-on '(not :stack-allocatable-fixed-objects))
552 (assert-no-consing (cons-on-stack 42))
553 (assert-no-consing (make-foo1-on-stack 123))
554 (assert-no-consing (nested-good 42))
555 (assert-no-consing (nested-dx-conses))
556 (assert-no-consing (dx-handler-bind 2))
557 (assert-no-consing (dx-handler-case 2)))
559 (with-test (:name (:no-consing :dx-vectors) :skipped-on '(not :stack-allocatable-vectors))
560 (assert-no-consing (force-make-array-on-stack 128))
561 (assert-no-consing (make-array-on-stack-1))
562 (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
563 (assert-no-consing (make-array-on-stack-3 9 8 7))
564 (assert-no-consing (make-array-on-stack-4))
565 (assert-no-consing (make-array-on-stack-5))
566 (assert-no-consing (vector-on-stack :x :y)))
568 (with-test (:name (:no-consing :specialized-dx-vectors)
569 :skipped-on `(not (and :stack-allocatable-vectors
570 :c-stack-is-control-stack)))
571 (assert-no-consing (make-array-on-stack-6))
572 (assert-no-consing (make-array-on-stack-7))
573 (assert-no-consing (make-array-on-stack-8))
574 (assert-no-consing (make-array-on-stack-9))
575 (assert-no-consing (make-array-on-stack-10)))
577 (with-test (:name (:no-consing :dx-raw-instances) :fails-on :ppc :skipped-on '(not :raw-instance-init-vops))
579 (setf a 1.24 b 1.23d0)
580 (assert-no-consing (make-foo2-on-stack a b)))
581 (assert-no-consing (make-foo3-on-stack)))
583 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
585 (defvar *table* (make-hash-table))
587 (defun test-hash-table ()
588 (setf (gethash 5 *table*) 13)
591 ;; This fails on threaded PPC because the hash-table implementation
592 ;; uses recursive system locks, which cons (see below for test
593 ;; (:no-consing :lock), which also fails on threaded PPC).
594 (with-test (:name (:no-consing :hash-tables) :fails-on '(and :ppc :sb-thread))
595 (assert-no-consing (test-hash-table)))
597 ;;; with-mutex should use DX and not cons
599 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
602 (sb-thread:with-mutex (*mutex*)
605 (with-test (:name (:no-consing :mutex) :fails-on :ppc :skipped-on '(not :sb-thread))
606 (assert-no-consing (test-mutex)))
609 ;;; Bugs found by Paul F. Dietz
611 (with-test (:name (:dx-bug-misc :pfdietz))
618 (declare (optimize (speed 2) (space 0) (safety 0)
619 (debug 1) (compilation-speed 3)))
620 (let* ((v5 (cons b b)))
621 (declare (dynamic-extent v5))
626 ;;; bug reported by Svein Ove Aas
627 (defun svein-2005-ii-07 (x y)
628 (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
629 (let ((args (list* y 1 2 x)))
630 (declare (dynamic-extent args))
631 (apply #'aref args)))
633 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
637 #3A(((1 1 1) (1 1 1) (1 1 1))
638 ((1 1 1) (1 1 1) (4 1 1))
639 ((1 1 1) (1 1 1) (1 1 1))))
642 ;;; bug reported by Brian Downing: stack-allocated arrays were not
643 ;;; filled with zeroes.
644 (defun-with-dx bdowning-2005-iv-16 ()
645 (let ((a (make-array 11 :initial-element 0)))
646 (declare (dynamic-extent a))
647 (assert (every (lambda (x) (eql x 0)) a))))
649 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
650 #+(or hppa mips x86 x86-64)
651 (assert-no-consing (bdowning-2005-iv-16))
652 (bdowning-2005-iv-16))
654 (declaim (inline my-nconc))
655 (defun-with-dx my-nconc (&rest lists)
656 (declare (dynamic-extent lists))
657 (apply #'nconc lists))
658 (defun-with-dx my-nconc-caller (a b c)
659 (let ((l1 (list a b c))
662 (with-test (:name :rest-stops-the-buck)
663 (let ((list1 (my-nconc-caller 1 2 3))
664 (list2 (my-nconc-caller 9 8 7)))
665 (assert (equal list1 '(1 2 3 1 2 3)))
666 (assert (equal list2 '(9 8 7 9 8 7)))))
668 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
669 (let* ((a (list x y z))
672 (declare (dynamic-extent c))
673 (values (first c) (second c))))
674 (with-test (:name :let-converted-vars-dx-allocated-bug)
675 (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
676 (assert (and (equal i j)
677 (equal i (list 1 2 3))))))
679 ;;; workaround for bug 419 -- real issue remains, but check that the
681 (defun-with-dx bug419 (x)
682 (multiple-value-call #'list
683 (eval '(values 1 2 3))
685 (declare (dynamic-extent x))
690 (declare (dynamic-extent #'mget #'mset))
691 ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
693 (with-test (:name (:dx-bug-misc :bug419))
694 (assert (equal (bug419 42) '(1 2 3 4 5 6))))
696 ;;; Multiple DX arguments in a local function call
697 (defun test-dx-flet-test (fun n f1 f2 f3)
698 (let ((res (with-output-to-string (s)
699 (assert (eql n (ignore-errors (funcall fun s)))))))
700 (multiple-value-bind (x pos) (read-from-string res nil)
701 (assert (equalp f1 x))
702 (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
703 (assert (equalp f2 y))
704 (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
705 #+(or hppa mips x86 x86-64)
706 (assert-no-consing (assert (eql n (funcall fun nil))))
707 (assert (eql n (funcall fun nil))))
709 (macrolet ((def (n f1 f2 f3)
710 (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
712 (defun-with-dx ,name (s)
714 (declare (dynamic-extent x))
723 (with-test (:name (:dx-flet-test ,n))
724 (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
725 (def 0 (list :one) (list :two) (list :three))
726 (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
727 (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
729 ;;; Test that unknown-values coming after a DX value won't mess up the
731 (defun test-update-uvl-live-sets (x y z)
732 (declare (optimize speed (safety 0)))
734 (declare (dynamic-extent a))
735 (eval `(list (length ',a) ',b))))
737 (bar (list x y z) ; dx push
739 (multiple-value-call 'list
740 (eval '(values 1 2 3)) ; uv push
746 (with-test (:name (:update-uvl-live-sets))
747 (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
748 (test-update-uvl-live-sets #() 4 5))))
750 (with-test (:name :regression-1.0.23.38)
751 (compile nil '(lambda ()
753 (let ((res (cons x x)))
756 (declaim (inline make))
757 (let ((z (make 1 2)))
758 (declare (dynamic-extent z))
761 (compile nil '(lambda ()
763 (let ((res (cons x x)))
766 (declaim (inline make))
767 (let ((z (make 1 2)))
768 (declare (dynamic-extent z))
772 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
773 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
774 ;;; location, leading to all manner of badness. ...reproducing this
775 ;;; reliably is hard, but this it at least used to break on x86-64.
776 (defun length-and-words-packed-in-same-tn (m)
777 (declare (optimize speed (safety 0) (debug 0) (space 0)))
778 (let ((array (make-array (max 1 m) :element-type 'fixnum)))
779 (declare (dynamic-extent array))
780 (array-total-size array)))
781 (with-test (:name :length-and-words-packed-in-same-tn)
782 (assert (= 1 (length-and-words-packed-in-same-tn -3))))
784 (with-test (:name :handler-case-bogus-compiler-note :fails-on :ppc)
786 ((compiler-note (lambda (note)
787 (error "compiler issued note ~S during test" note))))
788 ;; Taken from SWANK, used to signal a bogus stack allocation
791 `(lambda (files fasl-dir load)
792 (let ((needs-recompile nil))
794 (let ((dest (binary-pathname src fasl-dir)))
797 (when (or needs-recompile
798 (not (probe-file dest))
799 (file-newer-p src dest))
800 (setq needs-recompile t)
801 (ensure-directories-exist dest)
802 (compile-file src :output-file dest :print nil :verbose t))
804 (load dest :verbose t)))
805 (serious-condition (c)
806 (handle-loadtime-error c dest))))))))))
808 (declaim (inline foovector barvector))
809 (defun foovector (x y z)
810 (let ((v (make-array 3)))
815 (defun barvector (x y z)
816 (make-array 3 :initial-contents (list x y z)))
817 (with-test (:name :dx-compiler-notes :fails-on :ppc)
818 (flet ((assert-notes (j lambda)
820 (handler-bind ((compiler-note (lambda (c)
825 (error "Wanted ~S notes, got ~S for~% ~S"
827 ;; These ones should complain.
828 (assert-notes 1 `(lambda (x)
829 (let ((v (make-array x)))
830 (declare (dynamic-extent v))
832 (assert-notes 2 `(lambda (x)
833 (let ((y (if (plusp x)
836 (declare (dynamic-extent y))
839 (assert-notes 1 `(lambda (x)
840 (let ((y (foovector x x x)))
841 (declare (sb-int:truly-dynamic-extent y))
844 ;; These ones should not complain.
845 (assert-notes 0 `(lambda (name)
847 ((posix-getenv (function c-string c-string)
850 (alien-funcall posix-getenv name)))))
851 (assert-notes 0 `(lambda (x)
852 (let ((y (barvector x x x)))
853 (declare (dynamic-extent y))
856 (assert-notes 0 `(lambda (list)
857 (declare (optimize (space 0)))
859 (assert-notes 0 `(lambda (other)
861 (ignore-errors (funcall other s c n)))))))
863 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
864 ;;; in an unfortunate loop.
865 (defun handler-case-eating-stack ()
867 (do ((n 0 (logand most-positive-fixnum (1+ n))))
869 (multiple-value-bind (value error) (ignore-errors)
870 (when (and value error) nil))
872 (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
873 (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
874 (with-test (:name :handler-case-eating-stack :fails-on :ppc)
875 (assert-no-consing (handler-case-eating-stack)))
877 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
878 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
879 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
881 `(simple-array single-float (3)))
882 (declaim (ftype (function (t t t) vec) vec))
883 (declaim (inline vec))
885 (make-array 3 :element-type 'single-float :initial-contents (list a b c)))
887 (declare (type vec vec))
889 (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
890 (declare (dynamic-extent vec))
892 (with-test (:name :recheck-nested-dx-bug :fails-on :ppc)
893 (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
894 (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
895 (flet ((foo (x) (declare (ignore x))))
896 (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
897 (assert-no-consing (funcall bad-boy #'foo)))))
899 (with-test (:name :bug-497321)
900 (flet ((test (lambda type)
902 (handler-bind ((condition (lambda (c)
904 (unless (typep c type)
905 (error "wanted ~S for~% ~S~%got ~S"
906 type lambda (type-of c))))))
907 (compile nil lambda))
909 (test `(lambda () (declare (dynamic-extent #'bar)))
911 (test `(lambda () (declare (dynamic-extent bar)))
913 (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
914 'sb-ext:compiler-note)
917 (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
918 'sb-ext:compiler-note)))
920 (with-test (:name :bug-586105 :fails-on '(not (and :stack-allocatable-vectors
921 :stack-allocatable-lists)))
923 (let ((vec (make-array 1 :initial-contents (list (list x)))))
924 (declare (dynamic-extent vec))
925 (assert (eql x (car (aref vec 0)))))))
926 (assert-no-consing (test 42))))
929 (declare (optimize speed))
932 (declare (dynamic-extent #'bar))
933 (do () ((list) (bar))
936 (with-test (:name :bug-681092)
937 (assert (= 10 (bug-681092))))
939 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
940 ;;;; but required by sanity.
942 (declaim (inline rest-stops-dx))
943 (defun-with-dx rest-stops-dx (&rest args)
944 (declare (dynamic-extent args))
945 (apply #'opaque-identity args))
947 (defun-with-dx rest-stops-dx-ok ()
948 (equal '(:foo) (rest-stops-dx (list :foo))))
950 (with-test (:name :rest-stops-dynamic-extent)
951 (assert (rest-stops-dx-ok)))
953 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
955 (with-test (:name :rest-to-more-conversion)
956 (let ((f1 (compile nil `(lambda (f &rest args)
958 (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
959 (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
960 (values (apply f1 args) (apply f2 args))))))
961 (assert-no-consing (multiple-value-bind (a b)
962 (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
964 (assert (and (eql 6 a) (eql -4 b))))))
965 (let ((f3 (compile nil `(lambda (f &optional x &rest args)
967 (apply f x args))))))
968 (assert-no-consing (assert (eql 42 (funcall f3
969 (lambda (a b c) (+ a b c))
973 (let ((f4 (compile nil `(lambda (f &optional x &rest args &key y &allow-other-keys)
974 (apply f y x args)))))
975 (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
983 (let ((f5 (compile nil `(lambda (a b c &rest args)
984 (apply #'list* a b c args)))))
985 (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
986 (let ((f6 (compile nil `(lambda (x y)
987 (declare (optimize speed))
988 (concatenate 'string x y)))))
989 (assert (equal "foobar" (funcall f6 "foo" "bar"))))
990 (let ((f7 (compile nil `(lambda (&rest args)
993 (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
994 (let ((f8 (compile nil `(lambda (&rest args)
998 (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
999 (let ((f9 (compile nil `(lambda (f &rest args)
1002 (declare (dynamic-extent #'foo))
1003 (funcall f #'foo))))))
1004 (assert (equal '(a b c d e f)
1005 (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1006 (let ((f10 (compile nil `(lambda (f &rest args)
1009 (funcall f #'foo))))))
1010 (assert (equal '(a b c d e f)
1011 (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1012 (let ((f11 (compile nil `(lambda (x y z)
1014 (labels ((foo (x &rest rest)
1015 (apply (lambda (&rest rest2)
1016 (return-from out (values-list rest2)))
1021 (multiple-value-bind (a b c) (funcall f11 1 2 3)
1024 (assert (eql c 3)))))
1026 (defun opaque-funcall (function &rest arguments)
1027 (apply function arguments))
1029 (with-test (:name :implicit-value-cells)
1030 (flet ((test-it (type input output)
1031 (let ((f (compile nil `(lambda (x)
1032 (declare (type ,type x))
1035 (declare (dynamic-extent #'inc))
1036 (list (opaque-funcall #'inc) x))))))
1037 (assert (equal (funcall f input)
1038 (list output output))))))
1039 (let ((width sb-vm:n-word-bits))
1040 (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1041 (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1042 (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1043 (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1044 (test-it 'single-float 3f0 4f0)
1045 (test-it 'double-float 3d0 4d0)
1046 (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1047 (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1049 (with-test (:name :sap-implicit-value-cells)
1050 (let ((f (compile nil `(lambda (x)
1051 (declare (type system-area-pointer x))
1053 (setf x (sb-sys:sap+ x 16))))
1054 (declare (dynamic-extent #'inc))
1055 (list (opaque-funcall #'inc) x)))))
1056 (width sb-vm:n-machine-word-bits))
1057 (assert (every (lambda (x)
1058 (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1059 (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))