Fix comment about *code-coverage-info*.
[sbcl.git] / src / compiler / info-vector.lisp
blob2dba2f678bee163bd6c3588ed76e675bbd79f048
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!C")
12 ;;;; This file implements abstract types which map globaldb Info-Number/Name
13 ;;;; pairs to data values. The database itself is defined in 'globaldb'.
15 ;;; Quasi-lockfree concurrent hashtable
16 ;;; ===================================
18 ;; References:
19 ;; http://trac.clozure.com/ccl/wiki/Internals/LockFreeHashTables
20 ;; https://github.com/boundary/high-scale-lib/blob/master/src/main/java/org/cliffc/high_scale_lib/NonBlockingHashMap.java
22 ;; The basic hashable is a lightweight one using prime number sizing strategy,
23 ;; and a secondary hash for re-probing that is not necessarily co-prime with
24 ;; the table size (as it would be, ideally), and no support for deletion.
26 ;; The lock-free logic differs from each of the preceding reference algorithms.
27 ;; The Java algorithm is truly lock-free: death of any thread will never impede
28 ;; progress in other threads. The CCL algorithm is only quasi-lock-free, as is
29 ;; ours. Were a rehashing thread to terminate abnormally while holding the
30 ;; rehash mutex, all other threads will likely starve at some point.
32 ;; Unlike the CCL algorithm, we allow reading/writing concurrently with rehash.
33 ;; Most operations will never notice that the rehasher has acquired a mutex,
34 ;; because the only case where writers compete for the mutex is in trying
35 ;; to claim a new cell in a storage vector that is being transported, and where
36 ;; the rehasher has already visited the desired cell. Since the rehasher will
37 ;; never re-visit a cell, it disallows updates that it could not perceive.
38 ;; The Java algorithm has additional complexity imposed upon it by the
39 ;; feature that rehashing work is parceling out fairly to threads,
40 ;; whereas we avoid that by having one rehasher transport all cells.
42 ;; A marker for cells that may be claimed by a writer
43 (defconstant +empty-key+ 0)
44 ;; A marker for cells that may NOT be claimed by a writer.
45 (defconstant +unavailable-key+ -1)
47 ;; We have a 24-bit cell index. This is generous,
48 ;; allowing for ~5 million Names assuming 60% load factor.
49 (deftype info-cell-index () `(unsigned-byte 24))
51 ;; An INFO-STORAGE is a vector of a 3-element header followed by all keys
52 ;; and then all values. This way we can simply use FILL to set all keys to 0
53 ;; and all values to NIL.
54 (defconstant +info-keys-offset+ 3)
55 (defstruct (info-storage (:type vector) (:constructor nil) (:copier nil))
56 ;; Capacity refers to pairs of vector elements. The lower bound is to
57 ;; try (in vain) to elide a check for division by 0 in the secondary hash.
58 ;; The upper bound is harmlessly wrong by a factor of 2 because
59 ;; INFO-CELL-INDEX should be the max physical cell that can be addressed.
60 ;; No problem - you'll run out of memory before hitting the edge anyway.
61 (capacity 0 :type (and (integer 3 *) info-cell-index)) ; immutable
62 ;; Make a new storage if count exceeds this
63 (threshold 0 :type info-cell-index) ; immutable
64 (next nil :type simple-vector) ; Pending INFO-STORAGE during rehash
65 ;; keys ... data ...
68 (declaim (ftype (sfunction (unsigned-byte)
69 (unsigned-byte #.sb!vm:n-positive-fixnum-bits))
70 primify))
71 (defun make-info-storage (n-cells-min &optional (load-factor .7))
72 ;; If you ask for 40 entries at 50% load, you get (PRIMIFY 80) entries.
73 (let* ((n-cells (primify (ceiling n-cells-min load-factor)))
74 (a (make-array (+ +info-keys-offset+ (* 2 n-cells))))
75 (end (+ +info-keys-offset+ n-cells)))
76 (setf (info-storage-capacity a) n-cells
77 ;; The threshold should be approximately the same as
78 ;; the number you asked for in the first place.
79 (info-storage-threshold a) (floor (* n-cells load-factor))
80 (info-storage-next a) #()) ; type-correct initial value
81 (fill a +empty-key+ :start +info-keys-offset+ :end end)
82 (fill a nil :start end)
83 a))
85 (declaim (ftype (sfunction (t) (unsigned-byte #.sb!vm:n-positive-fixnum-bits))
86 globaldb-sxhashoid))
88 ;;; In the host, we use the host's sxhash, but ensure that the result
89 ;;; is a target fixnum. The definition for the target occurs later, as it
90 ;;; relies on MIX, which is inlined.
91 #+sb-xc-host
92 (defun globaldb-sxhashoid (name)
93 (logand (sxhash name) sb!xc:most-positive-fixnum))
95 (defstruct (info-hashtable (:conc-name info-env-))
96 (storage (make-info-storage 30) :type simple-vector)
97 (comparator #'equal :type function)
98 (hash-function #'globaldb-sxhashoid :type function)
99 (mutex (sb!thread:make-mutex))
100 ;; COUNT is always at *least* as large as the key count.
101 ;; If no insertions are in progress, it is exactly right.
102 (count 0 :type word))
104 (defmethod print-object ((self info-hashtable) stream)
105 (declare (stream stream))
106 (print-unreadable-object (self stream :type t :identity t)
107 (format stream "~D/~D entr~:@P" (info-env-count self)
108 (info-storage-capacity (info-env-storage self)))))
110 ;; We can't assume that the host lisp supports a CAS operation for info-gethash
111 ;; or info-puthash, but that's fine because there's only one thread.
112 (defmacro info-cas (storage index oldval newval)
113 #+sb-xc-host
114 `(xc-compare-and-swap-svref ,storage ,index ,oldval ,newval)
115 #-sb-xc-host
116 `(cas (svref ,storage ,index) ,oldval ,newval))
118 ;; Similarly we need a way to atomically adjust the hashtable count.
119 (declaim (inline info-env-adjust-count))
120 (defun info-env-adjust-count (table delta)
121 #+sb-xc-host
122 (prog1 (info-env-count table) (incf (info-env-count table) delta))
123 #-sb-xc-host
124 ;; Inform the compiler that this is not possibly a bignum,
125 ;; since it's true upper bound is the info storage threshold.
126 (truly-the info-cell-index (atomic-incf (info-env-count table) delta)))
128 (declaim (inline make-info-forwarding-pointer
129 info-forwarding-pointer-target
130 info-value-moved-p)
131 (ftype (sfunction (t) simple-vector) info-env-rehash)
132 (ftype (sfunction (t t) simple-vector) %wait-for-rehash))
134 ;; Concurrent access relies on a forwarding pointer being placed into
135 ;; transported value cells. Since this is not a fully general hashtable,
136 ;; we can use fixnums as forwarding pointers, as they're otherwise
137 ;; never present as a value.
138 #+sb-xc-host
139 (progn
140 (defun xc-compare-and-swap-svref (vector index old new)
141 (let ((actual-old (svref vector index)))
142 (if (eq old actual-old)
143 (progn (setf (svref vector index) new) old)
144 (error "xc bug. CAS expected ~S got ~S" old actual-old))))
145 (defun make-info-forwarding-pointer (index) index)
146 (defun info-forwarding-pointer-target (pointer) pointer)
147 (defun info-value-moved-p (val) (fixnump val)))
149 ;; However, here is a forwarding-pointer representation that allows fixnums
150 ;; as legal values in the table, so, since it's more general, ... why not?
151 #-sb-xc-host
152 (progn
153 (defun make-info-forwarding-pointer (index)
154 (declare (type info-cell-index index) (optimize (safety 0)))
155 (%make-lisp-obj (+ (ash index 8) sb!vm:unbound-marker-widetag)))
156 (defun info-forwarding-pointer-target (marker)
157 (ash (get-lisp-obj-address marker) -8))
158 (defun info-value-moved-p (x)
159 (eq (logand (get-lisp-obj-address x) #xff)
160 sb!vm:unbound-marker-widetag)))
162 ;; The common skeleton of {Get, Put, Rehash} operations. Probe key cells until
163 ;; either a hit occurs, in which case the :HIT form is executed and looping
164 ;; stops; or an empty slot is seen in which case the :MISS code is executed.
165 ;; :MISS should contain a GO or RETURN on success, otherwise probing continues.
166 ;; No precaution exists against probing forever, such as could happen if the
167 ;; probing strategy fails to visit all candidate free cells.
169 ;; Stepping is done by subtracting a secondary hash rather than adding,
170 ;; as this allows testing for wraparound by comparison to a constant
171 ;; instead of against storage capacity. Nonlinear probing jumps around in
172 ;; the L1 cache regardless, as they key space is a ring so the "direction"
173 ;; of probing is irrelevant. [In contrast, linear probing usually benefits
174 ;; from scanning physically forward, preferably within a cache line,
175 ;; but for practical purposes linear probing is worse.]
177 (defmacro !do-probe-sequence ((storage key table &optional hash)
178 &key probe hit miss)
179 (with-unique-names (test miss-fn len key-index step)
180 (once-only ((storage storage) (key key) (table table)
181 (hashval
182 `(the fixnum
183 ,(or hash
184 `(funcall (info-env-hash-function ,table) ,key)))))
185 `(macrolet ((key-index () ; expose key+value indices to invoking code
186 ',key-index)
187 (value-index ()
188 '(+ (info-storage-capacity ,storage) ,key-index))
189 (,test ()
190 `(let ((probed-key (svref ,',storage ,',key-index)))
191 ,',probe ; could keep a tally of the probes
192 ;; Optimistically test for hit first, then markers
193 (cond ((funcall (info-env-comparator ,',table)
194 probed-key ,',key)
195 (go :hit))
196 ((or (eql probed-key +unavailable-key+)
197 (eql probed-key +empty-key+))
198 (,',miss-fn))))))
199 (let* ((,len (info-storage-capacity ,storage))
200 (,key-index (+ (rem ,hashval ,len) +info-keys-offset+))
201 (,step 0))
202 (declare (type info-cell-index ,key-index ,step))
203 (dx-flet ((,miss-fn () ,miss))
204 (tagbody
205 (,test)
206 ;; only need the second hash if didn't hit on first try
207 (setq ,step (1+ (rem ,hashval (- ,len 2))))
208 :loop
209 (setq ,key-index (let ((next (- ,key-index ,step)))
210 (if (< next +info-keys-offset+)
211 (+ next ,len)
212 next)))
213 (,test)
214 (go :loop)
215 :HIT))
216 ,hit)))))
218 ;; Wait for ENV's storage to change to something other than STORAGE, and
219 ;; return the new one. As long as rehash finishes in finite time, every thread
220 ;; makes progess. We don't protect against untimely death of the thread
221 ;; that holds the lock.
223 (defun %wait-for-rehash (env storage)
224 ;; kinda spin, except not quite that bad
225 (loop (sb!thread:thread-yield) ; relinquish time slice, supposing it helps
226 (if (eq (info-env-storage env) storage)
227 ;; Grab and release the mutex for no other reason than to
228 ;; observe that a rehasher doesn't (for the moment) have it.
229 (sb!thread:with-mutex ((info-env-mutex env))) ; do nothing, retry
230 (return (info-env-storage env)))))
232 ;; Look in info-environment ENV for the name KEY. Arguments are like GETHASH.
234 ;; Invariant: any KEY's value is present in at most 1 storage.
235 ;; Uniqueness of the location ensures that when writing with CAS, the place is
236 ;; either current, or CAS fails in a way that informs the writer of a new place.
237 ;; At most one probing sequence occurs, in that hitting a key might entail
238 ;; more than one forwarding but never a re-probing.
239 ;; The same is not true on insert, for which probing restart is quite common.
240 ;; When chasing values, INFO-STORAGE-NEXT may or may not be EQ to what is in
241 ;; INFO-ENV-STORAGE, depending whether rehash is still running, has completed,
242 ;; or started all over again before the reader got a chance to chase one time.
244 (defun info-gethash (key env &aux (storage (info-env-storage env)))
245 (!do-probe-sequence (storage key env)
246 :miss (return-from info-gethash nil)
247 ;; With 99% certainty the :READ barrier is needed for non-x86, and if not,
248 ;; it can't hurt. 'info-storage-next' can be empty until at least one cell
249 ;; has been forwarded, but in general an unsynchronized read might be
250 ;; perceived as executing before a conditional that guards whether the
251 ;; read should happen at all. 'storage-next' has the deceptive look of a
252 ;; data dependency but it's not - it's a control dependency which as per
253 ;; the 'memory-barriers.txt' document at kernel.org, demands a barrier.
254 ;; The subsequent ref to storage (if the loop iterates) has a
255 ;; data-dependency, and will never be reordered except on an Alpha :-(
256 ;; so we _don't_ need a barrier after resetting 'storage' and 'index'.
257 :hit (let ((index (value-index)))
258 (loop (let ((value
259 (sb!thread:barrier (:read) (svref storage index))))
260 (if (info-value-moved-p value)
261 (setq storage (info-storage-next storage)
262 index (info-forwarding-pointer-target value))
263 (return value)))))))
265 ;; ENV and KEY are as above. UPDATE-PROC is invoked with the old value stored
266 ;; for KEY and it should return the possibly-unchanged new value to put.
268 ;; Note a tiny problem of semantics on 'miss' - for an instant after winning
269 ;; CAS of a new key, that key appears to exist with the default value NIL,
270 ;; causing readers to think "FOUND-P" = T which might have a different sense
271 ;; from "not found". If a Name is transiently "found" despite having no data,
272 ;; nobody cares, at least for globaldb. Remedies, if desired, involve either:
273 ;; 1. Making a "no value" marker that is distinct from NIL.
274 ;; 2. Placing keys/value pairs in adjacent cells and using double-wide CAS.
275 ;; The reason we don't care about this anomaly is that we have to
276 ;; look in an Info-Vector [q.v.] anyway to get the whole picture.
278 (defun info-puthash (env key update-proc)
279 (aver (not (member key '(0 -1))))
280 (labels ((follow/update (array value-index)
281 (let ((value ; see INFO-GETHASH for this barrier's rationale
282 (sb!thread:barrier (:read) (svref array value-index))))
283 (if (info-value-moved-p value)
284 (follow/update (info-storage-next array)
285 (info-forwarding-pointer-target value))
286 (update array value-index value))))
287 (update (array index oldval)
288 ;; invariant: OLDVAL is not a forwarding pointer.
289 (let ((newval (funcall update-proc oldval)))
290 (if (eq newval oldval)
291 oldval ; forgo update
292 (named-let put ((array array) (index index))
293 (let ((actual-old (info-cas array index oldval newval)))
294 ;; Unlike above, this read of storage-next can not
295 ;; be perceived as having occurred prior to CAS.
296 ;; x86 synchronizes at every locked instruction, and our
297 ;; PPC CAS vops sync as a nececessity of performing CAS.
298 (cond ((eq oldval actual-old) newval) ; win
299 ((info-value-moved-p actual-old) ; forwarded
300 (put (info-storage-next array)
301 (info-forwarding-pointer-target actual-old)))
302 (t ; collision with another writer
303 (update array index actual-old)))))))))
304 (named-let probe ((hashval (funcall (info-env-hash-function env) key))
305 (storage (info-env-storage env)))
306 (!do-probe-sequence (storage key env hashval)
307 :hit (follow/update storage (value-index))
308 :miss
309 (progn
310 (let ((old-count (info-env-adjust-count env 1)))
311 (declare (type info-cell-index old-count))
312 (when (>= old-count (info-storage-threshold storage))
313 (sb!thread:with-mutex ((info-env-mutex env))
314 ;; any thread could have beaten us to rehashing
315 (when (eq (info-env-storage env) storage)
316 (info-env-rehash env)))
317 (info-env-adjust-count env -1) ; prepare to retry
318 (return-from probe (probe hashval (info-env-storage env)))))
319 ;; Attempt to claim KEY-INDEX
320 (let ((oldkey (info-cas storage (key-index) +empty-key+ key)))
321 (when (eql oldkey +empty-key+) ; successful claim
322 ;; Optimistically assume that nobody else rewrote the value
323 (return-from probe (update storage (value-index) nil)))
324 (info-env-adjust-count env -1) ; failed
325 ;; The fallthrough branch of this COND is ordinary CAS failure where
326 ;; somebody else wanted this slot, and won. Looping tries again.
327 (cond ((funcall (info-env-comparator env) oldkey key) ; coincidence
328 (return-from probe (follow/update storage (value-index))))
329 ((eql oldkey +unavailable-key+) ; Highly unlikely
330 ;; as preemptive check up above ensured no rehash needed.
331 (return-from probe
332 (probe hashval (%wait-for-rehash env storage)))))))))))
334 ;; Rehash ENV to a larger storage. When writing a key into new storage,
335 ;; key cells are uniquely owned by this thread without contention.
336 ;; Other threads may not look in new storage without first observing that
337 ;; a key's value was definitely moved.
338 ;; The rehasher marks empty cells as unusable so that writers can't insert
339 ;; into the subsequence of storage already visited. The rehasher must of
340 ;; course vie for the cell it is trying to mark as unusable.
342 (defun info-env-rehash (env)
343 (let* ((old-count (info-env-count env))
344 (old-storage (info-env-storage env))
345 ;; the new storage begins life at ~50% capacity
346 (new-storage (make-info-storage (ceiling old-count .5)))
347 (old-capacity (info-storage-capacity old-storage))
348 (new-capacity (info-storage-capacity new-storage)))
350 (sb!thread:barrier (:write) ; Publish NEW-STORAGE before scanning keys.
351 (setf (info-storage-next old-storage) new-storage))
353 (loop for old-key-index of-type info-cell-index
354 from +info-keys-offset+ below (+ +info-keys-offset+ old-capacity)
355 ;; If the indexed cell is not in use, try once to prevent subsequent
356 ;; writes by changing the empty marker to 'unavailable'. The outcome
357 ;; determines whether to continue transporting the cell's value.
358 for key = (let ((key (svref old-storage old-key-index)))
359 (if (eql key +empty-key+)
360 (info-cas old-storage old-key-index
361 +empty-key+ +unavailable-key+)
362 key))
363 unless (eql key +empty-key+)
364 do (let* ((new-key-index
365 (block nil
366 (!do-probe-sequence (new-storage key env)
367 :hit (bug "Globaldb rehash failure. Mutated key?")
368 :miss (return (key-index)))))
369 (old-value-index (+ old-key-index old-capacity))
370 (new-value-index (+ new-key-index new-capacity))
371 (value (svref old-storage old-value-index)))
372 (setf (svref new-storage new-key-index) key) ; Q: barrier needed?
373 ;; Copy the current value into the new storage,
374 ;; and CAS in a forwarding pointer. Repeat until successful.
375 (loop
376 ;; Force VALUE to memory before publishing relocated status
377 (sb!thread:barrier (:write)
378 (setf (svref new-storage new-value-index) value))
379 (let ((actual-old
380 (info-cas
381 old-storage old-value-index value
382 (make-info-forwarding-pointer new-value-index))))
383 (if (eq value actual-old)
384 (return)
385 (setq value actual-old))))))
387 ;; Typical of most lockfree algorithms, we've no idea when
388 ;; the old storage can be freed. GC will figure it out.
389 ;; No write barrier needed. Threads still looking at old storage
390 ;; will eventually find all cells unavailable or forwarded.
391 (setf (info-env-storage env) new-storage)))
393 ;; This maphash implementation is not threadsafe.
394 ;; It can be made threadsafe by following forwarded values
395 ;; and skipping over unavailable keys.
397 (defmacro info-maphash (fun env) ; map FUN over each key/value
398 (with-unique-names (f storage capacity i key)
399 `(let* ((,f ,fun)
400 (,storage (info-env-storage ,env))
401 (,capacity (info-storage-capacity ,storage)))
402 (loop for ,i below ,capacity
403 for ,key = (svref ,storage (+ ,i +info-keys-offset+))
404 unless (eql ,key +empty-key+)
405 do (funcall ,f ,key
406 (svref ,storage
407 (+ ,i +info-keys-offset+ ,capacity)))))))
409 ;; CAS is the primitive operation on an info hashtable,
410 ;; and SETF is implemented in terms of CAS. For the most part it is
411 ;; inadvisable to use this for anything other than tinkering at the REPL.
413 (defun (setf info-gethash) (newval key env)
414 (dx-flet ((update (old) (declare (ignore old)) newval))
415 (info-puthash env key #'update)))
417 (defun show-info-env (env)
418 (info-maphash (lambda (k v) (format t "~S -> ~S~%" k v)) env))
420 ;;;; Info-Vectors
421 ;;;; ============
423 ;;; Info for a Name (an arbitrary object) is stored in an Info-Vector,
424 ;;; which is like is a 2-level association list. Info-Vectors are stored in
425 ;;; symbols for most names, or in the global hashtable for "complicated" names.
427 ;;; Such vectors exists in two variations: packed and unpacked.
428 ;;; The representations are nearly equivalent for lookup, but the packed format
429 ;;; is more space-efficient, though difficult to manipulate except by unpacking.
431 ;;; Consider a family of Names whose "root" is SB-MOP:STANDARD-INSTANCE-ACCESS.
432 ;;; 1. SB-MOP:STANDARD-INSTANCE-ACCESS
433 ;;; 2. (SETF SB-MOP:STANDARD-INSTANCE-ACCESS)
434 ;;; 3. (CAS SB-MOP:STANDARD-INSTANCE-ACCESS)
436 ;;; Those three names share one Info-Vector. Conceptually the outer alist key
437 ;;; is NIL for the first of those names, and SETF/CAS for the latter two.
438 ;;; The inner alist key is a number identifying a type of info.
439 ;;; If it were actually an alist, it would look like this:
441 ;;; ((nil (63 . #<fdefn SB-MOP:STANDARD-INSTANCE-ACCESS>) (1 . :FUNCTION) ...)
442 ;;; (SETF (63 . #<fdefn (SETF SB-MOP:STANDARD-INSTANCE-ACCESS)>) ...)
443 ;;; (CAS (63 . #<fdefn (CAS SB-MOP:STANDARD-INSTANCE-ACCESS)>) ...)
444 ;;; ...)
446 ;;; Note:
447 ;;; * The root name is exogenous to the vector - it is not stored.
448 ;;; * The info-number for (:FUNCTION :DEFINITION) is 63, :KIND is 1, etc.
449 ;;; * Names which are lists of length other than 2, or improper lists,
450 ;;; or whose elements are not both symbols, are disqualified.
452 ;;; Packed vector layout
453 ;;; --------------------
454 ;;; Because the keys to the inner lists are integers in the range 0 to 63,
455 ;;; either 5 or 10 keys will fit into a fixnum depending on word size.
456 ;;; This permits one memory read to retrieve a collection of keys. In packed
457 ;;; format, an ordered set of keys ("fields") is called a "descriptor".
459 ;;; Descriptors are stored from element 0 upward in the packed vector,
460 ;;; and data are indexed downward from the last element of the vector.
462 ;;; #(descriptor0 descriptor1 ... descriptorN valueN ... value1 value0)
464 ;;; e.g. The field at absolute index 3 - vector element 0, bit position 18 -
465 ;;; will find its data at index (- END 3). In this manner, it doesn't matter
466 ;;; how many more descriptors exist.
468 ;;; A "group" comprises all the info for a particular Name, and its list
469 ;;; of types may may span descriptors, though rarely.
470 ;;; An "auxilliary key" is the first element of a 2-list Name. It is interposed
471 ;;; within the data portion of the vector after the preceding info group.
472 ;;; Descriptors are self-delimiting in that the first field in a group
473 ;;; indicates the number of additional fields in the group.
475 ;;; Unpacked vector layout
476 ;;; ----------------------
477 ;;; This representation is used transiently during insertion/deletion.
478 ;;; It is a concatenation of plists as a vector, interposing at the splice
479 ;;; points the auxilliary key for the group, except for the root name which
480 ;;; does not store an auxilliary key.
482 ;;; Unpacked vector format looks like:
484 ;;; /- next group starts here
485 ;;; v
486 ;;; #(length type val type val ... KEY length type val ... KEY length ...)
487 ;;; ^
488 ;;; info group for the primary Name, a/k/a "root symbol", starts here
490 ;;; One can envision that the first info group stores its auxilliary key
491 ;;; at vector index -1 when thinking about the correctness of algorithms
492 ;;; that process unpacked info-vectors.
493 ;;; See !TEST-PACKIFY-INFOS for examples of each format.
495 ;;;;; Some stuff moved from 'globaldb.lisp':
497 (defconstant info-num-mask (ldb (byte info-number-bits 0) -1)) ; #b111111
499 ;; Using 6 bits per packed field, 5 infos can be described in a 30-bit fixnum,
500 ;; or 10 in a fixnum on 64-bit machines (regardless of n-fixnum-tag-bits).
501 ;; The eval-when seems to be necessary for building with CCL as host.
502 (eval-when (:compile-toplevel :load-toplevel :execute)
503 (defconstant +infos-per-word+ (floor sb!vm:n-fixnum-bits info-number-bits)))
505 ;; Descriptors are target fixnums
506 (deftype info-descriptor () `(signed-byte ,sb!vm:n-fixnum-bits))
508 ;; An empty info-vector. Its 0th field describes that there are no more fields.
509 (defconstant-eqx +nil-packed-infos+ #(0) #'equalp)
511 ;; FDEFINITIONs have an info-number that admits slightly clever logic
512 ;; for INFO-VECTOR-FDEFN. Do not change this constant without
513 ;; careful examination of that function.
514 (defconstant +fdefn-info-num+ info-num-mask)
516 ;; Extract a field from a packed info descriptor.
517 ;; A field is either a count of info-numbers, or an info-number.
518 (declaim (inline packed-info-field))
519 (defun packed-info-field (vector desc-index field-index)
520 (ldb (byte info-number-bits
521 (* (the (mod #.+infos-per-word+) field-index) info-number-bits))
522 (the info-descriptor (svref vector desc-index))))
524 ;; Compute the number of elements needed to hold unpacked VECTOR after packing.
525 ;; This is not "compute-packed-info-size" since that could be misconstrued
526 ;; as expecting the vector to be already packed.
528 (defun compute-packified-info-size (vector &optional (end (length vector)))
529 (declare (simple-vector vector)) ; unpacked format
530 (let ((index 0) ; index into the unpacked input vector
531 (n-fields 0)) ; will be the total number of packed fields
532 (declare (type index index end n-fields))
533 (loop
534 ;; 'length' is the number of vector elements in this info group,
535 ;; including itself but not including its auxilliary key.
536 (let ((length (the index (svref vector index))))
537 ;; Divide by 2 because we only count one field for the entry, but the
538 ;; input vector had 2 cells per entry. Add 1 because the group's field
539 ;; count is accounted for in the total packed field count.
540 (incf n-fields (1+ (ash length -1)))
541 (incf index (1+ length)) ; jump over the entries, +1 for aux-key
542 (when (>= index end)
543 ;; The first info group lacks an aux-key, making n-fields 1 too high
544 ;; in terms of data cells used, but correct for packed fields.
545 (return (+ (ceiling n-fields +infos-per-word+) (1- n-fields))))))))
547 ;; MAKE-INFO-DESCRIPTOR is basically ASH-LEFT-MODFX, shifting VAL by SHIFT.
548 ;; It is important that info descriptors be target fixnums, but 'cross-modular'
549 ;; isn't loaded early enough to use 'mask-signed-field'.
550 ;; It's not needed on 64-bit host/target combination because 10 fields (60 bits)
551 ;; never touch the sign bit.
552 ;; FIXME: figure out why the definition of ash-left-modfx is
553 ;; conditionalized out for platforms other than x86[-64].
554 ;; It looks like it ought to work whether or not there are vops.
555 (defmacro make-info-descriptor (val shift)
556 (if (> sb!vm:n-fixnum-bits 30)
557 `(ash ,val ,shift)
558 `(logior (if (logbitp (- 29 ,shift) ,val) sb!xc:most-negative-fixnum 0)
559 (ash ,val ,shift))))
561 ;; Convert unpacked vector to packed vector.
562 ;; 'pack-infos' would be a hypothetical accessor for the 'infos' of a 'pack'
563 ;; (whatever that is ...) so verbifying as such makes it more mnemonic to me.
565 (defun packify-infos (input &optional (end (length input)))
566 (declare (simple-vector input))
567 (let* ((output (make-array (compute-packified-info-size input end)))
568 (i -1) ; input index: pre-increment to read the next datum
569 (j -1) ; descriptor index: pre-increment to write
570 (k (length output)) ; data index: pre-decrement to write
571 (field-shift 0)
572 (word 0))
573 (declare (type index-or-minus-1 i j k end)
574 (type (mod #.(1+ (* (1- +infos-per-word+) info-number-bits)))
575 field-shift)
576 (type info-descriptor word))
577 (flet ((put-field (val) ; insert VAL into the current packed descriptor
578 (declare (type info-number val))
579 (setq word (logior (make-info-descriptor val field-shift) word))
580 (if (< field-shift (* (1- +infos-per-word+) info-number-bits))
581 (incf field-shift info-number-bits)
582 (setf (svref output (incf j)) word field-shift 0 word 0))))
583 ;; Truncating divide by 2: count = n-elements in the group @ 2 per entry,
584 ;; +1 for count itself but not including its aux-key.
585 (loop (let ((count (ash (the index (svref input (incf i))) -1)))
586 (put-field count) ; how many infos to follow
587 (dotimes (iter count)
588 (put-field (svref input (incf i))) ; an info-number
589 (setf (svref output (decf k)) (svref input (incf i)))) ; value
590 (when (>= (incf i) end)
591 (return))
592 (setf (svref output (decf k)) (svref input i))))) ; an aux-key
593 (unless (zerop field-shift) ; store the final descriptor word
594 (setf (svref output (incf j)) word))
595 (aver (eql (1+ j) k)) ; last descriptor must be adjacent final data cell
596 output))
598 ;; Within the scope of BODY, bind GENERATOR to a local function which
599 ;; returns the next field from a descriptor in INPUT-VAR, a packed vector.
600 ;; The generator uses DESCRIPTOR-INDEX and updates it as a side-effect.
602 (defmacro !with-packed-info-iterator ((generator input-var
603 &key descriptor-index)
604 &body body)
605 (with-unique-names (input word count)
606 `(let* ((,input (the simple-vector ,input-var))
607 (,descriptor-index -1)
608 (,count 0)
609 (,word 0))
610 (declare (type info-descriptor ,word)
611 (fixnum ,count)
612 (type index-or-minus-1 ,descriptor-index))
613 (flet ((,generator ()
614 (when (zerop ,count)
615 (incf ,descriptor-index)
616 (setq ,word (svref ,input ,descriptor-index)
617 ,count +infos-per-word+))
618 (prog1 (logand ,word info-num-mask)
619 (setq ,word (ash ,word (- info-number-bits)))
620 (decf ,count))))
621 ,@body))))
623 ;; Iterate over VECTOR, binding DATA-INDEX to the index of each aux-key in turn.
624 ;; TOTAL-N-FIELDS is deliberately exposed to invoking code.
626 (defmacro !do-packed-info-vector-aux-key ((vector &optional (data-index (gensym)))
627 step-form &optional result-form)
628 (with-unique-names (descriptor-idx field-idx)
629 (once-only ((vector vector))
630 `(let ((,data-index (length ,vector))
631 (,descriptor-idx 0)
632 (,field-idx 0)
633 (total-n-fields 0))
634 (declare (type index ,data-index ,descriptor-idx total-n-fields)
635 (type (mod #.+infos-per-word+) ,field-idx))
636 ;; Loop through the descriptors in random-access fashion.
637 ;; Skip 1+ n-infos each time, because the 'n-infos' is itself a field
638 ;; that is not accounted for in its own value.
639 (loop (let ((n (1+ (packed-info-field ,vector
640 ,descriptor-idx ,field-idx))))
641 (incf total-n-fields n)
642 (multiple-value-setq (,descriptor-idx ,field-idx)
643 (floor total-n-fields +infos-per-word+))
644 (decf ,data-index n))
645 ;; Done when the ascending index and descending index meet
646 (unless (< ,descriptor-idx ,data-index)
647 (return ,result-form))
648 ,@(if step-form (list step-form)))))))
650 ;; Return all function names that are stored in SYMBOL's info-vector.
651 ;; As an example, (INFO-VECTOR-NAME-LIST 'SB-PCL::DIRECT-SUPERCLASSES) =>
652 ;; ((SB-PCL::SLOT-ACCESSOR :GLOBAL SB-PCL::DIRECT-SUPERCLASSES SB-PCL::READER)
653 ;; (SB-PCL::SLOT-ACCESSOR :GLOBAL SB-PCL::DIRECT-SUPERCLASSES BOUNDP)
654 ;; (SB-PCL::SLOT-ACCESSOR :GLOBAL SB-PCL::DIRECT-SUPERCLASSES SB-PCL::WRITER))
655 (defun info-vector-name-list (symbol)
656 (let ((vector (symbol-info-vector symbol))
657 (list))
658 (when vector
659 (!do-packed-info-vector-aux-key (vector key-index)
660 (push (construct-globaldb-name (svref vector key-index) symbol)
661 list))
662 (nconc (and (plusp (packed-info-field vector 0 0)) (list symbol))
663 (nreverse list)))))
665 ;; Compute the number of elements needed to hold packed VECTOR after unpacking.
666 ;; The unpacked size is the number of auxilliary keys plus the number of entries
667 ;; @ 2 cells per entry, plus the number of length cells which indicate the
668 ;; number of data cells used (including length cells but not aux key cells).
669 ;; Equivalently, it's the number of packed fields times 2 minus 1.
671 (defun compute-unpackified-info-size (vector)
672 (declare (simple-vector vector))
673 (!do-packed-info-vector-aux-key (vector) ()
674 ;; off-by-one: the first info group's auxilliary key is imaginary
675 (1- (truly-the fixnum (ash total-n-fields 1)))))
677 ;; Convert packed INPUT vector to unpacked.
678 ;; If optional OUTPUT is supplied, it is used, otherwise output is allocated.
679 ;; For efficiency the OUTPUT should be provided as a dynamic-extent array.
681 (defun unpackify-infos (input &optional (output nil output-supplied-p))
682 (declare (simple-vector input))
683 (unless output-supplied-p
684 (setq output (make-array (compute-unpackified-info-size input))))
685 (let ((i (length input)) (j -1)) ; input index and output index respectively
686 (declare (type index-or-minus-1 i j))
687 (!with-packed-info-iterator (next-field input :descriptor-index desc-idx)
688 (loop ; over name
689 (let ((n-infos (next-field)))
690 ;; store the info group length, including the length cell in the length
691 (setf (svref output (incf j)) (1+ (ash n-infos 1)))
692 (dotimes (iter n-infos) ; over info-types
693 (setf (svref output (incf j)) (next-field) ; type-num
694 (svref output (incf j)) (svref input (decf i))))) ; value
695 (if (< desc-idx (decf i)) ; as long as the indices haven't met
696 (setf (svref output (incf j)) (svref input i)) ; copy next aux-key
697 (return (if output-supplied-p nil output))))))) ; else done
699 ;; Return the index of the 'length' item for an info group having
700 ;; auxilliary-key KEY in unpacked VECTOR bounded by END (exclusive),
701 ;; or NIL if not found.
703 (defun info-find-aux-key/unpacked (key vector end)
704 (declare (type index end))
705 (if (eql key +no-auxilliary-key+)
706 0 ; the first group's length (= end) is stored here always
707 (let ((index 0))
708 (declare (type index index))
709 (loop
710 ;; skip 'length' cells plus the next aux-key
711 (incf index (1+ (the index (svref vector index))))
712 (cond ((>= index end)
713 (return nil))
714 ;; backward a cell is where the aux-key resides.
715 ((eq (svref vector (1- index)) key)
716 (return index)))))))
718 ;; In packed info VECTOR try to find the auxilliary key SYMBOL.
719 ;; If found, return indices of its data, info descriptor word, and field.
720 ;; If not found, the first value is NIL and the descriptor indices
721 ;; arbitrarily point to the next available descriptor field.
723 (defun info-find-aux-key/packed (vector symbol)
724 ;; explicit bounds checking is done by the code below
725 (declare (optimize (safety 0)))
726 (aver (simple-vector-p vector))
727 (let ((descriptor-idx 0) ; physical index to vector
728 (field-idx 0) ; relative index within current descriptor
729 ;; On each iteration DATA-IDX points to an aux-key cell
730 ;; The first group's imaginary aux-key cell is past the end.
731 (data-idx (length (the simple-vector vector))))
732 (declare (type index descriptor-idx data-idx)
733 (fixnum field-idx)) ; can briefly exceed +infos-per-word+
734 ;; Efficiently skip past N-INFOS infos. If decrementing the data index
735 ;; hits the descriptor index, we're done. Otherwise increment the field
736 ;; index and maybe descriptor index and check again for loop termination.
737 (flet ((skip (n-infos &aux (n-fields (1+ n-infos))) ; return T on success
738 (cond ((<= (decf data-idx n-fields) descriptor-idx) nil)
739 ;; descriptor-idx < data-idx, so potentially more data.
740 ;; If current descriptor has another field, continue.
741 ((< (incf field-idx n-fields) +infos-per-word+) t)
742 (t ; The descriptor index advances.
743 (loop (incf descriptor-idx)
744 (when (< (decf field-idx +infos-per-word+)
745 +infos-per-word+)
746 (return (< descriptor-idx data-idx))))))))
747 (declare (inline skip))
748 ;; While this could compare aux-keys with #'EQUAL, it is not obvious how
749 ;; in general one would pick a symbol from the name as that which
750 ;; is delegated as the one to hold the info-vector.
751 (values (cond ((not (skip (packed-info-field vector 0 0))) nil)
752 ;; At least one aux key is present.
753 ((eq (aref vector data-idx) symbol) data-idx) ; yay
754 ;; aux-key order invariant allows early fail on SETF
755 ((eq symbol 'setf) nil)
757 (loop
758 (cond ((not (skip (packed-info-field vector descriptor-idx
759 field-idx)))
760 (return nil))
761 ((eq (aref vector data-idx) symbol)
762 (return data-idx))))))
763 descriptor-idx field-idx)))) ; can be ignored if 1st val is nil
765 ;; Take a packed info-vector INPUT and insert (AUX-KEY,INFO-NUMBER,VALUE).
766 ;; Packed info-vectors are immutable. Any alteration must create a copy.
767 ;; This is done by unpacking/repacking - it's easy enough and fairly
768 ;; efficient since the temporary vector is stack-allocated.
770 (defun %packed-info-insert (input aux-key info-number value)
771 (declare (simple-vector input) (type info-number info-number))
772 (let* ((n-extra-elts
773 ;; Test if the aux-key has been seen before or needs to be added.
774 (if (and (not (eql aux-key +no-auxilliary-key+))
775 (not (info-find-aux-key/packed input aux-key)))
776 4 ; need space for [aux-key, length, type-num, value]
777 2)) ; only space for [type-num, value]
778 (old-size (compute-unpackified-info-size input))
779 (new-size (+ old-size n-extra-elts))
780 (new (make-array new-size)))
781 (declare (type index old-size new-size)
782 (truly-dynamic-extent new))
783 (unpackify-infos input new)
784 (flet ((insert-at (point v0 v1)
785 (unless (eql point old-size) ; slide right
786 (replace new new :start1 (+ point n-extra-elts) :start2 point))
787 (setf (svref new point) v0
788 (svref new (+ point 1)) v1)))
789 (cond ((= n-extra-elts 4)
790 ;; creating a new aux key. SETF immediately follows the data
791 ;; for the primary Name. All other aux-keys go to the end.
792 (let ((point (if (eq aux-key 'setf) (svref new 0) old-size)))
793 (insert-at point aux-key 3) ; = add 3 data cells not incl. aux-key
794 (setf (svref new (+ point 2)) info-number
795 (svref new (+ point 3)) value)))
797 (let ((data-start (info-find-aux-key/unpacked
798 aux-key new old-size)))
799 ;; it had better be found - it was in the packed vector
800 (aver data-start)
801 ;; fdefn must be the first piece of info for any name.
802 ;; This facilitates implementing SYMBOL-FUNCTION without
803 ;; without completely decoding the vector.
804 (insert-at (+ data-start (if (eql info-number +fdefn-info-num+)
805 1 (svref new data-start)))
806 info-number value)
807 ;; add 2 cells, and verify that re-packing won't
808 ;; overflow the 'count' for this info group.
809 (aver (typep (ash (incf (svref new data-start) 2) -1)
810 'info-number))))))
811 (packify-infos new)))
813 ;; Return T if INFO-VECTOR admits quicker insertion logic - it must have
814 ;; exactly one descriptor for the root name, space for >= 1 more field,
815 ;; and no aux-keys.
816 (declaim (inline info-quickly-insertable-p))
817 (defun info-quickly-insertable-p (input)
818 (let ((n-infos (packed-info-field input 0 0)))
819 ;; We can easily determine if the no-aux-keys constraint is satisfied,
820 ;; because a secondary name's info occupies at least two cells,
821 ;; one for its aux-key and >= 1 for info values.
822 (and (< n-infos (1- +infos-per-word+))
823 (eql n-infos (1- (length input))))))
825 ;; Take a packed info-vector INPUT and return a new one with INFO-NUMBER/VALUE
826 ;; added for the root name. The vector must satisfy INFO-QUICKLY-INSERTABLE-P.
827 ;; This code is separate from PACKED-INFO-INSERT to facilitate writing
828 ;; a unit test of this logic against the complete logic.
830 (defun quick-packed-info-insert (input info-number value)
831 ;; Because INPUT contains 1 descriptor and its corresponding values,
832 ;; the current length is exactly NEW-N, the new number of fields.
833 (let* ((descriptor (svref input 0))
834 (new-n (truly-the info-number (length input)))
835 (new-vect (make-array (1+ new-n))))
836 ;; Two cases: we're either inserting info for the fdefn, or not.
837 (cond ((eq info-number +fdefn-info-num+)
838 ;; fdefn, if present, must remain the first packed field.
839 ;; Replace the lowest field (the count) with +fdefn-info-num+,
840 ;; shift everything left 6 bits, then OR in the new count.
841 (setf (svref new-vect 0)
842 (logior (make-info-descriptor
843 (dpb +fdefn-info-num+ (byte info-number-bits 0)
844 descriptor) info-number-bits) new-n)
845 ;; Packed vectors are indexed "backwards". The first
846 ;; field's info is in the highest numbered cell.
847 (svref new-vect new-n) value)
848 (loop for i from 1 below new-n
849 do (setf (svref new-vect i) (svref input i))))
851 ;; Add a field on the high end and increment the count.
852 (setf (svref new-vect 0)
853 (logior (make-info-descriptor
854 info-number (* info-number-bits new-n))
855 (1+ descriptor))
856 (svref new-vect 1) value)
857 ;; Slide the old data up 1 cell.
858 (loop for i from 2 to new-n
859 do (setf (svref new-vect i) (svref input (1- i))))))
860 new-vect))
862 (declaim (maybe-inline packed-info-insert))
863 (defun packed-info-insert (vector aux-key info-number newval)
864 (if (and (eql aux-key +no-auxilliary-key+)
865 (info-quickly-insertable-p vector))
866 (quick-packed-info-insert vector info-number newval)
867 (%packed-info-insert vector aux-key info-number newval)))
869 ;; Search packed VECTOR for AUX-KEY and INFO-NUMBER, returning
870 ;; the index of the data if found, or NIL if not found.
872 (defun packed-info-value-index (vector aux-key type-num)
873 (declare (optimize (safety 0))) ; vector bounds are AVERed
874 (let ((data-idx (length vector)) (descriptor-idx 0) (field-idx 0))
875 (declare (type index descriptor-idx)
876 (type (mod #.+infos-per-word+) field-idx))
877 (unless (eql aux-key +no-auxilliary-key+)
878 (multiple-value-setq (data-idx descriptor-idx field-idx)
879 (info-find-aux-key/packed vector aux-key))
880 (unless data-idx
881 (return-from packed-info-value-index nil)))
882 ;; Fetch a descriptor and shift out trailing bits that won't be scanned.
883 (let* ((descriptor (ash (the info-descriptor (aref vector descriptor-idx))
884 (* (- info-number-bits) field-idx)))
885 (n-infos (logand descriptor info-num-mask))
886 ;; Compute n things in this descriptor after extracting one field. e.g.
887 ;; if starting index = 2, there's space for 7 more fields in 60 bits.
888 (swath (min (- +infos-per-word+ field-idx 1) n-infos)))
889 ;; Type inference on n-infos deems it to have no lower bound due to DECF.
890 (declare (type info-descriptor descriptor)
891 (type (unsigned-byte #.info-number-bits) n-infos)
892 (type index data-idx))
893 ;; Repeatedly shift and mask, which is quicker than extracting a field at
894 ;; varying positions. Start by shifting out the n-infos field.
895 (setq descriptor (ash descriptor (- info-number-bits)))
896 (loop
897 (dotimes (j swath)
898 (when (eql type-num (logand descriptor info-num-mask))
899 (return-from packed-info-value-index
900 (the index (- data-idx j 1))))
901 (setq descriptor (ash descriptor (- info-number-bits))))
902 (when (zerop (decf n-infos swath))
903 (return nil))
904 (incf descriptor-idx)
905 (decf data-idx swath)
906 (aver (< descriptor-idx data-idx))
907 (setq descriptor (svref vector descriptor-idx)
908 swath (min n-infos +infos-per-word+))))))
910 ;; Helper for CLEAR-INFO-VALUES when Name has the efficient form.
911 ;; Given packed info-vector INPUT and auxilliary key KEY2
912 ;; return a new vector in which TYPE-NUMS are absent.
913 ;; When none of TYPE-NUMs were present to begin with, return NIL.
915 ;; While this could determine whether it would do anything before unpacking,
916 ;; clearing does not happen often enough to warrant the pre-check.
918 (defun packed-info-remove (input key2 type-nums)
919 (declare (simple-vector input))
920 (when (or (eql (length input) (length +nil-packed-infos+))
921 (and (not (eql key2 +no-auxilliary-key+))
922 (not (info-find-aux-key/packed input key2))))
923 (return-from packed-info-remove nil)) ; do nothing
924 (let* ((end (compute-unpackified-info-size input))
925 (new (make-array end))
926 (data-start 0))
927 (declare (truly-dynamic-extent new) (type index end data-start))
928 (unpackify-infos input new)
929 (let ((start (info-find-aux-key/unpacked key2 new end)))
930 (aver start) ; must be found - it was in the packed vector
931 (setq data-start start)) ; point to the group's length cell
932 (dolist (type-num type-nums)
933 (declare (type info-number type-num))
934 (let ((i (loop for probe from (1+ data-start) by 2
935 repeat (ash (svref new data-start) -1) ; =n-entries
936 when (eql (svref new probe) type-num)
937 return probe)))
938 ;; N.B. the early termination checks aren't just optimizations,
939 ;; they're requirements, because otherwise the loop could smash
940 ;; data that does not belong to this auxilliary key.
941 (cond ((not i)) ; not found - ignore
942 ;; Squash out 2 cells if deleting an info for primary name,
943 ;; or for secondary name with at least one more entry.
944 ((or (eql data-start 0) (> (svref new data-start) 3))
945 (replace new new :start1 i :start2 (+ i 2))
946 (decf end 2)
947 (when (= (decf (svref new data-start) 2) 1)
948 ;; the group is now comprised solely of its length cell
949 (return))) ; so bail out
951 ;; Delete the sole entry for a secondary name
952 ;; by removing aux-key, length, and one entry (a cell pair).
953 (replace new new :start1 (- i 2) :start2 (+ i 2))
954 (decf end 4) ; shorten by 4 cells, and stop now
955 (return)))))
956 (let ((delta (- (length new) end)))
957 (cond ((zerop delta) nil)
958 ;; All empty info-vectors are equivalent, so if
959 ;; unpacked vector has no data, return a constant.
960 ((eql end 1) +nil-packed-infos+)
961 (t (packify-infos new end)))))) ; otherwise repack
963 ;; We need a few magic constants to be shared between the next two functions.
964 (defconstant-eqx !+pcl-reader-name+ (make-symbol "READER") (constantly t))
965 (defconstant-eqx !+pcl-writer-name+ (make-symbol "WRITER") (constantly t))
966 (defconstant-eqx !+pcl-boundp-name+ (make-symbol "BOUNDP") (constantly t))
968 ;; PCL names are physically 4-lists (see "pcl/slot-name")
969 ;; that get treated as 2-component names for globaldb's purposes.
970 ;; Return the kind of PCL slot accessor name that NAME is, if it is one.
971 ;; i.e. it matches (SLOT-ACCESSOR :GLOBAL <sym> READER|WRITER|BOUNDP)
972 ;; When called, NAME is already known to start with 'SLOT-ACCESSOR.
973 ;; This has to be defined before building PCL.
974 (defun pcl-global-accessor-name-p (name)
975 (let* ((cdr (cdr name)) (tail (cdr cdr)) last
976 kind)
977 (if (and (eq (car cdr) :global)
978 (listp tail)
979 (symbolp (car tail))
980 (listp (setq last (cdr tail)))
981 (not (cdr last))
982 ;; Return symbols that can't conflict, in case somebody
983 ;; legitimates (BOUNDP <f>) via DEFINE-FUNCTION-NAME-SYNTAX.
984 ;; Especially important since BOUNDP is an external of CL.
985 (setq kind (case (car last)
986 (sb!pcl::reader !+pcl-reader-name+)
987 (sb!pcl::writer !+pcl-writer-name+)
988 (sb!pcl::boundp !+pcl-boundp-name+))))
989 ;; The first return value is what matters to WITH-GLOBALDB-NAME
990 ;; for deciding whether the name is "simple".
991 ;; Return the KIND first, just in case somehow we end up with
992 ;; (SLOT-ACCESSOR :GLOBAL NIL WRITER) as a name.
993 ;; [It "can't happen" since NIL is a constant though]
994 (values kind (car tail))
995 (values nil nil))))
997 ;; Construct a name from its parts.
998 ;; For PCL global accessors, produce the real name, not the 2-part name.
999 ;; This operation is not invoked in normal use of globaldb.
1000 ;; It is only for mapping over all names.
1001 (defun construct-globaldb-name (aux-symbol stem)
1002 (cond ((eq aux-symbol !+pcl-reader-name+) (sb!pcl::slot-reader-name stem))
1003 ((eq aux-symbol !+pcl-writer-name+) (sb!pcl::slot-writer-name stem))
1004 ((eq aux-symbol !+pcl-boundp-name+) (sb!pcl::slot-boundp-name stem))
1005 (t (list aux-symbol stem)))) ; something like (SETF frob)
1007 ;; Call FUNCTION with each piece of info in packed VECT using ROOT-SYMBOL
1008 ;; as the primary name. FUNCTION must accept 3 values (NAME INFO-NUMBER VALUE).
1009 (defun %call-with-each-info (function vect root-symbol)
1010 (let ((name root-symbol)
1011 (data-idx (length vect)))
1012 (declare (type index data-idx))
1013 (!with-packed-info-iterator (next-field vect :descriptor-index desc-idx)
1014 (loop ; over name
1015 (dotimes (i (next-field)) ; number of infos for this name
1016 (funcall function name (next-field) (svref vect (decf data-idx))))
1017 (if (< desc-idx (decf data-idx))
1018 (setq name
1019 (construct-globaldb-name (svref vect data-idx) root-symbol))
1020 (return))))))
1023 Info packing example. This example has 2 auxilliary-keys: SETF and CAS.
1025 (!test-packify-infos '(13 :XYZ 18 "nine" 28 :BAR 7 T)
1026 '(SETF 8 NIL 17 :FGX)
1027 '(CAS 6 :MUMBLE 2 :BAZ 47 :FOO))
1029 #(109006134805865284 3010 :FOO :BAZ :MUMBLE CAS :FGX NIL SETF T :BAR "nine" :XYZ)
1031 (format nil "~4,'0o ~20,'0o" 3010 109006134805865284)
1032 => "5702 06032110020734221504"
1033 Reading from right-to-left, converting each 2-digit octal number to decimal:
1034 4, 13, 18, 28, 7, 2, 8, 17, 3, 6, 2, 47
1035 Which is interpreted as:
1036 4 infos for the root name. type numbers: 13, 18, 28, 7
1037 2 infos for SETF auxilliary-key. type numbers: 8, 17
1038 3 infos for CAS auxilliary-key. type numbers: 6, 2, 47
1040 (unpackify-infos (!test-packify-infos ...)) ; same input
1041 => #(9 13 :XYZ 18 "nine" 28 :BAR 7 T
1042 SETF 5 8 NIL 17 :FGX
1043 CAS 7 6 :MUMBLE 2 :BAZ 47 :FOO)
1044 This is interpreted as
1045 root name, 9 cells: {13->:XYZ, 18->"nine", 28->:BAR, 7->T}
1046 SETF, 5 cells: {8->NIL, 17->:FGX}
1047 CAS, 7 cells: {6->:MUMBLE, 2->:BAZ, 47->:FOO}
1050 ;; Reshape inputs per the above commented example to a packed vector.
1051 ;; The info for a symbol's fdefn must precede other info-numbers.
1052 ;; and SETF must be the first aux key if other aux keys are present.
1053 ;; The test function does not enforce these invariants.
1054 #+nil ; for debugging only
1055 (defun !test-packify-infos (&rest lists)
1056 (flet ((check (plist)
1057 (and (evenp (length plist))
1058 (loop for (indicator value) on plist by #'cddr
1059 always (typep indicator 'info-number))))
1060 (add-length-prefix (list) ; computers count better than people
1061 (cons (1+ (length list)) list)))
1062 (unless (and (check (first lists))
1063 (every (lambda (list)
1064 (and (symbolp (car list)) (check (cdr list))))
1065 (rest lists)))
1066 (error "Malformed info entries"))
1067 (packify-infos
1068 (coerce (apply #'append
1069 (cons (add-length-prefix (first lists))
1070 (mapcar (lambda (more)
1071 (cons (car more)
1072 (add-length-prefix (cdr more))))
1073 (rest lists))))
1074 'vector))))
1076 ;; Given a NAME naming a globaldb object, decide whether the NAME has
1077 ;; an efficient or "simple" form, versus a general or "hairy" form.
1078 ;; The efficient form is either a symbol, a (CONS SYMBOL (CONS SYMBOL NULL)),
1079 ;; or a PCL global slot {reader, writer, boundp} function name.
1081 ;; If NAME is "simple", bind KEY2 and KEY1 to the elements
1082 ;; in that order, and execute the SIMPLE code, otherwise execute the HAIRY code.
1083 ;; If ALLOW-ATOM is T - the default - then NAME can be just a symbol
1084 ;; in which case its second component is +NO-AUXILLIARY-KEY+.
1086 (defmacro with-globaldb-name ((key1 key2 &optional (allow-atom t))
1087 name &key simple hairy)
1088 (with-unique-names (rest)
1089 `(let ((,key1 ,name) (,key2 +NO-AUXILLIARY-KEY+))
1090 (if (or ,@(if allow-atom `((symbolp ,key1)))
1091 (if (listp ,key1)
1092 (let ((,rest (cdr ,key1)))
1093 (when (listp ,rest)
1094 (cond ((not (cdr ,rest))
1095 (setq ,key2 (car ,key1) ,key1 (car ,rest))
1096 (and (symbolp ,key1) (symbolp ,key2) ,rest))
1097 ((eq (car ,key1) 'sb!pcl::slot-accessor)
1098 (multiple-value-setq (,key2 ,key1)
1099 (pcl-global-accessor-name-p ,key1))))))))
1100 ,simple
1101 ;; The KEYs remain bound, but they should not be used for anything.
1102 ,hairy))))
1104 ;; Given Info-Vector VECT, return the fdefn that it contains for its root name,
1105 ;; or nil if there is no value. NIL input is acceptable and will return NIL.
1106 (declaim (inline info-vector-fdefn))
1107 (defun info-vector-fdefn (vect)
1108 (when vect
1109 ;; This is safe: Info-Vector invariant requires that it have length >= 1.
1110 (let ((word (the fixnum (svref vect 0))))
1111 ;; Test that the first info-number is +fdefn-info-num+ and its n-infos
1112 ;; field is nonzero. These conditions can be tested simultaneously
1113 ;; using a SIMD-in-a-register idea. The low 6 bits must be nonzero
1114 ;; and the next 6 must be exactly #b111111, so considered together
1115 ;; as a 12-bit unsigned integer it must be >= #b111111000001
1116 (when (>= (ldb (byte (* info-number-bits 2) 0) word)
1117 (1+ (ash +fdefn-info-num+ info-number-bits)))
1118 ;; DATA-REF-WITH-OFFSET doesn't know the info-vector length invariant,
1119 ;; so depite (safety 0) eliding bounds check, FOLD-INDEX-ADDRESSING
1120 ;; wasn't kicking in without (TRULY-THE (INTEGER 1 *)).
1121 (aref vect (1- (truly-the (integer 1 *) (length vect))))))))
1123 ;;; Some of this stuff might belong in 'symbol.lisp', but can't be,
1124 ;;; because 'symbol.lisp' is :NOT-HOST in build-order.
1126 ;; In the target, UPDATE-SYMBOL-INFO is defined in 'symbol.lisp'.
1127 #+sb-xc-host
1128 (defun update-symbol-info (symbol update-fn)
1129 ;; Never pass NIL to an update-fn. Pass the minimal info-vector instead,
1130 ;; a vector describing 0 infos and 0 auxilliary keys.
1131 (let ((newval (funcall update-fn (or (symbol-info-vector symbol)
1132 +nil-packed-infos+))))
1133 (when newval
1134 (setf (symbol-info-vector symbol) newval))
1135 (values)))
1137 ;;; The current *INFO-ENVIRONMENT*, a structure of type INFO-HASHTABLE.
1138 ;;; Cheat by setting to nil before the type is proclaimed
1139 ;;; so that we can then also proclaim ALWAYS-BOUND.
1140 (defvar *info-environment* nil)
1141 #-sb-xc-host
1142 (declaim (type info-hashtable *info-environment*)
1143 (always-bound *info-environment*))
1145 ;;; Update the INFO-NUMBER for NAME in the global environment,
1146 ;;; setting it to NEW-VALUE. This is thread-safe in the presence
1147 ;;; of multiple readers/writers. Threads simultaneously writing data
1148 ;;; for the same NAME will succeed if the info type numbers differ,
1149 ;;; and the winner is indeterminate if the type numbers are the same.
1151 ;;; It is not usually a good idea to think of globaldb as an arbitrary
1152 ;;; key-to-value map supporting rapid or frequent update for a key.
1153 ;;; This is because each update must shallow-copy any data that existed
1154 ;;; for NAME, as a consequence of the very minimal lockfree protocol.
1156 ;;; If, for example, you wanted to track how many times a full-call to
1157 ;;; each global function was emitted during compilation, you could create
1158 ;;; an info-type (:function :full-calls) and the value of that info-type
1159 ;;; could be a cons cell holding an integer. In this way incrementing
1160 ;;; the cell contents does not affecting the globaldb. In contrast,
1161 ;;; (INCF (INFO :function :full-calls myname)) would perform poorly.
1163 ;;; See also ATOMIC-SET-INFO-VALUE and GET-INFO-VALUE-INITIALIZING
1164 ;;; for atomic read/modify/write operations.
1166 ;;; Return the new value so that this can be conveniently used in a
1167 ;;; SETF function.
1168 (defun set-info-value (name info-number new-value)
1169 (when (typep name 'fixnum)
1170 (error "~D is not a legal INFO name." name))
1172 ;; Storage of FAST-METHOD and SLOW-METHOD compiler info is largely pointless.
1173 ;; Why? Because the compiler can't even resolve the "name" of the function in
1174 ;; many cases. If there are EQL specializers involved, it's clearly impossible
1175 ;; because the form X in (EQL x) needs to be evaluated. So most of the data
1176 ;; can't be computed until the file defining the method is loaded. At that
1177 ;; point, you know that the :KIND is :FUNCTION, and :WHERE-FROM is :DEFINED
1178 ;; - they can't be anything else. And you also pretty much know the signature,
1179 ;; since it is based on the specializers, so storing again is a total waste.
1181 ;; FIXME: figure out a way not to store these, or else have globaldb ignore
1182 ;; the calls to set-info-value, and mock any calls to get-info-value
1183 ;; so that it looks like the data were stored, if someone tries to read it.
1184 ;: Or store the data in the method object somehow so that at least dropping
1185 ;; a method can drop the data. This would work because as mentioned above,
1186 ;; there is mostly no such thing as purely compile-time info for methods.
1187 #+nil ; So I can easily re-enable this to figure out what's going on.
1188 (when (and (consp name)
1189 (memq (car name) '(sb!pcl::slow-method sb!pcl::fast-method))
1190 (some #'consp (car (last name))))
1191 (let ((i (aref sb!c::*info-types* info-number)))
1192 (warn "Globaldb storing info for ~S~% ~S ~S~% -> ~S"
1193 name (meta-info-category i) (meta-info-kind i) new-value)))
1195 (let ((name (uncross name)))
1196 ;; If the INFO-NUMBER already exists in VECT, then copy it and
1197 ;; alter one cell; otherwise unpack it, grow the vector, and repack.
1198 (dx-flet ((augment (vect aux-key) ; VECT is a packed vector, never NIL
1199 (declare (simple-vector vect))
1200 (let ((index
1201 (packed-info-value-index vect aux-key info-number)))
1202 (if (not index)
1203 (packed-info-insert vect aux-key info-number new-value)
1204 (let ((copy (copy-seq vect)))
1205 (setf (svref copy index) new-value)
1206 copy)))))
1207 (with-globaldb-name (key1 key2) name
1208 :simple
1209 ;; UPDATE-SYMBOL-INFO never supplies OLD-INFO as NIL.
1210 (dx-flet ((simple-name (old-info) (augment old-info key2)))
1211 (update-symbol-info key1 #'simple-name))
1212 :hairy
1213 ;; INFO-PUTHASH supplies NIL for OLD-INFO if NAME was absent.
1214 (dx-flet ((hairy-name (old-info)
1215 (augment (or old-info +nil-packed-infos+)
1216 +no-auxilliary-key+)))
1217 (info-puthash *info-environment* name #'hairy-name)))))
1218 new-value)
1220 ;; Instead of accepting a new-value, call NEW-VALUE-FUN to compute it
1221 ;; from the existing value. The function receives two arguments:
1222 ;; if there was already a value, that value and T; otherwise two NILs.
1223 ;; Return the newly-computed value. If NEW-VALUE-FUN returns the old value
1224 ;; (compared by EQ) when there was one, then no globaldb update is made.
1225 (defun %atomic-set-info-value (name info-number new-value-fun)
1226 (declare (function new-value-fun))
1227 (when (typep name 'fixnum)
1228 (error "~D is not a legal INFO name." name))
1229 (let ((name (uncross name)) new-value)
1230 (dx-flet ((augment (vect aux-key) ; VECT is a packed vector, never NIL
1231 (declare (simple-vector vect))
1232 (let ((index
1233 (packed-info-value-index vect aux-key info-number)))
1234 (if (not index)
1235 (packed-info-insert
1236 vect aux-key info-number
1237 (setq new-value (funcall new-value-fun nil nil)))
1238 (let ((oldval (svref vect index)))
1239 (setq new-value (funcall new-value-fun oldval t))
1240 (if (eq new-value oldval)
1241 vect ; return the old vector
1242 (let ((copy (copy-seq vect)))
1243 (setf (svref copy index) new-value)
1244 copy)))))))
1245 (with-globaldb-name (key1 key2) name
1246 :simple
1247 ;; UPDATE-SYMBOL-INFO never supplies OLD-INFO as NIL.
1248 (dx-flet ((simple-name (old-info) (augment old-info key2)))
1249 (update-symbol-info key1 #'simple-name))
1250 :hairy
1251 ;; INFO-PUTHASH supplies NIL for OLD-INFO if NAME was absent.
1252 (dx-flet ((hairy-name (old-info)
1253 (augment (or old-info +nil-packed-infos+)
1254 +no-auxilliary-key+)))
1255 (info-puthash *info-environment* name #'hairy-name))))
1256 new-value))
1258 ;; %GET-INFO-VALUE-INITIALIZING is provided as a low-level operation similar
1259 ;; to the above because it does not require info metadata for defaulting,
1260 ;; nor deal with the keyword-based info type designators at all.
1261 ;; In contrast, GET-INFO-VALUE requires metadata.
1262 ;; For this operation to make sense, the objects produced should be permanently
1263 ;; assigned to their name, such as are fdefns and classoid-cells.
1264 ;; Note also that we do not do an initial attempt to read once with INFO,
1265 ;; followed up by a double-checking get-or-set operation. It is assumed that
1266 ;; the user of this already did an initial check, if such is warranted.
1267 (defun %get-info-value-initializing (info-number name creation-thunk)
1268 (when (typep name 'fixnum)
1269 (error "~D is not a legal INFO name." name))
1270 (let ((name (uncross name))
1271 result)
1272 (dx-flet ((get-or-set (info-vect aux-key)
1273 (let ((index
1274 (packed-info-value-index info-vect aux-key info-number)))
1275 (cond (index
1276 (setq result (svref info-vect index))
1277 nil) ; no update to info-vector
1279 ;; Update conflicts possibly for unrelated info-number
1280 ;; can force re-execution. (UNLESS result ...) tries
1281 ;; to avoid calling the thunk more than once.
1282 (unless result
1283 (setq result (funcall creation-thunk)))
1284 (packed-info-insert info-vect aux-key info-number
1285 result))))))
1286 (with-globaldb-name (key1 key2) name
1287 :simple
1288 ;; UPDATE-SYMBOL-INFO never supplies OLD-INFO as NIL.
1289 (dx-flet ((simple-name (old-info) (get-or-set old-info key2)))
1290 (update-symbol-info key1 #'simple-name))
1291 :hairy
1292 ;; INFO-PUTHASH supplies NIL for OLD-INFO if NAME was absent.
1293 (dx-flet ((hairy-name (old-info)
1294 (or (get-or-set (or old-info +nil-packed-infos+)
1295 +no-auxilliary-key+)
1296 ;; Return OLD-INFO to elide writeback. Unlike for
1297 ;; UPDATE-SYMBOL-INFO, NIL is not a no-op marker.
1298 old-info)))
1299 (info-puthash *info-environment* name #'hairy-name))))
1300 result))