7c12dc37ed653cf4f689fe7b266d626750cfcb05
[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 ;;; Optimize the case of moving a 64-bit value into RAX when not caring
17 ;;; about the upper 32 bits: often the REX prefix can be spared.
18 (defun move-qword-to-eax (value)
19   (if (and (sc-is value any-reg descriptor-reg)
20            (< (tn-offset value) r8-offset))
21       (move eax-tn (make-dword-tn value))
22       (move rax-tn value)))
23
24 (defun generate-fixnum-test (value)
25   "zero flag set if VALUE is fixnum"
26   (inst test
27         (cond ((sc-is value any-reg descriptor-reg)
28                (make-byte-tn value))
29               ((sc-is value control-stack)
30                (make-ea :byte :base rbp-tn
31                         :disp (frame-byte-offset (tn-offset value))))
32               (t
33                value))
34         sb!vm::fixnum-tag-mask))
35
36 (defun %test-fixnum (value target not-p)
37   (generate-fixnum-test value)
38   (inst jmp (if not-p :nz :z) target))
39
40 (defun %test-fixnum-and-headers (value target not-p headers)
41   (let ((drop-through (gen-label)))
42     (generate-fixnum-test value)
43     (inst jmp :z (if not-p drop-through target))
44     (%test-headers value target not-p nil headers drop-through)))
45
46 (defun %test-fixnum-and-immediate (value target not-p immediate)
47   (let ((drop-through (gen-label)))
48     (generate-fixnum-test value)
49     (inst jmp :z (if not-p drop-through target))
50     (%test-immediate value target not-p immediate drop-through)))
51
52 (defun %test-fixnum-immediate-and-headers (value target not-p immediate
53                                            headers)
54   (let ((drop-through (gen-label)))
55     (generate-fixnum-test value)
56     (inst jmp :z (if not-p drop-through target))
57     (%test-immediate-and-headers value target not-p immediate headers
58                                  drop-through)))
59
60 (defun %test-immediate (value target not-p immediate
61                         &optional (drop-through (gen-label)))
62   ;; Code a single instruction byte test if possible.
63   (cond ((sc-is value any-reg descriptor-reg)
64          (inst cmp (make-byte-tn value) immediate))
65         (t
66          (move rax-tn value)
67          (inst cmp al-tn immediate)))
68   (inst jmp (if not-p :ne :e) target)
69   (emit-label drop-through))
70
71 (defun %test-immediate-and-headers (value target not-p immediate headers
72                                     &optional (drop-through (gen-label)))
73   ;; Code a single instruction byte test if possible.
74   (cond ((sc-is value any-reg descriptor-reg)
75          (inst cmp (make-byte-tn value) immediate))
76         (t
77          (move rax-tn value)
78          (inst cmp al-tn immediate)))
79   (inst jmp :e (if not-p drop-through target))
80   (%test-headers value target not-p nil headers drop-through))
81
82 (defun %test-lowtag (value target not-p lowtag)
83   (inst lea eax-tn (make-ea :dword :base value :disp (- lowtag)))
84   (inst test al-tn lowtag-mask)
85   (inst jmp (if not-p :nz :z) target))
86
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 greater-or-equal when-true
91                                 when-false)
92         ;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
93         ;; for branching to TARGET.  WHEN-TRUE and WHEN-FALSE are the
94         ;; labels to branch to when we know it's true and when we know
95         ;; it's false respectively.
96         (if not-p
97             (values :ne :a :b drop-through target)
98             (values :e :na :nb target drop-through))
99       (%test-lowtag value when-false t lowtag)
100       (do ((remaining headers (cdr remaining))
101            ;; It is preferable (smaller and faster code) to directly
102            ;; compare the value in memory instead of loading it into
103            ;; a register first. Find out if this is possible and set
104            ;; WIDETAG-TN accordingly. If impossible, generate the
105            ;; register load.
106            ;; Compared to x86 we additionally optimize the cases of a
107            ;; range starting with BIGNUM-WIDETAG or ending with
108            ;; COMPLEX-ARRAY-WIDETAG.
109            (widetag-tn (if (and (null (cdr headers))
110                                 (or (atom (car headers))
111                                     (= (caar headers) bignum-widetag)
112                                     (= (cdar headers) complex-array-widetag)))
113                            (make-ea :byte :base value :disp (- lowtag))
114                            (progn
115                              (inst mov eax-tn (make-ea :dword :base value
116                                                        :disp (- lowtag)))
117                              al-tn))))
118           ((null remaining))
119         (let ((header (car remaining))
120               (last (null (cdr remaining))))
121           (cond
122            ((atom header)
123             (inst cmp widetag-tn header)
124             (if last
125                 (inst jmp equal target)
126                 (inst jmp :e when-true)))
127            (t
128              (let ((start (car header))
129                    (end (cdr header)))
130                (cond
131                  ((= start bignum-widetag)
132                   (inst cmp widetag-tn end)
133                   (if last
134                       (inst jmp less-or-equal target)
135                       (inst jmp :be when-true)))
136                  ((= end complex-array-widetag)
137                   (inst cmp widetag-tn start)
138                   (if last
139                       (inst jmp greater-or-equal target)
140                       (inst jmp :b when-false)))
141                  ((not last)
142                   (inst cmp al-tn start)
143                   (inst jmp :b when-false)
144                   (inst cmp al-tn end)
145                   (inst jmp :be when-true))
146                  (t
147                   (inst sub al-tn start)
148                   (inst cmp al-tn (- end start))
149                   (inst jmp less-or-equal target))))))))
150       (emit-label drop-through))))
151
152 \f
153 ;;;; type checking and testing
154
155 (define-vop (check-type)
156   (:args (value :target result :scs (any-reg descriptor-reg)))
157   (:results (result :scs (any-reg descriptor-reg)))
158   (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
159   (:ignore eax)
160   (:vop-var vop)
161   (:save-p :compute-only))
162
163 (define-vop (type-predicate)
164   (:args (value :scs (any-reg descriptor-reg)))
165   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
166   (:ignore eax)
167   (:conditional)
168   (:info target not-p)
169   (:policy :fast-safe))
170
171 ;;; simpler VOP that don't need a temporary register
172 (define-vop (simple-check-type)
173   (:args (value :target result :scs (any-reg descriptor-reg)))
174   (:results (result :scs (any-reg descriptor-reg)
175                     :load-if (not (and (sc-is value any-reg descriptor-reg)
176                                        (sc-is result control-stack)))))
177   (:vop-var vop)
178   (:save-p :compute-only))
179
180 (define-vop (simple-type-predicate)
181   (:args (value :scs (any-reg descriptor-reg control-stack)))
182   (:conditional)
183   (:info target not-p)
184   (:policy :fast-safe))
185
186 (defmacro !define-type-vops (pred-name check-name ptype error-code
187                              (&rest type-codes)
188                              &key (variant nil variant-p) &allow-other-keys)
189   ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
190   ;; expansion?
191   (flet ((cost-to-test-types (type-codes)
192            (+ (* 2 (length type-codes))
193               (if (> (apply #'max type-codes) lowtag-limit) 7 2))))
194     (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
195            (prefix (if variant-p
196                        (concatenate 'string (string variant) "-")
197                        "")))
198       `(progn
199          ,@(when pred-name
200              `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
201                  (:translate ,pred-name)
202                  (:generator ,cost
203                    (test-type value target not-p (,@type-codes))))))
204          ,@(when check-name
205              `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
206                  (:generator ,cost
207                    (let ((err-lab
208                            (generate-error-code vop ',error-code value)))
209                      (test-type value err-lab t (,@type-codes))
210                      (move result value))))))
211          ,@(when ptype
212              `((primitive-type-vop ,check-name (:check) ,ptype)))))))
213 \f
214 ;;;; other integer ranges
215
216 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
217   (:args (value :scs (unsigned-reg)))
218   (:arg-types unsigned-num)
219   (:translate fixnump)
220   (:temporary (:sc unsigned-reg :from (:argument 0)) tmp)
221   (:info)
222   (:conditional :z)
223   (:generator 5
224     (move tmp value)
225     (inst shr tmp n-positive-fixnum-bits)))
226
227 #-#.(cl:if (cl:= sb!vm:n-fixnum-tag-bits 1) '(:and) '(:or))
228 (define-vop (fixnump/signed-byte-64 simple-type-predicate)
229   (:args (value :scs (signed-reg)))
230   (:info)
231   (:conditional :z)
232   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
233   (:arg-types signed-num)
234   (:translate fixnump)
235   (:generator 5
236     ;; Hackers Delight, p. 53: signed
237     ;;    a <= x <= a + 2^n - 1
238     ;; is equivalent to unsigned
239     ;;    ((x-a) >> n) = 0
240     (inst mov rax-tn #.(- sb!xc:most-negative-fixnum))
241     (inst add rax-tn value)
242     (inst shr rax-tn n-fixnum-bits)))
243
244 #+#.(cl:if (cl:= sb!vm:n-fixnum-tag-bits 1) '(:and) '(:or))
245 (define-vop (fixnump/signed-byte-64 simple-type-predicate)
246   (:args (value :scs (signed-reg) :target temp))
247   (:info)
248   (:conditional :no)
249   (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
250   (:arg-types signed-num)
251   (:translate fixnump)
252   (:generator 5
253     (move temp value)
254     ;; The overflow flag will be set if the reg's sign bit changes.
255     (inst shl temp 1)))
256
257 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
258 ;;; exactly one digit.
259
260 (define-vop (signed-byte-64-p type-predicate)
261   (:translate signed-byte-64-p)
262   (:generator 45
263     (multiple-value-bind (yep nope)
264         (if not-p
265             (values not-target target)
266             (values target not-target))
267       (generate-fixnum-test value)
268       (inst jmp :e yep)
269       (move-qword-to-eax value)
270       (inst and al-tn lowtag-mask)
271       (inst cmp al-tn other-pointer-lowtag)
272       (inst jmp :ne nope)
273       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
274             (+ (ash 1 n-widetag-bits) bignum-widetag))
275       (inst jmp (if not-p :ne :e) target))
276     NOT-TARGET))
277
278 (define-vop (check-signed-byte-64 check-type)
279   (:generator 45
280     (let ((nope (generate-error-code vop
281                                      'object-not-signed-byte-64-error
282                                      value)))
283       (generate-fixnum-test value)
284       (inst jmp :e yep)
285       (move-qword-to-eax value)
286       (inst and al-tn lowtag-mask)
287       (inst cmp al-tn other-pointer-lowtag)
288       (inst jmp :ne nope)
289       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
290             (+ (ash 1 n-widetag-bits) bignum-widetag))
291       (inst jmp :ne nope))
292     YEP
293     (move result value)))
294
295 ;;; An (unsigned-byte 64) can be represented with either a positive
296 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
297 ;;; exactly two digits and the second digit all zeros.
298 (define-vop (unsigned-byte-64-p type-predicate)
299   (:translate unsigned-byte-64-p)
300   (:generator 45
301     (let ((not-target (gen-label))
302           (single-word (gen-label))
303           (fixnum (gen-label)))
304       (multiple-value-bind (yep nope)
305           (if not-p
306               (values not-target target)
307               (values target not-target))
308         ;; Is it a fixnum?
309         (generate-fixnum-test value)
310         (move rax-tn value)
311         (inst jmp :e fixnum)
312
313         ;; If not, is it an other pointer?
314         (inst and al-tn lowtag-mask)
315         (inst cmp al-tn other-pointer-lowtag)
316         (inst jmp :ne nope)
317         ;; Get the header.
318         (loadw rax-tn value 0 other-pointer-lowtag)
319         ;; Is it one?
320         (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
321         (inst jmp :e single-word)
322         ;; If it's other than two, we can't be an (unsigned-byte 64)
323         (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
324         (inst jmp :ne nope)
325         ;; Get the second digit.
326         (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
327         ;; All zeros, its an (unsigned-byte 64).
328         (inst test rax-tn rax-tn)
329         (inst jmp :z yep)
330         (inst jmp nope)
331
332         (emit-label single-word)
333         ;; Get the single digit.
334         (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
335
336         ;; positive implies (unsigned-byte 64).
337         (emit-label fixnum)
338         (inst test rax-tn rax-tn)
339         (inst jmp (if not-p :s :ns) target)
340
341         (emit-label not-target)))))
342
343 (define-vop (check-unsigned-byte-64 check-type)
344   (:generator 45
345     (let ((nope
346            (generate-error-code vop 'object-not-unsigned-byte-64-error value))
347           (yep (gen-label))
348           (fixnum (gen-label))
349           (single-word (gen-label)))
350
351       ;; Is it a fixnum?
352       (generate-fixnum-test value)
353       (move rax-tn value)
354       (inst jmp :e fixnum)
355
356       ;; If not, is it an other pointer?
357       (inst and al-tn lowtag-mask)
358       (inst cmp al-tn other-pointer-lowtag)
359       (inst jmp :ne nope)
360       ;; Get the header.
361       (loadw rax-tn value 0 other-pointer-lowtag)
362       ;; Is it one?
363       (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
364       (inst jmp :e single-word)
365       ;; If it's other than two, we can't be an (unsigned-byte 64)
366       (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
367       (inst jmp :ne nope)
368       ;; Get the second digit.
369       (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
370       ;; All zeros, its an (unsigned-byte 64).
371       (inst test rax-tn rax-tn)
372       (inst jmp :z yep)
373       (inst jmp nope)
374
375       (emit-label single-word)
376       ;; Get the single digit.
377       (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
378
379       ;; positive implies (unsigned-byte 64).
380       (emit-label fixnum)
381       (inst test rax-tn rax-tn)
382       (inst jmp :s nope)
383
384       (emit-label yep)
385       (move result value))))
386
387 (defun power-of-two-limit-p (x)
388   (and (fixnump x)
389        (= (logcount (1+ x)) 1)))
390
391 (define-vop (test-fixnum-mod-power-of-two)
392   (:args (value :scs (any-reg descriptor-reg
393                               unsigned-reg signed-reg
394                               immediate)))
395   (:arg-types *
396               (:constant (satisfies power-of-two-limit-p)))
397   (:translate fixnum-mod-p)
398   (:conditional :e)
399   (:info hi)
400   (:save-p :compute-only)
401   (:policy :fast-safe)
402   (:generator 4
403      (aver (not (sc-is value immediate)))
404      (let* ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
405                            hi
406                            (fixnumize hi))))
407        (inst test value (constantize (lognot fixnum-hi))))))
408
409 (define-vop (test-fixnum-mod-tagged-unsigned)
410   (:args (value :scs (any-reg descriptor-reg
411                               unsigned-reg signed-reg
412                               immediate)))
413   (:arg-types (:or tagged-num unsigned-num signed-num)
414               (:constant fixnum))
415   (:translate fixnum-mod-p)
416   (:conditional :be)
417   (:info hi)
418   (:save-p :compute-only)
419   (:policy :fast-safe)
420   (:generator 5
421      (aver (not (sc-is value immediate)))
422      (let ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
423                           hi
424                           (fixnumize hi))))
425        (inst cmp value (constantize fixnum-hi)))))
426
427 (define-vop (test-fixnum-mod-*)
428   (:args (value :scs (any-reg descriptor-reg)))
429   (:arg-types * (:constant fixnum))
430   (:translate fixnum-mod-p)
431   (:conditional)
432   (:info target not-p hi)
433   (:save-p :compute-only)
434   (:policy :fast-safe)
435   (:generator 6
436      (let* ((fixnum-hi (fixnumize hi))
437             (skip (gen-label)))
438        (generate-fixnum-test value)
439        (inst jmp :ne (if not-p target skip))
440        (inst cmp value (constantize fixnum-hi))
441        (inst jmp (if not-p :a :be) target)
442        (emit-label skip))))
443 \f
444 ;;;; list/symbol types
445 ;;;
446 ;;; symbolp (or symbol (eq nil))
447 ;;; consp (and list (not (eq nil)))
448
449 (define-vop (symbolp type-predicate)
450   (:translate symbolp)
451   (:generator 12
452     (let ((is-symbol-label (if not-p DROP-THRU target)))
453       (inst cmp value nil-value)
454       (inst jmp :e is-symbol-label)
455       (test-type value target not-p (symbol-header-widetag)))
456     DROP-THRU))
457
458 (define-vop (check-symbol check-type)
459   (:generator 12
460     (let ((error (generate-error-code vop 'object-not-symbol-error value)))
461       (inst cmp value nil-value)
462       (inst jmp :e DROP-THRU)
463       (test-type value error t (symbol-header-widetag)))
464     DROP-THRU
465     (move result value)))
466
467 (define-vop (consp type-predicate)
468   (:translate consp)
469   (:generator 8
470     (let ((is-not-cons-label (if not-p target DROP-THRU)))
471       (inst cmp value nil-value)
472       (inst jmp :e is-not-cons-label)
473       (test-type value target not-p (list-pointer-lowtag)))
474     DROP-THRU))
475
476 (define-vop (check-cons check-type)
477   (:generator 8
478     (let ((error (generate-error-code vop 'object-not-cons-error value)))
479       (inst cmp value nil-value)
480       (inst jmp :e error)
481       (test-type value error t (list-pointer-lowtag))
482       (move result value))))
483
484 #!+sb-simd-pack
485 (progn
486   (!define-type-vops simd-pack-p nil nil nil (simd-pack-widetag))
487
488   (define-vop (check-simd-pack check-type)
489     (:args (value :target result
490                   :scs (any-reg descriptor-reg
491                         int-sse-reg single-sse-reg double-sse-reg
492                         int-sse-stack single-sse-stack double-sse-stack)))
493     (:results (result :scs (any-reg descriptor-reg
494                            int-sse-reg single-sse-reg double-sse-reg)))
495     (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
496     (:ignore eax)
497     (:vop-var vop)
498     (:node-var node)
499     (:save-p :compute-only)
500     (:generator 50
501       (sc-case value
502         ((int-sse-reg single-sse-reg double-sse-reg
503           int-sse-stack single-sse-stack double-sse-stack)
504          (sc-case result
505            ((int-sse-reg single-sse-reg double-sse-reg)
506             (move result value))
507            ((any-reg descriptor-reg)
508             (with-fixed-allocation (result
509                                     simd-pack-widetag
510                                     simd-pack-size
511                                     node)
512               ;; see *simd-pack-element-types*
513               (storew (fixnumize
514                        (sc-case value
515                          ((int-sse-reg int-sse-stack) 0)
516                          ((single-sse-reg single-sse-stack) 1)
517                          ((double-sse-reg double-sse-stack) 2)))
518                   result simd-pack-tag-slot other-pointer-lowtag)
519               (let ((ea (make-ea-for-object-slot
520                          result simd-pack-lo-value-slot other-pointer-lowtag)))
521                 (if (float-simd-pack-p value)
522                     (inst movaps ea value)
523                     (inst movdqa ea value)))))))
524         ((any-reg descriptor-reg)
525          (let ((leaf (sb!c::tn-leaf value)))
526            (unless (and (sb!c::lvar-p leaf)
527                         (csubtypep (sb!c::lvar-type leaf)
528                                    (specifier-type 'simd-pack)))
529              (test-type
530                  value
531                  (generate-error-code vop 'object-not-simd-pack-error value)
532                  t (simd-pack-widetag))))
533          (sc-case result
534            ((int-sse-reg)
535             (let ((ea (make-ea-for-object-slot
536                        value simd-pack-lo-value-slot other-pointer-lowtag)))
537               (inst movdqa result ea)))
538            ((single-sse-reg double-sse-reg)
539             (let ((ea (make-ea-for-object-slot
540                        value simd-pack-lo-value-slot other-pointer-lowtag)))
541               (inst movaps result ea)))
542            ((any-reg descriptor-reg)
543             (move result value)))))))
544
545   (primitive-type-vop check-simd-pack (:check) simd-pack-int simd-pack-single simd-pack-double))