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