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 #.sb!vm:n-positive-fixnum-bits))
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 #.(1+ sb!vm:n-positive-fixnum-bits)))
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 character (character-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 (/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))
98 ;;; primitive other-pointer array types
99 (/show0 "primtype.lisp 96")
100 (macrolet ((define-simple-array-primitive-types ()
104 `(!def-primitive-type
105 ,(saetp-primitive-type-name saetp)
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.
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))
117 ;;; miscellaneous primitive types that don't exist at the LISP level
118 (!def-primitive-type catch-block (catch-block) :type nil)
120 ;;;; PRIMITIVE-TYPE-OF and friends
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))
130 *backend-t-primitive-type*))))
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.
136 ;;; In a bootstrapping situation, we should be careful to use the
137 ;;; correct values for the system parameters.
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))
150 :default (values nil :empty))
152 (declare (type ctype type))
153 (macrolet ((any () '(values *backend-t-primitive-type* nil))
155 `(values (primitive-type-or-lose ',type) t))
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)))
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))
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)
185 (#!-alpha unsigned-byte-31
186 #!+alpha unsigned-byte-63
187 (if (eq t2-name #!-alpha 'unsigned-byte-32
188 #!+alpha 'unsigned-byte-64)
192 (let ((lo (numeric-type-low type))
193 (hi (numeric-type-high type)))
194 (case (numeric-type-complexp type)
196 (case (numeric-type-class type)
200 `((positive-fixnum 0 ,sb!xc:most-positive-fixnum)
202 (unsigned-byte-31 0 ,(1- (ash 1 31)))
204 (unsigned-byte-32 0 ,(1- (ash 1 32)))
206 (unsigned-byte-63 0 ,(1- (ash 1 63)))
208 (unsigned-byte-64 0 ,(1- (ash 1 64)))
209 (fixnum ,sb!xc:most-negative-fixnum
210 ,sb!xc:most-positive-fixnum)
212 (signed-byte-32 ,(ash -1 31)
215 (signed-byte-64 ,(ash -1 63)
217 (if (or (< hi sb!xc:most-negative-fixnum)
218 (> lo sb!xc:most-positive-fixnum))
221 (let ((type (car spec))
224 (when (<= min lo hi max)
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)))
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)
240 (values (primitive-type-or-lose 'double-float)
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)
253 ((double-float long-float)
254 (values (primitive-type-or-lose 'complex-double-float)
262 (if (array-type-complexp type)
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*. --
270 (ptype (cdr (assoc type-spec *simple-array-primitive-types*
272 (if (and (consp dims) (null (rest dims)) ptype)
273 (values (primitive-type-or-lose ptype)
274 (eq (first dims) '*))
277 (if (type= type (specifier-type '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)
287 (or (maybe-numeric-type-union res ptype)
288 (maybe-numeric-type-union ptype res))))
291 (return (any)))))))))))
293 (let ((types (intersection-type-types type))
296 (dolist (type types (values res exact))
297 (when (eq type (specifier-type 'function))
298 ;; KLUDGE: Deal with (and function instance), both of which
299 ;; have an exact primitive type.
300 (return (part-of function)))
301 (multiple-value-bind (ptype ptype-exact)
302 (primitive-type type)
304 ;; Apart from the previous kludge exact primitive
305 ;; types should match, if indeed there are any. It
306 ;; may be that this assumption isn't really safe,
307 ;; but at least we'll see what breaks. -- NS 20041104
308 (aver (or (not exact) (eq ptype res)))
310 (when (or ptype-exact (and (not exact) (eq res (any))))
311 ;; Try to find a narrower representation then
312 ;; (any). Takes care of undecidable types in
313 ;; intersections with decidable ones.
314 (setq res ptype))))))
316 (let* ((members (member-type-members type))
317 (res (primitive-type-of (first members))))
318 (dolist (mem (rest members) (values res nil))
319 (let ((ptype (primitive-type-of mem)))
320 (unless (eq ptype res)
321 (let ((new-ptype (or (maybe-numeric-type-union res ptype)
322 (maybe-numeric-type-union ptype res))))
325 (return (any)))))))))
327 (ecase (named-type-name type)
328 ((t *) (values *backend-t-primitive-type* t))
331 (let ((pairs (character-set-type-pairs type)))
332 (if (and (= (length pairs) 1)
334 (= (cdar pairs) (1- sb!xc:char-code-limit)))
336 (part-of character))))
338 (case (classoid-name type)
339 ((complex function instance
340 system-area-pointer weak-pointer)
341 (values (primitive-type-or-lose (classoid-name type)) t))
342 (funcallable-instance
351 (if (csubtypep type (specifier-type 'function))
355 (if (csubtypep type (specifier-type 'function))
359 (/show0 "primtype.lisp end of file")