Hoist tests from scan_weak_pointers() into scav_weak_pointer()
[sbcl.git] / tests / dynamic-extent.impure.lisp
blobe9e9922884dd2f59e13f972d8635dc13ee9a5b78
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 (compile nil '(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 (compile nil '(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 (assert
810 (funcall
811 (compile
813 '(lambda (a b)
814 (declare (optimize (speed 2) (space 0) (safety 0)
815 (debug 1) (compilation-speed 3)))
816 (let* ((v5 (cons b b)))
817 (declare (dynamic-extent v5))
819 a)))
820 'x 'y)
821 'x)))
823 ;;; bug reported by Svein Ove Aas
824 (defun svein-2005-ii-07 (x y)
825 (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
826 (let ((args (list* y 1 2 x)))
827 (declare (dynamic-extent args))
828 (apply #'aref args)))
830 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
831 (assert (eql
832 (svein-2005-ii-07
833 '(0)
834 #3A(((1 1 1) (1 1 1) (1 1 1))
835 ((1 1 1) (1 1 1) (4 1 1))
836 ((1 1 1) (1 1 1) (1 1 1))))
837 4)))
839 ;;; bug reported by Brian Downing: stack-allocated arrays were not
840 ;;; filled with zeroes.
841 (defun-with-dx bdowning-2005-iv-16 ()
842 (let ((a (make-array 11 :initial-element 0)))
843 (declare (dynamic-extent a))
844 (assert (every (lambda (x) (eql x 0)) a))))
846 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
847 #+(or hppa mips x86 x86-64)
848 (assert-no-consing (bdowning-2005-iv-16))
849 (bdowning-2005-iv-16))
851 (declaim (inline my-nconc))
852 (defun my-nconc (&rest lists)
853 (declare (dynamic-extent lists))
854 (apply #'nconc lists))
855 (defun-with-dx my-nconc-caller (a b c)
856 (let ((l1 (list a b c))
857 (l2 (list a b c)))
858 (my-nconc l1 l2)))
859 (with-test (:name :rest-stops-the-buck)
860 (let ((list1 (my-nconc-caller 1 2 3))
861 (list2 (my-nconc-caller 9 8 7)))
862 (assert (equal list1 '(1 2 3 1 2 3)))
863 (assert (equal list2 '(9 8 7 9 8 7)))))
865 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
866 (let* ((a (list x y z))
867 (b (list x y z))
868 (c (list a b)))
869 (declare (dynamic-extent c))
870 (values (first c) (second c))))
871 (with-test (:name :let-converted-vars-dx-allocated-bug)
872 (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
873 (assert (and (equal i j)
874 (equal i (list 1 2 3))))))
876 ;;; workaround for bug 419 -- real issue remains, but check that the
877 ;;; bandaid holds.
878 (defun-with-dx bug419 (x)
879 (multiple-value-call #'list
880 (eval '(values 1 2 3))
881 (let ((x x))
882 (declare (dynamic-extent x))
883 (flet ((mget (y)
884 (+ x y))
885 (mset (z)
886 (incf x z)))
887 (declare (dynamic-extent #'mget #'mset))
888 ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
890 (with-test (:name (:dx-bug-misc :bug419))
891 (assert (equal (bug419 42) '(1 2 3 4 5 6))))
893 ;;; Multiple DX arguments in a local function call
894 (defun test-dx-flet-test (fun n f1 f2 f3)
895 (let ((res (with-output-to-string (s)
896 (assert (eql n (ignore-errors (funcall fun s)))))))
897 (multiple-value-bind (x pos) (read-from-string res nil)
898 (assert (equalp f1 x))
899 (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
900 (assert (equalp f2 y))
901 (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
902 #+(or hppa mips x86 x86-64)
903 (assert-no-consing (assert (eql n (funcall fun nil))))
904 (assert (eql n (funcall fun nil))))
906 (macrolet ((def (n f1 f2 f3)
907 (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
908 `(progn
909 (defun-with-dx ,name (s)
910 (flet ((f (x)
911 (declare (dynamic-extent x))
912 (when s
913 (print x s)
914 (finish-output s))
915 nil))
916 (f ,f1)
917 (f ,f2)
918 (f ,f3)
919 ,n))
920 (with-test (:name (:dx-flet-test ,n))
921 (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
922 (def 0 (list :one) (list :two) (list :three))
923 (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
924 (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
926 ;;; Test that unknown-values coming after a DX value won't mess up the
927 ;;; stack analysis
928 (defun test-update-uvl-live-sets (x y z)
929 (declare (optimize speed (safety 0)))
930 (flet ((bar (a b)
931 (declare (dynamic-extent a))
932 (eval `(list (length ',a) ',b))))
933 (list (bar x y)
934 (bar (list x y z) ; dx push
935 (list
936 (multiple-value-call 'list
937 (eval '(values 1 2 3)) ; uv push
938 (max y z)
939 ) ; uv pop
941 ))))
943 (with-test (:name (:update-uvl-live-sets))
944 (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
945 (test-update-uvl-live-sets #() 4 5))))
947 (with-test (:name :regression-1.0.23.38)
948 (compile nil '(lambda ()
949 (declare (muffle-conditions compiler-note))
950 (flet ((make (x y)
951 (let ((res (cons x x)))
952 (setf (cdr res) y)
953 res)))
954 (declare (inline make))
955 (let ((z (make 1 2)))
956 (declare (dynamic-extent z))
957 (print z)
958 t))))
959 (compile nil '(lambda ()
960 (declare (muffle-conditions compiler-note))
961 (flet ((make (x y)
962 (let ((res (cons x x)))
963 (setf (cdr res) y)
964 (if x res y))))
965 (declare (inline make))
966 (let ((z (make 1 2)))
967 (declare (dynamic-extent z))
968 (print z)
969 t)))))
971 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
972 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
973 ;;; location, leading to all manner of badness. ...reproducing this
974 ;;; reliably is hard, but this it at least used to break on x86-64.
975 (defun length-and-words-packed-in-same-tn (m)
976 (declare (optimize speed (safety 0) (debug 0) (space 0)))
977 (let ((array (make-array (max 1 m) :element-type 'fixnum)))
978 (declare (dynamic-extent array))
979 (array-total-size array)))
980 (with-test (:name :length-and-words-packed-in-same-tn)
981 (assert (= 1 (length-and-words-packed-in-same-tn -3))))
983 (with-test (:name :handler-case-bogus-compiler-note
984 :skipped-on '(not (and :stack-allocatable-fixed-objects
985 :stack-allocatable-closures)))
986 (handler-bind
987 ((compiler-note (lambda (note)
988 (error "compiler issued note ~S during test" note))))
989 ;; Taken from SWANK, used to signal a bogus stack allocation
990 ;; failure note.
991 (compile nil
992 `(lambda (files fasl-dir load)
993 (declare (muffle-conditions style-warning))
994 (let ((needs-recompile nil))
995 (dolist (src files)
996 (let ((dest (binary-pathname src fasl-dir)))
997 (handler-case
998 (progn
999 (when (or needs-recompile
1000 (not (probe-file dest))
1001 (file-newer-p src dest))
1002 (setq needs-recompile t)
1003 (ensure-directories-exist dest)
1004 (compile-file src :output-file dest :print nil :verbose t))
1005 (when load
1006 (load dest :verbose t)))
1007 (serious-condition (c)
1008 (handle-loadtime-error c dest))))))))))
1010 (declaim (inline foovector barvector))
1011 (defun foovector (x y z)
1012 (let ((v (make-array 3)))
1013 (setf (aref v 0) x
1014 (aref v 1) y
1015 (aref v 2) z)
1017 (defun barvector (x y z)
1018 (make-array 3 :initial-contents (list x y z)))
1019 (with-test (:name :dx-compiler-notes
1020 :skipped-on '(not (and :stack-allocatable-vectors
1021 :stack-allocatable-closures)))
1022 (flet ((assert-notes (j lambda)
1023 (let ((n 0))
1024 (handler-bind ((compiler-note (lambda (c)
1025 (declare (ignore c))
1026 (incf n))))
1027 (let ((*error-output* (make-broadcast-stream)))
1028 (compile nil lambda))
1029 (unless (= j n)
1030 (error "Wanted ~S notes, got ~S for~% ~S"
1031 j n lambda))))))
1032 ;; These ones should complain.
1033 (assert-notes 1 `(lambda (x)
1034 (let ((v (make-array x)))
1035 (declare (dynamic-extent v))
1036 (length v))))
1037 (assert-notes 2 `(lambda (x)
1038 (let ((y (if (plusp x)
1039 (true x)
1040 (true (- x)))))
1041 (declare (dynamic-extent y))
1042 (print y)
1043 nil)))
1044 (assert-notes 1 `(lambda (x)
1045 (let ((y (foovector x x x)))
1046 (declare (sb-int:truly-dynamic-extent y))
1047 (print y)
1048 nil)))
1049 ;; These ones should not complain.
1050 (assert-notes 0 `(lambda (name)
1051 (with-alien
1052 ((posix-getenv (function c-string c-string)
1053 :EXTERN "getenv"))
1054 (values
1055 (alien-funcall posix-getenv name)))))
1056 (assert-notes 0 `(lambda (x)
1057 (let ((y (barvector x x x)))
1058 (declare (dynamic-extent y))
1059 (print y)
1060 nil)))
1061 (assert-notes 0 `(lambda (list)
1062 (declare (optimize (space 0)))
1063 (sort list (lambda (x y) ; shut unrelated notes up
1064 (< (truly-the fixnum x)
1065 (truly-the fixnum y))))))
1066 (assert-notes 0 `(lambda (other)
1067 #'(lambda (s c n)
1068 (ignore-errors (funcall other s c n)))))))
1070 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
1071 ;;; in an unfortunate loop.
1072 (defun handler-case-eating-stack ()
1073 (declare (muffle-conditions warning)) ; "dead code detected ... IR1-PHASES"
1074 (let ((sp nil))
1075 (do ((n 0 (logand most-positive-fixnum (1+ n))))
1076 ((>= n 1024))
1077 (multiple-value-bind (value error) (ignore-errors)
1078 (when (and value error) nil))
1079 (if sp
1080 (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
1081 (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
1082 (with-test (:name :handler-case-eating-stack
1083 :skipped-on '(not (and :stack-allocatable-fixed-objects
1084 :stack-allocatable-closures)))
1085 (assert-no-consing (handler-case-eating-stack)))
1087 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
1088 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
1089 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
1090 (deftype vec ()
1091 `(simple-array t (3)))
1092 (declaim (ftype (function (t t t) vec) vec))
1093 (declaim (inline vec))
1094 (defun vec (a b c)
1095 (make-array 3 :initial-contents (list a b c)))
1096 (defun bad-boy (vec)
1097 (declare (type vec vec))
1098 (lambda (fun)
1099 (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
1100 (declare (dynamic-extent vec))
1101 (funcall fun vec))))
1102 (with-test (:name :recheck-nested-dx-bug
1103 :skipped-on '(not :stack-allocatable-vectors))
1104 (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
1105 (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
1106 (flet ((foo (x) (declare (ignore x))))
1107 (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
1108 (assert-no-consing (funcall bad-boy #'foo)))))
1110 (with-test (:name :bug-497321)
1111 (flet ((test (lambda type)
1112 (let ((n 0))
1113 (handler-bind ((condition (lambda (c)
1114 (incf n)
1115 (unless (typep c type)
1116 (error "wanted ~S for~% ~S~%got ~S"
1117 type lambda (type-of c))))))
1118 (let ((*error-output* (make-broadcast-stream)))
1119 (compile nil lambda)))
1120 (assert (= n 1)))))
1121 (test `(lambda () (declare (dynamic-extent #'bar)))
1122 'style-warning)
1123 (test `(lambda () (declare (dynamic-extent bar)))
1124 'style-warning)
1125 (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
1126 'sb-ext:compiler-note)
1127 (test `(lambda ()
1128 (flet ((bar () t))
1129 (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
1130 'sb-ext:compiler-note)))
1132 (with-test (:name :bug-586105
1133 :skipped-on '(not (and :stack-allocatable-vectors
1134 :stack-allocatable-lists)))
1135 (flet ((test (x)
1136 (let ((vec1 (make-array 1 :initial-contents (list (list x))))
1137 (vec2 (make-array 1 :initial-contents `((,x))))
1138 (vec3 (make-array 1 :initial-contents `#((,x))))
1139 (vec4 (make-array 1 :initial-contents `(#(,x)))))
1140 (declare (dynamic-extent vec1 vec2 vec3 vec4))
1141 (assert (eql x (car (aref vec1 0))))
1142 (assert (eql x (car (aref vec2 0))))
1143 (assert (eql x (car (aref vec3 0))))
1144 (assert (eql x (elt (aref vec4 0) 0))))))
1145 (assert-no-consing (test 42))))
1147 (defun bug-681092 ()
1148 (declare (optimize speed))
1149 (let ((c 0))
1150 (flet ((bar () c))
1151 (declare (dynamic-extent #'bar))
1152 (do () ((list) (bar))
1153 (setf c 10)
1154 (return (bar))))))
1155 (with-test (:name :bug-681092)
1156 (assert (= 10 (bug-681092))))
1158 ;;;; Including a loop in the flow graph between a DX-allocation and
1159 ;;;; the start of its environment would, for a while, cause executing
1160 ;;;; any of the code in the loop to discard the value. Found via
1161 ;;;; attempting to DX-allocate an array, which triggered the FILL
1162 ;;;; transform, which inserted such a loop.
1163 (defun bug-1472785 (x)
1164 (let ((y (let ((z (cons nil nil)))
1165 (let ((i 0))
1166 (tagbody
1168 (when (= x i) (go b2))
1169 (incf i)
1170 (go b1)
1171 b2))
1173 (w (cons t t)))
1174 (declare (dynamic-extent y w))
1175 (eq y w)))
1176 (with-test (:name :bug-1472785)
1177 (assert (null (bug-1472785 1))))
1179 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
1180 ;;;; but required by sanity.
1182 (declaim (inline rest-stops-dx))
1183 (defun-with-dx rest-stops-dx (&rest args)
1184 (declare (dynamic-extent args))
1185 (apply #'opaque-identity args))
1187 (defun-with-dx rest-stops-dx-ok ()
1188 (equal '(:foo) (rest-stops-dx (list :foo))))
1190 (with-test (:name :rest-stops-dynamic-extent)
1191 (assert (rest-stops-dx-ok)))
1193 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
1194 ;;;; conversion.
1195 (with-test (:name :rest-to-more-conversion)
1196 (let ((f1 (compile nil `(lambda (f &rest args)
1197 (apply f args)))))
1198 (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
1199 (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
1200 (values (apply f1 args) (apply f2 args))))))
1201 (assert-no-consing (multiple-value-bind (a b)
1202 (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
1203 1 2 3)
1204 (assert (and (eql 6 a) (eql -4 b))))))
1205 (let ((f3 (compile nil `(lambda (f &optional x &rest args)
1206 (when x
1207 (apply f x args))))))
1208 (assert-no-consing (assert (eql 42 (funcall f3
1209 (lambda (a b c) (+ a b c))
1212 21)))))
1213 (let ((f4
1214 (let ((*error-output* (make-broadcast-stream)))
1215 (compile nil `(lambda (f &optional x &rest args
1216 &key y &allow-other-keys)
1217 (apply f y x args))))))
1218 (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
1219 (assert (eq y 'y))
1220 (assert (= x 2))
1221 (assert (eq :y yk))
1222 (assert (eq y2 'y))
1223 (assert (eq b 'b))
1224 (assert (eq c 'c)))
1225 2 :y 'y 'b 'c)))
1226 (let ((f5 (compile nil `(lambda (a b c &rest args)
1227 (apply #'list* a b c args)))))
1228 (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
1229 (let ((f6 (compile nil `(lambda (x y)
1230 (declare (optimize speed))
1231 (concatenate 'string x y)))))
1232 (assert (equal "foobar" (funcall f6 "foo" "bar"))))
1233 (let ((f7 (compile nil `(lambda (&rest args)
1234 (lambda (f)
1235 (apply f args))))))
1236 (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
1237 (let ((f8 (compile nil `(lambda (&rest args)
1238 (flet ((foo (f)
1239 (apply f args)))
1240 #'foo)))))
1241 (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
1242 (let ((f9 (compile nil `(lambda (f &rest args)
1243 (flet ((foo (g)
1244 (apply g args)))
1245 (declare (dynamic-extent #'foo))
1246 (funcall f #'foo))))))
1247 (assert (equal '(a b c d e f)
1248 (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1249 (let ((f10 (compile nil `(lambda (f &rest args)
1250 (flet ((foo (g)
1251 (apply g args)))
1252 (funcall f #'foo))))))
1253 (assert (equal '(a b c d e f)
1254 (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1255 (let ((f11 (compile nil `(lambda (x y z)
1256 (block out
1257 (labels ((foo (x &rest rest)
1258 (apply (lambda (&rest rest2)
1259 (return-from out (values-list rest2)))
1260 x rest)))
1261 (if x
1262 (foo x y z)
1263 (foo y z x))))))))
1264 (multiple-value-bind (a b c) (funcall f11 1 2 3)
1265 (assert (eql a 1))
1266 (assert (eql b 2))
1267 (assert (eql c 3)))))
1269 (defun opaque-funcall (function &rest arguments)
1270 (apply function arguments))
1272 (with-test (:name :implicit-value-cells)
1273 (flet ((test-it (type input output)
1274 (let ((f (compile nil `(lambda (x)
1275 (declare (type ,type x))
1276 (flet ((inc ()
1277 (incf x)))
1278 (declare (dynamic-extent #'inc))
1279 (list (opaque-funcall #'inc) x))))))
1280 (assert (equal (funcall f input)
1281 (list output output))))))
1282 (let ((width sb-vm:n-word-bits))
1283 (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1284 (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1285 (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1286 (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1287 (test-it 'single-float 3f0 4f0)
1288 (test-it 'double-float 3d0 4d0)
1289 (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1290 (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1292 (with-test (:name :sap-implicit-value-cells)
1293 (let ((f (compile nil `(lambda (x)
1294 (declare (type system-area-pointer x))
1295 (flet ((inc ()
1296 (setf x (sb-sys:sap+ x 16))))
1297 (declare (dynamic-extent #'inc))
1298 (list (opaque-funcall #'inc) x)))))
1299 (width sb-vm:n-machine-word-bits))
1300 (assert (every (lambda (x)
1301 (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1302 (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))
1304 (with-test (:name :&more-bounds)
1305 ;; lp#1154946
1306 (assert (not (funcall (compile nil '(lambda (&rest args) (car args))))))
1307 (assert (not (funcall (compile nil '(lambda (&rest args) (nth 6 args))))))
1308 (assert (not (funcall (compile nil '(lambda (&rest args) (elt args 10))))))
1309 (assert (not (funcall (compile nil '(lambda (&rest args) (cadr args))))))
1310 (assert (not (funcall (compile nil '(lambda (&rest args) (third args)))))))
1312 (with-test (:name :local-notinline-functions)
1313 (multiple-value-bind (start result end)
1314 (funcall (checked-compile
1315 `(lambda ()
1316 (values (sb-kernel:current-sp)
1317 (flet ((x () (cons 1 2)))
1318 (declare (notinline x))
1319 (let ((x (x)))
1320 (declare (dynamic-extent x))
1321 (true x))
1322 (true 10))
1323 (sb-kernel:current-sp)))))
1324 (assert (sb-sys:sap= start end))
1325 (assert result))
1326 (multiple-value-bind (start result end)
1327 (funcall (checked-compile
1328 `(lambda ()
1329 (declare (optimize speed))
1330 (values (sb-kernel:current-sp)
1331 (flet ((x () (cons 1 2)))
1332 (let ((x (x))
1333 (y (x)))
1334 (declare (dynamic-extent x y))
1335 (true x)
1336 (true y))
1337 (true 10))
1338 (sb-kernel:current-sp)))))
1339 (assert (sb-sys:sap= start end))
1340 (assert result)))
1343 (with-test (:name :unused-paremeters-of-an-inlined-function)
1344 (let ((name (gensym "fun")))
1345 (proclaim `(inline ,name))
1346 (eval `(defun ,name (a b &optional c d)
1347 (declare (ignore c d))
1348 (cons a b)))
1349 (assert (equal (funcall
1350 (funcall
1351 (checked-compile
1352 `(lambda ()
1353 (let ((x (cons
1354 (,name 1 2)
1355 (,name 2 3))))
1356 (declare (dynamic-extent x))
1357 (true x))
1358 #',name)))
1359 2 3)
1360 '(2 . 3)))))