0.8.16.25:
[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          #+sb-xc-host
604          (dump-simple-base-string
605           (coerce (package-name pkg) 'simple-base-string)
606           file)
607          #-sb-xc-host
608          (#!+sb-unicode dump-simple-character-string
609           #!-sb-unicode dump-simple-base-string
610           (coerce (package-name pkg) '(simple-array character (*)))
611           file)
612          (dump-fop 'fop-package file)
613          (unless *cold-load-dump*
614            (dump-fop 'fop-maybe-cold-load file))
615          (let ((entry (dump-pop file)))
616            (push (cons pkg entry) (fasl-output-packages file))
617            entry))))
618 \f
619 ;;; dumper for lists
620
621 ;;; Dump a list, setting up patching information when there are
622 ;;; circularities. We scan down the list, checking for CDR and CAR
623 ;;; circularities.
624 ;;;
625 ;;; If there is a CDR circularity, we terminate the list with NIL and
626 ;;; make a CIRCULARITY notation for the CDR of the previous cons.
627 ;;;
628 ;;; If there is no CDR circularity, then we mark the current cons and
629 ;;; check for a CAR circularity. When there is a CAR circularity, we
630 ;;; make the CAR NIL initially, arranging for the current cons to be
631 ;;; patched later.
632 ;;;
633 ;;; Otherwise, we recursively call the dumper to dump the current
634 ;;; element.
635 ;;;
636 ;;; Marking of the conses is inhibited when *COLD-LOAD-DUMP* is true.
637 ;;; This inhibits all circularity detection.
638 (defun dump-list (list file)
639   (aver (and list
640              (not (gethash list (fasl-output-circularity-table file)))))
641   (do* ((l list (cdr l))
642         (n 0 (1+ n))
643         (circ (fasl-output-circularity-table file)))
644        ((atom l)
645         (cond ((null l)
646                (terminate-undotted-list n file))
647               (t
648                (sub-dump-object l file)
649                (terminate-dotted-list n file))))
650     (declare (type index n))
651     (let ((ref (gethash l circ)))
652       (when ref
653         (push (make-circularity :type :rplacd
654                                 :object list
655                                 :index (1- n)
656                                 :value l
657                                 :enclosing-object ref)
658               *circularities-detected*)
659         (terminate-undotted-list n file)
660         (return)))
661
662     (unless *cold-load-dump*
663       (setf (gethash l circ) list))
664
665     (let* ((obj (car l))
666            (ref (gethash obj circ)))
667       (cond (ref
668              (push (make-circularity :type :rplaca
669                                      :object list
670                                      :index n
671                                      :value obj
672                                      :enclosing-object ref)
673                    *circularities-detected*)
674              (sub-dump-object nil file))
675             (t
676              (sub-dump-object obj file))))))
677
678 (defun terminate-dotted-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 (do ((nn n (- nn 255)))
690            ((< nn 256)
691             (dump-fop 'fop-list* file)
692             (dump-byte nn file))
693          (declare (type index nn))
694          (dump-fop 'fop-list* file)
695          (dump-byte 255 file)))))
696
697 ;;; If N > 255, must build list with one LIST operator, then LIST*
698 ;;; operators.
699
700 (defun terminate-undotted-list (n file)
701   (declare (type index n) (type fasl-output file))
702   (case n
703     (1 (dump-fop 'fop-list-1 file))
704     (2 (dump-fop 'fop-list-2 file))
705     (3 (dump-fop 'fop-list-3 file))
706     (4 (dump-fop 'fop-list-4 file))
707     (5 (dump-fop 'fop-list-5 file))
708     (6 (dump-fop 'fop-list-6 file))
709     (7 (dump-fop 'fop-list-7 file))
710     (8 (dump-fop 'fop-list-8 file))
711     (T (cond ((< n 256)
712               (dump-fop 'fop-list file)
713               (dump-byte n file))
714              (t (dump-fop 'fop-list file)
715                 (dump-byte 255 file)
716                 (do ((nn (- n 255) (- nn 255)))
717                     ((< nn 256)
718                      (dump-fop 'fop-list* file)
719                      (dump-byte nn file))
720                   (declare (type index nn))
721                   (dump-fop 'fop-list* file)
722                   (dump-byte 255 file)))))))
723 \f
724 ;;;; array dumping
725
726 ;;; Dump the array thing.
727 (defun dump-array (x file)
728   (if (vectorp x)
729       (dump-vector x file)
730       (dump-multi-dim-array x file)))
731
732 ;;; Dump the vector object. If it's not simple, then actually dump a
733 ;;; simple version of it. But we enter the original in the EQ or EQUAL
734 ;;; tables.
735 (defun dump-vector (x file)
736   (let ((simple-version (if (array-header-p x)
737                             (coerce x `(simple-array
738                                         ,(array-element-type x)
739                                         (*)))
740                             x)))
741     (typecase simple-version
742       #+sb-xc-host
743       (simple-string
744        (unless (string-check-table x file)
745          (dump-simple-base-string simple-version file)
746          (string-save-object x file)))
747       #-sb-xc-host
748       (simple-base-string
749        (unless (string-check-table x file)
750          (dump-simple-base-string simple-version file)
751          (string-save-object x file)))
752       #-sb-xc-host
753       ((simple-array character (*))
754        #!+sb-unicode
755        (unless (string-check-table x file)
756          (dump-simple-character-string simple-version file)
757          (string-save-object x file))
758        #!-sb-unicode
759        (bug "how did we get here?"))
760       (simple-vector
761        (dump-simple-vector simple-version file)
762        (eq-save-object x file))
763       ((simple-array single-float (*))
764        (dump-single-float-vector simple-version file)
765        (eq-save-object x file))
766       ((simple-array double-float (*))
767        (dump-double-float-vector simple-version file)
768        (eq-save-object x file))
769       #!+long-float
770       ((simple-array long-float (*))
771        (dump-long-float-vector simple-version file)
772        (eq-save-object x file))
773       ((simple-array (complex single-float) (*))
774        (dump-complex-single-float-vector simple-version file)
775        (eq-save-object x file))
776       ((simple-array (complex double-float) (*))
777        (dump-complex-double-float-vector simple-version file)
778        (eq-save-object x file))
779       #!+long-float
780       ((simple-array (complex long-float) (*))
781        (dump-complex-long-float-vector simple-version file)
782        (eq-save-object x file))
783       (t
784        (dump-i-vector simple-version file)
785        (eq-save-object x file)))))
786
787 ;;; Dump a SIMPLE-VECTOR, handling any circularities.
788 (defun dump-simple-vector (v file)
789   (declare (type simple-vector v) (type fasl-output file))
790   (note-potential-circularity v file)
791   (do ((index 0 (1+ index))
792        (length (length v))
793        (circ (fasl-output-circularity-table file)))
794       ((= index length)
795        (dump-fop* length fop-small-vector fop-vector file))
796     (let* ((obj (aref v index))
797            (ref (gethash obj circ)))
798       (cond (ref
799              (push (make-circularity :type :svset
800                                      :object v
801                                      :index index
802                                      :value obj
803                                      :enclosing-object ref)
804                    *circularities-detected*)
805              (sub-dump-object nil file))
806             (t
807              (sub-dump-object obj file))))))
808
809 ;;; In the grand scheme of things I don't pretend to understand any
810 ;;; more how this works, or indeed whether.  But to write out specialized
811 ;;; vectors in the same format as fop-int-vector expects to read them
812 ;;; we need to be target-endian.  dump-integer-as-n-bytes always writes
813 ;;; little-endian (which is correct for all other integers) so for a bigendian
814 ;;; target we need to swap octets -- CSR, after DB
815
816 (defun octet-swap (word bits)
817   "BITS must be a multiple of 8"
818   (do ((input word (ash input -8))
819        (output 0 (logior (ash output 8) (logand input #xff)))
820        (bits bits (- bits 8)))
821       ((<= bits 0) output)))
822
823 (defun dump-i-vector (vec file &key data-only)
824   (declare (type (simple-array * (*)) vec))
825   (let ((len (length vec)))
826     (labels ((dump-unsigned-vector (size bytes)
827                (unless data-only
828                  (dump-fop 'fop-int-vector file)
829                  (dump-word len file)
830                  (dump-byte size file))
831                ;; The case which is easy to handle in a portable way is when
832                ;; the element size is a multiple of the output byte size, and
833                ;; happily that's the only case we need to be portable. (The
834                ;; cross-compiler has to output debug information (including
835                ;; (SIMPLE-ARRAY (UNSIGNED-BYTE 8) *).) The other cases are only
836                ;; needed in the target SBCL, so we let them be handled with
837                ;; unportable bit bashing.
838                (cond ((>= size 7) ; easy cases
839                       (multiple-value-bind (floor rem) (floor size 8)
840                         (aver (or (zerop rem) (= rem 7)))
841                         (when (= rem 7)
842                           (setq size (1+ size))
843                           (setq floor (1+ floor)))
844                         (dovector (i vec)
845                           (dump-integer-as-n-bytes
846                            (ecase sb!c:*backend-byte-order*
847                              (:little-endian i)
848                              (:big-endian (octet-swap i size)))
849                            floor file))))
850                      (t ; harder cases, not supported in cross-compiler
851                       (dump-raw-bytes vec bytes file))))
852              (dump-signed-vector (size bytes)
853                ;; Note: Dumping specialized signed vectors isn't
854                ;; supported in the cross-compiler. (All cases here end
855                ;; up trying to call DUMP-RAW-BYTES, which isn't
856                ;; provided in the cross-compilation host, only on the
857                ;; target machine.)
858                (unless data-only
859                  (dump-fop 'fop-signed-int-vector file)
860                  (dump-word len file)
861                  (dump-byte size file))
862                (dump-raw-bytes vec bytes file)))
863       (etypecase vec
864         #-sb-xc-host
865         ((simple-array nil (*))
866          (dump-unsigned-vector 0 0))
867         (simple-bit-vector
868          (dump-unsigned-vector 1 (ceiling len 8))) ; bits to bytes
869         ;; KLUDGE: This isn't the best way of expressing that the host
870         ;; may not have specializations for (unsigned-byte 2) and
871         ;; (unsigned-byte 4), which means that these types are
872         ;; type-equivalent to (simple-array (unsigned-byte 8) (*));
873         ;; the workaround is to remove them from the etypecase, since
874         ;; they can't be dumped from the cross-compiler anyway. --
875         ;; CSR, 2002-05-07
876         #-sb-xc-host
877         ((simple-array (unsigned-byte 2) (*))
878          (dump-unsigned-vector 2 (ceiling (ash len 1) 8))) ; bits to bytes
879         #-sb-xc-host
880         ((simple-array (unsigned-byte 4) (*))
881          (dump-unsigned-vector 4 (ceiling (ash len 2) 8))) ; bits to bytes
882         #-sb-xc-host
883         ((simple-array (unsigned-byte 7) (*))
884          (dump-unsigned-vector 7 len))
885         ((simple-array (unsigned-byte 8) (*))
886          (dump-unsigned-vector 8 len))
887         #-sb-xc-host
888         ((simple-array (unsigned-byte 15) (*))
889          (dump-unsigned-vector 15 (* 2 len)))
890         ((simple-array (unsigned-byte 16) (*))
891          (dump-unsigned-vector 16 (* 2 len)))
892         #-sb-xc-host
893         ((simple-array (unsigned-byte 31) (*))
894          (dump-unsigned-vector 31 (* 4 len)))
895         ((simple-array (unsigned-byte 32) (*))
896          (dump-unsigned-vector 32 (* 4 len)))
897         #-sb-xc-host
898         #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
899         ((simple-array (unsigned-byte-63) (*))
900          (dump-unsigned-vector 63 (* 8 len)))
901         #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
902         ((simple-array (unsigned-byte-64) (*))
903          (dump-unsigned-vector 64 (* 8 len)))
904         ((simple-array (signed-byte 8) (*))
905          (dump-signed-vector 8 len))
906         ((simple-array (signed-byte 16) (*))
907          (dump-signed-vector 16 (* 2 len)))
908         #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
909         ((simple-array (unsigned-byte 29) (*))
910          (dump-signed-vector 29 (* 4 len)))
911         #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
912         ((simple-array (signed-byte 30) (*))
913          (dump-signed-vector 30 (* 4 len)))
914         ((simple-array (signed-byte 32) (*))
915          (dump-signed-vector 32 (* 4 len)))
916         #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
917         ((simple-array (unsigned-byte 60) (*))
918          (dump-signed-vector 60 (* 8 len)))
919         #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
920         ((simple-array (signed-byte 61) (*))
921          (dump-signed-vector 61 (* 8 len)))
922         #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
923         ((simple-array (signed-byte 64) (*))
924          (dump-signed-vector 64 (* 8 len)))))))
925 \f
926 ;;; Dump characters and string-ish things.
927
928 (defun dump-character (char file)
929   (let ((code (sb!xc:char-code char)))
930     (cond
931       ((< code 256)
932   (dump-fop 'fop-short-character file)
933        (dump-byte code file))
934       (t
935        (dump-fop 'fop-character file)
936        (dump-word code file)))))
937
938 (defun dump-base-chars-of-string (s fasl-output)
939   (declare #+sb-xc-host (type simple-string s)
940            #-sb-xc-host (type simple-base-string s)
941            (type fasl-output fasl-output))
942   (dovector (c s)
943     (dump-byte (sb!xc:char-code c) fasl-output))
944   (values))
945
946
947 ;;; Dump a SIMPLE-BASE-STRING.
948 (defun dump-simple-base-string (s file)
949   #+sb-xc-host (declare (type simple-string s))
950   #-sb-xc-host (declare (type simple-base-string s))
951   (dump-fop* (length s) fop-small-base-string fop-base-string file)
952   (dump-base-chars-of-string s file)
953   (values))
954
955 ;;; If we get here, it is assumed that the symbol isn't in the table,
956 ;;; but we are responsible for putting it there when appropriate. To
957 ;;; avoid too much special-casing, we always push the symbol in the
958 ;;; table, but don't record that we have done so if *COLD-LOAD-DUMP*
959 ;;; is true.
960 (defun dump-symbol (s file)
961   (declare (type fasl-output file))
962   (let* ((pname (symbol-name s))
963          (pname-length (length pname))
964          (pkg (symbol-package s)))
965
966     (cond ((null pkg)
967            (dump-fop* pname-length
968                       fop-uninterned-small-symbol-save
969                       fop-uninterned-symbol-save
970                       file))
971           ;; CMU CL had FOP-SYMBOL-SAVE/FOP-SMALL-SYMBOL-SAVE fops which
972           ;; used the current value of *PACKAGE*. Unfortunately that's
973           ;; broken w.r.t. ANSI Common Lisp semantics, so those are gone
974           ;; from SBCL.
975           ;;((eq pkg *package*)
976           ;; (dump-fop* pname-length
977           ;;        fop-small-symbol-save
978           ;;        fop-symbol-save file))
979           ((eq pkg sb!int:*cl-package*)
980            (dump-fop* pname-length
981                       fop-lisp-small-symbol-save
982                       fop-lisp-symbol-save
983                       file))
984           ((eq pkg sb!int:*keyword-package*)
985            (dump-fop* pname-length
986                       fop-keyword-small-symbol-save
987                       fop-keyword-symbol-save
988                       file))
989           ((< pname-length 256)
990            (dump-fop* (dump-package pkg file)
991                       fop-small-symbol-in-byte-package-save
992                       fop-small-symbol-in-package-save
993                       file)
994            (dump-byte pname-length file))
995           (t
996            (dump-fop* (dump-package pkg file)
997                       fop-symbol-in-byte-package-save
998                       fop-symbol-in-package-save
999                       file)
1000            (dump-word pname-length file)))
1001
1002     #+sb-xc-host (dump-base-chars-of-string pname file)
1003     #-sb-xc-host (#!+sb-unicode dump-characters-of-string
1004                   #!-sb-unicode dump-base-chars-of-string
1005                   pname file)
1006
1007     (unless *cold-load-dump*
1008       (setf (gethash s (fasl-output-eq-table file))
1009             (fasl-output-table-free file)))
1010
1011     (incf (fasl-output-table-free file)))
1012
1013   (values))
1014 \f
1015 ;;;; component (function) dumping
1016
1017 (defun dump-segment (segment code-length fasl-output)
1018   (declare (type sb!assem:segment segment)
1019            (type fasl-output fasl-output))
1020   (let* ((stream (fasl-output-stream fasl-output))
1021          (n-written (write-segment-contents segment stream)))
1022     ;; In CMU CL there was no enforced connection between the CODE-LENGTH
1023     ;; argument and the number of bytes actually written. I added this
1024     ;; assertion while trying to debug portable genesis. -- WHN 19990902
1025     (unless (= code-length n-written)
1026       (bug "code-length=~W, n-written=~W" code-length n-written)))
1027   (values))
1028
1029 ;;; Dump all the fixups. Currently there are three flavors of fixup:
1030 ;;;  - assembly routines: named by a symbol
1031 ;;;  - foreign (C) symbols: named by a string
1032 ;;;  - code object references: don't need a name.
1033 (defun dump-fixups (fixups fasl-output)
1034   (declare (list fixups) (type fasl-output fasl-output))
1035   (dolist (note fixups)
1036     (let* ((kind (fixup-note-kind note))
1037            (fixup (fixup-note-fixup note))
1038            (position (fixup-note-position note))
1039            (name (fixup-name fixup))
1040            (flavor (fixup-flavor fixup)))
1041       (dump-fop 'fop-normal-load fasl-output)
1042       (let ((*cold-load-dump* t))
1043         (dump-object kind fasl-output))
1044       (dump-fop 'fop-maybe-cold-load fasl-output)
1045       ;; Depending on the flavor, we may have various kinds of
1046       ;; noise before the position.
1047       (ecase flavor
1048         (:assembly-routine
1049          (aver (symbolp name))
1050          (dump-fop 'fop-normal-load fasl-output)
1051          (let ((*cold-load-dump* t))
1052            (dump-object name fasl-output))
1053          (dump-fop 'fop-maybe-cold-load fasl-output)
1054          (dump-fop 'fop-assembler-fixup fasl-output))
1055         ((:foreign :foreign-dataref)
1056          (aver (stringp name))
1057          (ecase flavor
1058            (:foreign
1059             (dump-fop 'fop-foreign-fixup fasl-output))
1060            #!+linkage-table
1061            (:foreign-dataref
1062             (dump-fop 'fop-foreign-dataref-fixup fasl-output)))
1063          (let ((len (length name)))
1064            (aver (< len 256)) ; (limit imposed by fop definition)
1065            (dump-byte len fasl-output)
1066            (dotimes (i len)
1067              (dump-byte (char-code (schar name i)) fasl-output))))
1068         (:code-object
1069          (aver (null name))
1070          (dump-fop 'fop-code-object-fixup fasl-output)))
1071       ;; No matter what the flavor, we'll always dump the position
1072       (dump-word position fasl-output)))
1073   (values))
1074
1075 ;;; Dump out the constant pool and code-vector for component, push the
1076 ;;; result in the table, and return the offset.
1077 ;;;
1078 ;;; The only tricky thing is handling constant-pool references to
1079 ;;; functions. If we have already dumped the function, then we just
1080 ;;; push the code pointer. Otherwise, we must create back-patching
1081 ;;; information so that the constant will be set when the function is
1082 ;;; eventually dumped. This is a bit awkward, since we don't have the
1083 ;;; handle for the code object being dumped while we are dumping its
1084 ;;; constants.
1085 ;;;
1086 ;;; We dump trap objects in any unused slots or forward referenced slots.
1087 (defun dump-code-object (component
1088                          code-segment
1089                          code-length
1090                          trace-table-as-list
1091                          fixups
1092                          fasl-output)
1093
1094   (declare (type component component)
1095            (list trace-table-as-list)
1096            (type index code-length)
1097            (type fasl-output fasl-output))
1098
1099   (let* ((2comp (component-info component))
1100          (constants (sb!c::ir2-component-constants 2comp))
1101          (header-length (length constants))
1102          (packed-trace-table (pack-trace-table trace-table-as-list))
1103          (total-length (+ code-length
1104                           (* (length packed-trace-table)
1105                              sb!c::tt-bytes-per-entry))))
1106
1107     (collect ((patches))
1108
1109       ;; Dump the offset of the trace table.
1110       (dump-object code-length fasl-output)
1111       ;; FIXME: As long as we don't have GENGC, the trace table is
1112       ;; hardwired to be empty. And SBCL doesn't have GENGC (and as
1113       ;; far as I know no modern CMU CL does either -- WHN
1114       ;; 2001-10-05). So might we be able to get rid of trace tables?
1115       ;;
1116       ;; Note that gencgc also does something with the trace table.
1117
1118       ;; Dump the constants, noting any :ENTRY constants that have to
1119       ;; be patched.
1120       (loop for i from sb!vm:code-constants-offset below header-length do
1121         (let ((entry (aref constants i)))
1122           (etypecase entry
1123             (constant
1124              (dump-object (sb!c::constant-value entry) fasl-output))
1125             (cons
1126              (ecase (car entry)
1127                (:entry
1128                 (let* ((info (sb!c::leaf-info (cdr entry)))
1129                        (handle (gethash info
1130                                         (fasl-output-entry-table
1131                                          fasl-output))))
1132                   (declare (type sb!c::entry-info info))
1133                   (cond
1134                    (handle
1135                     (dump-push handle fasl-output))
1136                    (t
1137                     (patches (cons info i))
1138                     (dump-fop 'fop-misc-trap fasl-output)))))
1139                (:load-time-value
1140                 (dump-push (cdr entry) fasl-output))
1141                (:fdefinition
1142                 (dump-object (cdr entry) fasl-output)
1143                 (dump-fop 'fop-fdefinition fasl-output))))
1144             (null
1145              (dump-fop 'fop-misc-trap fasl-output)))))
1146
1147       ;; Dump the debug info.
1148       (let ((info (sb!c::debug-info-for-component component))
1149             (*dump-only-valid-structures* nil))
1150         (dump-object info fasl-output)
1151         (let ((info-handle (dump-pop fasl-output)))
1152           (dump-push info-handle fasl-output)
1153           (push info-handle (fasl-output-debug-info fasl-output))))
1154
1155       (let ((num-consts (- header-length sb!vm:code-trace-table-offset-slot)))
1156         (cond ((and (< num-consts #x100) (< total-length #x10000))
1157                (dump-fop 'fop-small-code fasl-output)
1158                (dump-byte num-consts fasl-output)
1159                (dump-integer-as-n-bytes total-length (/ sb!vm:n-word-bytes 2) fasl-output))
1160               (t
1161                (dump-fop 'fop-code fasl-output)
1162                (dump-word num-consts fasl-output)
1163                (dump-word total-length fasl-output))))
1164
1165       ;; These two dumps are only ones which contribute to our
1166       ;; TOTAL-LENGTH value.
1167       (dump-segment code-segment code-length fasl-output)
1168       (dump-i-vector packed-trace-table fasl-output :data-only t)
1169
1170       ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it
1171       ;; dumps aren't included in the TOTAL-LENGTH passed to our
1172       ;; FOP-CODE/FOP-SMALL-CODE fop.
1173       (dump-fixups fixups fasl-output)
1174
1175       (dump-fop 'fop-sanctify-for-execution fasl-output)
1176
1177       (let ((handle (dump-pop fasl-output)))
1178         (dolist (patch (patches))
1179           (push (cons handle (cdr patch))
1180                 (gethash (car patch)
1181                          (fasl-output-patch-table fasl-output))))
1182         handle))))
1183
1184 (defun dump-assembler-routines (code-segment length fixups routines file)
1185   (dump-fop 'fop-assembler-code file)
1186   (dump-word length file)
1187   (write-segment-contents code-segment (fasl-output-stream file))
1188   (dolist (routine routines)
1189     (dump-fop 'fop-normal-load file)
1190     (let ((*cold-load-dump* t))
1191       (dump-object (car routine) file))
1192     (dump-fop 'fop-maybe-cold-load file)
1193     (dump-fop 'fop-assembler-routine file)
1194     (dump-word (label-position (cdr routine)) file))
1195   (dump-fixups fixups file)
1196   (dump-fop 'fop-sanctify-for-execution file)
1197   (dump-pop file))
1198
1199 ;;; Dump a function entry data structure corresponding to ENTRY to
1200 ;;; FILE. CODE-HANDLE is the table offset of the code object for the
1201 ;;; component.
1202 (defun dump-one-entry (entry code-handle file)
1203   (declare (type sb!c::entry-info entry) (type index code-handle)
1204            (type fasl-output file))
1205   (let ((name (sb!c::entry-info-name entry)))
1206     (dump-push code-handle file)
1207     (dump-object name file)
1208     (dump-object (sb!c::entry-info-arguments entry) file)
1209     (dump-object (sb!c::entry-info-type entry) file)
1210     (dump-fop 'fop-fun-entry file)
1211     (dump-word (label-position (sb!c::entry-info-offset entry)) file)
1212     (dump-pop file)))
1213
1214 ;;; Alter the code object referenced by CODE-HANDLE at the specified
1215 ;;; OFFSET, storing the object referenced by ENTRY-HANDLE.
1216 (defun dump-alter-code-object (code-handle offset entry-handle file)
1217   (declare (type index code-handle entry-handle offset))
1218   (declare (type fasl-output file))
1219   (dump-push code-handle file)
1220   (dump-push entry-handle file)
1221   (dump-fop* offset fop-byte-alter-code fop-alter-code file)
1222   (values))
1223
1224 ;;; Dump the code, constants, etc. for component. We pass in the
1225 ;;; assembler fixups, code vector and node info.
1226 (defun fasl-dump-component (component
1227                             code-segment
1228                             code-length
1229                             trace-table
1230                             fixups
1231                             file)
1232   (declare (type component component) (list trace-table))
1233   (declare (type fasl-output file))
1234
1235   (dump-fop 'fop-verify-empty-stack file)
1236   (dump-fop 'fop-verify-table-size file)
1237   (dump-word (fasl-output-table-free file) file)
1238
1239   #!+sb-dyncount
1240   (let ((info (sb!c::ir2-component-dyncount-info (component-info component))))
1241     (when info
1242       (fasl-validate-structure info file)))
1243
1244   (let ((code-handle (dump-code-object component
1245                                        code-segment
1246                                        code-length
1247                                        trace-table
1248                                        fixups
1249                                        file))
1250         (2comp (component-info component)))
1251     (dump-fop 'fop-verify-empty-stack file)
1252
1253     (dolist (entry (sb!c::ir2-component-entries 2comp))
1254       (let ((entry-handle (dump-one-entry entry code-handle file)))
1255         (setf (gethash entry (fasl-output-entry-table file)) entry-handle)
1256         (let ((old (gethash entry (fasl-output-patch-table file))))
1257           (when old
1258             (dolist (patch old)
1259               (dump-alter-code-object (car patch)
1260                                       (cdr patch)
1261                                       entry-handle
1262                                       file))
1263             (remhash entry (fasl-output-patch-table file)))))))
1264   (values))
1265
1266 (defun dump-push-previously-dumped-fun (fun fasl-output)
1267   (declare (type sb!c::clambda fun))
1268   (let ((handle (gethash (sb!c::leaf-info fun)
1269                          (fasl-output-entry-table fasl-output))))
1270     (aver handle)
1271     (dump-push handle fasl-output))
1272   (values))
1273
1274 ;;; Dump a FOP-FUNCALL to call an already-dumped top level lambda at
1275 ;;; load time.
1276 (defun fasl-dump-toplevel-lambda-call (fun fasl-output)
1277   (declare (type sb!c::clambda fun))
1278   (dump-push-previously-dumped-fun fun fasl-output)
1279   (dump-fop 'fop-funcall-for-effect fasl-output)
1280   (dump-byte 0 fasl-output)
1281   (values))
1282
1283 ;;; Dump a FOP-FSET to arrange static linkage (at cold init) between
1284 ;;; FUN-NAME and the already-dumped function whose dump handle is
1285 ;;; FUN-DUMP-HANDLE.
1286 #+sb-xc-host
1287 (defun fasl-dump-cold-fset (fun-name fun-dump-handle fasl-output)
1288   (declare (type fixnum fun-dump-handle))
1289   (aver (legal-fun-name-p fun-name))
1290   (dump-non-immediate-object fun-name fasl-output)
1291   (dump-push fun-dump-handle fasl-output)
1292   (dump-fop 'fop-fset fasl-output)
1293   (values))
1294     
1295 ;;; Compute the correct list of DEBUG-SOURCE structures and backpatch
1296 ;;; all of the dumped DEBUG-INFO structures. We clear the
1297 ;;; FASL-OUTPUT-DEBUG-INFO, so that subsequent components with
1298 ;;; different source info may be dumped.
1299 (defun fasl-dump-source-info (info fasl-output)
1300   (declare (type sb!c::source-info info))
1301   (let ((res (sb!c::debug-source-for-info info))
1302         (*dump-only-valid-structures* nil))
1303     (dump-object res fasl-output)
1304     (let ((res-handle (dump-pop fasl-output)))
1305       (dolist (info-handle (fasl-output-debug-info fasl-output))
1306         (dump-push res-handle fasl-output)
1307         (dump-fop 'fop-structset fasl-output)
1308         (dump-word info-handle fasl-output)
1309         ;; FIXME: what is this bare `2'?  --njf, 2004-08-16
1310         (dump-word 2 fasl-output))))
1311   (setf (fasl-output-debug-info fasl-output) nil)
1312   (values))
1313 \f
1314 ;;;; dumping structures
1315
1316 (defun dump-structure (struct file)
1317   (when *dump-only-valid-structures*
1318     (unless (gethash struct (fasl-output-valid-structures file))
1319       (error "attempt to dump invalid structure:~%  ~S~%How did this happen?"
1320              struct)))
1321   (note-potential-circularity struct file)
1322   (do ((index 0 (1+ index))
1323        (length (%instance-length struct))
1324        (circ (fasl-output-circularity-table file)))
1325       ((= index length)
1326        (dump-fop* length fop-small-struct fop-struct file))
1327     (let* ((obj (%instance-ref struct index))
1328            (ref (gethash obj circ)))
1329       (cond (ref
1330              (push (make-circularity :type :struct-set
1331                                      :object struct
1332                                      :index index
1333                                      :value obj
1334                                      :enclosing-object ref)
1335                    *circularities-detected*)
1336              (sub-dump-object nil file))
1337             (t
1338              (sub-dump-object obj file))))))
1339
1340 (defun dump-layout (obj file)
1341   (when (layout-invalid obj)
1342     (compiler-error "attempt to dump reference to obsolete class: ~S"
1343                     (layout-classoid obj)))
1344   (let ((name (classoid-name (layout-classoid obj))))
1345     (unless name
1346       (compiler-error "dumping anonymous layout: ~S" obj))
1347     (dump-fop 'fop-normal-load file)
1348     (let ((*cold-load-dump* t))
1349       (dump-object name file))
1350     (dump-fop 'fop-maybe-cold-load file))
1351   (sub-dump-object (layout-inherits obj) file)
1352   (sub-dump-object (layout-depthoid obj) file)
1353   (sub-dump-object (layout-length obj) file)
1354   (dump-fop 'fop-layout file))