1.0.37.57: better DEFMETHOD pretty-printing
[sbcl/pkhuong.git] / src / compiler / generic / primtype.lisp
blob43c5e26f35c6483df2ba736dfa1aaa65cd5b2c5e
1 ;;;; machine-independent aspects of the object representation and
2 ;;;; primitive types
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.
13 (in-package "SB!VM")
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")
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))
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 (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 . #.(sort (copy-list (union (cdr '#1#) (cdr '#2#))) #'string<))))
69 ;;; other primitive immediate types
70 (/show0 "primtype.lisp 68")
71 (!def-primitive-type character (character-reg any-reg))
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))
79 (/show0 "primtype.lisp 77")
80 (!def-primitive-type funcallable-instance (descriptor-reg))
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))
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))
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.
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))
119 ;;; miscellaneous primitive types that don't exist at the LISP level
120 (!def-primitive-type catch-block (catch-block) :type nil)
122 ;;;; PRIMITIVE-TYPE-OF and friends
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 ((and (eql 1 (member-type-size 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 (sb!kernel::maybe-reparse-specifier! type)
147 (primitive-type-aux type))
148 (/show0 "primtype.lisp 191")
149 (defun-cached (primitive-type-aux
150 :hash-function (lambda (x)
151 (logand (type-hash-value x) #x1FF))
152 :hash-bits 9
153 :values 2
154 :default (values nil :empty))
155 ((type eq))
156 (declare (type ctype type))
157 (macrolet ((any () '(values *backend-t-primitive-type* nil))
158 (exactly (type)
159 `(values (primitive-type-or-lose ',type) t))
160 (part-of (type)
161 `(values (primitive-type-or-lose ',type) nil)))
162 (flet ((maybe-numeric-type-union (t1 t2)
163 (let ((t1-name (primitive-type-name t1))
164 (t2-name (primitive-type-name t2)))
165 (case t1-name
166 (positive-fixnum
167 (if (or (eq t2-name 'fixnum)
168 (eq t2-name
169 (ecase sb!vm::n-machine-word-bits
170 (32 'signed-byte-32)
171 (64 'signed-byte-64)))
172 (eq t2-name
173 (ecase sb!vm::n-machine-word-bits
174 (32 'unsigned-byte-31)
175 (64 'unsigned-byte-63)))
176 (eq t2-name
177 (ecase sb!vm::n-machine-word-bits
178 (32 'unsigned-byte-32)
179 (64 'unsigned-byte-64))))
180 t2))
181 (fixnum
182 (case t2-name
183 (#.(ecase sb!vm::n-machine-word-bits
184 (32 'signed-byte-32)
185 (64 'signed-byte-64))
187 (#.(ecase sb!vm::n-machine-word-bits
188 (32 'unsigned-byte-31)
189 (64 'unsigned-byte-63))
190 (primitive-type-or-lose
191 (ecase sb!vm::n-machine-word-bits
192 (32 'signed-byte-32)
193 (64 'signed-byte-64))))))
194 (#.(ecase sb!vm::n-machine-word-bits
195 (32 'signed-byte-32)
196 (64 'signed-byte-64))
197 (if (eq t2-name
198 (ecase sb!vm::n-machine-word-bits
199 (32 'unsigned-byte-31)
200 (64 'unsigned-byte-63)))
201 t1))
202 (#.(ecase sb!vm::n-machine-word-bits
203 (32 'unsigned-byte-31)
204 (64 'unsigned-byte-63))
205 (if (eq t2-name
206 (ecase sb!vm::n-machine-word-bits
207 (32 'unsigned-byte-32)
208 (64 'unsigned-byte-64)))
209 t2))))))
210 (etypecase type
211 (numeric-type
212 (let ((lo (numeric-type-low type))
213 (hi (numeric-type-high type)))
214 (case (numeric-type-complexp type)
215 (:real
216 (case (numeric-type-class type)
217 (integer
218 (cond ((and hi lo)
219 (dolist (spec
220 `((positive-fixnum 0 ,sb!xc:most-positive-fixnum)
221 ,@(ecase sb!vm::n-machine-word-bits
223 `((unsigned-byte-31
224 0 ,(1- (ash 1 31)))
225 (unsigned-byte-32
226 0 ,(1- (ash 1 32)))))
228 `((unsigned-byte-63
229 0 ,(1- (ash 1 63)))
230 (unsigned-byte-64
231 0 ,(1- (ash 1 64))))))
232 (fixnum ,sb!xc:most-negative-fixnum
233 ,sb!xc:most-positive-fixnum)
234 ,(ecase sb!vm::n-machine-word-bits
236 `(signed-byte-32 ,(ash -1 31)
237 ,(1- (ash 1 31))))
239 `(signed-byte-64 ,(ash -1 63)
240 ,(1- (ash 1 63))))))
241 (if (or (< hi sb!xc:most-negative-fixnum)
242 (> lo sb!xc:most-positive-fixnum))
243 (part-of bignum)
244 (any)))
245 (let ((type (car spec))
246 (min (cadr spec))
247 (max (caddr spec)))
248 (when (<= min lo hi max)
249 (return (values
250 (primitive-type-or-lose type)
251 (and (= lo min) (= hi max))))))))
252 ((or (and hi (< hi sb!xc:most-negative-fixnum))
253 (and lo (> lo sb!xc:most-positive-fixnum)))
254 (part-of bignum))
256 (any))))
257 (float
258 (let ((exact (and (null lo) (null hi))))
259 (case (numeric-type-format type)
260 ((short-float single-float)
261 (values (primitive-type-or-lose 'single-float)
262 exact))
263 ((double-float)
264 (values (primitive-type-or-lose 'double-float)
265 exact))
267 (any)))))
269 (any))))
270 (:complex
271 (if (eq (numeric-type-class type) 'float)
272 (let ((exact (and (null lo) (null hi))))
273 (case (numeric-type-format type)
274 ((short-float single-float)
275 (values (primitive-type-or-lose 'complex-single-float)
276 exact))
277 ((double-float long-float)
278 (values (primitive-type-or-lose 'complex-double-float)
279 exact))
281 (part-of complex))))
282 (part-of complex)))
284 (any)))))
285 (array-type
286 (if (array-type-complexp type)
287 (any)
288 (let* ((dims (array-type-dimensions type))
289 (etype (array-type-specialized-element-type type))
290 (type-spec (type-specifier etype))
291 ;; FIXME: We're _WHAT_? Testing for type equality
292 ;; with a specifier and #'EQUAL? *BOGGLE*. --
293 ;; CSR, 2003-06-24
294 (ptype (cdr (assoc type-spec *simple-array-primitive-types*
295 :test #'equal))))
296 (if (and (consp dims) (null (rest dims)) ptype)
297 (values (primitive-type-or-lose ptype)
298 (eq (first dims) '*))
299 (any)))))
300 (union-type
301 (if (type= type (specifier-type 'list))
302 (exactly list)
303 (let ((types (union-type-types type)))
304 (multiple-value-bind (res exact) (primitive-type (first types))
305 (dolist (type (rest types) (values res exact))
306 (multiple-value-bind (ptype ptype-exact)
307 (primitive-type type)
308 (unless ptype-exact (setq exact nil))
309 (unless (eq ptype res)
310 (let ((new-ptype
311 (or (maybe-numeric-type-union res ptype)
312 (maybe-numeric-type-union ptype res))))
313 (if new-ptype
314 (setq res new-ptype)
315 (return (any)))))))))))
316 (intersection-type
317 (let ((types (intersection-type-types type))
318 (res (any)))
319 ;; why NIL for the exact? Well, we assume that the
320 ;; intersection type is in fact doing something for us:
321 ;; that is, that each of the types in the intersection is
322 ;; in fact cutting off some of the type lattice. Since no
323 ;; intersection type is represented by a primitive type and
324 ;; primitive types are mutually exclusive, it follows that
325 ;; no intersection type can represent the entirety of the
326 ;; primitive type. (And NIL is the conservative answer,
327 ;; anyway). -- CSR, 2006-09-14
328 (dolist (type types (values res nil))
329 (multiple-value-bind (ptype)
330 (primitive-type type)
331 (cond
332 ;; if the result so far is (any), any improvement on
333 ;; the specificity of the primitive type is valid.
334 ((eq res (any))
335 (setq res ptype))
336 ;; if the primitive type returned is (any), the
337 ;; result so far is valid. Likewise, if the
338 ;; primitive type is the same as the result so far,
339 ;; everything is fine.
340 ((or (eq ptype (any)) (eq ptype res)))
341 ;; otherwise, we have something hairy and confusing,
342 ;; such as (and condition funcallable-instance).
343 ;; Punt.
344 (t (return (any))))))))
345 (member-type
346 (let (res)
347 (block nil
348 (mapc-member-type-members
349 (lambda (member)
350 (let ((ptype (primitive-type-of member)))
351 (if res
352 (unless (eq ptype res)
353 (let ((new-ptype (or (maybe-numeric-type-union res ptype)
354 (maybe-numeric-type-union ptype res))))
355 (if new-ptype
356 (setq res new-ptype)
357 (return (any)))))
358 (setf res ptype))))
359 type))
360 res))
361 (named-type
362 (ecase (named-type-name type)
363 ((t *) (values *backend-t-primitive-type* t))
364 ((instance) (exactly instance))
365 ((funcallable-instance) (part-of function))
366 ((extended-sequence) (any))
367 ((nil) (any))))
368 (character-set-type
369 (let ((pairs (character-set-type-pairs type)))
370 (if (and (= (length pairs) 1)
371 (= (caar pairs) 0)
372 (= (cdar pairs) (1- sb!xc:char-code-limit)))
373 (exactly character)
374 (part-of character))))
375 (built-in-classoid
376 (case (classoid-name type)
377 ((complex function system-area-pointer weak-pointer)
378 (values (primitive-type-or-lose (classoid-name type)) t))
379 (cons-type
380 (part-of list))
382 (any))))
383 (fun-type
384 (exactly function))
385 (classoid
386 (if (csubtypep type (specifier-type 'function))
387 (part-of function)
388 (part-of instance)))
389 (ctype
390 (if (csubtypep type (specifier-type 'function))
391 (part-of function)
392 (any)))))))
394 (/show0 "primtype.lisp end of file")