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