e3e59b28f83121b8fe81a7ca9896aab60589fb13
[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 #.sb!vm:n-positive-fixnum-bits))
27 (/show0 "primtype.lisp 27")
28 #!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 32) '(and) '(or))
29 (!def-primitive-type unsigned-byte-31 (signed-reg unsigned-reg descriptor-reg)
30   :type (unsigned-byte 31))
31 (/show0 "primtype.lisp 31")
32 #!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 32) '(and) '(or))
33 (!def-primitive-type unsigned-byte-32 (unsigned-reg descriptor-reg)
34   :type (unsigned-byte 32))
35 (/show0 "primtype.lisp 35")
36 #!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
37 (!def-primitive-type unsigned-byte-63 (signed-reg unsigned-reg descriptor-reg)
38   :type (unsigned-byte 63))
39 #!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
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 #.(1+ sb!vm:n-positive-fixnum-bits)))
44 #!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 32) '(and) '(or))
45 (!def-primitive-type signed-byte-32 (signed-reg descriptor-reg)
46   :type (signed-byte 32))
47 #!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
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 (progn
56   (!def-primitive-type-alias unsigned-num #1=
57     #!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
58     (:or unsigned-byte-64 unsigned-byte-63 positive-fixnum)
59     #!-#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
60     (:or unsigned-byte-32 unsigned-byte-31 positive-fixnum))
61   (!def-primitive-type-alias signed-num #2=
62     #!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
63     (:or signed-byte-64 fixnum unsigned-byte-63 positive-fixnum)
64     #!-#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
65     (:or signed-byte-32 fixnum unsigned-byte-31 positive-fixnum))
66   (!def-primitive-type-alias untagged-num
67     (:or . #.(print (union (cdr '#1#) (cdr '#2#))))))
68
69 ;;; other primitive immediate types
70 (/show0 "primtype.lisp 68")
71 (!def-primitive-type character (character-reg any-reg))
72
73 ;;; primitive pointer types
74 (/show0 "primtype.lisp 73")
75 (!def-primitive-type function (descriptor-reg))
76 (!def-primitive-type list (descriptor-reg))
77 (!def-primitive-type instance (descriptor-reg))
78
79 (/show0 "primtype.lisp 77")
80 (!def-primitive-type funcallable-instance (descriptor-reg))
81
82 ;;; primitive other-pointer number types
83 (/show0 "primtype.lisp 81")
84 (!def-primitive-type bignum (descriptor-reg))
85 (!def-primitive-type ratio (descriptor-reg))
86 (!def-primitive-type complex (descriptor-reg))
87 (/show0 "about to !DEF-PRIMITIVE-TYPE SINGLE-FLOAT")
88 (!def-primitive-type single-float (single-reg descriptor-reg))
89 (/show0 "about to !DEF-PRIMITIVE-TYPE DOUBLE-FLOAT")
90 (!def-primitive-type double-float (double-reg descriptor-reg))
91
92 (/show0 "about to !DEF-PRIMITIVE-TYPE COMPLEX-SINGLE-FLOAT")
93 (!def-primitive-type complex-single-float (complex-single-reg descriptor-reg)
94   :type (complex single-float))
95 (/show0 "about to !DEF-PRIMITIVE-TYPE COMPLEX-DOUBLE-FLOAT")
96 (!def-primitive-type complex-double-float (complex-double-reg descriptor-reg)
97   :type (complex double-float))
98
99
100 ;;; primitive other-pointer array types
101 (/show0 "primtype.lisp 96")
102 (macrolet ((define-simple-array-primitive-types ()
103                `(progn
104                  ,@(map 'list
105                         (lambda (saetp)
106                           `(!def-primitive-type
107                             ,(saetp-primitive-type-name saetp)
108                             (descriptor-reg)
109                             :type (simple-array ,(saetp-specifier saetp) (*))))
110                         *specialized-array-element-type-properties*))))
111   (define-simple-array-primitive-types))
112 ;;; Note: The complex array types are not included, 'cause it is
113 ;;; pointless to restrict VOPs to them.
114
115 ;;; other primitive other-pointer types
116 (!def-primitive-type system-area-pointer (sap-reg descriptor-reg))
117 (!def-primitive-type weak-pointer (descriptor-reg))
118
119 ;;; miscellaneous primitive types that don't exist at the LISP level
120 (!def-primitive-type catch-block (catch-block) :type nil)
121 \f
122 ;;;; PRIMITIVE-TYPE-OF and friends
123
124 ;;; Return the most restrictive primitive type that contains OBJECT.
125 (/show0 "primtype.lisp 147")
126 (!def-vm-support-routine primitive-type-of (object)
127   (let ((type (ctype-of object)))
128     (cond ((not (member-type-p type)) (primitive-type type))
129           ((equal (member-type-members type) '(nil))
130            (primitive-type-or-lose 'list))
131           (t
132            *backend-t-primitive-type*))))
133
134 ;;; Return the primitive type corresponding to a type descriptor
135 ;;; structure. The second value is true when the primitive type is
136 ;;; exactly equivalent to the argument Lisp type.
137 ;;;
138 ;;; In a bootstrapping situation, we should be careful to use the
139 ;;; correct values for the system parameters.
140 ;;;
141 ;;; We need an aux function because we need to use both
142 ;;; !DEF-VM-SUPPORT-ROUTINE and DEFUN-CACHED.
143 (/show0 "primtype.lisp 188")
144 (!def-vm-support-routine primitive-type (type)
145   (primitive-type-aux type))
146 (/show0 "primtype.lisp 191")
147 (defun-cached (primitive-type-aux
148                :hash-function (lambda (x)
149                                 (logand (type-hash-value x) #x1FF))
150                :hash-bits 9
151                :values 2
152                :default (values nil :empty))
153               ((type eq))
154   (declare (type ctype type))
155   (macrolet ((any () '(values *backend-t-primitive-type* nil))
156              (exactly (type)
157                `(values (primitive-type-or-lose ',type) t))
158              (part-of (type)
159                `(values (primitive-type-or-lose ',type) nil)))
160     (flet ((maybe-numeric-type-union (t1 t2)
161              (let ((t1-name (primitive-type-name t1))
162                    (t2-name (primitive-type-name t2)))
163                (case t1-name
164                  (positive-fixnum
165                   (if (or (eq t2-name 'fixnum)
166                           (eq t2-name
167                               (ecase sb!vm::n-machine-word-bits
168                                 (32 'signed-byte-32)
169                                 (64 'signed-byte-64)))
170                           (eq t2-name
171                               (ecase sb!vm::n-machine-word-bits
172                                 (32 'unsigned-byte-31)
173                                 (64 'unsigned-byte-63)))
174                           (eq t2-name
175                               (ecase sb!vm::n-machine-word-bits
176                                 (32 'unsigned-byte-32)
177                                 (64 'unsigned-byte-64))))
178                       t2))
179                  (fixnum
180                   (case t2-name
181                     (#.(ecase sb!vm::n-machine-word-bits
182                          (32 'signed-byte-32)
183                          (64 'signed-byte-64))
184                        t2)
185                     (#.(ecase sb!vm::n-machine-word-bits
186                          (32 'unsigned-byte-31)
187                          (64 'unsigned-byte-63))
188                        (primitive-type-or-lose
189                         (ecase sb!vm::n-machine-word-bits
190                           (32 'signed-byte-32)
191                           (64 'signed-byte-64))))))
192                  (#.(ecase sb!vm::n-machine-word-bits
193                       (32 'signed-byte-32)
194                       (64 'signed-byte-64))
195                   (if (eq t2-name
196                           (ecase sb!vm::n-machine-word-bits
197                             (32 'unsigned-byte-31)
198                             (64 'unsigned-byte-63)))
199                       t1))
200                  (#.(ecase sb!vm::n-machine-word-bits
201                       (32 'unsigned-byte-31)
202                       (64 'unsigned-byte-63))
203                     (if (eq t2-name
204                             (ecase sb!vm::n-machine-word-bits
205                               (32 'unsigned-byte-32)
206                               (64 'unsigned-byte-64)))
207                         t2))))))
208       (etypecase type
209         (numeric-type
210          (let ((lo (numeric-type-low type))
211                (hi (numeric-type-high type)))
212            (case (numeric-type-complexp type)
213              (:real
214               (case (numeric-type-class type)
215                 (integer
216                  (cond ((and hi lo)
217                         (dolist (spec
218                                   `((positive-fixnum 0 ,sb!xc:most-positive-fixnum)
219                                     ,@(ecase sb!vm::n-machine-word-bits
220                                         (32
221                                          `((unsigned-byte-31
222                                             0 ,(1- (ash 1 31)))
223                                            (unsigned-byte-32
224                                             0 ,(1- (ash 1 32)))))
225                                         (64
226                                          `((unsigned-byte-63
227                                             0 ,(1- (ash 1 63)))
228                                            (unsigned-byte-64
229                                             0 ,(1- (ash 1 64))))))
230                                     (fixnum ,sb!xc:most-negative-fixnum
231                                             ,sb!xc:most-positive-fixnum)
232                                     ,(ecase sb!vm::n-machine-word-bits
233                                        (32
234                                         `(signed-byte-32 ,(ash -1 31)
235                                                          ,(1- (ash 1 31))))
236                                        (64
237                                         `(signed-byte-64 ,(ash -1 63)
238                                                          ,(1- (ash 1 63))))))
239                                  (if (or (< hi sb!xc:most-negative-fixnum)
240                                          (> lo sb!xc:most-positive-fixnum))
241                                      (part-of bignum)
242                                      (any)))
243                           (let ((type (car spec))
244                                 (min (cadr spec))
245                                 (max (caddr spec)))
246                             (when (<= min lo hi max)
247                               (return (values
248                                        (primitive-type-or-lose type)
249                                        (and (= lo min) (= hi max))))))))
250                        ((or (and hi (< hi sb!xc:most-negative-fixnum))
251                             (and lo (> lo sb!xc:most-positive-fixnum)))
252                         (part-of bignum))
253                        (t
254                         (any))))
255                 (float
256                  (let ((exact (and (null lo) (null hi))))
257                    (case (numeric-type-format type)
258                      ((short-float single-float)
259                       (values (primitive-type-or-lose 'single-float)
260                               exact))
261                      ((double-float)
262                       (values (primitive-type-or-lose 'double-float)
263                               exact))
264                      (t
265                       (any)))))
266                 (t
267                  (any))))
268              (:complex
269               (if (eq (numeric-type-class type) 'float)
270                   (let ((exact (and (null lo) (null hi))))
271                     (case (numeric-type-format type)
272                       ((short-float single-float)
273                        (values (primitive-type-or-lose 'complex-single-float)
274                                exact))
275                       ((double-float long-float)
276                        (values (primitive-type-or-lose 'complex-double-float)
277                                exact))
278                       (t
279                        (part-of complex))))
280                   (part-of complex)))
281              (t
282               (any)))))
283         (array-type
284          (if (array-type-complexp type)
285              (any)
286              (let* ((dims (array-type-dimensions type))
287                     (etype (array-type-specialized-element-type type))
288                     (type-spec (type-specifier etype))
289                     ;; FIXME: We're _WHAT_?  Testing for type equality
290                     ;; with a specifier and #'EQUAL?  *BOGGLE*.  --
291                     ;; CSR, 2003-06-24
292                     (ptype (cdr (assoc type-spec *simple-array-primitive-types*
293                                        :test #'equal))))
294                (if (and (consp dims) (null (rest dims)) ptype)
295                    (values (primitive-type-or-lose ptype)
296                            (eq (first dims) '*))
297                    (any)))))
298         (union-type
299          (if (type= type (specifier-type 'list))
300              (exactly list)
301              (let ((types (union-type-types type)))
302                (multiple-value-bind (res exact) (primitive-type (first types))
303                  (dolist (type (rest types) (values res exact))
304                    (multiple-value-bind (ptype ptype-exact)
305                        (primitive-type type)
306                      (unless ptype-exact (setq exact nil))
307                      (unless (eq ptype res)
308                        (let ((new-ptype
309                               (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         (intersection-type
315          (let ((types (intersection-type-types type))
316                (res (any)))
317            ;; why NIL for the exact?  Well, we assume that the
318            ;; intersection type is in fact doing something for us:
319            ;; that is, that each of the types in the intersection is
320            ;; in fact cutting off some of the type lattice.  Since no
321            ;; intersection type is represented by a primitive type and
322            ;; primitive types are mutually exclusive, it follows that
323            ;; no intersection type can represent the entirety of the
324            ;; primitive type.  (And NIL is the conservative answer,
325            ;; anyway).  -- CSR, 2006-09-14
326            (dolist (type types (values res nil))
327              (multiple-value-bind (ptype)
328                  (primitive-type type)
329                (cond
330                  ;; if the result so far is (any), any improvement on
331                  ;; the specificity of the primitive type is valid.
332                  ((eq res (any))
333                   (setq res ptype))
334                  ;; if the primitive type returned is (any), the
335                  ;; result so far is valid.  Likewise, if the
336                  ;; primitive type is the same as the result so far,
337                  ;; everything is fine.
338                  ((or (eq ptype (any)) (eq ptype res)))
339                  ;; otherwise, we have something hairy and confusing,
340                  ;; such as (and condition funcallable-instance).
341                  ;; Punt.
342                  (t (return (any))))))))
343         (member-type
344          (let* ((members (member-type-members type))
345                 (res (primitive-type-of (first members))))
346            (dolist (mem (rest members) (values res nil))
347              (let ((ptype (primitive-type-of mem)))
348                (unless (eq ptype res)
349                  (let ((new-ptype (or (maybe-numeric-type-union res ptype)
350                                       (maybe-numeric-type-union ptype res))))
351                    (if new-ptype
352                        (setq res new-ptype)
353                        (return (any)))))))))
354         (named-type
355          (ecase (named-type-name type)
356            ((t *) (values *backend-t-primitive-type* t))
357            ((instance) (exactly instance))
358            ((funcallable-instance) (part-of function))
359            ((nil) (any))))
360         (character-set-type
361          (let ((pairs (character-set-type-pairs type)))
362            (if (and (= (length pairs) 1)
363                     (= (caar pairs) 0)
364                     (= (cdar pairs) (1- sb!xc:char-code-limit)))
365                (exactly character)
366                (part-of character))))
367         (built-in-classoid
368          (case (classoid-name type)
369            ((complex function system-area-pointer weak-pointer)
370             (values (primitive-type-or-lose (classoid-name type)) t))
371            (cons-type
372             (part-of list))
373            (t
374             (any))))
375         (fun-type
376          (exactly function))
377         (classoid
378          (if (csubtypep type (specifier-type 'function))
379              (part-of function)
380              (part-of instance)))
381         (ctype
382          (if (csubtypep type (specifier-type 'function))
383              (part-of function)
384              (any)))))))
385
386 (/show0 "primtype.lisp end of file")