0.pre7.88:
[sbcl.git] / src / compiler / globaldb.lisp
1 ;;;; This file provides a functional interface to global information
2 ;;;; about named things in the system. Information is considered to be
3 ;;;; global if it must persist between invocations of the compiler. The
4 ;;;; use of a functional interface eliminates the need for the compiler
5 ;;;; to worry about the actual representation. This is important, since
6 ;;;; the information may well have several representations.
7 ;;;;
8 ;;;; The database contains arbitrary Lisp values, addressed by a
9 ;;;; combination of Name, Class and Type. The Name is a EQUAL-thing
10 ;;;; which is the name of the thing we are recording information
11 ;;;; about. Class is the kind of object involved. Typical classes are
12 ;;;; :FUNCTION, :VARIABLE, :TYPE, ... A Type names a particular piece
13 ;;;; of information within a given class. Class and Type are keywords,
14 ;;;; and are compared with EQ.
15
16 ;;;; This software is part of the SBCL system. See the README file for
17 ;;;; more information.
18 ;;;;
19 ;;;; This software is derived from the CMU CL system, which was
20 ;;;; written at Carnegie Mellon University and released into the
21 ;;;; public domain. The software is in the public domain and is
22 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
23 ;;;; files for more information.
24
25 (in-package "SB!C")
26
27 (!begin-collecting-cold-init-forms)
28 #!+sb-show (!cold-init-forms (/show0 "early in globaldb.lisp cold init"))
29
30 ;;; The DEFVAR for this appears later.
31 ;;; FIXME: centralize
32 (declaim (special *universal-type*))
33
34 ;;; This is sorta semantically equivalent to SXHASH, but optimized for
35 ;;; legal function names. Note: semantically equivalent does *not*
36 ;;; mean that it always returns the same value as SXHASH, just that it
37 ;;; satisfies the formal definition of SXHASH. The ``sorta'' is
38 ;;; because SYMBOL-HASH will not necessarily return the same value in
39 ;;; different lisp images.
40 ;;;
41 ;;; Why optimize? We want to avoid the fully-general TYPECASE in ordinary
42 ;;; SXHASH, because
43 ;;;   1. This hash function has to run when we're initializing the globaldb,
44 ;;;      so it has to run before the type system is initialized, and it's
45 ;;;      easier to make it do this if we don't try to do a general TYPECASE.
46 ;;;   2. This function is in a potential bottleneck for the compiler,
47 ;;;      and avoiding the general TYPECASE lets us improve performance
48 ;;;      because
49 ;;;     2a. the general TYPECASE is intrinsically slow, and
50 ;;;     2b. the general TYPECASE is too big for us to easily afford
51 ;;;         to inline it, so it brings with it a full function call.
52 ;;;
53 ;;; Why not specialize instead of optimize? (I.e. why fall through to
54 ;;; general SXHASH as a last resort?) Because the INFO database is used
55 ;;; to hold all manner of things, e.g. (INFO :TYPE :BUILTIN ..)
56 ;;; which is called on values like (UNSIGNED-BYTE 29). Falling through
57 ;;; to SXHASH lets us support all manner of things (as long as they
58 ;;; aren't used too early in cold boot for SXHASH to run).
59 #!-sb-fluid (declaim (inline globaldb-sxhashoid))
60 (defun globaldb-sxhashoid (x)
61   (cond #-sb-xc-host ; (SYMBOL-HASH doesn't exist on cross-compilation host.)
62         ((symbolp x)
63          (symbol-hash x))
64         #-sb-xc-host ; (SYMBOL-HASH doesn't exist on cross-compilation host.)
65         ((and (listp x)
66               (eq (first x) 'setf)
67               (let ((rest (rest x)))
68                 (and (symbolp (car rest))
69                      (null (cdr rest)))))
70          (logxor (symbol-hash (second x))
71                  110680597))
72         (t (sxhash x))))
73
74 ;;; Given any non-negative integer, return a prime number >= to it.
75 ;;;
76 ;;; FIXME: This logic should be shared with ALMOST-PRIMIFY in
77 ;;; hash-table.lisp. Perhaps the merged logic should be
78 ;;; PRIMIFY-HASH-TABLE-SIZE, implemented as a lookup table of primes
79 ;;; after integral powers of two:
80 ;;;    #(17 37 67 131 ..)
81 ;;; (Or, if that's too coarse, after half-integral powers of two.) By
82 ;;; thus getting rid of any need for primality testing at runtime, we
83 ;;; could punt POSITIVE-PRIMEP, too.
84 (defun primify (x)
85   (declare (type unsigned-byte x))
86   (do ((n (logior x 1) (+ n 2)))
87       ((positive-primep n) n)))
88 \f
89 ;;;; info classes, info types, and type numbers, part I: what's needed
90 ;;;; not only at compile time but also at run time
91
92 ;;;; Note: This section is a blast from the past, a little trip down
93 ;;;; memory lane to revisit the weird host/target interactions of the
94 ;;;; CMU CL build process. Because of the way that the cross-compiler
95 ;;;; and target compiler share stuff here, if you change anything in
96 ;;;; here, you'd be well-advised to nuke all your fasl files and
97 ;;;; restart compilation from the very beginning of the bootstrap
98 ;;;; process.
99
100 ;;; At run time, we represent the type of info that we want by a small
101 ;;; non-negative integer.
102 (defconstant type-number-bits 6)
103 (deftype type-number () `(unsigned-byte ,type-number-bits))
104
105 ;;; Why do we suppress the :COMPILE-TOPLEVEL situation here when we're
106 ;;; running the cross-compiler? The cross-compiler (which was built
107 ;;; from these sources) has its version of these data and functions
108 ;;; defined in the same places we'd be defining into. We're happy with
109 ;;; its version, since it was compiled from the same sources, so
110 ;;; there's no point in overwriting its nice compiled version of this
111 ;;; stuff with our interpreted version. (And any time we're *not*
112 ;;; happy with its version, perhaps because we've been editing the
113 ;;; sources partway through bootstrapping, tch tch, overwriting its
114 ;;; version with our version would be unlikely to help, because that
115 ;;; would make the cross-compiler very confused.)
116 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
117
118 (defstruct (class-info
119             (:constructor make-class-info (name))
120             #-no-ansi-print-object
121             (:print-object (lambda (x s)
122                              (print-unreadable-object (x s :type t)
123                                (prin1 (class-info-name x)))))
124             (:copier nil))
125   ;; name of this class
126   (name nil :type keyword :read-only t)
127   ;; list of Type-Info structures for each type in this class
128   (types () :type list))
129
130 ;;; a map from type numbers to TYPE-INFO objects. There is one type
131 ;;; number for each defined CLASS/TYPE pair.
132 ;;;
133 ;;; We build its value at build-the-cross-compiler time (with calls to
134 ;;; DEFINE-INFO-TYPE), then generate code to recreate the compile time
135 ;;; value, and arrange for that code to be called in cold load.
136 ;;; KLUDGE: We don't try to reset its value when cross-compiling the
137 ;;; compiler, since that creates too many bootstrapping problems,
138 ;;; instead just reusing the built-in-the-cross-compiler version,
139 ;;; which is theoretically a little bit ugly but pretty safe in
140 ;;; practice because the cross-compiler is as close to the target
141 ;;; compiler as we can make it, i.e. identical in most ways, including
142 ;;; this one. -- WHN 2001-08-19
143 (defvar *info-types*)
144 (declaim (type simple-vector *info-types*))
145 #-sb-xc ; as per KLUDGE note above
146 (eval-when (:compile-toplevel :execute)
147   (setf *info-types*
148         (make-array (ash 1 type-number-bits) :initial-element nil)))
149
150 (defstruct (type-info
151             #-no-ansi-print-object
152             (:print-object (lambda (x s)
153                              (print-unreadable-object (x s)
154                                (format s
155                                        "~S ~S, Number = ~W"
156                                        (class-info-name (type-info-class x))
157                                        (type-info-name x)
158                                        (type-info-number x)))))
159             (:copier nil))
160   ;; the name of this type
161   (name (missing-arg) :type keyword)
162   ;; this type's class
163   (class (missing-arg) :type class-info)
164   ;; a number that uniquely identifies this type (and implicitly its class)
165   (number (missing-arg) :type type-number)
166   ;; a type specifier which info of this type must satisfy
167   (type nil :type t)
168   ;; a function called when there is no information of this type
169   (default (lambda () (error "type not defined yet")) :type function))
170
171 ;;; a map from class names to CLASS-INFO structures
172 ;;;
173 ;;; We build the value for this at compile time (with calls to
174 ;;; DEFINE-INFO-CLASS), then generate code to recreate the compile time
175 ;;; value, and arrange for that code to be called in cold load.
176 ;;; KLUDGE: Just as for *INFO-TYPES*, we don't try to rebuild this
177 ;;; when cross-compiling, but instead just reuse the cross-compiler's
178 ;;; version for the target compiler. -- WHN 2001-08-19
179 (defvar *info-classes*)
180 (declaim (hash-table *info-classes*))
181 #-sb-xc ; as per KLUDGE note above
182 (eval-when (:compile-toplevel :execute)
183   (setf *info-classes* (make-hash-table)))
184
185 ;;; If NAME is the name of a type in CLASS, then return the TYPE-INFO,
186 ;;; otherwise NIL.
187 (defun find-type-info (name class)
188   (declare (type keyword name) (type class-info class))
189   (dolist (type (class-info-types class) nil)
190     (when (eq (type-info-name type) name)
191       (return type))))
192
193 ;;; Return the info structure for an info class or type, or die trying.
194 (declaim (ftype (function (keyword) class-info) class-info-or-lose))
195 (defun class-info-or-lose (class)
196   (declare (type keyword class))
197   #+sb-xc (/noshow0 "entering CLASS-INFO-OR-LOSE, CLASS=..")
198   #+sb-xc (/nohexstr class)
199   (prog1
200       (or (gethash class *info-classes*)
201           (error "~S is not a defined info class." class))
202     #+sb-xc (/noshow0 "returning from CLASS-INFO-OR-LOSE")))
203 (declaim (ftype (function (keyword keyword) type-info) type-info-or-lose))
204 (defun type-info-or-lose (class type)
205   #+sb-xc (/noshow0 "entering TYPE-INFO-OR-LOSE, CLASS,TYPE=..")
206   #+sb-xc (/nohexstr class)
207   #+sb-xc (/nohexstr type)
208   (prog1
209       (or (find-type-info type (class-info-or-lose class))
210           (error "~S is not a defined info type." type))
211     #+sb-xc (/noshow0 "returning from TYPE-INFO-OR-LOSE")))
212
213 ) ; EVAL-WHEN
214 \f
215 ;;;; info classes, info types, and type numbers, part II: what's
216 ;;;; needed only at compile time, not at run time
217
218 ;;; FIXME: Perhaps this stuff (the definition of DEFINE-INFO-CLASS
219 ;;; and the calls to it) could/should go in a separate file,
220 ;;; perhaps info-classes.lisp?
221
222 (eval-when (:compile-toplevel :execute)
223
224 ;;; Set up the data structures to support an info class.
225 ;;;
226 ;;; comment from CMU CL:
227 ;;;   We make sure that the class exists at compile time so that
228 ;;;   macros can use it, but we don't actually store the init function
229 ;;;   until load time so that we don't break the running compiler.
230 ;;; KLUDGE: I don't think that's the way it is any more, but I haven't
231 ;;; looked into it enough to write a better comment. -- WHN 2001-03-06
232 (#+sb-xc-host defmacro
233  #-sb-xc-host sb!xc:defmacro
234      define-info-class (class)
235   (declare (type keyword class))
236   `(progn
237      ;; (We don't need to evaluate this at load time, compile time is
238      ;; enough. There's special logic elsewhere which deals with cold
239      ;; load initialization by inspecting the info class data
240      ;; structures at compile time and generating code to recreate
241      ;; those data structures.)
242      (eval-when (:compile-toplevel :execute)
243        (unless (gethash ,class *info-classes*)
244          (setf (gethash ,class *info-classes*) (make-class-info ,class))))
245      ,class))
246
247 ;;; Find a type number not already in use by looking for a null entry
248 ;;; in *INFO-TYPES*.
249 (defun find-unused-type-number ()
250   (or (position nil *info-types*)
251       (error "no more INFO type numbers available")))
252
253 ;;; a list of forms for initializing the DEFAULT slots of TYPE-INFO
254 ;;; objects, accumulated during compilation and eventually converted
255 ;;; into a function to be called at cold load time after the
256 ;;; appropriate TYPE-INFO objects have been created
257 ;;;
258 ;;; Note: This is quite similar to the !COLD-INIT-FORMS machinery, but
259 ;;; we can't conveniently use the ordinary !COLD-INIT-FORMS machinery
260 ;;; here. The problem is that the natural order in which the
261 ;;; default-slot-initialization forms are generated relative to the
262 ;;; order in which the TYPE-INFO-creation forms are generated doesn't
263 ;;; match the relative order in which the forms need to be executed at
264 ;;; cold load time.
265 (defparameter *reversed-type-info-init-forms* nil)
266
267 ;;; Define a new type of global information for CLASS. TYPE is the
268 ;;; name of the type, DEFAULT is the value for that type when it
269 ;;; hasn't been set, and TYPE-SPEC is a type specifier which values of
270 ;;; the type must satisfy. The default expression is evaluated each
271 ;;; time the information is needed, with NAME bound to the name for
272 ;;; which the information is being looked up. 
273 ;;;
274 ;;; The main thing we do is determine the type's number. We need to do
275 ;;; this at macroexpansion time, since both the COMPILE and LOAD time
276 ;;; calls to %DEFINE-INFO-TYPE must use the same type number.
277 (#+sb-xc-host defmacro
278  #-sb-xc-host sb!xc:defmacro
279     define-info-type (&key (class (missing-arg))
280                            (type (missing-arg))
281                            (type-spec (missing-arg))
282                            default)
283   (declare (type keyword class type))
284   `(progn
285      (eval-when (:compile-toplevel :execute)
286        ;; At compile time, ensure that the type number exists. It will
287        ;; need to be forced to exist at cold load time, too, but
288        ;; that's not handled here; it's handled by later code which
289        ;; looks at the compile time state and generates code to
290        ;; replicate it at cold load time.
291        (let* ((class-info (class-info-or-lose ',class))
292               (old-type-info (find-type-info ',type class-info)))
293          (unless old-type-info
294            (let* ((new-type-number (find-unused-type-number))
295                   (new-type-info
296                    (make-type-info :name ',type
297                                    :class class-info
298                                    :number new-type-number)))
299              (setf (aref *info-types* new-type-number) new-type-info)
300              (push new-type-info (class-info-types class-info)))))
301        ;; Arrange for TYPE-INFO-DEFAULT and TYPE-INFO-TYPE to be set
302        ;; at cold load time. (They can't very well be set at
303        ;; cross-compile time, since they differ between the
304        ;; cross-compiler and the target. The DEFAULT slot values
305        ;; differ because they're compiled closures, and the TYPE slot
306        ;; values differ in the use of SB!XC symbols instead of CL
307        ;; symbols.)
308        (push `(let ((type-info (type-info-or-lose ,',class ,',type)))
309                 (setf (type-info-default type-info)
310                        ;; FIXME: This code is sort of nasty. It would
311                        ;; be cleaner if DEFAULT accepted a real
312                        ;; function, instead of accepting a statement
313                        ;; which will be turned into a lambda assuming
314                        ;; that the argument name is NAME. It might
315                        ;; even be more microefficient, too, since many
316                        ;; DEFAULTs could be implemented as (CONSTANTLY
317                        ;; NIL) instead of full-blown (LAMBDA (X) NIL).
318                        (lambda (name)
319                          (declare (ignorable name))
320                          ,',default))
321                 (setf (type-info-type type-info) ',',type-spec))
322              *reversed-type-info-init-forms*))
323      ',type))
324
325 ) ; EVAL-WHEN
326 \f
327 ;;;; generic info environments
328
329 ;;; Note: the CACHE-NAME slot is deliberately not shared for
330 ;;; bootstrapping reasons. If we access with accessors for the exact
331 ;;; type, then the inline type check will win. If the inline check
332 ;;; didn't win, we would try to use the type system before it was
333 ;;; properly initialized.
334 (defstruct (info-env (:constructor nil)
335                      (:copier nil))
336   ;; some string describing what is in this environment, for
337   ;; printing/debugging purposes only
338   (name (missing-arg) :type string))
339 (def!method print-object ((x info-env) stream)
340   (print-unreadable-object (x stream :type t)
341     (prin1 (info-env-name x) stream)))
342 \f
343 ;;;; generic interfaces
344
345 ;;; FIXME: used only in this file, needn't be in runtime
346 (defmacro do-info ((env &key (name (gensym)) (class (gensym)) (type (gensym))
347                         (type-number (gensym)) (value (gensym)) known-volatile)
348                    &body body)
349   #!+sb-doc
350   "DO-INFO (Env &Key Name Class Type Value) Form*
351   Iterate over all the values stored in the Info-Env Env. Name is bound to
352   the entry's name, Class and Type are bound to the class and type
353   (represented as keywords), and Value is bound to the entry's value."
354   (once-only ((n-env env))
355     (if known-volatile
356         (do-volatile-info name class type type-number value n-env body)
357         `(if (typep ,n-env 'volatile-info-env)
358              ,(do-volatile-info name class type type-number value n-env body)
359              ,(do-compact-info name class type type-number value
360                                n-env body)))))
361
362 (eval-when (:compile-toplevel :load-toplevel :execute)
363
364 ;;; Return code to iterate over a compact info environment.
365 (defun do-compact-info (name-var class-var type-var type-number-var value-var
366                                  n-env body)
367   (let ((n-index (gensym))
368         (n-type (gensym))
369         (punt (gensym)))
370     (once-only ((n-table `(compact-info-env-table ,n-env))
371                 (n-entries-index `(compact-info-env-index ,n-env))
372                 (n-entries `(compact-info-env-entries ,n-env))
373                 (n-entries-info `(compact-info-env-entries-info ,n-env))
374                 (n-info-types '*info-types*))
375       `(dotimes (,n-index (length ,n-table))
376          (declare (type index ,n-index))
377          (block ,PUNT
378            (let ((,name-var (svref ,n-table ,n-index)))
379              (unless (eql ,name-var 0)
380                (do-anonymous ((,n-type (aref ,n-entries-index ,n-index)
381                                        (1+ ,n-type)))
382                              (nil)
383                  (declare (type index ,n-type))
384                  ,(once-only ((n-info `(aref ,n-entries-info ,n-type)))
385                     `(let ((,type-number-var
386                             (logand ,n-info compact-info-entry-type-mask)))
387                        ,(once-only ((n-type-info
388                                      `(svref ,n-info-types
389                                              ,type-number-var)))
390                           `(let ((,type-var (type-info-name ,n-type-info))
391                                  (,class-var (class-info-name
392                                               (type-info-class ,n-type-info)))
393                                  (,value-var (svref ,n-entries ,n-type)))
394                              (declare (ignorable ,type-var ,class-var
395                                                  ,value-var))
396                              ,@body
397                              (unless (zerop (logand ,n-info
398                                                     compact-info-entry-last))
399                                (return-from ,PUNT))))))))))))))
400
401 ;;; Return code to iterate over a volatile info environment.
402 (defun do-volatile-info (name-var class-var type-var type-number-var value-var
403                                   n-env body)
404   (let ((n-index (gensym)) (n-names (gensym)) (n-types (gensym)))
405     (once-only ((n-table `(volatile-info-env-table ,n-env))
406                 (n-info-types '*info-types*))
407       `(dotimes (,n-index (length ,n-table))
408          (declare (type index ,n-index))
409          (do-anonymous ((,n-names (svref ,n-table ,n-index)
410                                   (cdr ,n-names)))
411                        ((null ,n-names))
412            (let ((,name-var (caar ,n-names)))
413              (declare (ignorable ,name-var))
414              (do-anonymous ((,n-types (cdar ,n-names) (cdr ,n-types)))
415                            ((null ,n-types))
416                (let ((,type-number-var (caar ,n-types)))
417                  ,(once-only ((n-type `(svref ,n-info-types
418                                               ,type-number-var)))
419                     `(let ((,type-var (type-info-name ,n-type))
420                            (,class-var (class-info-name
421                                         (type-info-class ,n-type)))
422                            (,value-var (cdar ,n-types)))
423                        (declare (ignorable ,type-var ,class-var ,value-var))
424                        ,@body))))))))))
425
426 ) ; EVAL-WHEN
427 \f
428 ;;;; INFO cache
429
430 ;;;; We use a hash cache to cache name X type => value for the current
431 ;;;; value of *INFO-ENVIRONMENT*. This is in addition to the
432 ;;;; per-environment caching of name => types.
433
434 ;;; The value of *INFO-ENVIRONMENT* that has cached values.
435 ;;; *INFO-ENVIRONMENT* should never be destructively modified, so if
436 ;;; it is EQ to this, then the cache is valid.
437 (defvar *cached-info-environment*)
438 (!cold-init-forms
439   (setf *cached-info-environment* nil))
440
441 ;;; the hash function used for the INFO cache
442 #!-sb-fluid (declaim (inline info-cache-hash))
443 (defun info-cache-hash (name type)
444   (logand
445     (the fixnum
446          (logxor (globaldb-sxhashoid name)
447                  (ash (the fixnum type) 7)))
448     #x3FF))
449
450 (!cold-init-forms
451   (/show0 "before initialization of INFO hash cache"))
452 (define-hash-cache info ((name eq) (type eq))
453   :values 2
454   :hash-function info-cache-hash
455   :hash-bits 10
456   :default (values nil :empty)
457   :init-wrapper !cold-init-forms)
458 (!cold-init-forms
459   (/show0 "clearing INFO hash cache")
460   (info-cache-clear)
461   (/show0 "done clearing INFO hash cache"))
462
463 ;;; If the info cache is invalid, then clear it.
464 #!-sb-fluid (declaim (inline clear-invalid-info-cache))
465 (defun clear-invalid-info-cache ()
466   ;; Unless the cache is valid..
467   (unless (eq *info-environment* *cached-info-environment*)
468     (;; In the target Lisp, this should be done without interrupts,
469      ;; but in the host Lisp when cross-compiling, we don't need to
470      ;; sweat it, since no affected-by-GC hashes should be used when
471      ;; running under the host Lisp (since that's non-portable) and
472      ;; since only one thread should be used when running under the
473      ;; host Lisp (because multiple threads are non-portable too).
474      #-sb-xc-host without-interrupts
475      #+sb-xc-host progn
476       (info-cache-clear)
477       (setq *cached-info-environment* *info-environment*))))
478 \f
479 ;;;; compact info environments
480
481 ;;; The upper limit on the size of the ENTRIES vector in a COMPACT-INFO-ENV.
482 (defconstant compact-info-env-entries-bits 16)
483 (deftype compact-info-entries-index () `(unsigned-byte ,compact-info-env-entries-bits))
484
485 ;;; the type of the values in COMPACT-INFO-ENTRIES-INFO
486 (deftype compact-info-entry () `(unsigned-byte ,(1+ type-number-bits)))
487
488 ;;; This is an open hashtable with rehashing. Since modification is
489 ;;; not allowed, we don't have to worry about deleted entries. We
490 ;;; indirect through a parallel vector to find the index in the
491 ;;; ENTRIES at which the entries for a given name starts.
492 (defstruct (compact-info-env (:include info-env)
493                              #-sb-xc-host (:pure :substructure)
494                              (:copier nil))
495   ;; If this value is EQ to the name we want to look up, then the
496   ;; cache hit function can be called instead of the lookup function.
497   (cache-name 0)
498   ;; The index in ENTRIES for the CACHE-NAME, or NIL if that name has
499   ;; no entries.
500   (cache-index nil :type (or compact-info-entries-index null))
501   ;; hashtable of the names in this environment. If a bucket is
502   ;; unused, it is 0.
503   (table (missing-arg) :type simple-vector)
504   ;; an indirection vector parallel to TABLE, translating indices in
505   ;; TABLE to the start of the ENTRIES for that name. Unused entries
506   ;; are undefined.
507   (index (missing-arg) :type (simple-array compact-info-entries-index (*)))
508   ;; a vector contining in contiguous ranges the values of for all the
509   ;; types of info for each name.
510   (entries (missing-arg) :type simple-vector)
511   ;; a vector parallel to ENTRIES, indicating the type number for the
512   ;; value stored in that location and whether this location is the
513   ;; last type of info stored for this name. The type number is in the
514   ;; low TYPE-NUMBER-BITS bits, and the next bit is set if this is the
515   ;; last entry.
516   (entries-info (missing-arg) :type (simple-array compact-info-entry (*))))
517
518 (defconstant compact-info-entry-type-mask (ldb (byte type-number-bits 0) -1))
519 (defconstant compact-info-entry-last (ash 1 type-number-bits))
520
521 ;;; Return the value of the type corresponding to NUMBER for the
522 ;;; currently cached name in ENV.
523 #!-sb-fluid (declaim (inline compact-info-cache-hit))
524 (defun compact-info-cache-hit (env number)
525   (declare (type compact-info-env env) (type type-number number))
526   (let ((entries-info (compact-info-env-entries-info env))
527         (index (compact-info-env-cache-index env)))
528     (if index
529         (do ((index index (1+ index)))
530             (nil)
531           (declare (type index index))
532           (let ((info (aref entries-info index)))
533             (when (= (logand info compact-info-entry-type-mask) number)
534               (return (values (svref (compact-info-env-entries env) index)
535                               t)))
536             (unless (zerop (logand compact-info-entry-last info))
537               (return (values nil nil)))))
538         (values nil nil))))
539
540 ;;; Encache NAME in the compact environment ENV. HASH is the
541 ;;; GLOBALDB-SXHASHOID of NAME.
542 (defun compact-info-lookup (env name hash)
543   (declare (type compact-info-env env) (type index hash))
544   (let* ((table (compact-info-env-table env))
545          (len (length table))
546          (len-2 (- len 2))
547          (hash2 (- len-2 (rem hash len-2))))
548     (declare (type index len-2 hash2))
549     (macrolet ((lookup (test)
550                  `(do ((probe (rem hash len)
551                               (let ((new (+ probe hash2)))
552                                 (declare (type index new))
553                                 ;; same as (MOD NEW LEN), but faster.
554                                 (if (>= new len)
555                                     (the index (- new len))
556                                     new))))
557                       (nil)
558                     (let ((entry (svref table probe)))
559                       (when (eql entry 0)
560                         (return nil))
561                       (when (,test entry name)
562                         (return (aref (compact-info-env-index env)
563                                       probe)))))))
564       (setf (compact-info-env-cache-index env)
565             (if (symbolp name)
566                 (lookup eq)
567                 (lookup equal)))
568       (setf (compact-info-env-cache-name env) name)))
569
570   (values))
571
572 ;;; the exact density (modulo rounding) of the hashtable in a compact
573 ;;; info environment in names/bucket
574 (defconstant compact-info-environment-density 65)
575
576 ;;; Return a new compact info environment that holds the same
577 ;;; information as ENV.
578 (defun compact-info-environment (env &key (name (info-env-name env)))
579   (let ((name-count 0)
580         (prev-name 0)
581         (entry-count 0))
582     (/show0 "before COLLECT in COMPACT-INFO-ENVIRONMENT")
583
584     ;; Iterate over the environment once to find out how many names
585     ;; and entries it has, then build the result. This code assumes
586     ;; that all the entries for a name well be iterated over
587     ;; contiguously, which holds true for the implementation of
588     ;; iteration over both kinds of environments.
589     (collect ((names))
590
591       (/show0 "at head of COLLECT in COMPACT-INFO-ENVIRONMENT")
592       (let ((types ()))
593         (do-info (env :name name :type-number num :value value)
594           (/noshow0 "at head of DO-INFO in COMPACT-INFO-ENVIRONMENT")
595           (unless (eq name prev-name)
596             (/noshow0 "not (EQ NAME PREV-NAME) case")
597             (incf name-count)
598             (unless (eql prev-name 0)
599               (names (cons prev-name types)))
600             (setq prev-name name)
601             (setq types ()))
602           (incf entry-count)
603           (push (cons num value) types))
604         (unless (eql prev-name 0)
605           (/show0 "not (EQL PREV-NAME 0) case")
606           (names (cons prev-name types))))
607
608       ;; Now that we know how big the environment is, we can build
609       ;; a table to represent it.
610       ;; 
611       ;; When building the table, we sort the entries by pointer
612       ;; comparison in an attempt to preserve any VM locality present
613       ;; in the original load order, rather than randomizing with the
614       ;; original hash function.
615       (/show0 "about to make/sort vectors in COMPACT-INFO-ENVIRONMENT")
616       (let* ((table-size (primify
617                           (+ (truncate (* name-count 100)
618                                        compact-info-environment-density)
619                              3)))
620              (table (make-array table-size :initial-element 0))
621              (index (make-array table-size
622                                 :element-type 'compact-info-entries-index))
623              (entries (make-array entry-count))
624              (entries-info (make-array entry-count
625                                        :element-type 'compact-info-entry))
626              (sorted (sort (names)
627                            #+sb-xc-host #'<
628                            ;; (This MAKE-FIXNUM hack implements
629                            ;; pointer comparison, as explained above.)
630                            #-sb-xc-host (lambda (x y)
631                                           (< (%primitive make-fixnum x)
632                                              (%primitive make-fixnum y))))))
633         (/show0 "done making/sorting vectors in COMPACT-INFO-ENVIRONMENT")
634         (let ((entries-idx 0))
635           (dolist (types sorted)
636             (let* ((name (first types))
637                    (hash (globaldb-sxhashoid name))
638                    (len-2 (- table-size 2))
639                    (hash2 (- len-2 (rem hash len-2))))
640               (do ((probe (rem hash table-size)
641                           (rem (+ probe hash2) table-size)))
642                   (nil)
643                 (let ((entry (svref table probe)))
644                   (when (eql entry 0)
645                     (setf (svref table probe) name)
646                     (setf (aref index probe) entries-idx)
647                     (return))
648                   (aver (not (equal entry name))))))
649
650             (unless (zerop entries-idx)
651               (setf (aref entries-info (1- entries-idx))
652                     (logior (aref entries-info (1- entries-idx))
653                             compact-info-entry-last)))
654
655             (loop for (num . value) in (rest types) do
656               (setf (aref entries-info entries-idx) num)
657               (setf (aref entries entries-idx) value)
658               (incf entries-idx)))
659           (/show0 "done w/ DOLIST (TYPES SORTED) in COMPACT-INFO-ENVIRONMENT")
660
661           (unless (zerop entry-count)
662             (/show0 "nonZEROP ENTRY-COUNT")
663             (setf (aref entries-info (1- entry-count))
664                   (logior (aref entries-info (1- entry-count))
665                           compact-info-entry-last)))
666
667           (/show0 "falling through to MAKE-COMPACT-INFO-ENV")
668           (make-compact-info-env :name name
669                                  :table table
670                                  :index index
671                                  :entries entries
672                                  :entries-info entries-info))))))
673 \f
674 ;;;; volatile environments
675
676 ;;; This is a closed hashtable, with the bucket being computed by
677 ;;; taking the GLOBALDB-SXHASHOID of the NAME modulo the table size.
678 (defstruct (volatile-info-env (:include info-env)
679                               (:copier nil))
680   ;; If this value is EQ to the name we want to look up, then the
681   ;; cache hit function can be called instead of the lookup function.
682   (cache-name 0)
683   ;; the alist translating type numbers to values for the currently
684   ;; cached name
685   (cache-types nil :type list)
686   ;; vector of alists of alists of the form:
687   ;;    ((Name . ((Type-Number . Value) ...) ...)
688   (table (missing-arg) :type simple-vector)
689   ;; the number of distinct names currently in this table. Each name
690   ;; may have multiple entries, since there can be many types of info.
691   (count 0 :type index)
692   ;; the number of names at which we should grow the table and rehash
693   (threshold 0 :type index))
694
695 ;;; Just like COMPACT-INFO-CACHE-HIT, only do it on a volatile environment.
696 #!-sb-fluid (declaim (inline volatile-info-cache-hit))
697 (defun volatile-info-cache-hit (env number)
698   (declare (type volatile-info-env env) (type type-number number))
699   (dolist (type (volatile-info-env-cache-types env) (values nil nil))
700     (when (eql (car type) number)
701       (return (values (cdr type) t)))))
702
703 ;;; Just like COMPACT-INFO-LOOKUP, only do it on a volatile environment.
704 (defun volatile-info-lookup (env name hash)
705   (declare (type volatile-info-env env) (type index hash))
706   (let ((table (volatile-info-env-table env)))
707     (macrolet ((lookup (test)
708                  `(dolist (entry (svref table (mod hash (length table))) ())
709                     (when (,test (car entry) name)
710                       (return (cdr entry))))))
711       (setf (volatile-info-env-cache-types env)
712             (if (symbolp name)
713                 (lookup eq)
714                 (lookup equal)))
715       (setf (volatile-info-env-cache-name env) name)))
716
717   (values))
718
719 ;;; Given a volatile environment Env, bind Table-Var the environment's table
720 ;;; and Index-Var to the index of Name's bucket in the table. We also flush
721 ;;; the cache so that things will be consistent if body modifies something.
722 (eval-when (:compile-toplevel :execute)
723   (#+sb-xc-host cl:defmacro
724    #-sb-xc-host sb!xc:defmacro
725       with-info-bucket ((table-var index-var name env) &body body)
726     (once-only ((n-name name)
727                 (n-env env))
728       `(progn
729          (setf (volatile-info-env-cache-name ,n-env) 0)
730          (let* ((,table-var (volatile-info-env-table ,n-env))
731                 (,index-var (mod (globaldb-sxhashoid ,n-name)
732                                  (length ,table-var))))
733            ,@body)))))
734
735 ;;; Get the info environment that we use for write/modification operations.
736 ;;; This is always the first environment in the list, and must be a
737 ;;; VOLATILE-INFO-ENV.
738 #!-sb-fluid (declaim (inline get-write-info-env))
739 (defun get-write-info-env (&optional (env-list *info-environment*))
740   (let ((env (car env-list)))
741     (unless env
742       (error "no info environment?"))
743     (unless (typep env 'volatile-info-env)
744       (error "cannot modify this environment: ~S" env))
745     (the volatile-info-env env)))
746
747 ;;; If Name is already present in the table, then just create or
748 ;;; modify the specified type. Otherwise, add the new name and type,
749 ;;; checking for rehashing.
750 ;;;
751 ;;; We rehash by making a new larger environment, copying all of the
752 ;;; entries into it, then clobbering the old environment with the new
753 ;;; environment's table. We clear the old table to prevent it from
754 ;;; holding onto garbage if it is statically allocated.
755 ;;;
756 ;;; We return the new value so that this can be conveniently used in a
757 ;;; SETF function.
758 (defun set-info-value (name0 type new-value
759                              &optional (env (get-write-info-env)))
760   (declare (type type-number type) (type volatile-info-env env)
761            (inline assoc))
762   (let ((name (uncross name0)))
763     (when (eql name 0)
764       (error "0 is not a legal INFO name."))
765     ;; We don't enter the value in the cache because we don't know that this
766     ;; info-environment is part of *cached-info-environment*.
767     (info-cache-enter name type nil :empty)
768     (with-info-bucket (table index name env)
769       (let ((types (if (symbolp name)
770                        (assoc name (svref table index) :test #'eq)
771                        (assoc name (svref table index) :test #'equal))))
772         (cond
773          (types
774           (let ((value (assoc type (cdr types))))
775             (if value
776                 (setf (cdr value) new-value)
777                 (push (cons type new-value) (cdr types)))))
778          (t
779           (push (cons name (list (cons type new-value)))
780                 (svref table index))
781
782           (let ((count (incf (volatile-info-env-count env))))
783             (when (>= count (volatile-info-env-threshold env))
784               (let ((new (make-info-environment :size (* count 2))))
785                 (do-info (env :name entry-name :type-number entry-num
786                               :value entry-val :known-volatile t)
787                          (set-info-value entry-name entry-num entry-val new))
788                 (fill (volatile-info-env-table env) nil)
789                 (setf (volatile-info-env-table env)
790                       (volatile-info-env-table new))
791                 (setf (volatile-info-env-threshold env)
792                       (volatile-info-env-threshold new)))))))))
793     new-value))
794
795 ;;; FIXME: It should be possible to eliminate the hairy compiler macros below
796 ;;; by declaring INFO and (SETF INFO) inline and making a simple compiler macro
797 ;;; for TYPE-INFO-OR-LOSE. (If we didn't worry about efficiency of the
798 ;;; cross-compiler, we could even do it by just making TYPE-INFO-OR-LOSE
799 ;;; foldable.)
800
801 ;;; INFO is the standard way to access the database. It's settable.
802 ;;;
803 ;;; Return the information of the specified TYPE and CLASS for NAME.
804 ;;; The second value returned is true if there is any such information
805 ;;; recorded. If there is no information, the first value returned is
806 ;;; the default and the second value returned is NIL.
807 (defun info (class type name &optional (env-list nil env-list-p))
808   ;; FIXME: At some point check systematically to make sure that the
809   ;; system doesn't do any full calls to INFO or (SETF INFO), or at
810   ;; least none in any inner loops.
811   (let ((info (type-info-or-lose class type)))
812     (if env-list-p
813         (get-info-value name (type-info-number info) env-list)
814         (get-info-value name (type-info-number info)))))
815 #!-sb-fluid
816 (define-compiler-macro info
817   (&whole whole class type name &optional (env-list nil env-list-p))
818   ;; Constant CLASS and TYPE is an overwhelmingly common special case,
819   ;; and we can implement it much more efficiently than the general case.
820   (if (and (constantp class) (constantp type))
821       (let ((info (type-info-or-lose class type))
822             (value (gensym "VALUE"))
823             (foundp (gensym "FOUNDP")))
824         `(multiple-value-bind (,value ,foundp)
825              (get-info-value ,name
826                              ,(type-info-number info)
827                              ,@(when env-list-p `(,env-list))) 
828            (declare (type ,(type-info-type info) ,value))
829            (values ,value ,foundp)))
830       whole))
831 (defun (setf info) (new-value
832                     class
833                     type
834                     name
835                     &optional (env-list nil env-list-p))
836   (let* ((info (type-info-or-lose class type))
837          (tin (type-info-number info)))
838     (if env-list-p
839       (set-info-value name
840                       tin
841                       new-value
842                       (get-write-info-env env-list))
843       (set-info-value name
844                       tin
845                       new-value)))
846   new-value)
847 ;;; FIXME: We'd like to do this, but Python doesn't support
848 ;;; compiler macros and it's hard to change it so that it does.
849 ;;; It might make more sense to just convert INFO :FOO :BAR into
850 ;;; an ordinary function, so that instead of calling INFO :FOO :BAR
851 ;;; you call e.g. INFO%FOO%BAR. Then dynamic linking could be handled
852 ;;; by the ordinary Lisp mechanisms and we wouldn't have to maintain
853 ;;; all this cruft..
854 #|
855 #!-sb-fluid
856 (progn
857   (define-compiler-macro (setf info) (&whole whole
858                                       new-value
859                                       class
860                                       type
861                                       name
862                                       &optional (env-list nil env-list-p))
863     ;; Constant CLASS and TYPE is an overwhelmingly common special case, and we
864     ;; can resolve it much more efficiently than the general case.
865     (if (and (constantp class) (constantp type))
866         (let* ((info (type-info-or-lose class type))
867                (tin (type-info-number info)))
868           (if env-list-p
869               `(set-info-value ,name
870                                ,tin
871                                ,new-value
872                                (get-write-info-env ,env-list))
873               `(set-info-value ,name
874                                ,tin
875                                ,new-value)))
876         whole)))
877 |#
878
879 ;;; the maximum density of the hashtable in a volatile env (in
880 ;;; names/bucket)
881 ;;;
882 ;;; FIXME: actually seems to be measured in percent, should be
883 ;;; converted to be measured in names/bucket
884 (defconstant volatile-info-environment-density 50)
885
886 ;;; Make a new volatile environment of the specified size.
887 (defun make-info-environment (&key (size 42) (name "Unknown"))
888   (declare (type (integer 1) size))
889   (let ((table-size (primify (truncate (* size 100)
890                                        volatile-info-environment-density))))
891     (make-volatile-info-env :name name
892                             :table (make-array table-size :initial-element nil)
893                             :threshold size)))
894
895 ;;; Clear the information of the specified TYPE and CLASS for NAME in
896 ;;; the current environment, allowing any inherited info to become
897 ;;; visible. We return true if there was any info.
898 (defun clear-info (class type name)
899   #!+sb-doc
900   (let ((info (type-info-or-lose class type)))
901     (clear-info-value name (type-info-number info))))
902 #!-sb-fluid
903 (define-compiler-macro clear-info (&whole whole class type name)
904   ;; Constant CLASS and TYPE is an overwhelmingly common special case, and
905   ;; we can resolve it much more efficiently than the general case.
906   (if (and (keywordp class) (keywordp type))
907     (let ((info (type-info-or-lose class type)))
908       `(clear-info-value ,name ,(type-info-number info)))
909     whole))
910 (defun clear-info-value (name type)
911   (declare (type type-number type) (inline assoc))
912   (clear-invalid-info-cache)
913   (info-cache-enter name type nil :empty)
914   (with-info-bucket (table index name (get-write-info-env))
915     (let ((types (assoc name (svref table index) :test #'equal)))
916       (when (and types
917                  (assoc type (cdr types)))
918         (setf (cdr types)
919               (delete type (cdr types) :key #'car))
920         t))))
921 \f
922 ;;;; *INFO-ENVIRONMENT*
923
924 ;;; We do info access relative to the current *INFO-ENVIRONMENT*, a
925 ;;; list of INFO-ENVIRONMENT structures.
926 (defvar *info-environment*)
927 (declaim (type list *info-environment*))
928 (!cold-init-forms
929   (setq *info-environment*
930         (list (make-info-environment :name "initial global")))
931   (/show0 "done setting *INFO-ENVIRONMENT*"))
932 ;;; FIXME: should perhaps be *INFO-ENV-LIST*. And rename
933 ;;; all FOO-INFO-ENVIRONMENT-BAR stuff to FOO-INFO-ENV-BAR.
934 \f
935 ;;;; GET-INFO-VALUE
936
937 ;;; Check whether the name and type is in our cache, if so return it.
938 ;;; Otherwise, search for the value and encache it.
939 ;;;
940 ;;; Return the value from the first environment which has it defined,
941 ;;; or return the default if none does. We have a cache for the last
942 ;;; name looked up in each environment. We don't compute the hash
943 ;;; until the first time the cache misses. When the cache does miss,
944 ;;; we invalidate it before calling the lookup routine to eliminate
945 ;;; the possibility of the cache being partially updated if the lookup
946 ;;; is interrupted.
947 (defun get-info-value (name0 type &optional (env-list nil env-list-p))
948   (declare (type type-number type))
949   ;; sanity check: If we have screwed up initialization somehow, then
950   ;; *INFO-TYPES* could still be uninitialized at the time we try to
951   ;; get an info value, and then we'd be out of luck. (This happened,
952   ;; and was confusing to debug, when rewriting EVAL-WHEN in
953   ;; sbcl-0.pre7.x.)
954   (aver (aref *info-types* type))
955   (let ((name (uncross name0)))
956     (flet ((lookup-ignoring-global-cache (env-list)
957              (let ((hash nil))
958                (dolist (env env-list
959                             (multiple-value-bind (val winp)
960                                 (funcall (type-info-default
961                                           (svref *info-types* type))
962                                          name)
963                               (values val winp)))
964                  (macrolet ((frob (lookup cache slot)
965                               `(progn
966                                  (unless (eq name (,slot env))
967                                    (unless hash
968                                      (setq hash (globaldb-sxhashoid name)))
969                                    (setf (,slot env) 0)
970                                    (,lookup env name hash))
971                                  (multiple-value-bind (value winp)
972                                      (,cache env type)
973                                    (when winp (return (values value t)))))))
974                    (etypecase env
975                      (volatile-info-env (frob
976                                          volatile-info-lookup
977                                          volatile-info-cache-hit
978                                          volatile-info-env-cache-name))
979                      (compact-info-env (frob
980                                         compact-info-lookup
981                                         compact-info-cache-hit
982                                         compact-info-env-cache-name))))))))
983       (cond (env-list-p
984              (lookup-ignoring-global-cache env-list))
985             (t
986              (clear-invalid-info-cache)
987              (multiple-value-bind (val winp) (info-cache-lookup name type)
988                (if (eq winp :empty)
989                    (multiple-value-bind (val winp)
990                        (lookup-ignoring-global-cache *info-environment*)
991                      (info-cache-enter name type val winp)
992                      (values val winp))
993                    (values val winp))))))))
994 \f
995 ;;;; definitions for function information
996
997 (define-info-class :function)
998
999 ;;; the kind of functional object being described. If null, NAME isn't
1000 ;;; a known functional object.
1001 (define-info-type
1002   :class :function
1003   :type :kind
1004   :type-spec (member nil :function :macro :special-form)
1005   ;; I'm a little confused what the correct behavior of this default
1006   ;; is. It's not clear how to generalize the FBOUNDP expression to
1007   ;; the cross-compiler. As far as I can tell, NIL is a safe default
1008   ;; -- it might keep the compiler from making some valid
1009   ;; optimization, but it shouldn't produce incorrect code. -- WHN
1010   ;; 19990330
1011   :default
1012   #+sb-xc-host nil
1013   #-sb-xc-host (if (fboundp name) :function nil))
1014
1015 ;;; The type specifier for this function.
1016 (define-info-type
1017   :class :function
1018   :type :type
1019   :type-spec ctype
1020   ;; Again (as in DEFINE-INFO-TYPE :CLASS :FUNCTION :TYPE :KIND) it's
1021   ;; not clear how to generalize the FBOUNDP expression to the
1022   ;; cross-compiler. -- WHN 19990330
1023   :default
1024   #+sb-xc-host (specifier-type 'function)
1025   #-sb-xc-host (if (fboundp name)
1026                    (extract-fun-type (fdefinition name))
1027                    (specifier-type 'function)))
1028
1029 ;;; the ASSUMED-TYPE for this function, if we have to infer the type
1030 ;;; due to not having a declaration or definition
1031 (define-info-type
1032   :class :function
1033   :type :assumed-type
1034   ;; FIXME: The type-spec really should be
1035   ;;   (or approximate-fun-type null)).
1036   ;; It was changed to T as a hopefully-temporary hack while getting
1037   ;; cold init problems untangled.
1038   :type-spec t) 
1039
1040 ;;; where this information came from:
1041 ;;;    :ASSUMED  = from uses of the object
1042 ;;;    :DEFINED  = from examination of the definition
1043 ;;;    :DECLARED = from a declaration
1044 ;;; :DEFINED trumps :ASSUMED, and :DECLARED trumps :DEFINED.
1045 ;;; :DEFINED and :ASSUMED are useful for issuing compile-time warnings,
1046 ;;; and :DECLARED is useful for ANSIly specializing code which
1047 ;;; implements the function, or which uses the function's return values.
1048 (define-info-type
1049   :class :function
1050   :type :where-from
1051   :type-spec (member :declared :assumed :defined)
1052   :default
1053   ;; Again (as in DEFINE-INFO-TYPE :CLASS :FUNCTION :TYPE :KIND) it's
1054   ;; not clear how to generalize the FBOUNDP expression to the
1055   ;; cross-compiler. -- WHN 19990606
1056   #+sb-xc-host :assumed
1057   #-sb-xc-host (if (fboundp name) :defined :assumed))
1058
1059 ;;; something which can be decoded into the inline expansion of the
1060 ;;; function, or NIL if there is none
1061 ;;;
1062 ;;; To inline a function, we want a lambda expression, e.g.
1063 ;;; '(LAMBDA (X) (+ X 1)). That can be encoded here in one of two
1064 ;;; ways.
1065 ;;;   * The value in INFO can be the lambda expression itself, e.g. 
1066 ;;;       (SETF (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR 'FOO)
1067 ;;;             '(LAMBDA (X) (+ X 1)))
1068 ;;;     This is the ordinary way, the natural way of representing e.g.
1069 ;;;       (DECLAIM (INLINE FOO))
1070 ;;;       (DEFUN FOO (X) (+ X 1))
1071 ;;;   * The value in INFO can be a closure which returns the lambda
1072 ;;;     expression, e.g.
1073 ;;;       (SETF (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR 'BAR-LEFT-CHILD)
1074 ;;;             (LAMBDA ()
1075 ;;;               '(LAMBDA (BAR) (BAR-REF BAR 3))))
1076 ;;;     This twisty way of storing values is supported in order to
1077 ;;;     allow structure slot accessors, and perhaps later other
1078 ;;;     stereotyped functions, to be represented compactly.
1079 (define-info-type
1080   :class :function
1081   :type :inline-expansion-designator
1082   :type-spec (or list function)
1083   :default nil)
1084
1085 ;;; This specifies whether this function may be expanded inline. If
1086 ;;; null, we don't care.
1087 (define-info-type
1088   :class :function
1089   :type :inlinep
1090   :type-spec inlinep
1091   :default nil)
1092
1093 ;;; a macro-like function which transforms a call to this function
1094 ;;; into some other Lisp form. This expansion is inhibited if inline
1095 ;;; expansion is inhibited
1096 (define-info-type
1097   :class :function
1098   :type :source-transform
1099   :type-spec (or function null))
1100
1101 ;;; the macroexpansion function for this macro
1102 (define-info-type
1103   :class :function
1104   :type :macro-function
1105   :type-spec (or function null)
1106   :default nil)
1107
1108 ;;; the compiler-macroexpansion function for this macro
1109 (define-info-type
1110   :class :function
1111   :type :compiler-macro-function
1112   :type-spec (or function null)
1113   :default nil)
1114
1115 ;;; a function which converts this special form into IR1
1116 (define-info-type
1117   :class :function
1118   :type :ir1-convert
1119   :type-spec (or function null))
1120
1121 ;;; a function which gets a chance to do stuff to the IR1 for any call
1122 ;;; to this function.
1123 (define-info-type
1124   :class :function
1125   :type :ir1-transform
1126   :type-spec (or function null))
1127
1128 ;;; If a function is "known" to the compiler, then this is a
1129 ;;; FUNCTION-INFO structure containing the info used to special-case
1130 ;;; compilation.
1131 (define-info-type
1132   :class :function
1133   :type :info
1134   :type-spec (or function-info null)
1135   :default nil)
1136
1137 (define-info-type
1138   :class :function
1139   :type :documentation
1140   :type-spec (or string null)
1141   :default nil)
1142
1143 (define-info-type
1144   :class :function
1145   :type :definition
1146   :type-spec t
1147   :default nil)
1148 \f
1149 ;;;; definitions for other miscellaneous information
1150
1151 (define-info-class :variable)
1152
1153 ;;; the kind of variable-like thing described
1154 (define-info-type
1155   :class :variable
1156   :type :kind
1157   :type-spec (member :special :constant :global :alien)
1158   :default (if (symbol-self-evaluating-p name)
1159                :constant
1160                :global))
1161
1162 ;;; the declared type for this variable
1163 (define-info-type
1164   :class :variable
1165   :type :type
1166   :type-spec ctype
1167   :default *universal-type*)
1168
1169 ;;; where this type and kind information came from
1170 (define-info-type
1171   :class :variable
1172   :type :where-from
1173   :type-spec (member :declared :assumed :defined)
1174   :default :assumed)
1175
1176 ;;; the Lisp object which is the value of this constant, if known
1177 (define-info-type
1178   :class :variable
1179   :type :constant-value
1180   :type-spec t
1181   ;; CMU CL used to return two values for (INFO :VARIABLE :CONSTANT-VALUE ..).
1182   ;; Now we don't: it was the last remaining multiple-value return from
1183   ;; the INFO system, and bringing it down to one value lets us simplify
1184   ;; things, especially simplifying the declaration of return types.
1185   ;; Software which used to check the second value (for "is it defined
1186   ;; as a constant?") should check (EQL (INFO :VARIABLE :KIND ..) :CONSTANT)
1187   ;; instead.
1188   :default (if (symbol-self-evaluating-p name)
1189                name
1190                (error "internal error: constant lookup of nonconstant ~S"
1191                       name)))
1192
1193 (define-info-type
1194   :class :variable
1195   :type :alien-info
1196   :type-spec (or heap-alien-info null)
1197   :default nil)
1198
1199 (define-info-type
1200   :class :variable
1201   :type :documentation
1202   :type-spec (or string null)
1203   :default nil)
1204
1205 (define-info-class :type)
1206
1207 ;;; the kind of type described. We return :INSTANCE for standard types
1208 ;;; that are implemented as structures.
1209 (define-info-type
1210   :class :type
1211   :type :kind
1212   :type-spec (member :primitive :defined :instance nil)
1213   :default nil)
1214
1215 ;;; the expander function for a defined type
1216 (define-info-type
1217   :class :type
1218   :type :expander
1219   :type-spec (or function null)
1220   :default nil)
1221
1222 (define-info-type
1223   :class :type
1224   :type :documentation
1225   :type-spec (or string null))
1226
1227 ;;; function that parses type specifiers into CTYPE structures
1228 (define-info-type
1229   :class :type
1230   :type :translator
1231   :type-spec (or function null)
1232   :default nil)
1233
1234 ;;; If true, then the type coresponding to this name. Note that if
1235 ;;; this is a built-in class with a translation, then this is the
1236 ;;; translation, not the class object. This info type keeps track of
1237 ;;; various atomic types (NIL etc.) and also serves as a cache to
1238 ;;; ensure that common standard types (atomic and otherwise) are only
1239 ;;; consed once.
1240 (define-info-type
1241   :class :type
1242   :type :builtin
1243   :type-spec (or ctype null)
1244   :default nil)
1245
1246 ;;; If this is a class name, then the value is a cons (NAME . CLASS),
1247 ;;; where CLASS may be null if the class hasn't been defined yet. Note
1248 ;;; that for built-in classes, the kind may be :PRIMITIVE and not
1249 ;;; :INSTANCE. The the name is in the cons so that we can signal a
1250 ;;; meaningful error if we only have the cons.
1251 (define-info-type
1252   :class :type
1253   :type :class
1254   :type-spec (or sb!kernel::class-cell null)
1255   :default nil)
1256
1257 ;;; layout for this type being used by the compiler
1258 (define-info-type
1259   :class :type
1260   :type :compiler-layout
1261   :type-spec (or layout null)
1262   :default (let ((class (sb!xc:find-class name nil)))
1263              (when class (class-layout class))))
1264
1265 (define-info-class :typed-structure)
1266 (define-info-type
1267   :class :typed-structure
1268   :type :info
1269   :type-spec t
1270   :default nil)
1271
1272 (define-info-class :declaration)
1273 (define-info-type
1274   :class :declaration
1275   :type :recognized
1276   :type-spec boolean)
1277
1278 (define-info-class :alien-type)
1279 (define-info-type
1280   :class :alien-type
1281   :type :kind
1282   :type-spec (member :primitive :defined :unknown)
1283   :default :unknown)
1284 (define-info-type
1285   :class :alien-type
1286   :type :translator
1287   :type-spec (or function null)
1288   :default nil)
1289 (define-info-type
1290   :class :alien-type
1291   :type :definition
1292   :type-spec (or alien-type null)
1293   :default nil)
1294 (define-info-type
1295   :class :alien-type
1296   :type :struct
1297   :type-spec (or alien-type null)
1298   :default nil)
1299 (define-info-type
1300   :class :alien-type
1301   :type :union
1302   :type-spec (or alien-type null)
1303   :default nil)
1304 (define-info-type
1305   :class :alien-type
1306   :type :enum
1307   :type-spec (or alien-type null)
1308   :default nil)
1309
1310 (define-info-class :setf)
1311
1312 (define-info-type
1313   :class :setf
1314   :type :inverse
1315   :type-spec (or symbol null)
1316   :default nil)
1317
1318 (define-info-type
1319   :class :setf
1320   :type :documentation
1321   :type-spec (or string null)
1322   :default nil)
1323
1324 (define-info-type
1325   :class :setf
1326   :type :expander
1327   :type-spec (or function null)
1328   :default nil)
1329
1330 ;;; This is used for storing miscellaneous documentation types. The
1331 ;;; stuff is an alist translating documentation kinds to values.
1332 (define-info-class :random-documentation)
1333 (define-info-type
1334   :class :random-documentation
1335   :type :stuff
1336   :type-spec list
1337   :default ())
1338
1339 #!-sb-fluid (declaim (freeze-type info-env))
1340 \f
1341 ;;; Now that we have finished initializing *INFO-CLASSES* and
1342 ;;; *INFO-TYPES* (at compile time), generate code to set them at cold
1343 ;;; load time to the same state they have currently.
1344 (!cold-init-forms
1345   (/show0 "beginning *INFO-CLASSES* init, calling MAKE-HASH-TABLE")
1346   (setf *info-classes*
1347         (make-hash-table :size #.(hash-table-size *info-classes*)))
1348   (/show0 "done with MAKE-HASH-TABLE in *INFO-CLASSES* init")
1349   (dolist (class-info-name '#.(let ((result nil))
1350                                 (maphash (lambda (key value)
1351                                            (declare (ignore value))
1352                                            (push key result))
1353                                          *info-classes*)
1354                                 result))
1355     (let ((class-info (make-class-info class-info-name)))
1356       (setf (gethash class-info-name *info-classes*)
1357             class-info)))
1358   (/show0 "done with *INFO-CLASSES* initialization")
1359   (/show0 "beginning *INFO-TYPES* initialization")
1360   (setf *info-types*
1361         (map 'vector
1362              (lambda (x)
1363                (/show0 "in LAMBDA (X), X=..")
1364                (/hexstr x)
1365                (when x
1366                  (let* ((class-info (class-info-or-lose (second x)))
1367                         (type-info (make-type-info :name (first x)
1368                                                    :class class-info
1369                                                    :number (third x)
1370                                                    :type (fourth x))))
1371                    (/show0 "got CLASS-INFO in LAMBDA (X)")
1372                    (push type-info (class-info-types class-info))
1373                    type-info)))
1374              '#.(map 'list
1375                      (lambda (info-type)
1376                        (when info-type
1377                          (list (type-info-name info-type)
1378                                (class-info-name (type-info-class info-type))
1379                                (type-info-number info-type)
1380                                (type-info-type info-type))))
1381                      *info-types*)))
1382   (/show0 "done with *INFO-TYPES* initialization"))
1383
1384 ;;; At cold load time, after the INFO-TYPE objects have been created,
1385 ;;; we can set their DEFAULT and TYPE slots.
1386 (macrolet ((frob ()
1387              `(!cold-init-forms
1388                 ,@(reverse *reversed-type-info-init-forms*))))
1389   (frob))
1390 \f
1391 ;;;; a hack for detecting
1392 ;;;;   (DEFUN FOO (X Y)
1393 ;;;;     ..
1394 ;;;;     (SETF (BAR A FFH) 12) ; compiles to a call to #'(SETF BAR)
1395 ;;;;     ..)
1396 ;;;;   (DEFSETF BAR SET-BAR) ; can't influence previous compilation
1397 ;;;;
1398 ;;;; KLUDGE: Arguably it should be another class/type combination in
1399 ;;;; the globaldb. However, IMHO the whole globaldb/fdefinition
1400 ;;;; treatment of SETF functions is a mess which ought to be
1401 ;;;; rewritten, and I'm not inclined to mess with it short of that. So
1402 ;;;; I just put this bag on the side of it instead..
1403
1404 ;;; true for symbols FOO which have been assumed to have '(SETF FOO)
1405 ;;; bound to a function
1406 (defvar *setf-assumed-fboundp*)
1407 (!cold-init-forms (setf *setf-assumed-fboundp* (make-hash-table)))
1408 \f
1409 (!defun-from-collected-cold-init-forms !globaldb-cold-init)