Distinguish base/non-base-string in fop-symbol etc
[sbcl.git] / src / compiler / dump.lisp
blob7354708e0d04dfcd2ced801c649ad1aa9a4f67bc
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 (base-string-p (typep pname (or #-sb-xc-host 'base-string t)))
948 (length+flag (logior (ash pname-length 1) (if base-string-p 1 0)))
949 (dumped-as-copy nil)
950 (pkg (symbol-package s)))
951 ;; see comment in genesis: we need this here for repeatable fasls
952 #+sb-xc-host
953 (multiple-value-bind (cl-symbol cl-status)
954 (find-symbol (symbol-name s) sb!int:*cl-package*)
955 (when (and (eq s cl-symbol)
956 (eq cl-status :external))
957 ;; special case, to work around possible xc host "design
958 ;; choice" weirdness in COMMON-LISP package
959 (setq pkg sb!int:*cl-package*)))
961 (cond ((null pkg)
962 (let ((this-base-p base-string-p))
963 (dolist (lookalike (gethash pname (fasl-output-string=-table file))
964 (dump-fop 'fop-uninterned-symbol-save
965 file length+flag))
966 ;; Find the right kind of lookalike symbol.
967 ;; [what about a symbol whose name is a (simple-array nil (0))?]
968 (let ((that-base-p
969 #+sb-xc-host t
970 #-sb-xc-host (typep (symbol-name lookalike) 'base-string)))
971 (when (or (and this-base-p that-base-p)
972 (and (not this-base-p) (not that-base-p)))
973 (dump-fop 'fop-copy-symbol-save file
974 (gethash lookalike (fasl-output-eq-table file)))
975 (return (setq dumped-as-copy t)))))))
976 ((eq pkg sb!int:*cl-package*)
977 (dump-fop 'fop-lisp-symbol-save file length+flag))
978 ((eq pkg sb!int:*keyword-package*)
979 (dump-fop 'fop-keyword-symbol-save file length+flag))
981 (dump-fop 'fop-symbol-in-package-save file
982 (dump-package pkg file) length+flag)))
984 (unless dumped-as-copy
985 (funcall (if base-string-p
986 'dump-base-chars-of-string
987 'dump-characters-of-string)
988 pname file)
989 (push s (gethash (symbol-name s) (fasl-output-string=-table file))))
991 (setf (gethash s (fasl-output-eq-table file))
992 (fasl-output-table-free file))
994 (incf (fasl-output-table-free file)))
996 (values))
998 ;;;; component (function) dumping
1000 (defun dump-segment (segment code-length fasl-output)
1001 (declare (type sb!assem:segment segment)
1002 (type fasl-output fasl-output))
1003 (let* ((stream (fasl-output-stream fasl-output))
1004 (n-written (write-segment-contents segment stream)))
1005 ;; In CMU CL there was no enforced connection between the CODE-LENGTH
1006 ;; argument and the number of bytes actually written. I added this
1007 ;; assertion while trying to debug portable genesis. -- WHN 19990902
1008 (unless (= code-length n-written)
1009 (bug "code-length=~W, n-written=~W" code-length n-written)))
1010 (values))
1012 ;;; Dump all the fixups. Currently there are three flavors of fixup:
1013 ;;; - assembly routines: named by a symbol
1014 ;;; - foreign (C) symbols: named by a string
1015 ;;; - code object references: don't need a name.
1016 (defun dump-fixups (fixups fasl-output)
1017 (declare (list fixups) (type fasl-output fasl-output))
1018 (dolist (note fixups)
1019 (let* ((kind (fixup-note-kind note))
1020 (fixup (fixup-note-fixup note))
1021 (position (fixup-note-position note))
1022 (name (fixup-name fixup))
1023 (flavor (fixup-flavor fixup)))
1024 (dump-object kind fasl-output)
1025 ;; Depending on the flavor, we may have various kinds of
1026 ;; noise before the position.
1027 (ecase flavor
1028 (:assembly-routine
1029 (aver (symbolp name))
1030 (dump-object name fasl-output)
1031 (dump-fop 'fop-assembler-fixup fasl-output))
1032 ((:foreign :foreign-dataref)
1033 (aver (stringp name))
1034 (ecase flavor
1035 (:foreign
1036 (dump-fop 'fop-foreign-fixup fasl-output))
1037 #!+linkage-table
1038 (:foreign-dataref
1039 (dump-fop 'fop-foreign-dataref-fixup fasl-output)))
1040 (let ((len (length name)))
1041 (aver (< len 256)) ; (limit imposed by fop definition)
1042 (dump-byte len fasl-output)
1043 (dotimes (i len)
1044 (dump-byte (char-code (schar name i)) fasl-output))))
1045 (:code-object
1046 (aver (null name))
1047 (dump-fop 'fop-code-object-fixup fasl-output))
1048 (:symbol-tls-index
1049 (aver (symbolp name))
1050 (dump-non-immediate-object name fasl-output)
1051 (dump-fop 'fop-symbol-tls-fixup fasl-output)))
1052 ;; No matter what the flavor, we'll always dump the position
1053 (dump-word position fasl-output)))
1054 (values))
1056 ;;; Dump out the constant pool and code-vector for component, push the
1057 ;;; result in the table, and return the offset.
1059 ;;; The only tricky thing is handling constant-pool references to
1060 ;;; functions. If we have already dumped the function, then we just
1061 ;;; push the code pointer. Otherwise, we must create back-patching
1062 ;;; information so that the constant will be set when the function is
1063 ;;; eventually dumped. This is a bit awkward, since we don't have the
1064 ;;; handle for the code object being dumped while we are dumping its
1065 ;;; constants.
1067 ;;; We dump trap objects in any unused slots or forward referenced slots.
1068 (defun dump-code-object (component
1069 code-segment
1070 code-length
1071 fixups
1072 fasl-output)
1074 (declare (type component component)
1075 (type index code-length)
1076 (type fasl-output fasl-output))
1078 (let* ((2comp (component-info component))
1079 (constants (sb!c:ir2-component-constants 2comp))
1080 (header-length (length constants)))
1081 (collect ((patches))
1082 ;; Dump the constants, noting any :ENTRY constants that have to
1083 ;; be patched.
1084 (loop for i from sb!vm:code-constants-offset below header-length do
1085 (let ((entry (aref constants i)))
1086 (etypecase entry
1087 (constant
1088 (dump-object (sb!c::constant-value entry) fasl-output))
1089 (cons
1090 (ecase (car entry)
1091 (:entry
1092 (let* ((info (sb!c::leaf-info (cdr entry)))
1093 (handle (gethash info
1094 (fasl-output-entry-table
1095 fasl-output))))
1096 (declare (type sb!c::entry-info info))
1097 (cond
1098 (handle
1099 (dump-push handle fasl-output))
1101 (patches (cons info i))
1102 (dump-fop 'fop-misc-trap fasl-output)))))
1103 (:load-time-value
1104 (dump-push (cdr entry) fasl-output))
1105 (:fdefinition
1106 (dump-object (cdr entry) fasl-output)
1107 (dump-fop 'fop-fdefn fasl-output))
1108 (:known-fun
1109 (dump-object (cdr entry) fasl-output)
1110 (dump-fop 'fop-known-fun fasl-output))))
1111 (null
1112 (dump-fop 'fop-misc-trap fasl-output)))))
1114 ;; Dump the debug info.
1115 (let ((info (sb!c::debug-info-for-component component))
1116 (*dump-only-valid-structures* nil))
1117 (dump-object info fasl-output)
1118 (push (dump-to-table fasl-output)
1119 (fasl-output-debug-info fasl-output)))
1121 (let ((num-consts (- header-length sb!vm:code-constants-offset)))
1122 (dump-fop 'fop-code fasl-output num-consts code-length))
1124 (dump-segment code-segment code-length fasl-output)
1126 ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it
1127 ;; dumps aren't included in the LENGTH passed to FOP-CODE.
1128 (dump-fixups fixups fasl-output)
1130 #!-(or x86 x86-64)
1131 (dump-fop 'fop-sanctify-for-execution fasl-output)
1133 (let ((handle (dump-pop fasl-output)))
1134 (dolist (patch (patches))
1135 (push (cons handle (cdr patch))
1136 (gethash (car patch)
1137 (fasl-output-patch-table fasl-output))))
1138 handle))))
1140 ;;; This is only called from assemfile, which doesn't exist in the target.
1141 #+sb-xc-host
1142 (defun dump-assembler-routines (code-segment length fixups routines file)
1143 (dump-fop 'fop-assembler-code file)
1144 (dump-word length file)
1145 (write-segment-contents code-segment (fasl-output-stream file))
1146 (dolist (routine routines)
1147 (dump-object (car routine) file)
1148 (dump-fop 'fop-assembler-routine file)
1149 (dump-word (+ (label-position (cadr routine))
1150 (caddr routine))
1151 file))
1152 (dump-fixups fixups file)
1153 #!-(or x86 x86-64)
1154 (dump-fop 'fop-sanctify-for-execution file)
1155 (dump-pop file))
1157 ;;; Dump a function entry data structure corresponding to ENTRY to
1158 ;;; FILE. CODE-HANDLE is the table offset of the code object for the
1159 ;;; component.
1160 (defun dump-one-entry (entry code-handle file)
1161 (declare (type sb!c::entry-info entry) (type index code-handle)
1162 (type fasl-output file))
1163 (let ((name (sb!c::entry-info-name entry)))
1164 (dump-push code-handle file)
1165 (dump-object name file)
1166 (dump-object (sb!c::entry-info-arguments entry) file)
1167 (dump-object (sb!c::entry-info-type entry) file)
1168 (dump-object (sb!c::entry-info-info entry) file)
1169 (dump-fop 'fop-fun-entry file)
1170 (dump-word (label-position (sb!c::entry-info-offset entry)) file)
1171 (dump-pop file)))
1173 ;;; Alter the code object referenced by CODE-HANDLE at the specified
1174 ;;; OFFSET, storing the object referenced by ENTRY-HANDLE.
1175 (defun dump-alter-code-object (code-handle offset entry-handle file)
1176 (declare (type index code-handle entry-handle offset))
1177 (declare (type fasl-output file))
1178 (dump-push code-handle file)
1179 (dump-push entry-handle file)
1180 (dump-fop 'fop-alter-code file offset)
1181 (values))
1183 ;;; Dump the code, constants, etc. for component. We pass in the
1184 ;;; assembler fixups, code vector and node info.
1185 (defun fasl-dump-component (component
1186 code-segment
1187 code-length
1188 fixups
1189 file)
1190 (declare (type component component))
1191 (declare (type fasl-output file))
1193 (dump-fop 'fop-verify-table-size file)
1194 (dump-word (fasl-output-table-free file) file)
1196 #!+sb-dyncount
1197 (let ((info (sb!c::ir2-component-dyncount-info (component-info component))))
1198 (when info
1199 (fasl-validate-structure info file)))
1201 (let ((code-handle (dump-code-object component
1202 code-segment
1203 code-length
1204 fixups
1205 file))
1206 (2comp (component-info component)))
1208 (dolist (entry (sb!c::ir2-component-entries 2comp))
1209 (let ((entry-handle (dump-one-entry entry code-handle file)))
1210 (setf (gethash entry (fasl-output-entry-table file)) entry-handle)
1211 (let ((old (gethash entry (fasl-output-patch-table file))))
1212 (when old
1213 (dolist (patch old)
1214 (dump-alter-code-object (car patch)
1215 (cdr patch)
1216 entry-handle
1217 file))
1218 (remhash entry (fasl-output-patch-table file)))))))
1219 (values))
1221 (defun dump-push-previously-dumped-fun (fun fasl-output)
1222 (declare (type sb!c::clambda fun))
1223 (let ((handle (gethash (sb!c::leaf-info fun)
1224 (fasl-output-entry-table fasl-output))))
1225 (aver handle)
1226 (dump-push handle fasl-output))
1227 (values))
1229 ;;; Dump a FOP-FUNCALL to call an already-dumped top level lambda at
1230 ;;; load time.
1231 (defun fasl-dump-toplevel-lambda-call (fun fasl-output)
1232 (declare (type sb!c::clambda fun))
1233 (dump-push-previously-dumped-fun fun fasl-output)
1234 (dump-fop 'fop-funcall-for-effect fasl-output)
1235 (dump-byte 0 fasl-output)
1236 (values))
1238 ;;; Compute the correct list of DEBUG-SOURCE structures and backpatch
1239 ;;; all of the dumped DEBUG-INFO structures. We clear the
1240 ;;; FASL-OUTPUT-DEBUG-INFO, so that subsequent components with
1241 ;;; different source info may be dumped.
1242 (defun fasl-dump-source-info (info fasl-output)
1243 (declare (type sb!c::source-info info))
1244 (let ((res (sb!c::debug-source-for-info info))
1245 (*dump-only-valid-structures* nil))
1246 ;; Zero out the timestamps to get reproducible fasls.
1247 #+sb-xc-host (setf (sb!c::debug-source-created res) 0
1248 (sb!c::debug-source-compiled res) 0)
1249 (dump-object res fasl-output)
1250 (let ((res-handle (dump-pop fasl-output)))
1251 (dolist (info-handle (fasl-output-debug-info fasl-output))
1252 (dump-push res-handle fasl-output)
1253 (dump-fop 'fop-structset fasl-output)
1254 (dump-word info-handle fasl-output)
1255 (macrolet ((debug-info-source-index ()
1256 (let ((dd (find-defstruct-description 'sb!c::debug-info)))
1257 (dsd-index (find 'source (dd-slots dd)
1258 :key #'dsd-name :test 'string=)))))
1259 (dump-word (debug-info-source-index) fasl-output)))
1260 #+sb-xc-host
1261 (progn
1262 (dump-push res-handle fasl-output)
1263 (dump-fop 'fop-note-debug-source fasl-output))))
1264 (setf (fasl-output-debug-info fasl-output) nil)
1265 (values))
1267 ;;;; dumping structures
1269 ;; Having done nothing more than load all files in obj/from-host, the
1270 ;; cross-compiler running under any host Lisp begins life able to access
1271 ;; SBCL-format metadata for any structure that is a subtype of STRUCTURE!OBJECT.
1272 ;; But if it learns a layout by cross-compiling a DEFSTRUCT, that's ok too.
1273 (defun dump-structure (struct file)
1274 (when (and *dump-only-valid-structures*
1275 (not (gethash struct (fasl-output-valid-structures file))))
1276 (error "attempt to dump invalid structure:~% ~S~%How did this happen?"
1277 struct))
1278 (note-potential-circularity struct file)
1279 (do* ((length (%instance-length struct))
1280 (layout (%instance-layout struct))
1281 (bitmap (layout-bitmap layout))
1282 (circ (fasl-output-circularity-table file))
1283 (index sb!vm:instance-data-start (1+ index)))
1284 ((>= index length)
1285 (dump-non-immediate-object layout file)
1286 (dump-fop 'fop-struct file length))
1287 (let* ((obj (if (logbitp index bitmap)
1288 (%raw-instance-ref/word struct index)
1289 (%instance-ref struct index)))
1290 (ref (gethash obj circ)))
1291 (sub-dump-object (cond (ref
1292 (push (make-circularity :type :struct-set
1293 :object struct
1294 :index index
1295 :value obj
1296 :enclosing-object ref)
1297 *circularities-detected*)
1298 nil)
1299 (t obj))
1300 file))))
1302 (defun dump-layout (obj file)
1303 (when (layout-invalid obj)
1304 (compiler-error "attempt to dump reference to obsolete class: ~S"
1305 (layout-classoid obj)))
1306 (let ((name (classoid-name (layout-classoid obj))))
1307 ;; Q: Shouldn't we aver that NAME is the proper name for its classoid?
1308 (unless name
1309 (compiler-error "dumping anonymous layout: ~S" obj))
1310 ;; The target lisp can save some space in fasls (sometimes),
1311 ;; but the cross-compiler can't because we need to construct the
1312 ;; cold representation of all layouts, not reference host layouts.
1313 #-sb-xc-host
1314 (let ((fop (known-layout-fop name)))
1315 (when fop
1316 (return-from dump-layout (dump-byte fop file))))
1317 (dump-object name file))
1318 (sub-dump-object (layout-inherits obj) file)
1319 (sub-dump-object (layout-depthoid obj) file)
1320 (sub-dump-object (layout-length obj) file)
1321 (sub-dump-object (layout-bitmap obj) file)
1322 (dump-fop 'fop-layout file))