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 make-dword-tn (tn)
23 (aver (sc-is tn any-reg descriptor-reg unsigned-reg signed-reg))
24 (make-random-tn :kind :normal
25 :sc (sc-or-lose 'dword-reg)
26 :offset (tn-offset tn)))
28 (defun generate-fixnum-test (value)
29 "zero flag set if VALUE is fixnum"
31 (cond ((sc-is value any-reg descriptor-reg)
33 ((sc-is value control-stack)
34 (make-ea :byte :base rbp-tn
35 :disp (- (* (1+ (tn-offset value)) n-word-bytes))))
38 sb!vm::fixnum-tag-mask))
40 (defun %test-fixnum (value target not-p)
41 (generate-fixnum-test value)
42 (inst jmp (if not-p :nz :z) target))
44 (defun %test-fixnum-and-headers (value target not-p headers)
45 (let ((drop-through (gen-label)))
46 (generate-fixnum-test value)
47 (inst jmp :z (if not-p drop-through target))
48 (%test-headers value target not-p nil headers drop-through)))
50 (defun %test-fixnum-and-immediate (value target not-p immediate)
51 (let ((drop-through (gen-label)))
52 (generate-fixnum-test value)
53 (inst jmp :z (if not-p drop-through target))
54 (%test-immediate value target not-p immediate drop-through)))
56 (defun %test-fixnum-immediate-and-headers (value target not-p immediate
58 (let ((drop-through (gen-label)))
59 (generate-fixnum-test value)
60 (inst jmp :z (if not-p drop-through target))
61 (%test-immediate-and-headers value target not-p immediate headers
64 (defun %test-immediate (value target not-p immediate
65 &optional (drop-through (gen-label)))
66 ;; Code a single instruction byte test if possible.
67 (cond ((sc-is value any-reg descriptor-reg)
68 (inst cmp (make-byte-tn value) immediate))
71 (inst cmp al-tn immediate)))
72 (inst jmp (if not-p :ne :e) target)
73 (emit-label drop-through))
75 (defun %test-immediate-and-headers (value target not-p immediate headers
76 &optional (drop-through (gen-label)))
77 ;; Code a single instruction byte test if possible.
78 (cond ((sc-is value any-reg descriptor-reg)
79 (inst cmp (make-byte-tn value) immediate))
82 (inst cmp al-tn immediate)))
83 (inst jmp :e (if not-p drop-through target))
84 (%test-headers value target not-p nil headers drop-through))
86 (defun %test-lowtag (value target not-p lowtag)
87 (if (and (sc-is value any-reg descriptor-reg)
88 (< (tn-offset value) r8-offset))
89 (move eax-tn (make-dword-tn value)) ; shorter encoding (no REX prefix)
91 (inst and al-tn lowtag-mask)
92 (inst cmp al-tn lowtag)
93 (inst jmp (if not-p :ne :e) target))
95 (defun %test-headers (value target not-p function-p headers
96 &optional (drop-through (gen-label)))
97 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
98 (multiple-value-bind (equal less-or-equal when-true when-false)
99 ;; EQUAL and LESS-OR-EQUAL are the conditions for branching to TARGET.
100 ;; WHEN-TRUE and WHEN-FALSE are the labels to branch to when we know
101 ;; it's true and when we know it's false respectively.
103 (values :ne :a drop-through target)
104 (values :e :na target drop-through))
105 (%test-lowtag value when-false t lowtag)
106 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
107 (do ((remaining headers (cdr remaining)))
109 (let ((header (car remaining))
110 (last (null (cdr remaining))))
113 (inst cmp al-tn header)
115 (inst jmp equal target)
116 (inst jmp :e when-true)))
118 (let ((start (car header))
120 (unless (= start bignum-widetag)
121 (inst cmp al-tn start)
122 (inst jmp :b when-false)) ; was :l
125 (inst jmp less-or-equal target)
126 (inst jmp :be when-true))))))) ; was :le
127 (emit-label drop-through))))
130 ;;;; type checking and testing
132 (define-vop (check-type)
133 (:args (value :target result :scs (any-reg descriptor-reg)))
134 (:results (result :scs (any-reg descriptor-reg)))
135 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
138 (:save-p :compute-only))
140 (define-vop (type-predicate)
141 (:args (value :scs (any-reg descriptor-reg)))
142 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
146 (:policy :fast-safe))
148 ;;; simpler VOP that don't need a temporary register
149 (define-vop (simple-check-type)
150 (:args (value :target result :scs (any-reg descriptor-reg)))
151 (:results (result :scs (any-reg descriptor-reg)
152 :load-if (not (and (sc-is value any-reg descriptor-reg)
153 (sc-is result control-stack)))))
155 (:save-p :compute-only))
157 (define-vop (simple-type-predicate)
158 (:args (value :scs (any-reg descriptor-reg control-stack)))
161 (:policy :fast-safe))
163 (defun cost-to-test-types (type-codes)
164 (+ (* 2 (length type-codes))
165 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
167 (defmacro !define-type-vops (pred-name check-name ptype error-code
169 &key (variant nil variant-p) &allow-other-keys)
170 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
172 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
173 (prefix (if variant-p
174 (concatenate 'string (string variant) "-")
178 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
179 (:translate ,pred-name)
181 (test-type value target not-p (,@type-codes))))))
183 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
186 (generate-error-code vop ,error-code value)))
187 (test-type value err-lab t (,@type-codes))
188 (move result value))))))
190 `((primitive-type-vop ,check-name (:check) ,ptype))))))
192 ;;;; other integer ranges
194 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
195 (:args (value :scs (unsigned-reg)))
196 (:arg-types unsigned-num)
198 (:temporary (:sc unsigned-reg) tmp)
201 (inst shr tmp n-positive-fixnum-bits)
202 (inst jmp (if not-p :nz :z) target)))
204 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
205 ;;; exactly one digit.
207 (define-vop (signed-byte-64-p type-predicate)
208 (:translate signed-byte-64-p)
210 (multiple-value-bind (yep nope)
212 (values not-target target)
213 (values target not-target))
214 (generate-fixnum-test value)
217 (inst and al-tn lowtag-mask)
218 (inst cmp al-tn other-pointer-lowtag)
220 (loadw rax-tn value 0 other-pointer-lowtag)
221 (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
222 (inst jmp (if not-p :ne :e) target))
225 (define-vop (check-signed-byte-64 check-type)
227 (let ((nope (generate-error-code vop
228 object-not-signed-byte-64-error
230 (generate-fixnum-test value)
233 (inst and al-tn lowtag-mask)
234 (inst cmp al-tn other-pointer-lowtag)
236 (loadw rax-tn value 0 other-pointer-lowtag)
237 (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
240 (move result value)))
242 ;;; An (unsigned-byte 64) can be represented with either a positive
243 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
244 ;;; exactly two digits and the second digit all zeros.
245 (define-vop (unsigned-byte-64-p type-predicate)
246 (:translate unsigned-byte-64-p)
248 (let ((not-target (gen-label))
249 (single-word (gen-label))
250 (fixnum (gen-label)))
251 (multiple-value-bind (yep nope)
253 (values not-target target)
254 (values target not-target))
256 (generate-fixnum-test value)
260 ;; If not, is it an other pointer?
261 (inst and rax-tn lowtag-mask)
262 (inst cmp rax-tn other-pointer-lowtag)
265 (loadw rax-tn value 0 other-pointer-lowtag)
267 (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
268 (inst jmp :e single-word)
269 ;; If it's other than two, we can't be an (unsigned-byte 64)
270 (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
272 ;; Get the second digit.
273 (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
274 ;; All zeros, its an (unsigned-byte 64).
275 (inst or rax-tn rax-tn)
279 (emit-label single-word)
280 ;; Get the single digit.
281 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
283 ;; positive implies (unsigned-byte 64).
285 (inst or rax-tn rax-tn)
286 (inst jmp (if not-p :s :ns) target)
288 (emit-label not-target)))))
290 (define-vop (check-unsigned-byte-64 check-type)
293 (generate-error-code vop object-not-unsigned-byte-64-error value))
296 (single-word (gen-label)))
299 (generate-fixnum-test value)
303 ;; If not, is it an other pointer?
304 (inst and rax-tn lowtag-mask)
305 (inst cmp rax-tn other-pointer-lowtag)
308 (loadw rax-tn value 0 other-pointer-lowtag)
310 (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
311 (inst jmp :e single-word)
312 ;; If it's other than two, we can't be an (unsigned-byte 64)
313 (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
315 ;; Get the second digit.
316 (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
317 ;; All zeros, its an (unsigned-byte 64).
318 (inst or rax-tn rax-tn)
322 (emit-label single-word)
323 ;; Get the single digit.
324 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
326 ;; positive implies (unsigned-byte 64).
328 (inst or rax-tn rax-tn)
332 (move result value))))
334 ;;;; list/symbol types
336 ;;; symbolp (or symbol (eq nil))
337 ;;; consp (and list (not (eq nil)))
339 (define-vop (symbolp type-predicate)
342 (let ((is-symbol-label (if not-p DROP-THRU target)))
343 (inst cmp value nil-value)
344 (inst jmp :e is-symbol-label)
345 (test-type value target not-p (symbol-header-widetag)))
348 (define-vop (check-symbol check-type)
350 (let ((error (generate-error-code vop object-not-symbol-error value)))
351 (inst cmp value nil-value)
352 (inst jmp :e DROP-THRU)
353 (test-type value error t (symbol-header-widetag)))
355 (move result value)))
357 (define-vop (consp type-predicate)
360 (let ((is-not-cons-label (if not-p target DROP-THRU)))
361 (inst cmp value nil-value)
362 (inst jmp :e is-not-cons-label)
363 (test-type value target not-p (list-pointer-lowtag)))
366 (define-vop (check-cons check-type)
368 (let ((error (generate-error-code vop object-not-cons-error value)))
369 (inst cmp value nil-value)
371 (test-type value error t (list-pointer-lowtag))
372 (move result value))))