0.7.6.29:
[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:n-word-bits))
24 \f
25 ;;;; implementation-dependent DEFTYPEs
26
27 ;;; Make DOUBLE-FLOAT a synonym for LONG-FLOAT, SINGLE-FLOAT for
28 ;;; SHORT-FLOAT. This is expanded before the translator gets a chance,
29 ;;; so we will get 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:n-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 (or (eq eltype *wild-type*)
134                   ;; This is slightly dubious, but not as dubious as
135                   ;; assuming that the upgraded-element-type should be
136                   ;; equal to T, given the way that the AREF
137                   ;; DERIVE-TYPE optimizer works.  -- CSR, 2002-08-19
138                   (unknown-type-p eltype))
139               *wild-type*
140               (dolist (stype-name *specialized-array-element-types*
141                                   *universal-type*)
142                 ;; FIXME: Mightn't it be better to have
143                 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPES* be stored as precalculated
144                 ;; SPECIFIER-TYPE results, instead of having to calculate
145                 ;; them on the fly this way? (Call the new array
146                 ;; *SPECIALIZED-ARRAY-ELEMENT-SPECIFIER-TYPES* or something..)
147                 (let ((stype (specifier-type stype-name)))
148                   (when (csubtypep eltype stype)
149                     (return stype))))))
150     type))
151
152 ;;; Return the most specific integer type that can be quickly checked that
153 ;;; includes the given type.
154 (defun containing-integer-type (subtype)
155   (dolist (type '(fixnum
156                   (signed-byte 32)
157                   (unsigned-byte 32)
158                   integer)
159                 (error "~S isn't an integer type?" subtype))
160     (when (csubtypep subtype (specifier-type type))
161       (return type))))
162
163 ;;; If TYPE has a CHECK-xxx template, but doesn't have a corresponding
164 ;;; PRIMITIVE-TYPE, then return the template's name. Otherwise, return NIL.
165 (defun hairy-type-check-template-name (type)
166   (declare (type ctype type))
167   (typecase type
168     (cons-type
169      (if (type= type (specifier-type 'cons))
170          'sb!c:check-cons
171          nil))
172     (built-in-class
173      (if (type= type (specifier-type 'symbol))
174          'sb!c:check-symbol
175          nil))
176     (numeric-type
177      (cond ((type= type (specifier-type 'fixnum))
178             'sb!c:check-fixnum)
179            ((type= type (specifier-type '(signed-byte 32)))
180             'sb!c:check-signed-byte-32)
181            ((type= type (specifier-type '(unsigned-byte 32)))
182             'sb!c:check-unsigned-byte-32)
183            (t nil)))
184     (fun-type
185      'sb!c:check-fun)
186     (t
187      nil)))
188 \f
189 (!defun-from-collected-cold-init-forms !vm-type-cold-init)
190
191 (/show0 "vm-type.lisp end of file")