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