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