Change syntax of DEFINE-FOP, and remove from target image.
[sbcl.git] / src / compiler / dump.lisp
blob0f7176eecc2987b4e34b16a7dfb83d7c35cadec2
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 ;; the table's current free pointer: the next offset to be used
42 (table-free 0 :type index)
43 ;; an alist (PACKAGE . OFFSET) of the table offsets for each package
44 ;; we have currently located.
45 (packages () :type list)
46 ;; a table mapping from the ENTRY-INFO structures for dumped XEPs to
47 ;; the table offsets of the corresponding code pointers
48 (entry-table (make-hash-table :test 'eq) :type hash-table)
49 ;; a table holding back-patching info for forward references to XEPs.
50 ;; The key is the ENTRY-INFO structure for the XEP, and the value is
51 ;; a list of conses (<code-handle> . <offset>), where <code-handle>
52 ;; is the offset in the table of the code object needing to be
53 ;; patched, and <offset> is the offset that must be patched.
54 (patch-table (make-hash-table :test 'eq) :type hash-table)
55 ;; a list of the table handles for all of the DEBUG-INFO structures
56 ;; dumped in this file. These structures must be back-patched with
57 ;; source location information when the compilation is complete.
58 (debug-info () :type list)
59 ;; This is used to keep track of objects that we are in the process
60 ;; of dumping so that circularities can be preserved. The key is the
61 ;; object that we have previously seen, and the value is the object
62 ;; that we reference in the table to find this previously seen
63 ;; object. (The value is never NIL.)
65 ;; Except with list objects, the key and the value are always the
66 ;; same. In a list, the key will be some tail of the value.
67 (circularity-table (make-hash-table :test 'eq) :type hash-table)
68 ;; a hash table of structures that are allowed to be dumped. If we
69 ;; try to dump a structure that isn't in this hash table, we lose.
70 (valid-structures (make-hash-table :test 'eq) :type hash-table))
72 ;;; This structure holds information about a circularity.
73 (defstruct (circularity (:copier nil))
74 ;; the kind of modification to make to create circularity
75 (type (missing-arg) :type (member :rplaca :rplacd :svset :struct-set))
76 ;; the object containing circularity
77 object
78 ;; index in object for circularity
79 (index (missing-arg) :type index)
80 ;; the object to be stored at INDEX in OBJECT. This is that the key
81 ;; that we were using when we discovered the circularity.
82 value
83 ;; the value that was associated with VALUE in the
84 ;; CIRCULARITY-TABLE. This is the object that we look up in the
85 ;; EQ-TABLE to locate VALUE.
86 enclosing-object)
88 ;;; a list of the CIRCULARITY structures for all of the circularities
89 ;;; detected in the current top level call to DUMP-OBJECT. Setting
90 ;;; this lobotomizes circularity detection as well, since circular
91 ;;; dumping uses the table.
92 (defvar *circularities-detected*)
94 ;;; used to turn off the structure validation during dumping of source
95 ;;; info
96 (defvar *dump-only-valid-structures* t)
97 ;;;; utilities
99 ;;; Write the byte B to the specified FASL-OUTPUT stream.
100 (defun dump-byte (b fasl-output)
101 (declare (type (unsigned-byte 8) b) (type fasl-output fasl-output))
102 (write-byte b (fasl-output-stream fasl-output)))
104 ;; Dump a word-sized integer.
105 (defun dump-word (num fasl-output)
106 (declare (type sb!vm:word num))
107 (declare (type fasl-output fasl-output))
108 (let ((stream (fasl-output-stream fasl-output)))
109 (dotimes (i sb!vm:n-word-bytes)
110 (write-byte (ldb (byte 8 (* 8 i)) num) stream))))
112 ;; Dump a 32-bit integer.
113 (defun dump-unsigned-byte-32 (num fasl-output)
114 (declare (type sb!vm:word num))
115 (declare (type fasl-output fasl-output))
116 (let ((stream (fasl-output-stream fasl-output)))
117 (dotimes (i 4)
118 (write-byte (ldb (byte 8 (* 8 i)) num) stream))))
120 ;;; Dump NUM to the fasl stream, represented by N bytes. This works
121 ;;; for either signed or unsigned integers. There's no range checking
122 ;;; -- if you don't specify enough bytes for the number to fit, this
123 ;;; function cheerfully outputs the low bytes.
124 (defun dump-integer-as-n-bytes (num bytes fasl-output)
125 (declare (integer num) (type index bytes))
126 (declare (type fasl-output fasl-output))
127 (do ((n num (ash n -8))
128 (i bytes (1- i)))
129 ((= i 0))
130 (declare (type index i))
131 (dump-byte (logand n #xff) fasl-output))
132 (values))
134 (defun dump-fop+operands (fasl-output opcode arg1 &optional (arg2 0 arg2p))
135 (declare (type (unsigned-byte 8) opcode) (type word arg1 arg2))
136 (multiple-value-bind (modifier1 modifier2)
137 (flet ((fop-opcode-modifier (arg)
138 ;; The compiler should be able to figure that 32-bit builds
139 ;; can not use the 8-byte size, I think.
140 (if (< arg #x10000)
141 (if (< arg #x100) 0 1)
142 (if (< arg (ash 1 32)) 2 3))))
143 (declare (inline fop-opcode-modifier))
144 (values (fop-opcode-modifier arg1)
145 (if arg2p (fop-opcode-modifier arg2) 0)))
146 (dump-byte (logior opcode (ash modifier2 2) modifier1) fasl-output)
147 ;; special-case code optimized for each size might improve speed here
148 (dump-integer-as-n-bytes arg1 (ash 1 modifier1) fasl-output)
149 (when arg2p
150 (dump-integer-as-n-bytes arg2 (ash 1 modifier2) fasl-output))))
152 ;;; Setting this variable to an (UNSIGNED-BYTE 32) value causes
153 ;;; DUMP-FOP to use it as a counter and emit a FOP-NOP4 with the
154 ;;; counter value before every ordinary fop. This can make it easier
155 ;;; to follow the progress of LOAD-AS-FASL when
156 ;;; debugging/testing/experimenting.
157 #!+sb-show (defvar *fop-nop4-count* nil)
158 #!+sb-show (declaim (type (or (unsigned-byte 32) null) *fop-nop4-count*))
160 ;;; Dump the FOP code for the named FOP to the specified FASL-OUTPUT.
162 ;;; FIXME: This should be a function, with a compiler macro expansion
163 ;;; for the common constant-FS case. (Among other things, that'll stop
164 ;;; it from EVALing ,FILE multiple times.)
166 ;;; FIXME: Compiler macros, frozen classes, inlining, and similar
167 ;;; optimizations should be conditional on #!+SB-FROZEN.
168 (defmacro dump-fop (fs-expr file &rest args)
169 (let* ((fs (eval fs-expr))
170 (val (get fs 'opcode))
171 (fop-argc
172 (if val
173 (if (>= val +2-operand-fops+) 2 (sbit *fop-argp* (ash val -2)))
175 (supplied-argc (length args)))
176 (cond
177 ((not val)
178 (error "compiler bug: ~S is not a legal fasload operator." fs-expr))
179 ((not (eql supplied-argc fop-argc))
180 (error "~S takes ~D argument~:P" fs fop-argc))
182 `(progn
183 #!+sb-show
184 (when *fop-nop4-count*
185 (dump-byte ,(get 'fop-nop4 'fop-code) ,file)
186 (dump-integer-as-n-bytes (mod (incf *fop-nop4-count*) (expt 2 32))
187 4 ,file))
188 ,(if (zerop fop-argc)
189 `(dump-byte ,val ,file)
190 `(dump-fop+operands ,file ,val ,@args)))))))
192 ;;; Push the object at table offset Handle on the fasl stack.
193 (defun dump-push (handle fasl-output)
194 (declare (type index handle) (type fasl-output fasl-output))
195 (dump-fop 'fop-push fasl-output handle)
196 (values))
198 ;;; Pop the object currently on the fasl stack top into the table, and
199 ;;; return the table index, incrementing the free pointer.
200 (defun dump-pop (fasl-output)
201 (prog1
202 (fasl-output-table-free fasl-output)
203 (dump-fop 'fop-pop fasl-output)
204 (incf (fasl-output-table-free fasl-output))))
206 ;;; If X is in File's EQUAL-TABLE, then push the object and return T,
207 ;;; otherwise NIL.
208 (defun equal-check-table (x fasl-output)
209 (declare (type fasl-output fasl-output))
210 (let ((handle (gethash x (fasl-output-equal-table fasl-output))))
211 (cond
212 (handle (dump-push handle fasl-output) t)
213 (t nil))))
214 (defun string-check-table (x fasl-output)
215 (declare (type fasl-output fasl-output)
216 (type string x))
217 (let ((handle (cdr (assoc
218 #+sb-xc-host 'base-char ; for repeatable xc fasls
219 #-sb-xc-host (array-element-type x)
220 (gethash x (fasl-output-equal-table fasl-output))))))
221 (cond
222 (handle (dump-push handle fasl-output) t)
223 (t nil))))
225 ;;; These functions are called after dumping an object to save the
226 ;;; object in the table. The object (also passed in as X) must already
227 ;;; be on the top of the FOP stack.
228 (defun eq-save-object (x fasl-output)
229 (declare (type fasl-output fasl-output))
230 (let ((handle (dump-pop fasl-output)))
231 (setf (gethash x (fasl-output-eq-table fasl-output)) handle)
232 (dump-push handle fasl-output))
233 (values))
234 (defun equal-save-object (x fasl-output)
235 (declare (type fasl-output fasl-output))
236 (let ((handle (dump-pop fasl-output)))
237 (setf (gethash x (fasl-output-equal-table fasl-output)) handle)
238 (setf (gethash x (fasl-output-eq-table fasl-output)) handle)
239 (dump-push handle fasl-output))
240 (values))
241 (defun string-save-object (x fasl-output)
242 (declare (type fasl-output fasl-output)
243 (type string x))
244 (let ((handle (dump-pop fasl-output)))
245 (push (cons #+sb-xc-host 'base-char ; repeatable xc fasls
246 #-sb-xc-host (array-element-type x)
247 handle)
248 (gethash x (fasl-output-equal-table fasl-output)))
249 (setf (gethash x (fasl-output-eq-table fasl-output)) handle)
250 (dump-push handle fasl-output))
251 (values))
252 ;;; Record X in File's CIRCULARITY-TABLE. This is called on objects
253 ;;; that we are about to dump might have a circular path through them.
255 ;;; The object must not currently be in this table, since the dumper
256 ;;; should never be recursively called on a circular reference.
257 ;;; Instead, the dumping function must detect the circularity and
258 ;;; arrange for the dumped object to be patched.
259 (defun note-potential-circularity (x fasl-output)
260 (let ((circ (fasl-output-circularity-table fasl-output)))
261 (aver (not (gethash x circ)))
262 (setf (gethash x circ) x))
263 (values))
265 ;;;; opening and closing fasl files
267 ;;; Open a fasl file, write its header, and return a FASL-OUTPUT
268 ;;; object for dumping to it. Some human-readable information about
269 ;;; the source code is given by the string WHERE.
270 (defun open-fasl-output (name where)
271 (declare (type pathname name))
272 (flet ((fasl-write-string (string stream)
273 ;; SB-EXT:STRING-TO-OCTETS is not available while cross-compiling
274 #+sb-xc-host
275 (loop for char across string
276 do (let ((code (char-code char)))
277 (unless (<= 0 code 127)
278 (setf char #\?))
279 (write-byte code stream)))
280 ;; UTF-8 is safe to use, because +FASL-HEADER-STRING-STOP-CHAR-CODE+
281 ;; may not appear in UTF-8 encoded bytes
282 #-sb-xc-host
283 (write-sequence (string-to-octets string :external-format :utf-8)
284 stream)))
285 (let* ((stream (open name
286 :direction :output
287 :if-exists :supersede
288 :element-type 'sb!assem:assembly-unit))
289 (res (make-fasl-output :stream stream)))
290 ;; Before the actual FASL header, write a shebang line using the current
291 ;; runtime path, so our fasls can be executed directly from the shell.
292 (when *runtime-pathname*
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 *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 ;; KLUDGE: The code in this case has been hacked
370 ;; to match Douglas Crosher's quick fix to CMU CL
371 ;; (on cmucl-imp 1999-12-27), applied in sbcl-0.6.8.11
372 ;; with help from Martin Atzmueller. This is not an
373 ;; ideal solution; to quote DTC,
374 ;; The compiler locks up trying to coalesce the
375 ;; constant lists. The hack below will disable the
376 ;; coalescing of lists while dumping and allows
377 ;; the code to compile. The real fix would be to
378 ;; take a little more care while dumping these.
379 ;; So if better list coalescing is needed, start here.
380 ;; -- WHN 2000-11-07
381 (if (maybe-cyclic-p x)
382 (progn
383 (dump-list x file)
384 (eq-save-object x file))
385 (unless (equal-check-table x file)
386 (dump-list x file)
387 (equal-save-object x file))))
388 (layout
389 (dump-layout x file)
390 (eq-save-object x file))
391 (instance
392 (dump-structure x file)
393 (eq-save-object x file))
394 (array
395 ;; DUMP-ARRAY (and its callees) are responsible for
396 ;; updating the EQ and EQUAL hash tables.
397 (dump-array x file))
398 (number
399 (unless (equal-check-table x file)
400 (etypecase x
401 (ratio (dump-ratio x file))
402 (complex (dump-complex x file))
403 (float (dump-float x file))
404 (integer (dump-integer x file)))
405 (equal-save-object x file)))
406 #!+sb-simd-pack
407 (simd-pack
408 (unless (equal-check-table x file)
409 (dump-fop 'fop-simd-pack file)
410 (dump-integer-as-n-bytes (%simd-pack-tag x) 8 file)
411 (dump-integer-as-n-bytes (%simd-pack-low x) 8 file)
412 (dump-integer-as-n-bytes (%simd-pack-high x) 8 file))
413 (equal-save-object x file))
415 ;; This probably never happens, since bad things tend to
416 ;; be detected during IR1 conversion.
417 (error "This object cannot be dumped into a fasl file:~% ~S"
418 x))))))
419 (values))
421 ;;; Dump an object of any type by dispatching to the correct
422 ;;; type-specific dumping function. We pick off immediate objects,
423 ;;; symbols and magic lists here. Other objects are handled by
424 ;;; DUMP-NON-IMMEDIATE-OBJECT.
426 ;;; This is the function used for recursive calls to the fasl dumper.
427 ;;; We don't worry about creating circularities here, since it is
428 ;;; assumed that there is a top level call to DUMP-OBJECT.
429 (defun sub-dump-object (x file)
430 (cond ((listp x)
431 (if x
432 (dump-non-immediate-object x file)
433 (dump-fop 'fop-empty-list file)))
434 ((symbolp x)
435 (if (eq x t)
436 (dump-fop 'fop-truth file)
437 (dump-non-immediate-object x file)))
438 ((fixnump x) (dump-integer x file))
439 ((characterp x) (dump-character x file))
441 (dump-non-immediate-object x file))))
443 ;;; Dump stuff to backpatch already dumped objects. INFOS is the list
444 ;;; of CIRCULARITY structures describing what to do. The patching FOPs
445 ;;; take the value to store on the stack. We compute this value by
446 ;;; fetching the enclosing object from the table, and then CDR'ing it
447 ;;; if necessary.
448 (defun dump-circularities (infos file)
449 (let ((table (fasl-output-eq-table file)))
450 (dolist (info infos)
452 (let* ((value (circularity-value info))
453 (enclosing (circularity-enclosing-object info)))
454 (dump-push (gethash enclosing table) file)
455 (unless (eq enclosing value)
456 (do ((current enclosing (cdr current))
457 (i 0 (1+ i)))
458 ((eq current value)
459 (dump-fop 'fop-nthcdr file)
460 (dump-word i file))
461 (declare (type index i)))))
463 (ecase (circularity-type info)
464 (:rplaca (dump-fop 'fop-rplaca file))
465 (:rplacd (dump-fop 'fop-rplacd file))
466 (:svset (dump-fop 'fop-svset file))
467 (:struct-set (dump-fop 'fop-structset file)))
468 (dump-word (gethash (circularity-object info) table) file)
469 (dump-word (circularity-index info) file))))
471 ;;; Set up stuff for circularity detection, then dump an object. All
472 ;;; shared and circular structure will be exactly preserved within a
473 ;;; single call to DUMP-OBJECT. Sharing between objects dumped by
474 ;;; separate calls is only preserved when convenient.
476 ;;; We peek at the object type so that we only pay the circular
477 ;;; detection overhead on types of objects that might be circular.
478 (defun dump-object (x file)
479 (if (compound-object-p x)
480 (let ((*circularities-detected* ())
481 (circ (fasl-output-circularity-table file)))
482 (clrhash circ)
483 (sub-dump-object x file)
484 (when *circularities-detected*
485 (dump-circularities *circularities-detected* file)
486 (clrhash circ)))
487 (sub-dump-object x file)))
489 ;;;; LOAD-TIME-VALUE and MAKE-LOAD-FORM support
491 ;;; Emit a funcall of the function and return the handle for the
492 ;;; result.
493 (defun fasl-dump-load-time-value-lambda (fun file)
494 (declare (type sb!c::clambda fun) (type fasl-output file))
495 (let ((handle (gethash (sb!c::leaf-info fun)
496 (fasl-output-entry-table file))))
497 (aver handle)
498 (dump-push handle file)
499 (dump-fop 'fop-funcall file)
500 (dump-byte 0 file))
501 (dump-pop file))
503 ;;; Return T iff CONSTANT has already been dumped. It's been dumped if
504 ;;; it's in the EQ table.
506 ;;; Note: historically (1) the above comment was "T iff ... has not been dumped",
507 ;;; (2) the test was was also true if the constant had been validated / was in
508 ;;; the valid objects table. This led to substructures occasionally skipping the
509 ;;; validation, and hence failing the "must have been validated" test.
510 (defun fasl-constant-already-dumped-p (constant file)
511 (and (gethash constant (fasl-output-eq-table file)) t))
513 ;;; Use HANDLE whenever we try to dump CONSTANT. HANDLE should have been
514 ;;; returned earlier by FASL-DUMP-LOAD-TIME-VALUE-LAMBDA.
515 (defun fasl-note-handle-for-constant (constant handle file)
516 (let ((table (fasl-output-eq-table file)))
517 (when (gethash constant table)
518 (error "~S already dumped?" constant))
519 (setf (gethash constant table) handle))
520 (values))
522 ;;; Note that the specified structure can just be dumped by
523 ;;; enumerating the slots.
524 (defun fasl-validate-structure (structure file)
525 (setf (gethash structure (fasl-output-valid-structures file)) t)
526 (values))
528 ;;;; number dumping
530 (defun dump-ratio (x file)
531 (sub-dump-object (numerator x) file)
532 (sub-dump-object (denominator x) file)
533 (dump-fop 'fop-ratio file))
535 (defun dump-integer (n file)
536 (typecase n
537 ((signed-byte 8)
538 (dump-fop 'fop-byte-integer file)
539 (dump-byte (logand #xFF n) file))
540 ((unsigned-byte #.(1- sb!vm:n-word-bits))
541 (dump-fop 'fop-word-integer file)
542 (dump-word n file))
543 ((signed-byte #.sb!vm:n-word-bits)
544 (dump-fop 'fop-word-integer file)
545 (dump-integer-as-n-bytes n #.sb!vm:n-word-bytes file))
547 (let ((bytes (ceiling (1+ (integer-length n)) 8)))
548 (dump-fop 'fop-integer file bytes)
549 (dump-integer-as-n-bytes n bytes file)))))
551 (defun dump-float (x file)
552 (etypecase x
553 (single-float
554 (dump-fop 'fop-single-float file)
555 (dump-integer-as-n-bytes (single-float-bits x) 4 file))
556 (double-float
557 (dump-fop 'fop-double-float file)
558 (let ((x x))
559 (declare (double-float x))
560 (dump-integer-as-n-bytes (double-float-low-bits x) 4 file)
561 (dump-integer-as-n-bytes (double-float-high-bits x) 4 file)))
562 #!+long-float
563 (long-float
564 (dump-fop 'fop-long-float file)
565 (dump-long-float x file))))
567 (defun dump-complex-single-float (re im file)
568 (declare (single-float re im))
569 (dump-fop 'fop-complex-single-float file)
570 (dump-integer-as-n-bytes (single-float-bits re) 4 file)
571 (dump-integer-as-n-bytes (single-float-bits im) 4 file))
573 (defun dump-complex-double-float (re im file)
574 (declare (double-float re im))
575 (dump-fop 'fop-complex-double-float file)
576 (dump-integer-as-n-bytes (double-float-low-bits re) 4 file)
577 (dump-integer-as-n-bytes (double-float-high-bits re) 4 file)
578 (dump-integer-as-n-bytes (double-float-low-bits im) 4 file)
579 (dump-integer-as-n-bytes (double-float-high-bits im) 4 file))
581 (defun dump-complex-rational (re im file)
582 (sub-dump-object re file)
583 (sub-dump-object im file)
584 (dump-fop 'fop-complex file))
586 #+sb-xc-host
587 (defun dump-complex (x file)
588 (let ((re (realpart x))
589 (im (imagpart x)))
590 (cond ((and (typep re 'single-float)
591 (typep im 'single-float))
592 (dump-complex-single-float re im file))
593 ((and (typep re 'double-float)
594 (typep im 'double-float))
595 (dump-complex-double-float re im file))
596 ((and (typep re 'rational)
597 (typep im 'rational))
598 (dump-complex-rational re im file))
600 (bug "Complex number too complex: ~S" x)))))
602 #-sb-xc-host
603 (defun dump-complex (x file)
604 (typecase x
605 ((complex single-float)
606 (dump-complex-single-float (realpart x) (imagpart x) file))
607 ((complex double-float)
608 (dump-complex-double-float (realpart x) (imagpart x) file))
609 #!+long-float
610 ((complex long-float)
611 (dump-fop 'fop-complex-long-float file)
612 (dump-long-float (realpart x) file)
613 (dump-long-float (imagpart x) file))
615 (dump-complex-rational (realpart x) (imagpart x) file))))
617 ;;;; symbol dumping
619 ;;; Return the table index of PKG, adding the package to the table if
620 ;;; necessary. During cold load, we read the string as a normal string
621 ;;; so that we can do the package lookup at cold load time.
623 ;;; FIXME: Despite the parallelism in names, the functionality of
624 ;;; this function is not parallel to other functions DUMP-FOO, e.g.
625 ;;; DUMP-SYMBOL and DUMP-LIST. The mapping between names and behavior
626 ;;; should be made more consistent.
627 (declaim (ftype (function (package fasl-output) index) dump-package))
628 (defun dump-package (pkg file)
629 (declare (inline assoc))
630 (cond ((cdr (assoc pkg (fasl-output-packages file) :test #'eq)))
632 (let ((s (package-name pkg)))
633 (dump-fop 'fop-named-package-save file (length s))
634 #+sb-xc-host
635 (dump-base-chars-of-string (coerce s 'simple-base-string) file)
636 #-sb-xc-host
637 (#!+sb-unicode dump-characters-of-string
638 #!-sb-unicode dump-base-chars-of-string
639 (coerce s '(simple-array character (*))) file))
640 (let ((entry (fasl-output-table-free file)))
641 (incf (fasl-output-table-free file))
642 (push (cons pkg entry) (fasl-output-packages file))
643 entry))))
645 ;;; dumper for lists
647 ;;; Dump a list, setting up patching information when there are
648 ;;; circularities. We scan down the list, checking for CDR and CAR
649 ;;; circularities.
651 ;;; If there is a CDR circularity, we terminate the list with NIL and
652 ;;; make a CIRCULARITY notation for the CDR of the previous cons.
654 ;;; If there is no CDR circularity, then we mark the current cons and
655 ;;; check for a CAR circularity. When there is a CAR circularity, we
656 ;;; make the CAR NIL initially, arranging for the current cons to be
657 ;;; patched later.
659 ;;; Otherwise, we recursively call the dumper to dump the current
660 ;;; element.
661 (defun dump-list (list file)
662 (aver (and list
663 (not (gethash list (fasl-output-circularity-table file)))))
664 (let ((circ (fasl-output-circularity-table file)))
665 (flet ((cdr-circularity (obj n)
666 (let ((ref (gethash obj circ)))
667 (when ref
668 (push (make-circularity :type :rplacd
669 :object list
670 :index (1- n)
671 :value obj
672 :enclosing-object ref)
673 *circularities-detected*)
674 (terminate-undotted-list n file)
675 t))))
676 (do* ((l list (cdr l))
677 (n 0 (1+ n)))
678 ((atom l)
679 (cond ((null l)
680 (terminate-undotted-list n file))
682 (cond ((cdr-circularity l n))
684 (sub-dump-object l file)
685 (terminate-dotted-list n file))))))
686 (declare (type index n))
687 (when (cdr-circularity l n)
688 (return))
690 (setf (gethash l circ) list)
692 (let* ((obj (car l))
693 (ref (gethash obj circ)))
694 (cond (ref
695 (push (make-circularity :type :rplaca
696 :object list
697 :index n
698 :value obj
699 :enclosing-object ref)
700 *circularities-detected*)
701 (sub-dump-object nil file))
703 (sub-dump-object obj file))))))))
705 (defun terminate-dotted-list (n file)
706 (declare (type index n) (type fasl-output file))
707 (case n
708 (1 (dump-fop 'fop-list*-1 file))
709 (2 (dump-fop 'fop-list*-2 file))
710 (3 (dump-fop 'fop-list*-3 file))
711 (4 (dump-fop 'fop-list*-4 file))
712 (5 (dump-fop 'fop-list*-5 file))
713 (6 (dump-fop 'fop-list*-6 file))
714 (7 (dump-fop 'fop-list*-7 file))
715 (8 (dump-fop 'fop-list*-8 file))
716 (t (do ((nn n (- nn 255)))
717 ((< nn 256)
718 (dump-fop 'fop-list* file)
719 (dump-byte nn file))
720 (declare (type index nn))
721 (dump-fop 'fop-list* file)
722 (dump-byte 255 file)))))
724 ;;; If N > 255, must build list with one LIST operator, then LIST*
725 ;;; operators.
727 (defun terminate-undotted-list (n file)
728 (declare (type index n) (type fasl-output file))
729 (case n
730 (1 (dump-fop 'fop-list-1 file))
731 (2 (dump-fop 'fop-list-2 file))
732 (3 (dump-fop 'fop-list-3 file))
733 (4 (dump-fop 'fop-list-4 file))
734 (5 (dump-fop 'fop-list-5 file))
735 (6 (dump-fop 'fop-list-6 file))
736 (7 (dump-fop 'fop-list-7 file))
737 (8 (dump-fop 'fop-list-8 file))
738 (t (cond ((< n 256)
739 (dump-fop 'fop-list file)
740 (dump-byte n file))
741 (t (dump-fop 'fop-list file)
742 (dump-byte 255 file)
743 (do ((nn (- n 255) (- nn 255)))
744 ((< nn 256)
745 (dump-fop 'fop-list* file)
746 (dump-byte nn file))
747 (declare (type index nn))
748 (dump-fop 'fop-list* file)
749 (dump-byte 255 file)))))))
751 ;;;; array dumping
753 ;;; Dump the array thing.
754 (defun dump-array (x file)
755 (if (vectorp x)
756 (dump-vector x file)
757 (dump-multi-dim-array x file)))
759 ;;; Dump the vector object. If it's not simple, then actually dump a
760 ;;; simple version of it. But we enter the original in the EQ or EQUAL
761 ;;; tables.
762 (defun dump-vector (x file)
763 (let ((simple-version (if (array-header-p x)
764 (coerce x `(simple-array
765 ,(array-element-type x)
766 (*)))
767 x)))
768 (typecase simple-version
769 #+sb-xc-host
770 (simple-string
771 (unless (string-check-table x file)
772 (dump-simple-base-string simple-version file)
773 (string-save-object x file)))
774 #-sb-xc-host
775 (simple-base-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-array character (*))
781 #!+sb-unicode
782 (unless (string-check-table x file)
783 (dump-simple-character-string simple-version file)
784 (string-save-object x file))
785 #!-sb-unicode
786 (bug "how did we get here?"))
787 (simple-vector
788 ;; xc-host may upgrade anything to T, so pre-check that it
789 ;; wasn't actually supposed to be a specialized array,
790 ;; and in case a copy was made, tell DUMP-S-V the original type.
791 (cond #+sb-xc-host
792 ((neq (!specialized-array-element-type x) t)
793 (dump-specialized-vector (!specialized-array-element-type x)
794 simple-version file))
796 (dump-simple-vector simple-version file)))
797 (eq-save-object x file))
799 ;; Host may have a different specialization, which is ok in itself,
800 ;; but again we might have have copied the vector, losing the type.
801 (dump-specialized-vector
802 #+sb-xc-host (!specialized-array-element-type x) simple-version file)
803 (eq-save-object x file)))))
805 ;;; Dump a SIMPLE-VECTOR, handling any circularities.
806 (defun dump-simple-vector (v file)
807 (declare (type simple-vector v) (type fasl-output file))
808 (note-potential-circularity v file)
809 (do ((index 0 (1+ index))
810 (length (length v))
811 (circ (fasl-output-circularity-table file)))
812 ((= index length)
813 (dump-fop 'fop-vector file length))
814 (let* ((obj (aref v index))
815 (ref (gethash obj circ)))
816 (cond (ref
817 (push (make-circularity :type :svset
818 :object v
819 :index index
820 :value obj
821 :enclosing-object ref)
822 *circularities-detected*)
823 (sub-dump-object nil file))
825 (sub-dump-object obj file))))))
827 ;;; In the grand scheme of things I don't pretend to understand any
828 ;;; more how this works, or indeed whether. But to write out specialized
829 ;;; vectors in the same format as fop-spec-vector expects to read them
830 ;;; we need to be target-endian. dump-integer-as-n-bytes always writes
831 ;;; little-endian (which is correct for all other integers) so for a bigendian
832 ;;; target we need to swap octets -- CSR, after DB
833 ;;; We sanity-check that VECTOR was registered as a specializd array.
834 ;;; Slight problem: if the host upgraded an array to T and we wanted it
835 ;;; more specialized, this would be undetected because the check is that
836 ;;; _if_ the array is specialized, _then_ it must have been registered.
837 ;;; The reverse is always true. But we wouldn't get here at all for (array T).
838 ;;; As a practical matter, silent failure is unlikely because
839 ;;; when building SBCL in SBCL, the needed specializations exist,
840 ;;; so the sanity-check will be triggered, and we can fix the source.
841 #+sb-xc-host
842 (defun dump-specialized-vector (element-type vector file
843 &key data-only) ; basically unused now
844 (labels ((octet-swap (word bits)
845 "BITS must be a multiple of 8"
846 (do ((input word (ash input -8))
847 (output 0 (logior (ash output 8) (logand input #xff)))
848 (bits bits (- bits 8)))
849 ((<= bits 0) output)))
850 (dump-unsigned-vector (widetag bytes bits)
851 (unless data-only
852 (dump-fop 'fop-spec-vector file)
853 (dump-word (length vector) file)
854 (dump-byte widetag file))
855 (dovector (i vector)
856 (dump-integer-as-n-bytes
857 (ecase sb!c:*backend-byte-order*
858 (:little-endian i)
859 (:big-endian (octet-swap i bits))) ; signed or unsigned OK
860 bytes file))))
861 (cond
862 ((listp element-type)
863 (destructuring-bind (type-id bits) element-type
864 (dump-unsigned-vector
865 (ecase type-id
866 (signed-byte
867 (ecase bits
868 (8 sb!vm:simple-array-signed-byte-8-widetag)
869 (16 sb!vm:simple-array-signed-byte-16-widetag)
870 (32 sb!vm:simple-array-signed-byte-32-widetag)))
871 (unsigned-byte
872 (ecase bits
873 (8 sb!vm:simple-array-unsigned-byte-8-widetag)
874 (16 sb!vm:simple-array-unsigned-byte-16-widetag)
875 (32 sb!vm:simple-array-unsigned-byte-32-widetag))))
876 (/ bits sb!vm:n-byte-bits)
877 bits)))
878 ((typep vector '(simple-bit-vector 0))
879 ;; NIL bits+bytes are ok- DUMP-INTEGER-AS-N-BYTES is unreachable.
880 ;; Otherwise we'd need to fill up octets using an ash/logior loop.
881 (dump-unsigned-vector sb!vm:simple-bit-vector-widetag nil nil))
882 ((and (typep vector '(vector * 0)) data-only)
883 nil) ; empty vector and data-only => nothing to do
884 ((typep vector '(vector (unsigned-byte 8)))
885 ;; FIXME: eliminate this case, falling through to ERROR.
886 (compiler-style-warn
887 "Unportably dumping (ARRAY (UNSIGNED-BYTE 8)) ~S" vector)
888 (dump-unsigned-vector sb!vm:simple-array-unsigned-byte-8-widetag 1 8))
890 (error "Won't dump specialized array ~S" vector)))))
892 #-sb-xc-host
893 (defun dump-specialized-vector (vector file &key data-only)
894 ;; The DATA-ONLY option was for the now-obsolete trace-table,
895 ;; but it seems like a good option to keep around.
896 (declare (type (simple-unboxed-array (*)) vector))
897 (let* ((length (length vector))
898 (widetag (%other-pointer-widetag vector))
899 (bits-per-length (aref **saetp-bits-per-length** widetag)))
900 (aver (< bits-per-length 255))
901 (unless data-only
902 (dump-fop 'fop-spec-vector file)
903 (dump-word length file)
904 (dump-byte widetag file))
905 (dump-raw-bytes vector
906 (ceiling (* length bits-per-length) sb!vm:n-byte-bits)
907 file)))
909 ;;; Dump characters and string-ish things.
911 (defun dump-character (char file)
912 (dump-fop 'fop-character file (sb!xc:char-code char)))
914 (defun dump-base-chars-of-string (s fasl-output)
915 (declare #+sb-xc-host (type simple-string s)
916 #-sb-xc-host (type simple-base-string s)
917 (type fasl-output fasl-output))
918 (dovector (c s)
919 (dump-byte (sb!xc:char-code c) fasl-output))
920 (values))
923 ;;; Dump a SIMPLE-BASE-STRING.
924 (defun dump-simple-base-string (s file)
925 #+sb-xc-host (declare (type simple-string s))
926 #-sb-xc-host (declare (type simple-base-string s))
927 (dump-fop 'fop-base-string file (length s))
928 (dump-base-chars-of-string s file)
929 (values))
931 ;;; If we get here, it is assumed that the symbol isn't in the table,
932 ;;; but we are responsible for putting it there when appropriate.
933 (defun dump-symbol (s file)
934 (declare (type fasl-output file))
935 (let* ((pname (symbol-name s))
936 (pname-length (length pname))
937 (pkg (symbol-package s)))
938 ;; see comment in genesis: we need this here for repeatable fasls
939 #+sb-xc-host
940 (multiple-value-bind (cl-symbol cl-status)
941 (find-symbol (symbol-name s) sb!int:*cl-package*)
942 (when (and (eq s cl-symbol)
943 (eq cl-status :external))
944 ;; special case, to work around possible xc host "design
945 ;; choice" weirdness in COMMON-LISP package
946 (setq pkg sb!int:*cl-package*)))
948 (cond ((null pkg)
949 (dump-fop 'fop-uninterned-symbol-save
950 file pname-length))
951 ;; CMU CL had FOP-SYMBOL-SAVE/FOP-SMALL-SYMBOL-SAVE fops which
952 ;; used the current value of *PACKAGE*. Unfortunately that's
953 ;; broken w.r.t. ANSI Common Lisp semantics, so those are gone
954 ;; from SBCL.
955 ;;((eq pkg *package*)
956 ;; (dump-fop* pname-length
957 ;; fop-small-symbol-save
958 ;; fop-symbol-save file))
959 ((eq pkg sb!int:*cl-package*)
960 (dump-fop 'fop-lisp-symbol-save file pname-length))
961 ((eq pkg sb!int:*keyword-package*)
962 (dump-fop 'fop-keyword-symbol-save file pname-length))
964 (dump-fop 'fop-symbol-in-package-save file
965 (dump-package pkg file) pname-length)))
967 #+sb-xc-host (dump-base-chars-of-string pname file)
968 #-sb-xc-host (#!+sb-unicode dump-characters-of-string
969 #!-sb-unicode dump-base-chars-of-string
970 pname file)
972 (setf (gethash s (fasl-output-eq-table file))
973 (fasl-output-table-free file))
975 (incf (fasl-output-table-free file)))
977 (values))
979 ;;;; component (function) dumping
981 (defun dump-segment (segment code-length fasl-output)
982 (declare (type sb!assem:segment segment)
983 (type fasl-output fasl-output))
984 (let* ((stream (fasl-output-stream fasl-output))
985 (n-written (write-segment-contents segment stream)))
986 ;; In CMU CL there was no enforced connection between the CODE-LENGTH
987 ;; argument and the number of bytes actually written. I added this
988 ;; assertion while trying to debug portable genesis. -- WHN 19990902
989 (unless (= code-length n-written)
990 (bug "code-length=~W, n-written=~W" code-length n-written)))
991 (values))
993 ;;; Dump all the fixups. Currently there are three flavors of fixup:
994 ;;; - assembly routines: named by a symbol
995 ;;; - foreign (C) symbols: named by a string
996 ;;; - code object references: don't need a name.
997 (defun dump-fixups (fixups fasl-output)
998 (declare (list fixups) (type fasl-output fasl-output))
999 (dolist (note fixups)
1000 (let* ((kind (fixup-note-kind note))
1001 (fixup (fixup-note-fixup note))
1002 (position (fixup-note-position note))
1003 (name (fixup-name fixup))
1004 (flavor (fixup-flavor fixup)))
1005 (dump-object kind fasl-output)
1006 ;; Depending on the flavor, we may have various kinds of
1007 ;; noise before the position.
1008 (ecase flavor
1009 (:assembly-routine
1010 (aver (symbolp name))
1011 (dump-object name fasl-output)
1012 (dump-fop 'fop-assembler-fixup fasl-output))
1013 ((:foreign :foreign-dataref)
1014 (aver (stringp name))
1015 (ecase flavor
1016 (:foreign
1017 (dump-fop 'fop-foreign-fixup fasl-output))
1018 #!+linkage-table
1019 (:foreign-dataref
1020 (dump-fop 'fop-foreign-dataref-fixup fasl-output)))
1021 (let ((len (length name)))
1022 (aver (< len 256)) ; (limit imposed by fop definition)
1023 (dump-byte len fasl-output)
1024 (dotimes (i len)
1025 (dump-byte (char-code (schar name i)) fasl-output))))
1026 (:code-object
1027 (aver (null name))
1028 (dump-fop 'fop-code-object-fixup fasl-output))
1029 (:symbol-tls-index
1030 (aver (symbolp name))
1031 (dump-non-immediate-object name fasl-output)
1032 (dump-fop 'fop-symbol-tls-fixup fasl-output)))
1033 ;; No matter what the flavor, we'll always dump the position
1034 (dump-word position fasl-output)))
1035 (values))
1037 ;;; Dump out the constant pool and code-vector for component, push the
1038 ;;; result in the table, and return the offset.
1040 ;;; The only tricky thing is handling constant-pool references to
1041 ;;; functions. If we have already dumped the function, then we just
1042 ;;; push the code pointer. Otherwise, we must create back-patching
1043 ;;; information so that the constant will be set when the function is
1044 ;;; eventually dumped. This is a bit awkward, since we don't have the
1045 ;;; handle for the code object being dumped while we are dumping its
1046 ;;; constants.
1048 ;;; We dump trap objects in any unused slots or forward referenced slots.
1049 (defun dump-code-object (component
1050 code-segment
1051 code-length
1052 fixups
1053 fasl-output)
1055 (declare (type component component)
1056 (type index code-length)
1057 (type fasl-output fasl-output))
1059 (let* ((2comp (component-info component))
1060 (constants (sb!c:ir2-component-constants 2comp))
1061 (header-length (length constants)))
1062 (collect ((patches))
1063 ;; Dump the constants, noting any :ENTRY constants that have to
1064 ;; be patched.
1065 (loop for i from sb!vm:code-constants-offset below header-length do
1066 (let ((entry (aref constants i)))
1067 (etypecase entry
1068 (constant
1069 (dump-object (sb!c::constant-value entry) fasl-output))
1070 (cons
1071 (ecase (car entry)
1072 (:entry
1073 (let* ((info (sb!c::leaf-info (cdr entry)))
1074 (handle (gethash info
1075 (fasl-output-entry-table
1076 fasl-output))))
1077 (declare (type sb!c::entry-info info))
1078 (cond
1079 (handle
1080 (dump-push handle fasl-output))
1082 (patches (cons info i))
1083 (dump-fop 'fop-misc-trap fasl-output)))))
1084 (:load-time-value
1085 (dump-push (cdr entry) fasl-output))
1086 (:fdefinition
1087 (dump-object (cdr entry) fasl-output)
1088 (dump-fop 'fop-fdefn fasl-output))
1089 (:known-fun
1090 (dump-object (cdr entry) fasl-output)
1091 (dump-fop 'fop-known-fun fasl-output))))
1092 (null
1093 (dump-fop 'fop-misc-trap fasl-output)))))
1095 ;; Dump the debug info.
1096 (let ((info (sb!c::debug-info-for-component component))
1097 (*dump-only-valid-structures* nil))
1098 (dump-object info fasl-output)
1099 (let ((info-handle (dump-pop fasl-output)))
1100 (dump-push info-handle fasl-output)
1101 (push info-handle (fasl-output-debug-info fasl-output))))
1103 (let ((num-consts (- header-length sb!vm:code-constants-offset)))
1104 (dump-fop 'fop-code fasl-output num-consts code-length))
1106 (dump-segment code-segment code-length fasl-output)
1108 ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it
1109 ;; dumps aren't included in the LENGTH passed to FOP-CODE.
1110 (dump-fixups fixups fasl-output)
1112 #!-(or x86 x86-64)
1113 (dump-fop 'fop-sanctify-for-execution fasl-output)
1115 (let ((handle (dump-pop fasl-output)))
1116 (dolist (patch (patches))
1117 (push (cons handle (cdr patch))
1118 (gethash (car patch)
1119 (fasl-output-patch-table fasl-output))))
1120 handle))))
1122 (defun dump-assembler-routines (code-segment length fixups routines file)
1123 (dump-fop 'fop-assembler-code file)
1124 (dump-word length file)
1125 (write-segment-contents code-segment (fasl-output-stream file))
1126 (dolist (routine routines)
1127 (dump-object (car routine) file)
1128 (dump-fop 'fop-assembler-routine file)
1129 (dump-word (label-position (cdr routine)) file))
1130 (dump-fixups fixups file)
1131 #!-(or x86 x86-64)
1132 (dump-fop 'fop-sanctify-for-execution file)
1133 (dump-pop file))
1135 ;;; Dump a function entry data structure corresponding to ENTRY to
1136 ;;; FILE. CODE-HANDLE is the table offset of the code object for the
1137 ;;; component.
1138 (defun dump-one-entry (entry code-handle file)
1139 (declare (type sb!c::entry-info entry) (type index code-handle)
1140 (type fasl-output file))
1141 (let ((name (sb!c::entry-info-name entry)))
1142 (dump-push code-handle file)
1143 (dump-object name file)
1144 (dump-object (sb!c::entry-info-arguments entry) file)
1145 (dump-object (sb!c::entry-info-type entry) file)
1146 (dump-object (sb!c::entry-info-info entry) file)
1147 (dump-fop 'fop-fun-entry file)
1148 (dump-word (label-position (sb!c::entry-info-offset entry)) file)
1149 (dump-pop file)))
1151 ;;; Alter the code object referenced by CODE-HANDLE at the specified
1152 ;;; OFFSET, storing the object referenced by ENTRY-HANDLE.
1153 (defun dump-alter-code-object (code-handle offset entry-handle file)
1154 (declare (type index code-handle entry-handle offset))
1155 (declare (type fasl-output file))
1156 (dump-push code-handle file)
1157 (dump-push entry-handle file)
1158 (dump-fop 'fop-alter-code file offset)
1159 (values))
1161 ;;; Dump the code, constants, etc. for component. We pass in the
1162 ;;; assembler fixups, code vector and node info.
1163 (defun fasl-dump-component (component
1164 code-segment
1165 code-length
1166 fixups
1167 file)
1168 (declare (type component component))
1169 (declare (type fasl-output file))
1171 (dump-fop 'fop-verify-table-size file)
1172 (dump-word (fasl-output-table-free file) file)
1174 #!+sb-dyncount
1175 (let ((info (sb!c::ir2-component-dyncount-info (component-info component))))
1176 (when info
1177 (fasl-validate-structure info file)))
1179 (let ((code-handle (dump-code-object component
1180 code-segment
1181 code-length
1182 fixups
1183 file))
1184 (2comp (component-info component)))
1186 (dolist (entry (sb!c::ir2-component-entries 2comp))
1187 (let ((entry-handle (dump-one-entry entry code-handle file)))
1188 (setf (gethash entry (fasl-output-entry-table file)) entry-handle)
1189 (let ((old (gethash entry (fasl-output-patch-table file))))
1190 (when old
1191 (dolist (patch old)
1192 (dump-alter-code-object (car patch)
1193 (cdr patch)
1194 entry-handle
1195 file))
1196 (remhash entry (fasl-output-patch-table file)))))))
1197 (values))
1199 (defun dump-push-previously-dumped-fun (fun fasl-output)
1200 (declare (type sb!c::clambda fun))
1201 (let ((handle (gethash (sb!c::leaf-info fun)
1202 (fasl-output-entry-table fasl-output))))
1203 (aver handle)
1204 (dump-push handle fasl-output))
1205 (values))
1207 ;;; Dump a FOP-FUNCALL to call an already-dumped top level lambda at
1208 ;;; load time.
1209 (defun fasl-dump-toplevel-lambda-call (fun fasl-output)
1210 (declare (type sb!c::clambda fun))
1211 (dump-push-previously-dumped-fun fun fasl-output)
1212 (dump-fop 'fop-funcall-for-effect fasl-output)
1213 (dump-byte 0 fasl-output)
1214 (values))
1216 ;;; Dump a FOP-FSET to arrange static linkage (at cold init) between
1217 ;;; FUN-NAME and the already-dumped function whose dump handle is
1218 ;;; FUN-DUMP-HANDLE.
1219 #+sb-xc-host
1220 (defun fasl-dump-cold-fset (fun-name fun-dump-handle fasl-output)
1221 (declare (type fixnum fun-dump-handle))
1222 (aver (legal-fun-name-p fun-name))
1223 (dump-non-immediate-object fun-name fasl-output)
1224 (dump-push fun-dump-handle fasl-output)
1225 (dump-fop 'fop-fset fasl-output)
1226 (values))
1228 ;;; Compute the correct list of DEBUG-SOURCE structures and backpatch
1229 ;;; all of the dumped DEBUG-INFO structures. We clear the
1230 ;;; FASL-OUTPUT-DEBUG-INFO, so that subsequent components with
1231 ;;; different source info may be dumped.
1232 (defun fasl-dump-source-info (info fasl-output)
1233 (declare (type sb!c::source-info info))
1234 (let ((res (sb!c::debug-source-for-info info))
1235 (*dump-only-valid-structures* nil))
1236 #+sb-xc-host (setf (sb!c::debug-source-created res) 0
1237 (sb!c::debug-source-compiled res) 0)
1238 (dump-object res fasl-output)
1239 (let ((res-handle (dump-pop fasl-output)))
1240 (dolist (info-handle (fasl-output-debug-info fasl-output))
1241 (dump-push res-handle fasl-output)
1242 (dump-fop 'fop-structset fasl-output)
1243 (dump-word info-handle fasl-output)
1244 (dump-word sb!c::+debug-info-source-index+ fasl-output))
1245 #+sb-xc-host
1246 (progn
1247 (dump-push res-handle fasl-output)
1248 (dump-fop 'fop-note-debug-source fasl-output))))
1249 (setf (fasl-output-debug-info fasl-output) nil)
1250 (values))
1252 ;;;; dumping structures
1254 ;; Having done nothing more than load all files in obj/from-host, the
1255 ;; cross-compiler running under any host Lisp begins life able to access
1256 ;; SBCL-format metadata for any structure that is a subtype of STRUCTURE!OBJECT.
1257 ;; But if it learns a layout by cross-compiling a DEFSTRUCT, that's ok too.
1258 (defun dump-structure (struct file)
1259 (when (and *dump-only-valid-structures*
1260 (not (gethash struct (fasl-output-valid-structures file)))
1261 #+sb-xc-host
1262 (not (sb!kernel::xc-dumpable-structure-instance-p struct)))
1263 (error "attempt to dump invalid structure:~% ~S~%How did this happen?"
1264 struct))
1265 (note-potential-circularity struct file)
1266 (do* ((length (%instance-length struct))
1267 (layout (%instance-layout struct))
1268 #!-interleaved-raw-slots
1269 (ntagged (- length (layout-n-untagged-slots layout)))
1270 #!+interleaved-raw-slots
1271 (bitmap (layout-untagged-bitmap layout))
1272 (circ (fasl-output-circularity-table file))
1273 ;; last slot first on the stack, so that the layout is on top:
1274 (index (1- length) (1- index)))
1275 ((< index sb!vm:instance-data-start)
1276 (dump-non-immediate-object layout file)
1277 (dump-fop 'fop-struct file length))
1278 (let* ((obj #!-interleaved-raw-slots
1279 (if (>= index ntagged)
1280 (%raw-instance-ref/word struct (- length index 1))
1281 (%instance-ref struct index))
1282 #!+interleaved-raw-slots
1283 (if (logbitp index bitmap)
1284 (%raw-instance-ref/word struct index)
1285 (%instance-ref struct index)))
1286 (ref (gethash obj circ)))
1287 (sub-dump-object (cond (ref
1288 (push (make-circularity :type :struct-set
1289 :object struct
1290 :index index
1291 :value obj
1292 :enclosing-object ref)
1293 *circularities-detected*)
1294 nil)
1295 (t obj))
1296 file))))
1298 (defun dump-layout (obj file)
1299 (when (layout-invalid obj)
1300 (compiler-error "attempt to dump reference to obsolete class: ~S"
1301 (layout-classoid obj)))
1302 (let ((name (classoid-name (layout-classoid obj))))
1303 ;; Q: Shouldn't we aver that NAME is the proper name for its classoid?
1304 (unless name
1305 (compiler-error "dumping anonymous layout: ~S" obj))
1306 (dump-object name file))
1307 (sub-dump-object (layout-inherits obj) file)
1308 (sub-dump-object (layout-depthoid obj) file)
1309 (sub-dump-object (layout-length obj) file)
1310 (sub-dump-object (layout-raw-slot-metadata obj) file)
1311 (dump-fop 'fop-layout file))