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