0.8.0.78.vector-nil-string.7:
[sbcl.git] / src / compiler / generic / primtype.lisp
1 ;;;; machine-independent aspects of the object representation and
2 ;;;; primitive types
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!VM")
14 \f
15 ;;;; primitive type definitions
16
17 (/show0 "primtype.lisp 17")
18
19 (!def-primitive-type t (descriptor-reg))
20 (/show0 "primtype.lisp 20")
21 (setf *backend-t-primitive-type* (primitive-type-or-lose t))
22
23 ;;; primitive integer types that fit in registers
24 (/show0 "primtype.lisp 24")
25 (!def-primitive-type positive-fixnum (any-reg signed-reg unsigned-reg)
26   :type (unsigned-byte 29))
27 (/show0 "primtype.lisp 27")
28 #!-alpha
29 (!def-primitive-type unsigned-byte-31 (signed-reg unsigned-reg descriptor-reg)
30   :type (unsigned-byte 31))
31 (/show0 "primtype.lisp 31")
32 #!-alpha
33 (!def-primitive-type unsigned-byte-32 (unsigned-reg descriptor-reg)
34   :type (unsigned-byte 32))
35 (/show0 "primtype.lisp 35")
36 #!+alpha
37 (!def-primitive-type unsigned-byte-63 (signed-reg unsigned-reg descriptor-reg)
38   :type (unsigned-byte 63))
39 #!+alpha
40 (!def-primitive-type unsigned-byte-64 (unsigned-reg descriptor-reg)
41   :type (unsigned-byte 64))
42 (!def-primitive-type fixnum (any-reg signed-reg)
43   :type (signed-byte 30))
44 #!-alpha
45 (!def-primitive-type signed-byte-32 (signed-reg descriptor-reg)
46   :type (signed-byte 32))
47 #!+alpha
48 (!def-primitive-type signed-byte-64 (signed-reg descriptor-reg)
49   :type (signed-byte 64))
50
51 (defvar *fixnum-primitive-type* (primitive-type-or-lose 'fixnum))
52
53 (/show0 "primtype.lisp 53")
54 (!def-primitive-type-alias tagged-num (:or positive-fixnum fixnum))
55 (!def-primitive-type-alias unsigned-num (:or #!-alpha unsigned-byte-32
56                                              #!-alpha unsigned-byte-31
57                                              #!+alpha unsigned-byte-64
58                                              #!+alpha unsigned-byte-63
59                                              positive-fixnum))
60 (!def-primitive-type-alias signed-num (:or #!-alpha signed-byte-32
61                                            #!+alpha signed-byte-64
62                                            fixnum
63                                            #!-alpha unsigned-byte-31
64                                            #!+alpha unsigned-byte-63
65                                            positive-fixnum))
66
67 ;;; other primitive immediate types
68 (/show0 "primtype.lisp 68")
69 (!def-primitive-type base-char (base-char-reg any-reg))
70
71 ;;; primitive pointer types
72 (/show0 "primtype.lisp 73")
73 (!def-primitive-type function (descriptor-reg))
74 (!def-primitive-type list (descriptor-reg))
75 (!def-primitive-type instance (descriptor-reg))
76
77 (/show0 "primtype.lisp 77")
78 (!def-primitive-type funcallable-instance (descriptor-reg))
79
80 ;;; primitive other-pointer number types
81 (/show0 "primtype.lisp 81")
82 (!def-primitive-type bignum (descriptor-reg))
83 (!def-primitive-type ratio (descriptor-reg))
84 (!def-primitive-type complex (descriptor-reg))
85 (/show0 "about to !DEF-PRIMITIVE-TYPE SINGLE-FLOAT")
86 (!def-primitive-type single-float (single-reg descriptor-reg))
87 (/show0 "about to !DEF-PRIMITIVE-TYPE DOUBLE-FLOAT")
88 (!def-primitive-type double-float (double-reg descriptor-reg))
89 #!+long-float
90 (!def-primitive-type long-float (long-reg descriptor-reg))
91 (/show0 "about to !DEF-PRIMITIVE-TYPE COMPLEX-SINGLE-FLOAT")
92 (!def-primitive-type complex-single-float (complex-single-reg descriptor-reg)
93   :type (complex single-float))
94 (/show0 "about to !DEF-PRIMITIVE-TYPE COMPLEX-DOUBLE-FLOAT")
95 (!def-primitive-type complex-double-float (complex-double-reg descriptor-reg)
96   :type (complex double-float))
97 #!+long-float
98 (!def-primitive-type complex-long-float (complex-long-reg descriptor-reg)
99   :type (complex long-float))
100
101 ;;; primitive other-pointer array types
102 (/show0 "primtype.lisp 96")
103 (macrolet ((define-simple-array-primitive-types ()
104                `(progn
105                  ,@(map 'list
106                         (lambda (saetp)
107                           `(!def-primitive-type
108                             ,(saetp-primitive-type-name saetp)
109                             (descriptor-reg)
110                             :type (simple-array ,(saetp-specifier saetp) (*))))
111                         *specialized-array-element-type-properties*))))
112   (define-simple-array-primitive-types))
113 ;;; Note: The complex array types are not included, 'cause it is
114 ;;; pointless to restrict VOPs to them.
115
116 ;;; other primitive other-pointer types
117 (!def-primitive-type system-area-pointer (sap-reg descriptor-reg))
118 (!def-primitive-type weak-pointer (descriptor-reg))
119
120 ;;; miscellaneous primitive types that don't exist at the LISP level
121 (!def-primitive-type catch-block (catch-block) :type nil)
122 \f
123 ;;;; PRIMITIVE-TYPE-OF and friends
124
125 ;;; Return the most restrictive primitive type that contains OBJECT.
126 (/show0 "primtype.lisp 147")
127 (!def-vm-support-routine primitive-type-of (object)
128   (let ((type (ctype-of object)))
129     (cond ((not (member-type-p type)) (primitive-type type))
130           ((equal (member-type-members type) '(nil))
131            (primitive-type-or-lose 'list))
132           (t
133            *backend-t-primitive-type*))))
134
135 ;;; Return the primitive type corresponding to a type descriptor
136 ;;; structure. The second value is true when the primitive type is
137 ;;; exactly equivalent to the argument Lisp type.
138 ;;;
139 ;;; In a bootstrapping situation, we should be careful to use the
140 ;;; correct values for the system parameters.
141 ;;;
142 ;;; We need an aux function because we need to use both
143 ;;; !DEF-VM-SUPPORT-ROUTINE and DEFUN-CACHED.
144 (/show0 "primtype.lisp 188")
145 (!def-vm-support-routine primitive-type (type)
146   (primitive-type-aux type))
147 (/show0 "primtype.lisp 191")
148 (defun-cached (primitive-type-aux
149                :hash-function (lambda (x)
150                                 (logand (type-hash-value x) #x1FF))
151                :hash-bits 9
152                :values 2
153                :default (values nil :empty))
154               ((type eq))
155   (declare (type ctype type))
156   (macrolet ((any () '(values *backend-t-primitive-type* nil))
157              (exactly (type)
158                `(values (primitive-type-or-lose ',type) t))
159              (part-of (type)
160                `(values (primitive-type-or-lose ',type) nil)))
161     (flet ((maybe-numeric-type-union (t1 t2)
162              (let ((t1-name (primitive-type-name t1))
163                    (t2-name (primitive-type-name t2)))
164                (case t1-name
165                  (positive-fixnum
166                   (if (or (eq t2-name 'fixnum)
167                           (eq t2-name #!-alpha 'signed-byte-32
168                                       #!+alpha 'signed-byte-64)
169                           (eq t2-name #!-alpha 'unsigned-byte-31
170                                       #!+alpha 'unsigned-byte-63)
171                           (eq t2-name #!-alpha 'unsigned-byte-32
172                                       #!+alpha 'unsigned-byte-64))
173                       t2))
174                  (fixnum
175                   (case t2-name
176                     (#!-alpha signed-byte-32
177                      #!+alpha signed-byte-64 t2)
178                     (#!-alpha unsigned-byte-31
179                      #!+alpha unsigned-byte-63
180                      (primitive-type-or-lose
181                       #!-alpha 'signed-byte-32
182                       #!+alpha 'signed-byte-64))))
183                  (#!-alpha signed-byte-32
184                   #!+alpha signed-byte-64
185                   (if (eq t2-name #!-alpha 'unsigned-byte-31
186                                   #!+alpha 'unsigned-byte-63)
187                       t1))
188                  (#!-alpha unsigned-byte-31
189                   #!+alpha unsigned-byte-63
190                   (if (eq t2-name #!-alpha 'unsigned-byte-32
191                                   #!+alpha 'unsigned-byte-64)
192                       t2))))))
193       (etypecase type
194         (numeric-type
195          (let ((lo (numeric-type-low type))
196                (hi (numeric-type-high type)))
197            (case (numeric-type-complexp type)
198              (:real
199               (case (numeric-type-class type)
200                 (integer
201                  (cond ((and hi lo)
202                         (dolist (spec
203                                   `((positive-fixnum 0 ,(1- (ash 1 29)))
204                                     #!-alpha
205                                     (unsigned-byte-31 0 ,(1- (ash 1 31)))
206                                     #!-alpha
207                                     (unsigned-byte-32 0 ,(1- (ash 1 32)))
208                                     #!+alpha
209                                     (unsigned-byte-63 0 ,(1- (ash 1 63)))
210                                     #!+alpha
211                                     (unsigned-byte-64 0 ,(1- (ash 1 64)))
212                                     (fixnum ,(ash -1 29)
213                                             ,(1- (ash 1 29)))
214                                     #!-alpha
215                                     (signed-byte-32 ,(ash -1 31)
216                                                           ,(1- (ash 1 31)))
217                                     #!+alpha
218                                     (signed-byte-64 ,(ash -1 63)
219                                                     ,(1- (ash 1 63))))
220                                  (if (or (< hi (ash -1 29))
221                                          (> lo (1- (ash 1 29))))
222                                      (part-of bignum)
223                                      (any)))
224                           (let ((type (car spec))
225                                 (min (cadr spec))
226                                 (max (caddr spec)))
227                             (when (<= min lo hi max)
228                               (return (values
229                                        (primitive-type-or-lose type)
230                                        (and (= lo min) (= hi max))))))))
231                        ((or (and hi (< hi most-negative-fixnum))
232                             (and lo (> lo most-positive-fixnum)))
233                         (part-of bignum))
234                        (t
235                         (any))))
236                 (float
237                  (let ((exact (and (null lo) (null hi))))
238                    (case (numeric-type-format type)
239                      ((short-float single-float)
240                       (values (primitive-type-or-lose 'single-float)
241                               exact))
242                      ((double-float #!-long-float long-float)
243                       (values (primitive-type-or-lose 'double-float)
244                               exact))
245                      #!+long-float
246                      (long-float
247                       (values (primitive-type-or-lose 'long-float)
248                               exact))
249                      (t
250                       (any)))))
251                 (t
252                  (any))))
253              (:complex
254               (if (eq (numeric-type-class type) 'float)
255                   (let ((exact (and (null lo) (null hi))))
256                     (case (numeric-type-format type)
257                       ((short-float single-float)
258                        (values (primitive-type-or-lose 'complex-single-float)
259                                exact))
260                       ((double-float #!-long-float long-float)
261                        (values (primitive-type-or-lose 'complex-double-float)
262                                exact))
263                       #!+long-float
264                       (long-float
265                        (values (primitive-type-or-lose 'complex-long-float)
266                                exact))
267                       (t
268                        (part-of complex))))
269                   (part-of complex)))
270              (t
271               (any)))))
272         (array-type
273          (if (array-type-complexp type)
274              (any)
275              (let* ((dims (array-type-dimensions type))
276                     (etype (array-type-specialized-element-type type))
277                     (type-spec (type-specifier etype))
278                     ;; FIXME: We're _WHAT_?  Testing for type equality
279                     ;; with a specifier and #'EQUAL?  *BOGGLE*.  --
280                     ;; CSR, 2003-06-24
281                     (ptype (cdr (assoc type-spec *simple-array-primitive-types*
282                                        :test #'equal))))
283                (if (and (consp dims) (null (rest dims)) ptype)
284                    (values (primitive-type-or-lose ptype)
285                            (eq (first dims) '*))
286                    (any)))))
287         (union-type
288          (if (type= type (specifier-type 'list))
289              (exactly list)
290              (let ((types (union-type-types type)))
291                (multiple-value-bind (res exact) (primitive-type (first types))
292                  (dolist (type (rest types) (values res exact))
293                    (multiple-value-bind (ptype ptype-exact)
294                        (primitive-type type)
295                      (unless ptype-exact (setq exact nil))
296                      (unless (eq ptype res)
297                        (let ((new-ptype
298                               (or (maybe-numeric-type-union res ptype)
299                                   (maybe-numeric-type-union ptype res))))
300                          (if new-ptype
301                              (setq res new-ptype)
302                              (return (any)))))))))))
303         (member-type
304          (let* ((members (member-type-members type))
305                 (res (primitive-type-of (first members))))
306            (dolist (mem (rest members) (values res nil))
307              (let ((ptype (primitive-type-of mem)))
308                (unless (eq ptype res)
309                  (let ((new-ptype (or (maybe-numeric-type-union res ptype)
310                                       (maybe-numeric-type-union ptype res))))
311                    (if new-ptype
312                        (setq res new-ptype)
313                        (return (any)))))))))
314         (named-type
315          (ecase (named-type-name type)
316            ((t *) (values *backend-t-primitive-type* t))
317            ((nil) (any))))
318         (built-in-classoid
319          (case (classoid-name type)
320            ((complex function instance
321              system-area-pointer weak-pointer)
322             (values (primitive-type-or-lose (classoid-name type)) t))
323            (funcallable-instance
324             (part-of function))
325            (base-char
326             (exactly base-char))
327            (cons-type
328             (part-of list))
329            (t
330             (any))))
331         (fun-type
332          (exactly function))
333         (classoid
334          (if (csubtypep type (specifier-type 'function))
335              (part-of function)
336              (part-of instance)))
337         (ctype
338          (if (csubtypep type (specifier-type 'function))
339              (part-of function)
340              (any)))))))
341
342 (/show0 "primtype.lisp end of file")