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