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