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