1 ;;;; that part of the CMU CL package.lisp file which can run on the
2 ;;;; cross-compilation host
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!IMPL")
15 ;;;; the PACKAGE-HASHTABLE structure
17 ;;; comment from CMU CL:
18 ;;; Packages are implemented using a special kind of hashtable. It is
19 ;;; an open hashtable with a parallel 8-bit I-vector of hash-codes. The
20 ;;; primary purpose of the hash for each entry is to reduce paging by
21 ;;; allowing collisions and misses to be detected without paging in the
22 ;;; symbol and pname for an entry. If the hash for an entry doesn't
23 ;;; match that for the symbol that we are looking for, then we can
24 ;;; go on without touching the symbol, pname, or even hastable vector.
25 ;;; It turns out that, contrary to my expectations, paging is a very
26 ;;; important consideration the design of the package representation.
27 ;;; Using a similar scheme without the entry hash, the fasloader was
28 ;;; spending more than half its time paging in INTERN.
29 ;;; The hash code also indicates the status of an entry. If it zero,
30 ;;; the entry is unused. If it is one, then it is deleted.
31 ;;; Double-hashing is used for collision resolution.
33 (def!type hash-vector () '(simple-array (unsigned-byte 8) (*)))
35 (def!struct (package-hashtable
36 (:constructor %make-package-hashtable
37 (table hash size &aux (free size)))
39 ;; The g-vector of symbols.
40 (table (missing-arg) :type simple-vector)
41 ;; The i-vector of pname hash values.
42 (hash (missing-arg) :type hash-vector)
43 ;; The total number of entries allowed before resizing.
45 ;; FIXME: CAPACITY would be a more descriptive name. (This is
46 ;; related to but not quite the same as HASH-TABLE-SIZE, so calling
47 ;; it SIZE seems somewhat misleading.)
48 (size (missing-arg) :type index)
49 ;; The remaining number of entries that can be made before we have to rehash.
50 (free (missing-arg) :type index)
51 ;; The number of deleted entries.
52 (deleted 0 :type index))
54 ;;;; the PACKAGE structure
56 ;;; KLUDGE: We use DEF!STRUCT to define this not because we need to
57 ;;; manipulate target package objects on the cross-compilation host,
58 ;;; but only because its MAKE-LOAD-FORM function needs to be hooked
59 ;;; into the pre-CLOS DEF!STRUCT MAKE-LOAD-FORM system so that we can
60 ;;; compile things like IN-PACKAGE in warm init before CLOS is set up.
61 ;;; The DEF!STRUCT side effect of defining a new PACKAGE type on the
62 ;;; cross-compilation host is just a nuisance, and in order to avoid
63 ;;; breaking the cross-compilation host, we need to work around it
64 ;;; around by putting the new PACKAGE type (and the PACKAGEP predicate
65 ;;; too..) into SB!XC. -- WHN 20000309
66 (def!struct (sb!xc:package
67 (:constructor internal-make-package)
68 (:make-load-form-fun (lambda (p)
69 (values `(find-undeleted-package-or-lose
72 (:predicate sb!xc:packagep))
74 "the standard structure for the description of a package"
75 ;; the name of the package, or NIL for a deleted package
76 (%name nil :type (or simple-string null))
78 (%nicknames () :type list)
79 ;; packages used by this package
80 (%use-list () :type list)
81 ;; a list of all the hashtables for inherited symbols. This is
82 ;; derived from %USE-LIST, but maintained separately from %USE-LIST
83 ;; for some reason. (Perhaps the reason is that when FIND-SYMBOL*
84 ;; hits an inherited symbol, it pulls it to the head of the list.)
86 ;; FIXME: This needs a more-descriptive name
87 ;; (USED-PACKAGE-HASH-TABLES?). It also needs an explanation of why
88 ;; the last entry is NIL. Perhaps it should even just go away and
89 ;; let us do indirection on the fly through %USE-LIST. (If so,
90 ;; benchmark to make sure that performance doesn't get stomped..)
91 ;; (If benchmark performance is important, this should prob'ly
92 ;; become a vector instead of a list.)
93 (tables (list nil) :type list)
94 ;; packages that use this package
95 (%used-by-list () :type list)
96 ;; PACKAGE-HASHTABLEs of internal & external symbols
97 (internal-symbols (missing-arg) :type package-hashtable)
98 (external-symbols (missing-arg) :type package-hashtable)
100 (%shadowing-symbols () :type list)
101 ;; documentation string for this package
102 (doc-string nil :type (or simple-string null))
105 (lock nil :type boolean)
107 (%implementation-packages nil :type list)
108 ;; Definition source location
109 (source-location nil :type (or null sb!c:definition-source-location))
110 ;; Local package nicknames.
111 (%local-nicknames nil :type list)
112 (%locally-nicknamed-by nil :type list))
114 ;;;; iteration macros
116 (defmacro-mundanely do-symbols ((var &optional
121 "DO-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECLARATION}* {TAG | FORM}*
122 Executes the FORMs at least once for each symbol accessible in the given
123 PACKAGE with VAR bound to the current symbol."
124 (multiple-value-bind (body decls)
125 (parse-body body-decls :doc-string-allowed nil)
126 (let ((flet-name (sb!xc:gensym "DO-SYMBOLS-")))
128 (flet ((,flet-name (,var)
131 (let* ((package (find-undeleted-package-or-lose ,package))
132 (shadows (package-%shadowing-symbols package)))
133 (flet ((iterate-over-hash-table (table ignore)
134 (let ((hash-vec (package-hashtable-hash table))
135 (sym-vec (package-hashtable-table table)))
136 (dotimes (i (length sym-vec))
137 (when (>= (aref hash-vec i) 2)
138 (let ((sym (aref sym-vec i)))
139 (declare (inline member))
140 (unless (member sym ignore :test #'string=)
141 (,flet-name sym))))))))
142 (iterate-over-hash-table (package-internal-symbols package) nil)
143 (iterate-over-hash-table (package-external-symbols package) nil)
144 (dolist (use (package-%use-list package))
145 (iterate-over-hash-table (package-external-symbols use)
148 (declare (ignorable ,var))
152 (defmacro-mundanely do-external-symbols ((var &optional
157 "DO-EXTERNAL-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECL}* {TAG | FORM}*
158 Executes the FORMs once for each external symbol in the given PACKAGE with
159 VAR bound to the current symbol."
160 (multiple-value-bind (body decls)
161 (parse-body body-decls :doc-string-allowed nil)
162 (let ((flet-name (sb!xc:gensym "DO-SYMBOLS-")))
164 (flet ((,flet-name (,var)
167 (let* ((package (find-undeleted-package-or-lose ,package))
168 (table (package-external-symbols package))
169 (hash-vec (package-hashtable-hash table))
170 (sym-vec (package-hashtable-table table)))
171 (dotimes (i (length sym-vec))
172 (when (>= (aref hash-vec i) 2)
173 (,flet-name (aref sym-vec i))))))
175 (declare (ignorable ,var))
179 (defmacro-mundanely do-all-symbols ((var &optional
183 "DO-ALL-SYMBOLS (VAR [RESULT-FORM]) {DECLARATION}* {TAG | FORM}*
184 Executes the FORMs once for each symbol in every package with VAR bound
185 to the current symbol."
186 (multiple-value-bind (body decls)
187 (parse-body body-decls :doc-string-allowed nil)
188 (let ((flet-name (sb!xc:gensym "DO-SYMBOLS-")))
190 (flet ((,flet-name (,var)
193 (dolist (package (list-all-packages))
194 (flet ((iterate-over-hash-table (table)
195 (let ((hash-vec (package-hashtable-hash table))
196 (sym-vec (package-hashtable-table table)))
197 (dotimes (i (length sym-vec))
198 (when (>= (aref hash-vec i) 2)
199 (,flet-name (aref sym-vec i)))))))
200 (iterate-over-hash-table (package-internal-symbols package))
201 (iterate-over-hash-table (package-external-symbols package)))))
203 (declare (ignorable ,var))
207 ;;;; WITH-PACKAGE-ITERATOR
209 (defmacro-mundanely with-package-iterator ((mname package-list
213 "Within the lexical scope of the body forms, MNAME is defined via macrolet
214 such that successive invocations of (MNAME) will return the symbols, one by
215 one, from the packages in PACKAGE-LIST. SYMBOL-TYPES may be any
216 of :INHERITED :EXTERNAL :INTERNAL."
217 (with-unique-names (packages these-packages counter kind hash-vector vector
218 package-use-list init-macro end-test-macro real-symbol-p
219 inherited-symbol-p BLOCK)
220 (let ((ordered-types (let ((res nil))
221 (dolist (kind '(:inherited :external :internal) res)
222 (when (member kind symbol-types)
223 (push kind res)))))) ; Order SYMBOL-TYPES.
224 `(let* ((,these-packages ,package-list)
225 (,packages `,(mapcar (lambda (package)
226 (if (packagep package)
228 ;; Maybe FIND-PACKAGE-OR-DIE?
229 (or (find-package package)
230 (error 'simple-package-error
231 ;; could be a character
232 :package (string package)
233 :format-control "~@<~S does not name a package ~:>"
234 :format-arguments (list package)))))
235 (if (consp ,these-packages)
237 (list ,these-packages))))
239 (,kind (car ,packages))
242 (,package-use-list nil))
243 ,(if (member :inherited ordered-types)
244 `(setf ,package-use-list (package-%use-list (car ,packages)))
245 `(declare (ignore ,package-use-list)))
246 (macrolet ((,init-macro (next-kind)
247 (declare (optimize (inhibit-warnings 3)))
248 (let ((symbols (gensym)))
250 (setf ,',kind ,next-kind)
251 (setf ,',counter nil)
254 `(let ((,symbols (package-internal-symbols
257 (setf ,',vector (package-hashtable-table ,symbols))
259 (package-hashtable-hash ,symbols)))))
261 `(let ((,symbols (package-external-symbols
264 (setf ,',vector (package-hashtable-table ,symbols))
266 (package-hashtable-hash ,symbols)))))
268 `(let ((,symbols (and ,',package-use-list
269 (package-external-symbols
270 (car ,',package-use-list)))))
272 (setf ,',vector (package-hashtable-table ,symbols))
274 (package-hashtable-hash ,symbols)))))))))
275 (,end-test-macro (this-kind)
276 `,(let ((next-kind (cadr (member this-kind
279 `(,',init-macro ,next-kind)
280 `(if (endp (setf ,',packages (cdr ,',packages)))
281 (return-from ,',BLOCK)
282 (,',init-macro ,(car ',ordered-types)))))))
284 ,(when (null symbol-types)
285 (error 'simple-program-error
287 "At least one of :INTERNAL, :EXTERNAL, or ~
288 :INHERITED must be supplied."))
289 ,(dolist (symbol symbol-types)
290 (unless (member symbol '(:internal :external :inherited))
291 (error 'simple-program-error
293 "~S is not one of :INTERNAL, :EXTERNAL, or :INHERITED."
294 :format-arguments (list symbol))))
295 (,init-macro ,(car ordered-types))
296 (flet ((,real-symbol-p (number)
298 (macrolet ((,mname ()
299 (declare (optimize (inhibit-warnings 3)))
303 ,@(when (member :internal ',ordered-types)
306 (position-if #',',real-symbol-p
307 (the hash-vector ,',hash-vector)
308 :start (if ,',counter
312 (return-from ,',BLOCK
313 (values t (svref ,',vector ,',counter)
314 ,',kind (car ,',packages)))
315 (,',end-test-macro :internal)))))
316 ,@(when (member :external ',ordered-types)
319 (position-if #',',real-symbol-p
320 (the hash-vector ,',hash-vector)
321 :start (if ,',counter
325 (return-from ,',BLOCK
326 (values t (svref ,',vector ,',counter)
327 ,',kind (car ,',packages)))
328 (,',end-test-macro :external)))))
329 ,@(when (member :inherited ',ordered-types)
331 (flet ((,',inherited-symbol-p (number)
332 (when (,',real-symbol-p number)
337 :start (if ,',counter
340 (s (svref ,',vector p)))
348 (position-if #',',inherited-symbol-p
351 :start (if ,',counter
357 (values t (svref ,',vector ,',counter)
358 ,',kind (car ,',packages))
361 (setf ,',package-use-list
362 (cdr ,',package-use-list))
363 (cond ((endp ,',package-use-list)
364 (setf ,',packages (cdr ,',packages))
365 (when (endp ,',packages)
366 (return-from ,',BLOCK))
367 (setf ,',package-use-list
372 (t (,',init-macro :inherited)
373 (setf ,',counter nil)))))))))))))
376 (defmacro-mundanely with-package-graph ((&key) &body forms)
377 `(flet ((thunk () ,@forms))
378 (declare (dynamic-extent #'thunk))
379 (call-with-package-graph #'thunk)))