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