1 ;;;; type testing and checking VOPs for the x86 VM
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 ;;;; test generation utilities
16 ;;; Emit the most compact form of the test immediate instruction,
17 ;;; using an 8 bit test when the immediate is only 8 bits and the
18 ;;; value is one of the four low registers (eax, ebx, ecx, edx) or the
20 (defun generate-fixnum-test (value)
21 (let ((offset (tn-offset value)))
22 (cond ((and (sc-is value any-reg descriptor-reg)
23 (or (= offset eax-offset) (= offset ebx-offset)
24 (= offset ecx-offset) (= offset edx-offset)))
25 (inst test (make-random-tn :kind :normal
26 :sc (sc-or-lose 'byte-reg)
29 ((sc-is value control-stack)
30 (inst test (make-ea :byte :base ebp-tn
31 :disp (- (* (1+ offset) n-word-bytes)))
34 (inst test value 3)))))
36 (defun %test-fixnum (value target not-p)
37 (generate-fixnum-test value)
38 (inst jmp (if not-p :nz :z) target))
40 (defun %test-fixnum-and-headers (value target not-p headers)
41 (let ((drop-through (gen-label)))
42 (generate-fixnum-test value)
43 (inst jmp :z (if not-p drop-through target))
44 (%test-headers value target not-p nil headers drop-through)))
46 (defun %test-immediate (value target not-p immediate)
47 ;; Code a single instruction byte test if possible.
48 (let ((offset (tn-offset value)))
49 (cond ((and (sc-is value any-reg descriptor-reg)
50 (or (= offset eax-offset) (= offset ebx-offset)
51 (= offset ecx-offset) (= offset edx-offset)))
52 (inst cmp (make-random-tn :kind :normal
53 :sc (sc-or-lose 'byte-reg)
58 (inst cmp al-tn immediate))))
59 (inst jmp (if not-p :ne :e) target))
61 (defun %test-lowtag (value target not-p lowtag &optional al-loaded)
64 (inst and al-tn lowtag-mask))
65 ;; FIXME: another 'optimization' which doesn't appear to work:
66 ;; prefetching the hypothetically pointed-to version should help,
67 ;; but this is in fact non-ideal in plenty of ways: we emit way too
68 ;; many of these prefetch instructions; pointed-to objects are very
69 ;; often in the cache anyway; etc. etc. Still, as proof-of-concept,
70 ;; not too bad. -- CSR, 2004-07-27
71 (when (member :prefetch *backend-subfeatures*)
72 (inst prefetchnta (make-ea :byte :base value :disp (- lowtag))))
73 (inst cmp al-tn lowtag)
74 (inst jmp (if not-p :ne :e) target))
76 (defun %test-headers (value target not-p function-p headers
77 &optional (drop-through (gen-label)) al-loaded)
78 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
79 (multiple-value-bind (equal less-or-equal greater-or-equal when-true when-false)
80 ;; EQUAL, LESS-OR-EQUAL and GREATER-OR-EQUAL are the conditions for
81 ;; branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
82 ;; labels to branch to when we know it's true and when we know
83 ;; it's false respectively.
85 (values :ne :a :b drop-through target)
86 (values :e :na :nb target drop-through))
87 (%test-lowtag value when-false t lowtag al-loaded)
89 ((and (null (cdr headers))
90 (numberp (car headers)))
91 ;; Optimize the common case: referencing the value from memory
92 ;; is slightly smaller than loading it and then doing the
93 ;; comparison. Doing this for other cases (e.g. range of
94 ;; [BIGNUM-WIDETAG..FOO-WIDETAG]) is also possible, but such
95 ;; opportunities don't come up very often and the code would
96 ;; get pretty hairy...
97 (inst cmp (make-ea :byte :base value :disp (- lowtag)) (car headers))
98 (inst jmp equal target))
100 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
101 (do ((remaining headers (cdr remaining)))
103 (let ((header (car remaining))
104 (last (null (cdr remaining))))
108 ((and (not last) (null (cddr remaining))
109 (atom (cadr remaining))
110 (= (logcount (logxor header (cadr remaining))) 1))
111 ;; BASE-STRING, (VECTOR NIL), BIT-VECTOR, (VECTOR T)
112 (inst and al-tn (ldb (byte 8 0) (logeqv header (cadr remaining))))
113 (inst cmp al-tn (ldb (byte 8 0) (logand header (cadr remaining))))
114 (inst jmp equal target)
117 (inst cmp al-tn header)
119 (inst jmp equal target)
120 (inst jmp :e when-true)))))
122 (let ((start (car header))
125 ;; LAST = don't need al-tn later
126 ((and last (not (= start bignum-widetag))
127 (= (+ start 4) end) (= (logcount (logxor start end)) 1))
129 (inst and al-tn (ldb (byte 8 0) (logeqv start end)))
130 (inst cmp al-tn (ldb (byte 8 0) (logand start end)))
131 (inst jmp equal target))
132 ((and (not last) (null (cddr remaining))
133 (= (+ start 4) end) (= (logcount (logxor start end)) 1)
134 (listp (cadr remaining))
135 (= (+ (caadr remaining) 4) (cdadr remaining))
136 (= (logcount (logxor (caadr remaining) (cdadr remaining))) 1)
137 (= (logcount (logxor (caadr remaining) start)) 1))
139 (inst and al-tn (ldb (byte 8 0) (logeqv start (cdadr remaining))))
140 (inst cmp al-tn (ldb (byte 8 0) (logand start (cdadr remaining))))
141 (inst jmp equal target)
142 ;; we've shortcircuited the DO, so we must return.
143 ;; It's OK to do so, because (NULL (CDDR REMAINING))
147 (unless (= start bignum-widetag)
148 (inst cmp al-tn start)
149 (if (= end complex-array-widetag)
152 (inst jmp greater-or-equal target))
153 (inst jmp :b when-false))) ; was :l
154 (unless (= end complex-array-widetag)
157 (inst jmp less-or-equal target)
158 (inst jmp :be when-true)))))))))))) ; was :le
159 (emit-label drop-through))))
161 ;;;; type checking and testing
163 (define-vop (check-type)
164 (:args (value :target result :scs (any-reg descriptor-reg)))
165 (:results (result :scs (any-reg descriptor-reg)))
166 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
169 (:save-p :compute-only))
171 (define-vop (type-predicate)
172 (:args (value :scs (any-reg descriptor-reg)))
173 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
177 (:policy :fast-safe))
179 ;;; simpler VOP that don't need a temporary register
180 (define-vop (simple-check-type)
181 (:args (value :target result :scs (any-reg descriptor-reg)))
182 (:results (result :scs (any-reg descriptor-reg)
183 :load-if (not (and (sc-is value any-reg descriptor-reg)
184 (sc-is result control-stack)))))
186 (:save-p :compute-only))
188 (define-vop (simple-type-predicate)
189 (:args (value :scs (any-reg descriptor-reg control-stack)))
192 (:policy :fast-safe))
194 (defun cost-to-test-types (type-codes)
195 (+ (* 2 (length type-codes))
196 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
198 (defmacro !define-type-vops (pred-name check-name ptype error-code
200 &key (variant nil variant-p) &allow-other-keys)
201 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
203 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
204 (prefix (if variant-p
205 (concatenate 'string (string variant) "-")
209 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
210 (:translate ,pred-name)
212 (test-type value target not-p (,@type-codes))))))
214 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
217 (generate-error-code vop ,error-code value)))
218 (test-type value err-lab t (,@type-codes))
219 (move result value))))))
221 `((primitive-type-vop ,check-name (:check) ,ptype))))))
223 ;;;; other integer ranges
225 (define-vop (fixnump/unsigned-byte-32 simple-type-predicate)
226 (:args (value :scs (unsigned-reg)))
227 (:arg-types unsigned-num)
230 (inst cmp value #.sb!xc:most-positive-fixnum)
231 (inst jmp (if not-p :a :be) target)))
233 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
234 ;;; exactly one digit.
236 (define-vop (signed-byte-32-p type-predicate)
237 (:translate signed-byte-32-p)
239 (multiple-value-bind (yep nope)
241 (values not-target target)
242 (values target not-target))
243 (generate-fixnum-test value)
246 (inst and al-tn lowtag-mask)
247 (inst cmp al-tn other-pointer-lowtag)
249 (loadw eax-tn value 0 other-pointer-lowtag)
250 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
251 (inst jmp (if not-p :ne :e) target))
254 (define-vop (check-signed-byte-32 check-type)
256 (let ((nope (generate-error-code vop
257 object-not-signed-byte-32-error
259 (generate-fixnum-test value)
262 (inst and al-tn lowtag-mask)
263 (inst cmp al-tn other-pointer-lowtag)
265 (loadw eax-tn value 0 other-pointer-lowtag)
266 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
269 (move result value)))
271 ;;; An (unsigned-byte 32) can be represented with either a positive
272 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
273 ;;; exactly two digits and the second digit all zeros.
274 (define-vop (unsigned-byte-32-p type-predicate)
275 (:translate unsigned-byte-32-p)
277 (let ((not-target (gen-label))
278 (single-word (gen-label))
279 (fixnum (gen-label)))
280 (multiple-value-bind (yep nope)
282 (values not-target target)
283 (values target not-target))
285 (generate-fixnum-test value)
289 ;; If not, is it an other pointer?
290 (inst and al-tn lowtag-mask)
291 (inst cmp al-tn other-pointer-lowtag)
294 (loadw eax-tn value 0 other-pointer-lowtag)
296 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
297 (inst jmp :e single-word)
298 ;; If it's other than two, we can't be an (unsigned-byte 32)
299 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
301 ;; Get the second digit.
302 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
303 ;; All zeros, its an (unsigned-byte 32).
304 (inst or eax-tn eax-tn)
308 (emit-label single-word)
309 ;; Get the single digit.
310 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
312 ;; positive implies (unsigned-byte 32).
314 (inst or eax-tn eax-tn)
315 (inst jmp (if not-p :s :ns) target)
317 (emit-label not-target)))))
319 (define-vop (check-unsigned-byte-32 check-type)
322 (generate-error-code vop object-not-unsigned-byte-32-error value))
325 (single-word (gen-label)))
328 (generate-fixnum-test value)
332 ;; If not, is it an other pointer?
333 (inst and al-tn lowtag-mask)
334 (inst cmp al-tn other-pointer-lowtag)
337 (loadw eax-tn value 0 other-pointer-lowtag)
339 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
340 (inst jmp :e single-word)
341 ;; If it's other than two, we can't be an (unsigned-byte 32)
342 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
344 ;; Get the second digit.
345 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
346 ;; All zeros, its an (unsigned-byte 32).
347 (inst or eax-tn eax-tn)
351 (emit-label single-word)
352 ;; Get the single digit.
353 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
355 ;; positive implies (unsigned-byte 32).
357 (inst or eax-tn eax-tn)
361 (move result value))))
363 ;;;; list/symbol types
365 ;;; symbolp (or symbol (eq nil))
366 ;;; consp (and list (not (eq nil)))
368 (define-vop (symbolp type-predicate)
371 (let ((is-symbol-label (if not-p drop-thru target)))
372 (inst cmp value nil-value)
373 (inst jmp :e is-symbol-label)
374 (test-type value target not-p (symbol-header-widetag)))
377 (define-vop (check-symbol check-type)
379 (let ((error (generate-error-code vop object-not-symbol-error value)))
380 (inst cmp value nil-value)
381 (inst jmp :e drop-thru)
382 (test-type value error t (symbol-header-widetag)))
384 (move result value)))
386 (define-vop (consp type-predicate)
389 (let ((is-not-cons-label (if not-p target drop-thru)))
390 (inst cmp value nil-value)
391 (inst jmp :e is-not-cons-label)
392 (test-type value target not-p (list-pointer-lowtag)))
395 (define-vop (check-cons check-type)
397 (let ((error (generate-error-code vop object-not-cons-error value)))
398 (inst cmp value nil-value)
400 (test-type value error t (list-pointer-lowtag))
401 (move result value))))