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