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