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 (inst cmp value
#.sb
!xc
:most-positive-fixnum
)))
217 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
218 ;;; exactly one digit.
220 (define-vop (signed-byte-32-p type-predicate
)
221 (:translate signed-byte-32-p
)
223 (multiple-value-bind (yep nope
)
225 (values not-target target
)
226 (values target not-target
))
227 (generate-fixnum-test value
)
230 (inst and al-tn lowtag-mask
)
231 (inst cmp al-tn other-pointer-lowtag
)
233 (loadw eax-tn value
0 other-pointer-lowtag
)
234 (inst cmp eax-tn
(+ (ash 1 n-widetag-bits
) bignum-widetag
))
235 (inst jmp
(if not-p
:ne
:e
) target
))
238 (define-vop (check-signed-byte-32 check-type
)
240 (let ((nope (generate-error-code vop
241 'object-not-signed-byte-32-error
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
))
253 (move result value
)))
255 ;;; An (unsigned-byte 32) can be represented with either a positive
256 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
257 ;;; exactly two digits and the second digit all zeros.
258 (define-vop (unsigned-byte-32-p type-predicate
)
259 (:translate unsigned-byte-32-p
)
261 (let ((not-target (gen-label))
262 (single-word (gen-label))
263 (fixnum (gen-label)))
264 (multiple-value-bind (yep nope
)
266 (values not-target target
)
267 (values target not-target
))
269 (generate-fixnum-test value
)
273 ;; If not, is it an other pointer?
274 (inst and al-tn lowtag-mask
)
275 (inst cmp al-tn other-pointer-lowtag
)
278 (loadw eax-tn value
0 other-pointer-lowtag
)
280 (inst cmp eax-tn
(+ (ash 1 n-widetag-bits
) bignum-widetag
))
281 (inst jmp
:e single-word
)
282 ;; If it's other than two, we can't be an (unsigned-byte 32)
283 (inst cmp eax-tn
(+ (ash 2 n-widetag-bits
) bignum-widetag
))
285 ;; Get the second digit.
286 (loadw eax-tn value
(1+ bignum-digits-offset
) other-pointer-lowtag
)
287 ;; All zeros, its an (unsigned-byte 32).
288 (inst or eax-tn eax-tn
)
292 (emit-label single-word
)
293 ;; Get the single digit.
294 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag
)
296 ;; positive implies (unsigned-byte 32).
298 (inst or eax-tn eax-tn
)
299 (inst jmp
(if not-p
:s
:ns
) target
)
301 (emit-label not-target
)))))
303 (define-vop (check-unsigned-byte-32 check-type
)
306 (generate-error-code vop
'object-not-unsigned-byte-32-error value
))
309 (single-word (gen-label)))
312 (generate-fixnum-test value
)
316 ;; If not, is it an other pointer?
317 (inst and al-tn lowtag-mask
)
318 (inst cmp al-tn other-pointer-lowtag
)
321 (loadw eax-tn value
0 other-pointer-lowtag
)
323 (inst cmp eax-tn
(+ (ash 1 n-widetag-bits
) bignum-widetag
))
324 (inst jmp
:e single-word
)
325 ;; If it's other than two, we can't be an (unsigned-byte 32)
326 (inst cmp eax-tn
(+ (ash 2 n-widetag-bits
) bignum-widetag
))
328 ;; Get the second digit.
329 (loadw eax-tn value
(1+ bignum-digits-offset
) other-pointer-lowtag
)
330 ;; All zeros, its an (unsigned-byte 32).
331 (inst or eax-tn eax-tn
)
335 (emit-label single-word
)
336 ;; Get the single digit.
337 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag
)
339 ;; positive implies (unsigned-byte 32).
341 (inst or eax-tn eax-tn
)
345 (move result value
))))
347 ;;;; list/symbol types
349 ;;; symbolp (or symbol (eq nil))
350 ;;; consp (and list (not (eq nil)))
352 (define-vop (symbolp type-predicate
)
355 (let ((is-symbol-label (if not-p drop-thru target
)))
356 (inst cmp value nil-value
)
357 (inst jmp
:e is-symbol-label
)
358 (test-type value target not-p
(symbol-header-widetag)))
361 (define-vop (check-symbol check-type
)
363 (let ((error (generate-error-code vop
'object-not-symbol-error value
)))
364 (inst cmp value nil-value
)
365 (inst jmp
:e drop-thru
)
366 (test-type value error t
(symbol-header-widetag)))
368 (move result value
)))
370 (define-vop (consp type-predicate
)
373 (let ((is-not-cons-label (if not-p target drop-thru
)))
374 (inst cmp value nil-value
)
375 (inst jmp
:e is-not-cons-label
)
376 (test-type value target not-p
(list-pointer-lowtag)))
379 (define-vop (check-cons check-type
)
381 (let ((error (generate-error-code vop
'object-not-cons-error value
)))
382 (inst cmp value nil-value
)
384 (test-type value error t
(list-pointer-lowtag))
385 (move result value
))))