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)
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 loads an untagged pointer into EAX, which allows
69 ;; to avoid untagging below
70 (%test-lowtag value when-false t lowtag)
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 eax-tn) (car headers))
81 (inst jmp equal target))
83 (inst mov al-tn (make-ea :byte :base eax-tn))
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))
131 ((= start bignum-widetag)
134 (inst jmp less-or-equal target)
135 (inst jmp :be when-true)))
136 ((= end complex-array-widetag)
137 (inst cmp al-tn start)
139 (inst jmp greater-or-equal target)
140 (inst jmp :b when-false)))
142 (inst cmp al-tn start)
143 (inst jmp :b when-false)
146 (inst jmp less-or-equal target)
147 (inst jmp :be when-true)))
149 (inst sub al-tn start)
150 (inst cmp al-tn (- end start))
151 (inst jmp less-or-equal target))))))))))))
152 (emit-label drop-through))))
154 ;;;; type checking and testing
156 (define-vop (check-type)
157 (:args (value :target result :scs (any-reg descriptor-reg)))
158 (:results (result :scs (any-reg descriptor-reg)))
159 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
162 (:save-p :compute-only))
164 (define-vop (type-predicate)
165 (:args (value :scs (any-reg descriptor-reg)))
166 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
170 (:policy :fast-safe))
172 ;;; simpler VOP that don't need a temporary register
173 (define-vop (simple-check-type)
174 (:args (value :target result :scs (any-reg descriptor-reg)))
175 (:results (result :scs (any-reg descriptor-reg)
176 :load-if (not (and (sc-is value any-reg descriptor-reg)
177 (sc-is result control-stack)))))
179 (:save-p :compute-only))
181 (define-vop (simple-type-predicate)
182 (:args (value :scs (any-reg descriptor-reg control-stack)))
185 (:policy :fast-safe))
187 (defun cost-to-test-types (type-codes)
188 (+ (* 2 (length type-codes))
189 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
191 (defmacro !define-type-vops (pred-name check-name ptype error-code
193 &key (variant nil variant-p) &allow-other-keys)
194 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
196 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
197 (prefix (if variant-p
198 (concatenate 'string (string variant) "-")
202 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
203 (:translate ,pred-name)
205 (test-type value target not-p (,@type-codes))))))
207 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
210 (generate-error-code vop ',error-code value)))
211 (test-type value err-lab t (,@type-codes))
212 (move result value))))))
214 `((primitive-type-vop ,check-name (:check) ,ptype))))))
216 ;;;; other integer ranges
218 (define-vop (fixnump/unsigned-byte-32 simple-type-predicate)
219 (:args (value :scs (unsigned-reg)))
222 (:arg-types unsigned-num)
225 ;; We could encode this with :Z and SHR, analogously to the signed-byte-32
226 ;; case below -- as we do on x86-64 -- but that costs us an extra
227 ;; register. Compromises...
228 (inst cmp value #.sb!xc:most-positive-fixnum)))
230 (define-vop (fixnump/signed-byte-32 type-predicate)
231 (:args (value :scs (signed-reg)))
234 (:arg-types signed-num)
237 ;; Hackers Delight, p. 53: signed
238 ;; a <= x <= a + 2^n - 1
239 ;; is equivalent to unsigned
241 (inst mov eax-tn value)
242 (inst sub eax-tn #.sb!xc:most-negative-fixnum)
243 (inst shr eax-tn #.(integer-length (- sb!xc:most-positive-fixnum
244 sb!xc:most-negative-fixnum)))))
246 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
247 ;;; exactly one digit.
249 (define-vop (signed-byte-32-p type-predicate)
250 (:translate signed-byte-32-p)
252 (multiple-value-bind (yep nope)
254 (values not-target target)
255 (values target not-target))
256 (generate-fixnum-test value)
258 (inst lea eax-tn (make-ea :dword :base value
259 :disp (- other-pointer-lowtag)))
260 (inst test al-tn lowtag-mask)
262 (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
263 (+ (ash 1 n-widetag-bits) bignum-widetag))
264 (inst jmp (if not-p :ne :e) target))
267 (define-vop (check-signed-byte-32 check-type)
269 (let ((nope (generate-error-code vop
270 'object-not-signed-byte-32-error
272 (generate-fixnum-test value)
274 (inst lea eax-tn (make-ea :dword :base value
275 :disp (- other-pointer-lowtag)))
276 (inst test al-tn lowtag-mask)
278 (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
279 (+ (ash 1 n-widetag-bits) bignum-widetag))
282 (move result value)))
284 ;;; An (unsigned-byte 32) can be represented with either a positive
285 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
286 ;;; exactly two digits and the second digit all zeros.
287 (define-vop (unsigned-byte-32-p type-predicate)
288 (:translate unsigned-byte-32-p)
290 (let ((not-target (gen-label))
291 (single-word (gen-label))
292 (fixnum (gen-label)))
293 (multiple-value-bind (yep nope)
295 (values not-target target)
296 (values target not-target))
298 (generate-fixnum-test value)
302 ;; If not, is it an other pointer?
303 (inst and al-tn lowtag-mask)
304 (inst cmp al-tn other-pointer-lowtag)
307 (loadw eax-tn value 0 other-pointer-lowtag)
309 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
310 (inst jmp :e single-word)
311 ;; If it's other than two, we can't be an (unsigned-byte 32)
312 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
314 ;; Get the second digit.
315 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
316 ;; All zeros, its an (unsigned-byte 32).
317 (inst test eax-tn eax-tn)
321 (emit-label single-word)
322 ;; Get the single digit.
323 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
325 ;; positive implies (unsigned-byte 32).
327 (inst test eax-tn eax-tn)
328 (inst jmp (if not-p :s :ns) target)
330 (emit-label not-target)))))
332 (define-vop (check-unsigned-byte-32 check-type)
335 (generate-error-code vop 'object-not-unsigned-byte-32-error value))
338 (single-word (gen-label)))
341 (generate-fixnum-test value)
345 ;; If not, is it an other pointer?
346 (inst and al-tn lowtag-mask)
347 (inst cmp al-tn other-pointer-lowtag)
350 (loadw eax-tn value 0 other-pointer-lowtag)
352 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
353 (inst jmp :e single-word)
354 ;; If it's other than two, we can't be an (unsigned-byte 32)
355 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
357 ;; Get the second digit.
358 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
359 ;; All zeros, its an (unsigned-byte 32).
360 (inst test eax-tn eax-tn)
364 (emit-label single-word)
365 ;; Get the single digit.
366 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
368 ;; positive implies (unsigned-byte 32).
370 (inst test eax-tn eax-tn)
374 (move result value))))
376 (defun power-of-two-limit-p (x)
378 (= (logcount (1+ x)) 1)))
380 (define-vop (test-fixnum-mod-power-of-two)
381 (:args (value :scs (any-reg descriptor-reg
382 unsigned-reg signed-reg
385 (:constant (satisfies power-of-two-limit-p)))
386 (:translate sb!c::fixnum-mod-p)
389 (:save-p :compute-only)
392 (aver (not (sc-is value immediate)))
393 (let* ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
396 (inst test value (lognot fixnum-hi)))))
398 (define-vop (test-fixnum-mod-tagged-unsigned)
399 (:args (value :scs (any-reg descriptor-reg
400 unsigned-reg signed-reg
402 (:arg-types (:or tagged-num unsigned-num signed-num)
404 (:translate sb!c::fixnum-mod-p)
407 (:save-p :compute-only)
410 (aver (not (sc-is value immediate)))
411 (let ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
414 (inst cmp value fixnum-hi))))
416 (define-vop (test-fixnum-mod-*)
417 (:args (value :scs (any-reg descriptor-reg)))
418 (:arg-types * (:constant fixnum))
419 (:translate sb!c::fixnum-mod-p)
421 (:info target not-p hi)
422 (:save-p :compute-only)
425 (let* ((fixnum-hi (fixnumize hi))
427 (generate-fixnum-test value)
428 (inst jmp :ne (if not-p target skip))
429 (inst cmp value fixnum-hi)
430 (inst jmp (if not-p :a :be) target)
434 ;;;; list/symbol types
436 ;;; symbolp (or symbol (eq nil))
437 ;;; consp (and list (not (eq nil)))
439 (define-vop (symbolp type-predicate)
442 (let ((is-symbol-label (if not-p drop-thru target)))
443 (inst cmp value nil-value)
444 (inst jmp :e is-symbol-label)
445 (test-type value target not-p (symbol-header-widetag)))
448 (define-vop (check-symbol check-type)
450 (let ((error (generate-error-code vop 'object-not-symbol-error value)))
451 (inst cmp value nil-value)
452 (inst jmp :e drop-thru)
453 (test-type value error t (symbol-header-widetag)))
455 (move result value)))
457 (define-vop (consp type-predicate)
460 (let ((is-not-cons-label (if not-p target drop-thru)))
461 (inst cmp value nil-value)
462 (inst jmp :e is-not-cons-label)
463 (test-type value target not-p (list-pointer-lowtag)))
466 (define-vop (check-cons check-type)
468 (let ((error (generate-error-code vop 'object-not-cons-error value)))
469 (inst cmp value nil-value)
471 (test-type value error t (list-pointer-lowtag))
472 (move result value))))