1.0.10.7: multiply-used DX LVARS
[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 (setq sb-c::*check-consistency* t)
18
19 (defmacro defun-with-dx (name arglist &body body)
20   `(locally
21      (declare (optimize sb-c::stack-allocate-dynamic-extent))
22      (defun ,name ,arglist
23        ,@body)))
24
25 (declaim (notinline opaque-identity))
26 (defun opaque-identity (x)
27   x)
28
29 ;;; &REST lists
30 (defun-with-dx dxlength (&rest rest)
31   (declare (dynamic-extent rest))
32   (length rest))
33
34 (assert (= (dxlength 1 2 3) 3))
35 (assert (= (dxlength t t t t t t) 6))
36 (assert (= (dxlength) 0))
37
38 (defun callee (list)
39   (destructuring-bind (a b c d e f &rest g) list
40     (+ a b c d e f (length g))))
41
42 (defun-with-dx dxcaller (&rest rest)
43   (declare (dynamic-extent rest))
44   (callee rest))
45 (assert (= (dxcaller 1 2 3 4 5 6 7) 22))
46
47 (defun-with-dx dxcaller-align-1 (x &rest rest)
48   (declare (dynamic-extent rest))
49   (+ x (callee rest)))
50 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
51 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40))
52
53 ;;; %NIP-VALUES
54 (defun-with-dx test-nip-values ()
55   (flet ((bar (x &rest y)
56            (declare (dynamic-extent y))
57            (if (> x 0)
58                (values x (length y))
59                (values (car y)))))
60     (multiple-value-call #'values
61       (bar 1 2 3 4 5 6)
62       (bar -1 'a 'b))))
63
64 (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a)))
65
66 ;;; LET-variable substitution
67 (defun-with-dx test-let-var-subst1 (x)
68   (let ((y (list x (1- x))))
69     (opaque-identity :foo)
70     (let ((z (the list y)))
71       (declare (dynamic-extent z))
72       (length z))))
73 (assert (eql (test-let-var-subst1 17) 2))
74
75 (defun-with-dx test-let-var-subst2 (x)
76   (let ((y (list x (1- x))))
77     (declare (dynamic-extent y))
78     (opaque-identity :foo)
79     (let ((z (the list y)))
80       (length z))))
81 (assert (eql (test-let-var-subst2 17) 2))
82
83 ;;; DX propagation through LET-return.
84 (defun-with-dx test-lvar-subst (x)
85   (let ((y (list x (1- x))))
86     (declare (dynamic-extent y))
87     (second (let ((z (the list y)))
88               (opaque-identity :foo)
89               z))))
90 (assert (eql (test-lvar-subst 11) 10))
91
92 ;;; this code is incorrect, but the compiler should not fail
93 (defun-with-dx test-let-var-subst-incorrect (x)
94   (let ((y (list x (1- x))))
95     (opaque-identity :foo)
96     (let ((z (the list y)))
97       (declare (dynamic-extent z))
98       (opaque-identity :bar)
99       z)))
100 \f
101 ;;; alignment
102 (defvar *x*)
103 (defun-with-dx test-alignment-dx-list (form)
104   (multiple-value-prog1 (eval form)
105     (let ((l (list 1 2 3 4)))
106       (declare (dynamic-extent l))
107       (setq *x* (copy-list l)))))
108 (dotimes (n 64)
109   (let* ((res (loop for i below n collect i))
110          (form `(values ,@res)))
111     (assert (equal (multiple-value-list (test-alignment-dx-list form)) res))
112     (assert (equal *x* '(1 2 3 4)))))
113
114 ;;; closure
115
116 (declaim (notinline true))
117 (defun true (x)
118   (declare (ignore x))
119   t)
120
121 (defun-with-dx dxclosure (x)
122   (flet ((f (y)
123            (+ y x)))
124     (declare (dynamic-extent #'f))
125     (true #'f)))
126
127 (assert (eq t (dxclosure 13)))
128
129 ;;; value-cells
130
131 (defun-with-dx dx-value-cell (x)
132   ;; Not implemented everywhere, yet.
133   #+(or x86 x86-64 mips)
134   (let ((cell x))
135     (declare (dynamic-extent cell))
136     (flet ((f ()
137              (incf cell)))
138       (declare (dynamic-extent #'f))
139       (true #'f))))
140
141 ;;; CONS
142
143 (defun-with-dx cons-on-stack (x)
144   (let ((cons (cons x x)))
145     (declare (dynamic-extent cons))
146     (true cons)
147     nil))
148
149 ;;; Nested DX
150
151 (defun-with-dx nested-dx-lists ()
152   (let ((dx (list (list 1 2) (list 3 4))))
153     (declare (dynamic-extent dx))
154     (true dx)
155     nil))
156
157 (defun-with-dx nested-dx-conses ()
158   (let ((dx (cons 1 (cons 2 (cons 3 (cons (cons t t) nil))))))
159     (declare (dynamic-extent dx))
160     (true dx)
161     nil))
162
163 ;;; multiple uses for dx lvar
164
165 (defun-with-dx multiple-dx-uses ()
166   (let ((dx (if (true t)
167                 (list 1 2 3)
168                 (list 2 3 4))))
169     (declare (dynamic-extent dx))
170     (true dx)
171     nil))
172
173 ;;; with-spinlock should use DX and not cons
174
175 (defvar *slock* (sb-thread::make-spinlock :name "slocklock"))
176
177 (defun test-spinlock ()
178   (sb-thread::with-spinlock (*slock*)
179     (true *slock*)))
180
181 ;;; not really DX, but GETHASH and (SETF GETHASH) should not cons
182
183 (defvar *table* (make-hash-table))
184
185 (defun test-hash-table ()
186   (setf (gethash 5 *table*) 13)
187   (gethash 5 *table*))
188 \f
189 (defmacro assert-no-consing (form &optional times)
190   `(%assert-no-consing (lambda () ,form) ,times))
191 (defun %assert-no-consing (thunk &optional times)
192   (let ((before (get-bytes-consed))
193         (times (or times 10000)))
194     (declare (type (integer 1 *) times))
195     (dotimes (i times)
196       (funcall thunk))
197     (assert (< (- (get-bytes-consed) before) times))))
198
199 #+(or x86 x86-64 alpha ppc sparc mips)
200 (progn
201   (assert-no-consing (dxclosure 42))
202   (assert-no-consing (dxlength 1 2 3))
203   (assert-no-consing (dxlength t t t t t t))
204   (assert-no-consing (dxlength))
205   (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
206   (assert-no-consing (test-nip-values))
207   (assert-no-consing (test-let-var-subst1 17))
208   (assert-no-consing (test-let-var-subst2 17))
209   (assert-no-consing (test-lvar-subst 11))
210   (assert-no-consing (dx-value-cell 13))
211   (assert-no-consing (cons-on-stack 42))
212   (assert-no-consing (nested-dx-conses))
213   (assert-no-consing (nested-dx-lists))
214   (assert-no-consing (multiple-dx-uses))
215   ;; Not strictly DX..
216   (assert-no-consing (test-hash-table))
217   #+sb-thread
218   (assert-no-consing (test-spinlock)))
219
220 \f
221 ;;; Bugs found by Paul F. Dietz
222 (assert
223  (eq
224   (funcall
225    (compile
226     nil
227     '(lambda (a b)
228       (declare (optimize (speed 2) (space 0) (safety 0)
229                 (debug 1) (compilation-speed 3)))
230       (let* ((v5 (cons b b)))
231         (declare (dynamic-extent v5))
232         a)))
233    'x 'y)
234   'x))
235
236 \f
237 ;;; other bugs
238
239 ;;; bug reported by Svein Ove Aas
240 (defun svein-2005-ii-07 (x y)
241   (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
242   (let ((args (list* y 1 2 x)))
243     (declare (dynamic-extent args))
244     (apply #'aref args)))
245 (assert (eql
246          (svein-2005-ii-07
247           '(0)
248           #3A(((1 1 1) (1 1 1) (1 1 1))
249               ((1 1 1) (1 1 1) (4 1 1))
250               ((1 1 1) (1 1 1) (1 1 1))))
251          4))
252
253 ;;; bug reported by Brian Downing: stack-allocated arrays were not
254 ;;; filled with zeroes.
255 (defun-with-dx bdowning-2005-iv-16 ()
256   (let ((a (make-array 11 :initial-element 0)))
257     (declare (dynamic-extent a))
258     (assert (every (lambda (x) (eql x 0)) a))))
259 (bdowning-2005-iv-16)
260
261 \f