Signal floating-point-overflow from bignum-to-float.
[sbcl.git] / tests / dynamic-extent.impure.lisp
blobb0453016772204c139a387a0fb938eb04770c41d
1 ;;;; tests that dynamic-extent functionality works.
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (when (eq sb-ext:*evaluator-mode* :interpret)
15 (sb-ext:exit :code 104))
17 (load "compiler-test-util.lisp")
18 (use-package :ctu)
20 (setq sb-c::*check-consistency* t
21 sb-ext:*stack-allocate-dynamic-extent* t)
23 (defun crashme (a)
24 (declare (optimize (speed 3) (safety 0) (space 0)))
25 (declare (muffle-conditions style-warning)) ; re F1 and F2
26 (restart-case
27 (progn (ignore-errors (error "Foo")) (write-char #\.))
28 (retry () (f1 a))
29 (use-value (new) (f2 new))))
31 ;; lp#1530390
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")))
42 `(progn
43 (defun ,debug-name ,arglist
44 (declare (optimize debug))
45 ,@body)
46 (defun ,default-name ,arglist
47 ,@body)
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)
56 ;;; &REST lists
58 (defun-with-dx dxlength (&rest rest)
59 (declare (dynamic-extent rest))
60 (length 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)))
67 (defun callee (list)
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))
73 (callee 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))
80 (+ x (callee 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)))
86 ;;; %NIP-VALUES
88 (defun-with-dx test-nip-values ()
89 (flet ((bar (x &rest y)
90 (declare (dynamic-extent y))
91 (if (> x 0)
92 (values x (length y))
93 (values (car y)))))
94 (multiple-value-call #'values
95 (bar 1 2 3 4 5 6)
96 (bar -1 'a 'b))))
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))
108 (length 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)))
118 (length z))))
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)
131 z))))
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)
143 z)))
145 ;;; alignment
147 (defvar *x*)
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))
155 (dotimes (n 64)
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))))))
161 ;;; closure
163 (declaim (notinline true))
164 (defun true (x)
165 (declare (ignore x))
168 (defun-with-dx dxclosure (x)
169 (flet ((f (y)
170 (+ y x)))
171 (declare (dynamic-extent #'f))
172 (true #'f)))
174 (with-test (:name (:dx-closure))
175 (assert (eq t (dxclosure 13))))
177 ;;; value-cells
179 (defun-with-dx dx-value-cell (x)
180 ;; Not implemented everywhere, yet.
181 #+(or x86 x86-64 mips hppa)
182 (let ((cell x))
183 (declare (sb-int:truly-dynamic-extent cell))
184 (flet ((f ()
185 (incf cell)))
186 (declare (dynamic-extent #'f))
187 (true #'f))))
189 ;;; CONS
191 (defun-with-dx cons-on-stack (x)
192 (let ((cons (cons x x)))
193 (declare (dynamic-extent cons))
194 (true cons)
195 nil))
197 ;;; MAKE-ARRAY
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))
203 (true v)
204 (true v)
205 nil))
207 (defun-with-dx make-array-on-stack-1 ()
208 (let ((v (make-array '(42) :element-type 'single-float)))
209 (declare (dynamic-extent v))
210 (true v)
211 (true v)
212 nil))
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))
218 (true v)
219 (true v)
220 nil))
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))
227 (true v)
228 (true v)
229 nil))
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))
234 (true v)
235 (true v)
236 nil))
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))
241 (true v)
242 (true v)
243 nil))
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))
248 (true v)
249 (true v)
250 nil))
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))
255 (true v)
256 (true v)
257 nil))
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))
262 (true v)
263 (true v)
264 nil))
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))
269 (true v)
270 (true v)
271 nil))
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))
276 (true v)
277 (true v)
278 nil))
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))
283 (true v)
284 (true v)
285 nil))
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))
290 (true v)
291 nil))
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))
296 (true a)
297 (true a)
298 nil))
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)
301 :element-type 'bit
302 :initial-contents `(#((,a) (,b)) (#(,c) (,d))))))
303 (true a)
304 (true a)
305 nil))
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))
311 (true a)
312 (true a)
313 nil))
315 (defun 2d-array-initializer (n)
316 (ecase n
317 (1 '((a)))
318 (2 '((a b) (c d)))
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))
326 (true a)
327 (true a)
328 nil))
330 ;;; MAKE-LIST
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))))
336 (funcall thunk s)))
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))))))
343 ;;; MAKE-STRUCTURE
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
515 ;; was compiled.
516 (dolist (s '(make-foo1 make-foo2 make-foo3))
517 (assert (null (sb-int:info :function :inline-expansion-designator s))))
519 (defstruct foo1 x)
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)))))
527 (defstruct foo2
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
545 ;;; slot types.
546 (defstruct foo3
547 (a 0 :type t)
548 (b 1 :type fixnum)
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)))))
563 ;;; Nested DX
565 (defun-with-dx nested-dx-lists ()
566 (let ((dx (list (list 1 2) (list 3 4))))
567 (declare (dynamic-extent dx))
568 (true dx)
569 nil))
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))
574 (true dx)
575 nil))
577 (defun-with-dx nested-dx-not-used (x)
578 (declare (list x))
579 (let ((l (setf (car x) (list x x x))))
580 (declare (dynamic-extent l))
581 (true l)
582 (true (length l))
583 nil))
585 (defun-with-dx nested-evil-dx-used (x)
586 (declare (list x))
587 (let ((l (list x x x)))
588 (declare (dynamic-extent l))
589 (unwind-protect
590 (progn
591 (setf (car x) l)
592 (true l))
593 (setf (car x) nil))
594 nil))
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)))
600 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))
605 (true 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 ;;; Conditional nested DX
622 ;; These two test cases prompted a substantial redesign of the STACK
623 ;; phase of the compiler to handle their particular permutation of
624 ;; nested DX.
626 (with-test (:name (:bug-1044465 :reduced))
627 ;; Test case from Stas Boukarev
628 (checked-compile '(lambda (x)
629 (let ((a (if x
630 (list (list x))
631 (list (list x)))))
632 (declare (dynamic-extent a))
633 (prin1 a)
634 1))))
636 (with-test (:name (:bug-1044465 :nasty))
637 ;; Test case from Alastair Bridgewater
638 (checked-compile '(lambda (x y)
639 (dotimes (i 2)
640 (block bar
641 (let ((a (if x
642 (list (list x))
643 (list (list x))))
644 (b (if y x (return-from bar x))))
645 (declare (dynamic-extent a))
646 (prin1 a)
647 b))))))
649 ;;; multiple uses for dx lvar
651 (defun-with-dx multiple-dx-uses ()
652 (let ((dx (if (true t)
653 (list 1 2 3)
654 (list 2 3 4))))
655 (declare (dynamic-extent dx))
656 (true dx)
657 nil))
659 ;;; mapfoo should make the initial cons dx
661 (defun mapcar-negate (x) (mapcar #'- x))
662 (defun mapcan-reverse (x) (mapcan #'reverse x))
664 ;;; handler-case and handler-bind should use DX internally
666 (defun dx-handler-bind (x)
667 (handler-bind ((error
668 #'(lambda (c)
669 (break "OOPS: ~S caused ~S" x c)))
670 ((and serious-condition (not error))
671 #'(lambda (c)
672 (break "OOPS2: ~S did ~S" x c))))
673 (/ 2 x)))
675 (defun dx-handler-case (x)
676 (assert (zerop (handler-case (/ 2 x)
677 (error (c)
678 (break "OOPS: ~S caused ~S" x c)
680 (:no-error (res)
681 (1- res))))))
683 (defun list-delete-some-stuff ()
684 ;; opaque-identity hides the fact that we are calling a destructive function
685 ;; on a constant, which is technically illegal. But no deletion occurs,
686 ;; so it's innocuous. Also these aren't tests of DX, but oh well...
687 (declare (muffle-conditions style-warning))
688 (delete 'a (opaque-identity '(x y)))
689 (delete 'a (opaque-identity '(x y)) :from-end t)
690 (delete-duplicates (opaque-identity '(x y))))
692 (defvar *a-cons* (cons nil nil))
694 (with-test (:name (:no-consing :dx-closures) :skipped-on (not :stack-allocatable-closures))
695 (assert-no-consing (dxclosure 42)))
697 (with-test (:name (:no-consing :dx-lists)
698 :skipped-on (not (and :stack-allocatable-lists
699 :stack-allocatable-fixed-objects)))
700 (assert-no-consing (dxlength 1 2 3))
701 (assert-no-consing (dxlength t t t t t t))
702 (assert-no-consing (dxlength))
703 (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
704 (assert-no-consing (test-nip-values))
705 (assert-no-consing (test-let-var-subst2 17))
706 (assert-no-consing (test-lvar-subst 11))
707 (assert-no-consing (nested-dx-lists))
708 (assert-consing (nested-dx-not-used *a-cons*))
709 (assert-no-consing (nested-evil-dx-used *a-cons*))
710 (assert-no-consing (mapcar-negate nil))
711 (assert-no-consing (mapcan-reverse nil))
712 (assert-no-consing (list-delete-some-stuff))
713 (assert-no-consing (multiple-dx-uses)))
715 (with-test (:name (:no-consing :dx-value-cell)
716 :skipped-on (not :stack-allocatable-closures))
717 (assert-no-consing (dx-value-cell 13)))
719 (with-test (:name (:no-consing :dx-fixed-objects)
720 :skipped-on (not (and :stack-allocatable-fixed-objects
721 :stack-allocatable-closures)))
722 (assert-no-consing (cons-on-stack 42))
723 (assert-no-consing (make-foo1-on-stack 123))
724 (assert-no-consing (nested-good 42))
725 (assert-no-consing (nested-dx-conses))
726 (assert-no-consing (dx-handler-bind 2))
727 (assert-no-consing (dx-handler-case 2)))
729 (with-test (:name (:no-consing :dx-vectors) :skipped-on (not :stack-allocatable-vectors))
730 (assert-no-consing (force-make-array-on-stack 128))
731 (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
732 (assert-no-consing (make-array-on-stack-3 9 8 7))
733 (assert-no-consing (make-array-on-stack-4))
734 (assert-no-consing (make-array-on-stack-5))
735 (assert-no-consing (vector-on-stack :x :y)))
737 (with-test (:name (:no-consing :dx-arrays) :skipped-on (not :stack-allocatable-vectors))
738 (assert-no-consing (make-3d-fixed-array-on-stack-1))
739 (assert-no-consing (make-2d-variable-array-on-stack))
740 (assert-no-consing (make-2d-array-function-initializer 1))
741 (assert-no-consing (make-2d-array-function-initializer 2))
742 (assert-no-consing (make-2d-array-function-initializer 3)))
744 (with-test (:name (:no-consing :dx-specialized-arrays)
745 :skipped-on (not (and :stack-allocatable-vectors
746 :c-stack-is-control-stack)))
747 (assert-no-consing (make-3d-fixed-array-on-stack-2 0 0 1 1)))
749 (with-test (:name (:no-consing :specialized-dx-vectors)
750 :skipped-on (not (and :stack-allocatable-vectors
751 :c-stack-is-control-stack)))
752 (assert-no-consing (make-array-on-stack-1))
753 (assert-no-consing (make-array-on-stack-6))
754 (assert-no-consing (make-array-on-stack-7))
755 (assert-no-consing (make-array-on-stack-8))
756 (assert-no-consing (make-array-on-stack-9))
757 (assert-no-consing (make-array-on-stack-10))
758 (assert-no-consing (make-array-on-stack-11)))
760 (with-test (:name (:no-consing :dx-raw-instances) :skipped-on (or (not :raw-instance-init-vops)
761 (not (and :gencgc :c-stack-is-control-stack))))
762 (let (a b)
763 (setf a 1.24 b 1.23d0)
764 (assert-no-consing (make-foo2-on-stack a b)))
765 (assert-no-consing (make-foo3-on-stack)))
767 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
769 (defvar *table* (make-hash-table))
771 (defun test-hash-table ()
772 (setf (gethash 5 *table*) 13)
773 (gethash 5 *table*))
775 (with-test (:name (:no-consing :hash-tables))
776 (assert-no-consing (test-hash-table)))
778 ;;; Both with-pinned-objects and without-gcing should not cons
780 (defun call-without-gcing (fun)
781 (sb-sys:without-gcing (funcall fun)))
783 (defun call-with-pinned-object (fun obj)
784 (sb-sys:with-pinned-objects (obj)
785 (funcall fun obj)))
787 (with-test (:name (:no-consing :without-gcing))
788 (assert-no-consing (call-without-gcing (lambda ()))))
790 (with-test (:name (:no-consing :with-pinned-objects))
791 (assert-no-consing (call-with-pinned-object #'identity 42)))
793 ;;; with-mutex should use DX and not cons
795 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
797 (defun test-mutex ()
798 (sb-thread:with-mutex (*mutex*)
799 (true *mutex*)))
801 (with-test (:name (:no-consing :mutex) :skipped-on (not :sb-thread))
802 (assert-no-consing (test-mutex)))
805 ;;; Bugs found by Paul F. Dietz
807 (with-test (:name (:dx-bug-misc :pfdietz))
808 (checked-compile-and-assert (:optimize :all)
809 '(lambda (a b)
810 (let* ((v5 (cons b b)))
811 (declare (dynamic-extent v5))
814 (('x 'y) 'x)))
816 (with-test (:name :bug-1738095)
817 ;; STACK analysis wasn't marking UVL or DX LVARs that are held live
818 ;; by a DX allocation as being live in blocks along the path from
819 ;; the allocation to the ENTRY node, causing problems when there is
820 ;; a block that is outside the lexical environment of the "earlier"
821 ;; DX allocation (in the case below, such a block would be the first
822 ;; (outermost) use of LIST).
823 (checked-compile '(lambda ()
824 (let ((* (list (let ((* (list nil)))
825 (declare (dynamic-extent *))
826 (list 1)))))
827 (declare (dynamic-extent *))))))
829 (with-test (:name :bug-1739308)
830 ;; STACK analysis wasn't propagating DX LVARs back from ENTRY to
831 ;; allocation through non-local-entry environments (below, the CATCH
832 ;; entry point), causing problems when such is the ONLY live path
833 ;; back to the allocation.
834 (checked-compile '(lambda (x y)
835 (let ((s
836 (multiple-value-prog1
837 (list x)
838 (catch 'ct1
839 (throw 'ct8 30)))))
840 (declare (dynamic-extent s))
841 (funcall (the function y) s)))))
843 ;;; bug reported by Svein Ove Aas
844 (defun svein-2005-ii-07 (x y)
845 (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
846 (let ((args (list* y 1 2 x)))
847 (declare (dynamic-extent args))
848 (apply #'aref args)))
850 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
851 (assert (eql
852 (svein-2005-ii-07
853 '(0)
854 #3A(((1 1 1) (1 1 1) (1 1 1))
855 ((1 1 1) (1 1 1) (4 1 1))
856 ((1 1 1) (1 1 1) (1 1 1))))
857 4)))
859 ;;; bug reported by Brian Downing: stack-allocated arrays were not
860 ;;; filled with zeroes.
861 (defun-with-dx bdowning-2005-iv-16 ()
862 (let ((a (make-array 11 :initial-element 0)))
863 (declare (dynamic-extent a))
864 (assert (every (lambda (x) (eql x 0)) a))))
866 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
867 #+(or hppa mips x86 x86-64)
868 (assert-no-consing (bdowning-2005-iv-16))
869 (bdowning-2005-iv-16))
871 (declaim (inline my-nconc))
872 (defun my-nconc (&rest lists)
873 (declare (dynamic-extent lists))
874 (apply #'nconc lists))
875 (defun-with-dx my-nconc-caller (a b c)
876 (let ((l1 (list a b c))
877 (l2 (list a b c)))
878 (my-nconc l1 l2)))
879 (with-test (:name :rest-stops-the-buck)
880 (let ((list1 (my-nconc-caller 1 2 3))
881 (list2 (my-nconc-caller 9 8 7)))
882 (assert (equal list1 '(1 2 3 1 2 3)))
883 (assert (equal list2 '(9 8 7 9 8 7)))))
885 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
886 (let* ((a (list x y z))
887 (b (list x y z))
888 (c (list a b)))
889 (declare (dynamic-extent c))
890 (values (first c) (second c))))
891 (with-test (:name :let-converted-vars-dx-allocated-bug)
892 (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
893 (assert (and (equal i j)
894 (equal i (list 1 2 3))))))
896 ;;; workaround for bug 419 -- real issue remains, but check that the
897 ;;; bandaid holds.
898 (defun-with-dx bug419 (x)
899 (multiple-value-call #'list
900 (eval '(values 1 2 3))
901 (let ((x x))
902 (declare (dynamic-extent x))
903 (flet ((mget (y)
904 (+ x y))
905 (mset (z)
906 (incf x z)))
907 (declare (dynamic-extent #'mget #'mset))
908 ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
910 (with-test (:name (:dx-bug-misc :bug419))
911 (assert (equal (bug419 42) '(1 2 3 4 5 6))))
913 ;;; Multiple DX arguments in a local function call
914 (defun test-dx-flet-test (fun n f1 f2 f3)
915 (let ((res (with-output-to-string (s)
916 (assert (eql n (ignore-errors (funcall fun s)))))))
917 (multiple-value-bind (x pos) (read-from-string res nil)
918 (assert (equalp f1 x))
919 (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
920 (assert (equalp f2 y))
921 (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
922 #+(or hppa mips x86 x86-64)
923 (assert-no-consing (assert (eql n (funcall fun nil))))
924 (assert (eql n (funcall fun nil))))
926 (macrolet ((def (n f1 f2 f3)
927 (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
928 `(progn
929 (defun-with-dx ,name (s)
930 (flet ((f (x)
931 (declare (dynamic-extent x))
932 (when s
933 (print x s)
934 (finish-output s))
935 nil))
936 (f ,f1)
937 (f ,f2)
938 (f ,f3)
939 ,n))
940 (with-test (:name (:dx-flet-test ,n))
941 (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
942 (def 0 (list :one) (list :two) (list :three))
943 (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
944 (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
946 ;;; Test that unknown-values coming after a DX value won't mess up the
947 ;;; stack analysis
948 (defun test-update-uvl-live-sets (x y z)
949 (declare (optimize speed (safety 0)))
950 (flet ((bar (a b)
951 (declare (dynamic-extent a))
952 (eval `(list (length ',a) ',b))))
953 (list (bar x y)
954 (bar (list x y z) ; dx push
955 (list
956 (multiple-value-call 'list
957 (eval '(values 1 2 3)) ; uv push
958 (max y z)
959 ) ; uv pop
961 ))))
963 (with-test (:name (:update-uvl-live-sets))
964 (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
965 (test-update-uvl-live-sets #() 4 5))))
967 (with-test (:name :regression-1.0.23.38)
968 (checked-compile '(lambda ()
969 (declare (muffle-conditions compiler-note))
970 (flet ((make (x y)
971 (let ((res (cons x x)))
972 (setf (cdr res) y)
973 res)))
974 (declare (inline make))
975 (let ((z (make 1 2)))
976 (declare (dynamic-extent z))
977 (print z)
978 t))))
979 (checked-compile '(lambda ()
980 (declare (muffle-conditions compiler-note))
981 (flet ((make (x y)
982 (let ((res (cons x x)))
983 (setf (cdr res) y)
984 (if x res y))))
985 (declare (inline make))
986 (let ((z (make 1 2)))
987 (declare (dynamic-extent z))
988 (print z)
989 t)))))
991 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
992 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
993 ;;; location, leading to all manner of badness. ...reproducing this
994 ;;; reliably is hard, but this it at least used to break on x86-64.
995 (defun length-and-words-packed-in-same-tn (m)
996 (declare (optimize speed (safety 0) (debug 0) (space 0)))
997 (let ((array (make-array (max 1 m) :element-type 'fixnum)))
998 (declare (dynamic-extent array))
999 (array-total-size array)))
1000 (with-test (:name :length-and-words-packed-in-same-tn)
1001 (assert (= 1 (length-and-words-packed-in-same-tn -3))))
1003 (with-test (:name :handler-case-bogus-compiler-note
1004 :skipped-on (not (and :stack-allocatable-fixed-objects
1005 :stack-allocatable-closures)))
1006 ;; Taken from SWANK, used to signal a bogus stack allocation
1007 ;; failure note.
1008 (checked-compile
1009 `(lambda (files fasl-dir load)
1010 (declare (muffle-conditions style-warning))
1011 (let ((needs-recompile nil))
1012 (dolist (src files)
1013 (let ((dest (binary-pathname src fasl-dir)))
1014 (handler-case
1015 (progn
1016 (when (or needs-recompile
1017 (not (probe-file dest))
1018 (file-newer-p src dest))
1019 (setq needs-recompile t)
1020 (ensure-directories-exist dest)
1021 (compile-file src :output-file dest :print nil :verbose t))
1022 (when load
1023 (load dest :verbose t)))
1024 (serious-condition (c)
1025 (handle-loadtime-error c dest)))))))
1026 :allow-notes nil))
1028 (declaim (inline foovector barvector))
1029 (defun foovector (x y z)
1030 (let ((v (make-array 3)))
1031 (setf (aref v 0) x
1032 (aref v 1) y
1033 (aref v 2) z)
1035 (defun barvector (x y z)
1036 (make-array 3 :initial-contents (list x y z)))
1037 (with-test (:name :dx-compiler-notes
1038 :skipped-on (not (and :stack-allocatable-vectors
1039 :stack-allocatable-closures)))
1040 (flet ((assert-notes (j lambda)
1041 (let ((notes (nth 4 (multiple-value-list (checked-compile lambda))))) ; TODO
1042 (unless (= (length notes) j)
1043 (error "Wanted ~S notes, got ~S for~% ~S"
1044 j (length notes) lambda)))))
1045 ;; These ones should complain.
1046 (assert-notes 1 `(lambda (x)
1047 (let ((v (make-array x)))
1048 (declare (dynamic-extent v))
1049 (length v))))
1050 (assert-notes 2 `(lambda (x)
1051 (let ((y (if (plusp x)
1052 (true x)
1053 (true (- x)))))
1054 (declare (dynamic-extent y))
1055 (print y)
1056 nil)))
1057 (assert-notes 1 `(lambda (x)
1058 (let ((y (foovector x x x)))
1059 (declare (sb-int:truly-dynamic-extent y))
1060 (print y)
1061 nil)))
1062 ;; These ones should not complain.
1063 (assert-notes 0 `(lambda (name)
1064 (with-alien
1065 ((posix-getenv (function c-string c-string)
1066 :EXTERN "getenv"))
1067 (values
1068 (alien-funcall posix-getenv name)))))
1069 (assert-notes 0 `(lambda (x)
1070 (let ((y (barvector x x x)))
1071 (declare (dynamic-extent y))
1072 (print y)
1073 nil)))
1074 (assert-notes 0 `(lambda (list)
1075 (declare (optimize (space 0)))
1076 (sort list (lambda (x y) ; shut unrelated notes up
1077 (< (truly-the fixnum x)
1078 (truly-the fixnum y))))))
1079 (assert-notes 0 `(lambda (other)
1080 #'(lambda (s c n)
1081 (ignore-errors (funcall other s c n)))))))
1083 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
1084 ;;; in an unfortunate loop.
1085 (defun handler-case-eating-stack ()
1086 (declare (muffle-conditions warning)) ; "dead code detected ... IR1-PHASES"
1087 (let ((sp nil))
1088 (do ((n 0 (logand most-positive-fixnum (1+ n))))
1089 ((>= n 1024))
1090 (multiple-value-bind (value error) (ignore-errors)
1091 (when (and value error) nil))
1092 (if sp
1093 (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
1094 (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
1095 (with-test (:name :handler-case-eating-stack
1096 :skipped-on (not (and :stack-allocatable-fixed-objects
1097 :stack-allocatable-closures)))
1098 (assert-no-consing (handler-case-eating-stack)))
1100 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
1101 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
1102 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
1103 (deftype vec ()
1104 `(simple-array t (3)))
1105 (declaim (ftype (function (t t t) vec) vec))
1106 (declaim (inline vec))
1107 (defun vec (a b c)
1108 (make-array 3 :initial-contents (list a b c)))
1109 (defun bad-boy (vec)
1110 (declare (type vec vec))
1111 (lambda (fun)
1112 (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
1113 (declare (dynamic-extent vec))
1114 (funcall fun vec))))
1115 (with-test (:name :recheck-nested-dx-bug
1116 :skipped-on (not :stack-allocatable-vectors))
1117 (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
1118 (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
1119 (flet ((foo (x) (declare (ignore x))))
1120 (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
1121 (assert-no-consing (funcall bad-boy #'foo)))))
1123 (with-test (:name :bug-497321)
1124 (flet ((test (lambda &rest args)
1125 (multiple-value-bind (fun failure-p warnings style-warnings notes)
1126 (apply #'checked-compile lambda args)
1127 (declare (ignore fun failure-p))
1128 (assert (= (length (append warnings style-warnings notes)) 1)))))
1129 (test `(lambda () (declare (dynamic-extent #'bar)))
1130 :allow-style-warnings 'style-warning)
1131 (test `(lambda () (declare (dynamic-extent bar)))
1132 :allow-style-warnings 'style-warning)
1133 (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
1134 :allow-notes 'sb-ext:compiler-note)
1135 (test `(lambda ()
1136 (flet ((bar () t))
1137 (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
1138 :allow-notes 'sb-ext:compiler-note)))
1140 (with-test (:name :bug-586105
1141 :skipped-on (not (and :stack-allocatable-vectors
1142 :stack-allocatable-lists)))
1143 (flet ((test (x)
1144 (let ((vec1 (make-array 1 :initial-contents (list (list x))))
1145 (vec2 (make-array 1 :initial-contents `((,x))))
1146 (vec3 (make-array 1 :initial-contents `#((,x))))
1147 (vec4 (make-array 1 :initial-contents `(#(,x)))))
1148 (declare (dynamic-extent vec1 vec2 vec3 vec4))
1149 (assert (eql x (car (aref vec1 0))))
1150 (assert (eql x (car (aref vec2 0))))
1151 (assert (eql x (car (aref vec3 0))))
1152 (assert (eql x (elt (aref vec4 0) 0))))))
1153 (assert-no-consing (test 42))))
1155 (defun bug-681092 ()
1156 (declare (optimize speed))
1157 (let ((c 0))
1158 (flet ((bar () c))
1159 (declare (dynamic-extent #'bar))
1160 (do () ((list) (bar))
1161 (setf c 10)
1162 (return (bar))))))
1163 (with-test (:name :bug-681092)
1164 (assert (= 10 (bug-681092))))
1166 ;;;; Including a loop in the flow graph between a DX-allocation and
1167 ;;;; the start of its environment would, for a while, cause executing
1168 ;;;; any of the code in the loop to discard the value. Found via
1169 ;;;; attempting to DX-allocate an array, which triggered the FILL
1170 ;;;; transform, which inserted such a loop.
1171 (defun bug-1472785 (x)
1172 (let ((y (let ((z (cons nil nil)))
1173 (let ((i 0))
1174 (tagbody
1176 (when (= x i) (go b2))
1177 (incf i)
1178 (go b1)
1179 b2))
1181 (w (cons t t)))
1182 (declare (dynamic-extent y w))
1183 (eq y w)))
1184 (with-test (:name :bug-1472785)
1185 (assert (null (bug-1472785 1))))
1187 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
1188 ;;;; but required by sanity.
1190 (declaim (inline rest-stops-dx))
1191 (defun-with-dx rest-stops-dx (&rest args)
1192 (declare (dynamic-extent args))
1193 (apply #'opaque-identity args))
1195 (defun-with-dx rest-stops-dx-ok ()
1196 (equal '(:foo) (rest-stops-dx (list :foo))))
1198 (with-test (:name :rest-stops-dynamic-extent)
1199 (assert (rest-stops-dx-ok)))
1201 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
1202 ;;;; conversion.
1203 (with-test (:name :rest-to-more-conversion)
1204 (let ((f1 (checked-compile `(lambda (f &rest args)
1205 (apply f args)))))
1206 (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
1207 (let ((f2 (checked-compile `(lambda (f1 f2 &rest args)
1208 (values (apply f1 args) (apply f2 args))))))
1209 (assert-no-consing (multiple-value-bind (a b)
1210 (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
1211 1 2 3)
1212 (assert (and (eql 6 a) (eql -4 b))))))
1213 (let ((f3 (checked-compile `(lambda (f &optional x &rest args)
1214 (when x
1215 (apply f x args))))))
1216 (assert-no-consing (assert (eql 42 (funcall f3
1217 (lambda (a b c) (+ a b c))
1220 21)))))
1221 (let ((f4
1222 (checked-compile `(lambda (f &optional x &rest args
1223 &key y &allow-other-keys)
1224 (apply f y x args))
1225 :allow-style-warnings t)))
1226 (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
1227 (assert (eq y 'y))
1228 (assert (= x 2))
1229 (assert (eq :y yk))
1230 (assert (eq y2 'y))
1231 (assert (eq b 'b))
1232 (assert (eq c 'c)))
1233 2 :y 'y 'b 'c)))
1234 (checked-compile-and-assert ()
1235 `(lambda (a b c &rest args)
1236 (apply #'list* a b c args))
1237 ((1 2 3 4 5 6 '(7)) '(1 2 3 4 5 6 7)))
1238 (checked-compile-and-assert ()
1239 `(lambda (x y)
1240 (concatenate 'string x y))
1241 (("foo" "bar") "foobar"))
1242 (checked-compile-and-assert ()
1243 `(lambda (&rest args)
1244 (lambda (f)
1245 (apply f args)))
1246 (('a 'b 'c 'd 'e 'f) '(a b c d e f)
1247 :test (lambda (values expected)
1248 (equal (multiple-value-list
1249 (funcall (first values) 'list))
1250 expected))))
1251 (checked-compile-and-assert ()
1252 `(lambda (&rest args)
1253 (flet ((foo (f)
1254 (apply f args)))
1255 #'foo))
1256 (('a 'b 'c 'd 'e 'f) '(a b c d e f)
1257 :test (lambda (values expected)
1258 (equal (multiple-value-list
1259 (funcall (first values) 'list))
1260 expected))))
1261 (checked-compile-and-assert ()
1262 `(lambda (f &rest args)
1263 (flet ((foo (g)
1264 (apply g args)))
1265 (declare (dynamic-extent #'foo))
1266 (funcall f #'foo)))
1267 (((lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f)
1268 '(a b c d e f)))
1269 (checked-compile-and-assert ()
1270 `(lambda (f &rest args)
1271 (flet ((foo (g)
1272 (apply g args)))
1273 (funcall f #'foo)))
1274 (((lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f)
1275 '(a b c d e f)))
1276 (checked-compile-and-assert ()
1277 `(lambda (x y z)
1278 (block out
1279 (labels ((foo (x &rest rest)
1280 (apply (lambda (&rest rest2)
1281 (return-from out (values-list rest2)))
1282 x rest)))
1283 (if x
1284 (foo x y z)
1285 (foo y z x)))))
1286 ((1 2 3) (values 1 2 3))))
1288 (defun opaque-funcall (function &rest arguments)
1289 (apply function arguments))
1291 (with-test (:name :implicit-value-cells)
1292 (flet ((test-it (type input output)
1293 (checked-compile-and-assert ()
1294 `(lambda (x)
1295 (declare (type ,type x))
1296 (flet ((inc ()
1297 (incf x)))
1298 (declare (dynamic-extent #'inc))
1299 (list (opaque-funcall #'inc) x)))
1300 ((input) (list output output)))))
1301 (let ((width sb-vm:n-word-bits))
1302 (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1303 (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1304 (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1305 (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1306 (test-it 'single-float 3f0 4f0)
1307 (test-it 'double-float 3d0 4d0)
1308 (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1309 (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1311 (with-test (:name :sap-implicit-value-cells)
1312 (let ((f (checked-compile `(lambda (x)
1313 (declare (type system-area-pointer x))
1314 (flet ((inc ()
1315 (setf x (sb-sys:sap+ x 16))))
1316 (declare (dynamic-extent #'inc))
1317 (list (opaque-funcall #'inc) x)))))
1318 (width sb-vm:n-machine-word-bits))
1319 (assert (every (lambda (x)
1320 (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1321 (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))
1323 (with-test (:name (:&more-bounds :lp-1154946))
1324 (checked-compile-and-assert () '(lambda (&rest args) (car args)) (() nil))
1325 (checked-compile-and-assert () '(lambda (&rest args) (nth 6 args)) (() nil))
1326 (checked-compile-and-assert () '(lambda (&rest args) (cadr args)) (() nil))
1327 (checked-compile-and-assert () '(lambda (&rest args) (third args)) (() nil)))
1329 (with-test (:name :local-notinline-functions)
1330 (multiple-value-bind (start result end)
1331 (funcall (checked-compile
1332 `(lambda ()
1333 (values (sb-kernel:current-sp)
1334 (flet ((x () (cons 1 2)))
1335 (declare (notinline x))
1336 (let ((x (x)))
1337 (declare (dynamic-extent x))
1338 (true x))
1339 (true 10))
1340 (sb-kernel:current-sp)))))
1341 (assert (sb-sys:sap= start end))
1342 (assert result))
1343 (multiple-value-bind (start result end)
1344 (funcall (checked-compile
1345 `(lambda ()
1346 (declare (optimize speed))
1347 (values (sb-kernel:current-sp)
1348 (flet ((x () (cons 1 2)))
1349 (let ((x (x))
1350 (y (x)))
1351 (declare (dynamic-extent x y))
1352 (true x)
1353 (true y))
1354 (true 10))
1355 (sb-kernel:current-sp)))))
1356 (assert (sb-sys:sap= start end))
1357 (assert result)))
1360 (with-test (:name :unused-paremeters-of-an-inlined-function)
1361 (let ((name (gensym "fun")))
1362 (proclaim `(inline ,name))
1363 (eval `(defun ,name (a b &optional c d)
1364 (declare (ignore c d))
1365 (cons a b)))
1366 (checked-compile-and-assert ()
1367 `(lambda ()
1368 (let ((x (cons
1369 (,name 1 2)
1370 (,name 2 3))))
1371 (declare (dynamic-extent x))
1372 (true x))
1373 #',name)
1374 (() '(2 . 3) :test (lambda (values expected)
1375 (equal (multiple-value-list
1376 (funcall (first values) 2 3))
1377 expected))))))