Correct address computation in atomic-incf/aref for wide fixnums
[sbcl.git] / tests / compiler.pure.lisp
1 ;;;; various compiler tests without side effects
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 (cl:in-package :cl-user)
15
16 (load "compiler-test-util.lisp")
17
18 ;; The tests in this file assume that EVAL will use the compiler
19 (when (eq sb-ext:*evaluator-mode* :interpret)
20   (invoke-restart 'run-tests::skip-file))
21
22 ;;; Exercise a compiler bug (by crashing the compiler).
23 ;;;
24 ;;; This test code is from Douglas Crosher's simplified TICKLE-BUG
25 ;;; (2000-09-06 on cmucl-imp).
26 ;;;
27 ;;; The bug was fixed by Douglas Crosher's patch, massaged for SBCL by
28 ;;; Martin Atzmueller (2000-09-13 on sbcl-devel).
29 (funcall (compile nil
30                   '(lambda ()
31                      (labels ((fun1 ()
32                                 (fun2))
33                               (fun2 ()
34                                 (when nil
35                                   (tagbody
36                                    tag
37                                    (fun2)
38                                    (go tag)))
39                                 (when nil
40                                   (tagbody
41                                    tag
42                                    (fun1)
43                                    (go tag)))))
44
45                        (fun1)
46                        nil))))
47
48 ;;; Exercise a compiler bug (by crashing the compiler).
49 ;;;
50 ;;; Tim Moore gave a patch for this bug in CMU CL 2000-05-24 on
51 ;;; cmucl-imp, and Martin Atzmueller applied it to SBCL.
52 (funcall (compile nil
53                   '(lambda (x)
54                      (or (integerp x)
55                          (block used-by-some-y?
56                            (flet ((frob (stk)
57                                     (dolist (y stk)
58                                       (unless (rejected? y)
59                                         (return-from used-by-some-y? t)))))
60                              (declare (inline frob))
61                              (frob (rstk x))
62                              (frob (mrstk x)))
63                            nil))))
64          13)
65
66 ;;; bug 112, reported by Martin Atzmueller 2001-06-25 (originally
67 ;;; from Bruno Haible in CMU CL bugs collection), fixed by
68 ;;; Alexey Dejneka 2002-01-27
69 (assert (= 1 ; (used to give 0 under bug 112)
70            (let ((x 0))
71              (declare (special x))
72              (let ((x 1))
73                (let ((y x))
74                  (declare (special x)) y)))))
75 (assert (= 1 ; (used to give 1 even under bug 112, still works after fix)
76            (let ((x 0))
77              (declare (special x))
78              (let ((x 1))
79                (let ((y x) (x 5))
80                  (declare (special x)) y)))))
81
82 ;;; another LET-related bug fixed by Alexey Dejneka at the same
83 ;;; time as bug 112
84 (multiple-value-bind (fun warnings-p failure-p)
85     ;; should complain about duplicate variable names in LET binding
86     (compile nil
87              '(lambda ()
88                (let (x
89                      (x 1))
90                  (list x))))
91   (declare (ignore warnings-p))
92   (assert (functionp fun))
93   (assert failure-p))
94
95 ;;; bug 169 (reported by Alexey Dejneka 2002-05-12, fixed by David
96 ;;; Lichteblau 2002-05-21)
97 (progn
98   (multiple-value-bind (fun warnings-p failure-p)
99       (compile nil
100                ;; Compiling this code should cause a STYLE-WARNING
101                ;; about *X* looking like a special variable but not
102                ;; being one.
103                '(lambda (n)
104                   (let ((*x* n))
105                     (funcall (symbol-function 'x-getter))
106                     (print *x*))))
107     (assert (functionp fun))
108     (assert warnings-p)
109     (assert (not failure-p)))
110   (multiple-value-bind (fun warnings-p failure-p)
111       (compile nil
112                ;; Compiling this code should not cause a warning
113                ;; (because the DECLARE turns *X* into a special
114                ;; variable as its name suggests it should be).
115                '(lambda (n)
116                   (let ((*x* n))
117                     (declare (special *x*))
118                     (funcall (symbol-function 'x-getter))
119                     (print *x*))))
120     (assert (functionp fun))
121     (assert (not warnings-p))
122     (assert (not failure-p))))
123
124 ;;; a bug in 0.7.4.11
125 (dolist (i '(a b 1 2 "x" "y"))
126   ;; In sbcl-0.7.4.11, the compiler tried to source-transform the
127   ;; TYPEP here but got confused and died, doing
128   ;;   (ASSOC '(AND INTEGERP (SATISFIES PLUSP)))
129   ;;          *BACKEND-TYPE-PREDICATES*
130   ;;          :TEST #'TYPE=)
131   ;; and blowing up because TYPE= tried to call PLUSP on the
132   ;; characters of the MEMBER-TYPE representing STANDARD-CHAR.
133   (when (typep i '(and integer (satisfies oddp)))
134     (print i)))
135 (dotimes (i 14)
136   (when (typep i '(and integer (satisfies oddp)))
137     (print i)))
138
139 ;;; bug 156 (reported by APD sbcl-devel 2002-04-12, fixed by CSR patch
140 ;;; sbcl-devel 2002-07-02): FUNCTION-LAMBDA-EXPRESSION of
141 ;;; interactively-compiled functions was broken by sleaziness and
142 ;;; confusion in the assault on 0.7.0, so this expression used to
143 ;;; signal TYPE-ERROR when it found NIL instead of a DEBUG-SOURCE.
144 (eval '(function-lambda-expression #'(lambda (x) x)))
145
146 ;;; bug caught and fixed by Raymond Toy cmucl-imp 2002-07-10: &REST
147 ;;; variable is not optional.
148 (assert (null (ignore-errors (eval '(funcall (lambda (&rest) 12))))))
149
150 ;;; on the PPC, we got the magic numbers in undefined_tramp wrong for
151 ;;; a while; fixed by CSR 2002-07-18
152 (multiple-value-bind (value error)
153     (ignore-errors (some-undefined-function))
154   (assert (null value))
155   (assert (eq (cell-error-name error) 'some-undefined-function)))
156
157 ;;; Non-symbols shouldn't be allowed as VARs in lambda lists. (Where VAR
158 ;;; is a variable name, as in section 3.4.1 of the ANSI spec.)
159 (assert (null (ignore-errors (eval '(lambda ("foo") 12)))))
160 (assert (ignore-errors (eval '(lambda (foo) 12))))
161 (assert (null (ignore-errors (eval '(lambda (&optional 12) "foo")))))
162 (assert (ignore-errors (eval '(lambda (&optional twelve) "foo"))))
163 (assert (null (ignore-errors (eval '(lambda (&optional (12 12)) "foo")))))
164 (assert (ignore-errors (eval '(lambda (&optional (twelve 12)) "foo"))))
165 (assert (null (ignore-errors (eval '(lambda (&key #\c) "foo")))))
166 (assert (ignore-errors (eval '(lambda (&key c) "foo"))))
167 (assert (null (ignore-errors (eval '(lambda (&key (#\c #\c)) "foo")))))
168 (assert (ignore-errors (eval '(lambda (&key (c #\c)) "foo"))))
169 (assert (null (ignore-errors (eval '(lambda (&key ((#\c #\c) #\c)) "foo")))))
170 (assert (ignore-errors (eval '(lambda (&key ((:c cbyanyothername) #\c)) "foo"))))
171
172 ;;; As reported and fixed by Antonio Martinez-Shotton sbcl-devel
173 ;;; 2002-09-12, this failed in sbcl-0.7.7.23. (with failed AVER
174 ;;; "(LEAF-HAS-SOURCE-NAME-P LEAF)")
175 (assert (= (funcall (eval `(lambda (x) (funcall ,(lambda (y) (+ y 3)) x))) 14)
176            17))
177
178 ;;; bug 181: bad type specifier dropped compiler into debugger
179 (assert (list (compile nil '(lambda (x)
180                              (declare (type (0) x))
181                              x))))
182
183 (let ((f (compile nil '(lambda (x)
184                         (make-array 1 :element-type '(0))))))
185   (assert (null (ignore-errors (funcall f)))))
186
187 ;;; the following functions must not be flushable
188 (dolist (form '((make-sequence 'fixnum 10)
189                 (concatenate 'fixnum nil)
190                 (map 'fixnum #'identity nil)
191                 (merge 'fixnum nil nil #'<)))
192   (assert (not (eval `(locally (declare (optimize (safety 0)))
193                         (ignore-errors (progn ,form t)))))))
194
195 (dolist (form '((values-list (car (list '(1 . 2))))
196                 (fboundp '(set bet))
197                 (atan #c(1 1) (car (list #c(2 2))))
198                 (nthcdr (car (list (floor (cos 3)))) '(1 2 3 4 5))
199                 (nthcdr (car (list 5)) '(1 2 . 3))))
200   (assert (not (eval `(locally (declare (optimize (safety 3)))
201                         (ignore-errors (progn ,form t)))))))
202
203 ;;; feature: we shall complain if functions which are only useful for
204 ;;; their result are called and their result ignored.
205 (loop for (form expected-des) in
206         '(((progn (nreverse (list 1 2)) t)
207            "The return value of NREVERSE should not be discarded.")
208           ((progn (nreconc (list 1 2) (list 3 4)) t)
209            "The return value of NRECONC should not be discarded.")
210           ((locally
211              (declare (inline sort))
212              (sort (list 1 2) #'<) t)
213            ;; FIXME: it would be nice if this warned on non-inlined sort
214            ;; but the current simple boolean function attribute
215            ;; can't express the condition that would be required.
216            "The return value of STABLE-SORT-LIST should not be discarded.")
217           ((progn (sort (vector 1 2) #'<) t)
218            ;; Apparently, SBCL (but not CL) guarantees in-place vector
219            ;; sort, so no warning.
220            nil)
221           ((progn (delete 2 (list 1 2)) t)
222            "The return value of DELETE should not be discarded.")
223           ((progn (delete-if #'evenp (list 1 2)) t)
224            ("The return value of DELETE-IF should not be discarded."))
225           ((progn (delete-if #'evenp (vector 1 2)) t)
226            ("The return value of DELETE-IF should not be discarded."))
227           ((progn (delete-if-not #'evenp (list 1 2)) t)
228            "The return value of DELETE-IF-NOT should not be discarded.")
229           ((progn (delete-duplicates (list 1 2)) t)
230            "The return value of DELETE-DUPLICATES should not be discarded.")
231           ((progn (merge 'list (list 1 3) (list 2 4) #'<) t)
232            "The return value of MERGE should not be discarded.")
233           ((progn (nreconc (list 1 3) (list 2 4)) t)
234            "The return value of NRECONC should not be discarded.")
235           ((progn (nunion (list 1 3) (list 2 4)) t)
236            "The return value of NUNION should not be discarded.")
237           ((progn (nintersection (list 1 3) (list 2 4)) t)
238            "The return value of NINTERSECTION should not be discarded.")
239           ((progn (nset-difference (list 1 3) (list 2 4)) t)
240            "The return value of NSET-DIFFERENCE should not be discarded.")
241           ((progn (nset-exclusive-or (list 1 3) (list 2 4)) t)
242            "The return value of NSET-EXCLUSIVE-OR should not be discarded."))
243       for expected = (if (listp expected-des)
244                        expected-des
245                        (list expected-des))
246       do
247   (multiple-value-bind (fun warnings-p failure-p)
248       (handler-bind ((style-warning (lambda (c)
249                       (if expected
250                         (let ((expect-one (pop expected)))
251                           (assert (search expect-one
252                                           (with-standard-io-syntax
253                                             (let ((*print-right-margin* nil))
254                                               (princ-to-string c))))
255                                   ()
256                                   "~S should have warned ~S, but instead warned: ~A"
257                                   form expect-one c))
258                         (error "~S shouldn't give a(nother) warning, but did: ~A" form c)))))
259         (compile nil `(lambda () ,form)))
260   (declare (ignore warnings-p))
261   (assert (functionp fun))
262   (assert (null expected)
263           ()
264           "~S should have warned ~S, but didn't."
265           form expected)
266   (assert (not failure-p))))
267
268 ;;; a bug in the MAP deftransform caused non-VECTOR array specifiers
269 ;;; to cause errors in the compiler.  Fixed by CSR in 0.7.8.10
270 (assert (list (compile nil '(lambda (x) (map 'simple-array 'identity x)))))
271
272 ;;; bug 129: insufficient syntax checking in MACROLET
273 (multiple-value-bind (result error)
274     (ignore-errors (eval '(macrolet ((foo x `',x)) (foo 1 2 3))))
275   (assert (null result))
276   (assert (typep error 'error)))
277
278 ;;; bug 124: environment of MACROLET-introduced macro expanders
279 (assert (equal
280          (macrolet ((mext (x) `(cons :mext ,x)))
281            (macrolet ((mint (y) `'(:mint ,(mext y))))
282              (list (mext '(1 2))
283                    (mint (1 2)))))
284          '((:MEXT 1 2) (:MINT (:MEXT 1 2)))))
285
286 ;;; bug 48c: SYMBOL-MACROLET should signal PROGRAM-ERROR if introduced
287 ;;; symbol is declared to be SPECIAL
288 (multiple-value-bind (result error)
289     (ignore-errors (funcall (lambda ()
290                               (symbol-macrolet ((s '(1 2)))
291                                   (declare (special s))
292                                 s))))
293   (assert (null result))
294   (assert (typep error 'program-error)))
295
296 ;;; ECASE should treat a bare T as a literal key
297 (multiple-value-bind (result error)
298     (ignore-errors (ecase 1 (t 0)))
299   (assert (null result))
300   (assert (typep error 'type-error)))
301
302 (multiple-value-bind (result error)
303     (ignore-errors (ecase 1 (t 0) (1 2)))
304   (assert (eql result 2))
305   (assert (null error)))
306
307 ;;; FTYPE should accept any functional type specifier
308 (compile nil '(lambda (x) (declare (ftype function f)) (f x)))
309
310 ;;; FUNCALL of special operators and macros should signal an
311 ;;; UNDEFINED-FUNCTION error
312 (multiple-value-bind (result error)
313     (ignore-errors (funcall 'quote 1))
314   (assert (null result))
315   (assert (typep error 'undefined-function))
316   (assert (eq (cell-error-name error) 'quote)))
317 (multiple-value-bind (result error)
318     (ignore-errors (funcall 'and 1))
319   (assert (null result))
320   (assert (typep error 'undefined-function))
321   (assert (eq (cell-error-name error) 'and)))
322
323 ;;; PSETQ should behave when given complex symbol-macro arguments
324 (multiple-value-bind (sequence index)
325     (symbol-macrolet ((x (aref a (incf i)))
326                       (y (aref a (incf i))))
327         (let ((a (copy-seq #(0 1 2 3 4 5 6 7 8 9)))
328               (i 0))
329           (psetq x (aref a (incf i))
330                  y (aref a (incf i)))
331           (values a i)))
332   (assert (equalp sequence #(0 2 2 4 4 5 6 7 8 9)))
333   (assert (= index 4)))
334
335 (multiple-value-bind (result error)
336     (ignore-errors
337       (let ((x (list 1 2)))
338         (psetq (car x) 3)
339         x))
340   (assert (null result))
341   (assert (typep error 'program-error)))
342
343 ;;; COPY-SEQ should work on known-complex vectors:
344 (assert (equalp #(1)
345                 (let ((v (make-array 0 :fill-pointer 0)))
346                   (vector-push-extend 1 v)
347                   (copy-seq v))))
348
349 ;;; to support INLINE functions inside MACROLET, it is necessary for
350 ;;; FUNCTION-LAMBDA-EXPRESSION to return a proper lambda expression in
351 ;;; certain circumstances, one of which is when compile is called from
352 ;;; top-level.
353 (assert (equal
354          (function-lambda-expression
355           (compile nil '(lambda (x) (block nil (print x)))))
356          '(lambda (x) (block nil (print x)))))
357
358 ;;; bug 62: too cautious type inference in a loop
359 (assert (nth-value
360          2
361          (compile nil
362                   '(lambda (a)
363                     (declare (optimize speed (safety 0)))
364                     (typecase a
365                       (array (loop (print (car a)))))))))
366
367 ;;; Bug reported by Robert E. Brown sbcl-devel 2003-02-02: compiler
368 ;;; failure
369 (compile nil
370          '(lambda (key tree collect-path-p)
371            (let ((lessp (key-lessp tree))
372                  (equalp (key-equalp tree)))
373              (declare (type (function (t t) boolean) lessp equalp))
374              (let ((path '(nil)))
375                (loop for node = (root-node tree)
376                   then (if (funcall lessp key (node-key node))
377                            (left-child node)
378                            (right-child node))
379                   when (null node)
380                   do (return (values nil nil nil))
381                   do (when collect-path-p
382                        (push node path))
383                   (when (funcall equalp key (node-key node))
384                     (return (values node path t))))))))
385
386 ;;; CONSTANTLY should return a side-effect-free function (bug caught
387 ;;; by Paul Dietz' test suite)
388 (let ((i 0))
389   (let ((fn (constantly (progn (incf i) 1))))
390     (assert (= i 1))
391     (assert (= (funcall fn) 1))
392     (assert (= i 1))
393     (assert (= (funcall fn) 1))
394     (assert (= i 1))))
395
396 ;;; Bug 240 reported by tonyms on #lisp IRC 2003-02-25 (modified version)
397 (loop for (fun warns-p) in
398      '(((lambda (&optional *x*) *x*) t)
399        ((lambda (&optional *x* &rest y) (values *x* y)) t)
400        ((lambda (&optional *print-length*) (values *print-length*)) nil)
401        ((lambda (&optional *print-length* &rest y) (values *print-length* y)) nil)
402        ((lambda (&optional *x*) (declare (special *x*)) (values *x*)) nil)
403        ((lambda (&optional *x* &rest y) (declare (special *x*)) (values *x* y)) nil))
404    for real-warns-p = (nth-value 1 (compile nil fun))
405    do (assert (eq warns-p real-warns-p)))
406
407 ;;; Bug reported by Gilbert Baumann on #lisp IRC 2003-03-26
408 (assert (equal (funcall (eval '(lambda (x &optional (y (pop x))) (list x y)))
409                         '(1 2))
410                '((2) 1)))
411
412 ;;; Bug reported by Paul Dietz on cmucl-imp and fixed by Gerd
413 ;;; Moellmann: CONVERT-MORE-CALL failed on the following call
414 (assert (eq (eval '((lambda (&key) 'u) :allow-other-keys nil)) 'u))
415
416 (assert
417  (raises-error? (multiple-value-bind (a b c)
418                     (eval '(truncate 3 4))
419                   (declare (integer c))
420                   (list a b c))
421                 type-error))
422
423 (assert (equal (multiple-value-list (the (values &rest integer)
424                                       (eval '(values 3))))
425                '(3)))
426
427 ;;; Bug relating to confused representation for the wild function
428 ;;; type:
429 (assert (null (funcall (eval '(lambda () (multiple-value-list (values)))))))
430
431 ;;; &ENVIRONMENT parameter should be bound first (from Paul Dietz'
432 ;;; test suite)
433 (assert (eql (macrolet ((foo () 1))
434                (macrolet ((%f (&optional (x (macroexpand '(foo) env)) &environment env)
435                             x))
436                  (%f)))
437              1))
438
439 ;;; MACROLET should check for duplicated names
440 (dolist (ll '((x (z x))
441               (x y &optional z x w)
442               (x y &optional z z)
443               (x &rest x)
444               (x &rest (y x))
445               (x &optional (y nil x))
446               (x &optional (y nil y))
447               (x &key x)
448               (x &key (y nil x))
449               (&key (y nil z) (z nil w))
450               (&whole x &optional x)
451               (&environment x &whole x)))
452   (assert (nth-value 2
453                      (handler-case
454                          (compile nil
455                                   `(lambda ()
456                                      (macrolet ((foo ,ll nil)
457                                                 (bar (&environment env)
458                                                   `',(macro-function 'foo env)))
459                                        (bar))))
460                        (error (c)
461                          (values nil t t))))))
462
463 (assert (typep (eval `(the arithmetic-error
464                            ',(make-condition 'arithmetic-error)))
465                'arithmetic-error))
466
467 (assert (not (nth-value
468               2 (compile nil '(lambda ()
469                                (make-array nil :initial-element 11))))))
470
471 (assert (raises-error? (funcall (eval #'open) "assertoid.lisp"
472                                 :external-format '#:nonsense)))
473 (assert (raises-error? (funcall (eval #'load) "assertoid.lisp"
474                                 :external-format '#:nonsense)))
475
476 (assert (= (the (values integer symbol) (values 1 'foo 13)) 1))
477
478 (let ((f (compile nil
479                   '(lambda (v)
480                     (declare (optimize (safety 3)))
481                     (list (the fixnum (the (real 0) (eval v))))))))
482   (assert (raises-error? (funcall f 0.1) type-error))
483   (assert (raises-error? (funcall f -1) type-error)))
484
485 ;;; the implicit block does not enclose lambda list
486 (let ((forms '((defmacro #1=#:foo (&optional (x (return-from #1#))))
487                #+nil(macrolet ((#2=#:foo (&optional (x (return-from #2#))))))
488                (define-compiler-macro #3=#:foo (&optional (x (return-from #3#))))
489                (deftype #4=#:foo (&optional (x (return-from #4#))))
490                (define-setf-expander #5=#:foo (&optional (x (return-from #5#))))
491                (defsetf #6=#:foo (&optional (x (return-from #6#))) ()))))
492   (dolist (form forms)
493     (assert (nth-value 2 (compile nil `(lambda () ,form))))))
494
495 (assert (nth-value 2 (compile nil
496                               '(lambda ()
497                                 (svref (make-array '(8 9) :adjustable t) 1)))))
498
499 ;;; CHAR= did not check types of its arguments (reported by Adam Warner)
500 (raises-error? (funcall (compile nil '(lambda (x y z) (char= x y z)))
501                         #\a #\b nil)
502                type-error)
503 (raises-error? (funcall (compile nil
504                                  '(lambda (x y z)
505                                    (declare (optimize (speed 3) (safety 3)))
506                                    (char/= x y z)))
507                         nil #\a #\a)
508                type-error)
509
510 ;;; Compiler lost return type of MAPCAR and friends
511 (dolist (fun '(mapcar mapc maplist mapl))
512   (assert (nth-value 2 (compile nil
513                                 `(lambda (x)
514                                    (1+ (,fun #'print x)))))))
515
516 (assert (nth-value 2 (compile nil
517                               '(lambda ()
518                                 (declare (notinline mapcar))
519                                 (1+ (mapcar #'print '(1 2 3)))))))
520
521 ;;; bug found by Paul Dietz: (SETF AREF) for bit vectors with constant
522 ;;; index was effectless
523 (let ((f (compile nil '(lambda (a v)
524                         (declare (type simple-bit-vector a) (type bit v))
525                         (declare (optimize (speed 3) (safety 0)))
526                         (setf (aref a 0) v)
527                         a))))
528   (let ((y (make-array 2 :element-type 'bit :initial-element 0)))
529     (assert (equal y #*00))
530     (funcall f y 1)
531     (assert (equal y #*10))))
532
533 ;;; use of declared array types
534 (handler-bind ((sb-ext:compiler-note #'error))
535   (compile nil '(lambda (x)
536                  (declare (type (simple-array (simple-string 3) (5)) x)
537                           (optimize speed))
538                  (aref (aref x 0) 0))))
539
540 (handler-bind ((sb-ext:compiler-note #'error))
541   (compile nil '(lambda (x)
542                  (declare (type (simple-array (simple-array bit (10)) (10)) x)
543                           (optimize speed))
544                  (1+ (aref (aref x 0) 0)))))
545
546 ;;; compiler failure
547 (let ((f (compile nil '(lambda (x) (typep x '(not (member 0d0)))))))
548   (assert (funcall f 1d0)))
549
550 (compile nil '(lambda (x)
551                (declare (double-float x))
552                (let ((y (* x pi)))
553                  (atan y y))))
554
555 ;;; bogus optimization of BIT-NOT
556 (multiple-value-bind (result x)
557     (eval '(let ((x (eval #*1001)))
558             (declare (optimize (speed 2) (space 3))
559                      (type (bit-vector) x))
560             (values (bit-not x nil) x)))
561   (assert (equal x #*1001))
562   (assert (equal result #*0110)))
563
564 ;;; the VECTOR type in CONCATENATE/MERGE/MAKE-SEQUENCE means (VECTOR T).
565 (handler-bind ((sb-ext:compiler-note #'error))
566   (assert (equalp (funcall
567                    (compile
568                     nil
569                     '(lambda ()
570                       (let ((x (make-sequence 'vector 10 :initial-element 'a)))
571                         (setf (aref x 4) 'b)
572                         x))))
573                   #(a a a a b a a a a a))))
574
575 ;;; this is not a check for a bug, but rather a test of compiler
576 ;;; quality
577 (dolist (type '((integer 0 *)           ; upper bound
578                 (real (-1) *)
579                 float                   ; class
580                 (real * (-10))          ; lower bound
581                 ))
582   (assert (nth-value
583            1 (compile nil
584                       `(lambda (n)
585                          (declare (optimize (speed 3) (compilation-speed 0)))
586                          (loop for i from 1 to (the (integer -17 10) n) by 2
587                                collect (when (> (random 10) 5)
588                                          (the ,type (- i 11)))))))))
589
590 ;;; bug 278b
591 ;;;
592 ;;; We suppose that INTEGER arithmetic cannot be efficient, and the
593 ;;; compiler has an optimized VOP for +; so this code should cause an
594 ;;; efficiency note.
595 (assert (eq (block nil
596               (handler-case
597                   (compile nil '(lambda (i)
598                                  (declare (optimize speed))
599                                  (declare (type integer i))
600                                  (+ i 2)))
601                 (sb-ext:compiler-note (c) (return :good))))
602             :good))
603
604 ;;; bug 277: IGNORE/IGNORABLE declarations should be acceptable for
605 ;;; symbol macros
606 (assert (not (nth-value 1 (compile nil '(lambda (u v)
607                                          (symbol-macrolet ((x u)
608                                                            (y v))
609                                              (declare (ignore x)
610                                                       (ignorable y))
611                                            (list u v)))))))
612
613 ;;; bug reported by Paul Dietz: wrong optimizer for (EXPT ... 0)
614 (loop for (x type) in
615       '((14 integer)
616         (14 rational)
617         (-14/3 (rational -8 11))
618         (3s0 short-float)
619         (4f0 single-float)
620         (5d0 double-float)
621         (6l0 long-float)
622         (14 real)
623         (13/2 real)
624         (2s0 real)
625         (2d0 real)
626         (#c(-3 4) (complex fixnum))
627         (#c(-3 4) (complex rational))
628         (#c(-3/7 4) (complex rational))
629         (#c(2s0 3s0) (complex short-float))
630         (#c(2f0 3f0) (complex single-float))
631         (#c(2d0 3d0) (complex double-float))
632         (#c(2l0 3l0) (complex long-float))
633         (#c(2d0 3s0) (complex float))
634         (#c(2 3f0) (complex real))
635         (#c(2 3d0) (complex real))
636         (#c(-3/7 4) (complex real))
637         (#c(-3/7 4) complex)
638         (#c(2 3l0) complex))
639       do (dolist (zero '(0 0s0 0f0 0d0 0l0))
640            (dolist (real-zero (list zero (- zero)))
641              (let* ((src `(lambda (x) (expt (the ,type x) ,real-zero)))
642                     (fun (compile nil src))
643                     (result (1+ (funcall (eval #'*) x real-zero))))
644                (assert (eql result (funcall fun x)))))))
645
646 ;;; (SIGNED-BYTE 1) [ returned from the logxor derive-type optimizer ]
647 ;;; wasn't recognized as a good type specifier.
648 (let ((fun (lambda (x y)
649              (declare (type (integer -1 0) x y) (optimize speed))
650              (logxor x y))))
651   (assert (= (funcall fun 0 0) 0))
652   (assert (= (funcall fun 0 -1) -1))
653   (assert (= (funcall fun -1 -1) 0)))
654
655 ;;; from PFD's torture test, triggering a bug in our effective address
656 ;;; treatment.
657 (compile
658  nil
659  `(lambda (a b)
660     (declare (type (integer 8 22337) b))
661     (logandc2
662      (logandc2
663       (* (logandc1 (max -29303 b) 4) b)
664       (abs (logorc1 (+ (logandc1 -11 b) 2607688420) -31153924)))
665      (logeqv (max a 0) b))))
666
667 ;;; Alpha floating point modes weren't being reset after an exception,
668 ;;; leading to an exception on the second compile, below.
669 (compile nil '(lambda (x y) (declare (type (double-float 0.0d0) x y)) (/ x y)))
670 (handler-case (/ 1.0 0.0)
671   ;; provoke an exception
672   (arithmetic-error ()))
673 (compile nil '(lambda (x y) (declare (type (double-float 0.0d0) x y)) (/ x y)))
674
675 ;;; bug reported by Paul Dietz: component last block does not have
676 ;;; start ctran
677 (compile nil
678          '(lambda ()
679            (declare (notinline + logand)
680             (optimize (speed 0)))
681            (LOGAND
682             (BLOCK B5
683               (FLET ((%F1 ()
684                        (RETURN-FROM B5 -220)))
685                 (LET ((V7 (%F1)))
686                   (+ 359749 35728422))))
687             -24076)))
688
689 ;;; bug 294 reported by Paul Dietz: miscompilation of REM and MOD
690 (assert (= (funcall (compile nil `(lambda (b)
691                                     (declare (optimize (speed 3))
692                                              (type (integer 2 152044363) b))
693                                     (rem b (min -16 0))))
694                     108251912)
695            8))
696
697 (assert (= (funcall (compile nil `(lambda (c)
698                                     (declare (optimize (speed 3))
699                                              (type (integer 23062188 149459656) c))
700                                     (mod c (min -2 0))))
701                     95019853)
702            -1))
703
704 ;;; bug reported by Paul Dietz: block splitting inside FLUSH-DEAD-CODE
705 (compile nil
706          '(LAMBDA (A B C)
707            (BLOCK B6
708              (LOGEQV (REM C -6758)
709                      (REM B (MAX 44 (RETURN-FROM B6 A)))))))
710
711 (compile nil '(lambda ()
712                (block nil
713                  (flet ((foo (x y) (if (> x y) (print x) (print y))))
714                    (foo 1 2)
715                    (bar)
716                    (foo (return 14) 2)))))
717
718 ;;; bug in Alpha backend: not enough sanity checking of arguments to
719 ;;; instructions
720 (assert (= (funcall (compile nil
721                              '(lambda (x)
722                                 (declare (fixnum x))
723                                 (ash x -257)))
724                     1024)
725            0))
726
727 ;;; bug found by WHN and pfdietz: compiler failure while referencing
728 ;;; an entry point inside a deleted lambda
729 (compile nil '(lambda ()
730                (let (r3533)
731                  (flet ((bbfn ()
732                           (setf r3533
733                                 (progn
734                                   (flet ((truly (fn bbd)
735                                            (let (r3534)
736                                              (let ((p3537 nil))
737                                                (unwind-protect
738                                                     (multiple-value-prog1
739                                                         (progn
740                                                           (setf r3534
741                                                                 (progn
742                                                                   (bubf bbd t)
743                                                                   (flet ((c-3536 ()
744                                                                            (funcall fn)))
745                                                                     (cdec #'c-3536
746                                                                           (vector bbd))))))
747                                                       (setf p3537 t))
748                                                  (unless p3537
749                                                    (error "j"))))
750                                              r3534))
751                                          (c (pd) (pdc pd)))
752                                     (let ((a (smock a))
753                                           (b (smock b))
754                                           (b (smock c)))))))))
755                    (wum #'bbfn "hc3" (list)))
756                  r3533)))
757 (compile nil '(lambda () (flet ((%f () (unwind-protect nil))) nil)))
758
759 ;;; the strength reduction of constant multiplication used (before
760 ;;; sbcl-0.8.4.x) to lie to the compiler.  This meant that, under
761 ;;; certain circumstances, the compiler would derive that a perfectly
762 ;;; reasonable multiplication never returned, causing chaos.  Fixed by
763 ;;; explicitly doing modular arithmetic, and relying on the backends
764 ;;; being smart.
765 (assert (= (funcall
766             (compile nil
767                      '(lambda (x)
768                         (declare (type (integer 178956970 178956970) x)
769                                  (optimize speed))
770                         (* x 24)))
771             178956970)
772            4294967280))
773
774 ;;; bug in modular arithmetic and type specifiers
775 (assert (= (funcall (compile nil (lambda (x) (logand x x 0)))
776                     -1)
777            0))
778
779 ;;; MISC.99 from Paul Dietz' random tester: FAST-ASH-MOD32-C VOP
780 ;;; produced wrong result for shift >=32 on X86
781 (assert (= 0 (funcall
782               (compile nil
783                        '(lambda (a)
784                          (declare (type (integer 4303063 101130078) a))
785                          (mask-field (byte 18 2) (ash a 77))))
786               57132532)))
787 ;;; rewrite the test case to get the unsigned-byte 32/64
788 ;;; implementation even after implementing some modular arithmetic
789 ;;; with signed-byte 30:
790 (assert (= 0 (funcall
791               (compile nil
792                        '(lambda (a)
793                          (declare (type (integer 4303063 101130078) a))
794                          (mask-field (byte 30 2) (ash a 77))))
795               57132532)))
796 (assert (= 0 (funcall
797               (compile nil
798                        '(lambda (a)
799                          (declare (type (integer 4303063 101130078) a))
800                          (mask-field (byte 64 2) (ash a 77))))
801               57132532)))
802 ;;; and a similar test case for the signed masking extension (not the
803 ;;; final interface, so change the call when necessary):
804 (assert (= 0 (funcall
805               (compile nil
806                        '(lambda (a)
807                          (declare (type (integer 4303063 101130078) a))
808                          (sb-c::mask-signed-field 30 (ash a 77))))
809               57132532)))
810 (assert (= 0 (funcall
811               (compile nil
812                        '(lambda (a)
813                          (declare (type (integer 4303063 101130078) a))
814                          (sb-c::mask-signed-field 61 (ash a 77))))
815               57132532)))
816
817 ;;; MISC.101 and MISC.103: FLUSH-DEST did not mark the USE's block for
818 ;;; type check regeneration
819 (assert (eql (funcall
820               (compile nil '(lambda (a c)
821                              (declare (type (integer 185501219873 303014665162) a))
822                              (declare (type (integer -160758 255724) c))
823                              (declare (optimize (speed 3)))
824                              (let ((v8
825                                     (- -554046873252388011622614991634432
826                                        (ignore-errors c)
827                                        (unwind-protect 2791485))))
828                                (max (ignore-errors a)
829                                     (let ((v6 (- v8 (restart-case 980))))
830                                       (min v8 v6))))))
831               259448422916 173715)
832              259448422916))
833 (assert (eql (funcall
834               (compile nil '(lambda (a b)
835                              (min -80
836                               (abs
837                                (ignore-errors
838                                  (+
839                                   (logeqv b
840                                           (block b6
841                                             (return-from b6
842                                               (load-time-value -6876935))))
843                                   (if (logbitp 1 a) b (setq a -1522022182249))))))))
844               -1802767029877 -12374959963)
845              -80))
846
847 ;;; various MISC.*, related to NODEs/LVARs with derived type NIL
848 (assert (eql (funcall (compile nil '(lambda (c)
849                                      (declare (type (integer -3924 1001809828) c))
850                                      (declare (optimize (speed 3)))
851                                      (min 47 (if (ldb-test (byte 2 14) c)
852                                                  -570344431
853                                                  (ignore-errors -732893970)))))
854                       705347625)
855              -570344431))
856 (assert (eql (funcall
857               (compile nil '(lambda (b)
858                              (declare (type (integer -1598566306 2941) b))
859                              (declare (optimize (speed 3)))
860                              (max -148949 (ignore-errors b))))
861               0)
862              0))
863 (assert (eql (funcall
864               (compile nil '(lambda (b c)
865                              (declare (type (integer -4 -3) c))
866                              (block b7
867                                (flet ((%f1 (f1-1 f1-2 f1-3)
868                                         (if (logbitp 0 (return-from b7
869                                                          (- -815145138 f1-2)))
870                                             (return-from b7 -2611670)
871                                             99345)))
872                                  (let ((v2 (%f1 -2464 (%f1 -1146 c c) -2)))
873                                    b)))))
874               2950453607 -4)
875              -815145134))
876 (assert (eql (funcall
877               (compile nil
878                        '(lambda (b c)
879                          (declare (type (integer -29742055786 23602182204) b))
880                          (declare (type (integer -7409 -2075) c))
881                          (declare (optimize (speed 3)))
882                          (floor
883                           (labels ((%f2 ()
884                                      (block b6
885                                        (ignore-errors (return-from b6
886                                                         (if (= c 8) b 82674))))))
887                             (%f2)))))
888               22992834060 -5833)
889              82674))
890 (assert (equal (multiple-value-list
891                 (funcall
892                  (compile nil '(lambda (a)
893                                 (declare (type (integer -944 -472) a))
894                                 (declare (optimize (speed 3)))
895                                 (round
896                                  (block b3
897                                    (return-from b3
898                                      (if (= 55957 a) -117 (ignore-errors
899                                                             (return-from b3 a))))))))
900                  -589))
901                '(-589 0)))
902
903 ;;; MISC.158
904 (assert (zerop (funcall
905                 (compile nil
906                          '(lambda (a b c)
907                            (declare (type (integer 79828 2625480458) a))
908                            (declare (type (integer -4363283 8171697) b))
909                            (declare (type (integer -301 0) c))
910                            (if (equal 6392154 (logxor a b))
911                                1706
912                                (let ((v5 (abs c)))
913                                  (logand v5
914                                          (logior (logandc2 c v5)
915                                                  (common-lisp:handler-case
916                                                      (ash a (min 36 22477)))))))))
917                 100000 0 0)))
918
919 ;;; MISC.152, 153: deleted code and iteration var type inference
920 (assert (eql (funcall
921               (compile nil
922                        '(lambda (a)
923                          (block b5
924                            (let ((v1 (let ((v8 (unwind-protect 9365)))
925                                        8862008)))
926                              (*
927                               (return-from b5
928                                 (labels ((%f11 (f11-1) f11-1))
929                                   (%f11 87246015)))
930                               (return-from b5
931                                 (setq v1
932                                       (labels ((%f6 (f6-1 f6-2 f6-3) v1))
933                                         (dpb (unwind-protect a)
934                                              (byte 18 13)
935                                              (labels ((%f4 () 27322826))
936                                                (%f6 -2 -108626545 (%f4))))))))))))
937               -6)
938              87246015))
939
940 (assert (eql (funcall
941               (compile nil
942                        '(lambda (a)
943                          (if (logbitp 3
944                                       (case -2
945                                         ((-96879 -1035 -57680 -106404 -94516 -125088)
946                                          (unwind-protect 90309179))
947                                         ((-20811 -86901 -9368 -98520 -71594)
948                                          (let ((v9 (unwind-protect 136707)))
949                                            (block b3
950                                              (setq v9
951                                                    (let ((v4 (return-from b3 v9)))
952                                                      (- (ignore-errors (return-from b3 v4))))))))
953                                         (t -50)))
954                              -20343
955                              a)))
956               0)
957              -20343))
958
959 ;;; MISC.165
960 (assert (eql (funcall
961               (compile
962                nil
963                '(lambda (a b c)
964                  (block b3
965                    (flet ((%f15
966                               (f15-1 f15-2 f15-3
967                                      &optional
968                                      (f15-4
969                                       (flet ((%f17
970                                                  (f17-1 f17-2 f17-3
971                                                         &optional (f17-4 185155520) (f17-5 c)
972                                                         (f17-6 37))
973                                                c))
974                                         (%f17 -1046 a 1115306 (%f17 b -146330 422) -337817)))
975                                      (f15-5 a) (f15-6 -40))
976                             (return-from b3 -16)))
977                      (multiple-value-call #'%f15 (values -519354 a 121 c -1905))))))
978               0 0 -5)
979              -16))
980
981 ;;; MISC.172
982 (assert (eql (funcall
983               (compile
984                nil
985                '(lambda (a b c)
986                  (declare (notinline list apply))
987                  (declare (optimize (safety 3)))
988                  (declare (optimize (speed 0)))
989                  (declare (optimize (debug 0)))
990                  (labels ((%f12 (f12-1 f12-2)
991                             (labels ((%f2 (f2-1 f2-2)
992                                        (flet ((%f6 ()
993                                                 (flet ((%f18
994                                                            (f18-1
995                                                             &optional (f18-2 a)
996                                                             (f18-3 -207465075)
997                                                             (f18-4 a))
998                                                          (return-from %f12 b)))
999                                                   (%f18 -3489553
1000                                                         -7
1001                                                         (%f18 (%f18 150 -64 f12-1)
1002                                                               (%f18 (%f18 -8531)
1003                                                                     11410)
1004                                                               b)
1005                                                         56362666))))
1006                                          (labels ((%f7
1007                                                       (f7-1 f7-2
1008                                                             &optional (f7-3 (%f6)))
1009                                                     7767415))
1010                                            f12-1))))
1011                               (%f2 b -36582571))))
1012                    (apply #'%f12 (list 774 -4413)))))
1013               0 1 2)
1014              774))
1015
1016 ;;; MISC.173
1017 (assert (eql (funcall
1018               (compile
1019                nil
1020                '(lambda (a b c)
1021                  (declare (notinline values))
1022                  (declare (optimize (safety 3)))
1023                  (declare (optimize (speed 0)))
1024                  (declare (optimize (debug 0)))
1025                  (flet ((%f11
1026                             (f11-1 f11-2
1027                                    &optional (f11-3 c) (f11-4 7947114)
1028                                    (f11-5
1029                                     (flet ((%f3 (f3-1 &optional (f3-2 b) (f3-3 5529))
1030                                              8134))
1031                                       (multiple-value-call #'%f3
1032                                         (values (%f3 -30637724 b) c)))))
1033                           (setq c 555910)))
1034                    (if (and nil (%f11 a a))
1035                        (if (%f11 a 421778 4030 1)
1036                            (labels ((%f7
1037                                         (f7-1 f7-2
1038                                               &optional
1039                                               (f7-3
1040                                                (%f11 -79192293
1041                                                      (%f11 c a c -4 214720)
1042                                                      b
1043                                                      b
1044                                                      (%f11 b 985)))
1045                                               (f7-4 a))
1046                                       b))
1047                              (%f11 c b -25644))
1048                            54)
1049                        -32326608))))
1050               1 2 3)
1051              -32326608))
1052
1053 ;;; MISC.177, 182: IR2 copy propagation missed a hidden write to a
1054 ;;; local lambda argument
1055 (assert
1056  (equal
1057   (funcall
1058    (compile nil
1059             '(lambda (a b c)
1060               (declare (type (integer 804561 7640697) a))
1061               (declare (type (integer -1 10441401) b))
1062               (declare (type (integer -864634669 55189745) c))
1063               (declare (ignorable a b c))
1064               (declare (optimize (speed 3)))
1065               (declare (optimize (safety 1)))
1066               (declare (optimize (debug 1)))
1067               (flet ((%f11
1068                          (f11-1 f11-2)
1069                        (labels ((%f4 () (round 200048 (max 99 c))))
1070                          (logand
1071                           f11-1
1072                           (labels ((%f3 (f3-1) -162967612))
1073                             (%f3 (let* ((v8 (%f4)))
1074                                    (setq f11-1 (%f4)))))))))
1075                 (%f11 -120429363 (%f11 62362 b)))))
1076    6714367 9645616 -637681868)
1077   -264223548))
1078
1079 ;;; Bug reported by Paul F. Dietz caused by derive type loss in VALUE
1080 ;;; transform
1081 (assert (equal (multiple-value-list
1082                 (funcall
1083                  (compile nil '(lambda ()
1084                                 (declare (optimize (speed 1) (space 0) (safety 3) (debug 3) (compilation-speed 1)))
1085                                 (ceiling
1086                                  (ceiling
1087                                   (flet ((%f16 () 0)) (%f16))))))))
1088                '(0 0)))
1089
1090 ;;; MISC.184
1091 (assert (zerop
1092          (funcall
1093           (compile
1094            nil
1095            '(lambda (a b c)
1096              (declare (type (integer 867934833 3293695878) a))
1097              (declare (type (integer -82111 1776797) b))
1098              (declare (type (integer -1432413516 54121964) c))
1099              (declare (optimize (speed 3)))
1100              (declare (optimize (safety 1)))
1101              (declare (optimize (debug 1)))
1102              (if nil
1103                  (flet ((%f15 (f15-1 &optional (f15-2 c))
1104                           (labels ((%f1 (f1-1 f1-2) 0))
1105                             (%f1 a 0))))
1106                    (flet ((%f4 ()
1107                             (multiple-value-call #'%f15
1108                               (values (%f15 c 0) (%f15 0)))))
1109                      (if nil (%f4)
1110                          (flet ((%f8 (f8-1 &optional (f8-2 (%f4)) (f8-3 0))
1111                                   f8-3))
1112                            0))))
1113                  0)))
1114           3040851270 1664281 -1340106197)))
1115
1116 ;;; MISC.249
1117 (assert (zerop
1118          (funcall
1119           (compile
1120            nil
1121            '(lambda (a b)
1122              (declare (notinline <=))
1123              (declare (optimize (speed 2) (space 3) (safety 0)
1124                        (debug 1) (compilation-speed 3)))
1125              (if (if (<= 0) nil nil)
1126                  (labels ((%f9 (f9-1 f9-2 f9-3)
1127                             (ignore-errors 0)))
1128                    (dotimes (iv4 5 a) (%f9 0 0 b)))
1129                  0)))
1130           1 2)))
1131
1132 ;;; MISC.259-264 (aka "CSR screwed up implementing *-MOD32")
1133 (assert
1134  (= (funcall
1135      (compile
1136       nil
1137       '(lambda (a)
1138          (declare (type (integer 177547470 226026978) a))
1139          (declare (optimize (speed 3) (space 0) (safety 0) (debug 0)
1140                             (compilation-speed 1)))
1141          (logand a (* a 438810))))
1142      215067723)
1143     13739018))
1144
1145 \f
1146 ;;;; Bugs in stack analysis
1147 ;;; bug 299 (reported by PFD)
1148 (assert
1149  (equal (funcall
1150          (compile
1151           nil
1152           '(lambda ()
1153             (declare (optimize (debug 1)))
1154             (multiple-value-call #'list
1155               (if (eval t) (eval '(values :a :b :c)) nil)
1156               (catch 'foo (throw 'foo (values :x :y)))))))
1157         '(:a :b :c :x :y)))
1158 ;;; bug 298 (= MISC.183)
1159 (assert (zerop (funcall
1160                 (compile
1161                  nil
1162                  '(lambda (a b c)
1163                    (declare (type (integer -368154 377964) a))
1164                    (declare (type (integer 5044 14959) b))
1165                    (declare (type (integer -184859815 -8066427) c))
1166                    (declare (ignorable a b c))
1167                    (declare (optimize (speed 3)))
1168                    (declare (optimize (safety 1)))
1169                    (declare (optimize (debug 1)))
1170                    (block b7
1171                      (flet ((%f3 (f3-1 f3-2 f3-3) 0))
1172                        (apply #'%f3 0 (catch 'foo (return-from b7 (%f3 0 b c))) c nil)))))
1173                 0 6000 -9000000)))
1174 (assert (equal (eval '(let () (apply #'list 1 (list (catch 'a (throw 'a (block b 2)))))))
1175                '(1 2)))
1176 (let ((f (compile
1177           nil
1178           '(lambda (x)
1179             (block foo
1180               (multiple-value-call #'list
1181                 :a
1182                 (block bar
1183                   (return-from foo
1184                     (multiple-value-call #'list
1185                       :b
1186                       (block quux
1187                         (return-from bar
1188                           (catch 'baz
1189                             (if x
1190                                 (return-from quux 1)
1191                                 (throw 'baz 2))))))))))))))
1192   (assert (equal (funcall f t) '(:b 1)))
1193   (assert (equal (funcall f nil) '(:a 2))))
1194
1195 ;;; MISC.185
1196 (assert (equal
1197          (funcall
1198           (compile
1199            nil
1200            '(lambda (a b c)
1201              (declare (type (integer 5 155656586618) a))
1202              (declare (type (integer -15492 196529) b))
1203              (declare (type (integer 7 10) c))
1204              (declare (optimize (speed 3)))
1205              (declare (optimize (safety 1)))
1206              (declare (optimize (debug 1)))
1207              (flet ((%f3
1208                         (f3-1 f3-2 f3-3
1209                               &optional (f3-4 a) (f3-5 0)
1210                               (f3-6
1211                                (labels ((%f10 (f10-1 f10-2 f10-3)
1212                                           0))
1213                                  (apply #'%f10
1214                                         0
1215                                         a
1216                                         (- (if (equal a b) b (%f10 c a 0))
1217                                            (catch 'ct2 (throw 'ct2 c)))
1218                                         nil))))
1219                       0))
1220                (%f3 (%f3 (%f3 b 0 0 0) a 0) a b b b c)))) 5 0 7)
1221          0))
1222 ;;; MISC.186
1223 (assert (eq
1224          (eval
1225           '(let* ((form '(labels ((%f3 (f3-1 f3-2) f3-1))
1226                           (apply #'%f3 b (catch 'ct8 (throw 'ct8 (logeqv (%f3 c 0)))) nil)))
1227                   (vars '(b c))
1228                   (fn1 `(lambda ,vars
1229                           (declare (type (integer -2 19) b)
1230                                    (type (integer -1520 218978) c)
1231                                    (optimize (speed 3) (safety 1) (debug 1)))
1232                           ,form))
1233                   (fn2 `(lambda ,vars
1234                           (declare (notinline logeqv apply)
1235                                    (optimize (safety 3) (speed 0) (debug 0)))
1236                           ,form))
1237                   (cf1 (compile nil fn1))
1238                   (cf2 (compile nil fn2))
1239                   (result1 (multiple-value-list (funcall cf1 2 18886)))
1240                   (result2 (multiple-value-list (funcall cf2 2 18886))))
1241             (if (equal result1 result2)
1242                 :good
1243                 (values result1 result2))))
1244          :good))
1245
1246 ;;; MISC.290
1247 (assert (zerop
1248          (funcall
1249           (compile
1250            nil
1251            '(lambda ()
1252              (declare
1253               (optimize (speed 3) (space 3) (safety 1)
1254                (debug 2) (compilation-speed 0)))
1255              (apply (constantly 0) (catch 'ct2 0) 0 (catch 'ct2 0) nil))))))
1256
1257 ;;; MISC.292
1258 (assert (zerop (funcall
1259                 (compile
1260                  nil
1261                  '(lambda (a b)
1262                    (declare (optimize (speed 2) (space 0) (safety 3) (debug 1)
1263                              (compilation-speed 2)))
1264                    (apply (constantly 0)
1265                     a
1266                     0
1267                     (catch 'ct6
1268                       (apply (constantly 0)
1269                              0
1270                              0
1271                              (let* ((v1
1272                                      (let ((*s7* 0))
1273                                        b)))
1274                                0)
1275                              0
1276                              nil))
1277                     0
1278                     nil)))
1279                 1 2)))
1280
1281 ;;; misc.295
1282 (assert (eql
1283          (funcall
1284           (compile
1285            nil
1286            '(lambda ()
1287              (declare (optimize (speed 1) (space 0) (safety 0) (debug 0)))
1288              (multiple-value-prog1
1289                  (the integer (catch 'ct8 (catch 'ct7 15867134)))
1290                (catch 'ct1 (throw 'ct1 0))))))
1291          15867134))
1292
1293 ;;; misc.361: replacing CAST with (m-v-call #'%compile-time-type-error)
1294 ;;; could transform known-values LVAR to UVL
1295 (assert (zerop (funcall
1296    (compile
1297     nil
1298     '(lambda (a b c)
1299        (declare (notinline boole values denominator list))
1300        (declare
1301         (optimize (speed 2)
1302                   (space 0)
1303                   (safety 1)
1304                   (debug 0)
1305                   (compilation-speed 2)))
1306        (catch 'ct6
1307          (progv
1308              '(*s8*)
1309              (list 0)
1310            (let ((v9 (ignore-errors (throw 'ct6 0))))
1311              (denominator
1312               (progv nil nil (values (boole boole-and 0 v9)))))))))
1313    1 2 3)))
1314
1315 ;;; non-continuous dead UVL blocks
1316 (defun non-continuous-stack-test (x)
1317   (multiple-value-call #'list
1318     (eval '(values 11 12))
1319     (eval '(values 13 14))
1320     (block ext
1321       (return-from non-continuous-stack-test
1322         (multiple-value-call #'list
1323           (eval '(values :b1 :b2))
1324           (eval '(values :b3 :b4))
1325           (block int
1326             (return-from ext
1327               (multiple-value-call (eval #'values)
1328                 (eval '(values 1 2))
1329                 (eval '(values 3 4))
1330                 (block ext
1331                   (return-from int
1332                     (multiple-value-call (eval #'values)
1333                       (eval '(values :a1 :a2))
1334                       (eval '(values :a3 :a4))
1335                       (block int
1336                         (return-from ext
1337                           (multiple-value-call (eval #'values)
1338                             (eval '(values 5 6))
1339                             (eval '(values 7 8))
1340                             (if x
1341                                 :ext
1342                                 (return-from int :int))))))))))))))))
1343 (assert (equal (non-continuous-stack-test t) '(11 12 13 14 1 2 3 4 5 6 7 8 :ext)))
1344 (assert (equal (non-continuous-stack-test nil) '(:b1 :b2 :b3 :b4 :a1 :a2 :a3 :a4 :int)))
1345
1346 ;;; MISC.362: environment of UNWIND-PROTECTor is different from that
1347 ;;; if ENTRY.
1348 (assert (equal (multiple-value-list (funcall
1349    (compile
1350     nil
1351     '(lambda (b g h)
1352        (declare (optimize (speed 3) (space 3) (safety 2)
1353                           (debug 2) (compilation-speed 3)))
1354        (catch 'ct5
1355          (unwind-protect
1356              (labels ((%f15 (f15-1 f15-2 f15-3)
1357                             (rational (throw 'ct5 0))))
1358                (%f15 0
1359                      (apply #'%f15
1360                             0
1361                             h
1362                             (progn
1363                               (progv '(*s2* *s5*) (list 0 (%f15 0 g 0)) b)
1364                               0)
1365                             nil)
1366                      0))
1367            (common-lisp:handler-case 0)))))
1368    1 2 3))
1369  '(0)))
1370
1371 \f
1372 ;;; MISC.275
1373 (assert
1374  (zerop
1375   (funcall
1376    (compile
1377     nil
1378     '(lambda (b)
1379       (declare (notinline funcall min coerce))
1380       (declare
1381        (optimize (speed 1)
1382         (space 2)
1383         (safety 2)
1384         (debug 1)
1385         (compilation-speed 1)))
1386       (flet ((%f12 (f12-1)
1387                (coerce
1388                 (min
1389                  (if f12-1 (multiple-value-prog1
1390                                b (return-from %f12 0))
1391                      0))
1392                 'integer)))
1393         (funcall #'%f12 0))))
1394    -33)))
1395
1396 ;;; Discussion of a CMUCL PCL bug on Sparc with Raymond Toy revealed a
1397 ;;; potential problem: optimizers and type derivers for MAX and MIN
1398 ;;; were not consistent in treating EQUALP, but not EQL, arguments.
1399 (dolist (f '(min max))
1400   (loop for complex-arg-args in '((1d0 2d0) (0d0 1d0))
1401         for complex-arg = `(if x ,@complex-arg-args)
1402         do
1403         (loop for args in `((1 ,complex-arg)
1404                             (,complex-arg 1))
1405               for form = `(,f ,@args)
1406               for f1 = (compile nil `(lambda (x) ,form))
1407               and f2 = (compile nil `(lambda (x) (declare (notinline min max))
1408                                              ,form))
1409               do
1410               (dolist (x '(nil t))
1411                 (assert (eql (funcall f1 x) (funcall f2 x)))))))
1412
1413 ;;;
1414 (handler-case (compile nil '(lambda (x)
1415                              (declare (optimize (speed 3) (safety 0)))
1416                              (the double-float (sqrt (the double-float x)))))
1417   (sb-ext:compiler-note (c)
1418     ;; Ignore the note for the float -> pointer conversion of the
1419     ;; return value.
1420     (unless (string= (car (last (sb-c::simple-condition-format-arguments c)))
1421                      "<return value>")
1422       (error "Compiler does not trust result type assertion."))))
1423
1424 (let ((f (compile nil '(lambda (x)
1425                         (declare (optimize speed (safety 0)))
1426                         (block nil
1427                           (the double-float
1428                             (multiple-value-prog1
1429                                 (sqrt (the double-float x))
1430                               (when (< x 0)
1431                                 (return :minus)))))))))
1432   (assert (eql (funcall f -1d0) :minus))
1433   (assert (eql (funcall f 4d0) 2d0)))
1434
1435 ;;; bug 304: SBCL produced something similar to (/ (ASH x 4) 8)
1436 (handler-case
1437     (compile nil '(lambda (a i)
1438                    (locally
1439                      (declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
1440                                         (inhibit-warnings 0)))
1441                      (declare (type (alien (* (unsigned 8))) a)
1442                               (type (unsigned-byte 32) i))
1443                      (deref a i))))
1444   (compiler-note () (error "The code is not optimized.")))
1445
1446 (handler-case
1447     (compile nil '(lambda (x)
1448                    (declare (type (integer -100 100) x))
1449                    (declare (optimize speed))
1450                    (declare (notinline identity))
1451                    (1+ (identity x))))
1452   (compiler-note () (error "IDENTITY derive-type not applied.")))
1453
1454 (assert (null (funcall (compile nil '(lambda (x) (funcall #'cddr x))) nil)))
1455
1456 ;;; MISC.293 = easy variant of bug 303: repeated write to the same
1457 ;;; LVAR; here the first write may be cleared before the second is
1458 ;;; made.
1459 (assert
1460  (zerop
1461   (funcall
1462    (compile
1463     nil
1464     '(lambda ()
1465       (declare (notinline complex))
1466       (declare (optimize (speed 1) (space 0) (safety 1)
1467                 (debug 3) (compilation-speed 3)))
1468       (flet ((%f () (multiple-value-prog1 0 (return-from %f 0))))
1469         (complex (%f) 0)))))))
1470
1471 ;;; MISC.110A: CAST optimizer forgot to flush LVAR derived type
1472 (assert (zerop (funcall
1473   (compile
1474    nil
1475    '(lambda (a c)
1476      (declare (type (integer -1294746569 1640996137) a))
1477      (declare (type (integer -807801310 3) c))
1478      (declare (optimize (speed 3) (space 3) (safety 0) (debug 0) (compilation-speed 3)))
1479      (catch 'ct7
1480        (if
1481         (logbitp 0
1482                  (if (/= 0 a)
1483                      c
1484                      (ignore-errors
1485                        (progn (if (ldb-test (byte 0 0) (rational (throw 'ct7 0))) 0 0) 0))))
1486         0 0))))
1487    391833530 -32785211)))
1488
1489 ;;; efficiency notes for ordinary code
1490 (macrolet ((frob (arglist &body body)
1491              `(progn
1492                (handler-case
1493                    (compile nil '(lambda ,arglist ,@body))
1494                  (sb-ext:compiler-note (e)
1495                    (error "bad compiler note for ~S:~%  ~A" ',body e)))
1496                (catch :got-note
1497                  (handler-case
1498                      (compile nil '(lambda ,arglist (declare (optimize speed))
1499                                     ,@body))
1500                    (sb-ext:compiler-note (e) (throw :got-note nil)))
1501                  (error "missing compiler note for ~S" ',body)))))
1502   (frob (x) (funcall x))
1503   (frob (x y) (find x y))
1504   (frob (x y) (find-if x y))
1505   (frob (x y) (find-if-not x y))
1506   (frob (x y) (position x y))
1507   (frob (x y) (position-if x y))
1508   (frob (x y) (position-if-not x y))
1509   (frob (x) (aref x 0)))
1510
1511 (macrolet ((frob (style-warn-p form)
1512              (if style-warn-p
1513                  `(catch :got-style-warning
1514                    (handler-case
1515                        (eval ',form)
1516                      (style-warning (e) (throw :got-style-warning nil)))
1517                    (error "missing style-warning for ~S" ',form))
1518                  `(handler-case
1519                    (eval ',form)
1520                    (style-warning (e)
1521                     (error "bad style-warning for ~S: ~A" ',form e))))))
1522   (frob t (lambda (x &optional y &key z) (list x y z)))
1523   (frob nil (lambda (x &optional y z) (list x y z)))
1524   (frob nil (lambda (x &key y z) (list x y z)))
1525   (frob t (defgeneric #:foo (x &optional y &key z)))
1526   (frob nil (defgeneric #:foo (x &optional y z)))
1527   (frob nil (defgeneric #:foo (x &key y z)))
1528   (frob t (defun #:foo (x) (flet ((foo (x &optional y &key z) (list x y z))) (foo x x :z x)))))
1529
1530 ;;; this was a bug in the LOGXOR type deriver.  The top form gave a
1531 ;;; note, because the system failed to derive the fact that the return
1532 ;;; from LOGXOR was small and negative, though the bottom one worked.
1533 (handler-bind ((sb-ext:compiler-note #'error))
1534   (compile nil '(lambda ()
1535                  (declare (optimize speed (safety 0)))
1536                  (lambda (x y)
1537                    (declare (type (integer 3 6) x)
1538                             (type (integer -6 -3) y))
1539                    (+ (logxor x y) most-positive-fixnum)))))
1540 (handler-bind ((sb-ext:compiler-note #'error))
1541   (compile nil '(lambda ()
1542                  (declare (optimize speed (safety 0)))
1543                  (lambda (x y)
1544                    (declare (type (integer 3 6) y)
1545                             (type (integer -6 -3) x))
1546                    (+ (logxor x y) most-positive-fixnum)))))
1547
1548 ;;; check that modular ash gives the right answer, to protect against
1549 ;;; possible misunderstandings about the hardware shift instruction.
1550 (assert (zerop (funcall
1551                 (compile nil '(lambda (x y)
1552                                (declare (optimize speed)
1553                                         (type (unsigned-byte 32) x y))
1554                                (logand #xffffffff (ash x y))))
1555                 1 257)))
1556
1557 ;;; code instrumenting problems
1558 (compile nil
1559   '(lambda ()
1560     (declare (optimize (debug 3)))
1561     (list (the integer (if nil 14 t)))))
1562
1563 (compile nil
1564   '(LAMBDA (A B C D)
1565     (DECLARE (NOTINLINE LOGORC1 BYTE MASK-FIELD))
1566     (DECLARE
1567      (OPTIMIZE (SPEED 1)
1568       (SPACE 1)
1569       (SAFETY 1)
1570       (DEBUG 3)
1571       (COMPILATION-SPEED 0)))
1572     (MASK-FIELD (BYTE 7 26)
1573      (PROGN
1574        (TAGBODY (THE INTEGER (CATCH 'CT4 (LOGORC1 C -15950))) 1)
1575        B))))
1576
1577 (compile nil
1578   '(lambda (buffer i end)
1579     (declare (optimize (debug 3)))
1580     (loop (when (not (eql 0 end)) (return)))
1581     (let ((s (make-string end)))
1582       (setf (schar s i) (schar buffer i))
1583       s)))
1584
1585 ;;; check that constant string prefix and suffix don't cause the
1586 ;;; compiler to emit code deletion notes.
1587 (handler-bind ((sb-ext:code-deletion-note #'error))
1588   (compile nil '(lambda (s x)
1589                  (pprint-logical-block (s x :prefix "(")
1590                    (print x s))))
1591   (compile nil '(lambda (s x)
1592                  (pprint-logical-block (s x :per-line-prefix ";")
1593                    (print x s))))
1594   (compile nil '(lambda (s x)
1595                  (pprint-logical-block (s x :suffix ">")
1596                    (print x s)))))
1597
1598 ;;; MISC.427: loop analysis requires complete DFO structure
1599 (assert (eql 17 (funcall
1600   (compile
1601    nil
1602    '(lambda (a)
1603      (declare (notinline list reduce logior))
1604      (declare (optimize (safety 2) (compilation-speed 1)
1605                (speed 3) (space 2) (debug 2)))
1606      (logior
1607       (let* ((v5 (reduce #'+ (list 0 a))))
1608         (declare (dynamic-extent v5))
1609         v5))))
1610     17)))
1611
1612 ;;;  MISC.434
1613 (assert (zerop (funcall
1614    (compile
1615     nil
1616     '(lambda (a b)
1617        (declare (type (integer -8431780939320 1571817471932) a))
1618        (declare (type (integer -4085 0) b))
1619        (declare (ignorable a b))
1620        (declare
1621         (optimize (space 2)
1622                   (compilation-speed 0)
1623                   #+sbcl (sb-c:insert-step-conditions 0)
1624                   (debug 2)
1625                   (safety 0)
1626                   (speed 3)))
1627        (let ((*s5* 0))
1628          (dotimes (iv1 2 0)
1629            (let ((*s5*
1630                   (elt '(1954479092053)
1631                        (min 0
1632                             (max 0
1633                                  (if (< iv1 iv1)
1634                                      (lognand iv1 (ash iv1 (min 53 iv1)))
1635                                    iv1))))))
1636              0)))))
1637    -7639589303599 -1368)))
1638
1639 (compile
1640  nil
1641  '(lambda (a b)
1642    (declare (type (integer) a))
1643    (declare (type (integer) b))
1644    (declare (ignorable a b))
1645    (declare (optimize (space 2) (compilation-speed 0)
1646              (debug 0) (safety 0) (speed 3)))
1647    (dotimes (iv1 2 0)
1648      (when (< iv1 2) (print 'x)) ;; request for second constraint propagation pass
1649      (print (if (< iv1 iv1)
1650                 (logand (ash iv1 iv1) 1)
1651                 iv1)))))
1652
1653 ;;; MISC.435: lambda var substitution in a deleted code.
1654 (assert (zerop (funcall
1655    (compile
1656     nil
1657     '(lambda (a b c d)
1658        (declare (notinline aref logandc2 gcd make-array))
1659        (declare
1660         (optimize (space 0) (safety 0) (compilation-speed 3)
1661                   (speed 3) (debug 1)))
1662        (progn
1663          (tagbody
1664           (let* ((v2 (make-array nil :initial-element (catch 'ct1 (go tag2)))))
1665             (declare (dynamic-extent v2))
1666             (gcd (go tag2) (logandc2 (catch 'ct2 c) (aref v2))))
1667           tag2)
1668          0)))
1669    3021871717588 -866608 -2 -17194)))
1670
1671 ;;; MISC.436, 438: lost reoptimization
1672 (assert (zerop (funcall
1673    (compile
1674     nil
1675     '(lambda (a b)
1676        (declare (type (integer -2917822 2783884) a))
1677        (declare (type (integer 0 160159) b))
1678        (declare (ignorable a b))
1679        (declare
1680         (optimize (compilation-speed 1)
1681                   (speed 3)
1682                   (safety 3)
1683                   (space 0)
1684                   ; #+sbcl (sb-c:insert-step-conditions 0)
1685                   (debug 0)))
1686        (if
1687            (oddp
1688             (loop for
1689                   lv1
1690                   below
1691                   2
1692                   count
1693                   (logbitp 0
1694                            (1-
1695                             (ash b
1696                                  (min 8
1697                                       (count 0
1698                                              '(-10197561 486 430631291
1699                                                          9674068))))))))
1700            b
1701          0)))
1702    1265797 110757)))
1703
1704 (assert (zerop (funcall
1705    (compile
1706     nil
1707     ' (lambda (a)
1708         (declare (type (integer 0 1696) a))
1709         ; (declare (ignorable a))
1710         (declare (optimize (space 2) (debug 0) (safety 1)
1711                    (compilation-speed 0) (speed 1)))
1712         (if (logbitp 0 (ash (1- a) (min 11 a))) 0 0)))
1713    805)))
1714
1715 ;;; bug #302
1716 (assert (compile
1717          nil
1718          '(lambda (s ei x y)
1719            (declare (type (simple-array function (2)) s) (type ei ei))
1720            (funcall (aref s ei) x y))))
1721
1722 ;;; MISC.320: ir1-transform can create an intercomponent reference to
1723 ;;; a DEFINED-FUN.
1724 (assert (eql 102 (funcall
1725   (compile
1726    nil
1727    '(lambda ()
1728      (declare (optimize (speed 3) (space 0) (safety 2)
1729                (debug 2) (compilation-speed 0)))
1730      (catch 'ct2
1731        (elt '(102)
1732             (flet ((%f12 () (rem 0 -43)))
1733               (multiple-value-call #'%f12 (values))))))))))
1734
1735 ;;; MISC.437: lost reoptimization after FLUSH-DEST
1736 (assert (zerop (funcall
1737   (compile
1738    nil
1739    '(lambda (a b c d e)
1740      (declare (notinline values complex eql))
1741      (declare
1742       (optimize (compilation-speed 3)
1743        (speed 3)
1744        (debug 1)
1745        (safety 1)
1746        (space 0)))
1747      (flet ((%f10
1748                 (f10-1 f10-2 f10-3
1749                        &optional (f10-4 (ignore-errors 0)) (f10-5 0)
1750                        &key &allow-other-keys)
1751               (if (or (eql 0 0) t) 0 (if f10-1 0 0))))
1752        (complex (multiple-value-call #'%f10 (values a c b 0 0)) 0))))
1753    80043 74953652306 33658947 -63099937105 -27842393)))
1754
1755 ;;; bug #351 -- program-error for malformed LET and LET*, including those
1756 ;;; resulting from SETF of LET.
1757 (dolist (fun (list (compile nil '(lambda () (let :bogus-let :oops)))
1758                    (compile nil '(lambda () (let* :bogus-let* :oops)))
1759                    (compile nil '(lambda (x) (push x (let ((y 0)) y))))))
1760   (assert (functionp fun))
1761   (multiple-value-bind (res err) (ignore-errors (funcall fun))
1762     (assert (not res))
1763     (assert (typep err 'program-error))))
1764
1765 (let ((fun (compile nil '(lambda (x) (random (if x 10 20))))))
1766   (dotimes (i 100 (error "bad RANDOM distribution"))
1767     (when (> (funcall fun nil) 9)
1768       (return t)))
1769   (dotimes (i 100)
1770     (when (> (funcall fun t) 9)
1771       (error "bad RANDOM event"))))
1772
1773 ;;; 0.8.17.28-sma.1 lost derived type information.
1774 (with-test (:name "0.8.17.28-sma.1" :fails-on :sparc)
1775   (handler-bind ((sb-ext:compiler-note (lambda (c) (error "~A" c))))
1776     (compile nil
1777       '(lambda (x y v)
1778         (declare (optimize (speed 3) (safety 0)))
1779         (declare (type (integer 0 80) x)
1780          (type (integer 0 11) y)
1781          (type (simple-array (unsigned-byte 32) (*)) v))
1782         (setf (aref v 0) (* (* x #.(floor (ash 1 32) (* 11 80))) y))
1783         nil))))
1784
1785 ;;; Bug reported by Robert J. Macomber: instrumenting of more-entry
1786 ;;; prevented open coding of %LISTIFY-REST-ARGS.
1787 (let ((f (compile nil '(lambda ()
1788                         (declare (optimize (debug 3)))
1789                         (with-simple-restart (blah "blah") (error "blah"))))))
1790   (handler-bind ((error (lambda (c) (invoke-restart 'blah))))
1791     (assert (equal (multiple-value-list (funcall f)) '(nil t)))))
1792
1793 ;;; Bug reported by Timmy Douglas: overflow in bit vector setter with
1794 ;;; constant index and value.
1795 (loop for n-bits = 1 then (* n-bits 2)
1796       for type = `(unsigned-byte ,n-bits)
1797       and v-max = (1- (ash 1 n-bits))
1798       while (<= n-bits sb-vm:n-word-bits)
1799       do
1800       (let* ((n (* 2 (1+ (- sb-vm::n-word-bits n-bits))))
1801              (array1 (make-array n :element-type type))
1802              (array2 (make-array n :element-type type)))
1803         (dotimes (i n)
1804           (dolist (v (list 0 v-max))
1805             (let ((f (compile nil `(lambda (a)
1806                                      (declare (type (simple-array ,type (,n)) a))
1807                                      (setf (aref a ,i) ,v)))))
1808               (fill array1 (- v-max v))
1809               (fill array2 (- v-max v))
1810               (funcall f array1)
1811               (setf (aref array2 i) v)
1812               (assert (every #'= array1 array2)))))))
1813
1814 (let ((fn (compile nil '(lambda (x)
1815                           (declare (type bit x))
1816                           (declare (optimize speed))
1817                           (let ((b (make-array 64 :element-type 'bit
1818                                                :initial-element 0)))
1819                             (count x b))))))
1820   (assert (= (funcall fn 0) 64))
1821   (assert (= (funcall fn 1) 0)))
1822
1823 (let ((fn (compile nil '(lambda (x y)
1824                           (declare (type simple-bit-vector x y))
1825                           (declare (optimize speed))
1826                           (equal x y)))))
1827   (assert (funcall
1828            fn
1829            (make-array 64 :element-type 'bit :initial-element 0)
1830            (make-array 64 :element-type 'bit :initial-element 0)))
1831   (assert (not
1832            (funcall
1833             fn
1834             (make-array 64 :element-type 'bit :initial-element 0)
1835             (let ((b (make-array 64 :element-type 'bit :initial-element 0)))
1836               (setf (sbit b 63) 1)
1837               b)))))
1838
1839 ;;; MISC.535: compiler failure
1840 (let ((c0 #c(4196.088977268509d0 -15943.3603515625d0)))
1841     (assert (not (funcall
1842      (compile
1843       nil
1844       `(lambda (p1 p2)
1845          (declare (optimize speed (safety 1))
1846                   (type (eql ,c0) p1)
1847                   (type number p2))
1848          (eql (the (complex double-float) p1) p2)))
1849      c0 #c(12 612/979)))))
1850
1851 ;;; reported by Lutz Euler: we shouldn't signal a compiler note for
1852 ;;; simple-bit-vector functions.
1853 (handler-bind ((sb-ext:compiler-note #'error))
1854   (compile nil '(lambda (x)
1855                  (declare (type simple-bit-vector x))
1856                  (count 1 x))))
1857 (handler-bind ((sb-ext:compiler-note #'error))
1858   (compile nil '(lambda (x y)
1859                  (declare (type simple-bit-vector x y))
1860                  (equal x y))))
1861
1862 ;;; MISC.550: CAST merging in IR1 finalization caused unexpected
1863 ;;; code transformations.
1864 (assert (eql (funcall
1865   (compile
1866    nil
1867    '(lambda (p1 p2)
1868      (declare (optimize (speed 3) (safety 2) (debug 3) (space 3))
1869       (type atom p1)
1870       (type symbol p2))
1871      (or p1 (the (eql t) p2))))
1872    nil t)
1873   t))
1874
1875 ;;; MISC.548: type check weakening converts required type into
1876 ;;; optional
1877 (assert (eql t
1878   (funcall
1879    (compile
1880     nil
1881     '(lambda (p1)
1882       (declare (optimize (speed 2) (safety 1) (debug 3) (space 2)))
1883       (atom (the (member f assoc-if write-line t w) p1))))
1884    t)))
1885
1886 ;;; Free special bindings only apply to the body of the binding form, not
1887 ;;; the initialization forms.
1888 (assert (eq :good
1889             (funcall (compile 'nil
1890                               (lambda ()
1891                                 (let ((x :bad))
1892                                   (declare (special x))
1893                                   (let ((x :good))
1894                                     ((lambda (&optional (y x))
1895                                        (declare (special x)) y)))))))))
1896
1897 ;;; Bug from pfdietz's random tester: the compiler knew that IMAGPART of
1898 ;;; a rational was zero, but didn't do the substitution, leading to a
1899 ;;; crash in the ASH vop (since a shift of 57 wouldn't fit in the
1900 ;;; machine's ASH instruction's immediate field) that the compiler
1901 ;;; thought was legitimate.
1902 ;;;
1903 ;;; FIXME: this has been recorded as bug 383.  The attempted fix (sbcl
1904 ;;; 0.9.2.6) led to lots of spurious optimization notes.  So the bug stil
1905 ;;; exist and this test case serves as a reminder of the problem.
1906 ;;;   --njf, 2005-07-05
1907 #+nil
1908 (compile 'nil
1909          (LAMBDA (B)
1910            (DECLARE (TYPE (INTEGER -2 14) B))
1911            (DECLARE (IGNORABLE B))
1912            (ASH (IMAGPART B) 57)))
1913
1914 ;;; bug reported by Eduardo Mu\~noz
1915 (multiple-value-bind (fun warnings failure)
1916     (compile nil '(lambda (struct first)
1917                    (declare (optimize speed))
1918                    (let* ((nodes (nodes struct))
1919                           (bars (bars struct))
1920                           (length (length nodes))
1921                           (new (make-array length :fill-pointer 0)))
1922                      (vector-push first new)
1923                      (loop with i fixnum = 0
1924                            for newl fixnum = (length new)
1925                            while (< newl length) do
1926                            (let ((oldl (length new)))
1927                              (loop for j fixnum from i below newl do
1928                                    (dolist (n (node-neighbours (aref new j) bars))
1929                                      (unless (find n new)
1930                                        (vector-push n new))))
1931                              (setq i oldl)))
1932                      new)))
1933   (declare (ignore fun warnings failure))
1934   (assert (not failure)))
1935
1936 ;;; bug #389: "0.0 can't be converted to type NIL."  (Brian Rowe
1937 ;;; sbcl-devel)
1938 (compile nil '(lambda (x y a b c)
1939                (- y (* (signum x) (sqrt (abs (- (* b x) c)))))))
1940
1941 ;;; Type inference from CHECK-TYPE
1942 (let ((count0 0) (count1 0))
1943   (handler-bind ((sb-ext:compiler-note (lambda (c) (incf count0))))
1944     (compile nil '(lambda (x)
1945                    (declare (optimize (speed 3)))
1946                    (1+ x))))
1947   ;; forced-to-do GENERIC-+, etc, possible word -> bignum conversion note
1948   (assert (> count0 1))
1949   (handler-bind ((sb-ext:compiler-note (lambda (c) (incf count1))))
1950     (compile nil '(lambda (x)
1951                    (declare (optimize (speed 3)))
1952                    (check-type x fixnum)
1953                    (1+ x))))
1954   ;; Only the posssible word -> bignum conversion note
1955   (assert (= count1 1)))
1956
1957 ;;; Up to 0.9.8.22 x86-64 had broken return value handling in the
1958 ;;; %SET-SAP-REF-DOUBLE/SINGLE VOPs.
1959 (with-test (:name :sap-ref-float)
1960   (compile nil '(lambda (sap)
1961                  (let ((x (setf (sb-vm::sap-ref-double sap 0) 1d0)))
1962                    (1+ x))))
1963   (compile nil '(lambda (sap)
1964                  (let ((x (setf (sb-vm::sap-ref-single sap 0) 1d0)))
1965                    (1+ x)))))
1966
1967 ;;; bug #399
1968 (with-test (:name :string-union-types)
1969   (compile nil '(lambda (x)
1970                  (declare (type (or (simple-array character (6))
1971                                     (simple-array character (5))) x))
1972                  (aref x 0))))
1973
1974 ;;; MISC.623: missing functions for constant-folding
1975 (assert (eql 0
1976              (funcall
1977               (compile
1978                nil
1979                '(lambda ()
1980                  (declare (optimize (space 2) (speed 0) (debug 2)
1981                            (compilation-speed 3) (safety 0)))
1982                  (loop for lv3 below 1
1983                     count (minusp
1984                            (loop for lv2 below 2
1985                               count (logbitp 0
1986                                              (bit #*1001101001001
1987                                                   (min 12 (max 0 lv3))))))))))))
1988
1989 ;;; MISC.624: erroneous AVER in x86's %LOGBITP VOPs
1990 (assert (eql 0
1991              (funcall
1992               (compile
1993                nil
1994                '(lambda (a)
1995                  (declare (type (integer 21 28) a))
1996                  (declare       (optimize (compilation-speed 1) (safety 2)
1997                                  (speed 0) (debug 0) (space 1)))
1998                  (let* ((v7 (flet ((%f3 (f3-1 f3-2)
1999                                      (loop for lv2 below 1
2000                                         count
2001                                         (logbitp 29
2002                                                  (sbit #*10101111
2003                                                        (min 7 (max 0 (eval '0))))))))
2004                               (%f3 0 a))))
2005                    0)))
2006               22)))
2007
2008 ;;; MISC.626: bandaged AVER was still wrong
2009 (assert (eql -829253
2010              (funcall
2011               (compile
2012                nil
2013                '(lambda (a)
2014                   (declare (type (integer -902970 2) a))
2015                   (declare (optimize (space 2) (debug 0) (compilation-speed 1)
2016                                      (speed 0) (safety 3)))
2017                   (prog2 (if (logbitp 30 a) 0 (block b3 0)) a)))
2018               -829253)))
2019
2020 ;; MISC.628: constant-folding %LOGBITP was buggy
2021 (assert (eql t
2022              (funcall
2023               (compile
2024                nil
2025                '(lambda ()
2026                   (declare (optimize (safety 3) (space 3) (compilation-speed 3)
2027                                      (speed 0) (debug 1)))
2028                   (not (not (logbitp 0 (floor 2147483651 (min -23 0))))))))))
2029
2030 ;; mistyping found by random-tester
2031 (assert (zerop
2032   (funcall
2033    (compile
2034     nil
2035     '(lambda ()
2036       (declare (optimize (speed 1) (debug 0)
2037                 (space 2) (safety 0) (compilation-speed 0)))
2038       (unwind-protect 0
2039         (* (/ (multiple-value-prog1 -29457482 -5602513511) 1))))))))
2040
2041 ;; aggressive constant folding (bug #400)
2042 (assert
2043  (eq t (funcall (compile nil '(lambda () (or t (the integer (/ 1 0))))))))
2044
2045 (with-test (:name (:compiler :constraint-propagation :var-eql-to-non-var-1))
2046   (assert
2047    (handler-case
2048        (compile nil '(lambda (x y)
2049                        (when (eql x (length y))
2050                          (locally
2051                              (declare (optimize (speed 3)))
2052                            (1+ x)))))
2053      (compiler-note () (error "The code is not optimized.")))))
2054
2055 (with-test (:name (:compiler :constraint-propagation :var-eql-to-non-var-2))
2056   (assert
2057    (handler-case
2058        (compile nil '(lambda (x y)
2059                        (when (eql (length y) x)
2060                          (locally
2061                              (declare (optimize (speed 3)))
2062                            (1+ x)))))
2063      (compiler-note () (error "The code is not optimized.")))))
2064
2065 (with-test (:name (:compiler :constraint-propagation :float-bounds-1))
2066   (handler-case
2067       (compile nil '(lambda (x)
2068                       (declare (type (single-float * (3.0)) x))
2069                       (when (<= x 2.0)
2070                         (when (<= 2.0 x)
2071                           x))))
2072     (compiler-note () (error "Deleted reachable code."))))
2073
2074 (with-test (:name (:compiler :constraint-propagation :float-bounds-2))
2075   (catch :note
2076     (handler-case
2077         (compile nil '(lambda (x)
2078                         (declare (type single-float x))
2079                         (when (< 1.0 x)
2080                           (when (<= x 1.0)
2081                             (error "This is unreachable.")))))
2082       (compiler-note () (throw :note nil)))
2083     (error "Unreachable code undetected.")))
2084
2085 (with-test (:name (:compiler :constraint-propagation :var-eql-to-var-1))
2086   (catch :note
2087     (handler-case
2088         (compile nil '(lambda (x y)
2089                         (when (typep y 'fixnum)
2090                           (when (eql x y)
2091                             (unless (typep x 'fixnum)
2092                               (error "This is unreachable"))
2093                             (setq y nil)))))
2094       (compiler-note () (throw :note nil)))
2095     (error "Unreachable code undetected.")))
2096
2097 (with-test (:name (:compiler :constraint-propagation :var-eql-to-var-2))
2098   (catch :note
2099     (handler-case
2100         (compile nil '(lambda (x y)
2101                         (when (typep y 'fixnum)
2102                           (when (eql y x)
2103                             (unless (typep x 'fixnum)
2104                               (error "This is unreachable"))
2105                             (setq y nil)))))
2106       (compiler-note () (throw :note nil)))
2107     (error "Unreachable code undetected.")))
2108
2109 ;; Reported by John Wiseman, sbcl-devel
2110 ;; Subject: [Sbcl-devel] float type derivation bug?
2111 ;; Date: Tue, 4 Apr 2006 15:28:15 -0700
2112 (with-test (:name (:type-derivation :float-bounds))
2113   (compile nil '(lambda (bits)
2114                  (let* ((s (if (= (ash bits -31) 0) 1 -1))
2115                         (e (logand (ash bits -23) #xff))
2116                         (m (if (= e 0)
2117                                (ash (logand bits #x7fffff) 1)
2118                                (logior (logand bits #x7fffff) #x800000))))
2119                    (float (* s m (expt 2 (- e 150))))))))
2120
2121 ;; Reported by James Knight
2122 ;; Subject: [Sbcl-devel] AVER: "(EQ (SB-NAME (SC-SB (TN-SC TN))) 'REGISTERS)"
2123 ;; Date: Fri, 24 Mar 2006 19:30:00 -0500
2124 (with-test (:name :logbitp-vop)
2125   (compile nil
2126            '(lambda (days shift)
2127              (declare (type fixnum shift days))
2128              (let* ((result 0)
2129                     (canonicalized-shift (+ shift 1))
2130                     (first-wrapping-day (- 1 canonicalized-shift)))
2131                (declare (type fixnum result))
2132                (dotimes (source-day 7)
2133                  (declare (type (integer 0 6) source-day))
2134                  (when (logbitp source-day days)
2135                    (setf result
2136                          (logior result
2137                                  (the fixnum
2138                                    (if (< source-day first-wrapping-day)
2139                                        (+ source-day canonicalized-shift)
2140                                        (- (+ source-day
2141                                              canonicalized-shift) 7)))))))
2142                result))))
2143
2144 ;;; MISC.637: incorrect delaying of conversion of optional entries
2145 ;;; with hairy constant defaults
2146 (let ((f '(lambda ()
2147   (labels ((%f11 (f11-2 &key key1)
2148              (labels ((%f8 (f8-2 &optional (f8-5 (if nil (return-from %f11 0) 0)))
2149                         :bad1))
2150                (%f8 (%f8 0)))
2151              :bad2))
2152     :good))))
2153   (assert (eq (funcall (compile nil f)) :good)))
2154
2155 ;;; MISC.555: new reference to an already-optimized local function
2156 (let* ((l '(lambda (p1)
2157     (declare (optimize (speed 1) (safety 2) (debug 2) (space 0)) (type keyword p1))
2158     (keywordp p1)))
2159        (f (compile nil l)))
2160   (assert (funcall f :good))
2161   (assert (nth-value 1 (ignore-errors (funcall f 42)))))
2162
2163 ;;; Check that the compiler doesn't munge *RANDOM-STATE*.
2164 (let* ((state (make-random-state))
2165        (*random-state* (make-random-state state))
2166        (a (random most-positive-fixnum)))
2167   (setf *random-state* state)
2168   (compile nil `(lambda (x a)
2169                   (declare (single-float x)
2170                            (type (simple-array double-float) a))
2171                   (+ (loop for i across a
2172                            summing i)
2173                      x)))
2174   (assert (= a (random most-positive-fixnum))))
2175
2176 ;;; MISC.641: LET-conversion after physical environment analysis lost NLX-INFOs
2177 (let ((form '(lambda ()
2178               (declare (optimize (speed 1) (space 0) (debug 2)
2179                            (compilation-speed 0) (safety 1)))
2180               (flet ((%f3 (f3-1 &key (key1 (count (floor 0 (min -74 0)) #())))
2181                           0))
2182                    (apply #'%f3 0 nil)))))
2183   (assert (zerop (funcall (compile nil form)))))
2184
2185 ;;;  size mismatch: #<SB-VM::EA :DWORD base=#<SB-C:TN t1[RDX]> disp=1> is a :DWORD and #<SB-C:TN t2[RAX]> is a :QWORD. on x86-64
2186 (compile nil '(lambda ()
2187                (let ((x (make-array '(1) :element-type '(signed-byte 32))))
2188                  (setf (aref x 0) 1))))
2189
2190 ;;; step instrumentation confusing the compiler, reported by Faré
2191 (handler-bind ((warning #'error))
2192   (compile nil '(lambda ()
2193                  (declare (optimize (debug 2))) ; not debug 3!
2194                  (let ((val "foobar"))
2195                    (map-into (make-array (list (length val))
2196                                          :element-type '(unsigned-byte 8))
2197                              #'char-code val)))))
2198
2199 ;;; overconfident primitive type computation leading to bogus type
2200 ;;; checking.
2201 (let* ((form1 '(lambda (x)
2202                 (declare (type (and condition function) x))
2203                 x))
2204        (fun1 (compile nil form1))
2205        (form2 '(lambda (x)
2206                 (declare (type (and standard-object function) x))
2207                 x))
2208        (fun2 (compile nil form2)))
2209   (assert (raises-error? (funcall fun1 (make-condition 'error))))
2210   (assert (raises-error? (funcall fun1 fun1)))
2211   (assert (raises-error? (funcall fun2 fun2)))
2212   (assert (eq (funcall fun2 #'print-object) #'print-object)))
2213
2214 ;;; LET* + VALUES declaration: while the declaration is a non-standard
2215 ;;; and possibly a non-conforming extension, as long as we do support
2216 ;;; it, we might as well get it right.
2217 ;;;
2218 ;;; Bug reported by Kaersten Poeck on sbcl-devel 20061023.
2219 (compile nil '(lambda () (let* () (declare (values list)))))
2220
2221
2222 ;;; test for some problems with too large immediates in x86-64 modular
2223 ;;; arithmetic vops
2224 (compile nil '(lambda (x) (declare (fixnum x))
2225                (logand most-positive-fixnum (logxor x most-positive-fixnum))))
2226
2227 (compile nil '(lambda (x) (declare (fixnum x))
2228                (logand most-positive-fixnum (+ x most-positive-fixnum))))
2229
2230 (compile nil '(lambda (x) (declare (fixnum x))
2231                (logand most-positive-fixnum (* x most-positive-fixnum))))
2232
2233 ;;; bug 256.b
2234 (with-test (:name :propagate-type-through-error-and-binding)
2235   (assert (let (warned-p)
2236             (handler-bind ((warning (lambda (w) (setf warned-p t))))
2237               (compile nil
2238                        '(lambda (x)
2239                          (list (let ((y (the real x)))
2240                                  (unless (floatp y) (error ""))
2241                                  y)
2242                           (integer-length x)))))
2243             warned-p)))
2244
2245 ;; Dead / in safe code
2246 (with-test (:name :safe-dead-/)
2247   (assert (eq :error
2248               (handler-case
2249                   (funcall (compile nil
2250                                     '(lambda (x y)
2251                                       (declare (optimize (safety 3)))
2252                                       (/ x y)
2253                                       (+ x y)))
2254                            1
2255                            0)
2256                 (division-by-zero ()
2257                   :error)))))
2258
2259 ;;; Dead unbound variable (bug 412)
2260 (with-test (:name :dead-unbound)
2261   (assert (eq :error
2262               (handler-case
2263                   (funcall (compile nil
2264                                     '(lambda ()
2265                                       #:unbound
2266                                       42)))
2267                 (unbound-variable ()
2268                   :error)))))
2269
2270 ;;; No compiler notes from compiling SUBSEQ SIMPLE-VECTOR.
2271 (handler-bind ((sb-ext:compiler-note 'error))
2272   (assert
2273    (equalp #(2 3)
2274            (funcall (compile nil `(lambda (s p e)
2275                                     (declare (optimize speed)
2276                                              (simple-vector s))
2277                                     (subseq s p e)))
2278                     (vector 1 2 3 4)
2279                     1
2280                     3))))
2281
2282 ;;; No compiler notes from compiling COPY-SEQ SIMPLE-VECTOR.
2283 (handler-bind ((sb-ext:compiler-note 'error))
2284   (assert
2285    (equalp #(1 2 3 4)
2286            (funcall (compile nil `(lambda (s)
2287                                     (declare (optimize speed)
2288                                              (simple-vector s))
2289                                     (copy-seq s)))
2290                     (vector 1 2 3 4)))))
2291
2292 ;;; bug in adding DATA-VECTOR-REF-WITH-OFFSET to x86-64
2293 (assert (not (mismatch #(1.0f0 2.0f0) (make-array 2 :element-type 'single-float :initial-contents (list 1.0f0 2.0f0)))))
2294
2295 ;;; bug in interval-arithmetic used by the compiler: needless attempt to coerce too
2296 ;;; large bignums to floats
2297 (dolist (op '(* / + -))
2298   (let ((fun (compile
2299               nil
2300               `(lambda (x)
2301                  (declare (type (integer 0 #.(* 2 (truncate most-positive-double-float))) x))
2302                  (,op 0.0d0 x)))))
2303     (loop repeat 10
2304           do (let ((arg (random (truncate most-positive-double-float))))
2305                (assert (eql (funcall fun arg)
2306                             (funcall op 0.0d0 arg)))))))
2307
2308 (with-test (:name :high-debug-known-function-inlining)
2309   (let ((fun (compile nil
2310                       '(lambda ()
2311                         (declare (optimize (debug 3)) (inline append))
2312                         (let ((fun (lambda (body)
2313                                      (append
2314                                       (first body)
2315                                       nil))))
2316                           (funcall fun
2317                                    '((foo (bar)))))))))
2318     (funcall fun)))
2319
2320 (with-test (:name :high-debug-known-function-transform-with-optional-arguments)
2321   (compile nil '(lambda (x y)
2322                (declare (optimize sb-c::preserve-single-use-debug-variables))
2323                (if (block nil
2324                      (some-unknown-function
2325                       (lambda ()
2326                         (return (member x y))))
2327                      t)
2328                    t
2329                    (error "~a" y)))))
2330
2331 ;;; Compiling W-P-O when the pinned objects are known to be fixnums
2332 ;;; or characters.
2333 (compile nil '(lambda (x y)
2334                (declare (fixnum y) (character x))
2335                (sb-sys:with-pinned-objects (x y)
2336                  (some-random-function))))
2337
2338 ;;; *CHECK-CONSISTENCY* and TRULY-THE
2339
2340 (with-test (:name :bug-423)
2341   (let ((sb-c::*check-consistency* t))
2342     (handler-bind ((warning #'error))
2343       (flet ((make-lambda (type)
2344                `(lambda (x)
2345                   ((lambda (z)
2346                      (if (listp z)
2347                          (let ((q (truly-the list z)))
2348                            (length q))
2349                          (if (arrayp z)
2350                              (let ((q (truly-the vector z)))
2351                                (length q))
2352                              (error "oops"))))
2353                    (the ,type x)))))
2354         (compile nil (make-lambda 'list))
2355         (compile nil (make-lambda 'vector))))))
2356
2357 ;;; this caused a momentary regression when an ill-adviced fix to
2358 ;;; bug 427 made ANY-REG suitable for primitive-type T:
2359 ;;;
2360 ;;; no :MOVE-ARG VOP defined to move #<SB-C:TN t1> (SC SB-VM::SINGLE-REG) to #<SB-C:TN t2> (SC SB-VM::ANY-REG)
2361 ;;;    [Condition of type SIMPLE-ERROR]
2362 (compile nil
2363          '(lambda (frob)
2364            (labels
2365                ((%zig (frob)
2366                   (typecase frob
2367                     (double-float
2368                      (setf (sb-alien:deref (sb-alien:cast (sb-alien:sap-alien (unknown1) (* unsigned-char))
2369                                                           (* double-float))) frob))
2370                     (hash-table
2371                      (%zig (the (values (single-float (0.0) 1.0) &optional) (unknown2)))
2372                      nil))))
2373              (%zig))))
2374
2375 ;;; non-required arguments in HANDLER-BIND
2376 (assert (eq :oops (car (funcall (compile nil
2377                                          '(lambda (x)
2378                                            (block nil
2379                                              (handler-bind ((error (lambda (&rest args) (return (cons :oops args)))))
2380                                                (/ 2 x)))))
2381                                 0))))
2382
2383 ;;; NIL is a legal function name
2384 (assert (eq 'a (flet ((nil () 'a)) (nil))))
2385
2386 ;;; misc.528
2387 (assert (null (let* ((x 296.3066f0)
2388                      (y 22717067)
2389                      (form `(lambda (r p2)
2390                               (declare (optimize speed (safety 1))
2391                                        (type (simple-array single-float nil) r)
2392                                        (type (integer -9369756340 22717335) p2))
2393                               (setf (aref r) (* ,x (the (eql 22717067) p2)))
2394                            (values)))
2395                      (r (make-array nil :element-type 'single-float))
2396                      (expected (* x y)))
2397                 (funcall (compile nil form) r y)
2398                 (let ((actual (aref r)))
2399                   (unless (eql expected actual)
2400                     (list expected actual))))))
2401 ;;; misc.529
2402 (assert (null (let* ((x -2367.3296f0)
2403                      (y 46790178)
2404                      (form `(lambda (r p2)
2405                               (declare (optimize speed (safety 1))
2406                                        (type (simple-array single-float nil) r)
2407                                        (type (eql 46790178) p2))
2408                               (setf (aref r) (+ ,x (the (integer 45893897) p2)))
2409                               (values)))
2410                      (r (make-array nil :element-type 'single-float))
2411                      (expected (+ x y)))
2412                 (funcall (compile nil form) r y)
2413                 (let ((actual (aref r)))
2414                   (unless (eql expected actual)
2415                     (list expected actual))))))
2416
2417 ;;; misc.556
2418 (assert (eql -1
2419              (funcall
2420               (compile nil '(lambda (p1 p2)
2421                              (declare
2422                               (optimize (speed 1) (safety 0)
2423                                (debug 0) (space 0))
2424                               (type (member 8174.8604) p1)
2425                               (type (member -95195347) p2))
2426                              (floor p1 p2)))
2427               8174.8604 -95195347)))
2428
2429 ;;; misc.557
2430 (assert (eql -1
2431              (funcall
2432               (compile
2433                nil
2434                '(lambda (p1)
2435                  (declare (optimize (speed 3) (safety 0) (debug 3) (space 1))
2436                   (type (member -94430.086f0) p1))
2437                  (floor (the single-float p1) 19311235)))
2438               -94430.086f0)))
2439
2440 ;;; misc.558
2441 (assert (eql -1.0f0
2442              (funcall
2443               (compile
2444                nil
2445                '(lambda (p1)
2446                  (declare (optimize (speed 1) (safety 2)
2447                            (debug 2) (space 3))
2448                   (type (eql -39466.56f0) p1))
2449                  (ffloor p1 305598613)))
2450               -39466.56f0)))
2451
2452 ;;; misc.559
2453 (assert (eql 1
2454              (funcall
2455               (compile
2456                nil
2457                '(lambda (p1)
2458                  (declare (optimize (speed 1) (safety 1) (debug 1) (space 2))
2459                   (type (eql -83232.09f0) p1))
2460                  (ceiling p1 -83381228)))
2461               -83232.09f0)))
2462
2463 ;;; misc.560
2464 (assert (eql 1
2465              (funcall
2466               (compile
2467                nil
2468                '(lambda (p1)
2469                  (declare (optimize (speed 1) (safety 1)
2470                            (debug 1) (space 0))
2471                   (type (member -66414.414f0) p1))
2472                  (ceiling p1 -63019173f0)))
2473               -66414.414f0)))
2474
2475 ;;; misc.561
2476 (assert (eql 1.0f0
2477              (funcall
2478               (compile
2479                nil
2480                '(lambda (p1)
2481                  (declare (optimize (speed 0) (safety 1)
2482                            (debug 0) (space 1))
2483                   (type (eql 20851.398f0) p1))
2484                  (fceiling p1 80839863)))
2485               20851.398f0)))
2486
2487 ;;; misc.581
2488 (assert (floatp
2489          (funcall
2490           (compile nil '(lambda (x)
2491                          (declare (type (eql -5067.2056) x))
2492                          (+ 213734822 x)))
2493           -5067.2056)))
2494
2495 ;;; misc.581a
2496 (assert (typep
2497          (funcall
2498           (compile nil '(lambda (x) (declare (type (eql -1.0) x))
2499                          (+ #x1000001 x)))
2500           -1.0f0)
2501          'single-float))
2502
2503 ;;; misc.582
2504 (assert (plusp (funcall
2505                 (compile
2506                  nil
2507                  ' (lambda (p1)
2508                      (declare (optimize (speed 0) (safety 1) (debug 1) (space 1))
2509                               (type (eql -39887.645) p1))
2510                      (mod p1 382352925)))
2511               -39887.645)))
2512
2513 ;;; misc.587
2514 (assert (let ((result (funcall
2515                        (compile
2516                         nil
2517                         '(lambda (p2)
2518                           (declare (optimize (speed 0) (safety 3) (debug 1) (space 0))
2519                            (type (eql 33558541) p2))
2520                           (- 92215.266 p2)))
2521                        33558541)))
2522           (typep result 'single-float)))
2523
2524 ;;; misc.635
2525 (assert (eql 1
2526              (let* ((form '(lambda (p2)
2527                             (declare (optimize (speed 0) (safety 1)
2528                                       (debug 2) (space 2))
2529                              (type (member -19261719) p2))
2530                             (ceiling -46022.094 p2))))
2531                (values (funcall (compile nil form) -19261719)))))
2532
2533 ;;; misc.636
2534 (assert (let* ((x 26899.875)
2535                (form `(lambda (p2)
2536                         (declare (optimize (speed 3) (safety 1) (debug 3) (space 1))
2537                                  (type (member ,x #:g5437 char-code #:g5438) p2))
2538                         (* 104102267 p2))))
2539           (floatp (funcall (compile nil form) x))))
2540
2541 ;;; misc.622
2542 (assert (eql
2543          (funcall
2544            (compile
2545             nil
2546             '(lambda (p2)
2547               (declare (optimize (speed 3) (safety 2) (debug 3) (space 0))
2548                (type real p2))
2549               (+ 81535869 (the (member 17549.955 #:g35917) p2))))
2550            17549.955)
2551           (+ 81535869 17549.955)))
2552
2553 ;;; misc.654
2554 (assert (eql 2
2555              (let ((form '(lambda (p2)
2556                            (declare (optimize (speed 0) (safety 2) (debug 0) (space 2))
2557                             (type (member integer eql) p2))
2558                            (coerce 2 p2))))
2559                (funcall (compile nil form) 'integer))))
2560
2561 ;;; misc.656
2562 (assert (eql 2
2563              (let ((form '(lambda (p2)
2564                            (declare (optimize (speed 0) (safety 2) (debug 0) (space 2))
2565                             (type (member integer mod) p2))
2566                            (coerce 2 p2))))
2567                (funcall (compile nil form) 'integer))))
2568
2569 ;;; misc.657
2570 (assert (eql 2
2571          (let ((form '(lambda (p2)
2572                        (declare (optimize (speed 0) (safety 2) (debug 0) (space 2))
2573                         (type (member integer values) p2))
2574                        (coerce 2 p2))))
2575            (funcall (compile nil form) 'integer))))
2576
2577 (with-test (:name :string-aref-type)
2578  (assert (eq 'character
2579              (funcall (compile nil
2580                                '(lambda (s)
2581                                  (ctu:compiler-derived-type (aref (the string s) 0))))
2582                       "foo"))))
2583
2584 (with-test (:name :base-string-aref-type)
2585  (assert (eq #+sb-unicode 'base-char
2586              #-sb-unicode 'character
2587              (funcall (compile nil
2588                                '(lambda (s)
2589                                  (ctu:compiler-derived-type (aref (the base-string s) 0))))
2590                       (coerce "foo" 'base-string)))))
2591
2592 (with-test (:name :dolist-constant-type-derivation)
2593   (assert (equal '(integer 1 3)
2594                  (funcall (compile nil
2595                                    '(lambda (x)
2596                                      (dolist (y '(1 2 3))
2597                                        (when x
2598                                          (return (ctu:compiler-derived-type y))))))
2599                           t))))
2600
2601 (with-test (:name :dolist-simple-list-type-derivation)
2602   (assert (equal '(integer 1 3)
2603                  (funcall (compile nil
2604                                    '(lambda (x)
2605                                      (dolist (y (list 1 2 3))
2606                                        (when x
2607                                          (return (ctu:compiler-derived-type y))))))
2608                           t))))
2609
2610 (with-test (:name :dolist-dotted-constant-list-type-derivation)
2611   (let* ((warned nil)
2612          (fun (handler-bind ((style-warning (lambda (c) (push c warned))))
2613                 (compile nil
2614                          '(lambda (x)
2615                            (dolist (y '(1 2 3 . 4) :foo)
2616                              (when x
2617                                (return (ctu:compiler-derived-type y)))))))))
2618     (assert (equal '(integer 1 3) (funcall fun t)))
2619     (assert (= 1 (length warned)))
2620     (multiple-value-bind (res err) (ignore-errors (funcall fun nil))
2621       (assert (not res))
2622       (assert (typep err 'type-error)))))
2623
2624 (with-test (:name :constant-list-destructuring)
2625   (handler-bind ((sb-ext:compiler-note #'error))
2626     (progn
2627       (assert (= 10
2628                  (funcall
2629                   (compile nil
2630                            '(lambda ()
2631                              (destructuring-bind (a (b c) d) '(1 (2 3) 4)
2632                                (+ a b c d)))))))
2633       (assert (eq :feh
2634                   (funcall
2635                    (compile nil
2636                             '(lambda (x)
2637                               (or x
2638                                (destructuring-bind (a (b c) d) '(1 "foo" 4)
2639                                  (+ a b c d)))))
2640                    :feh))))))
2641
2642 ;;; Functions with non-required arguments used to end up with
2643 ;;; (&OPTIONAL-DISPATCH ...) as their names.
2644 (with-test (:name :hairy-function-name)
2645   (assert (eq 'read-line (nth-value 2 (function-lambda-expression #'read-line))))
2646   (assert (equal "#<FUNCTION READ-LINE>" (princ-to-string #'read-line))))
2647
2648 ;;; PROGV + RESTRICT-COMPILER-POLICY
2649 (with-test (:name :progv-and-restrict-compiler-policy)
2650   (let ((sb-c::*policy-restrictions* sb-c::*policy-restrictions*))
2651     (restrict-compiler-policy 'debug 3)
2652     (let ((fun (compile nil '(lambda (x)
2653                               (let ((i x))
2654                                 (declare (special i))
2655                                 (list i
2656                                       (progv '(i) (list (+ i 1))
2657                                         i)
2658                                       i))))))
2659       (assert (equal '(1 2 1) (funcall fun 1))))))
2660
2661 ;;; It used to be possible to confuse the compiler into
2662 ;;; IR2-converting such a call to CONS
2663 (with-test (:name :late-bound-primitive)
2664   (compile nil `(lambda ()
2665                   (funcall 'cons 1))))
2666
2667 (with-test (:name :hairy-array-element-type-derivation)
2668   (compile nil '(lambda (x)
2669                  (declare (type (and simple-string (satisfies array-has-fill-pointer-p)) x))
2670                  (array-element-type x))))
2671
2672 (with-test (:name :rest-list-type-derivation)
2673   (multiple-value-bind (type derivedp)
2674       (funcall (compile nil `(lambda (&rest args)
2675                                (ctu:compiler-derived-type args)))
2676                nil)
2677     (assert (eq 'list type))
2678     (assert derivedp)))
2679
2680 (with-test (:name :rest-list-type-derivation2)
2681   (multiple-value-bind (type derivedp)
2682       (funcall (funcall (compile nil `(lambda ()
2683                                         (lambda (&rest args)
2684                                           (ctu:compiler-derived-type args))))))
2685     (assert (eq 'list type))
2686     (assert derivedp)))
2687
2688 (with-test (:name :rest-list-type-derivation3)
2689   (multiple-value-bind (type derivedp)
2690       (funcall (funcall (compile nil `(lambda ()
2691                                         (lambda (&optional x &rest args)
2692                                           (unless x (error "oops"))
2693                                           (ctu:compiler-derived-type args)))))
2694                t)
2695     (assert (eq 'list type))
2696     (assert derivedp)))
2697
2698 (with-test (:name :rest-list-type-derivation4)
2699   (multiple-value-bind (type derivedp)
2700       (funcall (funcall (compile nil `(lambda ()
2701                                         (lambda (&optional x &rest args)
2702                                           (declare (type (or null integer) x))
2703                                           (when x (setf args x))
2704                                           (ctu:compiler-derived-type args)))))
2705                42)
2706     (assert (equal '(or cons null integer) type))
2707     (assert derivedp)))
2708
2709 (with-test (:name :base-char-typep-elimination)
2710   (assert (eq (funcall (compile nil
2711                                 `(lambda (ch)
2712                                    (declare (type base-char ch) (optimize (speed 3) (safety 0)))
2713                                    (typep ch 'base-char)))
2714                        t)
2715               t)))
2716
2717 (with-test (:name :regression-1.0.24.37)
2718   (compile nil '(lambda (&key (test (constantly t)))
2719                  (when (funcall test)
2720                    :quux))))
2721
2722 ;;; Attempt to test a decent cross section of conditions
2723 ;;; and values types to move conditionally.
2724 (macrolet
2725     ((test-comparison (comparator type x y)
2726        `(progn
2727           ,@(loop for (result-type a b)
2728                     in '((nil t   nil)
2729                          (nil 0   1)
2730                          (nil 0.0 1.0)
2731                          (nil 0d0 0d0)
2732                          (nil 0.0 0d0)
2733                          (nil #c(1.0 1.0) #c(2.0 2.0))
2734
2735                          (t      t  nil)
2736                          (fixnum 0 1)
2737                          ((unsigned-byte #.sb-vm:n-word-bits)
2738                           (1+ most-positive-fixnum)
2739                           (+ 2 most-positive-fixnum))
2740                          ((signed-byte #.sb-vm:n-word-bits)
2741                           -1 (* 2 most-negative-fixnum))
2742                          (single-float 0.0 1.0)
2743                          (double-float 0d0 1d0))
2744                   for lambda = (if result-type
2745                                    `(lambda (x y a b)
2746                                       (declare (,type x y)
2747                                                (,result-type a b))
2748                                       (if (,comparator x y)
2749                                           a b))
2750                                    `(lambda (x y)
2751                                       (declare (,type x y))
2752                                       (if (,comparator x y)
2753                                           ,a ,b)))
2754                   for args = `(,x ,y ,@(and result-type
2755                                             `(,a ,b)))
2756                   collect
2757                   `(progn
2758                      (eql (funcall (compile nil ',lambda)
2759                                    ,@args)
2760                           (eval '(,lambda ,@args))))))))
2761   (sb-vm::with-float-traps-masked
2762       (:divide-by-zero :overflow :inexact :invalid)
2763     (let (#+sb-eval (sb-ext:*evaluator-mode* :interpret))
2764       (declare (sb-ext:muffle-conditions style-warning))
2765       (test-comparison eql t t nil)
2766       (test-comparison eql t t t)
2767
2768       (test-comparison =   t 1 0)
2769       (test-comparison =   t 1 1)
2770       (test-comparison =   t (1+ most-positive-fixnum) (+ 2 most-positive-fixnum))
2771       (test-comparison =   fixnum 1 0)
2772       (test-comparison =   fixnum 0 0)
2773       (test-comparison =   (unsigned-byte #.sb-vm:n-word-bits) 1 0)
2774       (test-comparison =   (unsigned-byte #.sb-vm:n-word-bits) 0 0)
2775       (test-comparison =   (signed-byte #.sb-vm:n-word-bits)   1 0)
2776       (test-comparison =   (signed-byte #.sb-vm:n-word-bits)   1 1)
2777
2778       (test-comparison =   single-float 0.0 1.0)
2779       (test-comparison =   single-float 1.0 1.0)
2780       (test-comparison =   single-float (/ 1.0 0.0) (/ 1.0 0.0))
2781       (test-comparison =   single-float (/ 1.0 0.0) 1.0)
2782       (test-comparison =   single-float (/ 0.0 0.0) (/ 0.0 0.0))
2783       (test-comparison =   single-float (/ 0.0 0.0) 0.0)
2784
2785       (test-comparison =   double-float 0d0 1d0)
2786       (test-comparison =   double-float 1d0 1d0)
2787       (test-comparison =   double-float (/ 1d0 0d0) (/ 1d0 0d0))
2788       (test-comparison =   double-float (/ 1d0 0d0) 1d0)
2789       (test-comparison =   double-float (/ 0d0 0d0) (/ 0d0 0d0))
2790       (test-comparison =   double-float (/ 0d0 0d0) 0d0)
2791
2792       (test-comparison <   t 1 0)
2793       (test-comparison <   t 0 1)
2794       (test-comparison <   t 1 1)
2795       (test-comparison <   t (1+ most-positive-fixnum)  (+ 2 most-positive-fixnum))
2796       (test-comparison <   t (+ 2 most-positive-fixnum) (1+ most-positive-fixnum))
2797       (test-comparison <   fixnum 1 0)
2798       (test-comparison <   fixnum 0 1)
2799       (test-comparison <   fixnum 0 0)
2800       (test-comparison <   (unsigned-byte #.sb-vm:n-word-bits) 1 0)
2801       (test-comparison <   (unsigned-byte #.sb-vm:n-word-bits) 0 1)
2802       (test-comparison <   (unsigned-byte #.sb-vm:n-word-bits) 0 0)
2803       (test-comparison <   (signed-byte #.sb-vm:n-word-bits)   1 0)
2804       (test-comparison <   (signed-byte #.sb-vm:n-word-bits)   0 1)
2805       (test-comparison <   (signed-byte #.sb-vm:n-word-bits)   1 1)
2806
2807       (test-comparison <   single-float 0.0 1.0)
2808       (test-comparison <   single-float 1.0 0.0)
2809       (test-comparison <   single-float 1.0 1.0)
2810       (test-comparison <   single-float (/ 1.0 0.0) (/ 1.0 0.0))
2811       (test-comparison <   single-float (/ 1.0 0.0) 1.0)
2812       (test-comparison <   single-float 1.0 (/ 1.0 0.0))
2813       (test-comparison <   single-float (/ 0.0 0.0) (/ 0.0 0.0))
2814       (test-comparison <   single-float (/ 0.0 0.0) 0.0)
2815
2816       (test-comparison <   double-float 0d0 1d0)
2817       (test-comparison <   double-float 1d0 0d0)
2818       (test-comparison <   double-float 1d0 1d0)
2819       (test-comparison <   double-float (/ 1d0 0d0) (/ 1d0 0d0))
2820       (test-comparison <   double-float (/ 1d0 0d0) 1d0)
2821       (test-comparison <   double-float 1d0 (/ 1d0 0d0))
2822       (test-comparison <   double-float (/ 0d0 0d0) (/ 0d0 0d0))
2823       (test-comparison <   double-float (/ 0d0 0d0) 0d0)
2824       (test-comparison <   double-float 0d0 (/ 0d0 0d0))
2825
2826       (test-comparison >   t 1 0)
2827       (test-comparison >   t 0 1)
2828       (test-comparison >   t 1 1)
2829       (test-comparison >   t (1+ most-positive-fixnum)  (+ 2 most-positive-fixnum))
2830       (test-comparison >   t (+ 2 most-positive-fixnum) (1+ most-positive-fixnum))
2831       (test-comparison >   fixnum 1 0)
2832       (test-comparison >   fixnum 0 1)
2833       (test-comparison >   fixnum 0 0)
2834       (test-comparison >   (unsigned-byte #.sb-vm:n-word-bits) 1 0)
2835       (test-comparison >   (unsigned-byte #.sb-vm:n-word-bits) 0 1)
2836       (test-comparison >   (unsigned-byte #.sb-vm:n-word-bits) 0 0)
2837       (test-comparison >   (signed-byte #.sb-vm:n-word-bits)   1 0)
2838       (test-comparison >   (signed-byte #.sb-vm:n-word-bits)   0 1)
2839       (test-comparison >   (signed-byte #.sb-vm:n-word-bits)   1 1)
2840
2841       (test-comparison >   single-float 0.0 1.0)
2842       (test-comparison >   single-float 1.0 0.0)
2843       (test-comparison >   single-float 1.0 1.0)
2844       (test-comparison >   single-float (/ 1.0 0.0) (/ 1.0 0.0))
2845       (test-comparison >   single-float (/ 1.0 0.0) 1.0)
2846       (test-comparison >   single-float 1.0 (/ 1.0 0.0))
2847       (test-comparison >   single-float (/ 0.0 0.0) (/ 0.0 0.0))
2848       (test-comparison >   single-float (/ 0.0 0.0) 0.0)
2849
2850       (test-comparison >   double-float 0d0 1d0)
2851       (test-comparison >   double-float 1d0 0d0)
2852       (test-comparison >   double-float 1d0 1d0)
2853       (test-comparison >   double-float (/ 1d0 0d0) (/ 1d0 0d0))
2854       (test-comparison >   double-float (/ 1d0 0d0) 1d0)
2855       (test-comparison >   double-float 1d0 (/ 1d0 0d0))
2856       (test-comparison >   double-float (/ 0d0 0d0) (/ 0d0 0d0))
2857       (test-comparison >   double-float (/ 0d0 0d0) 0d0)
2858       (test-comparison >   double-float 0d0 (/ 0d0 0d0)))))
2859
2860 (with-test (:name :car-and-cdr-type-derivation-conservative)
2861   (let ((f1 (compile nil
2862                      `(lambda (y)
2863                         (declare (optimize speed))
2864                         (let ((x (the (cons fixnum fixnum) (cons 1 2))))
2865                           (declare (type (cons t fixnum) x))
2866                           (rplaca x y)
2867                           (+ (car x) (cdr x))))))
2868         (f2 (compile nil
2869                      `(lambda (y)
2870                         (declare (optimize speed))
2871                         (let ((x (the (cons fixnum fixnum) (cons 1 2))))
2872                           (setf (cdr x) y)
2873                           (+ (car x) (cdr x)))))))
2874     (flet ((test-error (e value)
2875              (assert (typep e 'type-error))
2876              (assert (eq 'number (type-error-expected-type e)))
2877              (assert (eq value (type-error-datum e)))))
2878       (let ((v1 "foo")
2879             (v2 "bar"))
2880         (multiple-value-bind (res err) (ignore-errors (funcall f1 v1))
2881           (assert (not res))
2882           (test-error err v1))
2883         (multiple-value-bind (res err) (ignore-errors (funcall f2 v2))
2884           (assert (not res))
2885           (test-error err v2))))))
2886
2887 (with-test (:name :array-dimension-derivation-conservative)
2888   (let ((f (compile nil
2889                     `(lambda (x)
2890                        (declare (optimize speed))
2891                        (declare (type (array * (4 4)) x))
2892                        (let ((y x))
2893                          (setq x (make-array '(4 4)))
2894                          (adjust-array y '(3 5))
2895                          (array-dimension y 0))))))
2896     (assert (= 3 (funcall f (make-array '(4 4) :adjustable t))))))
2897
2898 (with-test (:name :with-timeout-code-deletion-note)
2899   (handler-bind ((sb-ext:code-deletion-note #'error))
2900     (compile nil `(lambda ()
2901                     (sb-ext:with-timeout 0
2902                       (sleep 1))))))
2903
2904 (with-test (:name :full-warning-for-undefined-type-in-cl)
2905   (assert (eq :full
2906               (handler-case
2907                   (compile nil `(lambda (x) (the replace x)))
2908                 (style-warning ()
2909                   :style)
2910                 (warning ()
2911                   :full)))))
2912
2913 (with-test (:name :single-warning-for-single-undefined-type)
2914   (let ((n 0))
2915     (handler-bind ((warning (lambda (c)
2916                               (declare (ignore c))
2917                               (incf n))))
2918       (compile nil `(lambda (x) (the #:no-type x)))
2919       (assert (= 1 n))
2920       (compile nil `(lambda (x) (the 'fixnum x)))
2921       (assert (= 2 n)))))
2922
2923 (with-test (:name :complex-subtype-dumping-in-xc)
2924   (assert
2925    (= sb-vm:complex-single-float-widetag
2926       (sb-kernel:widetag-of
2927        (sb-vm:saetp-initial-element-default (sb-c::find-saetp '(complex single-float))))))
2928   (assert
2929    (= sb-vm:complex-double-float-widetag
2930       (sb-kernel:widetag-of
2931        (sb-vm:saetp-initial-element-default (sb-c::find-saetp '(complex double-float)))))))
2932
2933 (with-test (:name :complex-single-float-fill)
2934   (assert (every (lambda (x) (= #c(1.0 2.0) x))
2935                  (funcall
2936                   (compile nil
2937                            `(lambda (n x)
2938                               (make-array (list n)
2939                                           :element-type '(complex single-float)
2940                                           :initial-element x)))
2941                   10
2942                   #c(1.0 2.0)))))
2943
2944 (with-test (:name :regression-1.0.28.21)
2945   (let ((fun (compile nil `(lambda (x) (typep x '(simple-array * 1))))))
2946     (assert (funcall fun (vector 1 2 3)))
2947     (assert (funcall fun "abc"))
2948     (assert (not (funcall fun (make-array '(2 2)))))))
2949
2950 (with-test (:name :no-silly-compiler-notes-from-character-function)
2951   (let (current)
2952     (handler-bind ((compiler-note (lambda (e) (error "~S: ~A" current e))))
2953       (dolist (name '(char-code char-int character char-name standard-char-p
2954                       graphic-char-p alpha-char-p upper-case-p lower-case-p
2955                       both-case-p digit-char-p alphanumericp digit-char-p))
2956         (setf current name)
2957         (compile nil `(lambda (x)
2958                         (declare (character x) (optimize speed))
2959                         (,name x))))
2960       (dolist (name '(char= char/= char< char> char<= char>= char-equal
2961                       char-not-equal char-lessp char-greaterp char-not-greaterp
2962                       char-not-lessp))
2963         (setf current name)
2964         (compile nil `(lambda (x y)
2965                         (declare (character x y) (optimize speed))
2966                         (,name x y)))))))
2967
2968 ;;; optimizing make-array
2969 (with-test (:name (make-array :open-code-initial-contents))
2970   (assert (not (ctu:find-named-callees
2971                 (compile nil
2972                          `(lambda (x y z)
2973                             (make-array '(3) :initial-contents (list x y z)))))))
2974   (assert (not (ctu:find-named-callees
2975                 (compile nil
2976                          `(lambda (x y z)
2977                             (make-array '3 :initial-contents (vector x y z)))))))
2978   (assert (not (ctu:find-named-callees
2979                 (compile nil
2980                          `(lambda (x y z)
2981                             (make-array '3 :initial-contents `(,x ,y ,z))))))))
2982
2983 ;;; optimizing array-in-bounds-p
2984 (with-test (:name :optimize-array-in-bounds-p)
2985   (locally
2986     (macrolet ((find-callees (&body body)
2987                  `(ctu:find-named-callees
2988                     (compile nil
2989                              '(lambda ()
2990                                 ,@body))
2991                     :name 'array-in-bounds-p))
2992                (must-optimize (&body exprs)
2993                  `(progn
2994                     ,@(loop for expr in exprs
2995                             collect `(assert (not (find-callees
2996                                                    ,expr))))))
2997                (must-not-optimize (&body exprs)
2998                  `(progn
2999                     ,@(loop for expr in exprs
3000                             collect `(assert (find-callees
3001                                               ,expr))))))
3002       (must-optimize
3003         ;; in bounds
3004         (let ((a (make-array '(1))))
3005           (array-in-bounds-p a 0))
3006         ;; exceeds upper bound (constant)
3007         (let ((a (make-array '(1))))
3008           (array-in-bounds-p a 1))
3009         ;; exceeds upper bound (interval)
3010         (let ((a (make-array '(1))))
3011           (array-in-bounds-p a (+ 1 (random 2))))
3012         ;; negative lower bound (constant)
3013         (let ((a (make-array '(1))))
3014           (array-in-bounds-p a -1))
3015         ;; negative lower bound (interval)
3016         (let ((a (make-array 3))
3017               (i (- (random 1) 20)))
3018           (array-in-bounds-p a i))
3019         ;; multiple known dimensions
3020         (let ((a (make-array '(1 1))))
3021           (array-in-bounds-p a 0 0))
3022         ;; union types
3023         (let ((s (the (simple-string 10) (eval "0123456789"))))
3024           (array-in-bounds-p s 9)))
3025       (must-not-optimize
3026        ;; don't trust non-simple array length in safety=1
3027        (let ((a (the (array * (10)) (make-array 10 :adjustable t))))
3028          (eval `(adjust-array ,a 0))
3029          (array-in-bounds-p a 9))
3030        ;; same for a union type
3031        (let ((s (the (string 10) (make-array 10
3032                                              :element-type 'character
3033                                              :adjustable t))))
3034          (eval `(adjust-array ,s 0))
3035          (array-in-bounds-p s 9))
3036        ;; single unknown dimension
3037        (let ((a (make-array (random 20))))
3038          (array-in-bounds-p a 10))
3039        ;; multiple unknown dimensions
3040        (let ((a (make-array (list (random 20) (random 5)))))
3041          (array-in-bounds-p a 5 2))
3042        ;; some other known dimensions
3043        (let ((a (make-array (list 1 (random 5)))))
3044          (array-in-bounds-p a 0 2))
3045        ;; subscript might be negative
3046        (let ((a (make-array 5)))
3047          (array-in-bounds-p a (- (random 3) 2)))
3048        ;; subscript might be too large
3049        (let ((a (make-array 5)))
3050          (array-in-bounds-p a (random 6)))
3051        ;; unknown upper bound
3052        (let ((a (make-array 5)))
3053          (array-in-bounds-p a (get-universal-time)))
3054        ;; unknown lower bound
3055        (let ((a (make-array 5)))
3056          (array-in-bounds-p a (- (get-universal-time))))
3057        ;; in theory we should be able to optimize
3058        ;; the following but the current implementation
3059        ;; doesn't cut it because the array type's
3060        ;; dimensions get reported as (* *).
3061        (let ((a (make-array (list (random 20) 1))))
3062          (array-in-bounds-p a 5 2))))))
3063
3064 ;;; optimizing (EXPT -1 INTEGER)
3065 (test-util:with-test (:name (expt minus-one integer))
3066   (dolist (x '(-1 -1.0 -1.0d0))
3067     (let ((fun (compile nil `(lambda (x) (expt ,x (the fixnum x))))))
3068       (assert (not (ctu:find-named-callees fun)))
3069       (dotimes (i 12)
3070         (if (oddp i)
3071             (assert (eql x (funcall fun i)))
3072             (assert (eql (- x) (funcall fun i))))))))
3073
3074 (with-test (:name :float-division-using-exact-reciprocal)
3075   (flet ((test (lambda-form arg res &key (check-insts t))
3076            (let* ((fun (compile nil lambda-form))
3077                   (disassembly (with-output-to-string (s)
3078                                   (disassemble fun :stream s))))
3079              ;; Let's make sure there is no division at runtime: for x86 and
3080              ;; x86-64 that implies an FDIV, DIVSS, or DIVSD instruction, so
3081              ;; look for DIV in the disassembly. It's a terrible KLUDGE, but
3082              ;; it works.
3083              #+(or x86 x86-64)
3084              (when check-insts
3085                (assert (not (search "DIV" disassembly))))
3086              ;; No generic arithmetic!
3087              (assert (not (search "GENERIC" disassembly)))
3088              (assert (eql res (funcall fun arg))))))
3089     (dolist (c '(128 64 32 16 8 4 2 1 1/2 1/4 1/8 1/16 1/32 1/64))
3090       (dolist (type '(single-float double-float))
3091         (let* ((cf (coerce c type))
3092                (arg (- (random (* 2 cf)) cf))
3093                (r1 (eval `(/ ,arg ,cf)))
3094                (r2 (eval `(/ ,arg ,(- cf)))))
3095           (test `(lambda (x) (declare (,type x)) (/ x ,cf)) arg r1)
3096           (test `(lambda (x) (declare (,type x)) (/ x ,(- cf))) arg r2)
3097           ;; rational args should get optimized as well
3098           (test `(lambda (x) (declare (,type x)) (/ x ,c)) arg r1)
3099           (test `(lambda (x) (declare (,type x)) (/ x ,(- c))) arg r2))))
3100     ;; Also check that inexact reciprocals (1) are not used by default (2) are
3101     ;; used with FLOAT-ACCURACY=0.
3102     (dolist (type '(single-float double-float))
3103       (let ((trey (coerce 3 type))
3104             (one (coerce 1 type)))
3105         (test `(lambda (x) (declare (,type x)) (/ x 3)) trey one
3106               :check-insts nil)
3107         (test `(lambda (x)
3108                  (declare (,type x)
3109                           (optimize (sb-c::float-accuracy 0)))
3110                  (/ x 3))
3111               trey (eval `(* ,trey (/ ,trey))))))))
3112
3113 (with-test (:name :float-multiplication-by-one)
3114   (flet ((test (lambda-form arg &optional (result arg))
3115            (let* ((fun1 (compile nil lambda-form))
3116                   (fun2 (funcall (compile nil `(lambda ()
3117                                                  (declare (optimize (sb-c::float-accuracy 0)))
3118                                                  ,lambda-form))))
3119                   (disassembly1 (with-output-to-string (s)
3120                                   (disassemble fun1 :stream s)))
3121                   (disassembly2 (with-output-to-string (s)
3122                                   (disassemble fun2 :stream s))))
3123              ;; Multiplication at runtime should be eliminated only with
3124              ;; FLOAT-ACCURACY=0. (To catch SNaNs.)
3125              #+(or x86 x86-64)
3126              (assert (and (search "MUL" disassembly1)
3127                           (not (search "MUL" disassembly2))))
3128              ;; Not generic arithmetic, please!
3129              (assert (and (not (search "GENERIC" disassembly1))
3130                           (not (search "GENERIC" disassembly2))))
3131              (assert (eql result (funcall fun1 arg)))
3132              (assert (eql result (funcall fun2 arg))))))
3133     (dolist (type '(single-float double-float))
3134       (let* ((one (coerce 1 type))
3135              (arg (random (* 2 one)))
3136              (-r (- arg)))
3137         (test `(lambda (x) (declare (,type x)) (* x 1)) arg)
3138         (test `(lambda (x) (declare (,type x)) (* x -1)) arg -r)
3139         (test `(lambda (x) (declare (,type x)) (* x ,one)) arg)
3140         (test `(lambda (x) (declare (,type x)) (* x ,(- one))) arg -r)))))
3141
3142 (with-test (:name :float-addition-of-zero)
3143   (flet ((test (lambda-form arg &optional (result arg))
3144            (let* ((fun1 (compile nil lambda-form))
3145                   (fun2 (funcall (compile nil `(lambda ()
3146                                                  (declare (optimize (sb-c::float-accuracy 0)))
3147                                                  ,lambda-form))))
3148                   (disassembly1 (with-output-to-string (s)
3149                                   (disassemble fun1 :stream s)))
3150                   (disassembly2 (with-output-to-string (s)
3151                                   (disassemble fun2 :stream s))))
3152              ;; Let's make sure there is no addition at runtime: for x86 and
3153              ;; x86-64 that implies an FADD, ADDSS, or ADDSD instruction, so
3154              ;; look for the ADDs in the disassembly. It's a terrible KLUDGE,
3155              ;; but it works. Unless FLOAT-ACCURACY is zero, we leave the
3156              ;; addition in to catch SNaNs.
3157              #+x86
3158              (assert (and (search "FADD" disassembly1)
3159                           (not (search "FADD" disassembly2))))
3160              #+x86-64
3161              (let ((inst (if (typep result 'double-float)
3162                              "ADDSD" "ADDSS")))
3163                (assert (and (search inst disassembly1)
3164                             (not (search inst disassembly2)))))
3165              (assert (eql result (funcall fun1 arg)))
3166              (assert (eql result (funcall fun2 arg))))))
3167     (test `(lambda (x) (declare (single-float x)) (+ x 0)) 123.45)
3168     (test `(lambda (x) (declare (single-float x)) (+ x 0.0)) 543.21)
3169     (test `(lambda (x) (declare (single-float x)) (+ x 0.0d0)) 42.00 42.d0)
3170     (test `(lambda (x) (declare (double-float x)) (+ x 0)) 123.45d0)
3171     (test `(lambda (x) (declare (double-float x)) (+ x 0.0)) 543.21d0)
3172     (test `(lambda (x) (declare (double-float x)) (+ x 0.0d0)) 42.d0)))
3173
3174 (with-test (:name :float-substraction-of-zero)
3175   (flet ((test (lambda-form arg &optional (result arg))
3176            (let* ((fun1 (compile nil lambda-form))
3177                   (fun2 (funcall (compile nil `(lambda ()
3178                                                  (declare (optimize (sb-c::float-accuracy 0)))
3179                                                  ,lambda-form))))
3180                   (disassembly1 (with-output-to-string (s)
3181                                   (disassemble fun1 :stream s)))
3182                   (disassembly2 (with-output-to-string (s)
3183                                   (disassemble fun2 :stream s))))
3184              ;; Let's make sure there is no substraction at runtime: for x86
3185              ;; and x86-64 that implies an FSUB, SUBSS, or SUBSD instruction,
3186              ;; so look for SUB in the disassembly. It's a terrible KLUDGE,
3187              ;; but it works. Unless FLOAT-ACCURACY is zero, we leave the
3188              ;; substraction in in to catch SNaNs.
3189              #+x86
3190              (assert (and (search "FSUB" disassembly1)
3191                           (not (search "FSUB" disassembly2))))
3192              #+x86-64
3193              (let ((inst (if (typep result 'double-float)
3194                              "SUBSD" "SUBSS")))
3195                (assert (and (search inst disassembly1)
3196                             (not (search inst disassembly2)))))
3197              (assert (eql result (funcall fun1 arg)))
3198              (assert (eql result (funcall fun2 arg))))))
3199     (test `(lambda (x) (declare (single-float x)) (- x 0)) 123.45)
3200     (test `(lambda (x) (declare (single-float x)) (- x 0.0)) 543.21)
3201     (test `(lambda (x) (declare (single-float x)) (- x 0.0d0)) 42.00 42.d0)
3202     (test `(lambda (x) (declare (double-float x)) (- x 0)) 123.45d0)
3203     (test `(lambda (x) (declare (double-float x)) (- x 0.0)) 543.21d0)
3204     (test `(lambda (x) (declare (double-float x)) (- x 0.0d0)) 42.d0)))
3205
3206 (with-test (:name :float-multiplication-by-two)
3207   (flet ((test (lambda-form arg &optional (result arg))
3208            (let* ((fun1 (compile nil lambda-form))
3209                   (fun2 (funcall (compile nil `(lambda ()
3210                                                  (declare (optimize (sb-c::float-accuracy 0)))
3211                                                  ,lambda-form))))
3212                   (disassembly1 (with-output-to-string (s)
3213                                   (disassemble fun1 :stream s)))
3214                   (disassembly2 (with-output-to-string (s)
3215                                   (disassemble fun2 :stream s))))
3216              ;; Let's make sure there is no multiplication at runtime: for x86
3217              ;; and x86-64 that implies an FMUL, MULSS, or MULSD instruction,
3218              ;; so look for MUL in the disassembly. It's a terrible KLUDGE,
3219              ;; but it works.
3220              #+(or x86 x86-64)
3221              (assert (and (not (search "MUL" disassembly1))
3222                           (not (search "MUL" disassembly2))))
3223              (assert (eql result (funcall fun1 arg)))
3224              (assert (eql result (funcall fun2 arg))))))
3225     (test `(lambda (x) (declare (single-float x)) (* x 2)) 123.45 246.9)
3226     (test `(lambda (x) (declare (single-float x)) (* x 2.0)) 543.21 1086.42)
3227     (test `(lambda (x) (declare (single-float x)) (* x 2.0d0)) 42.00 84.d0)
3228     (test `(lambda (x) (declare (double-float x)) (* x 2)) 123.45d0 246.9d0)
3229     (test `(lambda (x) (declare (double-float x)) (* x 2.0)) 543.21d0 1086.42d0)
3230     (test `(lambda (x) (declare (double-float x)) (* x 2.0d0)) 42.0d0 84.0d0)))
3231
3232 (with-test (:name :bug-392203)
3233   ;; Used to hit an AVER in COMVERT-MV-CALL.
3234   (assert (zerop
3235            (funcall
3236             (compile nil
3237                      `(lambda ()
3238                         (flet ((k (&rest x) (declare (ignore x)) 0))
3239                           (multiple-value-call #'k #'k))))))))
3240
3241 (with-test (:name :allocate-closures-failing-aver)
3242   (let ((f (compile nil `(lambda ()
3243                            (labels ((k (&optional x) #'k)))))))
3244     (assert (null (funcall f)))))
3245
3246 (with-test (:name :flush-vector-creation)
3247   (let ((f (compile nil `(lambda ()
3248                            (dotimes (i 1024)
3249                              (vector i i i))
3250                            t))))
3251     (ctu:assert-no-consing (funcall f))))
3252
3253 (with-test (:name :array-type-predicates)
3254   (dolist (et sb-kernel::*specialized-array-element-types*)
3255     (when et
3256       (let* ((v (make-array 3 :element-type et))
3257              (fun (compile nil `(lambda ()
3258                                   (list
3259                                    (if (typep ,v '(simple-array ,et (*)))
3260                                        :good
3261                                        :bad)
3262                                    (if (typep (elt ,v 0) '(simple-array ,et (*)))
3263                                        :bad
3264                                        :good))))))
3265         (assert (equal '(:good :good) (funcall fun)))))))
3266
3267 (with-test (:name :truncate-float)
3268   (let ((s (compile nil `(lambda (x)
3269                            (declare (single-float x))
3270                            (truncate x))))
3271         (d (compile nil `(lambda (x)
3272                            (declare (double-float x))
3273                            (truncate x))))
3274         (s-inlined (compile nil '(lambda (x)
3275                                   (declare (type (single-float 0.0s0 1.0s0) x))
3276                                   (truncate x))))
3277         (d-inlined (compile nil '(lambda (x)
3278                                   (declare (type (double-float 0.0d0 1.0d0) x))
3279                                   (truncate x)))))
3280     ;; Check that there is no generic arithmetic
3281     (assert (not (search "GENERIC"
3282                          (with-output-to-string (out)
3283                            (disassemble s :stream out)))))
3284     (assert (not (search "GENERIC"
3285                          (with-output-to-string (out)
3286                            (disassemble d :stream out)))))
3287     ;; Check that we actually inlined the call when we were supposed to.
3288     (assert (not (search "UNARY-TRUNCATE"
3289                          (with-output-to-string (out)
3290                            (disassemble s-inlined :stream out)))))
3291     (assert (not (search "UNARY-TRUNCATE"
3292                          (with-output-to-string (out)
3293                            (disassemble d-inlined :stream out)))))))
3294
3295 (with-test (:name :make-array-unnamed-dimension-leaf)
3296   (let ((fun (compile nil `(lambda (stuff)
3297                              (make-array (map 'list 'length stuff))))))
3298     (assert (equalp #2A((0 0 0) (0 0 0))
3299                     (funcall fun '((1 2) (1 2 3)))))))
3300
3301 (with-test (:name :fp-decoding-funs-not-flushable-in-safe-code)
3302   (dolist (name '(float-sign float-radix float-digits float-precision decode-float
3303                   integer-decode-float))
3304     (let ((fun (compile nil `(lambda (x)
3305                                (declare (optimize safety))
3306                                (,name x)
3307                                nil))))
3308       (flet ((test (arg)
3309                (unless (eq :error
3310                            (handler-case
3311                                (funcall fun arg)
3312                              (error () :error)))
3313                  (error "(~S ~S) did not error"
3314                         name arg))))
3315         ;; No error
3316         (funcall fun 1.0)
3317         ;; Error
3318         (test 'not-a-float)
3319         (when (member name '(decode-float integer-decode-float))
3320           (test sb-ext:single-float-positive-infinity))))))
3321
3322 (with-test (:name :sap-ref-16)
3323   (let* ((fun (compile nil `(lambda (x y)
3324                               (declare (type sb-sys:system-area-pointer x)
3325                                        (type (integer 0 100) y))
3326                               (sb-sys:sap-ref-16 x (+ 4 y)))))
3327          (vector (coerce '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
3328                          '(simple-array (unsigned-byte 8) (*))))
3329          (sap (sb-sys:vector-sap vector))
3330          (ret (funcall fun sap 0)))
3331     ;; test for either endianness
3332     (assert (or (= ret (+ (* 5 256) 4)) (= ret (+ (* 4 256) 5))))))
3333
3334 (with-test (:name :coerce-type-warning)
3335   (dolist (type '(t (unsigned-byte 8) (unsigned-byte 16) (unsigned-byte 32)
3336                   (signed-byte 8) (signed-byte 16) (signed-byte 32)))
3337     (multiple-value-bind (fun warningsp failurep)
3338         (compile nil `(lambda (x)
3339                         (declare (type simple-vector x))
3340                         (coerce x '(vector ,type))))
3341       (assert (null warningsp))
3342       (assert (null failurep))
3343       (assert (typep (funcall fun #(1)) `(simple-array ,type (*)))))))
3344
3345 (with-test (:name :truncate-double-float)
3346   (let ((fun (compile nil `(lambda (x)
3347                              (multiple-value-bind (q r)
3348                                  (truncate (coerce x 'double-float))
3349                                (declare (type unsigned-byte q)
3350                                         (type double-float r))
3351                                (list q r))))))
3352     (assert (equal (funcall fun 1.0d0) '(1 0.0d0)))))
3353
3354 (with-test (:name :set-slot-value-no-warning)
3355   (let ((notes 0))
3356     (handler-bind ((warning #'error)
3357                    (sb-ext:compiler-note (lambda (c)
3358                                            (declare (ignore c))
3359                                            (incf notes))))
3360       (compile nil `(lambda (x y)
3361                       (declare (optimize speed safety))
3362                       (setf (slot-value x 'bar) y))))
3363     (assert (= 1 notes))))
3364
3365 (with-test (:name :concatenate-string-opt)
3366   (flet ((test (type grep)
3367            (let* ((fun (compile nil `(lambda (a b c d e)
3368                                       (concatenate ',type a b c d e))))
3369                   (args '("foo" #(#\.) "bar" (#\-) "quux"))
3370                   (res (apply fun args)))
3371              (assert (search grep (with-output-to-string (out)
3372                                     (disassemble fun :stream out))))
3373              (assert (equal (apply #'concatenate type args)
3374                             res))
3375              (assert (typep res type)))))
3376     (test 'string "%CONCATENATE-TO-STRING")
3377     (test 'simple-string "%CONCATENATE-TO-STRING")
3378     (test 'base-string "%CONCATENATE-TO-BASE-STRING")
3379     (test 'simple-base-string "%CONCATENATE-TO-BASE-STRING")))
3380
3381 (with-test (:name :satisfies-no-local-fun)
3382   (let ((fun (compile nil `(lambda (arg)
3383                              (labels ((local-not-global-bug (x)
3384                                         t)
3385                                       (bar (x)
3386                                         (typep x '(satisfies local-not-global-bug))))
3387                                (bar arg))))))
3388     (assert (eq 'local-not-global-bug
3389                 (handler-case
3390                     (funcall fun 42)
3391                   (undefined-function (c)
3392                     (cell-error-name c)))))))
3393
3394 ;;; Prior to 1.0.32.x, dumping a fasl with a function with a default
3395 ;;; argument that is a complex structure (needing make-load-form
3396 ;;; processing) failed an AVER.  The first attempt at a fix caused
3397 ;;; doing the same in-core to break.
3398 (with-test (:name :bug-310132)
3399   (compile nil '(lambda (&optional (foo #p"foo/bar")))))
3400
3401 (with-test (:name :bug-309129)
3402   (let* ((src '(lambda (v) (values (svref v 0) (vector-pop v))))
3403          (warningp nil)
3404          (fun (handler-bind ((warning (lambda (c)
3405                                         (setf warningp t) (muffle-warning c))))
3406                 (compile nil src))))
3407     (assert warningp)
3408     (handler-case (funcall fun #(1))
3409       (type-error (c)
3410         ;; we used to put simply VECTOR into EXPECTED-TYPE, rather
3411         ;; than explicitly (AND VECTOR (NOT SIMPLE-ARRAY))
3412         (assert (not (typep (type-error-datum c) (type-error-expected-type c)))))
3413       (:no-error (&rest values)
3414         (declare (ignore values))
3415         (error "no error")))))
3416
3417 (with-test (:name :unary-round-type-derivation)
3418   (let* ((src '(lambda (zone)
3419                 (multiple-value-bind (h m) (truncate (abs zone) 1.0)
3420                   (declare (ignore h))
3421                   (round (* 60.0 m)))))
3422          (fun (compile nil src)))
3423     (assert (= (funcall fun 0.5) 30))))
3424
3425 (with-test (:name :bug-525949)
3426   (let* ((src '(lambda ()
3427                 (labels ((always-one () 1)
3428                          (f (z)
3429                            (let ((n (funcall z)))
3430                              (declare (fixnum n))
3431                              (the double-float (expt n 1.0d0)))))
3432                   (f #'always-one))))
3433          (warningp nil)
3434          (fun (handler-bind ((warning (lambda (c)
3435                                         (setf warningp t) (muffle-warning c))))
3436                 (compile nil src))))
3437     (assert (not warningp))
3438     (assert (= 1.0d0 (funcall fun)))))
3439
3440 (with-test (:name :%array-data-vector-type-derivation)
3441   (let* ((f (compile nil
3442                      `(lambda (ary)
3443                         (declare (type (simple-array (unsigned-byte 32) (3 3)) ary))
3444                         (setf (aref ary 0 0) 0))))
3445          (text (with-output-to-string (s)
3446                  (disassemble f :stream s))))
3447     (assert (not (search "OBJECT-NOT-SIMPLE-ARRAY-UNSIGNED-BYTE-32-ERROR" text)))))
3448
3449 (with-test (:name :array-storage-vector-type-derivation)
3450   (let ((f (compile nil
3451                     `(lambda (ary)
3452                        (declare (type (simple-array (unsigned-byte 32) (3 3)) ary))
3453                        (ctu:compiler-derived-type (array-storage-vector ary))))))
3454     (assert (equal '(simple-array (unsigned-byte 32) (9))
3455                    (funcall f (make-array '(3 3) :element-type '(unsigned-byte 32)))))))
3456
3457 (with-test (:name :bug-523612)
3458   (let ((fun
3459          (compile nil
3460                   `(lambda (&key toff)
3461                      (make-array 3 :element-type 'double-float
3462                                  :initial-contents
3463                                  (if toff (list toff 0d0 0d0) (list 0d0 0d0 0d0)))))))
3464     (assert (equalp (vector 0.0d0 0.0d0 0.0d0) (funcall fun :toff nil)))
3465     (assert (equalp (vector 2.3d0 0.0d0 0.0d0) (funcall fun :toff 2.3d0)))))
3466
3467 (with-test (:name :bug-309788)
3468   (let ((fun
3469          (compile nil
3470                   `(lambda (x)
3471                      (declare (optimize speed))
3472                      (let ((env nil))
3473                        (typep x 'fixnum env))))))
3474     (assert (not (ctu:find-named-callees fun)))))
3475
3476 (with-test (:name :bug-309124)
3477   (let ((fun
3478          (compile nil
3479                   `(lambda (x)
3480                      (declare (integer x))
3481                      (declare (optimize speed))
3482                      (cond ((typep x 'fixnum)
3483                             "hala")
3484                            ((typep x 'fixnum)
3485                             "buba")
3486                            ((typep x 'bignum)
3487                             "hip")
3488                            (t
3489                             "zuz"))))))
3490     (assert (equal (list "hala" "hip")
3491                    (sort (ctu:find-code-constants fun :type 'string)
3492                          #'string<)))))
3493
3494 (with-test (:name :bug-316078)
3495   (let ((fun
3496          (compile nil
3497                   `(lambda (x)
3498                      (declare (type (and simple-bit-vector (satisfies bar)) x)
3499                               (optimize speed))
3500                      (elt x 5)))))
3501     (assert (not (ctu:find-named-callees fun)))
3502     (assert (= 1 (funcall fun #*000001)))
3503     (assert (= 0 (funcall fun #*000010)))))
3504
3505 (with-test (:name :mult-by-one-in-float-acc-zero)
3506   (assert (eql 1.0 (funcall (compile nil `(lambda (x)
3507                                             (declare (optimize (sb-c::float-accuracy 0)))
3508                                             (* x 1.0)))
3509                             1)))
3510   (assert (eql -1.0 (funcall (compile nil `(lambda (x)
3511                                              (declare (optimize (sb-c::float-accuracy 0)))
3512                                              (* x -1.0)))
3513                              1)))
3514   (assert (eql 1.0d0 (funcall (compile nil `(lambda (x)
3515                                               (declare (optimize (sb-c::float-accuracy 0)))
3516                                               (* x 1.0d0)))
3517                               1)))
3518   (assert (eql -1.0d0 (funcall (compile nil `(lambda (x)
3519                                                (declare (optimize (sb-c::float-accuracy 0)))
3520                                                (* x -1.0d0)))
3521                                1))))
3522
3523 (with-test (:name :dotimes-non-integer-counter-value)
3524   (assert (raises-error? (dotimes (i 8.6)) type-error)))
3525
3526 (with-test (:name :bug-454681)
3527   ;; This used to break due to reference to a dead lambda-var during
3528   ;; inline expansion.
3529   (assert (compile nil
3530                    `(lambda ()
3531                       (multiple-value-bind (iterator+977 getter+978)
3532                           (does-not-exist-but-does-not-matter)
3533                         (flet ((iterator+976 ()
3534                                  (funcall iterator+977)))
3535                           (declare (inline iterator+976))
3536                           (let ((iterator+976 #'iterator+976))
3537                             (funcall iterator+976))))))))
3538
3539 (with-test (:name :complex-float-local-fun-args)
3540   ;; As of 1.0.27.14, the lambda below failed to compile due to the
3541   ;; compiler attempting to pass unboxed complex floats to Z and the
3542   ;; MOVE-ARG method not expecting the register being used as a
3543   ;; temporary frame pointer.  Reported by sykopomp in #lispgames,
3544   ;; reduced test case provided by _3b`.
3545   (compile nil '(lambda (a)
3546                   (labels ((z (b c)
3547                               (declare ((complex double-float) b c))
3548                               (* b (z b c))))
3549                           (loop for i below 10 do
3550                                 (setf a (z a a)))))))
3551
3552 (with-test (:name :bug-309130)
3553   (assert (eq :warning
3554               (handler-case
3555                   (compile nil `(lambda () (svref (make-array 8 :adjustable t) 1)))
3556                 ((and warning (not style-warning)) ()
3557                   :warning))))
3558   (assert (eq :warning
3559               (handler-case
3560                   (compile nil `(lambda (x)
3561                                   (declare (optimize (debug 0)))
3562                                   (declare (type vector x))
3563                                   (list (fill-pointer x) (svref x 1))))
3564                 ((and warning (not style-warning)) ()
3565                   :warning))))
3566   (assert (eq :warning
3567               (handler-case
3568                   (compile nil `(lambda (x)
3569                                   (list (vector-push (svref x 0) x))))
3570                 ((and warning (not style-warning)) ()
3571                   :warning))))
3572   (assert (eq :warning
3573               (handler-case
3574                   (compile nil `(lambda (x)
3575                                   (list (vector-push-extend (svref x 0) x))))
3576                 ((and warning (not style-warning)) ()
3577                   :warning)))))
3578
3579 (with-test (:name :bug-646796)
3580   (assert 42
3581           (funcall
3582            (compile nil
3583                     `(lambda ()
3584                        (load-time-value (the (values fixnum) 42)))))))
3585
3586 (with-test (:name :bug-654289)
3587   ;; Test that compile-times don't explode when quoted constants
3588   ;; get big.
3589   (labels ((time-n (n)
3590              (gc :full t) ; Let's not confuse the issue with GC
3591              (let* ((tree (make-tree (expt 10 n) nil))
3592                     (t0 (get-internal-run-time))
3593                     (f (compile nil `(lambda (x) (eq x (quote ,tree)))))
3594                     (t1 (get-internal-run-time)))
3595                (assert (funcall f tree))
3596                (- t1 t0)))
3597            (make-tree (n acc)
3598              (cond ((zerop n) acc)
3599                    (t (make-tree (1- n) (cons acc acc))))))
3600     (let* ((times (loop for i from 0 upto 4
3601                         collect (time-n i)))
3602            (max-small (reduce #'max times :end 3))
3603            (max-big (reduce #'max times :start 3)))
3604       ;; This way is hopefully fairly CPU-performance insensitive.
3605       (unless (> (+ (truncate internal-time-units-per-second 10)
3606                     (* 2 max-small))
3607                  max-big)
3608         (error "Bad scaling or test? ~S" times)))))
3609
3610 (with-test (:name :bug-309063)
3611   (let ((fun (compile nil `(lambda (x)
3612                              (declare (type (integer 0 0) x))
3613                              (ash x 100)))))
3614     (assert (zerop (funcall fun 0)))))
3615
3616 (with-test (:name :bug-655872)
3617   (let ((f (compile nil `(lambda (x)
3618                            (declare (optimize (safety 3)))
3619                            (aref (locally (declare (optimize (safety 0)))
3620                                    (coerce x '(simple-vector 128)))
3621                                  60))))
3622         (long (make-array 100 :element-type 'fixnum)))
3623     (dotimes (i 100)
3624       (setf (aref long i) i))
3625     ;; 1. COERCE doesn't check the length in unsafe code.
3626     (assert (eql 60 (funcall f long)))
3627     ;; 2. The compiler doesn't trust the length from COERCE
3628     (assert (eq :caught
3629                 (handler-case
3630                     (funcall f (list 1 2 3))
3631                   (sb-int:invalid-array-index-error (e)
3632                     (assert (eql 60 (type-error-datum e)))
3633                     (assert (equal '(integer 0 (3)) (type-error-expected-type e)))
3634                     :caught))))))
3635
3636 (with-test (:name :bug-655203-regression)
3637   (let ((fun (compile nil
3638                       `(LAMBDA (VARIABLE)
3639                          (LET ((CONTINUATION
3640                                 (LAMBDA
3641                                     (&OPTIONAL DUMMY &REST OTHER)
3642                                   (DECLARE (IGNORE OTHER))
3643                                   (PRIN1 DUMMY)
3644                                   (PRIN1 VARIABLE))))
3645                            (FUNCALL CONTINUATION (LIST 1 2)))))))
3646     ;; This used to signal a bogus type-error.
3647     (assert (equal (with-output-to-string (*standard-output*)
3648                      (funcall fun t))
3649                    "(1 2)T"))))
3650
3651 (with-test (:name :constant-concatenate-compile-time)
3652   (flet ((make-lambda (n)
3653            `(lambda (x)
3654               (declare (optimize (speed 3) (space 0)))
3655               (concatenate 'string x ,(make-string n)))))
3656     (let* ((l0 (make-lambda 1))
3657            (l1 (make-lambda 10))
3658            (l2 (make-lambda 100))
3659            (l3 (make-lambda 1000))
3660            (t0 (get-internal-run-time))
3661            (f0 (compile nil l0))
3662            (t1 (get-internal-run-time))
3663            (f1 (compile nil l1))
3664            (t2 (get-internal-run-time))
3665            (f2 (compile nil l2))
3666            (t3 (get-internal-run-time))
3667            (f3 (compile nil l3))
3668            (t4 (get-internal-run-time))
3669            (d0 (- t1 t0))
3670            (d1 (- t2 t1))
3671            (d2 (- t3 t2))
3672            (d3 (- t4 t3))
3673            (short-avg (/ (+ d0 d1 d2) 3)))
3674       (assert (and f1 f2 f3))
3675       (assert (< d3 (* 10 short-avg))))))
3676
3677 (with-test (:name :bug-384892)
3678   (assert (equal
3679            '(function (fixnum fixnum &key (:k1 (member nil t)))
3680              (values (member t) &optional))
3681            (sb-kernel:%simple-fun-type
3682             (compile nil `(lambda (x y &key k1)
3683                             (declare (fixnum x y))
3684                             (declare (boolean k1))
3685                             (declare (ignore x y k1))
3686                             t))))))
3687
3688 (with-test (:name :bug-309448)
3689   ;; Like all tests trying to verify that something doesn't blow up
3690   ;; compile-times this is bound to be a bit brittle, but at least
3691   ;; here we try to establish a decent baseline.
3692   (flet ((time-it (lambda want)
3693            (let* ((start (get-internal-run-time))
3694                   (fun (compile nil lambda))
3695                   (end (get-internal-run-time))
3696                   (got (funcall fun)))
3697              (unless (eql want got)
3698                (error "wanted ~S, got ~S" want got))
3699              (- end start))))
3700     (let ((time-1/simple
3701            ;; This is mostly identical as the next one, but doesn't create
3702            ;; hairy unions of numeric types.
3703            (time-it `(lambda ()
3704                        (labels ((bar (baz bim)
3705                                   (let ((n (+ baz bim)))
3706                                  (* n (+ n 1) bim))))
3707                       (let ((a (bar 1 1))
3708                             (b (bar 1 1))
3709                             (c (bar 1 1)))
3710                         (- (+ a b) c))))
3711                     6))
3712           (time-1/hairy
3713            (time-it `(lambda ()
3714                        (labels ((bar (baz bim)
3715                                   (let ((n (+ baz bim)))
3716                                  (* n (+ n 1) bim))))
3717                       (let ((a (bar 1 1))
3718                             (b (bar 1 5))
3719                             (c (bar 1 15)))
3720                         (- (+ a b) c))))
3721                     -3864)))
3722       (assert (>= (* 10 (1+ time-1/simple)) time-1/hairy)))
3723     (let ((time-2/simple
3724            ;; This is mostly identical as the next one, but doesn't create
3725            ;; hairy unions of numeric types.
3726            (time-it `(lambda ()
3727                        (labels ((sum-d (n)
3728                                   (let ((m (truncate 999 n)))
3729                                     (/ (* n m (1+ m)) 2))))
3730                          (- (+ (sum-d 3)
3731                                (sum-d 3))
3732                             (sum-d 3))))
3733                     166833))
3734           (time-2/hairy
3735            (time-it `(lambda ()
3736                        (labels ((sum-d (n)
3737                                   (let ((m (truncate 999 n)))
3738                                     (/ (* n m (1+ m)) 2))))
3739                          (- (+ (sum-d 3)
3740                                (sum-d 5))
3741                             (sum-d 15))))
3742                     233168)))
3743       (assert (>= (* 10 (1+ time-2/simple)) time-2/hairy)))))
3744
3745 (with-test (:name :regression-1.0.44.34)
3746   (compile nil '(lambda (z &rest args)
3747                  (declare (dynamic-extent args))
3748                  (flet ((foo (w v) (list v w)))
3749                    (setq z 0)
3750                    (flet ((foo ()
3751                             (foo z args)))
3752                      (declare (sb-int:truly-dynamic-extent #'foo))
3753                      (call #'foo nil))))))
3754
3755 (with-test (:name :bug-713626)
3756   (let ((f (eval '(constantly 42))))
3757     (handler-bind ((warning #'error))
3758       (assert (= 42 (funcall (compile nil `(lambda () (funcall ,f 1 2 3)))))))))
3759
3760 (with-test (:name :known-fun-allows-other-keys)
3761   (handler-bind ((warning #'error))
3762     (funcall (compile nil '(lambda () (directory "." :allow-other-keys t))))
3763     (funcall (compile nil `(lambda () (directory "." :bar t :allow-other-keys t))))))
3764
3765 (with-test (:name :bug-551227)
3766   ;; This function causes constraint analysis to perform a
3767   ;; ref-substitution that alters the A referred to in (G A) at in the
3768   ;; consequent of the IF to refer to be NUMBER, from the
3769   ;; LET-converted inline-expansion of MOD.  This leads to attempting
3770   ;; to CLOSE-OVER a variable that simply isn't in scope when it is
3771   ;; referenced.
3772   (compile nil '(lambda (a)
3773                   (if (let ((s a))
3774                         (block :block
3775                           (map nil
3776                                (lambda (e)
3777                                  (return-from :block
3778                                    (f (mod a e))))
3779                                s)))
3780                       (g a)))))
3781
3782 (with-test (:name :funcall-lambda-inlined)
3783   (assert (not
3784            (ctu:find-code-constants
3785             (compile nil
3786                      `(lambda (x y)
3787                         (+ x (funcall (lambda (z) z) y))))
3788             :type 'function))))
3789
3790 (with-test (:name :bug-720382)
3791   (let ((w 0))
3792     (let ((f
3793            (handler-bind (((and warning (not style-warning))
3794                            (lambda (c) (incf w))))
3795              (compile nil `(lambda (b) ((lambda () b) 1))))))
3796       (assert (= w 1))
3797       (assert (eq :error
3798                   (handler-case (funcall f 0)
3799                     (error () :error)))))))
3800
3801 (with-test (:name :multiple-args-to-function)
3802   (let ((form `(flet ((foo (&optional (x 13)) x))
3803                  (funcall (function foo 42))))
3804         (*evaluator-mode* :interpret))
3805     (assert (eq :error
3806                 (handler-case (eval form)
3807                   (error () :error))))
3808     (multiple-value-bind (fun warn fail)
3809         (compile nil `(lambda () ,form))
3810       (assert (and warn fail))
3811           (assert (eq :error
3812                       (handler-case (funcall fun)
3813                         (error () :error)))))))
3814
3815 ;;; This doesn't test LVAR-FUN-IS directly, but captures it
3816 ;;; pretty accurately anyways.
3817 (with-test (:name :lvar-fun-is)
3818   (dolist (fun (list
3819                 (lambda (x) (member x x :test #'eq))
3820                 (lambda (x) (member x x :test 'eq))
3821                 (lambda (x) (member x x :test #.#'eq))))
3822     (assert (equal (list #'sb-kernel:%member-eq)
3823                    (ctu:find-named-callees fun))))
3824   (dolist (fun (list
3825                 (lambda (x)
3826                   (declare (notinline eq))
3827                   (member x x :test #'eq))
3828                 (lambda (x)
3829                   (declare (notinline eq))
3830                   (member x x :test 'eq))
3831                 (lambda (x)
3832                   (declare (notinline eq))
3833                   (member x x :test #.#'eq))))
3834     (assert (member #'sb-kernel:%member-test
3835                     (ctu:find-named-callees fun)))))
3836
3837 (with-test (:name :delete-to-delq-opt)
3838   (dolist (fun (list (lambda (x y)
3839                        (declare (list y))
3840                        (delete x y :test #'eq))
3841                      (lambda (x y)
3842                        (declare (fixnum x) (list y))
3843                        (delete x y))
3844                      (lambda (x y)
3845                        (declare (symbol x) (list y))
3846                        (delete x y :test #'eql))))
3847     (assert (equal (list #'sb-int:delq)
3848                    (ctu:find-named-callees fun)))))
3849
3850 (with-test (:name :bug-767959)
3851   ;; This used to signal an error.
3852   (compile nil `(lambda ()
3853                   (declare (optimize sb-c:store-coverage-data))
3854                   (assoc
3855                    nil
3856                    '((:ordinary . ordinary-lambda-list))))))
3857
3858 (with-test (:name :member-on-long-constant-list)
3859   ;; This used to blow stack with a sufficiently long list.
3860   (let ((cycle (list t)))
3861     (nconc cycle cycle)
3862     (compile nil `(lambda (x)
3863                     (member x ',cycle)))))
3864
3865 (with-test (:name :bug-722734)
3866   (assert (raises-error?
3867             (funcall (compile
3868                       nil
3869                       '(lambda ()
3870                         (eql (make-array 6)
3871                          (list unbound-variable-1 unbound-variable-2))))))))
3872
3873 (with-test (:name :bug-771673)
3874   (assert (equal `(the foo bar) (macroexpand `(truly-the foo bar))))
3875   ;; Make sure the compiler doesn't use THE, and check that setf-expansions
3876   ;; work.
3877   (let ((f (compile nil `(lambda (x y)
3878                            (setf (truly-the fixnum (car x)) y)))))
3879     (let* ((cell (cons t t)))
3880       (funcall f cell :ok)
3881       (assert (equal '(:ok . t) cell)))))
3882
3883 (with-test (:name (:bug-793771 +))
3884   (let ((f (compile nil `(lambda (x y)
3885                             (declare (type (single-float 2.0) x)
3886                                      (type (single-float (0.0)) y))
3887                            (+ x y)))))
3888     (assert (equal `(function ((single-float 2.0) (single-float (0.0)))
3889                               (values (single-float 2.0) &optional))
3890                    (sb-kernel:%simple-fun-type f)))))
3891
3892 (with-test (:name (:bug-793771 -))
3893   (let ((f (compile nil `(lambda (x y)
3894                             (declare (type (single-float * 2.0) x)
3895                                      (type (single-float (0.0)) y))
3896                            (- x y)))))
3897     (assert (equal `(function ((single-float * 2.0) (single-float (0.0)))
3898                               (values (single-float * 2.0) &optional))
3899                    (sb-kernel:%simple-fun-type f)))))
3900
3901 (with-test (:name (:bug-793771 *))
3902   (let ((f (compile nil `(lambda (x)
3903                             (declare (type (single-float (0.0)) x))
3904                            (* x 0.1)))))
3905     (assert (equal `(function ((single-float (0.0)))
3906                               (values (or (member 0.0) (single-float (0.0))) &optional))
3907                    (sb-kernel:%simple-fun-type f)))))
3908
3909 (with-test (:name (:bug-793771 /))
3910   (let ((f (compile nil `(lambda (x)
3911                             (declare (type (single-float (0.0)) x))
3912                            (/ x 3.0)))))
3913     (assert (equal `(function ((single-float (0.0)))
3914                               (values (or (member 0.0) (single-float (0.0))) &optional))
3915                    (sb-kernel:%simple-fun-type f)))))
3916
3917 (with-test (:name (:bug-486812 single-float))
3918   (compile nil `(lambda ()
3919                   (sb-kernel:make-single-float -1))))
3920
3921 (with-test (:name (:bug-486812 double-float))
3922   (compile nil `(lambda ()
3923                   (sb-kernel:make-double-float -1 0))))
3924
3925 (with-test (:name :bug-729765)
3926   (compile nil `(lambda (a b)
3927                   (declare ((integer 1 1) a)
3928                            ((integer 0 1) b)
3929                            (optimize debug))
3930                   (lambda () (< b a)))))
3931
3932 ;; Actually tests the assembly of RIP-relative operands to comparison
3933 ;; functions (one of the few x86 instructions that have extra bytes
3934 ;; *after* the mem operand's effective address, resulting in a wrong
3935 ;; offset).
3936 (with-test (:name :cmpps)
3937   (let ((foo (compile nil `(lambda (x)
3938                              (= #C(2.0 3.0) (the (complex single-float) x))))))
3939     (assert (funcall foo #C(2.0 3.0)))
3940     (assert (not (funcall foo #C(1.0 2.0))))))
3941
3942 (with-test (:name :cmppd)
3943   (let ((foo (compile nil `(lambda (x)
3944                              (= #C(2d0 3d0) (the (complex double-float) x))))))
3945     (assert (funcall foo #C(2d0 3d0)))
3946     (assert (not (funcall foo #C(1d0 2d0))))))
3947
3948 (with-test (:name :lvar-externally-checkable-type-nil)
3949   ;; Used to signal a BUG during compilation.
3950   (let ((fun (compile nil `(lambda (a) (parse-integer "12321321" (the (member :start) a) 1)))))
3951     (multiple-value-bind (i p) (funcall fun :start)
3952       (assert (= 2321321 i))
3953       (assert (= 8 p)))
3954     (multiple-value-bind (i e) (ignore-errors (funcall fun :end))
3955       (assert (not i))
3956       (assert (typep e 'type-error)))))
3957
3958 (with-test (:name :simple-type-error-in-bound-propagation-a)
3959   (compile nil `(lambda (i)
3960                   (declare (unsigned-byte i))
3961                   (expt 10 (expt 7 (- 2 i))))))
3962
3963 (with-test (:name :simple-type-error-in-bound-propagation-b)
3964   (assert (equal `(FUNCTION (UNSIGNED-BYTE)
3965                             (VALUES (SINGLE-FLOAT -1F0 1F0) &OPTIONAL))
3966                  (sb-kernel:%simple-fun-type
3967                   (compile nil `(lambda (i)
3968                                   (declare (unsigned-byte i))
3969                                   (cos (expt 10 (+ 4096 i)))))))))
3970
3971 (with-test (:name :fixed-%more-arg-values)
3972   (let ((fun (compile nil `(lambda (&rest rest)
3973                              (declare (optimize (safety 0)))
3974                              (apply #'cons rest)))))
3975     (assert (equal '(car . cdr) (funcall fun 'car 'cdr)))))
3976
3977 (with-test (:name :bug-826970)
3978   (let ((fun (compile nil `(lambda (a b c)
3979                              (declare (type (member -2 1) b))
3980                              (array-in-bounds-p a 4 b c)))))
3981     (assert (funcall fun (make-array '(5 2 2)) 1 1))))
3982
3983 (with-test (:name :bug-826971)
3984   (let* ((foo "foo")
3985          (fun (compile nil `(lambda (p1 p2)
3986                               (schar (the (eql ,foo) p1) p2)))))
3987     (assert (eql #\f (funcall fun foo 0)))))
3988
3989 (with-test (:name :bug-738464)
3990   (multiple-value-bind (fun warn fail)
3991       (compile nil `(lambda ()
3992                       (flet ((foo () 42))
3993                         (declare (ftype non-function-type foo))
3994                         (foo))))
3995     (assert (eql 42 (funcall fun)))
3996     (assert (and warn (not fail)))))
3997
3998 (with-test (:name :bug-832005)
3999   (let ((fun (compile nil `(lambda (x)
4000                              (declare (type (complex single-float) x))
4001                              (+ #C(0.0 1.0) x)))))
4002     (assert (= (funcall fun #C(1.0 2.0))
4003                #C(1.0 3.0)))))
4004
4005 ;; A refactoring  1.0.12.18 caused lossy computation of primitive
4006 ;; types for member types.
4007 (with-test (:name :member-type-primitive-type)
4008   (let ((fun (compile nil `(lambda (p1 p2 p3)
4009                              (if p1
4010                                  (the (member #c(1.2d0 1d0)) p2)
4011                                  (the (eql #c(1.0 1.0)) p3))))))
4012     (assert (eql (funcall fun 1 #c(1.2d0 1d0) #c(1.0 1.0))
4013                  #c(1.2d0 1.0d0)))))
4014
4015 ;; Fall-through jump elimination made control flow fall through to trampolines.
4016 ;; Reported by Eric Marsden on sbcl-devel@ 2011.10.26, with a test case
4017 ;; reproduced below (triggered a corruption warning and a memory fault).
4018 (with-test (:name :bug-883500)
4019   (funcall (compile nil `(lambda (a)
4020                            (declare (type (integer -50 50) a))
4021                            (declare (optimize (speed 0)))
4022                            (mod (mod a (min -5 a)) 5)))
4023            1))
4024
4025 ;; Test for literals too large for the ISA (e.g. (SIGNED-BYTE 13) on SPARC).
4026 #+sb-unicode
4027 (with-test (:name :bug-883519)
4028   (compile nil `(lambda (x)
4029                   (declare (type character x))
4030                   (eql x #\U0010FFFF))))
4031
4032 ;; Wide fixnum platforms had buggy address computation in atomic-incf/aref
4033 (with-test (:name :bug-887220)
4034   (let ((incfer (compile
4035                  nil
4036                  `(lambda (vector index)
4037                     (declare (type (simple-array sb-ext:word (4))
4038                                    vector)
4039                              (type (mod 4) index))
4040                     (sb-ext:atomic-incf (aref vector index) 1)
4041                     vector))))
4042     (assert (equalp (funcall incfer
4043                              (make-array 4 :element-type 'sb-ext:word
4044                                            :initial-element 0)
4045                              1)
4046                     #(0 1 0 0)))))