Optimize the compiler a bit.
[sbcl.git] / src / code / typedefs.lisp
1 ;;;; This file contains the definition of the CTYPE (Compiler TYPE)
2 ;;;; structure and related macros used for manipulating it. This is
3 ;;;; sort of a mini object system with rather odd dispatching rules.
4 ;;;; Other compile-time definitions needed by multiple files are also
5 ;;;; here.
6 ;;;;
7 ;;;; FIXME: The comment above about what's in this file is no longer so
8 ;;;; true now that I've split off type-class.lisp. Perhaps we should
9 ;;;; split off CTYPE into the same file as type-class.lisp, rename that
10 ;;;; file to ctype.lisp, move the current comment to the head of that file,
11 ;;;; and write a new comment for this file saying how this file holds
12 ;;;; concrete types.
13
14 ;;;; This software is part of the SBCL system. See the README file for
15 ;;;; more information.
16 ;;;;
17 ;;;; This software is derived from the CMU CL system, which was
18 ;;;; written at Carnegie Mellon University and released into the
19 ;;;; public domain. The software is in the public domain and is
20 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
21 ;;;; files for more information.
22
23 (in-package "SB!KERNEL")
24
25 (!begin-collecting-cold-init-forms)
26
27 ;;; Define the translation from a type-specifier to a type structure for
28 ;;; some particular type. Syntax is identical to DEFTYPE.
29 (defmacro !def-type-translator (name arglist &body body)
30   (declare (type symbol name))
31   ;; FIXME: Now that the T%CL hack is ancient history and we just use CL
32   ;; instead, we can probably return to using PARSE-DEFMACRO here.
33   ;;
34   ;; was:
35   ;;   This song and dance more or less emulates PARSE-DEFMACRO. The reason for
36   ;;   doing this emulation instead of just calling PARSE-DEFMACRO is just that
37   ;;   at cross-compile time PARSE-DEFMACRO expects lambda-list keywords in the
38   ;;   T%CL package, which is not what we have here. Maybe there's a tidier
39   ;;   solution.. (Other than wishing that ANSI had used symbols in the KEYWORD
40   ;;   package as lambda list keywords, rather than using symbols in the LISP
41   ;;   package!)
42   (multiple-value-bind (whole wholeless-arglist)
43       (if (eq '&whole (car arglist))
44           (values (cadr arglist) (cddr arglist))
45           (values (sb!xc:gensym) arglist))
46     (multiple-value-bind (forms decls)
47         (parse-body body :doc-string-allowed nil)
48       `(progn
49          (!cold-init-forms
50           (let ((fun (lambda (,whole)
51                        (block ,name
52                          (destructuring-bind ,wholeless-arglist
53                              (rest ,whole)  ; discarding NAME
54                            ,@decls
55                        ,@forms)))))
56             #-sb-xc-host
57             (setf (%simple-fun-arglist (the simple-fun fun)) ',wholeless-arglist)
58             (setf (info :type :translator ',name) fun)))
59          ',name))))
60
61 ;;; DEFVARs for these come later, after we have enough stuff defined.
62 (declaim (special *wild-type* *universal-type* *empty-type*))
63 \f
64 (defvar *type-random-state*)
65
66 ;;; the base class for the internal representation of types
67 (def!struct (ctype (:conc-name type-)
68                    (:constructor nil)
69                    (:make-load-form-fun make-type-load-form)
70                    #-sb-xc-host (:pure t))
71   ;; the class of this type
72   ;;
73   ;; FIXME: It's unnecessarily confusing to have a structure accessor
74   ;; named TYPE-CLASS-INFO which is an accessor for the CTYPE structure
75   ;; even though the TYPE-CLASS structure also exists in the system.
76   ;; Rename this slot: TYPE-CLASS or ASSOCIATED-TYPE-CLASS or something.
77   (class-info (missing-arg) :type type-class)
78   ;; True if this type has a fixed number of members, and as such
79   ;; could possibly be completely specified in a MEMBER type. This is
80   ;; used by the MEMBER type methods.
81   (enumerable nil :read-only t)
82   ;; an arbitrary hash code used in EQ-style hashing of identity
83   ;; (since EQ hashing can't be done portably)
84   (hash-value (random #.(ash 1 15)
85                       (if (boundp '*type-random-state*)
86                           *type-random-state*
87                           (setf *type-random-state*
88                                 (make-random-state))))
89               :type (and fixnum unsigned-byte)
90               :read-only t)
91   ;; Can this object contain other types? A global property of our
92   ;; implementation (which unfortunately seems impossible to enforce
93   ;; with assertions or other in-the-code checks and constraints) is
94   ;; that subclasses which don't contain other types correspond to
95   ;; disjoint subsets (except of course for the NAMED-TYPE T, which
96   ;; covers everything). So NUMBER-TYPE is disjoint from CONS-TYPE is
97   ;; is disjoint from MEMBER-TYPE and so forth. But types which can
98   ;; contain other types, like HAIRY-TYPE and INTERSECTION-TYPE, can
99   ;; violate this rule.
100   (might-contain-other-types-p nil :read-only t))
101 (def!method print-object ((ctype ctype) stream)
102   (print-unreadable-object (ctype stream :type t)
103     (prin1 (type-specifier ctype) stream)))
104
105 ;;; Just dump it as a specifier. (We'll convert it back upon loading.)
106 (defun make-type-load-form (type)
107   (declare (type ctype type))
108   `(specifier-type ',(type-specifier type)))
109 \f
110 ;;;; miscellany
111
112 ;;; Look for nice relationships for types that have nice relationships
113 ;;; only when one is a hierarchical subtype of the other.
114 (defun hierarchical-intersection2 (type1 type2)
115   (multiple-value-bind (subtypep1 win1) (csubtypep type1 type2)
116     (multiple-value-bind (subtypep2 win2) (csubtypep type2 type1)
117       (cond (subtypep1 type1)
118             (subtypep2 type2)
119             ((and win1 win2) *empty-type*)
120             (t nil)))))
121 (defun hierarchical-union2 (type1 type2)
122   (cond ((csubtypep type1 type2) type2)
123         ((csubtypep type2 type1) type1)
124         (t nil)))
125
126 ;;; Hash two things (types) down to 8 bits. In CMU CL this was an EQ
127 ;;; hash, but since it now needs to run in vanilla ANSI Common Lisp at
128 ;;; cross-compile time, it's now based on the CTYPE-HASH-VALUE field
129 ;;; instead.
130 ;;;
131 ;;; FIXME: This was a macro in CMU CL, and is now an INLINE function. Is
132 ;;; it important for it to be INLINE, or could be become an ordinary
133 ;;; function without significant loss? -- WHN 19990413
134 #!-sb-fluid (declaim (inline type-cache-hash))
135 (declaim (ftype (function (ctype ctype) (unsigned-byte 8)) type-cache-hash))
136 (defun type-cache-hash (type1 type2)
137   (logand (logxor (ash (type-hash-value type1) -3)
138                   (type-hash-value type2))
139           #xFF))
140 #!-sb-fluid (declaim (inline type-list-cache-hash))
141 (declaim (ftype (function (list) (unsigned-byte 8)) type-list-cache-hash))
142 (defun type-list-cache-hash (types)
143   (logand #xFF
144           (loop with res fixnum = 0
145                 for type in types
146                 for hash = (type-hash-value type)
147                 do (setq res (logxor res hash))
148                 finally (return res))))
149 \f
150 ;;;; cold loading initializations
151
152 (!defun-from-collected-cold-init-forms !typedefs-cold-init)