12381ca71ef4a934b741105af389ed52b66fec26
[sbcl.git] / src / compiler / dump.lisp
1 ;;;; stuff that knows about dumping FASL files
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!C")
13
14 ;;; FIXME: Double colons are bad, and there are lots of them in this
15 ;;; file, because both dump logic in SB!C and load logic in SB!IMPL
16 ;;; need to know about fops. Perhaps all the load/dump logic should be
17 ;;; moved into a single package, perhaps called SB-LD.
18 \f
19 ;;;; fasl dumper state
20
21 ;;; The FASL-FILE structure represents everything we need to know
22 ;;; about dumping to a fasl file. We need to objectify the state,
23 ;;; since the fasdumper must be reentrant.
24 (defstruct (fasl-file
25             #-no-ansi-print-object
26             (:print-object (lambda (x s)
27                              (print-unreadable-object (x s :type t)
28                                (prin1 (namestring (fasl-file-stream x)) s)))))
29   ;; The stream we dump to.
30   (stream (required-argument) :type stream)
31   ;; Hashtables we use to keep track of dumped constants so that we
32   ;; can get them from the table rather than dumping them again. The
33   ;; EQUAL-TABLE is used for lists and strings, and the EQ-TABLE is
34   ;; used for everything else. We use a separate EQ table to avoid
35   ;; performance patholigies with objects for which EQUAL degnerates
36   ;; to EQL. Everything entered in the EQUAL table is also entered in
37   ;; the EQ table.
38   (equal-table (make-hash-table :test 'equal) :type hash-table)
39   (eq-table (make-hash-table :test 'eq) :type hash-table)
40   ;; The table's current free pointer: the next offset to be used.
41   (table-free 0 :type index)
42   ;; an alist (PACKAGE . OFFSET) of the table offsets for each package
43   ;; we have currently located.
44   (packages () :type list)
45   ;; a table mapping from the Entry-Info structures for dumped XEPs to
46   ;; the table offsets of the corresponding code pointers
47   (entry-table (make-hash-table :test 'eq) :type hash-table)
48   ;; a table holding back-patching info for forward references to XEPs.
49   ;; The key is the Entry-Info structure for the XEP, and the value is
50   ;; a list of conses (<code-handle> . <offset>), where <code-handle>
51   ;; is the offset in the table of the code object needing to be
52   ;; patched, and <offset> is the offset that must be patched.
53   (patch-table (make-hash-table :test 'eq) :type hash-table)
54   ;; a list of the table handles for all of the DEBUG-INFO structures
55   ;; dumped in this file. These structures must be back-patched with
56   ;; source location information when the compilation is complete.
57   (debug-info () :type list)
58   ;; This is used to keep track of objects that we are in the process
59   ;; of dumping so that circularities can be preserved. The key is the
60   ;; object that we have previously seen, and the value is the object
61   ;; that we reference in the table to find this previously seen
62   ;; object. (The value is never NIL.)
63   ;;
64   ;; Except with list objects, the key and the value are always the
65   ;; same. In a list, the key will be some tail of the value.
66   (circularity-table (make-hash-table :test 'eq) :type hash-table)
67   ;; a hash table of structures that are allowed to be dumped. If we
68   ;; try to dump a structure that isn't in this hash table, we lose.
69   (valid-structures (make-hash-table :test 'eq) :type hash-table))
70
71 ;;; This structure holds information about a circularity.
72 (defstruct circularity
73   ;; the kind of modification to make to create circularity
74   (type (required-argument) :type (member :rplaca :rplacd :svset :struct-set))
75   ;; the object containing circularity
76   object
77   ;; index in object for circularity
78   (index (required-argument) :type index)
79   ;; the object to be stored at INDEX in OBJECT. This is that the key
80   ;; that we were using when we discovered the circularity.
81   value
82   ;; the value that was associated with VALUE in the
83   ;; CIRCULARITY-TABLE. This is the object that we look up in the
84   ;; EQ-TABLE to locate VALUE.
85   enclosing-object)
86
87 ;;; a list of the CIRCULARITY structures for all of the circularities
88 ;;; detected in the current top-level call to DUMP-OBJECT. Setting
89 ;;; this lobotomizes circularity detection as well, since circular
90 ;;; dumping uses the table.
91 (defvar *circularities-detected*)
92
93 ;;; used to inhibit table access when dumping forms to be read by the
94 ;;; cold loader
95 (defvar *cold-load-dump* nil)
96
97 ;;; used to turn off the structure validation during dumping of source
98 ;;; info
99 (defvar *dump-only-valid-structures* t)
100 ;;;; utilities
101
102 ;;; Write the byte B to the specified fasl-file stream.
103 (defun dump-byte (b fasl-file)
104   (declare (type (unsigned-byte 8) b) (type fasl-file fasl-file))
105   (write-byte b (fasl-file-stream fasl-file)))
106
107 ;;; Dump a 4 byte unsigned integer.
108 (defun dump-unsigned-32 (num fasl-file)
109   (declare (type (unsigned-byte 32) num) (type fasl-file fasl-file))
110   (let ((stream (fasl-file-stream fasl-file)))
111     (dotimes (i 4)
112       (write-byte (ldb (byte 8 (* 8 i)) num) stream))))
113
114 ;;; Dump NUM to the fasl stream, represented by N bytes. This works for either
115 ;;; signed or unsigned integers. There's no range checking -- if you don't
116 ;;; specify enough bytes for the number to fit, this function cheerfully
117 ;;; outputs the low bytes.
118 (defun dump-integer-as-n-bytes  (num bytes file)
119   (declare (integer num) (type index bytes) (type fasl-file file))
120   (do ((n num (ash n -8))
121        (i bytes (1- i)))
122       ((= i 0))
123     (declare (type index i))
124     (dump-byte (logand n #xff) file))
125   (values))
126
127 ;;; Setting this variable to an (UNSIGNED-BYTE 32) value causes DUMP-FOP to use
128 ;;; it as a counter and emit a FOP-NOP4 with the counter value before every
129 ;;; ordinary fop. This can make it easier to follow the progress of FASLOAD
130 ;;; when debugging/testing/experimenting.
131 #!+sb-show (defvar *fop-nop4-count* 0)
132 #!+sb-show (declaim (type (or (unsigned-byte 32) null) *fop-nop4-count*))
133 ;;; FIXME: The default value here should become NIL once I get the system to
134 ;;; run.
135
136 ;;; Dump the FOP code for the named FOP to the specified fasl-file.
137 ;;;
138 ;;; FIXME: This should be a function, with a compiler macro expansion for the
139 ;;; common constant-FS case. (Among other things, that'll stop it from
140 ;;; EVALing ,FILE multiple times.)
141 ;;;
142 ;;; FIXME: Compiler macros, frozen classes, inlining, and similar optimizations
143 ;;; should be conditional on #!+SB-FROZEN.
144 (defmacro dump-fop (fs file)
145   (let* ((fs (eval fs))
146          (val (get fs 'sb!impl::fop-code)))
147     (if val
148       `(progn
149          #!+sb-show
150          (when *fop-nop4-count*
151            (dump-byte ,(get 'sb!impl::fop-nop4 'sb!impl::fop-code) ,file)
152            (dump-unsigned-32 (mod (incf *fop-nop4-count*) (expt 2 32)) ,file))
153          (dump-byte ',val ,file))
154       (error "compiler bug: ~S is not a legal fasload operator." fs))))
155
156 ;;; Dump a FOP-Code along with an integer argument, choosing the FOP based
157 ;;; on whether the argument will fit in a single byte.
158 ;;;
159 ;;; FIXME: This, like DUMP-FOP, should be a function with a compiler-macro
160 ;;; expansion.
161 (defmacro dump-fop* (n byte-fop word-fop file)
162   (once-only ((n-n n)
163               (n-file file))
164     `(cond ((< ,n-n 256)
165             (dump-fop ',byte-fop ,n-file)
166             (dump-byte ,n-n ,n-file))
167            (t
168             (dump-fop ',word-fop ,n-file)
169             (dump-unsigned-32 ,n-n ,n-file)))))
170
171 ;;; Push the object at table offset Handle on the fasl stack.
172 (defun dump-push (handle file)
173   (declare (type index handle) (type fasl-file file))
174   (dump-fop* handle sb!impl::fop-byte-push sb!impl::fop-push file)
175   (values))
176
177 ;;; Pop the object currently on the fasl stack top into the table, and
178 ;;; return the table index, incrementing the free pointer.
179 (defun dump-pop (file)
180   (prog1
181       (fasl-file-table-free file)
182     (dump-fop 'sb!impl::fop-pop file)
183     (incf (fasl-file-table-free file))))
184
185 ;;; If X is in File's EQUAL-TABLE, then push the object and return T,
186 ;;; otherwise NIL. If *COLD-LOAD-DUMP* is true, then do nothing and
187 ;;; return NIL.
188 (defun equal-check-table (x file)
189   (declare (type fasl-file file))
190   (unless *cold-load-dump*
191     (let ((handle (gethash x (fasl-file-equal-table file))))
192       (cond (handle
193              (dump-push handle file)
194              t)
195             (t
196              nil)))))
197
198 ;;; These functions are called after dumping an object to save the
199 ;;; object in the table. The object (also passed in as X) must already
200 ;;; be on the top of the FOP stack. If *COLD-LOAD-DUMP* is true, then
201 ;;; we don't do anything.
202 (defun eq-save-object (x file)
203   (declare (type fasl-file file))
204   (unless *cold-load-dump*
205     (let ((handle (dump-pop file)))
206       (setf (gethash x (fasl-file-eq-table file)) handle)
207       (dump-push handle file)))
208   (values))
209 (defun equal-save-object (x file)
210   (declare (type fasl-file file))
211   (unless *cold-load-dump*
212     (let ((handle (dump-pop file)))
213       (setf (gethash x (fasl-file-equal-table file)) handle)
214       (setf (gethash x (fasl-file-eq-table file)) handle)
215       (dump-push handle file)))
216   (values))
217
218 ;;; Record X in File's CIRCULARITY-TABLE unless *COLD-LOAD-DUMP* is
219 ;;; true. This is called on objects that we are about to dump might
220 ;;; have a circular path through them.
221 ;;;
222 ;;; The object must not currently be in this table, since the dumper
223 ;;; should never be recursively called on a circular reference.
224 ;;; Instead, the dumping function must detect the circularity and
225 ;;; arrange for the dumped object to be patched.
226 (defun note-potential-circularity (x file)
227   (unless *cold-load-dump*
228     (let ((circ (fasl-file-circularity-table file)))
229       (assert (not (gethash x circ)))
230       (setf (gethash x circ) x)))
231   (values))
232
233 ;;; Dump FORM to a fasl file so that it evaluated at load time in normal
234 ;;; load and at cold-load time in cold load. This is used to dump package
235 ;;; frobbing forms.
236 (defun fasl-dump-cold-load-form (form file)
237   (declare (type fasl-file file))
238   (dump-fop 'sb!impl::fop-normal-load file)
239   (let ((*cold-load-dump* t))
240     (dump-object form file))
241   (dump-fop 'sb!impl::fop-eval-for-effect file)
242   (dump-fop 'sb!impl::fop-maybe-cold-load file)
243   (values))
244 \f
245 ;;;; opening and closing fasl files
246
247 ;;; Open a fasl file, write its header, and return a FASL-FILE object for
248 ;;; dumping to it. Some human-readable information about the source code is
249 ;;; given by the string WHERE. If BYTE-P is true, this file will contain no
250 ;;; native code, and is thus largely implementation independent.
251 (defun open-fasl-file (name where &optional byte-p)
252   (declare (type pathname name))
253   (let* ((stream (open name
254                        :direction :output
255                        :if-exists :new-version
256                        :element-type 'sb!assem:assembly-unit))
257          (res (make-fasl-file :stream stream)))
258
259     ;; Begin the header with the constant machine-readable (and
260     ;; semi-human-readable) string which is used to identify fasl files.
261     (write-string sb!c:*fasl-header-string-start-string* stream)
262
263     ;; The constant string which begins the header is followed by arbitrary
264     ;; human-readable text, terminated by a special character code.
265     (with-standard-io-syntax
266      (format stream
267              "~%  ~
268              compiled from ~S~%  ~
269              at ~A~%  ~
270              on ~A~%  ~
271              using ~A version ~A~%"
272              where
273              (format-universal-time nil (get-universal-time))
274              (machine-instance)
275              (sb!xc:lisp-implementation-type)
276              (sb!xc:lisp-implementation-version)))
277     (dump-byte sb!c:*fasl-header-string-stop-char-code* res)
278
279     ;; Finish the header by outputting fasl file implementation and version in
280     ;; machine-readable form.
281     (multiple-value-bind (implementation version)
282         (if byte-p
283             (values *backend-byte-order*
284                     byte-fasl-file-version)
285             (values *backend-fasl-file-implementation*
286                     *backend-fasl-file-version*))
287       (dump-unsigned-32 (length (symbol-name implementation)) res)
288       (dotimes (i (length (symbol-name implementation)))
289         (dump-byte (char-code (aref (symbol-name implementation) i)) res))
290       (dump-unsigned-32 version res))
291
292     res))
293
294 ;;; Close the specified FASL-FILE, aborting the write if ABORT-P.
295 ;;; We do various sanity checks, then end the group.
296 (defun close-fasl-file (file abort-p)
297   (declare (type fasl-file file))
298   (assert (zerop (hash-table-count (fasl-file-patch-table file))))
299   (dump-fop 'sb!impl::fop-verify-empty-stack file)
300   (dump-fop 'sb!impl::fop-verify-table-size file)
301   (dump-unsigned-32 (fasl-file-table-free file) file)
302   (dump-fop 'sb!impl::fop-end-group file)
303   (close (fasl-file-stream file) :abort abort-p)
304   (values))
305 \f
306 ;;;; main entries to object dumping
307
308 ;;; This function deals with dumping objects that are complex enough so that
309 ;;; we want to cache them in the table, rather than repeatedly dumping them.
310 ;;; If the object is in the EQ-TABLE, then we push it, otherwise, we do a type
311 ;;; dispatch to a type specific dumping function. The type specific branches
312 ;;; do any appropriate EQUAL-TABLE check and table entry.
313 ;;;
314 ;;; When we go to dump the object, we enter it in the CIRCULARITY-TABLE.
315 (defun dump-non-immediate-object (x file)
316   (let ((index (gethash x (fasl-file-eq-table file))))
317     (cond ((and index (not *cold-load-dump*))
318            (dump-push index file))
319           (t
320            (typecase x
321              (symbol (dump-symbol x file))
322              (list
323               (unless (equal-check-table x file)
324                 (dump-list x file)
325                 (equal-save-object x file)))
326              (layout
327               (dump-layout x file)
328               (eq-save-object x file))
329              (instance
330               (dump-structure x file)
331               (eq-save-object x file))
332              (array
333               ;; FIXME: The comment at the head of DUMP-NON-IMMEDIATE-OBJECT
334               ;; says it's for objects which we want to save, instead of
335               ;; repeatedly dumping them. But then we dump arrays here without
336               ;; doing anything a la EQUAL-SAVE-OBJECT. What gives?
337               (dump-array x file))
338              (number
339               (unless (equal-check-table x file)
340                 (etypecase x
341                   (ratio (dump-ratio x file))
342                   (complex (dump-complex x file))
343                   (float (dump-float x file))
344                   (integer (dump-integer x file)))
345                 (equal-save-object x file)))
346              (t
347               ;; This probably never happens, since bad things tend to be
348               ;; detected during IR1 conversion.
349               (error "This object cannot be dumped into a fasl file:~% ~S"
350                      x))))))
351   (values))
352
353 ;;; Dump an object of any type by dispatching to the correct type-specific
354 ;;; dumping function. We pick off immediate objects, symbols and and magic
355 ;;; lists here. Other objects are handled by Dump-Non-Immediate-Object.
356 ;;;
357 ;;; This is the function used for recursive calls to the fasl dumper. We don't
358 ;;; worry about creating circularities here, since it is assumed that there is
359 ;;; a top-level call to Dump-Object.
360 (defun sub-dump-object (x file)
361   (cond ((listp x)
362          (if x
363              (dump-non-immediate-object x file)
364              (dump-fop 'sb!impl::fop-empty-list file)))
365         ((symbolp x)
366          (if (eq x t)
367              (dump-fop 'sb!impl::fop-truth file)
368              (dump-non-immediate-object x file)))
369         ((target-fixnump x) (dump-integer x file))
370         ((characterp x) (dump-character x file))
371         (t
372          (dump-non-immediate-object x file))))
373
374 ;;; Dump stuff to backpatch already dumped objects. Infos is the list of
375 ;;; Circularity structures describing what to do. The patching FOPs take the
376 ;;; value to store on the stack. We compute this value by fetching the
377 ;;; enclosing object from the table, and then CDR'ing it if necessary.
378 (defun dump-circularities (infos file)
379   (let ((table (fasl-file-eq-table file)))
380     (dolist (info infos)
381       (let* ((value (circularity-value info))
382              (enclosing (circularity-enclosing-object info)))
383         (dump-push (gethash enclosing table) file)
384         (unless (eq enclosing value)
385           (do ((current enclosing (cdr current))
386                (i 0 (1+ i)))
387               ((eq current value)
388                (dump-fop 'sb!impl::fop-nthcdr file)
389                (dump-unsigned-32 i file))
390             (declare (type index i)))))
391
392       (ecase (circularity-type info)
393         (:rplaca (dump-fop 'sb!impl::fop-rplaca file))
394         (:rplacd (dump-fop 'sb!impl::fop-rplacd file))
395         (:svset (dump-fop 'sb!impl::fop-svset file))
396         (:struct-set (dump-fop 'sb!impl::fop-structset file)))
397       (dump-unsigned-32 (gethash (circularity-object info) table) file)
398       (dump-unsigned-32 (circularity-index info) file))))
399
400 ;;; Set up stuff for circularity detection, then dump an object. All shared
401 ;;; and circular structure will be exactly preserved within a single call to
402 ;;; Dump-Object. Sharing between objects dumped by separate calls is only
403 ;;; preserved when convenient.
404 ;;;
405 ;;; We peek at the object type so that we only pay the circular detection
406 ;;; overhead on types of objects that might be circular.
407 (defun dump-object (x file)
408   (if (or (array-header-p x)
409           (simple-vector-p x)
410           (consp x)
411           (typep x 'instance))
412       (let ((*circularities-detected* ())
413             (circ (fasl-file-circularity-table file)))
414         (clrhash circ)
415         (sub-dump-object x file)
416         (when *circularities-detected*
417           (dump-circularities *circularities-detected* file)
418           (clrhash circ)))
419       (sub-dump-object x file)))
420 \f
421 ;;;; LOAD-TIME-VALUE and MAKE-LOAD-FORM support
422
423 ;;; Emit a funcall of the function and return the handle for the result.
424 (defun fasl-dump-load-time-value-lambda (fun file)
425   (declare (type clambda fun) (type fasl-file file))
426   (let ((handle (gethash (leaf-info fun) (fasl-file-entry-table file))))
427     (assert handle)
428     (dump-push handle file)
429     (dump-fop 'sb!impl::fop-funcall file)
430     (dump-byte 0 file))
431   (dump-pop file))
432
433 ;;; Return T iff CONSTANT has not already been dumped. It's been dumped
434 ;;; if it's in the EQ table.
435 (defun fasl-constant-already-dumped (constant file)
436   (if (or (gethash constant (fasl-file-eq-table file))
437           (gethash constant (fasl-file-valid-structures file)))
438       t
439       nil))
440
441 ;;; Use HANDLE whenever we try to dump CONSTANT. HANDLE should have been
442 ;;; returned earlier by FASL-DUMP-LOAD-TIME-VALUE-LAMBDA.
443 (defun fasl-note-handle-for-constant (constant handle file)
444   (let ((table (fasl-file-eq-table file)))
445     (when (gethash constant table)
446       (error "~S already dumped?" constant))
447     (setf (gethash constant table) handle))
448   (values))
449
450 ;;; Note that the specified structure can just be dumped by enumerating the
451 ;;; slots.
452 (defun fasl-validate-structure (structure file)
453   (setf (gethash structure (fasl-file-valid-structures file)) t)
454   (values))
455 \f
456 ;;;; number dumping
457
458 ;;; Dump a ratio
459
460 (defun dump-ratio (x file)
461   (sub-dump-object (numerator x) file)
462   (sub-dump-object (denominator x) file)
463   (dump-fop 'sb!impl::fop-ratio file))
464
465 ;;; Dump an integer.
466
467 (defun dump-integer (n file)
468   (typecase n
469     ((signed-byte 8)
470      (dump-fop 'sb!impl::fop-byte-integer file)
471      (dump-byte (logand #xFF n) file))
472     ((unsigned-byte 31)
473      (dump-fop 'sb!impl::fop-word-integer file)
474      (dump-unsigned-32 n file))
475     ((signed-byte 32)
476      (dump-fop 'sb!impl::fop-word-integer file)
477      (dump-integer-as-n-bytes n 4 file))
478     (t
479      (let ((bytes (ceiling (1+ (integer-length n)) 8)))
480        (dump-fop* bytes
481                   sb!impl::fop-small-integer
482                   sb!impl::fop-integer
483                   file)
484        (dump-integer-as-n-bytes n bytes file)))))
485
486 (defun dump-float (x file)
487   (etypecase x
488     (single-float
489      (dump-fop 'sb!impl::fop-single-float file)
490      (dump-integer-as-n-bytes (single-float-bits x) 4 file))
491     (double-float
492      (dump-fop 'sb!impl::fop-double-float file)
493      (let ((x x))
494        (declare (double-float x))
495        ;; FIXME: Why sometimes DUMP-UNSIGNED-32 and sometimes
496        ;; DUMP-INTEGER-AS-N-BYTES .. 4?
497        (dump-unsigned-32 (double-float-low-bits x) file)
498        (dump-integer-as-n-bytes (double-float-high-bits x) 4 file)))
499     #!+long-float
500     (long-float
501      (dump-fop 'sb!impl::fop-long-float file)
502      (dump-long-float x file))))
503
504 (defun dump-complex (x file)
505   (typecase x
506     #-sb-xc-host
507     ((complex single-float)
508      (dump-fop 'sb!impl::fop-complex-single-float file)
509      (dump-integer-as-n-bytes (single-float-bits (realpart x)) 4 file)
510      (dump-integer-as-n-bytes (single-float-bits (imagpart x)) 4 file))
511     #-sb-xc-host
512     ((complex double-float)
513      (dump-fop 'sb!impl::fop-complex-double-float file)
514      (let ((re (realpart x)))
515        (declare (double-float re))
516        (dump-unsigned-32 (double-float-low-bits re) file)
517        (dump-integer-as-n-bytes (double-float-high-bits re) 4 file))
518      (let ((im (imagpart x)))
519        (declare (double-float im))
520        (dump-unsigned-32 (double-float-low-bits im) file)
521        (dump-integer-as-n-bytes (double-float-high-bits im) 4 file)))
522     #!+(and long-float (not sb-xc))
523     ((complex long-float)
524      (dump-fop 'sb!impl::fop-complex-long-float file)
525      (dump-long-float (realpart x) file)
526      (dump-long-float (imagpart x) file))
527     (t
528      (sub-dump-object (realpart x) file)
529      (sub-dump-object (imagpart x) file)
530      (dump-fop 'sb!impl::fop-complex file))))
531 \f
532 ;;;; symbol dumping
533
534 ;;; Return the table index of PKG, adding the package to the table if
535 ;;; necessary. During cold load, we read the string as a normal string so that
536 ;;; we can do the package lookup at cold load time.
537 ;;;
538 ;;; KLUDGE: Despite the parallelism in names, the functionality of this
539 ;;; function is not parallel to other functions DUMP-FOO, e.g. DUMP-SYMBOL
540 ;;; and DUMP-LIST. -- WHN 19990119
541 (defun dump-package (pkg file)
542   (declare (type package pkg) (type fasl-file file) (values index)
543            (inline assoc))
544   (cond ((cdr (assoc pkg (fasl-file-packages file) :test #'eq)))
545         (t
546          (unless *cold-load-dump*
547            (dump-fop 'sb!impl::fop-normal-load file))
548          (dump-simple-string (package-name pkg) file)
549          (dump-fop 'sb!impl::fop-package file)
550          (unless *cold-load-dump*
551            (dump-fop 'sb!impl::fop-maybe-cold-load file))
552          (let ((entry (dump-pop file)))
553            (push (cons pkg entry) (fasl-file-packages file))
554            entry))))
555 \f
556 ;;; dumper for lists
557
558 ;;; Dump a list, setting up patching information when there are
559 ;;; circularities. We scan down the list, checking for CDR and CAR
560 ;;; circularities.
561 ;;;
562 ;;; If there is a CDR circularity, we terminate the list with NIL and
563 ;;; make a CIRCULARITY notation for the CDR of the previous cons.
564 ;;;
565 ;;; If there is no CDR circularity, then we mark the current cons and
566 ;;; check for a CAR circularity. When there is a CAR circularity, we
567 ;;; make the CAR NIL initially, arranging for the current cons to be
568 ;;; patched later.
569 ;;;
570 ;;; Otherwise, we recursively call the dumper to dump the current
571 ;;; element.
572 ;;;
573 ;;; Marking of the conses is inhibited when *COLD-LOAD-DUMP* is true.
574 ;;; This inhibits all circularity detection.
575 (defun dump-list (list file)
576   (assert (and list
577                (not (gethash list (fasl-file-circularity-table file)))))
578   (do* ((l list (cdr l))
579         (n 0 (1+ n))
580         (circ (fasl-file-circularity-table file)))
581        ((atom l)
582         (cond ((null l)
583                (terminate-undotted-list n file))
584               (t
585                (sub-dump-object l file)
586                (terminate-dotted-list n file))))
587     (declare (type index n))
588     (let ((ref (gethash l circ)))
589       (when ref
590         (push (make-circularity :type :rplacd
591                                 :object list
592                                 :index (1- n)
593                                 :value l
594                                 :enclosing-object ref)
595               *circularities-detected*)
596         (terminate-undotted-list n file)
597         (return)))
598
599     (unless *cold-load-dump*
600       (setf (gethash l circ) list))
601
602     (let* ((obj (car l))
603            (ref (gethash obj circ)))
604       (cond (ref
605              (push (make-circularity :type :rplaca
606                                      :object list
607                                      :index n
608                                      :value obj
609                                      :enclosing-object ref)
610                    *circularities-detected*)
611              (sub-dump-object nil file))
612             (t
613              (sub-dump-object obj file))))))
614
615 (defun terminate-dotted-list (n file)
616   (declare (type index n) (type fasl-file file))
617   (case n
618     (1 (dump-fop 'sb!impl::fop-list*-1 file))
619     (2 (dump-fop 'sb!impl::fop-list*-2 file))
620     (3 (dump-fop 'sb!impl::fop-list*-3 file))
621     (4 (dump-fop 'sb!impl::fop-list*-4 file))
622     (5 (dump-fop 'sb!impl::fop-list*-5 file))
623     (6 (dump-fop 'sb!impl::fop-list*-6 file))
624     (7 (dump-fop 'sb!impl::fop-list*-7 file))
625     (8 (dump-fop 'sb!impl::fop-list*-8 file))
626     (T (do ((nn n (- nn 255)))
627            ((< nn 256)
628             (dump-fop 'sb!impl::fop-list* file)
629             (dump-byte nn file))
630          (declare (type index nn))
631          (dump-fop 'sb!impl::fop-list* file)
632          (dump-byte 255 file)))))
633
634 ;;; If N > 255, must build list with one list operator, then list* operators.
635
636 (defun terminate-undotted-list (n file)
637   (declare (type index n) (type fasl-file file))
638   (case n
639     (1 (dump-fop 'sb!impl::fop-list-1 file))
640     (2 (dump-fop 'sb!impl::fop-list-2 file))
641     (3 (dump-fop 'sb!impl::fop-list-3 file))
642     (4 (dump-fop 'sb!impl::fop-list-4 file))
643     (5 (dump-fop 'sb!impl::fop-list-5 file))
644     (6 (dump-fop 'sb!impl::fop-list-6 file))
645     (7 (dump-fop 'sb!impl::fop-list-7 file))
646     (8 (dump-fop 'sb!impl::fop-list-8 file))
647     (T (cond ((< n 256)
648               (dump-fop 'sb!impl::fop-list file)
649               (dump-byte n file))
650              (t (dump-fop 'sb!impl::fop-list file)
651                 (dump-byte 255 file)
652                 (do ((nn (- n 255) (- nn 255)))
653                     ((< nn 256)
654                      (dump-fop 'sb!impl::fop-list* file)
655                      (dump-byte nn file))
656                   (declare (type index nn))
657                   (dump-fop 'sb!impl::fop-list* file)
658                   (dump-byte 255 file)))))))
659 \f
660 ;;;; array dumping
661
662 ;;; Dump the array thing.
663 (defun dump-array (x file)
664   (if (vectorp x)
665       (dump-vector x file)
666       (dump-multi-dim-array x file)))
667
668 ;;; Dump the vector object. If it's not simple, then actually dump a simple
669 ;;; version of it. But we enter the original in the EQ or EQUAL tables.
670 (defun dump-vector (x file)
671   (let ((simple-version (if (array-header-p x)
672                             (coerce x 'simple-array)
673                             x)))
674     (typecase simple-version
675       (simple-base-string
676        (unless (equal-check-table x file)
677          (dump-simple-string simple-version file)
678          (equal-save-object x file)))
679       (simple-vector
680        (dump-simple-vector simple-version file)
681        (eq-save-object x file))
682       ((simple-array single-float (*))
683        (dump-single-float-vector simple-version file)
684        (eq-save-object x file))
685       ((simple-array double-float (*))
686        (dump-double-float-vector simple-version file)
687        (eq-save-object x file))
688       #!+long-float
689       ((simple-array long-float (*))
690        (dump-long-float-vector simple-version file)
691        (eq-save-object x file))
692       ((simple-array (complex single-float) (*))
693        (dump-complex-single-float-vector simple-version file)
694        (eq-save-object x file))
695       ((simple-array (complex double-float) (*))
696        (dump-complex-double-float-vector simple-version file)
697        (eq-save-object x file))
698       #!+long-float
699       ((simple-array (complex long-float) (*))
700        (dump-complex-long-float-vector simple-version file)
701        (eq-save-object x file))
702       (t
703        (dump-i-vector simple-version file)
704        (eq-save-object x file)))))
705
706 ;;; Dump a SIMPLE-VECTOR, handling any circularities.
707 (defun dump-simple-vector (v file)
708   (declare (type simple-vector v) (type fasl-file file))
709   (note-potential-circularity v file)
710   (do ((index 0 (1+ index))
711        (length (length v))
712        (circ (fasl-file-circularity-table file)))
713       ((= index length)
714        (dump-fop* length
715                   sb!impl::fop-small-vector
716                   sb!impl::fop-vector
717                   file))
718     (let* ((obj (aref v index))
719            (ref (gethash obj circ)))
720       (cond (ref
721              (push (make-circularity :type :svset
722                                      :object v
723                                      :index index
724                                      :value obj
725                                      :enclosing-object ref)
726                    *circularities-detected*)
727              (sub-dump-object nil file))
728             (t
729              (sub-dump-object obj file))))))
730
731 (defun dump-i-vector (vec file &key data-only)
732   (declare (type (simple-array * (*)) vec))
733   (let ((len (length vec)))
734     (labels ((dump-unsigned-vector (size bytes)
735                (unless data-only
736                  (dump-fop 'sb!impl::fop-int-vector file)
737                  (dump-unsigned-32 len file)
738                  (dump-byte size file))
739                ;; The case which is easy to handle in a portable way is when
740                ;; the element size is a multiple of the output byte size, and
741                ;; happily that's the only case we need to be portable. (The
742                ;; cross-compiler has to output debug information (including
743                ;; (SIMPLE-ARRAY (UNSIGNED-BYTE 8) *).) The other cases are only
744                ;; needed in the target SBCL, so we let them be handled with
745                ;; unportable bit bashing.
746                (cond ((>= size 8) ; easy cases
747                       (multiple-value-bind (floor rem) (floor size 8)
748                         (assert (zerop rem))
749                         (dovector (i vec)
750                           (dump-integer-as-n-bytes i floor file))))
751                      (t ; harder cases, not supported in cross-compiler
752                       (dump-raw-bytes vec bytes file))))
753              (dump-signed-vector (size bytes)
754                ;; Note: Dumping specialized signed vectors isn't supported in
755                ;; the cross-compiler. (All cases here end up trying to call
756                ;; DUMP-RAW-BYTES, which isn't provided in the cross-compilation
757                ;; host, only on the target machine.)
758                (unless data-only
759                  (dump-fop 'sb!impl::fop-signed-int-vector file)
760                  (dump-unsigned-32 len file)
761                  (dump-byte size file))
762                (dump-raw-bytes vec bytes file)))
763       (etypecase vec
764         ;; KLUDGE: What exactly does the (ASH .. -3) stuff do? -- WHN 19990902
765         (simple-bit-vector
766          (dump-unsigned-vector 1 (ash (+ (the index len) 7) -3)))
767         ((simple-array (unsigned-byte 2) (*))
768          (dump-unsigned-vector 2 (ash (+ (the index (ash len 1)) 7) -3)))
769         ((simple-array (unsigned-byte 4) (*))
770          (dump-unsigned-vector 4 (ash (+ (the index (ash len 2)) 7) -3)))
771         ((simple-array (unsigned-byte 8) (*))
772          (dump-unsigned-vector 8 len))
773         ((simple-array (unsigned-byte 16) (*))
774          (dump-unsigned-vector 16 (* 2 len)))
775         ((simple-array (unsigned-byte 32) (*))
776          (dump-unsigned-vector 32 (* 4 len)))
777         ((simple-array (signed-byte 8) (*))
778          (dump-signed-vector 8 len))
779         ((simple-array (signed-byte 16) (*))
780          (dump-signed-vector 16 (* 2 len)))
781         ((simple-array (signed-byte 30) (*))
782          (dump-signed-vector 30 (* 4 len)))
783         ((simple-array (signed-byte 32) (*))
784          (dump-signed-vector 32 (* 4 len)))))))
785 \f
786 ;;; Dump characters and string-ish things.
787
788 (defun dump-character (ch file)
789   (dump-fop 'sb!impl::fop-short-character file)
790   (dump-byte (char-code ch) file))
791
792 ;;; a helper function shared by DUMP-SIMPLE-STRING and DUMP-SYMBOL
793 (defun dump-characters-of-string (s fasl-file)
794   (declare (type string s) (type fasl-file fasl-file))
795   (dovector (c s)
796     (dump-byte (char-code c) fasl-file))
797   (values))
798
799 ;;; Dump a SIMPLE-BASE-STRING.
800 ;;; FIXME: should be called DUMP-SIMPLE-BASE-STRING then
801 (defun dump-simple-string (s file)
802   (declare (type simple-base-string s))
803   (dump-fop* (length s)
804              sb!impl::fop-small-string
805              sb!impl::fop-string
806              file)
807   (dump-characters-of-string s file)
808   (values))
809
810 ;;; If we get here, it is assumed that the symbol isn't in the table,
811 ;;; but we are responsible for putting it there when appropriate. To
812 ;;; avoid too much special-casing, we always push the symbol in the
813 ;;; table, but don't record that we have done so if *COLD-LOAD-DUMP*
814 ;;; is true.
815 (defun dump-symbol (s file)
816   (let* ((pname (symbol-name s))
817          (pname-length (length pname))
818          (pkg (symbol-package s)))
819
820     (cond ((null pkg)
821            (dump-fop* pname-length
822                       sb!impl::fop-uninterned-small-symbol-save
823                       sb!impl::fop-uninterned-symbol-save
824                       file))
825           ;; CMU CL had FOP-SYMBOL-SAVE/FOP-SMALL-SYMBOL-SAVE fops which
826           ;; used the current value of *PACKAGE*. Unfortunately that's
827           ;; broken w.r.t. ANSI Common Lisp semantics, so those are gone
828           ;; from SBCL.
829           ;;((eq pkg *package*)
830           ;; (dump-fop* pname-length
831           ;;        sb!impl::fop-small-symbol-save
832           ;;        sb!impl::fop-symbol-save file))
833           ((eq pkg sb!int:*cl-package*)
834            (dump-fop* pname-length
835                       sb!impl::fop-lisp-small-symbol-save
836                       sb!impl::fop-lisp-symbol-save
837                       file))
838           ((eq pkg sb!int:*keyword-package*)
839            (dump-fop* pname-length
840                       sb!impl::fop-keyword-small-symbol-save
841                       sb!impl::fop-keyword-symbol-save
842                       file))
843           ((< pname-length 256)
844            (dump-fop* (dump-package pkg file)
845                       sb!impl::fop-small-symbol-in-byte-package-save
846                       sb!impl::fop-small-symbol-in-package-save
847                       file)
848            (dump-byte pname-length file))
849           (t
850            (dump-fop* (dump-package pkg file)
851                       sb!impl::fop-symbol-in-byte-package-save
852                       sb!impl::fop-symbol-in-package-save
853                       file)
854            (dump-unsigned-32 pname-length file)))
855
856     (dump-characters-of-string pname file)
857
858     (unless *cold-load-dump*
859       (setf (gethash s (fasl-file-eq-table file))
860             (fasl-file-table-free file)))
861
862     (incf (fasl-file-table-free file)))
863
864   (values))
865 \f
866 ;;;; component (function) dumping
867
868 (defun dump-segment (segment code-length fasl-file)
869   (declare (type sb!assem:segment segment)
870            (type fasl-file fasl-file))
871   (let* ((stream (fasl-file-stream fasl-file))
872          (nwritten (write-segment-contents segment stream)))
873     ;; In CMU CL there was no enforced connection between the CODE-LENGTH
874     ;; argument and the number of bytes actually written. I added this
875     ;; assertion while trying to debug portable genesis. -- WHN 19990902
876     (unless (= code-length nwritten)
877       (error "internal error, code-length=~D, nwritten=~D"
878              code-length
879              nwritten)))
880   ;; KLUDGE: It's not clear what this is trying to do, but it looks as though
881   ;; it's an implicit undocumented dependence on a 4-byte wordsize which could
882   ;; be painful in porting. Note also that there are other undocumented
883   ;; modulo-4 things scattered throughout the code and conditionalized
884   ;; with GENGC, and I don't know what those do either. -- WHN 19990323
885   #!+gengc (unless (zerop (logand code-length 3))
886              (dotimes (i (- 4 (logand code-length 3)))
887                (dump-byte 0 fasl-file)))
888   (values))
889
890 ;;; Dump all the fixups. Currently there are three flavors of fixup:
891 ;;;  - assembly routines: named by a symbol
892 ;;;  - foreign (C) symbols: named by a string
893 ;;;  - code object references: don't need a name.
894 (defun dump-fixups (fixups fasl-file)
895   (declare (list fixups) (type fasl-file fasl-file))
896   (dolist (info fixups)
897     ;; FIXME: Packing data with LIST in NOTE-FIXUP and unpacking them
898     ;; with FIRST, SECOND, and THIRD here is hard to follow and maintain.
899     ;; Perhaps we could define a FIXUP-INFO structure to use instead, and
900     ;; rename *FIXUPS* to *FIXUP-INFO-LIST*?
901     (let* ((kind (first info))
902            (fixup (second info))
903            (name (fixup-name fixup))
904            (flavor (fixup-flavor fixup))
905            (offset (third info)))
906       ;; FIXME: This OFFSET is not what's called OFFSET in
907       ;; the FIXUP structure, it's what's called POSN in NOTE-FIXUP.
908       ;; (As far as I can tell, FIXUP-OFFSET is not actually an offset,
909       ;; it's an internal label used instead of NAME for :CODE-OBJECT
910       ;; fixups. Notice that in the :CODE-OBJECT case, NAME is ignored.)
911       (dump-fop 'sb!impl::fop-normal-load fasl-file)
912       (let ((*cold-load-dump* t))
913         (dump-object kind fasl-file))
914       (dump-fop 'sb!impl::fop-maybe-cold-load fasl-file)
915       ;; Depending on the flavor, we may have various kinds of
916       ;; noise before the offset.
917       (ecase flavor
918         (:assembly-routine
919          (assert (symbolp name))
920          (dump-fop 'sb!impl::fop-normal-load fasl-file)
921          (let ((*cold-load-dump* t))
922            (dump-object name fasl-file))
923          (dump-fop 'sb!impl::fop-maybe-cold-load fasl-file)
924          (dump-fop 'sb!impl::fop-assembler-fixup fasl-file))
925         (:foreign
926          (assert (stringp name))
927          (dump-fop 'sb!impl::fop-foreign-fixup fasl-file)
928          (let ((len (length name)))
929            (assert (< len 256)) ; (limit imposed by fop definition)
930            (dump-byte len fasl-file)
931            (dotimes (i len)
932              (dump-byte (char-code (schar name i)) fasl-file))))
933         (:code-object
934          (assert (null name))
935          (dump-fop 'sb!impl::fop-code-object-fixup fasl-file)))
936       ;; No matter what the flavor, we'll always dump the offset.
937       (dump-unsigned-32 offset fasl-file)))
938   (values))
939
940 ;;; Dump out the constant pool and code-vector for component, push the
941 ;;; result in the table, and return the offset.
942 ;;;
943 ;;; The only tricky thing is handling constant-pool references to functions.
944 ;;; If we have already dumped the function, then we just push the code pointer.
945 ;;; Otherwise, we must create back-patching information so that the constant
946 ;;; will be set when the function is eventually dumped. This is a bit awkward,
947 ;;; since we don't have the handle for the code object being dumped while we
948 ;;; are dumping its constants.
949 ;;;
950 ;;; We dump trap objects in any unused slots or forward referenced slots.
951 (defun dump-code-object (component
952                          code-segment
953                          code-length
954                          trace-table-as-list
955                          fixups
956                          fasl-file)
957
958   (declare (type component component)
959            (list trace-table-as-list)
960            (type index code-length)
961            (type fasl-file fasl-file))
962
963   (let* ((2comp (component-info component))
964          (constants (ir2-component-constants 2comp))
965          (header-length (length constants))
966          (packed-trace-table (pack-trace-table trace-table-as-list))
967          (total-length (+ code-length
968                           (* (length packed-trace-table) tt-bytes-per-entry))))
969
970     (collect ((patches))
971
972       ;; Dump the debug info.
973       #!+gengc
974       (let ((info (debug-info-for-component component))
975             (*dump-only-valid-structures* nil))
976         (dump-object info fasl-file)
977         (let ((info-handle (dump-pop fasl-file)))
978           (dump-push info-handle fasl-file)
979           (push info-handle (fasl-file-debug-info fasl-file))))
980
981       ;; Dump the offset of the trace table.
982       (dump-object code-length fasl-file)
983       ;; KLUDGE: Now that we don't have GENGC, the trace table is hardwired
984       ;; to be empty. Could we get rid of trace tables? What are the
985       ;; virtues of GENGC vs. GENCGC vs. whatnot?
986
987       ;; Dump the constants, noting any :entries that have to be fixed up.
988       (do ((i sb!vm:code-constants-offset (1+ i)))
989           ((>= i header-length))
990         (let ((entry (aref constants i)))
991           (etypecase entry
992             (constant
993              (dump-object (constant-value entry) fasl-file))
994             (cons
995              (ecase (car entry)
996                (:entry
997                 (let* ((info (leaf-info (cdr entry)))
998                        (handle (gethash info
999                                         (fasl-file-entry-table fasl-file))))
1000                   (cond
1001                    (handle
1002                     (dump-push handle fasl-file))
1003                    (t
1004                     (patches (cons info i))
1005                     (dump-fop 'sb!impl::fop-misc-trap fasl-file)))))
1006                (:load-time-value
1007                 (dump-push (cdr entry) fasl-file))
1008                (:fdefinition
1009                 (dump-object (cdr entry) fasl-file)
1010                 (dump-fop 'sb!impl::fop-fdefinition fasl-file))))
1011             (null
1012              (dump-fop 'sb!impl::fop-misc-trap fasl-file)))))
1013
1014       ;; Dump the debug info.
1015       #!-gengc
1016       (let ((info (debug-info-for-component component))
1017             (*dump-only-valid-structures* nil))
1018         (dump-object info fasl-file)
1019         (let ((info-handle (dump-pop fasl-file)))
1020           (dump-push info-handle fasl-file)
1021           (push info-handle (fasl-file-debug-info fasl-file))))
1022
1023       (let ((num-consts #!+gengc (- header-length
1024                                     sb!vm:code-debug-info-slot)
1025                         #!-gengc (- header-length
1026                                     sb!vm:code-trace-table-offset-slot))
1027             (total-length #!+gengc (ceiling total-length 4)
1028                           #!-gengc total-length))
1029         (cond ((and (< num-consts #x100) (< total-length #x10000))
1030                (dump-fop 'sb!impl::fop-small-code fasl-file)
1031                (dump-byte num-consts fasl-file)
1032                (dump-integer-as-n-bytes total-length 2 fasl-file))
1033               (t
1034                (dump-fop 'sb!impl::fop-code fasl-file)
1035                (dump-unsigned-32 num-consts fasl-file)
1036                (dump-unsigned-32 total-length fasl-file))))
1037
1038       ;; These two dumps are only ones which contribute to our TOTAL-LENGTH
1039       ;; value.
1040       (dump-segment code-segment code-length fasl-file)
1041       (dump-i-vector packed-trace-table fasl-file :data-only t)
1042
1043       ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it dumps aren't
1044       ;; included in the TOTAL-LENGTH passed to our FOP-CODE/FOP-SMALL-CODE
1045       ;; fop.
1046       (dump-fixups fixups fasl-file)
1047
1048       (dump-fop 'sb!impl::fop-sanctify-for-execution fasl-file)
1049       (let ((handle (dump-pop fasl-file)))
1050         (dolist (patch (patches))
1051           (push (cons handle (cdr patch))
1052                 (gethash (car patch) (fasl-file-patch-table fasl-file))))
1053         handle))))
1054
1055 (defun dump-assembler-routines (code-segment length fixups routines file)
1056   (dump-fop 'sb!impl::fop-assembler-code file)
1057   (dump-unsigned-32 #!+gengc (ceiling length 4)
1058                     #!-gengc length
1059                     file)
1060   (write-segment-contents code-segment (fasl-file-stream file))
1061   (dolist (routine routines)
1062     (dump-fop 'sb!impl::fop-normal-load file)
1063     (let ((*cold-load-dump* t))
1064       (dump-object (car routine) file))
1065     (dump-fop 'sb!impl::fop-maybe-cold-load file)
1066     (dump-fop 'sb!impl::fop-assembler-routine file)
1067     (dump-unsigned-32 (label-position (cdr routine)) file))
1068   (dump-fixups fixups file)
1069   (dump-fop 'sb!impl::fop-sanctify-for-execution file)
1070   (dump-pop file))
1071
1072 ;;; Dump a function-entry data structure corresponding to Entry to File.
1073 ;;; Code-Handle is the table offset of the code object for the component.
1074 ;;;
1075 ;;; If the entry is a DEFUN, then we also dump a FOP-FSET so that the cold
1076 ;;; loader can instantiate the definition at cold-load time, allowing forward
1077 ;;; references to functions in top-level forms.
1078 (defun dump-one-entry (entry code-handle file)
1079   (declare (type entry-info entry) (type index code-handle)
1080            (type fasl-file file))
1081   (let ((name (entry-info-name entry)))
1082     (dump-push code-handle file)
1083     (dump-object name file)
1084     (dump-object (entry-info-arguments entry) file)
1085     (dump-object (entry-info-type entry) file)
1086     (dump-fop 'sb!impl::fop-function-entry file)
1087     (dump-unsigned-32 (label-position (entry-info-offset entry)) file)
1088     (let ((handle (dump-pop file)))
1089       (when (and name (or (symbolp name) (listp name)))
1090         (dump-object name file)
1091         (dump-push handle file)
1092         (dump-fop 'sb!impl::fop-fset file))
1093       handle)))
1094
1095 ;;; Alter the code object referenced by Code-Handle at the specified Offset,
1096 ;;; storing the object referenced by Entry-Handle.
1097 (defun dump-alter-code-object (code-handle offset entry-handle file)
1098   (declare (type index code-handle entry-handle offset) (type fasl-file file))
1099   (dump-push code-handle file)
1100   (dump-push entry-handle file)
1101   (dump-fop* offset
1102              sb!impl::fop-byte-alter-code
1103              sb!impl::fop-alter-code
1104              file)
1105   (values))
1106
1107 ;;; Dump the code, constants, etc. for component. We pass in the assembler
1108 ;;; fixups, code vector and node info.
1109 (defun fasl-dump-component (component
1110                             code-segment
1111                             code-length
1112                             trace-table
1113                             fixups
1114                             file)
1115   (declare (type component component) (list trace-table) (type fasl-file file))
1116
1117   (dump-fop 'sb!impl::fop-verify-empty-stack file)
1118   (dump-fop 'sb!impl::fop-verify-table-size file)
1119   (dump-unsigned-32 (fasl-file-table-free file) file)
1120
1121   #!+sb-dyncount
1122   (let ((info (ir2-component-dyncount-info (component-info component))))
1123     (when info
1124       (fasl-validate-structure info file)))
1125
1126   (let ((code-handle (dump-code-object component
1127                                        code-segment
1128                                        code-length
1129                                        trace-table
1130                                        fixups
1131                                        file))
1132         (2comp (component-info component)))
1133     (dump-fop 'sb!impl::fop-verify-empty-stack file)
1134
1135     (dolist (entry (ir2-component-entries 2comp))
1136       (let ((entry-handle (dump-one-entry entry code-handle file)))
1137         (setf (gethash entry (fasl-file-entry-table file)) entry-handle)
1138
1139         (let ((old (gethash entry (fasl-file-patch-table file))))
1140           ;; KLUDGE: All this code is shared with FASL-DUMP-BYTE-COMPONENT,
1141           ;; and should probably be gathered up into a named function
1142           ;; (DUMP-PATCHES?) called from both functions.
1143           (when old
1144             (dolist (patch old)
1145               (dump-alter-code-object (car patch)
1146                                       (cdr patch)
1147                                       entry-handle
1148                                       file))
1149             (remhash entry (fasl-file-patch-table file)))))))
1150   (values))
1151
1152 (defun dump-byte-code-object (segment code-length constants file)
1153   (declare (type sb!assem:segment segment)
1154            (type index code-length)
1155            (type vector constants)
1156            (type fasl-file file))
1157   (collect ((entry-patches))
1158
1159     ;; Dump the debug info.
1160     #!+gengc
1161     (let ((info (make-debug-info
1162                  :name (component-name *component-being-compiled*)))
1163           (*dump-only-valid-structures* nil))
1164       (dump-object info file)
1165       (let ((info-handle (dump-pop file)))
1166         (dump-push info-handle file)
1167         (push info-handle (fasl-file-debug-info file))))
1168
1169     ;; The "trace table" is initialized by loader to hold a list of all byte
1170     ;; functions in this code object (for debug info.)
1171     (dump-object nil file)
1172
1173     ;; Dump the constants.
1174     (dotimes (i (length constants))
1175       (let ((entry (aref constants i)))
1176         (etypecase entry
1177           (constant
1178            (dump-object (constant-value entry) file))
1179           (null
1180            (dump-fop 'sb!impl::fop-misc-trap file))
1181           (list
1182            (ecase (car entry)
1183              (:entry
1184               (let* ((info (leaf-info (cdr entry)))
1185                      (handle (gethash info (fasl-file-entry-table file))))
1186                 (cond
1187                  (handle
1188                   (dump-push handle file))
1189                  (t
1190                   (entry-patches (cons info
1191                                        (+ i sb!vm:code-constants-offset)))
1192                   (dump-fop 'sb!impl::fop-misc-trap file)))))
1193              (:load-time-value
1194               (dump-push (cdr entry) file))
1195              (:fdefinition
1196               (dump-object (cdr entry) file)
1197               (dump-fop 'sb!impl::fop-fdefinition file))
1198              (:type-predicate
1199               (dump-object 'load-type-predicate file)
1200               (let ((*unparse-function-type-simplify* t))
1201                 (dump-object (type-specifier (cdr entry)) file))
1202               (dump-fop 'sb!impl::fop-funcall file)
1203               (dump-byte 1 file)))))))
1204
1205     ;; Dump the debug info.
1206     #!-gengc
1207     (let ((info (make-debug-info :name
1208                                  (component-name *component-being-compiled*)))
1209           (*dump-only-valid-structures* nil))
1210       (dump-object info file)
1211       (let ((info-handle (dump-pop file)))
1212         (dump-push info-handle file)
1213         (push info-handle (fasl-file-debug-info file))))
1214
1215     (let ((num-consts #!+gengc (+ (length constants) 2)
1216                       #!-gengc (1+ (length constants)))
1217           (code-length #!+gengc (ceiling code-length 4)
1218                        #!-gengc code-length))
1219       (cond ((and (< num-consts #x100) (< code-length #x10000))
1220              (dump-fop 'sb!impl::fop-small-code file)
1221              (dump-byte num-consts file)
1222              (dump-integer-as-n-bytes code-length 2 file))
1223             (t
1224              (dump-fop 'sb!impl::fop-code file)
1225              (dump-unsigned-32 num-consts file)
1226              (dump-unsigned-32 code-length file))))
1227     (dump-segment segment code-length file)
1228     (let ((code-handle (dump-pop file))
1229           (patch-table (fasl-file-patch-table file)))
1230       (dolist (patch (entry-patches))
1231         (push (cons code-handle (cdr patch))
1232               (gethash (car patch) patch-table)))
1233       code-handle)))
1234
1235 ;;; Dump a BYTE-FUNCTION object. We dump the layout and
1236 ;;; funcallable-instance info, but rely on the loader setting up the correct
1237 ;;; funcallable-instance-function.
1238 (defun dump-byte-function (xep code-handle file)
1239   (let ((nslots (- (get-closure-length xep)
1240                    ;; 1- for header
1241                    (1- sb!vm:funcallable-instance-info-offset))))
1242     (dotimes (i nslots)
1243       (if (zerop i)
1244           (dump-push code-handle file)
1245           (dump-object (%funcallable-instance-info xep i) file)))
1246     (dump-object (%funcallable-instance-layout xep) file)
1247     (dump-fop 'sb!impl::fop-make-byte-compiled-function file)
1248     (dump-byte nslots file))
1249   (values))
1250
1251 ;;; Dump a byte-component. This is similar to FASL-DUMP-COMPONENT, but
1252 ;;; different.
1253 (defun fasl-dump-byte-component (segment length constants xeps file)
1254   (declare (type sb!assem:segment segment)
1255            (type index length)
1256            (type vector constants)
1257            (type list xeps)
1258            (type fasl-file file))
1259
1260   (let ((code-handle (dump-byte-code-object segment length constants file)))
1261     (dolist (noise xeps)
1262       (let* ((lambda (car noise))
1263              (info (lambda-info lambda))
1264              (xep (cdr noise)))
1265         (dump-byte-function xep code-handle file)
1266         (let* ((entry-handle (dump-pop file))
1267                (patch-table (fasl-file-patch-table file))
1268                (old (gethash info patch-table)))
1269           (setf (gethash info (fasl-file-entry-table file)) entry-handle)
1270           (when old
1271             (dolist (patch old)
1272               (dump-alter-code-object (car patch)
1273                                       (cdr patch)
1274                                       entry-handle
1275                                       file))
1276             (remhash info patch-table))))))
1277   (values))
1278
1279 ;;; Dump a FOP-FUNCALL to call an already dumped top-level lambda at load time.
1280 (defun fasl-dump-top-level-lambda-call (fun file)
1281   (declare (type clambda fun) (type fasl-file file))
1282   (let ((handle (gethash (leaf-info fun) (fasl-file-entry-table file))))
1283     (assert handle)
1284     (dump-push handle file)
1285     (dump-fop 'sb!impl::fop-funcall-for-effect file)
1286     (dump-byte 0 file))
1287   (values))
1288
1289 ;;; Compute the correct list of DEBUG-SOURCE structures and backpatch all of
1290 ;;; the dumped DEBUG-INFO structures. We clear the FASL-FILE-DEBUG-INFO,
1291 ;;; so that subsequent components with different source info may be dumped.
1292 (defun fasl-dump-source-info (info file)
1293   (declare (type source-info info) (type fasl-file file))
1294   (let ((res (debug-source-for-info info))
1295         (*dump-only-valid-structures* nil))
1296     (dump-object res file)
1297     (let ((res-handle (dump-pop file)))
1298       (dolist (info-handle (fasl-file-debug-info file))
1299         (dump-push res-handle file)
1300         (dump-fop 'sb!impl::fop-structset file)
1301         (dump-unsigned-32 info-handle file)
1302         (dump-unsigned-32 2 file))))
1303
1304   (setf (fasl-file-debug-info file) ())
1305   (values))
1306 \f
1307 ;;;; dumping structures
1308
1309 (defun dump-structure (struct file)
1310   ;; FIXME: Probably *DUMP-ONLY-VALID-STRUCTURES* should become constantly T,
1311   ;; right?
1312   (when *dump-only-valid-structures*
1313     (unless (gethash struct (fasl-file-valid-structures file))
1314       (error "attempt to dump invalid structure:~%  ~S~%How did this happen?"
1315              struct)))
1316   (note-potential-circularity struct file)
1317   (do ((index 0 (1+ index))
1318        (length (%instance-length struct))
1319        (circ (fasl-file-circularity-table file)))
1320       ((= index length)
1321        (dump-fop* length
1322                   sb!impl::fop-small-struct
1323                   sb!impl::fop-struct
1324                   file))
1325     (let* ((obj (%instance-ref struct index))
1326            (ref (gethash obj circ)))
1327       (cond (ref
1328              (push (make-circularity :type :struct-set
1329                                      :object struct
1330                                      :index index
1331                                      :value obj
1332                                      :enclosing-object ref)
1333                    *circularities-detected*)
1334              (sub-dump-object nil file))
1335             (t
1336              (sub-dump-object obj file))))))
1337
1338 (defun dump-layout (obj file)
1339   (when (layout-invalid obj)
1340     (compiler-error "attempt to dump reference to obsolete class: ~S"
1341                     (layout-class obj)))
1342   (let ((name (sb!xc:class-name (layout-class obj))))
1343     (unless name
1344       (compiler-error "dumping anonymous layout: ~S" obj))
1345     (dump-fop 'sb!impl::fop-normal-load file)
1346     (let ((*cold-load-dump* t))
1347       (dump-object name file))
1348     (dump-fop 'sb!impl::fop-maybe-cold-load file))
1349   (sub-dump-object (layout-inherits obj) file)
1350   (sub-dump-object (layout-depthoid obj) file)
1351   (sub-dump-object (layout-length obj) file)
1352   (dump-fop 'sb!impl::fop-layout file))