1 ;;;; This file contains implementation-dependent parts of the type
2 ;;;; support code. This is stuff which deals with the mapping from
3 ;;;; types defined in Common Lisp to types actually supported by an
6 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
15 (in-package "SB!KERNEL")
17 ;;;; FIXME: I'm not sure where to put this. -- WHN 19990817
19 (def!type sb!vm:word () `(unsigned-byte ,sb!vm:n-word-bits))
20 (def!type sb!vm:signed-word () `(signed-byte ,sb!vm:n-word-bits))
23 ;;;; implementation-dependent DEFTYPEs
25 ;;; Make DOUBLE-FLOAT a synonym for LONG-FLOAT, SINGLE-FLOAT for
26 ;;; SHORT-FLOAT. This is expanded before the translator gets a chance,
27 ;;; so we will get precedence.
29 (setf (info :type :kind 'long-float) :defined)
31 (sb!xc:deftype long-float (&optional low high)
32 `(double-float ,low ,high))
33 (setf (info :type :kind 'short-float) :defined)
34 (sb!xc:deftype short-float (&optional low high)
35 `(single-float ,low ,high))
37 ;;; an index into an integer
38 (sb!xc:deftype bit-index () `(integer 0 ,sb!xc:most-positive-fixnum))
40 ;;; worst-case values for float attributes
41 (sb!xc:deftype float-exponent ()
42 #!-long-float 'double-float-exponent
43 #!+long-float 'long-float-exponent)
44 (sb!xc:deftype float-digits ()
45 #!-long-float `(integer 0 ,sb!vm:double-float-digits)
46 #!+long-float `(integer 0 ,sb!vm:long-float-digits))
47 (sb!xc:deftype float-radix () '(integer 2 2))
48 (sb!xc:deftype float-int-exponent ()
49 #!-long-float 'double-float-int-exponent
50 #!+long-float 'long-float-int-exponent)
53 (sb!xc:deftype boole-code () '(unsigned-byte 4))
55 ;;; a byte specifier (as generated by BYTE)
56 (sb!xc:deftype byte-specifier () 'cons)
58 ;;; result of CHAR-INT
59 (sb!xc:deftype char-int () 'char-code)
61 ;;; PATHNAME pieces, as returned by the PATHNAME-xxx functions
62 (sb!xc:deftype pathname-host () '(or sb!impl::host null))
63 (sb!xc:deftype pathname-device ()
64 '(or simple-string (member nil :unspecific)))
65 (sb!xc:deftype pathname-directory () 'list)
66 (sb!xc:deftype pathname-name ()
67 '(or simple-string sb!impl::pattern (member nil :unspecific :wild)))
68 (sb!xc:deftype pathname-type ()
69 '(or simple-string sb!impl::pattern (member nil :unspecific :wild)))
70 (sb!xc:deftype pathname-version ()
71 '(or integer (member nil :newest :wild :unspecific)))
73 ;;; internal time format. (Note: not a FIXNUM, ouch..)
74 (sb!xc:deftype internal-time () 'unsigned-byte)
76 (sb!xc:deftype bignum-element-type () `(unsigned-byte ,sb!vm:n-word-bits))
77 (sb!xc:deftype bignum-type () 'bignum)
78 ;;; FIXME: see also DEFCONSTANT MAXIMUM-BIGNUM-LENGTH in
79 ;;; src/code/bignum.lisp. -- CSR, 2004-07-19
80 (sb!xc:deftype bignum-index ()
81 '(integer 0 #.(1- (ash 1 (- 32 sb!vm:n-widetag-bits)))))
83 ;;;; hooks into the type system
85 (sb!xc:deftype unboxed-array (&optional dims)
86 (collect ((types (list 'or)))
87 (dolist (type *specialized-array-element-types*)
88 (when (subtypep type '(or integer character float (complex float)))
89 (types `(array ,type ,dims))))
92 (sb!xc:deftype simple-unboxed-array (&optional dims)
93 (collect ((types (list 'or)))
94 (dolist (type *specialized-array-element-types*)
95 (when (subtypep type '(or integer character float (complex float)))
96 (types `(simple-array ,type ,dims))))
99 ;;; Return the symbol that describes the format of FLOAT.
100 (declaim (ftype (function (float) symbol) float-format-name))
101 (defun float-format-name (x)
103 (single-float 'single-float)
104 (double-float 'double-float)
105 #!+long-float (long-float 'long-float)))
107 ;;; This function is called when the type code wants to find out how
108 ;;; an array will actually be implemented. We set the
109 ;;; SPECIALIZED-ELEMENT-TYPE to correspond to the actual
110 ;;; specialization used in this implementation.
111 (declaim (ftype (function (array-type) array-type) specialize-array-type))
112 (defun specialize-array-type (type)
113 (let ((eltype (array-type-element-type type)))
114 (setf (array-type-specialized-element-type type)
115 (if (or (eq eltype *wild-type*)
116 ;; This is slightly dubious, but not as dubious as
117 ;; assuming that the upgraded-element-type should be
118 ;; equal to T, given the way that the AREF
119 ;; DERIVE-TYPE optimizer works. -- CSR, 2002-08-19
120 (unknown-type-p eltype))
122 (dolist (stype-name *specialized-array-element-types*
124 ;; FIXME: Mightn't it be better to have
125 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPES* be stored as precalculated
126 ;; SPECIFIER-TYPE results, instead of having to calculate
127 ;; them on the fly this way? (Call the new array
128 ;; *SPECIALIZED-ARRAY-ELEMENT-SPECIFIER-TYPES* or something..)
129 (let ((stype (specifier-type stype-name)))
130 (aver (not (unknown-type-p stype)))
131 (when (csubtypep eltype stype)
135 (defun sb!xc:upgraded-array-element-type (spec &optional environment)
137 "Return the element type that will actually be used to implement an array
138 with the specifier :ELEMENT-TYPE Spec."
139 (declare (ignore environment))
140 (if (unknown-type-p (specifier-type spec))
141 (error "undefined type: ~S" spec)
142 (type-specifier (array-type-specialized-element-type
143 (specifier-type `(array ,spec))))))
145 (defun sb!xc:upgraded-complex-part-type (spec &optional environment)
147 "Return the element type of the most specialized COMPLEX number type that
148 can hold parts of type SPEC."
149 (declare (ignore environment))
150 (let ((type (specifier-type spec)))
152 ((eq type *empty-type*) nil)
153 ((unknown-type-p type) (error "undefined type: ~S" spec))
155 (let ((ctype (specifier-type `(complex ,spec))))
157 ((eq ctype *empty-type*) '(eql 0))
158 ((csubtypep ctype (specifier-type '(complex single-float)))
160 ((csubtypep ctype (specifier-type '(complex double-float)))
163 ((csubtypep ctype (specifier-type '(complex long-float)))
165 ((csubtypep ctype (specifier-type '(complex rational)))
169 ;;; Return the most specific integer type that can be quickly checked that
170 ;;; includes the given type.
171 (defun containing-integer-type (subtype)
172 (dolist (type '(fixnum
176 (error "~S isn't an integer type?" subtype))
177 (when (csubtypep subtype (specifier-type type))
180 ;;; If TYPE has a CHECK-xxx template, but doesn't have a corresponding
181 ;;; PRIMITIVE-TYPE, then return the template's name. Otherwise, return NIL.
182 (defun hairy-type-check-template-name (type)
183 (declare (type ctype type))
186 (if (type= type (specifier-type 'cons))
190 (if (type= type (specifier-type 'symbol))
194 (cond ((type= type (specifier-type 'fixnum))
196 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
197 ((type= type (specifier-type '(signed-byte 32)))
198 'sb!c:check-signed-byte-32)
199 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
200 ((type= type (specifier-type '(unsigned-byte 32)))
201 'sb!c:check-unsigned-byte-32)
202 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
203 ((type= type (specifier-type '(signed-byte 64)))
204 'sb!c:check-signed-byte-64)
205 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
206 ((type= type (specifier-type '(unsigned-byte 64)))
207 'sb!c:check-unsigned-byte-64)