x86: Add a slot for code->fixups
[sbcl.git] / src / compiler / generic / genesis.lisp
blobf0181aae91ee77ce36d8397bec9b047156c81034
1 ;;;; "cold" core image builder: This is how we create a target Lisp
2 ;;;; system from scratch, by converting from fasl files to an image
3 ;;;; file in the cross-compilation host, without the help of the
4 ;;;; target Lisp system.
5 ;;;;
6 ;;;; As explained by Rob MacLachlan on the CMU CL mailing list Wed, 06
7 ;;;; Jan 1999 11:05:02 -0500, this cold load generator more or less
8 ;;;; fakes up static function linking. I.e. it makes sure that all the
9 ;;;; DEFUN-defined functions in the fasl files it reads are bound to the
10 ;;;; corresponding symbols before execution starts. It doesn't do
11 ;;;; anything to initialize variable values; instead it just arranges
12 ;;;; for !COLD-INIT to be called at cold load time. !COLD-INIT is
13 ;;;; responsible for explicitly initializing anything which has to be
14 ;;;; initialized early before it transfers control to the ordinary
15 ;;;; top level forms.
16 ;;;;
17 ;;;; (In CMU CL, and in SBCL as of 0.6.9 anyway, functions not defined
18 ;;;; by DEFUN aren't set up specially by GENESIS.)
20 ;;;; This software is part of the SBCL system. See the README file for
21 ;;;; more information.
22 ;;;;
23 ;;;; This software is derived from the CMU CL system, which was
24 ;;;; written at Carnegie Mellon University and released into the
25 ;;;; public domain. The software is in the public domain and is
26 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
27 ;;;; files for more information.
29 (in-package "SB!FASL")
31 ;;; a magic number used to identify our core files
32 (defconstant core-magic
33 (logior (ash (sb!xc:char-code #\S) 24)
34 (ash (sb!xc:char-code #\B) 16)
35 (ash (sb!xc:char-code #\C) 8)
36 (sb!xc:char-code #\L)))
38 (defun round-up (number size)
39 "Round NUMBER up to be an integral multiple of SIZE."
40 (* size (ceiling number size)))
42 ;;;; implementing the concept of "vector" in (almost) portable
43 ;;;; Common Lisp
44 ;;;;
45 ;;;; "If you only need to do such simple things, it doesn't really
46 ;;;; matter which language you use." -- _ANSI Common Lisp_, p. 1, Paul
47 ;;;; Graham (evidently not considering the abstraction "vector" to be
48 ;;;; such a simple thing:-)
50 (eval-when (:compile-toplevel :load-toplevel :execute)
51 (defconstant +smallvec-length+
52 (expt 2 16)))
54 ;;; an element of a BIGVEC -- a vector small enough that we have
55 ;;; a good chance of it being portable to other Common Lisps
56 (deftype smallvec ()
57 `(simple-array (unsigned-byte 8) (,+smallvec-length+)))
59 (defun make-smallvec ()
60 (make-array +smallvec-length+ :element-type '(unsigned-byte 8)
61 :initial-element 0))
63 ;;; a big vector, implemented as a vector of SMALLVECs
64 ;;;
65 ;;; KLUDGE: This implementation seems portable enough for our
66 ;;; purposes, since realistically every modern implementation is
67 ;;; likely to support vectors of at least 2^16 elements. But if you're
68 ;;; masochistic enough to read this far into the contortions imposed
69 ;;; on us by ANSI and the Lisp community, for daring to use the
70 ;;; abstraction of a large linearly addressable memory space, which is
71 ;;; after all only directly supported by the underlying hardware of at
72 ;;; least 99% of the general-purpose computers in use today, then you
73 ;;; may be titillated to hear that in fact this code isn't really
74 ;;; portable, because as of sbcl-0.7.4 we need somewhat more than
75 ;;; 16Mbytes to represent a core, and ANSI only guarantees that
76 ;;; ARRAY-DIMENSION-LIMIT is not less than 1024. -- WHN 2002-06-13
77 (defstruct bigvec
78 (outer-vector (vector (make-smallvec)) :type (vector smallvec)))
80 ;;; analogous to SVREF, but into a BIGVEC
81 (defun bvref (bigvec index)
82 (multiple-value-bind (outer-index inner-index)
83 (floor index +smallvec-length+)
84 (aref (the smallvec
85 (svref (bigvec-outer-vector bigvec) outer-index))
86 inner-index)))
87 (defun (setf bvref) (new-value bigvec index)
88 (multiple-value-bind (outer-index inner-index)
89 (floor index +smallvec-length+)
90 (setf (aref (the smallvec
91 (svref (bigvec-outer-vector bigvec) outer-index))
92 inner-index)
93 new-value)))
95 ;;; analogous to LENGTH, but for a BIGVEC
96 ;;;
97 ;;; the length of BIGVEC, measured in the number of BVREFable bytes it
98 ;;; can hold
99 (defun bvlength (bigvec)
100 (* (length (bigvec-outer-vector bigvec))
101 +smallvec-length+))
103 ;;; analogous to WRITE-SEQUENCE, but for a BIGVEC
104 (defun write-bigvec-as-sequence (bigvec stream &key (start 0) end pad-with-zeros)
105 (let* ((bvlength (bvlength bigvec))
106 (data-length (min (or end bvlength) bvlength)))
107 (loop for i of-type index from start below data-length do
108 (write-byte (bvref bigvec i)
109 stream))
110 (when (and pad-with-zeros (< bvlength data-length))
111 (loop repeat (- data-length bvlength) do (write-byte 0 stream)))))
113 ;;; analogous to READ-SEQUENCE-OR-DIE, but for a BIGVEC
114 (defun read-bigvec-as-sequence-or-die (bigvec stream &key (start 0) end)
115 (loop for i of-type index from start below (or end (bvlength bigvec)) do
116 (setf (bvref bigvec i)
117 (read-byte stream))))
119 ;;; Grow BIGVEC (exponentially, so that large increases in size have
120 ;;; asymptotic logarithmic cost per byte).
121 (defun expand-bigvec (bigvec)
122 (let* ((old-outer-vector (bigvec-outer-vector bigvec))
123 (length-old-outer-vector (length old-outer-vector))
124 (new-outer-vector (make-array (* 2 length-old-outer-vector))))
125 (replace new-outer-vector old-outer-vector)
126 (loop for i from length-old-outer-vector below (length new-outer-vector) do
127 (setf (svref new-outer-vector i)
128 (make-smallvec)))
129 (setf (bigvec-outer-vector bigvec)
130 new-outer-vector))
131 bigvec)
133 ;;;; looking up bytes and multi-byte values in a BIGVEC (considering
134 ;;;; it as an image of machine memory on the cross-compilation target)
136 ;;; BVREF-32 and friends. These are like SAP-REF-n, except that
137 ;;; instead of a SAP we use a BIGVEC.
138 (macrolet ((make-bvref-n
140 (let* ((name (intern (format nil "BVREF-~A" n)))
141 (number-octets (/ n 8))
142 (ash-list-le
143 (loop for i from 0 to (1- number-octets)
144 collect `(ash (bvref bigvec (+ byte-index ,i))
145 ,(* i 8))))
146 (ash-list-be
147 (loop for i from 0 to (1- number-octets)
148 collect `(ash (bvref bigvec
149 (+ byte-index
150 ,(- number-octets 1 i)))
151 ,(* i 8))))
152 (setf-list-le
153 (loop for i from 0 to (1- number-octets)
154 append
155 `((bvref bigvec (+ byte-index ,i))
156 (ldb (byte 8 ,(* i 8)) new-value))))
157 (setf-list-be
158 (loop for i from 0 to (1- number-octets)
159 append
160 `((bvref bigvec (+ byte-index ,i))
161 (ldb (byte 8 ,(- n 8 (* i 8))) new-value)))))
162 `(progn
163 (defun ,name (bigvec byte-index)
164 (logior ,@(ecase sb!c:*backend-byte-order*
165 (:little-endian ash-list-le)
166 (:big-endian ash-list-be))))
167 (defun (setf ,name) (new-value bigvec byte-index)
168 (setf ,@(ecase sb!c:*backend-byte-order*
169 (:little-endian setf-list-le)
170 (:big-endian setf-list-be))))))))
171 (make-bvref-n 8)
172 (make-bvref-n 16)
173 (make-bvref-n 32)
174 (make-bvref-n 64))
176 ;; lispobj-sized word, whatever that may be
177 ;; hopefully nobody ever wants a 128-bit SBCL...
178 (macrolet ((acc (bv index) `(#!+64-bit bvref-64 #!-64-bit bvref-32 ,bv ,index)))
179 (defun (setf bvref-word) (new-val bytes index) (setf (acc bytes index) new-val))
180 (defun bvref-word (bytes index) (acc bytes index)))
182 ;;;; representation of spaces in the core
184 ;;; If there is more than one dynamic space in memory (i.e., if a
185 ;;; copying GC is in use), then only the active dynamic space gets
186 ;;; dumped to core.
187 (defvar *dynamic*)
188 (defconstant dynamic-core-space-id 1)
190 (defvar *static*)
191 (defconstant static-core-space-id 2)
193 (defvar *read-only*)
194 (defconstant read-only-core-space-id 3)
196 #!+immobile-space
197 (progn
198 (defvar *immobile-fixedobj*)
199 (defvar *immobile-varyobj*)
200 (defconstant immobile-fixedobj-core-space-id 4)
201 (defconstant immobile-varyobj-core-space-id 5)
202 (defvar *immobile-space-map* nil))
204 (defconstant max-core-space-id 5)
205 (defconstant deflated-core-space-id-flag 8)
207 ;;; a GENESIS-time representation of a memory space (e.g. read-only
208 ;;; space, dynamic space, or static space)
209 (defstruct (gspace (:constructor %make-gspace)
210 (:copier nil))
211 ;; name and identifier for this GSPACE
212 (name (missing-arg) :type symbol :read-only t)
213 (identifier (missing-arg) :type fixnum :read-only t)
214 ;; the word address where the data will be loaded
215 (word-address (missing-arg) :type unsigned-byte :read-only t)
216 ;; the gspace contents as a BIGVEC
217 (bytes (make-bigvec) :type bigvec :read-only t)
218 ;; the index of the next unwritten word (i.e. chunk of
219 ;; SB!VM:N-WORD-BYTES bytes) in BYTES, or equivalently the number of
220 ;; words actually written in BYTES. In order to convert to an actual
221 ;; index into BYTES, thus must be multiplied by SB!VM:N-WORD-BYTES.
222 (free-word-index 0))
224 (defun gspace-byte-address (gspace)
225 (ash (gspace-word-address gspace) sb!vm:word-shift))
227 (cl:defmethod print-object ((gspace gspace) stream)
228 (print-unreadable-object (gspace stream :type t)
229 (format stream "@#x~X ~S" (gspace-byte-address gspace) (gspace-name gspace))))
231 (defun make-gspace (name identifier byte-address)
232 ;; Genesis should be agnostic of space alignment except in so far as it must
233 ;; be a multiple of the backend page size. We used to care more, in that
234 ;; descriptor-bits were composed of a high half and low half for the
235 ;; questionable motive of caring about fixnum-ness of the halves,
236 ;; despite the wonderful abstraction INTEGER that transparently becomes
237 ;; a BIGNUM if the host's fixnum is limited in size.
238 ;; So it's not clear whether this test belongs here, because if we do need it,
239 ;; then it best belongs where we assign space addresses in the first place.
240 (let ((target-space-alignment (ash 1 16)))
241 (unless (zerop (rem byte-address target-space-alignment))
242 (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
243 byte-address target-space-alignment)))
244 (%make-gspace :name name
245 :identifier identifier
246 :word-address (ash byte-address (- sb!vm:word-shift))))
248 ;;;; representation of descriptors
250 (declaim (inline is-fixnum-lowtag))
251 (defun is-fixnum-lowtag (lowtag)
252 (zerop (logand lowtag sb!vm:fixnum-tag-mask)))
254 (defun is-other-immediate-lowtag (lowtag)
255 ;; The other-immediate lowtags are similar to the fixnum lowtags, in
256 ;; that they have an "effective length" that is shorter than is used
257 ;; for the pointer lowtags. Unlike the fixnum lowtags, however, the
258 ;; other-immediate lowtags are always effectively two bits wide.
259 (= (logand lowtag 3) sb!vm:other-immediate-0-lowtag))
261 (defstruct (descriptor
262 (:constructor make-descriptor (bits &optional gspace word-offset))
263 (:copier nil))
264 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
265 (gspace nil :type (or gspace (eql :load-time-value) null))
266 ;; the offset in words from the start of GSPACE, or NIL if not set yet
267 (word-offset nil :type (or sb!vm:word null))
268 (bits 0 :read-only t :type (unsigned-byte #.sb!vm:n-machine-word-bits)))
270 (declaim (inline descriptor=))
271 (defun descriptor= (a b) (eql (descriptor-bits a) (descriptor-bits b)))
273 (defun make-random-descriptor (bits)
274 (make-descriptor (logand bits sb!ext:most-positive-word)))
276 (declaim (inline descriptor-lowtag))
277 (defun descriptor-lowtag (des)
278 "the lowtag bits for DES"
279 (logand (descriptor-bits des) sb!vm:lowtag-mask))
281 (defmethod print-object ((des descriptor) stream)
282 (let ((gspace (descriptor-gspace des))
283 (bits (descriptor-bits des))
284 (lowtag (descriptor-lowtag des)))
285 (print-unreadable-object (des stream :type t)
286 (cond ((eq gspace :load-time-value)
287 (format stream "for LTV ~D" (descriptor-word-offset des)))
288 ((is-fixnum-lowtag lowtag)
289 (format stream "for fixnum: ~W" (descriptor-fixnum des)))
290 ((is-other-immediate-lowtag lowtag)
291 (format stream
292 "for other immediate: #X~X, type #b~8,'0B"
293 (ash bits (- sb!vm:n-widetag-bits))
294 (logand bits sb!vm:widetag-mask)))
296 (format stream
297 "for pointer: #X~X, lowtag #b~v,'0B, ~A"
298 (logandc2 bits sb!vm:lowtag-mask)
299 sb!vm:n-lowtag-bits lowtag
300 (if gspace (gspace-name gspace) "unknown")))))))
302 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
303 ;;; free word index is boosted as necessary, and if additional memory
304 ;;; is needed, we grow the GSPACE. The descriptor returned is a
305 ;;; pointer of type LOWTAG.
306 (defun allocate-cold-descriptor (gspace length lowtag &optional page-attributes)
307 (let* ((word-index
308 (gspace-claim-n-bytes gspace length page-attributes))
309 (ptr (+ (gspace-word-address gspace) word-index)))
310 (make-descriptor (logior (ash ptr sb!vm:word-shift) lowtag)
311 gspace
312 word-index)))
314 (defun gspace-claim-n-words (gspace n-words)
315 (let* ((old-free-word-index (gspace-free-word-index gspace))
316 (new-free-word-index (+ old-free-word-index n-words)))
317 ;; Grow GSPACE as necessary until it's big enough to handle
318 ;; NEW-FREE-WORD-INDEX.
319 (do ()
320 ((>= (bvlength (gspace-bytes gspace))
321 (* new-free-word-index sb!vm:n-word-bytes)))
322 (expand-bigvec (gspace-bytes gspace)))
323 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
324 (setf (gspace-free-word-index gspace) new-free-word-index)
325 old-free-word-index))
327 ;; align256p is true if we need to force objects on this page to 256-byte
328 ;; boundaries. This doesn't need to be generalized - everything of type
329 ;; INSTANCE is either on its natural alignment, or 256-byte.
330 ;; [See doc/internals-notes/compact-instance for why you might want it at all]
331 ;; PAGE-KIND is a heuristic for placement of symbols
332 ;; based on being interned/uninterned/likely-special-variable.
333 (defun make-page-attributes (align256p page-kind)
334 (declare (type (or null (integer 0 3)) page-kind))
335 (logior (ash (or page-kind 0) 1) (if align256p 1 0)))
336 (defun immobile-obj-spacing-words (page-attributes)
337 (if (logbitp 0 page-attributes)
338 (/ 256 sb!vm:n-word-bytes)))
340 (defun gspace-claim-n-bytes (gspace specified-n-bytes page-attributes)
341 (declare (ignorable page-attributes))
342 (let* ((n-bytes (round-up specified-n-bytes (ash 1 sb!vm:n-lowtag-bits)))
343 (n-words (ash n-bytes (- sb!vm:word-shift))))
344 (aver (evenp n-words))
345 (cond #!+immobile-space
346 ((eq gspace *immobile-fixedobj*)
347 (aver page-attributes)
348 ;; An immobile fixedobj page can only have one value of object-spacing
349 ;; and size for all objects on it. Different widetags are ok.
350 (let* ((key (cons specified-n-bytes page-attributes))
351 (found (cdr (assoc key *immobile-space-map* :test 'equal)))
352 (page-n-words (/ sb!vm:immobile-card-bytes sb!vm:n-word-bytes)))
353 (unless found ; grab one whole GC page from immobile space
354 (let ((free-word-index
355 (gspace-claim-n-words gspace page-n-words)))
356 (setf found (cons 0 free-word-index))
357 (push (cons key found) *immobile-space-map*)))
358 (destructuring-bind (page-word-index . page-base-index) found
359 (let ((next-word
360 (+ page-word-index
361 (or (immobile-obj-spacing-words page-attributes)
362 n-words))))
363 (if (> next-word (- page-n-words n-words))
364 ;; no more objects fit on this page
365 (setf *immobile-space-map*
366 (delete key *immobile-space-map* :key 'car :test 'equal))
367 (setf (car found) next-word)))
368 (+ page-word-index page-base-index))))
370 (gspace-claim-n-words gspace n-words)))))
372 (defun descriptor-fixnum (des)
373 (unless (is-fixnum-lowtag (descriptor-lowtag des))
374 (error "descriptor-fixnum called on non-fixnum ~S" des))
375 (let* ((descriptor-bits (descriptor-bits des))
376 (bits (ash descriptor-bits (- sb!vm:n-fixnum-tag-bits))))
377 (if (logbitp (1- sb!vm:n-word-bits) descriptor-bits)
378 (logior bits (ash -1 (1+ sb!vm:n-positive-fixnum-bits)))
379 bits)))
381 (defun descriptor-word-sized-integer (des)
382 ;; Extract an (unsigned-byte 32), from either its fixnum or bignum
383 ;; representation.
384 (let ((lowtag (descriptor-lowtag des)))
385 (if (is-fixnum-lowtag lowtag)
386 (make-random-descriptor (descriptor-fixnum des))
387 (read-wordindexed des 1))))
389 ;;; common idioms
390 (defun descriptor-mem (des)
391 (gspace-bytes (descriptor-intuit-gspace des)))
392 (defun descriptor-byte-offset (des)
393 (ash (descriptor-word-offset des) sb!vm:word-shift))
395 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
396 ;;; figure out a GSPACE which corresponds to DES, set it into
397 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
398 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
399 (declaim (ftype (function (descriptor) gspace) descriptor-intuit-gspace))
400 (defun descriptor-intuit-gspace (des)
401 (or (descriptor-gspace des)
403 ;; gspace wasn't set, now we have to search for it.
404 (let* ((lowtag (descriptor-lowtag des))
405 (abs-word-addr (ash (- (descriptor-bits des) lowtag)
406 (- sb!vm:word-shift))))
408 ;; Non-pointer objects don't have a gspace.
409 (unless (or (eql lowtag sb!vm:fun-pointer-lowtag)
410 (eql lowtag sb!vm:instance-pointer-lowtag)
411 (eql lowtag sb!vm:list-pointer-lowtag)
412 (eql lowtag sb!vm:other-pointer-lowtag))
413 (error "don't even know how to look for a GSPACE for ~S" des))
415 (dolist (gspace (list *dynamic* *static* *read-only*
416 #!+immobile-space *immobile-fixedobj*
417 #!+immobile-space *immobile-varyobj*)
418 (error "couldn't find a GSPACE for ~S" des))
419 ;; Bounds-check the descriptor against the allocated area
420 ;; within each gspace.
421 (when (and (<= (gspace-word-address gspace)
422 abs-word-addr
423 (+ (gspace-word-address gspace)
424 (gspace-free-word-index gspace))))
425 ;; Update the descriptor with the correct gspace and the
426 ;; offset within the gspace and return the gspace.
427 (setf (descriptor-word-offset des)
428 (- abs-word-addr (gspace-word-address gspace)))
429 (return (setf (descriptor-gspace des) gspace)))))))
431 (defun %fixnum-descriptor-if-possible (num)
432 (and (typep num '(signed-byte #.sb!vm:n-fixnum-bits))
433 (make-random-descriptor (ash num sb!vm:n-fixnum-tag-bits))))
435 (defun make-fixnum-descriptor (num)
436 (or (%fixnum-descriptor-if-possible num)
437 (error "~W is too big for a fixnum." num)))
439 (defun make-other-immediate-descriptor (data type)
440 (make-descriptor (logior (ash data sb!vm:n-widetag-bits) type)))
442 (defun make-character-descriptor (data)
443 (make-other-immediate-descriptor data sb!vm:character-widetag))
446 ;;;; miscellaneous variables and other noise
448 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
449 ;;; undefined foreign symbols are to be treated as an error.
450 ;;; (In the first pass of GENESIS, needed to create a header file before
451 ;;; the C runtime can be built, various foreign symbols will necessarily
452 ;;; be undefined, but we don't need actual values for them anyway, and
453 ;;; we can just use 0 or some other placeholder. In the second pass of
454 ;;; GENESIS, all foreign symbols should be defined, so any undefined
455 ;;; foreign symbol is a problem.)
457 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
458 ;;; never tries to look up foreign symbols in the first place unless
459 ;;; it's actually creating a core file (as in the second pass) instead
460 ;;; of using this hack to allow it to go through the motions without
461 ;;; causing an error. -- WHN 20000825
462 (defvar *foreign-symbol-placeholder-value*)
464 ;;; a handle on the trap object
465 (defvar *unbound-marker*
466 (make-other-immediate-descriptor 0 sb!vm:unbound-marker-widetag))
468 ;;; a handle on the NIL object
469 (defvar *nil-descriptor*)
471 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
472 ;;; when the target Lisp starts up
474 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
475 ;;; loadtime value, represented by (CONS KEYWORD ..).
476 (declaim (special *!cold-toplevels* *!cold-defconstants*
477 *!cold-defuns* *cold-methods*))
479 ;;; the head of a list of DEBUG-SOURCEs which need to be patched when
480 ;;; the cold core starts up
481 (defvar *current-debug-sources*)
483 ;;; foreign symbol references
484 (defparameter *cold-foreign-undefined-symbols* nil)
486 ;;;; miscellaneous stuff to read and write the core memory
488 ;;; FIXME: should be DEFINE-MODIFY-MACRO
489 (defmacro cold-push (thing list) ; for making a target list held in a host symbol
490 "Push THING onto the given cold-load LIST."
491 `(setq ,list (cold-cons ,thing ,list)))
493 ;; Like above, but the list is held in the target's image of the host symbol,
494 ;; not the host's value of the symbol.
495 (defun cold-target-push (cold-thing host-symbol)
496 (cold-set host-symbol (cold-cons cold-thing (cold-symbol-value host-symbol))))
498 (declaim (ftype (function (descriptor sb!vm:word) descriptor) read-wordindexed))
499 (macrolet ((read-bits ()
500 `(bvref-word (descriptor-mem address)
501 (ash (+ index (descriptor-word-offset address))
502 sb!vm:word-shift))))
503 (defun read-bits-wordindexed (address index)
504 (read-bits))
505 (defun read-wordindexed (address index)
506 "Return the value which is displaced by INDEX words from ADDRESS."
507 (make-random-descriptor (read-bits))))
509 (declaim (ftype (function (descriptor) descriptor) read-memory))
510 (defun read-memory (address)
511 "Return the value at ADDRESS."
512 (read-wordindexed address 0))
514 (declaim (ftype (function (descriptor
515 (integer #.(- sb!vm:list-pointer-lowtag)
516 #.sb!ext:most-positive-word)
517 descriptor)
518 (values))
519 note-load-time-value-reference))
520 (defun note-load-time-value-reference (address offset marker)
521 (push (cold-list (cold-intern :load-time-value-fixup)
522 address
523 (number-to-core offset)
524 (number-to-core (descriptor-word-offset marker)))
525 *!cold-toplevels*)
526 (values))
528 (declaim (ftype (function (descriptor sb!vm:word (or symbol descriptor))) write-wordindexed))
529 (macrolet ((write-bits (bits)
530 `(setf (bvref-word (descriptor-mem address)
531 (ash (+ index (descriptor-word-offset address))
532 sb!vm:word-shift))
533 ,bits)))
534 (defun write-wordindexed (address index value)
535 "Write VALUE displaced INDEX words from ADDRESS."
536 ;; If we're passed a symbol as a value then it needs to be interned.
537 (let ((value (cond ((symbolp value) (cold-intern value))
538 (t value))))
539 (if (eql (descriptor-gspace value) :load-time-value)
540 (note-load-time-value-reference address
541 (- (ash index sb!vm:word-shift)
542 (logand (descriptor-bits address)
543 sb!vm:lowtag-mask))
544 value)
545 (write-bits (descriptor-bits value)))))
547 (defun write-wordindexed/raw (address index bits)
548 (declare (type descriptor address) (type sb!vm:word index)
549 (type (or sb!vm:word sb!vm:signed-word) bits))
550 (write-bits (logand bits sb!ext:most-positive-word))))
552 (declaim (ftype (function (descriptor (or symbol descriptor))) write-memory))
553 (defun write-memory (address value)
554 "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
555 (write-wordindexed address 0 value))
557 ;;;; allocating images of primitive objects in the cold core
559 (defun write-header-word (des header-data widetag)
560 ;; In immobile space, all objects start life as pseudo-static as if by 'save'.
561 (let ((gen #!+gencgc (if (or #!+immobile-space
562 (let ((gspace (descriptor-gspace des)))
563 (or (eq gspace *immobile-fixedobj*)
564 (eq gspace *immobile-varyobj*))))
565 sb!vm:+pseudo-static-generation+
567 #!-gencgc 0))
568 (write-wordindexed/raw des 0
569 (logior (ash (logior (ash gen 16) header-data)
570 sb!vm:n-widetag-bits) widetag))))
572 (defun set-header-data (object data)
573 (write-header-word object data (ldb (byte sb!vm:n-widetag-bits 0)
574 (read-bits-wordindexed object 0))))
576 (defun get-header-data (object)
577 (ash (read-bits-wordindexed object 0) (- sb!vm:n-widetag-bits)))
579 ;;; There are three kinds of blocks of memory in the type system:
580 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
581 ;;; header as all slots are descriptors.
582 ;;; * Unboxed objects (bignums): There is a single header word that contains
583 ;;; the length.
584 ;;; * Vector objects: There is a header word with the type, then a word for
585 ;;; the length, then the data.
586 (defun allocate-object (gspace length lowtag &optional align256p)
587 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
588 pointing to them."
589 (allocate-cold-descriptor gspace (ash length sb!vm:word-shift) lowtag
590 (make-page-attributes align256p 0)))
591 (defun allocate-header+object (gspace length widetag &optional page-kind)
592 "Allocate LENGTH words plus a header word in GSPACE and
593 return an ``other-pointer'' descriptor to them. Initialize the header word
594 with the resultant length and WIDETAG."
595 (let ((des (allocate-cold-descriptor
596 gspace (ash (1+ length) sb!vm:word-shift)
597 sb!vm:other-pointer-lowtag
598 (make-page-attributes nil page-kind))))
599 (write-header-word des length widetag)
600 des))
601 (defun allocate-vector-object (gspace element-bits length widetag)
602 "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
603 GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
604 header word with WIDETAG and the length slot with LENGTH."
605 ;; ALLOCATE-COLD-DESCRIPTOR will take any rational number of bytes
606 ;; and round up to a double-word. This doesn't need to use CEILING.
607 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
608 (des (allocate-cold-descriptor gspace
609 (+ bytes (* 2 sb!vm:n-word-bytes))
610 sb!vm:other-pointer-lowtag)))
611 (write-header-word des 0 widetag)
612 (write-wordindexed des
613 sb!vm:vector-length-slot
614 (make-fixnum-descriptor length))
615 des))
617 ;;; the hosts's representation of LAYOUT-of-LAYOUT
618 (eval-when (:compile-toplevel :load-toplevel :execute)
619 (defvar *host-layout-of-layout* (find-layout 'layout)))
621 (defun cold-layout-length (layout)
622 (descriptor-fixnum (read-slot layout *host-layout-of-layout* :length)))
623 (defun cold-layout-depthoid (layout)
624 (descriptor-fixnum (read-slot layout *host-layout-of-layout* :depthoid)))
626 ;; Make a structure and set the header word and layout.
627 ;; LAYOUT-LENGTH is as returned by the like-named function.
628 (defun allocate-struct
629 (gspace layout &optional (layout-length (cold-layout-length layout))
630 is-layout)
631 ;; Count +1 for the header word when allocating.
632 (let ((des (allocate-object gspace (1+ layout-length)
633 sb!vm:instance-pointer-lowtag is-layout)))
634 ;; Length as stored in the header is the exact number of useful words
635 ;; that follow, as is customary. A padding word, if any is not "useful"
636 (write-header-word des
637 (logior layout-length
638 #!+compact-instance-header
639 (if layout (ash (descriptor-bits layout) 24) 0))
640 sb!vm:instance-header-widetag)
641 #!-compact-instance-header
642 (write-wordindexed des sb!vm:instance-slots-offset layout)
643 des))
645 ;;;; copying simple objects into the cold core
647 (defun base-string-to-core (string &optional (gspace *dynamic*))
648 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
649 core and return a descriptor to it."
650 ;; (Remember that the system convention for storage of strings leaves an
651 ;; extra null byte at the end to aid in call-out to C.)
652 (let* ((length (length string))
653 (des (allocate-vector-object gspace
654 sb!vm:n-byte-bits
655 (1+ length)
656 sb!vm:simple-base-string-widetag))
657 (bytes (gspace-bytes gspace))
658 (offset (+ (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
659 (descriptor-byte-offset des))))
660 (write-wordindexed des
661 sb!vm:vector-length-slot
662 (make-fixnum-descriptor length))
663 (dotimes (i length)
664 (setf (bvref bytes (+ offset i))
665 (sb!xc:char-code (aref string i))))
666 (setf (bvref bytes (+ offset length))
667 0) ; null string-termination character for C
668 des))
670 (defun base-string-from-core (descriptor)
671 (let* ((len (descriptor-fixnum
672 (read-wordindexed descriptor sb!vm:vector-length-slot)))
673 (str (make-string len))
674 (bytes (descriptor-mem descriptor)))
675 (dotimes (i len str)
676 (setf (aref str i)
677 (code-char (bvref bytes
678 (+ (descriptor-byte-offset descriptor)
679 (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
680 i)))))))
682 (defun bignum-to-core (n)
683 "Copy a bignum to the cold core."
684 (let* ((words (ceiling (1+ (integer-length n)) sb!vm:n-word-bits))
685 (handle
686 (allocate-header+object *dynamic* words sb!vm:bignum-widetag)))
687 (declare (fixnum words))
688 (do ((index 1 (1+ index))
689 (remainder n (ash remainder (- sb!vm:n-word-bits))))
690 ((> index words)
691 (unless (zerop (integer-length remainder))
692 ;; FIXME: Shouldn't this be a fatal error?
693 (warn "~W words of ~W were written, but ~W bits were left over."
694 words n remainder)))
695 (write-wordindexed/raw handle index
696 (ldb (byte sb!vm:n-word-bits 0) remainder)))
697 handle))
699 (defun bignum-from-core (descriptor)
700 (let ((n-words (ash (descriptor-bits (read-memory descriptor))
701 (- sb!vm:n-widetag-bits)))
702 (val 0))
703 (dotimes (i n-words val)
704 (let ((bits (read-bits-wordindexed descriptor
705 (+ i sb!vm:bignum-digits-offset))))
706 ;; sign-extend the highest word
707 (when (and (= i (1- n-words)) (logbitp (1- sb!vm:n-word-bits) bits))
708 (setq bits (dpb bits (byte sb!vm:n-word-bits 0) -1)))
709 (setq val (logior (ash bits (* i sb!vm:n-word-bits)) val))))))
711 (defun number-pair-to-core (first second type)
712 "Makes a number pair of TYPE (ratio or complex) and fills it in."
713 (let ((des (allocate-header+object *dynamic* 2 type)))
714 (write-wordindexed des 1 first)
715 (write-wordindexed des 2 second)
716 des))
718 (defun write-double-float-bits (address index x)
719 (let ((high-bits (double-float-high-bits x))
720 (low-bits (double-float-low-bits x)))
721 (ecase sb!vm::n-word-bits
723 (ecase sb!c:*backend-byte-order*
724 (:little-endian
725 (write-wordindexed/raw address index low-bits)
726 (write-wordindexed/raw address (1+ index) high-bits))
727 (:big-endian
728 (write-wordindexed/raw address index high-bits)
729 (write-wordindexed/raw address (1+ index) low-bits))))
731 (let ((bits (ecase sb!c:*backend-byte-order*
732 (:little-endian (logior low-bits (ash high-bits 32)))
733 ;; Just guessing.
734 #+nil (:big-endian (logior (logand high-bits #xffffffff)
735 (ash low-bits 32))))))
736 (write-wordindexed/raw address index bits))))
738 address))
740 (defun float-to-core (x)
741 (etypecase x
742 (single-float
743 ;; 64-bit platforms have immediate single-floats.
744 #!+64-bit
745 (make-random-descriptor (logior (ash (single-float-bits x) 32)
746 sb!vm::single-float-widetag))
747 #!-64-bit
748 (let ((des (allocate-header+object *dynamic*
749 (1- sb!vm:single-float-size)
750 sb!vm:single-float-widetag)))
751 (write-wordindexed/raw des sb!vm:single-float-value-slot
752 (single-float-bits x))
753 des))
754 (double-float
755 (let ((des (allocate-header+object *dynamic*
756 (1- sb!vm:double-float-size)
757 sb!vm:double-float-widetag)))
758 (write-double-float-bits des sb!vm:double-float-value-slot x)))))
760 (defun complex-single-float-to-core (num)
761 (declare (type (complex single-float) num))
762 (let ((des (allocate-header+object *dynamic*
763 (1- sb!vm:complex-single-float-size)
764 sb!vm:complex-single-float-widetag)))
765 #!-64-bit
766 (progn
767 (write-wordindexed/raw des sb!vm:complex-single-float-real-slot
768 (single-float-bits (realpart num)))
769 (write-wordindexed/raw des sb!vm:complex-single-float-imag-slot
770 (single-float-bits (imagpart num))))
771 #!+64-bit
772 (write-wordindexed/raw
773 des sb!vm:complex-single-float-data-slot
774 (logior (ldb (byte 32 0) (single-float-bits (realpart num)))
775 (ash (single-float-bits (imagpart num)) 32)))
776 des))
778 (defun complex-double-float-to-core (num)
779 (declare (type (complex double-float) num))
780 (let ((des (allocate-header+object *dynamic*
781 (1- sb!vm:complex-double-float-size)
782 sb!vm:complex-double-float-widetag)))
783 (write-double-float-bits des sb!vm:complex-double-float-real-slot
784 (realpart num))
785 (write-double-float-bits des sb!vm:complex-double-float-imag-slot
786 (imagpart num))))
788 ;;; Copy the given number to the core.
789 (defun number-to-core (number)
790 (typecase number
791 (integer (or (%fixnum-descriptor-if-possible number)
792 (bignum-to-core number)))
793 (ratio (number-pair-to-core (number-to-core (numerator number))
794 (number-to-core (denominator number))
795 sb!vm:ratio-widetag))
796 ((complex single-float) (complex-single-float-to-core number))
797 ((complex double-float) (complex-double-float-to-core number))
798 #!+long-float
799 ((complex long-float)
800 (error "~S isn't a cold-loadable number at all!" number))
801 (complex (number-pair-to-core (number-to-core (realpart number))
802 (number-to-core (imagpart number))
803 sb!vm:complex-widetag))
804 (float (float-to-core number))
805 (t (error "~S isn't a cold-loadable number at all!" number))))
807 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
808 (defun cold-cons (car cdr &optional (gspace *dynamic*))
809 (let ((dest (allocate-object gspace 2 sb!vm:list-pointer-lowtag)))
810 (write-wordindexed dest sb!vm:cons-car-slot car)
811 (write-wordindexed dest sb!vm:cons-cdr-slot cdr)
812 dest))
813 (defun list-to-core (list)
814 (let ((head *nil-descriptor*)
815 (tail nil))
816 ;; A recursive algorithm would have the first cons at the highest
817 ;; address. This way looks nicer when viewed in ldb.
818 (loop
819 (unless list (return head))
820 (let ((cons (cold-cons (pop list) *nil-descriptor*)))
821 (if tail (cold-rplacd tail cons) (setq head cons))
822 (setq tail cons)))))
823 (defun cold-list (&rest args) (list-to-core args))
824 (defun cold-list-length (list) ; but no circularity detection
825 ;; a recursive implementation uses too much stack for some Lisps
826 (let ((n 0))
827 (loop (if (cold-null list) (return n))
828 (incf n)
829 (setq list (cold-cdr list)))))
831 ;;; Make a simple-vector on the target that holds the specified
832 ;;; OBJECTS, and return its descriptor.
833 ;;; This is really "vectorify-list-into-core" but that's too wordy,
834 ;;; so historically it was "vector-in-core" which is a fine name.
835 (defun vector-in-core (objects &optional (gspace *dynamic*))
836 (let* ((size (length objects))
837 (result (allocate-vector-object gspace sb!vm:n-word-bits size
838 sb!vm:simple-vector-widetag)))
839 (dotimes (index size)
840 (write-wordindexed result (+ index sb!vm:vector-data-offset)
841 (pop objects)))
842 result))
843 (defun cold-svset (vector index value)
844 (let ((i (if (integerp index) index (descriptor-fixnum index))))
845 (write-wordindexed vector (+ i sb!vm:vector-data-offset) value)))
847 (setf (get 'vector :sb-cold-funcall-handler/for-value)
848 (lambda (&rest args) (vector-in-core args)))
850 (declaim (inline cold-vector-len cold-svref))
851 (defun cold-vector-len (vector)
852 (descriptor-fixnum (read-wordindexed vector sb!vm:vector-length-slot)))
853 (defun cold-svref (vector i)
854 (read-wordindexed vector (+ (if (integerp i) i (descriptor-fixnum i))
855 sb!vm:vector-data-offset)))
856 (defun cold-vector-elements-eq (a b)
857 (and (eql (cold-vector-len a) (cold-vector-len b))
858 (dotimes (k (cold-vector-len a) t)
859 (unless (descriptor= (cold-svref a k) (cold-svref b k))
860 (return nil)))))
861 (defun vector-from-core (descriptor &optional (transform #'identity))
862 (let* ((len (cold-vector-len descriptor))
863 (vector (make-array len)))
864 (dotimes (i len vector)
865 (setf (aref vector i) (funcall transform (cold-svref descriptor i))))))
867 ;;;; symbol magic
869 ;; Simulate *FREE-TLS-INDEX*. This is a count, not a displacement.
870 ;; In C, sizeof counts 1 word for the variable-length interrupt_contexts[]
871 ;; but primitive-object-size counts 0, so add 1, though in fact the C code
872 ;; implies that it might have overcounted by 1. We could make this agnostic
873 ;; of MAX-INTERRUPTS by moving the thread base register up by TLS-SIZE words,
874 ;; using negative offsets for all dynamically assigned indices.
875 (defvar *genesis-tls-counter*
876 (+ 1 sb!vm::max-interrupts
877 (sb!vm:primitive-object-size
878 (find 'sb!vm::thread sb!vm:*primitive-objects*
879 :key #'sb!vm:primitive-object-name))))
881 #!+sb-thread
882 (progn
883 ;; Assign SYMBOL the tls-index INDEX. SYMBOL must be a descriptor.
884 ;; This is a backend support routine, but the style within this file
885 ;; is to conditionalize by the target features.
886 (defun cold-assign-tls-index (symbol index)
887 #!+64-bit
888 (write-wordindexed/raw
889 symbol 0 (logior (ash index 32) (read-bits-wordindexed symbol 0)))
890 #!-64-bit
891 (write-wordindexed/raw symbol sb!vm:symbol-tls-index-slot index))
893 ;; Return SYMBOL's tls-index,
894 ;; choosing a new index if it doesn't have one yet.
895 (defun ensure-symbol-tls-index (symbol)
896 (let* ((cold-sym (cold-intern symbol))
897 (tls-index
898 #!+64-bit
899 (ldb (byte 32 32) (read-bits-wordindexed cold-sym 0))
900 #!-64-bit
901 (read-bits-wordindexed cold-sym sb!vm:symbol-tls-index-slot)))
902 (unless (plusp tls-index)
903 (let ((next (prog1 *genesis-tls-counter* (incf *genesis-tls-counter*))))
904 (setq tls-index (ash next sb!vm:word-shift))
905 (cold-assign-tls-index cold-sym tls-index)))
906 tls-index)))
908 ;; A table of special variable names which get known TLS indices.
909 ;; Some of them are mapped onto 'struct thread' and have pre-determined offsets.
910 ;; Others are static symbols used with bind_variable() in the C runtime,
911 ;; and might not, in the absence of this table, get an index assigned by genesis
912 ;; depending on whether the cross-compiler used the BIND vop on them.
913 ;; Indices for those static symbols can be chosen arbitrarily, which is to say
914 ;; the value doesn't matter but must update the tls-counter correctly.
915 ;; All symbols other than the ones in this table get the indices assigned
916 ;; by the fasloader on demand.
917 #!+sb-thread
918 (defvar *known-tls-symbols*
919 ;; FIXME: no mechanism exists to determine which static symbols C code will
920 ;; dynamically bind. TLS is a finite resource, and wasting indices for all
921 ;; static symbols isn't the best idea. This list was hand-made with 'grep'.
922 '(sb!vm:*alloc-signal*
923 sb!sys:*allow-with-interrupts*
924 sb!vm:*current-catch-block*
925 sb!vm::*current-unwind-protect-block*
926 sb!kernel:*free-interrupt-context-index*
927 sb!kernel:*gc-inhibit*
928 sb!kernel:*gc-pending*
929 sb!impl::*gc-safe*
930 sb!impl::*in-safepoint*
931 sb!sys:*interrupt-pending*
932 sb!sys:*interrupts-enabled*
933 sb!vm::*pinned-objects*
934 sb!kernel:*restart-clusters*
935 sb!kernel:*stop-for-gc-pending*
936 #!+sb-thruption
937 sb!sys:*thruption-pending*))
939 ;;; Symbol print names are coalesced by string=.
940 ;;; This is valid because it is an error to modify a print name.
941 (defvar *symbol-name-strings* (make-hash-table :test 'equal))
943 (defvar *cold-symbol-gspace* (or #!+immobile-space '*immobile-fixedobj* '*dynamic*))
945 ;;; Allocate (and initialize) a symbol.
946 (defun allocate-symbol (name interned
947 &key (gspace (symbol-value *cold-symbol-gspace*)))
948 (declare (simple-string name))
949 (declare (ignore interned))
950 #!+immobile-space
951 (when (and (eq gspace *immobile-fixedobj*) (char/= (char name 0) #\*))
952 ;; immobile symbols that aren't likely to be special vars
953 ;; should go in regular dynamic space until a de-frag pass is
954 ;; implemented for save-lisp-and-die. Otherwise they create
955 ;; tons of holes all over the immobile space.
956 (setq gspace *dynamic*))
957 (let ((symbol (allocate-header+object
958 gspace (1- sb!vm:symbol-size)
959 sb!vm:symbol-header-widetag
960 ;; Tell the allocator what kind of symbol page to prefer.
961 ;; This only affects gc performance, not correctness.
962 0)))
963 (write-wordindexed symbol sb!vm:symbol-value-slot *unbound-marker*)
964 (write-wordindexed symbol sb!vm:symbol-hash-slot (make-fixnum-descriptor 0))
965 (write-wordindexed symbol sb!vm:symbol-info-slot *nil-descriptor*)
966 (write-wordindexed symbol sb!vm:symbol-name-slot
967 (or (gethash name *symbol-name-strings*)
968 (setf (gethash name *symbol-name-strings*)
969 (base-string-to-core name *dynamic*))))
970 (write-wordindexed symbol sb!vm:symbol-package-slot *nil-descriptor*)
971 symbol))
973 #!+sb-thread
974 (defun assign-tls-index (symbol cold-symbol)
975 (let ((index (info :variable :wired-tls symbol)))
976 (cond ((integerp index) ; thread slot
977 (cold-assign-tls-index cold-symbol index))
978 ((memq symbol *known-tls-symbols*)
979 ;; symbols without which the C runtime could not start
980 (shiftf index *genesis-tls-counter* (1+ *genesis-tls-counter*))
981 (cold-assign-tls-index cold-symbol (ash index sb!vm:word-shift))))))
983 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
984 ;;; descriptor of a cold symbol or (in an abbreviation for the
985 ;;; most common usage pattern) an ordinary symbol, which will be
986 ;;; automatically cold-interned.
987 (declaim (ftype (function ((or symbol descriptor) descriptor)) cold-set))
988 (defun cold-set (symbol-or-symbol-des value)
989 (let ((symbol-des (etypecase symbol-or-symbol-des
990 (descriptor symbol-or-symbol-des)
991 (symbol (cold-intern symbol-or-symbol-des)))))
992 (write-wordindexed symbol-des sb!vm:symbol-value-slot value)))
993 (defun cold-symbol-value (symbol)
994 (let ((val (read-wordindexed (cold-intern symbol) sb!vm:symbol-value-slot)))
995 (if (= (descriptor-bits val) sb!vm:unbound-marker-widetag)
996 (unbound-cold-symbol-handler symbol)
997 val)))
998 (defun cold-fdefn-fun (cold-fdefn)
999 (read-wordindexed cold-fdefn sb!vm:fdefn-fun-slot))
1001 (defun unbound-cold-symbol-handler (symbol)
1002 (let ((host-val (and (boundp symbol) (symbol-value symbol))))
1003 (if (typep host-val 'sb!kernel:named-type)
1004 (let ((target-val (ctype-to-core (sb!kernel:named-type-name host-val)
1005 host-val)))
1006 ;; Though it looks complicated to assign cold symbols on demand,
1007 ;; it avoids writing code to build the layout of NAMED-TYPE in the
1008 ;; way we build other primordial stuff such as layout-of-layout.
1009 (cold-set symbol target-val)
1010 target-val)
1011 (error "Taking Cold-symbol-value of unbound symbol ~S" symbol))))
1013 ;;;; layouts and type system pre-initialization
1015 ;;; Since we want to be able to dump structure constants and
1016 ;;; predicates with reference layouts, we need to create layouts at
1017 ;;; cold-load time. We use the name to intern layouts by, and dump a
1018 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
1019 ;;; initialization can find them. The only thing that's tricky [sic --
1020 ;;; WHN 19990816] is initializing layout's layout, which must point to
1021 ;;; itself.
1023 ;;; a map from name as a host symbol to the descriptor of its target layout
1024 (defvar *cold-layouts*)
1026 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
1027 ;;; mapping
1028 (defvar *cold-layout-names*)
1030 ;;; the descriptor for layout's layout (needed when making layouts)
1031 (defvar *layout-layout*)
1033 (defvar *known-structure-classoids*)
1035 (defconstant target-layout-length
1036 ;; LAYOUT-LENGTH counts the number of words in an instance,
1037 ;; including the layout itself as 1 word
1038 (layout-length *host-layout-of-layout*))
1040 ;;; Trivial methods [sic] require that we sort possible methods by the depthoid.
1041 ;;; Most of the objects printed in cold-init are ordered hierarchically in our
1042 ;;; type lattice; the major exceptions are ARRAY and VECTOR at depthoid -1.
1043 ;;; Of course we need to print VECTORs because a STRING is a vector,
1044 ;;; and vector has to precede ARRAY. Kludge it for now.
1045 (defun class-depthoid (class-name) ; DEPTHOID-ish thing, any which way you can
1046 (case class-name
1047 (vector 0.5)
1048 (array 0.25)
1049 ;; The depthoid of CONDITION has to be faked. The proper value is 1.
1050 ;; But STRUCTURE-OBJECT is also at depthoid 1, and its predicate
1051 ;; is %INSTANCEP (which is too weak), so to select the correct method
1052 ;; we have to make CONDITION more specific.
1053 ;; In reality it is type disjoint from structure-object.
1054 (condition 2)
1056 (let ((target-layout (gethash class-name *cold-layouts*)))
1057 (if target-layout
1058 (cold-layout-depthoid target-layout)
1059 (let ((host-layout (find-layout class-name)))
1060 (if (layout-invalid host-layout)
1061 (error "~S has neither a host not target layout" class-name)
1062 (layout-depthoid host-layout))))))))
1064 ;;; Return a list of names created from the cold layout INHERITS data
1065 ;;; in X.
1066 (defun listify-cold-inherits (x)
1067 (map 'list (lambda (cold-layout)
1068 (or (gethash (descriptor-bits cold-layout) *cold-layout-names*)
1069 (error "~S is not the descriptor of a cold-layout" cold-layout)))
1070 (vector-from-core x)))
1072 ;;; COLD-DD-SLOTS is a cold descriptor for the list of slots
1073 ;;; in a cold defstruct-description. INDEX is a DSD-INDEX.
1074 ;;; Return the host's accessor name for the host image of that slot.
1075 (defun dsd-accessor-from-cold-slots (cold-dd-slots desired-index)
1076 (let* ((dsd-slots (dd-slots
1077 (find-defstruct-description 'defstruct-slot-description)))
1078 (index-slot
1079 (dsd-index (find 'sb!kernel::index dsd-slots :key #'dsd-name)))
1080 (accessor-fun-name-slot
1081 (dsd-index (find 'sb!kernel::accessor-name dsd-slots :key #'dsd-name))))
1082 (do ((list cold-dd-slots (cold-cdr list)))
1083 ((cold-null list))
1084 (when (= (descriptor-fixnum
1085 (read-wordindexed (cold-car list)
1086 (+ sb!vm:instance-slots-offset index-slot)))
1087 desired-index)
1088 (return
1089 (warm-symbol
1090 (read-wordindexed (cold-car list)
1091 (+ sb!vm:instance-slots-offset
1092 accessor-fun-name-slot))))))))
1094 (flet ((get-slots (host-layout-or-type)
1095 (etypecase host-layout-or-type
1096 (layout (dd-slots (layout-info host-layout-or-type)))
1097 (symbol (dd-slots-from-core host-layout-or-type))))
1098 (get-slot-index (slots initarg)
1099 (+ sb!vm:instance-slots-offset
1100 (if (descriptor-p slots)
1101 (do ((dsd-layout (find-layout 'defstruct-slot-description))
1102 (slots slots (cold-cdr slots)))
1103 ((cold-null slots) (error "No slot for ~S" initarg))
1104 (let* ((dsd (cold-car slots))
1105 (slot-name (read-slot dsd dsd-layout :name)))
1106 (when (eq (keywordicate (warm-symbol slot-name)) initarg)
1107 ;; Untagged slots are not accessible during cold-load
1108 (aver (eql (descriptor-fixnum
1109 (read-slot dsd dsd-layout :%raw-type)) -1))
1110 (return (descriptor-fixnum
1111 (read-slot dsd dsd-layout :index))))))
1112 (let ((dsd (find initarg slots
1113 :test (lambda (x y)
1114 (eq x (keywordicate (dsd-name y)))))))
1115 (aver (eq (dsd-raw-type dsd) t)) ; Same as above: no can do.
1116 (dsd-index dsd))))))
1117 (defun write-slots (cold-object host-layout-or-type &rest assignments)
1118 (aver (evenp (length assignments)))
1119 (let ((slots (get-slots host-layout-or-type)))
1120 (loop for (initarg value) on assignments by #'cddr
1121 do (write-wordindexed
1122 cold-object (get-slot-index slots initarg) value)))
1123 cold-object)
1125 ;; For symmetry, the reader takes an initarg, not a slot name.
1126 (defun read-slot (cold-object host-layout-or-type slot-initarg)
1127 (let ((slots (get-slots host-layout-or-type)))
1128 (read-wordindexed cold-object (get-slot-index slots slot-initarg)))))
1130 ;; Given a TYPE-NAME of a structure-class, find its defstruct-description
1131 ;; as a target descriptor, and return the slot list as a target descriptor.
1132 (defun dd-slots-from-core (type-name)
1133 (let* ((host-dd-layout (find-layout 'defstruct-description))
1134 (target-dd
1135 ;; This is inefficient, but not enough so to worry about.
1136 (or (car (assoc (cold-intern type-name) *known-structure-classoids*
1137 :key (lambda (x) (read-slot x host-dd-layout :name))
1138 :test #'descriptor=))
1139 (error "No known layout for ~S" type-name))))
1140 (read-slot target-dd host-dd-layout :slots)))
1142 (defvar *simple-vector-0-descriptor*)
1143 (defvar *vacuous-slot-table*)
1144 (defvar *cold-layout-gspace* (or #!+immobile-space '*immobile-fixedobj* '*dynamic*))
1145 (declaim (ftype (function (symbol descriptor descriptor descriptor descriptor)
1146 descriptor)
1147 make-cold-layout))
1148 (defun make-cold-layout (name length inherits depthoid bitmap)
1149 (let ((result (allocate-struct (symbol-value *cold-layout-gspace*) *layout-layout*
1150 target-layout-length t)))
1151 ;; Don't set the CLOS hash value: done in cold-init instead.
1153 ;; Set other slot values.
1155 ;; leave CLASSOID uninitialized for now
1156 (multiple-value-call
1157 #'write-slots result *host-layout-of-layout*
1158 :invalid *nil-descriptor*
1159 :inherits inherits
1160 :depthoid depthoid
1161 :length length
1162 :info *nil-descriptor*
1163 :pure *nil-descriptor*
1164 :bitmap bitmap
1165 ;; Nothing in cold-init needs to call EQUALP on a structure with raw slots,
1166 ;; but for type-correctness this slot needs to be a simple-vector.
1167 :equalp-tests *simple-vector-0-descriptor*
1168 :source-location *nil-descriptor*
1169 :%for-std-class-b (make-fixnum-descriptor 0)
1170 :slot-list *nil-descriptor*
1171 (if (member name '(null list symbol))
1172 ;; Assign an empty slot-table. Why this is done only for three
1173 ;; classoids is ... too complicated to explain here in a few words,
1174 ;; but revision 18c239205d9349abc017b07e7894a710835c5205 broke it.
1175 ;; Keep this in sync with MAKE-SLOT-TABLE in pcl/slots-boot.
1176 (values :slot-table (if (boundp '*vacuous-slot-table*)
1177 *vacuous-slot-table*
1178 (setq *vacuous-slot-table*
1179 (host-constant-to-core '#(1 nil)))))
1180 (values)))
1182 (setf (gethash (descriptor-bits result) *cold-layout-names*) name
1183 (gethash name *cold-layouts*) result)))
1185 (defun predicate-for-specializer (type-name)
1186 (let ((classoid (find-classoid type-name nil)))
1187 (typecase classoid
1188 (structure-classoid
1189 (cond ((dd-predicate-name (layout-info (classoid-layout classoid))))
1190 ;; All early INSTANCEs should be STRUCTURE-OBJECTs.
1191 ;; Except: see hack for CONDITIONs in CLASS-DEPTHOID.
1192 ((eq type-name 'structure-object) 'sb!kernel:%instancep)))
1193 (built-in-classoid
1194 (let ((translation (specifier-type type-name)))
1195 (aver (not (contains-unknown-type-p translation)))
1196 (let ((predicate (find translation sb!c::*backend-type-predicates*
1197 :test #'type= :key #'car)))
1198 (cond (predicate (cdr predicate))
1199 ((eq type-name 't) 'sb!int:constantly-t)
1200 (t (error "No predicate for builtin: ~S" type-name))))))
1201 (null
1202 #+nil (format t "~&; PREDICATE-FOR-SPECIALIZER: no classoid for ~S~%"
1203 type-name)
1204 (case type-name
1205 (condition 'sb!kernel::!condition-p))))))
1207 ;;; Convert SPECIFIER (equivalently OBJ) to its representation as a ctype
1208 ;;; in the cold core.
1209 (defvar *ctype-cache*)
1211 (defvar *ctype-nullified-slots* nil)
1212 (defvar *built-in-classoid-nullified-slots* nil)
1214 ;; This function is memoized because it's essentially a constant,
1215 ;; but *nil-descriptor* isn't initialized by the time it's defined.
1216 (defun get-exceptional-slots (obj-type)
1217 (flet ((index (classoid-name slot-name)
1218 (dsd-index (find slot-name
1219 (dd-slots (find-defstruct-description classoid-name))
1220 :key #'dsd-name))))
1221 (case obj-type
1222 (built-in-classoid
1223 (or *built-in-classoid-nullified-slots*
1224 (setq *built-in-classoid-nullified-slots*
1225 (append (get-exceptional-slots 'ctype)
1226 (list (cons (index 'built-in-classoid 'sb!kernel::subclasses)
1227 *nil-descriptor*)
1228 (cons (index 'built-in-classoid 'layout)
1229 *nil-descriptor*))))))
1231 (or *ctype-nullified-slots*
1232 (setq *ctype-nullified-slots*
1233 (list (cons (index 'ctype 'sb!kernel::class-info)
1234 *nil-descriptor*))))))))
1236 (defun ctype-to-core (specifier obj)
1237 (declare (type ctype obj))
1238 (if (classoid-p obj)
1239 (let* ((cell (cold-find-classoid-cell (classoid-name obj) :create t))
1240 (cold-classoid
1241 (read-slot cell (find-layout 'sb!kernel::classoid-cell) :classoid)))
1242 (unless (cold-null cold-classoid)
1243 (return-from ctype-to-core cold-classoid)))
1244 ;; CTYPEs can't be TYPE=-hashed, but specifiers can be EQUAL-hashed.
1245 ;; Don't check the cache for classoids though; that would be wrong.
1246 ;; e.g. named-type T and classoid T both unparse to T.
1247 (awhen (gethash specifier *ctype-cache*)
1248 (return-from ctype-to-core it)))
1249 (let ((result
1250 (ctype-to-core-helper
1252 (lambda (obj)
1253 (typecase obj
1254 (xset (ctype-to-core-helper obj nil nil))
1255 (ctype (ctype-to-core (type-specifier obj) obj))))
1256 (get-exceptional-slots (type-of obj)))))
1257 (let ((type-class-vector
1258 (cold-symbol-value 'sb!kernel::*type-classes*))
1259 (index (position (sb!kernel::type-class-info obj)
1260 sb!kernel::*type-classes*)))
1261 ;; Push this instance into the list of fixups for its type class
1262 (cold-svset type-class-vector index
1263 (cold-cons result (cold-svref type-class-vector index))))
1264 (if (classoid-p obj)
1265 ;; Place this classoid into its clasoid-cell.
1266 (let ((cell (cold-find-classoid-cell (classoid-name obj) :create t)))
1267 (write-slots cell (find-layout 'sb!kernel::classoid-cell)
1268 :classoid result))
1269 ;; Otherwise put it in the general cache
1270 (setf (gethash specifier *ctype-cache*) result))
1271 result))
1273 (defun ctype-to-core-helper (obj obj-to-core-helper exceptional-slots)
1274 (let* ((host-type (type-of obj))
1275 (target-layout (or (gethash host-type *cold-layouts*)
1276 (error "No target layout for ~S" obj)))
1277 (result (allocate-struct *dynamic* target-layout))
1278 (cold-dd-slots (dd-slots-from-core host-type)))
1279 (aver (eql (layout-bitmap (find-layout host-type))
1280 sb!kernel::+layout-all-tagged+))
1281 ;; Dump the slots.
1282 (do ((len (cold-layout-length target-layout))
1283 (index sb!vm:instance-data-start (1+ index)))
1284 ((= index len) result)
1285 (write-wordindexed
1286 result
1287 (+ sb!vm:instance-slots-offset index)
1288 (acond ((assq index exceptional-slots) (cdr it))
1289 (t (host-constant-to-core
1290 (funcall (dsd-accessor-from-cold-slots cold-dd-slots index)
1291 obj)
1292 obj-to-core-helper)))))))
1294 ;; This is called to backpatch two small sets of objects:
1295 ;; - layouts created before layout-of-layout is made (3 counting LAYOUT itself)
1296 ;; - a small number of classoid-cells (~ 4).
1297 (defun set-instance-layout (thing layout)
1298 #!+compact-instance-header
1299 ;; High half of the header points to the layout
1300 (write-wordindexed/raw thing 0 (logior (ash (descriptor-bits layout) 32)
1301 (read-bits-wordindexed thing 0)))
1302 #!-compact-instance-header
1303 ;; Word following the header is the layout
1304 (write-wordindexed thing sb!vm:instance-slots-offset layout))
1306 (defun cold-layout-of (cold-struct)
1307 #!+compact-instance-header
1308 (let ((bits (ash (read-bits-wordindexed cold-struct 0) -32)))
1309 (if (zerop bits) *nil-descriptor* (make-random-descriptor bits)))
1310 #!-compact-instance-header
1311 (read-wordindexed cold-struct sb!vm:instance-slots-offset))
1313 (defun initialize-layouts ()
1314 (clrhash *cold-layouts*)
1315 ;; This assertion is due to the fact that MAKE-COLD-LAYOUT does not
1316 ;; know how to set any raw slots.
1317 (aver (eql (layout-bitmap *host-layout-of-layout*)
1318 sb!kernel::+layout-all-tagged+))
1319 (setq *layout-layout* (make-fixnum-descriptor 0))
1320 (flet ((chill-layout (name &rest inherits)
1321 ;; Check that the number of specified INHERITS matches
1322 ;; the length of the layout's inherits in the cross-compiler.
1323 (let ((warm-layout (classoid-layout (find-classoid name))))
1324 (assert (eql (length (layout-inherits warm-layout))
1325 (length inherits)))
1326 (make-cold-layout
1327 name
1328 (number-to-core (layout-length warm-layout))
1329 (vector-in-core inherits)
1330 (number-to-core (layout-depthoid warm-layout))
1331 (number-to-core (layout-bitmap warm-layout))))))
1332 (let* ((t-layout (chill-layout 't))
1333 (s-o-layout (chill-layout 'structure-object t-layout)))
1334 (setf *layout-layout* (chill-layout 'layout t-layout s-o-layout))
1335 (dolist (layout (list t-layout s-o-layout *layout-layout*))
1336 (set-instance-layout layout *layout-layout*))
1337 (chill-layout 'package t-layout s-o-layout))))
1339 ;;;; interning symbols in the cold image
1341 ;;; a map from package name as a host string to
1342 ;;; (cold-package-descriptor . (external-symbols . internal-symbols))
1343 (defvar *cold-package-symbols*)
1344 (declaim (type hash-table *cold-package-symbols*))
1346 (setf (get 'find-package :sb-cold-funcall-handler/for-value)
1347 (lambda (descriptor &aux (name (base-string-from-core descriptor)))
1348 (or (car (gethash name *cold-package-symbols*))
1349 (error "Genesis could not find a target package named ~S" name))))
1351 (defvar *classoid-cells*)
1352 (defun cold-find-classoid-cell (name &key create)
1353 (aver (eq create t))
1354 (or (gethash name *classoid-cells*)
1355 (let ((layout (gethash 'sb!kernel::classoid-cell *cold-layouts*)) ; ok if nil
1356 (host-layout (find-layout 'sb!kernel::classoid-cell)))
1357 (setf (gethash name *classoid-cells*)
1358 (write-slots (allocate-struct *dynamic* layout
1359 (layout-length host-layout))
1360 host-layout
1361 :name name
1362 :pcl-class *nil-descriptor*
1363 :classoid *nil-descriptor*)))))
1365 (setf (get 'find-classoid-cell :sb-cold-funcall-handler/for-value)
1366 #'cold-find-classoid-cell)
1368 ;;; a map from descriptors to symbols, so that we can back up. The key
1369 ;;; is the address in the target core.
1370 (defvar *cold-symbols*)
1371 (declaim (type hash-table *cold-symbols*))
1373 (defun initialize-packages ()
1374 (let ((package-data-list
1375 ;; docstrings are set in src/cold/warm. It would work to do it here,
1376 ;; but seems preferable not to saddle Genesis with such responsibility.
1377 (list* (sb-cold:make-package-data :name "COMMON-LISP" :doc nil)
1378 (sb-cold:make-package-data :name "KEYWORD" :doc nil)
1379 ;; ANSI encourages us to put extension packages
1380 ;; in the USE list of COMMON-LISP-USER.
1381 (sb-cold:make-package-data
1382 :name "COMMON-LISP-USER" :doc nil
1383 :use '("COMMON-LISP" "SB!ALIEN" "SB!DEBUG" "SB!EXT" "SB!GRAY" "SB!PROFILE"))
1384 (sb-cold::package-list-for-genesis)))
1385 (package-layout (find-layout 'package))
1386 (target-pkg-list nil))
1387 (labels ((init-cold-package (name &optional docstring)
1388 (let ((cold-package (allocate-struct (symbol-value *cold-layout-gspace*)
1389 (gethash 'package *cold-layouts*))))
1390 (setf (gethash name *cold-package-symbols*)
1391 (list* cold-package nil nil))
1392 ;; Initialize string slots
1393 (write-slots cold-package package-layout
1394 :%name (base-string-to-core
1395 (target-package-name name))
1396 :%nicknames (chill-nicknames name)
1397 :doc-string (if docstring
1398 (base-string-to-core docstring)
1399 *nil-descriptor*)
1400 :%use-list *nil-descriptor*)
1401 ;; the cddr of this will accumulate the 'used-by' package list
1402 (push (list name cold-package) target-pkg-list)))
1403 (target-package-name (string)
1404 (if (eql (mismatch string "SB!") 3)
1405 (concatenate 'string "SB-" (subseq string 3))
1406 string))
1407 (chill-nicknames (pkg-name)
1408 (let ((result *nil-descriptor*))
1409 ;; Make the package nickname lists for the standard packages
1410 ;; be the minimum specified by ANSI, regardless of what value
1411 ;; the cross-compilation host happens to use.
1412 ;; For packages other than the standard packages, the nickname
1413 ;; list was specified by our package setup code, and we can just
1414 ;; propagate the current state into the target.
1415 (dolist (nickname
1416 (cond ((string= pkg-name "COMMON-LISP") '("CL"))
1417 ((string= pkg-name "COMMON-LISP-USER")
1418 '("CL-USER"))
1419 ((string= pkg-name "KEYWORD") '())
1421 ;; 'package-data-list' contains no nicknames.
1422 ;; (See comment in 'set-up-cold-packages')
1423 (aver (null (package-nicknames
1424 (find-package pkg-name))))
1425 nil))
1426 result)
1427 (cold-push (base-string-to-core nickname) result))))
1428 (find-cold-package (name)
1429 (cadr (find-package-cell name)))
1430 (find-package-cell (name)
1431 (or (assoc (if (string= name "CL") "COMMON-LISP" name)
1432 target-pkg-list :test #'string=)
1433 (error "No cold package named ~S" name))))
1434 ;; pass 1: make all proto-packages
1435 (dolist (pd package-data-list)
1436 (init-cold-package (sb-cold:package-data-name pd)
1437 #!+sb-doc(sb-cold::package-data-doc pd)))
1438 ;; pass 2: set the 'use' lists and collect the 'used-by' lists
1439 (dolist (pd package-data-list)
1440 (let ((this (find-cold-package (sb-cold:package-data-name pd)))
1441 (use nil))
1442 (dolist (that (sb-cold:package-data-use pd))
1443 (let ((cell (find-package-cell that)))
1444 (push (cadr cell) use)
1445 (push this (cddr cell))))
1446 (write-slots this package-layout
1447 :%use-list (list-to-core (nreverse use)))))
1448 ;; pass 3: set the 'used-by' lists
1449 (dolist (cell target-pkg-list)
1450 (write-slots (cadr cell) package-layout
1451 :%used-by-list (list-to-core (cddr cell)))))))
1453 ;;; sanity check for a symbol we're about to create on the target
1455 ;;; Make sure that the symbol has an appropriate package. In
1456 ;;; particular, catch the so-easy-to-make error of typing something
1457 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1458 ;;; need is SB!KERNEL:%BYTE-BLT.
1459 (defun package-ok-for-target-symbol-p (package)
1460 (let ((package-name (package-name package)))
1462 ;; Cold interning things in these standard packages is OK. (Cold
1463 ;; interning things in the other standard package, CL-USER, isn't
1464 ;; OK. We just use CL-USER to expose symbols whose homes are in
1465 ;; other packages. Thus, trying to cold intern a symbol whose
1466 ;; home package is CL-USER probably means that a coding error has
1467 ;; been made somewhere.)
1468 (find package-name '("COMMON-LISP" "KEYWORD") :test #'string=)
1469 ;; Cold interning something in one of our target-code packages,
1470 ;; which are ever-so-rigorously-and-elegantly distinguished by
1471 ;; this prefix on their names, is OK too.
1472 (string= package-name "SB!" :end1 3 :end2 3)
1473 ;; This one is OK too, since it ends up being COMMON-LISP on the
1474 ;; target.
1475 (string= package-name "SB-XC")
1476 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1477 ;; package in the xc host? something we can't think of
1478 ;; a valid reason to cold intern, anyway...)
1481 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1483 ;;; Most host symbols we dump onto the target are created by SBCL
1484 ;;; itself, so that as long as we avoid gratuitously
1485 ;;; cross-compilation-unfriendly hacks, it just happens that their
1486 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1487 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1488 ;;; in the COMMON-LISP package, where we don't get to create the
1489 ;;; symbols but instead have to use the ones that the xc host created.
1490 ;;; In particular, while ANSI specifies which symbols are exported
1491 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1492 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1493 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1494 ;;; symbols in the CLOS package).
1495 (defun symbol-package-for-target-symbol (symbol)
1496 ;; We want to catch weird symbols like CLISP's
1497 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1498 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1499 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1500 (multiple-value-bind (cl-symbol cl-status)
1501 (find-symbol (symbol-name symbol) *cl-package*)
1502 (if (and (eq symbol cl-symbol)
1503 (eq cl-status :external))
1504 ;; special case, to work around possible xc host weirdness
1505 ;; in COMMON-LISP package
1506 *cl-package*
1507 ;; ordinary case
1508 (let ((result (symbol-package symbol)))
1509 (unless (package-ok-for-target-symbol-p result)
1510 (bug "~A in bad package for target: ~A" symbol result))
1511 result))))
1513 (defvar *uninterned-symbol-table* (make-hash-table :test #'equal))
1514 ;; This coalesces references to uninterned symbols, which is allowed because
1515 ;; "similar-as-constant" is defined by string comparison, and since we only have
1516 ;; base-strings during Genesis, there is no concern about upgraded array type.
1517 ;; There is a subtlety of whether coalescing may occur across files
1518 ;; - the target compiler doesn't and couldn't - but here it doesn't matter.
1519 (defun get-uninterned-symbol (name)
1520 (or (gethash name *uninterned-symbol-table*)
1521 (let ((cold-symbol (allocate-symbol name nil)))
1522 (setf (gethash name *uninterned-symbol-table*) cold-symbol))))
1524 ;;; Dump the target representation of HOST-VALUE,
1525 ;;; the type of which is in a restrictive set.
1526 (defun host-constant-to-core (host-value &optional helper)
1527 (let ((visited (make-hash-table :test #'eq)))
1528 (named-let target-representation ((value host-value))
1529 (unless (typep value '(or symbol number descriptor))
1530 (let ((found (gethash value visited)))
1531 (cond ((eq found :pending)
1532 (bug "circular constant?")) ; Circularity not permitted
1533 (found
1534 (return-from target-representation found))))
1535 (setf (gethash value visited) :pending))
1536 (setf (gethash value visited)
1537 (typecase value
1538 (descriptor value)
1539 (symbol (if (symbol-package value)
1540 (cold-intern value)
1541 (get-uninterned-symbol (string value))))
1542 (number (number-to-core value))
1543 (string (base-string-to-core value))
1544 (cons (cold-cons (target-representation (car value))
1545 (target-representation (cdr value))))
1546 (simple-vector
1547 (vector-in-core (map 'list #'target-representation value)))
1549 (or (and helper (funcall helper value))
1550 (error "host-constant-to-core: can't convert ~S"
1551 value))))))))
1553 ;; Look up the target's descriptor for #'FUN where FUN is a host symbol.
1554 (defun target-symbol-function (symbol)
1555 (let ((f (cold-fdefn-fun (cold-fdefinition-object symbol))))
1556 ;; It works only if DEFUN F was seen first.
1557 (aver (not (cold-null f)))
1560 ;;; Create the effect of executing a (MAKE-ARRAY) call on the target.
1561 ;;; This is for initializing a restricted set of vector constants
1562 ;;; whose contents are typically function pointers.
1563 (defun emulate-target-make-array (form)
1564 (destructuring-bind (size-expr &key initial-element) (cdr form)
1565 (let* ((size (eval size-expr))
1566 (result (allocate-vector-object *dynamic* sb!vm:n-word-bits size
1567 sb!vm:simple-vector-widetag)))
1568 (aver (integerp size))
1569 (unless (eql initial-element 0)
1570 (let ((target-initial-element
1571 (etypecase initial-element
1572 ((cons (eql function) (cons symbol null))
1573 (target-symbol-function (second initial-element)))
1574 (null *nil-descriptor*)
1575 ;; Insert more types here ...
1577 (dotimes (index size)
1578 (cold-svset result (make-fixnum-descriptor index)
1579 target-initial-element))))
1580 result)))
1582 ;; Return a target object produced by emulating evaluation of EXPR
1583 ;; with *package* set to ORIGINAL-PACKAGE.
1584 (defun emulate-target-eval (expr original-package)
1585 (let ((*package* (find-package original-package)))
1586 ;; For most things, just call EVAL and dump the host object's
1587 ;; target representation. But with MAKE-ARRAY we allow that the
1588 ;; initial-element might not be evaluable in the host.
1589 ;; Embedded MAKE-ARRAY is kept as-is because we don't "look into"
1590 ;; the EXPR, just hope that it works.
1591 (if (typep expr '(cons (eql make-array)))
1592 (emulate-target-make-array expr)
1593 (host-constant-to-core (eval expr)))))
1595 ;;; Return a handle on an interned symbol. If necessary allocate the
1596 ;;; symbol and record its home package.
1597 (defun cold-intern (symbol
1598 &key (access nil)
1599 (gspace (symbol-value *cold-symbol-gspace*))
1600 &aux (package (symbol-package-for-target-symbol symbol)))
1601 (aver (package-ok-for-target-symbol-p package))
1603 ;; Anything on the cross-compilation host which refers to the target
1604 ;; machinery through the host SB-XC package should be translated to
1605 ;; something on the target which refers to the same machinery
1606 ;; through the target COMMON-LISP package.
1607 (let ((p (find-package "SB-XC")))
1608 (when (eq package p)
1609 (setf package *cl-package*))
1610 (when (eq (symbol-package symbol) p)
1611 (setf symbol (intern (symbol-name symbol) *cl-package*))))
1613 (or (get symbol 'cold-intern-info)
1614 (let ((handle (allocate-symbol (symbol-name symbol) t :gspace gspace)))
1615 (setf (get symbol 'cold-intern-info) handle)
1616 ;; maintain reverse map from target descriptor to host symbol
1617 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1618 (let ((pkg-info (or (gethash (package-name package) *cold-package-symbols*)
1619 (error "No target package descriptor for ~S" package))))
1620 (write-wordindexed handle sb!vm:symbol-package-slot (car pkg-info))
1621 (record-accessibility
1622 (or access (nth-value 1 (find-symbol (symbol-name symbol) package)))
1623 pkg-info handle package symbol))
1624 #!+sb-thread
1625 (assign-tls-index symbol handle)
1626 (acond ((eq package *keyword-package*)
1627 (setq access :external)
1628 (cold-set handle handle))
1629 ((assoc symbol sb-cold:*symbol-values-for-genesis*)
1630 (cold-set handle (destructuring-bind (expr . package) (cdr it)
1631 (emulate-target-eval expr package)))))
1632 handle)))
1634 (defun record-accessibility (accessibility target-pkg-info symbol-descriptor
1635 &optional host-package host-symbol)
1636 (let ((access-lists (cdr target-pkg-info)))
1637 (case accessibility
1638 (:external (push symbol-descriptor (car access-lists)))
1639 (:internal (push symbol-descriptor (cdr access-lists)))
1640 (t (error "~S inaccessible in package ~S" host-symbol host-package)))))
1642 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1643 ;;; It might be nice to put NIL on a readonly page by itself to prevent unsafe
1644 ;;; code from destroying the world with (RPLACx nil 'kablooey)
1645 (defun make-nil-descriptor ()
1646 (let* ((des (allocate-header+object *static* sb!vm:symbol-size 0))
1647 (result (make-descriptor (+ (descriptor-bits des)
1648 (* 2 sb!vm:n-word-bytes)
1649 (- sb!vm:list-pointer-lowtag
1650 sb!vm:other-pointer-lowtag)))))
1651 (write-wordindexed des
1653 (make-other-immediate-descriptor
1655 sb!vm:symbol-header-widetag))
1656 (write-wordindexed des
1657 (+ 1 sb!vm:symbol-value-slot)
1658 result)
1659 (write-wordindexed des
1660 (+ 2 sb!vm:symbol-value-slot) ; = 1 + symbol-hash-slot
1661 result)
1662 (write-wordindexed des
1663 (+ 1 sb!vm:symbol-info-slot)
1664 (cold-cons result result)) ; NIL's info is (nil . nil)
1665 (write-wordindexed des
1666 (+ 1 sb!vm:symbol-name-slot)
1667 ;; NIL's name is in dynamic space because any extra
1668 ;; bytes allocated in static space would need to
1669 ;; be accounted for by STATIC-SYMBOL-OFFSET.
1670 (base-string-to-core "NIL" *dynamic*))
1671 (setf (gethash (descriptor-bits result) *cold-symbols*) nil
1672 (get nil 'cold-intern-info) result)))
1674 ;;; Since the initial symbols must be allocated before we can intern
1675 ;;; anything else, we intern those here. We also set the value of T.
1676 (defun initialize-static-symbols ()
1677 "Initialize the cold load symbol-hacking data structures."
1678 ;; NIL did not have its package assigned. Do that now.
1679 (let ((target-cl-pkg-info (gethash "COMMON-LISP" *cold-package-symbols*)))
1680 ;; -1 is magic having to do with nil-as-cons vs. nil-as-symbol
1681 (write-wordindexed *nil-descriptor* (- sb!vm:symbol-package-slot 1)
1682 (car target-cl-pkg-info))
1683 (record-accessibility :external target-cl-pkg-info *nil-descriptor*))
1684 ;; Intern the others.
1685 (dolist (symbol sb!vm:*static-symbols*)
1686 (let* ((des (cold-intern symbol :gspace *static*))
1687 (offset-wanted (sb!vm:static-symbol-offset symbol))
1688 (offset-found (- (descriptor-bits des)
1689 (descriptor-bits *nil-descriptor*))))
1690 (unless (= offset-wanted offset-found)
1691 (error "Offset from ~S to ~S is ~W, not ~W"
1692 symbol
1694 offset-found
1695 offset-wanted))))
1696 ;; Establish the value of T.
1697 (let ((t-symbol (cold-intern t :gspace *static*)))
1698 (cold-set t-symbol t-symbol))
1699 ;; Establish the value of *PSEUDO-ATOMIC-BITS* so that the
1700 ;; allocation sequences that expect it to be zero upon entrance
1701 ;; actually find it to be so.
1702 #!+(or x86-64 x86)
1703 (let ((p-a-a-symbol (cold-intern '*pseudo-atomic-bits*
1704 :gspace *static*)))
1705 (cold-set p-a-a-symbol (make-fixnum-descriptor 0))))
1707 ;;; Sort *COLD-LAYOUTS* to return them in a deterministic order.
1708 (defun sort-cold-layouts ()
1709 (sort (%hash-table-alist *cold-layouts*) #'<
1710 :key (lambda (x) (descriptor-bits (cdr x)))))
1712 ;;; a helper function for FINISH-SYMBOLS: Return a cold alist suitable
1713 ;;; to be stored in *!INITIAL-LAYOUTS*.
1714 (defun cold-list-all-layouts ()
1715 (let ((result *nil-descriptor*))
1716 (dolist (layout (sort-cold-layouts) result)
1717 (cold-push (cold-cons (cold-intern (car layout)) (cdr layout))
1718 result))))
1720 ;;; Establish initial values for magic symbols.
1722 (defun finish-symbols ()
1724 ;; Everything between this preserved-for-posterity comment down to
1725 ;; the assignment of *CURRENT-CATCH-BLOCK* could be entirely deleted,
1726 ;; including the list of *C-CALLABLE-STATIC-SYMBOLS* itself,
1727 ;; if it is GC-safe for the C runtime to have its own implementation
1728 ;; of the INFO-VECTOR-FDEFN function in a multi-threaded build.
1730 ;; "I think the point of setting these functions into SYMBOL-VALUEs
1731 ;; here, instead of using SYMBOL-FUNCTION, is that in CMU CL
1732 ;; SYMBOL-FUNCTION reduces to FDEFINITION, which is a pretty
1733 ;; hairy operation (involving globaldb.lisp etc.) which we don't
1734 ;; want to invoke early in cold init. -- WHN 2001-12-05"
1736 ;; So... that's no longer true. We _do_ associate symbol -> fdefn in genesis.
1737 ;; Additionally, the INFO-VECTOR-FDEFN function is extremely simple and could
1738 ;; easily be implemented in C. However, info-vectors are inevitably
1739 ;; reallocated when new info is attached to a symbol, so the vectors can't be
1740 ;; in static space; they'd gradually become permanent garbage if they did.
1741 ;; That's the real reason for preserving the approach of storing an #<fdefn>
1742 ;; in a symbol's value cell - that location is static, the symbol-info is not.
1744 ;; FIXME: So OK, that's a reasonable reason to do something weird like
1745 ;; this, but this is still a weird thing to do, and we should change
1746 ;; the names to highlight that something weird is going on. Perhaps
1747 ;; *MAYBE-GC-FUN*, *INTERNAL-ERROR-FUN*, *HANDLE-BREAKPOINT-FUN*,
1748 ;; and *HANDLE-FUN-END-BREAKPOINT-FUN*...
1749 (dolist (symbol sb!vm::*c-callable-static-symbols*)
1750 (cold-set symbol (cold-fdefinition-object (cold-intern symbol))))
1752 (cold-set 'sb!vm::*current-catch-block* (make-fixnum-descriptor 0))
1753 (cold-set 'sb!vm::*current-unwind-protect-block* (make-fixnum-descriptor 0))
1755 (cold-set '*free-interrupt-context-index* (make-fixnum-descriptor 0))
1757 (cold-set '*!initial-layouts* (cold-list-all-layouts))
1759 #!+sb-thread
1760 (cold-set 'sb!vm::*free-tls-index*
1761 (make-descriptor (ash *genesis-tls-counter* sb!vm:word-shift)))
1763 (dolist (symbol sb!impl::*cache-vector-symbols*)
1764 (cold-set symbol *nil-descriptor*))
1766 ;; Symbols for which no call to COLD-INTERN would occur - due to not being
1767 ;; referenced until warm init - must be artificially cold-interned.
1768 ;; Inasmuch as the "offending" things are compiled by ordinary target code
1769 ;; and not cold-init, I think we should use an ordinary DEFPACKAGE for
1770 ;; the added-on bits. What I've done is somewhat of a fragile kludge.
1771 (let (syms)
1772 (with-package-iterator (iter '("SB!PCL" "SB!MOP" "SB!GRAY" "SB!SEQUENCE"
1773 "SB!PROFILE" "SB!EXT" "SB!VM"
1774 "SB!C" "SB!FASL" "SB!DEBUG")
1775 :external)
1776 (loop
1777 (multiple-value-bind (foundp sym accessibility package) (iter)
1778 (declare (ignore accessibility))
1779 (cond ((not foundp) (return))
1780 ((eq (symbol-package sym) package) (push sym syms))))))
1781 (setf syms (stable-sort syms #'string<))
1782 (dolist (sym syms)
1783 (cold-intern sym)))
1785 (let ((cold-pkg-inits *nil-descriptor*)
1786 cold-package-symbols-list)
1787 (maphash (lambda (name info)
1788 (push (cons name info) cold-package-symbols-list))
1789 *cold-package-symbols*)
1790 (setf cold-package-symbols-list
1791 (sort cold-package-symbols-list #'string< :key #'car))
1792 (dolist (pkgcons cold-package-symbols-list)
1793 (destructuring-bind (pkg-name . pkg-info) pkgcons
1794 (let ((shadow
1795 ;; Record shadowing symbols (except from SB-XC) in SB! packages.
1796 (when (eql (mismatch pkg-name "SB!") 3)
1797 ;; Be insensitive to the host's ordering.
1798 (sort (remove (find-package "SB-XC")
1799 (package-shadowing-symbols (find-package pkg-name))
1800 :key #'symbol-package) #'string<))))
1801 (write-slots (car (gethash pkg-name *cold-package-symbols*)) ; package
1802 (find-layout 'package)
1803 :%shadowing-symbols (list-to-core
1804 (mapcar 'cold-intern shadow))))
1805 (unless (member pkg-name '("COMMON-LISP" "KEYWORD") :test 'string=)
1806 (let ((host-pkg (find-package pkg-name))
1807 (sb-xc-pkg (find-package "SB-XC"))
1808 syms)
1809 ;; Now for each symbol directly present in this host-pkg,
1810 ;; i.e. accessible but not :INHERITED, figure out if the symbol
1811 ;; came from a different package, and if so, make a note of it.
1812 (with-package-iterator (iter host-pkg :internal :external)
1813 (loop (multiple-value-bind (foundp sym accessibility) (iter)
1814 (unless foundp (return))
1815 (unless (or (eq (symbol-package sym) host-pkg)
1816 (eq (symbol-package sym) sb-xc-pkg))
1817 (push (cons sym accessibility) syms)))))
1818 (dolist (symcons (sort syms #'string< :key #'car))
1819 (destructuring-bind (sym . accessibility) symcons
1820 (record-accessibility accessibility pkg-info (cold-intern sym)
1821 host-pkg sym)))))
1822 (cold-push (cold-cons (car pkg-info)
1823 (cold-cons (vector-in-core (cadr pkg-info))
1824 (vector-in-core (cddr pkg-info))))
1825 cold-pkg-inits)))
1826 (cold-set 'sb!impl::*!initial-symbols* cold-pkg-inits))
1828 (dump-symbol-info-vectors
1829 (attach-fdefinitions-to-symbols
1830 (attach-classoid-cells-to-symbols (make-hash-table :test #'eq))))
1832 (cold-set '*!initial-debug-sources* *current-debug-sources*)
1834 #!+x86
1835 (progn
1836 (cold-set 'sb!vm::*fp-constant-0d0* (number-to-core 0d0))
1837 (cold-set 'sb!vm::*fp-constant-1d0* (number-to-core 1d0))
1838 (cold-set 'sb!vm::*fp-constant-0f0* (number-to-core 0f0))
1839 (cold-set 'sb!vm::*fp-constant-1f0* (number-to-core 1f0))))
1841 ;;;; functions and fdefinition objects
1843 ;;; a hash table mapping from fdefinition names to descriptors of cold
1844 ;;; objects
1846 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1847 ;;; we want to have only one entry per name, this must be an 'EQUAL
1848 ;;; hash table, not the default 'EQL.
1849 (defvar *cold-fdefn-objects*)
1851 (defvar *cold-fdefn-gspace* nil)
1853 ;;; Given a cold representation of a symbol, return a warm
1854 ;;; representation.
1855 (defun warm-symbol (des)
1856 ;; Note that COLD-INTERN is responsible for keeping the
1857 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1858 ;; uninterned symbol, the code below will fail. But as long as we
1859 ;; don't need to look up uninterned symbols during bootstrapping,
1860 ;; that's OK..
1861 (multiple-value-bind (symbol found-p)
1862 (gethash (descriptor-bits des) *cold-symbols*)
1863 (declare (type symbol symbol))
1864 (unless found-p
1865 (error "no warm symbol"))
1866 symbol))
1868 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1869 (defun cold-car (des)
1870 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1871 (read-wordindexed des sb!vm:cons-car-slot))
1872 (defun cold-cdr (des)
1873 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1874 (read-wordindexed des sb!vm:cons-cdr-slot))
1875 (defun cold-rplacd (des newval)
1876 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1877 (write-wordindexed des sb!vm:cons-cdr-slot newval)
1878 des)
1879 (defun cold-null (des) (descriptor= des *nil-descriptor*))
1881 ;;; Given a cold representation of a function name, return a warm
1882 ;;; representation.
1883 (declaim (ftype (function ((or symbol descriptor)) (or symbol list)) warm-fun-name))
1884 (defun warm-fun-name (des)
1885 (let ((result
1886 (if (symbolp des)
1887 ;; This parallels the logic at the start of COLD-INTERN
1888 ;; which re-homes symbols in SB-XC to COMMON-LISP.
1889 (if (eq (symbol-package des) (find-package "SB-XC"))
1890 (intern (symbol-name des) *cl-package*)
1891 des)
1892 (ecase (descriptor-lowtag des)
1893 (#.sb!vm:list-pointer-lowtag
1894 (aver (not (cold-null des))) ; function named NIL? please no..
1895 (let ((rest (cold-cdr des)))
1896 (aver (cold-null (cold-cdr rest)))
1897 (list (warm-symbol (cold-car des))
1898 (warm-symbol (cold-car rest)))))
1899 (#.sb!vm:other-pointer-lowtag
1900 (warm-symbol des))))))
1901 (legal-fun-name-or-type-error result)
1902 result))
1904 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw)
1905 (declare (type (or symbol descriptor) cold-name))
1906 (declare (special core-file-name))
1907 (let ((warm-name (warm-fun-name cold-name)))
1908 (or (gethash warm-name *cold-fdefn-objects*)
1909 (let ((fdefn (allocate-header+object (or *cold-fdefn-gspace*
1910 #!+immobile-space *immobile-fixedobj*
1911 #!-immobile-space *dynamic*)
1912 (1- sb!vm:fdefn-size)
1913 sb!vm:fdefn-widetag)))
1914 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
1915 (write-wordindexed fdefn sb!vm:fdefn-name-slot cold-name)
1916 (unless leave-fn-raw
1917 (write-wordindexed fdefn sb!vm:fdefn-fun-slot *nil-descriptor*)
1918 (write-wordindexed/raw fdefn
1919 sb!vm:fdefn-raw-addr-slot
1920 (or (lookup-assembler-reference
1921 'sb!vm::undefined-tramp core-file-name)
1922 ;; Our preload for the tramps
1923 ;; doesn't happen during host-1,
1924 ;; so substitute a usable value.
1925 0)))
1926 fdefn))))
1928 (defun cold-functionp (descriptor)
1929 (eql (descriptor-lowtag descriptor) sb!vm:fun-pointer-lowtag))
1931 (defun cold-fun-entry-addr (fun)
1932 (aver (= (descriptor-lowtag fun) sb!vm:fun-pointer-lowtag))
1933 (+ (descriptor-bits fun)
1934 (- sb!vm:fun-pointer-lowtag)
1935 (ash sb!vm:simple-fun-code-offset sb!vm:word-shift)))
1937 ;;; Handle a DEFUN in cold-load.
1938 (defun cold-fset (name defn source-loc &optional inline-expansion)
1939 ;; SOURCE-LOC can be ignored, because functions intrinsically store
1940 ;; their location as part of the code component.
1941 ;; The argument is supplied here only to provide context for
1942 ;; a redefinition warning, which can't happen in cold load.
1943 (declare (ignore source-loc))
1944 (sb!int:binding* (((cold-name warm-name)
1945 ;; (SETF f) was descriptorized when dumped, symbols were not.
1946 (if (symbolp name)
1947 (values (cold-intern name) name)
1948 (values name (warm-fun-name name))))
1949 (fdefn (cold-fdefinition-object cold-name t)))
1950 (when (cold-functionp (cold-fdefn-fun fdefn))
1951 (error "Duplicate DEFUN for ~S" warm-name))
1952 ;; There can't be any closures or funcallable instances.
1953 (aver (= (logand (descriptor-bits (read-memory defn)) sb!vm:widetag-mask)
1954 sb!vm:simple-fun-header-widetag))
1955 (push (cold-cons cold-name inline-expansion) *!cold-defuns*)
1956 (write-wordindexed fdefn sb!vm:fdefn-fun-slot defn)
1957 (write-wordindexed fdefn
1958 sb!vm:fdefn-raw-addr-slot
1959 #!+(or sparc arm) defn
1960 #!-(or sparc arm)
1961 (make-random-descriptor
1962 (+ (logandc2 (descriptor-bits defn)
1963 sb!vm:lowtag-mask)
1964 (ash sb!vm:simple-fun-code-offset
1965 sb!vm:word-shift))))
1966 fdefn))
1968 ;;; Handle a DEFMETHOD in cold-load. "Very easily done". Right.
1969 (defun cold-defmethod (name &rest stuff)
1970 (let ((gf (assoc name *cold-methods*)))
1971 (unless gf
1972 (setq gf (cons name nil))
1973 (push gf *cold-methods*))
1974 (push stuff (cdr gf))))
1976 (defun initialize-static-fns ()
1977 (let ((*cold-fdefn-gspace* *static*))
1978 (dolist (sym sb!vm:*static-funs*)
1979 (let* ((fdefn (cold-fdefinition-object (cold-intern sym)))
1980 (offset (- (+ (- (descriptor-bits fdefn)
1981 sb!vm:other-pointer-lowtag)
1982 (* sb!vm:fdefn-raw-addr-slot sb!vm:n-word-bytes))
1983 (descriptor-bits *nil-descriptor*)))
1984 (desired (sb!vm:static-fun-offset sym)))
1985 (unless (= offset desired)
1986 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1987 sym nil offset desired))))))
1989 (defun attach-classoid-cells-to-symbols (hashtable)
1990 (let ((num (sb!c::meta-info-number (sb!c::meta-info :type :classoid-cell)))
1991 (layout (gethash 'sb!kernel::classoid-cell *cold-layouts*)))
1992 (when (plusp (hash-table-count *classoid-cells*))
1993 (aver layout))
1994 ;; Iteration order is immaterial. The symbols will get sorted later.
1995 (maphash (lambda (symbol cold-classoid-cell)
1996 ;; Some classoid-cells are dumped before the cold layout
1997 ;; of classoid-cell has been made, so fix those cases now.
1998 ;; Obviously it would be better if, in general, ALLOCATE-STRUCT
1999 ;; knew when something later must backpatch a cold layout
2000 ;; so that it could make a note to itself to do those ASAP
2001 ;; after the cold layout became known.
2002 (when (cold-null (cold-layout-of cold-classoid-cell))
2003 (set-instance-layout cold-classoid-cell layout))
2004 (setf (gethash symbol hashtable)
2005 (packed-info-insert
2006 (gethash symbol hashtable +nil-packed-infos+)
2007 sb!c::+no-auxilliary-key+ num cold-classoid-cell)))
2008 *classoid-cells*))
2009 hashtable)
2011 ;; Create pointer from SYMBOL and/or (SETF SYMBOL) to respective fdefinition
2013 (defun attach-fdefinitions-to-symbols (hashtable)
2014 ;; Collect fdefinitions that go with one symbol, e.g. CAR and (SETF CAR),
2015 ;; using the host's code for manipulating a packed info-vector.
2016 (maphash (lambda (warm-name cold-fdefn)
2017 (with-globaldb-name (key1 key2) warm-name
2018 :hairy (error "Hairy fdefn name in genesis: ~S" warm-name)
2019 :simple
2020 (setf (gethash key1 hashtable)
2021 (packed-info-insert
2022 (gethash key1 hashtable +nil-packed-infos+)
2023 key2 +fdefn-info-num+ cold-fdefn))))
2024 *cold-fdefn-objects*)
2025 hashtable)
2027 (defun dump-symbol-info-vectors (hashtable)
2028 ;; Emit in the same order symbols reside in core to avoid
2029 ;; sensitivity to the iteration order of host's maphash.
2030 (loop for (warm-sym . info)
2031 in (sort (%hash-table-alist hashtable) #'<
2032 :key (lambda (x) (descriptor-bits (cold-intern (car x)))))
2033 do (write-wordindexed
2034 (cold-intern warm-sym) sb!vm:symbol-info-slot
2035 ;; Each vector will have one fixnum, possibly the symbol SETF,
2036 ;; and one or two #<fdefn> objects in it, and/or a classoid-cell.
2037 (vector-in-core
2038 (map 'list (lambda (elt)
2039 (etypecase elt
2040 (symbol (cold-intern elt))
2041 (fixnum (make-fixnum-descriptor elt))
2042 (descriptor elt)))
2043 info)))))
2046 ;;;; fixups and related stuff
2048 ;;; an EQUAL hash table
2049 (defvar *cold-foreign-symbol-table*)
2050 (declaim (type hash-table *cold-foreign-symbol-table*))
2052 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
2053 ;; the C runtime.
2054 (defun load-cold-foreign-symbol-table (filename)
2055 (/show "load-cold-foreign-symbol-table" filename)
2056 (with-open-file (file filename)
2057 (loop for line = (read-line file nil nil)
2058 while line do
2059 ;; UNIX symbol tables might have tabs in them, and tabs are
2060 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
2061 ;; nice portable way to deal with them within Lisp, alas.
2062 ;; Fortunately, it's easy to use UNIX command line tools like
2063 ;; sed to remove the problem, so it's not too painful for us
2064 ;; to push responsibility for converting tabs to spaces out to
2065 ;; the caller.
2067 ;; Other non-STANDARD-CHARs are problematic for the same reason.
2068 ;; Make sure that there aren't any..
2069 (let ((ch (find-if (lambda (char)
2070 (not (typep char 'standard-char)))
2071 line)))
2072 (when ch
2073 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
2075 line)))
2076 (setf line (string-trim '(#\space) line))
2077 (let ((p1 (position #\space line :from-end nil))
2078 (p2 (position #\space line :from-end t)))
2079 (if (not (and p1 p2 (< p1 p2)))
2080 ;; KLUDGE: It's too messy to try to understand all
2081 ;; possible output from nm, so we just punt the lines we
2082 ;; don't recognize. We realize that there's some chance
2083 ;; that might get us in trouble someday, so we warn
2084 ;; about it.
2085 (warn "ignoring unrecognized line ~S in ~A" line filename)
2086 (multiple-value-bind (value name)
2087 (if (string= "0x" line :end2 2)
2088 (values (parse-integer line :start 2 :end p1 :radix 16)
2089 (subseq line (1+ p2)))
2090 (values (parse-integer line :end p1 :radix 16)
2091 (subseq line (1+ p2))))
2092 ;; KLUDGE CLH 2010-05-31: on darwin, nm gives us
2093 ;; _function but dlsym expects us to look up
2094 ;; function, without the leading _ . Therefore, we
2095 ;; strip it off here.
2096 #!+darwin
2097 (when (equal (char name 0) #\_)
2098 (setf name (subseq name 1)))
2099 (multiple-value-bind (old-value found)
2100 (gethash name *cold-foreign-symbol-table*)
2101 (when (and found
2102 (not (= old-value value)))
2103 (warn "redefining ~S from #X~X to #X~X"
2104 name old-value value)))
2105 (/show "adding to *cold-foreign-symbol-table*:" name value)
2106 (setf (gethash name *cold-foreign-symbol-table*) value)
2107 #!+win32
2108 (let ((at-position (position #\@ name)))
2109 (when at-position
2110 (let ((name (subseq name 0 at-position)))
2111 (multiple-value-bind (old-value found)
2112 (gethash name *cold-foreign-symbol-table*)
2113 (when (and found
2114 (not (= old-value value)))
2115 (warn "redefining ~S from #X~X to #X~X"
2116 name old-value value)))
2117 (setf (gethash name *cold-foreign-symbol-table*)
2118 value)))))))))
2119 (values)) ;; PROGN
2121 (defun cold-foreign-symbol-address (name)
2122 (or (find-foreign-symbol-in-table name *cold-foreign-symbol-table*)
2123 *foreign-symbol-placeholder-value*
2124 (progn
2125 (format *error-output* "~&The foreign symbol table is:~%")
2126 (maphash (lambda (k v)
2127 (format *error-output* "~&~S = #X~8X~%" k v))
2128 *cold-foreign-symbol-table*)
2129 (error "The foreign symbol ~S is undefined." name))))
2131 (defvar *cold-assembler-routines*)
2133 (defvar *cold-assembler-fixups*)
2134 (defvar *cold-static-call-fixups*)
2136 (defun record-cold-assembler-routine (name address)
2137 (/xhow "in RECORD-COLD-ASSEMBLER-ROUTINE" name address)
2138 (push (cons name address)
2139 *cold-assembler-routines*))
2141 (defun record-cold-assembler-fixup (routine
2142 code-object
2143 offset
2144 &optional
2145 (kind :both))
2146 (push (list routine code-object offset kind)
2147 *cold-assembler-fixups*))
2149 (defun lookup-assembler-reference (symbol &optional (errorp t))
2150 (let ((value (cdr (assoc symbol *cold-assembler-routines*))))
2151 (unless value
2152 (when errorp
2153 (error "Assembler routine ~S not defined." symbol)))
2154 value))
2156 ;;; Unlike in the target, FOP-KNOWN-FUN sometimes has to backpatch.
2157 (defvar *deferred-known-fun-refs*)
2159 ;;; The x86 port needs to store code fixups along with code objects if
2160 ;;; they are to be moved, so fixups for code objects in the dynamic
2161 ;;; heap need to be noted.
2162 #!+x86
2163 (defvar *load-time-code-fixups*)
2165 #!+x86
2166 (defun note-load-time-code-fixup (code-object offset)
2167 ;; If CODE-OBJECT might be moved
2168 (when (= (gspace-identifier (descriptor-intuit-gspace code-object))
2169 dynamic-core-space-id)
2170 (push offset (gethash (descriptor-bits code-object)
2171 *load-time-code-fixups*
2172 nil)))
2173 (values))
2175 #!+x86
2176 (defun output-load-time-code-fixups ()
2177 (let ((fixup-infos nil))
2178 (maphash
2179 (lambda (code-object-address fixup-offsets)
2180 (push (cons code-object-address fixup-offsets) fixup-infos))
2181 *load-time-code-fixups*)
2182 (setq fixup-infos (sort fixup-infos #'< :key #'car))
2183 (dolist (fixup-info fixup-infos)
2184 (let ((code-object-address (car fixup-info))
2185 (fixup-offsets (cdr fixup-info)))
2186 (let ((fixup-vector
2187 (allocate-vector-object
2188 *dynamic* sb!vm:n-word-bits (length fixup-offsets)
2189 sb!vm:simple-array-unsigned-byte-32-widetag)))
2190 (do ((index sb!vm:vector-data-offset (1+ index))
2191 (fixups fixup-offsets (cdr fixups)))
2192 ((null fixups))
2193 (write-wordindexed/raw fixup-vector index (car fixups)))
2194 (write-wordindexed (make-random-descriptor code-object-address)
2195 sb!vm::code-fixups-slot fixup-vector))))))
2197 ;;; Given a pointer to a code object and a byte offset relative to the
2198 ;;; tail of the code object's header, return a byte offset relative to the
2199 ;;; (beginning of the) code object.
2201 (declaim (ftype (function (descriptor sb!vm:word)) calc-offset))
2202 (defun calc-offset (code-object insts-offset-bytes)
2203 (+ (ash (logand (get-header-data code-object) sb!vm:short-header-max-words)
2204 sb!vm:word-shift)
2205 insts-offset-bytes))
2207 (declaim (ftype (function (descriptor sb!vm:word sb!vm:word keyword))
2208 do-cold-fixup))
2209 (defun do-cold-fixup (code-object after-header value kind)
2210 (let* ((offset-within-code-object (calc-offset code-object after-header))
2211 (gspace-bytes (descriptor-mem code-object))
2212 (gspace-byte-offset (+ (descriptor-byte-offset code-object)
2213 offset-within-code-object))
2214 (gspace-byte-address (gspace-byte-address
2215 (descriptor-gspace code-object))))
2216 ;; There's just a ton of code here that gets deleted,
2217 ;; inhibiting the view of the the forest through the trees.
2218 ;; Use of #+sbcl would say "probable bug in read-time conditional"
2219 #+#.(cl:if (cl:member :sbcl cl:*features*) '(and) '(or))
2220 (declare (sb-ext:muffle-conditions sb-ext:code-deletion-note))
2221 (ecase +backend-fasl-file-implementation+
2222 ;; See CMU CL source for other formerly-supported architectures
2223 ;; (and note that you have to rewrite them to use BVREF-X
2224 ;; instead of SAP-REF).
2225 (:alpha
2226 (ecase kind
2227 (:jmp-hint
2228 (assert (zerop (ldb (byte 2 0) value))))
2229 (:bits-63-48
2230 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
2231 (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
2232 (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
2233 (setf (bvref-8 gspace-bytes gspace-byte-offset)
2234 (ldb (byte 8 48) value)
2235 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
2236 (ldb (byte 8 56) value))))
2237 (:bits-47-32
2238 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
2239 (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
2240 (setf (bvref-8 gspace-bytes gspace-byte-offset)
2241 (ldb (byte 8 32) value)
2242 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
2243 (ldb (byte 8 40) value))))
2244 (:ldah
2245 (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
2246 (setf (bvref-8 gspace-bytes gspace-byte-offset)
2247 (ldb (byte 8 16) value)
2248 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
2249 (ldb (byte 8 24) value))))
2250 (:lda
2251 (setf (bvref-8 gspace-bytes gspace-byte-offset)
2252 (ldb (byte 8 0) value)
2253 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
2254 (ldb (byte 8 8) value)))
2255 (:absolute32
2256 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))))
2257 (:arm
2258 (ecase kind
2259 (:absolute
2260 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))))
2261 (:arm64
2262 (ecase kind
2263 (:absolute
2264 (setf (bvref-64 gspace-bytes gspace-byte-offset) value))
2265 (:cond-branch
2266 (setf (ldb (byte 19 5)
2267 (bvref-32 gspace-bytes gspace-byte-offset))
2268 (ash (- value (+ gspace-byte-address gspace-byte-offset))
2269 -2)))
2270 (:uncond-branch
2271 (setf (ldb (byte 26 0)
2272 (bvref-32 gspace-bytes gspace-byte-offset))
2273 (ash (- value (+ gspace-byte-address gspace-byte-offset))
2274 -2)))))
2275 (:hppa
2276 (ecase kind
2277 (:absolute
2278 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))
2279 (:load
2280 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2281 (logior (mask-field (byte 18 14)
2282 (bvref-32 gspace-bytes gspace-byte-offset))
2283 (if (< value 0)
2284 (1+ (ash (ldb (byte 13 0) value) 1))
2285 (ash (ldb (byte 13 0) value) 1)))))
2286 (:load11u
2287 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2288 (logior (mask-field (byte 18 14)
2289 (bvref-32 gspace-bytes gspace-byte-offset))
2290 (if (< value 0)
2291 (1+ (ash (ldb (byte 10 0) value) 1))
2292 (ash (ldb (byte 11 0) value) 1)))))
2293 (:load-short
2294 (let ((low-bits (ldb (byte 11 0) value)))
2295 (assert (<= 0 low-bits (1- (ash 1 4)))))
2296 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2297 (logior (ash (dpb (ldb (byte 4 0) value)
2298 (byte 4 1)
2299 (ldb (byte 1 4) value)) 17)
2300 (logand (bvref-32 gspace-bytes gspace-byte-offset)
2301 #xffe0ffff))))
2302 (:hi
2303 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2304 (logior (mask-field (byte 11 21)
2305 (bvref-32 gspace-bytes gspace-byte-offset))
2306 (ash (ldb (byte 5 13) value) 16)
2307 (ash (ldb (byte 2 18) value) 14)
2308 (ash (ldb (byte 2 11) value) 12)
2309 (ash (ldb (byte 11 20) value) 1)
2310 (ldb (byte 1 31) value))))
2311 (:branch
2312 (let ((bits (ldb (byte 9 2) value)))
2313 (assert (zerop (ldb (byte 2 0) value)))
2314 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2315 (logior (ash bits 3)
2316 (mask-field (byte 1 1) (bvref-32 gspace-bytes gspace-byte-offset))
2317 (mask-field (byte 3 13) (bvref-32 gspace-bytes gspace-byte-offset))
2318 (mask-field (byte 11 21) (bvref-32 gspace-bytes gspace-byte-offset))))))))
2319 (:mips
2320 (ecase kind
2321 (:absolute
2322 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))
2323 (:jump
2324 (assert (zerop (ash value -28)))
2325 (setf (ldb (byte 26 0)
2326 (bvref-32 gspace-bytes gspace-byte-offset))
2327 (ash value -2)))
2328 (:lui
2329 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2330 (logior (mask-field (byte 16 16)
2331 (bvref-32 gspace-bytes gspace-byte-offset))
2332 (ash (1+ (ldb (byte 17 15) value)) -1))))
2333 (:addi
2334 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2335 (logior (mask-field (byte 16 16)
2336 (bvref-32 gspace-bytes gspace-byte-offset))
2337 (ldb (byte 16 0) value))))))
2338 ;; FIXME: PowerPC Fixups are not fully implemented. The bit
2339 ;; here starts to set things up to work properly, but there
2340 ;; needs to be corresponding code in ppc-vm.lisp
2341 (:ppc
2342 (ecase kind
2343 (:absolute
2344 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))
2345 (:ba
2346 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2347 (dpb (ash value -2) (byte 24 2)
2348 (bvref-32 gspace-bytes gspace-byte-offset))))
2349 (:ha
2350 (let* ((un-fixed-up (bvref-16 gspace-bytes
2351 (+ gspace-byte-offset 2)))
2352 (fixed-up (+ un-fixed-up value))
2353 (h (ldb (byte 16 16) fixed-up))
2354 (l (ldb (byte 16 0) fixed-up)))
2355 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
2356 (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
2358 (let* ((un-fixed-up (bvref-16 gspace-bytes
2359 (+ gspace-byte-offset 2)))
2360 (fixed-up (+ un-fixed-up value)))
2361 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
2362 (ldb (byte 16 0) fixed-up))))))
2363 (:sparc
2364 (ecase kind
2365 (:call
2366 (error "can't deal with call fixups yet"))
2367 (:sethi
2368 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2369 (dpb (ldb (byte 22 10) value)
2370 (byte 22 0)
2371 (bvref-32 gspace-bytes gspace-byte-offset))))
2372 (:add
2373 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2374 (dpb (ldb (byte 10 0) value)
2375 (byte 10 0)
2376 (bvref-32 gspace-bytes gspace-byte-offset))))
2377 (:absolute
2378 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))))
2379 ((:x86 :x86-64)
2380 ;; XXX: Note that un-fixed-up is read via bvref-word, which is
2381 ;; 64 bits wide on x86-64, but the fixed-up value is written
2382 ;; via bvref-32. This would make more sense if we supported
2383 ;; :absolute64 fixups, but apparently the cross-compiler
2384 ;; doesn't dump them.
2385 (let* ((un-fixed-up (bvref-word gspace-bytes
2386 gspace-byte-offset))
2387 (code-object-start-addr (logandc2 (descriptor-bits code-object)
2388 sb!vm:lowtag-mask)))
2389 (assert (= code-object-start-addr
2390 (+ gspace-byte-address
2391 (descriptor-byte-offset code-object))))
2392 (ecase kind
2393 (:absolute
2394 (let ((fixed-up (+ value un-fixed-up)))
2395 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2396 fixed-up)
2397 ;; comment from CMU CL sources:
2399 ;; Note absolute fixups that point within the object.
2400 ;; KLUDGE: There seems to be an implicit assumption in
2401 ;; the old CMU CL code here, that if it doesn't point
2402 ;; before the object, it must point within the object
2403 ;; (not beyond it). It would be good to add an
2404 ;; explanation of why that's true, or an assertion that
2405 ;; it's really true, or both.
2407 ;; One possible explanation is that all absolute fixups
2408 ;; point either within the code object, within the
2409 ;; runtime, within read-only or static-space, or within
2410 ;; the linkage-table space. In all x86 configurations,
2411 ;; these areas are prior to the start of dynamic space,
2412 ;; where all the code-objects are loaded.
2413 #!+x86
2414 (unless (< fixed-up code-object-start-addr)
2415 (note-load-time-code-fixup code-object
2416 after-header))))
2417 (:relative ; (used for arguments to X86 relative CALL instruction)
2418 (let ((fixed-up (- (+ value un-fixed-up)
2419 gspace-byte-address
2420 gspace-byte-offset
2421 4))) ; "length of CALL argument"
2422 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2423 fixed-up)
2424 ;; Note relative fixups that point outside the code
2425 ;; object, which is to say all relative fixups, since
2426 ;; relative addressing within a code object never needs
2427 ;; a fixup.
2428 #!+x86
2429 (note-load-time-code-fixup code-object
2430 after-header))))))))
2431 (values))
2433 (defun resolve-assembler-fixups ()
2434 (dolist (fixup *cold-assembler-fixups*)
2435 (let* ((routine (car fixup))
2436 (value (lookup-assembler-reference routine)))
2437 (when value
2438 (do-cold-fixup (second fixup) (third fixup) value (fourth fixup)))))
2439 ;; Static calls are very similar to assembler routine calls,
2440 ;; so take care of those too.
2441 (dolist (fixup *cold-static-call-fixups*)
2442 (destructuring-bind (name kind code offset) fixup
2443 (do-cold-fixup code offset
2444 (cold-fun-entry-addr
2445 (cold-fdefn-fun (cold-fdefinition-object name)))
2446 kind))))
2448 #!+sb-dynamic-core
2449 (progn
2450 (defparameter *dyncore-address* sb!vm::linkage-table-space-start)
2451 (defparameter *dyncore-linkage-keys* nil)
2452 (defparameter *dyncore-table* (make-hash-table :test 'equal))
2454 (defun dyncore-note-symbol (symbol-name datap)
2455 "Register a symbol and return its address in proto-linkage-table."
2456 (let ((key (cons symbol-name datap)))
2457 (symbol-macrolet ((entry (gethash key *dyncore-table*)))
2458 (or entry
2459 (setf entry
2460 (prog1 *dyncore-address*
2461 (push key *dyncore-linkage-keys*)
2462 (incf *dyncore-address* sb!vm::linkage-table-entry-size))))))))
2464 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
2465 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
2466 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
2467 ;;; target-load.lisp refers to.
2468 (defun foreign-symbols-to-core ()
2469 (let ((result *nil-descriptor*))
2470 #!-sb-dynamic-core
2471 (dolist (symbol (sort (%hash-table-alist *cold-foreign-symbol-table*)
2472 #'string< :key #'car))
2473 (cold-push (cold-cons (base-string-to-core (car symbol))
2474 (number-to-core (cdr symbol)))
2475 result))
2476 (cold-set '*!initial-foreign-symbols* result)
2477 #!+sb-dynamic-core
2478 (let ((runtime-linking-list *nil-descriptor*))
2479 (dolist (symbol *dyncore-linkage-keys*)
2480 (cold-push (cold-cons (base-string-to-core (car symbol))
2481 (cdr symbol))
2482 runtime-linking-list))
2483 (cold-set 'sb!vm::*required-runtime-c-symbols*
2484 runtime-linking-list)))
2485 (let ((result *nil-descriptor*))
2486 (dolist (rtn (sort (copy-list *cold-assembler-routines*) #'string< :key #'car))
2487 (cold-push (cold-cons (cold-intern (car rtn))
2488 (number-to-core (cdr rtn)))
2489 result))
2490 (cold-set '*!initial-assembler-routines* result)))
2493 ;;;; general machinery for cold-loading FASL files
2495 (defun pop-fop-stack (stack)
2496 (let ((top (svref stack 0)))
2497 (declare (type index top))
2498 (when (eql 0 top)
2499 (error "FOP stack empty"))
2500 (setf (svref stack 0) (1- top))
2501 (svref stack top)))
2503 ;;; Cause a fop to have a special definition for cold load.
2505 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
2506 ;;; looks up the encoding for this name (created by a previous DEFINE-FOP)
2507 ;;; instead of creating a new encoding.
2508 (defmacro define-cold-fop ((name &optional arglist) &rest forms)
2509 (let* ((code (get name 'opcode))
2510 (argc (aref (car **fop-signatures**) code))
2511 (fname (symbolicate "COLD-" name)))
2512 (unless code
2513 (error "~S is not a defined FOP." name))
2514 (when (and (plusp argc) (not (singleton-p arglist)))
2515 (error "~S must take one argument" name))
2516 `(progn
2517 (defun ,fname (.fasl-input. ,@arglist)
2518 (declare (ignorable .fasl-input.))
2519 (macrolet ((fasl-input () '(the fasl-input .fasl-input.))
2520 (fasl-input-stream () '(%fasl-input-stream (fasl-input)))
2521 (pop-stack ()
2522 '(pop-fop-stack (%fasl-input-stack (fasl-input)))))
2523 ,@forms))
2524 ;; We simply overwrite elements of **FOP-FUNS** since the contents
2525 ;; of the host are never propagated directly into the target core.
2526 ,@(loop for i from code to (logior code (if (plusp argc) 3 0))
2527 collect `(setf (svref **fop-funs** ,i) #',fname)))))
2529 ;;; Cause a fop to be undefined in cold load.
2530 (defmacro not-cold-fop (name)
2531 `(define-cold-fop (,name)
2532 (error "The fop ~S is not supported in cold load." ',name)))
2534 ;;; COLD-LOAD loads stuff into the core image being built by calling
2535 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
2536 ;;; loading functions.
2537 (defun cold-load (filename)
2538 "Load the file named by FILENAME into the cold load image being built."
2539 (write-line (namestring filename))
2540 (with-open-file (s filename :element-type '(unsigned-byte 8))
2541 (load-as-fasl s nil nil)))
2543 ;;;; miscellaneous cold fops
2545 (define-cold-fop (fop-misc-trap) *unbound-marker*)
2547 (define-cold-fop (fop-character (c))
2548 (make-character-descriptor c))
2550 (define-cold-fop (fop-empty-list) nil)
2551 (define-cold-fop (fop-truth) t)
2553 (define-cold-fop (fop-struct (size)) ; n-words incl. layout, excluding header
2554 (let* ((layout (pop-stack))
2555 (result (allocate-struct *dynamic* layout size))
2556 (bitmap (descriptor-fixnum
2557 (read-slot layout *host-layout-of-layout* :bitmap))))
2558 ;; Raw slots can not possibly work because dump-struct uses
2559 ;; %RAW-INSTANCE-REF/WORD which does not exist in the cross-compiler.
2560 ;; Remove this assertion if that problem is somehow circumvented.
2561 (unless (eql bitmap sb!kernel::+layout-all-tagged+)
2562 (error "Raw slots not working in genesis."))
2563 (loop for index downfrom (1- size) to sb!vm:instance-data-start
2564 for val = (pop-stack) then (pop-stack)
2565 do (write-wordindexed result
2566 (+ index sb!vm:instance-slots-offset)
2567 (if (logbitp index bitmap)
2569 (descriptor-word-sized-integer val))))
2570 result))
2572 (define-cold-fop (fop-layout)
2573 (let* ((bitmap-des (pop-stack))
2574 (length-des (pop-stack))
2575 (depthoid-des (pop-stack))
2576 (cold-inherits (pop-stack))
2577 (name (pop-stack))
2578 (old-layout-descriptor (gethash name *cold-layouts*)))
2579 (declare (type descriptor length-des depthoid-des cold-inherits))
2580 (declare (type symbol name))
2581 ;; If a layout of this name has been defined already
2582 (if old-layout-descriptor
2583 ;; Enforce consistency between the previous definition and the
2584 ;; current definition, then return the previous definition.
2585 (flet ((get-slot (keyword)
2586 (read-slot old-layout-descriptor *host-layout-of-layout* keyword)))
2587 (let ((old-length (descriptor-fixnum (get-slot :length)))
2588 (old-depthoid (descriptor-fixnum (get-slot :depthoid)))
2589 (old-bitmap (host-object-from-core (get-slot :bitmap)))
2590 (length (descriptor-fixnum length-des))
2591 (depthoid (descriptor-fixnum depthoid-des))
2592 (bitmap (host-object-from-core bitmap-des)))
2593 (unless (= length old-length)
2594 (error "cold loading a reference to class ~S when the compile~%~
2595 time length was ~S and current length is ~S"
2596 name
2597 length
2598 old-length))
2599 (unless (cold-vector-elements-eq cold-inherits (get-slot :inherits))
2600 (error "cold loading a reference to class ~S when the compile~%~
2601 time inherits were ~S~%~
2602 and current inherits are ~S"
2603 name
2604 (listify-cold-inherits cold-inherits)
2605 (listify-cold-inherits (get-slot :inherits))))
2606 (unless (= depthoid old-depthoid)
2607 (error "cold loading a reference to class ~S when the compile~%~
2608 time inheritance depthoid was ~S and current inheritance~%~
2609 depthoid is ~S"
2610 name
2611 depthoid
2612 old-depthoid))
2613 (unless (= bitmap old-bitmap)
2614 (error "cold loading a reference to class ~S when the compile~%~
2615 time raw-slot-bitmap was ~S and is currently ~S"
2616 name bitmap old-bitmap)))
2617 old-layout-descriptor)
2618 ;; Make a new definition from scratch.
2619 (make-cold-layout name length-des cold-inherits depthoid-des bitmap-des))))
2621 ;;;; cold fops for loading symbols
2623 ;;; Load a symbol SIZE characters long from FASL-INPUT, and
2624 ;;; intern that symbol in PACKAGE.
2625 (defun cold-load-symbol (length+flag package fasl-input)
2626 (let ((string (make-string (ash length+flag -1))))
2627 (read-string-as-bytes (%fasl-input-stream fasl-input) string)
2628 (push-fop-table (intern string package) fasl-input)))
2630 ;; I don't feel like hacking up DEFINE-COLD-FOP any more than necessary,
2631 ;; so this code is handcrafted to accept two operands.
2632 (flet ((fop-cold-symbol-in-package-save (fasl-input length+flag pkg-index)
2633 (cold-load-symbol length+flag (ref-fop-table fasl-input pkg-index)
2634 fasl-input)))
2635 (let ((i (get 'fop-symbol-in-package-save 'opcode)))
2636 (fill **fop-funs** #'fop-cold-symbol-in-package-save :start i :end (+ i 4))))
2638 (define-cold-fop (fop-lisp-symbol-save (length+flag))
2639 (cold-load-symbol length+flag *cl-package* (fasl-input)))
2641 (define-cold-fop (fop-keyword-symbol-save (length+flag))
2642 (cold-load-symbol length+flag *keyword-package* (fasl-input)))
2644 (define-cold-fop (fop-uninterned-symbol-save (length+flag))
2645 (let ((name (make-string (ash length+flag -1))))
2646 (read-string-as-bytes (fasl-input-stream) name)
2647 (push-fop-table (get-uninterned-symbol name) (fasl-input))))
2649 (define-cold-fop (fop-copy-symbol-save (index))
2650 (let* ((symbol (ref-fop-table (fasl-input) index))
2651 (name
2652 (if (symbolp symbol)
2653 (symbol-name symbol)
2654 (base-string-from-core
2655 (read-wordindexed symbol sb!vm:symbol-name-slot)))))
2656 ;; Genesis performs additional coalescing of uninterned symbols
2657 (push-fop-table (get-uninterned-symbol name) (fasl-input))))
2659 ;;;; cold fops for loading packages
2661 (define-cold-fop (fop-named-package-save (namelen))
2662 (let ((name (make-string namelen)))
2663 (read-string-as-bytes (fasl-input-stream) name)
2664 (push-fop-table (find-package name) (fasl-input))))
2666 ;;;; cold fops for loading lists
2668 ;;; Make a list of the top LENGTH things on the fop stack. The last
2669 ;;; cdr of the list is set to LAST.
2670 (defmacro cold-stack-list (length last)
2671 `(do* ((index ,length (1- index))
2672 (result ,last (cold-cons (pop-stack) result)))
2673 ((= index 0) result)
2674 (declare (fixnum index))))
2676 (define-cold-fop (fop-list)
2677 (cold-stack-list (read-byte-arg (fasl-input-stream)) *nil-descriptor*))
2678 (define-cold-fop (fop-list*)
2679 (cold-stack-list (read-byte-arg (fasl-input-stream)) (pop-stack)))
2680 (define-cold-fop (fop-list-1)
2681 (cold-stack-list 1 *nil-descriptor*))
2682 (define-cold-fop (fop-list-2)
2683 (cold-stack-list 2 *nil-descriptor*))
2684 (define-cold-fop (fop-list-3)
2685 (cold-stack-list 3 *nil-descriptor*))
2686 (define-cold-fop (fop-list-4)
2687 (cold-stack-list 4 *nil-descriptor*))
2688 (define-cold-fop (fop-list-5)
2689 (cold-stack-list 5 *nil-descriptor*))
2690 (define-cold-fop (fop-list-6)
2691 (cold-stack-list 6 *nil-descriptor*))
2692 (define-cold-fop (fop-list-7)
2693 (cold-stack-list 7 *nil-descriptor*))
2694 (define-cold-fop (fop-list-8)
2695 (cold-stack-list 8 *nil-descriptor*))
2696 (define-cold-fop (fop-list*-1)
2697 (cold-stack-list 1 (pop-stack)))
2698 (define-cold-fop (fop-list*-2)
2699 (cold-stack-list 2 (pop-stack)))
2700 (define-cold-fop (fop-list*-3)
2701 (cold-stack-list 3 (pop-stack)))
2702 (define-cold-fop (fop-list*-4)
2703 (cold-stack-list 4 (pop-stack)))
2704 (define-cold-fop (fop-list*-5)
2705 (cold-stack-list 5 (pop-stack)))
2706 (define-cold-fop (fop-list*-6)
2707 (cold-stack-list 6 (pop-stack)))
2708 (define-cold-fop (fop-list*-7)
2709 (cold-stack-list 7 (pop-stack)))
2710 (define-cold-fop (fop-list*-8)
2711 (cold-stack-list 8 (pop-stack)))
2713 ;;;; cold fops for loading vectors
2715 (define-cold-fop (fop-base-string (len))
2716 (let ((string (make-string len)))
2717 (read-string-as-bytes (fasl-input-stream) string)
2718 (base-string-to-core string)))
2720 #!+sb-unicode
2721 (define-cold-fop (fop-character-string (len))
2722 (bug "CHARACTER-STRING[~D] dumped by cross-compiler." len))
2724 (define-cold-fop (fop-vector (size))
2725 (let* ((result (allocate-vector-object *dynamic*
2726 sb!vm:n-word-bits
2727 size
2728 sb!vm:simple-vector-widetag)))
2729 (do ((index (1- size) (1- index)))
2730 ((minusp index))
2731 (declare (fixnum index))
2732 (write-wordindexed result
2733 (+ index sb!vm:vector-data-offset)
2734 (pop-stack)))
2735 result))
2737 (define-cold-fop (fop-spec-vector)
2738 (let* ((len (read-word-arg (fasl-input-stream)))
2739 (type (read-byte-arg (fasl-input-stream)))
2740 (sizebits (aref **saetp-bits-per-length** type))
2741 (result (progn (aver (< sizebits 255))
2742 (allocate-vector-object *dynamic* sizebits len type)))
2743 (start (+ (descriptor-byte-offset result)
2744 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2745 (end (+ start
2746 (ceiling (* len sizebits)
2747 sb!vm:n-byte-bits))))
2748 (read-bigvec-as-sequence-or-die (descriptor-mem result)
2749 (fasl-input-stream)
2750 :start start
2751 :end end)
2752 result))
2754 (not-cold-fop fop-array)
2755 #+nil
2756 ;; This code is unexercised. The only use of FOP-ARRAY is from target-dump.
2757 ;; It would be a shame to delete it though, as it might come in handy.
2758 (define-cold-fop (fop-array)
2759 (let* ((rank (read-word-arg (fasl-input-stream)))
2760 (data-vector (pop-stack))
2761 (result (allocate-object *dynamic*
2762 (+ sb!vm:array-dimensions-offset rank)
2763 sb!vm:other-pointer-lowtag)))
2764 (write-header-word result rank sb!vm:simple-array-widetag)
2765 (write-wordindexed result sb!vm:array-fill-pointer-slot *nil-descriptor*)
2766 (write-wordindexed result sb!vm:array-data-slot data-vector)
2767 (write-wordindexed result sb!vm:array-displacement-slot *nil-descriptor*)
2768 (write-wordindexed result sb!vm:array-displaced-p-slot *nil-descriptor*)
2769 (write-wordindexed result sb!vm:array-displaced-from-slot *nil-descriptor*)
2770 (let ((total-elements 1))
2771 (dotimes (axis rank)
2772 (let ((dim (pop-stack)))
2773 (unless (is-fixnum-lowtag (descriptor-lowtag dim))
2774 (error "non-fixnum dimension? (~S)" dim))
2775 (setf total-elements (* total-elements (descriptor-fixnum dim)))
2776 (write-wordindexed result
2777 (+ sb!vm:array-dimensions-offset axis)
2778 dim)))
2779 (write-wordindexed result
2780 sb!vm:array-elements-slot
2781 (make-fixnum-descriptor total-elements)))
2782 result))
2785 ;;;; cold fops for loading numbers
2787 (defmacro define-cold-number-fop (fop &optional arglist)
2788 ;; Invoke the ordinary warm version of this fop to cons the number.
2789 `(define-cold-fop (,fop ,arglist)
2790 (number-to-core (,fop (fasl-input) ,@arglist))))
2792 (define-cold-number-fop fop-single-float)
2793 (define-cold-number-fop fop-double-float)
2794 (define-cold-number-fop fop-word-integer)
2795 (define-cold-number-fop fop-byte-integer)
2796 (define-cold-number-fop fop-complex-single-float)
2797 (define-cold-number-fop fop-complex-double-float)
2798 (define-cold-number-fop fop-integer (n-bytes))
2800 (define-cold-fop (fop-ratio)
2801 (let ((den (pop-stack)))
2802 (number-pair-to-core (pop-stack) den sb!vm:ratio-widetag)))
2804 (define-cold-fop (fop-complex)
2805 (let ((im (pop-stack)))
2806 (number-pair-to-core (pop-stack) im sb!vm:complex-widetag)))
2808 ;;;; cold fops for calling (or not calling)
2810 (not-cold-fop fop-eval)
2811 (not-cold-fop fop-eval-for-effect)
2813 (defvar *load-time-value-counter*)
2815 (flet ((pop-args (fasl-input)
2816 (let ((args)
2817 (stack (%fasl-input-stack fasl-input)))
2818 (dotimes (i (read-byte-arg (%fasl-input-stream fasl-input))
2819 (values (pop-fop-stack stack) args))
2820 (push (pop-fop-stack stack) args))))
2821 (call (fun-name handler-name args)
2822 (acond ((get fun-name handler-name) (apply it args))
2823 (t (error "Can't ~S ~S in cold load" handler-name fun-name)))))
2825 (define-cold-fop (fop-funcall)
2826 (multiple-value-bind (fun args) (pop-args (fasl-input))
2827 (if args
2828 (case fun
2829 (fdefinition
2830 ;; Special form #'F fopcompiles into `(FDEFINITION ,f)
2831 (aver (and (singleton-p args) (symbolp (car args))))
2832 (target-symbol-function (car args)))
2833 (cons (cold-cons (first args) (second args)))
2834 (symbol-global-value (cold-symbol-value (first args)))
2835 (t (call fun :sb-cold-funcall-handler/for-value args)))
2836 (let ((counter *load-time-value-counter*))
2837 (push (cold-list (cold-intern :load-time-value) fun
2838 (number-to-core counter)) *!cold-toplevels*)
2839 (setf *load-time-value-counter* (1+ counter))
2840 (make-descriptor 0 :load-time-value counter)))))
2842 (define-cold-fop (fop-funcall-for-effect)
2843 (multiple-value-bind (fun args) (pop-args (fasl-input))
2844 (if (not args)
2845 (push fun *!cold-toplevels*)
2846 (case fun
2847 (sb!impl::%defun (apply #'cold-fset args))
2848 (sb!pcl::!trivial-defmethod (apply #'cold-defmethod args))
2849 (sb!kernel::%defstruct
2850 (push args *known-structure-classoids*)
2851 (push (apply #'cold-list (cold-intern 'defstruct) args)
2852 *!cold-toplevels*))
2853 (sb!c::%defconstant
2854 (destructuring-bind (name val . rest) args
2855 (cold-set name (if (symbolp val) (cold-intern val) val))
2856 (push (cold-cons (cold-intern name) (list-to-core rest))
2857 *!cold-defconstants*)))
2858 (set
2859 (aver (= (length args) 2))
2860 (cold-set (first args)
2861 (let ((val (second args)))
2862 (if (symbolp val) (cold-intern val) val))))
2863 (%svset (apply 'cold-svset args))
2864 (t (call fun :sb-cold-funcall-handler/for-effect args)))))))
2866 (defun finalize-load-time-value-noise ()
2867 (cold-set '*!load-time-values*
2868 (allocate-vector-object *dynamic*
2869 sb!vm:n-word-bits
2870 *load-time-value-counter*
2871 sb!vm:simple-vector-widetag)))
2874 ;;;; cold fops for fixing up circularities
2876 (define-cold-fop (fop-rplaca)
2877 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2878 (idx (read-word-arg (fasl-input-stream))))
2879 (write-memory (cold-nthcdr idx obj) (pop-stack))))
2881 (define-cold-fop (fop-rplacd)
2882 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2883 (idx (read-word-arg (fasl-input-stream))))
2884 (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2886 (define-cold-fop (fop-svset)
2887 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2888 (idx (read-word-arg (fasl-input-stream))))
2889 (write-wordindexed obj
2890 (+ idx
2891 (ecase (descriptor-lowtag obj)
2892 (#.sb!vm:instance-pointer-lowtag 1)
2893 (#.sb!vm:other-pointer-lowtag 2)))
2894 (pop-stack))))
2896 (define-cold-fop (fop-structset)
2897 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2898 (idx (read-word-arg (fasl-input-stream))))
2899 (write-wordindexed obj (+ idx sb!vm:instance-slots-offset) (pop-stack))))
2901 (define-cold-fop (fop-nthcdr)
2902 (cold-nthcdr (read-word-arg (fasl-input-stream)) (pop-stack)))
2904 (defun cold-nthcdr (index obj)
2905 (dotimes (i index)
2906 (setq obj (read-wordindexed obj sb!vm:cons-cdr-slot)))
2907 obj)
2909 ;;;; cold fops for loading code objects and functions
2911 (define-cold-fop (fop-note-debug-source)
2912 (let ((debug-source (pop-stack)))
2913 (cold-push debug-source *current-debug-sources*)))
2915 (define-cold-fop (fop-fdefn)
2916 (cold-fdefinition-object (pop-stack)))
2918 (define-cold-fop (fop-known-fun)
2919 (let* ((name (pop-stack))
2920 (fun (cold-fdefn-fun (cold-fdefinition-object name))))
2921 (if (cold-null fun) `(:known-fun . ,name) fun)))
2923 #!-(or x86 x86-64)
2924 (define-cold-fop (fop-sanctify-for-execution)
2925 (pop-stack))
2927 ;;; Setting this variable shows what code looks like before any
2928 ;;; fixups (or function headers) are applied.
2929 #!+sb-show (defvar *show-pre-fixup-code-p* nil)
2931 (defun cold-load-code (fasl-input code-size nconst nfuns)
2932 (macrolet ((pop-stack () '(pop-fop-stack (%fasl-input-stack fasl-input))))
2933 (let* ((raw-header-n-words (+ sb!vm:code-constants-offset nconst))
2934 ;; Note that the number of constants is rounded up to ensure
2935 ;; that the code vector will be properly aligned.
2936 (header-n-words (round-up raw-header-n-words 2))
2937 (toplevel-p (pop-stack))
2938 (debug-info (pop-stack))
2939 (des (allocate-cold-descriptor
2940 #!-immobile-code *dynamic*
2941 ;; toplevel-p is an indicator of whether the code will
2942 ;; will become garbage. If so, put it in dynamic space,
2943 ;; otherwise immobile space.
2944 #!+immobile-code
2945 (if toplevel-p *dynamic* *immobile-varyobj*)
2946 (+ (ash header-n-words sb!vm:word-shift) code-size)
2947 sb!vm:other-pointer-lowtag)))
2948 (declare (ignorable toplevel-p))
2949 (write-header-word des header-n-words sb!vm:code-header-widetag)
2950 (write-wordindexed des sb!vm:code-code-size-slot
2951 (make-fixnum-descriptor code-size))
2952 (write-wordindexed des sb!vm:code-debug-info-slot debug-info)
2953 (do ((index (1- raw-header-n-words) (1- index)))
2954 ((< index sb!vm:code-constants-offset))
2955 (let ((obj (pop-stack)))
2956 (if (and (consp obj) (eq (car obj) :known-fun))
2957 (push (list* (cdr obj) des index) *deferred-known-fun-refs*)
2958 (write-wordindexed des index obj))))
2959 (let* ((start (+ (descriptor-byte-offset des)
2960 (ash header-n-words sb!vm:word-shift)))
2961 (end (+ start code-size)))
2962 (read-bigvec-as-sequence-or-die (descriptor-mem des)
2963 (%fasl-input-stream fasl-input)
2964 :start start
2965 :end end)
2967 ;; Emulate NEW-SIMPLE-FUN in target-core
2968 (loop for fun-index from (1- nfuns) downto 0
2969 do (let ((offset (read-varint-arg fasl-input)))
2970 (if (> fun-index 0)
2971 (let ((bytes (descriptor-mem des))
2972 (index (+ (descriptor-byte-offset des)
2973 (calc-offset des (ash (1- fun-index) 2)))))
2974 (aver (eql (bvref-32 bytes index) 0))
2975 (setf (bvref-32 bytes index) offset))
2976 #!-64-bit
2977 (write-wordindexed/raw
2979 sb!vm::code-n-entries-slot
2980 (logior (ash offset 16)
2981 (ash nfuns sb!vm:n-fixnum-tag-bits)))
2982 #!+64-bit
2983 (write-wordindexed/raw
2984 des 0
2985 (logior (ash (logior (ash offset 16) nfuns) 32)
2986 (read-bits-wordindexed des 0))))))
2988 #!+sb-show
2989 (when *show-pre-fixup-code-p*
2990 (format *trace-output*
2991 "~&/raw code from code-fop ~W ~W:~%"
2992 nconst
2993 code-size)
2994 (do ((i start (+ i sb!vm:n-word-bytes)))
2995 ((>= i end))
2996 (format *trace-output*
2997 "/#X~8,'0x: #X~8,'0x~%"
2998 (+ i (gspace-byte-address (descriptor-gspace des)))
2999 (bvref-32 (descriptor-mem des) i)))))
3000 des)))
3002 (let ((i (get 'fop-code 'opcode)))
3003 (fill **fop-funs** #'cold-load-code :start i :end (+ i 4)))
3005 (defun resolve-deferred-known-funs ()
3006 (dolist (item *deferred-known-fun-refs*)
3007 (let ((fun (cold-fdefn-fun (cold-fdefinition-object (car item)))))
3008 (aver (not (cold-null fun)))
3009 (let ((place (cdr item)))
3010 (write-wordindexed (car place) (cdr place) fun)))))
3012 (define-cold-fop (fop-alter-code (slot))
3013 (let ((value (pop-stack))
3014 (code (pop-stack)))
3015 (write-wordindexed code slot value)))
3017 (defvar *simple-fun-metadata* (make-hash-table :test 'equalp))
3019 ;; Return an expression that can be used to coalesce type-specifiers
3020 ;; and lambda lists attached to simple-funs. It doesn't have to be
3021 ;; a "correct" host representation, just something that preserves EQUAL-ness.
3022 (defun make-equal-comparable-thing (descriptor)
3023 (labels ((recurse (x)
3024 (cond ((cold-null x) (return-from recurse nil))
3025 ((is-fixnum-lowtag (descriptor-lowtag x))
3026 (return-from recurse (descriptor-fixnum x)))
3027 #!+64-bit
3028 ((is-other-immediate-lowtag (descriptor-lowtag x))
3029 (let ((bits (descriptor-bits x)))
3030 (when (= (logand bits sb!vm:widetag-mask)
3031 sb!vm:single-float-widetag)
3032 (return-from recurse `(:ffloat-bits ,bits))))))
3033 (ecase (descriptor-lowtag x)
3034 (#.sb!vm:list-pointer-lowtag
3035 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
3036 (#.sb!vm:other-pointer-lowtag
3037 (ecase (logand (descriptor-bits (read-memory x)) sb!vm:widetag-mask)
3038 (#.sb!vm:symbol-header-widetag
3039 (if (cold-null (read-wordindexed x sb!vm:symbol-package-slot))
3040 (get-or-make-uninterned-symbol
3041 (base-string-from-core
3042 (read-wordindexed x sb!vm:symbol-name-slot)))
3043 (warm-symbol x)))
3044 #!-64-bit
3045 (#.sb!vm:single-float-widetag
3046 `(:ffloat-bits
3047 ,(read-bits-wordindexed x sb!vm:single-float-value-slot)))
3048 (#.sb!vm:double-float-widetag
3049 `(:dfloat-bits
3050 ,(read-bits-wordindexed x sb!vm:double-float-value-slot)
3051 #!-64-bit
3052 ,(read-bits-wordindexed
3053 x (1+ sb!vm:double-float-value-slot))))
3054 (#.sb!vm:bignum-widetag
3055 (bignum-from-core x))
3056 (#.sb!vm:simple-base-string-widetag
3057 (base-string-from-core x))
3058 ;; Why do function lambda lists have simple-vectors in them?
3059 ;; Because we expose all &OPTIONAL and &KEY default forms.
3060 ;; I think this is abstraction leakage, except possibly for
3061 ;; advertised constant defaults of NIL and such.
3062 ;; How one expresses a value as a sexpr should otherwise
3063 ;; be of no concern to a user of the code.
3064 (#.sb!vm:simple-vector-widetag
3065 (vector-from-core x #'recurse))))))
3066 ;; Return a warm symbol whose name is similar to NAME, coaelescing
3067 ;; all occurrences of #:.WHOLE. across all files, e.g.
3068 (get-or-make-uninterned-symbol (name)
3069 (let ((key `(:uninterned-symbol ,name)))
3070 (or (gethash key *simple-fun-metadata*)
3071 (let ((symbol (make-symbol name)))
3072 (setf (gethash key *simple-fun-metadata*) symbol))))))
3073 (recurse descriptor)))
3075 (defun fun-offset (code-object fun-index)
3076 (if (> fun-index 0)
3077 (bvref-32 (descriptor-mem code-object)
3078 (+ (descriptor-byte-offset code-object)
3079 (calc-offset code-object (ash (1- fun-index) 2))))
3080 (ldb (byte 16 16)
3081 #!-64-bit (read-bits-wordindexed code-object sb!vm::code-n-entries-slot)
3082 #!+64-bit (ldb (byte 32 32) (read-bits-wordindexed code-object 0)))))
3084 (defun compute-fun (code-object fun-index)
3085 (let* ((offset-from-insns-start (fun-offset code-object fun-index))
3086 (offset-from-code-start (calc-offset code-object offset-from-insns-start)))
3087 (unless (zerop (logand offset-from-code-start sb!vm:lowtag-mask))
3088 (error "unaligned function entry ~S ~S" code-object fun-index))
3089 (values (ash offset-from-code-start (- sb!vm:word-shift))
3090 (make-descriptor
3091 (logior (+ (logandc2 (descriptor-bits code-object) sb!vm:lowtag-mask)
3092 offset-from-code-start)
3093 sb!vm:fun-pointer-lowtag)))))
3095 (defun cold-fop-fun-entry (fasl-input fun-index)
3096 (binding* (((info type arglist name code-object)
3097 (macrolet ((pop-stack ()
3098 '(pop-fop-stack (%fasl-input-stack fasl-input))))
3099 (values (pop-stack) (pop-stack) (pop-stack) (pop-stack) (pop-stack))))
3100 ((word-offset fn)
3101 (compute-fun code-object fun-index)))
3102 (write-memory fn (make-other-immediate-descriptor
3103 word-offset sb!vm:simple-fun-header-widetag))
3104 #!+(or x86 x86-64) ; store a machine-native pointer to the function entry
3105 ;; note that the bit pattern looks like fixnum due to alignment
3106 (write-wordindexed/raw fn sb!vm:simple-fun-self-slot
3107 (+ (- (descriptor-bits fn) sb!vm:fun-pointer-lowtag)
3108 (ash sb!vm:simple-fun-code-offset sb!vm:word-shift)))
3109 #!-(or x86 x86-64) ; store a pointer back to the function itself in 'self'
3110 (write-wordindexed fn sb!vm:simple-fun-self-slot fn)
3111 (write-wordindexed fn sb!vm:simple-fun-name-slot name)
3112 (flet ((coalesce (sexpr) ; a warm symbol or a cold cons tree
3113 (if (symbolp sexpr) ; will be cold-interned automatically
3114 sexpr
3115 (let ((representation (make-equal-comparable-thing sexpr)))
3116 (or (gethash representation *simple-fun-metadata*)
3117 (setf (gethash representation *simple-fun-metadata*)
3118 sexpr))))))
3119 (write-wordindexed fn sb!vm:simple-fun-arglist-slot (coalesce arglist))
3120 (write-wordindexed fn sb!vm:simple-fun-type-slot (coalesce type)))
3121 (write-wordindexed fn sb!vm::simple-fun-info-slot info)
3122 fn))
3124 (let ((i (get 'fop-fun-entry 'opcode)))
3125 (fill **fop-funs** #'cold-fop-fun-entry :start i :end (+ i 4)))
3127 #!+sb-thread
3128 (define-cold-fop (fop-symbol-tls-fixup)
3129 (let* ((symbol (pop-stack))
3130 (kind (pop-stack))
3131 (code-object (pop-stack)))
3132 (do-cold-fixup code-object
3133 (read-word-arg (fasl-input-stream))
3134 (ensure-symbol-tls-index symbol) kind)
3135 code-object))
3137 (define-cold-fop (fop-foreign-fixup)
3138 (let* ((kind (pop-stack))
3139 (code-object (pop-stack))
3140 (len (read-byte-arg (fasl-input-stream)))
3141 (sym (make-string len)))
3142 (read-string-as-bytes (fasl-input-stream) sym)
3143 #!+sb-dynamic-core
3144 (let ((offset (read-word-arg (fasl-input-stream)))
3145 (value (dyncore-note-symbol sym nil)))
3146 (do-cold-fixup code-object offset value kind))
3147 #!- (and) (format t "Bad non-plt fixup: ~S~S~%" sym code-object)
3148 #!-sb-dynamic-core
3149 (let ((offset (read-word-arg (fasl-input-stream)))
3150 (value (cold-foreign-symbol-address sym)))
3151 (do-cold-fixup code-object offset value kind))
3152 code-object))
3154 #!+linkage-table
3155 (define-cold-fop (fop-foreign-dataref-fixup)
3156 (let* ((kind (pop-stack))
3157 (code-object (pop-stack))
3158 (len (read-byte-arg (fasl-input-stream)))
3159 (sym (make-string len)))
3160 #!-sb-dynamic-core (declare (ignore code-object))
3161 (read-string-as-bytes (fasl-input-stream) sym)
3162 #!+sb-dynamic-core
3163 (let ((offset (read-word-arg (fasl-input-stream)))
3164 (value (dyncore-note-symbol sym t)))
3165 (do-cold-fixup code-object offset value kind)
3166 code-object)
3167 #!-sb-dynamic-core
3168 (progn
3169 (maphash (lambda (k v)
3170 (format *error-output* "~&~S = #X~8X~%" k v))
3171 *cold-foreign-symbol-table*)
3172 (error "shared foreign symbol in cold load: ~S (~S)" sym kind))))
3174 (define-cold-fop (fop-assembler-code)
3175 (let* ((length (read-word-arg (fasl-input-stream)))
3176 (header-n-words
3177 ;; Note: we round the number of constants up to ensure that
3178 ;; the code vector will be properly aligned.
3179 (round-up sb!vm:code-constants-offset 2))
3180 (des (allocate-cold-descriptor *read-only*
3181 (+ (ash header-n-words
3182 sb!vm:word-shift)
3183 length)
3184 sb!vm:other-pointer-lowtag)))
3185 (write-header-word des header-n-words sb!vm:code-header-widetag)
3186 (write-wordindexed des
3187 sb!vm:code-code-size-slot
3188 (make-fixnum-descriptor length))
3189 (write-wordindexed des sb!vm:code-debug-info-slot *nil-descriptor*)
3191 (let* ((start (+ (descriptor-byte-offset des)
3192 (ash header-n-words sb!vm:word-shift)))
3193 (end (+ start length)))
3194 (read-bigvec-as-sequence-or-die (descriptor-mem des)
3195 (fasl-input-stream)
3196 :start start
3197 :end end))
3198 des))
3200 (define-cold-fop (fop-assembler-routine)
3201 (let* ((routine (pop-stack))
3202 (des (pop-stack))
3203 (offset (calc-offset des (read-word-arg (fasl-input-stream)))))
3204 (record-cold-assembler-routine
3205 routine
3206 (+ (logandc2 (descriptor-bits des) sb!vm:lowtag-mask) offset))
3207 des))
3209 (define-cold-fop (fop-assembler-fixup)
3210 (let* ((routine (pop-stack))
3211 (kind (pop-stack))
3212 (code-object (pop-stack))
3213 (offset (read-word-arg (fasl-input-stream))))
3214 (record-cold-assembler-fixup routine code-object offset kind)
3215 code-object))
3217 (define-cold-fop (fop-code-object-fixup)
3218 (let* ((kind (pop-stack))
3219 (code-object (pop-stack))
3220 (offset (read-word-arg (fasl-input-stream)))
3221 (value (descriptor-bits code-object)))
3222 (do-cold-fixup code-object offset value kind)
3223 code-object))
3225 #!+immobile-code
3226 (define-cold-fop (fop-static-call-fixup)
3227 (let ((name (pop-stack))
3228 (kind (pop-stack))
3229 (code-object (pop-stack))
3230 (offset (read-word-arg (fasl-input-stream))))
3231 (push (list name kind code-object offset) *cold-static-call-fixups*)
3232 code-object))
3235 ;;;; sanity checking space layouts
3237 (defun check-spaces ()
3238 ;;; Co-opt type machinery to check for intersections...
3239 (let (types)
3240 (flet ((check (start end space)
3241 (unless (< start end)
3242 (error "Bogus space: ~A" space))
3243 (let ((type (specifier-type `(integer ,start ,end))))
3244 (dolist (other types)
3245 (unless (eq *empty-type* (type-intersection (cdr other) type))
3246 (error "Space overlap: ~A with ~A" space (car other))))
3247 (push (cons space type) types))))
3248 (check sb!vm:read-only-space-start sb!vm:read-only-space-end :read-only)
3249 (check sb!vm:static-space-start sb!vm:static-space-end :static)
3250 #!+gencgc
3251 (check sb!vm:dynamic-space-start sb!vm:dynamic-space-end :dynamic)
3252 #!+immobile-space
3253 ;; Must be a multiple of 32 because it makes the math a nicer
3254 ;; when computing word and bit index into the 'touched' bitmap.
3255 (assert (zerop (rem sb!vm:immobile-fixedobj-subspace-size
3256 (* 32 sb!vm:immobile-card-bytes))))
3257 #!-gencgc
3258 (progn
3259 (check sb!vm:dynamic-0-space-start sb!vm:dynamic-0-space-end :dynamic-0)
3260 (check sb!vm:dynamic-1-space-start sb!vm:dynamic-1-space-end :dynamic-1))
3261 #!+linkage-table
3262 (check sb!vm:linkage-table-space-start sb!vm:linkage-table-space-end :linkage-table))))
3264 ;;;; emitting C header file
3266 (defun tailwise-equal (string tail)
3267 (and (>= (length string) (length tail))
3268 (string= string tail :start1 (- (length string) (length tail)))))
3270 (defun write-boilerplate (*standard-output*)
3271 (format t "/*~%")
3272 (dolist (line
3273 '("This is a machine-generated file. Please do not edit it by hand."
3274 "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
3276 "This file contains low-level information about the"
3277 "internals of a particular version and configuration"
3278 "of SBCL. It is used by the C compiler to create a runtime"
3279 "support environment, an executable program in the host"
3280 "operating system's native format, which can then be used to"
3281 "load and run 'core' files, which are basically programs"
3282 "in SBCL's own format."))
3283 (format t " *~@[ ~A~]~%" line))
3284 (format t " */~%"))
3286 (defun c-name (string &optional strip)
3287 (delete #\+
3288 (substitute-if #\_ (lambda (c) (member c '(#\- #\/ #\%)))
3289 (remove-if (lambda (c) (position c strip))
3290 string))))
3292 (defun c-symbol-name (symbol &optional strip)
3293 (c-name (symbol-name symbol) strip))
3295 (defun write-makefile-features (*standard-output*)
3296 ;; propagating *SHEBANG-FEATURES* into the Makefiles
3297 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
3298 sb-cold:*shebang-features*)
3299 #'string<))
3300 (format t "LISP_FEATURE_~A=1~%" shebang-feature-name)))
3302 (defun write-config-h (*standard-output*)
3303 ;; propagating *SHEBANG-FEATURES* into C-level #define's
3304 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
3305 sb-cold:*shebang-features*)
3306 #'string<))
3307 (format t "#define LISP_FEATURE_~A~%" shebang-feature-name))
3308 (terpri)
3309 ;; and miscellaneous constants
3310 (format t "#define SBCL_VERSION_STRING ~S~%"
3311 (sb!xc:lisp-implementation-version))
3312 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
3313 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3314 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
3315 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
3316 (format t "#define LISPOBJ(thing) thing~2%")
3317 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")
3318 (terpri))
3320 (defun write-constants-h (*standard-output*)
3321 ;; writing entire families of named constants
3322 (let ((constants nil))
3323 (dolist (package-name '("SB!VM"
3324 ;; We also propagate magic numbers
3325 ;; related to file format,
3326 ;; which live here instead of SB!VM.
3327 "SB!FASL"))
3328 (do-external-symbols (symbol (find-package package-name))
3329 (when (constantp symbol)
3330 (let ((name (symbol-name symbol)))
3331 (labels ( ;; shared machinery
3332 (record (string priority suffix)
3333 (push (list string
3334 priority
3335 (symbol-value symbol)
3336 suffix
3337 (documentation symbol 'variable))
3338 constants))
3339 ;; machinery for old-style CMU CL Lisp-to-C
3340 ;; arbitrary renaming, being phased out in favor of
3341 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
3342 ;; renaming
3343 (record-with-munged-name (prefix string priority)
3344 (record (concatenate
3345 'simple-string
3346 prefix
3347 (delete #\- (string-capitalize string)))
3348 priority
3349 ""))
3350 (maybe-record-with-munged-name (tail prefix priority)
3351 (when (tailwise-equal name tail)
3352 (record-with-munged-name prefix
3353 (subseq name 0
3354 (- (length name)
3355 (length tail)))
3356 priority)))
3357 ;; machinery for new-style SBCL Lisp-to-C naming
3358 (record-with-translated-name (priority large)
3359 (record (c-name name) priority
3360 (if large
3361 #!+(and win32 x86-64) "LLU"
3362 #!-(and win32 x86-64) "LU"
3363 "")))
3364 (maybe-record-with-translated-name (suffixes priority &key large)
3365 (when (some (lambda (suffix)
3366 (tailwise-equal name suffix))
3367 suffixes)
3368 (record-with-translated-name priority large))))
3369 (maybe-record-with-translated-name '("-LOWTAG") 0)
3370 (maybe-record-with-translated-name '("-WIDETAG" "-SHIFT") 1)
3371 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
3372 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
3373 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
3374 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
3375 (maybe-record-with-translated-name '("-SIZE" "-INTERRUPTS") 6)
3376 (maybe-record-with-translated-name '("-START" "-END" "-PAGE-BYTES"
3377 "-CARD-BYTES" "-GRANULARITY")
3378 7 :large t)
3379 (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 8)
3380 (maybe-record-with-translated-name '("-CORE-SPACE-ID") 9)
3381 (maybe-record-with-translated-name '("-CORE-SPACE-ID-FLAG") 9)
3382 (maybe-record-with-translated-name '("-GENERATION+") 10))))))
3383 ;; KLUDGE: these constants are sort of important, but there's no
3384 ;; pleasing way to inform the code above about them. So we fake
3385 ;; it for now. nikodemus on #lisp (2004-08-09) suggested simply
3386 ;; exporting every numeric constant from SB!VM; that would work,
3387 ;; but the C runtime would have to be altered to use Lisp-like names
3388 ;; rather than the munged names currently exported. --njf, 2004-08-09
3389 (dolist (c '(sb!vm:n-word-bits sb!vm:n-word-bytes
3390 sb!vm:n-lowtag-bits sb!vm:lowtag-mask
3391 sb!vm:n-widetag-bits sb!vm:widetag-mask
3392 sb!vm:n-fixnum-tag-bits sb!vm:fixnum-tag-mask
3393 sb!vm:short-header-max-words))
3394 (push (list (c-symbol-name c)
3395 -1 ; invent a new priority
3396 (symbol-value c)
3398 nil)
3399 constants))
3400 ;; One more symbol that doesn't fit into the code above.
3401 (let ((c 'sb!impl::+magic-hash-vector-value+))
3402 (push (list (c-symbol-name c)
3404 (symbol-value c)
3405 #!+(and win32 x86-64) "LLU"
3406 #!-(and win32 x86-64) "LU"
3407 nil)
3408 constants))
3409 (setf constants
3410 (sort constants
3411 (lambda (const1 const2)
3412 (if (= (second const1) (second const2))
3413 (if (= (third const1) (third const2))
3414 (string< (first const1) (first const2))
3415 (< (third const1) (third const2)))
3416 (< (second const1) (second const2))))))
3417 (let ((prev-priority (second (car constants))))
3418 (dolist (const constants)
3419 (destructuring-bind (name priority value suffix doc) const
3420 (unless (= prev-priority priority)
3421 (terpri)
3422 (setf prev-priority priority))
3423 (when (minusp value)
3424 (error "stub: negative values unsupported"))
3425 (format t "#define ~A ~A~A /* 0x~X ~@[ -- ~A ~]*/~%" name value suffix value doc))))
3426 (terpri))
3428 ;; writing information about internal errors
3429 ;; Assembly code needs only the constants for UNDEFINED_[ALIEN_]FUN_ERROR
3430 ;; but to avoid imparting that knowledge here, we'll expose all error
3431 ;; number constants except for OBJECT-NOT-<x>-ERROR ones.
3432 (loop for (description name) across sb!c:+backend-internal-errors+
3433 for i from 0
3434 when (stringp description)
3435 do (format t "#define ~A ~D~%" (c-symbol-name name) i))
3436 ;; C code needs strings for describe_internal_error()
3437 (format t "#define INTERNAL_ERROR_NAMES ~{\\~%~S~^, ~}~2%"
3438 (map 'list 'sb!kernel::!c-stringify-internal-error
3439 sb!c:+backend-internal-errors+))
3440 (format t "#define INTERNAL_ERROR_NARGS {~{~S~^, ~}}~2%"
3441 (map 'list #'cddr sb!c:+backend-internal-errors+))
3443 ;; I'm not really sure why this is in SB!C, since it seems
3444 ;; conceptually like something that belongs to SB!VM. In any case,
3445 ;; it's needed C-side.
3446 (format t "#define BACKEND_PAGE_BYTES ~DLU~%" sb!c:*backend-page-bytes*)
3448 (terpri)
3450 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
3451 ;; platforms. If we export this from the SB!VM package, it gets
3452 ;; written out as #define trap_PseudoAtomic, which is confusing as
3453 ;; the runtime treats trap_ as the prefix for illegal instruction
3454 ;; type things. We therefore don't export it, but instead do
3455 #!+sparc
3456 (when (boundp 'sb!vm::pseudo-atomic-trap)
3457 (format t
3458 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
3459 sb!vm::pseudo-atomic-trap)
3460 (terpri))
3461 ;; possibly this is another candidate for a rename (to
3462 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
3463 ;; [possibly applicable to other platforms])
3465 #!+sb-safepoint
3466 (format t "#define GC_SAFEPOINT_PAGE_ADDR ((void*)0x~XUL) /* ~:*~A */~%"
3467 sb!vm:gc-safepoint-page-addr)
3469 (dolist (symbol '(sb!vm::float-traps-byte
3470 sb!vm::float-exceptions-byte
3471 sb!vm::float-sticky-bits
3472 sb!vm::float-rounding-mode))
3473 (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
3474 (c-symbol-name symbol)
3475 (sb!xc:byte-position (symbol-value symbol)))
3476 (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
3477 (c-symbol-name symbol)
3478 (sb!xc:mask-field (symbol-value symbol) -1))))
3480 #!+sb-ldb
3481 (defun write-tagnames-h (out)
3482 (labels
3483 ((pretty-name (symbol strip)
3484 (let ((name (string-downcase symbol)))
3485 (substitute #\Space #\-
3486 (subseq name 0 (- (length name) (length strip))))))
3487 (list-sorted-tags (tail)
3488 (loop for symbol being the external-symbols of "SB!VM"
3489 when (and (constantp symbol)
3490 (tailwise-equal (string symbol) tail))
3491 collect symbol into tags
3492 finally (return (sort tags #'< :key #'symbol-value))))
3493 (write-tags (kind limit ash-count)
3494 (format out "~%static const char *~(~A~)_names[] = {~%"
3495 (subseq kind 1))
3496 (let ((tags (list-sorted-tags kind)))
3497 (dotimes (i limit)
3498 (if (eql i (ash (or (symbol-value (first tags)) -1) ash-count))
3499 (format out " \"~A\"" (pretty-name (pop tags) kind))
3500 (format out " \"unknown [~D]\"" i))
3501 (unless (eql i (1- limit))
3502 (write-string "," out))
3503 (terpri out)))
3504 (write-line "};" out)))
3505 (write-tags "-LOWTAG" sb!vm:lowtag-limit 0)
3506 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
3507 ;; ending with the same 2 bits. (#b10)
3508 (write-tags "-WIDETAG" (ash (1+ sb!vm:widetag-mask) -2) -2))
3509 ;; Inform print_otherptr() of all array types that it's too dumb to print
3510 (let ((array-type-bits (make-array 32 :initial-element 0)))
3511 (flet ((toggle (b)
3512 (multiple-value-bind (ofs bit) (floor b 8)
3513 (setf (aref array-type-bits ofs) (ash 1 bit)))))
3514 (dovector (saetp sb!vm:*specialized-array-element-type-properties*)
3515 (unless (or (typep (sb!vm:saetp-ctype saetp) 'character-set-type)
3516 (eq (sb!vm:saetp-specifier saetp) t))
3517 (toggle (sb!vm:saetp-typecode saetp))
3518 (awhen (sb!vm:saetp-complex-typecode saetp) (toggle it)))))
3519 (format out
3520 "~%static unsigned char unprintable_array_types[32] =~% {~{~d~^,~}};~%"
3521 (coerce array-type-bits 'list)))
3522 (values))
3524 (defun write-primitive-object (obj *standard-output*)
3525 ;; writing primitive object layouts
3526 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3527 (format t "struct ~A {~%" (c-name (string-downcase (sb!vm:primitive-object-name obj))))
3528 (when (sb!vm:primitive-object-widetag obj)
3529 (format t " lispobj header;~%"))
3530 (dolist (slot (sb!vm:primitive-object-slots obj))
3531 (format t " ~A ~A~@[[1]~];~%"
3532 (getf (sb!vm:slot-options slot) :c-type "lispobj")
3533 (c-name (string-downcase (sb!vm:slot-name slot)))
3534 (sb!vm:slot-rest-p slot)))
3535 (format t "};~2%")
3536 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
3537 (format t "/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
3538 (format t " * so they work directly on tagged addresses. */~2%")
3539 (let ((name (sb!vm:primitive-object-name obj))
3540 (lowtag (or (symbol-value (sb!vm:primitive-object-lowtag obj))
3541 0)))
3542 (dolist (slot (sb!vm:primitive-object-slots obj))
3543 (format t "#define ~A_~A_OFFSET ~D~%"
3544 (c-symbol-name name)
3545 (c-symbol-name (sb!vm:slot-name slot))
3546 (- (* (sb!vm:slot-offset slot) sb!vm:n-word-bytes) lowtag)))
3547 (terpri))
3548 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%"))
3550 (defun write-structure-object (dd *standard-output*)
3551 (flet ((cstring (designator) (c-name (string-downcase designator))))
3552 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3553 (format t "struct ~A {~%" (cstring (dd-name dd)))
3554 (format t " lispobj header; // = word_0_~%")
3555 ;; "self layout" slots are named '_layout' instead of 'layout' so that
3556 ;; classoid's expressly declared layout isn't renamed as a special-case.
3557 #!-compact-instance-header (format t " lispobj _layout;~%")
3558 ;; Output exactly the number of Lisp words consumed by the structure,
3559 ;; no more, no less. C code can always compute the padded length from
3560 ;; the precise length, but the other way doesn't work.
3561 (let ((names
3562 (coerce (loop for i from sb!vm:instance-data-start below (dd-length dd)
3563 collect (list (format nil "word_~D_" (1+ i))))
3564 'vector)))
3565 (dolist (slot (dd-slots dd))
3566 (let ((cell (aref names (- (dsd-index slot) sb!vm:instance-data-start)))
3567 (name (cstring (dsd-name slot))))
3568 (if (eq (dsd-raw-type slot) t)
3569 (rplaca cell name)
3570 (rplacd cell name))))
3571 (loop for slot across names
3572 do (format t " lispobj ~A;~@[ // ~A~]~%" (car slot) (cdr slot))))
3573 (format t "};~2%")
3574 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")))
3576 (defun write-static-symbols (stream)
3577 (dolist (symbol (cons nil sb!vm:*static-symbols*))
3578 ;; FIXME: It would be nice to use longer names than NIL and
3579 ;; (particularly) T in #define statements.
3580 (format stream "#define ~A LISPOBJ(0x~X)~%"
3581 ;; FIXME: It would be nice not to need to strip anything
3582 ;; that doesn't get stripped always by C-SYMBOL-NAME.
3583 (c-symbol-name symbol "%*.!")
3584 (if *static* ; if we ran GENESIS
3585 ;; We actually ran GENESIS, use the real value.
3586 (descriptor-bits (cold-intern symbol))
3587 ;; We didn't run GENESIS, so guess at the address.
3588 (+ sb!vm:static-space-start
3589 sb!vm:n-word-bytes
3590 sb!vm:other-pointer-lowtag
3591 (if symbol (sb!vm:static-symbol-offset symbol) 0))))))
3593 (defun write-sc-offset-coding (stream)
3594 (flet ((write-array (name bytes)
3595 (format stream "static struct sc_offset_byte ~A[] = {~@
3596 ~{ {~{ ~2D, ~2D ~}}~^,~%~}~@
3597 };~2%"
3598 name
3599 (mapcar (lambda (byte)
3600 (list (byte-size byte) (byte-position byte)))
3601 bytes))))
3602 (format stream "struct sc_offset_byte {
3603 int size;
3604 int position;
3605 };~2%")
3606 (write-array "sc_offset_sc_number_bytes" sb!c::+sc-offset-scn-bytes+)
3607 (write-array "sc_offset_offset_bytes" sb!c::+sc-offset-offset-bytes+)))
3609 ;;;; writing map file
3611 ;;; Write a map file describing the cold load. Some of this
3612 ;;; information is subject to change due to relocating GC, but even so
3613 ;;; it can be very handy when attempting to troubleshoot the early
3614 ;;; stages of cold load.
3615 (defun write-map (*standard-output*)
3616 (let ((*print-pretty* nil)
3617 (*print-case* :upcase))
3618 (format t "assembler routines defined in core image:~2%")
3619 (dolist (routine (sort (copy-list *cold-assembler-routines*) #'<
3620 :key #'cdr))
3621 (format t "~8,'0X: ~S~%" (cdr routine) (car routine)))
3622 (let ((fdefns nil)
3623 (funs nil)
3624 (undefs nil))
3625 (maphash (lambda (name fdefn &aux (fun (cold-fdefn-fun fdefn)))
3626 (push (list (- (descriptor-bits fdefn) (descriptor-lowtag fdefn))
3627 name) fdefns)
3628 (if (cold-null fun)
3629 (push name undefs)
3630 (push (list (- (descriptor-bits fun) (descriptor-lowtag fun))
3631 name) funs)))
3632 *cold-fdefn-objects*)
3633 (format t "~%~|~%fdefns (native pointer):
3634 ~:{~%~8,'0X: ~S~}~%" (sort fdefns #'< :key #'car))
3635 (format t "~%~|~%initially defined functions (native pointer):
3636 ~:{~%~8,'0X: ~S~}~%" (sort funs #'< :key #'car))
3637 (format t
3638 "~%~|
3639 (a note about initially undefined function references: These functions
3640 are referred to by code which is installed by GENESIS, but they are not
3641 installed by GENESIS. This is not necessarily a problem; functions can
3642 be defined later, by cold init toplevel forms, or in files compiled and
3643 loaded at warm init, or elsewhere. As long as they are defined before
3644 they are called, everything should be OK. Things are also OK if the
3645 cross-compiler knew their inline definition and used that everywhere
3646 that they were called before the out-of-line definition is installed,
3647 as is fairly common for structure accessors.)
3648 initially undefined function references:~2%")
3650 (setf undefs (sort undefs #'string< :key #'fun-name-block-name))
3651 (dolist (name undefs)
3652 (format t "~8,'0X: ~S~%"
3653 (descriptor-bits (gethash name *cold-fdefn-objects*))
3654 name)))
3656 (format t "~%~|~%layout names:~2%")
3657 (dolist (x (sort-cold-layouts))
3658 (let* ((des (cdr x))
3659 (inherits (read-slot des *host-layout-of-layout* :inherits)))
3660 (format t "~8,'0X: ~S[~D]~%~10T~:S~%" (descriptor-bits des) (car x)
3661 (cold-layout-length des) (listify-cold-inherits inherits))))
3663 (format t "~%~|~%parsed type specifiers:~2%")
3664 (mapc (lambda (cell)
3665 (format t "~X: ~S~%" (descriptor-bits (cdr cell)) (car cell)))
3666 (sort (%hash-table-alist *ctype-cache*) #'<
3667 :key (lambda (x) (descriptor-bits (cdr x))))))
3668 (values))
3670 ;;;; writing core file
3672 (defvar *core-file*)
3673 (defvar *data-page*)
3675 ;;; magic numbers to identify entries in a core file
3677 ;;; (In case you were wondering: No, AFAIK there's no special magic about
3678 ;;; these which requires them to be in the 38xx range. They're just
3679 ;;; arbitrary words, tested not for being in a particular range but just
3680 ;;; for equality. However, if you ever need to look at a .core file and
3681 ;;; figure out what's going on, it's slightly convenient that they're
3682 ;;; all in an easily recognizable range, and displacing the range away from
3683 ;;; zero seems likely to reduce the chance that random garbage will be
3684 ;;; misinterpreted as a .core file.)
3685 (defconstant build-id-core-entry-type-code 3860)
3686 (defconstant new-directory-core-entry-type-code 3861)
3687 (defconstant initial-fun-core-entry-type-code 3863)
3688 (defconstant page-table-core-entry-type-code 3880)
3689 (defconstant end-core-entry-type-code 3840)
3691 (declaim (ftype (function (sb!vm:word) sb!vm:word) write-word))
3692 (defun write-word (num)
3693 (ecase sb!c:*backend-byte-order*
3694 (:little-endian
3695 (dotimes (i sb!vm:n-word-bytes)
3696 (write-byte (ldb (byte 8 (* i 8)) num) *core-file*)))
3697 (:big-endian
3698 (dotimes (i sb!vm:n-word-bytes)
3699 (write-byte (ldb (byte 8 (* (- (1- sb!vm:n-word-bytes) i) 8)) num)
3700 *core-file*))))
3701 num)
3703 (defun output-gspace (gspace)
3704 (force-output *core-file*)
3705 (let* ((posn (file-position *core-file*))
3706 (bytes (* (gspace-free-word-index gspace) sb!vm:n-word-bytes))
3707 (pages (ceiling bytes sb!c:*backend-page-bytes*))
3708 (total-bytes (* pages sb!c:*backend-page-bytes*)))
3710 (file-position *core-file*
3711 (* sb!c:*backend-page-bytes* (1+ *data-page*)))
3712 (format t
3713 "writing ~S byte~:P [~S page~:P] from ~S~%"
3714 total-bytes
3715 pages
3716 gspace)
3717 (force-output)
3719 ;; Note: It is assumed that the GSPACE allocation routines always
3720 ;; allocate whole pages (of size *target-page-size*) and that any
3721 ;; empty gspace between the free pointer and the end of page will
3722 ;; be zero-filled. This will always be true under Mach on machines
3723 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3724 ;; 8K).
3725 (write-bigvec-as-sequence (gspace-bytes gspace)
3726 *core-file*
3727 :end total-bytes
3728 :pad-with-zeros t)
3729 (force-output *core-file*)
3730 (file-position *core-file* posn)
3732 ;; Write part of a (new) directory entry which looks like this:
3733 ;; GSPACE IDENTIFIER
3734 ;; WORD COUNT
3735 ;; DATA PAGE
3736 ;; ADDRESS
3737 ;; PAGE COUNT
3738 (write-word (gspace-identifier gspace))
3739 (write-word (gspace-free-word-index gspace))
3740 (write-word *data-page*)
3741 (multiple-value-bind (floor rem)
3742 (floor (gspace-byte-address gspace) sb!c:*backend-page-bytes*)
3743 (aver (zerop rem))
3744 (write-word floor))
3745 (write-word pages)
3747 (incf *data-page* pages)))
3749 ;;; Create a core file created from the cold loaded image. (This is
3750 ;;; the "initial core file" because core files could be created later
3751 ;;; by executing SAVE-LISP in a running system, perhaps after we've
3752 ;;; added some functionality to the system.)
3753 (declaim (ftype (function (string)) write-initial-core-file))
3754 (defun write-initial-core-file (filename)
3756 (let ((filenamestring (namestring filename))
3757 (*data-page* 0))
3759 (format t "[building initial core file in ~S: ~%" filenamestring)
3760 (force-output)
3762 (with-open-file (*core-file* filenamestring
3763 :direction :output
3764 :element-type '(unsigned-byte 8)
3765 :if-exists :rename-and-delete)
3767 ;; Write the magic number.
3768 (write-word core-magic)
3770 ;; Write the build ID.
3771 (write-word build-id-core-entry-type-code)
3772 (let ((build-id (with-open-file (s "output/build-id.tmp")
3773 (read s))))
3774 (declare (type simple-string build-id))
3775 (/show build-id (length build-id))
3776 ;; Write length of build ID record: BUILD-ID-CORE-ENTRY-TYPE-CODE
3777 ;; word, this length word, and one word for each char of BUILD-ID.
3778 (write-word (+ 2 (length build-id)))
3779 (dovector (char build-id)
3780 ;; (We write each character as a word in order to avoid
3781 ;; having to think about word alignment issues in the
3782 ;; sbcl-0.7.8 version of coreparse.c.)
3783 (write-word (sb!xc:char-code char))))
3785 ;; Write the New Directory entry header.
3786 (write-word new-directory-core-entry-type-code)
3787 (let ((spaces (nconc (list *read-only* *static*)
3788 #!+immobile-space
3789 (list *immobile-fixedobj* *immobile-varyobj*)
3790 (list *dynamic*))))
3791 ;; length = (5 words/space) * N spaces + 2 for header.
3792 (write-word (+ (* (length spaces) 5) 2))
3793 (mapc #'output-gspace spaces))
3795 ;; Write the initial function.
3796 (write-word initial-fun-core-entry-type-code)
3797 (write-word 3)
3798 (let* ((cold-name (cold-intern '!cold-init))
3799 (initial-fun
3800 (cold-fdefn-fun (cold-fdefinition-object cold-name))))
3801 (format t
3802 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
3803 (descriptor-bits initial-fun))
3804 (write-word (descriptor-bits initial-fun)))
3806 ;; Write the End entry.
3807 (write-word end-core-entry-type-code)
3808 (write-word 2)))
3810 (format t "done]~%")
3811 (force-output)
3812 (/show "leaving WRITE-INITIAL-CORE-FILE")
3813 (values))
3815 ;;;; the actual GENESIS function
3817 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3818 ;;; and/or information about a Lisp core, therefrom.
3820 ;;; input file arguments:
3821 ;;; SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
3822 ;;; *tab* *characters* *converted* *to* *spaces*. (We push
3823 ;;; responsibility for removing tabs out to the caller it's
3824 ;;; trivial to remove them using UNIX command line tools like
3825 ;;; sed, whereas it's a headache to do it portably in Lisp because
3826 ;;; #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
3827 ;;; a core file cannot be built (but a C header file can be).
3829 ;;; output files arguments (any of which may be NIL to suppress output):
3830 ;;; CORE-FILE-NAME gets a Lisp core.
3831 ;;; C-HEADER-DIR-NAME gets the path in which to place generated headers
3832 ;;; MAP-FILE-NAME gets the name of the textual 'cold-sbcl.map' file
3833 (defun sb-cold:genesis (&key object-file-names preload-file
3834 core-file-name c-header-dir-name map-file-name
3835 symbol-table-file-name)
3836 #!+sb-dynamic-core
3837 (declare (ignorable symbol-table-file-name))
3838 (declare (special core-file-name))
3840 (format t
3841 "~&beginning GENESIS, ~A~%"
3842 (if core-file-name
3843 ;; Note: This output summarizing what we're doing is
3844 ;; somewhat telegraphic in style, not meant to imply that
3845 ;; we're not e.g. also creating a header file when we
3846 ;; create a core.
3847 (format nil "creating core ~S" core-file-name)
3848 (format nil "creating headers in ~S" c-header-dir-name)))
3850 (let ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
3852 #!-sb-dynamic-core
3853 (when core-file-name
3854 (if symbol-table-file-name
3855 (load-cold-foreign-symbol-table symbol-table-file-name)
3856 (error "can't output a core file without symbol table file input")))
3858 ;; Now that we've successfully read our only input file (by
3859 ;; loading the symbol table, if any), it's a good time to ensure
3860 ;; that there'll be someplace for our output files to go when
3861 ;; we're done.
3862 (flet ((frob (filename)
3863 (when filename
3864 (ensure-directories-exist filename :verbose t))))
3865 (frob core-file-name)
3866 (frob map-file-name))
3868 ;; (This shouldn't matter in normal use, since GENESIS normally
3869 ;; only runs once in any given Lisp image, but it could reduce
3870 ;; confusion if we ever experiment with running, tweaking, and
3871 ;; rerunning genesis interactively.)
3872 (do-all-symbols (sym)
3873 (remprop sym 'cold-intern-info))
3875 (check-spaces)
3877 (let* ((*foreign-symbol-placeholder-value* (if core-file-name nil 0))
3878 (*load-time-value-counter* 0)
3879 (*cold-fdefn-objects* (make-hash-table :test 'equal))
3880 (*cold-symbols* (make-hash-table :test 'eql)) ; integer keys
3881 (*cold-package-symbols* (make-hash-table :test 'equal)) ; string keys
3882 (*read-only* (make-gspace :read-only
3883 read-only-core-space-id
3884 sb!vm:read-only-space-start))
3885 (*static* (make-gspace :static
3886 static-core-space-id
3887 sb!vm:static-space-start))
3888 #!+immobile-space
3889 (*immobile-fixedobj* (make-gspace :immobile-fixedobj
3890 immobile-fixedobj-core-space-id
3891 sb!vm:immobile-space-start))
3892 #!+immobile-space
3893 (*immobile-varyobj* (make-gspace :immobile-varyobj
3894 immobile-varyobj-core-space-id
3895 (+ sb!vm:immobile-space-start
3896 sb!vm:immobile-fixedobj-subspace-size)))
3897 (*dynamic* (make-gspace :dynamic
3898 dynamic-core-space-id
3899 #!+gencgc sb!vm:dynamic-space-start
3900 #!-gencgc sb!vm:dynamic-0-space-start))
3901 (*nil-descriptor* (make-nil-descriptor))
3902 (*simple-vector-0-descriptor* (vector-in-core nil))
3903 (*known-structure-classoids* nil)
3904 (*classoid-cells* (make-hash-table :test 'eq))
3905 (*ctype-cache* (make-hash-table :test 'equal))
3906 (*cold-layouts* (make-hash-table :test 'eq)) ; symbol -> cold-layout
3907 (*cold-layout-names* (make-hash-table :test 'eql)) ; addr -> symbol
3908 (*!cold-defconstants* nil)
3909 (*!cold-defuns* nil)
3910 ;; '*COLD-METHODS* is never seen in the target, so does not need
3911 ;; to adhere to the #\! convention for automatic uninterning.
3912 (*cold-methods* nil)
3913 (*!cold-toplevels* nil)
3914 (*current-debug-sources* *nil-descriptor*)
3915 *cold-static-call-fixups*
3916 *cold-assembler-fixups*
3917 *cold-assembler-routines*
3918 (*deferred-known-fun-refs* nil)
3919 #!+x86 (*load-time-code-fixups* (make-hash-table)))
3921 ;; If we're given a preload file, it contains tramps and whatnot
3922 ;; that must be loaded before we create any FDEFNs. It can in
3923 ;; theory be loaded any time between binding
3924 ;; *COLD-ASSEMBLER-ROUTINES* above and calling
3925 ;; INITIALIZE-STATIC-FNS below.
3926 (when preload-file
3927 (cold-load preload-file))
3929 ;; Prepare for cold load.
3930 (initialize-layouts)
3931 (initialize-packages)
3932 (initialize-static-symbols)
3933 (initialize-static-fns)
3935 ;; Initialize the *COLD-SYMBOLS* system with the information
3936 ;; from common-lisp-exports.lisp-expr.
3937 ;; Packages whose names match SB!THING were set up on the host according
3938 ;; to "package-data-list.lisp-expr" which expresses the desired target
3939 ;; package configuration, so we can just mirror the host into the target.
3940 ;; But by waiting to observe calls to COLD-INTERN that occur during the
3941 ;; loading of the cross-compiler's outputs, it is possible to rid the
3942 ;; target of accidental leftover symbols, not that it wouldn't also be
3943 ;; a good idea to clean up package-data-list once in a while.
3944 (dolist (exported-name
3945 (sb-cold:read-from-file "common-lisp-exports.lisp-expr"))
3946 (cold-intern (intern exported-name *cl-package*) :access :external))
3948 ;; Create SB!KERNEL::*TYPE-CLASSES* as an array of NIL
3949 (cold-set (cold-intern 'sb!kernel::*type-classes*)
3950 (vector-in-core (make-list (length sb!kernel::*type-classes*))))
3952 ;; Cold load.
3953 (dolist (file-name object-file-names)
3954 (cold-load file-name))
3956 (when *known-structure-classoids*
3957 (let ((dd-layout (find-layout 'defstruct-description)))
3958 (dolist (defstruct-args *known-structure-classoids*)
3959 (let* ((dd (first defstruct-args))
3960 (name (warm-symbol (read-slot dd dd-layout :name)))
3961 (layout (gethash name *cold-layouts*)))
3962 (aver layout)
3963 (write-slots layout *host-layout-of-layout* :info dd))))
3964 (format t "~&; SB!Loader: (~D~@{+~D~}) structs/consts/funs/methods/other~%"
3965 (length *known-structure-classoids*)
3966 (length *!cold-defconstants*)
3967 (length *!cold-defuns*)
3968 (reduce #'+ *cold-methods* :key (lambda (x) (length (cdr x))))
3969 (length *!cold-toplevels*)))
3971 (dolist (symbol '(*!cold-defconstants* *!cold-defuns* *!cold-toplevels*))
3972 (cold-set symbol (list-to-core (nreverse (symbol-value symbol))))
3973 (makunbound symbol)) ; so no further PUSHes can be done
3975 (cold-set
3976 'sb!pcl::*!trivial-methods*
3977 (list-to-core
3978 (loop for (gf-name . methods) in *cold-methods*
3979 collect
3980 (cold-cons
3981 (cold-intern gf-name)
3982 (vector-in-core
3983 (loop for (class qual lambda-list fun source-loc)
3984 ;; Methods must be sorted because we invoke
3985 ;; only the first applicable one.
3986 in (stable-sort methods #'> ; highest depthoid first
3987 :key (lambda (method)
3988 (class-depthoid (car method))))
3989 collect
3990 (cold-list (cold-intern
3991 (and (null qual) (predicate-for-specializer class)))
3993 (cold-intern class)
3994 (cold-intern qual)
3995 lambda-list source-loc)))))))
3997 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3998 (resolve-deferred-known-funs)
3999 (resolve-assembler-fixups)
4000 #!+x86 (output-load-time-code-fixups)
4001 (foreign-symbols-to-core)
4002 (finish-symbols)
4003 (/show "back from FINISH-SYMBOLS")
4004 (finalize-load-time-value-noise)
4006 ;; Tell the target Lisp how much stuff we've allocated.
4007 ;; ALLOCATE-COLD-DESCRIPTOR is a weird trick to locate a space's end,
4008 ;; and it doesn't work on immobile space.
4009 (cold-set 'sb!vm:*read-only-space-free-pointer*
4010 (allocate-cold-descriptor *read-only*
4012 sb!vm:even-fixnum-lowtag))
4013 (cold-set 'sb!vm:*static-space-free-pointer*
4014 (allocate-cold-descriptor *static*
4016 sb!vm:even-fixnum-lowtag))
4017 #!+immobile-space
4018 (progn
4019 (cold-set 'sb!vm:*immobile-fixedobj-free-pointer*
4020 (make-random-descriptor
4021 (ash (+ (gspace-word-address *immobile-fixedobj*)
4022 (gspace-free-word-index *immobile-fixedobj*))
4023 sb!vm:word-shift)))
4024 ;; The upper bound of the varyobj subspace is delimited by
4025 ;; a structure with no layout and no slots.
4026 ;; This is necessary because 'coreparse' does not have the actual
4027 ;; value of the free pointer, but the space must not contain any
4028 ;; objects that look like conses (due to the tail of 0 words).
4029 (let ((des (allocate-object *immobile-varyobj* 1 ; 1 word in total
4030 sb!vm:instance-pointer-lowtag nil)))
4031 (write-wordindexed/raw des 0 sb!vm:instance-header-widetag)
4032 (write-wordindexed/raw des sb!vm:instance-slots-offset 0))
4033 (cold-set 'sb!vm:*immobile-space-free-pointer*
4034 (make-random-descriptor
4035 (ash (+ (gspace-word-address *immobile-varyobj*)
4036 (gspace-free-word-index *immobile-varyobj*))
4037 sb!vm:word-shift))))
4039 (/show "done setting free pointers")
4041 ;; Write results to files.
4042 (when map-file-name
4043 (with-open-file (stream map-file-name :direction :output :if-exists :supersede)
4044 (write-map stream)))
4045 (let ((filename (format nil "~A/Makefile.features" c-header-dir-name)))
4046 (ensure-directories-exist filename)
4047 (with-open-file (stream filename :direction :output :if-exists :supersede)
4048 (write-makefile-features stream)))
4049 (macrolet ((out-to (name &body body) ; write boilerplate and inclusion guard
4050 `(with-open-file (stream (format nil "~A/~A.h" c-header-dir-name ,name)
4051 :direction :output :if-exists :supersede)
4052 (write-boilerplate stream)
4053 (format stream
4054 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~:*~A~%"
4055 (c-name (string-upcase ,name)))
4056 ,@body
4057 (format stream "#endif~%"))))
4058 (out-to "config" (write-config-h stream))
4059 (out-to "constants" (write-constants-h stream))
4060 #!+sb-ldb
4061 (out-to "tagnames" (write-tagnames-h stream))
4062 (let ((structs (sort (copy-list sb!vm:*primitive-objects*) #'string<
4063 :key #'sb!vm:primitive-object-name)))
4064 (dolist (obj structs)
4065 (out-to (string-downcase (sb!vm:primitive-object-name obj))
4066 (write-primitive-object obj stream)))
4067 (out-to "primitive-objects"
4068 (dolist (obj structs)
4069 (format stream "~&#include \"~A.h\"~%"
4070 (string-downcase (sb!vm:primitive-object-name obj))))))
4071 (dolist (class '(classoid hash-table layout package
4072 sb!c::compiled-debug-info sb!c::compiled-debug-fun))
4073 (out-to (string-downcase class)
4074 (write-structure-object (layout-info (find-layout class)) stream)))
4075 (out-to "static-symbols" (write-static-symbols stream))
4076 (out-to "sc-offset" (write-sc-offset-coding stream)))
4078 (when core-file-name
4079 (write-initial-core-file core-file-name)))))
4081 ;;; Invert the action of HOST-CONSTANT-TO-CORE. If STRICTP is given as NIL,
4082 ;;; then we can produce a host object even if it is not a faithful rendition.
4083 (defun host-object-from-core (descriptor &optional (strictp t))
4084 (named-let recurse ((x descriptor))
4085 (when (cold-null x)
4086 (return-from recurse nil))
4087 (when (eq (descriptor-gspace x) :load-time-value)
4088 (error "Can't warm a deferred LTV placeholder"))
4089 (when (is-fixnum-lowtag (descriptor-lowtag x))
4090 (return-from recurse (descriptor-fixnum x)))
4091 (ecase (descriptor-lowtag x)
4092 (#.sb!vm:instance-pointer-lowtag
4093 (if strictp (error "Can't invert INSTANCE type") "#<instance>"))
4094 (#.sb!vm:list-pointer-lowtag
4095 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
4096 (#.sb!vm:fun-pointer-lowtag
4097 (if strictp
4098 (error "Can't map cold-fun -> warm-fun")
4099 (let ((name (read-wordindexed x sb!vm:simple-fun-name-slot)))
4100 `(function ,(recurse name)))))
4101 (#.sb!vm:other-pointer-lowtag
4102 (let ((widetag (logand (descriptor-bits (read-memory x))
4103 sb!vm:widetag-mask)))
4104 (ecase widetag
4105 (#.sb!vm:symbol-header-widetag
4106 (if strictp
4107 (warm-symbol x)
4108 (or (gethash (descriptor-bits x) *cold-symbols*) ; first try
4109 (make-symbol
4110 (recurse (read-wordindexed x sb!vm:symbol-name-slot))))))
4111 (#.sb!vm:simple-base-string-widetag (base-string-from-core x))
4112 (#.sb!vm:simple-vector-widetag (vector-from-core x #'recurse))
4113 (#.sb!vm:bignum-widetag (bignum-from-core x))))))))