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 3)
19 (inst bne temp zero-tn target)
20 (inst beq temp zero-tn 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 3)
27 (inst beq temp zero-tn (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 255)
34 (inst xor temp immediate)
36 (inst bne temp zero-tn target)
37 (inst beq temp zero-tn target))
40 (defun %test-lowtag (value target not-p lowtag &key skip-nop temp)
42 (inst and temp value lowtag-mask)
43 (inst xor temp lowtag)
45 (inst bne temp zero-tn target)
46 (inst beq temp zero-tn target))
50 (defun %test-lowtag-and-headers (value target not-p lowtag
51 function-p headers &key temp)
52 (let ((drop-through (gen-label)))
53 (%test-lowtag value (if not-p drop-through target) nil lowtag
54 :skip-nop t :temp temp)
55 (%test-headers value target not-p function-p headers
56 :drop-through drop-through :temp temp)))
58 (defun %test-headers (value target not-p function-p headers
59 &key (drop-through (gen-label)) temp)
60 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
62 (when-true when-false)
63 ;; WHEN-TRUE and WHEN-FALSE are the labels to branch to when
64 ;; we know it's true and when we know it's false respectively.
66 (values drop-through target)
67 (values target drop-through))
69 (%test-lowtag value when-false t lowtag :temp temp)
70 (load-type temp value (- lowtag))
73 (do ((remaining headers (cdr remaining)))
75 (let ((header (car remaining))
76 (last (null (cdr remaining))))
79 (inst subu temp (- header delta))
83 (inst bne temp zero-tn target)
84 (inst beq temp zero-tn target))
85 (inst beq temp zero-tn when-true)))
87 (let ((start (car header))
89 (unless (= start bignum-widetag)
90 (inst subu temp (- start delta))
92 (inst bltz temp when-false))
93 (inst subu temp (- end delta))
97 (inst bgtz temp target)
98 (inst blez temp target))
99 (inst blez temp when-true))))))))
101 (emit-label drop-through)))))
105 ;;; Type checking and testing (see also the use of !DEFINE-TYPE-VOPS
106 ;;; in src/compiler/generic/late-type-vops.lisp):
108 ;;; [FIXME: Like some of the other comments in this file, this one
109 ;;; really belongs somewhere else]
110 (define-vop (check-type)
111 (:args (value :target result :scs (any-reg descriptor-reg)))
112 (:results (result :scs (any-reg descriptor-reg)))
113 (:temporary (:scs (non-descriptor-reg) :to (:result 0)) temp)
115 (:save-p :compute-only))
117 (define-vop (type-predicate)
118 (:args (value :scs (any-reg descriptor-reg)))
119 (:temporary (:scs (non-descriptor-reg)) temp)
122 (:policy :fast-safe))
124 (defun cost-to-test-types (type-codes)
125 (+ (* 2 (length type-codes))
126 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
128 (defmacro !define-type-vops (pred-name check-name ptype error-code
130 &key &allow-other-keys)
131 (let ((cost (cost-to-test-types (mapcar #'eval type-codes))))
134 `((define-vop (,pred-name type-predicate)
135 (:translate ,pred-name)
137 (test-type value target not-p (,@type-codes)
140 `((define-vop (,check-name check-type)
143 (generate-error-code vop ,error-code value)))
144 (test-type value err-lab t (,@type-codes)
146 (move result value))))))
148 `((primitive-type-vop ,check-name (:check) ,ptype))))))
150 ;;;; TYPE-VOPs for types that are more complex to test for than simple
151 ;;;; LOWTAG and WIDETAG tests, but that are nevertheless important:
153 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a
154 ;;; bignum with exactly one digit.
155 (defun signed-byte-32-test (value temp not-p target not-target)
159 (values not-target target)
160 (values target not-target))
162 (inst and temp value 3)
163 (inst beq temp zero-tn yep)
164 (inst and temp value lowtag-mask)
165 (inst xor temp other-pointer-lowtag)
166 (inst bne temp zero-tn nope)
168 (loadw temp value 0 other-pointer-lowtag)
169 (inst xor temp (+ (ash 1 n-widetag-bits) bignum-widetag))
171 (inst bne temp zero-tn target)
172 (inst beq temp zero-tn target))
176 (define-vop (signed-byte-32-p type-predicate)
177 (:translate signed-byte-32-p)
179 (signed-byte-32-test value temp not-p target not-target)
182 (define-vop (check-signed-byte-32 check-type)
184 (let ((loose (generate-error-code vop object-not-signed-byte-32-error
186 (signed-byte-32-test value temp t loose okay))
188 (move result value)))
190 ;;; An (UNSIGNED-BYTE 32) can be represented with either a positive
191 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
192 ;;; exactly two digits and the second digit all zeros.
193 (defun unsigned-byte-32-test (value temp not-p target not-target)
194 (multiple-value-bind (yep nope)
196 (values not-target target)
197 (values target not-target))
200 (inst and temp value 3)
201 (inst beq temp zero-tn fixnum)
202 (inst move temp value)
204 ;; If not, is it an other pointer?
205 (inst and temp value lowtag-mask)
206 (inst xor temp other-pointer-lowtag)
207 (inst bne temp zero-tn nope)
210 (loadw temp value 0 other-pointer-lowtag)
212 (inst xor temp (+ (ash 1 n-widetag-bits) bignum-widetag))
213 (inst beq temp zero-tn single-word)
214 ;; If it's other than two, we can't be an (unsigned-byte 32)
215 (inst xor temp (logxor (+ (ash 1 n-widetag-bits) bignum-widetag)
216 (+ (ash 2 n-widetag-bits) bignum-widetag)))
217 (inst bne temp zero-tn nope)
218 ;; Get the second digit.
219 (loadw temp value (1+ bignum-digits-offset) other-pointer-lowtag)
220 ;; All zeros, its an (unsigned-byte 32).
221 (inst beq temp zero-tn yep)
226 ;; Get the single digit.
227 (loadw temp value bignum-digits-offset other-pointer-lowtag)
229 ;; positive implies (unsigned-byte 32).
232 (inst bltz temp target)
233 (inst bgez temp target))
237 (define-vop (unsigned-byte-32-p type-predicate)
238 (:translate unsigned-byte-32-p)
240 (unsigned-byte-32-test value temp not-p target not-target)
243 (define-vop (check-unsigned-byte-32 check-type)
245 (let ((loose (generate-error-code vop object-not-unsigned-byte-32-error
247 (unsigned-byte-32-test value temp t loose okay))
249 (move result value)))
251 ;;; Because of our LOWTAG representation, SYMBOLP and CONSP are
252 ;;; slightly more complex:
254 ;;; * SYMBOLP is true if the object has SYMBOL-HEADER-WIDETAG or is EQ
257 ;;; * CONSP is true if the object has LIST-POINTER-LOWTAG and is not
260 ;;; [ FIXME: This comment should not really be here, in the bowels of
261 ;;; the MIPS type-vops, but where should it be?]
262 (define-vop (symbolp type-predicate)
265 (inst beq value null-tn (if not-p drop-thru target))
266 (test-type value target not-p (symbol-header-widetag) :temp temp)
269 (define-vop (check-symbol check-type)
271 (inst beq value null-tn drop-thru)
272 (let ((error (generate-error-code vop object-not-symbol-error value)))
273 (test-type value error t (symbol-header-widetag) :temp temp))
275 (move result value)))
277 (define-vop (consp type-predicate)
280 (inst beq value null-tn (if not-p target drop-thru))
281 (test-type value target not-p (list-pointer-lowtag) :temp temp)
284 (define-vop (check-cons check-type)
286 (let ((error (generate-error-code vop object-not-cons-error value)))
287 (inst beq value null-tn error)
288 (test-type value error t (list-pointer-lowtag) :temp temp))
289 (move result value)))