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 fixnum-tag-mask))
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)
45 (inst lea eax-tn (make-ea :dword :base value :disp (- lowtag)))
46 (inst test al-tn lowtag-mask)
47 ;; FIXME: another 'optimization' which doesn't appear to work:
48 ;; prefetching the hypothetically pointed-to version should help,
49 ;; but this is in fact non-ideal in plenty of ways: we emit way too
50 ;; many of these prefetch instructions; pointed-to objects are very
51 ;; often in the cache anyway; etc. etc. Still, as proof-of-concept,
52 ;; not too bad. -- CSR, 2004-07-27
53 (when (member :prefetch *backend-subfeatures*)
54 (inst prefetchnta (make-ea :byte :base value :disp (- lowtag))))
55 (inst jmp (if not-p :ne :e) target))
57 (defun %test-headers (value target not-p function-p headers
58 &optional (drop-through (gen-label)))
59 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
60 (multiple-value-bind (equal less-or-equal greater-or-equal when-true when-false)
61 ;; EQUAL, LESS-OR-EQUAL and GREATER-OR-EQUAL are the conditions for
62 ;; branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
63 ;; labels to branch to when we know it's true and when we know
64 ;; it's false respectively.
66 (values :ne :a :b drop-through target)
67 (values :e :na :nb target drop-through))
68 (%test-lowtag value when-false t lowtag)
70 ((and (null (cdr headers))
71 (numberp (car headers)))
72 ;; Optimize the common case: referencing the value from memory
73 ;; is slightly smaller than loading it and then doing the
74 ;; comparison. Doing this for other cases (e.g. range of
75 ;; [BIGNUM-WIDETAG..FOO-WIDETAG]) is also possible, but such
76 ;; opportunities don't come up very often and the code would
77 ;; get pretty hairy...
78 (inst cmp (make-ea :byte :base value :disp (- lowtag)) (car headers))
79 (inst jmp equal target))
81 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
82 (do ((remaining headers (cdr remaining)))
84 (let ((header (car remaining))
85 (last (null (cdr remaining))))
89 ((and (not last) (null (cddr remaining))
90 (atom (cadr remaining))
91 (= (logcount (logxor header (cadr remaining))) 1))
92 ;; BASE-STRING, (VECTOR NIL), BIT-VECTOR, (VECTOR T)
93 (inst and al-tn (ldb (byte 8 0) (logeqv header (cadr remaining))))
94 (inst cmp al-tn (ldb (byte 8 0) (logand header (cadr remaining))))
95 (inst jmp equal target)
98 (inst cmp al-tn header)
100 (inst jmp equal target)
101 (inst jmp :e when-true)))))
103 (let ((start (car header))
106 ;; LAST = don't need al-tn later
107 ((and last (not (= start bignum-widetag))
108 (= (+ start 4) end) (= (logcount (logxor start end)) 1))
110 (inst and al-tn (ldb (byte 8 0) (logeqv start end)))
111 (inst cmp al-tn (ldb (byte 8 0) (logand start end)))
112 (inst jmp equal target))
113 ((and (not last) (null (cddr remaining))
114 (= (+ start 4) end) (= (logcount (logxor start end)) 1)
115 (listp (cadr remaining))
116 (= (+ (caadr remaining) 4) (cdadr remaining))
117 (= (logcount (logxor (caadr remaining) (cdadr remaining))) 1)
118 (= (logcount (logxor (caadr remaining) start)) 1))
120 (inst and al-tn (ldb (byte 8 0) (logeqv start (cdadr remaining))))
121 (inst cmp al-tn (ldb (byte 8 0) (logand start (cdadr remaining))))
122 (inst jmp equal target)
123 ;; we've shortcircuited the DO, so we must return.
124 ;; It's OK to do so, because (NULL (CDDR REMAINING))
129 ((= start bignum-widetag)
132 (inst jmp less-or-equal target)
133 (inst jmp :be when-true)))
134 ((= end complex-array-widetag)
135 (inst cmp al-tn start)
137 (inst jmp greater-or-equal target)
138 (inst jmp :b when-false)))
140 (inst cmp al-tn start)
141 (inst jmp :b when-false)
144 (inst jmp less-or-equal target)
145 (inst jmp :be when-true)))
147 (inst sub al-tn start)
148 (inst cmp al-tn (- end start))
149 (inst jmp less-or-equal target))))))))))))
150 (emit-label drop-through))))
152 ;;;; type checking and testing
154 (define-vop (check-type)
155 (:args (value :target result :scs (any-reg descriptor-reg)))
156 (:results (result :scs (any-reg descriptor-reg)))
157 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
160 (:save-p :compute-only))
162 (define-vop (type-predicate)
163 (:args (value :scs (any-reg descriptor-reg)))
164 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
168 (:policy :fast-safe))
170 ;;; simpler VOP that don't need a temporary register
171 (define-vop (simple-check-type)
172 (:args (value :target result :scs (any-reg descriptor-reg)))
173 (:results (result :scs (any-reg descriptor-reg)
174 :load-if (not (and (sc-is value any-reg descriptor-reg)
175 (sc-is result control-stack)))))
177 (:save-p :compute-only))
179 (define-vop (simple-type-predicate)
180 (:args (value :scs (any-reg descriptor-reg control-stack)))
183 (:policy :fast-safe))
185 (defun cost-to-test-types (type-codes)
186 (+ (* 2 (length type-codes))
187 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
189 (defmacro !define-type-vops (pred-name check-name ptype error-code
191 &key (variant nil variant-p) &allow-other-keys)
192 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
194 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
195 (prefix (if variant-p
196 (concatenate 'string (string variant) "-")
200 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
201 (:translate ,pred-name)
203 (test-type value target not-p (,@type-codes))))))
205 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
208 (generate-error-code vop ',error-code value)))
209 (test-type value err-lab t (,@type-codes))
210 (move result value))))))
212 `((primitive-type-vop ,check-name (:check) ,ptype))))))
214 ;;;; other integer ranges
216 (define-vop (fixnump/unsigned-byte-32 simple-type-predicate)
217 (:args (value :scs (unsigned-reg)))
220 (:arg-types unsigned-num)
223 ;; We could encode this with :Z and SHR, analogously to the signed-byte-32
224 ;; case below -- as we do on x86-64 -- but that costs us an extra
225 ;; register. Compromises...
226 (inst cmp value #.sb!xc:most-positive-fixnum)))
228 (define-vop (fixnump/signed-byte-32 type-predicate)
229 (:args (value :scs (signed-reg)))
232 (:arg-types signed-num)
235 ;; Hackers Delight, p. 53: signed
236 ;; a <= x <= a + 2^n - 1
237 ;; is equivalent to unsigned
239 (inst mov eax-tn value)
240 (inst sub eax-tn #.sb!xc:most-negative-fixnum)
241 (inst shr eax-tn #.(integer-length (- sb!xc:most-positive-fixnum
242 sb!xc:most-negative-fixnum)))))
244 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
245 ;;; exactly one digit.
247 (define-vop (signed-byte-32-p type-predicate)
248 (:translate signed-byte-32-p)
250 (multiple-value-bind (yep nope)
252 (values not-target target)
253 (values target not-target))
254 (generate-fixnum-test value)
256 (inst lea eax-tn (make-ea :dword :base value
257 :disp (- other-pointer-lowtag)))
258 (inst test al-tn lowtag-mask)
260 (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
261 (+ (ash 1 n-widetag-bits) bignum-widetag))
262 (inst jmp (if not-p :ne :e) target))
265 (define-vop (check-signed-byte-32 check-type)
267 (let ((nope (generate-error-code vop
268 'object-not-signed-byte-32-error
270 (generate-fixnum-test value)
272 (inst lea eax-tn (make-ea :dword :base value
273 :disp (- other-pointer-lowtag)))
274 (inst test al-tn lowtag-mask)
276 (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
277 (+ (ash 1 n-widetag-bits) bignum-widetag))
280 (move result value)))
282 ;;; An (unsigned-byte 32) can be represented with either a positive
283 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
284 ;;; exactly two digits and the second digit all zeros.
285 (define-vop (unsigned-byte-32-p type-predicate)
286 (:translate unsigned-byte-32-p)
288 (let ((not-target (gen-label))
289 (single-word (gen-label))
290 (fixnum (gen-label)))
291 (multiple-value-bind (yep nope)
293 (values not-target target)
294 (values target not-target))
297 (inst test al-tn fixnum-tag-mask)
300 ;; If not, is it an other pointer?
301 (inst and al-tn lowtag-mask)
302 (inst cmp al-tn other-pointer-lowtag)
305 (loadw eax-tn value 0 other-pointer-lowtag)
307 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
308 (inst jmp :e single-word)
309 ;; If it's other than two, we can't be an (unsigned-byte 32)
310 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
312 ;; Get the second digit.
313 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
314 ;; All zeros, its an (unsigned-byte 32).
315 (inst test eax-tn eax-tn)
319 (emit-label single-word)
320 ;; Get the single digit.
321 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
323 ;; positive implies (unsigned-byte 32).
325 (inst test eax-tn eax-tn)
326 (inst jmp (if not-p :s :ns) target)
328 (emit-label not-target)))))
330 (define-vop (check-unsigned-byte-32 check-type)
333 (generate-error-code vop 'object-not-unsigned-byte-32-error value))
336 (single-word (gen-label)))
339 (generate-fixnum-test value)
343 ;; If not, is it an other pointer?
344 (inst and al-tn lowtag-mask)
345 (inst cmp al-tn other-pointer-lowtag)
348 (loadw eax-tn value 0 other-pointer-lowtag)
350 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
351 (inst jmp :e single-word)
352 ;; If it's other than two, we can't be an (unsigned-byte 32)
353 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
355 ;; Get the second digit.
356 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
357 ;; All zeros, its an (unsigned-byte 32).
358 (inst test eax-tn eax-tn)
362 (emit-label single-word)
363 ;; Get the single digit.
364 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
366 ;; positive implies (unsigned-byte 32).
368 (inst test eax-tn eax-tn)
372 (move result value))))
374 (defun power-of-two-limit-p (x)
376 (= (logcount (1+ x)) 1)))
378 (define-vop (test-fixnum-mod-power-of-two)
379 (:args (value :scs (any-reg descriptor-reg
380 unsigned-reg signed-reg
383 (:constant (satisfies power-of-two-limit-p)))
384 (:translate sb!c::fixnum-mod-p)
387 (:save-p :compute-only)
390 (aver (not (sc-is value immediate)))
391 (let* ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
394 (inst test value (lognot fixnum-hi)))))
396 (define-vop (test-fixnum-mod-tagged-unsigned)
397 (:args (value :scs (any-reg descriptor-reg
398 unsigned-reg signed-reg
400 (:arg-types (:or tagged-num unsigned-num signed-num)
402 (:translate sb!c::fixnum-mod-p)
405 (:save-p :compute-only)
408 (aver (not (sc-is value immediate)))
409 (let ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
412 (inst cmp value fixnum-hi))))
414 (define-vop (test-fixnum-mod-*)
415 (:args (value :scs (any-reg descriptor-reg)))
416 (:arg-types * (:constant fixnum))
417 (:translate sb!c::fixnum-mod-p)
419 (:info target not-p hi)
420 (:save-p :compute-only)
423 (let* ((fixnum-hi (fixnumize hi))
425 (generate-fixnum-test value)
426 (inst jmp :ne (if not-p target skip))
427 (inst cmp value fixnum-hi)
428 (inst jmp (if not-p :a :be) target)
432 ;;;; list/symbol types
434 ;;; symbolp (or symbol (eq nil))
435 ;;; consp (and list (not (eq nil)))
437 (define-vop (symbolp type-predicate)
440 (let ((is-symbol-label (if not-p drop-thru target)))
441 (inst cmp value nil-value)
442 (inst jmp :e is-symbol-label)
443 (test-type value target not-p (symbol-header-widetag)))
446 (define-vop (check-symbol check-type)
448 (let ((error (generate-error-code vop 'object-not-symbol-error value)))
449 (inst cmp value nil-value)
450 (inst jmp :e drop-thru)
451 (test-type value error t (symbol-header-widetag)))
453 (move result value)))
455 (define-vop (consp type-predicate)
458 (let ((is-not-cons-label (if not-p target drop-thru)))
459 (inst cmp value nil-value)
460 (inst jmp :e is-not-cons-label)
461 (test-type value target not-p (list-pointer-lowtag)))
464 (define-vop (check-cons check-type)
466 (let ((error (generate-error-code vop 'object-not-cons-error value)))
467 (inst cmp value nil-value)
469 (test-type value error t (list-pointer-lowtag))
470 (move result value))))