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 ;;; Emit the most compact form of the test immediate instruction,
17 ;;; using an 8 bit test when the immediate is only 8 bits and the
18 ;;; value is one of the four low registers (eax, ebx, ecx, edx) or the
20 (defun generate-fixnum-test (value)
21 (let ((offset (tn-offset value)))
22 (cond ((and (sc-is value any-reg descriptor-reg)
23 (or (= offset eax-offset) (= offset ebx-offset)
24 (= offset ecx-offset) (= offset edx-offset)))
25 (inst test (make-random-tn :kind :normal
26 :sc (sc-or-lose 'byte-reg)
29 ((sc-is value control-stack)
30 (inst test (make-ea :byte :base ebp-tn
31 :disp (- (* (1+ offset) n-word-bytes)))
34 (inst test value 3)))))
36 (defun %test-fixnum (value target not-p)
37 (generate-fixnum-test value)
38 (inst jmp (if not-p :nz :z) target))
40 (defun %test-fixnum-and-headers (value target not-p headers)
41 (let ((drop-through (gen-label)))
42 (generate-fixnum-test value)
43 (inst jmp :z (if not-p drop-through target))
44 (%test-headers value target not-p nil headers drop-through)))
46 (defun %test-immediate (value target not-p immediate)
47 ;; Code a single instruction byte test if possible.
48 (let ((offset (tn-offset value)))
49 (cond ((and (sc-is value any-reg descriptor-reg)
50 (or (= offset eax-offset) (= offset ebx-offset)
51 (= offset ecx-offset) (= offset edx-offset)))
52 (inst cmp (make-random-tn :kind :normal
53 :sc (sc-or-lose 'byte-reg)
58 (inst cmp al-tn immediate))))
59 (inst jmp (if not-p :ne :e) target))
61 (defun %test-lowtag (value target not-p lowtag &optional al-loaded)
64 (inst and al-tn lowtag-mask))
65 (inst cmp al-tn lowtag)
66 (inst jmp (if not-p :ne :e) target))
68 (defun %test-headers (value target not-p function-p headers
69 &optional (drop-through (gen-label)) al-loaded)
70 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
71 (multiple-value-bind (equal less-or-equal greater-or-equal when-true when-false)
72 ;; EQUAL, LESS-OR-EQUAL and GREATER-OR-EQUAL are the conditions for
73 ;; branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
74 ;; labels to branch to when we know it's true and when we know
75 ;; it's false respectively.
77 (values :ne :a :b drop-through target)
78 (values :e :na :nb target drop-through))
79 (%test-lowtag value when-false t lowtag al-loaded)
80 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
81 (do ((remaining headers (cdr remaining)))
83 (let ((header (car remaining))
84 (last (null (cdr remaining))))
88 ((and (not last) (null (cddr remaining))
89 (atom (cadr remaining))
90 (= (logcount (logxor header (cadr remaining))) 1))
91 ;; BASE-STRING, (VECTOR NIL), BIT-VECTOR, (VECTOR T)
92 (inst and al-tn (ldb (byte 8 0) (logeqv header (cadr remaining))))
93 (inst cmp al-tn (ldb (byte 8 0) (logand header (cadr remaining))))
94 (inst jmp equal target)
97 (inst cmp al-tn header)
99 (inst jmp equal target)
100 (inst jmp :e when-true)))))
102 (let ((start (car header))
105 ;; LAST = don't need al-tn later
106 ((and last (not (= start bignum-widetag))
107 (= (+ start 4) end) (= (logcount (logxor start end)) 1))
109 (inst and al-tn (ldb (byte 8 0) (logeqv start end)))
110 (inst cmp al-tn (ldb (byte 8 0) (logand start end)))
111 (inst jmp equal target))
112 ((and (not last) (null (cddr remaining))
113 (= (+ start 4) end) (= (logcount (logxor start end)) 1)
114 (listp (cadr remaining))
115 (= (+ (caadr remaining) 4) (cdadr remaining))
116 (= (logcount (logxor (caadr remaining) (cdadr remaining))) 1)
117 (= (logcount (logxor (caadr remaining) start)) 1))
119 (inst and al-tn (ldb (byte 8 0) (logeqv start (cdadr remaining))))
120 (inst cmp al-tn (ldb (byte 8 0) (logand start (cdadr remaining))))
121 (inst jmp equal target)
122 ;; we've shortcircuited the DO, so we must return.
123 ;; It's OK to do so, because (NULL (CDDR REMAINING))
127 (unless (= start bignum-widetag)
128 (inst cmp al-tn start)
129 (if (= end complex-array-widetag)
132 (inst jmp greater-or-equal target))
133 (inst jmp :b when-false))) ; was :l
134 (unless (= end complex-array-widetag)
137 (inst jmp less-or-equal target)
138 (inst jmp :be when-true)))))))))) ; was :le
139 (emit-label drop-through))))
141 ;;;; type checking and testing
143 (define-vop (check-type)
144 (:args (value :target result :scs (any-reg descriptor-reg)))
145 (:results (result :scs (any-reg descriptor-reg)))
146 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
149 (:save-p :compute-only))
151 (define-vop (type-predicate)
152 (:args (value :scs (any-reg descriptor-reg)))
153 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
157 (:policy :fast-safe))
159 ;;; simpler VOP that don't need a temporary register
160 (define-vop (simple-check-type)
161 (:args (value :target result :scs (any-reg descriptor-reg)))
162 (:results (result :scs (any-reg descriptor-reg)
163 :load-if (not (and (sc-is value any-reg descriptor-reg)
164 (sc-is result control-stack)))))
166 (:save-p :compute-only))
168 (define-vop (simple-type-predicate)
169 (:args (value :scs (any-reg descriptor-reg control-stack)))
172 (:policy :fast-safe))
174 (defun cost-to-test-types (type-codes)
175 (+ (* 2 (length type-codes))
176 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
178 (defmacro !define-type-vops (pred-name check-name ptype error-code
180 &key (variant nil variant-p) &allow-other-keys)
181 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
183 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
184 (prefix (if variant-p
185 (concatenate 'string (string variant) "-")
189 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
190 (:translate ,pred-name)
192 (test-type value target not-p (,@type-codes))))))
194 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
197 (generate-error-code vop ,error-code value)))
198 (test-type value err-lab t (,@type-codes))
199 (move result value))))))
201 `((primitive-type-vop ,check-name (:check) ,ptype))))))
203 ;;;; other integer ranges
205 (define-vop (fixnump/unsigned-byte-32 simple-type-predicate)
206 (:args (value :scs (unsigned-reg)))
207 (:arg-types unsigned-num)
210 (inst cmp value #.sb!xc:most-positive-fixnum)
211 (inst jmp (if not-p :a :be) target)))
213 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
214 ;;; exactly one digit.
216 (define-vop (signed-byte-32-p type-predicate)
217 (:translate signed-byte-32-p)
219 (multiple-value-bind (yep nope)
221 (values not-target target)
222 (values target not-target))
223 (generate-fixnum-test value)
226 (inst and al-tn lowtag-mask)
227 (inst cmp al-tn other-pointer-lowtag)
229 (loadw eax-tn value 0 other-pointer-lowtag)
230 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
231 (inst jmp (if not-p :ne :e) target))
234 (define-vop (check-signed-byte-32 check-type)
236 (let ((nope (generate-error-code vop
237 object-not-signed-byte-32-error
239 (generate-fixnum-test value)
242 (inst and al-tn lowtag-mask)
243 (inst cmp al-tn other-pointer-lowtag)
245 (loadw eax-tn value 0 other-pointer-lowtag)
246 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
249 (move result value)))
251 ;;; An (unsigned-byte 32) can be represented with either a positive
252 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
253 ;;; exactly two digits and the second digit all zeros.
254 (define-vop (unsigned-byte-32-p type-predicate)
255 (:translate unsigned-byte-32-p)
257 (let ((not-target (gen-label))
258 (single-word (gen-label))
259 (fixnum (gen-label)))
260 (multiple-value-bind (yep nope)
262 (values not-target target)
263 (values target not-target))
265 (generate-fixnum-test value)
269 ;; If not, is it an other pointer?
270 (inst and al-tn lowtag-mask)
271 (inst cmp al-tn other-pointer-lowtag)
274 (loadw eax-tn value 0 other-pointer-lowtag)
276 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
277 (inst jmp :e single-word)
278 ;; If it's other than two, we can't be an (unsigned-byte 32)
279 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
281 ;; Get the second digit.
282 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
283 ;; All zeros, its an (unsigned-byte 32).
284 (inst or eax-tn eax-tn)
288 (emit-label single-word)
289 ;; Get the single digit.
290 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
292 ;; positive implies (unsigned-byte 32).
294 (inst or eax-tn eax-tn)
295 (inst jmp (if not-p :s :ns) target)
297 (emit-label not-target)))))
299 (define-vop (check-unsigned-byte-32 check-type)
302 (generate-error-code vop object-not-unsigned-byte-32-error value))
305 (single-word (gen-label)))
308 (generate-fixnum-test value)
312 ;; If not, is it an other pointer?
313 (inst and al-tn lowtag-mask)
314 (inst cmp al-tn other-pointer-lowtag)
317 (loadw eax-tn value 0 other-pointer-lowtag)
319 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
320 (inst jmp :e single-word)
321 ;; If it's other than two, we can't be an (unsigned-byte 32)
322 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
324 ;; Get the second digit.
325 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
326 ;; All zeros, its an (unsigned-byte 32).
327 (inst or eax-tn eax-tn)
331 (emit-label single-word)
332 ;; Get the single digit.
333 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
335 ;; positive implies (unsigned-byte 32).
337 (inst or eax-tn eax-tn)
341 (move result value))))
343 ;;;; list/symbol types
345 ;;; symbolp (or symbol (eq nil))
346 ;;; consp (and list (not (eq nil)))
348 (define-vop (symbolp type-predicate)
351 (let ((is-symbol-label (if not-p drop-thru target)))
352 (inst cmp value nil-value)
353 (inst jmp :e is-symbol-label)
354 (test-type value target not-p (symbol-header-widetag)))
357 (define-vop (check-symbol check-type)
359 (let ((error (generate-error-code vop object-not-symbol-error value)))
360 (inst cmp value nil-value)
361 (inst jmp :e drop-thru)
362 (test-type value error t (symbol-header-widetag)))
364 (move result value)))
366 (define-vop (consp type-predicate)
369 (let ((is-not-cons-label (if not-p target drop-thru)))
370 (inst cmp value nil-value)
371 (inst jmp :e is-not-cons-label)
372 (test-type value target not-p (list-pointer-lowtag)))
375 (define-vop (check-cons check-type)
377 (let ((error (generate-error-code vop object-not-cons-error value)))
378 (inst cmp value nil-value)
380 (test-type value error t (list-pointer-lowtag))
381 (move result value))))