forked from sbcl/sbcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler-slow.pure.lisp
246 lines (230 loc) · 9.35 KB
/
compiler-slow.pure.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
(with-test (:name (compile eval the type-error))
(checked-compile-and-assert (:optimize :safe)
'(lambda (v)
(list (the fixnum (the (real 0) (eval v)))))
((0.1) (condition 'type-error))
((-1) (condition 'type-error))))
(defun pick-acceptable-default (specifier)
(let ((parse (sb-kernel:specifier-type specifier)))
; (format t "~&testcase: ~s~%" specifier)
(typecase parse
(sb-kernel:character-set-type #\a)
(sb-kernel:numeric-type
(cond ((eq (sb-kernel:numeric-type-class parse) 'float)
(ecase (sb-kernel:numeric-type-complexp parse)
(:real
(ecase (sb-kernel:numeric-type-format parse)
(single-float 1009f0)
(double-float pi)))
(:complex
(ecase (sb-kernel:numeric-type-format parse)
(single-float #c(101f0 -1f0))
(double-float #c(2d0 3.5d0))))))
(t
1)))
(t
(cond ((equal specifier '(or (eql 1.0d0) (eql 10.0d0))) ; KLUDGE
1.0d0)
((equal specifier '(member 1 2 10))
2)
((equal specifier '(complex (member 10.0 20.0)))
(complex 10.0 10.0))
(t
'whatever))))))
(with-test (:name :array-type-predicates)
(dolist (et (list* '(integer -1 200) '(integer -256 1)
'(integer 0 128)
'(integer 0 (128))
'(double-float 0d0 (1d0))
'(single-float (0s0) (1s0))
'(or (eql 1d0) (eql 10d0))
'(member 1 2 10)
'(complex (member 10 20))
'(complex (member 10d0 20d0))
'(complex (member 10s0 20s0))
'(or integer double-float)
'(mod 1)
'(member #\a #\b)
'(eql #\a)
#+sb-unicode 'extended-char
#+sb-unicode '(eql #\cyrillic_small_letter_yu)
(map 'list 'sb-vm:saetp-specifier
sb-vm:*specialized-array-element-type-properties*)))
(when et
(let* ((v (make-array 3 :element-type et
;; Pick an initial element because of the (ELT ,v 0)
:initial-element (pick-acceptable-default et))))
(checked-compile-and-assert ()
`(lambda ()
(list (if (typep ,v '(simple-array ,et (*)))
:good
',et)
(if (typep (elt ,v 0) '(simple-array ,et (*)))
',et
:good)))
(() '(:good :good)))))))
(with-test (:name (compile equal equalp :transforms))
(let* ((s "foo")
(bit-vector #*11001100)
(values `(nil 1 2 "test"
;; Floats duplicated here to ensure we get newly created instances
(read-from-string "1.1") (read-from-string "1.2d0")
(read-from-string "1.1") (read-from-string "1.2d0")
1.1 1.2d0 '("foo" "bar" "test")
#(1 2 3 4) #*101010 (make-broadcast-stream) #p"/tmp/file"
,s (copy-seq ,s) ,bit-vector (copy-seq ,bit-vector)
,(make-hash-table) #\a #\b #\A #\C
,(make-random-state) 1/2 2/3)))
(dolist (predicate '(equal equalp))
;; Test all permutations of different types
(loop for x in values
do (loop for y in values
do (checked-compile-and-assert (:optimize nil)
`(lambda (x y)
(,predicate (the ,(type-of x) x)
(the ,(type-of y) y)))
((x y) (funcall predicate x y)))))
(checked-compile-and-assert ()
`(lambda (x y)
(,predicate (the (cons (or simple-bit-vector simple-base-string))
x)
(the (cons (or (and bit-vector (not simple-array))
(simple-array character (*))))
y)))
(((list (string 'list)) (list "LIST")) t)))))
(with-test (:name (sb-c::mask-signed-field :randomized))
(let (result)
(dotimes (i 1000)
(let* ((ool (checked-compile '(lambda (s i)
(sb-c::mask-signed-field s i))))
(size (random (* sb-vm:n-word-bits 2)))
(constant (checked-compile `(lambda (i)
(sb-c::mask-signed-field ,size i))))
(arg (- (random (* most-positive-fixnum 8)) (* most-positive-fixnum 4)))
(declared (checked-compile `(lambda (i)
(declare (type (integer ,(- (abs arg)) ,(abs arg)) i))
(sb-c::mask-signed-field ,size i))))
(ool-answer (funcall ool size arg))
(constant-answer (funcall constant arg))
(declared-answer (funcall declared arg)))
(unless (= ool-answer constant-answer declared-answer)
(push (list size arg ool-answer constant-answer declared-answer) result))))
(assert (null result))))
(with-test (:name (multiple-value-call :type-checking-rest))
(checked-compile-and-assert (:allow-warnings t
:optimize :safe)
`(lambda (list)
(multiple-value-call
(lambda (&optional a &rest r)
(declare ((satisfies eval) r)
(ignore r))
(list a))
(values-list list)))
(('(1 list 2)) '(1))
(('(1)) (condition 'type-error))))
(with-test (:name (multiple-value-call :type-checking-rest.2))
(checked-compile-and-assert (:allow-warnings t
:optimize :safe)
`(lambda (list)
(multiple-value-call
(lambda (&optional a &rest r)
(declare (null r)
(ignore r))
(list a))
(values-list list)))
(('(1 list 2)) (condition 'type-error))
(('(1)) '(1))))
(with-test (:name (multiple-value-call :type-checking-rest :type-derivation))
(checked-compile-and-assert (:allow-warnings t
:optimize :safe)
`(lambda (list)
(multiple-value-call
(lambda (&optional a &rest r)
(declare (cons r)
(ignore r))
(list a))
(values-list list)))
(('(1 2)) '(1))
(('(1)) (condition 'type-error))))
(declaim (maybe-inline inline-recursive))
(defun inline-recursive (x)
(declare (muffle-conditions compiler-note
style-warning))
(if (zerop x)
x
(inline-recursive (1- x))))
(declaim (inline inline-recursive))
(with-test (:name :reanalyze-functionals-when-inlining)
(checked-compile-and-assert
()
`(lambda (x)
(inline-recursive x)
(inline-recursive x))
((5) 0)))
(with-test (:name :interval-div-zero)
(checked-compile-and-assert (:optimize :safe)
`(lambda (x y)
(truncate (the (integer 0 0) x)
(the (rational (1) (2)) y)))
((0 3/2) (values 0 0))))
(with-test (:name :float-quotient-rounding-errors
:skipped-on :x86) ;; x87 has different precision loss
(checked-compile-and-assert (:optimize :safe)
`(lambda ()
(floor -114658225103614 84619.58))
(() (values -1354984704 8388608.0)))
(checked-compile-and-assert (:optimize :safe)
`(lambda ()
(floor -302254842 50510.5))
(() (eval '(floor -302254842 50510.5))))
(checked-compile-and-assert (:optimize :safe)
`(lambda ()
(ceiling 114658225103614 84619.58))
(() (values 1354984704 -8388608.0)))
(checked-compile-and-assert (:optimize :safe)
`(lambda ()
(ceiling 285493348393 94189.93))
(() (values 3031040 -65536.0))))
(with-test (:name :check-function-designator-cast-key-lambda-var)
(checked-compile-and-assert
(:optimize '(:speed 3 :space 0))
`(lambda (p1 p4)
(declare (vector p1)
((member ,#'car "x" cdr) p4))
(stable-sort p1 #'<= :key p4))
(((vector '(2) '(3) '(1)) #'car) #((1) (2) (3)) :test #'equalp)))
(with-test (:name :functional-may-escape-p
;; INVALID-UNWIND-ERROR crashes fatally on ppc32. Not sure as of when.
:broken-on :ppc)
(checked-compile-and-assert
(:optimize :safe)
'(lambda ()
(let (x)
(block nil
(flet ((x () (let (*)
(return 33))))
(setf x #'x)))
(funcall x)))
(() (condition 'control-error))))
(with-test (:name :lvar-fun-type-on-literal-funs)
(checked-compile-and-assert
()
`(lambda (p)
(declare (type (or null string) p))
(locally (declare (optimize (space 0)))
(stable-sort p ,#'string<)))
(((copy-seq "acb")) "abc" :test #'equal)))
(with-test (:name :ir2-optimize-jumps-multiway-branch-if-eq-delete-branch)
(checked-compile-and-assert
()
`(lambda (a)
(declare (type (integer -345 1) a))
(case (ldb (byte 24 5) a)
((4 47 61 17 10 39) 1)
((2 7 55) A)
((42 48 16 33 40 20) A)
((60 54 28) 3)
((15 1 44 29 57 41 52) 32771)
((46 64 3 18 36 49 37) 1)
(t A)))
((-5) -5)))