stack-allocatable fill-initialized specialized arrays, take 2
[sbcl.git] / tests / dynamic-extent.impure.lisp
1 ;;;; tests that dynamic-extent functionality works.
2
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.
13
14 (when (eq sb-ext:*evaluator-mode* :interpret)
15   (sb-ext:quit :unix-status 104))
16
17 (load "compiler-test-util.lisp")
18 (use-package :ctu)
19
20 (setq sb-c::*check-consistency* t
21       sb-ext:*stack-allocate-dynamic-extent* t)
22
23 (defmacro defun-with-dx (name arglist &body body)
24   `(defun ,name ,arglist
25      ,@body))
26
27 (declaim (notinline opaque-identity))
28 (defun opaque-identity (x)
29   x)
30
31 ;;; &REST lists
32
33 (defun-with-dx dxlength (&rest rest)
34   (declare (dynamic-extent rest))
35   (length rest))
36
37 (with-test (:name (:dx-&rest :basics))
38   (assert (= (dxlength 1 2 3) 3))
39   (assert (= (dxlength t t t t t t) 6))
40   (assert (= (dxlength) 0)))
41
42 (defun callee (list)
43   (destructuring-bind (a b c d e f &rest g) list
44     (+ a b c d e f (length g))))
45
46 (defun-with-dx dxcaller (&rest rest)
47   (declare (dynamic-extent rest))
48   (callee rest))
49
50 (with-test (:name (:dx-&rest :pass-down-to-callee :tail-call))
51   (assert (= (dxcaller 1 2 3 4 5 6 7) 22)))
52
53 (defun-with-dx dxcaller-align-1 (x &rest rest)
54   (declare (dynamic-extent rest))
55   (+ x (callee rest)))
56
57 (with-test (:name (:dx-&rest :pass-down-to-callee :non-tail-call))
58   (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
59   (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40)))
60
61 ;;; %NIP-VALUES
62
63 (defun-with-dx test-nip-values ()
64   (flet ((bar (x &rest y)
65            (declare (dynamic-extent y))
66            (if (> x 0)
67                (values x (length y))
68                (values (car y)))))
69     (multiple-value-call #'values
70       (bar 1 2 3 4 5 6)
71       (bar -1 'a 'b))))
72
73 (with-test (:name (:nip-values))
74   (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a))))
75
76 ;;; LET-variable substitution
77
78 (defun-with-dx test-let-var-subst1 (x)
79   (let ((y (list x (1- x))))
80     (opaque-identity :foo)
81     (let ((z (the list y)))
82       (declare (dynamic-extent z))
83       (length z))))
84
85 (with-test (:name (:let-variable-substitution))
86   (assert (eql (test-let-var-subst1 17) 2)))
87
88 (defun-with-dx test-let-var-subst2 (x)
89   (let ((y (list x (1- x))))
90     (declare (dynamic-extent y))
91     (opaque-identity :foo)
92     (let ((z (the list y)))
93       (length z))))
94
95 (with-test (:name (:let-variable-substitution-2))
96   (assert (eql (test-let-var-subst2 17) 2)))
97
98
99 ;;; DX propagation through LET-return.
100
101 (defun-with-dx test-lvar-subst (x)
102   (let ((y (list x (1- x))))
103     (declare (dynamic-extent y))
104     (second (let ((z (the list y)))
105               (opaque-identity :foo)
106               z))))
107
108 (with-test (:name (:dx-propagation-through-let-return))
109   (assert (eql (test-lvar-subst 11) 10)))
110
111 ;;; this code is incorrect, but the compiler should not fail
112 (defun-with-dx test-let-var-subst-incorrect (x)
113   (let ((y (list x (1- x))))
114     (opaque-identity :foo)
115     (let ((z (the list y)))
116       (declare (dynamic-extent z))
117       (opaque-identity :bar)
118       z)))
119 \f
120 ;;; alignment
121
122 (defvar *x*)
123 (defun-with-dx test-alignment-dx-list (form)
124   (multiple-value-prog1 (eval form)
125     (let ((l (list 1 2 3 4)))
126       (declare (dynamic-extent l))
127       (setq *x* (copy-list l)))))
128
129 (with-test (:name (:dx-list :alignment))
130   (dotimes (n 64)
131     (let* ((res (loop for i below n collect i))
132            (form `(values ,@res)))
133       (assert (equal (multiple-value-list (test-alignment-dx-list form)) res))
134       (assert (equal *x* '(1 2 3 4))))))
135
136 ;;; closure
137
138 (declaim (notinline true))
139 (defun true (x)
140   (declare (ignore x))
141   t)
142
143 (defun-with-dx dxclosure (x)
144   (flet ((f (y)
145            (+ y x)))
146     (declare (dynamic-extent #'f))
147     (true #'f)))
148
149 (with-test (:name (:dx-closure))
150   (assert (eq t (dxclosure 13))))
151
152 ;;; value-cells
153
154 (defun-with-dx dx-value-cell (x)
155   ;; Not implemented everywhere, yet.
156   #+(or x86 x86-64 mips hppa)
157   (let ((cell x))
158     (declare (sb-int:truly-dynamic-extent cell))
159     (flet ((f ()
160              (incf cell)))
161       (declare (dynamic-extent #'f))
162       (true #'f))))
163
164 ;;; CONS
165
166 (defun-with-dx cons-on-stack (x)
167   (let ((cons (cons x x)))
168     (declare (dynamic-extent cons))
169     (true cons)
170     nil))
171
172 ;;; MAKE-ARRAY
173
174 (defun force-make-array-on-stack (n)
175   (declare (optimize safety))
176   (let ((v (make-array (min n 1))))
177     (declare (sb-int:truly-dynamic-extent v))
178     (true v)
179     (true v)
180     nil))
181
182 (defun-with-dx make-array-on-stack-1 ()
183   (let ((v (make-array '(42) :element-type 'single-float)))
184     (declare (dynamic-extent v))
185     (true v)
186     (true v)
187     nil))
188
189 (defun-with-dx make-array-on-stack-2 (n x)
190   (declare (integer n))
191   (let ((v (make-array n :initial-contents x)))
192     (declare (sb-int:truly-dynamic-extent v))
193     (true v)
194     (true v)
195     nil))
196
197 (defun-with-dx make-array-on-stack-3 (x y z)
198   (let ((v (make-array 3
199                        :element-type 'fixnum :initial-contents (list x y z)
200                        :element-type t :initial-contents x)))
201     (declare (sb-int:truly-dynamic-extent v))
202     (true v)
203     (true v)
204     nil))
205
206 (defun-with-dx make-array-on-stack-4 ()
207   (let ((v (make-array 3 :initial-contents '(1 2 3))))
208     (declare (sb-int:truly-dynamic-extent v))
209     (true v)
210     (true v)
211     nil))
212
213 (defun-with-dx make-array-on-stack-5 ()
214   (let ((v (make-array 3 :initial-element 12 :element-type t)))
215     (declare (sb-int:truly-dynamic-extent v))
216     (true v)
217     (true v)
218     nil))
219
220 (defun-with-dx make-array-on-stack-6 ()
221   (let ((v (make-array 3 :initial-element 12 :element-type '(unsigned-byte 8))))
222     (declare (sb-int:truly-dynamic-extent v))
223     (true v)
224     (true v)
225     nil))
226
227 (defun-with-dx make-array-on-stack-7 ()
228   (let ((v (make-array 3 :initial-element 12 :element-type '(signed-byte 8))))
229     (declare (sb-int:truly-dynamic-extent v))
230     (true v)
231     (true v)
232     nil))
233
234 (defun-with-dx make-array-on-stack-8 ()
235   (let ((v (make-array 3 :initial-element 12 :element-type 'word)))
236     (declare (sb-int:truly-dynamic-extent v))
237     (true v)
238     (true v)
239     nil))
240
241 (defun-with-dx make-array-on-stack-9 ()
242   (let ((v (make-array 3 :initial-element 12.0 :element-type 'single-float)))
243     (declare (sb-int:truly-dynamic-extent v))
244     (true v)
245     (true v)
246     nil))
247
248 (defun-with-dx make-array-on-stack-10 ()
249   (let ((v (make-array 3 :initial-element 12.0d0 :element-type 'double-float)))
250     (declare (sb-int:truly-dynamic-extent v))
251     (true v)
252     (true v)
253     nil))
254
255 (defun-with-dx vector-on-stack (x y)
256   (let ((v (vector 1 x 2 y 3)))
257     (declare (sb-int:truly-dynamic-extent v))
258     (true v)
259     nil))
260
261 ;;; MAKE-STRUCTURE
262
263 (declaim (inline make-fp-struct-1))
264 (defstruct fp-struct-1
265   (s 0.0 :type single-float)
266   (d 0.0d0 :type double-float))
267
268 (defun-with-dx test-fp-struct-1.1 (s d)
269   (let ((fp (make-fp-struct-1 :s s)))
270     (declare (dynamic-extent fp))
271     (assert (eql s (fp-struct-1-s fp)))
272     (assert (eql 0.0d0 (fp-struct-1-d fp)))))
273
274 (defun-with-dx test-fp-struct-1.2 (s d)
275   (let ((fp (make-fp-struct-1 :d d)))
276     (declare (dynamic-extent fp))
277     (assert (eql 0.0 (fp-struct-1-s fp)))
278     (assert (eql d (fp-struct-1-d fp)))))
279
280 (defun-with-dx test-fp-struct-1.3 (s d)
281   (let ((fp (make-fp-struct-1 :d d :s s)))
282     (declare (dynamic-extent fp))
283     (assert (eql s (fp-struct-1-s fp)))
284     (assert (eql d (fp-struct-1-d fp)))))
285
286 (defun-with-dx test-fp-struct-1.4 (s d)
287   (let ((fp (make-fp-struct-1 :s s :d d)))
288     (declare (dynamic-extent fp))
289     (assert (eql s (fp-struct-1-s fp)))
290     (assert (eql d (fp-struct-1-d fp)))))
291
292 (with-test (:name (:test-fp-struct-1.1))
293   (test-fp-struct-1.1 123.456 876.243d0))
294 (with-test (:name (:test-fp-struct-1.2))
295   (test-fp-struct-1.2 123.456 876.243d0))
296 (with-test (:name (:test-fp-struct-1.3))
297   (test-fp-struct-1.3 123.456 876.243d0))
298 (with-test (:name (:test-fp-struct-1.4))
299   (test-fp-struct-1.4 123.456 876.243d0))
300
301 (declaim (inline make-fp-struct-2))
302 (defstruct fp-struct-2
303   (d 0.0d0 :type double-float)
304   (s 0.0 :type single-float))
305
306 (defun-with-dx test-fp-struct-2.1 (s d)
307   (let ((fp (make-fp-struct-2 :s s)))
308     (declare (dynamic-extent fp))
309     (assert (eql s (fp-struct-2-s fp)))
310     (assert (eql 0.0d0 (fp-struct-2-d fp)))))
311
312 (defun-with-dx test-fp-struct-2.2 (s d)
313   (let ((fp (make-fp-struct-2 :d d)))
314     (declare (dynamic-extent fp))
315     (assert (eql 0.0 (fp-struct-2-s fp)))
316     (assert (eql d (fp-struct-2-d fp)))))
317
318 (defun-with-dx test-fp-struct-2.3 (s d)
319   (let ((fp (make-fp-struct-2 :d d :s s)))
320     (declare (dynamic-extent fp))
321     (assert (eql s (fp-struct-2-s fp)))
322     (assert (eql d (fp-struct-2-d fp)))))
323
324 (defun-with-dx test-fp-struct-2.4 (s d)
325   (let ((fp (make-fp-struct-2 :s s :d d)))
326     (declare (dynamic-extent fp))
327     (assert (eql s (fp-struct-2-s fp)))
328     (assert (eql d (fp-struct-2-d fp)))))
329
330 (with-test (:name (:test-fp-struct-2.1))
331   (test-fp-struct-2.1 123.456 876.243d0))
332 (with-test (:name (:test-fp-struct-2.2))
333   (test-fp-struct-2.2 123.456 876.243d0))
334 (with-test (:name (:test-fp-struct-2.3))
335   (test-fp-struct-2.3 123.456 876.243d0))
336 (with-test (:name (:test-fp-struct-2.4))
337   (test-fp-struct-2.4 123.456 876.243d0))
338
339 (declaim (inline make-cfp-struct-1))
340 (defstruct cfp-struct-1
341   (s (complex 0.0) :type (complex single-float))
342   (d (complex 0.0d0) :type (complex double-float)))
343
344 (defun-with-dx test-cfp-struct-1.1 (s d)
345   (let ((cfp (make-cfp-struct-1 :s s)))
346     (declare (dynamic-extent cfp))
347     (assert (eql s (cfp-struct-1-s cfp)))
348     (assert (eql (complex 0.0d0) (cfp-struct-1-d cfp)))))
349
350 (defun-with-dx test-cfp-struct-1.2 (s d)
351   (let ((cfp (make-cfp-struct-1 :d d)))
352     (declare (dynamic-extent cfp))
353     (assert (eql (complex 0.0) (cfp-struct-1-s cfp)))
354     (assert (eql d (cfp-struct-1-d cfp)))))
355
356 (defun-with-dx test-cfp-struct-1.3 (s d)
357   (let ((cfp (make-cfp-struct-1 :d d :s s)))
358     (declare (dynamic-extent cfp))
359     (assert (eql s (cfp-struct-1-s cfp)))
360     (assert (eql d (cfp-struct-1-d cfp)))))
361
362 (defun-with-dx test-cfp-struct-1.4 (s d)
363   (let ((cfp (make-cfp-struct-1 :s s :d d)))
364     (declare (dynamic-extent cfp))
365     (assert (eql s (cfp-struct-1-s cfp)))
366     (assert (eql d (cfp-struct-1-d cfp)))))
367
368 (with-test (:name (:test-cfp-struct-1.1))
369   (test-cfp-struct-1.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
370 (with-test (:name (:test-cfp-struct-1.2))
371   (test-cfp-struct-1.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
372 (with-test (:name (:test-cfp-struct-1.3))
373   (test-cfp-struct-1.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
374 (with-test (:name (:test-cfp-struct-1.4))
375   (test-cfp-struct-1.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
376
377 (declaim (inline make-cfp-struct-2))
378 (defstruct cfp-struct-2
379   (d (complex 0.0d0) :type (complex double-float))
380   (s (complex 0.0) :type (complex single-float)))
381
382 (defun-with-dx test-cfp-struct-2.1 (s d)
383   (let ((cfp (make-cfp-struct-2 :s s)))
384     (declare (dynamic-extent cfp))
385     (assert (eql s (cfp-struct-2-s cfp)))
386     (assert (eql (complex 0.0d0) (cfp-struct-2-d cfp)))))
387
388 (defun-with-dx test-cfp-struct-2.2 (s d)
389   (let ((cfp (make-cfp-struct-2 :d d)))
390     (declare (dynamic-extent cfp))
391     (assert (eql (complex 0.0) (cfp-struct-2-s cfp)))
392     (assert (eql d (cfp-struct-2-d cfp)))))
393
394 (defun-with-dx test-cfp-struct-2.3 (s d)
395   (let ((cfp (make-cfp-struct-2 :d d :s s)))
396     (declare (dynamic-extent cfp))
397     (assert (eql s (cfp-struct-2-s cfp)))
398     (assert (eql d (cfp-struct-2-d cfp)))))
399
400 (defun-with-dx test-cfp-struct-2.4 (s d)
401   (let ((cfp (make-cfp-struct-2 :s s :d d)))
402     (declare (dynamic-extent cfp))
403     (assert (eql s (cfp-struct-2-s cfp)))
404     (assert (eql d (cfp-struct-2-d cfp)))))
405
406 (with-test (:name (:test-cfp-struct-2.1))
407   (test-cfp-struct-2.1 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
408 (with-test (:name (:test-cfp-struct-2.2))
409   (test-cfp-struct-2.2 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
410 (with-test (:name (:test-cfp-struct-2.3))
411   (test-cfp-struct-2.3 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
412 (with-test (:name (:test-cfp-struct-2.4))
413   (test-cfp-struct-2.4 (complex 0.123 123.456) (complex 908132.41d0 876.243d0)))
414
415 (declaim (inline make-foo1 make-foo2 make-foo3))
416 (defstruct foo1 x)
417
418 (defun-with-dx make-foo1-on-stack (x)
419   (let ((foo (make-foo1 :x x)))
420     (declare (dynamic-extent foo))
421     (assert (eql x (foo1-x foo)))))
422
423 (defstruct foo2
424   (x 0.0 :type single-float)
425   (y 0.0d0 :type double-float)
426   a
427   b
428   c)
429
430 (defun-with-dx make-foo2-on-stack (x y)
431   (let ((foo (make-foo2 :y y :c 'c)))
432     (declare (dynamic-extent foo))
433     (assert (eql 0.0 (foo2-x foo)))
434     (assert (eql y (foo2-y foo)))
435     (assert (eql 'c (foo2-c foo)))
436     (assert (eql nil (foo2-b foo)))))
437
438 ;;; Check that constants work out as argument for all relevant
439 ;;; slot types.
440 (defstruct foo3
441   (a 0 :type t)
442   (b 1 :type fixnum)
443   (c 2 :type sb-vm:word)
444   (d 3.0 :type single-float)
445   (e 4.0d0 :type double-float))
446
447 (defun-with-dx make-foo3-on-stack ()
448   (let ((foo (make-foo3)))
449     (declare (dynamic-extent foo))
450     (assert (eql 0 (foo3-a foo)))
451     (assert (eql 1 (foo3-b foo)))
452     (assert (eql 2 (foo3-c foo)))
453     (assert (eql 3.0 (foo3-d foo)))
454     (assert (eql 4.0d0 (foo3-e foo)))))
455
456 ;;; Nested DX
457
458 (defun-with-dx nested-dx-lists ()
459   (let ((dx (list (list 1 2) (list 3 4))))
460     (declare (dynamic-extent dx))
461     (true dx)
462     nil))
463
464 (defun-with-dx nested-dx-conses ()
465   (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t) nil))))))
466     (declare (dynamic-extent dx))
467     (true dx)
468     nil))
469
470 (defun-with-dx nested-dx-not-used (x)
471   (declare (list x))
472   (let ((l (setf (car x) (list x x x))))
473     (declare (dynamic-extent l))
474     (true l)
475     (true (length l))
476     nil))
477
478 (defun-with-dx nested-evil-dx-used (x)
479   (declare (list x))
480   (let ((l (list x x x)))
481     (declare (dynamic-extent l))
482     (unwind-protect
483          (progn
484            (setf (car x) l)
485            (true l))
486       (setf (car x) nil))
487     nil))
488
489 (defparameter *bar* nil)
490 (declaim (inline make-nested-bad make-nested-good))
491 (defstruct (nested (:constructor make-nested-bad (&key bar &aux (bar (setf *bar* bar))))
492                    (:constructor make-nested-good (&key bar)))
493   bar)
494
495 (defun-with-dx nested-good (y)
496   (let ((x (list (list (make-nested-good :bar (list (list (make-nested-good :bar (list y)))))))))
497     (declare (dynamic-extent x))
498     (true x)))
499
500 (defun-with-dx nested-bad (y)
501   (let ((x (list (list (make-nested-bad :bar (list (list (make-nested-bad :bar (list y)))))))))
502     (declare (dynamic-extent x))
503     (unless (equalp (caar x) (make-nested-good :bar *bar*))
504       (error "got ~S, wanted ~S" (caar x) (make-nested-good :bar *bar*)))
505     (caar x)))
506
507 (with-test (:name :conservative-nested-dx)
508   ;; NESTED-BAD should not stack-allocate :BAR due to the SETF.
509   (assert (equalp (nested-bad 42) (make-nested-good :bar *bar*)))
510   (assert (equalp *bar* (list (list (make-nested-bad :bar (list 42)))))))
511
512 ;;; multiple uses for dx lvar
513
514 (defun-with-dx multiple-dx-uses ()
515   (let ((dx (if (true t)
516                 (list 1 2 3)
517                 (list 2 3 4))))
518     (declare (dynamic-extent dx))
519     (true dx)
520     nil))
521
522 ;;; handler-case and handler-bind should use DX internally
523
524 (defun dx-handler-bind (x)
525   (handler-bind ((error
526                   #'(lambda (c)
527                       (break "OOPS: ~S caused ~S" x c)))
528                  ((and serious-condition (not error))
529                   #'(lambda (c)
530                       (break "OOPS2: ~S did ~S" x c))))
531     (/ 2 x)))
532
533 (defun dx-handler-case (x)
534   (assert (zerop (handler-case (/ 2 x)
535                    (error (c)
536                      (break "OOPS: ~S caused ~S" x c)
537                      -1)
538                    (:no-error (res)
539                      (1- res))))))
540
541 (defvar *a-cons* (cons nil nil))
542
543 (with-test (:name (:no-consing :dx-closures) :skipped-on '(not :stack-allocatable-closures))
544   (assert-no-consing (dxclosure 42)))
545
546 (with-test (:name (:no-consing :dx-lists) :skipped-on '(not :stack-allocatable-lists))
547   (assert-no-consing (dxlength 1 2 3))
548   (assert-no-consing (dxlength t t t t t t))
549   (assert-no-consing (dxlength))
550   (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
551   (assert-no-consing (test-nip-values))
552   (assert-no-consing (test-let-var-subst2 17))
553   (assert-no-consing (test-lvar-subst 11))
554   (assert-no-consing (nested-dx-lists))
555   (assert-consing (nested-dx-not-used *a-cons*))
556   (assert-no-consing (nested-evil-dx-used *a-cons*))
557   (assert-no-consing (multiple-dx-uses)))
558
559 (with-test (:name (:no-consing :dx-value-cell))
560   (assert-no-consing (dx-value-cell 13)))
561
562 (with-test (:name (:no-consing :dx-fixed-objects) :skipped-on '(not :stack-allocatable-fixed-objects))
563   (assert-no-consing (cons-on-stack 42))
564   (assert-no-consing (make-foo1-on-stack 123))
565   (assert-no-consing (nested-good 42))
566   (assert-no-consing (nested-dx-conses))
567   (assert-no-consing (dx-handler-bind 2))
568   (assert-no-consing (dx-handler-case 2)))
569
570 (with-test (:name (:no-consing :dx-vectors) :skipped-on '(not :stack-allocatable-vectors))
571   (assert-no-consing (force-make-array-on-stack 128))
572   (assert-no-consing (make-array-on-stack-1))
573   (assert-no-consing (make-array-on-stack-2 5 '(1 2.0 3 4.0 5)))
574   (assert-no-consing (make-array-on-stack-3 9 8 7))
575   (assert-no-consing (make-array-on-stack-4))
576   (assert-no-consing (make-array-on-stack-5))
577   (assert-no-consing (vector-on-stack :x :y)))
578
579 (with-test (:name (:no-consing :specialized-dx-vectors)
580             :skipped-on `(not (and :stack-allocatable-vectors
581                                    :c-stack-is-control-stack)))
582   (assert-no-consing (make-array-on-stack-6))
583   (assert-no-consing (make-array-on-stack-7))
584   (assert-no-consing (make-array-on-stack-8))
585   (assert-no-consing (make-array-on-stack-9))
586   (assert-no-consing (make-array-on-stack-10)))
587
588 (with-test (:name (:no-consing :dx-raw-instances) :fails-on :ppc :skipped-on '(not :raw-instance-init-vops))
589   (let (a b)
590     (setf a 1.24 b 1.23d0)
591     (assert-no-consing (make-foo2-on-stack a b)))
592     (assert-no-consing (make-foo3-on-stack)))
593
594 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
595
596 (defvar *table* (make-hash-table))
597
598 (defun test-hash-table ()
599   (setf (gethash 5 *table*) 13)
600   (gethash 5 *table*))
601
602 ;; This fails on threaded PPC because the hash-table implementation
603 ;; uses recursive system locks, which cons (see below for test
604 ;; (:no-consing :lock), which also fails on threaded PPC).
605 (with-test (:name (:no-consing :hash-tables) :fails-on '(and :ppc :sb-thread))
606   (assert-no-consing (test-hash-table)))
607
608 ;;; with-mutex should use DX and not cons
609
610 (defvar *mutex* (sb-thread::make-mutex :name "mutexlock"))
611
612 (defun test-mutex ()
613   (sb-thread:with-mutex (*mutex*)
614     (true *mutex*)))
615
616 (with-test (:name (:no-consing :mutex) :fails-on :ppc :skipped-on '(not :sb-thread))
617   (assert-no-consing (test-mutex)))
618 \f
619
620 ;;; Bugs found by Paul F. Dietz
621
622 (with-test (:name (:dx-bug-misc :pfdietz))
623   (assert
624    (eq
625     (funcall
626      (compile
627       nil
628       '(lambda (a b)
629         (declare (optimize (speed 2) (space 0) (safety 0)
630                   (debug 1) (compilation-speed 3)))
631         (let* ((v5 (cons b b)))
632           (declare (dynamic-extent v5))
633           a)))
634      'x 'y)
635     'x)))
636
637 ;;; bug reported by Svein Ove Aas
638 (defun svein-2005-ii-07 (x y)
639   (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
640   (let ((args (list* y 1 2 x)))
641     (declare (dynamic-extent args))
642     (apply #'aref args)))
643
644 (with-test (:name (:dx-bugs-misc :svein-2005-ii-07))
645   (assert (eql
646            (svein-2005-ii-07
647             '(0)
648             #3A(((1 1 1) (1 1 1) (1 1 1))
649                 ((1 1 1) (1 1 1) (4 1 1))
650                 ((1 1 1) (1 1 1) (1 1 1))))
651            4)))
652
653 ;;; bug reported by Brian Downing: stack-allocated arrays were not
654 ;;; filled with zeroes.
655 (defun-with-dx bdowning-2005-iv-16 ()
656   (let ((a (make-array 11 :initial-element 0)))
657     (declare (dynamic-extent a))
658     (assert (every (lambda (x) (eql x 0)) a))))
659
660 (with-test (:name (:dx-bug-misc :bdowning-2005-iv-16))
661   #+(or hppa mips x86 x86-64)
662   (assert-no-consing (bdowning-2005-iv-16))
663   (bdowning-2005-iv-16))
664
665 (declaim (inline my-nconc))
666 (defun-with-dx my-nconc (&rest lists)
667   (declare (dynamic-extent lists))
668   (apply #'nconc lists))
669 (defun-with-dx my-nconc-caller (a b c)
670   (let ((l1 (list a b c))
671         (l2 (list a b c)))
672     (my-nconc l1 l2)))
673 (with-test (:name :rest-stops-the-buck)
674   (let ((list1 (my-nconc-caller 1 2 3))
675         (list2 (my-nconc-caller 9 8 7)))
676     (assert (equal list1 '(1 2 3 1 2 3)))
677     (assert (equal list2 '(9 8 7 9 8 7)))))
678
679 (defun-with-dx let-converted-vars-dx-allocated-bug (x y z)
680   (let* ((a (list x y z))
681          (b (list x y z))
682          (c (list a b)))
683     (declare (dynamic-extent c))
684     (values (first c) (second c))))
685 (with-test (:name :let-converted-vars-dx-allocated-bug)
686   (multiple-value-bind (i j) (let-converted-vars-dx-allocated-bug 1 2 3)
687     (assert (and (equal i j)
688                  (equal i (list 1 2 3))))))
689
690 ;;; workaround for bug 419 -- real issue remains, but check that the
691 ;;; bandaid holds.
692 (defun-with-dx bug419 (x)
693   (multiple-value-call #'list
694     (eval '(values 1 2 3))
695     (let ((x x))
696       (declare (dynamic-extent x))
697       (flet ((mget (y)
698                (+ x y))
699              (mset (z)
700                (incf x z)))
701         (declare (dynamic-extent #'mget #'mset))
702         ((lambda (f g) (eval `(progn ,f ,g (values 4 5 6)))) #'mget #'mset)))))
703
704 (with-test (:name (:dx-bug-misc :bug419))
705   (assert (equal (bug419 42) '(1 2 3 4 5 6))))
706
707 ;;; Multiple DX arguments in a local function call
708 (defun test-dx-flet-test (fun n f1 f2 f3)
709   (let ((res (with-output-to-string (s)
710                (assert (eql n (ignore-errors (funcall fun s)))))))
711     (multiple-value-bind (x pos) (read-from-string res nil)
712       (assert (equalp f1 x))
713       (multiple-value-bind (y pos2) (read-from-string res nil nil :start pos)
714         (assert (equalp f2 y))
715         (assert (equalp f3 (read-from-string res nil nil :start pos2))))))
716   #+(or hppa mips x86 x86-64)
717   (assert-no-consing (assert (eql n (funcall fun nil))))
718   (assert (eql n (funcall fun nil))))
719
720 (macrolet ((def (n f1 f2 f3)
721              (let ((name (sb-pcl::format-symbol :cl-user "DX-FLET-TEST.~A" n)))
722                `(progn
723                   (defun-with-dx ,name (s)
724                     (flet ((f (x)
725                              (declare (dynamic-extent x))
726                              (when s
727                                (print x s)
728                                (finish-output s))
729                              nil))
730                       (f ,f1)
731                       (f ,f2)
732                       (f ,f3)
733                       ,n))
734                   (with-test (:name (:dx-flet-test ,n))
735                     (test-dx-flet-test #',name ,n ,f1 ,f2 ,f3))))))
736   (def 0 (list :one) (list :two) (list :three))
737   (def 1 (make-array 128) (list 1 2 3 4 5 6 7 8) (list 'list))
738   (def 2 (list 1) (list 2 3) (list 4 5 6 7)))
739
740 ;;; Test that unknown-values coming after a DX value won't mess up the
741 ;;; stack analysis
742 (defun test-update-uvl-live-sets (x y z)
743  (declare (optimize speed (safety 0)))
744  (flet ((bar (a b)
745           (declare (dynamic-extent a))
746           (eval `(list (length ',a) ',b))))
747    (list (bar x y)
748          (bar (list x y z)                  ; dx push
749               (list
750                (multiple-value-call 'list
751                  (eval '(values 1 2 3))     ; uv push
752                  (max y z)
753                )                            ; uv pop
754                14)
755          ))))
756
757 (with-test (:name (:update-uvl-live-sets))
758   (assert (equal '((0 4) (3 ((1 2 3 5) 14)))
759                  (test-update-uvl-live-sets #() 4 5))))
760
761 (with-test (:name :regression-1.0.23.38)
762   (compile nil '(lambda ()
763                  (flet ((make (x y)
764                           (let ((res (cons x x)))
765                             (setf (cdr res) y)
766                             res)))
767                    (declaim (inline make))
768                    (let ((z (make 1 2)))
769                      (declare (dynamic-extent z))
770                      (print z)
771                      t))))
772   (compile nil '(lambda ()
773                  (flet ((make (x y)
774                           (let ((res (cons x x)))
775                             (setf (cdr res) y)
776                             (if x res y))))
777                    (declaim (inline make))
778                    (let ((z (make 1 2)))
779                      (declare (dynamic-extent z))
780                      (print z)
781                      t)))))
782
783 ;;; On x86 and x86-64 upto 1.0.28.16 LENGTH and WORDS argument
784 ;;; tns to ALLOCATE-VECTOR-ON-STACK could be packed in the same
785 ;;; location, leading to all manner of badness. ...reproducing this
786 ;;; reliably is hard, but this it at least used to break on x86-64.
787 (defun length-and-words-packed-in-same-tn (m)
788   (declare (optimize speed (safety 0) (debug 0) (space 0)))
789   (let ((array (make-array (max 1 m) :element-type 'fixnum)))
790     (declare (dynamic-extent array))
791     (array-total-size array)))
792 (with-test (:name :length-and-words-packed-in-same-tn)
793   (assert (= 1 (length-and-words-packed-in-same-tn -3))))
794
795 (with-test (:name :handler-case-bogus-compiler-note :fails-on :ppc)
796   (handler-bind
797       ((compiler-note (lambda (note)
798                         (error "compiler issued note ~S during test" note))))
799     ;; Taken from SWANK, used to signal a bogus stack allocation
800     ;; failure note.
801     (compile nil
802              `(lambda (files fasl-dir load)
803                 (let ((needs-recompile nil))
804                   (dolist (src files)
805                     (let ((dest (binary-pathname src fasl-dir)))
806                       (handler-case
807                           (progn
808                             (when (or needs-recompile
809                                       (not (probe-file dest))
810                                       (file-newer-p src dest))
811                               (setq needs-recompile t)
812                               (ensure-directories-exist dest)
813                               (compile-file src :output-file dest :print nil :verbose t))
814                             (when load
815                               (load dest :verbose t)))
816                         (serious-condition (c)
817                           (handle-loadtime-error c dest))))))))))
818
819 (declaim (inline foovector barvector))
820 (defun foovector (x y z)
821   (let ((v (make-array 3)))
822     (setf (aref v 0) x
823           (aref v 1) y
824           (aref v 2) z)
825     v))
826 (defun barvector (x y z)
827   (make-array 3 :initial-contents (list x y z)))
828 (with-test (:name :dx-compiler-notes :fails-on :ppc)
829   (flet ((assert-notes (j lambda)
830            (let ((n 0))
831              (handler-bind ((compiler-note (lambda (c)
832                                              (declare (ignore c))
833                                              (incf n))))
834                (compile nil lambda)
835                (unless (= j n)
836                  (error "Wanted ~S notes, got ~S for~%   ~S"
837                         j n lambda))))))
838     ;; These ones should complain.
839     (assert-notes 1 `(lambda (x)
840                        (let ((v (make-array x)))
841                          (declare (dynamic-extent v))
842                          (length v))))
843     (assert-notes 2 `(lambda (x)
844                        (let ((y (if (plusp x)
845                                     (true x)
846                                     (true (- x)))))
847                          (declare (dynamic-extent y))
848                          (print y)
849                          nil)))
850     (assert-notes 1 `(lambda (x)
851                        (let ((y (foovector x x x)))
852                          (declare (sb-int:truly-dynamic-extent y))
853                          (print y)
854                          nil)))
855     ;; These ones should not complain.
856     (assert-notes 0 `(lambda (name)
857                        (with-alien
858                            ((posix-getenv (function c-string c-string)
859                                           :EXTERN "getenv"))
860                          (values
861                           (alien-funcall posix-getenv name)))))
862     (assert-notes 0 `(lambda (x)
863                        (let ((y (barvector x x x)))
864                          (declare (dynamic-extent y))
865                          (print y)
866                          nil)))
867     (assert-notes 0 `(lambda (list)
868                        (declare (optimize (space 0)))
869                        (sort list #'<)))
870     (assert-notes 0 `(lambda (other)
871                        #'(lambda (s c n)
872                            (ignore-errors (funcall other s c n)))))))
873
874 ;;; Stack allocating a value cell in HANDLER-CASE would blow up stack
875 ;;; in an unfortunate loop.
876 (defun handler-case-eating-stack ()
877   (let ((sp nil))
878     (do ((n 0 (logand most-positive-fixnum (1+ n))))
879         ((>= n 1024))
880      (multiple-value-bind (value error) (ignore-errors)
881        (when (and value error) nil))
882       (if sp
883           (assert (= sp (sb-c::%primitive sb-c:current-stack-pointer)))
884           (setf sp (sb-c::%primitive sb-c:current-stack-pointer))))))
885 (with-test (:name :handler-case-eating-stack :fails-on :ppc)
886   (assert-no-consing (handler-case-eating-stack)))
887
888 ;;; A nasty bug where RECHECK-DYNAMIC-EXTENT-LVARS thought something was going
889 ;;; to be stack allocated when it was not, leading to a bogus %NIP-VALUES.
890 ;;; Fixed by making RECHECK-DYNAMIC-EXTENT-LVARS deal properly with nested DX.
891 (deftype vec ()
892   `(simple-array single-float (3)))
893 (declaim (ftype (function (t t t) vec) vec))
894 (declaim (inline vec))
895 (defun vec (a b c)
896   (make-array 3 :element-type 'single-float :initial-contents (list a b c)))
897 (defun bad-boy (vec)
898   (declare (type vec vec))
899   (lambda (fun)
900     (let ((vec (vec (aref vec 0) (aref vec 1) (aref vec 2))))
901       (declare (dynamic-extent vec))
902       (funcall fun vec))))
903 (with-test (:name :recheck-nested-dx-bug :fails-on :ppc)
904   (assert (funcall (bad-boy (vec 1.0 2.0 3.3))
905                    (lambda (vec) (equalp vec (vec 1.0 2.0 3.3)))))
906   (flet ((foo (x) (declare (ignore x))))
907     (let ((bad-boy (bad-boy (vec 2.0 3.0 4.0))))
908       (assert-no-consing (funcall bad-boy #'foo)))))
909
910 (with-test (:name :bug-497321)
911   (flet ((test (lambda type)
912            (let ((n 0))
913              (handler-bind ((condition (lambda (c)
914                                          (incf n)
915                                          (unless (typep c type)
916                                            (error "wanted ~S for~%  ~S~%got ~S"
917                                                   type lambda (type-of c))))))
918                (compile nil lambda))
919              (assert (= n 1)))))
920     (test `(lambda () (declare (dynamic-extent #'bar)))
921           'style-warning)
922     (test `(lambda () (declare (dynamic-extent bar)))
923           'style-warning)
924     (test `(lambda (bar) (cons bar (lambda () (declare (dynamic-extent bar)))))
925           'sb-ext:compiler-note)
926     (test `(lambda ()
927              (flet ((bar () t))
928                (cons #'bar (lambda () (declare (dynamic-extent #'bar))))))
929           'sb-ext:compiler-note)))
930
931 (with-test (:name :bug-586105 :fails-on '(not (and :stack-allocatable-vectors
932                                                    :stack-allocatable-lists)))
933   (flet ((test (x)
934            (let ((vec (make-array 1 :initial-contents (list (list x)))))
935              (declare (dynamic-extent vec))
936              (assert (eql x (car (aref vec 0)))))))
937     (assert-no-consing (test 42))))
938 \f
939 (defun bug-681092 ()
940   (declare (optimize speed))
941   (let ((c 0))
942     (flet ((bar () c))
943       (declare (dynamic-extent #'bar))
944       (do () ((list) (bar))
945         (setf c 10)
946         (return (bar))))))
947 (with-test (:name :bug-681092)
948   (assert (= 10 (bug-681092))))
949
950 ;;;; &REST lists should stop DX propagation -- not required by ANSI,
951 ;;;; but required by sanity.
952
953 (declaim (inline rest-stops-dx))
954 (defun-with-dx rest-stops-dx (&rest args)
955   (declare (dynamic-extent args))
956   (apply #'opaque-identity args))
957
958 (defun-with-dx rest-stops-dx-ok ()
959   (equal '(:foo) (rest-stops-dx (list :foo))))
960
961 (with-test (:name :rest-stops-dynamic-extent)
962   (assert (rest-stops-dx-ok)))
963
964 ;;;; These tests aren't strictly speaking DX, but rather &REST -> &MORE
965 ;;;; conversion.
966 (with-test (:name :rest-to-more-conversion)
967   (let ((f1 (compile nil `(lambda (f &rest args)
968                             (apply f args)))))
969     (assert-no-consing (assert (eql f1 (funcall f1 #'identity f1)))))
970   (let ((f2 (compile nil `(lambda (f1 f2 &rest args)
971                             (values (apply f1 args) (apply f2 args))))))
972     (assert-no-consing (multiple-value-bind (a b)
973                            (funcall f2 (lambda (x y z) (+ x y z)) (lambda (x y z) (- x y z))
974                                     1 2 3)
975                          (assert (and (eql 6 a) (eql -4 b))))))
976   (let ((f3 (compile nil `(lambda (f &optional x &rest args)
977                             (when x
978                               (apply f x args))))))
979     (assert-no-consing (assert (eql 42 (funcall f3
980                                                 (lambda (a b c) (+ a b c))
981                                                 11
982                                                 10
983                                                 21)))))
984   (let ((f4 (compile nil `(lambda (f &optional x &rest args &key y &allow-other-keys)
985                             (apply f y x args)))))
986     (assert-no-consing (funcall f4 (lambda (y x yk y2 b c)
987                                      (assert (eq y 'y))
988                                      (assert (= x 2))
989                                      (assert (eq :y yk))
990                                      (assert (eq y2 'y))
991                                      (assert (eq b 'b))
992                                      (assert (eq c 'c)))
993                                 2 :y 'y 'b 'c)))
994   (let ((f5 (compile nil `(lambda (a b c &rest args)
995                             (apply #'list* a b c args)))))
996     (assert (equal '(1 2 3 4 5 6 7) (funcall f5 1 2 3 4 5 6 '(7)))))
997   (let ((f6 (compile nil `(lambda (x y)
998                             (declare (optimize speed))
999                             (concatenate 'string x y)))))
1000     (assert (equal "foobar" (funcall f6 "foo" "bar"))))
1001   (let ((f7 (compile nil `(lambda (&rest args)
1002                             (lambda (f)
1003                               (apply f args))))))
1004     (assert (equal '(a b c d e f) (funcall (funcall f7 'a 'b 'c 'd 'e 'f) 'list))))
1005   (let ((f8 (compile nil `(lambda (&rest args)
1006                             (flet ((foo (f)
1007                                      (apply f args)))
1008                               #'foo)))))
1009     (assert (equal '(a b c d e f) (funcall (funcall f8 'a 'b 'c 'd 'e 'f) 'list))))
1010   (let ((f9 (compile nil `(lambda (f &rest args)
1011                             (flet ((foo (g)
1012                                      (apply g args)))
1013                               (declare (dynamic-extent #'foo))
1014                               (funcall f #'foo))))))
1015     (assert (equal '(a b c d e f)
1016                    (funcall f9 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1017   (let ((f10 (compile nil `(lambda (f &rest args)
1018                             (flet ((foo (g)
1019                                      (apply g args)))
1020                               (funcall f #'foo))))))
1021     (assert (equal '(a b c d e f)
1022                    (funcall f10 (lambda (f) (funcall f 'list)) 'a 'b 'c 'd 'e 'f))))
1023   (let ((f11 (compile nil `(lambda (x y z)
1024                              (block out
1025                                (labels ((foo (x &rest rest)
1026                                           (apply (lambda (&rest rest2)
1027                                                    (return-from out (values-list rest2)))
1028                                                  x rest)))
1029                                 (if x
1030                                     (foo x y z)
1031                                     (foo y z x))))))))
1032     (multiple-value-bind (a b c) (funcall f11 1 2 3)
1033       (assert (eql a 1))
1034       (assert (eql b 2))
1035       (assert (eql c 3)))))
1036
1037 (defun opaque-funcall (function &rest arguments)
1038   (apply function arguments))
1039
1040 (with-test (:name :implicit-value-cells)
1041   (flet ((test-it (type input output)
1042            (let ((f (compile nil `(lambda (x)
1043                                     (declare (type ,type x))
1044                                     (flet ((inc ()
1045                                              (incf x)))
1046                                       (declare (dynamic-extent #'inc))
1047                                       (list (opaque-funcall #'inc) x))))))
1048              (assert (equal (funcall f input)
1049                             (list output output))))))
1050     (let ((width sb-vm:n-word-bits))
1051       (test-it t (1- most-positive-fixnum) most-positive-fixnum)
1052       (test-it `(unsigned-byte ,(1- width)) (ash 1 (- width 2)) (1+ (ash 1 (- width 2))))
1053       (test-it `(signed-byte ,width) (ash -1 (- width 2)) (1+ (ash -1 (- width 2))))
1054       (test-it `(unsigned-byte ,width) (ash 1 (1- width)) (1+ (ash 1 (1- width))))
1055       (test-it 'single-float 3f0 4f0)
1056       (test-it 'double-float 3d0 4d0)
1057       (test-it '(complex single-float) #c(3f0 4f0) #c(4f0 4f0))
1058       (test-it '(complex double-float) #c(3d0 4d0) #c(4d0 4d0)))))
1059
1060 (with-test (:name :sap-implicit-value-cells)
1061   (let ((f (compile nil `(lambda (x)
1062                            (declare (type system-area-pointer x))
1063                            (flet ((inc ()
1064                                     (setf x (sb-sys:sap+ x 16))))
1065                              (declare (dynamic-extent #'inc))
1066                              (list (opaque-funcall #'inc) x)))))
1067         (width sb-vm:n-machine-word-bits))
1068     (assert (every (lambda (x)
1069                      (sb-sys:sap= x (sb-sys:int-sap (+ 16 (ash 1 (1- width))))))
1070                    (funcall f (sb-sys:int-sap (ash 1 (1- width))))))))