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