Additional niceties and middle end support for short vector SIMD packs
[sbcl.git] / src / code / target-type.lisp
1 ;;;; type-related stuff which exists only in the target SBCL runtime
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!KERNEL")
13
14 (!begin-collecting-cold-init-forms)
15 \f
16 ;;; If TYPE is a type that we can do a compile-time test on, then
17 ;;; return whether the object is of that type as the first value and
18 ;;; second value true. Otherwise return NIL, NIL.
19 ;;;
20 ;;; We give up on unknown types and pick off FUNCTION- and COMPOUND-
21 ;;; types. For STRUCTURE- types, we require that the type be defined
22 ;;; in both the current and compiler environments, and that the
23 ;;; INCLUDES be the same.
24 ;;;
25 ;;; KLUDGE: This should probably be a type method instead of a big
26 ;;; ETYPECASE. But then the type method system should probably be CLOS
27 ;;; too, and until that happens wedging more stuff into it might be
28 ;;; messy. So I've left it a big ETYPECASE. -- 2001-03-16
29 (defun ctypep (obj type)
30   (declare (type ctype type))
31   (etypecase type
32     ((or numeric-type
33          named-type
34          member-type
35          array-type
36          character-set-type
37          built-in-classoid
38          cons-type
39          #!+sb-simd-pack simd-pack-type)
40      (values (%typep obj type) t))
41     (classoid
42      (if (if (csubtypep type (specifier-type 'function))
43              (funcallable-instance-p obj)
44              (%instancep obj))
45          (if (eq (classoid-layout type)
46                  (info :type :compiler-layout (classoid-name type)))
47              (values (sb!xc:typep obj type) t)
48              (values nil nil))
49          (values nil t)))
50     (compound-type
51      (funcall (etypecase type
52                 (intersection-type #'every/type)
53                 (union-type #'any/type))
54               #'ctypep
55               obj
56               (compound-type-types type)))
57     (fun-type
58      (values (functionp obj) t))
59     (unknown-type
60      (values nil nil))
61     (alien-type-type
62      (values (alien-typep obj (alien-type-type-alien-type type)) t))
63     (negation-type
64      (multiple-value-bind (res win)
65          (ctypep obj (negation-type-type type))
66        (if win
67            (values (not res) t)
68            (values nil nil))))
69     (hairy-type
70      ;; Now the tricky stuff.
71      (let* ((hairy-spec (hairy-type-specifier type))
72             (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
73        (ecase symbol
74          (and
75           (if (atom hairy-spec)
76               (values t t)
77               (dolist (spec (cdr hairy-spec) (values t t))
78                 (multiple-value-bind (res win)
79                     (ctypep obj (specifier-type spec))
80                   (unless win (return (values nil nil)))
81                   (unless res (return (values nil t)))))))
82          (not
83           (multiple-value-bind (res win)
84               (ctypep obj (specifier-type (cadr hairy-spec)))
85             (if win
86                 (values (not res) t)
87                 (values nil nil))))
88          (satisfies
89           ;; If the SATISFIES function is not foldable, we cannot answer!
90           (let* ((form `(,(second hairy-spec) ',obj)))
91             (multiple-value-bind (ok result)
92                 (sb!c::constant-function-call-p form nil nil)
93               (values (not (null result)) ok)))))))))
94 \f
95 ;;; Return the layout for an object. This is the basic operation for
96 ;;; finding out the "type" of an object, and is used for generic
97 ;;; function dispatch. The standard doesn't seem to say as much as it
98 ;;; should about what this returns for built-in objects. For example,
99 ;;; it seems that we must return NULL rather than LIST when X is NIL
100 ;;; so that GF's can specialize on NULL.
101 #!-sb-fluid (declaim (inline layout-of))
102 (defun layout-of (x)
103   (declare (optimize (speed 3) (safety 0)))
104   (cond ((%instancep x) (%instance-layout x))
105         ((funcallable-instance-p x) (%funcallable-instance-layout x))
106         ((null x)
107          ;; Note: was #.((CLASS-LAYOUT (SB!XC:FIND-CLASS 'NULL))).
108          ;; I (WHN 19990209) replaced this with an expression evaluated at
109          ;; run time in order to make it easier to build the cross-compiler.
110          ;;
111          ;; KLUDGE: Since there's a DEFTRANSFORM for FIND-CLASSOID on
112          ;; constant names which creates non-cold-loadable code, we
113          ;; can't just use (CLASSOID-LAYOUT (FIND-CLASSOID 'NULL))
114          ;; here. The original (WHN 19991004) solution was to locally
115          ;; notinline FIND-CLASSOID. However, the full call to
116          ;; FIND-CLASSOID caused suboptimal register allocation in PCL
117          ;; dfuns. So instead we now use a special variable which is
118          ;; initialized during cold init. -- JES, 2006-07-04
119          *null-classoid-layout*)
120         (t (svref *built-in-class-codes* (widetag-of x)))))
121
122 #!-sb-fluid (declaim (inline classoid-of))
123 (defun classoid-of (object)
124   #!+sb-doc
125   "Return the class of the supplied object, which may be any Lisp object, not
126    just a CLOS STANDARD-OBJECT."
127   (layout-classoid (layout-of object)))
128 \f
129 ;;;; miscellaneous interfaces
130
131 ;;; Clear memoization of all type system operations that can be
132 ;;; altered by type definition/redefinition.
133 ;;;
134 (defun clear-type-caches ()
135   ;; FIXME: We would like to differentiate between different cache
136   ;; kinds, but at the moment all our caches pretty much are type
137   ;; caches.
138   (drop-all-hash-caches)
139   (values))
140
141 ;;; This is like TYPE-OF, only we return a CTYPE structure instead of
142 ;;; a type specifier, and we try to return the type most useful for
143 ;;; type checking, rather than trying to come up with the one that the
144 ;;; user might find most informative.
145 (declaim (ftype (function (t) ctype) ctype-of))
146 (defun-cached (ctype-of
147                :hash-function (lambda (x) (logand (sxhash x) #x1FF))
148                :hash-bits 9
149                :init-wrapper !cold-init-forms)
150               ((x eq))
151   (typecase x
152     (function
153      (if (funcallable-instance-p x)
154          (classoid-of x)
155          (specifier-type (sb!impl::%fun-type x))))
156     (symbol
157      (make-member-type :members (list x)))
158     (number
159      (ctype-of-number x))
160     (array
161      (let ((etype (specifier-type (array-element-type x))))
162        (make-array-type :dimensions (array-dimensions x)
163                         :complexp (not (typep x 'simple-array))
164                         :element-type etype
165                         :specialized-element-type etype)))
166     (cons
167      (make-cons-type *universal-type* *universal-type*))
168     (character
169      (specifier-type 'character))
170     #!+sb-simd-pack
171     (simd-pack
172      (let ((type (nth (%simd-pack-tag x) *simd-pack-element-types*)))
173        (if type
174            (specifier-type `(simd-pack ,type))
175            (specifier-type 'simd-pack))))
176     (t
177      (classoid-of x))))
178 \f
179 (!defun-from-collected-cold-init-forms !target-type-cold-init)