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
)
24 (declare (optimize (speed 3) (safety 0) (space 0)))
25 (declare (muffle-conditions style-warning
)) ; re F1 and F2
27 (progn (ignore-errors (error "Foo")) (write-char #\.
))
29 (use-value (new) (f2 new
))))
32 (with-test (:name
:do-not-dxify-restarts
)
33 (let ((a (make-array 100))) (unwind-protect (crashme 'bork
) (fill a
0)))
34 (let ((a (make-array 100))) (unwind-protect (crashme 'bork
) (fill a
0)))
35 (let ((a (make-array 100))) (unwind-protect (crashme 'bork
) (fill a
0)))
36 (let ((a (make-array 100))) (unwind-protect (crashme 'bork
) (fill a
0)))
39 (defmacro defun-with-dx
(name arglist
&body body
)
40 (let ((debug-name (sb-int:symbolicate name
"-HIGH-DEBUG"))
41 (default-name (sb-int:symbolicate name
"-DEFAULT")))
43 (defun ,debug-name
,arglist
44 (declare (optimize debug
))
46 (defun ,default-name
,arglist
48 (defun ,name
(&rest args
)
49 (apply #',debug-name args
)
50 (apply #',default-name args
)))))
52 (declaim (notinline opaque-identity
))
53 (defun opaque-identity (x)
58 (defun-with-dx dxlength
(&rest rest
)
59 (declare (dynamic-extent rest
))
62 (with-test (:name
(:dx-
&rest
:basics
))
63 (assert (= (dxlength 1 2 3) 3))
64 (assert (= (dxlength t t t t t t
) 6))
65 (assert (= (dxlength) 0)))
68 (destructuring-bind (a b c d e f
&rest g
) list
69 (+ a b c d e f
(length g
))))
71 (defun-with-dx dxcaller
(&rest rest
)
72 (declare (dynamic-extent rest
))
75 (with-test (:name
(:dx-
&rest
:pass-down-to-callee
:tail-call
))
76 (assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
78 (defun-with-dx dxcaller-align-1
(x &rest rest
)
79 (declare (dynamic-extent rest
))
82 (with-test (:name
(:dx-
&rest
:pass-down-to-callee
:non-tail-call
))
83 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
84 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40)))
88 (defun-with-dx test-nip-values
()
89 (flet ((bar (x &rest y
)
90 (declare (dynamic-extent y
))
94 (multiple-value-call #'values
98 (with-test (:name
(:nip-values
))
99 (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a
))))
101 ;;; LET-variable substitution
103 (defun-with-dx test-let-var-subst1
(x)
104 (let ((y (list x
(1- x
))))
105 (opaque-identity :foo
)
106 (let ((z (the list y
)))
107 (declare (dynamic-extent z
))
110 (with-test (:name
(:let-variable-substitution
))
111 (assert (eql (test-let-var-subst1 17) 2)))
113 (defun-with-dx test-let-var-subst2
(x)
114 (let ((y (list x
(1- x
))))
115 (declare (dynamic-extent y
))
116 (opaque-identity :foo
)
117 (let ((z (the list y
)))
120 (with-test (:name
(:let-variable-substitution-2
))
121 (assert (eql (test-let-var-subst2 17) 2)))
124 ;;; DX propagation through LET-return.
126 (defun-with-dx test-lvar-subst
(x)
127 (let ((y (list x
(1- x
))))
128 (declare (dynamic-extent y
))
129 (second (let ((z (the list y
)))
130 (opaque-identity :foo
)
133 (with-test (:name
(:dx-propagation-through-let-return
))
134 (assert (eql (test-lvar-subst 11) 10)))
136 ;;; this code is incorrect, but the compiler should not fail
137 (defun-with-dx test-let-var-subst-incorrect
(x)
138 (let ((y (list x
(1- x
))))
139 (opaque-identity :foo
)
140 (let ((z (the list y
)))
141 (declare (dynamic-extent z
))
142 (opaque-identity :bar
)
148 (defun-with-dx test-alignment-dx-list
(form)
149 (multiple-value-prog1 (eval form
)
150 (let ((l (list 1 2 3 4)))
151 (declare (dynamic-extent l
))
152 (setq *x
* (copy-list l
)))))
154 (with-test (:name
(:dx-list
:alignment
))
156 (let* ((res (loop for i below n collect i
))
157 (form `(values ,@res
)))
158 (assert (equal (multiple-value-list (test-alignment-dx-list form
)) res
))
159 (assert (equal *x
* '(1 2 3 4))))))
163 (declaim (notinline true
))
168 (defun-with-dx dxclosure
(x)
171 (declare (dynamic-extent #'f
))
174 (with-test (:name
(:dx-closure
))
175 (assert (eq t
(dxclosure 13))))
179 (defun-with-dx dx-value-cell
(x)
180 ;; Not implemented everywhere, yet.
181 #+(or x86 x86-64 mips hppa
)
183 (declare (sb-int:truly-dynamic-extent cell
))
186 (declare (dynamic-extent #'f
))
191 (defun-with-dx cons-on-stack
(x)
192 (let ((cons (cons x x
)))
193 (declare (dynamic-extent cons
))
199 (defun force-make-array-on-stack (n)
200 (declare (optimize safety
))
201 (let ((v (make-array (min n
1))))
202 (declare (sb-int:truly-dynamic-extent v
))
207 (defun-with-dx make-array-on-stack-1
()
208 (let ((v (make-array '(42) :element-type
'single-float
)))
209 (declare (dynamic-extent v
))
214 (defun-with-dx make-array-on-stack-2
(n x
)
215 (declare (integer n
))
216 (let ((v (make-array n
:initial-contents x
)))
217 (declare (sb-int:truly-dynamic-extent v
))
222 (defun-with-dx make-array-on-stack-3
(x y z
)
223 (let ((v (make-array 3
224 :element-type
'fixnum
:initial-contents
(list x y z
)
225 :element-type t
:initial-contents x
)))
226 (declare (sb-int:truly-dynamic-extent v
))
231 (defun-with-dx make-array-on-stack-4
()
232 (let ((v (make-array 3 :initial-contents
'(1 2 3))))
233 (declare (sb-int:truly-dynamic-extent v
))
238 (defun-with-dx make-array-on-stack-5
()
239 (let ((v (make-array 3 :initial-element
12 :element-type t
)))
240 (declare (sb-int:truly-dynamic-extent v
))
245 (defun-with-dx make-array-on-stack-6
()
246 (let ((v (make-array 3 :initial-element
12 :element-type
'(unsigned-byte 8))))
247 (declare (sb-int:truly-dynamic-extent v
))
252 (defun-with-dx make-array-on-stack-7
()
253 (let ((v (make-array 3 :initial-element
12 :element-type
'(signed-byte 8))))
254 (declare (sb-int:truly-dynamic-extent v
))
259 (defun-with-dx make-array-on-stack-8
()
260 (let ((v (make-array 3 :initial-element
12 :element-type
'word
)))
261 (declare (sb-int:truly-dynamic-extent v
))
266 (defun-with-dx make-array-on-stack-9
()
267 (let ((v (make-array 3 :initial-element
12.0 :element-type
'single-float
)))
268 (declare (sb-int:truly-dynamic-extent v
))
273 (defun-with-dx make-array-on-stack-10
()
274 (let ((v (make-array 3 :initial-element
12.0d0
:element-type
'double-float
)))
275 (declare (sb-int:truly-dynamic-extent v
))
280 (defun-with-dx make-array-on-stack-11
()
281 (let ((v (make-array (the integer
(opaque-identity 3)) :initial-element
12.0d0
:element-type
'double-float
)))
282 (declare (sb-int:truly-dynamic-extent v
))
287 (defun-with-dx vector-on-stack
(x y
)
288 (let ((v (vector 1 x
2 y
3)))
289 (declare (sb-int:truly-dynamic-extent v
))
293 (defun-with-dx make-3d-fixed-array-on-stack-1
()
294 (let ((a (make-array '(4 8 3) :initial-element
12 :element-type t
)))
295 (declare (sb-int:truly-dynamic-extent a
))
299 (defun-with-dx make-3d-fixed-array-on-stack-2
(a b c d
)
300 (sb-int:dx-let
((a (make-array '(2 2 1)
302 :initial-contents
`(#((,a
) (,b
)) (#(,c
) (,d
))))))
307 (defun-with-dx make-2d-variable-array-on-stack
()
308 (let* ((n (opaque-identity 5))
309 (a (make-array `(,n
2) :initial-element
12 :element-type t
)))
310 (declare (sb-int:truly-dynamic-extent a
))
315 (defun 2d-array-initializer (n)
319 (3 '((a b c
) (c d e
) (f g h
)))))
321 (defun-with-dx make-2d-array-function-initializer
(n)
322 (let* ((x (opaque-identity n
))
323 (y (opaque-identity x
))
324 (a (make-array `(,x
,y
) :initial-contents
(2d-array-initializer x
))))
325 (declare (sb-int:truly-dynamic-extent a
))
332 (declaim (inline make-list-container
))
333 (defstruct list-container listy-slot
)
334 (defun make-var-length-dx-list (n thunk
)
335 (sb-int:dx-let
((s (make-list-container :listy-slot
(make-list n
))))
337 ;; :stack-allocatable-lists is necessary but not sufficient
338 (with-test (:name
(:dx-list
:make-list
) :skipped-on
'(not :x86-64
))
339 (assert (null (ctu:find-named-callees
#'make-var-length-dx-list
)))
340 (assert-no-consing (make-var-length-dx-list
341 50 (lambda (x) (declare (ignore x
))))))
345 (declaim (inline make-fp-struct-1
))
346 (defstruct fp-struct-1
347 (s 0.0 :type single-float
)
348 (d 0.0d0
:type double-float
))
350 (defun-with-dx test-fp-struct-1.1
(s d
)
352 (let ((fp (make-fp-struct-1 :s s
)))
353 (declare (dynamic-extent fp
))
354 (assert (eql s
(fp-struct-1-s fp
)))
355 (assert (eql 0.0d0
(fp-struct-1-d fp
)))))
357 (defun-with-dx test-fp-struct-1.2
(s d
)
359 (let ((fp (make-fp-struct-1 :d d
)))
360 (declare (dynamic-extent fp
))
361 (assert (eql 0.0 (fp-struct-1-s fp
)))
362 (assert (eql d
(fp-struct-1-d fp
)))))
364 (defun-with-dx test-fp-struct-1.3
(s d
)
365 (let ((fp (make-fp-struct-1 :d d
:s s
)))
366 (declare (dynamic-extent fp
))
367 (assert (eql s
(fp-struct-1-s fp
)))
368 (assert (eql d
(fp-struct-1-d fp
)))))
370 (defun-with-dx test-fp-struct-1.4
(s d
)
371 (let ((fp (make-fp-struct-1 :s s
:d d
)))
372 (declare (dynamic-extent fp
))
373 (assert (eql s
(fp-struct-1-s fp
)))
374 (assert (eql d
(fp-struct-1-d fp
)))))
376 (with-test (:name
(:test-fp-struct-1.1
))
377 (test-fp-struct-1.1
123.456 876.243d0
))
378 (with-test (:name
(:test-fp-struct-1.2
))
379 (test-fp-struct-1.2
123.456 876.243d0
))
380 (with-test (:name
(:test-fp-struct-1.3
))
381 (test-fp-struct-1.3
123.456 876.243d0
))
382 (with-test (:name
(:test-fp-struct-1.4
))
383 (test-fp-struct-1.4
123.456 876.243d0
))
385 (declaim (inline make-fp-struct-2
))
386 (defstruct fp-struct-2
387 (d 0.0d0
:type double-float
)
388 (s 0.0 :type single-float
))
390 (defun-with-dx test-fp-struct-2.1
(s d
)
392 (let ((fp (make-fp-struct-2 :s s
)))
393 (declare (dynamic-extent fp
))
394 (assert (eql s
(fp-struct-2-s fp
)))
395 (assert (eql 0.0d0
(fp-struct-2-d fp
)))))
397 (defun-with-dx test-fp-struct-2.2
(s d
)
399 (let ((fp (make-fp-struct-2 :d d
)))
400 (declare (dynamic-extent fp
))
401 (assert (eql 0.0 (fp-struct-2-s fp
)))
402 (assert (eql d
(fp-struct-2-d fp
)))))
404 (defun-with-dx test-fp-struct-2.3
(s d
)
405 (let ((fp (make-fp-struct-2 :d d
:s s
)))
406 (declare (dynamic-extent fp
))
407 (assert (eql s
(fp-struct-2-s fp
)))
408 (assert (eql d
(fp-struct-2-d fp
)))))
410 (defun-with-dx test-fp-struct-2.4
(s d
)
411 (let ((fp (make-fp-struct-2 :s s
:d d
)))
412 (declare (dynamic-extent fp
))
413 (assert (eql s
(fp-struct-2-s fp
)))
414 (assert (eql d
(fp-struct-2-d fp
)))))
416 (with-test (:name
(:test-fp-struct-2.1
))
417 (test-fp-struct-2.1
123.456 876.243d0
))
418 (with-test (:name
(:test-fp-struct-2.2
))
419 (test-fp-struct-2.2
123.456 876.243d0
))
420 (with-test (:name
(:test-fp-struct-2.3
))
421 (test-fp-struct-2.3
123.456 876.243d0
))
422 (with-test (:name
(:test-fp-struct-2.4
))
423 (test-fp-struct-2.4
123.456 876.243d0
))
425 (declaim (inline make-cfp-struct-1
))
426 (defstruct cfp-struct-1
427 (s (complex 0.0) :type
(complex single-float
))
428 (d (complex 0.0d0
) :type
(complex double-float
)))
430 (defun-with-dx test-cfp-struct-1.1
(s d
)
432 (let ((cfp (make-cfp-struct-1 :s s
)))
433 (declare (dynamic-extent cfp
))
434 (assert (eql s
(cfp-struct-1-s cfp
)))
435 (assert (eql (complex 0.0d0
) (cfp-struct-1-d cfp
)))))
437 (defun-with-dx test-cfp-struct-1.2
(s d
)
439 (let ((cfp (make-cfp-struct-1 :d d
)))
440 (declare (dynamic-extent cfp
))
441 (assert (eql (complex 0.0) (cfp-struct-1-s cfp
)))
442 (assert (eql d
(cfp-struct-1-d cfp
)))))
444 (defun-with-dx test-cfp-struct-1.3
(s d
)
445 (let ((cfp (make-cfp-struct-1 :d d
:s s
)))
446 (declare (dynamic-extent cfp
))
447 (assert (eql s
(cfp-struct-1-s cfp
)))
448 (assert (eql d
(cfp-struct-1-d cfp
)))))
450 (defun-with-dx test-cfp-struct-1.4
(s d
)
451 (let ((cfp (make-cfp-struct-1 :s s
:d d
)))
452 (declare (dynamic-extent cfp
))
453 (assert (eql s
(cfp-struct-1-s cfp
)))
454 (assert (eql d
(cfp-struct-1-d cfp
)))))
456 (with-test (:name
(:test-cfp-struct-1.1
))
457 (test-cfp-struct-1.1
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
458 (with-test (:name
(:test-cfp-struct-1.2
))
459 (test-cfp-struct-1.2
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
460 (with-test (:name
(:test-cfp-struct-1.3
))
461 (test-cfp-struct-1.3
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
462 (with-test (:name
(:test-cfp-struct-1.4
))
463 (test-cfp-struct-1.4
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
465 (declaim (inline make-cfp-struct-2
))
466 (defstruct cfp-struct-2
467 (d (complex 0.0d0
) :type
(complex double-float
))
468 (s (complex 0.0) :type
(complex single-float
)))
470 (defun-with-dx test-cfp-struct-2.1
(s d
)
472 (let ((cfp (make-cfp-struct-2 :s s
)))
473 (declare (dynamic-extent cfp
))
474 (assert (eql s
(cfp-struct-2-s cfp
)))
475 (assert (eql (complex 0.0d0
) (cfp-struct-2-d cfp
)))))
477 (defun-with-dx test-cfp-struct-2.2
(s d
)
479 (let ((cfp (make-cfp-struct-2 :d d
)))
480 (declare (dynamic-extent cfp
))
481 (assert (eql (complex 0.0) (cfp-struct-2-s cfp
)))
482 (assert (eql d
(cfp-struct-2-d cfp
)))))
484 (defun-with-dx test-cfp-struct-2.3
(s d
)
485 (let ((cfp (make-cfp-struct-2 :d d
:s s
)))
486 (declare (dynamic-extent cfp
))
487 (assert (eql s
(cfp-struct-2-s cfp
)))
488 (assert (eql d
(cfp-struct-2-d cfp
)))))
490 (defun-with-dx test-cfp-struct-2.4
(s d
)
491 (let ((cfp (make-cfp-struct-2 :s s
:d d
)))
492 (declare (dynamic-extent cfp
))
493 (assert (eql s
(cfp-struct-2-s cfp
)))
494 (assert (eql d
(cfp-struct-2-d cfp
)))))
496 (with-test (:name
(:test-cfp-struct-2.1
))
497 (test-cfp-struct-2.1
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
498 (with-test (:name
(:test-cfp-struct-2.2
))
499 (test-cfp-struct-2.2
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
500 (with-test (:name
(:test-cfp-struct-2.3
))
501 (test-cfp-struct-2.3
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
502 (with-test (:name
(:test-cfp-struct-2.4
))
503 (test-cfp-struct-2.4
(complex 0.123 123.456) (complex 908132.41d0
876.243d0
)))
505 ;; It works to declare a structure constructor INLINE after the DEFSTRUCT
506 ;; was processed, as long as it was in the null lexical environment.
507 ;; In a perfect world, we'd figure out that a variable declared DX which
508 ;; receives the value of a structure constructor defined in the same file
509 ;; and neither expressly INLINE nor NOTINLINE should be locally inlined.
510 ;; But at least this shows that a declaration at the intended use point
511 ;; is sufficient, without also a bracketing INLINE/NOTINLINE at the DEFSTRUCT.
513 ;; Verify the precondition for the assertions that claim that DXifying works
514 ;; even though the MAKE- function was not expressly INLINE when its DEFSTRUCT
516 (dolist (s '(make-foo1 make-foo2 make-foo3
))
517 (assert (null (sb-int:info
:function
:inline-expansion-designator s
))))
521 (defun-with-dx make-foo1-on-stack
(x)
522 (declare (inline make-foo1
))
523 (let ((foo (make-foo1 :x x
)))
524 (declare (dynamic-extent foo
))
525 (assert (eql x
(foo1-x foo
)))))
528 (x 0.0 :type single-float
)
529 (y 0.0d0
:type double-float
)
534 (defun-with-dx make-foo2-on-stack
(x y
)
535 (declare (inline make-foo2
))
537 (let ((foo (make-foo2 :y y
:c
'c
)))
538 (declare (dynamic-extent foo
))
539 (assert (eql 0.0 (foo2-x foo
)))
540 (assert (eql y
(foo2-y foo
)))
541 (assert (eql 'c
(foo2-c foo
)))
542 (assert (eql nil
(foo2-b foo
)))))
544 ;;; Check that constants work out as argument for all relevant
549 (c 2 :type sb-vm
:word
)
550 (d 3.0 :type single-float
)
551 (e 4.0d0
:type double-float
))
553 (defun-with-dx make-foo3-on-stack
()
554 (declare (inline make-foo3
))
555 (let ((foo (make-foo3)))
556 (declare (dynamic-extent foo
))
557 (assert (eql 0 (foo3-a foo
)))
558 (assert (eql 1 (foo3-b foo
)))
559 (assert (eql 2 (foo3-c foo
)))
560 (assert (eql 3.0 (foo3-d foo
)))
561 (assert (eql 4.0d0
(foo3-e foo
)))))
565 (defun-with-dx nested-dx-lists
()
566 (let ((dx (list (list 1 2) (list 3 4))))
567 (declare (dynamic-extent dx
))
571 (defun-with-dx nested-dx-conses
()
572 (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t
) nil
))))))
573 (declare (dynamic-extent dx
))
577 (defun-with-dx nested-dx-not-used
(x)
579 (let ((l (setf (car x
) (list x x x
))))
580 (declare (dynamic-extent l
))
585 (defun-with-dx nested-evil-dx-used
(x)
587 (let ((l (list x x x
)))
588 (declare (dynamic-extent l
))
596 (defparameter *bar
* nil
)
597 (declaim (inline make-nested-bad make-nested-good
))
598 (defstruct (nested (:constructor make-nested-bad
(&key bar
&aux
(bar (setf *bar
* bar
))))
599 (:constructor make-nested-good
(&key bar
)))
602 (defun-with-dx nested-good
(y)
603 (let ((x (list (list (make-nested-good :bar
(list (list (make-nested-good :bar
(list y
)))))))))
604 (declare (dynamic-extent x
))
607 (defun-with-dx nested-bad
(y)
608 (let ((x (list (list (make-nested-bad :bar
(list (list (make-nested-bad :bar
(list y
)))))))))
609 (declare (dynamic-extent x
))
610 (unless (equalp (caar x
) (make-nested-good :bar
*bar
*))
611 (error "got ~S, wanted ~S" (caar x
) (make-nested-good :bar
*bar
*)))
612 ;; the NESTED instance itself *should* be DX!
613 (copy-nested (caar x
))))
615 (with-test (:name
:conservative-nested-dx
)
616 ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
617 (assert (equalp (nested-bad 42) (make-nested-good :bar
*bar
*)))
618 (assert (equalp *bar
* (list (list (make-nested-bad :bar
(list 42)))))))
620 ;;; multiple uses for dx lvar
622 (defun-with-dx multiple-dx-uses
()
623 (let ((dx (if (true t
)
626 (declare (dynamic-extent dx
))
630 ;;; mapfoo should make the initial cons dx
632 (defun mapcar-negate (x) (mapcar #'- x
))
633 (defun mapcan-reverse (x) (mapcan #'reverse x
))
635 ;;; handler-case and handler-bind should use DX internally
637 (defun dx-handler-bind (x)
638 (handler-bind ((error
640 (break "OOPS: ~S caused ~S" x c
)))
641 ((and serious-condition
(not error
))
643 (break "OOPS2: ~S did ~S" x c
))))
646 (defun dx-handler-case (x)
647 (assert (zerop (handler-case (/ 2 x
)
649 (break "OOPS: ~S caused ~S" x c
)
654 (defun list-delete-some-stuff ()
655 ;; opaque-identity hides the fact that we are calling a destructive function
656 ;; on a constant, which is technically illegal. But no deletion occurs,
657 ;; so it's innocuous. Also these aren't tests of DX, but oh well...
658 (declare (muffle-conditions style-warning
))
659 (delete 'a
(opaque-identity '(x y
)))
660 (delete 'a
(opaque-identity '(x y
)) :from-end t
)
661 (delete-duplicates (opaque-identity '(x y
))))
663 (defvar *a-cons
* (cons nil nil
))
665 (with-test (:name
(:no-consing
:dx-closures
) :skipped-on
'(not :stack-allocatable-closures
))
666 (assert-no-consing (dxclosure 42)))
668 (with-test (:name
(:no-consing
:dx-lists
)
669 :skipped-on
'(not (and :stack-allocatable-lists
670 :stack-allocatable-fixed-objects
)))
671 (assert-no-consing (dxlength 1 2 3))
672 (assert-no-consing (dxlength t t t t t t
))
673 (assert-no-consing (dxlength))
674 (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
675 (assert-no-consing (test-nip-values))
676 (assert-no-consing (test-let-var-subst2 17))
677 (assert-no-consing (test-lvar-subst 11))
678 (assert-no-consing (nested-dx-lists))
679 (assert-consing (nested-dx-not-used *a-cons
*))
680 (assert-no-consing (nested-evil-dx-used *a-cons
*))
681 (assert-no-consing (mapcar-negate nil
))
682 (assert-no-consing (mapcan-reverse nil
))
683 (assert-no-consing (list-delete-some-stuff))
684 (assert-no-consing (multiple-dx-uses)))
686 (with-test (:name
(:no-consing
:dx-value-cell
)
687 :skipped-on
'(not :stack-allocatable-closures
))
688 (assert-no-consing (dx-value-cell 13)))
690 (with-test (:name
(:no-consing
:dx-fixed-objects
)
691 :skipped-on
'(not (and :stack-allocatable-fixed-objects
692 :stack-allocatable-closures
)))
693 (assert-no-consing (cons-on-stack 42))
694 (assert-no-consing (make-foo1-on-stack 123))
695 (assert-no-consing (nested-good 42))
696 (assert-no-consing (nested-dx-conses))
697 (assert-no-consing (dx-handler-bind 2))
698 (assert-no-consing (dx-handler-case 2)))
700 (with-test (:name
(:no-consing
:dx-vectors
) :skipped-on
'(not :stack-allocatable-vectors
))
701 (assert-no-consing (force-make-array-on-stack 128))
702 (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
703 (assert-no-consing (make-array-on-stack-3 9 8 7))
704 (assert-no-consing (make-array-on-stack-4))
705 (assert-no-consing (make-array-on-stack-5))
706 (assert-no-consing (vector-on-stack :x
:y
)))
708 (with-test (:name
(:no-consing
:dx-arrays
) :skipped-on
'(not :stack-allocatable-vectors
))
709 (assert-no-consing (make-3d-fixed-array-on-stack-1))
710 (assert-no-consing (make-2d-variable-array-on-stack))
711 (assert-no-consing (make-2d-array-function-initializer 1))
712 (assert-no-consing (make-2d-array-function-initializer 2))
713 (assert-no-consing (make-2d-array-function-initializer 3)))
715 (with-test (:name
(:no-consing
:dx-specialized-arrays
)
716 :skipped-on
'(not (and :stack-allocatable-vectors
717 :c-stack-is-control-stack
)))
718 (assert-no-consing (make-3d-fixed-array-on-stack-2 0 0 1 1)))
720 (with-test (:name
(:no-consing
:specialized-dx-vectors
)
721 :skipped-on
`(not (and :stack-allocatable-vectors
722 :c-stack-is-control-stack
)))
723 (assert-no-consing (make-array-on-stack-1))
724 (assert-no-consing (make-array-on-stack-6))
725 (assert-no-consing (make-array-on-stack-7))
726 (assert-no-consing (make-array-on-stack-8))
727 (assert-no-consing (make-array-on-stack-9))
728 (assert-no-consing (make-array-on-stack-10))
729 (assert-no-consing (make-array-on-stack-11)))
731 (with-test (:name
(:no-consing
:dx-raw-instances
) :skipped-on
'(or (not :raw-instance-init-vops
)
732 (not (and :gencgc
:c-stack-is-control-stack
))))
734 (setf a
1.24 b
1.23d0
)
735 (assert-no-consing (make-foo2-on-stack a b
)))
736 (assert-no-consing (make-foo3-on-stack)))
738 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
740 (defvar *table
* (make-hash-table))
742 (defun test-hash-table ()
743 (setf (gethash 5 *table
*) 13)
746 (with-test (:name
(:no-consing
:hash-tables
))
747 (assert-no-consing (test-hash-table)))
749 ;;; Both with-pinned-objects and without-gcing should not cons
751 (defun call-without-gcing (fun)
752 (sb-sys:without-gcing
(funcall fun
)))
754 (defun call-with-pinned-object (fun obj
)
755 (sb-sys:with-pinned-objects
(obj)
758 (with-test (:name
(:no-consing
:without-gcing
))
759 (assert-no-consing (call-without-gcing (lambda ()))))
761 (with-test (:name
(:no-consing
:with-pinned-objects
))
762 (assert-no-consing (call-with-pinned-object #'identity
42)))
764 ;;; with-mutex should use DX and not cons
766 (defvar *mutex
* (sb-thread::make-mutex
:name
"mutexlock"))
769 (sb-thread:with-mutex
(*mutex
*)
772 (with-test (:name
(:no-consing
:mutex
) :skipped-on
'(not :sb-thread
))
773 (assert-no-consing (test-mutex)))
776 ;;; Bugs found by Paul F. Dietz
778 (with-test (:name
(:dx-bug-misc
:pfdietz
))
785 (declare (optimize (speed 2) (space 0) (safety 0)
786 (debug 1) (compilation-speed 3)))
787 (let* ((v5 (cons b b
)))
788 (declare (dynamic-extent v5
))
794 ;;; bug reported by Svein Ove Aas
795 (defun svein-2005-ii-07 (x y
)
796 (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
797 (let ((args (list* y
1 2 x
)))
798 (declare (dynamic-extent args
))
799 (apply #'aref args
)))
801 (with-test (:name
(:dx-bugs-misc
:svein-2005-ii-07
))
805 #3A
(((1 1 1) (1 1 1) (1 1 1))
806 ((1 1 1) (1 1 1) (4 1 1))
807 ((1 1 1) (1 1 1) (1 1 1))))
810 ;;; bug reported by Brian Downing: stack-allocated arrays were not
811 ;;; filled with zeroes.
812 (defun-with-dx bdowning-2005-iv-16
()
813 (let ((a (make-array 11 :initial-element
0)))
814 (declare (dynamic-extent a
))
815 (assert (every (lambda (x) (eql x
0)) a
))))
817 (with-test (:name
(:dx-bug-misc
:bdowning-2005-iv-16
))
818 #+(or hppa mips x86 x86-64
)
819 (assert-no-consing (bdowning-2005-iv-16))
820 (bdowning-2005-iv-16))
822 (declaim (inline my-nconc
))
823 (defun my-nconc (&rest lists
)
824 (declare (dynamic-extent lists
))
825 (apply #'nconc lists
))
826 (defun-with-dx my-nconc-caller
(a b c
)
827 (let ((l1 (list a b c
))
830 (with-test (:name
:rest-stops-the-buck
)
831 (let ((list1 (my-nconc-caller 1 2 3))
832 (list2 (my-nconc-caller 9 8 7)))
833 (assert (equal list1
'(1 2 3 1 2 3)))
834 (assert (equal list2
'(9 8 7 9 8 7)))))
836 (defun-with-dx let-converted-vars-dx-allocated-bug
(x y z
)
837 (let* ((a (list x y z
))
840 (declare (dynamic-extent c
))
841 (values (first c
) (second c
))))
842 (with-test (:name
:let-converted-vars-dx-allocated-bug
)
843 (multiple-value-bind (i j
) (let-converted-vars-dx-allocated-bug 1 2 3)
844 (assert (and (equal i j
)
845 (equal i
(list 1 2 3))))))
847 ;;; workaround for bug 419 -- real issue remains, but check that the
849 (defun-with-dx bug419
(x)
850 (multiple-value-call #'list
851 (eval '(values 1 2 3))
853 (declare (dynamic-extent x
))
858 (declare (dynamic-extent #'mget
#'mset
))
859 ((lambda (f g
) (eval `(progn ,f
,g
(values 4 5 6)))) #'mget
#'mset
)))))
861 (with-test (:name
(:dx-bug-misc
:bug419
))
862 (assert (equal (bug419 42) '(1 2 3 4 5 6))))
864 ;;; Multiple DX arguments in a local function call
865 (defun test-dx-flet-test (fun n f1 f2 f3
)
866 (let ((res (with-output-to-string (s)
867 (assert (eql n
(ignore-errors (funcall fun s
)))))))
868 (multiple-value-bind (x pos
) (read-from-string res nil
)
869 (assert (equalp f1 x
))
870 (multiple-value-bind (y pos2
) (read-from-string res nil nil
:start pos
)
871 (assert (equalp f2 y
))
872 (assert (equalp f3
(read-from-string res nil nil
:start pos2
))))))
873 #+(or hppa mips x86 x86-64
)
874 (assert-no-consing (assert (eql n
(funcall fun nil
))))
875 (assert (eql n
(funcall fun nil
))))
877 (macrolet ((def (n f1 f2 f3
)
878 (let ((name (sb-pcl::format-symbol
:cl-user
"DX-FLET-TEST.~A" n
)))
880 (defun-with-dx ,name
(s)
882 (declare (dynamic-extent x
))
891 (with-test (:name
(:dx-flet-test
,n
))
892 (test-dx-flet-test #',name
,n
,f1
,f2
,f3
))))))
893 (def 0 (list :one
) (list :two
) (list :three
))
894 (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list
))
895 (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
897 ;;; Test that unknown-values coming after a DX value won't mess up the
899 (defun test-update-uvl-live-sets (x y z
)
900 (declare (optimize speed
(safety 0)))
902 (declare (dynamic-extent a
))
903 (eval `(list (length ',a
) ',b
))))
905 (bar (list x y z
) ; dx push
907 (multiple-value-call 'list
908 (eval '(values 1 2 3)) ; uv push
914 (with-test (:name
(:update-uvl-live-sets
))
915 (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
916 (test-update-uvl-live-sets #() 4 5))))
918 (with-test (:name
:regression-1.0
.23.38)
919 (compile nil
'(lambda ()
920 (declare (muffle-conditions compiler-note
))
922 (let ((res (cons x x
)))
925 (declare (inline make
))
926 (let ((z (make 1 2)))
927 (declare (dynamic-extent z
))
930 (compile nil
'(lambda ()
931 (declare (muffle-conditions compiler-note
))
933 (let ((res (cons x x
)))
936 (declare (inline make
))
937 (let ((z (make 1 2)))
938 (declare (dynamic-extent z
))
942 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
943 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
944 ;;; location, leading to all manner of badness. ...reproducing this
945 ;;; reliably is hard, but this it at least used to break on x86-64.
946 (defun length-and-words-packed-in-same-tn (m)
947 (declare (optimize speed
(safety 0) (debug 0) (space 0)))
948 (let ((array (make-array (max 1 m
) :element-type
'fixnum
)))
949 (declare (dynamic-extent array
))
950 (array-total-size array
)))
951 (with-test (:name
:length-and-words-packed-in-same-tn
)
952 (assert (= 1 (length-and-words-packed-in-same-tn -
3))))
954 (with-test (:name
:handler-case-bogus-compiler-note
955 :skipped-on
'(not (and :stack-allocatable-fixed-objects
956 :stack-allocatable-closures
)))
958 ((compiler-note (lambda (note)
959 (error "compiler issued note ~S during test" note
))))
960 ;; Taken from SWANK, used to signal a bogus stack allocation
963 `(lambda (files fasl-dir load
)
964 (declare (muffle-conditions style-warning
))
965 (let ((needs-recompile nil
))
967 (let ((dest (binary-pathname src fasl-dir
)))
970 (when (or needs-recompile
971 (not (probe-file dest
))
972 (file-newer-p src dest
))
973 (setq needs-recompile t
)
974 (ensure-directories-exist dest
)
975 (compile-file src
:output-file dest
:print nil
:verbose t
))
977 (load dest
:verbose t
)))
978 (serious-condition (c)
979 (handle-loadtime-error c dest
))))))))))
981 (declaim (inline foovector barvector
))
982 (defun foovector (x y z
)
983 (let ((v (make-array 3)))
988 (defun barvector (x y z
)
989 (make-array 3 :initial-contents
(list x y z
)))
990 (with-test (:name
:dx-compiler-notes
991 :skipped-on
'(not (and :stack-allocatable-vectors
992 :stack-allocatable-closures
)))
993 (flet ((assert-notes (j lambda
)
995 (handler-bind ((compiler-note (lambda (c)
998 (let ((*error-output
* (make-broadcast-stream)))
999 (compile nil lambda
))
1001 (error "Wanted ~S notes, got ~S for~% ~S"
1003 ;; These ones should complain.
1004 (assert-notes 1 `(lambda (x)
1005 (let ((v (make-array x
)))
1006 (declare (dynamic-extent v
))
1008 (assert-notes 2 `(lambda (x)
1009 (let ((y (if (plusp x
)
1012 (declare (dynamic-extent y
))
1015 (assert-notes 1 `(lambda (x)
1016 (let ((y (foovector x x x
)))
1017 (declare (sb-int:truly-dynamic-extent y
))
1020 ;; These ones should not complain.
1021 (assert-notes 0 `(lambda (name)
1023 ((posix-getenv (function c-string c-string
)
1026 (alien-funcall posix-getenv name
)))))
1027 (assert-notes 0 `(lambda (x)
1028 (let ((y (barvector x x x
)))
1029 (declare (dynamic-extent y
))
1032 (assert-notes 0 `(lambda (list)
1033 (declare (optimize (space 0)))
1034 (sort list
(lambda (x y
) ; shut unrelated notes up
1035 (< (truly-the fixnum x
)
1036 (truly-the fixnum y
))))))
1037 (assert-notes 0 `(lambda (other)
1039 (ignore-errors (funcall other s c n
)))))))
1041 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
1042 ;;; in an unfortunate loop.
1043 (defun handler-case-eating-stack ()
1044 (declare (muffle-conditions warning
)) ; "dead code detected ... IR1-PHASES"
1046 (do ((n 0 (logand most-positive-fixnum
(1+ n
))))
1048 (multiple-value-bind (value error
) (ignore-errors)
1049 (when (and value error
) nil
))
1051 (assert (= sp
(sb-c::%primitive sb-c
:current-stack-pointer
)))
1052 (setf sp
(sb-c::%primitive sb-c
:current-stack-pointer
))))))
1053 (with-test (:name
:handler-case-eating-stack
1054 :skipped-on
'(not (and :stack-allocatable-fixed-objects
1055 :stack-allocatable-closures
)))
1056 (assert-no-consing (handler-case-eating-stack)))
1058 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
1059 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
1060 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
1062 `(simple-array t
(3)))
1063 (declaim (ftype (function (t t t
) vec
) vec
))
1064 (declaim (inline vec
))
1066 (make-array 3 :initial-contents
(list a b c
)))
1067 (defun bad-boy (vec)
1068 (declare (type vec vec
))
1070 (let ((vec (vec (aref vec
0) (aref vec
1) (aref vec
2))))
1071 (declare (dynamic-extent vec
))
1072 (funcall fun vec
))))
1073 (with-test (:name
:recheck-nested-dx-bug
1074 :skipped-on
'(not :stack-allocatable-vectors
))
1075 (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
1076 (lambda (vec) (equalp vec
(vec 1.0 2.0 3.3)))))
1077 (flet ((foo (x) (declare (ignore x
))))
1078 (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
1079 (assert-no-consing (funcall bad-boy
#'foo
)))))
1081 (with-test (:name
:bug-497321
)
1082 (flet ((test (lambda type
)
1084 (handler-bind ((condition (lambda (c)
1086 (unless (typep c type
)
1087 (error "wanted ~S for~% ~S~%got ~S"
1088 type lambda
(type-of c
))))))
1089 (let ((*error-output
* (make-broadcast-stream)))
1090 (compile nil lambda
)))
1092 (test `(lambda () (declare (dynamic-extent #'bar
)))
1094 (test `(lambda () (declare (dynamic-extent bar
)))
1096 (test `(lambda (bar) (cons bar
(lambda () (declare (dynamic-extent bar
)))))
1097 'sb-ext
:compiler-note
)
1100 (cons #'bar
(lambda () (declare (dynamic-extent #'bar
))))))
1101 'sb-ext
:compiler-note
)))
1103 (with-test (:name
:bug-586105
1104 :skipped-on
'(not (and :stack-allocatable-vectors
1105 :stack-allocatable-lists
)))
1107 (let ((vec1 (make-array 1 :initial-contents
(list (list x
))))
1108 (vec2 (make-array 1 :initial-contents
`((,x
))))
1109 (vec3 (make-array 1 :initial-contents
`#((,x
))))
1110 (vec4 (make-array 1 :initial-contents
`(#(,x
)))))
1111 (declare (dynamic-extent vec1 vec2 vec3 vec4
))
1112 (assert (eql x
(car (aref vec1
0))))
1113 (assert (eql x
(car (aref vec2
0))))
1114 (assert (eql x
(car (aref vec3
0))))
1115 (assert (eql x
(elt (aref vec4
0) 0))))))
1116 (assert-no-consing (test 42))))
1118 (defun bug-681092 ()
1119 (declare (optimize speed
))
1122 (declare (dynamic-extent #'bar
))
1123 (do () ((list) (bar))
1126 (with-test (:name
:bug-681092
)
1127 (assert (= 10 (bug-681092))))
1129 ;;;; Including a loop in the flow graph between a DX-allocation and
1130 ;;;; the start of its environment would, for a while, cause executing
1131 ;;;; any of the code in the loop to discard the value. Found via
1132 ;;;; attempting to DX-allocate an array, which triggered the FILL
1133 ;;;; transform, which inserted such a loop.
1134 (defun bug-1472785 (x)
1135 (let ((y (let ((z (cons nil nil
)))
1139 (when (= x i
) (go b2
))
1145 (declare (dynamic-extent y w
))
1147 (with-test (:name
:bug-1472785
)
1148 (assert (null (bug-1472785 1))))
1150 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
1151 ;;;; but required by sanity.
1153 (declaim (inline rest-stops-dx
))
1154 (defun-with-dx rest-stops-dx
(&rest args
)
1155 (declare (dynamic-extent args
))
1156 (apply #'opaque-identity args
))
1158 (defun-with-dx rest-stops-dx-ok
()
1159 (equal '(:foo
) (rest-stops-dx (list :foo
))))
1161 (with-test (:name
:rest-stops-dynamic-extent
)
1162 (assert (rest-stops-dx-ok)))
1164 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
1166 (with-test (:name
:rest-to-more-conversion
)
1167 (let ((f1 (compile nil
`(lambda (f &rest args
)
1169 (assert-no-consing (assert (eql f1
(funcall f1
#'identity f1
)))))
1170 (let ((f2 (compile nil
`(lambda (f1 f2
&rest args
)
1171 (values (apply f1 args
) (apply f2 args
))))))
1172 (assert-no-consing (multiple-value-bind (a b
)
1173 (funcall f2
(lambda (x y z
) (+ x y z
)) (lambda (x y z
) (- x y z
))
1175 (assert (and (eql 6 a
) (eql -
4 b
))))))
1176 (let ((f3 (compile nil
`(lambda (f &optional x
&rest args
)
1178 (apply f x args
))))))
1179 (assert-no-consing (assert (eql 42 (funcall f3
1180 (lambda (a b c
) (+ a b c
))
1185 (let ((*error-output
* (make-broadcast-stream)))
1186 (compile nil
`(lambda (f &optional x
&rest args
1187 &key y
&allow-other-keys
)
1188 (apply f y x args
))))))
1189 (assert-no-consing (funcall f4
(lambda (y x yk y2 b c
)
1197 (let ((f5 (compile nil
`(lambda (a b c
&rest args
)
1198 (apply #'list
* a b c args
)))))
1199 (assert (equal '(1 2 3 4 5 6 7) (funcall f5
1 2 3 4 5 6 '(7)))))
1200 (let ((f6 (compile nil
`(lambda (x y
)
1201 (declare (optimize speed
))
1202 (concatenate 'string x y
)))))
1203 (assert (equal "foobar" (funcall f6
"foo" "bar"))))
1204 (let ((f7 (compile nil
`(lambda (&rest args
)
1207 (assert (equal '(a b c d e f
) (funcall (funcall f7
'a
'b
'c
'd
'e
'f
) 'list
))))
1208 (let ((f8 (compile nil
`(lambda (&rest args
)
1212 (assert (equal '(a b c d e f
) (funcall (funcall f8
'a
'b
'c
'd
'e
'f
) 'list
))))
1213 (let ((f9 (compile nil
`(lambda (f &rest args
)
1216 (declare (dynamic-extent #'foo
))
1217 (funcall f
#'foo
))))))
1218 (assert (equal '(a b c d e f
)
1219 (funcall f9
(lambda (f) (funcall f
'list
)) 'a
'b
'c
'd
'e
'f
))))
1220 (let ((f10 (compile nil
`(lambda (f &rest args
)
1223 (funcall f
#'foo
))))))
1224 (assert (equal '(a b c d e f
)
1225 (funcall f10
(lambda (f) (funcall f
'list
)) 'a
'b
'c
'd
'e
'f
))))
1226 (let ((f11 (compile nil
`(lambda (x y z
)
1228 (labels ((foo (x &rest rest
)
1229 (apply (lambda (&rest rest2
)
1230 (return-from out
(values-list rest2
)))
1235 (multiple-value-bind (a b c
) (funcall f11
1 2 3)
1238 (assert (eql c
3)))))
1240 (defun opaque-funcall (function &rest arguments
)
1241 (apply function arguments
))
1243 (with-test (:name
:implicit-value-cells
)
1244 (flet ((test-it (type input output
)
1245 (let ((f (compile nil
`(lambda (x)
1246 (declare (type ,type x
))
1249 (declare (dynamic-extent #'inc
))
1250 (list (opaque-funcall #'inc
) x
))))))
1251 (assert (equal (funcall f input
)
1252 (list output output
))))))
1253 (let ((width sb-vm
:n-word-bits
))
1254 (test-it t
(1- most-positive-fixnum
) most-positive-fixnum
)
1255 (test-it `(unsigned-byte ,(1- width
)) (ash 1 (- width
2)) (1+ (ash 1 (- width
2))))
1256 (test-it `(signed-byte ,width
) (ash -
1 (- width
2)) (1+ (ash -
1 (- width
2))))
1257 (test-it `(unsigned-byte ,width
) (ash 1 (1- width
)) (1+ (ash 1 (1- width
))))
1258 (test-it 'single-float
3f0
4f0
)
1259 (test-it 'double-float
3d0
4d0
)
1260 (test-it '(complex single-float
) #c
(3f0 4f0
) #c
(4f0 4f0
))
1261 (test-it '(complex double-float
) #c
(3d0 4d0
) #c
(4d0 4d0
)))))
1263 (with-test (:name
:sap-implicit-value-cells
)
1264 (let ((f (compile nil
`(lambda (x)
1265 (declare (type system-area-pointer x
))
1267 (setf x
(sb-sys:sap
+ x
16))))
1268 (declare (dynamic-extent #'inc
))
1269 (list (opaque-funcall #'inc
) x
)))))
1270 (width sb-vm
:n-machine-word-bits
))
1271 (assert (every (lambda (x)
1272 (sb-sys:sap
= x
(sb-sys:int-sap
(+ 16 (ash 1 (1- width
))))))
1273 (funcall f
(sb-sys:int-sap
(ash 1 (1- width
))))))))
1275 (with-test (:name
:&more-bounds
)
1277 (assert (not (funcall (compile nil
'(lambda (&rest args
) (car args
))))))
1278 (assert (not (funcall (compile nil
'(lambda (&rest args
) (nth 6 args
))))))
1279 (assert (not (funcall (compile nil
'(lambda (&rest args
) (elt args
10))))))
1280 (assert (not (funcall (compile nil
'(lambda (&rest args
) (cadr args
))))))
1281 (assert (not (funcall (compile nil
'(lambda (&rest args
) (third args
)))))))