1.0.34.4: micro-optimize widetag checking on x86oids
[sbcl.git] / src / compiler / x86-64 / type-vops.lisp
1 ;;;; type testing and checking VOPs for the x86-64 VM
2
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.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; test generation utilities
15
16 (defun generate-fixnum-test (value)
17   "zero flag set if VALUE is fixnum"
18   (inst test
19         (cond ((sc-is value any-reg descriptor-reg)
20                (make-byte-tn value))
21               ((sc-is value control-stack)
22                (make-ea :byte :base rbp-tn
23                         :disp (frame-byte-offset (tn-offset value))))
24               (t
25                value))
26         sb!vm::fixnum-tag-mask))
27
28 (defun %test-fixnum (value target not-p)
29   (generate-fixnum-test value)
30   (inst jmp (if not-p :nz :z) target))
31
32 (defun %test-fixnum-and-headers (value target not-p headers)
33   (let ((drop-through (gen-label)))
34     (generate-fixnum-test value)
35     (inst jmp :z (if not-p drop-through target))
36     (%test-headers value target not-p nil headers drop-through)))
37
38 (defun %test-fixnum-and-immediate (value target not-p immediate)
39   (let ((drop-through (gen-label)))
40     (generate-fixnum-test value)
41     (inst jmp :z (if not-p drop-through target))
42     (%test-immediate value target not-p immediate drop-through)))
43
44 (defun %test-fixnum-immediate-and-headers (value target not-p immediate
45                                            headers)
46   (let ((drop-through (gen-label)))
47     (generate-fixnum-test value)
48     (inst jmp :z (if not-p drop-through target))
49     (%test-immediate-and-headers value target not-p immediate headers
50                                  drop-through)))
51
52 (defun %test-immediate (value target not-p immediate
53                         &optional (drop-through (gen-label)))
54   ;; Code a single instruction byte test if possible.
55   (cond ((sc-is value any-reg descriptor-reg)
56          (inst cmp (make-byte-tn value) immediate))
57         (t
58          (move rax-tn value)
59          (inst cmp al-tn immediate)))
60   (inst jmp (if not-p :ne :e) target)
61   (emit-label drop-through))
62
63 (defun %test-immediate-and-headers (value target not-p immediate headers
64                                     &optional (drop-through (gen-label)))
65   ;; Code a single instruction byte test if possible.
66   (cond ((sc-is value any-reg descriptor-reg)
67          (inst cmp (make-byte-tn value) immediate))
68         (t
69          (move rax-tn value)
70          (inst cmp al-tn immediate)))
71   (inst jmp :e (if not-p drop-through target))
72   (%test-headers value target not-p nil headers drop-through))
73
74 (defun %test-lowtag (value target not-p lowtag)
75   (if (and (sc-is value any-reg descriptor-reg)
76            (< (tn-offset value) r8-offset))
77       (move eax-tn (make-dword-tn value)) ; shorter encoding (no REX prefix)
78       (move rax-tn value))
79   (inst and al-tn lowtag-mask)
80   (inst cmp al-tn lowtag)
81   (inst jmp (if not-p :ne :e) target))
82
83 (defun %test-headers (value target not-p function-p headers
84                             &optional (drop-through (gen-label)))
85   (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
86     (multiple-value-bind (equal less-or-equal greater-or-equal when-true when-false)
87         ;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
88         ;; for branching to TARGET.  WHEN-TRUE and WHEN-FALSE are the
89         ;; labels to branch to when we know it's true and when we know
90         ;; it's false respectively.
91         (if not-p
92             (values :ne :a :b drop-through target)
93             (values :e :na :nb target drop-through))
94       (%test-lowtag value when-false t lowtag)
95       (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
96       (do ((remaining headers (cdr remaining)))
97           ((null remaining))
98         (let ((header (car remaining))
99               (last (null (cdr remaining))))
100           (cond
101            ((atom header)
102             (inst cmp al-tn header)
103             (if last
104                 (inst jmp equal target)
105                 (inst jmp :e when-true)))
106            (t
107              (let ((start (car header))
108                    (end (cdr header)))
109                (cond
110                  ((= start bignum-widetag)
111                   (inst cmp al-tn end)
112                   (if last
113                       (inst jmp less-or-equal target)
114                       (inst jmp :be when-true)))
115                  ((= end complex-array-widetag)
116                   (inst cmp al-tn start)
117                   (if last
118                       (inst jmp greater-or-equal target)
119                       (inst jmp :b when-false)))
120                  ((not last)
121                   (inst cmp al-tn start)
122                   (inst jmp :b when-false)
123                   (inst cmp al-tn end)
124                   (if last
125                       (inst jmp less-or-equal target)
126                       (inst jmp :be when-true)))
127                  (t
128                   (inst sub al-tn start)
129                   (inst cmp al-tn (- end start))
130                   (inst jmp less-or-equal target))))))))
131       (emit-label drop-through))))
132
133 \f
134 ;;;; type checking and testing
135
136 (define-vop (check-type)
137   (:args (value :target result :scs (any-reg descriptor-reg)))
138   (:results (result :scs (any-reg descriptor-reg)))
139   (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
140   (:ignore eax)
141   (:vop-var vop)
142   (:save-p :compute-only))
143
144 (define-vop (type-predicate)
145   (:args (value :scs (any-reg descriptor-reg)))
146   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
147   (:ignore eax)
148   (:conditional)
149   (:info target not-p)
150   (:policy :fast-safe))
151
152 ;;; simpler VOP that don't need a temporary register
153 (define-vop (simple-check-type)
154   (:args (value :target result :scs (any-reg descriptor-reg)))
155   (:results (result :scs (any-reg descriptor-reg)
156                     :load-if (not (and (sc-is value any-reg descriptor-reg)
157                                        (sc-is result control-stack)))))
158   (:vop-var vop)
159   (:save-p :compute-only))
160
161 (define-vop (simple-type-predicate)
162   (:args (value :scs (any-reg descriptor-reg control-stack)))
163   (:conditional)
164   (:info target not-p)
165   (:policy :fast-safe))
166
167 (defun cost-to-test-types (type-codes)
168   (+ (* 2 (length type-codes))
169      (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
170
171 (defmacro !define-type-vops (pred-name check-name ptype error-code
172                              (&rest type-codes)
173                              &key (variant nil variant-p) &allow-other-keys)
174   ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
175   ;; expansion?
176   (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
177          (prefix (if variant-p
178                      (concatenate 'string (string variant) "-")
179                      "")))
180     `(progn
181        ,@(when pred-name
182            `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
183                (:translate ,pred-name)
184                (:generator ,cost
185                  (test-type value target not-p (,@type-codes))))))
186        ,@(when check-name
187            `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
188                (:generator ,cost
189                  (let ((err-lab
190                         (generate-error-code vop ',error-code value)))
191                    (test-type value err-lab t (,@type-codes))
192                    (move result value))))))
193        ,@(when ptype
194            `((primitive-type-vop ,check-name (:check) ,ptype))))))
195 \f
196 ;;;; other integer ranges
197
198 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
199   (:args (value :scs (unsigned-reg)))
200   (:arg-types unsigned-num)
201   (:translate fixnump)
202   (:temporary (:sc unsigned-reg) tmp)
203   (:info)
204   (:conditional :z)
205   (:generator 5
206     (inst mov tmp value)
207     (inst shr tmp n-positive-fixnum-bits)))
208
209 (define-vop (fixnump/signed-byte-64 type-predicate)
210   (:args (value :scs (signed-reg)))
211   (:info)
212   (:conditional :z)
213   (:arg-types signed-num)
214   (:translate fixnump)
215   (:generator 5
216     ;; Hackers Delight, p. 53: signed
217     ;;    a <= x <= a + 2^n - 1
218     ;; is equivalent to unsigned
219     ;;    ((x-a) >> n) = 0
220     (inst mov rax-tn #.(- sb!xc:most-negative-fixnum))
221     (inst add rax-tn value)
222     (inst shr rax-tn #.(integer-length (- sb!xc:most-positive-fixnum
223                                           sb!xc:most-negative-fixnum)))))
224
225 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
226 ;;; exactly one digit.
227
228 (define-vop (signed-byte-64-p type-predicate)
229   (:translate signed-byte-64-p)
230   (:generator 45
231     (multiple-value-bind (yep nope)
232         (if not-p
233             (values not-target target)
234             (values target not-target))
235       (generate-fixnum-test value)
236       (inst jmp :e yep)
237       (move rax-tn value)
238       (inst and al-tn lowtag-mask)
239       (inst cmp al-tn other-pointer-lowtag)
240       (inst jmp :ne nope)
241       (loadw rax-tn value 0 other-pointer-lowtag)
242       (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
243       (inst jmp (if not-p :ne :e) target))
244     NOT-TARGET))
245
246 (define-vop (check-signed-byte-64 check-type)
247   (:generator 45
248     (let ((nope (generate-error-code vop
249                                      'object-not-signed-byte-64-error
250                                      value)))
251       (generate-fixnum-test value)
252       (inst jmp :e yep)
253       (move rax-tn value)
254       (inst and al-tn lowtag-mask)
255       (inst cmp al-tn other-pointer-lowtag)
256       (inst jmp :ne nope)
257       (loadw rax-tn value 0 other-pointer-lowtag)
258       (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
259       (inst jmp :ne nope))
260     YEP
261     (move result value)))
262
263 ;;; An (unsigned-byte 64) can be represented with either a positive
264 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
265 ;;; exactly two digits and the second digit all zeros.
266 (define-vop (unsigned-byte-64-p type-predicate)
267   (:translate unsigned-byte-64-p)
268   (:generator 45
269     (let ((not-target (gen-label))
270           (single-word (gen-label))
271           (fixnum (gen-label)))
272       (multiple-value-bind (yep nope)
273           (if not-p
274               (values not-target target)
275               (values target not-target))
276         ;; Is it a fixnum?
277         (generate-fixnum-test value)
278         (move rax-tn value)
279         (inst jmp :e fixnum)
280
281         ;; If not, is it an other pointer?
282         (inst and rax-tn lowtag-mask)
283         (inst cmp rax-tn other-pointer-lowtag)
284         (inst jmp :ne nope)
285         ;; Get the header.
286         (loadw rax-tn value 0 other-pointer-lowtag)
287         ;; Is it one?
288         (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
289         (inst jmp :e single-word)
290         ;; If it's other than two, we can't be an (unsigned-byte 64)
291         (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
292         (inst jmp :ne nope)
293         ;; Get the second digit.
294         (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
295         ;; All zeros, its an (unsigned-byte 64).
296         (inst or rax-tn rax-tn)
297         (inst jmp :z yep)
298         (inst jmp nope)
299
300         (emit-label single-word)
301         ;; Get the single digit.
302         (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
303
304         ;; positive implies (unsigned-byte 64).
305         (emit-label fixnum)
306         (inst or rax-tn rax-tn)
307         (inst jmp (if not-p :s :ns) target)
308
309         (emit-label not-target)))))
310
311 (define-vop (check-unsigned-byte-64 check-type)
312   (:generator 45
313     (let ((nope
314            (generate-error-code vop 'object-not-unsigned-byte-64-error value))
315           (yep (gen-label))
316           (fixnum (gen-label))
317           (single-word (gen-label)))
318
319       ;; Is it a fixnum?
320       (generate-fixnum-test value)
321       (move rax-tn value)
322       (inst jmp :e fixnum)
323
324       ;; If not, is it an other pointer?
325       (inst and rax-tn lowtag-mask)
326       (inst cmp rax-tn other-pointer-lowtag)
327       (inst jmp :ne nope)
328       ;; Get the header.
329       (loadw rax-tn value 0 other-pointer-lowtag)
330       ;; Is it one?
331       (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
332       (inst jmp :e single-word)
333       ;; If it's other than two, we can't be an (unsigned-byte 64)
334       (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
335       (inst jmp :ne nope)
336       ;; Get the second digit.
337       (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
338       ;; All zeros, its an (unsigned-byte 64).
339       (inst or rax-tn rax-tn)
340       (inst jmp :z yep)
341       (inst jmp nope)
342
343       (emit-label single-word)
344       ;; Get the single digit.
345       (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
346
347       ;; positive implies (unsigned-byte 64).
348       (emit-label fixnum)
349       (inst or rax-tn rax-tn)
350       (inst jmp :s nope)
351
352       (emit-label yep)
353       (move result value))))
354 \f
355 ;;;; list/symbol types
356 ;;;
357 ;;; symbolp (or symbol (eq nil))
358 ;;; consp (and list (not (eq nil)))
359
360 (define-vop (symbolp type-predicate)
361   (:translate symbolp)
362   (:generator 12
363     (let ((is-symbol-label (if not-p DROP-THRU target)))
364       (inst cmp value nil-value)
365       (inst jmp :e is-symbol-label)
366       (test-type value target not-p (symbol-header-widetag)))
367     DROP-THRU))
368
369 (define-vop (check-symbol check-type)
370   (:generator 12
371     (let ((error (generate-error-code vop 'object-not-symbol-error value)))
372       (inst cmp value nil-value)
373       (inst jmp :e DROP-THRU)
374       (test-type value error t (symbol-header-widetag)))
375     DROP-THRU
376     (move result value)))
377
378 (define-vop (consp type-predicate)
379   (:translate consp)
380   (:generator 8
381     (let ((is-not-cons-label (if not-p target DROP-THRU)))
382       (inst cmp value nil-value)
383       (inst jmp :e is-not-cons-label)
384       (test-type value target not-p (list-pointer-lowtag)))
385     DROP-THRU))
386
387 (define-vop (check-cons check-type)
388   (:generator 8
389     (let ((error (generate-error-code vop 'object-not-cons-error value)))
390       (inst cmp value nil-value)
391       (inst jmp :e error)
392       (test-type value error t (list-pointer-lowtag))
393       (move result value))))