1.0.31.12: use global lexicals for world-lock and finalizers
[sbcl/pkhuong.git] / src / compiler / dump.lisp
blob55b48a174026d36d11e8e5fc2e261e7411dce3db
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 inhibit table access when dumping forms to be read by the
95 ;;; cold loader
96 (defvar *cold-load-dump* nil)
98 ;;; used to turn off the structure validation during dumping of source
99 ;;; info
100 (defvar *dump-only-valid-structures* t)
101 ;;;; utilities
103 ;;; Write the byte B to the specified FASL-OUTPUT stream.
104 (defun dump-byte (b fasl-output)
105 (declare (type (unsigned-byte 8) b) (type fasl-output fasl-output))
106 (write-byte b (fasl-output-stream fasl-output)))
108 ;; Dump a word-sized integer.
109 (defun dump-word (num fasl-output)
110 (declare (type sb!vm:word num))
111 (declare (type fasl-output fasl-output))
112 (let ((stream (fasl-output-stream fasl-output)))
113 (dotimes (i sb!vm:n-word-bytes)
114 (write-byte (ldb (byte 8 (* 8 i)) num) stream))))
116 ;; Dump a 32-bit integer.
117 (defun dump-unsigned-byte-32 (num fasl-output)
118 (declare (type sb!vm:word num))
119 (declare (type fasl-output fasl-output))
120 (let ((stream (fasl-output-stream fasl-output)))
121 (dotimes (i 4)
122 (write-byte (ldb (byte 8 (* 8 i)) num) stream))))
124 ;;; Dump NUM to the fasl stream, represented by N bytes. This works
125 ;;; for either signed or unsigned integers. There's no range checking
126 ;;; -- if you don't specify enough bytes for the number to fit, this
127 ;;; function cheerfully outputs the low bytes.
128 (defun dump-integer-as-n-bytes (num bytes fasl-output)
129 (declare (integer num) (type index bytes))
130 (declare (type fasl-output fasl-output))
131 (do ((n num (ash n -8))
132 (i bytes (1- i)))
133 ((= i 0))
134 (declare (type index i))
135 (dump-byte (logand n #xff) fasl-output))
136 (values))
138 ;;; Setting this variable to an (UNSIGNED-BYTE 32) value causes
139 ;;; DUMP-FOP to use it as a counter and emit a FOP-NOP4 with the
140 ;;; counter value before every ordinary fop. This can make it easier
141 ;;; to follow the progress of LOAD-AS-FASL when
142 ;;; debugging/testing/experimenting.
143 #!+sb-show (defvar *fop-nop4-count* nil)
144 #!+sb-show (declaim (type (or (unsigned-byte 32) null) *fop-nop4-count*))
146 ;;; Dump the FOP code for the named FOP to the specified FASL-OUTPUT.
148 ;;; FIXME: This should be a function, with a compiler macro expansion
149 ;;; for the common constant-FS case. (Among other things, that'll stop
150 ;;; it from EVALing ,FILE multiple times.)
152 ;;; FIXME: Compiler macros, frozen classes, inlining, and similar
153 ;;; optimizations should be conditional on #!+SB-FROZEN.
154 (defmacro dump-fop (fs file)
155 (let* ((fs (eval fs))
156 (val (get fs 'fop-code)))
157 (if val
158 `(progn
159 #!+sb-show
160 (when *fop-nop4-count*
161 (dump-byte ,(get 'fop-nop4 'fop-code) ,file)
162 (dump-integer-as-n-bytes (mod (incf *fop-nop4-count*) (expt 2 32))
163 4 ,file))
164 (dump-byte ',val ,file))
165 (error "compiler bug: ~S is not a legal fasload operator." fs))))
167 ;;; Dump a FOP-CODE along with an integer argument, choosing the FOP
168 ;;; based on whether the argument will fit in a single byte.
170 ;;; FIXME: This, like DUMP-FOP, should be a function with a
171 ;;; compiler-macro expansion.
172 (defmacro dump-fop* (n byte-fop word-fop file)
173 (once-only ((n-n n)
174 (n-file file))
175 `(cond ((< ,n-n 256)
176 (dump-fop ',byte-fop ,n-file)
177 (dump-byte ,n-n ,n-file))
179 (dump-fop ',word-fop ,n-file)
180 (dump-word ,n-n ,n-file)))))
182 ;;; Push the object at table offset Handle on the fasl stack.
183 (defun dump-push (handle fasl-output)
184 (declare (type index handle) (type fasl-output fasl-output))
185 (dump-fop* handle fop-byte-push fop-push fasl-output)
186 (values))
188 ;;; Pop the object currently on the fasl stack top into the table, and
189 ;;; return the table index, incrementing the free pointer.
190 (defun dump-pop (fasl-output)
191 (prog1
192 (fasl-output-table-free fasl-output)
193 (dump-fop 'fop-pop fasl-output)
194 (incf (fasl-output-table-free fasl-output))))
196 ;;; If X is in File's EQUAL-TABLE, then push the object and return T,
197 ;;; otherwise NIL. If *COLD-LOAD-DUMP* is true, then do nothing and
198 ;;; return NIL.
199 (defun equal-check-table (x fasl-output)
200 (declare (type fasl-output fasl-output))
201 (unless *cold-load-dump*
202 (let ((handle (gethash x (fasl-output-equal-table fasl-output))))
203 (cond
204 (handle (dump-push handle fasl-output) t)
205 (t nil)))))
206 (defun string-check-table (x fasl-output)
207 (declare (type fasl-output fasl-output)
208 (type string x))
209 (unless *cold-load-dump*
210 (let ((handle (cdr (assoc
211 #+sb-xc-host 'base-char ; for repeatable xc fasls
212 #-sb-xc-host (array-element-type x)
213 (gethash x (fasl-output-equal-table fasl-output))))))
214 (cond
215 (handle (dump-push handle fasl-output) t)
216 (t nil)))))
218 ;;; These functions are called after dumping an object to save the
219 ;;; object in the table. The object (also passed in as X) must already
220 ;;; be on the top of the FOP stack. If *COLD-LOAD-DUMP* is true, then
221 ;;; we don't do anything.
222 (defun eq-save-object (x fasl-output)
223 (declare (type fasl-output fasl-output))
224 (unless *cold-load-dump*
225 (let ((handle (dump-pop fasl-output)))
226 (setf (gethash x (fasl-output-eq-table fasl-output)) handle)
227 (dump-push handle fasl-output)))
228 (values))
229 (defun equal-save-object (x fasl-output)
230 (declare (type fasl-output fasl-output))
231 (unless *cold-load-dump*
232 (let ((handle (dump-pop fasl-output)))
233 (setf (gethash x (fasl-output-equal-table fasl-output)) handle)
234 (setf (gethash x (fasl-output-eq-table fasl-output)) handle)
235 (dump-push handle fasl-output)))
236 (values))
237 (defun string-save-object (x fasl-output)
238 (declare (type fasl-output fasl-output)
239 (type string x))
240 (unless *cold-load-dump*
241 (let ((handle (dump-pop fasl-output)))
242 (push (cons #+sb-xc-host 'base-char ; repeatable xc fasls
243 #-sb-xc-host (array-element-type x)
244 handle)
245 (gethash x (fasl-output-equal-table fasl-output)))
246 (setf (gethash x (fasl-output-eq-table fasl-output)) handle)
247 (dump-push handle fasl-output)))
248 (values))
249 ;;; Record X in File's CIRCULARITY-TABLE unless *COLD-LOAD-DUMP* is
250 ;;; true. This is called on objects that we are about to dump might
251 ;;; have a circular path through them.
253 ;;; The object must not currently be in this table, since the dumper
254 ;;; should never be recursively called on a circular reference.
255 ;;; Instead, the dumping function must detect the circularity and
256 ;;; arrange for the dumped object to be patched.
257 (defun note-potential-circularity (x fasl-output)
258 (unless *cold-load-dump*
259 (let ((circ (fasl-output-circularity-table fasl-output)))
260 (aver (not (gethash x circ)))
261 (setf (gethash x circ) x)))
262 (values))
264 ;;; Dump FORM to a fasl file so that it evaluated at load time in normal
265 ;;; load and at cold-load time in cold load. This is used to dump package
266 ;;; frobbing forms.
267 (defun fasl-dump-cold-load-form (form fasl-output)
268 (declare (type fasl-output fasl-output))
269 (dump-fop 'fop-normal-load fasl-output)
270 (let ((*cold-load-dump* t))
271 (dump-object form fasl-output))
272 (dump-fop 'fop-eval-for-effect fasl-output)
273 (dump-fop 'fop-maybe-cold-load fasl-output)
274 (values))
276 ;;;; opening and closing fasl files
278 ;;; Open a fasl file, write its header, and return a FASL-OUTPUT
279 ;;; object for dumping to it. Some human-readable information about
280 ;;; the source code is given by the string WHERE.
281 (defun open-fasl-output (name where)
282 (declare (type pathname name))
283 (flet ((fasl-write-string (string stream)
284 ;; SB-EXT:STRING-TO-OCTETS is not available while cross-compiling
285 #+sb-xc-host
286 (loop for char across string
287 do (let ((code (char-code char)))
288 (unless (<= 0 code 127)
289 (setf char #\?))
290 (write-byte code stream)))
291 ;; UTF-8 is safe to use, because +FASL-HEADER-STRING-STOP-CHAR-CODE+
292 ;; may not appear in UTF-8 encoded bytes
293 #-sb-xc-host
294 (write-sequence (string-to-octets string :external-format :utf-8)
295 stream)))
296 (let* ((stream (open name
297 :direction :output
298 :if-exists :supersede
299 :element-type 'sb!assem:assembly-unit))
300 (res (make-fasl-output :stream stream)))
301 ;; Before the actual FASL header, write a shebang line using the current
302 ;; runtime path, so our fasls can be executed directly from the shell.
303 (when *runtime-pathname*
304 (fasl-write-string
305 (format nil "#!~A --script~%"
306 (native-namestring *runtime-pathname* :as-file t))
307 stream))
308 ;; Begin the header with the constant machine-readable (and
309 ;; semi-human-readable) string which is used to identify fasl files.
310 (fasl-write-string *fasl-header-string-start-string* stream)
311 ;; The constant string which begins the header is followed by
312 ;; arbitrary human-readable text, terminated by
313 ;; +FASL-HEADER-STRING-STOP-CHAR-CODE+.
314 (fasl-write-string
315 (with-standard-io-syntax
316 (let ((*print-readably* nil)
317 (*print-pretty* nil))
318 (format nil
319 "~% ~
320 compiled from ~S~% ~
321 using ~A version ~A~%"
322 where
323 (sb!xc:lisp-implementation-type)
324 (sb!xc:lisp-implementation-version))))
325 stream)
326 (dump-byte +fasl-header-string-stop-char-code+ res)
327 ;; Finish the header by outputting fasl file implementation,
328 ;; version, and key *FEATURES*.
329 (flet ((dump-counted-string (string)
330 ;; The count is dumped as a 32-bit unsigned-byte even on 64-bit
331 ;; platforms. This ensures that a x86-64 SBCL can gracefully
332 ;; detect an error when trying to read a x86 fasl, instead
333 ;; of choking on a ridiculously long counted string.
334 ;; -- JES, 2005-12-30
335 (dump-unsigned-byte-32 (length string) res)
336 (dotimes (i (length string))
337 (dump-byte (char-code (aref string i)) res))))
338 (dump-counted-string (symbol-name +backend-fasl-file-implementation+))
339 (dump-word +fasl-file-version+ res)
340 (dump-counted-string (sb!xc:lisp-implementation-version))
341 (dump-counted-string *features-affecting-fasl-format*))
342 res)))
344 ;;; Close the specified FASL-OUTPUT, aborting the write if ABORT-P.
345 (defun close-fasl-output (fasl-output abort-p)
346 (declare (type fasl-output fasl-output))
348 (unless abort-p
349 ;; sanity checks
350 (aver (zerop (hash-table-count (fasl-output-patch-table fasl-output))))
351 ;; End the group.
352 (dump-fop 'fop-verify-empty-stack fasl-output)
353 (dump-fop 'fop-verify-table-size fasl-output)
354 (dump-word (fasl-output-table-free fasl-output)
355 fasl-output)
356 (dump-fop 'fop-end-group fasl-output))
358 ;; That's all, folks.
359 (close (fasl-output-stream fasl-output) :abort abort-p)
360 (values))
362 ;;;; main entries to object dumping
364 ;;; This function deals with dumping objects that are complex enough
365 ;;; so that we want to cache them in the table, rather than repeatedly
366 ;;; dumping them. If the object is in the EQ-TABLE, then we push it,
367 ;;; otherwise, we do a type dispatch to a type specific dumping
368 ;;; function. The type specific branches do any appropriate
369 ;;; EQUAL-TABLE check and table entry.
371 ;;; When we go to dump the object, we enter it in the CIRCULARITY-TABLE.
372 (defun dump-non-immediate-object (x file)
373 (let ((index (gethash x (fasl-output-eq-table file))))
374 (cond ((and index (not *cold-load-dump*))
375 (dump-push index file))
377 (typecase x
378 (symbol (dump-symbol x file))
379 (list
380 ;; KLUDGE: The code in this case has been hacked
381 ;; to match Douglas Crosher's quick fix to CMU CL
382 ;; (on cmucl-imp 1999-12-27), applied in sbcl-0.6.8.11
383 ;; with help from Martin Atzmueller. This is not an
384 ;; ideal solution; to quote DTC,
385 ;; The compiler locks up trying to coalesce the
386 ;; constant lists. The hack below will disable the
387 ;; coalescing of lists while dumping and allows
388 ;; the code to compile. The real fix would be to
389 ;; take a little more care while dumping these.
390 ;; So if better list coalescing is needed, start here.
391 ;; -- WHN 2000-11-07
392 (if (maybe-cyclic-p x)
393 (progn
394 (dump-list x file)
395 (eq-save-object x file))
396 (unless (equal-check-table x file)
397 (dump-list x file)
398 (equal-save-object x file))))
399 (layout
400 (dump-layout x file)
401 (eq-save-object x file))
402 (instance
403 (dump-structure x file)
404 (eq-save-object x file))
405 (array
406 ;; DUMP-ARRAY (and its callees) are responsible for
407 ;; updating the EQ and EQUAL hash tables.
408 (dump-array x file))
409 (number
410 (unless (equal-check-table x file)
411 (etypecase x
412 (ratio (dump-ratio x file))
413 (complex (dump-complex x file))
414 (float (dump-float x file))
415 (integer (dump-integer x file)))
416 (equal-save-object x file)))
418 ;; This probably never happens, since bad things tend to
419 ;; be detected during IR1 conversion.
420 (error "This object cannot be dumped into a fasl file:~% ~S"
421 x))))))
422 (values))
424 ;;; Dump an object of any type by dispatching to the correct
425 ;;; type-specific dumping function. We pick off immediate objects,
426 ;;; symbols and magic lists here. Other objects are handled by
427 ;;; DUMP-NON-IMMEDIATE-OBJECT.
429 ;;; This is the function used for recursive calls to the fasl dumper.
430 ;;; We don't worry about creating circularities here, since it is
431 ;;; assumed that there is a top level call to DUMP-OBJECT.
432 (defun sub-dump-object (x file)
433 (cond ((listp x)
434 (if x
435 (dump-non-immediate-object x file)
436 (dump-fop 'fop-empty-list file)))
437 ((symbolp x)
438 (if (eq x t)
439 (dump-fop 'fop-truth file)
440 (dump-non-immediate-object x file)))
441 ((fixnump x) (dump-integer x file))
442 ((characterp x) (dump-character x file))
444 (dump-non-immediate-object x file))))
446 ;;; Dump stuff to backpatch already dumped objects. INFOS is the list
447 ;;; of CIRCULARITY structures describing what to do. The patching FOPs
448 ;;; take the value to store on the stack. We compute this value by
449 ;;; fetching the enclosing object from the table, and then CDR'ing it
450 ;;; if necessary.
451 (defun dump-circularities (infos file)
452 (let ((table (fasl-output-eq-table file)))
453 (dolist (info infos)
455 (let* ((value (circularity-value info))
456 (enclosing (circularity-enclosing-object info)))
457 (dump-push (gethash enclosing table) file)
458 (unless (eq enclosing value)
459 (do ((current enclosing (cdr current))
460 (i 0 (1+ i)))
461 ((eq current value)
462 (dump-fop 'fop-nthcdr file)
463 (dump-word i file))
464 (declare (type index i)))))
466 (ecase (circularity-type info)
467 (:rplaca (dump-fop 'fop-rplaca file))
468 (:rplacd (dump-fop 'fop-rplacd file))
469 (:svset (dump-fop 'fop-svset file))
470 (:struct-set (dump-fop 'fop-structset file)))
471 (dump-word (gethash (circularity-object info) table) file)
472 (dump-word (circularity-index info) file))))
474 ;;; Set up stuff for circularity detection, then dump an object. All
475 ;;; shared and circular structure will be exactly preserved within a
476 ;;; single call to DUMP-OBJECT. Sharing between objects dumped by
477 ;;; separate calls is only preserved when convenient.
479 ;;; We peek at the object type so that we only pay the circular
480 ;;; detection overhead on types of objects that might be circular.
481 (defun dump-object (x file)
482 (if (compound-object-p x)
483 (let ((*circularities-detected* ())
484 (circ (fasl-output-circularity-table file)))
485 (clrhash circ)
486 (sub-dump-object x file)
487 (when *circularities-detected*
488 (dump-circularities *circularities-detected* file)
489 (clrhash circ)))
490 (sub-dump-object x file)))
492 ;;;; LOAD-TIME-VALUE and MAKE-LOAD-FORM support
494 ;;; Emit a funcall of the function and return the handle for the
495 ;;; result.
496 (defun fasl-dump-load-time-value-lambda (fun file)
497 (declare (type sb!c::clambda fun) (type fasl-output file))
498 (let ((handle (gethash (sb!c::leaf-info fun)
499 (fasl-output-entry-table file))))
500 (aver handle)
501 (dump-push handle file)
502 (dump-fop 'fop-funcall file)
503 (dump-byte 0 file))
504 (dump-pop file))
506 ;;; Return T iff CONSTANT has already been dumped. It's been dumped if
507 ;;; it's in the EQ table.
509 ;;; Note: historically (1) the above comment was "T iff ... has not been dumped",
510 ;;; (2) the test was was also true if the constant had been validated / was in
511 ;;; the valid objects table. This led to substructures occasionally skipping the
512 ;;; validation, and hence failing the "must have been validated" test.
513 (defun fasl-constant-already-dumped-p (constant file)
514 (and (gethash constant (fasl-output-eq-table file)) t))
516 ;;; Use HANDLE whenever we try to dump CONSTANT. HANDLE should have been
517 ;;; returned earlier by FASL-DUMP-LOAD-TIME-VALUE-LAMBDA.
518 (defun fasl-note-handle-for-constant (constant handle file)
519 (let ((table (fasl-output-eq-table file)))
520 (when (gethash constant table)
521 (error "~S already dumped?" constant))
522 (setf (gethash constant table) handle))
523 (values))
525 ;;; Note that the specified structure can just be dumped by
526 ;;; enumerating the slots.
527 (defun fasl-validate-structure (structure file)
528 (setf (gethash structure (fasl-output-valid-structures file)) t)
529 (values))
531 ;;;; number dumping
533 (defun dump-ratio (x file)
534 (sub-dump-object (numerator x) file)
535 (sub-dump-object (denominator x) file)
536 (dump-fop 'fop-ratio file))
538 (defun dump-integer (n file)
539 (typecase n
540 ((signed-byte 8)
541 (dump-fop 'fop-byte-integer file)
542 (dump-byte (logand #xFF n) file))
543 ((unsigned-byte #.(1- sb!vm:n-word-bits))
544 (dump-fop 'fop-word-integer file)
545 (dump-word n file))
546 ((signed-byte #.sb!vm:n-word-bits)
547 (dump-fop 'fop-word-integer file)
548 (dump-integer-as-n-bytes n #.sb!vm:n-word-bytes file))
550 (let ((bytes (ceiling (1+ (integer-length n)) 8)))
551 (dump-fop* bytes fop-small-integer fop-integer file)
552 (dump-integer-as-n-bytes n bytes file)))))
554 (defun dump-float (x file)
555 (etypecase x
556 (single-float
557 (dump-fop 'fop-single-float file)
558 (dump-integer-as-n-bytes (single-float-bits x) 4 file))
559 (double-float
560 (dump-fop 'fop-double-float file)
561 (let ((x x))
562 (declare (double-float x))
563 (dump-integer-as-n-bytes (double-float-low-bits x) 4 file)
564 (dump-integer-as-n-bytes (double-float-high-bits x) 4 file)))
565 #!+long-float
566 (long-float
567 (dump-fop 'fop-long-float file)
568 (dump-long-float x file))))
570 (defun dump-complex-single-float (re im file)
571 (declare (single-float re im))
572 (dump-fop 'fop-complex-single-float file)
573 (dump-integer-as-n-bytes (single-float-bits re) 4 file)
574 (dump-integer-as-n-bytes (single-float-bits im) 4 file))
576 (defun dump-complex-double-float (re im file)
577 (declare (double-float re im))
578 (dump-fop 'fop-complex-double-float file)
579 (dump-integer-as-n-bytes (double-float-low-bits re) 4 file)
580 (dump-integer-as-n-bytes (double-float-high-bits re) 4 file)
581 (dump-integer-as-n-bytes (double-float-low-bits im) 4 file)
582 (dump-integer-as-n-bytes (double-float-high-bits im) 4 file))
584 (defun dump-complex-rational (re im file)
585 (sub-dump-object re file)
586 (sub-dump-object im file)
587 (dump-fop 'fop-complex file))
589 #+sb-xc-host
590 (defun dump-complex (x file)
591 (let ((re (realpart x))
592 (im (imagpart x)))
593 (cond ((and (typep re 'single-float)
594 (typep im 'single-float))
595 (dump-complex-single-float re im file))
596 ((and (typep re 'double-float)
597 (typep im 'double-float))
598 (dump-complex-double-float re im file))
599 ((and (typep re 'rational)
600 (typep im 'rational))
601 (dump-complex-rational re im file))
603 (bug "Complex number too complex: ~S" x)))))
605 #-sb-xc-host
606 (defun dump-complex (x file)
607 (typecase x
608 ((complex single-float)
609 (dump-complex-single-float (realpart x) (imagpart x) file))
610 ((complex double-float)
611 (dump-complex-double-float (realpart x) (imagpart x) file))
612 #!+long-float
613 ((complex long-float)
614 (dump-fop 'fop-complex-long-float file)
615 (dump-long-float (realpart x) file)
616 (dump-long-float (imagpart x) file))
618 (dump-complex-rational (realpart x) (imagpart x) file))))
620 ;;;; symbol dumping
622 ;;; Return the table index of PKG, adding the package to the table if
623 ;;; necessary. During cold load, we read the string as a normal string
624 ;;; so that we can do the package lookup at cold load time.
626 ;;; FIXME: Despite the parallelism in names, the functionality of
627 ;;; this function is not parallel to other functions DUMP-FOO, e.g.
628 ;;; DUMP-SYMBOL and DUMP-LIST. The mapping between names and behavior
629 ;;; should be made more consistent.
630 (declaim (ftype (function (package fasl-output) index) dump-package))
631 (defun dump-package (pkg file)
632 (declare (inline assoc))
633 (cond ((cdr (assoc pkg (fasl-output-packages file) :test #'eq)))
635 (unless *cold-load-dump*
636 (dump-fop 'fop-normal-load file))
637 #+sb-xc-host
638 (dump-simple-base-string
639 (coerce (package-name pkg) 'simple-base-string)
640 file)
641 #-sb-xc-host
642 (#!+sb-unicode dump-simple-character-string
643 #!-sb-unicode dump-simple-base-string
644 (coerce (package-name pkg) '(simple-array character (*)))
645 file)
646 (dump-fop 'fop-package file)
647 (unless *cold-load-dump*
648 (dump-fop 'fop-maybe-cold-load file))
649 (let ((entry (dump-pop file)))
650 (push (cons pkg entry) (fasl-output-packages file))
651 entry))))
653 ;;; dumper for lists
655 ;;; Dump a list, setting up patching information when there are
656 ;;; circularities. We scan down the list, checking for CDR and CAR
657 ;;; circularities.
659 ;;; If there is a CDR circularity, we terminate the list with NIL and
660 ;;; make a CIRCULARITY notation for the CDR of the previous cons.
662 ;;; If there is no CDR circularity, then we mark the current cons and
663 ;;; check for a CAR circularity. When there is a CAR circularity, we
664 ;;; make the CAR NIL initially, arranging for the current cons to be
665 ;;; patched later.
667 ;;; Otherwise, we recursively call the dumper to dump the current
668 ;;; element.
670 ;;; Marking of the conses is inhibited when *COLD-LOAD-DUMP* is true.
671 ;;; This inhibits all circularity detection.
672 (defun dump-list (list file)
673 (aver (and list
674 (not (gethash list (fasl-output-circularity-table file)))))
675 (do* ((l list (cdr l))
676 (n 0 (1+ n))
677 (circ (fasl-output-circularity-table file)))
678 ((atom l)
679 (cond ((null l)
680 (terminate-undotted-list n file))
682 (sub-dump-object l file)
683 (terminate-dotted-list n file))))
684 (declare (type index n))
685 (let ((ref (gethash l circ)))
686 (when ref
687 (push (make-circularity :type :rplacd
688 :object list
689 :index (1- n)
690 :value l
691 :enclosing-object ref)
692 *circularities-detected*)
693 (terminate-undotted-list n file)
694 (return)))
696 (unless *cold-load-dump*
697 (setf (gethash l circ) list))
699 (let* ((obj (car l))
700 (ref (gethash obj circ)))
701 (cond (ref
702 (push (make-circularity :type :rplaca
703 :object list
704 :index n
705 :value obj
706 :enclosing-object ref)
707 *circularities-detected*)
708 (sub-dump-object nil file))
710 (sub-dump-object obj file))))))
712 (defun terminate-dotted-list (n file)
713 (declare (type index n) (type fasl-output file))
714 (case n
715 (1 (dump-fop 'fop-list*-1 file))
716 (2 (dump-fop 'fop-list*-2 file))
717 (3 (dump-fop 'fop-list*-3 file))
718 (4 (dump-fop 'fop-list*-4 file))
719 (5 (dump-fop 'fop-list*-5 file))
720 (6 (dump-fop 'fop-list*-6 file))
721 (7 (dump-fop 'fop-list*-7 file))
722 (8 (dump-fop 'fop-list*-8 file))
723 (t (do ((nn n (- nn 255)))
724 ((< nn 256)
725 (dump-fop 'fop-list* file)
726 (dump-byte nn file))
727 (declare (type index nn))
728 (dump-fop 'fop-list* file)
729 (dump-byte 255 file)))))
731 ;;; If N > 255, must build list with one LIST operator, then LIST*
732 ;;; operators.
734 (defun terminate-undotted-list (n file)
735 (declare (type index n) (type fasl-output file))
736 (case n
737 (1 (dump-fop 'fop-list-1 file))
738 (2 (dump-fop 'fop-list-2 file))
739 (3 (dump-fop 'fop-list-3 file))
740 (4 (dump-fop 'fop-list-4 file))
741 (5 (dump-fop 'fop-list-5 file))
742 (6 (dump-fop 'fop-list-6 file))
743 (7 (dump-fop 'fop-list-7 file))
744 (8 (dump-fop 'fop-list-8 file))
745 (t (cond ((< n 256)
746 (dump-fop 'fop-list file)
747 (dump-byte n file))
748 (t (dump-fop 'fop-list file)
749 (dump-byte 255 file)
750 (do ((nn (- n 255) (- nn 255)))
751 ((< nn 256)
752 (dump-fop 'fop-list* file)
753 (dump-byte nn file))
754 (declare (type index nn))
755 (dump-fop 'fop-list* file)
756 (dump-byte 255 file)))))))
758 ;;;; array dumping
760 ;;; Dump the array thing.
761 (defun dump-array (x file)
762 (if (vectorp x)
763 (dump-vector x file)
764 (dump-multi-dim-array x file)))
766 ;;; Dump the vector object. If it's not simple, then actually dump a
767 ;;; simple version of it. But we enter the original in the EQ or EQUAL
768 ;;; tables.
769 (defun dump-vector (x file)
770 (let ((simple-version (if (array-header-p x)
771 (coerce x `(simple-array
772 ,(array-element-type x)
773 (*)))
774 x)))
775 (typecase simple-version
776 #+sb-xc-host
777 (simple-string
778 (unless (string-check-table x file)
779 (dump-simple-base-string simple-version file)
780 (string-save-object x file)))
781 #-sb-xc-host
782 (simple-base-string
783 (unless (string-check-table x file)
784 (dump-simple-base-string simple-version file)
785 (string-save-object x file)))
786 #-sb-xc-host
787 ((simple-array character (*))
788 #!+sb-unicode
789 (unless (string-check-table x file)
790 (dump-simple-character-string simple-version file)
791 (string-save-object x file))
792 #!-sb-unicode
793 (bug "how did we get here?"))
794 (simple-vector
795 (dump-simple-vector simple-version file)
796 (eq-save-object x file))
797 ((simple-array single-float (*))
798 (dump-single-float-vector simple-version file)
799 (eq-save-object x file))
800 ((simple-array double-float (*))
801 (dump-double-float-vector simple-version file)
802 (eq-save-object x file))
803 #!+long-float
804 ((simple-array long-float (*))
805 (dump-long-float-vector simple-version file)
806 (eq-save-object x file))
807 ((simple-array (complex single-float) (*))
808 (dump-complex-single-float-vector simple-version file)
809 (eq-save-object x file))
810 ((simple-array (complex double-float) (*))
811 (dump-complex-double-float-vector simple-version file)
812 (eq-save-object x file))
813 #!+long-float
814 ((simple-array (complex long-float) (*))
815 (dump-complex-long-float-vector simple-version file)
816 (eq-save-object x file))
818 (dump-i-vector simple-version file)
819 (eq-save-object x file)))))
821 ;;; Dump a SIMPLE-VECTOR, handling any circularities.
822 (defun dump-simple-vector (v file)
823 (declare (type simple-vector v) (type fasl-output file))
824 (note-potential-circularity v file)
825 (do ((index 0 (1+ index))
826 (length (length v))
827 (circ (fasl-output-circularity-table file)))
828 ((= index length)
829 (dump-fop* length fop-small-vector fop-vector file))
830 (let* ((obj (aref v index))
831 (ref (gethash obj circ)))
832 (cond (ref
833 (push (make-circularity :type :svset
834 :object v
835 :index index
836 :value obj
837 :enclosing-object ref)
838 *circularities-detected*)
839 (sub-dump-object nil file))
841 (sub-dump-object obj file))))))
843 ;;; In the grand scheme of things I don't pretend to understand any
844 ;;; more how this works, or indeed whether. But to write out specialized
845 ;;; vectors in the same format as fop-int-vector expects to read them
846 ;;; we need to be target-endian. dump-integer-as-n-bytes always writes
847 ;;; little-endian (which is correct for all other integers) so for a bigendian
848 ;;; target we need to swap octets -- CSR, after DB
850 (defun octet-swap (word bits)
851 "BITS must be a multiple of 8"
852 (do ((input word (ash input -8))
853 (output 0 (logior (ash output 8) (logand input #xff)))
854 (bits bits (- bits 8)))
855 ((<= bits 0) output)))
857 (defun dump-i-vector (vec file &key data-only)
858 (declare (type (simple-array * (*)) vec))
859 (let ((len (length vec)))
860 (labels ((dump-unsigned-vector (size bytes)
861 (unless data-only
862 (dump-fop 'fop-int-vector file)
863 (dump-word len file)
864 (dump-byte size file))
865 ;; The case which is easy to handle in a portable way is when
866 ;; the element size is a multiple of the output byte size, and
867 ;; happily that's the only case we need to be portable. (The
868 ;; cross-compiler has to output debug information (including
869 ;; (SIMPLE-ARRAY (UNSIGNED-BYTE 8) *).) The other cases are only
870 ;; needed in the target SBCL, so we let them be handled with
871 ;; unportable bit bashing.
872 (cond ((>= size 7) ; easy cases
873 (multiple-value-bind (floor rem) (floor size 8)
874 (aver (or (zerop rem) (= rem 7)))
875 (when (= rem 7)
876 (setq size (1+ size))
877 (setq floor (1+ floor)))
878 (dovector (i vec)
879 (dump-integer-as-n-bytes
880 (ecase sb!c:*backend-byte-order*
881 (:little-endian i)
882 (:big-endian (octet-swap i size)))
883 floor file))))
884 (t ; harder cases, not supported in cross-compiler
885 (dump-raw-bytes vec bytes file))))
886 (dump-signed-vector (size bytes)
887 ;; Note: Dumping specialized signed vectors isn't
888 ;; supported in the cross-compiler. (All cases here end
889 ;; up trying to call DUMP-RAW-BYTES, which isn't
890 ;; provided in the cross-compilation host, only on the
891 ;; target machine.)
892 (unless data-only
893 (dump-fop 'fop-signed-int-vector file)
894 (dump-word len file)
895 (dump-byte size file))
896 (dump-raw-bytes vec bytes file)))
897 (etypecase vec
898 #-sb-xc-host
899 ((simple-array nil (*))
900 (dump-unsigned-vector 0 0))
901 (simple-bit-vector
902 (dump-unsigned-vector 1 (ceiling len 8))) ; bits to bytes
903 ;; KLUDGE: This isn't the best way of expressing that the host
904 ;; may not have specializations for (unsigned-byte 2) and
905 ;; (unsigned-byte 4), which means that these types are
906 ;; type-equivalent to (simple-array (unsigned-byte 8) (*));
907 ;; the workaround is to remove them from the etypecase, since
908 ;; they can't be dumped from the cross-compiler anyway. --
909 ;; CSR, 2002-05-07
910 #-sb-xc-host
911 ((simple-array (unsigned-byte 2) (*))
912 (dump-unsigned-vector 2 (ceiling (ash len 1) 8))) ; bits to bytes
913 #-sb-xc-host
914 ((simple-array (unsigned-byte 4) (*))
915 (dump-unsigned-vector 4 (ceiling (ash len 2) 8))) ; bits to bytes
916 #-sb-xc-host
917 ((simple-array (unsigned-byte 7) (*))
918 (dump-unsigned-vector 7 len))
919 ((simple-array (unsigned-byte 8) (*))
920 (dump-unsigned-vector 8 len))
921 #-sb-xc-host
922 ((simple-array (unsigned-byte 15) (*))
923 (dump-unsigned-vector 15 (* 2 len)))
924 ((simple-array (unsigned-byte 16) (*))
925 (dump-unsigned-vector 16 (* 2 len)))
926 #-sb-xc-host
927 ((simple-array (unsigned-byte 31) (*))
928 (dump-unsigned-vector 31 (* 4 len)))
929 ((simple-array (unsigned-byte 32) (*))
930 (dump-unsigned-vector 32 (* 4 len)))
931 #-sb-xc-host
932 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
933 ((simple-array (unsigned-byte 63) (*))
934 (dump-unsigned-vector 63 (* 8 len)))
935 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
936 ((simple-array (unsigned-byte 64) (*))
937 (dump-unsigned-vector 64 (* 8 len)))
938 ((simple-array (signed-byte 8) (*))
939 (dump-signed-vector 8 len))
940 ((simple-array (signed-byte 16) (*))
941 (dump-signed-vector 16 (* 2 len)))
942 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
943 ((simple-array (unsigned-byte 29) (*))
944 (dump-signed-vector 29 (* 4 len)))
945 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
946 ((simple-array (signed-byte 30) (*))
947 (dump-signed-vector 30 (* 4 len)))
948 ((simple-array (signed-byte 32) (*))
949 (dump-signed-vector 32 (* 4 len)))
950 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
951 ((simple-array (unsigned-byte 60) (*))
952 (dump-signed-vector 60 (* 8 len)))
953 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
954 ((simple-array (signed-byte 61) (*))
955 (dump-signed-vector 61 (* 8 len)))
956 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
957 ((simple-array (signed-byte 64) (*))
958 (dump-signed-vector 64 (* 8 len)))))))
960 ;;; Dump characters and string-ish things.
962 (defun dump-character (char file)
963 (let ((code (sb!xc:char-code char)))
964 (cond
965 ((< code 256)
966 (dump-fop 'fop-short-character file)
967 (dump-byte code file))
969 (dump-fop 'fop-character file)
970 (dump-word code file)))))
972 (defun dump-base-chars-of-string (s fasl-output)
973 (declare #+sb-xc-host (type simple-string s)
974 #-sb-xc-host (type simple-base-string s)
975 (type fasl-output fasl-output))
976 (dovector (c s)
977 (dump-byte (sb!xc:char-code c) fasl-output))
978 (values))
981 ;;; Dump a SIMPLE-BASE-STRING.
982 (defun dump-simple-base-string (s file)
983 #+sb-xc-host (declare (type simple-string s))
984 #-sb-xc-host (declare (type simple-base-string s))
985 (dump-fop* (length s) fop-small-base-string fop-base-string file)
986 (dump-base-chars-of-string s file)
987 (values))
989 ;;; If we get here, it is assumed that the symbol isn't in the table,
990 ;;; but we are responsible for putting it there when appropriate. To
991 ;;; avoid too much special-casing, we always push the symbol in the
992 ;;; table, but don't record that we have done so if *COLD-LOAD-DUMP*
993 ;;; is true.
994 (defun dump-symbol (s file)
995 (declare (type fasl-output file))
996 (let* ((pname (symbol-name s))
997 (pname-length (length pname))
998 (pkg (symbol-package s)))
999 ;; see comment in genesis: we need this here for repeatable fasls
1000 #+sb-xc-host
1001 (multiple-value-bind (cl-symbol cl-status)
1002 (find-symbol (symbol-name s) sb!int:*cl-package*)
1003 (when (and (eq s cl-symbol)
1004 (eq cl-status :external))
1005 ;; special case, to work around possible xc host "design
1006 ;; choice" weirdness in COMMON-LISP package
1007 (setq pkg sb!int:*cl-package*)))
1009 (cond ((null pkg)
1010 (dump-fop* pname-length
1011 fop-uninterned-small-symbol-save
1012 fop-uninterned-symbol-save
1013 file))
1014 ;; CMU CL had FOP-SYMBOL-SAVE/FOP-SMALL-SYMBOL-SAVE fops which
1015 ;; used the current value of *PACKAGE*. Unfortunately that's
1016 ;; broken w.r.t. ANSI Common Lisp semantics, so those are gone
1017 ;; from SBCL.
1018 ;;((eq pkg *package*)
1019 ;; (dump-fop* pname-length
1020 ;; fop-small-symbol-save
1021 ;; fop-symbol-save file))
1022 ((eq pkg sb!int:*cl-package*)
1023 (dump-fop* pname-length
1024 fop-lisp-small-symbol-save
1025 fop-lisp-symbol-save
1026 file))
1027 ((eq pkg sb!int:*keyword-package*)
1028 (dump-fop* pname-length
1029 fop-keyword-small-symbol-save
1030 fop-keyword-symbol-save
1031 file))
1032 ((< pname-length 256)
1033 (dump-fop* (dump-package pkg file)
1034 fop-small-symbol-in-byte-package-save
1035 fop-small-symbol-in-package-save
1036 file)
1037 (dump-byte pname-length file))
1039 (dump-fop* (dump-package pkg file)
1040 fop-symbol-in-byte-package-save
1041 fop-symbol-in-package-save
1042 file)
1043 (dump-word pname-length file)))
1045 #+sb-xc-host (dump-base-chars-of-string pname file)
1046 #-sb-xc-host (#!+sb-unicode dump-characters-of-string
1047 #!-sb-unicode dump-base-chars-of-string
1048 pname file)
1050 (unless *cold-load-dump*
1051 (setf (gethash s (fasl-output-eq-table file))
1052 (fasl-output-table-free file)))
1054 (incf (fasl-output-table-free file)))
1056 (values))
1058 ;;;; component (function) dumping
1060 (defun dump-segment (segment code-length fasl-output)
1061 (declare (type sb!assem:segment segment)
1062 (type fasl-output fasl-output))
1063 (let* ((stream (fasl-output-stream fasl-output))
1064 (n-written (write-segment-contents segment stream)))
1065 ;; In CMU CL there was no enforced connection between the CODE-LENGTH
1066 ;; argument and the number of bytes actually written. I added this
1067 ;; assertion while trying to debug portable genesis. -- WHN 19990902
1068 (unless (= code-length n-written)
1069 (bug "code-length=~W, n-written=~W" code-length n-written)))
1070 (values))
1072 ;;; Dump all the fixups. Currently there are three flavors of fixup:
1073 ;;; - assembly routines: named by a symbol
1074 ;;; - foreign (C) symbols: named by a string
1075 ;;; - code object references: don't need a name.
1076 (defun dump-fixups (fixups fasl-output)
1077 (declare (list fixups) (type fasl-output fasl-output))
1078 (dolist (note fixups)
1079 (let* ((kind (fixup-note-kind note))
1080 (fixup (fixup-note-fixup note))
1081 (position (fixup-note-position note))
1082 (name (fixup-name fixup))
1083 (flavor (fixup-flavor fixup)))
1084 (dump-fop 'fop-normal-load fasl-output)
1085 (let ((*cold-load-dump* t))
1086 (dump-object kind fasl-output))
1087 (dump-fop 'fop-maybe-cold-load fasl-output)
1088 ;; Depending on the flavor, we may have various kinds of
1089 ;; noise before the position.
1090 (ecase flavor
1091 (:assembly-routine
1092 (aver (symbolp name))
1093 (dump-fop 'fop-normal-load fasl-output)
1094 (let ((*cold-load-dump* t))
1095 (dump-object name fasl-output))
1096 (dump-fop 'fop-maybe-cold-load fasl-output)
1097 (dump-fop 'fop-assembler-fixup fasl-output))
1098 ((:foreign :foreign-dataref)
1099 (aver (stringp name))
1100 (ecase flavor
1101 (:foreign
1102 (dump-fop 'fop-foreign-fixup fasl-output))
1103 #!+linkage-table
1104 (:foreign-dataref
1105 (dump-fop 'fop-foreign-dataref-fixup fasl-output)))
1106 (let ((len (length name)))
1107 (aver (< len 256)) ; (limit imposed by fop definition)
1108 (dump-byte len fasl-output)
1109 (dotimes (i len)
1110 (dump-byte (char-code (schar name i)) fasl-output))))
1111 (:code-object
1112 (aver (null name))
1113 (dump-fop 'fop-code-object-fixup fasl-output)))
1114 ;; No matter what the flavor, we'll always dump the position
1115 (dump-word position fasl-output)))
1116 (values))
1118 ;;; Dump out the constant pool and code-vector for component, push the
1119 ;;; result in the table, and return the offset.
1121 ;;; The only tricky thing is handling constant-pool references to
1122 ;;; functions. If we have already dumped the function, then we just
1123 ;;; push the code pointer. Otherwise, we must create back-patching
1124 ;;; information so that the constant will be set when the function is
1125 ;;; eventually dumped. This is a bit awkward, since we don't have the
1126 ;;; handle for the code object being dumped while we are dumping its
1127 ;;; constants.
1129 ;;; We dump trap objects in any unused slots or forward referenced slots.
1130 (defun dump-code-object (component
1131 code-segment
1132 code-length
1133 trace-table-as-list
1134 fixups
1135 fasl-output)
1137 (declare (type component component)
1138 (list trace-table-as-list)
1139 (type index code-length)
1140 (type fasl-output fasl-output))
1142 (let* ((2comp (component-info component))
1143 (constants (sb!c::ir2-component-constants 2comp))
1144 (header-length (length constants))
1145 (packed-trace-table (pack-trace-table trace-table-as-list))
1146 (total-length (+ code-length
1147 (* (length packed-trace-table)
1148 sb!c::tt-bytes-per-entry))))
1150 (collect ((patches))
1152 ;; Dump the offset of the trace table.
1153 (dump-object code-length fasl-output)
1154 ;; FIXME: As long as we don't have GENGC, the trace table is
1155 ;; hardwired to be empty. And SBCL doesn't have GENGC (and as
1156 ;; far as I know no modern CMU CL does either -- WHN
1157 ;; 2001-10-05). So might we be able to get rid of trace tables?
1159 ;; Note that gencgc also does something with the trace table.
1161 ;; Dump the constants, noting any :ENTRY constants that have to
1162 ;; be patched.
1163 (loop for i from sb!vm:code-constants-offset below header-length do
1164 (let ((entry (aref constants i)))
1165 (etypecase entry
1166 (constant
1167 (dump-object (sb!c::constant-value entry) fasl-output))
1168 (cons
1169 (ecase (car entry)
1170 (:entry
1171 (let* ((info (sb!c::leaf-info (cdr entry)))
1172 (handle (gethash info
1173 (fasl-output-entry-table
1174 fasl-output))))
1175 (declare (type sb!c::entry-info info))
1176 (cond
1177 (handle
1178 (dump-push handle fasl-output))
1180 (patches (cons info i))
1181 (dump-fop 'fop-misc-trap fasl-output)))))
1182 (:load-time-value
1183 (dump-push (cdr entry) fasl-output))
1184 (:fdefinition
1185 (dump-object (cdr entry) fasl-output)
1186 (dump-fop 'fop-fdefinition fasl-output))))
1187 (null
1188 (dump-fop 'fop-misc-trap fasl-output)))))
1190 ;; Dump the debug info.
1191 (let ((info (sb!c::debug-info-for-component component))
1192 (*dump-only-valid-structures* nil))
1193 (dump-object info fasl-output)
1194 (let ((info-handle (dump-pop fasl-output)))
1195 (dump-push info-handle fasl-output)
1196 (push info-handle (fasl-output-debug-info fasl-output))))
1198 (let ((num-consts (- header-length sb!vm:code-trace-table-offset-slot)))
1199 (cond ((and (< num-consts #x100) (< total-length #x10000))
1200 (dump-fop 'fop-small-code fasl-output)
1201 (dump-byte num-consts fasl-output)
1202 (dump-integer-as-n-bytes total-length (/ sb!vm:n-word-bytes 2) fasl-output))
1204 (dump-fop 'fop-code fasl-output)
1205 (dump-word num-consts fasl-output)
1206 (dump-word total-length fasl-output))))
1208 ;; These two dumps are only ones which contribute to our
1209 ;; TOTAL-LENGTH value.
1210 (dump-segment code-segment code-length fasl-output)
1211 (dump-i-vector packed-trace-table fasl-output :data-only t)
1213 ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it
1214 ;; dumps aren't included in the TOTAL-LENGTH passed to our
1215 ;; FOP-CODE/FOP-SMALL-CODE fop.
1216 (dump-fixups fixups fasl-output)
1218 (dump-fop 'fop-sanctify-for-execution fasl-output)
1220 (let ((handle (dump-pop fasl-output)))
1221 (dolist (patch (patches))
1222 (push (cons handle (cdr patch))
1223 (gethash (car patch)
1224 (fasl-output-patch-table fasl-output))))
1225 handle))))
1227 (defun dump-assembler-routines (code-segment length fixups routines file)
1228 (dump-fop 'fop-assembler-code file)
1229 (dump-word length file)
1230 (write-segment-contents code-segment (fasl-output-stream file))
1231 (dolist (routine routines)
1232 (dump-fop 'fop-normal-load file)
1233 (let ((*cold-load-dump* t))
1234 (dump-object (car routine) file))
1235 (dump-fop 'fop-maybe-cold-load file)
1236 (dump-fop 'fop-assembler-routine file)
1237 (dump-word (label-position (cdr routine)) file))
1238 (dump-fixups fixups file)
1239 (dump-fop 'fop-sanctify-for-execution file)
1240 (dump-pop file))
1242 ;;; Dump a function entry data structure corresponding to ENTRY to
1243 ;;; FILE. CODE-HANDLE is the table offset of the code object for the
1244 ;;; component.
1245 (defun dump-one-entry (entry code-handle file)
1246 (declare (type sb!c::entry-info entry) (type index code-handle)
1247 (type fasl-output file))
1248 (let ((name (sb!c::entry-info-name entry)))
1249 (dump-push code-handle file)
1250 (dump-object name file)
1251 (dump-object (sb!c::entry-info-arguments entry) file)
1252 (dump-object (sb!c::entry-info-type entry) file)
1253 (dump-object (sb!c::entry-info-info entry) file)
1254 (dump-fop 'fop-fun-entry file)
1255 (dump-word (label-position (sb!c::entry-info-offset entry)) file)
1256 (dump-pop file)))
1258 ;;; Alter the code object referenced by CODE-HANDLE at the specified
1259 ;;; OFFSET, storing the object referenced by ENTRY-HANDLE.
1260 (defun dump-alter-code-object (code-handle offset entry-handle file)
1261 (declare (type index code-handle entry-handle offset))
1262 (declare (type fasl-output file))
1263 (dump-push code-handle file)
1264 (dump-push entry-handle file)
1265 (dump-fop* offset fop-byte-alter-code fop-alter-code file)
1266 (values))
1268 ;;; Dump the code, constants, etc. for component. We pass in the
1269 ;;; assembler fixups, code vector and node info.
1270 (defun fasl-dump-component (component
1271 code-segment
1272 code-length
1273 trace-table
1274 fixups
1275 file)
1276 (declare (type component component) (list trace-table))
1277 (declare (type fasl-output file))
1279 (dump-fop 'fop-verify-table-size file)
1280 (dump-word (fasl-output-table-free file) file)
1282 #!+sb-dyncount
1283 (let ((info (sb!c::ir2-component-dyncount-info (component-info component))))
1284 (when info
1285 (fasl-validate-structure info file)))
1287 (let ((code-handle (dump-code-object component
1288 code-segment
1289 code-length
1290 trace-table
1291 fixups
1292 file))
1293 (2comp (component-info component)))
1295 (dolist (entry (sb!c::ir2-component-entries 2comp))
1296 (let ((entry-handle (dump-one-entry entry code-handle file)))
1297 (setf (gethash entry (fasl-output-entry-table file)) entry-handle)
1298 (let ((old (gethash entry (fasl-output-patch-table file))))
1299 (when old
1300 (dolist (patch old)
1301 (dump-alter-code-object (car patch)
1302 (cdr patch)
1303 entry-handle
1304 file))
1305 (remhash entry (fasl-output-patch-table file)))))))
1306 (values))
1308 (defun dump-push-previously-dumped-fun (fun fasl-output)
1309 (declare (type sb!c::clambda fun))
1310 (let ((handle (gethash (sb!c::leaf-info fun)
1311 (fasl-output-entry-table fasl-output))))
1312 (aver handle)
1313 (dump-push handle fasl-output))
1314 (values))
1316 ;;; Dump a FOP-FUNCALL to call an already-dumped top level lambda at
1317 ;;; load time.
1318 (defun fasl-dump-toplevel-lambda-call (fun fasl-output)
1319 (declare (type sb!c::clambda fun))
1320 (dump-push-previously-dumped-fun fun fasl-output)
1321 (dump-fop 'fop-funcall-for-effect fasl-output)
1322 (dump-byte 0 fasl-output)
1323 (values))
1325 ;;; Dump a FOP-FSET to arrange static linkage (at cold init) between
1326 ;;; FUN-NAME and the already-dumped function whose dump handle is
1327 ;;; FUN-DUMP-HANDLE.
1328 #+sb-xc-host
1329 (defun fasl-dump-cold-fset (fun-name fun-dump-handle fasl-output)
1330 (declare (type fixnum fun-dump-handle))
1331 (aver (legal-fun-name-p fun-name))
1332 (dump-non-immediate-object fun-name fasl-output)
1333 (dump-push fun-dump-handle fasl-output)
1334 (dump-fop 'fop-fset fasl-output)
1335 (values))
1337 ;;; Compute the correct list of DEBUG-SOURCE structures and backpatch
1338 ;;; all of the dumped DEBUG-INFO structures. We clear the
1339 ;;; FASL-OUTPUT-DEBUG-INFO, so that subsequent components with
1340 ;;; different source info may be dumped.
1341 (defun fasl-dump-source-info (info fasl-output)
1342 (declare (type sb!c::source-info info))
1343 (let ((res (sb!c::debug-source-for-info info))
1344 (*dump-only-valid-structures* nil))
1345 #+sb-xc-host (setf (sb!c::debug-source-created res) 0
1346 (sb!c::debug-source-compiled res) 0)
1347 (dump-object res fasl-output)
1348 (let ((res-handle (dump-pop fasl-output)))
1349 (dolist (info-handle (fasl-output-debug-info fasl-output))
1350 (dump-push res-handle fasl-output)
1351 (dump-fop 'fop-structset fasl-output)
1352 (dump-word info-handle fasl-output)
1353 (dump-word sb!c::+debug-info-source-index+ fasl-output))
1354 #+sb-xc-host
1355 (progn
1356 (dump-push res-handle fasl-output)
1357 (dump-fop 'fop-note-debug-source fasl-output))))
1358 (setf (fasl-output-debug-info fasl-output) nil)
1359 (values))
1361 ;;;; dumping structures
1363 (defun dump-structure (struct file)
1364 (when *dump-only-valid-structures*
1365 (unless (gethash struct (fasl-output-valid-structures file))
1366 (error "attempt to dump invalid structure:~% ~S~%How did this happen?"
1367 struct)))
1368 (note-potential-circularity struct file)
1369 (aver (%instance-ref struct 0))
1370 (do* ((length (%instance-length struct))
1371 (ntagged (- length (layout-n-untagged-slots (%instance-ref struct 0))))
1372 (circ (fasl-output-circularity-table file))
1373 ;; last slot first on the stack, so that the layout is on top:
1374 (index (1- length) (1- index)))
1375 ((minusp index)
1376 (dump-fop* length fop-small-struct fop-struct file))
1377 (let* ((obj (if (>= index ntagged)
1378 (%raw-instance-ref/word struct (- length index 1))
1379 (%instance-ref struct index)))
1380 (ref (gethash obj circ)))
1381 (cond (ref
1382 (aver (not (zerop index)))
1383 (push (make-circularity :type :struct-set
1384 :object struct
1385 :index index
1386 :value obj
1387 :enclosing-object ref)
1388 *circularities-detected*)
1389 (sub-dump-object nil file))
1391 (sub-dump-object obj file))))))
1393 (defun dump-layout (obj file)
1394 (when (layout-invalid obj)
1395 (compiler-error "attempt to dump reference to obsolete class: ~S"
1396 (layout-classoid obj)))
1397 (let ((name (classoid-name (layout-classoid obj))))
1398 (unless name
1399 (compiler-error "dumping anonymous layout: ~S" obj))
1400 (dump-fop 'fop-normal-load file)
1401 (let ((*cold-load-dump* t))
1402 (dump-object name file))
1403 (dump-fop 'fop-maybe-cold-load file))
1404 (sub-dump-object (layout-inherits obj) file)
1405 (sub-dump-object (layout-depthoid obj) file)
1406 (sub-dump-object (layout-length obj) file)
1407 (sub-dump-object (layout-n-untagged-slots obj) file)
1408 (dump-fop 'fop-layout file))