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