x86-64: LEA with neither disp nor index is MOV
[sbcl.git] / src / compiler / dump.lisp
blob481358cb6cbb937bf53f20ce8d4e8cf6b7ecad14
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 (cond ((not (coalesce-tree-p x))
378 (dump-list x file)
379 (eq-save-object x file))
380 ((not (equal-check-table x file))
381 (dump-list x file t)
382 (equal-save-object x file))))
383 (layout
384 (dump-layout x file)
385 (eq-save-object x file))
386 (instance
387 (dump-structure x file)
388 (eq-save-object x file))
389 (array
390 ;; DUMP-ARRAY (and its callees) are responsible for
391 ;; updating the EQ and EQUAL hash tables.
392 (dump-array x file))
393 (number
394 (unless (equal-check-table x file)
395 (etypecase x
396 (ratio (dump-ratio x file))
397 (complex (dump-complex x file))
398 (float (dump-float x file))
399 (integer (dump-integer x file)))
400 (equal-save-object x file)))
401 #!+(and (not (host-feature sb-xc-host)) sb-simd-pack)
402 (simd-pack
403 (unless (equal-check-table x file)
404 (dump-fop 'fop-simd-pack file)
405 (dump-integer-as-n-bytes (%simd-pack-tag x) 8 file)
406 (dump-integer-as-n-bytes (%simd-pack-low x) 8 file)
407 (dump-integer-as-n-bytes (%simd-pack-high x) 8 file))
408 (equal-save-object x file))
410 ;; This probably never happens, since bad things tend to
411 ;; be detected during IR1 conversion.
412 (error "This object cannot be dumped into a fasl file:~% ~S"
413 x))))))
414 (values))
416 ;;; Dump an object of any type by dispatching to the correct
417 ;;; type-specific dumping function. We pick off immediate objects,
418 ;;; symbols and magic lists here. Other objects are handled by
419 ;;; DUMP-NON-IMMEDIATE-OBJECT.
421 ;;; This is the function used for recursive calls to the fasl dumper.
422 ;;; We don't worry about creating circularities here, since it is
423 ;;; assumed that there is a top level call to DUMP-OBJECT.
424 (defun sub-dump-object (x file)
425 (cond ((listp x)
426 (if x
427 (dump-non-immediate-object x file)
428 (dump-fop 'fop-empty-list file)))
429 ((symbolp x)
430 (if (eq x t)
431 (dump-fop 'fop-truth file)
432 (dump-non-immediate-object x file)))
433 ((fixnump x) (dump-integer x file))
434 ((characterp x) (dump-character x file))
436 (dump-non-immediate-object x file))))
438 ;;; Dump stuff to backpatch already dumped objects. INFOS is the list
439 ;;; of CIRCULARITY structures describing what to do. The patching FOPs
440 ;;; take the value to store on the stack. We compute this value by
441 ;;; fetching the enclosing object from the table, and then CDR'ing it
442 ;;; if necessary.
443 (defun dump-circularities (infos file)
444 (let ((table (fasl-output-eq-table file)))
445 (dolist (info infos)
447 (let* ((value (circularity-value info))
448 (enclosing (circularity-enclosing-object info)))
449 (dump-push (gethash enclosing table) file)
450 (unless (eq enclosing value)
451 (do ((current enclosing (cdr current))
452 (i 0 (1+ i)))
453 ((eq current value)
454 (dump-fop 'fop-nthcdr file)
455 (dump-word i file))
456 (declare (type index i)))))
458 (ecase (circularity-type info)
459 (:rplaca (dump-fop 'fop-rplaca file))
460 (:rplacd (dump-fop 'fop-rplacd file))
461 (:svset (dump-fop 'fop-svset file))
462 (:struct-set (dump-fop 'fop-structset file)))
463 (dump-word (gethash (circularity-object info) table) file)
464 (dump-word (circularity-index info) file))))
466 ;;; Set up stuff for circularity detection, then dump an object. All
467 ;;; shared and circular structure will be exactly preserved within a
468 ;;; single call to DUMP-OBJECT. Sharing between objects dumped by
469 ;;; separate calls is only preserved when convenient.
471 ;;; We peek at the object type so that we only pay the circular
472 ;;; detection overhead on types of objects that might be circular.
473 (defun dump-object (x file)
474 (if (compound-object-p x)
475 (let ((*circularities-detected* ())
476 (circ (fasl-output-circularity-table file)))
477 (clrhash circ)
478 (sub-dump-object x file)
479 (when *circularities-detected*
480 (dump-circularities *circularities-detected* file)
481 (clrhash circ)))
482 (sub-dump-object x file)))
484 ;;;; LOAD-TIME-VALUE and MAKE-LOAD-FORM support
486 ;;; Emit a funcall of the function and return the handle for the
487 ;;; result.
488 (defun fasl-dump-load-time-value-lambda (fun file no-skip)
489 (declare (type sb!c::clambda fun) (type fasl-output file))
490 (let ((handle (gethash (sb!c::leaf-info fun)
491 (fasl-output-entry-table file))))
492 (aver handle)
493 (dump-push handle file)
494 ;; Can't skip MAKE-LOAD-FORM due to later references
495 (if no-skip
496 (dump-fop 'fop-funcall-no-skip file)
497 (dump-fop 'fop-funcall file))
498 (dump-byte 0 file))
499 (dump-pop file))
501 ;;; Return T iff CONSTANT has already been dumped. It's been dumped if
502 ;;; it's in the EQ table.
504 ;;; Note: historically (1) the above comment was "T iff ... has not been dumped",
505 ;;; (2) the test was was also true if the constant had been validated / was in
506 ;;; the valid objects table. This led to substructures occasionally skipping the
507 ;;; validation, and hence failing the "must have been validated" test.
508 (defun fasl-constant-already-dumped-p (constant file)
509 (and (gethash constant (fasl-output-eq-table file)) t))
511 ;;; Use HANDLE whenever we try to dump CONSTANT. HANDLE should have been
512 ;;; returned earlier by FASL-DUMP-LOAD-TIME-VALUE-LAMBDA.
513 (defun fasl-note-handle-for-constant (constant handle file)
514 (let ((table (fasl-output-eq-table file)))
515 (when (gethash constant table)
516 (error "~S already dumped?" constant))
517 (setf (gethash constant table) handle))
518 (values))
520 ;;; Note that the specified structure can just be dumped by
521 ;;; enumerating the slots.
522 (defun fasl-validate-structure (structure file)
523 (setf (gethash structure (fasl-output-valid-structures file)) t)
524 (values))
526 ;;;; number dumping
528 (defun dump-ratio (x file)
529 (sub-dump-object (numerator x) file)
530 (sub-dump-object (denominator x) file)
531 (dump-fop 'fop-ratio file))
533 (defun dump-integer (n file)
534 (typecase n
535 ((signed-byte 8)
536 (dump-fop 'fop-byte-integer file)
537 (dump-byte (logand #xFF n) file))
538 ((unsigned-byte #.(1- sb!vm:n-word-bits))
539 (dump-fop 'fop-word-integer file)
540 (dump-word n file))
541 ((signed-byte #.sb!vm:n-word-bits)
542 (dump-fop 'fop-word-integer file)
543 (dump-integer-as-n-bytes n #.sb!vm:n-word-bytes file))
545 (let ((bytes (ceiling (1+ (integer-length n)) 8)))
546 (dump-fop 'fop-integer file bytes)
547 (dump-integer-as-n-bytes n bytes file)))))
549 (defun dump-float (x file)
550 (etypecase x
551 (single-float
552 (dump-fop 'fop-single-float file)
553 (dump-integer-as-n-bytes (single-float-bits x) 4 file))
554 (double-float
555 (dump-fop 'fop-double-float file)
556 (let ((x x))
557 (declare (double-float x))
558 (dump-integer-as-n-bytes (double-float-low-bits x) 4 file)
559 (dump-integer-as-n-bytes (double-float-high-bits x) 4 file)))
560 #!+long-float
561 (long-float
562 (dump-fop 'fop-long-float file)
563 (dump-long-float x file))))
565 (defun dump-complex-single-float (re im file)
566 (declare (single-float re im))
567 (dump-fop 'fop-complex-single-float file)
568 (dump-integer-as-n-bytes (single-float-bits re) 4 file)
569 (dump-integer-as-n-bytes (single-float-bits im) 4 file))
571 (defun dump-complex-double-float (re im file)
572 (declare (double-float re im))
573 (dump-fop 'fop-complex-double-float file)
574 (dump-integer-as-n-bytes (double-float-low-bits re) 4 file)
575 (dump-integer-as-n-bytes (double-float-high-bits re) 4 file)
576 (dump-integer-as-n-bytes (double-float-low-bits im) 4 file)
577 (dump-integer-as-n-bytes (double-float-high-bits im) 4 file))
579 (defun dump-complex-rational (re im file)
580 (sub-dump-object re file)
581 (sub-dump-object im file)
582 (dump-fop 'fop-complex file))
584 #+sb-xc-host
585 (defun dump-complex (x file)
586 (let ((re (realpart x))
587 (im (imagpart x)))
588 (cond ((and (typep re 'single-float)
589 (typep im 'single-float))
590 (dump-complex-single-float re im file))
591 ((and (typep re 'double-float)
592 (typep im 'double-float))
593 (dump-complex-double-float re im file))
594 ((and (typep re 'rational)
595 (typep im 'rational))
596 (dump-complex-rational re im file))
598 (bug "Complex number too complex: ~S" x)))))
600 #-sb-xc-host
601 (defun dump-complex (x file)
602 (typecase x
603 ((complex single-float)
604 (dump-complex-single-float (realpart x) (imagpart x) file))
605 ((complex double-float)
606 (dump-complex-double-float (realpart x) (imagpart x) file))
607 #!+long-float
608 ((complex long-float)
609 (dump-fop 'fop-complex-long-float file)
610 (dump-long-float (realpart x) file)
611 (dump-long-float (imagpart x) file))
613 (dump-complex-rational (realpart x) (imagpart x) file))))
615 ;;;; symbol dumping
617 ;;; Return the table index of PKG, adding the package to the table if
618 ;;; necessary. During cold load, we read the string as a normal string
619 ;;; so that we can do the package lookup at cold load time.
621 ;;; FIXME: Despite the parallelism in names, the functionality of
622 ;;; this function is not parallel to other functions DUMP-FOO, e.g.
623 ;;; DUMP-SYMBOL and DUMP-LIST. The mapping between names and behavior
624 ;;; should be made more consistent.
625 (declaim (ftype (function (package fasl-output) index) dump-package))
626 (defun dump-package (pkg file)
627 (declare (inline assoc))
628 (cond ((cdr (assoc pkg (fasl-output-packages file) :test #'eq)))
630 (let ((s (package-name pkg)))
631 (dump-fop 'fop-named-package-save file (length s))
632 #+sb-xc-host
633 (dump-base-chars-of-string (coerce s 'simple-base-string) file)
634 #-sb-xc-host
635 (#!+sb-unicode dump-characters-of-string
636 #!-sb-unicode dump-base-chars-of-string
637 (coerce s '(simple-array character (*))) file))
638 (let ((entry (fasl-output-table-free file)))
639 (incf (fasl-output-table-free file))
640 (push (cons pkg entry) (fasl-output-packages file))
641 entry))))
643 ;;; dumper for lists
645 ;;; Dump a list, setting up patching information when there are
646 ;;; circularities. We scan down the list, checking for CDR and CAR
647 ;;; circularities.
649 ;;; If there is a CDR circularity, we terminate the list with NIL and
650 ;;; make a CIRCULARITY notation for the CDR of the previous cons.
652 ;;; If there is no CDR circularity, then we mark the current cons and
653 ;;; check for a CAR circularity. When there is a CAR circularity, we
654 ;;; make the CAR NIL initially, arranging for the current cons to be
655 ;;; patched later.
657 ;;; Otherwise, we recursively call the dumper to dump the current
658 ;;; element.
659 (defun dump-list (list file &optional coalesce)
660 (aver (and list
661 (not (gethash list (fasl-output-circularity-table file)))))
662 (let ((circ (fasl-output-circularity-table file)))
663 (flet ((cdr-circularity (obj n)
664 ;; COALESCE means there's no cycles
665 (let ((ref (gethash obj circ)))
666 (when ref
667 (push (make-circularity :type :rplacd
668 :object list
669 :index (1- n)
670 :value obj
671 :enclosing-object ref)
672 *circularities-detected*)
673 (terminate-undotted-list n file)
674 t))))
675 (do* ((l list (cdr l))
676 (n 0 (1+ n)))
677 ((atom l)
678 (cond ((null l)
679 (terminate-undotted-list n file))
681 (cond ((cdr-circularity l n))
683 (sub-dump-object l file)
684 (terminate-dotted-list n file))))))
685 (declare (type index n))
686 (when (cdr-circularity l n)
687 (return))
689 (setf (gethash l circ) list)
691 (let* ((obj (car l))
692 (ref (gethash obj circ)))
693 (cond (ref
694 (push (make-circularity :type :rplaca
695 :object list
696 :index n
697 :value obj
698 :enclosing-object ref)
699 *circularities-detected*)
700 (sub-dump-object nil file))
701 ;; Avoid coalescing if COALESCE-TREE-P decided not to
702 ((consp obj)
703 ;; This is the same as DUMP-NON-IMMEDIATE-OBJECT but
704 ;; without calling COALESCE-TREE-P again.
705 (let ((index (gethash obj (fasl-output-eq-table file))))
706 (cond (index
707 (dump-push index file))
708 ((not coalesce)
709 (dump-list obj file)
710 (eq-save-object obj file))
711 ((not (equal-check-table obj file))
712 (dump-list obj file t)
713 (equal-save-object obj file)))))
715 (sub-dump-object obj file))))))))
717 (defun terminate-dotted-list (n file)
718 (declare (type index n) (type fasl-output file))
719 (case n
720 (1 (dump-fop 'fop-list*-1 file))
721 (2 (dump-fop 'fop-list*-2 file))
722 (3 (dump-fop 'fop-list*-3 file))
723 (4 (dump-fop 'fop-list*-4 file))
724 (5 (dump-fop 'fop-list*-5 file))
725 (6 (dump-fop 'fop-list*-6 file))
726 (7 (dump-fop 'fop-list*-7 file))
727 (8 (dump-fop 'fop-list*-8 file))
728 (t (do ((nn n (- nn 255)))
729 ((< nn 256)
730 (dump-fop 'fop-list* file)
731 (dump-byte nn file))
732 (declare (type index nn))
733 (dump-fop 'fop-list* file)
734 (dump-byte 255 file)))))
736 ;;; If N > 255, must build list with one LIST operator, then LIST*
737 ;;; operators.
739 (defun terminate-undotted-list (n file)
740 (declare (type index n) (type fasl-output file))
741 (case n
742 (1 (dump-fop 'fop-list-1 file))
743 (2 (dump-fop 'fop-list-2 file))
744 (3 (dump-fop 'fop-list-3 file))
745 (4 (dump-fop 'fop-list-4 file))
746 (5 (dump-fop 'fop-list-5 file))
747 (6 (dump-fop 'fop-list-6 file))
748 (7 (dump-fop 'fop-list-7 file))
749 (8 (dump-fop 'fop-list-8 file))
750 (t (cond ((< n 256)
751 (dump-fop 'fop-list file)
752 (dump-byte n file))
753 (t (dump-fop 'fop-list file)
754 (dump-byte 255 file)
755 (do ((nn (- n 255) (- nn 255)))
756 ((< nn 256)
757 (dump-fop 'fop-list* file)
758 (dump-byte nn file))
759 (declare (type index nn))
760 (dump-fop 'fop-list* file)
761 (dump-byte 255 file)))))))
763 ;;;; array dumping
765 ;;; Dump the array thing.
766 (defun dump-array (x file)
767 (if (vectorp x)
768 (dump-vector x file)
769 #-sb-xc-host (dump-multi-dim-array x file)
770 #+sb-xc-host (bug "Can't dump multi-dim array")))
772 ;;; Dump the vector object. If it's not simple, then actually dump a
773 ;;; simple version of it. But we enter the original in the EQ or EQUAL
774 ;;; tables.
775 (defun dump-vector (x file)
776 (let ((simple-version (if (array-header-p x)
777 (coerce x `(simple-array
778 ,(array-element-type x)
779 (*)))
780 x)))
781 (typecase simple-version
782 #+sb-xc-host
783 (simple-string
784 (unless (string-check-table x file)
785 (dump-simple-base-string simple-version file)
786 (string-save-object x file)))
787 #-sb-xc-host
788 (simple-base-string
789 (unless (string-check-table x file)
790 (dump-simple-base-string simple-version file)
791 (string-save-object x file)))
792 #-sb-xc-host
793 ((simple-array character (*))
794 #!+sb-unicode
795 (unless (string-check-table x file)
796 (dump-simple-character-string simple-version file)
797 (string-save-object x file))
798 #!-sb-unicode
799 (bug "how did we get here?"))
800 (simple-vector
801 ;; xc-host may upgrade anything to T, so pre-check that it
802 ;; wasn't actually supposed to be a specialized array,
803 ;; and in case a copy was made, tell DUMP-S-V the original type.
804 (cond #+sb-xc-host
805 ((neq (!specialized-array-element-type x) t)
806 (dump-specialized-vector (!specialized-array-element-type x)
807 simple-version file))
809 (dump-simple-vector simple-version file)))
810 (eq-save-object x file))
812 ;; Host may have a different specialization, which is ok in itself,
813 ;; but again we might have have copied the vector, losing the type.
814 (dump-specialized-vector
815 #+sb-xc-host (!specialized-array-element-type x) simple-version file)
816 (eq-save-object x file)))))
818 ;;; Dump a SIMPLE-VECTOR, handling any circularities.
819 (defun dump-simple-vector (v file)
820 (declare (type simple-vector v) (type fasl-output file))
821 (note-potential-circularity v file)
822 (do ((index 0 (1+ index))
823 (length (length v))
824 (circ (fasl-output-circularity-table file)))
825 ((= index length)
826 (dump-fop 'fop-vector file length))
827 (let* ((obj (aref v index))
828 (ref (gethash obj circ)))
829 (cond (ref
830 (push (make-circularity :type :svset
831 :object v
832 :index index
833 :value obj
834 :enclosing-object ref)
835 *circularities-detected*)
836 (sub-dump-object nil file))
838 (sub-dump-object obj file))))))
840 ;;; In the grand scheme of things I don't pretend to understand any
841 ;;; more how this works, or indeed whether. But to write out specialized
842 ;;; vectors in the same format as fop-spec-vector expects to read them
843 ;;; we need to be target-endian. dump-integer-as-n-bytes always writes
844 ;;; little-endian (which is correct for all other integers) so for a bigendian
845 ;;; target we need to swap octets -- CSR, after DB
846 ;;; We sanity-check that VECTOR was registered as a specializd array.
847 ;;; Slight problem: if the host upgraded an array to T and we wanted it
848 ;;; more specialized, this would be undetected because the check is that
849 ;;; _if_ the array is specialized, _then_ it must have been registered.
850 ;;; The reverse is always true. But we wouldn't get here at all for (array T).
851 ;;; As a practical matter, silent failure is unlikely because
852 ;;; when building SBCL in SBCL, the needed specializations exist,
853 ;;; so the sanity-check will be triggered, and we can fix the source.
854 #+sb-xc-host
855 (defun dump-specialized-vector (element-type vector file
856 &key data-only) ; basically unused now
857 (labels ((octet-swap (word bits)
858 "BITS must be a multiple of 8"
859 (do ((input word (ash input -8))
860 (output 0 (logior (ash output 8) (logand input #xff)))
861 (bits bits (- bits 8)))
862 ((<= bits 0) output)))
863 (dump-unsigned-vector (widetag bytes bits)
864 (unless data-only
865 (dump-fop 'fop-spec-vector file)
866 (dump-word (length vector) file)
867 (dump-byte widetag file))
868 (dovector (i vector)
869 (dump-integer-as-n-bytes
870 (ecase sb!c:*backend-byte-order*
871 (:little-endian i)
872 (:big-endian (octet-swap i bits))) ; signed or unsigned OK
873 bytes file))))
874 (cond
875 ((listp element-type)
876 (destructuring-bind (type-id bits) element-type
877 (dump-unsigned-vector
878 (ecase type-id
879 (signed-byte
880 (ecase bits
881 (8 sb!vm:simple-array-signed-byte-8-widetag)
882 (16 sb!vm:simple-array-signed-byte-16-widetag)
883 (32 sb!vm:simple-array-signed-byte-32-widetag)
884 #!+64-bit
885 (64 sb!vm:simple-array-signed-byte-64-widetag)))
886 (unsigned-byte
887 (ecase bits
888 (8 sb!vm:simple-array-unsigned-byte-8-widetag)
889 (16 sb!vm:simple-array-unsigned-byte-16-widetag)
890 (32 sb!vm:simple-array-unsigned-byte-32-widetag)
891 #!+64-bit
892 (64 sb!vm:simple-array-unsigned-byte-64-widetag))))
893 (/ bits sb!vm:n-byte-bits)
894 bits)))
895 ((typep vector '(simple-bit-vector 0))
896 ;; NIL bits+bytes are ok- DUMP-INTEGER-AS-N-BYTES is unreachable.
897 ;; Otherwise we'd need to fill up octets using an ash/logior loop.
898 (dump-unsigned-vector sb!vm:simple-bit-vector-widetag nil nil))
899 ((and (typep vector '(vector * 0)) data-only)
900 nil) ; empty vector and data-only => nothing to do
901 ((typep vector '(vector (unsigned-byte 8)))
902 ;; FIXME: eliminate this case, falling through to ERROR.
903 (compiler-style-warn
904 "Unportably dumping (ARRAY (UNSIGNED-BYTE 8)) ~S" vector)
905 (dump-unsigned-vector sb!vm:simple-array-unsigned-byte-8-widetag 1 8))
907 (error "Won't dump specialized array ~S" vector)))))
909 #-sb-xc-host
910 (defun dump-specialized-vector (vector file &key data-only)
911 ;; The DATA-ONLY option was for the now-obsolete trace-table,
912 ;; but it seems like a good option to keep around.
913 (declare (type (simple-unboxed-array (*)) vector))
914 (let* ((length (length vector))
915 (widetag (%other-pointer-widetag vector))
916 (bits-per-length (aref **saetp-bits-per-length** widetag)))
917 (aver (< bits-per-length 255))
918 (unless data-only
919 (dump-fop 'fop-spec-vector file)
920 (dump-word length file)
921 (dump-byte widetag file))
922 (dump-raw-bytes vector
923 (ceiling (* length bits-per-length) sb!vm:n-byte-bits)
924 file)))
926 ;;; Dump characters and string-ish things.
928 (defun dump-character (char file)
929 (dump-fop 'fop-character file (sb!xc:char-code char)))
931 (defun dump-base-chars-of-string (s fasl-output)
932 (declare #+sb-xc-host (type simple-string s)
933 #-sb-xc-host (type simple-base-string s)
934 (type fasl-output fasl-output))
935 (dovector (c s)
936 (dump-byte (sb!xc:char-code c) fasl-output))
937 (values))
940 ;;; Dump a SIMPLE-BASE-STRING.
941 (defun dump-simple-base-string (s file)
942 #+sb-xc-host (declare (type simple-string s))
943 #-sb-xc-host (declare (type simple-base-string s))
944 (dump-fop 'fop-base-string file (length s))
945 (dump-base-chars-of-string s file)
946 (values))
948 ;;; If we get here, it is assumed that the symbol isn't in the table,
949 ;;; but we are responsible for putting it there when appropriate.
950 (defun dump-symbol (s file)
951 (declare (type fasl-output file))
952 (let* ((pname (symbol-name s))
953 (pname-length (length pname))
954 (base-string-p (typep pname (or #-sb-xc-host 'base-string t)))
955 (length+flag (logior (ash pname-length 1) (if base-string-p 1 0)))
956 (dumped-as-copy nil)
957 (pkg (symbol-package s)))
958 ;; see comment in genesis: we need this here for repeatable fasls
959 #+sb-xc-host
960 (multiple-value-bind (cl-symbol cl-status)
961 (find-symbol (symbol-name s) sb!int:*cl-package*)
962 (when (and (eq s cl-symbol)
963 (eq cl-status :external))
964 ;; special case, to work around possible xc host "design
965 ;; choice" weirdness in COMMON-LISP package
966 (setq pkg sb!int:*cl-package*)))
968 (cond ((null pkg)
969 (let ((this-base-p base-string-p))
970 (dolist (lookalike (gethash pname (fasl-output-string=-table file))
971 (dump-fop 'fop-uninterned-symbol-save
972 file length+flag))
973 ;; Find the right kind of lookalike symbol.
974 ;; [what about a symbol whose name is a (simple-array nil (0))?]
975 (let ((that-base-p
976 #+sb-xc-host t
977 #-sb-xc-host (typep (symbol-name lookalike) 'base-string)))
978 (when (or (and this-base-p that-base-p)
979 (and (not this-base-p) (not that-base-p)))
980 (dump-fop 'fop-copy-symbol-save file
981 (gethash lookalike (fasl-output-eq-table file)))
982 (return (setq dumped-as-copy t)))))))
983 ((eq pkg sb!int:*cl-package*)
984 (dump-fop 'fop-lisp-symbol-save file length+flag))
985 ((eq pkg sb!int:*keyword-package*)
986 (dump-fop 'fop-keyword-symbol-save file length+flag))
988 (let ((pkg-index (dump-package pkg file)))
989 (dump-fop 'fop-symbol-in-package-save file
990 length+flag pkg-index))))
992 (unless dumped-as-copy
993 (funcall (if base-string-p
994 'dump-base-chars-of-string
995 'dump-characters-of-string)
996 pname file)
997 (push s (gethash (symbol-name s) (fasl-output-string=-table file))))
999 (setf (gethash s (fasl-output-eq-table file))
1000 (fasl-output-table-free file))
1002 (incf (fasl-output-table-free file)))
1004 (values))
1006 ;;;; component (function) dumping
1008 (defun dump-segment (segment code-length fasl-output)
1009 (declare (type sb!assem:segment segment)
1010 (type fasl-output fasl-output))
1011 (let* ((stream (fasl-output-stream fasl-output))
1012 (n-written (write-segment-contents segment stream)))
1013 ;; In CMU CL there was no enforced connection between the CODE-LENGTH
1014 ;; argument and the number of bytes actually written. I added this
1015 ;; assertion while trying to debug portable genesis. -- WHN 19990902
1016 (unless (= code-length n-written)
1017 (bug "code-length=~W, n-written=~W" code-length n-written)))
1018 (values))
1020 ;;; Dump all the fixups. Currently there are three flavors of fixup:
1021 ;;; - assembly routines: named by a symbol
1022 ;;; - foreign (C) symbols: named by a string
1023 ;;; - code object references: don't need a name.
1024 (defun dump-fixups (fixups fasl-output)
1025 (declare (list fixups) (type fasl-output fasl-output))
1026 (dolist (note fixups)
1027 (let* ((kind (fixup-note-kind note))
1028 (fixup (fixup-note-fixup note))
1029 (position (fixup-note-position note))
1030 (name (fixup-name fixup))
1031 (flavor (fixup-flavor fixup)))
1032 (dump-object kind fasl-output)
1033 ;; Depending on the flavor, we may have various kinds of
1034 ;; noise before the position.
1035 (ecase flavor
1036 (:assembly-routine
1037 (aver (symbolp name))
1038 (dump-object name fasl-output)
1039 (dump-fop 'fop-assembler-fixup fasl-output))
1040 ((:foreign :foreign-dataref)
1041 (aver (stringp name))
1042 (ecase flavor
1043 (:foreign
1044 (dump-fop 'fop-foreign-fixup fasl-output))
1045 #!+linkage-table
1046 (:foreign-dataref
1047 (dump-fop 'fop-foreign-dataref-fixup fasl-output)))
1048 (let ((len (length name)))
1049 (aver (< len 256)) ; (limit imposed by fop definition)
1050 (dump-byte len fasl-output)
1051 (dotimes (i len)
1052 (dump-byte (char-code (schar name i)) fasl-output))))
1053 (:code-object
1054 (aver (null name))
1055 (dump-fop 'fop-code-object-fixup fasl-output))
1056 #!+immobile-space
1057 (:layout
1058 (dump-non-immediate-object (classoid-name (layout-classoid name))
1059 fasl-output)
1060 (dump-fop 'fop-layout-fixup fasl-output))
1061 #!+immobile-space
1062 (:immobile-object
1063 (dump-non-immediate-object (the symbol name) fasl-output)
1064 (dump-fop 'fop-immobile-obj-fixup fasl-output))
1065 #!+immobile-code
1066 (:named-call
1067 (dump-non-immediate-object name fasl-output)
1068 (dump-fop 'fop-named-call-fixup fasl-output))
1069 #!+immobile-code
1070 (:static-call
1071 (dump-non-immediate-object name fasl-output)
1072 (dump-fop 'fop-static-call-fixup fasl-output))
1073 (:symbol-tls-index
1074 (aver (symbolp name))
1075 (dump-non-immediate-object name fasl-output)
1076 (dump-fop 'fop-symbol-tls-fixup fasl-output)))
1077 ;; No matter what the flavor, we'll always dump the position
1078 (dump-word position fasl-output)))
1079 (values))
1081 ;;; Dump out the constant pool and code-vector for component, push the
1082 ;;; result in the table, and return the offset.
1084 ;;; The only tricky thing is handling constant-pool references to
1085 ;;; functions. If we have already dumped the function, then we just
1086 ;;; push the code pointer. Otherwise, we must create back-patching
1087 ;;; information so that the constant will be set when the function is
1088 ;;; eventually dumped. This is a bit awkward, since we don't have the
1089 ;;; handle for the code object being dumped while we are dumping its
1090 ;;; constants.
1092 ;;; We dump trap objects in any unused slots or forward referenced slots.
1093 (defun dump-code-object (component code-segment code-length fixups
1094 fasl-output entry-offsets)
1095 (declare (type component component)
1096 (type index code-length)
1097 (type fasl-output fasl-output))
1098 (let* ((2comp (component-info component))
1099 (constants (sb!c:ir2-component-constants 2comp))
1100 (header-length (length constants)))
1101 (collect ((patches))
1102 ;; Dump the constants, noting any :ENTRY constants that have to
1103 ;; be patched.
1104 (loop for i from sb!vm:code-constants-offset below header-length do
1105 (let ((entry (aref constants i)))
1106 (etypecase entry
1107 (constant
1108 (dump-object (sb!c::constant-value entry) fasl-output))
1109 (cons
1110 (ecase (car entry)
1111 (:entry
1112 (let* ((info (sb!c::leaf-info (cdr entry)))
1113 (handle (gethash info
1114 (fasl-output-entry-table
1115 fasl-output))))
1116 (declare (type sb!c::entry-info info))
1117 (cond
1118 (handle
1119 (dump-push handle fasl-output))
1121 (patches (cons info i))
1122 (dump-fop 'fop-misc-trap fasl-output)))))
1123 (:load-time-value
1124 (dump-push (cdr entry) fasl-output))
1125 (:fdefinition
1126 (dump-object (cdr entry) fasl-output)
1127 (dump-fop 'fop-fdefn fasl-output))
1128 (:known-fun
1129 (dump-object (cdr entry) fasl-output)
1130 (dump-fop 'fop-known-fun fasl-output))))
1131 (null
1132 (dump-fop 'fop-misc-trap fasl-output)))))
1134 ;; Dump the debug info.
1135 (let ((info (sb!c::debug-info-for-component component))
1136 (*dump-only-valid-structures* nil))
1137 (setf (sb!c::debug-info-source info)
1138 (fasl-output-source-info fasl-output))
1139 (dump-object info fasl-output))
1141 (dump-object (if (eq (sb!c::component-kind component) :toplevel) :toplevel nil)
1142 fasl-output)
1143 (dump-fop 'fop-code fasl-output code-length
1144 (- header-length sb!vm:code-constants-offset)
1145 (length entry-offsets))
1147 (dump-segment code-segment code-length fasl-output)
1148 (dolist (val entry-offsets) (dump-varint val fasl-output))
1150 ;; DUMP-FIXUPS does its own internal DUMP-FOPs: the bytes it
1151 ;; dumps aren't included in the LENGTH passed to FOP-CODE.
1152 (dump-fixups fixups fasl-output)
1154 #!-(or x86 (and x86-64 (not immobile-space)))
1155 (dump-fop 'fop-sanctify-for-execution fasl-output)
1157 (let ((handle (dump-pop fasl-output)))
1158 (dolist (patch (patches))
1159 (push (cons handle (cdr patch))
1160 (gethash (car patch)
1161 (fasl-output-patch-table fasl-output))))
1162 handle))))
1164 ;;; This is only called from assemfile, which doesn't exist in the target.
1165 #+sb-xc-host
1166 (defun dump-assembler-routines (code-segment length fixups routines file)
1167 (dump-fop 'fop-assembler-code file)
1168 (dump-word length file)
1169 (write-segment-contents code-segment (fasl-output-stream file))
1170 (dolist (routine routines)
1171 (dump-object (car routine) file)
1172 (dump-fop 'fop-assembler-routine file)
1173 (dump-word (+ (label-position (cadr routine))
1174 (caddr routine))
1175 file))
1176 (dump-fixups fixups file)
1177 #!-(or x86 x86-64)
1178 (dump-fop 'fop-sanctify-for-execution file)
1179 (dump-pop file))
1181 ;;; Alter the code object referenced by CODE-HANDLE at the specified
1182 ;;; OFFSET, storing the object referenced by ENTRY-HANDLE.
1183 (defun dump-alter-code-object (code-handle offset entry-handle file)
1184 (declare (type index code-handle entry-handle offset))
1185 (declare (type fasl-output file))
1186 (dump-push code-handle file)
1187 (dump-push entry-handle file)
1188 (dump-fop 'fop-alter-code file offset)
1189 (values))
1191 ;;; Dump the code, constants, etc. for component. We pass in the
1192 ;;; assembler fixups, code vector and node info.
1193 (defun fasl-dump-component (component
1194 code-segment
1195 code-length
1196 fixups
1197 file)
1198 (declare (type component component))
1199 (declare (type fasl-output file))
1201 (dump-fop 'fop-verify-table-size file)
1202 (dump-word (fasl-output-table-free file) file)
1204 #!+sb-dyncount
1205 (let ((info (sb!c::ir2-component-dyncount-info (component-info component))))
1206 (when info
1207 (fasl-validate-structure info file)))
1209 (let* ((2comp (component-info component))
1210 (entries (sb!c::ir2-component-entries 2comp))
1211 (nfuns (length entries))
1212 (code-handle (dump-code-object
1213 component code-segment code-length
1214 fixups file
1215 (mapcar (lambda (entry)
1216 (label-position (sb!c::entry-info-offset entry)))
1217 entries)))
1218 (fun-index nfuns))
1220 (dolist (entry entries)
1221 (dump-push code-handle file)
1222 (dump-object (sb!c::entry-info-name entry) file)
1223 (dump-object (sb!c::entry-info-arguments entry) file)
1224 (dump-object (sb!c::entry-info-type entry) file)
1225 (dump-object (sb!c::entry-info-info entry) file)
1226 (dump-fop 'fop-fun-entry file (decf fun-index))
1227 (let ((entry-handle (dump-pop file)))
1228 (setf (gethash entry (fasl-output-entry-table file)) entry-handle)
1229 (let ((old (gethash entry (fasl-output-patch-table file))))
1230 (when old
1231 (dolist (patch old)
1232 (dump-alter-code-object (car patch)
1233 (cdr patch)
1234 entry-handle
1235 file))
1236 (remhash entry (fasl-output-patch-table file)))))))
1237 (values))
1239 (defun dump-push-previously-dumped-fun (fun fasl-output)
1240 (declare (type sb!c::clambda fun))
1241 (let ((handle (gethash (sb!c::leaf-info fun)
1242 (fasl-output-entry-table fasl-output))))
1243 (aver handle)
1244 (dump-push handle fasl-output))
1245 (values))
1247 ;;; Dump a FOP-FUNCALL to call an already-dumped top level lambda at
1248 ;;; load time.
1249 (defun fasl-dump-toplevel-lambda-call (fun fasl-output)
1250 (declare (type sb!c::clambda fun))
1251 (dump-push-previously-dumped-fun fun fasl-output)
1252 (dump-fop 'fop-funcall-for-effect fasl-output)
1253 (dump-byte 0 fasl-output)
1254 (values))
1256 ;;;; dumping structures
1258 ;; Having done nothing more than load all files in obj/from-host, the
1259 ;; cross-compiler running under any host Lisp begins life able to access
1260 ;; SBCL-format metadata for any structure that is a subtype of STRUCTURE!OBJECT.
1261 ;; But if it learns a layout by cross-compiling a DEFSTRUCT, that's ok too.
1262 (defun dump-structure (struct file)
1263 (when (and *dump-only-valid-structures*
1264 (not (gethash struct (fasl-output-valid-structures file))))
1265 (error "attempt to dump invalid structure:~% ~S~%How did this happen?"
1266 struct))
1267 (note-potential-circularity struct file)
1268 (do* ((length (%instance-length struct))
1269 (layout (%instance-layout struct))
1270 (bitmap (layout-bitmap layout))
1271 (circ (fasl-output-circularity-table file))
1272 (index sb!vm:instance-data-start (1+ index)))
1273 ((>= index length)
1274 (dump-non-immediate-object layout file)
1275 (dump-fop 'fop-struct file length))
1276 (let* ((obj (if (logbitp index bitmap)
1277 (%instance-ref struct index)
1278 (%raw-instance-ref/word struct index)))
1279 (ref (gethash obj circ)))
1280 (sub-dump-object (cond (ref
1281 (push (make-circularity :type :struct-set
1282 :object struct
1283 :index index
1284 :value obj
1285 :enclosing-object ref)
1286 *circularities-detected*)
1287 nil)
1288 (t obj))
1289 file))))
1291 (defun dump-layout (obj file)
1292 (when (layout-invalid obj)
1293 (compiler-error "attempt to dump reference to obsolete class: ~S"
1294 (layout-classoid obj)))
1295 (let ((name (classoid-name (layout-classoid obj))))
1296 ;; Q: Shouldn't we aver that NAME is the proper name for its classoid?
1297 (unless name
1298 (compiler-error "dumping anonymous layout: ~S" obj))
1299 ;; The target lisp can save some space in fasls (sometimes),
1300 ;; but the cross-compiler can't because we need to construct the
1301 ;; cold representation of all layouts, not reference host layouts.
1302 #-sb-xc-host
1303 (let ((fop (known-layout-fop name)))
1304 (when fop
1305 (return-from dump-layout (dump-byte fop file))))
1306 (dump-object name file))
1307 (sub-dump-object (layout-inherits obj) file)
1308 (sub-dump-object (layout-depthoid obj) file)
1309 (sub-dump-object (layout-length obj) file)
1310 (sub-dump-object (layout-bitmap obj) file)
1311 (dump-fop 'fop-layout file))