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