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