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-lowtag-and-headers (value target not-p lowtag function-p headers)
69 (let ((drop-through (gen-label)))
70 (%test-lowtag value (if not-p drop-through target) nil lowtag)
71 (%test-headers value target not-p function-p headers drop-through t)))
74 (defun %test-headers (value target not-p function-p headers
75 &optional (drop-through (gen-label)) al-loaded)
76 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
77 (multiple-value-bind (equal less-or-equal when-true when-false)
78 ;; EQUAL and LESS-OR-EQUAL are the conditions for branching to TARGET.
79 ;; WHEN-TRUE and WHEN-FALSE are the labels to branch to when we know
80 ;; it's true and when we know it's false respectively.
82 (values :ne :a drop-through target)
83 (values :e :na target drop-through))
84 (%test-lowtag value when-false t lowtag al-loaded)
85 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
86 (do ((remaining headers (cdr remaining)))
88 (let ((header (car remaining))
89 (last (null (cdr remaining))))
92 (inst cmp al-tn header)
94 (inst jmp equal target)
95 (inst jmp :e when-true)))
97 (let ((start (car header))
99 (unless (= start bignum-widetag)
100 (inst cmp al-tn start)
101 (inst jmp :b when-false)) ; was :l
104 (inst jmp less-or-equal target)
105 (inst jmp :be when-true))))))) ; was :le
106 (emit-label drop-through))))
109 ;;;; type checking and testing
111 (define-vop (check-type)
112 (:args (value :target result :scs (any-reg descriptor-reg)))
113 (:results (result :scs (any-reg descriptor-reg)))
114 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
117 (:save-p :compute-only))
119 (define-vop (type-predicate)
120 (:args (value :scs (any-reg descriptor-reg)))
121 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
125 (:policy :fast-safe))
127 ;;; simpler VOP that don't need a temporary register
128 (define-vop (simple-check-type)
129 (:args (value :target result :scs (any-reg descriptor-reg)))
130 (:results (result :scs (any-reg descriptor-reg)
131 :load-if (not (and (sc-is value any-reg descriptor-reg)
132 (sc-is result control-stack)))))
134 (:save-p :compute-only))
136 (define-vop (simple-type-predicate)
137 (:args (value :scs (any-reg descriptor-reg control-stack)))
140 (:policy :fast-safe))
142 ;;; FIXME: DEF-TYPE-VOPS and DEF-SIMPLE-TYPE-VOPS are only used in
143 ;;; this file, so they should be in the EVAL-WHEN above, or otherwise
144 ;;; tweaked so that they don't appear in the target system.
146 (defun cost-to-test-types (type-codes)
147 (+ (* 2 (length type-codes))
148 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
150 (defmacro def-type-vops (pred-name check-name ptype error-code
152 (let ((cost (cost-to-test-types (mapcar #'eval type-codes))))
155 `((define-vop (,pred-name type-predicate)
156 (:translate ,pred-name)
158 (test-type value target not-p ,@type-codes)))))
160 `((define-vop (,check-name check-type)
163 (generate-error-code vop ,error-code value)))
164 (test-type value err-lab t ,@type-codes)
165 (move result value))))))
167 `((primitive-type-vop ,check-name (:check) ,ptype))))))
169 (defmacro def-simple-type-vops (pred-name check-name ptype error-code
171 (let ((cost (cost-to-test-types (mapcar #'eval type-codes))))
174 `((define-vop (,pred-name simple-type-predicate)
175 (:translate ,pred-name)
177 (test-type value target not-p ,@type-codes)))))
179 `((define-vop (,check-name simple-check-type)
182 (generate-error-code vop ,error-code value)))
183 (test-type value err-lab t ,@type-codes)
184 (move result value))))))
186 `((primitive-type-vop ,check-name (:check) ,ptype))))))
188 ;;;; other integer ranges
190 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
191 ;;; exactly one digit.
193 (define-vop (signed-byte-32-p type-predicate)
194 (:translate signed-byte-32-p)
196 (multiple-value-bind (yep nope)
198 (values not-target target)
199 (values target not-target))
200 (generate-fixnum-test value)
203 (inst and al-tn lowtag-mask)
204 (inst cmp al-tn other-pointer-lowtag)
206 (loadw eax-tn value 0 other-pointer-lowtag)
207 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
208 (inst jmp (if not-p :ne :e) target))
211 (define-vop (check-signed-byte-32 check-type)
213 (let ((nope (generate-error-code vop
214 object-not-signed-byte-32-error
216 (generate-fixnum-test value)
219 (inst and al-tn lowtag-mask)
220 (inst cmp al-tn other-pointer-lowtag)
222 (loadw eax-tn value 0 other-pointer-lowtag)
223 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
226 (move result value)))
228 ;;; An (unsigned-byte 32) can be represented with either a positive
229 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
230 ;;; exactly two digits and the second digit all zeros.
231 (define-vop (unsigned-byte-32-p type-predicate)
232 (:translate unsigned-byte-32-p)
234 (let ((not-target (gen-label))
235 (single-word (gen-label))
236 (fixnum (gen-label)))
237 (multiple-value-bind (yep nope)
239 (values not-target target)
240 (values target not-target))
242 (generate-fixnum-test value)
246 ;; If not, is it an other pointer?
247 (inst and al-tn lowtag-mask)
248 (inst cmp al-tn other-pointer-lowtag)
251 (loadw eax-tn value 0 other-pointer-lowtag)
253 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
254 (inst jmp :e single-word)
255 ;; If it's other than two, we can't be an (unsigned-byte 32)
256 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
258 ;; Get the second digit.
259 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
260 ;; All zeros, its an (unsigned-byte 32).
261 (inst or eax-tn eax-tn)
265 (emit-label single-word)
266 ;; Get the single digit.
267 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
269 ;; positive implies (unsigned-byte 32).
271 (inst or eax-tn eax-tn)
272 (inst jmp (if not-p :s :ns) target)
274 (emit-label not-target)))))
276 (define-vop (check-unsigned-byte-32 check-type)
279 (generate-error-code vop object-not-unsigned-byte-32-error value))
282 (single-word (gen-label)))
285 (generate-fixnum-test value)
289 ;; If not, is it an other pointer?
290 (inst and al-tn lowtag-mask)
291 (inst cmp al-tn other-pointer-lowtag)
294 (loadw eax-tn value 0 other-pointer-lowtag)
296 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
297 (inst jmp :e single-word)
298 ;; If it's other than two, we can't be an (unsigned-byte 32)
299 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
301 ;; Get the second digit.
302 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
303 ;; All zeros, its an (unsigned-byte 32).
304 (inst or eax-tn eax-tn)
308 (emit-label single-word)
309 ;; Get the single digit.
310 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
312 ;; positive implies (unsigned-byte 32).
314 (inst or eax-tn eax-tn)
318 (move result value))))
320 ;;;; list/symbol types
322 ;;; symbolp (or symbol (eq nil))
323 ;;; consp (and list (not (eq nil)))
325 (define-vop (symbolp type-predicate)
328 (let ((is-symbol-label (if not-p drop-thru target)))
329 (inst cmp value nil-value)
330 (inst jmp :e is-symbol-label)
331 (test-type value target not-p symbol-header-widetag))
334 (define-vop (check-symbol check-type)
336 (let ((error (generate-error-code vop object-not-symbol-error value)))
337 (inst cmp value nil-value)
338 (inst jmp :e drop-thru)
339 (test-type value error t symbol-header-widetag))
341 (move result value)))
343 (define-vop (consp type-predicate)
346 (let ((is-not-cons-label (if not-p target drop-thru)))
347 (inst cmp value nil-value)
348 (inst jmp :e is-not-cons-label)
349 (test-type value target not-p list-pointer-lowtag))
352 (define-vop (check-cons check-type)
354 (let ((error (generate-error-code vop object-not-cons-error value)))
355 (inst cmp value nil-value)
357 (test-type value error t list-pointer-lowtag)
358 (move result value))))