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