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)))
210 (:arg-types unsigned-num)
213 (inst cmp value #.sb!xc:most-positive-fixnum)
214 (inst jmp (if not-p :a :be) target)))
216 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
217 ;;; exactly one digit.
219 (define-vop (signed-byte-32-p type-predicate)
220 (:translate signed-byte-32-p)
222 (multiple-value-bind (yep nope)
224 (values not-target target)
225 (values target not-target))
226 (generate-fixnum-test value)
229 (inst and al-tn lowtag-mask)
230 (inst cmp al-tn other-pointer-lowtag)
232 (loadw eax-tn value 0 other-pointer-lowtag)
233 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
234 (inst jmp (if not-p :ne :e) target))
237 (define-vop (check-signed-byte-32 check-type)
239 (let ((nope (generate-error-code vop
240 object-not-signed-byte-32-error
242 (generate-fixnum-test value)
245 (inst and al-tn lowtag-mask)
246 (inst cmp al-tn other-pointer-lowtag)
248 (loadw eax-tn value 0 other-pointer-lowtag)
249 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
252 (move result value)))
254 ;;; An (unsigned-byte 32) can be represented with either a positive
255 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
256 ;;; exactly two digits and the second digit all zeros.
257 (define-vop (unsigned-byte-32-p type-predicate)
258 (:translate unsigned-byte-32-p)
260 (let ((not-target (gen-label))
261 (single-word (gen-label))
262 (fixnum (gen-label)))
263 (multiple-value-bind (yep nope)
265 (values not-target target)
266 (values target not-target))
268 (generate-fixnum-test value)
272 ;; If not, is it an other pointer?
273 (inst and al-tn lowtag-mask)
274 (inst cmp al-tn other-pointer-lowtag)
277 (loadw eax-tn value 0 other-pointer-lowtag)
279 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
280 (inst jmp :e single-word)
281 ;; If it's other than two, we can't be an (unsigned-byte 32)
282 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
284 ;; Get the second digit.
285 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
286 ;; All zeros, its an (unsigned-byte 32).
287 (inst or eax-tn eax-tn)
291 (emit-label single-word)
292 ;; Get the single digit.
293 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
295 ;; positive implies (unsigned-byte 32).
297 (inst or eax-tn eax-tn)
298 (inst jmp (if not-p :s :ns) target)
300 (emit-label not-target)))))
302 (define-vop (check-unsigned-byte-32 check-type)
305 (generate-error-code vop object-not-unsigned-byte-32-error value))
308 (single-word (gen-label)))
311 (generate-fixnum-test value)
315 ;; If not, is it an other pointer?
316 (inst and al-tn lowtag-mask)
317 (inst cmp al-tn other-pointer-lowtag)
320 (loadw eax-tn value 0 other-pointer-lowtag)
322 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
323 (inst jmp :e single-word)
324 ;; If it's other than two, we can't be an (unsigned-byte 32)
325 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
327 ;; Get the second digit.
328 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
329 ;; All zeros, its an (unsigned-byte 32).
330 (inst or eax-tn eax-tn)
334 (emit-label single-word)
335 ;; Get the single digit.
336 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
338 ;; positive implies (unsigned-byte 32).
340 (inst or eax-tn eax-tn)
344 (move result value))))
346 ;;;; list/symbol types
348 ;;; symbolp (or symbol (eq nil))
349 ;;; consp (and list (not (eq nil)))
351 (define-vop (symbolp type-predicate)
354 (let ((is-symbol-label (if not-p drop-thru target)))
355 (inst cmp value nil-value)
356 (inst jmp :e is-symbol-label)
357 (test-type value target not-p (symbol-header-widetag)))
360 (define-vop (check-symbol check-type)
362 (let ((error (generate-error-code vop object-not-symbol-error value)))
363 (inst cmp value nil-value)
364 (inst jmp :e drop-thru)
365 (test-type value error t (symbol-header-widetag)))
367 (move result value)))
369 (define-vop (consp type-predicate)
372 (let ((is-not-cons-label (if not-p target drop-thru)))
373 (inst cmp value nil-value)
374 (inst jmp :e is-not-cons-label)
375 (test-type value target not-p (list-pointer-lowtag)))
378 (define-vop (check-cons check-type)
380 (let ((error (generate-error-code vop object-not-cons-error value)))
381 (inst cmp value nil-value)
383 (test-type value error t (list-pointer-lowtag))
384 (move result value))))