0.6.11.32:
[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 (/show0 "vm-type.lisp 17")
18
19 (!begin-collecting-cold-init-forms)
20 \f
21 ;;;; FIXME: I'm not sure where to put this. -- WHN 19990817
22
23 (deftype sb!vm:word () `(unsigned-byte ,sb!vm:word-bits))
24 \f
25 ;;;; implementation-dependent DEFTYPEs
26
27 ;;; Make DOUBLE-FLOAT a synonym for LONG-FLOAT, SINGLE-FLOAT for SHORT-FLOAT.
28 ;;; This is expanded before the translator gets a chance, so we will get
29 ;;; precedence.
30 #!-long-float
31 (setf (info :type :kind 'long-float) :defined)
32 #!-long-float
33 (sb!xc:deftype long-float (&optional low high)
34   `(double-float ,low ,high))
35 (setf (info :type :kind 'short-float) :defined)
36 (sb!xc:deftype short-float (&optional low high)
37   `(single-float ,low ,high))
38
39 ;;; an index into an integer
40 (sb!xc:deftype bit-index () `(integer 0 ,most-positive-fixnum))
41
42 ;;; worst-case values for float attributes
43 (sb!xc:deftype float-exponent ()
44   #!-long-float 'double-float-exponent
45   #!+long-float 'long-float-exponent)
46 (sb!xc:deftype float-digits ()
47   #!-long-float `(integer 0 ,sb!vm:double-float-digits)
48   #!+long-float `(integer 0 ,sb!vm:long-float-digits))
49 (sb!xc:deftype float-radix () '(integer 2 2))
50
51 ;;; a code for BOOLE
52 (sb!xc:deftype boole-code () '(unsigned-byte 4))
53
54 ;;; a byte specifier (as generated by BYTE)
55 (sb!xc:deftype byte-specifier () 'cons)
56
57 ;;; result of CHAR-INT
58 (sb!xc:deftype char-int () 'char-code)
59
60 ;;; PATHNAME pieces, as returned by the PATHNAME-xxx functions
61 (sb!xc:deftype pathname-host () '(or sb!impl::host null))
62 (sb!xc:deftype pathname-device ()
63   '(or simple-string (member nil :unspecific)))
64 (sb!xc:deftype pathname-directory () 'list)
65 (sb!xc:deftype pathname-name ()
66   '(or simple-string sb!impl::pattern (member nil :unspecific :wild)))
67 (sb!xc:deftype pathname-type ()
68   '(or simple-string sb!impl::pattern (member nil :unspecific :wild)))
69 (sb!xc:deftype pathname-version ()
70   '(or integer (member nil :newest :wild :unspecific)))
71
72 ;;; internal time format. (Note: not a FIXNUM, ouch..)
73 (sb!xc:deftype internal-time () 'unsigned-byte)
74
75 (sb!xc:deftype bignum-element-type () `(unsigned-byte ,sb!vm:word-bits))
76 (sb!xc:deftype bignum-type () 'bignum)
77 (sb!xc:deftype bignum-index () 'index)
78 \f
79 ;;;; hooks into the type system
80
81 ;;; the kinds of specialized array that actually exist in this implementation
82 (defvar *specialized-array-element-types*)
83 (!cold-init-forms
84   (setf *specialized-array-element-types*
85         '(bit
86           (unsigned-byte 2)
87           (unsigned-byte 4)
88           (unsigned-byte 8)
89           (unsigned-byte 16)
90           (unsigned-byte 32)
91           (signed-byte 8)
92           (signed-byte 16)
93           (signed-byte 30)
94           (signed-byte 32)
95           (complex single-float)
96           (complex double-float)
97           #!+long-float (complex long-float)
98           base-char
99           single-float
100           double-float
101           #!+long-float long-float)))
102
103 (sb!xc:deftype unboxed-array (&optional dims)
104   (collect ((types (list 'or)))
105     (dolist (type *specialized-array-element-types*)
106       (when (subtypep type '(or integer character float (complex float)))
107         (types `(array ,type ,dims))))
108     (types)))
109
110 (sb!xc:deftype simple-unboxed-array (&optional dims)
111   (collect ((types (list 'or)))
112     (dolist (type *specialized-array-element-types*)
113       (when (subtypep type '(or integer character float (complex float)))
114         (types `(simple-array ,type ,dims))))
115     (types)))
116
117 ;;; Return the symbol that describes the format of FLOAT.
118 (declaim (ftype (function (float) symbol) float-format-name))
119 (defun float-format-name (x)
120   (etypecase x
121     (single-float 'single-float)
122     (double-float 'double-float)
123     #!+long-float (long-float 'long-float)))
124
125 ;;; This function is called when the type code wants to find out how
126 ;;; an array will actually be implemented. We set the
127 ;;; SPECIALIZED-ELEMENT-TYPE to correspond to the actual
128 ;;; specialization used in this implementation.
129 (declaim (ftype (function (array-type) array-type) specialize-array-type))
130 (defun specialize-array-type (type)
131   (let ((eltype (array-type-element-type type)))
132     (setf (array-type-specialized-element-type type)
133           (if (eq eltype *wild-type*)
134               *wild-type*
135               (dolist (stype-name *specialized-array-element-types*
136                                   *universal-type*)
137                 ;; FIXME: Mightn't it be better to have
138                 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPES* be stored as precalculated
139                 ;; SPECIFIER-TYPE results, instead of having to calculate
140                 ;; them on the fly this way? (Call the new array
141                 ;; *SPECIALIZED-ARRAY-ELEMENT-SPECIFIER-TYPES* or something..)
142                 (let ((stype (specifier-type stype-name)))
143                   (when (csubtypep eltype stype)
144                     (return stype))))))
145     type))
146
147 ;;; Return the most specific integer type that can be quickly checked that
148 ;;; includes the given type.
149 (defun containing-integer-type (subtype)
150   (dolist (type '(fixnum
151                   (signed-byte 32)
152                   (unsigned-byte 32)
153                   integer)
154                 (error "~S isn't an integer type?" subtype))
155     (when (csubtypep subtype (specifier-type type))
156       (return type))))
157
158 ;;; If TYPE has a CHECK-xxx template, but doesn't have a corresponding
159 ;;; PRIMITIVE-TYPE, then return the template's name. Otherwise, return NIL.
160 (defun hairy-type-check-template-name (type)
161   (declare (type ctype type))
162   (typecase type
163     (cons-type
164      (if (type= type (specifier-type 'cons))
165          'sb!c:check-cons
166          nil))
167     (built-in-class
168      (if (type= type (specifier-type 'symbol))
169          'sb!c:check-symbol
170          nil))
171     (numeric-type
172      (cond ((type= type (specifier-type 'fixnum))
173             'sb!c:check-fixnum)
174            ((type= type (specifier-type '(signed-byte 32)))
175             'sb!c:check-signed-byte-32)
176            ((type= type (specifier-type '(unsigned-byte 32)))
177             'sb!c:check-unsigned-byte-32)
178            (t nil)))
179     (function-type
180      'sb!c:check-function)
181     (t
182      nil)))
183 \f
184 (!defun-from-collected-cold-init-forms !vm-type-cold-init)
185
186 (/show0 "vm-type.lisp end of file")