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.
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.
16 ;;;; This software is part of the SBCL system. See the README file for
17 ;;;; more information.
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.
27 (!begin-collecting-cold-init-forms)
28 #!+sb-show (!cold-init-forms (/show0 "early in globaldb.lisp cold init"))
30 ;;; The DEFVAR for this appears later.
32 (declaim (special *universal-type*))
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.
41 ;;; Why optimize? We want to avoid the fully-general TYPECASE in ordinary
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
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.
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.)
64 #-sb-xc-host ; (SYMBOL-HASH doesn't exist on cross-compilation host.)
67 (let ((rest (rest x)))
68 (and (symbolp (car rest))
70 (logxor (symbol-hash (second x))
74 ;;; Given any non-negative integer, return a prime number >= to it.
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.
85 (declare (type unsigned-byte x))
86 (do ((n (logior x 1) (+ n 2)))
87 ((positive-primep n) n)))
89 ;;;; info classes, info types, and type numbers, part I: what's needed
90 ;;;; not only at compile time but also at run time
92 ;;;; Note: This section is a blast from the past, a little trip down
93 ;;;; memory lane to revisit the weird host/target interactions of the
94 ;;;; CMU CL build process. Because of the way that the cross-compiler
95 ;;;; and target compiler share stuff here, if you change anything in
96 ;;;; here, you'd be well-advised to nuke all your fasl files and
97 ;;;; restart compilation from the very beginning of the bootstrap
100 ;;; At run time, we represent the type of info that we want by a small
101 ;;; non-negative integer.
102 (defconstant type-number-bits 6)
103 (deftype type-number () `(unsigned-byte ,type-number-bits))
105 ;;; Why do we suppress the :COMPILE-TOPLEVEL situation here when we're
106 ;;; running the cross-compiler? The cross-compiler (which was built
107 ;;; from these sources) has its version of these data and functions
108 ;;; defined in the same places we'd be defining into. We're happy with
109 ;;; its version, since it was compiled from the same sources, so
110 ;;; there's no point in overwriting its nice compiled version of this
111 ;;; stuff with our interpreted version. (And any time we're *not*
112 ;;; happy with its version, perhaps because we've been editing the
113 ;;; sources partway through bootstrapping, tch tch, overwriting its
114 ;;; version with our version would be unlikely to help, because that
115 ;;; would make the cross-compiler very confused.)
116 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
118 (defstruct (class-info
119 (:constructor make-class-info (name))
120 #-no-ansi-print-object
121 (:print-object (lambda (x s)
122 (print-unreadable-object (x s :type t)
123 (prin1 (class-info-name x)))))
125 ;; name of this class
126 (name nil :type keyword :read-only t)
127 ;; list of Type-Info structures for each type in this class
128 (types () :type list))
130 ;;; a map from type numbers to TYPE-INFO objects. There is one type
131 ;;; number for each defined CLASS/TYPE pair.
133 ;;; We build its value at build-the-cross-compiler time (with calls to
134 ;;; DEFINE-INFO-TYPE), then generate code to recreate the compile time
135 ;;; value, and arrange for that code to be called in cold load.
136 ;;; KLUDGE: We don't try to reset its value when cross-compiling the
137 ;;; compiler, since that creates too many bootstrapping problems,
138 ;;; instead just reusing the built-in-the-cross-compiler version,
139 ;;; which is theoretically a little bit ugly but pretty safe in
140 ;;; practice because the cross-compiler is as close to the target
141 ;;; compiler as we can make it, i.e. identical in most ways, including
142 ;;; this one. -- WHN 2001-08-19
143 (defvar *info-types*)
144 (declaim (type simple-vector *info-types*))
145 #-sb-xc ; as per KLUDGE note above
146 (eval-when (:compile-toplevel :execute)
148 (make-array (ash 1 type-number-bits) :initial-element nil)))
150 (defstruct (type-info
151 #-no-ansi-print-object
152 (:print-object (lambda (x s)
153 (print-unreadable-object (x s)
156 (class-info-name (type-info-class x))
158 (type-info-number x)))))
160 ;; the name of this type
161 (name (required-argument) :type keyword)
163 (class (required-argument) :type class-info)
164 ;; a number that uniquely identifies this type (and implicitly its class)
165 (number (required-argument) :type type-number)
166 ;; a type specifier which info of this type must satisfy
168 ;; a function called when there is no information of this type
169 (default (lambda () (error "type not defined yet")) :type function))
171 ;;; a map from class names to CLASS-INFO structures
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)))
185 ;;; If NAME is the name of a type in CLASS, then return the TYPE-INFO,
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)
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)
200 (or (gethash class *info-classes*)
201 (error "~S is not a defined info class." class))
202 #+sb-xc (/noshow0 "returning from CLASS-INFO-OR-LOSE")))
203 (declaim (ftype (function (keyword keyword) type-info) type-info-or-lose))
204 (defun type-info-or-lose (class type)
205 #+sb-xc (/noshow0 "entering TYPE-INFO-OR-LOSE, CLASS,TYPE=..")
206 #+sb-xc (/nohexstr class)
207 #+sb-xc (/nohexstr type)
209 (or (find-type-info type (class-info-or-lose class))
210 (error "~S is not a defined info type." type))
211 #+sb-xc (/noshow0 "returning from TYPE-INFO-OR-LOSE")))
215 ;;;; info classes, info types, and type numbers, part II: what's
216 ;;;; needed only at compile time, not at run time
218 ;;; FIXME: Perhaps this stuff (the definition of DEFINE-INFO-CLASS
219 ;;; and the calls to it) could/should go in a separate file,
220 ;;; perhaps info-classes.lisp?
222 (eval-when (:compile-toplevel :execute)
224 ;;; Set up the data structures to support an info class.
226 ;;; comment from CMU CL:
227 ;;; We make sure that the class exists at compile time so that
228 ;;; macros can use it, but we don't actually store the init function
229 ;;; until load time so that we don't break the running compiler.
230 ;;; KLUDGE: I don't think that's the way it is any more, but I haven't
231 ;;; looked into it enough to write a better comment. -- WHN 2001-03-06
232 (#+sb-xc-host defmacro
233 #-sb-xc-host sb!xc:defmacro
234 define-info-class (class)
235 (declare (type keyword class))
237 ;; (We don't need to evaluate this at load time, compile time is
238 ;; enough. There's special logic elsewhere which deals with cold
239 ;; load initialization by inspecting the info class data
240 ;; structures at compile time and generating code to recreate
241 ;; those data structures.)
242 (eval-when (:compile-toplevel :execute)
243 (unless (gethash ,class *info-classes*)
244 (setf (gethash ,class *info-classes*) (make-class-info ,class))))
247 ;;; Find a type number not already in use by looking for a null entry
249 (defun find-unused-type-number ()
250 (or (position nil *info-types*)
251 (error "no more INFO type numbers available")))
253 ;;; a list of forms for initializing the DEFAULT slots of TYPE-INFO
254 ;;; objects, accumulated during compilation and eventually converted
255 ;;; into a function to be called at cold load time after the
256 ;;; appropriate TYPE-INFO objects have been created
258 ;;; Note: This is quite similar to the !COLD-INIT-FORMS machinery, but
259 ;;; we can't conveniently use the ordinary !COLD-INIT-FORMS machinery
260 ;;; here. The problem is that the natural order in which the
261 ;;; default-slot-initialization forms are generated relative to the
262 ;;; order in which the TYPE-INFO-creation forms are generated doesn't
263 ;;; match the relative order in which the forms need to be executed at
265 (defparameter *reversed-type-info-init-forms* nil)
267 ;;; Define a new type of global information for CLASS. TYPE is the
268 ;;; name of the type, DEFAULT is the value for that type when it
269 ;;; hasn't been set, and TYPE-SPEC is a type specifier which values of
270 ;;; the type must satisfy. The default expression is evaluated each
271 ;;; time the information is needed, with NAME bound to the name for
272 ;;; which the information is being looked up.
274 ;;; The main thing we do is determine the type's number. We need to do
275 ;;; this at macroexpansion time, since both the COMPILE and LOAD time
276 ;;; calls to %DEFINE-INFO-TYPE must use the same type number.
277 (#+sb-xc-host defmacro
278 #-sb-xc-host sb!xc:defmacro
279 define-info-type (&key (class (required-argument))
280 (type (required-argument))
281 (type-spec (required-argument))
283 (declare (type keyword class type))
285 (eval-when (:compile-toplevel :execute)
286 ;; At compile time, ensure that the type number exists. It will
287 ;; need to be forced to exist at cold load time, too, but
288 ;; that's not handled here; it's handled by later code which
289 ;; looks at the compile time state and generates code to
290 ;; replicate it at cold load time.
291 (let* ((class-info (class-info-or-lose ',class))
292 (old-type-info (find-type-info ',type class-info)))
293 (unless old-type-info
294 (let* ((new-type-number (find-unused-type-number))
296 (make-type-info :name ',type
298 :number new-type-number)))
299 (setf (aref *info-types* new-type-number) new-type-info)
300 (push new-type-info (class-info-types class-info)))))
301 ;; Arrange for TYPE-INFO-DEFAULT and TYPE-INFO-TYPE to be set
302 ;; at cold load time. (They can't very well be set at
303 ;; cross-compile time, since they differ between the
304 ;; cross-compiler and the target. The DEFAULT slot values
305 ;; differ because they're compiled closures, and the TYPE slot
306 ;; values differ in the use of SB!XC symbols instead of CL
308 (push `(let ((type-info (type-info-or-lose ,',class ,',type)))
309 (setf (type-info-default type-info)
310 ;; FIXME: This code is sort of nasty. It would
311 ;; be cleaner if DEFAULT accepted a real
312 ;; function, instead of accepting a statement
313 ;; which will be turned into a lambda assuming
314 ;; that the argument name is NAME. It might
315 ;; even be more microefficient, too, since many
316 ;; DEFAULTs could be implemented as (CONSTANTLY
317 ;; NIL) instead of full-blown (LAMBDA (X) NIL).
319 (declare (ignorable name))
321 (setf (type-info-type type-info) ',',type-spec))
322 *reversed-type-info-init-forms*))
327 ;;;; generic info environments
329 ;;; Note: the CACHE-NAME slot is deliberately not shared for
330 ;;; bootstrapping reasons. If we access with accessors for the exact
331 ;;; type, then the inline type check will win. If the inline check
332 ;;; didn't win, we would try to use the type system before it was
333 ;;; properly initialized.
334 (defstruct (info-env (:constructor nil)
336 ;; some string describing what is in this environment, for
337 ;; printing/debugging purposes only
338 (name (required-argument) :type string))
339 (def!method print-object ((x info-env) stream)
340 (print-unreadable-object (x stream :type t)
341 (prin1 (info-env-name x) stream)))
343 ;;;; generic interfaces
345 ;;; FIXME: used only in this file, needn't be in runtime
346 (defmacro do-info ((env &key (name (gensym)) (class (gensym)) (type (gensym))
347 (type-number (gensym)) (value (gensym)) known-volatile)
350 "DO-INFO (Env &Key Name Class Type Value) Form*
351 Iterate over all the values stored in the Info-Env Env. Name is bound to
352 the entry's name, Class and Type are bound to the class and type
353 (represented as keywords), and Value is bound to the entry's value."
354 (once-only ((n-env env))
356 (do-volatile-info name class type type-number value n-env body)
357 `(if (typep ,n-env 'volatile-info-env)
358 ,(do-volatile-info name class type type-number value n-env body)
359 ,(do-compact-info name class type type-number value
362 (eval-when (:compile-toplevel :load-toplevel :execute)
364 ;;; Return code to iterate over a compact info environment.
365 (defun do-compact-info (name-var class-var type-var type-number-var value-var
367 (let ((n-index (gensym))
370 (once-only ((n-table `(compact-info-env-table ,n-env))
371 (n-entries-index `(compact-info-env-index ,n-env))
372 (n-entries `(compact-info-env-entries ,n-env))
373 (n-entries-info `(compact-info-env-entries-info ,n-env))
374 (n-info-types '*info-types*))
375 `(dotimes (,n-index (length ,n-table))
376 (declare (type index ,n-index))
378 (let ((,name-var (svref ,n-table ,n-index)))
379 (unless (eql ,name-var 0)
380 (do-anonymous ((,n-type (aref ,n-entries-index ,n-index)
383 (declare (type index ,n-type))
384 ,(once-only ((n-info `(aref ,n-entries-info ,n-type)))
385 `(let ((,type-number-var
386 (logand ,n-info compact-info-entry-type-mask)))
387 ,(once-only ((n-type-info
388 `(svref ,n-info-types
390 `(let ((,type-var (type-info-name ,n-type-info))
391 (,class-var (class-info-name
392 (type-info-class ,n-type-info)))
393 (,value-var (svref ,n-entries ,n-type)))
394 (declare (ignorable ,type-var ,class-var
397 (unless (zerop (logand ,n-info
398 compact-info-entry-last))
399 (return-from ,PUNT))))))))))))))
401 ;;; Return code to iterate over a volatile info environment.
402 (defun do-volatile-info (name-var class-var type-var type-number-var value-var
404 (let ((n-index (gensym)) (n-names (gensym)) (n-types (gensym)))
405 (once-only ((n-table `(volatile-info-env-table ,n-env))
406 (n-info-types '*info-types*))
407 `(dotimes (,n-index (length ,n-table))
408 (declare (type index ,n-index))
409 (do-anonymous ((,n-names (svref ,n-table ,n-index)
412 (let ((,name-var (caar ,n-names)))
413 (declare (ignorable ,name-var))
414 (do-anonymous ((,n-types (cdar ,n-names) (cdr ,n-types)))
416 (let ((,type-number-var (caar ,n-types)))
417 ,(once-only ((n-type `(svref ,n-info-types
419 `(let ((,type-var (type-info-name ,n-type))
420 (,class-var (class-info-name
421 (type-info-class ,n-type)))
422 (,value-var (cdar ,n-types)))
423 (declare (ignorable ,type-var ,class-var ,value-var))
430 ;;;; We use a hash cache to cache name X type => value for the current
431 ;;;; value of *INFO-ENVIRONMENT*. This is in addition to the
432 ;;;; per-environment caching of name => types.
434 ;;; The value of *INFO-ENVIRONMENT* that has cached values.
435 ;;; *INFO-ENVIRONMENT* should never be destructively modified, so if
436 ;;; it is EQ to this, then the cache is valid.
437 (defvar *cached-info-environment*)
439 (setf *cached-info-environment* nil))
441 ;;; the hash function used for the INFO cache
442 #!-sb-fluid (declaim (inline info-cache-hash))
443 (defun info-cache-hash (name type)
446 (logxor (globaldb-sxhashoid name)
447 (ash (the fixnum type) 7)))
451 (/show0 "before initialization of INFO hash cache"))
452 (define-hash-cache info ((name eq) (type eq))
454 :hash-function info-cache-hash
456 :default (values nil :empty)
457 :init-wrapper !cold-init-forms)
459 (/show0 "clearing INFO hash cache")
461 (/show0 "done clearing INFO hash cache"))
463 ;;; If the info cache is invalid, then clear it.
464 #!-sb-fluid (declaim (inline clear-invalid-info-cache))
465 (defun clear-invalid-info-cache ()
466 ;; Unless the cache is valid..
467 (unless (eq *info-environment* *cached-info-environment*)
468 (;; In the target Lisp, this should be done without interrupts,
469 ;; but in the host Lisp when cross-compiling, we don't need to
470 ;; sweat it, since no affected-by-GC hashes should be used when
471 ;; running under the host Lisp (since that's non-portable) and
472 ;; since only one thread should be used when running under the
473 ;; host Lisp (because multiple threads are non-portable too).
474 #-sb-xc-host without-interrupts
477 (setq *cached-info-environment* *info-environment*))))
479 ;;;; compact info environments
481 ;;; The upper limit on the size of the ENTRIES vector in a COMPACT-INFO-ENV.
482 (defconstant compact-info-env-entries-bits 16)
483 (deftype compact-info-entries-index () `(unsigned-byte ,compact-info-env-entries-bits))
485 ;;; the type of the values in COMPACT-INFO-ENTRIES-INFO
486 (deftype compact-info-entry () `(unsigned-byte ,(1+ type-number-bits)))
488 ;;; This is an open hashtable with rehashing. Since modification is
489 ;;; not allowed, we don't have to worry about deleted entries. We
490 ;;; indirect through a parallel vector to find the index in the
491 ;;; ENTRIES at which the entries for a given name starts.
492 (defstruct (compact-info-env (:include info-env)
493 #-sb-xc-host (:pure :substructure)
495 ;; If this value is EQ to the name we want to look up, then the
496 ;; cache hit function can be called instead of the lookup function.
498 ;; The index in ENTRIES for the CACHE-NAME, or NIL if that name has
500 (cache-index nil :type (or compact-info-entries-index null))
501 ;; hashtable of the names in this environment. If a bucket is
503 (table (required-argument) :type simple-vector)
504 ;; an indirection vector parallel to TABLE, translating indices in
505 ;; TABLE to the start of the ENTRIES for that name. Unused entries
507 (index (required-argument)
508 :type (simple-array compact-info-entries-index (*)))
509 ;; a vector contining in contiguous ranges the values of for all the
510 ;; types of info for each name.
511 (entries (required-argument) :type simple-vector)
512 ;; a vector parallel to ENTRIES, indicating the type number for the
513 ;; value stored in that location and whether this location is the
514 ;; last type of info stored for this name. The type number is in the
515 ;; low TYPE-NUMBER-BITS bits, and the next bit is set if this is the
517 (entries-info (required-argument)
518 :type (simple-array compact-info-entry (*))))
520 (defconstant compact-info-entry-type-mask (ldb (byte type-number-bits 0) -1))
521 (defconstant compact-info-entry-last (ash 1 type-number-bits))
523 ;;; Return the value of the type corresponding to NUMBER for the
524 ;;; currently cached name in ENV.
525 #!-sb-fluid (declaim (inline compact-info-cache-hit))
526 (defun compact-info-cache-hit (env number)
527 (declare (type compact-info-env env) (type type-number number))
528 (let ((entries-info (compact-info-env-entries-info env))
529 (index (compact-info-env-cache-index env)))
531 (do ((index index (1+ index)))
533 (declare (type index index))
534 (let ((info (aref entries-info index)))
535 (when (= (logand info compact-info-entry-type-mask) number)
536 (return (values (svref (compact-info-env-entries env) index)
538 (unless (zerop (logand compact-info-entry-last info))
539 (return (values nil nil)))))
542 ;;; Encache NAME in the compact environment ENV. HASH is the
543 ;;; GLOBALDB-SXHASHOID of NAME.
544 (defun compact-info-lookup (env name hash)
545 (declare (type compact-info-env env) (type index hash))
546 (let* ((table (compact-info-env-table env))
549 (hash2 (- len-2 (rem hash len-2))))
550 (declare (type index len-2 hash2))
551 (macrolet ((lookup (test)
552 `(do ((probe (rem hash len)
553 (let ((new (+ probe hash2)))
554 (declare (type index new))
555 ;; same as (MOD NEW LEN), but faster.
557 (the index (- new len))
560 (let ((entry (svref table probe)))
563 (when (,test entry name)
564 (return (aref (compact-info-env-index env)
566 (setf (compact-info-env-cache-index env)
570 (setf (compact-info-env-cache-name env) name)))
574 ;;; the exact density (modulo rounding) of the hashtable in a compact
575 ;;; info environment in names/bucket
576 (defconstant compact-info-environment-density 65)
578 ;;; Return a new compact info environment that holds the same
579 ;;; information as ENV.
580 (defun compact-info-environment (env &key (name (info-env-name env)))
584 (/show0 "before COLLECT in COMPACT-INFO-ENVIRONMENT")
586 ;; Iterate over the environment once to find out how many names
587 ;; and entries it has, then build the result. This code assumes
588 ;; that all the entries for a name well be iterated over
589 ;; contiguously, which holds true for the implementation of
590 ;; iteration over both kinds of environments.
593 (/show0 "at head of COLLECT in COMPACT-INFO-ENVIRONMENT")
595 (do-info (env :name name :type-number num :value value)
596 (/noshow0 "at head of DO-INFO in COMPACT-INFO-ENVIRONMENT")
597 (unless (eq name prev-name)
598 (/noshow0 "not (EQ NAME PREV-NAME) case")
600 (unless (eql prev-name 0)
601 (names (cons prev-name types)))
602 (setq prev-name name)
605 (push (cons num value) types))
606 (unless (eql prev-name 0)
607 (/show0 "not (EQL PREV-NAME 0) case")
608 (names (cons prev-name types))))
610 ;; Now that we know how big the environment is, we can build
611 ;; a table to represent it.
613 ;; When building the table, we sort the entries by pointer
614 ;; comparison in an attempt to preserve any VM locality present
615 ;; in the original load order, rather than randomizing with the
616 ;; original hash function.
617 (/show0 "about to make/sort vectors in COMPACT-INFO-ENVIRONMENT")
618 (let* ((table-size (primify
619 (+ (truncate (* name-count 100)
620 compact-info-environment-density)
622 (table (make-array table-size :initial-element 0))
623 (index (make-array table-size
624 :element-type 'compact-info-entries-index))
625 (entries (make-array entry-count))
626 (entries-info (make-array entry-count
627 :element-type 'compact-info-entry))
628 (sorted (sort (names)
630 ;; (This MAKE-FIXNUM hack implements
631 ;; pointer comparison, as explained above.)
632 #-sb-xc-host (lambda (x y)
633 (< (%primitive make-fixnum x)
634 (%primitive make-fixnum y))))))
635 (/show0 "done making/sorting vectors in COMPACT-INFO-ENVIRONMENT")
636 (let ((entries-idx 0))
637 (dolist (types sorted)
638 (let* ((name (first types))
639 (hash (globaldb-sxhashoid name))
640 (len-2 (- table-size 2))
641 (hash2 (- len-2 (rem hash len-2))))
642 (do ((probe (rem hash table-size)
643 (rem (+ probe hash2) table-size)))
645 (let ((entry (svref table probe)))
647 (setf (svref table probe) name)
648 (setf (aref index probe) entries-idx)
650 (aver (not (equal entry name))))))
652 (unless (zerop entries-idx)
653 (setf (aref entries-info (1- entries-idx))
654 (logior (aref entries-info (1- entries-idx))
655 compact-info-entry-last)))
657 (loop for (num . value) in (rest types) do
658 (setf (aref entries-info entries-idx) num)
659 (setf (aref entries entries-idx) value)
661 (/show0 "done w/ DOLIST (TYPES SORTED) in COMPACT-INFO-ENVIRONMENT")
663 (unless (zerop entry-count)
664 (/show0 "nonZEROP ENTRY-COUNT")
665 (setf (aref entries-info (1- entry-count))
666 (logior (aref entries-info (1- entry-count))
667 compact-info-entry-last)))
669 (/show0 "falling through to MAKE-COMPACT-INFO-ENV")
670 (make-compact-info-env :name name
674 :entries-info entries-info))))))
676 ;;;; volatile environments
678 ;;; This is a closed hashtable, with the bucket being computed by
679 ;;; taking the GLOBALDB-SXHASHOID of the NAME modulo the table size.
680 (defstruct (volatile-info-env (:include info-env)
682 ;; If this value is EQ to the name we want to look up, then the
683 ;; cache hit function can be called instead of the lookup function.
685 ;; the alist translating type numbers to values for the currently
687 (cache-types nil :type list)
688 ;; vector of alists of alists of the form:
689 ;; ((Name . ((Type-Number . Value) ...) ...)
690 (table (required-argument) :type simple-vector)
691 ;; the number of distinct names currently in this table. Each name
692 ;; may have multiple entries, since there can be many types of info.
693 (count 0 :type index)
694 ;; the number of names at which we should grow the table and rehash
695 (threshold 0 :type index))
697 ;;; Just like COMPACT-INFO-CACHE-HIT, only do it on a volatile environment.
698 #!-sb-fluid (declaim (inline volatile-info-cache-hit))
699 (defun volatile-info-cache-hit (env number)
700 (declare (type volatile-info-env env) (type type-number number))
701 (dolist (type (volatile-info-env-cache-types env) (values nil nil))
702 (when (eql (car type) number)
703 (return (values (cdr type) t)))))
705 ;;; Just like COMPACT-INFO-LOOKUP, only do it on a volatile environment.
706 (defun volatile-info-lookup (env name hash)
707 (declare (type volatile-info-env env) (type index hash))
708 (let ((table (volatile-info-env-table env)))
709 (macrolet ((lookup (test)
710 `(dolist (entry (svref table (mod hash (length table))) ())
711 (when (,test (car entry) name)
712 (return (cdr entry))))))
713 (setf (volatile-info-env-cache-types env)
717 (setf (volatile-info-env-cache-name env) name)))
721 ;;; Given a volatile environment Env, bind Table-Var the environment's table
722 ;;; and Index-Var to the index of Name's bucket in the table. We also flush
723 ;;; the cache so that things will be consistent if body modifies something.
724 (eval-when (:compile-toplevel :execute)
725 (#+sb-xc-host cl:defmacro
726 #-sb-xc-host sb!xc:defmacro
727 with-info-bucket ((table-var index-var name env) &body body)
728 (once-only ((n-name name)
731 (setf (volatile-info-env-cache-name ,n-env) 0)
732 (let* ((,table-var (volatile-info-env-table ,n-env))
733 (,index-var (mod (globaldb-sxhashoid ,n-name)
734 (length ,table-var))))
737 ;;; Get the info environment that we use for write/modification operations.
738 ;;; This is always the first environment in the list, and must be a
739 ;;; VOLATILE-INFO-ENV.
740 #!-sb-fluid (declaim (inline get-write-info-env))
741 (defun get-write-info-env (&optional (env-list *info-environment*))
742 (let ((env (car env-list)))
744 (error "no info environment?"))
745 (unless (typep env 'volatile-info-env)
746 (error "cannot modify this environment: ~S" env))
747 (the volatile-info-env env)))
749 ;;; If Name is already present in the table, then just create or
750 ;;; modify the specified type. Otherwise, add the new name and type,
751 ;;; checking for rehashing.
753 ;;; We rehash by making a new larger environment, copying all of the
754 ;;; entries into it, then clobbering the old environment with the new
755 ;;; environment's table. We clear the old table to prevent it from
756 ;;; holding onto garbage if it is statically allocated.
758 ;;; We return the new value so that this can be conveniently used in a
760 (defun set-info-value (name0 type new-value
761 &optional (env (get-write-info-env)))
762 (declare (type type-number type) (type volatile-info-env env)
764 (let ((name (uncross name0)))
766 (error "0 is not a legal INFO name."))
767 ;; We don't enter the value in the cache because we don't know that this
768 ;; info-environment is part of *cached-info-environment*.
769 (info-cache-enter name type nil :empty)
770 (with-info-bucket (table index name env)
771 (let ((types (if (symbolp name)
772 (assoc name (svref table index) :test #'eq)
773 (assoc name (svref table index) :test #'equal))))
776 (let ((value (assoc type (cdr types))))
778 (setf (cdr value) new-value)
779 (push (cons type new-value) (cdr types)))))
781 (push (cons name (list (cons type new-value)))
784 (let ((count (incf (volatile-info-env-count env))))
785 (when (>= count (volatile-info-env-threshold env))
786 (let ((new (make-info-environment :size (* count 2))))
787 (do-info (env :name entry-name :type-number entry-num
788 :value entry-val :known-volatile t)
789 (set-info-value entry-name entry-num entry-val new))
790 (fill (volatile-info-env-table env) nil)
791 (setf (volatile-info-env-table env)
792 (volatile-info-env-table new))
793 (setf (volatile-info-env-threshold env)
794 (volatile-info-env-threshold new)))))))))
797 ;;; FIXME: It should be possible to eliminate the hairy compiler macros below
798 ;;; by declaring INFO and (SETF INFO) inline and making a simple compiler macro
799 ;;; for TYPE-INFO-OR-LOSE. (If we didn't worry about efficiency of the
800 ;;; cross-compiler, we could even do it by just making TYPE-INFO-OR-LOSE
803 ;;; INFO is the standard way to access the database. It's settable.
805 ;;; Return the information of the specified TYPE and CLASS for NAME.
806 ;;; The second value returned is true if there is any such information
807 ;;; recorded. If there is no information, the first value returned is
808 ;;; the default and the second value returned is NIL.
809 (defun info (class type name &optional (env-list nil env-list-p))
810 ;; FIXME: At some point check systematically to make sure that the
811 ;; system doesn't do any full calls to INFO or (SETF INFO), or at
812 ;; least none in any inner loops.
813 (let ((info (type-info-or-lose class type)))
815 (get-info-value name (type-info-number info) env-list)
816 (get-info-value name (type-info-number info)))))
818 (define-compiler-macro info
819 (&whole whole class type name &optional (env-list nil env-list-p))
820 ;; Constant CLASS and TYPE is an overwhelmingly common special case,
821 ;; and we can implement it much more efficiently than the general case.
822 (if (and (constantp class) (constantp type))
823 (let ((info (type-info-or-lose class type))
824 (value (gensym "VALUE"))
825 (foundp (gensym "FOUNDP")))
826 `(multiple-value-bind (,value ,foundp)
827 (get-info-value ,name
828 ,(type-info-number info)
829 ,@(when env-list-p `(,env-list)))
830 (declare (type ,(type-info-type info) ,value))
831 (values ,value ,foundp)))
833 (defun (setf info) (new-value
837 &optional (env-list nil env-list-p))
838 (let* ((info (type-info-or-lose class type))
839 (tin (type-info-number info)))
844 (get-write-info-env env-list))
849 ;;; FIXME: We'd like to do this, but Python doesn't support
850 ;;; compiler macros and it's hard to change it so that it does.
851 ;;; It might make more sense to just convert INFO :FOO :BAR into
852 ;;; an ordinary function, so that instead of calling INFO :FOO :BAR
853 ;;; you call e.g. INFO%FOO%BAR. Then dynamic linking could be handled
854 ;;; by the ordinary Lisp mechanisms and we wouldn't have to maintain
859 (define-compiler-macro (setf info) (&whole whole
864 &optional (env-list nil env-list-p))
865 ;; Constant CLASS and TYPE is an overwhelmingly common special case, and we
866 ;; can resolve it much more efficiently than the general case.
867 (if (and (constantp class) (constantp type))
868 (let* ((info (type-info-or-lose class type))
869 (tin (type-info-number info)))
871 `(set-info-value ,name
874 (get-write-info-env ,env-list))
875 `(set-info-value ,name
881 ;;; the maximum density of the hashtable in a volatile env (in
884 ;;; FIXME: actually seems to be measured in percent, should be
885 ;;; converted to be measured in names/bucket
886 (defconstant volatile-info-environment-density 50)
888 ;;; Make a new volatile environment of the specified size.
889 (defun make-info-environment (&key (size 42) (name "Unknown"))
890 (declare (type (integer 1) size))
891 (let ((table-size (primify (truncate (* size 100)
892 volatile-info-environment-density))))
893 (make-volatile-info-env :name name
894 :table (make-array table-size :initial-element nil)
897 ;;; Clear the information of the specified TYPE and CLASS for NAME in
898 ;;; the current environment, allowing any inherited info to become
899 ;;; visible. We return true if there was any info.
900 (defun clear-info (class type name)
902 (let ((info (type-info-or-lose class type)))
903 (clear-info-value name (type-info-number info))))
905 (define-compiler-macro clear-info (&whole whole class type name)
906 ;; Constant CLASS and TYPE is an overwhelmingly common special case, and
907 ;; we can resolve it much more efficiently than the general case.
908 (if (and (keywordp class) (keywordp type))
909 (let ((info (type-info-or-lose class type)))
910 `(clear-info-value ,name ,(type-info-number info)))
912 (defun clear-info-value (name type)
913 (declare (type type-number type) (inline assoc))
914 (clear-invalid-info-cache)
915 (info-cache-enter name type nil :empty)
916 (with-info-bucket (table index name (get-write-info-env))
917 (let ((types (assoc name (svref table index) :test #'equal)))
919 (assoc type (cdr types)))
921 (delete type (cdr types) :key #'car))
924 ;;;; *INFO-ENVIRONMENT*
926 ;;; We do info access relative to the current *INFO-ENVIRONMENT*, a
927 ;;; list of INFO-ENVIRONMENT structures.
928 (defvar *info-environment*)
929 (declaim (type list *info-environment*))
931 (setq *info-environment*
932 (list (make-info-environment :name "initial global")))
933 (/show0 "done setting *INFO-ENVIRONMENT*"))
934 ;;; FIXME: should perhaps be *INFO-ENV-LIST*. And rename
935 ;;; all FOO-INFO-ENVIRONMENT-BAR stuff to FOO-INFO-ENV-BAR.
939 ;;; Check whether the name and type is in our cache, if so return it.
940 ;;; Otherwise, search for the value and encache it.
942 ;;; Return the value from the first environment which has it defined,
943 ;;; or return the default if none does. We have a cache for the last
944 ;;; name looked up in each environment. We don't compute the hash
945 ;;; until the first time the cache misses. When the cache does miss,
946 ;;; we invalidate it before calling the lookup routine to eliminate
947 ;;; the possibility of the cache being partially updated if the lookup
949 (defun get-info-value (name0 type &optional (env-list nil env-list-p))
950 (declare (type type-number type))
951 ;; sanity check: If we have screwed up initialization somehow, then
952 ;; *INFO-TYPES* could still be uninitialized at the time we try to
953 ;; get an info value, and then we'd be out of luck. (This happened,
954 ;; and was confusing to debug, when rewriting EVAL-WHEN in
956 (aver (aref *info-types* type))
957 (let ((name (uncross name0)))
958 (flet ((lookup-ignoring-global-cache (env-list)
960 (dolist (env env-list
961 (multiple-value-bind (val winp)
962 (funcall (type-info-default
963 (svref *info-types* type))
966 (macrolet ((frob (lookup cache slot)
968 (unless (eq name (,slot env))
970 (setq hash (globaldb-sxhashoid name)))
972 (,lookup env name hash))
973 (multiple-value-bind (value winp)
975 (when winp (return (values value t)))))))
977 (volatile-info-env (frob
979 volatile-info-cache-hit
980 volatile-info-env-cache-name))
981 (compact-info-env (frob
983 compact-info-cache-hit
984 compact-info-env-cache-name))))))))
986 (lookup-ignoring-global-cache env-list))
988 (clear-invalid-info-cache)
989 (multiple-value-bind (val winp) (info-cache-lookup name type)
991 (multiple-value-bind (val winp)
992 (lookup-ignoring-global-cache *info-environment*)
993 (info-cache-enter name type val winp)
995 (values val winp))))))))
997 ;;;; definitions for function information
999 (define-info-class :function)
1001 ;;; the kind of functional object being described. If null, NAME isn't
1002 ;;; a known functional object.
1006 :type-spec (member nil :function :macro :special-form)
1007 ;; I'm a little confused what the correct behavior of this default
1008 ;; is. It's not clear how to generalize the FBOUNDP expression to
1009 ;; the cross-compiler. As far as I can tell, NIL is a safe default
1010 ;; -- it might keep the compiler from making some valid
1011 ;; optimization, but it shouldn't produce incorrect code. -- WHN
1015 #-sb-xc-host (if (fboundp name) :function nil))
1017 ;;; The type specifier for this function.
1022 ;; Again (as in DEFINE-INFO-TYPE :CLASS :FUNCTION :TYPE :KIND) it's
1023 ;; not clear how to generalize the FBOUNDP expression to the
1024 ;; cross-compiler. -- WHN 19990330
1026 #+sb-xc-host (specifier-type 'function)
1027 #-sb-xc-host (if (fboundp name)
1028 (extract-fun-type (fdefinition name))
1029 (specifier-type 'function)))
1031 ;;; the ASSUMED-TYPE for this function, if we have to infer the type
1032 ;;; due to not having a declaration or definition
1036 ;; FIXME: The type-spec really should be
1037 ;; (or approximate-fun-type null)).
1038 ;; It was changed to T as a hopefully-temporary hack while getting
1039 ;; cold init problems untangled.
1042 ;;; where this information came from:
1043 ;;; :ASSUMED = from uses of the object
1044 ;;; :DEFINED = from examination of the definition
1045 ;;; :DECLARED = from a declaration
1046 ;;; :DEFINED trumps :ASSUMED, and :DECLARED trumps :DEFINED.
1047 ;;; :DEFINED and :ASSUMED are useful for issuing compile-time warnings,
1048 ;;; and :DECLARED is useful for ANSIly specializing code which
1049 ;;; implements the function, or which uses the function's return values.
1053 :type-spec (member :declared :assumed :defined)
1055 ;; Again (as in DEFINE-INFO-TYPE :CLASS :FUNCTION :TYPE :KIND) it's
1056 ;; not clear how to generalize the FBOUNDP expression to the
1057 ;; cross-compiler. -- WHN 19990606
1058 #+sb-xc-host :assumed
1059 #-sb-xc-host (if (fboundp name) :defined :assumed))
1061 ;;; something which can be decoded into the inline expansion of the
1062 ;;; function, or NIL if there is none
1064 ;;; To inline a function, we want a lambda expression, e.g.
1065 ;;; '(LAMBDA (X) (+ X 1)). That can be encoded here in one of two
1067 ;;; * The value in INFO can be the lambda expression itself, e.g.
1068 ;;; (SETF (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR 'FOO)
1069 ;;; '(LAMBDA (X) (+ X 1)))
1070 ;;; This is the ordinary way, the natural way of representing e.g.
1071 ;;; (DECLAIM (INLINE FOO))
1072 ;;; (DEFUN FOO (X) (+ X 1))
1073 ;;; * The value in INFO can be a closure which returns the lambda
1074 ;;; expression, e.g.
1075 ;;; (SETF (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR 'BAR-LEFT-CHILD)
1077 ;;; '(LAMBDA (BAR) (BAR-REF BAR 3))))
1078 ;;; This twisty way of storing values is supported in order to
1079 ;;; allow structure slot accessors, and perhaps later other
1080 ;;; stereotyped functions, to be represented compactly.
1083 :type :inline-expansion-designator
1084 :type-spec (or list function)
1087 ;;; This specifies whether this function may be expanded inline. If
1088 ;;; null, we don't care.
1095 ;;; a macro-like function which transforms a call to this function
1096 ;;; into some other Lisp form. This expansion is inhibited if inline
1097 ;;; expansion is inhibited
1100 :type :source-transform
1101 :type-spec (or function null))
1103 ;;; the macroexpansion function for this macro
1106 :type :macro-function
1107 :type-spec (or function null)
1110 ;;; the compiler-macroexpansion function for this macro
1113 :type :compiler-macro-function
1114 :type-spec (or function null)
1117 ;;; a function which converts this special form into IR1
1121 :type-spec (or function null))
1123 ;;; a function which gets a chance to do stuff to the IR1 for any call
1124 ;;; to this function.
1127 :type :ir1-transform
1128 :type-spec (or function null))
1130 ;;; If a function is "known" to the compiler, then this is a
1131 ;;; FUNCTION-INFO structure containing the info used to special-case
1136 :type-spec (or function-info null)
1141 :type :documentation
1142 :type-spec (or string null)
1151 ;;;; definitions for other miscellaneous information
1153 (define-info-class :variable)
1155 ;;; the kind of variable-like thing described
1159 :type-spec (member :special :constant :global :alien)
1160 :default (if (symbol-self-evaluating-p name)
1164 ;;; the declared type for this variable
1169 :default *universal-type*)
1171 ;;; where this type and kind information came from
1175 :type-spec (member :declared :assumed :defined)
1178 ;;; the Lisp object which is the value of this constant, if known
1181 :type :constant-value
1183 ;; CMU CL used to return two values for (INFO :VARIABLE :CONSTANT-VALUE ..).
1184 ;; Now we don't: it was the last remaining multiple-value return from
1185 ;; the INFO system, and bringing it down to one value lets us simplify
1186 ;; things, especially simplifying the declaration of return types.
1187 ;; Software which used to check the second value (for "is it defined
1188 ;; as a constant?") should check (EQL (INFO :VARIABLE :KIND ..) :CONSTANT)
1190 :default (if (symbol-self-evaluating-p name)
1192 (error "internal error: constant lookup of nonconstant ~S"
1198 :type-spec (or heap-alien-info null)
1203 :type :documentation
1204 :type-spec (or string null)
1207 (define-info-class :type)
1209 ;;; the kind of type described. We return :INSTANCE for standard types
1210 ;;; that are implemented as structures.
1214 :type-spec (member :primitive :defined :instance nil)
1217 ;;; the expander function for a defined type
1221 :type-spec (or function null)
1226 :type :documentation
1227 :type-spec (or string null))
1229 ;;; function that parses type specifiers into CTYPE structures
1233 :type-spec (or function null)
1236 ;;; If true, then the type coresponding to this name. Note that if
1237 ;;; this is a built-in class with a translation, then this is the
1238 ;;; translation, not the class object. This info type keeps track of
1239 ;;; various atomic types (NIL etc.) and also serves as a cache to
1240 ;;; ensure that common standard types (atomic and otherwise) are only
1245 :type-spec (or ctype null)
1248 ;;; If this is a class name, then the value is a cons (NAME . CLASS),
1249 ;;; where CLASS may be null if the class hasn't been defined yet. Note
1250 ;;; that for built-in classes, the kind may be :PRIMITIVE and not
1251 ;;; :INSTANCE. The the name is in the cons so that we can signal a
1252 ;;; meaningful error if we only have the cons.
1256 :type-spec (or sb!kernel::class-cell null)
1259 ;;; layout for this type being used by the compiler
1262 :type :compiler-layout
1263 :type-spec (or layout null)
1264 :default (let ((class (sb!xc:find-class name nil)))
1265 (when class (class-layout class))))
1267 (define-info-class :typed-structure)
1269 :class :typed-structure
1274 (define-info-class :declaration)
1280 (define-info-class :alien-type)
1284 :type-spec (member :primitive :defined :unknown)
1289 :type-spec (or function null)
1294 :type-spec (or alien-type null)
1299 :type-spec (or alien-type null)
1304 :type-spec (or alien-type null)
1309 :type-spec (or alien-type null)
1312 (define-info-class :setf)
1317 :type-spec (or symbol null)
1322 :type :documentation
1323 :type-spec (or string null)
1329 :type-spec (or function null)
1332 ;;; This is used for storing miscellaneous documentation types. The
1333 ;;; stuff is an alist translating documentation kinds to values.
1334 (define-info-class :random-documentation)
1336 :class :random-documentation
1341 #!-sb-fluid (declaim (freeze-type info-env))
1343 ;;; Now that we have finished initializing *INFO-CLASSES* and
1344 ;;; *INFO-TYPES* (at compile time), generate code to set them at cold
1345 ;;; load time to the same state they have currently.
1347 (/show0 "beginning *INFO-CLASSES* init, calling MAKE-HASH-TABLE")
1348 (setf *info-classes*
1349 (make-hash-table :size #.(hash-table-size *info-classes*)))
1350 (/show0 "done with MAKE-HASH-TABLE in *INFO-CLASSES* init")
1351 (dolist (class-info-name '#.(let ((result nil))
1352 (maphash (lambda (key value)
1353 (declare (ignore value))
1357 (let ((class-info (make-class-info class-info-name)))
1358 (setf (gethash class-info-name *info-classes*)
1360 (/show0 "done with *INFO-CLASSES* initialization")
1361 (/show0 "beginning *INFO-TYPES* initialization")
1365 (/show0 "in LAMBDA (X), X=..")
1368 (let* ((class-info (class-info-or-lose (second x)))
1369 (type-info (make-type-info :name (first x)
1373 (/show0 "got CLASS-INFO in LAMBDA (X)")
1374 (push type-info (class-info-types class-info))
1379 (list (type-info-name info-type)
1380 (class-info-name (type-info-class info-type))
1381 (type-info-number info-type)
1382 (type-info-type info-type))))
1384 (/show0 "done with *INFO-TYPES* initialization"))
1386 ;;; At cold load time, after the INFO-TYPE objects have been created,
1387 ;;; we can set their DEFAULT and TYPE slots.
1390 ,@(reverse *reversed-type-info-init-forms*))))
1393 ;;;; a hack for detecting
1394 ;;;; (DEFUN FOO (X Y)
1396 ;;;; (SETF (BAR A FFH) 12) ; compiles to a call to #'(SETF BAR)
1398 ;;;; (DEFSETF BAR SET-BAR) ; can't influence previous compilation
1400 ;;;; KLUDGE: Arguably it should be another class/type combination in
1401 ;;;; the globaldb. However, IMHO the whole globaldb/fdefinition
1402 ;;;; treatment of SETF functions is a mess which ought to be
1403 ;;;; rewritten, and I'm not inclined to mess with it short of that. So
1404 ;;;; I just put this bag on the side of it instead..
1406 ;;; true for symbols FOO which have been assumed to have '(SETF FOO)
1407 ;;; bound to a function
1408 (defvar *setf-assumed-fboundp*)
1409 (!cold-init-forms (setf *setf-assumed-fboundp* (make-hash-table)))
1411 (!defun-from-collected-cold-init-forms !globaldb-cold-init)