Generalize immobile space addresses
[sbcl.git] / src / compiler / dump.lisp
blobc2a8666b44a454801c8e25e6c05ae514a25623e5
1 ;;;; stuff that knows about dumping FASL files
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.
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))
28 s))))
29 (:copier nil))
30 ;; the stream we dump to
31 (stream (missing-arg) :type stream)
32 ;; scratch space for computing varint encodings
33 ;; FIXME: can't use the theoretical max of 10 bytes
34 ;; due to constraint in WRITE-VAR-INTEGER.
35 (varint-buf (make-array 10 :element-type '(unsigned-byte 8) :fill-pointer t))
36 ;; hashtables we use to keep track of dumped constants so that we
37 ;; can get them from the table rather than dumping them again. The
38 ;; EQUAL-TABLE is used for lists and strings, and the EQ-TABLE is
39 ;; used for everything else. We use a separate EQ table to avoid
40 ;; performance pathologies with objects for which EQUAL degenerates
41 ;; to EQL. Everything entered in the EQUAL table is also entered in
42 ;; the EQ table.
43 (equal-table (make-hash-table :test 'equal) :type hash-table)
44 (eq-table (make-hash-table :test 'eq) :type hash-table)
45 ;; Hashtable mapping a string to a list of fop-table indices of
46 ;; symbols whose name is that string. For any name as compared
47 ;; by STRING= there can be a symbol whose name is a base string
48 ;; and/or a symbol whose name is not a base string.
49 (string=-table (make-hash-table :test 'equal) :type hash-table)
50 ;; the table's current free pointer: the next offset to be used
51 (table-free 0 :type index)
52 ;; an alist (PACKAGE . OFFSET) of the table offsets for each package
53 ;; we have currently located.
54 (packages () :type list)
55 ;; a table mapping from the ENTRY-INFO structures for dumped XEPs to
56 ;; the table offsets of the corresponding code pointers
57 (entry-table (make-hash-table :test 'eq) :type hash-table)
58 ;; a table holding back-patching info for forward references to XEPs.
59 ;; The key is the ENTRY-INFO structure for the XEP, and the value is
60 ;; a list of conses (<code-handle> . <offset>), where <code-handle>
61 ;; is the offset in the table of the code object needing to be
62 ;; patched, and <offset> is the offset that must be patched.
63 (patch-table (make-hash-table :test 'eq) :type hash-table)
64 ;; This is used to keep track of objects that we are in the process
65 ;; of dumping so that circularities can be preserved. The key is the
66 ;; object that we have previously seen, and the value is the object
67 ;; that we reference in the table to find this previously seen
68 ;; object. (The value is never NIL.)
70 ;; Except with list objects, the key and the value are always the
71 ;; same. In a list, the key will be some tail of the value.
72 (circularity-table (make-hash-table :test 'eq) :type hash-table)
73 ;; a hash table of structures that are allowed to be dumped. If we
74 ;; try to dump a structure that isn't in this hash table, we lose.
75 (valid-structures (make-hash-table :test 'eq) :type hash-table)
76 ;; DEBUG-SOURCE written at the very beginning
77 (source-info nil :type (or null sb!c::debug-source)))
79 ;;; This structure holds information about a circularity.
80 (defstruct (circularity (:copier nil))
81 ;; the kind of modification to make to create circularity
82 (type (missing-arg) :type (member :rplaca :rplacd :svset :struct-set))
83 ;; the object containing circularity
84 object
85 ;; index in object for circularity
86 (index (missing-arg) :type index)
87 ;; the object to be stored at INDEX in OBJECT. This is that the key
88 ;; that we were using when we discovered the circularity.
89 value
90 ;; the value that was associated with VALUE in the
91 ;; CIRCULARITY-TABLE. This is the object that we look up in the
92 ;; EQ-TABLE to locate VALUE.
93 enclosing-object)
95 ;;; a list of the CIRCULARITY structures for all of the circularities
96 ;;; detected in the current top level call to DUMP-OBJECT. Setting
97 ;;; this lobotomizes circularity detection as well, since circular
98 ;;; dumping uses the table.
99 (defvar *circularities-detected*)
101 ;;; used to turn off the structure validation during dumping of source
102 ;;; info
103 (defvar *dump-only-valid-structures* t)
104 ;;;; utilities
106 ;;; Write the byte B to the specified FASL-OUTPUT stream.
107 (defun dump-byte (b fasl-output)
108 (declare (type (unsigned-byte 8) b) (type fasl-output fasl-output))
109 (write-byte b (fasl-output-stream fasl-output)))
111 ;; Dump a word-sized integer.
112 (defun dump-word (num fasl-output)
113 (declare (type sb!vm:word num))
114 (declare (type fasl-output fasl-output))
115 (let ((stream (fasl-output-stream fasl-output)))
116 (dotimes (i sb!vm:n-word-bytes)
117 (write-byte (ldb (byte 8 (* 8 i)) num) stream))))
119 ;; Dump a 32-bit integer.
120 (defun dump-unsigned-byte-32 (num fasl-output)
121 (declare (type sb!vm:word num))
122 (declare (type fasl-output fasl-output))
123 (let ((stream (fasl-output-stream fasl-output)))
124 (dotimes (i 4)
125 (write-byte (ldb (byte 8 (* 8 i)) num) stream))))
127 ;;; Dump NUM to the fasl stream, represented by N bytes. This works
128 ;;; for either signed or unsigned integers. There's no range checking
129 ;;; -- if you don't specify enough bytes for the number to fit, this
130 ;;; function cheerfully outputs the low bytes.
131 (defun dump-integer-as-n-bytes (num bytes fasl-output)
132 (declare (integer num) (type index bytes))
133 (declare (type fasl-output fasl-output))
134 (do ((n num (ash n -8))
135 (i bytes (1- i)))
136 ((= i 0))
137 (declare (type index i))
138 (dump-byte (logand n #xff) fasl-output))
139 (values))
141 (defun dump-varint (n fasl-output)
142 (let ((buf (fasl-output-varint-buf fasl-output)))
143 (setf (fill-pointer buf) 0)
144 (write-var-integer n buf)
145 (write-sequence buf (fasl-output-stream fasl-output))))
147 (defun dump-fop+operands (fasl-output opcode arg1
148 &optional (arg2 0 arg2p) (arg3 0 arg3p))
149 (declare (type (unsigned-byte 8) opcode) (type word arg1 arg2 arg3))
150 (let ((opcode-modifier (if (< arg1 #x10000)
151 (if (< arg1 #x100) 0 1)
152 (if (< arg1 (ash 1 32)) 2 3))))
153 (dump-byte (logior opcode opcode-modifier) fasl-output)
154 (dump-integer-as-n-bytes arg1 (ash 1 opcode-modifier) fasl-output)
155 (when arg2p (dump-varint arg2 fasl-output))
156 (when arg3p (dump-varint arg3 fasl-output))))
158 ;;; Setting this variable to an (UNSIGNED-BYTE 32) value causes
159 ;;; DUMP-FOP to use it as a counter and emit a FOP-NOP4 with the
160 ;;; counter value before every ordinary fop. This can make it easier
161 ;;; to follow the progress of LOAD-AS-FASL when
162 ;;; debugging/testing/experimenting.
163 #!+sb-show (defvar *fop-nop4-count* nil)
164 #!+sb-show (declaim (type (or (unsigned-byte 32) null) *fop-nop4-count*))
166 ;;; Dump the FOP code for the named FOP to the specified FASL-OUTPUT.
168 ;;; FIXME: This should be a function, with a compiler macro expansion
169 ;;; for the common constant-FS case. (Among other things, that'll stop
170 ;;; it from EVALing ,FILE multiple times.)
172 ;;; FIXME: Compiler macros, frozen classes, inlining, and similar
173 ;;; optimizations should be conditional on #!+SB-FROZEN.
174 (eval-when (:compile-toplevel :execute)
175 (#+sb-xc-host defmacro #-sb-xc-host sb!xc:defmacro dump-fop (fs-expr file &rest args)
176 (let* ((fs (eval fs-expr))
177 (val (or (get fs 'opcode)
178 (error "compiler bug: ~S is not a legal fasload operator."
179 fs-expr)))
180 (fop-argc (aref (car **fop-signatures**) val)))
181 (cond
182 ((not (eql (length args) fop-argc))
183 (error "~S takes ~D argument~:P" fs fop-argc))
185 `(progn
186 #!+sb-show
187 (when *fop-nop4-count*
188 (dump-byte (get 'fop-nop4 'fop-code) ,file)
189 (dump-integer-as-n-bytes (mod (incf *fop-nop4-count*) (expt 2 32))
190 4 ,file))
191 ,(if (zerop fop-argc)
192 `(dump-byte ,val ,file)
193 `(dump-fop+operands ,file ,val ,@args))))))))
195 ;;; Push the object at table offset Handle on the fasl stack.
196 (defun dump-push (handle fasl-output)
197 (declare (type index handle) (type fasl-output fasl-output))
198 (dump-fop 'fop-push fasl-output handle)
199 (values))
201 ;;; Pop the object currently on the fasl stack top into the table, and
202 ;;; return the table index, incrementing the free pointer.
203 (defun dump-pop (fasl-output)
204 (prog1
205 (fasl-output-table-free fasl-output)
206 (dump-fop 'fop-pop fasl-output)
207 (incf (fasl-output-table-free fasl-output))))
209 (defun dump-to-table (fasl-output)
210 (prog1
211 (fasl-output-table-free fasl-output)
212 (dump-fop 'fop-move-to-table fasl-output)
213 (incf (fasl-output-table-free fasl-output))))
215 ;;; If X is in File's EQUAL-TABLE, then push the object and return T,
216 ;;; otherwise NIL.
217 (defun equal-check-table (x fasl-output)
218 (declare (type fasl-output fasl-output))
219 (let ((handle (gethash x (fasl-output-equal-table fasl-output))))
220 (cond
221 (handle (dump-push handle fasl-output) t)
222 (t nil))))
223 (defun string-check-table (x fasl-output)
224 (declare (type fasl-output fasl-output)
225 (type string x))
226 (let ((handle (cdr (assoc
227 #+sb-xc-host 'base-char ; for repeatable xc fasls
228 #-sb-xc-host (array-element-type x)
229 (gethash x (fasl-output-equal-table fasl-output))))))
230 (cond
231 (handle (dump-push handle fasl-output) t)
232 (t nil))))
234 ;;; These functions are called after dumping an object to save the
235 ;;; object in the table. The object (also passed in as X) must already
236 ;;; be on the top of the FOP stack.
237 (defun eq-save-object (x fasl-output)
238 (declare (type fasl-output fasl-output))
239 (setf (gethash x (fasl-output-eq-table fasl-output))
240 (dump-to-table fasl-output))
241 (values))
242 (defun equal-save-object (x fasl-output)
243 (declare (type fasl-output fasl-output))
244 (let ((handle (dump-to-table fasl-output)))
245 (setf (gethash x (fasl-output-equal-table fasl-output)) handle)
246 (setf (gethash x (fasl-output-eq-table fasl-output)) handle))
247 (values))
248 (defun string-save-object (x fasl-output)
249 (declare (type fasl-output fasl-output)
250 (type string x))
251 (let ((handle (dump-to-table fasl-output)))
252 (push (cons #+sb-xc-host 'base-char ; repeatable xc fasls
253 #-sb-xc-host (array-element-type x)
254 handle)
255 (gethash x (fasl-output-equal-table fasl-output)))
256 (setf (gethash x (fasl-output-eq-table fasl-output)) handle))
257 (values))
258 ;;; Record X in File's CIRCULARITY-TABLE. This is called on objects
259 ;;; that we are about to dump might have a circular path through them.
261 ;;; The object must not currently be in this table, since the dumper
262 ;;; should never be recursively called on a circular reference.
263 ;;; Instead, the dumping function must detect the circularity and
264 ;;; arrange for the dumped object to be patched.
265 (defun note-potential-circularity (x fasl-output)
266 (let ((circ (fasl-output-circularity-table fasl-output)))
267 (aver (not (gethash x circ)))
268 (setf (gethash x circ) x))
269 (values))
271 ;;;; opening and closing fasl files
273 ;;; Open a fasl file, write its header, and return a FASL-OUTPUT
274 ;;; object for dumping to it. Some human-readable information about
275 ;;; the source code is given by the string WHERE.
276 (defun open-fasl-output (name where)
277 (declare (type pathname name))
278 (flet ((fasl-write-string (string stream)
279 ;; UTF-8 is safe to use, because +FASL-HEADER-STRING-STOP-CHAR-CODE+
280 ;; may not appear in UTF-8 encoded bytes
281 (write-sequence (string-to-octets string :external-format :utf-8)
282 stream)))
283 (let* ((stream (open name
284 :direction :output
285 :if-exists :supersede
286 :element-type 'sb!assem:assembly-unit))
287 (res (make-fasl-output :stream stream)))
288 ;; Before the actual FASL header, write a shebang line using the current
289 ;; runtime path, so our fasls can be executed directly from the shell.
290 (when *runtime-pathname*
291 #+sb-xc-host (bug "Can't write shebang line") ; no #'NATIVE-PATHNAME
292 #-sb-xc-host
293 (fasl-write-string
294 (format nil "#!~A --script~%"
295 (native-namestring *runtime-pathname* :as-file t))
296 stream))
297 ;; Begin the header with the constant machine-readable (and
298 ;; semi-human-readable) string which is used to identify fasl files.
299 (fasl-write-string *fasl-header-string-start-string* stream)
300 ;; The constant string which begins the header is followed by
301 ;; arbitrary human-readable text, terminated by
302 ;; +FASL-HEADER-STRING-STOP-CHAR-CODE+.
303 (fasl-write-string
304 (with-standard-io-syntax
305 (let ((*print-readably* nil)
306 (*print-pretty* nil))
307 (format nil
308 "~% ~
309 compiled from ~S~% ~
310 using ~A version ~A~%"
311 where
312 (sb!xc:lisp-implementation-type)
313 (sb!xc:lisp-implementation-version))))
314 stream)
315 (dump-byte +fasl-header-string-stop-char-code+ res)
316 ;; Finish the header by outputting fasl file implementation,
317 ;; version, and key *FEATURES*.
318 (flet ((dump-counted-string (string)
319 ;; The count is dumped as a 32-bit unsigned-byte even on 64-bit
320 ;; platforms. This ensures that a x86-64 SBCL can gracefully
321 ;; detect an error when trying to read a x86 fasl, instead
322 ;; of choking on a ridiculously long counted string.
323 ;; -- JES, 2005-12-30
324 (dump-unsigned-byte-32 (length string) res)
325 (dotimes (i (length string))
326 (dump-byte (char-code (aref string i)) res))))
327 (dump-counted-string (symbol-name +backend-fasl-file-implementation+))
328 (dump-word +fasl-file-version+ res)
329 (dump-counted-string (sb!xc:lisp-implementation-version))
330 (dump-counted-string (compute-features-affecting-fasl-format)))
331 res)))
333 ;;; Close the specified FASL-OUTPUT, aborting the write if ABORT-P.
334 (defun close-fasl-output (fasl-output abort-p)
335 (declare (type fasl-output fasl-output))
337 (unless abort-p
338 ;; sanity checks
339 (aver (zerop (hash-table-count (fasl-output-patch-table fasl-output))))
340 ;; End the group.
341 (dump-fop 'fop-verify-empty-stack fasl-output)
342 (dump-fop 'fop-verify-table-size fasl-output)
343 (dump-word (fasl-output-table-free fasl-output)
344 fasl-output)
345 (dump-fop 'fop-end-group fasl-output))
347 ;; That's all, folks.
348 (close (fasl-output-stream fasl-output) :abort abort-p)
349 (values))
351 ;;;; main entries to object dumping
353 ;;; This function deals with dumping objects that are complex enough
354 ;;; so that we want to cache them in the table, rather than repeatedly
355 ;;; dumping them. If the object is in the EQ-TABLE, then we push it,
356 ;;; otherwise, we do a type dispatch to a type specific dumping
357 ;;; function. The type specific branches do any appropriate
358 ;;; EQUAL-TABLE check and table entry.
360 ;;; When we go to dump the object, we enter it in the CIRCULARITY-TABLE.
361 (defun dump-non-immediate-object (x file)
362 (let ((index (gethash x (fasl-output-eq-table file))))
363 (cond (index
364 (dump-push index file))
366 (typecase x
367 (symbol (dump-symbol x file))
368 (list
369 (cond ((not (coalesce-tree-p x))
370 (dump-list x file)
371 (eq-save-object x file))
372 ((not (equal-check-table x file))
373 (dump-list x file t)
374 (equal-save-object x file))))
375 (layout
376 (dump-layout x file)
377 (eq-save-object x file))
378 (instance
379 (dump-structure x file)
380 (eq-save-object x file))
381 (array
382 ;; DUMP-ARRAY (and its callees) are responsible for
383 ;; updating the EQ and EQUAL hash tables.
384 (dump-array x file))
385 (number
386 (unless (equal-check-table x file)
387 (etypecase x
388 (ratio (dump-ratio x file))
389 (complex (dump-complex x file))
390 (float (dump-float x file))
391 (integer (dump-integer x file)))
392 (equal-save-object x file)))
393 #!+(and (not (host-feature sb-xc-host)) sb-simd-pack)
394 (simd-pack
395 (unless (equal-check-table x file)
396 (dump-fop 'fop-simd-pack file)
397 (dump-integer-as-n-bytes (%simd-pack-tag x) 8 file)
398 (dump-integer-as-n-bytes (%simd-pack-low x) 8 file)
399 (dump-integer-as-n-bytes (%simd-pack-high x) 8 file))
400 (equal-save-object x file))
402 ;; This probably never happens, since bad things tend to
403 ;; be detected during IR1 conversion.
404 (error "This object cannot be dumped into a fasl file:~% ~S"
405 x))))))
406 (values))
408 ;;; Dump an object of any type by dispatching to the correct
409 ;;; type-specific dumping function. We pick off immediate objects,
410 ;;; symbols and magic lists here. Other objects are handled by
411 ;;; DUMP-NON-IMMEDIATE-OBJECT.
413 ;;; This is the function used for recursive calls to the fasl dumper.
414 ;;; We don't worry about creating circularities here, since it is
415 ;;; assumed that there is a top level call to DUMP-OBJECT.
416 (defun sub-dump-object (x file)
417 (cond ((listp x)
418 (if x
419 (dump-non-immediate-object x file)
420 (dump-fop 'fop-empty-list file)))
421 ((symbolp x)
422 (if (eq x t)
423 (dump-fop 'fop-truth file)
424 (dump-non-immediate-object x file)))
425 ((fixnump x) (dump-integer x file))
426 ((characterp x) (dump-character x file))
428 (dump-non-immediate-object x file))))
430 ;;; Dump stuff to backpatch already dumped objects. INFOS is the list
431 ;;; of CIRCULARITY structures describing what to do. The patching FOPs
432 ;;; take the value to store on the stack. We compute this value by
433 ;;; fetching the enclosing object from the table, and then CDR'ing it
434 ;;; if necessary.
435 (defun dump-circularities (infos file)
436 (let ((table (fasl-output-eq-table file)))
437 (dolist (info infos)
439 (let* ((value (circularity-value info))
440 (enclosing (circularity-enclosing-object info)))
441 (dump-push (gethash enclosing table) file)
442 (unless (eq enclosing value)
443 (do ((current enclosing (cdr current))
444 (i 0 (1+ i)))
445 ((eq current value)
446 (dump-fop 'fop-nthcdr file)
447 (dump-word i file))
448 (declare (type index i)))))
450 (ecase (circularity-type info)
451 (:rplaca (dump-fop 'fop-rplaca file))
452 (:rplacd (dump-fop 'fop-rplacd file))
453 (:svset (dump-fop 'fop-svset file))
454 (:struct-set (dump-fop 'fop-structset file)))
455 (dump-word (gethash (circularity-object info) table) file)
456 (dump-word (circularity-index info) file))))
458 ;;; Set up stuff for circularity detection, then dump an object. All
459 ;;; shared and circular structure will be exactly preserved within a
460 ;;; single call to DUMP-OBJECT. Sharing between objects dumped by
461 ;;; separate calls is only preserved when convenient.
463 ;;; We peek at the object type so that we only pay the circular
464 ;;; detection overhead on types of objects that might be circular.
465 (defun dump-object (x file)
466 (if (compound-object-p x)
467 (let ((*circularities-detected* ())
468 (circ (fasl-output-circularity-table file)))
469 (clrhash circ)
470 (sub-dump-object x file)
471 (when *circularities-detected*
472 (dump-circularities *circularities-detected* file)
473 (clrhash circ)))
474 (sub-dump-object x file)))
476 ;;;; LOAD-TIME-VALUE and MAKE-LOAD-FORM support
478 ;;; Emit a funcall of the function and return the handle for the
479 ;;; result.
480 (defun fasl-dump-load-time-value-lambda (fun file no-skip)
481 (declare (type sb!c::clambda fun) (type fasl-output file))
482 (let ((handle (gethash (sb!c::leaf-info fun)
483 (fasl-output-entry-table file))))
484 (aver handle)
485 (dump-push handle file)
486 ;; Can't skip MAKE-LOAD-FORM due to later references
487 (if no-skip
488 (dump-fop 'fop-funcall-no-skip file)
489 (dump-fop 'fop-funcall file))
490 (dump-byte 0 file))
491 (dump-pop file))
493 ;;; Return T iff CONSTANT has already been dumped. It's been dumped if
494 ;;; it's in the EQ table.
496 ;;; Note: historically (1) the above comment was "T iff ... has not been dumped",
497 ;;; (2) the test was was also true if the constant had been validated / was in
498 ;;; the valid objects table. This led to substructures occasionally skipping the
499 ;;; validation, and hence failing the "must have been validated" test.
500 (defun fasl-constant-already-dumped-p (constant file)
501 (and (gethash constant (fasl-output-eq-table file)) t))
503 ;;; Use HANDLE whenever we try to dump CONSTANT. HANDLE should have been
504 ;;; returned earlier by FASL-DUMP-LOAD-TIME-VALUE-LAMBDA.
505 (defun fasl-note-handle-for-constant (constant handle file)
506 (let ((table (fasl-output-eq-table file)))
507 (when (gethash constant table)
508 (error "~S already dumped?" constant))
509 (setf (gethash constant table) handle))
510 (values))
512 ;;; Note that the specified structure can just be dumped by
513 ;;; enumerating the slots.
514 (defun fasl-validate-structure (structure file)
515 (setf (gethash structure (fasl-output-valid-structures file)) t)
516 (values))
518 ;;;; number dumping
520 (defun dump-ratio (x file)
521 (sub-dump-object (numerator x) file)
522 (sub-dump-object (denominator x) file)
523 (dump-fop 'fop-ratio file))
525 (defun dump-integer (n file)
526 (typecase n
527 ((signed-byte 8)
528 (dump-fop 'fop-byte-integer file)
529 (dump-byte (logand #xFF n) file))
530 ((unsigned-byte #.(1- sb!vm:n-word-bits))
531 (dump-fop 'fop-word-integer file)
532 (dump-word n file))
533 ((signed-byte #.sb!vm:n-word-bits)
534 (dump-fop 'fop-word-integer file)
535 (dump-integer-as-n-bytes n #.sb!vm:n-word-bytes file))
537 (let ((bytes (ceiling (1+ (integer-length n)) 8)))
538 (dump-fop 'fop-integer file bytes)
539 (dump-integer-as-n-bytes n bytes file)))))
541 (defun dump-float (x file)
542 (etypecase x
543 (single-float
544 (dump-fop 'fop-single-float file)
545 (dump-integer-as-n-bytes (single-float-bits x) 4 file))
546 (double-float
547 (dump-fop 'fop-double-float file)
548 (let ((x x))
549 (declare (double-float x))
550 (dump-integer-as-n-bytes (double-float-low-bits x) 4 file)
551 (dump-integer-as-n-bytes (double-float-high-bits x) 4 file)))
552 #!+long-float
553 (long-float
554 (dump-fop 'fop-long-float file)
555 (dump-long-float x file))))
557 (defun dump-complex-single-float (re im file)
558 (declare (single-float re im))
559 (dump-fop 'fop-complex-single-float file)
560 (dump-integer-as-n-bytes (single-float-bits re) 4 file)
561 (dump-integer-as-n-bytes (single-float-bits im) 4 file))
563 (defun dump-complex-double-float (re im file)
564 (declare (double-float re im))
565 (dump-fop 'fop-complex-double-float file)
566 (dump-integer-as-n-bytes (double-float-low-bits re) 4 file)
567 (dump-integer-as-n-bytes (double-float-high-bits re) 4 file)
568 (dump-integer-as-n-bytes (double-float-low-bits im) 4 file)
569 (dump-integer-as-n-bytes (double-float-high-bits im) 4 file))
571 (defun dump-complex-rational (re im file)
572 (sub-dump-object re file)
573 (sub-dump-object im file)
574 (dump-fop 'fop-complex file))
576 #+sb-xc-host
577 (defun dump-complex (x file)
578 (let ((re (realpart x))
579 (im (imagpart x)))
580 (cond ((and (typep re 'single-float)
581 (typep im 'single-float))
582 (dump-complex-single-float re im file))
583 ((and (typep re 'double-float)
584 (typep im 'double-float))
585 (dump-complex-double-float re im file))
586 ((and (typep re 'rational)
587 (typep im 'rational))
588 (dump-complex-rational re im file))
590 (bug "Complex number too complex: ~S" x)))))
592 #-sb-xc-host
593 (defun dump-complex (x file)
594 (typecase x
595 ((complex single-float)
596 (dump-complex-single-float (realpart x) (imagpart x) file))
597 ((complex double-float)
598 (dump-complex-double-float (realpart x) (imagpart x) file))
599 #!+long-float
600 ((complex long-float)
601 (dump-fop 'fop-complex-long-float file)
602 (dump-long-float (realpart x) file)
603 (dump-long-float (imagpart x) file))
605 (dump-complex-rational (realpart x) (imagpart x) file))))
607 ;;;; symbol dumping
609 ;;; Return the table index of PKG, adding the package to the table if
610 ;;; necessary. During cold load, we read the string as a normal string
611 ;;; so that we can do the package lookup at cold load time.
613 ;;; FIXME: Despite the parallelism in names, the functionality of
614 ;;; this function is not parallel to other functions DUMP-FOO, e.g.
615 ;;; DUMP-SYMBOL and DUMP-LIST. The mapping between names and behavior
616 ;;; should be made more consistent.
617 (declaim (ftype (function (package fasl-output) index) dump-package))
618 (defun dump-package (pkg file)
619 (declare (inline assoc))
620 (cond ((cdr (assoc pkg (fasl-output-packages file) :test #'eq)))
622 (let ((s (package-name pkg)))
623 (dump-fop 'fop-named-package-save file (length s))
624 #+sb-xc-host
625 (dump-base-chars-of-string (coerce s 'simple-base-string) file)
626 #-sb-xc-host
627 (#!+sb-unicode dump-characters-of-string
628 #!-sb-unicode dump-base-chars-of-string
629 (coerce s '(simple-array character (*))) file))
630 (let ((entry (fasl-output-table-free file)))
631 (incf (fasl-output-table-free file))
632 (push (cons pkg entry) (fasl-output-packages file))
633 entry))))
635 ;;; dumper for lists
637 ;;; Dump a list, setting up patching information when there are
638 ;;; circularities. We scan down the list, checking for CDR and CAR
639 ;;; circularities.
641 ;;; If there is a CDR circularity, we terminate the list with NIL and
642 ;;; make a CIRCULARITY notation for the CDR of the previous cons.
644 ;;; If there is no CDR circularity, then we mark the current cons and
645 ;;; check for a CAR circularity. When there is a CAR circularity, we
646 ;;; make the CAR NIL initially, arranging for the current cons to be
647 ;;; patched later.
649 ;;; Otherwise, we recursively call the dumper to dump the current
650 ;;; element.
651 (defun dump-list (list file &optional coalesce)
652 (aver (and list
653 (not (gethash list (fasl-output-circularity-table file)))))
654 (let ((circ (fasl-output-circularity-table file)))
655 (flet ((cdr-circularity (obj n)
656 ;; COALESCE means there's no cycles
657 (let ((ref (gethash obj circ)))
658 (when ref
659 (push (make-circularity :type :rplacd
660 :object list
661 :index (1- n)
662 :value obj
663 :enclosing-object ref)
664 *circularities-detected*)
665 (terminate-undotted-list n file)
666 t))))
667 (do* ((l list (cdr l))
668 (n 0 (1+ n)))
669 ((atom l)
670 (cond ((null l)
671 (terminate-undotted-list n file))
673 (cond ((cdr-circularity l n))
675 (sub-dump-object l file)
676 (terminate-dotted-list n file))))))
677 (declare (type index n))
678 (when (cdr-circularity l n)
679 (return))
681 (setf (gethash l circ) list)
683 (let* ((obj (car l))
684 (ref (gethash obj circ)))
685 (cond (ref
686 (push (make-circularity :type :rplaca
687 :object list
688 :index n
689 :value obj
690 :enclosing-object ref)
691 *circularities-detected*)
692 (sub-dump-object nil file))
693 ;; Avoid coalescing if COALESCE-TREE-P decided not to
694 ((consp obj)
695 ;; This is the same as DUMP-NON-IMMEDIATE-OBJECT but
696 ;; without calling COALESCE-TREE-P again.
697 (let ((index (gethash obj (fasl-output-eq-table file))))
698 (cond (index
699 (dump-push index file))
700 ((not coalesce)
701 (dump-list obj file)
702 (eq-save-object obj file))
703 ((not (equal-check-table obj file))
704 (dump-list obj file t)
705 (equal-save-object obj file)))))
707 (sub-dump-object obj file))))))))
709 (defun terminate-dotted-list (n file)
710 (declare (type index n) (type fasl-output file))
711 (case n
712 (1 (dump-fop 'fop-list*-1 file))
713 (2 (dump-fop 'fop-list*-2 file))
714 (3 (dump-fop 'fop-list*-3 file))
715 (4 (dump-fop 'fop-list*-4 file))
716 (5 (dump-fop 'fop-list*-5 file))
717 (6 (dump-fop 'fop-list*-6 file))
718 (7 (dump-fop 'fop-list*-7 file))
719 (8 (dump-fop 'fop-list*-8 file))
720 (t (do ((nn n (- nn 255)))
721 ((< nn 256)
722 (dump-fop 'fop-list* file)
723 (dump-byte nn file))
724 (declare (type index nn))
725 (dump-fop 'fop-list* file)
726 (dump-byte 255 file)))))
728 ;;; If N > 255, must build list with one LIST operator, then LIST*
729 ;;; operators.
731 (defun terminate-undotted-list (n file)
732 (declare (type index n) (type fasl-output file))
733 (case n
734 (1 (dump-fop 'fop-list-1 file))
735 (2 (dump-fop 'fop-list-2 file))
736 (3 (dump-fop 'fop-list-3 file))
737 (4 (dump-fop 'fop-list-4 file))
738 (5 (dump-fop 'fop-list-5 file))
739 (6 (dump-fop 'fop-list-6 file))
740 (7 (dump-fop 'fop-list-7 file))
741 (8 (dump-fop 'fop-list-8 file))
742 (t (cond ((< n 256)
743 (dump-fop 'fop-list file)
744 (dump-byte n file))
745 (t (dump-fop 'fop-list file)
746 (dump-byte 255 file)
747 (do ((nn (- n 255) (- nn 255)))
748 ((< nn 256)
749 (dump-fop 'fop-list* file)
750 (dump-byte nn file))
751 (declare (type index nn))
752 (dump-fop 'fop-list* file)
753 (dump-byte 255 file)))))))
755 ;;;; array dumping
757 ;;; Dump the array thing.
758 (defun dump-array (x file)
759 (if (vectorp x)
760 (dump-vector x file)
761 #-sb-xc-host (dump-multi-dim-array x file)
762 #+sb-xc-host (bug "Can't dump multi-dim array")))
764 ;;; Dump the vector object. If it's not simple, then actually dump a
765 ;;; simple version of it. But we enter the original in the EQ or EQUAL
766 ;;; tables.
767 (defun dump-vector (x file)
768 (let ((simple-version (if (array-header-p x)
769 (coerce x `(simple-array
770 ,(array-element-type x)
771 (*)))
772 x)))
773 (typecase simple-version
774 #+sb-xc-host
775 (simple-string
776 (unless (string-check-table x file)
777 (dump-simple-base-string simple-version file)
778 (string-save-object x file)))
779 #-sb-xc-host
780 (simple-base-string
781 (unless (string-check-table x file)
782 (dump-simple-base-string simple-version file)
783 (string-save-object x file)))
784 #-sb-xc-host
785 ((simple-array character (*))
786 #!+sb-unicode
787 (unless (string-check-table x file)
788 (dump-simple-character-string simple-version file)
789 (string-save-object x file))
790 #!-sb-unicode
791 (bug "how did we get here?"))
792 (simple-vector
793 ;; xc-host may upgrade anything to T, so pre-check that it
794 ;; wasn't actually supposed to be a specialized array,
795 ;; and in case a copy was made, tell DUMP-S-V the original type.
796 (cond #+sb-xc-host
797 ((neq (!specialized-array-element-type x) t)
798 (dump-specialized-vector (!specialized-array-element-type x)
799 simple-version file))
801 (dump-simple-vector simple-version file)))
802 (eq-save-object x file))
804 ;; Host may have a different specialization, which is ok in itself,
805 ;; but again we might have have copied the vector, losing the type.
806 (dump-specialized-vector
807 #+sb-xc-host (!specialized-array-element-type x) simple-version file)
808 (eq-save-object x file)))))
810 ;;; Dump a SIMPLE-VECTOR, handling any circularities.
811 (defun dump-simple-vector (v file)
812 (declare (type simple-vector v) (type fasl-output file))
813 (note-potential-circularity v file)
814 (do ((index 0 (1+ index))
815 (length (length v))
816 (circ (fasl-output-circularity-table file)))
817 ((= index length)
818 (dump-fop 'fop-vector file length))
819 (let* ((obj (aref v index))
820 (ref (gethash obj circ)))
821 (cond (ref
822 (push (make-circularity :type :svset
823 :object v
824 :index index
825 :value obj
826 :enclosing-object ref)
827 *circularities-detected*)
828 (sub-dump-object nil file))
830 (sub-dump-object obj file))))))
832 ;;; In the grand scheme of things I don't pretend to understand any
833 ;;; more how this works, or indeed whether. But to write out specialized
834 ;;; vectors in the same format as fop-spec-vector expects to read them
835 ;;; we need to be target-endian. dump-integer-as-n-bytes always writes
836 ;;; little-endian (which is correct for all other integers) so for a bigendian
837 ;;; target we need to swap octets -- CSR, after DB
838 ;;; We sanity-check that VECTOR was registered as a specializd array.
839 ;;; Slight problem: if the host upgraded an array to T and we wanted it
840 ;;; more specialized, this would be undetected because the check is that
841 ;;; _if_ the array is specialized, _then_ it must have been registered.
842 ;;; The reverse is always true. But we wouldn't get here at all for (array T).
843 ;;; As a practical matter, silent failure is unlikely because
844 ;;; when building SBCL in SBCL, the needed specializations exist,
845 ;;; so the sanity-check will be triggered, and we can fix the source.
846 #+sb-xc-host
847 (defun dump-specialized-vector (element-type vector file
848 &key data-only) ; basically unused now
849 (labels ((octet-swap (word bits)
850 "BITS must be a multiple of 8"
851 (do ((input word (ash input -8))
852 (output 0 (logior (ash output 8) (logand input #xff)))
853 (bits bits (- bits 8)))
854 ((<= bits 0) output)))
855 (dump-unsigned-vector (widetag bytes bits)
856 (unless data-only
857 (dump-fop 'fop-spec-vector file)
858 (dump-word (length vector) file)
859 (dump-byte widetag file))
860 (dovector (i vector)
861 (dump-integer-as-n-bytes
862 (ecase sb!c:*backend-byte-order*
863 (:little-endian i)
864 (:big-endian (octet-swap i bits))) ; signed or unsigned OK
865 bytes file))))
866 (cond
867 ((listp element-type)
868 (destructuring-bind (type-id bits) element-type
869 (dump-unsigned-vector
870 (ecase type-id
871 (signed-byte
872 (ecase bits
873 (8 sb!vm:simple-array-signed-byte-8-widetag)
874 (16 sb!vm:simple-array-signed-byte-16-widetag)
875 (32 sb!vm:simple-array-signed-byte-32-widetag)
876 #!+64-bit
877 (64 sb!vm:simple-array-signed-byte-64-widetag)))
878 (unsigned-byte
879 (ecase bits
880 (8 sb!vm:simple-array-unsigned-byte-8-widetag)
881 (16 sb!vm:simple-array-unsigned-byte-16-widetag)
882 (32 sb!vm:simple-array-unsigned-byte-32-widetag)
883 #!+64-bit
884 (64 sb!vm:simple-array-unsigned-byte-64-widetag))))
885 (/ bits sb!vm:n-byte-bits)
886 bits)))
887 ((typep vector '(simple-bit-vector 0))
888 ;; NIL bits+bytes are ok- DUMP-INTEGER-AS-N-BYTES is unreachable.
889 ;; Otherwise we'd need to fill up octets using an ash/logior loop.
890 (dump-unsigned-vector sb!vm:simple-bit-vector-widetag nil nil))
891 ((and (typep vector '(vector * 0)) data-only)
892 nil) ; empty vector and data-only => nothing to do
893 ((typep vector '(vector (unsigned-byte 8)))
894 ;; FIXME: eliminate this case, falling through to ERROR.
895 (compiler-style-warn
896 "Unportably dumping (ARRAY (UNSIGNED-BYTE 8)) ~S" vector)
897 (dump-unsigned-vector sb!vm:simple-array-unsigned-byte-8-widetag 1 8))
899 (error "Won't dump specialized array ~S" vector)))))
901 #-sb-xc-host
902 (defun dump-specialized-vector (vector file &key data-only)
903 ;; The DATA-ONLY option was for the now-obsolete trace-table,
904 ;; but it seems like a good option to keep around.
905 (declare (type (simple-unboxed-array (*)) vector))
906 (let* ((length (length vector))
907 (widetag (%other-pointer-widetag vector))
908 (bits-per-length (aref **saetp-bits-per-length** widetag)))
909 (aver (< bits-per-length 255))
910 (unless data-only
911 (dump-fop 'fop-spec-vector file)
912 (dump-word length file)
913 (dump-byte widetag file))
914 (dump-raw-bytes vector
915 (ceiling (* length bits-per-length) sb!vm:n-byte-bits)
916 file)))
918 ;;; Dump characters and string-ish things.
920 (defun dump-character (char file)
921 (dump-fop 'fop-character file (sb!xc:char-code char)))
923 (defun dump-base-chars-of-string (s fasl-output)
924 (declare #+sb-xc-host (type simple-string s)
925 #-sb-xc-host (type simple-base-string s)
926 (type fasl-output fasl-output))
927 (dovector (c s)
928 (dump-byte (sb!xc:char-code c) fasl-output))
929 (values))
932 ;;; Dump a SIMPLE-BASE-STRING.
933 (defun dump-simple-base-string (s file)
934 #+sb-xc-host (declare (type simple-string s))
935 #-sb-xc-host (declare (type simple-base-string s))
936 (dump-fop 'fop-base-string file (length s))
937 (dump-base-chars-of-string s file)
938 (values))
940 ;;; If we get here, it is assumed that the symbol isn't in the table,
941 ;;; but we are responsible for putting it there when appropriate.
942 (defun dump-symbol (s file)
943 (declare (type fasl-output file))
944 (let* ((pname (symbol-name s))
945 (pname-length (length pname))
946 (base-string-p (typep pname (or #-sb-xc-host 'base-string t)))
947 (length+flag (logior (ash pname-length 1) (if base-string-p 1 0)))
948 (dumped-as-copy nil)
949 (pkg (symbol-package s)))
950 ;; see comment in genesis: we need this here for repeatable fasls
951 #+sb-xc-host
952 (multiple-value-bind (cl-symbol cl-status)
953 (find-symbol (symbol-name s) sb!int:*cl-package*)
954 (when (and (eq s cl-symbol)
955 (eq cl-status :external))
956 ;; special case, to work around possible xc host "design
957 ;; choice" weirdness in COMMON-LISP package
958 (setq pkg sb!int:*cl-package*)))
960 (cond ((null pkg)
961 (let ((this-base-p base-string-p))
962 (dolist (lookalike (gethash pname (fasl-output-string=-table file))
963 (dump-fop 'fop-uninterned-symbol-save
964 file length+flag))
965 ;; Find the right kind of lookalike symbol.
966 ;; [what about a symbol whose name is a (simple-array nil (0))?]
967 (let ((that-base-p
968 #+sb-xc-host t
969 #-sb-xc-host (typep (symbol-name lookalike) 'base-string)))
970 (when (or (and this-base-p that-base-p)
971 (and (not this-base-p) (not that-base-p)))
972 (dump-fop 'fop-copy-symbol-save file
973 (gethash lookalike (fasl-output-eq-table file)))
974 (return (setq dumped-as-copy t)))))))
975 ((eq pkg sb!int:*cl-package*)
976 (dump-fop 'fop-lisp-symbol-save file length+flag))
977 ((eq pkg sb!int:*keyword-package*)
978 (dump-fop 'fop-keyword-symbol-save file length+flag))
980 (let ((pkg-index (dump-package pkg file)))
981 (dump-fop 'fop-symbol-in-package-save file
982 length+flag pkg-index))))
984 (unless dumped-as-copy
985 (funcall (if base-string-p
986 'dump-base-chars-of-string
987 'dump-characters-of-string)
988 pname file)
989 (push s (gethash (symbol-name s) (fasl-output-string=-table file))))
991 (setf (gethash s (fasl-output-eq-table file))
992 (fasl-output-table-free file))
994 (incf (fasl-output-table-free file)))
996 (values))
998 ;;;; component (function) dumping
1000 (defun dump-segment (segment code-length fasl-output)
1001 (declare (type sb!assem:segment segment)
1002 (type fasl-output fasl-output))
1003 (let* ((stream (fasl-output-stream fasl-output))
1004 (n-written (write-segment-contents segment stream)))
1005 ;; In CMU CL there was no enforced connection between the CODE-LENGTH
1006 ;; argument and the number of bytes actually written. I added this
1007 ;; assertion while trying to debug portable genesis. -- WHN 19990902
1008 (unless (= code-length n-written)
1009 (bug "code-length=~W, n-written=~W" code-length n-written)))
1010 (values))
1012 ;;; Dump all the fixups. Currently there are three flavors of fixup:
1013 ;;; - assembly routines: named by a symbol
1014 ;;; - foreign (C) symbols: named by a string
1015 ;;; - code object references: don't need a name.
1016 (defun dump-fixups (fixups fasl-output)
1017 (declare (list fixups) (type fasl-output fasl-output))
1018 (dolist (note fixups)
1019 (let* ((kind (fixup-note-kind note))
1020 (fixup (fixup-note-fixup note))
1021 (position (fixup-note-position note))
1022 (name (fixup-name fixup))
1023 (flavor (fixup-flavor fixup)))
1024 (dump-object kind fasl-output)
1025 ;; Depending on the flavor, we may have various kinds of
1026 ;; noise before the position.
1027 (ecase flavor
1028 (:assembly-routine
1029 (aver (symbolp name))
1030 (dump-object name fasl-output)
1031 (dump-fop 'fop-assembler-fixup fasl-output))
1032 ((:foreign :foreign-dataref)
1033 (aver (stringp name))
1034 (ecase flavor
1035 (:foreign
1036 (dump-fop 'fop-foreign-fixup fasl-output))
1037 #!+linkage-table
1038 (:foreign-dataref
1039 (dump-fop 'fop-foreign-dataref-fixup fasl-output)))
1040 (let ((len (length name)))
1041 (aver (< len 256)) ; (limit imposed by fop definition)
1042 (dump-byte len fasl-output)
1043 (dotimes (i len)
1044 (dump-byte (char-code (schar name i)) fasl-output))))
1045 (:code-object
1046 (aver (null name))
1047 (dump-fop 'fop-code-object-fixup fasl-output))
1048 #!+immobile-space
1049 (:layout
1050 (dump-non-immediate-object (classoid-name (layout-classoid name))
1051 fasl-output)
1052 (dump-fop 'fop-layout-fixup fasl-output))
1053 #!+immobile-space
1054 (:immobile-object
1055 (dump-non-immediate-object (the symbol name) fasl-output)
1056 (dump-fop 'fop-immobile-obj-fixup fasl-output))
1057 #!+immobile-code
1058 (:named-call
1059 (dump-non-immediate-object name fasl-output)
1060 (dump-fop 'fop-named-call-fixup fasl-output))
1061 #!+immobile-code
1062 (:static-call
1063 (dump-non-immediate-object name fasl-output)
1064 (dump-fop 'fop-static-call-fixup fasl-output))
1065 (:symbol-tls-index
1066 (aver (symbolp name))
1067 (dump-non-immediate-object name fasl-output)
1068 (dump-fop 'fop-symbol-tls-fixup fasl-output)))
1069 ;; No matter what the flavor, we'll always dump the position
1070 (dump-word position fasl-output)))
1071 (values))
1073 ;;; Dump out the constant pool and code-vector for component, push the
1074 ;;; result in the table, and return the offset.
1076 ;;; The only tricky thing is handling constant-pool references to
1077 ;;; functions. If we have already dumped the function, then we just
1078 ;;; push the code pointer. Otherwise, we must create back-patching
1079 ;;; information so that the constant will be set when the function is
1080 ;;; eventually dumped. This is a bit awkward, since we don't have the
1081 ;;; handle for the code object being dumped while we are dumping its
1082 ;;; constants.
1084 ;;; We dump trap objects in any unused slots or forward referenced slots.
1085 (defun dump-code-object (component code-segment code-length fixups
1086 fasl-output entry-offsets)
1087 (declare (type component component)
1088 (type index code-length)
1089 (type fasl-output fasl-output))
1090 (let* ((2comp (component-info component))
1091 (constants (sb!c:ir2-component-constants 2comp))
1092 (header-length (length constants)))
1093 (collect ((patches))
1094 ;; Dump the constants, noting any :ENTRY constants that have to
1095 ;; be patched.
1096 (loop for i from sb!vm:code-constants-offset below header-length do
1097 (let ((entry (aref constants i)))
1098 (etypecase entry
1099 (constant
1100 (dump-object (sb!c::constant-value entry) fasl-output))
1101 (cons
1102 (ecase (car entry)
1103 (:entry
1104 (let* ((info (sb!c::leaf-info (cdr entry)))
1105 (handle (gethash info
1106 (fasl-output-entry-table
1107 fasl-output))))
1108 (declare (type sb!c::entry-info info))
1109 (cond
1110 (handle
1111 (dump-push handle fasl-output))
1113 (patches (cons info i))
1114 (dump-fop 'fop-misc-trap fasl-output)))))
1115 (:load-time-value
1116 (dump-push (cdr entry) fasl-output))
1117 (:fdefinition
1118 (dump-object (cdr entry) fasl-output)
1119 (dump-fop 'fop-fdefn fasl-output))
1120 (:known-fun
1121 (dump-object (cdr entry) fasl-output)
1122 (dump-fop 'fop-known-fun fasl-output))))
1123 (null
1124 (dump-fop 'fop-misc-trap fasl-output)))))
1126 ;; Dump the debug info.
1127 (let ((info (sb!c::debug-info-for-component component))
1128 (*dump-only-valid-structures* nil))
1129 (setf (sb!c::debug-info-source info)
1130 (fasl-output-source-info fasl-output))
1131 (dump-object info fasl-output))
1133 (dump-object (if (eq (sb!c::component-kind component) :toplevel) :toplevel nil)
1134 fasl-output)
1135 (dump-fop 'fop-code fasl-output code-length
1136 (- header-length sb!vm:code-constants-offset)
1137 (length entry-offsets))
1139 (dump-segment code-segment code-length fasl-output)
1140 (dolist (val entry-offsets) (dump-varint val fasl-output))
1142 ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it
1143 ;; dumps aren't included in the LENGTH passed to FOP-CODE.
1144 (dump-fixups fixups fasl-output)
1146 #!-(or x86 (and x86-64 (not immobile-space)))
1147 (dump-fop 'fop-sanctify-for-execution fasl-output)
1149 (let ((handle (dump-pop fasl-output)))
1150 (dolist (patch (patches))
1151 (push (cons handle (cdr patch))
1152 (gethash (car patch)
1153 (fasl-output-patch-table fasl-output))))
1154 handle))))
1156 ;;; This is only called from assemfile, which doesn't exist in the target.
1157 #+sb-xc-host
1158 (defun dump-assembler-routines (code-segment length fixups routines file)
1159 (dump-fop 'fop-assembler-code file)
1160 (dump-word length file)
1161 (write-segment-contents code-segment (fasl-output-stream file))
1162 (dolist (routine routines)
1163 (dump-object (car routine) file)
1164 (dump-fop 'fop-assembler-routine file)
1165 (dump-word (+ (label-position (cadr routine))
1166 (caddr routine))
1167 file))
1168 (dump-fixups fixups file)
1169 #!-(or x86 x86-64)
1170 (dump-fop 'fop-sanctify-for-execution file)
1171 (dump-pop file))
1173 ;;; Alter the code object referenced by CODE-HANDLE at the specified
1174 ;;; OFFSET, storing the object referenced by ENTRY-HANDLE.
1175 (defun dump-alter-code-object (code-handle offset entry-handle file)
1176 (declare (type index code-handle entry-handle offset))
1177 (declare (type fasl-output file))
1178 (dump-push code-handle file)
1179 (dump-push entry-handle file)
1180 (dump-fop 'fop-alter-code file offset)
1181 (values))
1183 ;;; Dump the code, constants, etc. for component. We pass in the
1184 ;;; assembler fixups, code vector and node info.
1185 (defun fasl-dump-component (component
1186 code-segment
1187 code-length
1188 fixups
1189 file)
1190 (declare (type component component))
1191 (declare (type fasl-output file))
1193 (dump-fop 'fop-verify-table-size file)
1194 (dump-word (fasl-output-table-free file) file)
1196 #!+sb-dyncount
1197 (let ((info (sb!c::ir2-component-dyncount-info (component-info component))))
1198 (when info
1199 (fasl-validate-structure info file)))
1201 (let* ((2comp (component-info component))
1202 (entries (sb!c::ir2-component-entries 2comp))
1203 (nfuns (length entries))
1204 (code-handle (dump-code-object
1205 component code-segment code-length
1206 fixups file
1207 (mapcar (lambda (entry)
1208 (label-position (sb!c::entry-info-offset entry)))
1209 entries)))
1210 (fun-index nfuns))
1212 (dolist (entry entries)
1213 (dump-push code-handle file)
1214 (dump-object (sb!c::entry-info-name entry) file)
1215 (dump-object (sb!c::entry-info-arguments entry) file)
1216 (dump-object (sb!c::entry-info-type entry) file)
1217 (dump-object (sb!c::entry-info-info entry) file)
1218 (dump-fop 'fop-fun-entry file (decf fun-index))
1219 (let ((entry-handle (dump-pop file)))
1220 (setf (gethash entry (fasl-output-entry-table file)) entry-handle)
1221 (let ((old (gethash entry (fasl-output-patch-table file))))
1222 (when old
1223 (dolist (patch old)
1224 (dump-alter-code-object (car patch)
1225 (cdr patch)
1226 entry-handle
1227 file))
1228 (remhash entry (fasl-output-patch-table file)))))))
1229 (values))
1231 (defun dump-push-previously-dumped-fun (fun fasl-output)
1232 (declare (type sb!c::clambda fun))
1233 (let ((handle (gethash (sb!c::leaf-info fun)
1234 (fasl-output-entry-table fasl-output))))
1235 (aver handle)
1236 (dump-push handle fasl-output))
1237 (values))
1239 ;;; Dump a FOP-FUNCALL to call an already-dumped top level lambda at
1240 ;;; load time.
1241 (defun fasl-dump-toplevel-lambda-call (fun fasl-output)
1242 (declare (type sb!c::clambda fun))
1243 (dump-push-previously-dumped-fun fun fasl-output)
1244 (dump-fop 'fop-funcall-for-effect fasl-output)
1245 (dump-byte 0 fasl-output)
1246 (values))
1248 ;;;; dumping structures
1250 ;; Having done nothing more than load all files in obj/from-host, the
1251 ;; cross-compiler running under any host Lisp begins life able to access
1252 ;; SBCL-format metadata for any structure that is a subtype of STRUCTURE!OBJECT.
1253 ;; But if it learns a layout by cross-compiling a DEFSTRUCT, that's ok too.
1254 (defun dump-structure (struct file)
1255 (when (and *dump-only-valid-structures*
1256 (not (gethash struct (fasl-output-valid-structures file))))
1257 (error "attempt to dump invalid structure:~% ~S~%How did this happen?"
1258 struct))
1259 (note-potential-circularity struct file)
1260 (do* ((length (%instance-length struct))
1261 (layout (%instance-layout struct))
1262 (bitmap (layout-bitmap layout))
1263 (circ (fasl-output-circularity-table file))
1264 (index sb!vm:instance-data-start (1+ index)))
1265 ((>= index length)
1266 (dump-non-immediate-object layout file)
1267 (dump-fop 'fop-struct file length))
1268 (let* ((obj (if (logbitp index bitmap)
1269 (%instance-ref struct index)
1270 (%raw-instance-ref/word struct index)))
1271 (ref (gethash obj circ)))
1272 (sub-dump-object (cond (ref
1273 (push (make-circularity :type :struct-set
1274 :object struct
1275 :index index
1276 :value obj
1277 :enclosing-object ref)
1278 *circularities-detected*)
1279 nil)
1280 (t obj))
1281 file))))
1283 (defun dump-layout (obj file)
1284 (when (layout-invalid obj)
1285 (compiler-error "attempt to dump reference to obsolete class: ~S"
1286 (layout-classoid obj)))
1287 ;; STANDARD-OBJECT could in theory be dumpable, but nothing else,
1288 ;; because all its subclasses can evolve to have new layouts.
1289 (aver (not (logtest (layout-%flags obj) +pcl-object-layout-flag+)))
1290 (let ((name (classoid-name (layout-classoid obj))))
1291 ;; Q: Shouldn't we aver that NAME is the proper name for its classoid?
1292 (unless name
1293 (compiler-error "dumping anonymous layout: ~S" obj))
1294 ;; The target lisp can save some space in fasls (sometimes),
1295 ;; but the cross-compiler can't because we need to construct the
1296 ;; cold representation of all layouts, not reference host layouts.
1297 #-sb-xc-host
1298 (let ((fop (known-layout-fop name)))
1299 (when fop
1300 (return-from dump-layout (dump-byte fop file))))
1301 (dump-object name file))
1302 (sub-dump-object (layout-inherits obj) file)
1303 (sub-dump-object (layout-depthoid obj) file)
1304 (sub-dump-object (layout-length obj) file)
1305 (sub-dump-object (layout-bitmap obj) file)
1306 (dump-fop 'fop-layout file))