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