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 ;;; Optimize the case of moving a 64-bit value into RAX when not caring
17 ;;; about the upper 32 bits: often the REX prefix can be spared.
18 (defun move-qword-to-eax (value)
19 (if (and (sc-is value any-reg descriptor-reg
)
20 (< (tn-offset value
) r8-offset
))
21 (move eax-tn
(reg-in-size value
:dword
))
24 (defun generate-fixnum-test (value)
26 "Set the Z flag if VALUE is fixnum"
28 (cond ((sc-is value any-reg descriptor-reg
)
29 (reg-in-size value
:byte
))
30 ;; This is hooey. None of the type-vops presently allow
31 ;; control-stack as a storage class.
32 ((sc-is value control-stack
)
33 (make-ea :byte
:base rbp-tn
34 :disp
(frame-byte-offset (tn-offset value
))))
39 (defun %test-fixnum
(value target not-p
)
40 (generate-fixnum-test value
)
41 (inst jmp
(if not-p
:nz
:z
) target
))
43 ;;; General FIXME: it's fine that we wire these to use rAX which has
44 ;;; the shortest encoding, but for goodness sake can we pass the TN
45 ;;; from the VOP like every other backend does? Freely referencing the
46 ;;; permanent globals RAX-TN,EAX-TN,AL-TN is a bad way to go about it.
48 (defun %lea-for-lowtag-test
(target value lowtag
)
49 (inst lea target
(make-ea :dword
:base value
:disp
(- lowtag
))))
51 ;; Numerics including fixnum, excluding short-float. (INTEGER,RATIONAL)
52 (defun %test-fixnum-and-headers
(value target not-p headers
)
53 (let ((drop-through (gen-label)))
54 (case n-fixnum-tag-bits
55 (1 (%lea-for-lowtag-test eax-tn value other-pointer-lowtag
)
57 (inst jmp
:nz
(if not-p drop-through target
)) ; inverted
58 (%test-headers value target not-p nil headers
59 :drop-through drop-through
:compute-eax nil
))
61 (generate-fixnum-test value
)
62 (inst jmp
:z
(if not-p drop-through target
))
63 (%test-headers value target not-p nil headers
64 :drop-through drop-through
)))))
66 ;; I can see no reason this would ever be used.
67 ;; (or fixnum character|unbound-marker) is implausible.
68 (defun %test-fixnum-and-immediate
(value target not-p immediate
)
69 (let ((drop-through (gen-label)))
70 (generate-fixnum-test value
)
71 (inst jmp
:z
(if not-p drop-through target
))
72 (%test-immediate value target not-p immediate drop-through
)))
75 (defun %test-fixnum-immediate-and-headers
(value target not-p immediate
77 (let ((drop-through (gen-label)))
78 (case n-fixnum-tag-bits
79 (1 (%lea-for-lowtag-test eax-tn value other-pointer-lowtag
)
81 (inst jmp
:nz
(if not-p drop-through target
)) ; inverted
82 (inst cmp al-tn
(- immediate other-pointer-lowtag
))
83 (inst jmp
:e
(if not-p drop-through target
))
84 (%test-headers value target not-p nil headers
85 :drop-through drop-through
:compute-eax nil
))
86 (t (generate-fixnum-test value
)
87 (inst jmp
:z
(if not-p drop-through target
))
88 (%test-immediate-and-headers value target not-p immediate headers
91 (defun %test-immediate
(value target not-p immediate
92 &optional
(drop-through (gen-label)))
93 ;; Code a single instruction byte test if possible.
94 (cond ((sc-is value any-reg descriptor-reg
)
95 (inst cmp
(reg-in-size value
:byte
) immediate
))
98 (inst cmp al-tn immediate
)))
99 (inst jmp
(if not-p
:ne
:e
) target
)
100 (emit-label drop-through
))
102 ;; Numerics including short-float, excluding fixnum
103 (defun %test-immediate-and-headers
(value target not-p immediate headers
104 &optional
(drop-through (gen-label)))
105 ;; Code a single instruction byte test if possible.
106 (cond ((sc-is value any-reg descriptor-reg
)
107 (inst cmp
(reg-in-size value
:byte
) immediate
))
110 (inst cmp al-tn immediate
)))
111 (inst jmp
:e
(if not-p drop-through target
))
112 (%test-headers value target not-p nil headers
:drop-through drop-through
))
114 (defun %test-lowtag
(value target not-p lowtag
)
115 (%lea-for-lowtag-test eax-tn value lowtag
)
116 (inst test al-tn lowtag-mask
)
117 (inst jmp
(if not-p
:nz
:z
) target
))
119 (defun %test-headers
(value target not-p function-p headers
121 (drop-through (gen-label))
123 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag
)))
124 (multiple-value-bind (equal less-or-equal greater-or-equal when-true
126 ;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
127 ;; for branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
128 ;; labels to branch to when we know it's true and when we know
129 ;; it's false respectively.
131 (values :ne
:a
:b drop-through target
)
132 (values :e
:na
:nb target drop-through
))
134 (%lea-for-lowtag-test eax-tn value lowtag
))
135 (inst test al-tn lowtag-mask
)
136 (inst jmp
:nz when-false
)
137 ;; FIXME: this backend seems to be missing the special logic for
138 ;; testing exactly two widetags differing only in a single bit,
139 ;; which through evolution is almost totally unworkable anyway...
140 (do ((remaining headers
(cdr remaining
))
141 ;; It is preferable (smaller and faster code) to directly
142 ;; compare the value in memory instead of loading it into
143 ;; a register first. Find out if this is possible and set
144 ;; WIDETAG-TN accordingly. If impossible, generate the
146 ;; Compared to x86 we additionally optimize the cases of a
147 ;; range starting with BIGNUM-WIDETAG (= min widetag)
148 ;; or ending with COMPLEX-ARRAY-WIDETAG (= max widetag)
149 (widetag-tn (if (and (null (cdr headers
))
151 (or (atom (car headers
))
152 (= (caar headers
) bignum-widetag
)
153 (= (cdar headers
) complex-array-widetag
)))
154 (make-ea :byte
:base value
:disp
(- lowtag
))
156 (inst mov eax-tn
(make-ea :dword
:base value
160 (dolist (widetag except
) ; only after loading widetag-tn
161 (inst cmp al-tn widetag
)
162 (inst jmp
:e when-false
))
164 (let ((header (car remaining
))
165 (last (null (cdr remaining
))))
168 (inst cmp widetag-tn header
)
170 (inst jmp equal target
)
171 (inst jmp
:e when-true
)))
173 (let ((start (car header
))
176 ((= start bignum-widetag
)
177 (inst cmp widetag-tn end
)
179 (inst jmp less-or-equal target
)
180 (inst jmp
:be when-true
)))
181 ((= end complex-array-widetag
)
182 (inst cmp widetag-tn start
)
184 (inst jmp greater-or-equal target
)
185 (inst jmp
:b when-false
)))
187 (inst cmp al-tn start
)
188 (inst jmp
:b when-false
)
190 (inst jmp
:be when-true
))
192 (inst sub al-tn start
)
193 (inst cmp al-tn
(- end start
))
194 (inst jmp less-or-equal target
))))))))
195 (emit-label drop-through
))))
198 ;;;; type checking and testing
200 (define-vop (check-type)
201 (:args
(value :target result
:scs
(any-reg descriptor-reg
)))
202 (:results
(result :scs
(any-reg descriptor-reg
)))
203 (:temporary
(:sc unsigned-reg
:offset eax-offset
:to
(:result
0)) eax
)
206 (:save-p
:compute-only
))
208 (define-vop (type-predicate)
209 (:args
(value :scs
(any-reg descriptor-reg
)))
210 (:temporary
(:sc unsigned-reg
:offset eax-offset
) eax
)
214 (:policy
:fast-safe
))
216 ;;; simpler VOP that don't need a temporary register
217 (define-vop (simple-check-type)
218 (:args
(value :target result
:scs
(any-reg descriptor-reg
)))
219 (:results
(result :scs
(any-reg descriptor-reg
)
220 :load-if
(not (and (sc-is value any-reg descriptor-reg
)
221 (sc-is result control-stack
)))))
223 (:save-p
:compute-only
))
225 (define-vop (simple-type-predicate)
226 (:args
(value :scs
(any-reg descriptor-reg control-stack
)))
229 (:policy
:fast-safe
))
231 (defmacro !define-type-vops
(pred-name check-name ptype error-code
233 &key
(variant nil variant-p
) &allow-other-keys
)
234 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
236 (flet ((cost-to-test-types (type-codes)
237 (+ (* 2 (length type-codes
))
238 (if (> (apply #'max type-codes
) lowtag-limit
) 7 2))))
239 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes
)))
240 (prefix (if variant-p
241 (concatenate 'string
(string variant
) "-")
245 `((define-vop (,pred-name
,(intern (concatenate 'string prefix
"TYPE-PREDICATE")))
246 (:translate
,pred-name
)
248 (test-type value target not-p
(,@type-codes
))))))
250 `((define-vop (,check-name
,(intern (concatenate 'string prefix
"CHECK-TYPE")))
253 (generate-error-code vop
',error-code value
)))
254 (test-type value err-lab t
(,@type-codes
))
255 (move result value
))))))
257 `((primitive-type-vop ,check-name
(:check
) ,ptype
)))))))
259 ;;;; other integer ranges
261 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate
)
262 (:args
(value :scs
(unsigned-reg)))
263 (:arg-types unsigned-num
)
265 (:temporary
(:sc unsigned-reg
:from
(:argument
0)) tmp
)
270 (inst shr tmp n-positive-fixnum-bits
)))
272 #-
#.
(cl:if
(cl:= sb
!vm
:n-fixnum-tag-bits
1) '(:and
) '(:or
))
273 (define-vop (fixnump/signed-byte-64 simple-type-predicate
)
274 (:args
(value :scs
(signed-reg)))
277 (:temporary
(:sc unsigned-reg
) temp
)
278 (:arg-types signed-num
)
281 ;; Hackers Delight, p. 53: signed
282 ;; a <= x <= a + 2^n - 1
283 ;; is equivalent to unsigned
285 (inst mov temp
#.
(- sb
!xc
:most-negative-fixnum
))
286 (inst add temp value
)
287 (inst shr temp n-fixnum-bits
)))
289 #+#.
(cl:if
(cl:= sb
!vm
:n-fixnum-tag-bits
1) '(:and
) '(:or
))
290 (define-vop (fixnump/signed-byte-64 simple-type-predicate
)
291 (:args
(value :scs
(signed-reg) :target temp
))
294 (:temporary
(:sc unsigned-reg
:from
(:argument
0)) temp
)
295 (:arg-types signed-num
)
299 ;; The overflow flag will be set if the reg's sign bit changes.
302 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
303 ;;; exactly one digit.
305 (define-vop (signed-byte-64-p type-predicate
)
306 (:translate signed-byte-64-p
)
308 (multiple-value-bind (yep nope
)
310 (values not-target target
)
311 (values target not-target
))
312 #.
(case n-fixnum-tag-bits
314 (%lea-for-lowtag-test eax-tn value other-pointer-lowtag
)
315 (inst test al-tn fixnum-tag-mask
) ; 0th bit = 1 => fixnum
317 (inst test al-tn lowtag-mask
)))
319 (move-qword-to-eax value
)
320 (inst test al-tn fixnum-tag-mask
)
322 (inst and al-tn lowtag-mask
)
323 (inst cmp al-tn other-pointer-lowtag
))))
325 (inst cmp
(make-ea-for-object-slot value
0 other-pointer-lowtag
)
326 (+ (ash 1 n-widetag-bits
) bignum-widetag
))
327 (inst jmp
(if not-p
:ne
:e
) target
))
330 ;; FIXME: this vop is never emitted. I suspect that is because whenever
331 ;; we have something which needs to be asserted as (SIGNED-BYTE 64) and
332 ;; then moved to a signed-reg, the signed-byte-64-p vop is used and then
333 ;; move-to-word. We apparently never want to both assert the type and
334 ;; keep it as a tagged object. Anyway if it ever were emitted before,
335 ;; GENERATE-ERROR-CODE would have failed since OBJECT-NOT-SIGNED-BYTE-64
336 ;; did not have an error number.
337 (define-vop (check-signed-byte-64 check-type
)
339 (let ((nope (generate-error-code vop
340 'object-not-signed-byte-64-error
342 (generate-fixnum-test value
)
344 (move-qword-to-eax value
)
345 (inst and al-tn lowtag-mask
)
346 (inst cmp al-tn other-pointer-lowtag
)
348 (inst cmp
(make-ea-for-object-slot value
0 other-pointer-lowtag
)
349 (+ (ash 1 n-widetag-bits
) bignum-widetag
))
352 (move result value
)))
354 ;;; An (unsigned-byte 64) can be represented with either a positive
355 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
356 ;;; exactly two digits and the second digit all zeros.
357 (define-vop (unsigned-byte-64-p type-predicate
)
358 (:translate unsigned-byte-64-p
)
360 (let ((not-target (gen-label))
361 (single-word (gen-label))
362 (fixnum (gen-label)))
363 (multiple-value-bind (yep nope
)
365 (values not-target target
)
366 (values target not-target
))
369 (inst test al-tn fixnum-tag-mask
)
372 ;; If not, is it an other pointer?
373 (inst and al-tn lowtag-mask
)
374 (inst cmp al-tn other-pointer-lowtag
)
377 (loadw rax-tn value
0 other-pointer-lowtag
)
379 (inst cmp rax-tn
(+ (ash 1 n-widetag-bits
) bignum-widetag
))
380 (inst jmp
:e single-word
)
381 ;; If it's other than two, we can't be an (unsigned-byte 64)
382 (inst cmp rax-tn
(+ (ash 2 n-widetag-bits
) bignum-widetag
))
384 ;; Get the second digit.
385 (loadw rax-tn value
(1+ bignum-digits-offset
) other-pointer-lowtag
)
386 ;; All zeros, its an (unsigned-byte 64).
387 (inst test rax-tn rax-tn
)
391 (emit-label single-word
)
392 ;; Get the single digit.
393 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag
)
395 ;; positive implies (unsigned-byte 64).
397 (inst test rax-tn rax-tn
)
398 (inst jmp
(if not-p
:s
:ns
) target
)
400 (emit-label not-target
)))))
402 (define-vop (check-unsigned-byte-64 check-type
)
405 (generate-error-code vop
'object-not-unsigned-byte-64-error value
))
408 (single-word (gen-label)))
411 (generate-fixnum-test value
)
415 ;; If not, is it an other pointer?
416 (inst and al-tn lowtag-mask
)
417 (inst cmp al-tn other-pointer-lowtag
)
420 (loadw rax-tn value
0 other-pointer-lowtag
)
422 (inst cmp rax-tn
(+ (ash 1 n-widetag-bits
) bignum-widetag
))
423 (inst jmp
:e single-word
)
424 ;; If it's other than two, we can't be an (unsigned-byte 64)
425 (inst cmp rax-tn
(+ (ash 2 n-widetag-bits
) bignum-widetag
))
427 ;; Get the second digit.
428 (loadw rax-tn value
(1+ bignum-digits-offset
) other-pointer-lowtag
)
429 ;; All zeros, its an (unsigned-byte 64).
430 (inst test rax-tn rax-tn
)
434 (emit-label single-word
)
435 ;; Get the single digit.
436 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag
)
438 ;; positive implies (unsigned-byte 64).
440 (inst test rax-tn rax-tn
)
444 (move result value
))))
446 (defun power-of-two-limit-p (x)
448 (= (logcount (1+ x
)) 1)))
450 (define-vop (test-fixnum-mod-power-of-two)
451 (:args
(value :scs
(any-reg descriptor-reg
452 unsigned-reg signed-reg
455 (:constant
(satisfies power-of-two-limit-p
)))
456 (:translate fixnum-mod-p
)
459 (:save-p
:compute-only
)
462 (aver (not (sc-is value immediate
)))
463 (let* ((fixnum-hi (if (sc-is value unsigned-reg signed-reg
)
466 (inst test value
(constantize (lognot fixnum-hi
))))))
468 (define-vop (test-fixnum-mod-tagged-unsigned)
469 (:args
(value :scs
(any-reg descriptor-reg
470 unsigned-reg signed-reg
472 (:arg-types
(:or tagged-num unsigned-num signed-num
)
474 (:translate fixnum-mod-p
)
477 (:save-p
:compute-only
)
480 (aver (not (sc-is value immediate
)))
481 (let ((fixnum-hi (if (sc-is value unsigned-reg signed-reg
)
484 (inst cmp value
(constantize fixnum-hi
)))))
486 (define-vop (test-fixnum-mod-*)
487 (:args
(value :scs
(any-reg descriptor-reg
)))
488 (:arg-types
* (:constant fixnum
))
489 (:translate fixnum-mod-p
)
491 (:info target not-p hi
)
492 (:save-p
:compute-only
)
495 (let* ((fixnum-hi (fixnumize hi
))
497 (generate-fixnum-test value
)
498 (inst jmp
:ne
(if not-p target skip
))
499 (inst cmp value
(constantize fixnum-hi
))
500 (inst jmp
(if not-p
:a
:be
) target
)
503 ;;;; list/symbol types
505 ;;; symbolp (or symbol (eq nil))
506 ;;; consp (and list (not (eq nil)))
508 (define-vop (symbolp type-predicate
)
511 (let ((is-symbol-label (if not-p DROP-THRU target
)))
512 (inst cmp value nil-value
)
513 (inst jmp
:e is-symbol-label
)
514 (test-type value target not-p
(symbol-header-widetag)))
517 (define-vop (check-symbol check-type
)
519 (let ((error (generate-error-code vop
'object-not-symbol-error value
)))
520 (inst cmp value nil-value
)
521 (inst jmp
:e DROP-THRU
)
522 (test-type value error t
(symbol-header-widetag)))
524 (move result value
)))
526 (define-vop (consp type-predicate
)
529 (let ((is-not-cons-label (if not-p target DROP-THRU
)))
530 (inst cmp value nil-value
)
531 (inst jmp
:e is-not-cons-label
)
532 (test-type value target not-p
(list-pointer-lowtag)))
535 (define-vop (check-cons check-type
)
537 (let ((error (generate-error-code vop
'object-not-cons-error value
)))
538 (inst cmp value nil-value
)
540 (test-type value error t
(list-pointer-lowtag))
541 (move result value
))))
543 ;; A vop that accepts a computed set of widetags.
544 (define-vop (%other-pointer-subtype-p type-predicate
)
545 (:translate %other-pointer-subtype-p
)
546 (:info target not-p widetags
)
547 (:arg-types
* (:constant t
)) ; voodoo - 'target' and 'not-p' are absent
548 (:generator
15 ; arbitrary
549 (multiple-value-bind (headers exceptions
)
550 (canonicalize-headers-and-exceptions widetags
)
551 (%test-headers value target not-p nil headers
552 :except exceptions
))))
556 (!define-type-vops simd-pack-p nil nil nil
(simd-pack-widetag))
558 (define-vop (check-simd-pack check-type
)
559 (:args
(value :target result
560 :scs
(any-reg descriptor-reg
561 int-sse-reg single-sse-reg double-sse-reg
562 int-sse-stack single-sse-stack double-sse-stack
)))
563 (:results
(result :scs
(any-reg descriptor-reg
564 int-sse-reg single-sse-reg double-sse-reg
)))
565 (:temporary
(:sc unsigned-reg
:offset eax-offset
:to
(:result
0)) eax
)
569 (:save-p
:compute-only
)
572 ((int-sse-reg single-sse-reg double-sse-reg
573 int-sse-stack single-sse-stack double-sse-stack
)
575 ((int-sse-reg single-sse-reg double-sse-reg
)
577 ((any-reg descriptor-reg
)
578 (with-fixed-allocation (result
582 ;; see *simd-pack-element-types*
585 ((int-sse-reg int-sse-stack
) 0)
586 ((single-sse-reg single-sse-stack
) 1)
587 ((double-sse-reg double-sse-stack
) 2)))
588 result simd-pack-tag-slot other-pointer-lowtag
)
589 (let ((ea (make-ea-for-object-slot
590 result simd-pack-lo-value-slot other-pointer-lowtag
)))
591 (if (float-simd-pack-p value
)
592 (inst movaps ea value
)
593 (inst movdqa ea value
)))))))
594 ((any-reg descriptor-reg
)
595 (let ((leaf (sb!c
::tn-leaf value
)))
596 (unless (and (sb!c
::lvar-p leaf
)
597 (csubtypep (sb!c
::lvar-type leaf
)
598 (specifier-type 'simd-pack
)))
601 (generate-error-code vop
'object-not-simd-pack-error value
)
602 t
(simd-pack-widetag))))
605 (let ((ea (make-ea-for-object-slot
606 value simd-pack-lo-value-slot other-pointer-lowtag
)))
607 (inst movdqa result ea
)))
608 ((single-sse-reg double-sse-reg
)
609 (let ((ea (make-ea-for-object-slot
610 value simd-pack-lo-value-slot other-pointer-lowtag
)))
611 (inst movaps result ea
)))
612 ((any-reg descriptor-reg
)
613 (move result value
)))))))
615 (primitive-type-vop check-simd-pack
(:check
) simd-pack-int simd-pack-single simd-pack-double
))