0.9.2.48:
[sbcl/lichteblau.git] / src / compiler / x86-64 / type-vops.lisp
blob09efe21fef44e9586ef0bab4b03e42baecd88f9b
1 ;;;; type testing and checking VOPs for the x86 VM
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
12 (in-package "SB!VM")
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
52 headers)
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
57 drop-through)))
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))
65 (move rax-tn value)
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))
76 (move rax-tn value)
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)
82 (move rax-tn value)
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.
94 (if not-p
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)))
100 ((null remaining))
101 (let ((header (car remaining))
102 (last (null (cdr remaining))))
103 (cond
104 ((atom header)
105 (inst cmp al-tn header)
106 (if last
107 (inst jmp equal target)
108 (inst jmp :e when-true)))
110 (let ((start (car header))
111 (end (cdr header)))
112 (unless (= start bignum-widetag)
113 (inst cmp al-tn start)
114 (inst jmp :b when-false)) ; was :l
115 (inst cmp al-tn end)
116 (if last
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)
128 (:ignore eax)
129 (:vop-var vop)
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)
135 (:ignore eax)
136 (:conditional)
137 (:info target not-p)
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)))))
146 (:vop-var vop)
147 (:save-p :compute-only))
149 (define-vop (simple-type-predicate)
150 (:args (value :scs (any-reg descriptor-reg control-stack)))
151 (:conditional)
152 (:info target not-p)
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
160 (&rest type-codes)
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
163 ;; expansion?
164 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
165 (prefix (if variant-p
166 (concatenate 'string (string variant) "-")
167 "")))
168 `(progn
169 ,@(when pred-name
170 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
171 (:translate ,pred-name)
172 (:generator ,cost
173 (test-type value target not-p (,@type-codes))))))
174 ,@(when check-name
175 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
176 (:generator ,cost
177 (let ((err-lab
178 (generate-error-code vop ,error-code value)))
179 (test-type value err-lab t (,@type-codes))
180 (move result value))))))
181 ,@(when ptype
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)
189 (:translate fixnump)
190 (:temporary (:sc unsigned-reg) tmp)
191 (:generator 5
192 (inst mov tmp value)
193 (inst shr tmp 61)
194 (inst jmp (if not-p :nz :z) target)))
196 (define-vop (signed-byte-32-p type-predicate)
197 (:translate signed-byte-32-p)
198 (:generator 7
199 ;; (and (fixnum) (or (no bits set >31) (all bits set >31))
200 (move rax-tn value)
201 (inst test rax-tn 7)
202 (inst jmp :ne (if not-p target NOT-TARGET))
203 (inst sar rax-tn (+ 32 3 -1))
204 (if not-p
205 (progn
206 (inst jmp :nz MAYBE)
207 (inst jmp NOT-TARGET))
208 (inst jmp :z target))
209 MAYBE
210 (inst cmp rax-tn -1)
211 (inst jmp (if not-p :ne :eq) target)
212 NOT-TARGET))
214 (define-vop (check-signed-byte-32 check-type)
215 (:generator 8
216 (let ((nope (generate-error-code vop
217 object-not-signed-byte-32-error
218 value))
219 (ok (gen-label)))
220 (move rax-tn value)
221 (inst test rax-tn 7)
222 (inst jmp :ne nope)
223 (inst sar rax-tn (+ 32 3 -1))
224 (inst jmp :z ok)
225 (inst cmp rax-tn -1)
226 (inst jmp :ne nope)
227 (emit-label ok)
228 (move result value))))
231 (define-vop (unsigned-byte-32-p type-predicate)
232 (:translate unsigned-byte-32-p)
233 (:generator 7
234 ;; (and (fixnum) (no bits set >31))
235 (move rax-tn value)
236 (inst test rax-tn 7)
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)
240 NOT-TARGET))
242 (define-vop (check-unsigned-byte-32 check-type)
243 (:generator 8
244 (let ((nope
245 (generate-error-code vop object-not-unsigned-byte-32-error value)))
246 (move rax-tn value)
247 (inst test rax-tn 7)
248 (inst jmp :ne nope)
249 (inst shr rax-tn (+ 32 sb!vm::n-fixnum-tag-bits))
250 (inst jmp :nz nope)
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)
258 (:generator 45
259 (let ((not-target (gen-label))
260 (single-word (gen-label))
261 (fixnum (gen-label)))
262 (multiple-value-bind (yep nope)
263 (if not-p
264 (values not-target target)
265 (values target not-target))
266 ;; Is it a fixnum?
267 (generate-fixnum-test value)
268 (move rax-tn value)
269 (inst jmp :e fixnum)
271 ;; If not, is it an other pointer?
272 (inst and rax-tn lowtag-mask)
273 (inst cmp rax-tn other-pointer-lowtag)
274 (inst jmp :ne nope)
275 ;; Get the header.
276 (loadw rax-tn value 0 other-pointer-lowtag)
277 ;; Is it one?
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))
282 (inst jmp :ne nope)
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)
287 (inst jmp :z yep)
288 (inst jmp nope)
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).
295 (emit-label fixnum)
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)
302 (:generator 45
303 (let ((nope
304 (generate-error-code vop object-not-unsigned-byte-64-error value))
305 (yep (gen-label))
306 (fixnum (gen-label))
307 (single-word (gen-label)))
309 ;; Is it a fixnum?
310 (generate-fixnum-test value)
311 (move rax-tn value)
312 (inst jmp :e fixnum)
314 ;; If not, is it an other pointer?
315 (inst and rax-tn lowtag-mask)
316 (inst cmp rax-tn other-pointer-lowtag)
317 (inst jmp :ne nope)
318 ;; Get the header.
319 (loadw rax-tn value 0 other-pointer-lowtag)
320 ;; Is it one?
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))
325 (inst jmp :ne nope)
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)
330 (inst jmp :z yep)
331 (inst jmp nope)
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).
338 (emit-label fixnum)
339 (inst or rax-tn rax-tn)
340 (inst jmp :s nope)
342 (emit-label yep)
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)
351 (:translate symbolp)
352 (:generator 12
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)))
357 DROP-THRU))
359 (define-vop (check-symbol check-type)
360 (:generator 12
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)))
365 DROP-THRU
366 (move result value)))
368 (define-vop (consp type-predicate)
369 (:translate consp)
370 (:generator 8
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)))
375 DROP-THRU))
377 (define-vop (check-cons check-type)
378 (:generator 8
379 (let ((error (generate-error-code vop object-not-cons-error value)))
380 (inst cmp value nil-value)
381 (inst jmp :e error)
382 (test-type value error t (list-pointer-lowtag))
383 (move result value))))