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