0.6.8.6: applied MNA megapatch (will be edited shortly)
[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
307 ;;; MNA dump-circular hack
308 (defun circular-list-p (list)
309   (and (listp list)
310        (multiple-value-bind (res condition)
311            (ignore-errors (list-length list))
312          (if condition
313            nil
314            (null res)))))
315
316 ;;;; main entries to object dumping
317
318 ;;; This function deals with dumping objects that are complex enough so that
319 ;;; we want to cache them in the table, rather than repeatedly dumping them.
320 ;;; If the object is in the EQ-TABLE, then we push it, otherwise, we do a type
321 ;;; dispatch to a type specific dumping function. The type specific branches
322 ;;; do any appropriate EQUAL-TABLE check and table entry.
323 ;;;
324 ;;; When we go to dump the object, we enter it in the CIRCULARITY-TABLE.
325 (defun dump-non-immediate-object (x file)
326   (let ((index (gethash x (fasl-file-eq-table file))))
327     (cond ((and index (not *cold-load-dump*))
328            (dump-push index file))
329           (t
330            (typecase x
331              (symbol (dump-symbol x file))
332              (list
333                ;; MNA dump-circular hack
334                (if (circular-list-p x)
335                  (progn
336                    (dump-list x file)
337                    (eq-save-object x file))
338               (unless (equal-check-table x file)
339                 (dump-list x file)
340                    (equal-save-object x file))))
341              (layout
342               (dump-layout x file)
343               (eq-save-object x file))
344              (instance
345               (dump-structure x file)
346               (eq-save-object x file))
347              (array
348               ;; FIXME: The comment at the head of DUMP-NON-IMMEDIATE-OBJECT
349               ;; says it's for objects which we want to save, instead of
350               ;; repeatedly dumping them. But then we dump arrays here without
351               ;; doing anything like EQUAL-SAVE-OBJECT. What gives?
352               (dump-array x file))
353              (number
354               (unless (equal-check-table x file)
355                 (etypecase x
356                   (ratio (dump-ratio x file))
357                   (complex (dump-complex x file))
358                   (float (dump-float x file))
359                   (integer (dump-integer x file)))
360                 (equal-save-object x file)))
361              (t
362               ;; This probably never happens, since bad things tend to be
363               ;; detected during IR1 conversion.
364               (error "This object cannot be dumped into a fasl file:~% ~S"
365                      x))))))
366   (values))
367
368 ;;; Dump an object of any type by dispatching to the correct type-specific
369 ;;; dumping function. We pick off immediate objects, symbols and and magic
370 ;;; lists here. Other objects are handled by Dump-Non-Immediate-Object.
371 ;;;
372 ;;; This is the function used for recursive calls to the fasl dumper. We don't
373 ;;; worry about creating circularities here, since it is assumed that there is
374 ;;; a top-level call to Dump-Object.
375 (defun sub-dump-object (x file)
376   (cond ((listp x)
377          (if x
378              (dump-non-immediate-object x file)
379              (dump-fop 'sb!impl::fop-empty-list file)))
380         ((symbolp x)
381          (if (eq x t)
382              (dump-fop 'sb!impl::fop-truth file)
383              (dump-non-immediate-object x file)))
384         ((target-fixnump x) (dump-integer x file))
385         ((characterp x) (dump-character x file))
386         (t
387          (dump-non-immediate-object x file))))
388
389 ;;; Dump stuff to backpatch already dumped objects. Infos is the list of
390 ;;; Circularity structures describing what to do. The patching FOPs take the
391 ;;; value to store on the stack. We compute this value by fetching the
392 ;;; enclosing object from the table, and then CDR'ing it if necessary.
393 (defun dump-circularities (infos file)
394   (let ((table (fasl-file-eq-table file)))
395     (dolist (info infos)
396       (let* ((value (circularity-value info))
397              (enclosing (circularity-enclosing-object info)))
398         (dump-push (gethash enclosing table) file)
399         (unless (eq enclosing value)
400           (do ((current enclosing (cdr current))
401                (i 0 (1+ i)))
402               ((eq current value)
403                (dump-fop 'sb!impl::fop-nthcdr file)
404                (dump-unsigned-32 i file))
405             (declare (type index i)))))
406
407       (ecase (circularity-type info)
408         (:rplaca (dump-fop 'sb!impl::fop-rplaca file))
409         (:rplacd (dump-fop 'sb!impl::fop-rplacd file))
410         (:svset (dump-fop 'sb!impl::fop-svset file))
411         (:struct-set (dump-fop 'sb!impl::fop-structset file)))
412       (dump-unsigned-32 (gethash (circularity-object info) table) file)
413       (dump-unsigned-32 (circularity-index info) file))))
414
415 ;;; Set up stuff for circularity detection, then dump an object. All shared
416 ;;; and circular structure will be exactly preserved within a single call to
417 ;;; Dump-Object. Sharing between objects dumped by separate calls is only
418 ;;; preserved when convenient.
419 ;;;
420 ;;; We peek at the object type so that we only pay the circular detection
421 ;;; overhead on types of objects that might be circular.
422 (defun dump-object (x file)
423   (if (or (array-header-p x)
424           (simple-vector-p x)
425           (consp x)
426           (typep x 'instance))
427       (let ((*circularities-detected* ())
428             (circ (fasl-file-circularity-table file)))
429         (clrhash circ)
430         (sub-dump-object x file)
431         (when *circularities-detected*
432           (dump-circularities *circularities-detected* file)
433           (clrhash circ)))
434       (sub-dump-object x file)))
435 \f
436 ;;;; LOAD-TIME-VALUE and MAKE-LOAD-FORM support
437
438 ;;; Emit a funcall of the function and return the handle for the result.
439 (defun fasl-dump-load-time-value-lambda (fun file)
440   (declare (type clambda fun) (type fasl-file file))
441   (let ((handle (gethash (leaf-info fun) (fasl-file-entry-table file))))
442     (assert handle)
443     (dump-push handle file)
444     (dump-fop 'sb!impl::fop-funcall file)
445     (dump-byte 0 file))
446   (dump-pop file))
447
448 ;;; Return T iff CONSTANT has not already been dumped. It's been dumped
449 ;;; if it's in the EQ table.
450 (defun fasl-constant-already-dumped (constant file)
451   (if (or (gethash constant (fasl-file-eq-table file))
452           (gethash constant (fasl-file-valid-structures file)))
453       t
454       nil))
455
456 ;;; Use HANDLE whenever we try to dump CONSTANT. HANDLE should have been
457 ;;; returned earlier by FASL-DUMP-LOAD-TIME-VALUE-LAMBDA.
458 (defun fasl-note-handle-for-constant (constant handle file)
459   (let ((table (fasl-file-eq-table file)))
460     (when (gethash constant table)
461       (error "~S already dumped?" constant))
462     (setf (gethash constant table) handle))
463   (values))
464
465 ;;; Note that the specified structure can just be dumped by enumerating the
466 ;;; slots.
467 (defun fasl-validate-structure (structure file)
468   (setf (gethash structure (fasl-file-valid-structures file)) t)
469   (values))
470 \f
471 ;;;; number dumping
472
473 ;;; Dump a ratio
474
475 (defun dump-ratio (x file)
476   (sub-dump-object (numerator x) file)
477   (sub-dump-object (denominator x) file)
478   (dump-fop 'sb!impl::fop-ratio file))
479
480 ;;; Dump an integer.
481
482 (defun dump-integer (n file)
483   (typecase n
484     ((signed-byte 8)
485      (dump-fop 'sb!impl::fop-byte-integer file)
486      (dump-byte (logand #xFF n) file))
487     ((unsigned-byte 31)
488      (dump-fop 'sb!impl::fop-word-integer file)
489      (dump-unsigned-32 n file))
490     ((signed-byte 32)
491      (dump-fop 'sb!impl::fop-word-integer file)
492      (dump-integer-as-n-bytes n 4 file))
493     (t
494      (let ((bytes (ceiling (1+ (integer-length n)) 8)))
495        (dump-fop* bytes
496                   sb!impl::fop-small-integer
497                   sb!impl::fop-integer
498                   file)
499        (dump-integer-as-n-bytes n bytes file)))))
500
501 (defun dump-float (x file)
502   (etypecase x
503     (single-float
504      (dump-fop 'sb!impl::fop-single-float file)
505      (dump-integer-as-n-bytes (single-float-bits x) 4 file))
506     (double-float
507      (dump-fop 'sb!impl::fop-double-float file)
508      (let ((x x))
509        (declare (double-float x))
510        ;; FIXME: Why sometimes DUMP-UNSIGNED-32 and sometimes
511        ;; DUMP-INTEGER-AS-N-BYTES .. 4?
512        (dump-unsigned-32 (double-float-low-bits x) file)
513        (dump-integer-as-n-bytes (double-float-high-bits x) 4 file)))
514     #!+long-float
515     (long-float
516      (dump-fop 'sb!impl::fop-long-float file)
517      (dump-long-float x file))))
518
519 (defun dump-complex (x file)
520   (typecase x
521     #-sb-xc-host
522     ((complex single-float)
523      (dump-fop 'sb!impl::fop-complex-single-float file)
524      (dump-integer-as-n-bytes (single-float-bits (realpart x)) 4 file)
525      (dump-integer-as-n-bytes (single-float-bits (imagpart x)) 4 file))
526     #-sb-xc-host
527     ((complex double-float)
528      (dump-fop 'sb!impl::fop-complex-double-float file)
529      (let ((re (realpart x)))
530        (declare (double-float re))
531        (dump-unsigned-32 (double-float-low-bits re) file)
532        (dump-integer-as-n-bytes (double-float-high-bits re) 4 file))
533      (let ((im (imagpart x)))
534        (declare (double-float im))
535        (dump-unsigned-32 (double-float-low-bits im) file)
536        (dump-integer-as-n-bytes (double-float-high-bits im) 4 file)))
537     #!+(and long-float (not sb-xc))
538     ((complex long-float)
539      (dump-fop 'sb!impl::fop-complex-long-float file)
540      (dump-long-float (realpart x) file)
541      (dump-long-float (imagpart x) file))
542     (t
543      (sub-dump-object (realpart x) file)
544      (sub-dump-object (imagpart x) file)
545      (dump-fop 'sb!impl::fop-complex file))))
546 \f
547 ;;;; symbol dumping
548
549 ;;; Return the table index of PKG, adding the package to the table if
550 ;;; necessary. During cold load, we read the string as a normal string so that
551 ;;; we can do the package lookup at cold load time.
552 ;;;
553 ;;; KLUDGE: Despite the parallelism in names, the functionality of this
554 ;;; function is not parallel to other functions DUMP-FOO, e.g. DUMP-SYMBOL
555 ;;; and DUMP-LIST. -- WHN 19990119
556 (defun dump-package (pkg file)
557   (declare (type package pkg) (type fasl-file file) (values index)
558            (inline assoc))
559   (cond ((cdr (assoc pkg (fasl-file-packages file) :test #'eq)))
560         (t
561          (unless *cold-load-dump*
562            (dump-fop 'sb!impl::fop-normal-load file))
563          (dump-simple-string (package-name pkg) file)
564          (dump-fop 'sb!impl::fop-package file)
565          (unless *cold-load-dump*
566            (dump-fop 'sb!impl::fop-maybe-cold-load file))
567          (let ((entry (dump-pop file)))
568            (push (cons pkg entry) (fasl-file-packages file))
569            entry))))
570 \f
571 ;;; dumper for lists
572
573 ;;; Dump a list, setting up patching information when there are
574 ;;; circularities. We scan down the list, checking for CDR and CAR
575 ;;; circularities.
576 ;;;
577 ;;; If there is a CDR circularity, we terminate the list with NIL and
578 ;;; make a CIRCULARITY notation for the CDR of the previous cons.
579 ;;;
580 ;;; If there is no CDR circularity, then we mark the current cons and
581 ;;; check for a CAR circularity. When there is a CAR circularity, we
582 ;;; make the CAR NIL initially, arranging for the current cons to be
583 ;;; patched later.
584 ;;;
585 ;;; Otherwise, we recursively call the dumper to dump the current
586 ;;; element.
587 ;;;
588 ;;; Marking of the conses is inhibited when *COLD-LOAD-DUMP* is true.
589 ;;; This inhibits all circularity detection.
590 (defun dump-list (list file)
591   (assert (and list
592                (not (gethash list (fasl-file-circularity-table file)))))
593   (do* ((l list (cdr l))
594         (n 0 (1+ n))
595         (circ (fasl-file-circularity-table file)))
596        ((atom l)
597         (cond ((null l)
598                (terminate-undotted-list n file))
599               (t
600                (sub-dump-object l file)
601                (terminate-dotted-list n file))))
602     (declare (type index n))
603     (let ((ref (gethash l circ)))
604       (when ref
605         (push (make-circularity :type :rplacd
606                                 :object list
607                                 :index (1- n)
608                                 :value l
609                                 :enclosing-object ref)
610               *circularities-detected*)
611         (terminate-undotted-list n file)
612         (return)))
613
614     (unless *cold-load-dump*
615       (setf (gethash l circ) list))
616
617     (let* ((obj (car l))
618            (ref (gethash obj circ)))
619       (cond (ref
620              (push (make-circularity :type :rplaca
621                                      :object list
622                                      :index n
623                                      :value obj
624                                      :enclosing-object ref)
625                    *circularities-detected*)
626              (sub-dump-object nil file))
627             (t
628              (sub-dump-object obj file))))))
629
630 (defun terminate-dotted-list (n file)
631   (declare (type index n) (type fasl-file file))
632   (case n
633     (1 (dump-fop 'sb!impl::fop-list*-1 file))
634     (2 (dump-fop 'sb!impl::fop-list*-2 file))
635     (3 (dump-fop 'sb!impl::fop-list*-3 file))
636     (4 (dump-fop 'sb!impl::fop-list*-4 file))
637     (5 (dump-fop 'sb!impl::fop-list*-5 file))
638     (6 (dump-fop 'sb!impl::fop-list*-6 file))
639     (7 (dump-fop 'sb!impl::fop-list*-7 file))
640     (8 (dump-fop 'sb!impl::fop-list*-8 file))
641     (T (do ((nn n (- nn 255)))
642            ((< nn 256)
643             (dump-fop 'sb!impl::fop-list* file)
644             (dump-byte nn file))
645          (declare (type index nn))
646          (dump-fop 'sb!impl::fop-list* file)
647          (dump-byte 255 file)))))
648
649 ;;; If N > 255, must build list with one list operator, then list* operators.
650
651 (defun terminate-undotted-list (n file)
652   (declare (type index n) (type fasl-file file))
653   (case n
654     (1 (dump-fop 'sb!impl::fop-list-1 file))
655     (2 (dump-fop 'sb!impl::fop-list-2 file))
656     (3 (dump-fop 'sb!impl::fop-list-3 file))
657     (4 (dump-fop 'sb!impl::fop-list-4 file))
658     (5 (dump-fop 'sb!impl::fop-list-5 file))
659     (6 (dump-fop 'sb!impl::fop-list-6 file))
660     (7 (dump-fop 'sb!impl::fop-list-7 file))
661     (8 (dump-fop 'sb!impl::fop-list-8 file))
662     (T (cond ((< n 256)
663               (dump-fop 'sb!impl::fop-list file)
664               (dump-byte n file))
665              (t (dump-fop 'sb!impl::fop-list file)
666                 (dump-byte 255 file)
667                 (do ((nn (- n 255) (- nn 255)))
668                     ((< nn 256)
669                      (dump-fop 'sb!impl::fop-list* file)
670                      (dump-byte nn file))
671                   (declare (type index nn))
672                   (dump-fop 'sb!impl::fop-list* file)
673                   (dump-byte 255 file)))))))
674 \f
675 ;;;; array dumping
676
677 ;;; Dump the array thing.
678 (defun dump-array (x file)
679   (if (vectorp x)
680       (dump-vector x file)
681       (dump-multi-dim-array x file)))
682
683 ;;; Dump the vector object. If it's not simple, then actually dump a simple
684 ;;; version of it. But we enter the original in the EQ or EQUAL tables.
685 (defun dump-vector (x file)
686   (let ((simple-version (if (array-header-p x)
687                             (coerce x 'simple-array)
688                             x)))
689     (typecase simple-version
690       (simple-base-string
691        (unless (equal-check-table x file)
692          (dump-simple-string simple-version file)
693          (equal-save-object x file)))
694       (simple-vector
695        (dump-simple-vector simple-version file)
696        (eq-save-object x file))
697       ((simple-array single-float (*))
698        (dump-single-float-vector simple-version file)
699        (eq-save-object x file))
700       ((simple-array double-float (*))
701        (dump-double-float-vector simple-version file)
702        (eq-save-object x file))
703       #!+long-float
704       ((simple-array long-float (*))
705        (dump-long-float-vector simple-version file)
706        (eq-save-object x file))
707       ((simple-array (complex single-float) (*))
708        (dump-complex-single-float-vector simple-version file)
709        (eq-save-object x file))
710       ((simple-array (complex double-float) (*))
711        (dump-complex-double-float-vector simple-version file)
712        (eq-save-object x file))
713       #!+long-float
714       ((simple-array (complex long-float) (*))
715        (dump-complex-long-float-vector simple-version file)
716        (eq-save-object x file))
717       (t
718        (dump-i-vector simple-version file)
719        (eq-save-object x file)))))
720
721 ;;; Dump a SIMPLE-VECTOR, handling any circularities.
722 (defun dump-simple-vector (v file)
723   (declare (type simple-vector v) (type fasl-file file))
724   (note-potential-circularity v file)
725   (do ((index 0 (1+ index))
726        (length (length v))
727        (circ (fasl-file-circularity-table file)))
728       ((= index length)
729        (dump-fop* length
730                   sb!impl::fop-small-vector
731                   sb!impl::fop-vector
732                   file))
733     (let* ((obj (aref v index))
734            (ref (gethash obj circ)))
735       (cond (ref
736              (push (make-circularity :type :svset
737                                      :object v
738                                      :index index
739                                      :value obj
740                                      :enclosing-object ref)
741                    *circularities-detected*)
742              (sub-dump-object nil file))
743             (t
744              (sub-dump-object obj file))))))
745
746 (defun dump-i-vector (vec file &key data-only)
747   (declare (type (simple-array * (*)) vec))
748   (let ((len (length vec)))
749     (labels ((dump-unsigned-vector (size bytes)
750                (unless data-only
751                  (dump-fop 'sb!impl::fop-int-vector file)
752                  (dump-unsigned-32 len file)
753                  (dump-byte size file))
754                ;; The case which is easy to handle in a portable way is when
755                ;; the element size is a multiple of the output byte size, and
756                ;; happily that's the only case we need to be portable. (The
757                ;; cross-compiler has to output debug information (including
758                ;; (SIMPLE-ARRAY (UNSIGNED-BYTE 8) *).) The other cases are only
759                ;; needed in the target SBCL, so we let them be handled with
760                ;; unportable bit bashing.
761                (cond ((>= size 8) ; easy cases
762                       (multiple-value-bind (floor rem) (floor size 8)
763                         (assert (zerop rem))
764                         (dovector (i vec)
765                           (dump-integer-as-n-bytes i floor file))))
766                      (t ; harder cases, not supported in cross-compiler
767                       (dump-raw-bytes vec bytes file))))
768              (dump-signed-vector (size bytes)
769                ;; Note: Dumping specialized signed vectors isn't supported in
770                ;; the cross-compiler. (All cases here end up trying to call
771                ;; DUMP-RAW-BYTES, which isn't provided in the cross-compilation
772                ;; host, only on the target machine.)
773                (unless data-only
774                  (dump-fop 'sb!impl::fop-signed-int-vector file)
775                  (dump-unsigned-32 len file)
776                  (dump-byte size file))
777                (dump-raw-bytes vec bytes file)))
778       (etypecase vec
779         ;; KLUDGE: What exactly does the (ASH .. -3) stuff do? -- WHN 19990902
780         (simple-bit-vector
781          (dump-unsigned-vector 1 (ash (+ (the index len) 7) -3)))
782         ((simple-array (unsigned-byte 2) (*))
783          (dump-unsigned-vector 2 (ash (+ (the index (ash len 1)) 7) -3)))
784         ((simple-array (unsigned-byte 4) (*))
785          (dump-unsigned-vector 4 (ash (+ (the index (ash len 2)) 7) -3)))
786         ((simple-array (unsigned-byte 8) (*))
787          (dump-unsigned-vector 8 len))
788         ((simple-array (unsigned-byte 16) (*))
789          (dump-unsigned-vector 16 (* 2 len)))
790         ((simple-array (unsigned-byte 32) (*))
791          (dump-unsigned-vector 32 (* 4 len)))
792         ((simple-array (signed-byte 8) (*))
793          (dump-signed-vector 8 len))
794         ((simple-array (signed-byte 16) (*))
795          (dump-signed-vector 16 (* 2 len)))
796         ((simple-array (signed-byte 30) (*))
797          (dump-signed-vector 30 (* 4 len)))
798         ((simple-array (signed-byte 32) (*))
799          (dump-signed-vector 32 (* 4 len)))))))
800 \f
801 ;;; Dump characters and string-ish things.
802
803 (defun dump-character (ch file)
804   (dump-fop 'sb!impl::fop-short-character file)
805   (dump-byte (char-code ch) file))
806
807 ;;; a helper function shared by DUMP-SIMPLE-STRING and DUMP-SYMBOL
808 (defun dump-characters-of-string (s fasl-file)
809   (declare (type string s) (type fasl-file fasl-file))
810   (dovector (c s)
811     (dump-byte (char-code c) fasl-file))
812   (values))
813
814 ;;; Dump a SIMPLE-BASE-STRING.
815 ;;; FIXME: should be called DUMP-SIMPLE-BASE-STRING then
816 (defun dump-simple-string (s file)
817   (declare (type simple-base-string s))
818   (dump-fop* (length s)
819              sb!impl::fop-small-string
820              sb!impl::fop-string
821              file)
822   (dump-characters-of-string s file)
823   (values))
824
825 ;;; If we get here, it is assumed that the symbol isn't in the table,
826 ;;; but we are responsible for putting it there when appropriate. To
827 ;;; avoid too much special-casing, we always push the symbol in the
828 ;;; table, but don't record that we have done so if *COLD-LOAD-DUMP*
829 ;;; is true.
830 (defun dump-symbol (s file)
831   (let* ((pname (symbol-name s))
832          (pname-length (length pname))
833          (pkg (symbol-package s)))
834
835     (cond ((null pkg)
836            (dump-fop* pname-length
837                       sb!impl::fop-uninterned-small-symbol-save
838                       sb!impl::fop-uninterned-symbol-save
839                       file))
840           ;; CMU CL had FOP-SYMBOL-SAVE/FOP-SMALL-SYMBOL-SAVE fops which
841           ;; used the current value of *PACKAGE*. Unfortunately that's
842           ;; broken w.r.t. ANSI Common Lisp semantics, so those are gone
843           ;; from SBCL.
844           ;;((eq pkg *package*)
845           ;; (dump-fop* pname-length
846           ;;        sb!impl::fop-small-symbol-save
847           ;;        sb!impl::fop-symbol-save file))
848           ((eq pkg sb!int:*cl-package*)
849            (dump-fop* pname-length
850                       sb!impl::fop-lisp-small-symbol-save
851                       sb!impl::fop-lisp-symbol-save
852                       file))
853           ((eq pkg sb!int:*keyword-package*)
854            (dump-fop* pname-length
855                       sb!impl::fop-keyword-small-symbol-save
856                       sb!impl::fop-keyword-symbol-save
857                       file))
858           ((< pname-length 256)
859            (dump-fop* (dump-package pkg file)
860                       sb!impl::fop-small-symbol-in-byte-package-save
861                       sb!impl::fop-small-symbol-in-package-save
862                       file)
863            (dump-byte pname-length file))
864           (t
865            (dump-fop* (dump-package pkg file)
866                       sb!impl::fop-symbol-in-byte-package-save
867                       sb!impl::fop-symbol-in-package-save
868                       file)
869            (dump-unsigned-32 pname-length file)))
870
871     (dump-characters-of-string pname file)
872
873     (unless *cold-load-dump*
874       (setf (gethash s (fasl-file-eq-table file))
875             (fasl-file-table-free file)))
876
877     (incf (fasl-file-table-free file)))
878
879   (values))
880 \f
881 ;;;; component (function) dumping
882
883 (defun dump-segment (segment code-length fasl-file)
884   (declare (type sb!assem:segment segment)
885            (type fasl-file fasl-file))
886   (let* ((stream (fasl-file-stream fasl-file))
887          (nwritten (write-segment-contents segment stream)))
888     ;; In CMU CL there was no enforced connection between the CODE-LENGTH
889     ;; argument and the number of bytes actually written. I added this
890     ;; assertion while trying to debug portable genesis. -- WHN 19990902
891     (unless (= code-length nwritten)
892       (error "internal error, code-length=~D, nwritten=~D"
893              code-length
894              nwritten)))
895   ;; KLUDGE: It's not clear what this is trying to do, but it looks as though
896   ;; it's an implicit undocumented dependence on a 4-byte wordsize which could
897   ;; be painful in porting. Note also that there are other undocumented
898   ;; modulo-4 things scattered throughout the code and conditionalized
899   ;; with GENGC, and I don't know what those do either. -- WHN 19990323
900   #!+gengc (unless (zerop (logand code-length 3))
901              (dotimes (i (- 4 (logand code-length 3)))
902                (dump-byte 0 fasl-file)))
903   (values))
904
905 ;;; Dump all the fixups. Currently there are three flavors of fixup:
906 ;;;  - assembly routines: named by a symbol
907 ;;;  - foreign (C) symbols: named by a string
908 ;;;  - code object references: don't need a name.
909 (defun dump-fixups (fixups fasl-file)
910   (declare (list fixups) (type fasl-file fasl-file))
911   (dolist (info fixups)
912     ;; FIXME: Packing data with LIST in NOTE-FIXUP and unpacking them
913     ;; with FIRST, SECOND, and THIRD here is hard to follow and maintain.
914     ;; Perhaps we could define a FIXUP-INFO structure to use instead, and
915     ;; rename *FIXUPS* to *FIXUP-INFO-LIST*?
916     (let* ((kind (first info))
917            (fixup (second info))
918            (name (fixup-name fixup))
919            (flavor (fixup-flavor fixup))
920            (offset (third info)))
921       ;; FIXME: This OFFSET is not what's called OFFSET in
922       ;; the FIXUP structure, it's what's called POSN in NOTE-FIXUP.
923       ;; (As far as I can tell, FIXUP-OFFSET is not actually an offset,
924       ;; it's an internal label used instead of NAME for :CODE-OBJECT
925       ;; fixups. Notice that in the :CODE-OBJECT case, NAME is ignored.)
926       (dump-fop 'sb!impl::fop-normal-load fasl-file)
927       (let ((*cold-load-dump* t))
928         (dump-object kind fasl-file))
929       (dump-fop 'sb!impl::fop-maybe-cold-load fasl-file)
930       ;; Depending on the flavor, we may have various kinds of
931       ;; noise before the offset.
932       (ecase flavor
933         (:assembly-routine
934          (assert (symbolp name))
935          (dump-fop 'sb!impl::fop-normal-load fasl-file)
936          (let ((*cold-load-dump* t))
937            (dump-object name fasl-file))
938          (dump-fop 'sb!impl::fop-maybe-cold-load fasl-file)
939          (dump-fop 'sb!impl::fop-assembler-fixup fasl-file))
940         (:foreign
941          (assert (stringp name))
942          (dump-fop 'sb!impl::fop-foreign-fixup fasl-file)
943          (let ((len (length name)))
944            (assert (< len 256)) ; (limit imposed by fop definition)
945            (dump-byte len fasl-file)
946            (dotimes (i len)
947              (dump-byte (char-code (schar name i)) fasl-file))))
948         (:code-object
949          (assert (null name))
950          (dump-fop 'sb!impl::fop-code-object-fixup fasl-file)))
951       ;; No matter what the flavor, we'll always dump the offset.
952       (dump-unsigned-32 offset fasl-file)))
953   (values))
954
955 ;;; Dump out the constant pool and code-vector for component, push the
956 ;;; result in the table, and return the offset.
957 ;;;
958 ;;; The only tricky thing is handling constant-pool references to functions.
959 ;;; If we have already dumped the function, then we just push the code pointer.
960 ;;; Otherwise, we must create back-patching information so that the constant
961 ;;; will be set when the function is eventually dumped. This is a bit awkward,
962 ;;; since we don't have the handle for the code object being dumped while we
963 ;;; are dumping its constants.
964 ;;;
965 ;;; We dump trap objects in any unused slots or forward referenced slots.
966 (defun dump-code-object (component
967                          code-segment
968                          code-length
969                          trace-table-as-list
970                          fixups
971                          fasl-file)
972
973   (declare (type component component)
974            (list trace-table-as-list)
975            (type index code-length)
976            (type fasl-file fasl-file))
977
978   (let* ((2comp (component-info component))
979          (constants (ir2-component-constants 2comp))
980          (header-length (length constants))
981          (packed-trace-table (pack-trace-table trace-table-as-list))
982          (total-length (+ code-length
983                           (* (length packed-trace-table) tt-bytes-per-entry))))
984
985     (collect ((patches))
986
987       ;; Dump the debug info.
988       #!+gengc
989       (let ((info (debug-info-for-component component))
990             (*dump-only-valid-structures* nil))
991         (dump-object info fasl-file)
992         (let ((info-handle (dump-pop fasl-file)))
993           (dump-push info-handle fasl-file)
994           (push info-handle (fasl-file-debug-info fasl-file))))
995
996       ;; Dump the offset of the trace table.
997       (dump-object code-length fasl-file)
998       ;; KLUDGE: Now that we don't have GENGC, the trace table is hardwired
999       ;; to be empty. Could we get rid of trace tables? What are the
1000       ;; virtues of GENGC vs. GENCGC vs. whatnot?
1001
1002       ;; Dump the constants, noting any :entries that have to be fixed up.
1003       (do ((i sb!vm:code-constants-offset (1+ i)))
1004           ((>= i header-length))
1005         (let ((entry (aref constants i)))
1006           (etypecase entry
1007             (constant
1008              (dump-object (constant-value entry) fasl-file))
1009             (cons
1010              (ecase (car entry)
1011                (:entry
1012                 (let* ((info (leaf-info (cdr entry)))
1013                        (handle (gethash info
1014                                         (fasl-file-entry-table fasl-file))))
1015                   (cond
1016                    (handle
1017                     (dump-push handle fasl-file))
1018                    (t
1019                     (patches (cons info i))
1020                     (dump-fop 'sb!impl::fop-misc-trap fasl-file)))))
1021                (:load-time-value
1022                 (dump-push (cdr entry) fasl-file))
1023                (:fdefinition
1024                 (dump-object (cdr entry) fasl-file)
1025                 (dump-fop 'sb!impl::fop-fdefinition fasl-file))))
1026             (null
1027              (dump-fop 'sb!impl::fop-misc-trap fasl-file)))))
1028
1029       ;; Dump the debug info.
1030       #!-gengc
1031       (let ((info (debug-info-for-component component))
1032             (*dump-only-valid-structures* nil))
1033         (dump-object info fasl-file)
1034         (let ((info-handle (dump-pop fasl-file)))
1035           (dump-push info-handle fasl-file)
1036           (push info-handle (fasl-file-debug-info fasl-file))))
1037
1038       (let ((num-consts #!+gengc (- header-length
1039                                     sb!vm:code-debug-info-slot)
1040                         #!-gengc (- header-length
1041                                     sb!vm:code-trace-table-offset-slot))
1042             (total-length #!+gengc (ceiling total-length 4)
1043                           #!-gengc total-length))
1044         (cond ((and (< num-consts #x100) (< total-length #x10000))
1045                (dump-fop 'sb!impl::fop-small-code fasl-file)
1046                (dump-byte num-consts fasl-file)
1047                (dump-integer-as-n-bytes total-length 2 fasl-file))
1048               (t
1049                (dump-fop 'sb!impl::fop-code fasl-file)
1050                (dump-unsigned-32 num-consts fasl-file)
1051                (dump-unsigned-32 total-length fasl-file))))
1052
1053       ;; These two dumps are only ones which contribute to our TOTAL-LENGTH
1054       ;; value.
1055       (dump-segment code-segment code-length fasl-file)
1056       (dump-i-vector packed-trace-table fasl-file :data-only t)
1057
1058       ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it dumps aren't
1059       ;; included in the TOTAL-LENGTH passed to our FOP-CODE/FOP-SMALL-CODE
1060       ;; fop.
1061       (dump-fixups fixups fasl-file)
1062
1063       (dump-fop 'sb!impl::fop-sanctify-for-execution fasl-file)
1064       (let ((handle (dump-pop fasl-file)))
1065         (dolist (patch (patches))
1066           (push (cons handle (cdr patch))
1067                 (gethash (car patch) (fasl-file-patch-table fasl-file))))
1068         handle))))
1069
1070 (defun dump-assembler-routines (code-segment length fixups routines file)
1071   (dump-fop 'sb!impl::fop-assembler-code file)
1072   (dump-unsigned-32 #!+gengc (ceiling length 4)
1073                     #!-gengc length
1074                     file)
1075   (write-segment-contents code-segment (fasl-file-stream file))
1076   (dolist (routine routines)
1077     (dump-fop 'sb!impl::fop-normal-load file)
1078     (let ((*cold-load-dump* t))
1079       (dump-object (car routine) file))
1080     (dump-fop 'sb!impl::fop-maybe-cold-load file)
1081     (dump-fop 'sb!impl::fop-assembler-routine file)
1082     (dump-unsigned-32 (label-position (cdr routine)) file))
1083   (dump-fixups fixups file)
1084   (dump-fop 'sb!impl::fop-sanctify-for-execution file)
1085   (dump-pop file))
1086
1087 ;;; Dump a function-entry data structure corresponding to Entry to File.
1088 ;;; Code-Handle is the table offset of the code object for the component.
1089 ;;;
1090 ;;; If the entry is a DEFUN, then we also dump a FOP-FSET so that the cold
1091 ;;; loader can instantiate the definition at cold-load time, allowing forward
1092 ;;; references to functions in top-level forms.
1093 (defun dump-one-entry (entry code-handle file)
1094   (declare (type entry-info entry) (type index code-handle)
1095            (type fasl-file file))
1096   (let ((name (entry-info-name entry)))
1097     (dump-push code-handle file)
1098     (dump-object name file)
1099     (dump-object (entry-info-arguments entry) file)
1100     (dump-object (entry-info-type entry) file)
1101     (dump-fop 'sb!impl::fop-function-entry file)
1102     (dump-unsigned-32 (label-position (entry-info-offset entry)) file)
1103     (let ((handle (dump-pop file)))
1104       (when (and name (or (symbolp name) (listp name)))
1105         (dump-object name file)
1106         (dump-push handle file)
1107         (dump-fop 'sb!impl::fop-fset file))
1108       handle)))
1109
1110 ;;; Alter the code object referenced by Code-Handle at the specified Offset,
1111 ;;; storing the object referenced by Entry-Handle.
1112 (defun dump-alter-code-object (code-handle offset entry-handle file)
1113   (declare (type index code-handle entry-handle offset) (type fasl-file file))
1114   (dump-push code-handle file)
1115   (dump-push entry-handle file)
1116   (dump-fop* offset
1117              sb!impl::fop-byte-alter-code
1118              sb!impl::fop-alter-code
1119              file)
1120   (values))
1121
1122 ;;; Dump the code, constants, etc. for component. We pass in the assembler
1123 ;;; fixups, code vector and node info.
1124 (defun fasl-dump-component (component
1125                             code-segment
1126                             code-length
1127                             trace-table
1128                             fixups
1129                             file)
1130   (declare (type component component) (list trace-table) (type fasl-file file))
1131
1132   (dump-fop 'sb!impl::fop-verify-empty-stack file)
1133   (dump-fop 'sb!impl::fop-verify-table-size file)
1134   (dump-unsigned-32 (fasl-file-table-free file) file)
1135
1136   #!+sb-dyncount
1137   (let ((info (ir2-component-dyncount-info (component-info component))))
1138     (when info
1139       (fasl-validate-structure info file)))
1140
1141   (let ((code-handle (dump-code-object component
1142                                        code-segment
1143                                        code-length
1144                                        trace-table
1145                                        fixups
1146                                        file))
1147         (2comp (component-info component)))
1148     (dump-fop 'sb!impl::fop-verify-empty-stack file)
1149
1150     (dolist (entry (ir2-component-entries 2comp))
1151       (let ((entry-handle (dump-one-entry entry code-handle file)))
1152         (setf (gethash entry (fasl-file-entry-table file)) entry-handle)
1153
1154         (let ((old (gethash entry (fasl-file-patch-table file))))
1155           ;; KLUDGE: All this code is shared with FASL-DUMP-BYTE-COMPONENT,
1156           ;; and should probably be gathered up into a named function
1157           ;; (DUMP-PATCHES?) called from both functions.
1158           (when old
1159             (dolist (patch old)
1160               (dump-alter-code-object (car patch)
1161                                       (cdr patch)
1162                                       entry-handle
1163                                       file))
1164             (remhash entry (fasl-file-patch-table file)))))))
1165   (values))
1166
1167 (defun dump-byte-code-object (segment code-length constants file)
1168   (declare (type sb!assem:segment segment)
1169            (type index code-length)
1170            (type vector constants)
1171            (type fasl-file file))
1172   (collect ((entry-patches))
1173
1174     ;; Dump the debug info.
1175     #!+gengc
1176     (let ((info (make-debug-info
1177                  :name (component-name *component-being-compiled*)))
1178           (*dump-only-valid-structures* nil))
1179       (dump-object info file)
1180       (let ((info-handle (dump-pop file)))
1181         (dump-push info-handle file)
1182         (push info-handle (fasl-file-debug-info file))))
1183
1184     ;; The "trace table" is initialized by loader to hold a list of all byte
1185     ;; functions in this code object (for debug info.)
1186     (dump-object nil file)
1187
1188     ;; Dump the constants.
1189     (dotimes (i (length constants))
1190       (let ((entry (aref constants i)))
1191         (etypecase entry
1192           (constant
1193            (dump-object (constant-value entry) file))
1194           (null
1195            (dump-fop 'sb!impl::fop-misc-trap file))
1196           (list
1197            (ecase (car entry)
1198              (:entry
1199               (let* ((info (leaf-info (cdr entry)))
1200                      (handle (gethash info (fasl-file-entry-table file))))
1201                 (cond
1202                  (handle
1203                   (dump-push handle file))
1204                  (t
1205                   (entry-patches (cons info
1206                                        (+ i sb!vm:code-constants-offset)))
1207                   (dump-fop 'sb!impl::fop-misc-trap file)))))
1208              (:load-time-value
1209               (dump-push (cdr entry) file))
1210              (:fdefinition
1211               (dump-object (cdr entry) file)
1212               (dump-fop 'sb!impl::fop-fdefinition file))
1213              (:type-predicate
1214               (dump-object 'load-type-predicate file)
1215               (let ((*unparse-function-type-simplify* t))
1216                 (dump-object (type-specifier (cdr entry)) file))
1217               (dump-fop 'sb!impl::fop-funcall file)
1218               (dump-byte 1 file)))))))
1219
1220     ;; Dump the debug info.
1221     #!-gengc
1222     (let ((info (make-debug-info :name
1223                                  (component-name *component-being-compiled*)))
1224           (*dump-only-valid-structures* nil))
1225       (dump-object info file)
1226       (let ((info-handle (dump-pop file)))
1227         (dump-push info-handle file)
1228         (push info-handle (fasl-file-debug-info file))))
1229
1230     (let ((num-consts #!+gengc (+ (length constants) 2)
1231                       #!-gengc (1+ (length constants)))
1232           (code-length #!+gengc (ceiling code-length 4)
1233                        #!-gengc code-length))
1234       (cond ((and (< num-consts #x100) (< code-length #x10000))
1235              (dump-fop 'sb!impl::fop-small-code file)
1236              (dump-byte num-consts file)
1237              (dump-integer-as-n-bytes code-length 2 file))
1238             (t
1239              (dump-fop 'sb!impl::fop-code file)
1240              (dump-unsigned-32 num-consts file)
1241              (dump-unsigned-32 code-length file))))
1242     (dump-segment segment code-length file)
1243     (let ((code-handle (dump-pop file))
1244           (patch-table (fasl-file-patch-table file)))
1245       (dolist (patch (entry-patches))
1246         (push (cons code-handle (cdr patch))
1247               (gethash (car patch) patch-table)))
1248       code-handle)))
1249
1250 ;;; Dump a BYTE-FUNCTION object. We dump the layout and
1251 ;;; funcallable-instance info, but rely on the loader setting up the correct
1252 ;;; funcallable-instance-function.
1253 (defun dump-byte-function (xep code-handle file)
1254   (let ((nslots (- (get-closure-length xep)
1255                    ;; 1- for header
1256                    (1- sb!vm:funcallable-instance-info-offset))))
1257     (dotimes (i nslots)
1258       (if (zerop i)
1259           (dump-push code-handle file)
1260           (dump-object (%funcallable-instance-info xep i) file)))
1261     (dump-object (%funcallable-instance-layout xep) file)
1262     (dump-fop 'sb!impl::fop-make-byte-compiled-function file)
1263     (dump-byte nslots file))
1264   (values))
1265
1266 ;;; Dump a byte-component. This is similar to FASL-DUMP-COMPONENT, but
1267 ;;; different.
1268 (defun fasl-dump-byte-component (segment length constants xeps file)
1269   (declare (type sb!assem:segment segment)
1270            (type index length)
1271            (type vector constants)
1272            (type list xeps)
1273            (type fasl-file file))
1274
1275   (let ((code-handle (dump-byte-code-object segment length constants file)))
1276     (dolist (noise xeps)
1277       (let* ((lambda (car noise))
1278              (info (lambda-info lambda))
1279              (xep (cdr noise)))
1280         (dump-byte-function xep code-handle file)
1281         (let* ((entry-handle (dump-pop file))
1282                (patch-table (fasl-file-patch-table file))
1283                (old (gethash info patch-table)))
1284           (setf (gethash info (fasl-file-entry-table file)) entry-handle)
1285           (when old
1286             (dolist (patch old)
1287               (dump-alter-code-object (car patch)
1288                                       (cdr patch)
1289                                       entry-handle
1290                                       file))
1291             (remhash info patch-table))))))
1292   (values))
1293
1294 ;;; Dump a FOP-FUNCALL to call an already dumped top-level lambda at load time.
1295 (defun fasl-dump-top-level-lambda-call (fun file)
1296   (declare (type clambda fun) (type fasl-file file))
1297   (let ((handle (gethash (leaf-info fun) (fasl-file-entry-table file))))
1298     (assert handle)
1299     (dump-push handle file)
1300     (dump-fop 'sb!impl::fop-funcall-for-effect file)
1301     (dump-byte 0 file))
1302   (values))
1303
1304 ;;; Compute the correct list of DEBUG-SOURCE structures and backpatch all of
1305 ;;; the dumped DEBUG-INFO structures. We clear the FASL-FILE-DEBUG-INFO,
1306 ;;; so that subsequent components with different source info may be dumped.
1307 (defun fasl-dump-source-info (info file)
1308   (declare (type source-info info) (type fasl-file file))
1309   (let ((res (debug-source-for-info info))
1310         (*dump-only-valid-structures* nil))
1311     (dump-object res file)
1312     (let ((res-handle (dump-pop file)))
1313       (dolist (info-handle (fasl-file-debug-info file))
1314         (dump-push res-handle file)
1315         (dump-fop 'sb!impl::fop-structset file)
1316         (dump-unsigned-32 info-handle file)
1317         (dump-unsigned-32 2 file))))
1318
1319   (setf (fasl-file-debug-info file) ())
1320   (values))
1321 \f
1322 ;;;; dumping structures
1323
1324 (defun dump-structure (struct file)
1325   ;; FIXME: Probably *DUMP-ONLY-VALID-STRUCTURES* should become constantly T,
1326   ;; right?
1327   (when *dump-only-valid-structures*
1328     (unless (gethash struct (fasl-file-valid-structures file))
1329       (error "attempt to dump invalid structure:~%  ~S~%How did this happen?"
1330              struct)))
1331   (note-potential-circularity struct file)
1332   (do ((index 0 (1+ index))
1333        (length (%instance-length struct))
1334        (circ (fasl-file-circularity-table file)))
1335       ((= index length)
1336        (dump-fop* length
1337                   sb!impl::fop-small-struct
1338                   sb!impl::fop-struct
1339                   file))
1340     (let* ((obj (%instance-ref struct index))
1341            (ref (gethash obj circ)))
1342       (cond (ref
1343              (push (make-circularity :type :struct-set
1344                                      :object struct
1345                                      :index index
1346                                      :value obj
1347                                      :enclosing-object ref)
1348                    *circularities-detected*)
1349              (sub-dump-object nil file))
1350             (t
1351              (sub-dump-object obj file))))))
1352
1353 (defun dump-layout (obj file)
1354   (when (layout-invalid obj)
1355     (compiler-error "attempt to dump reference to obsolete class: ~S"
1356                     (layout-class obj)))
1357   (let ((name (sb!xc:class-name (layout-class obj))))
1358     (unless name
1359       (compiler-error "dumping anonymous layout: ~S" obj))
1360     (dump-fop 'sb!impl::fop-normal-load file)
1361     (let ((*cold-load-dump* t))
1362       (dump-object name file))
1363     (dump-fop 'sb!impl::fop-maybe-cold-load file))
1364   (sub-dump-object (layout-inherits obj) file)
1365   (sub-dump-object (layout-depthoid obj) file)
1366   (sub-dump-object (layout-length obj) file)
1367   (dump-fop 'sb!impl::fop-layout file))