1 ;;;; type testing and checking VOPs for the MIPS 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.
15 (defun %test-fixnum (value target not-p &key temp)
17 (inst and temp value fixnum-tag-mask)
19 (inst bne temp target)
20 (inst beq temp target))
23 (defun %test-fixnum-and-headers (value target not-p headers &key temp)
24 (let ((drop-through (gen-label)))
26 (inst and temp value fixnum-tag-mask)
27 (inst beq temp (if not-p drop-through target)))
28 (%test-headers value target not-p nil headers
29 :drop-through drop-through :temp temp)))
31 (defun %test-immediate (value target not-p immediate &key temp)
33 (inst and temp value widetag-mask)
34 (inst xor temp immediate)
36 (inst bne temp target)
37 (inst beq temp target))
40 (defun %test-lowtag (value target not-p lowtag &key temp)
42 (inst and temp value lowtag-mask)
43 (inst xor temp lowtag)
45 (inst bne temp target)
46 (inst beq temp target))
49 (defun %test-headers (value target not-p function-p headers
50 &key (drop-through (gen-label)) temp)
51 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
53 (when-true when-false)
54 ;; WHEN-TRUE and WHEN-FALSE are the labels to branch to when
55 ;; we know it's true and when we know it's false respectively.
57 (values drop-through target)
58 (values target drop-through))
60 (%test-lowtag value when-false t lowtag :temp temp)
61 (load-type temp value (- lowtag))
64 (do ((remaining headers (cdr remaining)))
66 (let ((header (car remaining))
67 (last (null (cdr remaining))))
70 (inst subu temp (- header delta))
74 (inst bne temp target)
75 (inst beq temp target))
76 (inst beq temp when-true)))
78 (let ((start (car header))
80 (unless (= start bignum-widetag)
81 (inst subu temp (- start delta))
83 (inst bltz temp when-false))
84 (inst subu temp (- end delta))
88 (inst bgtz temp target)
89 (inst blez temp target))
90 (inst blez temp when-true))))))))
92 (emit-label drop-through)))))
96 ;;; Type checking and testing (see also the use of !DEFINE-TYPE-VOPS
97 ;;; in src/compiler/generic/late-type-vops.lisp):
99 ;;; [FIXME: Like some of the other comments in this file, this one
100 ;;; really belongs somewhere else]
101 (define-vop (check-type)
102 (:args (value :target result :scs (any-reg descriptor-reg)))
103 (:results (result :scs (any-reg descriptor-reg)))
104 (:temporary (:scs (non-descriptor-reg) :to (:result 0)) temp)
106 (:save-p :compute-only))
108 (define-vop (type-predicate)
109 (:args (value :scs (any-reg descriptor-reg)))
110 (:temporary (:scs (non-descriptor-reg)) temp)
113 (:policy :fast-safe))
115 (defun cost-to-test-types (type-codes)
116 (+ (* 2 (length type-codes))
117 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
119 (defmacro !define-type-vops (pred-name check-name ptype error-code
121 &key &allow-other-keys)
122 (let ((cost (cost-to-test-types (mapcar #'eval type-codes))))
125 `((define-vop (,pred-name type-predicate)
126 (:translate ,pred-name)
128 (test-type value target not-p (,@type-codes)
131 `((define-vop (,check-name check-type)
134 (generate-error-code vop ,error-code value)))
135 (test-type value err-lab t (,@type-codes)
137 (move result value))))))
139 `((primitive-type-vop ,check-name (:check) ,ptype))))))
141 ;;;; TYPE-VOPs for types that are more complex to test for than simple
142 ;;;; LOWTAG and WIDETAG tests, but that are nevertheless important:
144 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a
145 ;;; bignum with exactly one digit.
146 (defun signed-byte-32-test (value temp not-p target not-target)
150 (values not-target target)
151 (values target not-target))
153 (inst and temp value fixnum-tag-mask)
155 (inst and temp value lowtag-mask)
156 (inst xor temp other-pointer-lowtag)
159 (loadw temp value 0 other-pointer-lowtag)
160 (inst xor temp (+ (ash 1 n-widetag-bits) bignum-widetag))
162 (inst bne temp target)
163 (inst beq temp target))
167 (define-vop (signed-byte-32-p type-predicate)
168 (:translate signed-byte-32-p)
170 (signed-byte-32-test value temp not-p target not-target)
173 (define-vop (check-signed-byte-32 check-type)
175 (let ((loose (generate-error-code vop object-not-signed-byte-32-error
177 (signed-byte-32-test value temp t loose okay))
179 (move result value)))
181 ;;; An (UNSIGNED-BYTE 32) can be represented with either a positive
182 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
183 ;;; exactly two digits and the second digit all zeros.
184 (defun unsigned-byte-32-test (value temp not-p target not-target)
185 (multiple-value-bind (yep nope)
187 (values not-target target)
188 (values target not-target))
191 (inst and temp value fixnum-tag-mask)
192 (inst beq temp fixnum)
195 ;; If not, is it an other pointer?
196 (inst and temp value lowtag-mask)
197 (inst xor temp other-pointer-lowtag)
201 (loadw temp value 0 other-pointer-lowtag)
203 (inst xor temp (+ (ash 1 n-widetag-bits) bignum-widetag))
204 (inst beq temp single-word)
205 ;; If it's other than two, we can't be an (unsigned-byte 32)
206 (inst xor temp (logxor (+ (ash 1 n-widetag-bits) bignum-widetag)
207 (+ (ash 2 n-widetag-bits) bignum-widetag)))
209 ;; Get the second digit.
210 (loadw temp value (1+ bignum-digits-offset) other-pointer-lowtag)
211 ;; All zeros, its an (unsigned-byte 32).
217 ;; Get the single digit.
218 (loadw temp value bignum-digits-offset other-pointer-lowtag)
220 ;; positive implies (unsigned-byte 32).
223 (inst bltz temp target)
224 (inst bgez temp target))
228 (define-vop (unsigned-byte-32-p type-predicate)
229 (:translate unsigned-byte-32-p)
231 (unsigned-byte-32-test value temp not-p target not-target)
234 (define-vop (check-unsigned-byte-32 check-type)
236 (let ((loose (generate-error-code vop object-not-unsigned-byte-32-error
238 (unsigned-byte-32-test value temp t loose okay))
240 (move result value)))
242 ;;; Because of our LOWTAG representation, SYMBOLP and CONSP are
243 ;;; slightly more complex:
245 ;;; * SYMBOLP is true if the object has SYMBOL-HEADER-WIDETAG or is EQ
248 ;;; * CONSP is true if the object has LIST-POINTER-LOWTAG and is not
251 ;;; [ FIXME: This comment should not really be here, in the bowels of
252 ;;; the MIPS type-vops, but where should it be?]
253 (define-vop (symbolp type-predicate)
256 (inst beq value null-tn (if not-p drop-thru target))
257 (test-type value target not-p (symbol-header-widetag) :temp temp)
260 (define-vop (check-symbol check-type)
262 (inst beq value null-tn drop-thru)
263 (let ((error (generate-error-code vop object-not-symbol-error value)))
264 (test-type value error t (symbol-header-widetag) :temp temp))
266 (move result value)))
268 (define-vop (consp type-predicate)
271 (inst beq value null-tn (if not-p target drop-thru))
272 (test-type value target not-p (list-pointer-lowtag) :temp temp)
275 (define-vop (check-cons check-type)
277 (let ((error (generate-error-code vop object-not-cons-error value)))
278 (inst beq value null-tn error)
279 (test-type value error t (list-pointer-lowtag) :temp temp))
280 (move result value)))