0.pre7.88:
[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 ;;; 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 (cyclic-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 (compound-object-p x)
435       (let ((*circularities-detected* ())
436             (circ (fasl-output-circularity-table file)))
437         (clrhash circ)
438         (sub-dump-object x file)
439         (when *circularities-detected*
440           (dump-circularities *circularities-detected* file)
441           (clrhash circ)))
442       (sub-dump-object x file)))
443 \f
444 ;;;; LOAD-TIME-VALUE and MAKE-LOAD-FORM support
445
446 ;;; Emit a funcall of the function and return the handle for the
447 ;;; result.
448 (defun fasl-dump-load-time-value-lambda (fun file)
449   (declare (type sb!c::clambda fun) (type fasl-output file))
450   (let ((handle (gethash (sb!c::leaf-info fun)
451                          (fasl-output-entry-table file))))
452     (aver handle)
453     (dump-push handle file)
454     (dump-fop 'fop-funcall file)
455     (dump-byte 0 file))
456   (dump-pop file))
457
458 ;;; Return T iff CONSTANT has not already been dumped. It's been
459 ;;; dumped if it's in the EQ table.
460 (defun fasl-constant-already-dumped-p (constant file)
461   (if (or (gethash constant (fasl-output-eq-table file))
462           (gethash constant (fasl-output-valid-structures file)))
463       t
464       nil))
465
466 ;;; Use HANDLE whenever we try to dump CONSTANT. HANDLE should have been
467 ;;; returned earlier by FASL-DUMP-LOAD-TIME-VALUE-LAMBDA.
468 (defun fasl-note-handle-for-constant (constant handle file)
469   (let ((table (fasl-output-eq-table file)))
470     (when (gethash constant table)
471       (error "~S already dumped?" constant))
472     (setf (gethash constant table) handle))
473   (values))
474
475 ;;; Note that the specified structure can just be dumped by
476 ;;; enumerating the slots.
477 (defun fasl-validate-structure (structure file)
478   (setf (gethash structure (fasl-output-valid-structures file)) t)
479   (values))
480 \f
481 ;;;; number dumping
482
483 ;;; Dump a ratio.
484 (defun dump-ratio (x file)
485   (sub-dump-object (numerator x) file)
486   (sub-dump-object (denominator x) file)
487   (dump-fop 'fop-ratio file))
488
489 ;;; Dump an integer.
490 (defun dump-integer (n file)
491   (typecase n
492     ((signed-byte 8)
493      (dump-fop 'fop-byte-integer file)
494      (dump-byte (logand #xFF n) file))
495     ((unsigned-byte 31)
496      (dump-fop 'fop-word-integer file)
497      (dump-unsigned-32 n file))
498     ((signed-byte 32)
499      (dump-fop 'fop-word-integer file)
500      (dump-integer-as-n-bytes n 4 file))
501     (t
502      (let ((bytes (ceiling (1+ (integer-length n)) 8)))
503        (dump-fop* bytes fop-small-integer fop-integer file)
504        (dump-integer-as-n-bytes n bytes file)))))
505
506 (defun dump-float (x file)
507   (etypecase x
508     (single-float
509      (dump-fop 'fop-single-float file)
510      (dump-integer-as-n-bytes (single-float-bits x) 4 file))
511     (double-float
512      (dump-fop 'fop-double-float file)
513      (let ((x x))
514        (declare (double-float x))
515        ;; FIXME: Why sometimes DUMP-UNSIGNED-32 and sometimes
516        ;; DUMP-INTEGER-AS-N-BYTES .. 4?
517        (dump-unsigned-32 (double-float-low-bits x) file)
518        (dump-integer-as-n-bytes (double-float-high-bits x) 4 file)))
519     #!+long-float
520     (long-float
521      (dump-fop 'fop-long-float file)
522      (dump-long-float x file))))
523
524 (defun dump-complex (x file)
525   (typecase x
526     #-sb-xc-host
527     ((complex single-float)
528      (dump-fop 'fop-complex-single-float file)
529      (dump-integer-as-n-bytes (single-float-bits (realpart x)) 4 file)
530      (dump-integer-as-n-bytes (single-float-bits (imagpart x)) 4 file))
531     #-sb-xc-host
532     ((complex double-float)
533      (dump-fop 'fop-complex-double-float file)
534      (let ((re (realpart x)))
535        (declare (double-float re))
536        (dump-unsigned-32 (double-float-low-bits re) file)
537        (dump-integer-as-n-bytes (double-float-high-bits re) 4 file))
538      (let ((im (imagpart x)))
539        (declare (double-float im))
540        (dump-unsigned-32 (double-float-low-bits im) file)
541        (dump-integer-as-n-bytes (double-float-high-bits im) 4 file)))
542     #!+long-float
543     ((complex long-float)
544      ;; (There's no easy way to mix #!+LONG-FLOAT and #-SB-XC-HOST
545      ;; conditionalization at read time, so we do this SB-XC-HOST
546      ;; conditional at runtime instead.)
547      #+sb-xc-host (error "can't dump COMPLEX-LONG-FLOAT in cross-compiler")
548      (dump-fop 'fop-complex-long-float file)
549      (dump-long-float (realpart x) file)
550      (dump-long-float (imagpart x) file))
551     (t
552      (sub-dump-object (realpart x) file)
553      (sub-dump-object (imagpart x) file)
554      (dump-fop 'fop-complex file))))
555 \f
556 ;;;; symbol dumping
557
558 ;;; Return the table index of PKG, adding the package to the table if
559 ;;; necessary. During cold load, we read the string as a normal string
560 ;;; so that we can do the package lookup at cold load time.
561 ;;;
562 ;;; FIXME: Despite the parallelism in names, the functionality of
563 ;;; this function is not parallel to other functions DUMP-FOO, e.g.
564 ;;; DUMP-SYMBOL and DUMP-LIST. The mapping between names and behavior
565 ;;; should be made more consistent.
566 (defun dump-package (pkg file)
567   (declare (type package pkg) (type fasl-output file))
568   (declare (values index))
569   (declare (inline assoc))
570   (cond ((cdr (assoc pkg (fasl-output-packages file) :test #'eq)))
571         (t
572          (unless *cold-load-dump*
573            (dump-fop 'fop-normal-load file))
574          (dump-simple-string (package-name pkg) file)
575          (dump-fop 'fop-package file)
576          (unless *cold-load-dump*
577            (dump-fop 'fop-maybe-cold-load file))
578          (let ((entry (dump-pop file)))
579            (push (cons pkg entry) (fasl-output-packages file))
580            entry))))
581 \f
582 ;;; dumper for lists
583
584 ;;; Dump a list, setting up patching information when there are
585 ;;; circularities. We scan down the list, checking for CDR and CAR
586 ;;; circularities.
587 ;;;
588 ;;; If there is a CDR circularity, we terminate the list with NIL and
589 ;;; make a CIRCULARITY notation for the CDR of the previous cons.
590 ;;;
591 ;;; If there is no CDR circularity, then we mark the current cons and
592 ;;; check for a CAR circularity. When there is a CAR circularity, we
593 ;;; make the CAR NIL initially, arranging for the current cons to be
594 ;;; patched later.
595 ;;;
596 ;;; Otherwise, we recursively call the dumper to dump the current
597 ;;; element.
598 ;;;
599 ;;; Marking of the conses is inhibited when *COLD-LOAD-DUMP* is true.
600 ;;; This inhibits all circularity detection.
601 (defun dump-list (list file)
602   (aver (and list
603              (not (gethash list (fasl-output-circularity-table file)))))
604   (do* ((l list (cdr l))
605         (n 0 (1+ n))
606         (circ (fasl-output-circularity-table file)))
607        ((atom l)
608         (cond ((null l)
609                (terminate-undotted-list n file))
610               (t
611                (sub-dump-object l file)
612                (terminate-dotted-list n file))))
613     (declare (type index n))
614     (let ((ref (gethash l circ)))
615       (when ref
616         (push (make-circularity :type :rplacd
617                                 :object list
618                                 :index (1- n)
619                                 :value l
620                                 :enclosing-object ref)
621               *circularities-detected*)
622         (terminate-undotted-list n file)
623         (return)))
624
625     (unless *cold-load-dump*
626       (setf (gethash l circ) list))
627
628     (let* ((obj (car l))
629            (ref (gethash obj circ)))
630       (cond (ref
631              (push (make-circularity :type :rplaca
632                                      :object list
633                                      :index n
634                                      :value obj
635                                      :enclosing-object ref)
636                    *circularities-detected*)
637              (sub-dump-object nil file))
638             (t
639              (sub-dump-object obj file))))))
640
641 (defun terminate-dotted-list (n file)
642   (declare (type index n) (type fasl-output file))
643   (case n
644     (1 (dump-fop 'fop-list*-1 file))
645     (2 (dump-fop 'fop-list*-2 file))
646     (3 (dump-fop 'fop-list*-3 file))
647     (4 (dump-fop 'fop-list*-4 file))
648     (5 (dump-fop 'fop-list*-5 file))
649     (6 (dump-fop 'fop-list*-6 file))
650     (7 (dump-fop 'fop-list*-7 file))
651     (8 (dump-fop 'fop-list*-8 file))
652     (T (do ((nn n (- nn 255)))
653            ((< nn 256)
654             (dump-fop 'fop-list* file)
655             (dump-byte nn file))
656          (declare (type index nn))
657          (dump-fop 'fop-list* file)
658          (dump-byte 255 file)))))
659
660 ;;; If N > 255, must build list with one LIST operator, then LIST*
661 ;;; operators.
662
663 (defun terminate-undotted-list (n file)
664   (declare (type index n) (type fasl-output file))
665   (case n
666     (1 (dump-fop 'fop-list-1 file))
667     (2 (dump-fop 'fop-list-2 file))
668     (3 (dump-fop 'fop-list-3 file))
669     (4 (dump-fop 'fop-list-4 file))
670     (5 (dump-fop 'fop-list-5 file))
671     (6 (dump-fop 'fop-list-6 file))
672     (7 (dump-fop 'fop-list-7 file))
673     (8 (dump-fop 'fop-list-8 file))
674     (T (cond ((< n 256)
675               (dump-fop 'fop-list file)
676               (dump-byte n file))
677              (t (dump-fop 'fop-list file)
678                 (dump-byte 255 file)
679                 (do ((nn (- n 255) (- nn 255)))
680                     ((< nn 256)
681                      (dump-fop 'fop-list* file)
682                      (dump-byte nn file))
683                   (declare (type index nn))
684                   (dump-fop 'fop-list* file)
685                   (dump-byte 255 file)))))))
686 \f
687 ;;;; array dumping
688
689 ;;; Dump the array thing.
690 (defun dump-array (x file)
691   (if (vectorp x)
692       (dump-vector x file)
693       (dump-multi-dim-array x file)))
694
695 ;;; Dump the vector object. If it's not simple, then actually dump a
696 ;;; simple version of it. But we enter the original in the EQ or EQUAL
697 ;;; tables.
698 (defun dump-vector (x file)
699   (let ((simple-version (if (array-header-p x)
700                             (coerce x 'simple-array)
701                             x)))
702     (typecase simple-version
703       (simple-base-string
704        (unless (equal-check-table x file)
705          (dump-simple-string simple-version file)
706          (equal-save-object x file)))
707       (simple-vector
708        (dump-simple-vector simple-version file)
709        (eq-save-object x file))
710       ((simple-array single-float (*))
711        (dump-single-float-vector simple-version file)
712        (eq-save-object x file))
713       ((simple-array double-float (*))
714        (dump-double-float-vector simple-version file)
715        (eq-save-object x file))
716       #!+long-float
717       ((simple-array long-float (*))
718        (dump-long-float-vector simple-version file)
719        (eq-save-object x file))
720       ((simple-array (complex single-float) (*))
721        (dump-complex-single-float-vector simple-version file)
722        (eq-save-object x file))
723       ((simple-array (complex double-float) (*))
724        (dump-complex-double-float-vector simple-version file)
725        (eq-save-object x file))
726       #!+long-float
727       ((simple-array (complex long-float) (*))
728        (dump-complex-long-float-vector simple-version file)
729        (eq-save-object x file))
730       (t
731        (dump-i-vector simple-version file)
732        (eq-save-object x file)))))
733
734 ;;; Dump a SIMPLE-VECTOR, handling any circularities.
735 (defun dump-simple-vector (v file)
736   (declare (type simple-vector v) (type fasl-output file))
737   (note-potential-circularity v file)
738   (do ((index 0 (1+ index))
739        (length (length v))
740        (circ (fasl-output-circularity-table file)))
741       ((= index length)
742        (dump-fop* length fop-small-vector fop-vector file))
743     (let* ((obj (aref v index))
744            (ref (gethash obj circ)))
745       (cond (ref
746              (push (make-circularity :type :svset
747                                      :object v
748                                      :index index
749                                      :value obj
750                                      :enclosing-object ref)
751                    *circularities-detected*)
752              (sub-dump-object nil file))
753             (t
754              (sub-dump-object obj file))))))
755
756 (defun dump-i-vector (vec file &key data-only)
757   (declare (type (simple-array * (*)) vec))
758   (let ((len (length vec)))
759     (labels ((dump-unsigned-vector (size bytes)
760                (unless data-only
761                  (dump-fop 'fop-int-vector file)
762                  (dump-unsigned-32 len file)
763                  (dump-byte size file))
764                ;; The case which is easy to handle in a portable way is when
765                ;; the element size is a multiple of the output byte size, and
766                ;; happily that's the only case we need to be portable. (The
767                ;; cross-compiler has to output debug information (including
768                ;; (SIMPLE-ARRAY (UNSIGNED-BYTE 8) *).) The other cases are only
769                ;; needed in the target SBCL, so we let them be handled with
770                ;; unportable bit bashing.
771                (cond ((>= size 8) ; easy cases
772                       (multiple-value-bind (floor rem) (floor size 8)
773                         (aver (zerop rem))
774                         (dovector (i vec)
775                           (dump-integer-as-n-bytes i floor file))))
776                      (t ; harder cases, not supported in cross-compiler
777                       (dump-raw-bytes vec bytes file))))
778              (dump-signed-vector (size bytes)
779                ;; Note: Dumping specialized signed vectors isn't
780                ;; supported in the cross-compiler. (All cases here end
781                ;; up trying to call DUMP-RAW-BYTES, which isn't
782                ;; provided in the cross-compilation host, only on the
783                ;; target machine.)
784                (unless data-only
785                  (dump-fop 'fop-signed-int-vector file)
786                  (dump-unsigned-32 len file)
787                  (dump-byte size file))
788                (dump-raw-bytes vec bytes file)))
789       (etypecase vec
790         ;; KLUDGE: What exactly does the (ASH .. -3) stuff do? -- WHN 19990902
791         (simple-bit-vector
792          (dump-unsigned-vector 1 (ash (+ (the index len) 7) -3)))
793         ((simple-array (unsigned-byte 2) (*))
794          (dump-unsigned-vector 2 (ash (+ (the index (ash len 1)) 7) -3)))
795         ((simple-array (unsigned-byte 4) (*))
796          (dump-unsigned-vector 4 (ash (+ (the index (ash len 2)) 7) -3)))
797         ((simple-array (unsigned-byte 8) (*))
798          (dump-unsigned-vector 8 len))
799         ((simple-array (unsigned-byte 16) (*))
800          (dump-unsigned-vector 16 (* 2 len)))
801         ((simple-array (unsigned-byte 32) (*))
802          (dump-unsigned-vector 32 (* 4 len)))
803         ((simple-array (signed-byte 8) (*))
804          (dump-signed-vector 8 len))
805         ((simple-array (signed-byte 16) (*))
806          (dump-signed-vector 16 (* 2 len)))
807         ((simple-array (signed-byte 30) (*))
808          (dump-signed-vector 30 (* 4 len)))
809         ((simple-array (signed-byte 32) (*))
810          (dump-signed-vector 32 (* 4 len)))))))
811 \f
812 ;;; Dump characters and string-ish things.
813
814 (defun dump-character (ch file)
815   (dump-fop 'fop-short-character file)
816   (dump-byte (char-code ch) file))
817
818 ;;; a helper function shared by DUMP-SIMPLE-STRING and DUMP-SYMBOL
819 (defun dump-characters-of-string (s fasl-output)
820   (declare (type string s) (type fasl-output fasl-output))
821   (dovector (c s)
822     (dump-byte (char-code c) fasl-output))
823   (values))
824
825 ;;; Dump a SIMPLE-BASE-STRING.
826 ;;; FIXME: should be called DUMP-SIMPLE-BASE-STRING then
827 (defun dump-simple-string (s file)
828   (declare (type simple-base-string s))
829   (dump-fop* (length s) fop-small-string fop-string file)
830   (dump-characters-of-string s file)
831   (values))
832
833 ;;; If we get here, it is assumed that the symbol isn't in the table,
834 ;;; but we are responsible for putting it there when appropriate. To
835 ;;; avoid too much special-casing, we always push the symbol in the
836 ;;; table, but don't record that we have done so if *COLD-LOAD-DUMP*
837 ;;; is true.
838 (defun dump-symbol (s file)
839   (declare (type fasl-output file))
840   (let* ((pname (symbol-name s))
841          (pname-length (length pname))
842          (pkg (symbol-package s)))
843
844     (cond ((null pkg)
845            (dump-fop* pname-length
846                       fop-uninterned-small-symbol-save
847                       fop-uninterned-symbol-save
848                       file))
849           ;; CMU CL had FOP-SYMBOL-SAVE/FOP-SMALL-SYMBOL-SAVE fops which
850           ;; used the current value of *PACKAGE*. Unfortunately that's
851           ;; broken w.r.t. ANSI Common Lisp semantics, so those are gone
852           ;; from SBCL.
853           ;;((eq pkg *package*)
854           ;; (dump-fop* pname-length
855           ;;        fop-small-symbol-save
856           ;;        fop-symbol-save file))
857           ((eq pkg sb!int:*cl-package*)
858            (dump-fop* pname-length
859                       fop-lisp-small-symbol-save
860                       fop-lisp-symbol-save
861                       file))
862           ((eq pkg sb!int:*keyword-package*)
863            (dump-fop* pname-length
864                       fop-keyword-small-symbol-save
865                       fop-keyword-symbol-save
866                       file))
867           ((< pname-length 256)
868            (dump-fop* (dump-package pkg file)
869                       fop-small-symbol-in-byte-package-save
870                       fop-small-symbol-in-package-save
871                       file)
872            (dump-byte pname-length file))
873           (t
874            (dump-fop* (dump-package pkg file)
875                       fop-symbol-in-byte-package-save
876                       fop-symbol-in-package-save
877                       file)
878            (dump-unsigned-32 pname-length file)))
879
880     (dump-characters-of-string pname file)
881
882     (unless *cold-load-dump*
883       (setf (gethash s (fasl-output-eq-table file))
884             (fasl-output-table-free file)))
885
886     (incf (fasl-output-table-free file)))
887
888   (values))
889 \f
890 ;;;; component (function) dumping
891
892 (defun dump-segment (segment code-length fasl-output)
893   (declare (type sb!assem:segment segment)
894            (type fasl-output fasl-output))
895   (let* ((stream (fasl-output-stream fasl-output))
896          (nwritten (write-segment-contents segment stream)))
897     ;; In CMU CL there was no enforced connection between the CODE-LENGTH
898     ;; argument and the number of bytes actually written. I added this
899     ;; assertion while trying to debug portable genesis. -- WHN 19990902
900     (unless (= code-length nwritten)
901       (error "internal error, code-length=~W, nwritten=~W"
902              code-length
903              nwritten)))
904   (values))
905
906 ;;; Dump all the fixups. Currently there are three flavors of fixup:
907 ;;;  - assembly routines: named by a symbol
908 ;;;  - foreign (C) symbols: named by a string
909 ;;;  - code object references: don't need a name.
910 (defun dump-fixups (fixups fasl-output)
911   (declare (list fixups) (type fasl-output fasl-output))
912   (dolist (info fixups)
913     ;; FIXME: Packing data with LIST in NOTE-FIXUP and unpacking them
914     ;; with FIRST, SECOND, and THIRD here is hard to follow and
915     ;; maintain. Perhaps we could define a FIXUP-INFO structure to use
916     ;; instead, and rename *FIXUPS* to *FIXUP-INFO-LIST*?
917     (let* ((kind (first info))
918            (fixup (second info))
919            (name (fixup-name fixup))
920            (flavor (fixup-flavor fixup))
921            (offset (third info)))
922       ;; FIXME: This OFFSET is not what's called OFFSET in the FIXUP
923       ;; structure, it's what's called POSN in NOTE-FIXUP. (As far as
924       ;; I can tell, FIXUP-OFFSET is not actually an offset, it's an
925       ;; internal label used instead of NAME for :CODE-OBJECT fixups.
926       ;; Notice that in the :CODE-OBJECT case, NAME is ignored.)
927       (dump-fop 'fop-normal-load fasl-output)
928       (let ((*cold-load-dump* t))
929         (dump-object kind fasl-output))
930       (dump-fop 'fop-maybe-cold-load fasl-output)
931       ;; Depending on the flavor, we may have various kinds of
932       ;; noise before the offset.
933       (ecase flavor
934         (:assembly-routine
935          (aver (symbolp name))
936          (dump-fop 'fop-normal-load fasl-output)
937          (let ((*cold-load-dump* t))
938            (dump-object name fasl-output))
939          (dump-fop 'fop-maybe-cold-load fasl-output)
940          (dump-fop 'fop-assembler-fixup fasl-output))
941         (:foreign
942          (aver (stringp name))
943          (dump-fop 'fop-foreign-fixup fasl-output)
944          (let ((len (length name)))
945            (aver (< len 256)) ; (limit imposed by fop definition)
946            (dump-byte len fasl-output)
947            (dotimes (i len)
948              (dump-byte (char-code (schar name i)) fasl-output))))
949         (:code-object
950          (aver (null name))
951          (dump-fop 'fop-code-object-fixup fasl-output)))
952       ;; No matter what the flavor, we'll always dump the offset.
953       (dump-unsigned-32 offset fasl-output)))
954   (values))
955
956 ;;; Dump out the constant pool and code-vector for component, push the
957 ;;; result in the table, and return the offset.
958 ;;;
959 ;;; The only tricky thing is handling constant-pool references to
960 ;;; functions. If we have already dumped the function, then we just
961 ;;; push the code pointer. Otherwise, we must create back-patching
962 ;;; information so that the constant will be set when the function is
963 ;;; eventually dumped. This is a bit awkward, since we don't have the
964 ;;; handle for the code object being dumped while we are dumping its
965 ;;; constants.
966 ;;;
967 ;;; We dump trap objects in any unused slots or forward referenced slots.
968 (defun dump-code-object (component
969                          code-segment
970                          code-length
971                          trace-table-as-list
972                          fixups
973                          fasl-output)
974
975   (declare (type component component)
976            (list trace-table-as-list)
977            (type index code-length)
978            (type fasl-output fasl-output))
979
980   (let* ((2comp (component-info component))
981          (constants (sb!c::ir2-component-constants 2comp))
982          (header-length (length constants))
983          (packed-trace-table (pack-trace-table trace-table-as-list))
984          (total-length (+ code-length
985                           (* (length packed-trace-table)
986                              sb!c::tt-bytes-per-entry))))
987
988     (collect ((patches))
989
990       ;; Dump the offset of the trace table.
991       (dump-object code-length fasl-output)
992       ;; FIXME: As long as we don't have GENGC, the trace table is
993       ;; hardwired to be empty. And SBCL doesn't have GENGC (and as
994       ;; far as I know no modern CMU CL does either -- WHN
995       ;; 2001-10-05). So might we be able to get rid of trace tables?
996
997       ;; Dump the constants, noting any :entries that have to be fixed up.
998       (do ((i sb!vm:code-constants-offset (1+ i)))
999           ((>= i header-length))
1000         (let ((entry (aref constants i)))
1001           (etypecase entry
1002             (constant
1003              (dump-object (sb!c::constant-value entry) fasl-output))
1004             (cons
1005              (ecase (car entry)
1006                (:entry
1007                 (let* ((info (sb!c::leaf-info (cdr entry)))
1008                        (handle (gethash info
1009                                         (fasl-output-entry-table
1010                                          fasl-output))))
1011                   (cond
1012                    (handle
1013                     (dump-push handle fasl-output))
1014                    (t
1015                     (patches (cons info i))
1016                     (dump-fop 'fop-misc-trap fasl-output)))))
1017                (:load-time-value
1018                 (dump-push (cdr entry) fasl-output))
1019                (:fdefinition
1020                 (dump-object (cdr entry) fasl-output)
1021                 (dump-fop 'fop-fdefinition fasl-output))))
1022             (null
1023              (dump-fop 'fop-misc-trap fasl-output)))))
1024
1025       ;; Dump the debug info.
1026       (let ((info (sb!c::debug-info-for-component component))
1027             (*dump-only-valid-structures* nil))
1028         (dump-object info fasl-output)
1029         (let ((info-handle (dump-pop fasl-output)))
1030           (dump-push info-handle fasl-output)
1031           (push info-handle (fasl-output-debug-info fasl-output))))
1032
1033       (let ((num-consts (- header-length sb!vm:code-trace-table-offset-slot)))
1034         (cond ((and (< num-consts #x100) (< total-length #x10000))
1035                (dump-fop 'fop-small-code fasl-output)
1036                (dump-byte num-consts fasl-output)
1037                (dump-integer-as-n-bytes total-length 2 fasl-output))
1038               (t
1039                (dump-fop 'fop-code fasl-output)
1040                (dump-unsigned-32 num-consts fasl-output)
1041                (dump-unsigned-32 total-length fasl-output))))
1042
1043       ;; These two dumps are only ones which contribute to our
1044       ;; TOTAL-LENGTH value.
1045       (dump-segment code-segment code-length fasl-output)
1046       (dump-i-vector packed-trace-table fasl-output :data-only t)
1047
1048       ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it
1049       ;; dumps aren't included in the TOTAL-LENGTH passed to our
1050       ;; FOP-CODE/FOP-SMALL-CODE fop.
1051       (dump-fixups fixups fasl-output)
1052
1053       (dump-fop 'fop-sanctify-for-execution fasl-output)
1054       (let ((handle (dump-pop fasl-output)))
1055         (dolist (patch (patches))
1056           (push (cons handle (cdr patch))
1057                 (gethash (car patch)
1058                          (fasl-output-patch-table fasl-output))))
1059         handle))))
1060
1061 (defun dump-assembler-routines (code-segment length fixups routines file)
1062   (dump-fop 'fop-assembler-code file)
1063   (dump-unsigned-32 length file)
1064   (write-segment-contents code-segment (fasl-output-stream file))
1065   (dolist (routine routines)
1066     (dump-fop 'fop-normal-load file)
1067     (let ((*cold-load-dump* t))
1068       (dump-object (car routine) file))
1069     (dump-fop 'fop-maybe-cold-load file)
1070     (dump-fop 'fop-assembler-routine file)
1071     (dump-unsigned-32 (label-position (cdr routine)) file))
1072   (dump-fixups fixups file)
1073   (dump-fop 'fop-sanctify-for-execution file)
1074   (dump-pop file))
1075
1076 ;;; Dump a function-entry data structure corresponding to ENTRY to
1077 ;;; FILE. CODE-HANDLE is the table offset of the code object for the
1078 ;;; component.
1079 (defun dump-one-entry (entry code-handle file)
1080   (declare (type sb!c::entry-info entry) (type index code-handle)
1081            (type fasl-output file))
1082   (let ((name (sb!c::entry-info-name entry)))
1083     (dump-push code-handle file)
1084     (dump-object name file)
1085     (dump-object (sb!c::entry-info-arguments entry) file)
1086     (dump-object (sb!c::entry-info-type entry) file)
1087     (dump-fop 'fop-function-entry file)
1088     (dump-unsigned-32 (label-position (sb!c::entry-info-offset entry)) file)
1089     (dump-pop file)))
1090
1091 ;;; Alter the code object referenced by CODE-HANDLE at the specified
1092 ;;; OFFSET, storing the object referenced by ENTRY-HANDLE.
1093 (defun dump-alter-code-object (code-handle offset entry-handle file)
1094   (declare (type index code-handle entry-handle offset))
1095   (declare (type fasl-output file))
1096   (dump-push code-handle file)
1097   (dump-push entry-handle file)
1098   (dump-fop* offset fop-byte-alter-code fop-alter-code file)
1099   (values))
1100
1101 ;;; Dump the code, constants, etc. for component. We pass in the
1102 ;;; assembler fixups, code vector and node info.
1103 (defun fasl-dump-component (component
1104                             code-segment
1105                             code-length
1106                             trace-table
1107                             fixups
1108                             file)
1109   (declare (type component component) (list trace-table))
1110   (declare (type fasl-output file))
1111
1112   (dump-fop 'fop-verify-empty-stack file)
1113   (dump-fop 'fop-verify-table-size file)
1114   (dump-unsigned-32 (fasl-output-table-free file) file)
1115
1116   #!+sb-dyncount
1117   (let ((info (sb!c::ir2-component-dyncount-info (component-info component))))
1118     (when info
1119       (fasl-validate-structure info file)))
1120
1121   (let ((code-handle (dump-code-object component
1122                                        code-segment
1123                                        code-length
1124                                        trace-table
1125                                        fixups
1126                                        file))
1127         (2comp (component-info component)))
1128     (dump-fop 'fop-verify-empty-stack file)
1129
1130     (dolist (entry (sb!c::ir2-component-entries 2comp))
1131       (let ((entry-handle (dump-one-entry entry code-handle file)))
1132         (setf (gethash entry (fasl-output-entry-table file)) entry-handle)
1133         (let ((old (gethash entry (fasl-output-patch-table file))))
1134           (when old
1135             (dolist (patch old)
1136               (dump-alter-code-object (car patch)
1137                                       (cdr patch)
1138                                       entry-handle
1139                                       file))
1140             (remhash entry (fasl-output-patch-table file)))))))
1141   (values))
1142
1143 (defun dump-push-previously-dumped-fun (fun fasl-output)
1144   (declare (type sb!c::clambda fun))
1145   (let ((handle (gethash (sb!c::leaf-info fun)
1146                          (fasl-output-entry-table fasl-output))))
1147     (aver handle)
1148     (dump-push handle fasl-output))
1149   (values))
1150
1151 ;;; Dump a FOP-FUNCALL to call an already-dumped top level lambda at
1152 ;;; load time.
1153 (defun fasl-dump-toplevel-lambda-call (fun fasl-output)
1154   (declare (type sb!c::clambda fun))
1155   (dump-push-previously-dumped-fun fun fasl-output)
1156   (dump-fop 'fop-funcall-for-effect fasl-output)
1157   (dump-byte 0 fasl-output)
1158   (values))
1159
1160 ;;; Dump a FOP-FSET to arrange static linkage (at cold init) between
1161 ;;; FUN-NAME and the already-dumped function whose dump handle is
1162 ;;; FUN-DUMP-HANDLE.
1163 #+sb-xc-host
1164 (defun fasl-dump-cold-fset (fun-name fun-dump-handle fasl-output)
1165   (declare (type fixnum fun-dump-handle))
1166   (aver (legal-fun-name-p fun-name))
1167   (dump-non-immediate-object fun-name fasl-output)
1168   (dump-push fun-dump-handle fasl-output)
1169   (dump-fop 'fop-fset fasl-output)
1170   (values))
1171     
1172 ;;; Compute the correct list of DEBUG-SOURCE structures and backpatch
1173 ;;; all of the dumped DEBUG-INFO structures. We clear the
1174 ;;; FASL-OUTPUT-DEBUG-INFO, so that subsequent components with
1175 ;;; different source info may be dumped.
1176 (defun fasl-dump-source-info (info fasl-output)
1177   (declare (type sb!c::source-info info))
1178   (let ((res (sb!c::debug-source-for-info info))
1179         (*dump-only-valid-structures* nil))
1180     (dump-object res fasl-output)
1181     (let ((res-handle (dump-pop fasl-output)))
1182       (dolist (info-handle (fasl-output-debug-info fasl-output))
1183         (dump-push res-handle fasl-output)
1184         (dump-fop 'fop-structset fasl-output)
1185         (dump-unsigned-32 info-handle fasl-output)
1186         (dump-unsigned-32 2 fasl-output))))
1187   (setf (fasl-output-debug-info fasl-output) nil)
1188   (values))
1189 \f
1190 ;;;; dumping structures
1191
1192 (defun dump-structure (struct file)
1193   (when *dump-only-valid-structures*
1194     (unless (gethash struct (fasl-output-valid-structures file))
1195       (error "attempt to dump invalid structure:~%  ~S~%How did this happen?"
1196              struct)))
1197   (note-potential-circularity struct file)
1198   (do ((index 0 (1+ index))
1199        (length (%instance-length struct))
1200        (circ (fasl-output-circularity-table file)))
1201       ((= index length)
1202        (dump-fop* length fop-small-struct fop-struct file))
1203     (let* ((obj (%instance-ref struct index))
1204            (ref (gethash obj circ)))
1205       (cond (ref
1206              (push (make-circularity :type :struct-set
1207                                      :object struct
1208                                      :index index
1209                                      :value obj
1210                                      :enclosing-object ref)
1211                    *circularities-detected*)
1212              (sub-dump-object nil file))
1213             (t
1214              (sub-dump-object obj file))))))
1215
1216 (defun dump-layout (obj file)
1217   (when (layout-invalid obj)
1218     (compiler-error "attempt to dump reference to obsolete class: ~S"
1219                     (layout-class obj)))
1220   (let ((name (sb!xc:class-name (layout-class obj))))
1221     (unless name
1222       (compiler-error "dumping anonymous layout: ~S" obj))
1223     (dump-fop 'fop-normal-load file)
1224     (let ((*cold-load-dump* t))
1225       (dump-object name file))
1226     (dump-fop 'fop-maybe-cold-load file))
1227   (sub-dump-object (layout-inherits obj) file)
1228   (sub-dump-object (layout-depthoid obj) file)
1229   (sub-dump-object (layout-length obj) file)
1230   (dump-fop 'fop-layout file))