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
88 ;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
89 ;; for branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
90 ;; labels to branch to when we know it's true and when we know
91 ;; it's false respectively.
93 (values :ne :a :b drop-through target)
94 (values :e :na :nb target drop-through))
95 (%test-lowtag value when-false t lowtag)
96 (do ((remaining headers (cdr remaining))
97 ;; It is preferable (smaller and faster code) to directly
98 ;; compare the value in memory instead of loading it into
99 ;; a register first. Find out if this is possible and set
100 ;; WIDETAG-TN accordingly. If impossible, generate the
102 ;; Compared to x86 we additionally optimize the cases of a
103 ;; range starting with BIGNUM-WIDETAG or ending with
104 ;; COMPLEX-ARRAY-WIDETAG.
105 (widetag-tn (if (and (null (cdr headers))
106 (or (atom (car headers))
107 (= (caar headers) bignum-widetag)
108 (= (cdar headers) complex-array-widetag)))
109 (make-ea :byte :base value :disp (- lowtag))
111 (inst mov eax-tn (make-ea :dword :base value
115 (let ((header (car remaining))
116 (last (null (cdr remaining))))
119 (inst cmp widetag-tn header)
121 (inst jmp equal target)
122 (inst jmp :e when-true)))
124 (let ((start (car header))
127 ((= start bignum-widetag)
128 (inst cmp widetag-tn end)
130 (inst jmp less-or-equal target)
131 (inst jmp :be when-true)))
132 ((= end complex-array-widetag)
133 (inst cmp widetag-tn start)
135 (inst jmp greater-or-equal target)
136 (inst jmp :b when-false)))
138 (inst cmp al-tn start)
139 (inst jmp :b when-false)
141 (inst jmp :be when-true))
143 (inst sub al-tn start)
144 (inst cmp al-tn (- end start))
145 (inst jmp less-or-equal target))))))))
146 (emit-label drop-through))))
149 ;;;; type checking and testing
151 (define-vop (check-type)
152 (:args (value :target result :scs (any-reg descriptor-reg)))
153 (:results (result :scs (any-reg descriptor-reg)))
154 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
157 (:save-p :compute-only))
159 (define-vop (type-predicate)
160 (:args (value :scs (any-reg descriptor-reg)))
161 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
165 (:policy :fast-safe))
167 ;;; simpler VOP that don't need a temporary register
168 (define-vop (simple-check-type)
169 (:args (value :target result :scs (any-reg descriptor-reg)))
170 (:results (result :scs (any-reg descriptor-reg)
171 :load-if (not (and (sc-is value any-reg descriptor-reg)
172 (sc-is result control-stack)))))
174 (:save-p :compute-only))
176 (define-vop (simple-type-predicate)
177 (:args (value :scs (any-reg descriptor-reg control-stack)))
180 (:policy :fast-safe))
182 (defun cost-to-test-types (type-codes)
183 (+ (* 2 (length type-codes))
184 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
186 (defmacro !define-type-vops (pred-name check-name ptype error-code
188 &key (variant nil variant-p) &allow-other-keys)
189 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
191 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
192 (prefix (if variant-p
193 (concatenate 'string (string variant) "-")
197 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
198 (:translate ,pred-name)
200 (test-type value target not-p (,@type-codes))))))
202 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
205 (generate-error-code vop ',error-code value)))
206 (test-type value err-lab t (,@type-codes))
207 (move result value))))))
209 `((primitive-type-vop ,check-name (:check) ,ptype))))))
211 ;;;; other integer ranges
213 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
214 (:args (value :scs (unsigned-reg)))
215 (:arg-types unsigned-num)
217 (:temporary (:sc unsigned-reg) tmp)
222 (inst shr tmp n-positive-fixnum-bits)))
224 (define-vop (fixnump/signed-byte-64 type-predicate)
225 (:args (value :scs (signed-reg)))
227 (:conditional #.(if (= sb!vm:n-fixnum-tag-bits 1) :ns :z))
228 (:arg-types signed-num)
231 ;; Hackers Delight, p. 53: signed
232 ;; a <= x <= a + 2^n - 1
233 ;; is equivalent to unsigned
235 (inst mov rax-tn #.(- sb!xc:most-negative-fixnum))
236 (inst add rax-tn value)
237 (unless (= n-fixnum-tag-bits 1)
238 (inst shr rax-tn n-fixnum-bits))))
240 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
241 ;;; exactly one digit.
243 (define-vop (signed-byte-64-p type-predicate)
244 (:translate signed-byte-64-p)
246 (multiple-value-bind (yep nope)
248 (values not-target target)
249 (values target not-target))
250 (generate-fixnum-test value)
253 (inst and al-tn lowtag-mask)
254 (inst cmp al-tn other-pointer-lowtag)
256 (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
257 (+ (ash 1 n-widetag-bits) bignum-widetag))
258 (inst jmp (if not-p :ne :e) target))
261 (define-vop (check-signed-byte-64 check-type)
263 (let ((nope (generate-error-code vop
264 'object-not-signed-byte-64-error
266 (generate-fixnum-test value)
269 (inst and al-tn lowtag-mask)
270 (inst cmp al-tn other-pointer-lowtag)
272 (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
273 (+ (ash 1 n-widetag-bits) bignum-widetag))
276 (move result value)))
278 ;;; An (unsigned-byte 64) can be represented with either a positive
279 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
280 ;;; exactly two digits and the second digit all zeros.
281 (define-vop (unsigned-byte-64-p type-predicate)
282 (:translate unsigned-byte-64-p)
284 (let ((not-target (gen-label))
285 (single-word (gen-label))
286 (fixnum (gen-label)))
287 (multiple-value-bind (yep nope)
289 (values not-target target)
290 (values target not-target))
292 (generate-fixnum-test value)
296 ;; If not, is it an other pointer?
297 (inst and rax-tn lowtag-mask)
298 (inst cmp rax-tn other-pointer-lowtag)
301 (loadw rax-tn value 0 other-pointer-lowtag)
303 (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
304 (inst jmp :e single-word)
305 ;; If it's other than two, we can't be an (unsigned-byte 64)
306 (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
308 ;; Get the second digit.
309 (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
310 ;; All zeros, its an (unsigned-byte 64).
311 (inst or rax-tn rax-tn)
315 (emit-label single-word)
316 ;; Get the single digit.
317 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
319 ;; positive implies (unsigned-byte 64).
321 (inst or rax-tn rax-tn)
322 (inst jmp (if not-p :s :ns) target)
324 (emit-label not-target)))))
326 (define-vop (check-unsigned-byte-64 check-type)
329 (generate-error-code vop 'object-not-unsigned-byte-64-error value))
332 (single-word (gen-label)))
335 (generate-fixnum-test value)
339 ;; If not, is it an other pointer?
340 (inst and rax-tn lowtag-mask)
341 (inst cmp rax-tn other-pointer-lowtag)
344 (loadw rax-tn value 0 other-pointer-lowtag)
346 (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
347 (inst jmp :e single-word)
348 ;; If it's other than two, we can't be an (unsigned-byte 64)
349 (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
351 ;; Get the second digit.
352 (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
353 ;; All zeros, its an (unsigned-byte 64).
354 (inst or rax-tn rax-tn)
358 (emit-label single-word)
359 ;; Get the single digit.
360 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
362 ;; positive implies (unsigned-byte 64).
364 (inst or rax-tn rax-tn)
368 (move result value))))
370 ;;;; list/symbol types
372 ;;; symbolp (or symbol (eq nil))
373 ;;; consp (and list (not (eq nil)))
375 (define-vop (symbolp type-predicate)
378 (let ((is-symbol-label (if not-p DROP-THRU target)))
379 (inst cmp value nil-value)
380 (inst jmp :e is-symbol-label)
381 (test-type value target not-p (symbol-header-widetag)))
384 (define-vop (check-symbol check-type)
386 (let ((error (generate-error-code vop 'object-not-symbol-error value)))
387 (inst cmp value nil-value)
388 (inst jmp :e DROP-THRU)
389 (test-type value error t (symbol-header-widetag)))
391 (move result value)))
393 (define-vop (consp type-predicate)
396 (let ((is-not-cons-label (if not-p target DROP-THRU)))
397 (inst cmp value nil-value)
398 (inst jmp :e is-not-cons-label)
399 (test-type value target not-p (list-pointer-lowtag)))
402 (define-vop (check-cons check-type)
404 (let ((error (generate-error-code vop 'object-not-cons-error value)))
405 (inst cmp value nil-value)
407 (test-type value error t (list-pointer-lowtag))
408 (move result value))))