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 when-true when-false)
72 ;; EQUAL and LESS-OR-EQUAL are the conditions for branching to TARGET.
73 ;; WHEN-TRUE and WHEN-FALSE are the labels to branch to when we know
74 ;; it's true and when we know it's false respectively.
76 (values :ne :a drop-through target)
77 (values :e :na target drop-through))
78 (%test-lowtag value when-false t lowtag al-loaded)
79 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
80 (do ((remaining headers (cdr remaining)))
82 (let ((header (car remaining))
83 (last (null (cdr remaining))))
86 (inst cmp al-tn header)
88 (inst jmp equal target)
89 (inst jmp :e when-true)))
91 (let ((start (car header))
93 (unless (= start bignum-widetag)
94 (inst cmp al-tn start)
95 (inst jmp :b when-false)) ; was :l
98 (inst jmp less-or-equal target)
99 (inst jmp :be when-true))))))) ; was :le
100 (emit-label drop-through))))
103 ;;;; type checking and testing
105 (define-vop (check-type)
106 (:args (value :target result :scs (any-reg descriptor-reg)))
107 (:results (result :scs (any-reg descriptor-reg)))
108 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
111 (:save-p :compute-only))
113 (define-vop (type-predicate)
114 (:args (value :scs (any-reg descriptor-reg)))
115 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
119 (:policy :fast-safe))
121 ;;; simpler VOP that don't need a temporary register
122 (define-vop (simple-check-type)
123 (:args (value :target result :scs (any-reg descriptor-reg)))
124 (:results (result :scs (any-reg descriptor-reg)
125 :load-if (not (and (sc-is value any-reg descriptor-reg)
126 (sc-is result control-stack)))))
128 (:save-p :compute-only))
130 (define-vop (simple-type-predicate)
131 (:args (value :scs (any-reg descriptor-reg control-stack)))
134 (:policy :fast-safe))
136 (defun cost-to-test-types (type-codes)
137 (+ (* 2 (length type-codes))
138 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
140 (defmacro !define-type-vops (pred-name check-name ptype error-code
142 &key (variant nil variant-p) &allow-other-keys)
143 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
145 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
146 (prefix (if variant-p
147 (concatenate 'string (string variant) "-")
151 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
152 (:translate ,pred-name)
154 (test-type value target not-p (,@type-codes))))))
156 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
159 (generate-error-code vop ,error-code value)))
160 (test-type value err-lab t (,@type-codes))
161 (move result value))))))
163 `((primitive-type-vop ,check-name (:check) ,ptype))))))
165 ;;;; other integer ranges
167 (define-vop (fixnump/unsigned-byte-32 simple-type-predicate)
168 (:args (value :scs (unsigned-reg)))
169 (:arg-types unsigned-num)
172 (inst cmp value #.sb!xc:most-positive-fixnum)
173 (inst jmp (if not-p :a :be) target)))
175 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
176 ;;; exactly one digit.
178 (define-vop (signed-byte-32-p type-predicate)
179 (:translate signed-byte-32-p)
181 (multiple-value-bind (yep nope)
183 (values not-target target)
184 (values target not-target))
185 (generate-fixnum-test value)
188 (inst and al-tn lowtag-mask)
189 (inst cmp al-tn other-pointer-lowtag)
191 (loadw eax-tn value 0 other-pointer-lowtag)
192 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
193 (inst jmp (if not-p :ne :e) target))
196 (define-vop (check-signed-byte-32 check-type)
198 (let ((nope (generate-error-code vop
199 object-not-signed-byte-32-error
201 (generate-fixnum-test value)
204 (inst and al-tn lowtag-mask)
205 (inst cmp al-tn other-pointer-lowtag)
207 (loadw eax-tn value 0 other-pointer-lowtag)
208 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
211 (move result value)))
213 ;;; An (unsigned-byte 32) can be represented with either a positive
214 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
215 ;;; exactly two digits and the second digit all zeros.
216 (define-vop (unsigned-byte-32-p type-predicate)
217 (:translate unsigned-byte-32-p)
219 (let ((not-target (gen-label))
220 (single-word (gen-label))
221 (fixnum (gen-label)))
222 (multiple-value-bind (yep nope)
224 (values not-target target)
225 (values target not-target))
227 (generate-fixnum-test value)
231 ;; If not, is it an other pointer?
232 (inst and al-tn lowtag-mask)
233 (inst cmp al-tn other-pointer-lowtag)
236 (loadw eax-tn value 0 other-pointer-lowtag)
238 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
239 (inst jmp :e single-word)
240 ;; If it's other than two, we can't be an (unsigned-byte 32)
241 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
243 ;; Get the second digit.
244 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
245 ;; All zeros, its an (unsigned-byte 32).
246 (inst or eax-tn eax-tn)
250 (emit-label single-word)
251 ;; Get the single digit.
252 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
254 ;; positive implies (unsigned-byte 32).
256 (inst or eax-tn eax-tn)
257 (inst jmp (if not-p :s :ns) target)
259 (emit-label not-target)))))
261 (define-vop (check-unsigned-byte-32 check-type)
264 (generate-error-code vop object-not-unsigned-byte-32-error value))
267 (single-word (gen-label)))
270 (generate-fixnum-test value)
274 ;; If not, is it an other pointer?
275 (inst and al-tn lowtag-mask)
276 (inst cmp al-tn other-pointer-lowtag)
279 (loadw eax-tn value 0 other-pointer-lowtag)
281 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
282 (inst jmp :e single-word)
283 ;; If it's other than two, we can't be an (unsigned-byte 32)
284 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
286 ;; Get the second digit.
287 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
288 ;; All zeros, its an (unsigned-byte 32).
289 (inst or eax-tn eax-tn)
293 (emit-label single-word)
294 ;; Get the single digit.
295 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
297 ;; positive implies (unsigned-byte 32).
299 (inst or eax-tn eax-tn)
303 (move result value))))
305 ;;;; list/symbol types
307 ;;; symbolp (or symbol (eq nil))
308 ;;; consp (and list (not (eq nil)))
310 (define-vop (symbolp type-predicate)
313 (let ((is-symbol-label (if not-p drop-thru target)))
314 (inst cmp value nil-value)
315 (inst jmp :e is-symbol-label)
316 (test-type value target not-p (symbol-header-widetag)))
319 (define-vop (check-symbol check-type)
321 (let ((error (generate-error-code vop object-not-symbol-error value)))
322 (inst cmp value nil-value)
323 (inst jmp :e drop-thru)
324 (test-type value error t (symbol-header-widetag)))
326 (move result value)))
328 (define-vop (consp type-predicate)
331 (let ((is-not-cons-label (if not-p target drop-thru)))
332 (inst cmp value nil-value)
333 (inst jmp :e is-not-cons-label)
334 (test-type value target not-p (list-pointer-lowtag)))
337 (define-vop (check-cons check-type)
339 (let ((error (generate-error-code vop object-not-cons-error value)))
340 (inst cmp value nil-value)
342 (test-type value error t (list-pointer-lowtag))
343 (move result value))))