Fix make-array transforms.
[sbcl.git] / src / compiler / generic / vm-type.lisp
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
4 ;;;; implementation.
5
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
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.
14
15 (in-package "SB!KERNEL")
16
17 ;;;; FIXME: I'm not sure where to put this. -- WHN 19990817
18
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))
21
22 \f
23 ;;;; implementation-dependent DEFTYPEs
24
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.
28 #!-long-float
29 (setf (info :type :kind 'long-float) :defined)
30 #!-long-float
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))
36
37 ;;; an index into an integer
38 (sb!xc:deftype bit-index () `(integer 0 ,sb!xc:most-positive-fixnum))
39
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)
51
52 ;;; a code for BOOLE
53 (sb!xc:deftype boole-code () '(unsigned-byte 4))
54
55 ;;; a byte specifier (as generated by BYTE)
56 (sb!xc:deftype byte-specifier () 'cons)
57
58 ;;; result of CHAR-INT
59 (sb!xc:deftype char-int () 'char-code)
60
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 :unc)))
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)))
72
73 ;;; internal time format. (Note: not a FIXNUM, ouch..)
74 (sb!xc:deftype internal-time () 'unsigned-byte)
75
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 (- sb!vm:n-word-bits sb!vm:n-widetag-bits)))))
82 \f
83 ;;;; hooks into the type system
84
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))))
90     (types)))
91
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))))
97     (types)))
98
99 (sb!xc:deftype complex-vector (&optional element-type length)
100   `(and (vector ,element-type ,length) (not simple-array)))
101
102 ;;; Return the symbol that describes the format of FLOAT.
103 (declaim (ftype (function (float) symbol) float-format-name))
104 (defun float-format-name (x)
105   (etypecase x
106     (single-float 'single-float)
107     (double-float 'double-float)
108     #!+long-float (long-float 'long-float)))
109
110 ;;; This function is called when the type code wants to find out how
111 ;;; an array will actually be implemented. We set the
112 ;;; SPECIALIZED-ELEMENT-TYPE to correspond to the actual
113 ;;; specialization used in this implementation.
114 (declaim (ftype (function (array-type) array-type) specialize-array-type))
115 (defun specialize-array-type (type)
116   (let ((eltype (array-type-element-type type)))
117     (setf (array-type-specialized-element-type type)
118           (if (or (eq eltype *wild-type*)
119                   ;; This is slightly dubious, but not as dubious as
120                   ;; assuming that the upgraded-element-type should be
121                   ;; equal to T, given the way that the AREF
122                   ;; DERIVE-TYPE optimizer works.  -- CSR, 2002-08-19
123                   (contains-unknown-type-p eltype))
124               *wild-type*
125               (dolist (stype-name *specialized-array-element-types*
126                                   *universal-type*)
127                 ;; FIXME: Mightn't it be better to have
128                 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPES* be stored as precalculated
129                 ;; SPECIFIER-TYPE results, instead of having to calculate
130                 ;; them on the fly this way? (Call the new array
131                 ;; *SPECIALIZED-ARRAY-ELEMENT-SPECIFIER-TYPES* or something..)
132                 (let ((stype (specifier-type stype-name)))
133                   (aver (not (unknown-type-p stype)))
134                   (when (csubtypep eltype stype)
135                     (return stype))))))
136     type))
137
138 (defun sb!xc:upgraded-array-element-type (spec &optional environment)
139   #!+sb-doc
140   "Return the element type that will actually be used to implement an array
141    with the specifier :ELEMENT-TYPE Spec."
142   (declare (ignore environment))
143   (handler-case
144       ;; Can't rely on SPECIFIER-TYPE to signal PARSE-UNKNOWN-TYPE in
145       ;; the case of (AND KNOWN UNKNOWN), since the result of the
146       ;; outter call to SPECIFIER-TYPE can be cached by the code that
147       ;; doesn't catch PARSE-UNKNOWN-TYPE signal.
148       (if (contains-unknown-type-p (specifier-type spec))
149           (error "Undefined type: ~S" spec)
150           (type-specifier (array-type-specialized-element-type
151                            (specifier-type `(array ,spec)))))
152     (parse-unknown-type (c)
153       (error "Undefined type: ~S" (parse-unknown-type-specifier c)))))
154
155 (defun sb!xc:upgraded-complex-part-type (spec &optional environment)
156   #!+sb-doc
157   "Return the element type of the most specialized COMPLEX number type that
158    can hold parts of type SPEC."
159   (declare (ignore environment))
160   (let ((type (specifier-type spec)))
161     (cond
162       ((eq type *empty-type*) nil)
163       ((unknown-type-p type) (error "undefined type: ~S" spec))
164       (t
165        (let ((ctype (specifier-type `(complex ,spec))))
166          (cond
167            ((eq ctype *empty-type*) '(eql 0))
168            ((csubtypep ctype (specifier-type '(complex single-float)))
169             'single-float)
170            ((csubtypep ctype (specifier-type '(complex double-float)))
171             'double-float)
172            #!+long-float
173            ((csubtypep ctype (specifier-type '(complex long-float)))
174             'long-float)
175            ((csubtypep ctype (specifier-type '(complex rational)))
176             'rational)
177            (t 'real)))))))
178
179 ;;; Return the most specific integer type that can be quickly checked that
180 ;;; includes the given type.
181 (defun containing-integer-type (subtype)
182   (dolist (type `(fixnum
183                   (signed-byte ,sb!vm:n-word-bits)
184                   (unsigned-byte ,sb!vm:n-word-bits)
185                   integer)
186                 (error "~S isn't an integer type?" subtype))
187     (when (csubtypep subtype (specifier-type type))
188       (return type))))
189
190 ;;; If TYPE has a CHECK-xxx template, but doesn't have a corresponding
191 ;;; PRIMITIVE-TYPE, then return the template's name. Otherwise, return NIL.
192 (defun hairy-type-check-template-name (type)
193   (declare (type ctype type))
194   (typecase type
195     (cons-type
196      (if (type= type (specifier-type 'cons))
197          'sb!c:check-cons
198          nil))
199     (built-in-classoid
200      (if (type= type (specifier-type 'symbol))
201          'sb!c:check-symbol
202          nil))
203     (numeric-type
204      (cond ((type= type (specifier-type 'fixnum))
205             'sb!c:check-fixnum)
206            #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
207            ((type= type (specifier-type '(signed-byte 32)))
208             'sb!c:check-signed-byte-32)
209            #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
210            ((type= type (specifier-type '(unsigned-byte 32)))
211             'sb!c:check-unsigned-byte-32)
212            #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
213            ((type= type (specifier-type '(signed-byte 64)))
214             'sb!c:check-signed-byte-64)
215            #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
216            ((type= type (specifier-type '(unsigned-byte 64)))
217             'sb!c:check-unsigned-byte-64)
218            (t nil)))
219     (fun-type
220      'sb!c:check-fun)
221     (t
222      nil)))