Stop splitting the bits of SB!FASL::DESCRIPTOR into two pieces.
[sbcl.git] / src / compiler / generic / genesis.lisp
blobc43636bed5fca9b7fba36d97295a1fe97de3f63b
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. In particular,
19 ;;;; structure slot accessors are not set up. Slot accessors are
20 ;;;; available at cold init time because they're usually compiled
21 ;;;; inline. They're not available as out-of-line functions until the
22 ;;;; toplevel forms installing them have run.)
24 ;;;; This software is part of the SBCL system. See the README file for
25 ;;;; more information.
26 ;;;;
27 ;;;; This software is derived from the CMU CL system, which was
28 ;;;; written at Carnegie Mellon University and released into the
29 ;;;; public domain. The software is in the public domain and is
30 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
31 ;;;; files for more information.
33 (in-package "SB!FASL")
35 ;;; a magic number used to identify our core files
36 (defconstant core-magic
37 (logior (ash (sb!xc:char-code #\S) 24)
38 (ash (sb!xc:char-code #\B) 16)
39 (ash (sb!xc:char-code #\C) 8)
40 (sb!xc:char-code #\L)))
42 (defun round-up (number size)
43 "Round NUMBER up to be an integral multiple of SIZE."
44 (* size (ceiling number size)))
46 ;;;; implementing the concept of "vector" in (almost) portable
47 ;;;; Common Lisp
48 ;;;;
49 ;;;; "If you only need to do such simple things, it doesn't really
50 ;;;; matter which language you use." -- _ANSI Common Lisp_, p. 1, Paul
51 ;;;; Graham (evidently not considering the abstraction "vector" to be
52 ;;;; such a simple thing:-)
54 (eval-when (:compile-toplevel :load-toplevel :execute)
55 (defconstant +smallvec-length+
56 (expt 2 16)))
58 ;;; an element of a BIGVEC -- a vector small enough that we have
59 ;;; a good chance of it being portable to other Common Lisps
60 (deftype smallvec ()
61 `(simple-array (unsigned-byte 8) (,+smallvec-length+)))
63 (defun make-smallvec ()
64 (make-array +smallvec-length+ :element-type '(unsigned-byte 8)
65 :initial-element 0))
67 ;;; a big vector, implemented as a vector of SMALLVECs
68 ;;;
69 ;;; KLUDGE: This implementation seems portable enough for our
70 ;;; purposes, since realistically every modern implementation is
71 ;;; likely to support vectors of at least 2^16 elements. But if you're
72 ;;; masochistic enough to read this far into the contortions imposed
73 ;;; on us by ANSI and the Lisp community, for daring to use the
74 ;;; abstraction of a large linearly addressable memory space, which is
75 ;;; after all only directly supported by the underlying hardware of at
76 ;;; least 99% of the general-purpose computers in use today, then you
77 ;;; may be titillated to hear that in fact this code isn't really
78 ;;; portable, because as of sbcl-0.7.4 we need somewhat more than
79 ;;; 16Mbytes to represent a core, and ANSI only guarantees that
80 ;;; ARRAY-DIMENSION-LIMIT is not less than 1024. -- WHN 2002-06-13
81 (defstruct bigvec
82 (outer-vector (vector (make-smallvec)) :type (vector smallvec)))
84 ;;; analogous to SVREF, but into a BIGVEC
85 (defun bvref (bigvec index)
86 (multiple-value-bind (outer-index inner-index)
87 (floor index +smallvec-length+)
88 (aref (the smallvec
89 (svref (bigvec-outer-vector bigvec) outer-index))
90 inner-index)))
91 (defun (setf bvref) (new-value bigvec index)
92 (multiple-value-bind (outer-index inner-index)
93 (floor index +smallvec-length+)
94 (setf (aref (the smallvec
95 (svref (bigvec-outer-vector bigvec) outer-index))
96 inner-index)
97 new-value)))
99 ;;; analogous to LENGTH, but for a BIGVEC
101 ;;; the length of BIGVEC, measured in the number of BVREFable bytes it
102 ;;; can hold
103 (defun bvlength (bigvec)
104 (* (length (bigvec-outer-vector bigvec))
105 +smallvec-length+))
107 ;;; analogous to WRITE-SEQUENCE, but for a BIGVEC
108 (defun write-bigvec-as-sequence (bigvec stream &key (start 0) end pad-with-zeros)
109 (let* ((bvlength (bvlength bigvec))
110 (data-length (min (or end bvlength) bvlength)))
111 (loop for i of-type index from start below data-length do
112 (write-byte (bvref bigvec i)
113 stream))
114 (when (and pad-with-zeros (< bvlength data-length))
115 (loop repeat (- data-length bvlength) do (write-byte 0 stream)))))
117 ;;; analogous to READ-SEQUENCE-OR-DIE, but for a BIGVEC
118 (defun read-bigvec-as-sequence-or-die (bigvec stream &key (start 0) end)
119 (loop for i of-type index from start below (or end (bvlength bigvec)) do
120 (setf (bvref bigvec i)
121 (read-byte stream))))
123 ;;; Grow BIGVEC (exponentially, so that large increases in size have
124 ;;; asymptotic logarithmic cost per byte).
125 (defun expand-bigvec (bigvec)
126 (let* ((old-outer-vector (bigvec-outer-vector bigvec))
127 (length-old-outer-vector (length old-outer-vector))
128 (new-outer-vector (make-array (* 2 length-old-outer-vector))))
129 (dotimes (i length-old-outer-vector)
130 (setf (svref new-outer-vector i)
131 (svref old-outer-vector i)))
132 (loop for i from length-old-outer-vector below (length new-outer-vector) do
133 (setf (svref new-outer-vector i)
134 (make-smallvec)))
135 (setf (bigvec-outer-vector bigvec)
136 new-outer-vector))
137 bigvec)
139 ;;;; looking up bytes and multi-byte values in a BIGVEC (considering
140 ;;;; it as an image of machine memory on the cross-compilation target)
142 ;;; BVREF-32 and friends. These are like SAP-REF-n, except that
143 ;;; instead of a SAP we use a BIGVEC.
144 (macrolet ((make-bvref-n
146 (let* ((name (intern (format nil "BVREF-~A" n)))
147 (number-octets (/ n 8))
148 (ash-list-le
149 (loop for i from 0 to (1- number-octets)
150 collect `(ash (bvref bigvec (+ byte-index ,i))
151 ,(* i 8))))
152 (ash-list-be
153 (loop for i from 0 to (1- number-octets)
154 collect `(ash (bvref bigvec
155 (+ byte-index
156 ,(- number-octets 1 i)))
157 ,(* i 8))))
158 (setf-list-le
159 (loop for i from 0 to (1- number-octets)
160 append
161 `((bvref bigvec (+ byte-index ,i))
162 (ldb (byte 8 ,(* i 8)) new-value))))
163 (setf-list-be
164 (loop for i from 0 to (1- number-octets)
165 append
166 `((bvref bigvec (+ byte-index ,i))
167 (ldb (byte 8 ,(- n 8 (* i 8))) new-value)))))
168 `(progn
169 (defun ,name (bigvec byte-index)
170 (logior ,@(ecase sb!c:*backend-byte-order*
171 (:little-endian ash-list-le)
172 (:big-endian ash-list-be))))
173 (defun (setf ,name) (new-value bigvec byte-index)
174 (setf ,@(ecase sb!c:*backend-byte-order*
175 (:little-endian setf-list-le)
176 (:big-endian setf-list-be))))))))
177 (make-bvref-n 8)
178 (make-bvref-n 16)
179 (make-bvref-n 32)
180 (make-bvref-n 64))
182 ;; lispobj-sized word, whatever that may be
183 ;; hopefully nobody ever wants a 128-bit SBCL...
184 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
185 (progn
186 (defun bvref-word (bytes index)
187 (bvref-64 bytes index))
188 (defun (setf bvref-word) (new-val bytes index)
189 (setf (bvref-64 bytes index) new-val)))
191 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
192 (progn
193 (defun bvref-word (bytes index)
194 (bvref-32 bytes index))
195 (defun (setf bvref-word) (new-val bytes index)
196 (setf (bvref-32 bytes index) new-val)))
199 ;;;; representation of spaces in the core
201 ;;; If there is more than one dynamic space in memory (i.e., if a
202 ;;; copying GC is in use), then only the active dynamic space gets
203 ;;; dumped to core.
204 (defvar *dynamic*)
205 (defconstant dynamic-core-space-id 1)
207 (defvar *static*)
208 (defconstant static-core-space-id 2)
210 (defvar *read-only*)
211 (defconstant read-only-core-space-id 3)
213 (defconstant max-core-space-id 3)
214 (defconstant deflated-core-space-id-flag 4)
216 ;; This is somewhat arbitrary as there is no concept of the the
217 ;; number of bits in the "low" part of a descriptor any more.
218 (defconstant target-space-alignment (ash 1 16)
219 "the alignment requirement for spaces in the target.")
221 ;;; a GENESIS-time representation of a memory space (e.g. read-only
222 ;;; space, dynamic space, or static space)
223 (defstruct (gspace (:constructor %make-gspace)
224 (:copier nil))
225 ;; name and identifier for this GSPACE
226 (name (missing-arg) :type symbol :read-only t)
227 (identifier (missing-arg) :type fixnum :read-only t)
228 ;; the word address where the data will be loaded
229 (word-address (missing-arg) :type unsigned-byte :read-only t)
230 ;; the data themselves. (Note that in CMU CL this was a pair of
231 ;; fields SAP and WORDS-ALLOCATED, but that wasn't very portable.)
232 ;; (And then in SBCL this was a VECTOR, but turned out to be
233 ;; unportable too, since ANSI doesn't think that arrays longer than
234 ;; 1024 (!) should needed by portable CL code...)
235 (bytes (make-bigvec) :read-only t)
236 ;; the index of the next unwritten word (i.e. chunk of
237 ;; SB!VM:N-WORD-BYTES bytes) in BYTES, or equivalently the number of
238 ;; words actually written in BYTES. In order to convert to an actual
239 ;; index into BYTES, thus must be multiplied by SB!VM:N-WORD-BYTES.
240 (free-word-index 0))
242 (defun gspace-byte-address (gspace)
243 (ash (gspace-word-address gspace) sb!vm:word-shift))
245 (def!method print-object ((gspace gspace) stream)
246 (print-unreadable-object (gspace stream :type t)
247 (format stream "~S" (gspace-name gspace))))
249 (defun make-gspace (name identifier byte-address)
250 (unless (zerop (rem byte-address target-space-alignment))
251 (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
252 byte-address
253 target-space-alignment))
254 (%make-gspace :name name
255 :identifier identifier
256 :word-address (ash byte-address (- sb!vm:word-shift))))
258 ;;;; representation of descriptors
260 (defun is-fixnum-lowtag (lowtag)
261 (zerop (logand lowtag sb!vm:fixnum-tag-mask)))
263 (defun is-other-immediate-lowtag (lowtag)
264 ;; The other-immediate lowtags are similar to the fixnum lowtags, in
265 ;; that they have an "effective length" that is shorter than is used
266 ;; for the pointer lowtags. Unlike the fixnum lowtags, however, the
267 ;; other-immediate lowtags are always effectively two bits wide.
268 (= (logand lowtag 3) sb!vm:other-immediate-0-lowtag))
270 (defstruct (descriptor
271 (:constructor make-descriptor (bits &optional gspace word-offset))
272 (:copier nil))
273 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
274 (gspace nil :type (or gspace (eql :load-time-value) null))
275 ;; the offset in words from the start of GSPACE, or NIL if not set yet
276 (word-offset nil :type (or sb!vm:word null))
277 (bits 0 :read-only t :type (unsigned-byte #.sb!vm:n-machine-word-bits)))
279 ;; FIXME: most uses of MAKE-RANDOM-DESCRIPTOR are abuses for writing a raw word
280 ;; into target memory as if it were a descriptor, because there is no variant
281 ;; of WRITE-WORDINDEXED taking a non-descriptor value.
282 ;; As an intermediary step, perhaps this should be renamed to MAKE-RAW-BITS.
283 (defun make-random-descriptor (bits)
284 (make-descriptor (logand bits sb!ext:most-positive-word)))
286 (def!method print-object ((des descriptor) stream)
287 (let ((lowtag (descriptor-lowtag des)))
288 (print-unreadable-object (des stream :type t)
289 (cond ((is-fixnum-lowtag lowtag)
290 (format stream "for fixnum: ~W" (descriptor-fixnum des)))
291 ((is-other-immediate-lowtag lowtag)
292 (format stream
293 "for other immediate: #X~X, type #b~8,'0B"
294 (ash (descriptor-bits des) (- sb!vm:n-widetag-bits))
295 (logand (descriptor-bits des) sb!vm:widetag-mask)))
297 (format stream
298 "for pointer: #X~X, lowtag #b~v,'0B, ~A"
299 (logandc2 (descriptor-bits des) sb!vm:lowtag-mask)
300 sb!vm:n-lowtag-bits lowtag
301 (let ((gspace (descriptor-gspace des)))
302 (if gspace
303 (gspace-name gspace)
304 "unknown"))))))))
306 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
307 ;;; free word index is boosted as necessary, and if additional memory
308 ;;; is needed, we grow the GSPACE. The descriptor returned is a
309 ;;; pointer of type LOWTAG.
310 (defun allocate-cold-descriptor (gspace length lowtag)
311 (let* ((bytes (round-up length (ash 1 sb!vm:n-lowtag-bits)))
312 (old-free-word-index (gspace-free-word-index gspace))
313 (new-free-word-index (+ old-free-word-index
314 (ash bytes (- sb!vm:word-shift)))))
315 ;; Grow GSPACE as necessary until it's big enough to handle
316 ;; NEW-FREE-WORD-INDEX.
317 (do ()
318 ((>= (bvlength (gspace-bytes gspace))
319 (* new-free-word-index sb!vm:n-word-bytes)))
320 (expand-bigvec (gspace-bytes gspace)))
321 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
322 (setf (gspace-free-word-index gspace) new-free-word-index)
323 (let ((ptr (+ (gspace-word-address gspace) old-free-word-index)))
324 (make-descriptor (logior (ash ptr sb!vm:word-shift) lowtag)
325 gspace
326 old-free-word-index))))
328 (defun descriptor-lowtag (des)
329 "the lowtag bits for DES"
330 (logand (descriptor-bits des) sb!vm:lowtag-mask))
332 (defun descriptor-fixnum (des)
333 (let ((bits (descriptor-bits des)))
334 (if (logbitp (1- sb!vm:n-word-bits) bits)
335 ;; KLUDGE: The (- SB!VM:N-WORD-BITS 2) term here looks right to
336 ;; me, and it works, but in CMU CL it was (1- SB!VM:N-WORD-BITS),
337 ;; and although that doesn't make sense for me, or work for me,
338 ;; it's hard to see how it could have been wrong, since CMU CL
339 ;; genesis worked. It would be nice to understand how this came
340 ;; to be.. -- WHN 19990901
341 (logior (ash bits (- sb!vm:n-fixnum-tag-bits))
342 (ash -1 (1+ sb!vm:n-positive-fixnum-bits)))
343 (ash bits (- sb!vm:n-fixnum-tag-bits)))))
345 (defun descriptor-word-sized-integer (des)
346 ;; Extract an (unsigned-byte 32), from either its fixnum or bignum
347 ;; representation.
348 (let ((lowtag (descriptor-lowtag des)))
349 (if (is-fixnum-lowtag lowtag)
350 (make-random-descriptor (descriptor-fixnum des))
351 (read-wordindexed des 1))))
353 ;;; common idioms
354 (defun descriptor-bytes (des)
355 (gspace-bytes (descriptor-intuit-gspace des)))
356 (defun descriptor-byte-offset (des)
357 (ash (descriptor-word-offset des) sb!vm:word-shift))
359 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
360 ;;; figure out a GSPACE which corresponds to DES, set it into
361 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
362 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
363 (declaim (ftype (function (descriptor) gspace) descriptor-intuit-gspace))
364 (defun descriptor-intuit-gspace (des)
365 (or (descriptor-gspace des)
367 ;; gspace wasn't set, now we have to search for it.
368 (let* ((lowtag (descriptor-lowtag des))
369 (abs-word-addr (ash (- (descriptor-bits des) lowtag)
370 (- sb!vm:word-shift))))
372 ;; Non-pointer objects don't have a gspace.
373 (unless (or (eql lowtag sb!vm:fun-pointer-lowtag)
374 (eql lowtag sb!vm:instance-pointer-lowtag)
375 (eql lowtag sb!vm:list-pointer-lowtag)
376 (eql lowtag sb!vm:other-pointer-lowtag))
377 (error "don't even know how to look for a GSPACE for ~S" des))
379 (dolist (gspace (list *dynamic* *static* *read-only*)
380 (error "couldn't find a GSPACE for ~S" des))
381 ;; Bounds-check the descriptor against the allocated area
382 ;; within each gspace.
383 (when (and (<= (gspace-word-address gspace)
384 abs-word-addr
385 (+ (gspace-word-address gspace)
386 (gspace-free-word-index gspace))))
387 ;; Update the descriptor with the correct gspace and the
388 ;; offset within the gspace and return the gspace.
389 (setf (descriptor-word-offset des)
390 (- abs-word-addr (gspace-word-address gspace)))
391 (return (setf (descriptor-gspace des) gspace)))))))
393 (defun %fixnum-descriptor-if-possible (num)
394 (and (typep num '(signed-byte #.sb!vm:n-fixnum-bits))
395 (make-random-descriptor (ash num sb!vm:n-fixnum-tag-bits))))
397 (defun make-fixnum-descriptor (num)
398 (or (%fixnum-descriptor-if-possible num)
399 (error "~W is too big for a fixnum." num)))
401 (defun make-other-immediate-descriptor (data type)
402 (make-descriptor (logior (ash data sb!vm:n-widetag-bits) type)))
404 (defun make-character-descriptor (data)
405 (make-other-immediate-descriptor data sb!vm:character-widetag))
407 (defun descriptor-beyond (des offset lowtag)
408 ;; OFFSET is in bytes and relative to the descriptor as a native pointer,
409 ;; not the tagged address. Then we slap on a new lowtag.
410 (make-descriptor
411 (logior (+ offset (logandc2 (descriptor-bits des) sb!vm:lowtag-mask))
412 lowtag)))
414 ;;;; miscellaneous variables and other noise
416 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
417 ;;; undefined foreign symbols are to be treated as an error.
418 ;;; (In the first pass of GENESIS, needed to create a header file before
419 ;;; the C runtime can be built, various foreign symbols will necessarily
420 ;;; be undefined, but we don't need actual values for them anyway, and
421 ;;; we can just use 0 or some other placeholder. In the second pass of
422 ;;; GENESIS, all foreign symbols should be defined, so any undefined
423 ;;; foreign symbol is a problem.)
425 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
426 ;;; never tries to look up foreign symbols in the first place unless
427 ;;; it's actually creating a core file (as in the second pass) instead
428 ;;; of using this hack to allow it to go through the motions without
429 ;;; causing an error. -- WHN 20000825
430 (defvar *foreign-symbol-placeholder-value*)
432 ;;; a handle on the trap object
433 (defvar *unbound-marker*)
434 ;; was: (make-other-immediate-descriptor 0 sb!vm:unbound-marker-widetag)
436 ;;; a handle on the NIL object
437 (defvar *nil-descriptor*)
439 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
440 ;;; when the target Lisp starts up
442 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
443 ;;; loadtime value, represented by (CONS KEYWORD ..). The FILENAME
444 ;;; tells which fasl file each list element came from, for debugging
445 ;;; purposes.
446 (defvar *current-reversed-cold-toplevels*)
448 ;;; the head of a list of DEBUG-SOURCEs which need to be patched when
449 ;;; the cold core starts up
450 (defvar *current-debug-sources*)
452 ;;; foreign symbol references
453 (defparameter *cold-foreign-undefined-symbols* nil)
455 ;;; the name of the object file currently being cold loaded (as a string, not a
456 ;;; pathname), or NIL if we're not currently cold loading any object file
457 (defvar *cold-load-filename* nil)
458 (declaim (type (or string null) *cold-load-filename*))
460 ;;;; miscellaneous stuff to read and write the core memory
462 ;;; FIXME: should be DEFINE-MODIFY-MACRO
463 (defmacro cold-push (thing list)
464 "Push THING onto the given cold-load LIST."
465 `(setq ,list (cold-cons ,thing ,list)))
467 (declaim (ftype (function (descriptor sb!vm:word) descriptor) read-wordindexed))
468 (macrolet ((read-bits ()
469 `(bvref-word (descriptor-bytes address)
470 (ash (+ index (descriptor-word-offset address))
471 sb!vm:word-shift))))
472 (defun read-bits-wordindexed (address index)
473 (read-bits))
474 (defun read-wordindexed (address index)
475 "Return the value which is displaced by INDEX words from ADDRESS."
476 (make-random-descriptor (read-bits))))
478 (declaim (ftype (function (descriptor) descriptor) read-memory))
479 (defun read-memory (address)
480 "Return the value at ADDRESS."
481 (read-wordindexed address 0))
483 ;;; (Note: In CMU CL, this function expected a SAP-typed ADDRESS
484 ;;; value, instead of the object-and-offset we use here.)
485 (declaim (ftype (function (descriptor sb!vm:word descriptor) (values))
486 note-load-time-value-reference))
487 (defun note-load-time-value-reference (address offset marker)
488 (cold-push (cold-cons
489 (cold-intern :load-time-value-fixup)
490 (cold-cons address
491 (cold-cons (number-to-core offset)
492 (cold-cons
493 (number-to-core (descriptor-word-offset marker))
494 *nil-descriptor*))))
495 *current-reversed-cold-toplevels*)
496 (values))
498 (declaim (ftype (function (descriptor sb!vm:word (or symbol descriptor))) write-wordindexed))
499 (defun write-wordindexed (address index value)
500 "Write VALUE displaced INDEX words from ADDRESS."
501 ;; If we're passed a symbol as a value then it needs to be interned.
502 (let ((value (cond ((symbolp value) (cold-intern value))
503 (t value))))
504 (if (eql (descriptor-gspace value) :load-time-value)
505 (note-load-time-value-reference address
506 (- (ash index sb!vm:word-shift)
507 (logand (descriptor-bits address)
508 sb!vm:lowtag-mask))
509 value)
510 (let* ((bytes (descriptor-bytes address))
511 (byte-index (ash (+ index (descriptor-word-offset address))
512 sb!vm:word-shift)))
513 (setf (bvref-word bytes byte-index) (descriptor-bits value))))))
515 (declaim (ftype (function (descriptor (or symbol descriptor))) write-memory))
516 (defun write-memory (address value)
517 "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
518 (write-wordindexed address 0 value))
520 ;;;; allocating images of primitive objects in the cold core
522 (defun write-header-word (des header-data widetag)
523 (write-memory des (make-other-immediate-descriptor header-data widetag)))
525 ;;; There are three kinds of blocks of memory in the type system:
526 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
527 ;;; header as all slots are descriptors.
528 ;;; * Unboxed objects (bignums): There is a single header word that contains
529 ;;; the length.
530 ;;; * Vector objects: There is a header word with the type, then a word for
531 ;;; the length, then the data.
532 (defun allocate-object (gspace length lowtag)
533 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
534 pointing to them."
535 (allocate-cold-descriptor gspace (ash length sb!vm:word-shift) lowtag))
536 (defun allocate-header+object (gspace length widetag)
537 "Allocate LENGTH words plus a header word in GSPACE and
538 return an ``other-pointer'' descriptor to them. Initialize the header word
539 with the resultant length and WIDETAG."
540 (let ((des (allocate-cold-descriptor gspace
541 (ash (1+ length) sb!vm:word-shift)
542 sb!vm:other-pointer-lowtag)))
543 (write-header-word des length widetag)
544 des))
545 (defun allocate-vector-object (gspace element-bits length widetag)
546 "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
547 GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
548 header word with WIDETAG and the length slot with LENGTH."
549 ;; ALLOCATE-COLD-DESCRIPTOR will take any rational number of bytes
550 ;; and round up to a double-word. This doesn't need to use CEILING.
551 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
552 (des (allocate-cold-descriptor gspace
553 (+ bytes (* 2 sb!vm:n-word-bytes))
554 sb!vm:other-pointer-lowtag)))
555 (write-header-word des 0 widetag)
556 (write-wordindexed des
557 sb!vm:vector-length-slot
558 (make-fixnum-descriptor length))
559 des))
561 (defun cold-layout-length (layout)
562 (descriptor-fixnum (read-slot layout (find-layout 'layout) :length)))
564 ;; Make a structure and set the header word and layout.
565 ;; LAYOUT-LENGTH is as returned by the like-named function.
566 (defun allocate-struct
567 (gspace layout &optional (layout-length (cold-layout-length layout)))
568 ;; The math in here is best illustrated by two examples:
569 ;; even: size 4 => request to allocate 5 => rounds up to 6, logior => 5
570 ;; odd : size 5 => request to allocate 6 => no rounding up, logior => 5
571 ;; In each case, the length of the memory block is even.
572 ;; ALLOCATE-OBJECT performs the rounding. It must be supplied
573 ;; the number of words minimally needed, counting the header itself.
574 ;; The number written into the header (%INSTANCE-LENGTH) is always odd.
575 (let ((des (allocate-object gspace (1+ layout-length)
576 sb!vm:instance-pointer-lowtag)))
577 (write-header-word des (logior layout-length 1)
578 sb!vm:instance-header-widetag)
579 (write-wordindexed des sb!vm:instance-slots-offset layout)
580 des))
582 ;;;; copying simple objects into the cold core
584 (defun base-string-to-core (string &optional (gspace *dynamic*))
585 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
586 core and return a descriptor to it."
587 ;; (Remember that the system convention for storage of strings leaves an
588 ;; extra null byte at the end to aid in call-out to C.)
589 (let* ((length (length string))
590 (des (allocate-vector-object gspace
591 sb!vm:n-byte-bits
592 (1+ length)
593 sb!vm:simple-base-string-widetag))
594 (bytes (gspace-bytes gspace))
595 (offset (+ (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
596 (descriptor-byte-offset des))))
597 (write-wordindexed des
598 sb!vm:vector-length-slot
599 (make-fixnum-descriptor length))
600 (dotimes (i length)
601 (setf (bvref bytes (+ offset i))
602 (sb!xc:char-code (aref string i))))
603 (setf (bvref bytes (+ offset length))
604 0) ; null string-termination character for C
605 des))
607 (defun base-string-from-core (descriptor)
608 (let* ((len (descriptor-fixnum
609 (read-wordindexed descriptor sb!vm:vector-length-slot)))
610 (str (make-string len))
611 (bytes (descriptor-bytes descriptor)))
612 (dotimes (i len str)
613 (setf (aref str i)
614 (code-char (bvref bytes
615 (+ (descriptor-byte-offset descriptor)
616 (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
617 i)))))))
619 (defun bignum-to-core (n)
620 "Copy a bignum to the cold core."
621 (let* ((words (ceiling (1+ (integer-length n)) sb!vm:n-word-bits))
622 (handle
623 (allocate-header+object *dynamic* words sb!vm:bignum-widetag)))
624 (declare (fixnum words))
625 (do ((index 1 (1+ index))
626 (remainder n (ash remainder (- sb!vm:n-word-bits))))
627 ((> index words)
628 (unless (zerop (integer-length remainder))
629 ;; FIXME: Shouldn't this be a fatal error?
630 (warn "~W words of ~W were written, but ~W bits were left over."
631 words n remainder)))
632 ;; FIXME: this is disgusting. there should be WRITE-BITS-WORDINDEXED.
633 (write-wordindexed handle index
634 (make-random-descriptor
635 (ldb (byte sb!vm:n-word-bits 0) remainder))))
636 handle))
638 (defun bignum-from-core (descriptor)
639 (let ((n-words (ash (descriptor-bits (read-memory descriptor))
640 (- sb!vm:n-widetag-bits)))
641 (val 0))
642 (dotimes (i n-words val)
643 (let ((bits (read-bits-wordindexed descriptor
644 (+ i sb!vm:bignum-digits-offset))))
645 ;; sign-extend the highest word
646 (when (and (= i (1- n-words)) (logbitp (1- sb!vm:n-word-bits) bits))
647 (setq bits (dpb bits (byte sb!vm:n-word-bits 0) -1)))
648 (setq val (logior (ash bits (* i sb!vm:n-word-bits)) val))))))
650 (defun number-pair-to-core (first second type)
651 "Makes a number pair of TYPE (ratio or complex) and fills it in."
652 (let ((des (allocate-header+object *dynamic* 2 type)))
653 (write-wordindexed des 1 first)
654 (write-wordindexed des 2 second)
655 des))
657 (defun write-double-float-bits (address index x)
658 (let ((hi (double-float-high-bits x))
659 (lo (double-float-low-bits x)))
660 (ecase sb!vm::n-word-bits
662 ;; As noted in BIGNUM-TO-CORE, this idiom is unclear - the things
663 ;; aren't descriptors. Same for COMPLEX-foo-TO-CORE
664 (let ((high-bits (make-random-descriptor hi))
665 (low-bits (make-random-descriptor lo)))
666 (ecase sb!c:*backend-byte-order*
667 (:little-endian
668 (write-wordindexed address index low-bits)
669 (write-wordindexed address (1+ index) high-bits))
670 (:big-endian
671 (write-wordindexed address index high-bits)
672 (write-wordindexed address (1+ index) low-bits)))))
674 (let ((bits (make-random-descriptor
675 (ecase sb!c:*backend-byte-order*
676 (:little-endian (logior lo (ash hi 32)))
677 ;; Just guessing.
678 #+nil (:big-endian (logior (logand hi #xffffffff)
679 (ash lo 32)))))))
680 (write-wordindexed address index bits))))
681 address))
683 (defun float-to-core (x)
684 (etypecase x
685 (single-float
686 ;; 64-bit platforms have immediate single-floats.
687 #!+#.(cl:if (cl:= sb!vm:n-word-bits 64) '(and) '(or))
688 (make-random-descriptor (logior (ash (single-float-bits x) 32)
689 sb!vm::single-float-widetag))
690 #!-#.(cl:if (cl:= sb!vm:n-word-bits 64) '(and) '(or))
691 (let ((des (allocate-header+object *dynamic*
692 (1- sb!vm:single-float-size)
693 sb!vm:single-float-widetag)))
694 (write-wordindexed des
695 sb!vm:single-float-value-slot
696 (make-random-descriptor (single-float-bits x)))
697 des))
698 (double-float
699 (let ((des (allocate-header+object *dynamic*
700 (1- sb!vm:double-float-size)
701 sb!vm:double-float-widetag)))
702 (write-double-float-bits des sb!vm:double-float-value-slot x)))))
704 (defun complex-single-float-to-core (num)
705 (declare (type (complex single-float) num))
706 (let ((des (allocate-header+object *dynamic*
707 (1- sb!vm:complex-single-float-size)
708 sb!vm:complex-single-float-widetag)))
709 #!-x86-64
710 (progn
711 (write-wordindexed des sb!vm:complex-single-float-real-slot
712 (make-random-descriptor (single-float-bits (realpart num))))
713 (write-wordindexed des sb!vm:complex-single-float-imag-slot
714 (make-random-descriptor (single-float-bits (imagpart num)))))
715 #!+x86-64
716 (write-wordindexed des sb!vm:complex-single-float-data-slot
717 (make-random-descriptor
718 (logior (ldb (byte 32 0) (single-float-bits (realpart num)))
719 (ash (single-float-bits (imagpart num)) 32))))
720 des))
722 (defun complex-double-float-to-core (num)
723 (declare (type (complex double-float) num))
724 (let ((des (allocate-header+object *dynamic*
725 (1- sb!vm:complex-double-float-size)
726 sb!vm:complex-double-float-widetag)))
727 (write-double-float-bits des sb!vm:complex-double-float-real-slot
728 (realpart num))
729 (write-double-float-bits des sb!vm:complex-double-float-imag-slot
730 (imagpart num))))
732 ;;; Copy the given number to the core.
733 (defun number-to-core (number)
734 (typecase number
735 (integer (or (%fixnum-descriptor-if-possible number)
736 (bignum-to-core number)))
737 (ratio (number-pair-to-core (number-to-core (numerator number))
738 (number-to-core (denominator number))
739 sb!vm:ratio-widetag))
740 ((complex single-float) (complex-single-float-to-core number))
741 ((complex double-float) (complex-double-float-to-core number))
742 #!+long-float
743 ((complex long-float)
744 (error "~S isn't a cold-loadable number at all!" number))
745 (complex (number-pair-to-core (number-to-core (realpart number))
746 (number-to-core (imagpart number))
747 sb!vm:complex-widetag))
748 (float (float-to-core number))
749 (t (error "~S isn't a cold-loadable number at all!" number))))
751 (declaim (ftype (function (sb!vm:word) descriptor) sap-int-to-core))
752 (defun sap-int-to-core (sap-int)
753 (let ((des (allocate-header+object *dynamic* (1- sb!vm:sap-size)
754 sb!vm:sap-widetag)))
755 (write-wordindexed des
756 sb!vm:sap-pointer-slot
757 (make-random-descriptor sap-int))
758 des))
760 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
761 (defun cold-cons (car cdr &optional (gspace *dynamic*))
762 (let ((dest (allocate-object gspace 2 sb!vm:list-pointer-lowtag)))
763 (write-memory dest car)
764 (write-wordindexed dest 1 cdr)
765 dest))
767 ;;; Make a simple-vector on the target that holds the specified
768 ;;; OBJECTS, and return its descriptor.
769 ;;; This is really "vectorify-list-into-core" but that's too wordy,
770 ;;; so historically it was "vector-in-core" which is a fine name.
771 (defun vector-in-core (objects &optional (gspace *dynamic*))
772 (let* ((size (length objects))
773 (result (allocate-vector-object gspace sb!vm:n-word-bits size
774 sb!vm:simple-vector-widetag)))
775 (dotimes (index size)
776 (write-wordindexed result (+ index sb!vm:vector-data-offset)
777 (pop objects)))
778 result))
780 (defun vector-from-core (descriptor transform)
781 (let* ((len (descriptor-fixnum
782 (read-wordindexed descriptor sb!vm:vector-length-slot)))
783 (vector (make-array len)))
784 (dotimes (i len vector)
785 (setf (aref vector i)
786 (funcall transform
787 (read-wordindexed descriptor
788 (+ sb!vm:vector-data-offset i)))))))
790 ;;;; symbol magic
792 ;; Simulate *FREE-TLS-INDEX*. This is a count, not a displacement.
793 ;; In C, sizeof counts 1 word for the variable-length interrupt_contexts[]
794 ;; but primitive-object-size counts 0, so add 1, though in fact the C code
795 ;; implies that it might have overcounted by 1. We could make this agnostic
796 ;; of MAX-INTERRUPTS by moving the thread base register up by TLS-SIZE words,
797 ;; using negative offsets for all dynamically assigned indices.
798 (defvar *genesis-tls-counter*
799 (+ 1 sb!vm::max-interrupts
800 (sb!vm:primitive-object-size
801 (find 'sb!vm::thread sb!vm:*primitive-objects*
802 :key #'sb!vm:primitive-object-name))))
804 #!+sb-thread
805 (progn
806 ;; Assign SYMBOL the tls-index INDEX. SYMBOL must be a descriptor.
807 ;; This is a backend support routine, but the style within this file
808 ;; is to conditionalize by the target features.
809 (defun cold-assign-tls-index (symbol index)
810 #!+x86-64
811 (let ((header-word
812 (logior (ash index 32)
813 (descriptor-bits (read-memory symbol)))))
814 (write-wordindexed symbol 0 (make-random-descriptor header-word)))
815 #!-x86-64
816 (write-wordindexed symbol sb!vm:symbol-tls-index-slot
817 (make-random-descriptor index)))
819 ;; Return SYMBOL's tls-index,
820 ;; choosing a new index if it doesn't have one yet.
821 (defun ensure-symbol-tls-index (symbol)
822 (let* ((cold-sym (cold-intern symbol))
823 (tls-index
824 #!+x86-64
825 (ldb (byte 32 32) (descriptor-bits (read-memory cold-sym)))
826 #!-x86-64
827 (descriptor-bits
828 (read-wordindexed cold-sym sb!vm:symbol-tls-index-slot))))
829 (unless (plusp tls-index)
830 (let ((next (prog1 *genesis-tls-counter* (incf *genesis-tls-counter*))))
831 (setq tls-index (ash next sb!vm:word-shift))
832 (cold-assign-tls-index cold-sym tls-index)))
833 tls-index)))
835 ;; A table of special variable names which get known TLS indices.
836 ;; Some of them are mapped onto 'struct thread' and have pre-determined offsets.
837 ;; Others are static symbols used with bind_variable() in the C runtime,
838 ;; and might not, in the absence of this table, get an index assigned by genesis
839 ;; depending on whether the cross-compiler used the BIND vop on them.
840 ;; Indices for those static symbols can be chosen arbitrarily, which is to say
841 ;; the value doesn't matter but must update the tls-counter correctly.
842 ;; All symbols other than the ones in this table get the indices assigned
843 ;; by the fasloader on demand.
844 #!+sb-thread
845 (defvar *known-tls-symbols*
846 ;; FIXME: no mechanism exists to determine which static symbols C code will
847 ;; dynamically bind. TLS is a finite resource, and wasting indices for all
848 ;; static symbols isn't the best idea. This list was hand-made with 'grep'.
849 '(sb!vm:*alloc-signal*
850 sb!sys:*allow-with-interrupts*
851 sb!vm:*current-catch-block*
852 sb!vm::*current-unwind-protect-block*
853 sb!kernel:*free-interrupt-context-index*
854 sb!kernel:*gc-inhibit*
855 sb!kernel:*gc-pending*
856 sb!impl::*gc-safe*
857 sb!impl::*in-safepoint*
858 sb!sys:*interrupt-pending*
859 sb!sys:*interrupts-enabled*
860 sb!vm::*pinned-objects*
861 sb!kernel:*restart-clusters*
862 sb!kernel:*stop-for-gc-pending*
863 #!+sb-thruption
864 sb!sys:*thruption-pending*))
866 ;;; Allocate (and initialize) a symbol.
867 (defun allocate-symbol (name &key (gspace *dynamic*))
868 (declare (simple-string name))
869 (let ((symbol (allocate-header+object gspace (1- sb!vm:symbol-size)
870 sb!vm:symbol-header-widetag)))
871 (write-wordindexed symbol sb!vm:symbol-value-slot *unbound-marker*)
872 (write-wordindexed symbol sb!vm:symbol-hash-slot (make-fixnum-descriptor 0))
873 (write-wordindexed symbol sb!vm:symbol-info-slot *nil-descriptor*)
874 (write-wordindexed symbol sb!vm:symbol-name-slot
875 (base-string-to-core name *dynamic*))
876 (write-wordindexed symbol sb!vm:symbol-package-slot *nil-descriptor*)
877 symbol))
879 #!+sb-thread
880 (defun assign-tls-index (symbol cold-symbol)
881 (let ((index (info :variable :wired-tls symbol)))
882 (cond ((integerp index) ; thread slot
883 (cold-assign-tls-index cold-symbol index))
884 ((memq symbol *known-tls-symbols*)
885 ;; symbols without which the C runtime could not start
886 (shiftf index *genesis-tls-counter* (1+ *genesis-tls-counter*))
887 (cold-assign-tls-index cold-symbol (ash index sb!vm:word-shift))))))
889 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
890 ;;; descriptor of a cold symbol or (in an abbreviation for the
891 ;;; most common usage pattern) an ordinary symbol, which will be
892 ;;; automatically cold-interned.
893 (declaim (ftype (function ((or symbol descriptor) descriptor)) cold-set))
894 (defun cold-set (symbol-or-symbol-des value)
895 (let ((symbol-des (etypecase symbol-or-symbol-des
896 (descriptor symbol-or-symbol-des)
897 (symbol (cold-intern symbol-or-symbol-des)))))
898 (write-wordindexed symbol-des sb!vm:symbol-value-slot value)))
900 ;;;; layouts and type system pre-initialization
902 ;;; Since we want to be able to dump structure constants and
903 ;;; predicates with reference layouts, we need to create layouts at
904 ;;; cold-load time. We use the name to intern layouts by, and dump a
905 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
906 ;;; initialization can find them. The only thing that's tricky [sic --
907 ;;; WHN 19990816] is initializing layout's layout, which must point to
908 ;;; itself.
910 ;;; a map from class names to lists of
911 ;;; `(,descriptor ,name ,length ,inherits ,depth)
912 ;;; KLUDGE: It would be more understandable and maintainable to use
913 ;;; DEFSTRUCT (:TYPE LIST) here. -- WHN 19990823
914 (defvar *cold-layouts* (make-hash-table :test 'equal))
916 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
917 ;;; mapping
918 (defvar *cold-layout-names* (make-hash-table :test 'eql))
920 ;;; FIXME: *COLD-LAYOUTS* and *COLD-LAYOUT-NAMES* should be
921 ;;; initialized by binding in GENESIS.
923 ;;; the descriptor for layout's layout (needed when making layouts)
924 (defvar *layout-layout*)
925 ;;; the descriptor for PACKAGE's layout (needed when making packages)
926 (defvar *package-layout*)
928 (defconstant target-layout-length
929 ;; LAYOUT-LENGTH counts the number of words in an instance,
930 ;; including the layout itself as 1 word
931 (layout-length (find-layout 'layout)))
933 ;;; Return a list of names created from the cold layout INHERITS data
934 ;;; in X.
935 (defun listify-cold-inherits (x)
936 (let ((len (descriptor-fixnum (read-wordindexed x
937 sb!vm:vector-length-slot))))
938 (collect ((res))
939 (dotimes (index len)
940 (let* ((des (read-wordindexed x (+ sb!vm:vector-data-offset index)))
941 (found (gethash (descriptor-bits des) *cold-layout-names*)))
942 (if found
943 (res found)
944 (error "unknown descriptor at index ~S (bits = ~8,'0X)"
945 index
946 (descriptor-bits des)))))
947 (res))))
949 (macrolet ((dsd-index-from-keyword (initarg slots)
950 `(let ((dsd (find ,initarg ,slots
951 :test (lambda (x y)
952 (eq x (keywordicate (dsd-name y)))))))
953 (aver (eq (dsd-raw-type dsd) t)) ; raw slots not implemented
954 (+ (dsd-index dsd) sb!vm:instance-slots-offset))))
956 (defun write-slots (cold-object host-layout &rest assignments)
957 (aver (evenp (length assignments)))
958 (let ((slots (dd-slots (layout-info host-layout))))
959 (loop for (initarg value) on assignments by #'cddr
960 do (write-wordindexed
961 cold-object (dsd-index-from-keyword initarg slots) value))))
963 (defun read-slot (cold-object host-layout slot-initarg) ; not "name"
964 (read-wordindexed cold-object
965 (dsd-index-from-keyword
966 slot-initarg (dd-slots (layout-info host-layout))))))
968 (defvar *simple-vector-0-descriptor*)
969 (defvar *vacuous-slot-table*)
970 (declaim (ftype (function (symbol descriptor descriptor descriptor descriptor)
971 descriptor)
972 make-cold-layout))
973 (defun make-cold-layout (name length inherits depthoid metadata)
974 (let ((result (allocate-struct *dynamic* *layout-layout*
975 target-layout-length)))
976 ;; Don't set the CLOS hash value: done in cold-init instead.
978 ;; Set other slot values.
980 ;; leave CLASSOID uninitialized for now
981 (multiple-value-call
982 #'write-slots result (find-layout 'layout)
983 :invalid *nil-descriptor*
984 :inherits inherits
985 :depthoid depthoid
986 :length length
987 :info *nil-descriptor*
988 :pure *nil-descriptor*
989 #!-interleaved-raw-slots
990 (values :n-untagged-slots metadata)
991 #!+interleaved-raw-slots
992 (values :untagged-bitmap metadata
993 ;; Nothing in cold-init needs to call EQUALP on a structure with raw slots,
994 ;; but for type-correctness this slot needs to be a simple-vector.
995 :equalp-tests (if (boundp '*simple-vector-0-descriptor*)
996 *simple-vector-0-descriptor*
997 (setq *simple-vector-0-descriptor*
998 (vector-in-core nil))))
999 :source-location *nil-descriptor*
1000 :%for-std-class-b (make-fixnum-descriptor 0)
1001 :slot-list *nil-descriptor*
1002 (if (member name '(null list symbol))
1003 ;; Assign an empty slot-table. Why this is done only for three
1004 ;; classoids is ... too complicated to explain here in a few words,
1005 ;; but revision 18c239205d9349abc017b07e7894a710835c5205 broke it.
1006 ;; Keep this in sync with MAKE-SLOT-TABLE in pcl/slots-boot.
1007 (values :slot-table (if (boundp '*vacuous-slot-table*)
1008 *vacuous-slot-table*
1009 (setq *vacuous-slot-table*
1010 (host-constant-to-core '#(1 nil)))))
1011 (values)))
1013 (setf (gethash (descriptor-bits result) *cold-layout-names*) name
1014 (gethash name *cold-layouts*) result)))
1016 ;; This is called to backpatch two small sets of objects:
1017 ;; - layouts which are made before layout-of-layout is made (4 of them)
1018 ;; - packages, which are made before layout-of-package is made (all of them)
1019 (defun patch-instance-layout (thing layout)
1020 ;; Layout pointer is in the word following the header
1021 (write-wordindexed thing sb!vm:instance-slots-offset layout))
1023 (defun initialize-layouts ()
1024 (clrhash *cold-layouts*)
1025 ;; This assertion is due to the fact that MAKE-COLD-LAYOUT does not
1026 ;; know how to set any raw slots.
1027 (aver (= 0 (layout-raw-slot-metadata (find-layout 'layout))))
1028 (setq *layout-layout* (make-fixnum-descriptor 0))
1029 (flet ((chill-layout (name &rest inherits)
1030 ;; Check that the number of specified INHERITS matches
1031 ;; the length of the layout's inherits in the cross-compiler.
1032 (let ((warm-layout (classoid-layout (find-classoid name))))
1033 (assert (eql (length (layout-inherits warm-layout))
1034 (length inherits)))
1035 (make-cold-layout
1036 name
1037 (number-to-core (layout-length warm-layout))
1038 (vector-in-core inherits)
1039 (number-to-core (layout-depthoid warm-layout))
1040 (number-to-core (layout-raw-slot-metadata warm-layout))))))
1041 (let* ((t-layout (chill-layout 't))
1042 (s-o-layout (chill-layout 'structure-object t-layout))
1043 (s!o-layout (chill-layout 'structure!object t-layout s-o-layout)))
1044 (setf *layout-layout*
1045 (chill-layout 'layout t-layout s-o-layout s!o-layout))
1046 (dolist (layout (list t-layout s-o-layout s!o-layout *layout-layout*))
1047 (patch-instance-layout layout *layout-layout*))
1048 (setf *package-layout*
1049 (chill-layout 'package ; *NOT* SB!XC:PACKAGE, or you lose
1050 t-layout s-o-layout s!o-layout)))))
1052 ;;;; interning symbols in the cold image
1054 ;;; a map from package name as a host string to
1055 ;;; (cold-package-descriptor . (external-symbols . internal-symbols))
1056 (defvar *cold-package-symbols*)
1057 (declaim (type hash-table *cold-package-symbols*))
1059 ;;; a map from descriptors to symbols, so that we can back up. The key
1060 ;;; is the address in the target core.
1061 (defvar *cold-symbols*)
1062 (declaim (type hash-table *cold-symbols*))
1064 (defun initialize-packages (package-data-list)
1065 (let ((package-layout (find-layout 'package))
1066 (target-pkg-list nil))
1067 (labels ((init-cold-package (name &optional docstring)
1068 (let ((cold-package (car (gethash name *cold-package-symbols*))))
1069 ;; patch in the layout
1070 (patch-instance-layout cold-package *package-layout*)
1071 ;; Initialize string slots
1072 (write-slots cold-package package-layout
1073 :%name (base-string-to-core name)
1074 :%nicknames (chill-nicknames name)
1075 :doc-string (if docstring
1076 (base-string-to-core docstring)
1077 *nil-descriptor*)
1078 :%use-list *nil-descriptor*)
1079 ;; the cddr of this will accumulate the 'used-by' package list
1080 (push (list name cold-package) target-pkg-list)))
1081 (chill-nicknames (pkg-name)
1082 (let ((result *nil-descriptor*))
1083 ;; Make the package nickname lists for the standard packages
1084 ;; be the minimum specified by ANSI, regardless of what value
1085 ;; the cross-compilation host happens to use.
1086 ;; For packages other than the standard packages, the nickname
1087 ;; list was specified by our package setup code, and we can just
1088 ;; propagate the current state into the target.
1089 (dolist (nickname
1090 (cond ((string= pkg-name "COMMON-LISP") '("CL"))
1091 ((string= pkg-name "COMMON-LISP-USER")
1092 '("CL-USER"))
1093 ((string= pkg-name "KEYWORD") '())
1094 (t (package-nicknames (find-package pkg-name))))
1095 result)
1096 (cold-push (base-string-to-core nickname) result))))
1097 (find-cold-package (name)
1098 (cadr (find-package-cell name)))
1099 (find-package-cell (name)
1100 (or (assoc (if (string= name "CL") "COMMON-LISP" name)
1101 target-pkg-list :test #'string=)
1102 (error "No cold package named ~S" name)))
1103 (list-to-core (list)
1104 (let ((res *nil-descriptor*))
1105 (dolist (x list res) (cold-push x res)))))
1106 ;; pass 1: make all proto-packages
1107 (dolist (pd package-data-list)
1108 (init-cold-package (sb-cold:package-data-name pd)
1109 #!+sb-doc(sb-cold::package-data-doc pd)))
1110 ;; MISMATCH needs !HAIRY-DATA-VECTOR-REFFER-INIT to have been done,
1111 ;; and FIND-PACKAGE calls MISMATCH - which it shouldn't - but until
1112 ;; that is fixed, doing this in genesis allows packages to be
1113 ;; completely sane, modulo the naming, extremely early in cold-init.
1114 (cold-set '*keyword-package* (find-cold-package "KEYWORD"))
1115 (cold-set '*cl-package* (find-cold-package "COMMON-LISP"))
1116 ;; pass 2: set the 'use' lists and collect the 'used-by' lists
1117 (dolist (pd package-data-list)
1118 (let ((this (find-cold-package (sb-cold:package-data-name pd)))
1119 (use nil))
1120 (dolist (that (sb-cold:package-data-use pd))
1121 (let ((cell (find-package-cell that)))
1122 (push (cadr cell) use)
1123 (push this (cddr cell))))
1124 (write-slots this package-layout
1125 :%use-list (list-to-core (nreverse use)))))
1126 ;; pass 3: set the 'used-by' lists
1127 (dolist (cell target-pkg-list)
1128 (write-slots (cadr cell) package-layout
1129 :%used-by-list (list-to-core (cddr cell)))))))
1131 ;;; sanity check for a symbol we're about to create on the target
1133 ;;; Make sure that the symbol has an appropriate package. In
1134 ;;; particular, catch the so-easy-to-make error of typing something
1135 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1136 ;;; need is SB!KERNEL:%BYTE-BLT.
1137 (defun package-ok-for-target-symbol-p (package)
1138 (let ((package-name (package-name package)))
1140 ;; Cold interning things in these standard packages is OK. (Cold
1141 ;; interning things in the other standard package, CL-USER, isn't
1142 ;; OK. We just use CL-USER to expose symbols whose homes are in
1143 ;; other packages. Thus, trying to cold intern a symbol whose
1144 ;; home package is CL-USER probably means that a coding error has
1145 ;; been made somewhere.)
1146 (find package-name '("COMMON-LISP" "KEYWORD") :test #'string=)
1147 ;; Cold interning something in one of our target-code packages,
1148 ;; which are ever-so-rigorously-and-elegantly distinguished by
1149 ;; this prefix on their names, is OK too.
1150 (string= package-name "SB!" :end1 3 :end2 3)
1151 ;; This one is OK too, since it ends up being COMMON-LISP on the
1152 ;; target.
1153 (string= package-name "SB-XC")
1154 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1155 ;; package in the xc host? something we can't think of
1156 ;; a valid reason to cold intern, anyway...)
1159 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1161 ;;; Most host symbols we dump onto the target are created by SBCL
1162 ;;; itself, so that as long as we avoid gratuitously
1163 ;;; cross-compilation-unfriendly hacks, it just happens that their
1164 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1165 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1166 ;;; in the COMMON-LISP package, where we don't get to create the
1167 ;;; symbols but instead have to use the ones that the xc host created.
1168 ;;; In particular, while ANSI specifies which symbols are exported
1169 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1170 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1171 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1172 ;;; symbols in the CLOS package).
1173 (defun symbol-package-for-target-symbol (symbol)
1174 ;; We want to catch weird symbols like CLISP's
1175 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1176 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1177 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1178 (multiple-value-bind (cl-symbol cl-status)
1179 (find-symbol (symbol-name symbol) *cl-package*)
1180 (if (and (eq symbol cl-symbol)
1181 (eq cl-status :external))
1182 ;; special case, to work around possible xc host weirdness
1183 ;; in COMMON-LISP package
1184 *cl-package*
1185 ;; ordinary case
1186 (let ((result (symbol-package symbol)))
1187 (unless (package-ok-for-target-symbol-p result)
1188 (bug "~A in bad package for target: ~A" symbol result))
1189 result))))
1191 (defvar *uninterned-symbol-table* (make-hash-table :test #'equal))
1192 ;; This coalesces references to uninterned symbols, which is allowed because
1193 ;; "similar-as-constant" is defined by string comparison, and since we only have
1194 ;; base-strings during Genesis, there is no concern about upgraded array type.
1195 ;; There is a subtlety of whether coalescing may occur across files
1196 ;; - the target compiler doesn't and couldn't - but here it doesn't matter.
1197 (defun get-uninterned-symbol (name)
1198 (or (gethash name *uninterned-symbol-table*)
1199 (let ((cold-symbol (allocate-symbol name)))
1200 (setf (gethash name *uninterned-symbol-table*) cold-symbol))))
1202 ;;; Dump the target representation of HOST-VALUE,
1203 ;;; the type of which is in a restrictive set.
1204 (defun host-constant-to-core (host-value)
1205 ;; rough check for no shared substructure and/or circularity.
1206 ;; of course this would be wrong if it were a string containing "#1="
1207 (when (search "#1=" (write-to-string host-value :circle t :readably t))
1208 (warn "Strange constant to core from Genesis: ~S" host-value))
1209 (labels ((target-representation (value)
1210 (etypecase value
1211 (symbol (if (symbol-package value)
1212 (cold-intern value)
1213 (get-uninterned-symbol (string value))))
1214 (number (number-to-core value))
1215 (string (base-string-to-core value))
1216 (cons (cold-cons (target-representation (car value))
1217 (target-representation (cdr value))))
1218 (simple-vector
1219 (vector-in-core (map 'list #'target-representation value))))))
1220 (target-representation host-value)))
1222 ;;; Return a handle on an interned symbol. If necessary allocate the
1223 ;;; symbol and record its home package.
1224 (defun cold-intern (symbol
1225 &key (access nil)
1226 (gspace *dynamic*)
1227 &aux (package (symbol-package-for-target-symbol symbol)))
1228 (aver (package-ok-for-target-symbol-p package))
1230 ;; Anything on the cross-compilation host which refers to the target
1231 ;; machinery through the host SB-XC package should be translated to
1232 ;; something on the target which refers to the same machinery
1233 ;; through the target COMMON-LISP package.
1234 (let ((p (find-package "SB-XC")))
1235 (when (eq package p)
1236 (setf package *cl-package*))
1237 (when (eq (symbol-package symbol) p)
1238 (setf symbol (intern (symbol-name symbol) *cl-package*))))
1240 (or (get symbol 'cold-intern-info)
1241 (let ((pkg-info (gethash (package-name package) *cold-package-symbols*))
1242 (handle (allocate-symbol (symbol-name symbol) :gspace gspace)))
1243 ;; maintain reverse map from target descriptor to host symbol
1244 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1245 (unless pkg-info
1246 (error "No target package descriptor for ~S" package))
1247 (record-accessibility
1248 (or access (nth-value 1 (find-symbol (symbol-name symbol) package)))
1249 handle pkg-info symbol package t)
1250 #!+sb-thread
1251 (assign-tls-index symbol handle)
1252 (acond ((eq package *keyword-package*)
1253 (setq access :external)
1254 (cold-set handle handle))
1255 ((assoc symbol sb-cold:*symbol-values-for-genesis*)
1256 (cold-set handle
1257 (host-constant-to-core
1258 (let ((*package* (find-package (cddr it))))
1259 (eval (cadr it)))))))
1260 (setf (get symbol 'cold-intern-info) handle))))
1262 (defun record-accessibility (accessibility symbol-descriptor target-pkg-info
1263 host-symbol host-package &optional set-home-p)
1264 (when set-home-p
1265 (write-wordindexed symbol-descriptor sb!vm:symbol-package-slot
1266 (car target-pkg-info)))
1267 (when (member host-symbol (package-shadowing-symbols host-package))
1268 ;; Fail in an obvious way if target shadowing symbols exist.
1269 ;; (This is simply not an important use-case during system bootstrap.)
1270 (error "Genesis doesn't like shadowing symbol ~S, sorry." host-symbol))
1271 (let ((access-lists (cdr target-pkg-info)))
1272 (case accessibility
1273 (:external (push symbol-descriptor (car access-lists)))
1274 (:internal (push symbol-descriptor (cdr access-lists)))
1275 (t (error "~S inaccessible in package ~S" host-symbol host-package)))))
1277 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1278 ;;; It might be nice to put NIL on a readonly page by itself to prevent unsafe
1279 ;;; code from destroying the world with (RPLACx nil 'kablooey)
1280 (defun make-nil-descriptor (target-cl-pkg-info)
1281 (let* ((des (allocate-header+object *static* sb!vm:symbol-size 0))
1282 (result (make-descriptor (+ (descriptor-bits des)
1283 (* 2 sb!vm:n-word-bytes)
1284 (- sb!vm:list-pointer-lowtag
1285 sb!vm:other-pointer-lowtag)))))
1286 (write-wordindexed des
1288 (make-other-immediate-descriptor
1290 sb!vm:symbol-header-widetag))
1291 (write-wordindexed des
1292 (+ 1 sb!vm:symbol-value-slot)
1293 result)
1294 (write-wordindexed des
1295 (+ 2 sb!vm:symbol-value-slot) ; = 1 + symbol-hash-slot
1296 result)
1297 (write-wordindexed des
1298 (+ 1 sb!vm:symbol-info-slot)
1299 (cold-cons result result)) ; NIL's info is (nil . nil)
1300 (write-wordindexed des
1301 (+ 1 sb!vm:symbol-name-slot)
1302 ;; NIL's name is in dynamic space because any extra
1303 ;; bytes allocated in static space would need to
1304 ;; be accounted for by STATIC-SYMBOL-OFFSET.
1305 (base-string-to-core "NIL" *dynamic*))
1306 ;; RECORD-ACCESSIBILITY can't assign to the package slot
1307 ;; due to NIL's base address and lowtag being nonstandard.
1308 (write-wordindexed des
1309 (+ 1 sb!vm:symbol-package-slot)
1310 (car target-cl-pkg-info))
1311 (record-accessibility :external result target-cl-pkg-info nil *cl-package*)
1312 (setf (gethash (descriptor-bits result) *cold-symbols*) nil
1313 (get nil 'cold-intern-info) result)))
1315 ;;; Since the initial symbols must be allocated before we can intern
1316 ;;; anything else, we intern those here. We also set the value of T.
1317 (defun initialize-non-nil-symbols ()
1318 "Initialize the cold load symbol-hacking data structures."
1319 ;; Intern the others.
1320 (dolist (symbol sb!vm:*static-symbols*)
1321 (let* ((des (cold-intern symbol :gspace *static*))
1322 (offset-wanted (sb!vm:static-symbol-offset symbol))
1323 (offset-found (- (descriptor-bits des)
1324 (descriptor-bits *nil-descriptor*))))
1325 (unless (= offset-wanted offset-found)
1326 ;; FIXME: should be fatal
1327 (warn "Offset from ~S to ~S is ~W, not ~W"
1328 symbol
1330 offset-found
1331 offset-wanted))))
1332 ;; Establish the value of T.
1333 (let ((t-symbol (cold-intern t :gspace *static*)))
1334 (cold-set t-symbol t-symbol))
1335 ;; Establish the value of *PSEUDO-ATOMIC-BITS* so that the
1336 ;; allocation sequences that expect it to be zero upon entrance
1337 ;; actually find it to be so.
1338 #!+(or x86-64 x86)
1339 (let ((p-a-a-symbol (cold-intern '*pseudo-atomic-bits*
1340 :gspace *static*)))
1341 (cold-set p-a-a-symbol (make-fixnum-descriptor 0))))
1343 ;;; a helper function for FINISH-SYMBOLS: Return a cold alist suitable
1344 ;;; to be stored in *!INITIAL-LAYOUTS*.
1345 (defun cold-list-all-layouts ()
1346 (let ((result *nil-descriptor*))
1347 (flet ((sorter (x y)
1348 (let ((xpn (package-name (symbol-package-for-target-symbol x)))
1349 (ypn (package-name (symbol-package-for-target-symbol y))))
1350 (cond
1351 ((string= x y) (string< xpn ypn))
1352 (t (string< x y))))))
1353 (dolist (layout (sort (%hash-table-alist *cold-layouts*) #'sorter
1354 :key #'car)
1355 result)
1356 (cold-push (cold-cons (cold-intern (car layout)) (cdr layout))
1357 result)))))
1359 ;;; Establish initial values for magic symbols.
1361 (defun finish-symbols ()
1363 ;; Everything between this preserved-for-posterity comment down to
1364 ;; the assignment of *CURRENT-CATCH-BLOCK* could be entirely deleted,
1365 ;; including the list of *C-CALLABLE-STATIC-SYMBOLS* itself,
1366 ;; if it is GC-safe for the C runtime to have its own implementation
1367 ;; of the INFO-VECTOR-FDEFN function in a multi-threaded build.
1369 ;; "I think the point of setting these functions into SYMBOL-VALUEs
1370 ;; here, instead of using SYMBOL-FUNCTION, is that in CMU CL
1371 ;; SYMBOL-FUNCTION reduces to FDEFINITION, which is a pretty
1372 ;; hairy operation (involving globaldb.lisp etc.) which we don't
1373 ;; want to invoke early in cold init. -- WHN 2001-12-05"
1375 ;; So... that's no longer true. We _do_ associate symbol -> fdefn in genesis.
1376 ;; Additionally, the INFO-VECTOR-FDEFN function is extremely simple and could
1377 ;; easily be implemented in C. However, info-vectors are inevitably
1378 ;; reallocated when new info is attached to a symbol, so the vectors can't be
1379 ;; in static space; they'd gradually become permanent garbage if they did.
1380 ;; That's the real reason for preserving the approach of storing an #<fdefn>
1381 ;; in a symbol's value cell - that location is static, the symbol-info is not.
1383 ;; FIXME: So OK, that's a reasonable reason to do something weird like
1384 ;; this, but this is still a weird thing to do, and we should change
1385 ;; the names to highlight that something weird is going on. Perhaps
1386 ;; *MAYBE-GC-FUN*, *INTERNAL-ERROR-FUN*, *HANDLE-BREAKPOINT-FUN*,
1387 ;; and *HANDLE-FUN-END-BREAKPOINT-FUN*...
1388 (dolist (symbol sb!vm::*c-callable-static-symbols*)
1389 (cold-set symbol (cold-fdefinition-object (cold-intern symbol))))
1391 (cold-set 'sb!vm::*current-catch-block* (make-fixnum-descriptor 0))
1392 (cold-set 'sb!vm::*current-unwind-protect-block* (make-fixnum-descriptor 0))
1394 (cold-set '*free-interrupt-context-index* (make-fixnum-descriptor 0))
1396 (cold-set '*!initial-layouts* (cold-list-all-layouts))
1398 #!+sb-thread
1399 (progn
1400 (cold-set 'sb!vm::*free-tls-index*
1401 (make-random-descriptor (ash *genesis-tls-counter*
1402 sb!vm:word-shift)))
1403 (cold-set 'sb!vm::*tls-index-lock* (make-fixnum-descriptor 0)))
1405 (dolist (symbol sb!impl::*cache-vector-symbols*)
1406 (cold-set symbol *nil-descriptor*))
1408 ;; Symbols for which no call to COLD-INTERN would occur - due to not being
1409 ;; referenced until warm init - must be artificially cold-interned.
1410 ;; Inasmuch as the "offending" things are compiled by ordinary target code
1411 ;; and not cold-init, I think we should use an ordinary DEFPACKAGE for
1412 ;; the added-on bits. What I've done is somewhat of a fragile kludge.
1413 (let (syms)
1414 (with-package-iterator (iter '("SB!PCL" "SB!MOP" "SB!GRAY" "SB!SEQUENCE"
1415 "SB!PROFILE" "SB!EXT" "SB!VM"
1416 "SB!C" "SB!FASL" "SB!DEBUG")
1417 :external)
1418 (loop
1419 (multiple-value-bind (foundp sym accessibility package) (iter)
1420 (declare (ignore accessibility))
1421 (cond ((not foundp) (return))
1422 ((eq (symbol-package sym) package) (push sym syms))))))
1423 (setf syms (stable-sort syms #'string<))
1424 (dolist (sym syms)
1425 (cold-intern sym)))
1427 (let ((cold-pkg-inits *nil-descriptor*)
1428 cold-package-symbols-list)
1429 (maphash (lambda (name info)
1430 (push (cons name info) cold-package-symbols-list))
1431 *cold-package-symbols*)
1432 (setf cold-package-symbols-list
1433 (sort cold-package-symbols-list #'string< :key #'car))
1434 (dolist (pkgcons cold-package-symbols-list)
1435 (destructuring-bind (pkg-name . pkg-info) pkgcons
1436 (unless (member pkg-name '("COMMON-LISP" "KEYWORD") :test 'string=)
1437 (let ((host-pkg (find-package pkg-name))
1438 (sb-xc-pkg (find-package "SB-XC"))
1439 syms)
1440 (with-package-iterator (iter host-pkg :internal :external)
1441 (loop (multiple-value-bind (foundp sym accessibility) (iter)
1442 (unless foundp (return))
1443 (unless (or (eq (symbol-package sym) host-pkg)
1444 (eq (symbol-package sym) sb-xc-pkg))
1445 (push (cons sym accessibility) syms)))))
1446 (setq syms (sort syms #'string< :key #'car))
1447 (dolist (symcons syms)
1448 (destructuring-bind (sym . accessibility) symcons
1449 (record-accessibility accessibility (cold-intern sym)
1450 pkg-info sym host-pkg)))))
1451 (cold-push (cold-cons (car pkg-info)
1452 (cold-cons (vector-in-core (cadr pkg-info))
1453 (vector-in-core (cddr pkg-info))))
1454 cold-pkg-inits)))
1455 (cold-set 'sb!impl::*!initial-symbols* cold-pkg-inits))
1457 (attach-fdefinitions-to-symbols)
1459 (cold-set '*!reversed-cold-toplevels* *current-reversed-cold-toplevels*)
1460 (cold-set '*!initial-debug-sources* *current-debug-sources*)
1462 #!+(or x86 x86-64)
1463 (progn
1464 (cold-set 'sb!vm::*fp-constant-0d0* (number-to-core 0d0))
1465 (cold-set 'sb!vm::*fp-constant-1d0* (number-to-core 1d0))
1466 (cold-set 'sb!vm::*fp-constant-0f0* (number-to-core 0f0))
1467 (cold-set 'sb!vm::*fp-constant-1f0* (number-to-core 1f0))))
1469 ;;;; functions and fdefinition objects
1471 ;;; a hash table mapping from fdefinition names to descriptors of cold
1472 ;;; objects
1474 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1475 ;;; we want to have only one entry per name, this must be an 'EQUAL
1476 ;;; hash table, not the default 'EQL.
1477 (defvar *cold-fdefn-objects*)
1479 (defvar *cold-fdefn-gspace* nil)
1481 ;;; Given a cold representation of a symbol, return a warm
1482 ;;; representation.
1483 (defun warm-symbol (des)
1484 ;; Note that COLD-INTERN is responsible for keeping the
1485 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1486 ;; uninterned symbol, the code below will fail. But as long as we
1487 ;; don't need to look up uninterned symbols during bootstrapping,
1488 ;; that's OK..
1489 (multiple-value-bind (symbol found-p)
1490 (gethash (descriptor-bits des) *cold-symbols*)
1491 (declare (type symbol symbol))
1492 (unless found-p
1493 (error "no warm symbol"))
1494 symbol))
1496 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1497 (defun cold-car (des)
1498 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1499 (read-wordindexed des sb!vm:cons-car-slot))
1500 (defun cold-cdr (des)
1501 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1502 (read-wordindexed des sb!vm:cons-cdr-slot))
1503 (defun cold-null (des)
1504 (= (descriptor-bits des)
1505 (descriptor-bits *nil-descriptor*)))
1507 ;;; Given a cold representation of a function name, return a warm
1508 ;;; representation.
1509 (declaim (ftype (function ((or symbol descriptor)) (or symbol list)) warm-fun-name))
1510 (defun warm-fun-name (des)
1511 (let ((result
1512 (if (symbolp des)
1513 ;; This parallels the logic at the start of COLD-INTERN
1514 ;; which re-homes symbols in SB-XC to COMMON-LISP.
1515 (if (eq (symbol-package des) (find-package "SB-XC"))
1516 (intern (symbol-name des) *cl-package*)
1517 des)
1518 (ecase (descriptor-lowtag des)
1519 (#.sb!vm:list-pointer-lowtag
1520 (aver (not (cold-null des))) ; function named NIL? please no..
1521 ;; Do cold (DESTRUCTURING-BIND (COLD-CAR COLD-CADR) DES ..).
1522 (let* ((car-des (cold-car des))
1523 (cdr-des (cold-cdr des))
1524 (cadr-des (cold-car cdr-des))
1525 (cddr-des (cold-cdr cdr-des)))
1526 (aver (cold-null cddr-des))
1527 (list (warm-symbol car-des)
1528 (warm-symbol cadr-des))))
1529 (#.sb!vm:other-pointer-lowtag
1530 (warm-symbol des))))))
1531 (legal-fun-name-or-type-error result)
1532 result))
1534 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw)
1535 (declare (type (or symbol descriptor) cold-name))
1536 (/noshow0 "/cold-fdefinition-object")
1537 (let ((warm-name (warm-fun-name cold-name)))
1538 (or (gethash warm-name *cold-fdefn-objects*)
1539 (let ((fdefn (allocate-header+object (or *cold-fdefn-gspace* *dynamic*)
1540 (1- sb!vm:fdefn-size)
1541 sb!vm:fdefn-widetag)))
1542 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
1543 (write-wordindexed fdefn sb!vm:fdefn-name-slot cold-name)
1544 (unless leave-fn-raw
1545 (write-wordindexed fdefn sb!vm:fdefn-fun-slot
1546 *nil-descriptor*)
1547 (write-wordindexed fdefn
1548 sb!vm:fdefn-raw-addr-slot
1549 (make-random-descriptor
1550 (cold-foreign-symbol-address "undefined_tramp"))))
1551 fdefn))))
1553 ;;; Handle the at-cold-init-time, fset-for-static-linkage operation.
1554 ;;; "static" is sort of a misnomer. It's just ordinary fdefinition linkage.
1555 (defun static-fset (cold-name defn)
1556 (declare (type (or symbol descriptor) cold-name))
1557 (let ((fdefn (cold-fdefinition-object cold-name t))
1558 (type (logand (descriptor-bits (read-memory defn)) sb!vm:widetag-mask)))
1559 (write-wordindexed fdefn sb!vm:fdefn-fun-slot defn)
1560 (write-wordindexed fdefn
1561 sb!vm:fdefn-raw-addr-slot
1562 (ecase type
1563 (#.sb!vm:simple-fun-header-widetag
1564 (/noshow0 "static-fset (simple-fun)")
1565 #!+(or sparc arm)
1566 defn
1567 #!-(or sparc arm)
1568 (make-random-descriptor
1569 (+ (logandc2 (descriptor-bits defn)
1570 sb!vm:lowtag-mask)
1571 (ash sb!vm:simple-fun-code-offset
1572 sb!vm:word-shift))))
1573 (#.sb!vm:closure-header-widetag
1574 ;; There's no way to create a closure.
1575 (bug "FSET got closure-header-widetag")
1576 (/show0 "/static-fset (closure)")
1577 (make-random-descriptor
1578 (cold-foreign-symbol-address "closure_tramp")))))
1579 fdefn))
1581 ;;; the names of things which have had COLD-FSET used on them already
1582 ;;; (used to make sure that we don't try to statically link a name to
1583 ;;; more than one definition)
1584 (defparameter *cold-fset-warm-names*
1585 (make-hash-table :test 'equal)) ; names can be conses, e.g. (SETF CAR)
1587 (defun cold-fset (name compiled-lambda docstring inline-expansion source-loc)
1588 (declare (ignore source-loc))
1589 (multiple-value-bind (cold-name warm-name)
1590 ;; (SETF f) was descriptorized when dumped, symbols were not,
1591 ;; Figure out what kind of name we're looking at.
1592 (if (symbolp name)
1593 (values (cold-intern name) name)
1594 (values name (warm-fun-name name)))
1595 (when (gethash warm-name *cold-fset-warm-names*)
1596 (error "duplicate COLD-FSET for ~S" warm-name))
1597 (setf (gethash warm-name *cold-fset-warm-names*) t)
1598 (let ((args (cold-cons cold-name
1599 (if (or docstring inline-expansion)
1600 (cold-cons docstring inline-expansion)
1601 *nil-descriptor*))))
1602 (cold-push (cold-cons 'defun args) *current-reversed-cold-toplevels*))
1603 (static-fset cold-name compiled-lambda)))
1605 (defun initialize-static-fns ()
1606 (let ((*cold-fdefn-gspace* *static*))
1607 (dolist (sym sb!vm:*static-funs*)
1608 (let* ((fdefn (cold-fdefinition-object (cold-intern sym)))
1609 (offset (- (+ (- (descriptor-bits fdefn)
1610 sb!vm:other-pointer-lowtag)
1611 (* sb!vm:fdefn-raw-addr-slot sb!vm:n-word-bytes))
1612 (descriptor-bits *nil-descriptor*)))
1613 (desired (sb!vm:static-fun-offset sym)))
1614 (unless (= offset desired)
1615 ;; FIXME: should be fatal
1616 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1617 sym nil offset desired))))))
1619 ;; Create pointer from SYMBOL and/or (SETF SYMBOL) to respective fdefinition
1621 (defun attach-fdefinitions-to-symbols ()
1622 (let ((hashtable (make-hash-table :test #'eq)))
1623 ;; Collect fdefinitions that go with one symbol, e.g. CAR and (SETF CAR),
1624 ;; using the host's code for manipulating a packed info-vector.
1625 (maphash (lambda (warm-name cold-fdefn)
1626 (sb!c::with-globaldb-name (key1 key2) warm-name
1627 :hairy (error "Hairy fdefn name in genesis: ~S" warm-name)
1628 :simple
1629 (setf (gethash key1 hashtable)
1630 (sb!c::packed-info-insert
1631 (gethash key1 hashtable sb!c::+nil-packed-infos+)
1632 key2 sb!c::+fdefn-type-num+ cold-fdefn))))
1633 *cold-fdefn-objects*)
1634 ;; Emit in the same order symbols reside in core to avoid
1635 ;; sensitivity to the iteration order of host's maphash.
1636 (loop for (warm-sym . info)
1637 in (sort (%hash-table-alist hashtable) #'<
1638 :key (lambda (x) (descriptor-bits (cold-intern (car x)))))
1639 do (write-wordindexed
1640 (cold-intern warm-sym) sb!vm:symbol-info-slot
1641 ;; Each vector will have one fixnum, possibly the symbol SETF,
1642 ;; and one or two #<fdefn> objects in it.
1643 (vector-in-core
1644 (map 'list (lambda (elt)
1645 (etypecase elt
1646 (symbol (cold-intern elt))
1647 (fixnum (make-fixnum-descriptor elt))
1648 (descriptor elt)))
1649 info))))))
1652 ;;;; fixups and related stuff
1654 ;;; an EQUAL hash table
1655 (defvar *cold-foreign-symbol-table*)
1656 (declaim (type hash-table *cold-foreign-symbol-table*))
1658 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1659 ;; the C runtime.
1660 (defun load-cold-foreign-symbol-table (filename)
1661 (/show "load-cold-foreign-symbol-table" filename)
1662 (with-open-file (file filename)
1663 (loop for line = (read-line file nil nil)
1664 while line do
1665 ;; UNIX symbol tables might have tabs in them, and tabs are
1666 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1667 ;; nice portable way to deal with them within Lisp, alas.
1668 ;; Fortunately, it's easy to use UNIX command line tools like
1669 ;; sed to remove the problem, so it's not too painful for us
1670 ;; to push responsibility for converting tabs to spaces out to
1671 ;; the caller.
1673 ;; Other non-STANDARD-CHARs are problematic for the same reason.
1674 ;; Make sure that there aren't any..
1675 (let ((ch (find-if (lambda (char)
1676 (not (typep char 'standard-char)))
1677 line)))
1678 (when ch
1679 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1681 line)))
1682 (setf line (string-trim '(#\space) line))
1683 (let ((p1 (position #\space line :from-end nil))
1684 (p2 (position #\space line :from-end t)))
1685 (if (not (and p1 p2 (< p1 p2)))
1686 ;; KLUDGE: It's too messy to try to understand all
1687 ;; possible output from nm, so we just punt the lines we
1688 ;; don't recognize. We realize that there's some chance
1689 ;; that might get us in trouble someday, so we warn
1690 ;; about it.
1691 (warn "ignoring unrecognized line ~S in ~A" line filename)
1692 (multiple-value-bind (value name)
1693 (if (string= "0x" line :end2 2)
1694 (values (parse-integer line :start 2 :end p1 :radix 16)
1695 (subseq line (1+ p2)))
1696 (values (parse-integer line :end p1 :radix 16)
1697 (subseq line (1+ p2))))
1698 ;; KLUDGE CLH 2010-05-31: on darwin, nm gives us
1699 ;; _function but dlsym expects us to look up
1700 ;; function, without the leading _ . Therefore, we
1701 ;; strip it off here.
1702 #!+darwin
1703 (when (equal (char name 0) #\_)
1704 (setf name (subseq name 1)))
1705 (multiple-value-bind (old-value found)
1706 (gethash name *cold-foreign-symbol-table*)
1707 (when (and found
1708 (not (= old-value value)))
1709 (warn "redefining ~S from #X~X to #X~X"
1710 name old-value value)))
1711 (/show "adding to *cold-foreign-symbol-table*:" name value)
1712 (setf (gethash name *cold-foreign-symbol-table*) value)
1713 #!+win32
1714 (let ((at-position (position #\@ name)))
1715 (when at-position
1716 (let ((name (subseq name 0 at-position)))
1717 (multiple-value-bind (old-value found)
1718 (gethash name *cold-foreign-symbol-table*)
1719 (when (and found
1720 (not (= old-value value)))
1721 (warn "redefining ~S from #X~X to #X~X"
1722 name old-value value)))
1723 (setf (gethash name *cold-foreign-symbol-table*)
1724 value)))))))))
1725 (values)) ;; PROGN
1727 (defun cold-foreign-symbol-address (name)
1728 (or (find-foreign-symbol-in-table name *cold-foreign-symbol-table*)
1729 *foreign-symbol-placeholder-value*
1730 (progn
1731 (format *error-output* "~&The foreign symbol table is:~%")
1732 (maphash (lambda (k v)
1733 (format *error-output* "~&~S = #X~8X~%" k v))
1734 *cold-foreign-symbol-table*)
1735 (error "The foreign symbol ~S is undefined." name))))
1737 (defvar *cold-assembler-routines*)
1739 (defvar *cold-assembler-fixups*)
1741 (defun record-cold-assembler-routine (name address)
1742 (/xhow "in RECORD-COLD-ASSEMBLER-ROUTINE" name address)
1743 (push (cons name address)
1744 *cold-assembler-routines*))
1746 (defun record-cold-assembler-fixup (routine
1747 code-object
1748 offset
1749 &optional
1750 (kind :both))
1751 (push (list routine code-object offset kind)
1752 *cold-assembler-fixups*))
1754 (defun lookup-assembler-reference (symbol)
1755 (let ((value (cdr (assoc symbol *cold-assembler-routines*))))
1756 ;; FIXME: Should this be ERROR instead of WARN?
1757 (unless value
1758 (warn "Assembler routine ~S not defined." symbol))
1759 value))
1761 ;;; The x86 port needs to store code fixups along with code objects if
1762 ;;; they are to be moved, so fixups for code objects in the dynamic
1763 ;;; heap need to be noted.
1764 #!+x86
1765 (defvar *load-time-code-fixups*)
1767 #!+x86
1768 (defun note-load-time-code-fixup (code-object offset)
1769 ;; If CODE-OBJECT might be moved
1770 (when (= (gspace-identifier (descriptor-intuit-gspace code-object))
1771 dynamic-core-space-id)
1772 (push offset (gethash (descriptor-bits code-object)
1773 *load-time-code-fixups*
1774 nil)))
1775 (values))
1777 #!+x86
1778 (defun output-load-time-code-fixups ()
1779 (let ((fixup-infos nil))
1780 (maphash
1781 (lambda (code-object-address fixup-offsets)
1782 (push (cons code-object-address fixup-offsets) fixup-infos))
1783 *load-time-code-fixups*)
1784 (setq fixup-infos (sort fixup-infos #'< :key #'car))
1785 (dolist (fixup-info fixup-infos)
1786 (let ((code-object-address (car fixup-info))
1787 (fixup-offsets (cdr fixup-info)))
1788 (let ((fixup-vector
1789 (allocate-vector-object
1790 *dynamic* sb!vm:n-word-bits (length fixup-offsets)
1791 sb!vm:simple-array-unsigned-byte-32-widetag)))
1792 (do ((index sb!vm:vector-data-offset (1+ index))
1793 (fixups fixup-offsets (cdr fixups)))
1794 ((null fixups))
1795 (write-wordindexed fixup-vector index
1796 (make-random-descriptor (car fixups))))
1797 ;; KLUDGE: The fixup vector is stored as the first constant,
1798 ;; not as a separately-named slot.
1799 (write-wordindexed (make-random-descriptor code-object-address)
1800 sb!vm:code-constants-offset
1801 fixup-vector))))))
1803 ;;; Given a pointer to a code object and an offset relative to the
1804 ;;; tail of the code object's header, return an offset relative to the
1805 ;;; (beginning of the) code object.
1807 ;;; FIXME: It might be clearer to reexpress
1808 ;;; (LET ((X (CALC-OFFSET CODE-OBJECT OFFSET0))) ..)
1809 ;;; as
1810 ;;; (LET ((X (+ OFFSET0 (CODE-OBJECT-HEADER-N-BYTES CODE-OBJECT)))) ..).
1811 (declaim (ftype (function (descriptor sb!vm:word)) calc-offset))
1812 (defun calc-offset (code-object offset-from-tail-of-header)
1813 (let* ((header (read-memory code-object))
1814 (header-n-words (ash (descriptor-bits header)
1815 (- sb!vm:n-widetag-bits)))
1816 (header-n-bytes (ash header-n-words sb!vm:word-shift))
1817 (result (+ offset-from-tail-of-header header-n-bytes)))
1818 result))
1820 (declaim (ftype (function (descriptor sb!vm:word sb!vm:word keyword))
1821 do-cold-fixup))
1822 (defun do-cold-fixup (code-object after-header value kind)
1823 (let* ((offset-within-code-object (calc-offset code-object after-header))
1824 (gspace-bytes (descriptor-bytes code-object))
1825 (gspace-byte-offset (+ (descriptor-byte-offset code-object)
1826 offset-within-code-object))
1827 (gspace-byte-address (gspace-byte-address
1828 (descriptor-gspace code-object))))
1829 ;; There's just a ton of code here that gets deleted,
1830 ;; inhibiting the view of the the forest through the trees.
1831 ;; Use of #+sbcl would say "probable bug in read-time conditional"
1832 #+#.(cl:if (cl:member :sbcl cl:*features*) '(and) '(or))
1833 (declare (sb-ext:muffle-conditions sb-ext:code-deletion-note))
1834 (ecase +backend-fasl-file-implementation+
1835 ;; See CMU CL source for other formerly-supported architectures
1836 ;; (and note that you have to rewrite them to use BVREF-X
1837 ;; instead of SAP-REF).
1838 (:alpha
1839 (ecase kind
1840 (:jmp-hint
1841 (assert (zerop (ldb (byte 2 0) value))))
1842 (:bits-63-48
1843 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1844 (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
1845 (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
1846 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1847 (ldb (byte 8 48) value)
1848 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1849 (ldb (byte 8 56) value))))
1850 (:bits-47-32
1851 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1852 (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
1853 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1854 (ldb (byte 8 32) value)
1855 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1856 (ldb (byte 8 40) value))))
1857 (:ldah
1858 (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
1859 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1860 (ldb (byte 8 16) value)
1861 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1862 (ldb (byte 8 24) value))))
1863 (:lda
1864 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1865 (ldb (byte 8 0) value)
1866 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1867 (ldb (byte 8 8) value)))))
1868 (:arm
1869 (ecase kind
1870 (:absolute
1871 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))))
1872 (:hppa
1873 (ecase kind
1874 (:load
1875 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1876 (logior (mask-field (byte 18 14)
1877 (bvref-32 gspace-bytes gspace-byte-offset))
1878 (if (< value 0)
1879 (1+ (ash (ldb (byte 13 0) value) 1))
1880 (ash (ldb (byte 13 0) value) 1)))))
1881 (:load11u
1882 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1883 (logior (mask-field (byte 18 14)
1884 (bvref-32 gspace-bytes gspace-byte-offset))
1885 (if (< value 0)
1886 (1+ (ash (ldb (byte 10 0) value) 1))
1887 (ash (ldb (byte 11 0) value) 1)))))
1888 (:load-short
1889 (let ((low-bits (ldb (byte 11 0) value)))
1890 (assert (<= 0 low-bits (1- (ash 1 4)))))
1891 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1892 (logior (ash (dpb (ldb (byte 4 0) value)
1893 (byte 4 1)
1894 (ldb (byte 1 4) value)) 17)
1895 (logand (bvref-32 gspace-bytes gspace-byte-offset)
1896 #xffe0ffff))))
1897 (:hi
1898 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1899 (logior (mask-field (byte 11 21)
1900 (bvref-32 gspace-bytes gspace-byte-offset))
1901 (ash (ldb (byte 5 13) value) 16)
1902 (ash (ldb (byte 2 18) value) 14)
1903 (ash (ldb (byte 2 11) value) 12)
1904 (ash (ldb (byte 11 20) value) 1)
1905 (ldb (byte 1 31) value))))
1906 (:branch
1907 (let ((bits (ldb (byte 9 2) value)))
1908 (assert (zerop (ldb (byte 2 0) value)))
1909 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1910 (logior (ash bits 3)
1911 (mask-field (byte 1 1) (bvref-32 gspace-bytes gspace-byte-offset))
1912 (mask-field (byte 3 13) (bvref-32 gspace-bytes gspace-byte-offset))
1913 (mask-field (byte 11 21) (bvref-32 gspace-bytes gspace-byte-offset))))))))
1914 (:mips
1915 (ecase kind
1916 (:jump
1917 (assert (zerop (ash value -28)))
1918 (setf (ldb (byte 26 0)
1919 (bvref-32 gspace-bytes gspace-byte-offset))
1920 (ash value -2)))
1921 (:lui
1922 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1923 (logior (mask-field (byte 16 16)
1924 (bvref-32 gspace-bytes gspace-byte-offset))
1925 (ash (1+ (ldb (byte 17 15) value)) -1))))
1926 (:addi
1927 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1928 (logior (mask-field (byte 16 16)
1929 (bvref-32 gspace-bytes gspace-byte-offset))
1930 (ldb (byte 16 0) value))))))
1931 ;; FIXME: PowerPC Fixups are not fully implemented. The bit
1932 ;; here starts to set things up to work properly, but there
1933 ;; needs to be corresponding code in ppc-vm.lisp
1934 (:ppc
1935 (ecase kind
1936 (:ba
1937 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1938 (dpb (ash value -2) (byte 24 2)
1939 (bvref-32 gspace-bytes gspace-byte-offset))))
1940 (:ha
1941 (let* ((un-fixed-up (bvref-16 gspace-bytes
1942 (+ gspace-byte-offset 2)))
1943 (fixed-up (+ un-fixed-up value))
1944 (h (ldb (byte 16 16) fixed-up))
1945 (l (ldb (byte 16 0) fixed-up)))
1946 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1947 (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
1949 (let* ((un-fixed-up (bvref-16 gspace-bytes
1950 (+ gspace-byte-offset 2)))
1951 (fixed-up (+ un-fixed-up value)))
1952 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1953 (ldb (byte 16 0) fixed-up))))))
1954 (:sparc
1955 (ecase kind
1956 (:call
1957 (error "can't deal with call fixups yet"))
1958 (:sethi
1959 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1960 (dpb (ldb (byte 22 10) value)
1961 (byte 22 0)
1962 (bvref-32 gspace-bytes gspace-byte-offset))))
1963 (:add
1964 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1965 (dpb (ldb (byte 10 0) value)
1966 (byte 10 0)
1967 (bvref-32 gspace-bytes gspace-byte-offset))))))
1968 ((:x86 :x86-64)
1969 ;; XXX: Note that un-fixed-up is read via bvref-word, which is
1970 ;; 64 bits wide on x86-64, but the fixed-up value is written
1971 ;; via bvref-32. This would make more sense if we supported
1972 ;; :absolute64 fixups, but apparently the cross-compiler
1973 ;; doesn't dump them.
1974 (let* ((un-fixed-up (bvref-word gspace-bytes
1975 gspace-byte-offset))
1976 (code-object-start-addr (logandc2 (descriptor-bits code-object)
1977 sb!vm:lowtag-mask)))
1978 (assert (= code-object-start-addr
1979 (+ gspace-byte-address
1980 (descriptor-byte-offset code-object))))
1981 (ecase kind
1982 (:absolute
1983 (let ((fixed-up (+ value un-fixed-up)))
1984 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1985 fixed-up)
1986 ;; comment from CMU CL sources:
1988 ;; Note absolute fixups that point within the object.
1989 ;; KLUDGE: There seems to be an implicit assumption in
1990 ;; the old CMU CL code here, that if it doesn't point
1991 ;; before the object, it must point within the object
1992 ;; (not beyond it). It would be good to add an
1993 ;; explanation of why that's true, or an assertion that
1994 ;; it's really true, or both.
1996 ;; One possible explanation is that all absolute fixups
1997 ;; point either within the code object, within the
1998 ;; runtime, within read-only or static-space, or within
1999 ;; the linkage-table space. In all x86 configurations,
2000 ;; these areas are prior to the start of dynamic space,
2001 ;; where all the code-objects are loaded.
2002 #!+x86
2003 (unless (< fixed-up code-object-start-addr)
2004 (note-load-time-code-fixup code-object
2005 after-header))))
2006 (:relative ; (used for arguments to X86 relative CALL instruction)
2007 (let ((fixed-up (- (+ value un-fixed-up)
2008 gspace-byte-address
2009 gspace-byte-offset
2010 4))) ; "length of CALL argument"
2011 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2012 fixed-up)
2013 ;; Note relative fixups that point outside the code
2014 ;; object, which is to say all relative fixups, since
2015 ;; relative addressing within a code object never needs
2016 ;; a fixup.
2017 #!+x86
2018 (note-load-time-code-fixup code-object
2019 after-header))))))))
2020 (values))
2022 (defun resolve-assembler-fixups ()
2023 (dolist (fixup *cold-assembler-fixups*)
2024 (let* ((routine (car fixup))
2025 (value (lookup-assembler-reference routine)))
2026 (when value
2027 (do-cold-fixup (second fixup) (third fixup) value (fourth fixup))))))
2029 #!+sb-dynamic-core
2030 (progn
2031 (defparameter *dyncore-address* sb!vm::linkage-table-space-start)
2032 (defparameter *dyncore-linkage-keys* nil)
2033 (defparameter *dyncore-table* (make-hash-table :test 'equal))
2035 (defun dyncore-note-symbol (symbol-name datap)
2036 "Register a symbol and return its address in proto-linkage-table."
2037 (let ((key (cons symbol-name datap)))
2038 (symbol-macrolet ((entry (gethash key *dyncore-table*)))
2039 (or entry
2040 (setf entry
2041 (prog1 *dyncore-address*
2042 (push key *dyncore-linkage-keys*)
2043 (incf *dyncore-address* sb!vm::linkage-table-entry-size))))))))
2045 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
2046 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
2047 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
2048 ;;; target-load.lisp refers to.
2049 (defun foreign-symbols-to-core ()
2050 (let ((symbols nil)
2051 (result *nil-descriptor*))
2052 #!-sb-dynamic-core
2053 (progn
2054 (maphash (lambda (symbol value)
2055 (push (cons symbol value) symbols))
2056 *cold-foreign-symbol-table*)
2057 (setq symbols (sort symbols #'string< :key #'car))
2058 (dolist (symbol symbols)
2059 (cold-push (cold-cons (base-string-to-core (car symbol))
2060 (number-to-core (cdr symbol)))
2061 result)))
2062 (cold-set '*!initial-foreign-symbols* result)
2063 #!+sb-dynamic-core
2064 (let ((runtime-linking-list *nil-descriptor*))
2065 (dolist (symbol *dyncore-linkage-keys*)
2066 (cold-push (cold-cons (base-string-to-core (car symbol))
2067 (cdr symbol))
2068 runtime-linking-list))
2069 (cold-set 'sb!vm::*required-runtime-c-symbols*
2070 runtime-linking-list)))
2071 (let ((result *nil-descriptor*))
2072 (dolist (rtn (sort (copy-list *cold-assembler-routines*) #'string< :key #'car))
2073 (cold-push (cold-cons (cold-intern (car rtn))
2074 (number-to-core (cdr rtn)))
2075 result))
2076 (cold-set '*!initial-assembler-routines* result)))
2079 ;;;; general machinery for cold-loading FASL files
2081 ;;; FOP functions for cold loading
2082 (defvar *cold-fop-funs*
2083 ;; We start out with a copy of the ordinary *FOP-FUNS*. The ones
2084 ;; which aren't appropriate for cold load will be destructively
2085 ;; modified.
2086 (copy-seq *fop-funs*))
2088 (defun pop-fop-stack ()
2089 (let* ((stack *fop-stack*)
2090 (top (svref stack 0)))
2091 (declare (type index top))
2092 (when (eql 0 top)
2093 (error "FOP stack empty"))
2094 (setf (svref stack 0) (1- top))
2095 (svref stack top)))
2097 ;;; Cause a fop to have a special definition for cold load.
2099 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
2100 ;;; (1) looks up the code for this name (created by a previous
2101 ;;; DEFINE-FOP) instead of creating a code, and
2102 ;;; (2) stores its definition in the *COLD-FOP-FUNS* vector,
2103 ;;; instead of storing in the *FOP-FUNS* vector.
2104 (defmacro define-cold-fop ((name &optional arglist) &rest forms)
2105 (let* ((code (get name 'opcode))
2106 (argp (plusp (sbit (car *fop-signatures*) (ash code -2))))
2107 (fname (symbolicate "COLD-" name)))
2108 (unless code
2109 (error "~S is not a defined FOP." name))
2110 (when (and argp (not (singleton-p arglist)))
2111 (error "~S must take one argument" name))
2112 `(progn
2113 (defun ,fname ,arglist
2114 (macrolet ((pop-stack () `(pop-fop-stack))) ,@forms))
2115 ,@(loop for i from code to (logior code (if argp 3 0))
2116 collect `(setf (svref *cold-fop-funs* ,i) #',fname)))))
2118 ;;; Cause a fop to be undefined in cold load.
2119 (defmacro not-cold-fop (name)
2120 `(define-cold-fop (,name)
2121 (error "The fop ~S is not supported in cold load." ',name)))
2123 ;;; COLD-LOAD loads stuff into the core image being built by calling
2124 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
2125 ;;; loading functions.
2126 (defun cold-load (filename)
2127 "Load the file named by FILENAME into the cold load image being built."
2128 (let* ((*fop-funs* *cold-fop-funs*)
2129 (*cold-load-filename* (etypecase filename
2130 (string filename)
2131 (pathname (namestring filename)))))
2132 (with-open-file (s filename :element-type '(unsigned-byte 8))
2133 (load-as-fasl s nil nil))))
2135 ;;;; miscellaneous cold fops
2137 (define-cold-fop (fop-misc-trap) *unbound-marker*)
2139 (define-cold-fop (fop-character (c))
2140 (make-character-descriptor c))
2142 (define-cold-fop (fop-empty-list) nil)
2143 (define-cold-fop (fop-truth) t)
2145 (define-cold-fop (fop-struct (size)) ; n-words incl. layout, excluding header
2146 (let* ((layout (pop-stack))
2147 (result (allocate-struct *dynamic* layout size))
2148 (metadata
2149 (descriptor-fixnum
2150 (read-slot layout (find-layout 'layout)
2151 #!-interleaved-raw-slots :n-untagged-slots
2152 #!+interleaved-raw-slots :untagged-bitmap)))
2153 #!-interleaved-raw-slots (ntagged (- size metadata))
2155 ;; Raw slots can not possibly work because dump-struct uses
2156 ;; %RAW-INSTANCE-REF/WORD which does not exist in the cross-compiler.
2157 ;; Remove this assertion if that problem is somehow circumvented.
2158 (unless (= metadata 0)
2159 (error "Raw slots not working in genesis."))
2161 (do ((index 1 (1+ index)))
2162 ((eql index size))
2163 (declare (fixnum index))
2164 (write-wordindexed result
2165 (+ index sb!vm:instance-slots-offset)
2166 (if #!-interleaved-raw-slots (>= index ntagged)
2167 #!+interleaved-raw-slots (logbitp index metadata)
2168 (descriptor-word-sized-integer (pop-stack))
2169 (pop-stack))))
2170 result))
2172 (define-cold-fop (fop-layout)
2173 (let* ((metadata-des (pop-stack))
2174 (length-des (pop-stack))
2175 (depthoid-des (pop-stack))
2176 (cold-inherits (pop-stack))
2177 (name (pop-stack))
2178 (old-layout-descriptor (gethash name *cold-layouts*)))
2179 (declare (type descriptor length-des depthoid-des cold-inherits))
2180 (declare (type symbol name))
2181 ;; If a layout of this name has been defined already
2182 (if old-layout-descriptor
2183 ;; Enforce consistency between the previous definition and the
2184 ;; current definition, then return the previous definition.
2185 (flet ((get-slot (keyword)
2186 (read-slot old-layout-descriptor (find-layout 'layout) keyword)))
2187 (let ((old-length (descriptor-fixnum (get-slot :length)))
2188 (old-inherits-list (listify-cold-inherits (get-slot :inherits)))
2189 (old-depthoid (descriptor-fixnum (get-slot :depthoid)))
2190 (old-metadata
2191 (descriptor-fixnum
2192 (get-slot #!-interleaved-raw-slots :n-untagged-slots
2193 #!+interleaved-raw-slots :untagged-bitmap)))
2194 (length (descriptor-fixnum length-des))
2195 (inherits-list (listify-cold-inherits cold-inherits))
2196 (depthoid (descriptor-fixnum depthoid-des))
2197 (metadata (descriptor-fixnum metadata-des)))
2198 (unless (= length old-length)
2199 (error "cold loading a reference to class ~S when the compile~%~
2200 time length was ~S and current length is ~S"
2201 name
2202 length
2203 old-length))
2204 (unless (equal inherits-list old-inherits-list)
2205 (error "cold loading a reference to class ~S when the compile~%~
2206 time inherits were ~S~%~
2207 and current inherits are ~S"
2208 name
2209 inherits-list
2210 old-inherits-list))
2211 (unless (= depthoid old-depthoid)
2212 (error "cold loading a reference to class ~S when the compile~%~
2213 time inheritance depthoid was ~S and current inheritance~%~
2214 depthoid is ~S"
2215 name
2216 depthoid
2217 old-depthoid))
2218 (unless (= metadata old-metadata)
2219 (error "cold loading a reference to class ~S when the compile~%~
2220 time raw-slot-metadata was ~S and is currently ~S"
2221 name
2222 metadata
2223 old-metadata)))
2224 old-layout-descriptor)
2225 ;; Make a new definition from scratch.
2226 (make-cold-layout name length-des cold-inherits depthoid-des
2227 metadata-des))))
2229 ;;;; cold fops for loading symbols
2231 ;;; Load a symbol SIZE characters long from *FASL-INPUT-STREAM* and
2232 ;;; intern that symbol in PACKAGE.
2233 (defun cold-load-symbol (size package)
2234 (let ((string (make-string size)))
2235 (read-string-as-bytes *fasl-input-stream* string)
2236 (push-fop-table (intern string package))))
2238 ;; I don't feel like hacking up DEFINE-COLD-FOP any more than necessary,
2239 ;; so this code is handcrafted to accept two operands.
2240 (flet ((fop-cold-symbol-in-package-save (index pname-len)
2241 (cold-load-symbol pname-len (ref-fop-table index))))
2242 (dotimes (i 16) ; occupies 16 cells in the dispatch table
2243 (setf (svref *cold-fop-funs* (+ (get 'fop-symbol-in-package-save 'opcode) i))
2244 #'fop-cold-symbol-in-package-save)))
2246 (define-cold-fop (fop-lisp-symbol-save (namelen))
2247 (cold-load-symbol namelen *cl-package*))
2249 (define-cold-fop (fop-keyword-symbol-save (namelen))
2250 (cold-load-symbol namelen *keyword-package*))
2252 (define-cold-fop (fop-uninterned-symbol-save (namelen))
2253 (let ((name (make-string namelen)))
2254 (read-string-as-bytes *fasl-input-stream* name)
2255 (push-fop-table (get-uninterned-symbol name))))
2257 (define-cold-fop (fop-copy-symbol-save (index))
2258 (let* ((symbol (ref-fop-table index))
2259 (name
2260 (if (symbolp symbol)
2261 (symbol-name symbol)
2262 (base-string-from-core
2263 (read-wordindexed symbol sb!vm:symbol-name-slot)))))
2264 ;; Genesis performs additional coalescing of uninterned symbols
2265 (push-fop-table (get-uninterned-symbol name))))
2267 ;;;; cold fops for loading packages
2269 (define-cold-fop (fop-named-package-save (namelen))
2270 (let ((name (make-string namelen)))
2271 (read-string-as-bytes *fasl-input-stream* name)
2272 (push-fop-table (find-package name))))
2274 ;;;; cold fops for loading lists
2276 ;;; Make a list of the top LENGTH things on the fop stack. The last
2277 ;;; cdr of the list is set to LAST.
2278 (defmacro cold-stack-list (length last)
2279 `(do* ((index ,length (1- index))
2280 (result ,last (cold-cons (pop-stack) result)))
2281 ((= index 0) result)
2282 (declare (fixnum index))))
2284 (define-cold-fop (fop-list)
2285 (cold-stack-list (read-byte-arg) *nil-descriptor*))
2286 (define-cold-fop (fop-list*)
2287 (cold-stack-list (read-byte-arg) (pop-stack)))
2288 (define-cold-fop (fop-list-1)
2289 (cold-stack-list 1 *nil-descriptor*))
2290 (define-cold-fop (fop-list-2)
2291 (cold-stack-list 2 *nil-descriptor*))
2292 (define-cold-fop (fop-list-3)
2293 (cold-stack-list 3 *nil-descriptor*))
2294 (define-cold-fop (fop-list-4)
2295 (cold-stack-list 4 *nil-descriptor*))
2296 (define-cold-fop (fop-list-5)
2297 (cold-stack-list 5 *nil-descriptor*))
2298 (define-cold-fop (fop-list-6)
2299 (cold-stack-list 6 *nil-descriptor*))
2300 (define-cold-fop (fop-list-7)
2301 (cold-stack-list 7 *nil-descriptor*))
2302 (define-cold-fop (fop-list-8)
2303 (cold-stack-list 8 *nil-descriptor*))
2304 (define-cold-fop (fop-list*-1)
2305 (cold-stack-list 1 (pop-stack)))
2306 (define-cold-fop (fop-list*-2)
2307 (cold-stack-list 2 (pop-stack)))
2308 (define-cold-fop (fop-list*-3)
2309 (cold-stack-list 3 (pop-stack)))
2310 (define-cold-fop (fop-list*-4)
2311 (cold-stack-list 4 (pop-stack)))
2312 (define-cold-fop (fop-list*-5)
2313 (cold-stack-list 5 (pop-stack)))
2314 (define-cold-fop (fop-list*-6)
2315 (cold-stack-list 6 (pop-stack)))
2316 (define-cold-fop (fop-list*-7)
2317 (cold-stack-list 7 (pop-stack)))
2318 (define-cold-fop (fop-list*-8)
2319 (cold-stack-list 8 (pop-stack)))
2321 ;;;; cold fops for loading vectors
2323 (define-cold-fop (fop-base-string (len))
2324 (let ((string (make-string len)))
2325 (read-string-as-bytes *fasl-input-stream* string)
2326 (base-string-to-core string)))
2328 #!+sb-unicode
2329 (define-cold-fop (fop-character-string (len))
2330 (bug "CHARACTER-STRING[~D] dumped by cross-compiler." len))
2332 (define-cold-fop (fop-vector (size))
2333 (let* ((result (allocate-vector-object *dynamic*
2334 sb!vm:n-word-bits
2335 size
2336 sb!vm:simple-vector-widetag)))
2337 (do ((index (1- size) (1- index)))
2338 ((minusp index))
2339 (declare (fixnum index))
2340 (write-wordindexed result
2341 (+ index sb!vm:vector-data-offset)
2342 (pop-stack)))
2343 result))
2345 (define-cold-fop (fop-spec-vector)
2346 (let* ((len (read-word-arg))
2347 (type (read-byte-arg))
2348 (sizebits (aref **saetp-bits-per-length** type))
2349 (result (progn (aver (< sizebits 255))
2350 (allocate-vector-object *dynamic* sizebits len type)))
2351 (start (+ (descriptor-byte-offset result)
2352 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2353 (end (+ start
2354 (ceiling (* len sizebits)
2355 sb!vm:n-byte-bits))))
2356 (read-bigvec-as-sequence-or-die (descriptor-bytes result)
2357 *fasl-input-stream*
2358 :start start
2359 :end end)
2360 result))
2362 (not-cold-fop fop-array)
2363 #+nil
2364 ;; This code is unexercised. The only use of FOP-ARRAY is from target-dump.
2365 ;; It would be a shame to delete it though, as it might come in handy.
2366 (define-cold-fop (fop-array)
2367 (let* ((rank (read-word-arg))
2368 (data-vector (pop-stack))
2369 (result (allocate-object *dynamic*
2370 (+ sb!vm:array-dimensions-offset rank)
2371 sb!vm:other-pointer-lowtag)))
2372 (write-header-word result rank sb!vm:simple-array-widetag)
2373 (write-wordindexed result sb!vm:array-fill-pointer-slot *nil-descriptor*)
2374 (write-wordindexed result sb!vm:array-data-slot data-vector)
2375 (write-wordindexed result sb!vm:array-displacement-slot *nil-descriptor*)
2376 (write-wordindexed result sb!vm:array-displaced-p-slot *nil-descriptor*)
2377 (write-wordindexed result sb!vm:array-displaced-from-slot *nil-descriptor*)
2378 (let ((total-elements 1))
2379 (dotimes (axis rank)
2380 (let ((dim (pop-stack)))
2381 (unless (is-fixnum-lowtag (descriptor-lowtag dim))
2382 (error "non-fixnum dimension? (~S)" dim))
2383 (setf total-elements (* total-elements (descriptor-fixnum dim)))
2384 (write-wordindexed result
2385 (+ sb!vm:array-dimensions-offset axis)
2386 dim)))
2387 (write-wordindexed result
2388 sb!vm:array-elements-slot
2389 (make-fixnum-descriptor total-elements)))
2390 result))
2393 ;;;; cold fops for loading numbers
2395 (defmacro define-cold-number-fop (fop &optional arglist)
2396 ;; Invoke the ordinary warm version of this fop to cons the number.
2397 `(define-cold-fop (,fop ,arglist) (number-to-core (,fop ,@arglist))))
2399 (define-cold-number-fop fop-single-float)
2400 (define-cold-number-fop fop-double-float)
2401 (define-cold-number-fop fop-word-integer)
2402 (define-cold-number-fop fop-byte-integer)
2403 (define-cold-number-fop fop-complex-single-float)
2404 (define-cold-number-fop fop-complex-double-float)
2405 (define-cold-number-fop fop-integer (n-bytes))
2407 (define-cold-fop (fop-ratio)
2408 (let ((den (pop-stack)))
2409 (number-pair-to-core (pop-stack) den sb!vm:ratio-widetag)))
2411 (define-cold-fop (fop-complex)
2412 (let ((im (pop-stack)))
2413 (number-pair-to-core (pop-stack) im sb!vm:complex-widetag)))
2415 ;;;; cold fops for calling (or not calling)
2417 (not-cold-fop fop-eval)
2418 (not-cold-fop fop-eval-for-effect)
2420 (defvar *load-time-value-counter*)
2422 (define-cold-fop (fop-funcall)
2423 (unless (= (read-byte-arg) 0)
2424 (error "You can't FOP-FUNCALL arbitrary stuff in cold load."))
2425 (let ((counter *load-time-value-counter*))
2426 (cold-push (cold-cons
2427 (cold-intern :load-time-value)
2428 (cold-cons
2429 (pop-stack)
2430 (cold-cons
2431 (number-to-core counter)
2432 *nil-descriptor*)))
2433 *current-reversed-cold-toplevels*)
2434 (setf *load-time-value-counter* (1+ counter))
2435 (make-descriptor 0 :load-time-value counter)))
2437 (defun finalize-load-time-value-noise ()
2438 (cold-set '*!load-time-values*
2439 (allocate-vector-object *dynamic*
2440 sb!vm:n-word-bits
2441 *load-time-value-counter*
2442 sb!vm:simple-vector-widetag)))
2444 (define-cold-fop (fop-funcall-for-effect)
2445 (let ((argc (read-byte-arg))
2446 (args))
2447 (when (plusp argc)
2448 (dotimes (i argc) (push (pop-stack) args))
2449 (let ((f (pop-stack)))
2450 (ecase f
2451 (sb!impl::%defun
2452 (return-from cold-fop-funcall-for-effect
2453 (apply #'cold-fset args)))))))
2454 (cold-push (pop-stack) *current-reversed-cold-toplevels*))
2456 ;;;; cold fops for fixing up circularities
2458 (define-cold-fop (fop-rplaca)
2459 (let ((obj (ref-fop-table (read-word-arg)))
2460 (idx (read-word-arg)))
2461 (write-memory (cold-nthcdr idx obj) (pop-stack))))
2463 (define-cold-fop (fop-rplacd)
2464 (let ((obj (ref-fop-table (read-word-arg)))
2465 (idx (read-word-arg)))
2466 (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2468 (define-cold-fop (fop-svset)
2469 (let ((obj (ref-fop-table (read-word-arg)))
2470 (idx (read-word-arg)))
2471 (write-wordindexed obj
2472 (+ idx
2473 (ecase (descriptor-lowtag obj)
2474 (#.sb!vm:instance-pointer-lowtag 1)
2475 (#.sb!vm:other-pointer-lowtag 2)))
2476 (pop-stack))))
2478 (define-cold-fop (fop-structset)
2479 (let ((obj (ref-fop-table (read-word-arg)))
2480 (idx (read-word-arg)))
2481 (write-wordindexed obj (1+ idx) (pop-stack))))
2483 (define-cold-fop (fop-nthcdr)
2484 (cold-nthcdr (read-word-arg) (pop-stack)))
2486 (defun cold-nthcdr (index obj)
2487 (dotimes (i index)
2488 (setq obj (read-wordindexed obj sb!vm:cons-cdr-slot)))
2489 obj)
2491 ;;;; cold fops for loading code objects and functions
2493 (define-cold-fop (fop-note-debug-source)
2494 (let ((debug-source (pop-stack)))
2495 (cold-push debug-source *current-debug-sources*)))
2497 (define-cold-fop (fop-fdefn)
2498 (cold-fdefinition-object (pop-stack)))
2500 #!-(or x86 x86-64)
2501 (define-cold-fop (fop-sanctify-for-execution)
2502 (pop-stack))
2504 ;;; Setting this variable shows what code looks like before any
2505 ;;; fixups (or function headers) are applied.
2506 #!+sb-show (defvar *show-pre-fixup-code-p* nil)
2508 (defun cold-load-code (nconst code-size)
2509 (macrolet ((pop-stack () '(pop-fop-stack)))
2510 (let* ((raw-header-n-words (+ sb!vm:code-constants-offset nconst))
2511 (header-n-words
2512 ;; Note: we round the number of constants up to ensure
2513 ;; that the code vector will be properly aligned.
2514 (round-up raw-header-n-words 2))
2515 (des (allocate-cold-descriptor *dynamic*
2516 (+ (ash header-n-words
2517 sb!vm:word-shift)
2518 code-size)
2519 sb!vm:other-pointer-lowtag)))
2520 (write-header-word des header-n-words sb!vm:code-header-widetag)
2521 (write-wordindexed des
2522 sb!vm:code-code-size-slot
2523 (make-fixnum-descriptor code-size))
2524 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2525 (write-wordindexed des sb!vm:code-debug-info-slot (pop-stack))
2526 (when (oddp raw-header-n-words)
2527 (write-wordindexed des raw-header-n-words (make-descriptor 0)))
2528 (do ((index (1- raw-header-n-words) (1- index)))
2529 ((< index sb!vm:code-constants-offset))
2530 (write-wordindexed des index (pop-stack)))
2531 (let* ((start (+ (descriptor-byte-offset des)
2532 (ash header-n-words sb!vm:word-shift)))
2533 (end (+ start code-size)))
2534 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2535 *fasl-input-stream*
2536 :start start
2537 :end end)
2538 #!+sb-show
2539 (when *show-pre-fixup-code-p*
2540 (format *trace-output*
2541 "~&/raw code from code-fop ~W ~W:~%"
2542 nconst
2543 code-size)
2544 (do ((i start (+ i sb!vm:n-word-bytes)))
2545 ((>= i end))
2546 (format *trace-output*
2547 "/#X~8,'0x: #X~8,'0x~%"
2548 (+ i (gspace-byte-address (descriptor-gspace des)))
2549 (bvref-32 (descriptor-bytes des) i)))))
2550 des)))
2552 (dotimes (i 16) ; occupies 16 cells in the dispatch table
2553 (setf (svref *cold-fop-funs* (+ (get 'fop-code 'opcode) i))
2554 #'cold-load-code))
2556 (define-cold-fop (fop-alter-code (slot))
2557 (let ((value (pop-stack))
2558 (code (pop-stack)))
2559 (write-wordindexed code slot value)))
2561 (defvar *simple-fun-metadata* (make-hash-table :test 'equalp))
2563 ;; Return an expression that can be used to coalesce type-specifiers
2564 ;; and lambda lists attached to simple-funs. It doesn't have to be
2565 ;; a "correct" host representation, just something that preserves EQUAL-ness.
2566 (defun make-equal-comparable-thing (descriptor)
2567 (labels ((recurse (x)
2568 (cond ((cold-null x) (return-from recurse nil))
2569 ((is-fixnum-lowtag (descriptor-lowtag x))
2570 (return-from recurse (descriptor-fixnum x)))
2571 #!+#.(cl:if (cl:= sb!vm:n-word-bits 64) '(and) '(or))
2572 ((is-other-immediate-lowtag (descriptor-lowtag x))
2573 (let ((bits (descriptor-bits x)))
2574 (when (= (logand bits sb!vm:widetag-mask)
2575 sb!vm:single-float-widetag)
2576 (return-from recurse `(:ffloat-bits ,bits))))))
2577 (ecase (descriptor-lowtag x)
2578 (#.sb!vm:list-pointer-lowtag
2579 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
2580 (#.sb!vm:other-pointer-lowtag
2581 (ecase (logand (descriptor-bits (read-memory x)) sb!vm:widetag-mask)
2582 (#.sb!vm:symbol-header-widetag
2583 (if (cold-null (read-wordindexed x sb!vm:symbol-package-slot))
2584 (get-or-make-uninterned-symbol
2585 (base-string-from-core
2586 (read-wordindexed x sb!vm:symbol-name-slot)))
2587 (warm-symbol x)))
2588 #!+#.(cl:if (cl:= sb!vm:n-word-bits 32) '(and) '(or))
2589 (#.sb!vm:single-float-widetag
2590 `(:ffloat-bits
2591 ,(read-bits-wordindexed x sb!vm:single-float-value-slot)))
2592 (#.sb!vm:double-float-widetag
2593 `(:dfloat-bits
2594 ,(read-bits-wordindexed x sb!vm:double-float-value-slot)
2595 #!+#.(cl:if (cl:= sb!vm:n-word-bits 32) '(and) '(or))
2596 ,(read-bits-wordindexed
2597 x (1+ sb!vm:double-float-value-slot))))
2598 (#.sb!vm:bignum-widetag
2599 (bignum-from-core x))
2600 (#.sb!vm:simple-base-string-widetag
2601 (base-string-from-core x))
2602 ;; Why do function lambda lists have simple-vectors in them?
2603 ;; Because we expose all &OPTIONAL and &KEY default forms.
2604 ;; I think this is abstraction leakage, except possibly for
2605 ;; advertised constant defaults of NIL and such.
2606 ;; How one expresses a value as a sexpr should otherwise
2607 ;; be of no concern to a user of the code.
2608 (#.sb!vm:simple-vector-widetag
2609 (vector-from-core x #'recurse))))))
2610 ;; Return a warm symbol whose name is similar to NAME, coaelescing
2611 ;; all occurrences of #:.WHOLE. across all files, e.g.
2612 (get-or-make-uninterned-symbol (name)
2613 (let ((key `(:uninterned-symbol ,name)))
2614 (or (gethash key *simple-fun-metadata*)
2615 (let ((symbol (make-symbol name)))
2616 (setf (gethash key *simple-fun-metadata*) symbol))))))
2617 (recurse descriptor)))
2619 (define-cold-fop (fop-fun-entry)
2620 (let* ((info (pop-stack))
2621 (type (pop-stack))
2622 (arglist (pop-stack))
2623 (name (pop-stack))
2624 (code-object (pop-stack))
2625 (offset (calc-offset code-object (read-word-arg)))
2626 (fn (descriptor-beyond code-object
2627 offset
2628 sb!vm:fun-pointer-lowtag))
2629 (next (read-wordindexed code-object sb!vm:code-entry-points-slot)))
2630 (unless (zerop (logand offset sb!vm:lowtag-mask))
2631 (error "unaligned function entry: ~S at #X~X" name offset))
2632 (write-wordindexed code-object sb!vm:code-entry-points-slot fn)
2633 (write-memory fn
2634 (make-other-immediate-descriptor
2635 (ash offset (- sb!vm:word-shift))
2636 sb!vm:simple-fun-header-widetag))
2637 (write-wordindexed fn
2638 sb!vm:simple-fun-self-slot
2639 ;; KLUDGE: Wiring decisions like this in at
2640 ;; this level ("if it's an x86") instead of a
2641 ;; higher level of abstraction ("if it has such
2642 ;; and such relocation peculiarities (which
2643 ;; happen to be confined to the x86)") is bad.
2644 ;; It would be nice if the code were instead
2645 ;; conditional on some more descriptive
2646 ;; feature, :STICKY-CODE or
2647 ;; :LOAD-GC-INTERACTION or something.
2649 ;; FIXME: The X86 definition of the function
2650 ;; self slot breaks everything object.tex says
2651 ;; about it. (As far as I can tell, the X86
2652 ;; definition makes it a pointer to the actual
2653 ;; code instead of a pointer back to the object
2654 ;; itself.) Ask on the mailing list whether
2655 ;; this is documented somewhere, and if not,
2656 ;; try to reverse engineer some documentation.
2657 #!-(or x86 x86-64)
2658 ;; a pointer back to the function object, as
2659 ;; described in CMU CL
2660 ;; src/docs/internals/object.tex
2662 #!+(or x86 x86-64)
2663 ;; KLUDGE: a pointer to the actual code of the
2664 ;; object, as described nowhere that I can find
2665 ;; -- WHN 19990907
2666 (make-descriptor ; raw bits that look like fixnum
2667 (+ (descriptor-bits fn)
2668 (- (ash sb!vm:simple-fun-code-offset
2669 sb!vm:word-shift)
2670 ;; FIXME: We should mask out the type
2671 ;; bits, not assume we know what they
2672 ;; are and subtract them out this way.
2673 sb!vm:fun-pointer-lowtag))))
2674 (write-wordindexed fn sb!vm:simple-fun-next-slot next)
2675 (write-wordindexed fn sb!vm:simple-fun-name-slot name)
2676 (flet ((coalesce (sexpr) ; a warm symbol or a cold cons tree
2677 (if (symbolp sexpr) ; will be cold-interned automatically
2678 sexpr
2679 (let ((representation (make-equal-comparable-thing sexpr)))
2680 (or (gethash representation *simple-fun-metadata*)
2681 (setf (gethash representation *simple-fun-metadata*)
2682 sexpr))))))
2683 (write-wordindexed fn sb!vm:simple-fun-arglist-slot (coalesce arglist))
2684 (write-wordindexed fn sb!vm:simple-fun-type-slot (coalesce type)))
2685 (write-wordindexed fn sb!vm::simple-fun-info-slot info)
2686 fn))
2688 #!+sb-thread
2689 (define-cold-fop (fop-symbol-tls-fixup)
2690 (let* ((symbol (pop-stack))
2691 (kind (pop-stack))
2692 (code-object (pop-stack)))
2693 (do-cold-fixup code-object (read-word-arg) (ensure-symbol-tls-index symbol)
2694 kind)
2695 code-object))
2697 (define-cold-fop (fop-foreign-fixup)
2698 (let* ((kind (pop-stack))
2699 (code-object (pop-stack))
2700 (len (read-byte-arg))
2701 (sym (make-string len)))
2702 (read-string-as-bytes *fasl-input-stream* sym)
2703 #!+sb-dynamic-core
2704 (let ((offset (read-word-arg))
2705 (value (dyncore-note-symbol sym nil)))
2706 (do-cold-fixup code-object offset value kind))
2707 #!- (and) (format t "Bad non-plt fixup: ~S~S~%" sym code-object)
2708 #!-sb-dynamic-core
2709 (let ((offset (read-word-arg))
2710 (value (cold-foreign-symbol-address sym)))
2711 (do-cold-fixup code-object offset value kind))
2712 code-object))
2714 #!+linkage-table
2715 (define-cold-fop (fop-foreign-dataref-fixup)
2716 (let* ((kind (pop-stack))
2717 (code-object (pop-stack))
2718 (len (read-byte-arg))
2719 (sym (make-string len)))
2720 #!-sb-dynamic-core (declare (ignore code-object))
2721 (read-string-as-bytes *fasl-input-stream* sym)
2722 #!+sb-dynamic-core
2723 (let ((offset (read-word-arg))
2724 (value (dyncore-note-symbol sym t)))
2725 (do-cold-fixup code-object offset value kind)
2726 code-object)
2727 #!-sb-dynamic-core
2728 (progn
2729 (maphash (lambda (k v)
2730 (format *error-output* "~&~S = #X~8X~%" k v))
2731 *cold-foreign-symbol-table*)
2732 (error "shared foreign symbol in cold load: ~S (~S)" sym kind))))
2734 (define-cold-fop (fop-assembler-code)
2735 (let* ((length (read-word-arg))
2736 (header-n-words
2737 ;; Note: we round the number of constants up to ensure that
2738 ;; the code vector will be properly aligned.
2739 (round-up sb!vm:code-constants-offset 2))
2740 (des (allocate-cold-descriptor *read-only*
2741 (+ (ash header-n-words
2742 sb!vm:word-shift)
2743 length)
2744 sb!vm:other-pointer-lowtag)))
2745 (write-header-word des header-n-words sb!vm:code-header-widetag)
2746 (write-wordindexed des
2747 sb!vm:code-code-size-slot
2748 (make-fixnum-descriptor length))
2749 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2750 (write-wordindexed des sb!vm:code-debug-info-slot *nil-descriptor*)
2752 (let* ((start (+ (descriptor-byte-offset des)
2753 (ash header-n-words sb!vm:word-shift)))
2754 (end (+ start length)))
2755 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2756 *fasl-input-stream*
2757 :start start
2758 :end end))
2759 des))
2761 (define-cold-fop (fop-assembler-routine)
2762 (let* ((routine (pop-stack))
2763 (des (pop-stack))
2764 (offset (calc-offset des (read-word-arg))))
2765 (record-cold-assembler-routine
2766 routine
2767 (+ (logandc2 (descriptor-bits des) sb!vm:lowtag-mask) offset))
2768 des))
2770 (define-cold-fop (fop-assembler-fixup)
2771 (let* ((routine (pop-stack))
2772 (kind (pop-stack))
2773 (code-object (pop-stack))
2774 (offset (read-word-arg)))
2775 (record-cold-assembler-fixup routine code-object offset kind)
2776 code-object))
2778 (define-cold-fop (fop-code-object-fixup)
2779 (let* ((kind (pop-stack))
2780 (code-object (pop-stack))
2781 (offset (read-word-arg))
2782 (value (descriptor-bits code-object)))
2783 (do-cold-fixup code-object offset value kind)
2784 code-object))
2786 ;;;; sanity checking space layouts
2788 (defun check-spaces ()
2789 ;;; Co-opt type machinery to check for intersections...
2790 (let (types)
2791 (flet ((check (start end space)
2792 (unless (< start end)
2793 (error "Bogus space: ~A" space))
2794 (let ((type (specifier-type `(integer ,start ,end))))
2795 (dolist (other types)
2796 (unless (eq *empty-type* (type-intersection (cdr other) type))
2797 (error "Space overlap: ~A with ~A" space (car other))))
2798 (push (cons space type) types))))
2799 (check sb!vm:read-only-space-start sb!vm:read-only-space-end :read-only)
2800 (check sb!vm:static-space-start sb!vm:static-space-end :static)
2801 #!+gencgc
2802 (check sb!vm:dynamic-space-start sb!vm:dynamic-space-end :dynamic)
2803 #!-gencgc
2804 (progn
2805 (check sb!vm:dynamic-0-space-start sb!vm:dynamic-0-space-end :dynamic-0)
2806 (check sb!vm:dynamic-1-space-start sb!vm:dynamic-1-space-end :dynamic-1))
2807 #!+linkage-table
2808 (check sb!vm:linkage-table-space-start sb!vm:linkage-table-space-end :linkage-table))))
2810 ;;;; emitting C header file
2812 (defun tailwise-equal (string tail)
2813 (and (>= (length string) (length tail))
2814 (string= string tail :start1 (- (length string) (length tail)))))
2816 (defun write-boilerplate ()
2817 (format t "/*~%")
2818 (dolist (line
2819 '("This is a machine-generated file. Please do not edit it by hand."
2820 "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
2822 "This file contains low-level information about the"
2823 "internals of a particular version and configuration"
2824 "of SBCL. It is used by the C compiler to create a runtime"
2825 "support environment, an executable program in the host"
2826 "operating system's native format, which can then be used to"
2827 "load and run 'core' files, which are basically programs"
2828 "in SBCL's own format."))
2829 (format t " *~@[ ~A~]~%" line))
2830 (format t " */~%"))
2832 (defun c-name (string &optional strip)
2833 (delete #\+
2834 (substitute-if #\_ (lambda (c) (member c '(#\- #\/ #\%)))
2835 (remove-if (lambda (c) (position c strip))
2836 string))))
2838 (defun c-symbol-name (symbol &optional strip)
2839 (c-name (symbol-name symbol) strip))
2841 (defun write-makefile-features ()
2842 ;; propagating *SHEBANG-FEATURES* into the Makefiles
2843 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2844 sb-cold:*shebang-features*)
2845 #'string<))
2846 (format t "LISP_FEATURE_~A=1~%" shebang-feature-name)))
2848 (defun write-config-h ()
2849 ;; propagating *SHEBANG-FEATURES* into C-level #define's
2850 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2851 sb-cold:*shebang-features*)
2852 #'string<))
2853 (format t "#define LISP_FEATURE_~A~%" shebang-feature-name))
2854 (terpri)
2855 ;; and miscellaneous constants
2856 (format t "#define SBCL_VERSION_STRING ~S~%"
2857 (sb!xc:lisp-implementation-version))
2858 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
2859 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2860 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
2861 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2862 (format t "#define LISPOBJ(thing) thing~2%")
2863 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")
2864 (terpri))
2866 (defun write-constants-h ()
2867 ;; writing entire families of named constants
2868 (let ((constants nil))
2869 (dolist (package-name '( ;; Even in CMU CL, constants from VM
2870 ;; were automatically propagated
2871 ;; into the runtime.
2872 "SB!VM"
2873 ;; In SBCL, we also propagate various
2874 ;; magic numbers related to file format,
2875 ;; which live here instead of SB!VM.
2876 "SB!FASL"))
2877 (do-external-symbols (symbol (find-package package-name))
2878 (when (constantp symbol)
2879 (let ((name (symbol-name symbol)))
2880 (labels ( ;; shared machinery
2881 (record (string priority suffix)
2882 (push (list string
2883 priority
2884 (symbol-value symbol)
2885 suffix
2886 (documentation symbol 'variable))
2887 constants))
2888 ;; machinery for old-style CMU CL Lisp-to-C
2889 ;; arbitrary renaming, being phased out in favor of
2890 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
2891 ;; renaming
2892 (record-with-munged-name (prefix string priority)
2893 (record (concatenate
2894 'simple-string
2895 prefix
2896 (delete #\- (string-capitalize string)))
2897 priority
2898 ""))
2899 (maybe-record-with-munged-name (tail prefix priority)
2900 (when (tailwise-equal name tail)
2901 (record-with-munged-name prefix
2902 (subseq name 0
2903 (- (length name)
2904 (length tail)))
2905 priority)))
2906 ;; machinery for new-style SBCL Lisp-to-C naming
2907 (record-with-translated-name (priority large)
2908 (record (c-name name) priority
2909 (if large
2910 #!+(and win32 x86-64) "LLU"
2911 #!-(and win32 x86-64) "LU"
2912 "")))
2913 (maybe-record-with-translated-name (suffixes priority &key large)
2914 (when (some (lambda (suffix)
2915 (tailwise-equal name suffix))
2916 suffixes)
2917 (record-with-translated-name priority large))))
2918 (maybe-record-with-translated-name '("-LOWTAG") 0)
2919 (maybe-record-with-translated-name '("-WIDETAG" "-SHIFT") 1)
2920 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
2921 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
2922 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
2923 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
2924 (maybe-record-with-translated-name '("-SIZE" "-INTERRUPTS") 6)
2925 (maybe-record-with-translated-name '("-START" "-END" "-PAGE-BYTES"
2926 "-CARD-BYTES" "-GRANULARITY")
2927 7 :large t)
2928 (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 8)
2929 (maybe-record-with-translated-name '("-CORE-SPACE-ID") 9)
2930 (maybe-record-with-translated-name '("-CORE-SPACE-ID-FLAG") 9)
2931 (maybe-record-with-translated-name '("-GENERATION+") 10))))))
2932 ;; KLUDGE: these constants are sort of important, but there's no
2933 ;; pleasing way to inform the code above about them. So we fake
2934 ;; it for now. nikodemus on #lisp (2004-08-09) suggested simply
2935 ;; exporting every numeric constant from SB!VM; that would work,
2936 ;; but the C runtime would have to be altered to use Lisp-like names
2937 ;; rather than the munged names currently exported. --njf, 2004-08-09
2938 (dolist (c '(sb!vm:n-word-bits sb!vm:n-word-bytes
2939 sb!vm:n-lowtag-bits sb!vm:lowtag-mask
2940 sb!vm:n-widetag-bits sb!vm:widetag-mask
2941 sb!vm:n-fixnum-tag-bits sb!vm:fixnum-tag-mask))
2942 (push (list (c-symbol-name c)
2943 -1 ; invent a new priority
2944 (symbol-value c)
2946 nil)
2947 constants))
2948 ;; One more symbol that doesn't fit into the code above.
2949 (let ((c 'sb!impl::+magic-hash-vector-value+))
2950 (push (list (c-symbol-name c)
2952 (symbol-value c)
2953 #!+(and win32 x86-64) "LLU"
2954 #!-(and win32 x86-64) "LU"
2955 nil)
2956 constants))
2957 (setf constants
2958 (sort constants
2959 (lambda (const1 const2)
2960 (if (= (second const1) (second const2))
2961 (if (= (third const1) (third const2))
2962 (string< (first const1) (first const2))
2963 (< (third const1) (third const2)))
2964 (< (second const1) (second const2))))))
2965 (let ((prev-priority (second (car constants))))
2966 (dolist (const constants)
2967 (destructuring-bind (name priority value suffix doc) const
2968 (unless (= prev-priority priority)
2969 (terpri)
2970 (setf prev-priority priority))
2971 (when (minusp value)
2972 (error "stub: negative values unsupported"))
2973 (format t "#define ~A ~A~A /* 0x~X ~@[ -- ~A ~]*/~%" name value suffix value doc))))
2974 (terpri))
2976 ;; writing information about internal errors
2977 ;; Assembly code needs only the constants for UNDEFINED_[ALIEN_]FUN_ERROR
2978 ;; but to avoid imparting that knowledge here, we'll expose all error
2979 ;; number constants except for OBJECT-NOT-<x>-ERROR ones.
2980 (loop for interr across sb!c:*backend-internal-errors*
2981 for i from 0
2982 when (stringp (car interr))
2983 do (format t "#define ~A ~D~%" (c-symbol-name (cdr interr)) i))
2984 ;; C code needs strings for describe_internal_error()
2985 (format t "#define INTERNAL_ERROR_NAMES ~{\\~%~S~^, ~}~2%"
2986 (map 'list 'sb!kernel::!c-stringify-internal-error
2987 sb!c:*backend-internal-errors*))
2989 ;; I'm not really sure why this is in SB!C, since it seems
2990 ;; conceptually like something that belongs to SB!VM. In any case,
2991 ;; it's needed C-side.
2992 (format t "#define BACKEND_PAGE_BYTES ~DLU~%" sb!c:*backend-page-bytes*)
2994 (terpri)
2996 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
2997 ;; platforms. If we export this from the SB!VM package, it gets
2998 ;; written out as #define trap_PseudoAtomic, which is confusing as
2999 ;; the runtime treats trap_ as the prefix for illegal instruction
3000 ;; type things. We therefore don't export it, but instead do
3001 #!+sparc
3002 (when (boundp 'sb!vm::pseudo-atomic-trap)
3003 (format t
3004 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
3005 sb!vm::pseudo-atomic-trap)
3006 (terpri))
3007 ;; possibly this is another candidate for a rename (to
3008 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
3009 ;; [possibly applicable to other platforms])
3011 #!+sb-safepoint
3012 (format t "#define GC_SAFEPOINT_PAGE_ADDR ((void*)0x~XUL) /* ~:*~A */~%"
3013 sb!vm:gc-safepoint-page-addr)
3015 (dolist (symbol '(sb!vm::float-traps-byte
3016 sb!vm::float-exceptions-byte
3017 sb!vm::float-sticky-bits
3018 sb!vm::float-rounding-mode))
3019 (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
3020 (c-symbol-name symbol)
3021 (sb!xc:byte-position (symbol-value symbol)))
3022 (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
3023 (c-symbol-name symbol)
3024 (sb!xc:mask-field (symbol-value symbol) -1))))
3026 #!+sb-ldb
3027 (defun write-tagnames-h (&optional (out *standard-output*))
3028 (labels
3029 ((pretty-name (symbol strip)
3030 (let ((name (string-downcase symbol)))
3031 (substitute #\Space #\-
3032 (subseq name 0 (- (length name) (length strip))))))
3033 (list-sorted-tags (tail)
3034 (loop for symbol being the external-symbols of "SB!VM"
3035 when (and (constantp symbol)
3036 (tailwise-equal (string symbol) tail))
3037 collect symbol into tags
3038 finally (return (sort tags #'< :key #'symbol-value))))
3039 (write-tags (kind limit ash-count)
3040 (format out "~%static const char *~(~A~)_names[] = {~%"
3041 (subseq kind 1))
3042 (let ((tags (list-sorted-tags kind)))
3043 (dotimes (i limit)
3044 (if (eql i (ash (or (symbol-value (first tags)) -1) ash-count))
3045 (format out " \"~A\"" (pretty-name (pop tags) kind))
3046 (format out " \"unknown [~D]\"" i))
3047 (unless (eql i (1- limit))
3048 (write-string "," out))
3049 (terpri out)))
3050 (write-line "};" out)))
3051 (write-tags "-LOWTAG" sb!vm:lowtag-limit 0)
3052 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
3053 ;; ending with the same 2 bits. (#b10)
3054 (write-tags "-WIDETAG" (ash (1+ sb!vm:widetag-mask) -2) -2))
3055 ;; Inform print_otherptr() of all array types that it's too dumb to print
3056 (let ((array-type-bits (make-array 32 :initial-element 0)))
3057 (flet ((toggle (b)
3058 (multiple-value-bind (ofs bit) (floor b 8)
3059 (setf (aref array-type-bits ofs) (ash 1 bit)))))
3060 (dovector (saetp sb!vm:*specialized-array-element-type-properties*)
3061 (unless (or (typep (sb!vm:saetp-ctype saetp) 'character-set-type)
3062 (eq (sb!vm:saetp-specifier saetp) t))
3063 (toggle (sb!vm:saetp-typecode saetp))
3064 (awhen (sb!vm:saetp-complex-typecode saetp) (toggle it)))))
3065 (format out
3066 "~%static unsigned char unprintable_array_types[32] = ~% {~{~d~^,~}};~%"
3067 (coerce array-type-bits 'list)))
3068 (values))
3070 (defun write-primitive-object (obj)
3071 ;; writing primitive object layouts
3072 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3073 (format t
3074 "struct ~A {~%"
3075 (c-name (string-downcase (string (sb!vm:primitive-object-name obj)))))
3076 (when (sb!vm:primitive-object-widetag obj)
3077 (format t " lispobj header;~%"))
3078 (dolist (slot (sb!vm:primitive-object-slots obj))
3079 (format t " ~A ~A~@[[1]~];~%"
3080 (getf (sb!vm:slot-options slot) :c-type "lispobj")
3081 (c-name (string-downcase (string (sb!vm:slot-name slot))))
3082 (sb!vm:slot-rest-p slot)))
3083 (format t "};~2%")
3084 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
3085 (format t "/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
3086 (format t " * so they work directly on tagged addresses. */~2%")
3087 (let ((name (sb!vm:primitive-object-name obj))
3088 (lowtag (or (symbol-value (sb!vm:primitive-object-lowtag obj))
3089 0)))
3090 (dolist (slot (sb!vm:primitive-object-slots obj))
3091 (format t "#define ~A_~A_OFFSET ~D~%"
3092 (c-symbol-name name)
3093 (c-symbol-name (sb!vm:slot-name slot))
3094 (- (* (sb!vm:slot-offset slot) sb!vm:n-word-bytes) lowtag)))
3095 (terpri))
3096 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%"))
3098 (defun write-structure-object (dd)
3099 (flet ((cstring (designator)
3100 (c-name (string-downcase (string designator)))))
3101 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3102 (format t "struct ~A {~%" (cstring (dd-name dd)))
3103 (format t " lispobj header;~%")
3104 ;; "self layout" slots are named '_layout' instead of 'layout' so that
3105 ;; classoid's expressly declared layout isn't renamed as a special-case.
3106 (format t " lispobj _layout;~%")
3107 #!-interleaved-raw-slots
3108 (progn
3109 ;; Note: if the structure has no raw slots, but has an even number of
3110 ;; ordinary slots (incl. layout, sans header), then the last slot gets
3111 ;; named 'raw_slot_paddingN' (not 'paddingN')
3112 ;; The choice of name is mildly disturbing, but harmless.
3113 (dolist (slot (dd-slots dd))
3114 (when (eq t (dsd-raw-type slot))
3115 (format t " lispobj ~A;~%" (cstring (dsd-name slot)))))
3116 (unless (oddp (+ (dd-length dd) (dd-raw-length dd)))
3117 (format t " lispobj raw_slot_padding;~%"))
3118 (dotimes (n (dd-raw-length dd))
3119 (format t " lispobj raw~D;~%" (- (dd-raw-length dd) n 1))))
3120 #!+interleaved-raw-slots
3121 (let ((index 1))
3122 (dolist (slot (dd-slots dd))
3123 (cond ((eq t (dsd-raw-type slot))
3124 (loop while (< index (dsd-index slot))
3126 (format t " lispobj raw_slot_padding~A;~%" index)
3127 (incf index))
3128 (format t " lispobj ~A;~%" (cstring (dsd-name slot)))
3129 (incf index))))
3130 (unless (oddp (dd-length dd))
3131 (format t " lispobj end_padding;~%")))
3132 (format t "};~2%")
3133 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")))
3135 (defun write-static-symbols ()
3136 (dolist (symbol (cons nil sb!vm:*static-symbols*))
3137 ;; FIXME: It would be nice to use longer names than NIL and
3138 ;; (particularly) T in #define statements.
3139 (format t "#define ~A LISPOBJ(0x~X)~%"
3140 ;; FIXME: It would be nice not to need to strip anything
3141 ;; that doesn't get stripped always by C-SYMBOL-NAME.
3142 (c-symbol-name symbol "%*.!")
3143 (if *static* ; if we ran GENESIS
3144 ;; We actually ran GENESIS, use the real value.
3145 (descriptor-bits (cold-intern symbol))
3146 ;; We didn't run GENESIS, so guess at the address.
3147 (+ sb!vm:static-space-start
3148 sb!vm:n-word-bytes
3149 sb!vm:other-pointer-lowtag
3150 (if symbol (sb!vm:static-symbol-offset symbol) 0))))))
3153 ;;;; writing map file
3155 ;;; Write a map file describing the cold load. Some of this
3156 ;;; information is subject to change due to relocating GC, but even so
3157 ;;; it can be very handy when attempting to troubleshoot the early
3158 ;;; stages of cold load.
3159 (defun write-map ()
3160 (let ((*print-pretty* nil)
3161 (*print-case* :upcase))
3162 (format t "assembler routines defined in core image:~2%")
3163 (dolist (routine (sort (copy-list *cold-assembler-routines*) #'<
3164 :key #'cdr))
3165 (format t "#X~8,'0X: ~S~%" (cdr routine) (car routine)))
3166 (let ((funs nil)
3167 (undefs nil))
3168 (maphash (lambda (name fdefn)
3169 (let ((fun (read-wordindexed fdefn
3170 sb!vm:fdefn-fun-slot)))
3171 (if (= (descriptor-bits fun)
3172 (descriptor-bits *nil-descriptor*))
3173 (push name undefs)
3174 (let ((addr (read-wordindexed
3175 fdefn sb!vm:fdefn-raw-addr-slot)))
3176 (push (cons name (descriptor-bits addr))
3177 funs)))))
3178 *cold-fdefn-objects*)
3179 (format t "~%~|~%initially defined functions:~2%")
3180 (setf funs (sort funs #'< :key #'cdr))
3181 (dolist (info funs)
3182 (format t "0x~8,'0X: ~S #X~8,'0X~%" (cdr info) (car info)
3183 (- (cdr info) #x17)))
3184 (format t
3185 "~%~|
3186 (a note about initially undefined function references: These functions
3187 are referred to by code which is installed by GENESIS, but they are not
3188 installed by GENESIS. This is not necessarily a problem; functions can
3189 be defined later, by cold init toplevel forms, or in files compiled and
3190 loaded at warm init, or elsewhere. As long as they are defined before
3191 they are called, everything should be OK. Things are also OK if the
3192 cross-compiler knew their inline definition and used that everywhere
3193 that they were called before the out-of-line definition is installed,
3194 as is fairly common for structure accessors.)
3195 initially undefined function references:~2%")
3197 (setf undefs (sort undefs #'string< :key #'fun-name-block-name))
3198 (dolist (name undefs)
3199 (format t "~8,'0X: ~S~%"
3200 (descriptor-bits (gethash name *cold-fdefn-objects*))
3201 name)))
3203 (format t "~%~|~%layout names:~2%")
3204 (dolist (x (sort (%hash-table-alist *cold-layouts*) #'<
3205 :key (lambda (x) (descriptor-bits (cdr x)))))
3206 (let* ((des (cdr x))
3207 (inherits (read-slot des (find-layout 'layout) :inherits)))
3208 (format t "~8,'0X: ~S[~D]~%~10T~:S~%" (descriptor-bits des) (car x)
3209 (cold-layout-length des) (listify-cold-inherits inherits)))))
3211 (values))
3213 ;;;; writing core file
3215 (defvar *core-file*)
3216 (defvar *data-page*)
3218 ;;; magic numbers to identify entries in a core file
3220 ;;; (In case you were wondering: No, AFAIK there's no special magic about
3221 ;;; these which requires them to be in the 38xx range. They're just
3222 ;;; arbitrary words, tested not for being in a particular range but just
3223 ;;; for equality. However, if you ever need to look at a .core file and
3224 ;;; figure out what's going on, it's slightly convenient that they're
3225 ;;; all in an easily recognizable range, and displacing the range away from
3226 ;;; zero seems likely to reduce the chance that random garbage will be
3227 ;;; misinterpreted as a .core file.)
3228 (defconstant build-id-core-entry-type-code 3860)
3229 (defconstant new-directory-core-entry-type-code 3861)
3230 (defconstant initial-fun-core-entry-type-code 3863)
3231 (defconstant page-table-core-entry-type-code 3880)
3232 (defconstant end-core-entry-type-code 3840)
3234 (declaim (ftype (function (sb!vm:word) sb!vm:word) write-word))
3235 (defun write-word (num)
3236 (ecase sb!c:*backend-byte-order*
3237 (:little-endian
3238 (dotimes (i sb!vm:n-word-bytes)
3239 (write-byte (ldb (byte 8 (* i 8)) num) *core-file*)))
3240 (:big-endian
3241 (dotimes (i sb!vm:n-word-bytes)
3242 (write-byte (ldb (byte 8 (* (- (1- sb!vm:n-word-bytes) i) 8)) num)
3243 *core-file*))))
3244 num)
3246 (defun advance-to-page ()
3247 (force-output *core-file*)
3248 (file-position *core-file*
3249 (round-up (file-position *core-file*)
3250 sb!c:*backend-page-bytes*)))
3252 (defun output-gspace (gspace)
3253 (force-output *core-file*)
3254 (let* ((posn (file-position *core-file*))
3255 (bytes (* (gspace-free-word-index gspace) sb!vm:n-word-bytes))
3256 (pages (ceiling bytes sb!c:*backend-page-bytes*))
3257 (total-bytes (* pages sb!c:*backend-page-bytes*)))
3259 (file-position *core-file*
3260 (* sb!c:*backend-page-bytes* (1+ *data-page*)))
3261 (format t
3262 "writing ~S byte~:P [~S page~:P] from ~S~%"
3263 total-bytes
3264 pages
3265 gspace)
3266 (force-output)
3268 ;; Note: It is assumed that the GSPACE allocation routines always
3269 ;; allocate whole pages (of size *target-page-size*) and that any
3270 ;; empty gspace between the free pointer and the end of page will
3271 ;; be zero-filled. This will always be true under Mach on machines
3272 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3273 ;; 8K).
3274 (write-bigvec-as-sequence (gspace-bytes gspace)
3275 *core-file*
3276 :end total-bytes
3277 :pad-with-zeros t)
3278 (force-output *core-file*)
3279 (file-position *core-file* posn)
3281 ;; Write part of a (new) directory entry which looks like this:
3282 ;; GSPACE IDENTIFIER
3283 ;; WORD COUNT
3284 ;; DATA PAGE
3285 ;; ADDRESS
3286 ;; PAGE COUNT
3287 (write-word (gspace-identifier gspace))
3288 (write-word (gspace-free-word-index gspace))
3289 (write-word *data-page*)
3290 (multiple-value-bind (floor rem)
3291 (floor (gspace-byte-address gspace) sb!c:*backend-page-bytes*)
3292 (aver (zerop rem))
3293 (write-word floor))
3294 (write-word pages)
3296 (incf *data-page* pages)))
3298 ;;; Create a core file created from the cold loaded image. (This is
3299 ;;; the "initial core file" because core files could be created later
3300 ;;; by executing SAVE-LISP in a running system, perhaps after we've
3301 ;;; added some functionality to the system.)
3302 (declaim (ftype (function (string)) write-initial-core-file))
3303 (defun write-initial-core-file (filename)
3305 (let ((filenamestring (namestring filename))
3306 (*data-page* 0))
3308 (format t
3309 "[building initial core file in ~S: ~%"
3310 filenamestring)
3311 (force-output)
3313 (with-open-file (*core-file* filenamestring
3314 :direction :output
3315 :element-type '(unsigned-byte 8)
3316 :if-exists :rename-and-delete)
3318 ;; Write the magic number.
3319 (write-word core-magic)
3321 ;; Write the build ID.
3322 (write-word build-id-core-entry-type-code)
3323 (let ((build-id (with-open-file (s "output/build-id.tmp")
3324 (read s))))
3325 (declare (type simple-string build-id))
3326 (/show build-id (length build-id))
3327 ;; Write length of build ID record: BUILD-ID-CORE-ENTRY-TYPE-CODE
3328 ;; word, this length word, and one word for each char of BUILD-ID.
3329 (write-word (+ 2 (length build-id)))
3330 (dovector (char build-id)
3331 ;; (We write each character as a word in order to avoid
3332 ;; having to think about word alignment issues in the
3333 ;; sbcl-0.7.8 version of coreparse.c.)
3334 (write-word (sb!xc:char-code char))))
3336 ;; Write the New Directory entry header.
3337 (write-word new-directory-core-entry-type-code)
3338 (write-word 17) ; length = (5 words/space) * 3 spaces + 2 for header.
3340 (output-gspace *read-only*)
3341 (output-gspace *static*)
3342 (output-gspace *dynamic*)
3344 ;; Write the initial function.
3345 (write-word initial-fun-core-entry-type-code)
3346 (write-word 3)
3347 (let* ((cold-name (cold-intern '!cold-init))
3348 (cold-fdefn (cold-fdefinition-object cold-name))
3349 (initial-fun (read-wordindexed cold-fdefn
3350 sb!vm:fdefn-fun-slot)))
3351 (format t
3352 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
3353 (descriptor-bits initial-fun))
3354 (write-word (descriptor-bits initial-fun)))
3356 ;; Write the End entry.
3357 (write-word end-core-entry-type-code)
3358 (write-word 2)))
3360 (format t "done]~%")
3361 (force-output)
3362 (/show "leaving WRITE-INITIAL-CORE-FILE")
3363 (values))
3365 ;;;; the actual GENESIS function
3367 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3368 ;;; and/or information about a Lisp core, therefrom.
3370 ;;; input file arguments:
3371 ;;; SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
3372 ;;; *tab* *characters* *converted* *to* *spaces*. (We push
3373 ;;; responsibility for removing tabs out to the caller it's
3374 ;;; trivial to remove them using UNIX command line tools like
3375 ;;; sed, whereas it's a headache to do it portably in Lisp because
3376 ;;; #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
3377 ;;; a core file cannot be built (but a C header file can be).
3379 ;;; output files arguments (any of which may be NIL to suppress output):
3380 ;;; CORE-FILE-NAME gets a Lisp core.
3381 ;;; C-HEADER-FILE-NAME gets a C header file, traditionally called
3382 ;;; internals.h, which is used by the C compiler when constructing
3383 ;;; the executable which will load the core.
3384 ;;; MAP-FILE-NAME gets (?) a map file. (dunno about this -- WHN 19990815)
3386 ;;; FIXME: GENESIS doesn't belong in SB!VM. Perhaps in %KERNEL for now,
3387 ;;; perhaps eventually in SB-LD or SB-BOOT.
3388 (defun sb!vm:genesis (&key
3389 object-file-names
3390 symbol-table-file-name
3391 core-file-name
3392 map-file-name
3393 c-header-dir-name
3394 #+nil (list-objects t))
3395 #!+sb-dynamic-core
3396 (declare (ignorable symbol-table-file-name))
3398 (format t
3399 "~&beginning GENESIS, ~A~%"
3400 (if core-file-name
3401 ;; Note: This output summarizing what we're doing is
3402 ;; somewhat telegraphic in style, not meant to imply that
3403 ;; we're not e.g. also creating a header file when we
3404 ;; create a core.
3405 (format nil "creating core ~S" core-file-name)
3406 (format nil "creating headers in ~S" c-header-dir-name)))
3408 (let ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
3410 #!-sb-dynamic-core
3411 (when core-file-name
3412 (if symbol-table-file-name
3413 (load-cold-foreign-symbol-table symbol-table-file-name)
3414 (error "can't output a core file without symbol table file input")))
3416 #!+sb-dynamic-core
3417 (progn
3418 (setf (gethash (extern-alien-name "undefined_tramp")
3419 *cold-foreign-symbol-table*)
3420 (dyncore-note-symbol "undefined_tramp" nil))
3421 (dyncore-note-symbol "undefined_alien_function" nil))
3423 ;; Now that we've successfully read our only input file (by
3424 ;; loading the symbol table, if any), it's a good time to ensure
3425 ;; that there'll be someplace for our output files to go when
3426 ;; we're done.
3427 (flet ((frob (filename)
3428 (when filename
3429 (ensure-directories-exist filename :verbose t))))
3430 (frob core-file-name)
3431 (frob map-file-name))
3433 ;; (This shouldn't matter in normal use, since GENESIS normally
3434 ;; only runs once in any given Lisp image, but it could reduce
3435 ;; confusion if we ever experiment with running, tweaking, and
3436 ;; rerunning genesis interactively.)
3437 (do-all-symbols (sym)
3438 (remprop sym 'cold-intern-info))
3440 (check-spaces)
3442 (let* ((*foreign-symbol-placeholder-value* (if core-file-name nil 0))
3443 (*load-time-value-counter* 0)
3444 (*cold-fdefn-objects* (make-hash-table :test 'equal))
3445 (*cold-symbols* (make-hash-table :test 'eql)) ; integer keys
3446 (*cold-package-symbols* (make-hash-table :test 'equal)) ; string keys
3447 (pkg-metadata (sb-cold:read-from-file "package-data-list.lisp-expr"))
3448 (*read-only* (make-gspace :read-only
3449 read-only-core-space-id
3450 sb!vm:read-only-space-start))
3451 (*static* (make-gspace :static
3452 static-core-space-id
3453 sb!vm:static-space-start))
3454 (*dynamic* (make-gspace :dynamic
3455 dynamic-core-space-id
3456 #!+gencgc sb!vm:dynamic-space-start
3457 #!-gencgc sb!vm:dynamic-0-space-start))
3458 ;; There's a cyclic dependency here: NIL refers to a package;
3459 ;; a package needs its layout which needs others layouts
3460 ;; which refer to NIL, which refers to a package ...
3461 ;; Break the cycle by preallocating packages without a layout.
3462 ;; This avoids having to track any symbols created prior to
3463 ;; creation of packages, since packages are primordial.
3464 (target-cl-pkg-info
3465 (dolist (name (list* "COMMON-LISP" "COMMON-LISP-USER" "KEYWORD"
3466 (mapcar #'sb-cold:package-data-name
3467 pkg-metadata))
3468 (gethash "COMMON-LISP" *cold-package-symbols*))
3469 (setf (gethash name *cold-package-symbols*)
3470 (cons (allocate-struct
3471 *dynamic* (make-fixnum-descriptor 0)
3472 (layout-length (find-layout 'package)))
3473 (cons nil nil))))) ; (externals . internals)
3474 (*nil-descriptor* (make-nil-descriptor target-cl-pkg-info))
3475 (*current-reversed-cold-toplevels* *nil-descriptor*)
3476 (*current-debug-sources* *nil-descriptor*)
3477 (*unbound-marker* (make-other-immediate-descriptor
3479 sb!vm:unbound-marker-widetag))
3480 *cold-assembler-fixups*
3481 *cold-assembler-routines*
3482 #!+x86 (*load-time-code-fixups* (make-hash-table)))
3484 ;; Prepare for cold load.
3485 (initialize-non-nil-symbols)
3486 (initialize-layouts)
3487 (initialize-packages
3488 ;; docstrings are set in src/cold/warm. It would work to do it here,
3489 ;; but seems preferable not to saddle Genesis with such responsibility.
3490 (list* (sb-cold:make-package-data :name "COMMON-LISP" :doc nil)
3491 (sb-cold:make-package-data :name "KEYWORD" :doc nil)
3492 (sb-cold:make-package-data :name "COMMON-LISP-USER" :doc nil
3493 :use '("COMMON-LISP"
3494 ;; ANSI encourages us to put extension packages
3495 ;; in the USE list of COMMON-LISP-USER.
3496 "SB!ALIEN" "SB!DEBUG" "SB!EXT" "SB!GRAY" "SB!PROFILE"))
3497 pkg-metadata))
3498 (initialize-static-fns)
3500 ;; Initialize the *COLD-SYMBOLS* system with the information
3501 ;; from common-lisp-exports.lisp-expr.
3502 ;; Packages whose names match SB!THING were set up on the host according
3503 ;; to "package-data-list.lisp-expr" which expresses the desired target
3504 ;; package configuration, so we can just mirror the host into the target.
3505 ;; But by waiting to observe calls to COLD-INTERN that occur during the
3506 ;; loading of the cross-compiler's outputs, it is possible to rid the
3507 ;; target of accidental leftover symbols, not that it wouldn't also be
3508 ;; a good idea to clean up package-data-list once in a while.
3509 (dolist (exported-name
3510 (sb-cold:read-from-file "common-lisp-exports.lisp-expr"))
3511 (cold-intern (intern exported-name *cl-package*) :access :external))
3513 ;; Cold load.
3514 (dolist (file-name object-file-names)
3515 (write-line (namestring file-name))
3516 (cold-load file-name))
3518 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3519 (resolve-assembler-fixups)
3520 #!+x86 (output-load-time-code-fixups)
3521 (foreign-symbols-to-core)
3522 (finish-symbols)
3523 (/show "back from FINISH-SYMBOLS")
3524 (finalize-load-time-value-noise)
3526 ;; Tell the target Lisp how much stuff we've allocated.
3527 (cold-set 'sb!vm:*read-only-space-free-pointer*
3528 (allocate-cold-descriptor *read-only*
3530 sb!vm:even-fixnum-lowtag))
3531 (cold-set 'sb!vm:*static-space-free-pointer*
3532 (allocate-cold-descriptor *static*
3534 sb!vm:even-fixnum-lowtag))
3535 (/show "done setting free pointers")
3537 ;; Write results to files.
3539 ;; FIXME: I dislike this approach of redefining
3540 ;; *STANDARD-OUTPUT* instead of putting the new stream in a
3541 ;; lexical variable, and it's annoying to have WRITE-MAP (to
3542 ;; *STANDARD-OUTPUT*) not be parallel to WRITE-INITIAL-CORE-FILE
3543 ;; (to a stream explicitly passed as an argument).
3544 (macrolet ((out-to (name &body body)
3545 `(let ((fn (format nil "~A/~A.h" c-header-dir-name ,name)))
3546 (ensure-directories-exist fn)
3547 (with-open-file (*standard-output* fn
3548 :if-exists :supersede :direction :output)
3549 (write-boilerplate)
3550 (let ((n (c-name (string-upcase ,name))))
3551 (format
3553 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~A 1~%"
3554 n n))
3555 ,@body
3556 (format t
3557 "#endif /* SBCL_GENESIS_~A */~%"
3558 (string-upcase ,name))))))
3559 (when map-file-name
3560 (with-open-file (*standard-output* map-file-name
3561 :direction :output
3562 :if-exists :supersede)
3563 (write-map)))
3564 (out-to "config" (write-config-h))
3565 (out-to "constants" (write-constants-h))
3566 #!+sb-ldb
3567 (out-to "tagnames" (write-tagnames-h))
3568 (let ((structs (sort (copy-list sb!vm:*primitive-objects*) #'string<
3569 :key (lambda (obj)
3570 (symbol-name
3571 (sb!vm:primitive-object-name obj))))))
3572 (dolist (obj structs)
3573 (out-to
3574 (string-downcase (string (sb!vm:primitive-object-name obj)))
3575 (write-primitive-object obj)))
3576 (out-to "primitive-objects"
3577 (dolist (obj structs)
3578 (format t "~&#include \"~A.h\"~%"
3579 (string-downcase
3580 (string (sb!vm:primitive-object-name obj)))))))
3581 (dolist (class '(hash-table
3582 classoid
3583 layout
3584 sb!c::compiled-debug-info
3585 sb!c::compiled-debug-fun
3586 sb!xc:package))
3587 (out-to
3588 (string-downcase (string class))
3589 (write-structure-object
3590 (layout-info (find-layout class)))))
3591 (out-to "static-symbols" (write-static-symbols))
3593 (let ((fn (format nil "~A/Makefile.features" c-header-dir-name)))
3594 (ensure-directories-exist fn)
3595 (with-open-file (*standard-output* fn :if-exists :supersede
3596 :direction :output)
3597 (write-makefile-features)))
3599 (when core-file-name
3600 (write-initial-core-file core-file-name))))))
3602 ;; This generalization of WARM-FUN-NAME will do in a pinch
3603 ;; to view strings and things in a gspace.
3604 (defun warmelize (descriptor)
3605 (labels ((recurse (x)
3606 (when (cold-null x)
3607 (return-from recurse nil))
3608 (ecase (descriptor-lowtag x)
3609 (#.sb!vm:list-pointer-lowtag
3610 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
3611 (#.sb!vm:fun-pointer-lowtag
3612 (let ((name (read-wordindexed x sb!vm:simple-fun-name-slot)))
3613 `(function ,(recurse name))))
3614 (#.sb!vm:other-pointer-lowtag
3615 (let ((widetag (logand (descriptor-bits (read-memory x))
3616 sb!vm:widetag-mask)))
3617 (ecase widetag
3618 (#.sb!vm:symbol-header-widetag
3619 ;; this is only approximate, as it disregards package
3620 (intern (recurse (read-wordindexed x sb!vm:symbol-name-slot))))
3621 (#.sb!vm:simple-base-string-widetag
3622 (base-string-from-core x))))))))
3623 (recurse descriptor)))