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-fixnum-and-immediate
(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 value target not-p immediate drop-through
)))
51 (defun %test-fixnum-immediate-and-headers
(value target not-p immediate
53 (let ((drop-through (gen-label)))
54 (generate-fixnum-test value
)
55 (inst jmp
:z
(if not-p drop-through target
))
56 (%test-immediate-and-headers value target not-p immediate headers
59 (defun %test-immediate
(value target not-p immediate
60 &optional
(drop-through (gen-label)))
61 ;; Code a single instruction byte test if possible.
62 (cond ((sc-is value any-reg descriptor-reg
)
63 (inst cmp
(make-byte-tn value
) immediate
))
66 (inst cmp al-tn immediate
)))
67 (inst jmp
(if not-p
:ne
:e
) target
)
68 (emit-label drop-through
))
70 (defun %test-immediate-and-headers
(value target not-p immediate headers
71 &optional
(drop-through (gen-label)))
72 ;; Code a single instruction byte test if possible.
73 (cond ((sc-is value any-reg descriptor-reg
)
74 (inst cmp
(make-byte-tn value
) immediate
))
77 (inst cmp al-tn immediate
)))
78 (inst jmp
:e
(if not-p drop-through target
))
79 (%test-headers value target not-p nil headers drop-through
))
81 (defun %test-lowtag
(value target not-p lowtag
)
83 (inst and rax-tn lowtag-mask
)
84 (inst cmp rax-tn lowtag
)
85 (inst jmp
(if not-p
:ne
:e
) target
))
87 (defun %test-headers
(value target not-p function-p headers
88 &optional
(drop-through (gen-label)))
89 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag
)))
90 (multiple-value-bind (equal less-or-equal when-true when-false
)
91 ;; EQUAL and LESS-OR-EQUAL are the conditions for branching to TARGET.
92 ;; WHEN-TRUE and WHEN-FALSE are the labels to branch to when we know
93 ;; it's true and when we know it's false respectively.
95 (values :ne
:a drop-through target
)
96 (values :e
:na target drop-through
))
97 (%test-lowtag value when-false t lowtag
)
98 (inst mov al-tn
(make-ea :byte
:base value
:disp
(- lowtag
)))
99 (do ((remaining headers
(cdr remaining
)))
101 (let ((header (car remaining
))
102 (last (null (cdr remaining
))))
105 (inst cmp al-tn header
)
107 (inst jmp equal target
)
108 (inst jmp
:e when-true
)))
110 (let ((start (car header
))
112 (unless (= start bignum-widetag
)
113 (inst cmp al-tn start
)
114 (inst jmp
:b when-false
)) ; was :l
117 (inst jmp less-or-equal target
)
118 (inst jmp
:be when-true
))))))) ; was :le
119 (emit-label drop-through
))))
122 ;;;; type checking and testing
124 (define-vop (check-type)
125 (:args
(value :target result
:scs
(any-reg descriptor-reg
)))
126 (:results
(result :scs
(any-reg descriptor-reg
)))
127 (:temporary
(:sc unsigned-reg
:offset eax-offset
:to
(:result
0)) eax
)
130 (:save-p
:compute-only
))
132 (define-vop (type-predicate)
133 (:args
(value :scs
(any-reg descriptor-reg
)))
134 (:temporary
(:sc unsigned-reg
:offset eax-offset
) eax
)
138 (:policy
:fast-safe
))
140 ;;; simpler VOP that don't need a temporary register
141 (define-vop (simple-check-type)
142 (:args
(value :target result
:scs
(any-reg descriptor-reg
)))
143 (:results
(result :scs
(any-reg descriptor-reg
)
144 :load-if
(not (and (sc-is value any-reg descriptor-reg
)
145 (sc-is result control-stack
)))))
147 (:save-p
:compute-only
))
149 (define-vop (simple-type-predicate)
150 (:args
(value :scs
(any-reg descriptor-reg control-stack
)))
153 (:policy
:fast-safe
))
155 (defun cost-to-test-types (type-codes)
156 (+ (* 2 (length type-codes
))
157 (if (> (apply #'max type-codes
) lowtag-limit
) 7 2)))
159 (defmacro !define-type-vops
(pred-name check-name ptype error-code
161 &key
(variant nil variant-p
) &allow-other-keys
)
162 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
164 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes
)))
165 (prefix (if variant-p
166 (concatenate 'string
(string variant
) "-")
170 `((define-vop (,pred-name
,(intern (concatenate 'string prefix
"TYPE-PREDICATE")))
171 (:translate
,pred-name
)
173 (test-type value target not-p
(,@type-codes
))))))
175 `((define-vop (,check-name
,(intern (concatenate 'string prefix
"CHECK-TYPE")))
178 (generate-error-code vop
,error-code value
)))
179 (test-type value err-lab t
(,@type-codes
))
180 (move result value
))))))
182 `((primitive-type-vop ,check-name
(:check
) ,ptype
))))))
184 ;;;; other integer ranges
186 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate
)
187 (:args
(value :scs
(unsigned-reg)))
188 (:arg-types unsigned-num
)
190 (:temporary
(:sc unsigned-reg
) tmp
)
194 (inst jmp
(if not-p
:nz
:z
) target
)))
196 (define-vop (signed-byte-32-p type-predicate
)
197 (:translate signed-byte-32-p
)
199 ;; (and (fixnum) (or (no bits set >31) (all bits set >31))
202 (inst jmp
:ne
(if not-p target NOT-TARGET
))
203 (inst sar rax-tn
(+ 32 3 -
1))
207 (inst jmp NOT-TARGET
))
208 (inst jmp
:z target
))
211 (inst jmp
(if not-p
:ne
:eq
) target
)
214 (define-vop (check-signed-byte-32 check-type
)
216 (let ((nope (generate-error-code vop
217 object-not-signed-byte-32-error
223 (inst sar rax-tn
(+ 32 3 -
1))
228 (move result value
))))
231 (define-vop (unsigned-byte-32-p type-predicate
)
232 (:translate unsigned-byte-32-p
)
234 ;; (and (fixnum) (no bits set >31))
237 (inst jmp
:ne
(if not-p target NOT-TARGET
))
238 (inst shr rax-tn
(+ 32 sb
!vm
::n-fixnum-tag-bits
))
239 (inst jmp
(if not-p
:nz
:z
) target
)
242 (define-vop (check-unsigned-byte-32 check-type
)
245 (generate-error-code vop object-not-unsigned-byte-32-error value
)))
249 (inst shr rax-tn
(+ 32 sb
!vm
::n-fixnum-tag-bits
))
251 (move result value
))))
253 ;;; An (unsigned-byte 64) can be represented with either a positive
254 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
255 ;;; exactly two digits and the second digit all zeros.
256 (define-vop (unsigned-byte-64-p type-predicate
)
257 (:translate unsigned-byte-64-p
)
259 (let ((not-target (gen-label))
260 (single-word (gen-label))
261 (fixnum (gen-label)))
262 (multiple-value-bind (yep nope
)
264 (values not-target target
)
265 (values target not-target
))
267 (generate-fixnum-test value
)
271 ;; If not, is it an other pointer?
272 (inst and rax-tn lowtag-mask
)
273 (inst cmp rax-tn other-pointer-lowtag
)
276 (loadw rax-tn value
0 other-pointer-lowtag
)
278 (inst cmp rax-tn
(+ (ash 1 n-widetag-bits
) bignum-widetag
))
279 (inst jmp
:e single-word
)
280 ;; If it's other than two, we can't be an (unsigned-byte 64)
281 (inst cmp rax-tn
(+ (ash 2 n-widetag-bits
) bignum-widetag
))
283 ;; Get the second digit.
284 (loadw rax-tn value
(1+ bignum-digits-offset
) other-pointer-lowtag
)
285 ;; All zeros, its an (unsigned-byte 64).
286 (inst or rax-tn rax-tn
)
290 (emit-label single-word
)
291 ;; Get the single digit.
292 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag
)
294 ;; positive implies (unsigned-byte 64).
296 (inst or rax-tn rax-tn
)
297 (inst jmp
(if not-p
:s
:ns
) target
)
299 (emit-label not-target
)))))
301 (define-vop (check-unsigned-byte-64 check-type
)
304 (generate-error-code vop object-not-unsigned-byte-64-error value
))
307 (single-word (gen-label)))
310 (generate-fixnum-test value
)
314 ;; If not, is it an other pointer?
315 (inst and rax-tn lowtag-mask
)
316 (inst cmp rax-tn other-pointer-lowtag
)
319 (loadw rax-tn value
0 other-pointer-lowtag
)
321 (inst cmp rax-tn
(+ (ash 1 n-widetag-bits
) bignum-widetag
))
322 (inst jmp
:e single-word
)
323 ;; If it's other than two, we can't be an (unsigned-byte 64)
324 (inst cmp rax-tn
(+ (ash 2 n-widetag-bits
) bignum-widetag
))
326 ;; Get the second digit.
327 (loadw rax-tn value
(1+ bignum-digits-offset
) other-pointer-lowtag
)
328 ;; All zeros, its an (unsigned-byte 64).
329 (inst or rax-tn rax-tn
)
333 (emit-label single-word
)
334 ;; Get the single digit.
335 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag
)
337 ;; positive implies (unsigned-byte 64).
339 (inst or rax-tn rax-tn
)
343 (move result value
))))
345 ;;;; list/symbol types
347 ;;; symbolp (or symbol (eq nil))
348 ;;; consp (and list (not (eq nil)))
350 (define-vop (symbolp type-predicate
)
353 (let ((is-symbol-label (if not-p DROP-THRU target
)))
354 (inst cmp value nil-value
)
355 (inst jmp
:e is-symbol-label
)
356 (test-type value target not-p
(symbol-header-widetag)))
359 (define-vop (check-symbol check-type
)
361 (let ((error (generate-error-code vop object-not-symbol-error value
)))
362 (inst cmp value nil-value
)
363 (inst jmp
:e DROP-THRU
)
364 (test-type value error t
(symbol-header-widetag)))
366 (move result value
)))
368 (define-vop (consp type-predicate
)
371 (let ((is-not-cons-label (if not-p target DROP-THRU
)))
372 (inst cmp value nil-value
)
373 (inst jmp
:e is-not-cons-label
)
374 (test-type value target not-p
(list-pointer-lowtag)))
377 (define-vop (check-cons check-type
)
379 (let ((error (generate-error-code vop object-not-cons-error value
)))
380 (inst cmp value nil-value
)
382 (test-type value error t
(list-pointer-lowtag))
383 (move result value
))))