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