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