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