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