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