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