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 (sb!xc:deftype hash-vector () '(simple-array (unsigned-byte 8) (*)))
35 (sb!xc:defstruct (package-hashtable (:constructor %make-package-hashtable ())
37 ;; The g-vector of symbols.
38 ;; FIXME: could just be type SIMPLE-VECTOR, with (MISSING-ARG) default
39 (table nil :type (or simple-vector null))
40 ;; The i-vector of pname hash values.
41 ;; FIXME: could just be type HASH-VECTOR, with (MISSING-ARG) default
42 (hash nil :type (or hash-vector null))
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.)
49 ;; The remaining number of entries that can be made before we have to rehash.
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. The DEF!STRUCT
60 ;;; side-effect of defining a new PACKAGE type on the
61 ;;; cross-compilation host is just a nuisance, and in order to avoid
62 ;;; breaking the cross-compilation host, we need to work around it
63 ;;; around by putting the new PACKAGE type (and the PACKAGEP predicate
64 ;;; too..) into SB!XC. -- WHN 20000309
65 (def!struct (sb!xc:package
66 (:constructor internal-make-package)
67 (:make-load-form-fun (lambda (p)
68 (values `(find-undeleted-package-or-lose
71 (:predicate sb!xc:packagep))
73 "the standard structure for the description of a package"
74 ;; the name of the package, or NIL for a deleted package
75 (%name nil :type (or simple-string null))
77 (%nicknames () :type list)
78 ;; packages used by this package
79 (%use-list () :type list)
80 ;; a list of all the hashtables for inherited symbols. This is
81 ;; derived from %USE-LIST, but maintained separately from %USE-LIST
82 ;; for some reason. (Perhaps the reason is that when FIND-SYMBOL*
83 ;; hits an inherited symbol, it pulls it to the head of the list.)
85 ;; FIXME: This needs a more-descriptive name
86 ;; (USED-PACKAGE-HASH-TABLES?). It also needs an explanation of why
87 ;; the last entry is NIL. Perhaps it should even just go away and
88 ;; let us do indirection on the fly through %USE-LIST. (If so,
89 ;; benchmark to make sure that performance doesn't get stomped..)
90 ;; (If benchmark performance is important, this should prob'ly
91 ;; become a vector instead of a list.)
92 (tables (list nil) :type list)
93 ;; packages that use this package
94 (%used-by-list () :type list)
95 ;; PACKAGE-HASHTABLEs of internal & external symbols
96 (internal-symbols (missing-arg) :type package-hashtable)
97 (external-symbols (missing-arg) :type package-hashtable)
99 (%shadowing-symbols () :type list)
100 ;; documentation string for this package
101 (doc-string nil :type (or simple-string null)))
103 ;;;; iteration macros
105 (defmacro-mundanely do-symbols ((var &optional
110 "DO-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECLARATION}* {TAG | FORM}*
111 Executes the FORMs at least once for each symbol accessible in the given
112 PACKAGE with VAR bound to the current symbol."
113 (multiple-value-bind (body decls) body-decls
114 (let ((flet-name (gensym "DO-SYMBOLS-")))
116 (flet ((,flet-name (,var)
119 (let* ((package (find-undeleted-package-or-lose ,package))
120 (shadows (package-%shadowing-symbols package)))
121 (flet ((iterate-over-hash-table (table ignore)
122 (let ((hash-vec (package-hashtable-hash table))
123 (sym-vec (package-hashtable-table table)))
124 (declare (type (simple-array (unsigned-byte 8) (*))
126 (type simple-vector sym-vec))
127 (dotimes (i (length sym-vec))
128 (when (>= (aref hash-vec i) 2)
129 (let ((sym (aref sym-vec i)))
130 (declare (inline member))
131 (unless (member sym ignore :test #'string=)
132 (,flet-name sym))))))))
133 (iterate-over-hash-table (package-internal-symbols package) nil)
134 (iterate-over-hash-table (package-external-symbols package) nil)
135 (dolist (use (package-%use-list package))
136 (iterate-over-hash-table (package-external-symbols use)
139 (declare (ignorable ,var))
143 (defmacro-mundanely do-external-symbols ((var &optional
148 "DO-EXTERNAL-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECL}* {TAG | FORM}*
149 Executes the FORMs once for each external symbol in the given PACKAGE with
150 VAR bound to the current symbol."
151 (multiple-value-bind (body decls) (parse-body body-decls nil)
152 (let ((flet-name (gensym "DO-SYMBOLS-")))
154 (flet ((,flet-name (,var)
157 (let* ((package (find-undeleted-package-or-lose ,package))
158 (table (package-external-symbols package))
159 (hash-vec (package-hashtable-hash table))
160 (sym-vec (package-hashtable-table table)))
161 (declare (type (simple-array (unsigned-byte 8) (*))
163 (type simple-vector sym-vec))
164 (dotimes (i (length sym-vec))
165 (when (>= (aref hash-vec i) 2)
166 (,flet-name (aref sym-vec i))))))
168 (declare (ignorable ,var))
172 (defmacro-mundanely do-all-symbols ((var &optional
176 "DO-ALL-SYMBOLS (VAR [RESULT-FORM]) {DECLARATION}* {TAG | FORM}*
177 Executes the FORMs once for each symbol in every package with VAR bound
178 to the current symbol."
179 (multiple-value-bind (body decls) (parse-body body-decls nil)
180 (let ((flet-name (gensym "DO-SYMBOLS-")))
182 (flet ((,flet-name (,var)
185 (dolist (package (list-all-packages))
186 (flet ((iterate-over-hash-table (table)
187 (let ((hash-vec (package-hashtable-hash table))
188 (sym-vec (package-hashtable-table table)))
189 (declare (type (simple-array (unsigned-byte 8) (*))
191 (type simple-vector sym-vec))
192 (dotimes (i (length sym-vec))
193 (when (>= (aref hash-vec i) 2)
194 (,flet-name (aref sym-vec i)))))))
195 (iterate-over-hash-table (package-internal-symbols package))
196 (iterate-over-hash-table (package-external-symbols package)))))
198 (declare (ignorable ,var))
202 ;;;; WITH-PACKAGE-ITERATOR
204 (defmacro-mundanely with-package-iterator ((mname package-list
208 "Within the lexical scope of the body forms, MNAME is defined via macrolet
209 such that successive invocations of (MNAME) will return the symbols,
210 one by one, from the packages in PACKAGE-LIST. SYMBOL-TYPES may be
211 any of :INHERITED :EXTERNAL :INTERNAL."
212 (let* ((packages (gensym))
213 (these-packages (gensym))
214 (ordered-types (let ((res nil))
215 (dolist (kind '(:inherited :external :internal)
217 (when (member kind symbol-types)
218 (push kind res))))) ; Order SYMBOL-TYPES.
221 (hash-vector (gensym))
223 (package-use-list (gensym))
224 (init-macro (gensym))
225 (end-test-macro (gensym))
226 (real-symbol-p (gensym))
227 (inherited-symbol-p (gensym))
229 `(let* ((,these-packages ,package-list)
230 (,packages `,(mapcar (lambda (package)
231 (if (packagep package)
233 (find-package package)))
234 (if (consp ,these-packages)
236 (list ,these-packages))))
238 (,kind (car ,packages))
241 (,package-use-list nil))
242 ,(if (member :inherited ordered-types)
243 `(setf ,package-use-list (package-%use-list (car ,packages)))
244 `(declare (ignore ,package-use-list)))
245 (macrolet ((,init-macro (next-kind)
246 (let ((symbols (gensym)))
248 (setf ,',kind ,next-kind)
249 (setf ,',counter nil)
252 `(let ((,symbols (package-internal-symbols
255 (setf ,',vector (package-hashtable-table ,symbols))
257 (package-hashtable-hash ,symbols)))))
259 `(let ((,symbols (package-external-symbols
262 (setf ,',vector (package-hashtable-table ,symbols))
264 (package-hashtable-hash ,symbols)))))
266 `(let ((,symbols (and ,',package-use-list
267 (package-external-symbols
268 (car ,',package-use-list)))))
270 (setf ,',vector (package-hashtable-table ,symbols))
272 (package-hashtable-hash ,symbols)))))))))
273 (,end-test-macro (this-kind)
274 `,(let ((next-kind (cadr (member this-kind
277 `(,',init-macro ,next-kind)
278 `(if (endp (setf ,',packages (cdr ,',packages)))
279 (return-from ,',BLOCK)
280 (,',init-macro ,(car ',ordered-types)))))))
282 ,(when (null symbol-types)
283 (error 'simple-program-error
285 "At least one of :INTERNAL, :EXTERNAL, or ~
286 :INHERITED must be supplied."))
287 ,(dolist (symbol symbol-types)
288 (unless (member symbol '(:internal :external :inherited))
289 (error 'program-error
291 "~S is not one of :INTERNAL, :EXTERNAL, or :INHERITED."
292 :format-argument symbol)))
293 (,init-macro ,(car ordered-types))
294 (flet ((,real-symbol-p (number)
296 (macrolet ((,mname ()
300 ,@(when (member :internal ',ordered-types)
303 (position-if #',',real-symbol-p
305 :start (if ,',counter
309 (return-from ,',BLOCK
310 (values t (svref ,',vector ,',counter)
311 ,',kind (car ,',packages)))
312 (,',end-test-macro :internal)))))
313 ,@(when (member :external ',ordered-types)
316 (position-if #',',real-symbol-p
318 :start (if ,',counter
322 (return-from ,',BLOCK
323 (values t (svref ,',vector ,',counter)
324 ,',kind (car ,',packages)))
325 (,',end-test-macro :external)))))
326 ,@(when (member :inherited ',ordered-types)
328 (flet ((,',inherited-symbol-p (number)
329 (when (,',real-symbol-p number)
331 number ,',hash-vector
332 :start (if ,',counter
335 (s (svref ,',vector p)))
342 (position-if #',',inherited-symbol-p
344 :start (if ,',counter
350 (values t (svref ,',vector ,',counter)
351 ,',kind (car ,',packages))
354 (setf ,',package-use-list
355 (cdr ,',package-use-list))
356 (cond ((endp ,',package-use-list)
357 (setf ,',packages (cdr ,',packages))
358 (when (endp ,',packages)
359 (return-from ,',BLOCK))
360 (setf ,',package-use-list
365 (t (,',init-macro :inherited)
366 (setf ,',counter nil)))))))))))))