1 ;;;; stuff that knows about dumping FASL files
3 ;;;; This software is part of the SBCL system. See the README file for
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.
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
18 ;;;; fasl dumper state
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))
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
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.)
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))
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
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.
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.
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*)
94 ;;; used to turn off the structure validation during dumping of source
96 (defvar *dump-only-valid-structures* t)
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)))
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))))
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)))
118 (write-byte (ldb (byte 8 (* 8 i)) num) stream))))
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))
130 (declare (type index i))
131 (dump-byte (logand n #xff) fasl-output))
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*))
142 ;;; Dump the FOP code for the named FOP to the specified FASL-OUTPUT.
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.)
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)))
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))
160 (dump-byte ',val ,file))
161 (error "compiler bug: ~S is not a legal fasload operator." fs))))
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.
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)
172 (dump-fop ',byte-fop ,n-file)
173 (dump-byte ,n-n ,n-file))
175 (dump-fop ',word-fop ,n-file)
176 (dump-word ,n-n ,n-file)))))
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)
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)
188 (fasl-output-table-free fasl-output)
189 (dump-fop 'fop-pop fasl-output)
190 (incf (fasl-output-table-free fasl-output))))
192 ;;; If X is in File's EQUAL-TABLE, then push the object and return T,
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))))
198 (handle (dump-push handle fasl-output) t)
200 (defun string-check-table (x fasl-output)
201 (declare (type fasl-output fasl-output)
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))))))
208 (handle (dump-push handle fasl-output) t)
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))
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))
227 (defun string-save-object (x fasl-output)
228 (declare (type fasl-output fasl-output)
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)
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))
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.
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))
251 ;;;; opening and closing fasl files
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
261 (loop for char across string
262 do (let ((code (char-code char)))
263 (unless (<= 0 code 127)
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
269 (write-sequence (string-to-octets string :external-format :utf-8)
271 (let* ((stream (open name
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*
280 (format nil "#!~A --script~%"
281 (native-namestring *runtime-pathname* :as-file t))
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+.
290 (with-standard-io-syntax
291 (let ((*print-readably* nil)
292 (*print-pretty* nil))
296 using ~A version ~A~%"
298 (sb!xc:lisp-implementation-type)
299 (sb!xc:lisp-implementation-version))))
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*))
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))
325 (aver (zerop (hash-table-count (fasl-output-patch-table fasl-output))))
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)
331 (dump-fop 'fop-end-group fasl-output))
333 ;; That's all, folks.
334 (close (fasl-output-stream fasl-output) :abort abort-p)
337 ;;;; main entries to object dumping
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.
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))))
350 (dump-push index file))
353 (symbol (dump-symbol x file))
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.
367 (if (maybe-cyclic-p x)
370 (eq-save-object x file))
371 (unless (equal-check-table x file)
373 (equal-save-object x file))))
376 (eq-save-object x file))
378 (dump-structure x file)
379 (eq-save-object x file))
381 ;; DUMP-ARRAY (and its callees) are responsible for
382 ;; updating the EQ and EQUAL hash tables.
385 (unless (equal-check-table x file)
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)))
394 (unless (equal-check-table x file)
395 (dump-fop 'fop-simd-pack file)
396 (dump-integer-as-n-bytes (%simd-pack-tag x) 8 file)
397 (dump-integer-as-n-bytes (%simd-pack-low x) 8 file)
398 (dump-integer-as-n-bytes (%simd-pack-high x) 8 file))
399 (equal-save-object x file))
401 ;; This probably never happens, since bad things tend to
402 ;; be detected during IR1 conversion.
403 (error "This object cannot be dumped into a fasl file:~% ~S"
407 ;;; Dump an object of any type by dispatching to the correct
408 ;;; type-specific dumping function. We pick off immediate objects,
409 ;;; symbols and magic lists here. Other objects are handled by
410 ;;; DUMP-NON-IMMEDIATE-OBJECT.
412 ;;; This is the function used for recursive calls to the fasl dumper.
413 ;;; We don't worry about creating circularities here, since it is
414 ;;; assumed that there is a top level call to DUMP-OBJECT.
415 (defun sub-dump-object (x file)
418 (dump-non-immediate-object x file)
419 (dump-fop 'fop-empty-list file)))
422 (dump-fop 'fop-truth file)
423 (dump-non-immediate-object x file)))
424 ((fixnump x) (dump-integer x file))
425 ((characterp x) (dump-character x file))
427 (dump-non-immediate-object x file))))
429 ;;; Dump stuff to backpatch already dumped objects. INFOS is the list
430 ;;; of CIRCULARITY structures describing what to do. The patching FOPs
431 ;;; take the value to store on the stack. We compute this value by
432 ;;; fetching the enclosing object from the table, and then CDR'ing it
434 (defun dump-circularities (infos file)
435 (let ((table (fasl-output-eq-table file)))
438 (let* ((value (circularity-value info))
439 (enclosing (circularity-enclosing-object info)))
440 (dump-push (gethash enclosing table) file)
441 (unless (eq enclosing value)
442 (do ((current enclosing (cdr current))
445 (dump-fop 'fop-nthcdr file)
447 (declare (type index i)))))
449 (ecase (circularity-type info)
450 (:rplaca (dump-fop 'fop-rplaca file))
451 (:rplacd (dump-fop 'fop-rplacd file))
452 (:svset (dump-fop 'fop-svset file))
453 (:struct-set (dump-fop 'fop-structset file)))
454 (dump-word (gethash (circularity-object info) table) file)
455 (dump-word (circularity-index info) file))))
457 ;;; Set up stuff for circularity detection, then dump an object. All
458 ;;; shared and circular structure will be exactly preserved within a
459 ;;; single call to DUMP-OBJECT. Sharing between objects dumped by
460 ;;; separate calls is only preserved when convenient.
462 ;;; We peek at the object type so that we only pay the circular
463 ;;; detection overhead on types of objects that might be circular.
464 (defun dump-object (x file)
465 (if (compound-object-p x)
466 (let ((*circularities-detected* ())
467 (circ (fasl-output-circularity-table file)))
469 (sub-dump-object x file)
470 (when *circularities-detected*
471 (dump-circularities *circularities-detected* file)
473 (sub-dump-object x file)))
475 ;;;; LOAD-TIME-VALUE and MAKE-LOAD-FORM support
477 ;;; Emit a funcall of the function and return the handle for the
479 (defun fasl-dump-load-time-value-lambda (fun file)
480 (declare (type sb!c::clambda fun) (type fasl-output file))
481 (let ((handle (gethash (sb!c::leaf-info fun)
482 (fasl-output-entry-table file))))
484 (dump-push handle file)
485 (dump-fop 'fop-funcall file)
489 ;;; Return T iff CONSTANT has already been dumped. It's been dumped if
490 ;;; it's in the EQ table.
492 ;;; Note: historically (1) the above comment was "T iff ... has not been dumped",
493 ;;; (2) the test was was also true if the constant had been validated / was in
494 ;;; the valid objects table. This led to substructures occasionally skipping the
495 ;;; validation, and hence failing the "must have been validated" test.
496 (defun fasl-constant-already-dumped-p (constant file)
497 (and (gethash constant (fasl-output-eq-table file)) t))
499 ;;; Use HANDLE whenever we try to dump CONSTANT. HANDLE should have been
500 ;;; returned earlier by FASL-DUMP-LOAD-TIME-VALUE-LAMBDA.
501 (defun fasl-note-handle-for-constant (constant handle file)
502 (let ((table (fasl-output-eq-table file)))
503 (when (gethash constant table)
504 (error "~S already dumped?" constant))
505 (setf (gethash constant table) handle))
508 ;;; Note that the specified structure can just be dumped by
509 ;;; enumerating the slots.
510 (defun fasl-validate-structure (structure file)
511 (setf (gethash structure (fasl-output-valid-structures file)) t)
516 (defun dump-ratio (x file)
517 (sub-dump-object (numerator x) file)
518 (sub-dump-object (denominator x) file)
519 (dump-fop 'fop-ratio file))
521 (defun dump-integer (n file)
524 (dump-fop 'fop-byte-integer file)
525 (dump-byte (logand #xFF n) file))
526 ((unsigned-byte #.(1- sb!vm:n-word-bits))
527 (dump-fop 'fop-word-integer file)
529 ((signed-byte #.sb!vm:n-word-bits)
530 (dump-fop 'fop-word-integer file)
531 (dump-integer-as-n-bytes n #.sb!vm:n-word-bytes file))
533 (let ((bytes (ceiling (1+ (integer-length n)) 8)))
534 (dump-fop* bytes fop-small-integer fop-integer file)
535 (dump-integer-as-n-bytes n bytes file)))))
537 (defun dump-float (x file)
540 (dump-fop 'fop-single-float file)
541 (dump-integer-as-n-bytes (single-float-bits x) 4 file))
543 (dump-fop 'fop-double-float file)
545 (declare (double-float x))
546 (dump-integer-as-n-bytes (double-float-low-bits x) 4 file)
547 (dump-integer-as-n-bytes (double-float-high-bits x) 4 file)))
550 (dump-fop 'fop-long-float file)
551 (dump-long-float x file))))
553 (defun dump-complex-single-float (re im file)
554 (declare (single-float re im))
555 (dump-fop 'fop-complex-single-float file)
556 (dump-integer-as-n-bytes (single-float-bits re) 4 file)
557 (dump-integer-as-n-bytes (single-float-bits im) 4 file))
559 (defun dump-complex-double-float (re im file)
560 (declare (double-float re im))
561 (dump-fop 'fop-complex-double-float file)
562 (dump-integer-as-n-bytes (double-float-low-bits re) 4 file)
563 (dump-integer-as-n-bytes (double-float-high-bits re) 4 file)
564 (dump-integer-as-n-bytes (double-float-low-bits im) 4 file)
565 (dump-integer-as-n-bytes (double-float-high-bits im) 4 file))
567 (defun dump-complex-rational (re im file)
568 (sub-dump-object re file)
569 (sub-dump-object im file)
570 (dump-fop 'fop-complex file))
573 (defun dump-complex (x file)
574 (let ((re (realpart x))
576 (cond ((and (typep re 'single-float)
577 (typep im 'single-float))
578 (dump-complex-single-float re im file))
579 ((and (typep re 'double-float)
580 (typep im 'double-float))
581 (dump-complex-double-float re im file))
582 ((and (typep re 'rational)
583 (typep im 'rational))
584 (dump-complex-rational re im file))
586 (bug "Complex number too complex: ~S" x)))))
589 (defun dump-complex (x file)
591 ((complex single-float)
592 (dump-complex-single-float (realpart x) (imagpart x) file))
593 ((complex double-float)
594 (dump-complex-double-float (realpart x) (imagpart x) file))
596 ((complex long-float)
597 (dump-fop 'fop-complex-long-float file)
598 (dump-long-float (realpart x) file)
599 (dump-long-float (imagpart x) file))
601 (dump-complex-rational (realpart x) (imagpart x) file))))
605 ;;; Return the table index of PKG, adding the package to the table if
606 ;;; necessary. During cold load, we read the string as a normal string
607 ;;; so that we can do the package lookup at cold load time.
609 ;;; FIXME: Despite the parallelism in names, the functionality of
610 ;;; this function is not parallel to other functions DUMP-FOO, e.g.
611 ;;; DUMP-SYMBOL and DUMP-LIST. The mapping between names and behavior
612 ;;; should be made more consistent.
613 (declaim (ftype (function (package fasl-output) index) dump-package))
614 (defun dump-package (pkg file)
615 (declare (inline assoc))
616 (cond ((cdr (assoc pkg (fasl-output-packages file) :test #'eq)))
618 (let ((s (package-name pkg)))
619 (dump-fop* (length s) fop-small-named-package-save fop-named-package-save file)
621 (dump-base-chars-of-string (coerce s 'simple-base-string) file)
623 (#!+sb-unicode dump-characters-of-string
624 #!-sb-unicode dump-base-chars-of-string
625 (coerce s '(simple-array character (*))) file))
626 (let ((entry (fasl-output-table-free file)))
627 (incf (fasl-output-table-free file))
628 (push (cons pkg entry) (fasl-output-packages file))
633 ;;; Dump a list, setting up patching information when there are
634 ;;; circularities. We scan down the list, checking for CDR and CAR
637 ;;; If there is a CDR circularity, we terminate the list with NIL and
638 ;;; make a CIRCULARITY notation for the CDR of the previous cons.
640 ;;; If there is no CDR circularity, then we mark the current cons and
641 ;;; check for a CAR circularity. When there is a CAR circularity, we
642 ;;; make the CAR NIL initially, arranging for the current cons to be
645 ;;; Otherwise, we recursively call the dumper to dump the current
647 (defun dump-list (list file)
649 (not (gethash list (fasl-output-circularity-table file)))))
650 (do* ((l list (cdr l))
652 (circ (fasl-output-circularity-table file)))
655 (terminate-undotted-list n file))
657 (sub-dump-object l file)
658 (terminate-dotted-list n file))))
659 (declare (type index n))
660 (let ((ref (gethash l circ)))
662 (push (make-circularity :type :rplacd
666 :enclosing-object ref)
667 *circularities-detected*)
668 (terminate-undotted-list n file)
671 (setf (gethash l circ) list)
674 (ref (gethash obj circ)))
676 (push (make-circularity :type :rplaca
680 :enclosing-object ref)
681 *circularities-detected*)
682 (sub-dump-object nil file))
684 (sub-dump-object obj file))))))
686 (defun terminate-dotted-list (n file)
687 (declare (type index n) (type fasl-output file))
689 (1 (dump-fop 'fop-list*-1 file))
690 (2 (dump-fop 'fop-list*-2 file))
691 (3 (dump-fop 'fop-list*-3 file))
692 (4 (dump-fop 'fop-list*-4 file))
693 (5 (dump-fop 'fop-list*-5 file))
694 (6 (dump-fop 'fop-list*-6 file))
695 (7 (dump-fop 'fop-list*-7 file))
696 (8 (dump-fop 'fop-list*-8 file))
697 (t (do ((nn n (- nn 255)))
699 (dump-fop 'fop-list* file)
701 (declare (type index nn))
702 (dump-fop 'fop-list* file)
703 (dump-byte 255 file)))))
705 ;;; If N > 255, must build list with one LIST operator, then LIST*
708 (defun terminate-undotted-list (n file)
709 (declare (type index n) (type fasl-output file))
711 (1 (dump-fop 'fop-list-1 file))
712 (2 (dump-fop 'fop-list-2 file))
713 (3 (dump-fop 'fop-list-3 file))
714 (4 (dump-fop 'fop-list-4 file))
715 (5 (dump-fop 'fop-list-5 file))
716 (6 (dump-fop 'fop-list-6 file))
717 (7 (dump-fop 'fop-list-7 file))
718 (8 (dump-fop 'fop-list-8 file))
720 (dump-fop 'fop-list file)
722 (t (dump-fop 'fop-list file)
724 (do ((nn (- n 255) (- nn 255)))
726 (dump-fop 'fop-list* file)
728 (declare (type index nn))
729 (dump-fop 'fop-list* file)
730 (dump-byte 255 file)))))))
734 ;;; Dump the array thing.
735 (defun dump-array (x file)
738 (dump-multi-dim-array x file)))
740 ;;; Dump the vector object. If it's not simple, then actually dump a
741 ;;; simple version of it. But we enter the original in the EQ or EQUAL
743 (defun dump-vector (x file)
744 (let ((simple-version (if (array-header-p x)
745 (coerce x `(simple-array
746 ,(array-element-type x)
749 (typecase simple-version
752 (unless (string-check-table x file)
753 (dump-simple-base-string simple-version file)
754 (string-save-object x file)))
757 (unless (string-check-table x file)
758 (dump-simple-base-string simple-version file)
759 (string-save-object x file)))
761 ((simple-array character (*))
763 (unless (string-check-table x file)
764 (dump-simple-character-string simple-version file)
765 (string-save-object x file))
767 (bug "how did we get here?"))
769 (dump-simple-vector simple-version file)
770 (eq-save-object x file))
772 (dump-specialized-vector simple-version file)
773 (eq-save-object x file)))))
775 ;;; Dump a SIMPLE-VECTOR, handling any circularities.
776 (defun dump-simple-vector (v file)
777 (declare (type simple-vector v) (type fasl-output file))
778 (note-potential-circularity v file)
779 (do ((index 0 (1+ index))
781 (circ (fasl-output-circularity-table file)))
783 (dump-fop* length fop-small-vector fop-vector file))
784 (let* ((obj (aref v index))
785 (ref (gethash obj circ)))
787 (push (make-circularity :type :svset
791 :enclosing-object ref)
792 *circularities-detected*)
793 (sub-dump-object nil file))
795 (sub-dump-object obj file))))))
797 ;;; In the grand scheme of things I don't pretend to understand any
798 ;;; more how this works, or indeed whether. But to write out specialized
799 ;;; vectors in the same format as fop-spec-vector expects to read them
800 ;;; we need to be target-endian. dump-integer-as-n-bytes always writes
801 ;;; little-endian (which is correct for all other integers) so for a bigendian
802 ;;; target we need to swap octets -- CSR, after DB
804 (defun dump-specialized-vector (vector file &key data-only)
805 (labels ((octet-swap (word bits)
806 "BITS must be a multiple of 8"
807 (do ((input word (ash input -8))
808 (output 0 (logior (ash output 8) (logand input #xff)))
809 (bits bits (- bits 8)))
810 ((<= bits 0) output)))
811 (dump-unsigned-vector (widetag bytes bits)
813 (dump-fop 'fop-spec-vector file)
814 (dump-word (length vector) file)
815 (dump-byte widetag file))
817 (dump-integer-as-n-bytes
818 (ecase sb!c:*backend-byte-order*
820 (:big-endian (octet-swap i bits)))
823 ((simple-array (unsigned-byte 8) (*))
824 (dump-unsigned-vector sb!vm:simple-array-unsigned-byte-8-widetag 1 8))
825 ((simple-array (unsigned-byte 16) (*))
826 (dump-unsigned-vector sb!vm:simple-array-unsigned-byte-16-widetag 2 16))
827 ((simple-array (unsigned-byte 32) (*))
828 (dump-unsigned-vector sb!vm:simple-array-unsigned-byte-32-widetag 4 32)))))
831 (defun dump-specialized-vector (vector file &key data-only)
832 (declare (type (simple-array * (*)) vector))
833 (let* ((length (length vector))
834 (widetag (widetag-of vector))
835 (bits-per-length (aref **saetp-bits-per-length** widetag)))
836 (aver (< bits-per-length 255))
838 (dump-fop 'fop-spec-vector file)
839 (dump-word length file)
840 (dump-byte widetag file))
841 (dump-raw-bytes vector
842 (ceiling (* length bits-per-length) sb!vm:n-byte-bits)
845 ;;; Dump characters and string-ish things.
847 (defun dump-character (char file)
848 (let ((code (sb!xc:char-code char)))
851 (dump-fop 'fop-short-character file)
852 (dump-byte code file))
854 (dump-fop 'fop-character file)
855 (dump-word code file)))))
857 (defun dump-base-chars-of-string (s fasl-output)
858 (declare #+sb-xc-host (type simple-string s)
859 #-sb-xc-host (type simple-base-string s)
860 (type fasl-output fasl-output))
862 (dump-byte (sb!xc:char-code c) fasl-output))
866 ;;; Dump a SIMPLE-BASE-STRING.
867 (defun dump-simple-base-string (s file)
868 #+sb-xc-host (declare (type simple-string s))
869 #-sb-xc-host (declare (type simple-base-string s))
870 (dump-fop* (length s) fop-small-base-string fop-base-string file)
871 (dump-base-chars-of-string s file)
874 ;;; If we get here, it is assumed that the symbol isn't in the table,
875 ;;; but we are responsible for putting it there when appropriate.
876 (defun dump-symbol (s file)
877 (declare (type fasl-output file))
878 (let* ((pname (symbol-name s))
879 (pname-length (length pname))
880 (pkg (symbol-package s)))
881 ;; see comment in genesis: we need this here for repeatable fasls
883 (multiple-value-bind (cl-symbol cl-status)
884 (find-symbol (symbol-name s) sb!int:*cl-package*)
885 (when (and (eq s cl-symbol)
886 (eq cl-status :external))
887 ;; special case, to work around possible xc host "design
888 ;; choice" weirdness in COMMON-LISP package
889 (setq pkg sb!int:*cl-package*)))
892 (dump-fop* pname-length
893 fop-uninterned-small-symbol-save
894 fop-uninterned-symbol-save
896 ;; CMU CL had FOP-SYMBOL-SAVE/FOP-SMALL-SYMBOL-SAVE fops which
897 ;; used the current value of *PACKAGE*. Unfortunately that's
898 ;; broken w.r.t. ANSI Common Lisp semantics, so those are gone
900 ;;((eq pkg *package*)
901 ;; (dump-fop* pname-length
902 ;; fop-small-symbol-save
903 ;; fop-symbol-save file))
904 ((eq pkg sb!int:*cl-package*)
905 (dump-fop* pname-length
906 fop-lisp-small-symbol-save
909 ((eq pkg sb!int:*keyword-package*)
910 (dump-fop* pname-length
911 fop-keyword-small-symbol-save
912 fop-keyword-symbol-save
914 ((< pname-length 256)
915 (dump-fop* (dump-package pkg file)
916 fop-small-symbol-in-byte-package-save
917 fop-small-symbol-in-package-save
919 (dump-byte pname-length file))
921 (dump-fop* (dump-package pkg file)
922 fop-symbol-in-byte-package-save
923 fop-symbol-in-package-save
925 (dump-word pname-length file)))
927 #+sb-xc-host (dump-base-chars-of-string pname file)
928 #-sb-xc-host (#!+sb-unicode dump-characters-of-string
929 #!-sb-unicode dump-base-chars-of-string
932 (setf (gethash s (fasl-output-eq-table file))
933 (fasl-output-table-free file))
935 (incf (fasl-output-table-free file)))
939 ;;;; component (function) dumping
941 (defun dump-segment (segment code-length fasl-output)
942 (declare (type sb!assem:segment segment)
943 (type fasl-output fasl-output))
944 (let* ((stream (fasl-output-stream fasl-output))
945 (n-written (write-segment-contents segment stream)))
946 ;; In CMU CL there was no enforced connection between the CODE-LENGTH
947 ;; argument and the number of bytes actually written. I added this
948 ;; assertion while trying to debug portable genesis. -- WHN 19990902
949 (unless (= code-length n-written)
950 (bug "code-length=~W, n-written=~W" code-length n-written)))
953 ;;; Dump all the fixups. Currently there are three flavors of fixup:
954 ;;; - assembly routines: named by a symbol
955 ;;; - foreign (C) symbols: named by a string
956 ;;; - code object references: don't need a name.
957 (defun dump-fixups (fixups fasl-output)
958 (declare (list fixups) (type fasl-output fasl-output))
959 (dolist (note fixups)
960 (let* ((kind (fixup-note-kind note))
961 (fixup (fixup-note-fixup note))
962 (position (fixup-note-position note))
963 (name (fixup-name fixup))
964 (flavor (fixup-flavor fixup)))
965 (dump-object kind fasl-output)
966 ;; Depending on the flavor, we may have various kinds of
967 ;; noise before the position.
970 (aver (symbolp name))
971 (dump-object name fasl-output)
972 (dump-fop 'fop-assembler-fixup fasl-output))
973 ((:foreign :foreign-dataref)
974 (aver (stringp name))
977 (dump-fop 'fop-foreign-fixup fasl-output))
980 (dump-fop 'fop-foreign-dataref-fixup fasl-output)))
981 (let ((len (length name)))
982 (aver (< len 256)) ; (limit imposed by fop definition)
983 (dump-byte len fasl-output)
985 (dump-byte (char-code (schar name i)) fasl-output))))
988 (dump-fop 'fop-code-object-fixup fasl-output)))
989 ;; No matter what the flavor, we'll always dump the position
990 (dump-word position fasl-output)))
993 ;;; Dump out the constant pool and code-vector for component, push the
994 ;;; result in the table, and return the offset.
996 ;;; The only tricky thing is handling constant-pool references to
997 ;;; functions. If we have already dumped the function, then we just
998 ;;; push the code pointer. Otherwise, we must create back-patching
999 ;;; information so that the constant will be set when the function is
1000 ;;; eventually dumped. This is a bit awkward, since we don't have the
1001 ;;; handle for the code object being dumped while we are dumping its
1004 ;;; We dump trap objects in any unused slots or forward referenced slots.
1005 (defun dump-code-object (component
1012 (declare (type component component)
1013 (list trace-table-as-list)
1014 (type index code-length)
1015 (type fasl-output fasl-output))
1017 (let* ((2comp (component-info component))
1018 (constants (sb!c::ir2-component-constants 2comp))
1019 (header-length (length constants))
1020 (packed-trace-table (pack-trace-table trace-table-as-list))
1021 (total-length (+ code-length
1022 (* (length packed-trace-table)
1023 sb!c::tt-bytes-per-entry))))
1025 (collect ((patches))
1027 ;; Dump the offset of the trace table.
1028 (dump-object code-length fasl-output)
1029 ;; FIXME: As long as we don't have GENGC, the trace table is
1030 ;; hardwired to be empty. And SBCL doesn't have GENGC (and as
1031 ;; far as I know no modern CMU CL does either -- WHN
1032 ;; 2001-10-05). So might we be able to get rid of trace tables?
1034 ;; Note that gencgc also does something with the trace table.
1036 ;; Dump the constants, noting any :ENTRY constants that have to
1038 (loop for i from sb!vm:code-constants-offset below header-length do
1039 (let ((entry (aref constants i)))
1042 (dump-object (sb!c::constant-value entry) fasl-output))
1046 (let* ((info (sb!c::leaf-info (cdr entry)))
1047 (handle (gethash info
1048 (fasl-output-entry-table
1050 (declare (type sb!c::entry-info info))
1053 (dump-push handle fasl-output))
1055 (patches (cons info i))
1056 (dump-fop 'fop-misc-trap fasl-output)))))
1058 (dump-push (cdr entry) fasl-output))
1060 (dump-object (cdr entry) fasl-output)
1061 (dump-fop 'fop-fdefinition fasl-output))
1063 (dump-object (cdr entry) fasl-output)
1064 (dump-fop 'fop-known-fun fasl-output))))
1066 (dump-fop 'fop-misc-trap fasl-output)))))
1068 ;; Dump the debug info.
1069 (let ((info (sb!c::debug-info-for-component component))
1070 (*dump-only-valid-structures* nil))
1071 (dump-object info fasl-output)
1072 (let ((info-handle (dump-pop fasl-output)))
1073 (dump-push info-handle fasl-output)
1074 (push info-handle (fasl-output-debug-info fasl-output))))
1076 (let ((num-consts (- header-length sb!vm:code-trace-table-offset-slot)))
1077 (cond ((and (< num-consts #x100) (< total-length #x10000))
1078 (dump-fop 'fop-small-code fasl-output)
1079 (dump-byte num-consts fasl-output)
1080 (dump-integer-as-n-bytes total-length (/ sb!vm:n-word-bytes 2) fasl-output))
1082 (dump-fop 'fop-code fasl-output)
1083 (dump-word num-consts fasl-output)
1084 (dump-word total-length fasl-output))))
1086 ;; These two dumps are only ones which contribute to our
1087 ;; TOTAL-LENGTH value.
1088 (dump-segment code-segment code-length fasl-output)
1089 (dump-specialized-vector packed-trace-table fasl-output :data-only t)
1091 ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it
1092 ;; dumps aren't included in the TOTAL-LENGTH passed to our
1093 ;; FOP-CODE/FOP-SMALL-CODE fop.
1094 (dump-fixups fixups fasl-output)
1096 (dump-fop 'fop-sanctify-for-execution fasl-output)
1098 (let ((handle (dump-pop fasl-output)))
1099 (dolist (patch (patches))
1100 (push (cons handle (cdr patch))
1101 (gethash (car patch)
1102 (fasl-output-patch-table fasl-output))))
1105 (defun dump-assembler-routines (code-segment length fixups routines file)
1106 (dump-fop 'fop-assembler-code file)
1107 (dump-word length file)
1108 (write-segment-contents code-segment (fasl-output-stream file))
1109 (dolist (routine routines)
1110 (dump-object (car routine) file)
1111 (dump-fop 'fop-assembler-routine file)
1112 (dump-word (label-position (cdr routine)) file))
1113 (dump-fixups fixups file)
1114 (dump-fop 'fop-sanctify-for-execution file)
1117 ;;; Dump a function entry data structure corresponding to ENTRY to
1118 ;;; FILE. CODE-HANDLE is the table offset of the code object for the
1120 (defun dump-one-entry (entry code-handle file)
1121 (declare (type sb!c::entry-info entry) (type index code-handle)
1122 (type fasl-output file))
1123 (let ((name (sb!c::entry-info-name entry)))
1124 (dump-push code-handle file)
1125 (dump-object name file)
1126 (dump-object (sb!c::entry-info-arguments entry) file)
1127 (dump-object (sb!c::entry-info-type entry) file)
1128 (dump-object (sb!c::entry-info-info entry) file)
1129 (dump-fop 'fop-fun-entry file)
1130 (dump-word (label-position (sb!c::entry-info-offset entry)) file)
1133 ;;; Alter the code object referenced by CODE-HANDLE at the specified
1134 ;;; OFFSET, storing the object referenced by ENTRY-HANDLE.
1135 (defun dump-alter-code-object (code-handle offset entry-handle file)
1136 (declare (type index code-handle entry-handle offset))
1137 (declare (type fasl-output file))
1138 (dump-push code-handle file)
1139 (dump-push entry-handle file)
1140 (dump-fop* offset fop-byte-alter-code fop-alter-code file)
1143 ;;; Dump the code, constants, etc. for component. We pass in the
1144 ;;; assembler fixups, code vector and node info.
1145 (defun fasl-dump-component (component
1151 (declare (type component component) (list trace-table))
1152 (declare (type fasl-output file))
1154 (dump-fop 'fop-verify-table-size file)
1155 (dump-word (fasl-output-table-free file) file)
1158 (let ((info (sb!c::ir2-component-dyncount-info (component-info component))))
1160 (fasl-validate-structure info file)))
1162 (let ((code-handle (dump-code-object component
1168 (2comp (component-info component)))
1170 (dolist (entry (sb!c::ir2-component-entries 2comp))
1171 (let ((entry-handle (dump-one-entry entry code-handle file)))
1172 (setf (gethash entry (fasl-output-entry-table file)) entry-handle)
1173 (let ((old (gethash entry (fasl-output-patch-table file))))
1176 (dump-alter-code-object (car patch)
1180 (remhash entry (fasl-output-patch-table file)))))))
1183 (defun dump-push-previously-dumped-fun (fun fasl-output)
1184 (declare (type sb!c::clambda fun))
1185 (let ((handle (gethash (sb!c::leaf-info fun)
1186 (fasl-output-entry-table fasl-output))))
1188 (dump-push handle fasl-output))
1191 ;;; Dump a FOP-FUNCALL to call an already-dumped top level lambda at
1193 (defun fasl-dump-toplevel-lambda-call (fun fasl-output)
1194 (declare (type sb!c::clambda fun))
1195 (dump-push-previously-dumped-fun fun fasl-output)
1196 (dump-fop 'fop-funcall-for-effect fasl-output)
1197 (dump-byte 0 fasl-output)
1200 ;;; Dump a FOP-FSET to arrange static linkage (at cold init) between
1201 ;;; FUN-NAME and the already-dumped function whose dump handle is
1202 ;;; FUN-DUMP-HANDLE.
1204 (defun fasl-dump-cold-fset (fun-name fun-dump-handle fasl-output)
1205 (declare (type fixnum fun-dump-handle))
1206 (aver (legal-fun-name-p fun-name))
1207 (dump-non-immediate-object fun-name fasl-output)
1208 (dump-push fun-dump-handle fasl-output)
1209 (dump-fop 'fop-fset fasl-output)
1212 ;;; Compute the correct list of DEBUG-SOURCE structures and backpatch
1213 ;;; all of the dumped DEBUG-INFO structures. We clear the
1214 ;;; FASL-OUTPUT-DEBUG-INFO, so that subsequent components with
1215 ;;; different source info may be dumped.
1216 (defun fasl-dump-source-info (info fasl-output)
1217 (declare (type sb!c::source-info info))
1218 (let ((res (sb!c::debug-source-for-info info))
1219 (*dump-only-valid-structures* nil))
1220 #+sb-xc-host (setf (sb!c::debug-source-created res) 0
1221 (sb!c::debug-source-compiled res) 0)
1222 (dump-object res fasl-output)
1223 (let ((res-handle (dump-pop fasl-output)))
1224 (dolist (info-handle (fasl-output-debug-info fasl-output))
1225 (dump-push res-handle fasl-output)
1226 (dump-fop 'fop-structset fasl-output)
1227 (dump-word info-handle fasl-output)
1228 (dump-word sb!c::+debug-info-source-index+ fasl-output))
1231 (dump-push res-handle fasl-output)
1232 (dump-fop 'fop-note-debug-source fasl-output))))
1233 (setf (fasl-output-debug-info fasl-output) nil)
1236 ;;;; dumping structures
1238 (defun dump-structure (struct file)
1239 (when *dump-only-valid-structures*
1240 (unless (gethash struct (fasl-output-valid-structures file))
1241 (error "attempt to dump invalid structure:~% ~S~%How did this happen?"
1243 (note-potential-circularity struct file)
1244 (aver (%instance-ref struct 0))
1245 (do* ((length (%instance-length struct))
1246 (ntagged (- length (layout-n-untagged-slots (%instance-ref struct 0))))
1247 (circ (fasl-output-circularity-table file))
1248 ;; last slot first on the stack, so that the layout is on top:
1249 (index (1- length) (1- index)))
1251 (dump-fop* length fop-small-struct fop-struct file))
1252 (let* ((obj (if (>= index ntagged)
1253 (%raw-instance-ref/word struct (- length index 1))
1254 (%instance-ref struct index)))
1255 (ref (gethash obj circ)))
1257 (aver (not (zerop index)))
1258 (push (make-circularity :type :struct-set
1262 :enclosing-object ref)
1263 *circularities-detected*)
1264 (sub-dump-object nil file))
1266 (sub-dump-object obj file))))))
1268 (defun dump-layout (obj file)
1269 (when (layout-invalid obj)
1270 (compiler-error "attempt to dump reference to obsolete class: ~S"
1271 (layout-classoid obj)))
1272 (let ((name (classoid-name (layout-classoid obj))))
1274 (compiler-error "dumping anonymous layout: ~S" obj))
1275 (dump-object name file))
1276 (sub-dump-object (layout-inherits obj) file)
1277 (sub-dump-object (layout-depthoid obj) file)
1278 (sub-dump-object (layout-length obj) file)
1279 (sub-dump-object (layout-n-untagged-slots obj) file)
1280 (dump-fop 'fop-layout file))