0.8.9.18
[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
90 (/show0 "about to !DEF-PRIMITIVE-TYPE COMPLEX-SINGLE-FLOAT")
91 (!def-primitive-type complex-single-float (complex-single-reg descriptor-reg)
92   :type (complex single-float))
93 (/show0 "about to !DEF-PRIMITIVE-TYPE COMPLEX-DOUBLE-FLOAT")
94 (!def-primitive-type complex-double-float (complex-double-reg descriptor-reg)
95   :type (complex double-float))
96
97
98 ;;; primitive other-pointer array types
99 (/show0 "primtype.lisp 96")
100 (macrolet ((define-simple-array-primitive-types ()
101                `(progn
102                  ,@(map 'list
103                         (lambda (saetp)
104                           `(!def-primitive-type
105                             ,(saetp-primitive-type-name saetp)
106                             (descriptor-reg)
107                             :type (simple-array ,(saetp-specifier saetp) (*))))
108                         *specialized-array-element-type-properties*))))
109   (define-simple-array-primitive-types))
110 ;;; Note: The complex array types are not included, 'cause it is
111 ;;; pointless to restrict VOPs to them.
112
113 ;;; other primitive other-pointer types
114 (!def-primitive-type system-area-pointer (sap-reg descriptor-reg))
115 (!def-primitive-type weak-pointer (descriptor-reg))
116
117 ;;; miscellaneous primitive types that don't exist at the LISP level
118 (!def-primitive-type catch-block (catch-block) :type nil)
119 \f
120 ;;;; PRIMITIVE-TYPE-OF and friends
121
122 ;;; Return the most restrictive primitive type that contains OBJECT.
123 (/show0 "primtype.lisp 147")
124 (!def-vm-support-routine primitive-type-of (object)
125   (let ((type (ctype-of object)))
126     (cond ((not (member-type-p type)) (primitive-type type))
127           ((equal (member-type-members type) '(nil))
128            (primitive-type-or-lose 'list))
129           (t
130            *backend-t-primitive-type*))))
131
132 ;;; Return the primitive type corresponding to a type descriptor
133 ;;; structure. The second value is true when the primitive type is
134 ;;; exactly equivalent to the argument Lisp type.
135 ;;;
136 ;;; In a bootstrapping situation, we should be careful to use the
137 ;;; correct values for the system parameters.
138 ;;;
139 ;;; We need an aux function because we need to use both
140 ;;; !DEF-VM-SUPPORT-ROUTINE and DEFUN-CACHED.
141 (/show0 "primtype.lisp 188")
142 (!def-vm-support-routine primitive-type (type)
143   (primitive-type-aux type))
144 (/show0 "primtype.lisp 191")
145 (defun-cached (primitive-type-aux
146                :hash-function (lambda (x)
147                                 (logand (type-hash-value x) #x1FF))
148                :hash-bits 9
149                :values 2
150                :default (values nil :empty))
151               ((type eq))
152   (declare (type ctype type))
153   (macrolet ((any () '(values *backend-t-primitive-type* nil))
154              (exactly (type)
155                `(values (primitive-type-or-lose ',type) t))
156              (part-of (type)
157                `(values (primitive-type-or-lose ',type) nil)))
158     (flet ((maybe-numeric-type-union (t1 t2)
159              (let ((t1-name (primitive-type-name t1))
160                    (t2-name (primitive-type-name t2)))
161                (case t1-name
162                  (positive-fixnum
163                   (if (or (eq t2-name 'fixnum)
164                           (eq t2-name #!-alpha 'signed-byte-32
165                                       #!+alpha 'signed-byte-64)
166                           (eq t2-name #!-alpha 'unsigned-byte-31
167                                       #!+alpha 'unsigned-byte-63)
168                           (eq t2-name #!-alpha 'unsigned-byte-32
169                                       #!+alpha 'unsigned-byte-64))
170                       t2))
171                  (fixnum
172                   (case t2-name
173                     (#!-alpha signed-byte-32
174                      #!+alpha signed-byte-64 t2)
175                     (#!-alpha unsigned-byte-31
176                      #!+alpha unsigned-byte-63
177                      (primitive-type-or-lose
178                       #!-alpha 'signed-byte-32
179                       #!+alpha 'signed-byte-64))))
180                  (#!-alpha signed-byte-32
181                   #!+alpha signed-byte-64
182                   (if (eq t2-name #!-alpha 'unsigned-byte-31
183                                   #!+alpha 'unsigned-byte-63)
184                       t1))
185                  (#!-alpha unsigned-byte-31
186                   #!+alpha unsigned-byte-63
187                   (if (eq t2-name #!-alpha 'unsigned-byte-32
188                                   #!+alpha 'unsigned-byte-64)
189                       t2))))))
190       (etypecase type
191         (numeric-type
192          (let ((lo (numeric-type-low type))
193                (hi (numeric-type-high type)))
194            (case (numeric-type-complexp type)
195              (:real
196               (case (numeric-type-class type)
197                 (integer
198                  (cond ((and hi lo)
199                         (dolist (spec
200                                   `((positive-fixnum 0 ,(1- (ash 1 29)))
201                                     #!-alpha
202                                     (unsigned-byte-31 0 ,(1- (ash 1 31)))
203                                     #!-alpha
204                                     (unsigned-byte-32 0 ,(1- (ash 1 32)))
205                                     #!+alpha
206                                     (unsigned-byte-63 0 ,(1- (ash 1 63)))
207                                     #!+alpha
208                                     (unsigned-byte-64 0 ,(1- (ash 1 64)))
209                                     (fixnum ,(ash -1 29)
210                                             ,(1- (ash 1 29)))
211                                     #!-alpha
212                                     (signed-byte-32 ,(ash -1 31)
213                                                           ,(1- (ash 1 31)))
214                                     #!+alpha
215                                     (signed-byte-64 ,(ash -1 63)
216                                                     ,(1- (ash 1 63))))
217                                  (if (or (< hi (ash -1 29))
218                                          (> lo (1- (ash 1 29))))
219                                      (part-of bignum)
220                                      (any)))
221                           (let ((type (car spec))
222                                 (min (cadr spec))
223                                 (max (caddr spec)))
224                             (when (<= min lo hi max)
225                               (return (values
226                                        (primitive-type-or-lose type)
227                                        (and (= lo min) (= hi max))))))))
228                        ((or (and hi (< hi sb!xc:most-negative-fixnum))
229                             (and lo (> lo sb!xc:most-positive-fixnum)))
230                         (part-of bignum))
231                        (t
232                         (any))))
233                 (float
234                  (let ((exact (and (null lo) (null hi))))
235                    (case (numeric-type-format type)
236                      ((short-float single-float)
237                       (values (primitive-type-or-lose 'single-float)
238                               exact))
239                      ((double-float)
240                       (values (primitive-type-or-lose 'double-float)
241                               exact))
242                      (t
243                       (any)))))
244                 (t
245                  (any))))
246              (:complex
247               (if (eq (numeric-type-class type) 'float)
248                   (let ((exact (and (null lo) (null hi))))
249                     (case (numeric-type-format type)
250                       ((short-float single-float)
251                        (values (primitive-type-or-lose 'complex-single-float)
252                                exact))
253                       ((double-float long-float)
254                        (values (primitive-type-or-lose 'complex-double-float)
255                                exact))
256                       (t
257                        (part-of complex))))
258                   (part-of complex)))
259              (t
260               (any)))))
261         (array-type
262          (if (array-type-complexp type)
263              (any)
264              (let* ((dims (array-type-dimensions type))
265                     (etype (array-type-specialized-element-type type))
266                     (type-spec (type-specifier etype))
267                     ;; FIXME: We're _WHAT_?  Testing for type equality
268                     ;; with a specifier and #'EQUAL?  *BOGGLE*.  --
269                     ;; CSR, 2003-06-24
270                     (ptype (cdr (assoc type-spec *simple-array-primitive-types*
271                                        :test #'equal))))
272                (if (and (consp dims) (null (rest dims)) ptype)
273                    (values (primitive-type-or-lose ptype)
274                            (eq (first dims) '*))
275                    (any)))))
276         (union-type
277          (if (type= type (specifier-type 'list))
278              (exactly list)
279              (let ((types (union-type-types type)))
280                (multiple-value-bind (res exact) (primitive-type (first types))
281                  (dolist (type (rest types) (values res exact))
282                    (multiple-value-bind (ptype ptype-exact)
283                        (primitive-type type)
284                      (unless ptype-exact (setq exact nil))
285                      (unless (eq ptype res)
286                        (let ((new-ptype
287                               (or (maybe-numeric-type-union res ptype)
288                                   (maybe-numeric-type-union ptype res))))
289                          (if new-ptype
290                              (setq res new-ptype)
291                              (return (any)))))))))))
292         (member-type
293          (let* ((members (member-type-members type))
294                 (res (primitive-type-of (first members))))
295            (dolist (mem (rest members) (values res nil))
296              (let ((ptype (primitive-type-of mem)))
297                (unless (eq ptype res)
298                  (let ((new-ptype (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         (named-type
304          (ecase (named-type-name type)
305            ((t *) (values *backend-t-primitive-type* t))
306            ((nil) (any))))
307         (built-in-classoid
308          (case (classoid-name type)
309            ((complex function instance
310              system-area-pointer weak-pointer)
311             (values (primitive-type-or-lose (classoid-name type)) t))
312            (funcallable-instance
313             (part-of function))
314            (base-char
315             (exactly base-char))
316            (cons-type
317             (part-of list))
318            (t
319             (any))))
320         (fun-type
321          (exactly function))
322         (classoid
323          (if (csubtypep type (specifier-type 'function))
324              (part-of function)
325              (part-of instance)))
326         (ctype
327          (if (csubtypep type (specifier-type 'function))
328              (part-of function)
329              (any)))))))
330
331 (/show0 "primtype.lisp end of file")