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