0.8.18.36:
[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 maybe)
180           (inst jmp not-target))
181         (inst jmp :z target))
182     MAYBE
183     (inst cmp rax-tn -1)
184     (inst jmp (if not-p :ne :eq) target)
185     NOT-TARGET))
186
187 (define-vop (check-signed-byte-32 check-type)
188   (:generator 8
189     (let ((nope (generate-error-code vop
190                                      object-not-signed-byte-32-error
191                                      value))
192           (ok (gen-label)))
193       (move rax-tn value)
194       (inst test rax-tn 7)
195       (inst jmp :ne nope)
196       (inst sar rax-tn (+ 32 3 -1))      
197       (inst jmp :z ok)
198       (inst cmp rax-tn -1)
199       (inst jmp :ne nope)
200       (emit-label OK)
201       (move result value))))
202
203
204 (define-vop (unsigned-byte-32-p type-predicate)
205   (:translate unsigned-byte-32-p)
206   (:generator 7
207     ;; (and (fixnum) (no bits set >31))
208     (move rax-tn value)
209     (inst test rax-tn 7)
210     (inst jmp :ne (if not-p target not-target))
211     (inst shr rax-tn (+ 32 sb!vm::n-fixnum-tag-bits))
212     (inst jmp (if not-p :nz :z) target)
213     NOT-TARGET))
214
215 (define-vop (check-unsigned-byte-32 check-type)
216   (:generator 8
217     (let ((nope
218            (generate-error-code vop object-not-unsigned-byte-32-error value)))
219       (move rax-tn value)
220       (inst test rax-tn 7)
221       (inst jmp :ne nope)
222       (inst shr rax-tn (+ 32 sb!vm::n-fixnum-tag-bits))
223       (inst jmp :nz nope)
224       (move result value))))
225
226 ;;; An (unsigned-byte 64) can be represented with either a positive
227 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
228 ;;; exactly two digits and the second digit all zeros.
229 (define-vop (unsigned-byte-64-p type-predicate)
230   (:translate unsigned-byte-64-p)
231   (:generator 45
232     (let ((not-target (gen-label))
233           (single-word (gen-label))
234           (fixnum (gen-label)))
235       (multiple-value-bind (yep nope)
236           (if not-p
237               (values not-target target)
238               (values target not-target))
239         ;; Is it a fixnum?
240         (generate-fixnum-test value)
241         (move eax-tn value)
242         (inst jmp :e fixnum)
243
244         ;; If not, is it an other pointer?
245         (inst and eax-tn lowtag-mask)
246         (inst cmp eax-tn other-pointer-lowtag)
247         (inst jmp :ne nope)
248         ;; Get the header.
249         (loadw eax-tn value 0 other-pointer-lowtag)
250         ;; Is it one?
251         (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
252         (inst jmp :e single-word)
253         ;; If it's other than two, we can't be an (unsigned-byte 64)
254         (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
255         (inst jmp :ne nope)
256         ;; Get the second digit.
257         (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
258         ;; All zeros, its an (unsigned-byte 64).
259         (inst or eax-tn eax-tn)
260         (inst jmp :z yep)
261         (inst jmp nope)
262         
263         (emit-label single-word)
264         ;; Get the single digit.
265         (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
266
267         ;; positive implies (unsigned-byte 64).
268         (emit-label fixnum)
269         (inst or eax-tn eax-tn)
270         (inst jmp (if not-p :s :ns) target)
271
272         (emit-label not-target)))))
273
274 (define-vop (check-unsigned-byte-64 check-type)
275   (:generator 45
276     (let ((nope
277            (generate-error-code vop object-not-unsigned-byte-64-error value))
278           (yep (gen-label))
279           (fixnum (gen-label))
280           (single-word (gen-label)))
281
282       ;; Is it a fixnum?
283       (generate-fixnum-test value)
284       (move eax-tn value)
285       (inst jmp :e fixnum)
286
287       ;; If not, is it an other pointer?
288       (inst and eax-tn lowtag-mask)
289       (inst cmp eax-tn other-pointer-lowtag)
290       (inst jmp :ne nope)
291       ;; Get the header.
292       (loadw eax-tn value 0 other-pointer-lowtag)
293       ;; Is it one?
294       (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
295       (inst jmp :e single-word)
296       ;; If it's other than two, we can't be an (unsigned-byte 64)
297       (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
298       (inst jmp :ne nope)
299       ;; Get the second digit.
300       (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
301       ;; All zeros, its an (unsigned-byte 64).
302       (inst or eax-tn eax-tn)
303       (inst jmp :z yep)
304       (inst jmp nope)
305         
306       (emit-label single-word)
307       ;; Get the single digit.
308       (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
309
310       ;; positive implies (unsigned-byte 64).
311       (emit-label fixnum)
312       (inst or eax-tn eax-tn)
313       (inst jmp :s nope)
314
315       (emit-label yep)
316       (move result value))))
317 \f
318 ;;;; list/symbol types
319 ;;;
320 ;;; symbolp (or symbol (eq nil))
321 ;;; consp (and list (not (eq nil)))
322
323 (define-vop (symbolp type-predicate)
324   (:translate symbolp)
325   (:generator 12
326     (let ((is-symbol-label (if not-p drop-thru target)))
327       (inst cmp value nil-value)
328       (inst jmp :e is-symbol-label)
329       (test-type value target not-p (symbol-header-widetag)))
330     DROP-THRU))
331
332 (define-vop (check-symbol check-type)
333   (:generator 12
334     (let ((error (generate-error-code vop object-not-symbol-error value)))
335       (inst cmp value nil-value)
336       (inst jmp :e drop-thru)
337       (test-type value error t (symbol-header-widetag)))
338     DROP-THRU
339     (move result value)))
340
341 (define-vop (consp type-predicate)
342   (:translate consp)
343   (:generator 8
344     (let ((is-not-cons-label (if not-p target drop-thru)))
345       (inst cmp value nil-value)
346       (inst jmp :e is-not-cons-label)
347       (test-type value target not-p (list-pointer-lowtag)))
348     DROP-THRU))
349
350 (define-vop (check-cons check-type)
351   (:generator 8
352     (let ((error (generate-error-code vop object-not-cons-error value)))
353       (inst cmp value nil-value)
354       (inst jmp :e error)
355       (test-type value error t (list-pointer-lowtag))
356       (move result value))))