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