a1c6655588e33854897f1cf692194b49e37af187
[sbcl.git] / src / compiler / x86 / 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 generate-fixnum-test (value)
17   (emit-optimized-test-inst value 3))
18
19 (defun %test-fixnum (value target not-p)
20   (generate-fixnum-test value)
21   (inst jmp (if not-p :nz :z) target))
22
23 (defun %test-fixnum-and-headers (value target not-p headers)
24   (let ((drop-through (gen-label)))
25     (generate-fixnum-test value)
26     (inst jmp :z (if not-p drop-through target))
27     (%test-headers value target not-p nil headers drop-through)))
28
29 (defun %test-immediate (value target not-p immediate)
30   ;; Code a single instruction byte test if possible.
31   (let ((offset (tn-offset value)))
32     (cond ((and (sc-is value any-reg descriptor-reg)
33                 (or (= offset eax-offset) (= offset ebx-offset)
34                     (= offset ecx-offset) (= offset edx-offset)))
35            (inst cmp (make-random-tn :kind :normal
36                                      :sc (sc-or-lose 'byte-reg)
37                                      :offset offset)
38                  immediate))
39           (t
40            (move eax-tn value)
41            (inst cmp al-tn immediate))))
42   (inst jmp (if not-p :ne :e) target))
43
44 (defun %test-lowtag (value target not-p lowtag)
45   (inst lea eax-tn (make-ea :dword :base value :disp (- lowtag)))
46   (inst test al-tn lowtag-mask)
47   ;; FIXME: another 'optimization' which doesn't appear to work:
48   ;; prefetching the hypothetically pointed-to version should help,
49   ;; but this is in fact non-ideal in plenty of ways: we emit way too
50   ;; many of these prefetch instructions; pointed-to objects are very
51   ;; often in the cache anyway; etc. etc.  Still, as proof-of-concept,
52   ;; not too bad.  -- CSR, 2004-07-27
53   (when (member :prefetch *backend-subfeatures*)
54     (inst prefetchnta (make-ea :byte :base value :disp (- lowtag))))
55   (inst jmp (if not-p :ne :e) target))
56
57 (defun %test-headers (value target not-p function-p headers
58                             &optional (drop-through (gen-label)))
59   (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
60     (multiple-value-bind (equal less-or-equal greater-or-equal when-true when-false)
61         ;; EQUAL, LESS-OR-EQUAL and GREATER-OR-EQUAL are the conditions for
62         ;; branching to TARGET.  WHEN-TRUE and WHEN-FALSE are the
63         ;; labels to branch to when we know it's true and when we know
64         ;; it's false respectively.
65         (if not-p
66             (values :ne :a :b drop-through target)
67             (values :e :na :nb target drop-through))
68       ;; %test-lowtag loads an untagged pointer into EAX, which allows
69       ;; to avoid untagging below
70       (%test-lowtag value when-false t lowtag)
71       (cond
72         ((and (null (cdr headers))
73               (numberp (car headers)))
74          ;; Optimize the common case: referencing the value from memory
75          ;; is slightly smaller than loading it and then doing the
76          ;; comparison.  Doing this for other cases (e.g. range of
77          ;; [BIGNUM-WIDETAG..FOO-WIDETAG]) is also possible, but such
78          ;; opportunities don't come up very often and the code would
79          ;; get pretty hairy...
80          (inst cmp (make-ea :byte :base eax-tn) (car headers))
81          (inst jmp equal target))
82         (t
83          (inst mov al-tn (make-ea :byte :base eax-tn))
84          (do ((remaining headers (cdr remaining)))
85              ((null remaining))
86            (let ((header (car remaining))
87                  (last (null (cdr remaining))))
88              (cond
89                ((atom header)
90                 (cond
91                   ((and (not last) (null (cddr remaining))
92                         (atom (cadr remaining))
93                         (= (logcount (logxor header (cadr remaining))) 1))
94                    ;; BASE-STRING, (VECTOR NIL), BIT-VECTOR, (VECTOR T)
95                    (inst and al-tn (ldb (byte 8 0) (logeqv header (cadr remaining))))
96                    (inst cmp al-tn (ldb (byte 8 0) (logand header (cadr remaining))))
97                    (inst jmp equal target)
98                    (return))
99                   (t
100                    (inst cmp al-tn header)
101                    (if last
102                        (inst jmp equal target)
103                        (inst jmp :e when-true)))))
104                (t
105                 (let ((start (car header))
106                       (end (cdr header)))
107                   (cond
108                     ;; LAST = don't need al-tn later
109                     ((and last (not (= start bignum-widetag))
110                           (= (+ start 4) end) (= (logcount (logxor start end)) 1))
111                      ;; SIMPLE-STRING
112                      (inst and al-tn (ldb (byte 8 0) (logeqv start end)))
113                      (inst cmp al-tn (ldb (byte 8 0) (logand start end)))
114                      (inst jmp equal target))
115                     ((and (not last) (null (cddr remaining))
116                           (= (+ start 4) end) (= (logcount (logxor start end)) 1)
117                           (listp (cadr remaining))
118                           (= (+ (caadr remaining) 4) (cdadr remaining))
119                           (= (logcount (logxor (caadr remaining) (cdadr remaining))) 1)
120                           (= (logcount (logxor (caadr remaining) start)) 1))
121                      ;; STRING
122                      (inst and al-tn (ldb (byte 8 0) (logeqv start (cdadr remaining))))
123                      (inst cmp al-tn (ldb (byte 8 0) (logand start (cdadr remaining))))
124                      (inst jmp equal target)
125                      ;; we've shortcircuited the DO, so we must return.
126                      ;; It's OK to do so, because (NULL (CDDR REMAINING))
127                      ;; was true.
128                      (return))
129                     (t
130                      (cond
131                        ((= start bignum-widetag)
132                         (inst cmp al-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 al-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                         (if last
146                             (inst jmp less-or-equal target)
147                             (inst jmp :be when-true)))
148                        (t
149                         (inst sub al-tn start)
150                         (inst cmp al-tn (- end start))
151                         (inst jmp less-or-equal target))))))))))))
152       (emit-label drop-through))))
153 \f
154 ;;;; type checking and testing
155
156 (define-vop (check-type)
157   (:args (value :target result :scs (any-reg descriptor-reg)))
158   (:results (result :scs (any-reg descriptor-reg)))
159   (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
160   (:ignore eax)
161   (:vop-var vop)
162   (:save-p :compute-only))
163
164 (define-vop (type-predicate)
165   (:args (value :scs (any-reg descriptor-reg)))
166   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
167   (:ignore eax)
168   (:conditional)
169   (:info target not-p)
170   (:policy :fast-safe))
171
172 ;;; simpler VOP that don't need a temporary register
173 (define-vop (simple-check-type)
174   (:args (value :target result :scs (any-reg descriptor-reg)))
175   (:results (result :scs (any-reg descriptor-reg)
176                     :load-if (not (and (sc-is value any-reg descriptor-reg)
177                                        (sc-is result control-stack)))))
178   (:vop-var vop)
179   (:save-p :compute-only))
180
181 (define-vop (simple-type-predicate)
182   (:args (value :scs (any-reg descriptor-reg control-stack)))
183   (:conditional)
184   (:info target not-p)
185   (:policy :fast-safe))
186
187 (defun cost-to-test-types (type-codes)
188   (+ (* 2 (length type-codes))
189      (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
190
191 (defmacro !define-type-vops (pred-name check-name ptype error-code
192                              (&rest type-codes)
193                              &key (variant nil variant-p) &allow-other-keys)
194   ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
195   ;; expansion?
196   (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
197          (prefix (if variant-p
198                      (concatenate 'string (string variant) "-")
199                      "")))
200     `(progn
201        ,@(when pred-name
202            `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
203                (:translate ,pred-name)
204                (:generator ,cost
205                  (test-type value target not-p (,@type-codes))))))
206        ,@(when check-name
207            `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
208                (:generator ,cost
209                  (let ((err-lab
210                         (generate-error-code vop ',error-code value)))
211                    (test-type value err-lab t (,@type-codes))
212                    (move result value))))))
213        ,@(when ptype
214            `((primitive-type-vop ,check-name (:check) ,ptype))))))
215 \f
216 ;;;; other integer ranges
217
218 (define-vop (fixnump/unsigned-byte-32 simple-type-predicate)
219   (:args (value :scs (unsigned-reg)))
220   (:info)
221   (:conditional :be)
222   (:arg-types unsigned-num)
223   (:translate fixnump)
224   (:generator 5
225     ;; We could encode this with :Z and SHR, analogously to the signed-byte-32
226     ;; case below -- as we do on x86-64 -- but that costs us an extra
227     ;; register. Compromises...
228     (inst cmp value #.sb!xc:most-positive-fixnum)))
229
230 (define-vop (fixnump/signed-byte-32 type-predicate)
231   (:args (value :scs (signed-reg)))
232   (:info)
233   (:conditional :z)
234   (:arg-types signed-num)
235   (:translate fixnump)
236   (:generator 5
237     ;; Hackers Delight, p. 53: signed
238     ;;    a <= x <= a + 2^n - 1
239     ;; is equivalent to unsigned
240     ;;    ((x-a) >> n) = 0
241     (inst mov eax-tn value)
242     (inst sub eax-tn #.sb!xc:most-negative-fixnum)
243     (inst shr eax-tn #.(integer-length (- sb!xc:most-positive-fixnum
244                                           sb!xc:most-negative-fixnum)))))
245
246 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
247 ;;; exactly one digit.
248
249 (define-vop (signed-byte-32-p type-predicate)
250   (:translate signed-byte-32-p)
251   (:generator 45
252     (multiple-value-bind (yep nope)
253         (if not-p
254             (values not-target target)
255             (values target not-target))
256       (generate-fixnum-test value)
257       (inst jmp :e yep)
258       (inst lea eax-tn (make-ea :dword :base value
259                                 :disp (- other-pointer-lowtag)))
260       (inst test al-tn lowtag-mask)
261       (inst jmp :ne nope)
262       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
263             (+ (ash 1 n-widetag-bits) bignum-widetag))
264       (inst jmp (if not-p :ne :e) target))
265     NOT-TARGET))
266
267 (define-vop (check-signed-byte-32 check-type)
268   (:generator 45
269     (let ((nope (generate-error-code vop
270                                      'object-not-signed-byte-32-error
271                                      value)))
272       (generate-fixnum-test value)
273       (inst jmp :e yep)
274       (inst lea eax-tn (make-ea :dword :base value
275                                 :disp (- other-pointer-lowtag)))
276       (inst test al-tn lowtag-mask)
277       (inst jmp :ne nope)
278       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
279             (+ (ash 1 n-widetag-bits) bignum-widetag))
280       (inst jmp :ne nope))
281     YEP
282     (move result value)))
283
284 ;;; An (unsigned-byte 32) can be represented with either a positive
285 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
286 ;;; exactly two digits and the second digit all zeros.
287 (define-vop (unsigned-byte-32-p type-predicate)
288   (:translate unsigned-byte-32-p)
289   (:generator 45
290     (let ((not-target (gen-label))
291           (single-word (gen-label))
292           (fixnum (gen-label)))
293       (multiple-value-bind (yep nope)
294           (if not-p
295               (values not-target target)
296               (values target not-target))
297         ;; Is it a fixnum?
298         (generate-fixnum-test value)
299         (move eax-tn value)
300         (inst jmp :e fixnum)
301
302         ;; If not, is it an other pointer?
303         (inst and al-tn lowtag-mask)
304         (inst cmp al-tn other-pointer-lowtag)
305         (inst jmp :ne nope)
306         ;; Get the header.
307         (loadw eax-tn value 0 other-pointer-lowtag)
308         ;; Is it one?
309         (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
310         (inst jmp :e single-word)
311         ;; If it's other than two, we can't be an (unsigned-byte 32)
312         (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
313         (inst jmp :ne nope)
314         ;; Get the second digit.
315         (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
316         ;; All zeros, its an (unsigned-byte 32).
317         (inst test eax-tn eax-tn)
318         (inst jmp :z yep)
319         (inst jmp nope)
320
321         (emit-label single-word)
322         ;; Get the single digit.
323         (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
324
325         ;; positive implies (unsigned-byte 32).
326         (emit-label fixnum)
327         (inst test eax-tn eax-tn)
328         (inst jmp (if not-p :s :ns) target)
329
330         (emit-label not-target)))))
331
332 (define-vop (check-unsigned-byte-32 check-type)
333   (:generator 45
334     (let ((nope
335            (generate-error-code vop 'object-not-unsigned-byte-32-error value))
336           (yep (gen-label))
337           (fixnum (gen-label))
338           (single-word (gen-label)))
339
340       ;; Is it a fixnum?
341       (generate-fixnum-test value)
342       (move eax-tn value)
343       (inst jmp :e fixnum)
344
345       ;; If not, is it an other pointer?
346       (inst and al-tn lowtag-mask)
347       (inst cmp al-tn other-pointer-lowtag)
348       (inst jmp :ne nope)
349       ;; Get the header.
350       (loadw eax-tn value 0 other-pointer-lowtag)
351       ;; Is it one?
352       (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
353       (inst jmp :e single-word)
354       ;; If it's other than two, we can't be an (unsigned-byte 32)
355       (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
356       (inst jmp :ne nope)
357       ;; Get the second digit.
358       (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
359       ;; All zeros, its an (unsigned-byte 32).
360       (inst test eax-tn eax-tn)
361       (inst jmp :z yep)
362       (inst jmp nope)
363
364       (emit-label single-word)
365       ;; Get the single digit.
366       (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
367
368       ;; positive implies (unsigned-byte 32).
369       (emit-label fixnum)
370       (inst test eax-tn eax-tn)
371       (inst jmp :s nope)
372
373       (emit-label yep)
374       (move result value))))
375
376 (defun power-of-two-limit-p (x)
377   (and (fixnump x)
378        (= (logcount (1+ x)) 1)))
379
380 (define-vop (test-fixnum-mod-power-of-two)
381   (:args (value :scs (any-reg descriptor-reg
382                               unsigned-reg signed-reg
383                               immediate)))
384   (:arg-types *
385               (:constant (satisfies power-of-two-limit-p)))
386   (:translate sb!c::fixnum-mod-p)
387   (:conditional :e)
388   (:info hi)
389   (:save-p :compute-only)
390   (:policy :fast-safe)
391   (:generator 4
392      (aver (not (sc-is value immediate)))
393      (let* ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
394                            hi
395                            (fixnumize hi))))
396        (inst test value (lognot fixnum-hi)))))
397
398 (define-vop (test-fixnum-mod-tagged-unsigned)
399   (:args (value :scs (any-reg descriptor-reg
400                               unsigned-reg signed-reg
401                               immediate)))
402   (:arg-types (:or tagged-num unsigned-num signed-num)
403               (:constant fixnum))
404   (:translate sb!c::fixnum-mod-p)
405   (:conditional :be)
406   (:info hi)
407   (:save-p :compute-only)
408   (:policy :fast-safe)
409   (:generator 5
410      (aver (not (sc-is value immediate)))
411      (let ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
412                           hi
413                           (fixnumize hi))))
414        (inst cmp value fixnum-hi))))
415
416 (define-vop (test-fixnum-mod-*)
417   (:args (value :scs (any-reg descriptor-reg)))
418   (:arg-types * (:constant fixnum))
419   (:translate sb!c::fixnum-mod-p)
420   (:conditional)
421   (:info target not-p hi)
422   (:save-p :compute-only)
423   (:policy :fast-safe)
424   (:generator 6
425      (let* ((fixnum-hi (fixnumize hi))
426             (skip (gen-label)))
427        (generate-fixnum-test value)
428        (inst jmp :ne (if not-p target skip))
429        (inst cmp value fixnum-hi)
430        (inst jmp (if not-p :a :be) target)
431        (emit-label skip))))
432
433 \f
434 ;;;; list/symbol types
435 ;;;
436 ;;; symbolp (or symbol (eq nil))
437 ;;; consp (and list (not (eq nil)))
438
439 (define-vop (symbolp type-predicate)
440   (:translate symbolp)
441   (:generator 12
442     (let ((is-symbol-label (if not-p drop-thru target)))
443       (inst cmp value nil-value)
444       (inst jmp :e is-symbol-label)
445       (test-type value target not-p (symbol-header-widetag)))
446     DROP-THRU))
447
448 (define-vop (check-symbol check-type)
449   (:generator 12
450     (let ((error (generate-error-code vop 'object-not-symbol-error value)))
451       (inst cmp value nil-value)
452       (inst jmp :e drop-thru)
453       (test-type value error t (symbol-header-widetag)))
454     DROP-THRU
455     (move result value)))
456
457 (define-vop (consp type-predicate)
458   (:translate consp)
459   (:generator 8
460     (let ((is-not-cons-label (if not-p target drop-thru)))
461       (inst cmp value nil-value)
462       (inst jmp :e is-not-cons-label)
463       (test-type value target not-p (list-pointer-lowtag)))
464     DROP-THRU))
465
466 (define-vop (check-cons check-type)
467   (:generator 8
468     (let ((error (generate-error-code vop 'object-not-cons-error value)))
469       (inst cmp value nil-value)
470       (inst jmp :e error)
471       (test-type value error t (list-pointer-lowtag))
472       (move result value))))