1 ;;;; type testing and checking VOPs for the x86-64 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 "zero flag set if VALUE is fixnum"
19 (cond ((sc-is value any-reg descriptor-reg)
21 ((sc-is value control-stack)
22 (make-ea :byte :base rbp-tn
23 :disp (frame-byte-offset (tn-offset value))))
26 sb!vm::fixnum-tag-mask))
28 (defun %test-fixnum (value target not-p)
29 (generate-fixnum-test value)
30 (inst jmp (if not-p :nz :z) target))
32 (defun %test-fixnum-and-headers (value target not-p headers)
33 (let ((drop-through (gen-label)))
34 (generate-fixnum-test value)
35 (inst jmp :z (if not-p drop-through target))
36 (%test-headers value target not-p nil headers drop-through)))
38 (defun %test-fixnum-and-immediate (value target not-p immediate)
39 (let ((drop-through (gen-label)))
40 (generate-fixnum-test value)
41 (inst jmp :z (if not-p drop-through target))
42 (%test-immediate value target not-p immediate drop-through)))
44 (defun %test-fixnum-immediate-and-headers (value target not-p immediate
46 (let ((drop-through (gen-label)))
47 (generate-fixnum-test value)
48 (inst jmp :z (if not-p drop-through target))
49 (%test-immediate-and-headers value target not-p immediate headers
52 (defun %test-immediate (value target not-p immediate
53 &optional (drop-through (gen-label)))
54 ;; Code a single instruction byte test if possible.
55 (cond ((sc-is value any-reg descriptor-reg)
56 (inst cmp (make-byte-tn value) immediate))
59 (inst cmp al-tn immediate)))
60 (inst jmp (if not-p :ne :e) target)
61 (emit-label drop-through))
63 (defun %test-immediate-and-headers (value target not-p immediate headers
64 &optional (drop-through (gen-label)))
65 ;; Code a single instruction byte test if possible.
66 (cond ((sc-is value any-reg descriptor-reg)
67 (inst cmp (make-byte-tn value) immediate))
70 (inst cmp al-tn immediate)))
71 (inst jmp :e (if not-p drop-through target))
72 (%test-headers value target not-p nil headers drop-through))
74 (defun %test-lowtag (value target not-p lowtag)
75 (if (and (sc-is value any-reg descriptor-reg)
76 (< (tn-offset value) r8-offset))
77 (move eax-tn (make-dword-tn value)) ; shorter encoding (no REX prefix)
79 (inst and al-tn lowtag-mask)
80 (inst cmp al-tn lowtag)
81 (inst jmp (if not-p :ne :e) target))
83 (defun %test-headers (value target not-p function-p headers
84 &optional (drop-through (gen-label)))
85 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
86 (multiple-value-bind (equal less-or-equal greater-or-equal when-true when-false)
87 ;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
88 ;; for branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
89 ;; labels to branch to when we know it's true and when we know
90 ;; it's false respectively.
92 (values :ne :a :b drop-through target)
93 (values :e :na :nb target drop-through))
94 (%test-lowtag value when-false t lowtag)
95 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
96 (do ((remaining headers (cdr remaining)))
98 (let ((header (car remaining))
99 (last (null (cdr remaining))))
102 (inst cmp al-tn header)
104 (inst jmp equal target)
105 (inst jmp :e when-true)))
107 (let ((start (car header))
110 ((= start bignum-widetag)
113 (inst jmp less-or-equal target)
114 (inst jmp :be when-true)))
115 ((= end complex-array-widetag)
116 (inst cmp al-tn start)
118 (inst jmp greater-or-equal target)
119 (inst jmp :b when-false)))
121 (inst cmp al-tn start)
122 (inst jmp :b when-false)
125 (inst jmp less-or-equal target)
126 (inst jmp :be when-true)))
128 (inst sub al-tn start)
129 (inst cmp al-tn (- end start))
130 (inst jmp less-or-equal target))))))))
131 (emit-label drop-through))))
134 ;;;; type checking and testing
136 (define-vop (check-type)
137 (:args (value :target result :scs (any-reg descriptor-reg)))
138 (:results (result :scs (any-reg descriptor-reg)))
139 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
142 (:save-p :compute-only))
144 (define-vop (type-predicate)
145 (:args (value :scs (any-reg descriptor-reg)))
146 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
150 (:policy :fast-safe))
152 ;;; simpler VOP that don't need a temporary register
153 (define-vop (simple-check-type)
154 (:args (value :target result :scs (any-reg descriptor-reg)))
155 (:results (result :scs (any-reg descriptor-reg)
156 :load-if (not (and (sc-is value any-reg descriptor-reg)
157 (sc-is result control-stack)))))
159 (:save-p :compute-only))
161 (define-vop (simple-type-predicate)
162 (:args (value :scs (any-reg descriptor-reg control-stack)))
165 (:policy :fast-safe))
167 (defun cost-to-test-types (type-codes)
168 (+ (* 2 (length type-codes))
169 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
171 (defmacro !define-type-vops (pred-name check-name ptype error-code
173 &key (variant nil variant-p) &allow-other-keys)
174 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
176 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
177 (prefix (if variant-p
178 (concatenate 'string (string variant) "-")
182 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
183 (:translate ,pred-name)
185 (test-type value target not-p (,@type-codes))))))
187 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
190 (generate-error-code vop ',error-code value)))
191 (test-type value err-lab t (,@type-codes))
192 (move result value))))))
194 `((primitive-type-vop ,check-name (:check) ,ptype))))))
196 ;;;; other integer ranges
198 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
199 (:args (value :scs (unsigned-reg)))
200 (:arg-types unsigned-num)
202 (:temporary (:sc unsigned-reg) tmp)
207 (inst shr tmp n-positive-fixnum-bits)))
209 (define-vop (fixnump/signed-byte-64 type-predicate)
210 (:args (value :scs (signed-reg)))
213 (:arg-types signed-num)
216 ;; Hackers Delight, p. 53: signed
217 ;; a <= x <= a + 2^n - 1
218 ;; is equivalent to unsigned
220 (inst mov rax-tn #.(- sb!xc:most-negative-fixnum))
221 (inst add rax-tn value)
222 (inst shr rax-tn #.(integer-length (- sb!xc:most-positive-fixnum
223 sb!xc:most-negative-fixnum)))))
225 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
226 ;;; exactly one digit.
228 (define-vop (signed-byte-64-p type-predicate)
229 (:translate signed-byte-64-p)
231 (multiple-value-bind (yep nope)
233 (values not-target target)
234 (values target not-target))
235 (generate-fixnum-test value)
238 (inst and al-tn lowtag-mask)
239 (inst cmp al-tn other-pointer-lowtag)
241 (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
242 (+ (ash 1 n-widetag-bits) bignum-widetag))
243 (inst jmp (if not-p :ne :e) target))
246 (define-vop (check-signed-byte-64 check-type)
248 (let ((nope (generate-error-code vop
249 'object-not-signed-byte-64-error
251 (generate-fixnum-test value)
254 (inst and al-tn lowtag-mask)
255 (inst cmp al-tn other-pointer-lowtag)
257 (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
258 (+ (ash 1 n-widetag-bits) bignum-widetag))
261 (move result value)))
263 ;;; An (unsigned-byte 64) can be represented with either a positive
264 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
265 ;;; exactly two digits and the second digit all zeros.
266 (define-vop (unsigned-byte-64-p type-predicate)
267 (:translate unsigned-byte-64-p)
269 (let ((not-target (gen-label))
270 (single-word (gen-label))
271 (fixnum (gen-label)))
272 (multiple-value-bind (yep nope)
274 (values not-target target)
275 (values target not-target))
277 (generate-fixnum-test value)
281 ;; If not, is it an other pointer?
282 (inst and rax-tn lowtag-mask)
283 (inst cmp rax-tn other-pointer-lowtag)
286 (loadw rax-tn value 0 other-pointer-lowtag)
288 (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
289 (inst jmp :e single-word)
290 ;; If it's other than two, we can't be an (unsigned-byte 64)
291 (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
293 ;; Get the second digit.
294 (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
295 ;; All zeros, its an (unsigned-byte 64).
296 (inst or rax-tn rax-tn)
300 (emit-label single-word)
301 ;; Get the single digit.
302 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
304 ;; positive implies (unsigned-byte 64).
306 (inst or rax-tn rax-tn)
307 (inst jmp (if not-p :s :ns) target)
309 (emit-label not-target)))))
311 (define-vop (check-unsigned-byte-64 check-type)
314 (generate-error-code vop 'object-not-unsigned-byte-64-error value))
317 (single-word (gen-label)))
320 (generate-fixnum-test value)
324 ;; If not, is it an other pointer?
325 (inst and rax-tn lowtag-mask)
326 (inst cmp rax-tn other-pointer-lowtag)
329 (loadw rax-tn value 0 other-pointer-lowtag)
331 (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
332 (inst jmp :e single-word)
333 ;; If it's other than two, we can't be an (unsigned-byte 64)
334 (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
336 ;; Get the second digit.
337 (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
338 ;; All zeros, its an (unsigned-byte 64).
339 (inst or rax-tn rax-tn)
343 (emit-label single-word)
344 ;; Get the single digit.
345 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
347 ;; positive implies (unsigned-byte 64).
349 (inst or rax-tn rax-tn)
353 (move result value))))
355 ;;;; list/symbol types
357 ;;; symbolp (or symbol (eq nil))
358 ;;; consp (and list (not (eq nil)))
360 (define-vop (symbolp type-predicate)
363 (let ((is-symbol-label (if not-p DROP-THRU target)))
364 (inst cmp value nil-value)
365 (inst jmp :e is-symbol-label)
366 (test-type value target not-p (symbol-header-widetag)))
369 (define-vop (check-symbol check-type)
371 (let ((error (generate-error-code vop 'object-not-symbol-error value)))
372 (inst cmp value nil-value)
373 (inst jmp :e DROP-THRU)
374 (test-type value error t (symbol-header-widetag)))
376 (move result value)))
378 (define-vop (consp type-predicate)
381 (let ((is-not-cons-label (if not-p target DROP-THRU)))
382 (inst cmp value nil-value)
383 (inst jmp :e is-not-cons-label)
384 (test-type value target not-p (list-pointer-lowtag)))
387 (define-vop (check-cons check-type)
389 (let ((error (generate-error-code vop 'object-not-cons-error value)))
390 (inst cmp value nil-value)
392 (test-type value error t (list-pointer-lowtag))
393 (move result value))))