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 (defun generate-fixnum-test (value)
17 (emit-optimized-test-inst value 3))
19 (defun %test-fixnum (value target not-p)
20 (generate-fixnum-test value)
21 (inst jmp (if not-p :nz :z) target))
23 (defun %test-fixnum-and-headers (value target not-p headers)
24 (let ((drop-through (gen-label)))
25 (generate-fixnum-test value)
26 (inst jmp :z (if not-p drop-through target))
27 (%test-headers value target not-p nil headers drop-through)))
29 (defun %test-immediate (value target not-p immediate)
30 ;; Code a single instruction byte test if possible.
31 (let ((offset (tn-offset value)))
32 (cond ((and (sc-is value any-reg descriptor-reg)
33 (or (= offset eax-offset) (= offset ebx-offset)
34 (= offset ecx-offset) (= offset edx-offset)))
35 (inst cmp (make-random-tn :kind :normal
36 :sc (sc-or-lose 'byte-reg)
41 (inst cmp al-tn immediate))))
42 (inst jmp (if not-p :ne :e) target))
44 (defun %test-lowtag (value target not-p lowtag &optional al-loaded)
47 (inst and al-tn lowtag-mask))
48 ;; FIXME: another 'optimization' which doesn't appear to work:
49 ;; prefetching the hypothetically pointed-to version should help,
50 ;; but this is in fact non-ideal in plenty of ways: we emit way too
51 ;; many of these prefetch instructions; pointed-to objects are very
52 ;; often in the cache anyway; etc. etc. Still, as proof-of-concept,
53 ;; not too bad. -- CSR, 2004-07-27
54 (when (member :prefetch *backend-subfeatures*)
55 (inst prefetchnta (make-ea :byte :base value :disp (- lowtag))))
56 (inst cmp al-tn lowtag)
57 (inst jmp (if not-p :ne :e) target))
59 (defun %test-headers (value target not-p function-p headers
60 &optional (drop-through (gen-label)) al-loaded)
61 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
62 (multiple-value-bind (equal less-or-equal greater-or-equal when-true when-false)
63 ;; EQUAL, LESS-OR-EQUAL and GREATER-OR-EQUAL are the conditions for
64 ;; branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
65 ;; labels to branch to when we know it's true and when we know
66 ;; it's false respectively.
68 (values :ne :a :b drop-through target)
69 (values :e :na :nb target drop-through))
70 (%test-lowtag value when-false t lowtag al-loaded)
72 ((and (null (cdr headers))
73 (numberp (car headers)))
74 ;; Optimize the common case: referencing the value from memory
75 ;; is slightly smaller than loading it and then doing the
76 ;; comparison. Doing this for other cases (e.g. range of
77 ;; [BIGNUM-WIDETAG..FOO-WIDETAG]) is also possible, but such
78 ;; opportunities don't come up very often and the code would
79 ;; get pretty hairy...
80 (inst cmp (make-ea :byte :base value :disp (- lowtag)) (car headers))
81 (inst jmp equal target))
83 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
84 (do ((remaining headers (cdr remaining)))
86 (let ((header (car remaining))
87 (last (null (cdr remaining))))
91 ((and (not last) (null (cddr remaining))
92 (atom (cadr remaining))
93 (= (logcount (logxor header (cadr remaining))) 1))
94 ;; BASE-STRING, (VECTOR NIL), BIT-VECTOR, (VECTOR T)
95 (inst and al-tn (ldb (byte 8 0) (logeqv header (cadr remaining))))
96 (inst cmp al-tn (ldb (byte 8 0) (logand header (cadr remaining))))
97 (inst jmp equal target)
100 (inst cmp al-tn header)
102 (inst jmp equal target)
103 (inst jmp :e when-true)))))
105 (let ((start (car header))
108 ;; LAST = don't need al-tn later
109 ((and last (not (= start bignum-widetag))
110 (= (+ start 4) end) (= (logcount (logxor start end)) 1))
112 (inst and al-tn (ldb (byte 8 0) (logeqv start end)))
113 (inst cmp al-tn (ldb (byte 8 0) (logand start end)))
114 (inst jmp equal target))
115 ((and (not last) (null (cddr remaining))
116 (= (+ start 4) end) (= (logcount (logxor start end)) 1)
117 (listp (cadr remaining))
118 (= (+ (caadr remaining) 4) (cdadr remaining))
119 (= (logcount (logxor (caadr remaining) (cdadr remaining))) 1)
120 (= (logcount (logxor (caadr remaining) start)) 1))
122 (inst and al-tn (ldb (byte 8 0) (logeqv start (cdadr remaining))))
123 (inst cmp al-tn (ldb (byte 8 0) (logand start (cdadr remaining))))
124 (inst jmp equal target)
125 ;; we've shortcircuited the DO, so we must return.
126 ;; It's OK to do so, because (NULL (CDDR REMAINING))
130 (unless (= start bignum-widetag)
131 (inst cmp al-tn start)
132 (if (= end complex-array-widetag)
135 (inst jmp greater-or-equal target))
136 (inst jmp :b when-false))) ; was :l
137 (unless (= end complex-array-widetag)
140 (inst jmp less-or-equal target)
141 (inst jmp :be when-true)))))))))))) ; was :le
142 (emit-label drop-through))))
144 ;;;; type checking and testing
146 (define-vop (check-type)
147 (:args (value :target result :scs (any-reg descriptor-reg)))
148 (:results (result :scs (any-reg descriptor-reg)))
149 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
152 (:save-p :compute-only))
154 (define-vop (type-predicate)
155 (:args (value :scs (any-reg descriptor-reg)))
156 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
160 (:policy :fast-safe))
162 ;;; simpler VOP that don't need a temporary register
163 (define-vop (simple-check-type)
164 (:args (value :target result :scs (any-reg descriptor-reg)))
165 (:results (result :scs (any-reg descriptor-reg)
166 :load-if (not (and (sc-is value any-reg descriptor-reg)
167 (sc-is result control-stack)))))
169 (:save-p :compute-only))
171 (define-vop (simple-type-predicate)
172 (:args (value :scs (any-reg descriptor-reg control-stack)))
175 (:policy :fast-safe))
177 (defun cost-to-test-types (type-codes)
178 (+ (* 2 (length type-codes))
179 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
181 (defmacro !define-type-vops (pred-name check-name ptype error-code
183 &key (variant nil variant-p) &allow-other-keys)
184 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
186 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
187 (prefix (if variant-p
188 (concatenate 'string (string variant) "-")
192 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
193 (:translate ,pred-name)
195 (test-type value target not-p (,@type-codes))))))
197 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
200 (generate-error-code vop ',error-code value)))
201 (test-type value err-lab t (,@type-codes))
202 (move result value))))))
204 `((primitive-type-vop ,check-name (:check) ,ptype))))))
206 ;;;; other integer ranges
208 (define-vop (fixnump/unsigned-byte-32 simple-type-predicate)
209 (:args (value :scs (unsigned-reg)))
212 (:arg-types unsigned-num)
215 ;; We could encode this with :Z and SHR, analogously to the signed-byte-32
216 ;; case below -- as we do on x86-64 -- but that costs us an extra
217 ;; register. Compromises...
218 (inst cmp value #.sb!xc:most-positive-fixnum)))
220 (define-vop (fixnump/signed-byte-32 type-predicate)
221 (:args (value :scs (signed-reg)))
224 (:arg-types signed-num)
227 ;; Hackers Delight, p. 53: signed
228 ;; a <= x <= a + 2^n - 1
229 ;; is equivalent to unsigned
231 (inst mov eax-tn value)
232 (inst sub eax-tn #.sb!xc:most-negative-fixnum)
233 (inst shr eax-tn #.(integer-length (- sb!xc:most-positive-fixnum
234 sb!xc:most-negative-fixnum)))))
236 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
237 ;;; exactly one digit.
239 (define-vop (signed-byte-32-p type-predicate)
240 (:translate signed-byte-32-p)
242 (multiple-value-bind (yep nope)
244 (values not-target target)
245 (values target not-target))
246 (generate-fixnum-test value)
249 (inst and al-tn lowtag-mask)
250 (inst cmp al-tn other-pointer-lowtag)
252 (loadw eax-tn value 0 other-pointer-lowtag)
253 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
254 (inst jmp (if not-p :ne :e) target))
257 (define-vop (check-signed-byte-32 check-type)
259 (let ((nope (generate-error-code vop
260 'object-not-signed-byte-32-error
262 (generate-fixnum-test value)
265 (inst and al-tn lowtag-mask)
266 (inst cmp al-tn other-pointer-lowtag)
268 (loadw eax-tn value 0 other-pointer-lowtag)
269 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
272 (move result value)))
274 ;;; An (unsigned-byte 32) can be represented with either a positive
275 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
276 ;;; exactly two digits and the second digit all zeros.
277 (define-vop (unsigned-byte-32-p type-predicate)
278 (:translate unsigned-byte-32-p)
280 (let ((not-target (gen-label))
281 (single-word (gen-label))
282 (fixnum (gen-label)))
283 (multiple-value-bind (yep nope)
285 (values not-target target)
286 (values target not-target))
288 (generate-fixnum-test value)
292 ;; If not, is it an other pointer?
293 (inst and al-tn lowtag-mask)
294 (inst cmp al-tn other-pointer-lowtag)
297 (loadw eax-tn value 0 other-pointer-lowtag)
299 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
300 (inst jmp :e single-word)
301 ;; If it's other than two, we can't be an (unsigned-byte 32)
302 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
304 ;; Get the second digit.
305 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
306 ;; All zeros, its an (unsigned-byte 32).
307 (inst or eax-tn eax-tn)
311 (emit-label single-word)
312 ;; Get the single digit.
313 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
315 ;; positive implies (unsigned-byte 32).
317 (inst or eax-tn eax-tn)
318 (inst jmp (if not-p :s :ns) target)
320 (emit-label not-target)))))
322 (define-vop (check-unsigned-byte-32 check-type)
325 (generate-error-code vop 'object-not-unsigned-byte-32-error value))
328 (single-word (gen-label)))
331 (generate-fixnum-test value)
335 ;; If not, is it an other pointer?
336 (inst and al-tn lowtag-mask)
337 (inst cmp al-tn other-pointer-lowtag)
340 (loadw eax-tn value 0 other-pointer-lowtag)
342 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
343 (inst jmp :e single-word)
344 ;; If it's other than two, we can't be an (unsigned-byte 32)
345 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
347 ;; Get the second digit.
348 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
349 ;; All zeros, its an (unsigned-byte 32).
350 (inst or eax-tn eax-tn)
354 (emit-label single-word)
355 ;; Get the single digit.
356 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
358 ;; positive implies (unsigned-byte 32).
360 (inst or eax-tn eax-tn)
364 (move result value))))
366 ;;;; list/symbol types
368 ;;; symbolp (or symbol (eq nil))
369 ;;; consp (and list (not (eq nil)))
371 (define-vop (symbolp type-predicate)
374 (let ((is-symbol-label (if not-p drop-thru target)))
375 (inst cmp value nil-value)
376 (inst jmp :e is-symbol-label)
377 (test-type value target not-p (symbol-header-widetag)))
380 (define-vop (check-symbol check-type)
382 (let ((error (generate-error-code vop 'object-not-symbol-error value)))
383 (inst cmp value nil-value)
384 (inst jmp :e drop-thru)
385 (test-type value error t (symbol-header-widetag)))
387 (move result value)))
389 (define-vop (consp type-predicate)
392 (let ((is-not-cons-label (if not-p target drop-thru)))
393 (inst cmp value nil-value)
394 (inst jmp :e is-not-cons-label)
395 (test-type value target not-p (list-pointer-lowtag)))
398 (define-vop (check-cons check-type)
400 (let ((error (generate-error-code vop 'object-not-cons-error value)))
401 (inst cmp value nil-value)
403 (test-type value error t (list-pointer-lowtag))
404 (move result value))))