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