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