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