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