1 ;;;; machine-independent aspects of the object representation and
4 ;;;; This software is part of the SBCL system. See the README file for
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.
15 ;;;; primitive type definitions
17 (/show0 "primtype.lisp 17")
19 (!def-primitive-type t (descriptor-reg))
20 (/show0 "primtype.lisp 20")
21 (setf *backend-t-primitive-type* (primitive-type-or-lose t))
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")
29 (!def-primitive-type unsigned-byte-31 (signed-reg unsigned-reg descriptor-reg)
30 :type (unsigned-byte 31))
31 (/show0 "primtype.lisp 31")
33 (!def-primitive-type unsigned-byte-32 (unsigned-reg descriptor-reg)
34 :type (unsigned-byte 32))
35 (/show0 "primtype.lisp 35")
37 (!def-primitive-type unsigned-byte-63 (signed-reg unsigned-reg descriptor-reg)
38 :type (unsigned-byte 63))
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))
45 (!def-primitive-type signed-byte-32 (signed-reg descriptor-reg)
46 :type (signed-byte 32))
48 (!def-primitive-type signed-byte-64 (signed-reg descriptor-reg)
49 :type (signed-byte 64))
51 (defvar *fixnum-primitive-type* (primitive-type-or-lose 'fixnum))
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
60 (!def-primitive-type-alias signed-num (:or #!-alpha signed-byte-32
61 #!+alpha signed-byte-64
63 #!-alpha unsigned-byte-31
64 #!+alpha unsigned-byte-63
67 ;;; other primitive immediate types
68 (/show0 "primtype.lisp 68")
69 (!def-primitive-type base-char (base-char-reg any-reg))
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))
77 (/show0 "primtype.lisp 77")
78 (!def-primitive-type funcallable-instance (descriptor-reg))
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))
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))
98 (!def-primitive-type complex-long-float (complex-long-reg descriptor-reg)
99 :type (complex long-float))
101 ;;; primitive other-pointer array types
102 (/show0 "primtype.lisp 96")
103 (macrolet ((define-simple-array-primitive-types ()
107 `(!def-primitive-type
108 ,(saetp-primitive-type-name saetp)
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.
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))
120 ;;; miscellaneous primitive types that don't exist at the LISP level
121 (!def-primitive-type catch-block (catch-block) :type nil)
123 ;;;; PRIMITIVE-TYPE-OF and friends
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))
133 *backend-t-primitive-type*))))
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.
139 ;;; In a bootstrapping situation, we should be careful to use the
140 ;;; correct values for the system parameters.
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))
153 :default (values nil :empty))
155 (declare (type ctype type))
156 (macrolet ((any () '(values *backend-t-primitive-type* nil))
158 `(values (primitive-type-or-lose ',type) t))
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)))
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))
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)
188 (#!-alpha unsigned-byte-31
189 #!+alpha unsigned-byte-63
190 (if (eq t2-name #!-alpha 'unsigned-byte-32
191 #!+alpha 'unsigned-byte-64)
195 (let ((lo (numeric-type-low type))
196 (hi (numeric-type-high type)))
197 (case (numeric-type-complexp type)
199 (case (numeric-type-class type)
203 `((positive-fixnum 0 ,(1- (ash 1 29)))
205 (unsigned-byte-31 0 ,(1- (ash 1 31)))
207 (unsigned-byte-32 0 ,(1- (ash 1 32)))
209 (unsigned-byte-63 0 ,(1- (ash 1 63)))
211 (unsigned-byte-64 0 ,(1- (ash 1 64)))
215 (signed-byte-32 ,(ash -1 31)
218 (signed-byte-64 ,(ash -1 63)
220 (if (or (< hi (ash -1 29))
221 (> lo (1- (ash 1 29))))
224 (let ((type (car spec))
227 (when (<= min lo hi max)
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)))
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)
242 ((double-float #!-long-float long-float)
243 (values (primitive-type-or-lose 'double-float)
247 (values (primitive-type-or-lose 'long-float)
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)
260 ((double-float #!-long-float long-float)
261 (values (primitive-type-or-lose 'complex-double-float)
265 (values (primitive-type-or-lose 'complex-long-float)
273 (if (array-type-complexp type)
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*. --
281 (ptype (cdr (assoc type-spec *simple-array-primitive-types*
283 (if (and (consp dims) (null (rest dims)) ptype)
284 (values (primitive-type-or-lose ptype)
285 (eq (first dims) '*))
288 (if (type= type (specifier-type '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)
298 (or (maybe-numeric-type-union res ptype)
299 (maybe-numeric-type-union ptype res))))
302 (return (any)))))))))))
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))))
313 (return (any)))))))))
315 (ecase (named-type-name type)
316 ((t *) (values *backend-t-primitive-type* t))
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
334 (if (csubtypep type (specifier-type 'function))
338 (if (csubtypep type (specifier-type 'function))
342 (/show0 "primtype.lisp end of file")