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 make-byte-tn (tn)
17 (aver (sc-is tn any-reg descriptor-reg unsigned-reg signed-reg))
18 (make-random-tn :kind :normal
19 :sc (sc-or-lose 'byte-reg)
20 :offset (tn-offset tn)))
22 (defun generate-fixnum-test (value)
23 "zero flag set if VALUE is fixnum"
24 (let ((offset (tn-offset value)))
25 ;; The x86 backend uses a pun from E[A-D]X -> [A-D]L for these
26 ;; tests. The Athlon 64 optimization guide says that this is a
27 ;; bad idea, so it's been removed.
28 (cond ((sc-is value control-stack)
29 (inst test (make-ea :byte :base rbp-tn
30 :disp (- (* (1+ offset) n-word-bytes)))
31 sb!vm::fixnum-tag-mask))
33 (inst test value sb!vm::fixnum-tag-mask)))))
35 (defun %test-fixnum (value target not-p)
36 (generate-fixnum-test value)
37 (inst jmp (if not-p :nz :z) target))
39 (defun %test-fixnum-and-headers (value target not-p headers)
40 (let ((drop-through (gen-label)))
41 (generate-fixnum-test value)
42 (inst jmp :z (if not-p drop-through target))
43 (%test-headers value target not-p nil headers drop-through)))
45 (defun %test-immediate (value target not-p immediate)
46 ;; Code a single instruction byte test if possible.
47 (cond ((sc-is value any-reg descriptor-reg)
48 (inst cmp (make-byte-tn value) immediate))
51 (inst cmp al-tn immediate)))
52 (inst jmp (if not-p :ne :e) target))
54 (defun %test-lowtag (value target not-p lowtag)
56 (inst and rax-tn lowtag-mask)
57 (inst cmp rax-tn lowtag)
58 (inst jmp (if not-p :ne :e) target))
60 (defun %test-headers (value target not-p function-p headers
61 &optional (drop-through (gen-label)))
62 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
63 (multiple-value-bind (equal less-or-equal when-true when-false)
64 ;; EQUAL and LESS-OR-EQUAL are the conditions for branching to TARGET.
65 ;; WHEN-TRUE and WHEN-FALSE are the labels to branch to when we know
66 ;; it's true and when we know it's false respectively.
68 (values :ne :a drop-through target)
69 (values :e :na target drop-through))
70 (%test-lowtag value when-false t lowtag)
71 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
72 (do ((remaining headers (cdr remaining)))
74 (let ((header (car remaining))
75 (last (null (cdr remaining))))
78 (inst cmp al-tn header)
80 (inst jmp equal target)
81 (inst jmp :e when-true)))
83 (let ((start (car header))
85 (unless (= start bignum-widetag)
86 (inst cmp al-tn start)
87 (inst jmp :b when-false)) ; was :l
90 (inst jmp less-or-equal target)
91 (inst jmp :be when-true))))))) ; was :le
92 (emit-label drop-through))))
95 ;;;; type checking and testing
97 (define-vop (check-type)
98 (:args (value :target result :scs (any-reg descriptor-reg)))
99 (:results (result :scs (any-reg descriptor-reg)))
100 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
103 (:save-p :compute-only))
105 (define-vop (type-predicate)
106 (:args (value :scs (any-reg descriptor-reg)))
107 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
111 (:policy :fast-safe))
113 ;;; simpler VOP that don't need a temporary register
114 (define-vop (simple-check-type)
115 (:args (value :target result :scs (any-reg descriptor-reg)))
116 (:results (result :scs (any-reg descriptor-reg)
117 :load-if (not (and (sc-is value any-reg descriptor-reg)
118 (sc-is result control-stack)))))
120 (:save-p :compute-only))
122 (define-vop (simple-type-predicate)
123 (:args (value :scs (any-reg descriptor-reg control-stack)))
126 (:policy :fast-safe))
128 (defun cost-to-test-types (type-codes)
129 (+ (* 2 (length type-codes))
130 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
132 (defmacro !define-type-vops (pred-name check-name ptype error-code
134 &key (variant nil variant-p) &allow-other-keys)
135 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
137 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
138 (prefix (if variant-p
139 (concatenate 'string (string variant) "-")
143 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
144 (:translate ,pred-name)
146 (test-type value target not-p (,@type-codes))))))
148 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
151 (generate-error-code vop ,error-code value)))
152 (test-type value err-lab t (,@type-codes))
153 (move result value))))))
155 `((primitive-type-vop ,check-name (:check) ,ptype))))))
157 ;;;; other integer ranges
159 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
160 (:args (value :scs (unsigned-reg)))
161 (:arg-types unsigned-num)
163 (:temporary (:sc unsigned-reg) tmp)
167 (inst jmp (if not-p :nz :z) target)))
169 (define-vop (signed-byte-32-p type-predicate)
170 (:translate signed-byte-32-p)
172 ;; (and (fixnum) (or (no bits set >31) (all bits set >31))
175 (inst jmp :ne (if not-p target not-target))
176 (inst sar rax-tn (+ 32 3 -1))
180 (inst jmp not-target))
181 (inst jmp :z target))
184 (inst jmp (if not-p :ne :eq) target)
187 (define-vop (check-signed-byte-32 check-type)
189 (let ((nope (generate-error-code vop
190 object-not-signed-byte-32-error
196 (inst sar rax-tn (+ 32 3 -1))
201 (move result value))))
204 (define-vop (unsigned-byte-32-p type-predicate)
205 (:translate unsigned-byte-32-p)
207 ;; (and (fixnum) (no bits set >31))
210 (inst jmp :ne (if not-p target not-target))
211 (inst shr rax-tn (+ 32 sb!vm::n-fixnum-tag-bits))
212 (inst jmp (if not-p :nz :z) target)
215 (define-vop (check-unsigned-byte-32 check-type)
218 (generate-error-code vop object-not-unsigned-byte-32-error value)))
222 (inst shr rax-tn (+ 32 sb!vm::n-fixnum-tag-bits))
224 (move result value))))
226 ;;; An (unsigned-byte 64) can be represented with either a positive
227 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
228 ;;; exactly two digits and the second digit all zeros.
229 (define-vop (unsigned-byte-64-p type-predicate)
230 (:translate unsigned-byte-64-p)
232 (let ((not-target (gen-label))
233 (single-word (gen-label))
234 (fixnum (gen-label)))
235 (multiple-value-bind (yep nope)
237 (values not-target target)
238 (values target not-target))
240 (generate-fixnum-test value)
244 ;; If not, is it an other pointer?
245 (inst and eax-tn lowtag-mask)
246 (inst cmp eax-tn other-pointer-lowtag)
249 (loadw eax-tn value 0 other-pointer-lowtag)
251 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
252 (inst jmp :e single-word)
253 ;; If it's other than two, we can't be an (unsigned-byte 64)
254 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
256 ;; Get the second digit.
257 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
258 ;; All zeros, its an (unsigned-byte 64).
259 (inst or eax-tn eax-tn)
263 (emit-label single-word)
264 ;; Get the single digit.
265 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
267 ;; positive implies (unsigned-byte 64).
269 (inst or eax-tn eax-tn)
270 (inst jmp (if not-p :s :ns) target)
272 (emit-label not-target)))))
274 (define-vop (check-unsigned-byte-64 check-type)
277 (generate-error-code vop object-not-unsigned-byte-64-error value))
280 (single-word (gen-label)))
283 (generate-fixnum-test value)
287 ;; If not, is it an other pointer?
288 (inst and eax-tn lowtag-mask)
289 (inst cmp eax-tn other-pointer-lowtag)
292 (loadw eax-tn value 0 other-pointer-lowtag)
294 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
295 (inst jmp :e single-word)
296 ;; If it's other than two, we can't be an (unsigned-byte 64)
297 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
299 ;; Get the second digit.
300 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
301 ;; All zeros, its an (unsigned-byte 64).
302 (inst or eax-tn eax-tn)
306 (emit-label single-word)
307 ;; Get the single digit.
308 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
310 ;; positive implies (unsigned-byte 64).
312 (inst or eax-tn eax-tn)
316 (move result value))))
318 ;;;; list/symbol types
320 ;;; symbolp (or symbol (eq nil))
321 ;;; consp (and list (not (eq nil)))
323 (define-vop (symbolp type-predicate)
326 (let ((is-symbol-label (if not-p drop-thru target)))
327 (inst cmp value nil-value)
328 (inst jmp :e is-symbol-label)
329 (test-type value target not-p (symbol-header-widetag)))
332 (define-vop (check-symbol check-type)
334 (let ((error (generate-error-code vop object-not-symbol-error value)))
335 (inst cmp value nil-value)
336 (inst jmp :e drop-thru)
337 (test-type value error t (symbol-header-widetag)))
339 (move result value)))
341 (define-vop (consp type-predicate)
344 (let ((is-not-cons-label (if not-p target drop-thru)))
345 (inst cmp value nil-value)
346 (inst jmp :e is-not-cons-label)
347 (test-type value target not-p (list-pointer-lowtag)))
350 (define-vop (check-cons check-type)
352 (let ((error (generate-error-code vop object-not-cons-error value)))
353 (inst cmp value nil-value)
355 (test-type value error t (list-pointer-lowtag))
356 (move result value))))