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.
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
17 ;;;; (In CMU CL, and in SBCL as of 0.6.9 anyway, functions not defined
18 ;;;; by DEFUN aren't set up specially by GENESIS. In particular,
19 ;;;; structure slot accessors are not set up. Slot accessors are
20 ;;;; available at cold init time because they're usually compiled
21 ;;;; inline. They're not available as out-of-line functions until the
22 ;;;; toplevel forms installing them have run.)
24 ;;;; This software is part of the SBCL system. See the README file for
25 ;;;; more information.
27 ;;;; This software is derived from the CMU CL system, which was
28 ;;;; written at Carnegie Mellon University and released into the
29 ;;;; public domain. The software is in the public domain and is
30 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
31 ;;;; files for more information.
33 (in-package "SB!FASL")
35 ;;; a magic number used to identify our core files
36 (defconstant core-magic
37 (logior (ash (sb!xc:char-code #\S) 24)
38 (ash (sb!xc:char-code #\B) 16)
39 (ash (sb!xc:char-code #\C) 8)
40 (sb!xc:char-code #\L)))
42 ;;; the current version of SBCL core files
44 ;;; FIXME: This is left over from CMU CL, and not well thought out.
45 ;;; It's good to make sure that the runtime doesn't try to run core
46 ;;; files from the wrong version, but a single number is not the ideal
47 ;;; way to do this in high level data like this (as opposed to e.g. in
48 ;;; IP packets), and in fact the CMU CL version number never ended up
49 ;;; being incremented past 0. A better approach might be to use a
50 ;;; string which is set from CVS data. (Though now as of sbcl-0.7.8 or
51 ;;; so, we have another problem that the core incompatibility
52 ;;; detection mechanisms are on such a hair trigger -- with even
53 ;;; different builds from the same sources being considered
54 ;;; incompatible -- that any coarser-grained versioning mechanisms
55 ;;; like this are largely irrelevant as long as the hair-triggering
58 ;;; 0: inherited from CMU CL
59 ;;; 1: rearranged static symbols for sbcl-0.6.8
60 ;;; 2: eliminated non-ANSI %DEFCONSTANT/%%DEFCONSTANT support,
61 ;;; deleted a slot from DEBUG-SOURCE structure
62 ;;; 3: added build ID to cores to discourage sbcl/.core mismatch
63 ;;; 4: added gc page table data
64 (defconstant sbcl-core-version-integer 4)
66 (defun round-up (number size)
68 "Round NUMBER up to be an integral multiple of SIZE."
69 (* size (ceiling number size)))
71 ;;;; implementing the concept of "vector" in (almost) portable
74 ;;;; "If you only need to do such simple things, it doesn't really
75 ;;;; matter which language you use." -- _ANSI Common Lisp_, p. 1, Paul
76 ;;;; Graham (evidently not considering the abstraction "vector" to be
77 ;;;; such a simple thing:-)
79 (eval-when (:compile-toplevel :load-toplevel :execute)
80 (defconstant +smallvec-length+
83 ;;; an element of a BIGVEC -- a vector small enough that we have
84 ;;; a good chance of it being portable to other Common Lisps
86 `(simple-array (unsigned-byte 8) (,+smallvec-length+)))
88 (defun make-smallvec ()
89 (make-array +smallvec-length+ :element-type '(unsigned-byte 8)
92 ;;; a big vector, implemented as a vector of SMALLVECs
94 ;;; KLUDGE: This implementation seems portable enough for our
95 ;;; purposes, since realistically every modern implementation is
96 ;;; likely to support vectors of at least 2^16 elements. But if you're
97 ;;; masochistic enough to read this far into the contortions imposed
98 ;;; on us by ANSI and the Lisp community, for daring to use the
99 ;;; abstraction of a large linearly addressable memory space, which is
100 ;;; after all only directly supported by the underlying hardware of at
101 ;;; least 99% of the general-purpose computers in use today, then you
102 ;;; may be titillated to hear that in fact this code isn't really
103 ;;; portable, because as of sbcl-0.7.4 we need somewhat more than
104 ;;; 16Mbytes to represent a core, and ANSI only guarantees that
105 ;;; ARRAY-DIMENSION-LIMIT is not less than 1024. -- WHN 2002-06-13
107 (outer-vector (vector (make-smallvec)) :type (vector smallvec)))
109 ;;; analogous to SVREF, but into a BIGVEC
110 (defun bvref (bigvec index)
111 (multiple-value-bind (outer-index inner-index)
112 (floor index +smallvec-length+)
114 (svref (bigvec-outer-vector bigvec) outer-index))
116 (defun (setf bvref) (new-value bigvec index)
117 (multiple-value-bind (outer-index inner-index)
118 (floor index +smallvec-length+)
119 (setf (aref (the smallvec
120 (svref (bigvec-outer-vector bigvec) outer-index))
124 ;;; analogous to LENGTH, but for a BIGVEC
126 ;;; the length of BIGVEC, measured in the number of BVREFable bytes it
128 (defun bvlength (bigvec)
129 (* (length (bigvec-outer-vector bigvec))
132 ;;; analogous to WRITE-SEQUENCE, but for a BIGVEC
133 (defun write-bigvec-as-sequence (bigvec stream &key (start 0) end)
134 (loop for i of-type index from start below (or end (bvlength bigvec)) do
135 (write-byte (bvref bigvec i)
138 ;;; analogous to READ-SEQUENCE-OR-DIE, but for a BIGVEC
139 (defun read-bigvec-as-sequence-or-die (bigvec stream &key (start 0) end)
140 (loop for i of-type index from start below (or end (bvlength bigvec)) do
141 (setf (bvref bigvec i)
142 (read-byte stream))))
144 ;;; Grow BIGVEC (exponentially, so that large increases in size have
145 ;;; asymptotic logarithmic cost per byte).
146 (defun expand-bigvec (bigvec)
147 (let* ((old-outer-vector (bigvec-outer-vector bigvec))
148 (length-old-outer-vector (length old-outer-vector))
149 (new-outer-vector (make-array (* 2 length-old-outer-vector))))
150 (dotimes (i length-old-outer-vector)
151 (setf (svref new-outer-vector i)
152 (svref old-outer-vector i)))
153 (loop for i from length-old-outer-vector below (length new-outer-vector) do
154 (setf (svref new-outer-vector i)
156 (setf (bigvec-outer-vector bigvec)
160 ;;;; looking up bytes and multi-byte values in a BIGVEC (considering
161 ;;;; it as an image of machine memory on the cross-compilation target)
163 ;;; BVREF-32 and friends. These are like SAP-REF-n, except that
164 ;;; instead of a SAP we use a BIGVEC.
165 (macrolet ((make-bvref-n
167 (let* ((name (intern (format nil "BVREF-~A" n)))
168 (number-octets (/ n 8))
170 (loop for i from 0 to (1- number-octets)
171 collect `(ash (bvref bigvec (+ byte-index ,i))
174 (loop for i from 0 to (1- number-octets)
175 collect `(ash (bvref bigvec
177 ,(- number-octets 1 i)))
180 (loop for i from 0 to (1- number-octets)
182 `((bvref bigvec (+ byte-index ,i))
183 (ldb (byte 8 ,(* i 8)) new-value))))
185 (loop for i from 0 to (1- number-octets)
187 `((bvref bigvec (+ byte-index ,i))
188 (ldb (byte 8 ,(- n 8 (* i 8))) new-value)))))
190 (defun ,name (bigvec byte-index)
191 (logior ,@(ecase sb!c:*backend-byte-order*
192 (:little-endian ash-list-le)
193 (:big-endian ash-list-be))))
194 (defun (setf ,name) (new-value bigvec byte-index)
195 (setf ,@(ecase sb!c:*backend-byte-order*
196 (:little-endian setf-list-le)
197 (:big-endian setf-list-be))))))))
203 ;; lispobj-sized word, whatever that may be
204 ;; hopefully nobody ever wants a 128-bit SBCL...
205 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
207 (defun bvref-word (bytes index)
208 (bvref-64 bytes index))
209 (defun (setf bvref-word) (new-val bytes index)
210 (setf (bvref-64 bytes index) new-val)))
212 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
214 (defun bvref-word (bytes index)
215 (bvref-32 bytes index))
216 (defun (setf bvref-word) (new-val bytes index)
217 (setf (bvref-32 bytes index) new-val)))
220 ;;;; representation of spaces in the core
222 ;;; If there is more than one dynamic space in memory (i.e., if a
223 ;;; copying GC is in use), then only the active dynamic space gets
226 (defconstant dynamic-core-space-id 1)
229 (defconstant static-core-space-id 2)
232 (defconstant read-only-core-space-id 3)
234 (defconstant max-core-space-id 3)
235 (defconstant deflated-core-space-id-flag 4)
237 (defconstant descriptor-low-bits 16
238 "the number of bits in the low half of the descriptor")
239 (defconstant target-space-alignment (ash 1 descriptor-low-bits)
240 "the alignment requirement for spaces in the target.
241 Must be at least (ASH 1 DESCRIPTOR-LOW-BITS)")
243 ;;; a GENESIS-time representation of a memory space (e.g. read-only
244 ;;; space, dynamic space, or static space)
245 (defstruct (gspace (:constructor %make-gspace)
247 ;; name and identifier for this GSPACE
248 (name (missing-arg) :type symbol :read-only t)
249 (identifier (missing-arg) :type fixnum :read-only t)
250 ;; the word address where the data will be loaded
251 (word-address (missing-arg) :type unsigned-byte :read-only t)
252 ;; the data themselves. (Note that in CMU CL this was a pair of
253 ;; fields SAP and WORDS-ALLOCATED, but that wasn't very portable.)
254 ;; (And then in SBCL this was a VECTOR, but turned out to be
255 ;; unportable too, since ANSI doesn't think that arrays longer than
256 ;; 1024 (!) should needed by portable CL code...)
257 (bytes (make-bigvec) :read-only t)
258 ;; the index of the next unwritten word (i.e. chunk of
259 ;; SB!VM:N-WORD-BYTES bytes) in BYTES, or equivalently the number of
260 ;; words actually written in BYTES. In order to convert to an actual
261 ;; index into BYTES, thus must be multiplied by SB!VM:N-WORD-BYTES.
264 (defun gspace-byte-address (gspace)
265 (ash (gspace-word-address gspace) sb!vm:word-shift))
267 (def!method print-object ((gspace gspace) stream)
268 (print-unreadable-object (gspace stream :type t)
269 (format stream "~S" (gspace-name gspace))))
271 (defun make-gspace (name identifier byte-address)
272 (unless (zerop (rem byte-address target-space-alignment))
273 (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
275 target-space-alignment))
276 (%make-gspace :name name
277 :identifier identifier
278 :word-address (ash byte-address (- sb!vm:word-shift))))
280 ;;;; representation of descriptors
282 (defun is-fixnum-lowtag (lowtag)
283 (zerop (logand lowtag sb!vm:fixnum-tag-mask)))
285 (defun is-other-immediate-lowtag (lowtag)
286 ;; The other-immediate lowtags are similar to the fixnum lowtags, in
287 ;; that they have an "effective length" that is shorter than is used
288 ;; for the pointer lowtags. Unlike the fixnum lowtags, however, the
289 ;; other-immediate lowtags are always effectively two bits wide.
290 (= (logand lowtag 3) sb!vm:other-immediate-0-lowtag))
292 (defstruct (descriptor
293 (:constructor make-descriptor
294 (high low &optional gspace word-offset))
296 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
297 (gspace nil :type (or gspace (eql :load-time-value) null))
298 ;; the offset in words from the start of GSPACE, or NIL if not set yet
299 (word-offset nil :type (or sb!vm:word null))
300 ;; the high and low halves of the descriptor
302 ;; KLUDGE: Judging from the comments in genesis.lisp of the CMU CL
303 ;; old-rt compiler, this split dates back from a very early version
304 ;; of genesis where 32-bit integers were represented as conses of
305 ;; two 16-bit integers. In any system with nice (UNSIGNED-BYTE 32)
306 ;; structure slots, like CMU CL >= 17 or any version of SBCL, there
307 ;; seems to be no reason to persist in this. -- WHN 19990917
310 (def!method print-object ((des descriptor) stream)
311 (let ((lowtag (descriptor-lowtag des)))
312 (print-unreadable-object (des stream :type t)
313 (cond ((is-fixnum-lowtag lowtag)
314 (let ((unsigned (logior (ash (descriptor-high des)
315 (1+ (- descriptor-low-bits
316 sb!vm:n-lowtag-bits)))
317 (ash (descriptor-low des)
318 (- 1 sb!vm:n-lowtag-bits)))))
321 (if (> unsigned #x1FFFFFFF)
322 (- unsigned #x40000000)
324 ((is-other-immediate-lowtag lowtag)
326 "for other immediate: #X~X, type #b~8,'0B"
327 (ash (descriptor-bits des) (- sb!vm:n-widetag-bits))
328 (logand (descriptor-low des) sb!vm:widetag-mask)))
331 "for pointer: #X~X, lowtag #b~3,'0B, ~A"
332 (logior (ash (descriptor-high des) descriptor-low-bits)
333 (logandc2 (descriptor-low des) sb!vm:lowtag-mask))
335 (let ((gspace (descriptor-gspace des)))
340 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
341 ;;; free word index is boosted as necessary, and if additional memory
342 ;;; is needed, we grow the GSPACE. The descriptor returned is a
343 ;;; pointer of type LOWTAG.
344 (defun allocate-cold-descriptor (gspace length lowtag)
345 (let* ((bytes (round-up length (ash 1 sb!vm:n-lowtag-bits)))
346 (old-free-word-index (gspace-free-word-index gspace))
347 (new-free-word-index (+ old-free-word-index
348 (ash bytes (- sb!vm:word-shift)))))
349 ;; Grow GSPACE as necessary until it's big enough to handle
350 ;; NEW-FREE-WORD-INDEX.
352 ((>= (bvlength (gspace-bytes gspace))
353 (* new-free-word-index sb!vm:n-word-bytes)))
354 (expand-bigvec (gspace-bytes gspace)))
355 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
356 (setf (gspace-free-word-index gspace) new-free-word-index)
357 (let ((ptr (+ (gspace-word-address gspace) old-free-word-index)))
358 (make-descriptor (ash ptr (- sb!vm:word-shift descriptor-low-bits))
359 (logior (ash (logand ptr
361 (- descriptor-low-bits
366 old-free-word-index))))
368 (defun descriptor-lowtag (des)
370 "the lowtag bits for DES"
371 (logand (descriptor-low des) sb!vm:lowtag-mask))
373 (defun descriptor-bits (des)
374 (logior (ash (descriptor-high des) descriptor-low-bits)
375 (descriptor-low des)))
377 (defun descriptor-fixnum (des)
378 (let ((bits (descriptor-bits des)))
379 (if (logbitp (1- sb!vm:n-word-bits) bits)
380 ;; KLUDGE: The (- SB!VM:N-WORD-BITS 2) term here looks right to
381 ;; me, and it works, but in CMU CL it was (1- SB!VM:N-WORD-BITS),
382 ;; and although that doesn't make sense for me, or work for me,
383 ;; it's hard to see how it could have been wrong, since CMU CL
384 ;; genesis worked. It would be nice to understand how this came
385 ;; to be.. -- WHN 19990901
386 (logior (ash bits (- sb!vm:n-fixnum-tag-bits))
387 (ash -1 (1+ sb!vm:n-positive-fixnum-bits)))
388 (ash bits (- sb!vm:n-fixnum-tag-bits)))))
390 (defun descriptor-word-sized-integer (des)
391 ;; Extract an (unsigned-byte 32), from either its fixnum or bignum
393 (let ((lowtag (descriptor-lowtag des)))
394 (if (is-fixnum-lowtag lowtag)
395 (make-random-descriptor (descriptor-fixnum des))
396 (read-wordindexed des 1))))
399 (defun descriptor-bytes (des)
400 (gspace-bytes (descriptor-intuit-gspace des)))
401 (defun descriptor-byte-offset (des)
402 (ash (descriptor-word-offset des) sb!vm:word-shift))
404 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
405 ;;; figure out a GSPACE which corresponds to DES, set it into
406 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
407 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
408 (declaim (ftype (function (descriptor) gspace) descriptor-intuit-gspace))
409 (defun descriptor-intuit-gspace (des)
410 (or (descriptor-gspace des)
412 ;; gspace wasn't set, now we have to search for it.
413 (let ((lowtag (descriptor-lowtag des))
414 (high (descriptor-high des))
415 (low (descriptor-low des)))
417 ;; Non-pointer objects don't have a gspace.
418 (unless (or (eql lowtag sb!vm:fun-pointer-lowtag)
419 (eql lowtag sb!vm:instance-pointer-lowtag)
420 (eql lowtag sb!vm:list-pointer-lowtag)
421 (eql lowtag sb!vm:other-pointer-lowtag))
422 (error "don't even know how to look for a GSPACE for ~S" des))
424 (dolist (gspace (list *dynamic* *static* *read-only*)
425 (error "couldn't find a GSPACE for ~S" des))
426 ;; Bounds-check the descriptor against the allocated area
427 ;; within each gspace.
429 ;; Most of the faffing around in here involving ash and
430 ;; various computed shift counts is due to the high/low
431 ;; split representation of the descriptor bits and an
432 ;; apparent disinclination to create intermediate values
433 ;; larger than a target fixnum.
435 ;; This code relies on the fact that GSPACEs are aligned
436 ;; such that the descriptor-low-bits low bits are zero.
437 (when (and (>= high (ash (gspace-word-address gspace)
438 (- sb!vm:word-shift descriptor-low-bits)))
439 (<= high (ash (+ (gspace-word-address gspace)
440 (gspace-free-word-index gspace))
441 (- sb!vm:word-shift descriptor-low-bits))))
442 ;; Update the descriptor with the correct gspace and the
443 ;; offset within the gspace and return the gspace.
444 (setf (descriptor-gspace des) gspace)
445 (setf (descriptor-word-offset des)
446 (+ (ash (- high (ash (gspace-word-address gspace)
448 descriptor-low-bits)))
449 (- descriptor-low-bits sb!vm:word-shift))
450 (ash (logandc2 low sb!vm:lowtag-mask)
451 (- sb!vm:word-shift))))
454 (defun make-random-descriptor (value)
455 (make-descriptor (logand (ash value (- descriptor-low-bits))
458 descriptor-low-bits))))
459 (logand value (1- (ash 1 descriptor-low-bits)))))
461 (defun make-fixnum-descriptor (num)
462 (when (>= (integer-length num)
463 (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits))
464 (error "~W is too big for a fixnum." num))
465 (make-random-descriptor (ash num sb!vm:n-fixnum-tag-bits)))
467 (defun make-other-immediate-descriptor (data type)
468 (make-descriptor (ash data (- sb!vm:n-widetag-bits descriptor-low-bits))
469 (logior (logand (ash data (- descriptor-low-bits
470 sb!vm:n-widetag-bits))
471 (1- (ash 1 descriptor-low-bits)))
474 (defun make-character-descriptor (data)
475 (make-other-immediate-descriptor data sb!vm:character-widetag))
477 (defun descriptor-beyond (des offset type)
478 (let* ((low (logior (+ (logandc2 (descriptor-low des) sb!vm:lowtag-mask)
481 (high (+ (descriptor-high des)
482 (ash low (- descriptor-low-bits)))))
483 (make-descriptor high (logand low (1- (ash 1 descriptor-low-bits))))))
485 ;;;; miscellaneous variables and other noise
487 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
488 ;;; undefined foreign symbols are to be treated as an error.
489 ;;; (In the first pass of GENESIS, needed to create a header file before
490 ;;; the C runtime can be built, various foreign symbols will necessarily
491 ;;; be undefined, but we don't need actual values for them anyway, and
492 ;;; we can just use 0 or some other placeholder. In the second pass of
493 ;;; GENESIS, all foreign symbols should be defined, so any undefined
494 ;;; foreign symbol is a problem.)
496 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
497 ;;; never tries to look up foreign symbols in the first place unless
498 ;;; it's actually creating a core file (as in the second pass) instead
499 ;;; of using this hack to allow it to go through the motions without
500 ;;; causing an error. -- WHN 20000825
501 (defvar *foreign-symbol-placeholder-value*)
503 ;;; a handle on the trap object
504 (defvar *unbound-marker*)
505 ;; was: (make-other-immediate-descriptor 0 sb!vm:unbound-marker-widetag)
507 ;;; a handle on the NIL object
508 (defvar *nil-descriptor*)
510 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
511 ;;; when the target Lisp starts up
513 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
514 ;;; loadtime value, represented by (CONS KEYWORD ..). The FILENAME
515 ;;; tells which fasl file each list element came from, for debugging
517 (defvar *current-reversed-cold-toplevels*)
519 ;;; the head of a list of DEBUG-SOURCEs which need to be patched when
520 ;;; the cold core starts up
521 (defvar *current-debug-sources*)
523 ;;; the name of the object file currently being cold loaded (as a string, not a
524 ;;; pathname), or NIL if we're not currently cold loading any object file
525 (defvar *cold-load-filename* nil)
526 (declaim (type (or string null) *cold-load-filename*))
528 ;;;; miscellaneous stuff to read and write the core memory
530 ;;; FIXME: should be DEFINE-MODIFY-MACRO
531 (defmacro cold-push (thing list)
533 "Push THING onto the given cold-load LIST."
534 `(setq ,list (cold-cons ,thing ,list)))
536 (declaim (ftype (function (descriptor sb!vm:word) descriptor) read-wordindexed))
537 (defun read-wordindexed (address index)
539 "Return the value which is displaced by INDEX words from ADDRESS."
540 (let* ((gspace (descriptor-intuit-gspace address))
541 (bytes (gspace-bytes gspace))
542 (byte-index (ash (+ index (descriptor-word-offset address))
544 (value (bvref-word bytes byte-index)))
545 (make-random-descriptor value)))
547 (declaim (ftype (function (descriptor) descriptor) read-memory))
548 (defun read-memory (address)
550 "Return the value at ADDRESS."
551 (read-wordindexed address 0))
553 ;;; (Note: In CMU CL, this function expected a SAP-typed ADDRESS
554 ;;; value, instead of the object-and-offset we use here.)
555 (declaim (ftype (function (descriptor sb!vm:word descriptor) (values))
556 note-load-time-value-reference))
557 (defun note-load-time-value-reference (address offset marker)
558 (cold-push (cold-cons
559 (cold-intern :load-time-value-fixup)
561 (cold-cons (number-to-core offset)
563 (number-to-core (descriptor-word-offset marker))
565 *current-reversed-cold-toplevels*)
568 (declaim (ftype (function (descriptor sb!vm:word (or descriptor symbol))) write-wordindexed))
569 (defun write-wordindexed (address index value)
571 "Write VALUE displaced INDEX words from ADDRESS."
572 ;; If we're passed a symbol as a value then it needs to be interned.
573 (when (symbolp value) (setf value (cold-intern value)))
574 (if (eql (descriptor-gspace value) :load-time-value)
575 (note-load-time-value-reference address
576 (- (ash index sb!vm:word-shift)
577 (logand (descriptor-bits address)
580 (let* ((bytes (gspace-bytes (descriptor-intuit-gspace address)))
581 (byte-index (ash (+ index (descriptor-word-offset address))
583 (setf (bvref-word bytes byte-index)
584 (descriptor-bits value)))))
586 (declaim (ftype (function (descriptor (or descriptor symbol))) write-memory))
587 (defun write-memory (address value)
589 "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
590 (write-wordindexed address 0 value))
592 ;;;; allocating images of primitive objects in the cold core
594 ;;; There are three kinds of blocks of memory in the type system:
595 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
596 ;;; header as all slots are descriptors.
597 ;;; * Unboxed objects (bignums): There is a single header word that contains
599 ;;; * Vector objects: There is a header word with the type, then a word for
600 ;;; the length, then the data.
601 (defun allocate-boxed-object (gspace length lowtag)
603 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
605 (allocate-cold-descriptor gspace (ash length sb!vm:word-shift) lowtag))
606 (defun allocate-unboxed-object (gspace element-bits length type)
608 "Allocate LENGTH units of ELEMENT-BITS bits plus a header word in GSPACE and
609 return an ``other-pointer'' descriptor to them. Initialize the header word
610 with the resultant length and TYPE."
611 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
612 (des (allocate-cold-descriptor gspace
613 (+ bytes sb!vm:n-word-bytes)
614 sb!vm:other-pointer-lowtag)))
616 (make-other-immediate-descriptor (ash bytes
617 (- sb!vm:word-shift))
620 (defun allocate-vector-object (gspace element-bits length type)
622 "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
623 GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
624 header word with TYPE and the length slot with LENGTH."
625 ;; FIXME: Here and in ALLOCATE-UNBOXED-OBJECT, BYTES is calculated using
626 ;; #'/ instead of #'CEILING, which seems wrong.
627 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
628 (des (allocate-cold-descriptor gspace
629 (+ bytes (* 2 sb!vm:n-word-bytes))
630 sb!vm:other-pointer-lowtag)))
631 (write-memory des (make-other-immediate-descriptor 0 type))
632 (write-wordindexed des
633 sb!vm:vector-length-slot
634 (make-fixnum-descriptor length))
637 ;;;; copying simple objects into the cold core
639 (defun base-string-to-core (string &optional (gspace *dynamic*))
641 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
642 core and return a descriptor to it."
643 ;; (Remember that the system convention for storage of strings leaves an
644 ;; extra null byte at the end to aid in call-out to C.)
645 (let* ((length (length string))
646 (des (allocate-vector-object gspace
649 sb!vm:simple-base-string-widetag))
650 (bytes (gspace-bytes gspace))
651 (offset (+ (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
652 (descriptor-byte-offset des))))
653 (write-wordindexed des
654 sb!vm:vector-length-slot
655 (make-fixnum-descriptor length))
657 (setf (bvref bytes (+ offset i))
658 (sb!xc:char-code (aref string i))))
659 (setf (bvref bytes (+ offset length))
660 0) ; null string-termination character for C
663 (defun bignum-to-core (n)
665 "Copy a bignum to the cold core."
666 (let* ((words (ceiling (1+ (integer-length n)) sb!vm:n-word-bits))
667 (handle (allocate-unboxed-object *dynamic*
670 sb!vm:bignum-widetag)))
671 (declare (fixnum words))
672 (do ((index 1 (1+ index))
673 (remainder n (ash remainder (- sb!vm:n-word-bits))))
675 (unless (zerop (integer-length remainder))
676 ;; FIXME: Shouldn't this be a fatal error?
677 (warn "~W words of ~W were written, but ~W bits were left over."
679 (let ((word (ldb (byte sb!vm:n-word-bits 0) remainder)))
680 (write-wordindexed handle index
681 (make-descriptor (ash word (- descriptor-low-bits))
682 (ldb (byte descriptor-low-bits 0)
686 (defun number-pair-to-core (first second type)
688 "Makes a number pair of TYPE (ratio or complex) and fills it in."
689 (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits 2 type)))
690 (write-wordindexed des 1 first)
691 (write-wordindexed des 2 second)
694 (defun write-double-float-bits (address index x)
695 (let ((hi (double-float-high-bits x))
696 (lo (double-float-low-bits x)))
697 (ecase sb!vm::n-word-bits
699 (let ((high-bits (make-random-descriptor hi))
700 (low-bits (make-random-descriptor lo)))
701 (ecase sb!c:*backend-byte-order*
703 (write-wordindexed address index low-bits)
704 (write-wordindexed address (1+ index) high-bits))
706 (write-wordindexed address index high-bits)
707 (write-wordindexed address (1+ index) low-bits)))))
709 (let ((bits (make-random-descriptor
710 (ecase sb!c:*backend-byte-order*
711 (:little-endian (logior lo (ash hi 32)))
713 #+nil (:big-endian (logior (logand hi #xffffffff)
715 (write-wordindexed address index bits))))
718 (defun float-to-core (x)
721 ;; 64-bit platforms have immediate single-floats.
722 #!+#.(cl:if (cl:= sb!vm:n-word-bits 64) '(and) '(or))
723 (make-random-descriptor (logior (ash (single-float-bits x) 32)
724 sb!vm::single-float-widetag))
725 #!-#.(cl:if (cl:= sb!vm:n-word-bits 64) '(and) '(or))
726 (let ((des (allocate-unboxed-object *dynamic*
728 (1- sb!vm:single-float-size)
729 sb!vm:single-float-widetag)))
730 (write-wordindexed des
731 sb!vm:single-float-value-slot
732 (make-random-descriptor (single-float-bits x)))
735 (let ((des (allocate-unboxed-object *dynamic*
737 (1- sb!vm:double-float-size)
738 sb!vm:double-float-widetag)))
739 (write-double-float-bits des sb!vm:double-float-value-slot x)))))
741 (defun complex-single-float-to-core (num)
742 (declare (type (complex single-float) num))
743 (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
744 (1- sb!vm:complex-single-float-size)
745 sb!vm:complex-single-float-widetag)))
748 (write-wordindexed des sb!vm:complex-single-float-real-slot
749 (make-random-descriptor (single-float-bits (realpart num))))
750 (write-wordindexed des sb!vm:complex-single-float-imag-slot
751 (make-random-descriptor (single-float-bits (imagpart num)))))
753 (write-wordindexed des sb!vm:complex-single-float-data-slot
754 (make-random-descriptor
755 (logior (ldb (byte 32 0) (single-float-bits (realpart num)))
756 (ash (single-float-bits (imagpart num)) 32))))
759 (defun complex-double-float-to-core (num)
760 (declare (type (complex double-float) num))
761 (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
762 (1- sb!vm:complex-double-float-size)
763 sb!vm:complex-double-float-widetag)))
764 (write-double-float-bits des sb!vm:complex-double-float-real-slot
766 (write-double-float-bits des sb!vm:complex-double-float-imag-slot
769 ;;; Copy the given number to the core.
770 (defun number-to-core (number)
772 (integer (if (< (integer-length number)
773 (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits))
774 (make-fixnum-descriptor number)
775 (bignum-to-core number)))
776 (ratio (number-pair-to-core (number-to-core (numerator number))
777 (number-to-core (denominator number))
778 sb!vm:ratio-widetag))
779 ((complex single-float) (complex-single-float-to-core number))
780 ((complex double-float) (complex-double-float-to-core number))
782 ((complex long-float)
783 (error "~S isn't a cold-loadable number at all!" number))
784 (complex (number-pair-to-core (number-to-core (realpart number))
785 (number-to-core (imagpart number))
786 sb!vm:complex-widetag))
787 (float (float-to-core number))
788 (t (error "~S isn't a cold-loadable number at all!" number))))
790 (declaim (ftype (function (sb!vm:word) descriptor) sap-int-to-core))
791 (defun sap-int-to-core (sap-int)
792 (let ((des (allocate-unboxed-object *dynamic*
796 (write-wordindexed des
797 sb!vm:sap-pointer-slot
798 (make-random-descriptor sap-int))
801 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
802 (defun cold-cons (car cdr &optional (gspace *dynamic*))
803 (let ((dest (allocate-boxed-object gspace 2 sb!vm:list-pointer-lowtag)))
804 (write-memory dest car)
805 (write-wordindexed dest 1 cdr)
808 ;;; Make a simple-vector on the target that holds the specified
809 ;;; OBJECTS, and return its descriptor.
810 (defun vector-in-core (&rest objects)
811 (let* ((size (length objects))
812 (result (allocate-vector-object *dynamic* sb!vm:n-word-bits size
813 sb!vm:simple-vector-widetag)))
814 (dotimes (index size)
815 (write-wordindexed result (+ index sb!vm:vector-data-offset)
821 ;;; Allocate (and initialize) a symbol.
822 (defun allocate-symbol (name &key (gspace *dynamic*))
823 (declare (simple-string name))
824 (let ((symbol (allocate-unboxed-object gspace
826 (1- sb!vm:symbol-size)
827 sb!vm:symbol-header-widetag)))
828 (write-wordindexed symbol sb!vm:symbol-value-slot *unbound-marker*)
829 (write-wordindexed symbol
830 sb!vm:symbol-hash-slot
831 (make-fixnum-descriptor 0))
832 (write-wordindexed symbol sb!vm:symbol-plist-slot *nil-descriptor*)
833 (write-wordindexed symbol sb!vm:symbol-name-slot
834 (base-string-to-core name *dynamic*))
835 (write-wordindexed symbol sb!vm:symbol-package-slot *nil-descriptor*)
838 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
839 ;;; descriptor of a cold symbol or (in an abbreviation for the
840 ;;; most common usage pattern) an ordinary symbol, which will be
841 ;;; automatically cold-interned.
842 (declaim (ftype (function ((or descriptor symbol) descriptor)) cold-set))
843 (defun cold-set (symbol-or-symbol-des value)
844 (let ((symbol-des (etypecase symbol-or-symbol-des
845 (descriptor symbol-or-symbol-des)
846 (symbol (cold-intern symbol-or-symbol-des)))))
847 (write-wordindexed symbol-des sb!vm:symbol-value-slot value)))
849 ;;;; layouts and type system pre-initialization
851 ;;; Since we want to be able to dump structure constants and
852 ;;; predicates with reference layouts, we need to create layouts at
853 ;;; cold-load time. We use the name to intern layouts by, and dump a
854 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
855 ;;; initialization can find them. The only thing that's tricky [sic --
856 ;;; WHN 19990816] is initializing layout's layout, which must point to
859 ;;; a map from class names to lists of
860 ;;; `(,descriptor ,name ,length ,inherits ,depth)
861 ;;; KLUDGE: It would be more understandable and maintainable to use
862 ;;; DEFSTRUCT (:TYPE LIST) here. -- WHN 19990823
863 (defvar *cold-layouts* (make-hash-table :test 'equal))
865 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
867 (defvar *cold-layout-names* (make-hash-table :test 'eql))
869 ;;; FIXME: *COLD-LAYOUTS* and *COLD-LAYOUT-NAMES* should be
870 ;;; initialized by binding in GENESIS.
872 ;;; the descriptor for layout's layout (needed when making layouts)
873 (defvar *layout-layout*)
875 (defconstant target-layout-length
876 (layout-length (find-layout 'layout)))
878 (defun target-layout-index (slot-name)
879 ;; KLUDGE: this is a little bit sleazy, but the tricky thing is that
880 ;; structure slots don't have a terribly firm idea of their names.
881 ;; At least here if we change LAYOUT's package of definition, we
882 ;; only have to change one thing...
883 (let* ((name (find-symbol (symbol-name slot-name) "SB!KERNEL"))
884 (layout (find-layout 'layout))
885 (dd (layout-info layout))
886 (slots (dd-slots dd))
887 (dsd (find name slots :key #'dsd-name)))
891 (defun cold-set-layout-slot (cold-layout slot-name value)
894 (+ sb!vm:instance-slots-offset (target-layout-index slot-name))
897 ;;; Return a list of names created from the cold layout INHERITS data
899 (defun listify-cold-inherits (x)
900 (let ((len (descriptor-fixnum (read-wordindexed x
901 sb!vm:vector-length-slot))))
904 (let* ((des (read-wordindexed x (+ sb!vm:vector-data-offset index)))
905 (found (gethash (descriptor-bits des) *cold-layout-names*)))
908 (error "unknown descriptor at index ~S (bits = ~8,'0X)"
910 (descriptor-bits des)))))
913 (declaim (ftype (function (symbol descriptor descriptor descriptor descriptor)
916 (defun make-cold-layout (name length inherits depthoid nuntagged)
917 (let ((result (allocate-boxed-object *dynamic*
918 ;; KLUDGE: Why 1+? -- WHN 19990901
919 ;; header word? -- CSR 20051204
920 (1+ target-layout-length)
921 sb!vm:instance-pointer-lowtag)))
923 (make-other-immediate-descriptor
924 target-layout-length sb!vm:instance-header-widetag))
926 ;; KLUDGE: The offsets into LAYOUT below should probably be pulled out
927 ;; of the cross-compiler's tables at genesis time instead of inserted
928 ;; by hand as bare numeric constants. -- WHN ca. 19990901
930 ;; Set slot 0 = the layout of the layout.
931 (write-wordindexed result sb!vm:instance-slots-offset *layout-layout*)
933 ;; Don't set the CLOS hash value: done in cold-init instead.
935 ;; Set other slot values.
937 ;; leave CLASSOID uninitialized for now
938 (cold-set-layout-slot result 'invalid *nil-descriptor*)
939 (cold-set-layout-slot result 'inherits inherits)
940 (cold-set-layout-slot result 'depthoid depthoid)
941 (cold-set-layout-slot result 'length length)
942 (cold-set-layout-slot result 'info *nil-descriptor*)
943 (cold-set-layout-slot result 'pure *nil-descriptor*)
944 (cold-set-layout-slot result 'n-untagged-slots nuntagged)
945 (cold-set-layout-slot result 'source-location *nil-descriptor*)
946 (cold-set-layout-slot result 'for-std-class-p *nil-descriptor*)
948 (setf (gethash name *cold-layouts*)
951 (descriptor-fixnum length)
952 (listify-cold-inherits inherits)
953 (descriptor-fixnum depthoid)
954 (descriptor-fixnum nuntagged)))
955 (setf (gethash (descriptor-bits result) *cold-layout-names*) name)
959 (defun initialize-layouts ()
961 (clrhash *cold-layouts*)
963 ;; We initially create the layout of LAYOUT itself with NIL as the LAYOUT and
965 (setq *layout-layout* *nil-descriptor*)
966 (let ((xlayout-layout (find-layout 'layout)))
967 (aver (= 0 (layout-n-untagged-slots xlayout-layout)))
968 (setq *layout-layout*
969 (make-cold-layout 'layout
970 (number-to-core target-layout-length)
972 (number-to-core (layout-depthoid xlayout-layout))
975 *layout-layout* sb!vm:instance-slots-offset *layout-layout*)
977 ;; Then we create the layouts that we'll need to make a correct INHERITS
978 ;; vector for the layout of LAYOUT itself..
980 ;; FIXME: The various LENGTH and DEPTHOID numbers should be taken from
981 ;; the compiler's tables, not set by hand.
989 (make-cold-layout 'structure-object
991 (vector-in-core t-layout)
995 (make-cold-layout 'structure!object
997 (vector-in-core t-layout so-layout)
1000 (layout-inherits (vector-in-core t-layout
1004 ;; ..and return to backpatch the layout of LAYOUT.
1005 (setf (fourth (gethash 'layout *cold-layouts*))
1006 (listify-cold-inherits layout-inherits))
1007 (cold-set-layout-slot *layout-layout* 'inherits layout-inherits))))
1009 ;;;; interning symbols in the cold image
1011 ;;; In order to avoid having to know about the package format, we
1012 ;;; build a data structure in *COLD-PACKAGE-SYMBOLS* that holds all
1013 ;;; interned symbols along with info about their packages. The data
1014 ;;; structure is a list of sublists, where the sublists have the
1015 ;;; following format:
1016 ;;; (<make-package-arglist>
1017 ;;; <internal-symbols>
1018 ;;; <external-symbols>
1019 ;;; <imported-internal-symbols>
1020 ;;; <imported-external-symbols>
1021 ;;; <shadowing-symbols>
1022 ;;; <package-documentation>)
1024 ;;; KLUDGE: It would be nice to implement the sublists as instances of
1025 ;;; a DEFSTRUCT (:TYPE LIST). (They'd still be lists, but at least we'd be
1026 ;;; using mnemonically-named operators to access them, instead of trying
1027 ;;; to remember what THIRD and FIFTH mean, and hoping that we never
1028 ;;; need to change the list layout..) -- WHN 19990825
1030 ;;; an alist from packages to lists of that package's symbols to be dumped
1031 (defvar *cold-package-symbols*)
1032 (declaim (type list *cold-package-symbols*))
1034 ;;; a map from descriptors to symbols, so that we can back up. The key
1035 ;;; is the address in the target core.
1036 (defvar *cold-symbols*)
1037 (declaim (type hash-table *cold-symbols*))
1039 ;;; sanity check for a symbol we're about to create on the target
1041 ;;; Make sure that the symbol has an appropriate package. In
1042 ;;; particular, catch the so-easy-to-make error of typing something
1043 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1044 ;;; need is SB!KERNEL:%BYTE-BLT.
1045 (defun package-ok-for-target-symbol-p (package)
1046 (let ((package-name (package-name package)))
1048 ;; Cold interning things in these standard packages is OK. (Cold
1049 ;; interning things in the other standard package, CL-USER, isn't
1050 ;; OK. We just use CL-USER to expose symbols whose homes are in
1051 ;; other packages. Thus, trying to cold intern a symbol whose
1052 ;; home package is CL-USER probably means that a coding error has
1053 ;; been made somewhere.)
1054 (find package-name '("COMMON-LISP" "KEYWORD") :test #'string=)
1055 ;; Cold interning something in one of our target-code packages,
1056 ;; which are ever-so-rigorously-and-elegantly distinguished by
1057 ;; this prefix on their names, is OK too.
1058 (string= package-name "SB!" :end1 3 :end2 3)
1059 ;; This one is OK too, since it ends up being COMMON-LISP on the
1061 (string= package-name "SB-XC")
1062 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1063 ;; package in the xc host? something we can't think of
1064 ;; a valid reason to cold intern, anyway...)
1067 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1069 ;;; Most host symbols we dump onto the target are created by SBCL
1070 ;;; itself, so that as long as we avoid gratuitously
1071 ;;; cross-compilation-unfriendly hacks, it just happens that their
1072 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1073 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1074 ;;; in the COMMON-LISP package, where we don't get to create the
1075 ;;; symbols but instead have to use the ones that the xc host created.
1076 ;;; In particular, while ANSI specifies which symbols are exported
1077 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1078 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1079 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1080 ;;; symbols in the CLOS package).
1081 (defun symbol-package-for-target-symbol (symbol)
1082 ;; We want to catch weird symbols like CLISP's
1083 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1084 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1085 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1086 (multiple-value-bind (cl-symbol cl-status)
1087 (find-symbol (symbol-name symbol) *cl-package*)
1088 (if (and (eq symbol cl-symbol)
1089 (eq cl-status :external))
1090 ;; special case, to work around possible xc host weirdness
1091 ;; in COMMON-LISP package
1094 (let ((result (symbol-package symbol)))
1095 (unless (package-ok-for-target-symbol-p result)
1096 (bug "~A in bad package for target: ~A" symbol result))
1099 ;;; Return a handle on an interned symbol. If necessary allocate the
1100 ;;; symbol and record which package the symbol was referenced in. When
1101 ;;; we allocate the symbol, make sure we record a reference to the
1102 ;;; symbol in the home package so that the package gets set.
1103 (defun cold-intern (symbol
1105 (package (symbol-package-for-target-symbol symbol))
1108 (aver (package-ok-for-target-symbol-p package))
1110 ;; Anything on the cross-compilation host which refers to the target
1111 ;; machinery through the host SB-XC package should be translated to
1112 ;; something on the target which refers to the same machinery
1113 ;; through the target COMMON-LISP package.
1114 (let ((p (find-package "SB-XC")))
1115 (when (eq package p)
1116 (setf package *cl-package*))
1117 (when (eq (symbol-package symbol) p)
1118 (setf symbol (intern (symbol-name symbol) *cl-package*))))
1120 (let (;; Information about each cold-interned symbol is stored
1121 ;; in COLD-INTERN-INFO.
1122 ;; (CAR COLD-INTERN-INFO) = descriptor of symbol
1123 ;; (CDR COLD-INTERN-INFO) = list of packages, other than symbol's
1124 ;; own package, referring to symbol
1125 ;; (*COLD-PACKAGE-SYMBOLS* and *COLD-SYMBOLS* store basically the
1126 ;; same information, but with the mapping running the opposite way.)
1127 (cold-intern-info (get symbol 'cold-intern-info)))
1128 (unless cold-intern-info
1129 (cond ((eq (symbol-package-for-target-symbol symbol) package)
1130 (let ((handle (allocate-symbol (symbol-name symbol) :gspace gspace)))
1131 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1132 (when (eq package *keyword-package*)
1133 (cold-set handle handle))
1134 (setq cold-intern-info
1135 (setf (get symbol 'cold-intern-info) (cons handle nil)))))
1137 (cold-intern symbol)
1138 (setq cold-intern-info (get symbol 'cold-intern-info)))))
1139 (unless (or (null package)
1140 (member package (cdr cold-intern-info)))
1141 (push package (cdr cold-intern-info))
1142 (let* ((old-cps-entry (assoc package *cold-package-symbols*))
1143 (cps-entry (or old-cps-entry
1144 (car (push (list package)
1145 *cold-package-symbols*)))))
1146 (unless old-cps-entry
1147 (/show "created *COLD-PACKAGE-SYMBOLS* entry for" package symbol))
1148 (push symbol (rest cps-entry))))
1149 (car cold-intern-info)))
1151 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1152 (defun make-nil-descriptor ()
1153 (let* ((des (allocate-unboxed-object
1158 (result (make-descriptor (descriptor-high des)
1159 (+ (descriptor-low des)
1160 (* 2 sb!vm:n-word-bytes)
1161 (- sb!vm:list-pointer-lowtag
1162 sb!vm:other-pointer-lowtag)))))
1163 (write-wordindexed des
1165 (make-other-immediate-descriptor
1167 sb!vm:symbol-header-widetag))
1168 (write-wordindexed des
1169 (+ 1 sb!vm:symbol-value-slot)
1171 (write-wordindexed des
1172 (+ 2 sb!vm:symbol-value-slot)
1174 (write-wordindexed des
1175 (+ 1 sb!vm:symbol-plist-slot)
1177 (write-wordindexed des
1178 (+ 1 sb!vm:symbol-name-slot)
1179 ;; This is *DYNAMIC*, and DES is *STATIC*,
1180 ;; because that's the way CMU CL did it; I'm
1181 ;; not sure whether there's an underlying
1182 ;; reason. -- WHN 1990826
1183 (base-string-to-core "NIL" *dynamic*))
1184 (write-wordindexed des
1185 (+ 1 sb!vm:symbol-package-slot)
1187 (setf (get nil 'cold-intern-info)
1192 ;;; Since the initial symbols must be allocated before we can intern
1193 ;;; anything else, we intern those here. We also set the value of T.
1194 (defun initialize-non-nil-symbols ()
1196 "Initialize the cold load symbol-hacking data structures."
1197 ;; Intern the others.
1198 (dolist (symbol sb!vm:*static-symbols*)
1199 (let* ((des (cold-intern symbol :gspace *static*))
1200 (offset-wanted (sb!vm:static-symbol-offset symbol))
1201 (offset-found (- (descriptor-low des)
1202 (descriptor-low *nil-descriptor*))))
1203 (unless (= offset-wanted offset-found)
1204 ;; FIXME: should be fatal
1205 (warn "Offset from ~S to ~S is ~W, not ~W"
1210 ;; Establish the value of T.
1211 (let ((t-symbol (cold-intern t :gspace *static*)))
1212 (cold-set t-symbol t-symbol))
1213 ;; Establish the value of *PSEUDO-ATOMIC-BITS* so that the
1214 ;; allocation sequences that expect it to be zero upon entrance
1215 ;; actually find it to be so.
1217 (let ((p-a-a-symbol (cold-intern 'sb!kernel:*pseudo-atomic-bits*
1219 (cold-set p-a-a-symbol (make-fixnum-descriptor 0))))
1221 ;;; a helper function for FINISH-SYMBOLS: Return a cold alist suitable
1222 ;;; to be stored in *!INITIAL-LAYOUTS*.
1223 (defun cold-list-all-layouts ()
1225 (result *nil-descriptor*))
1226 (maphash (lambda (key stuff)
1227 (push (cons key (first stuff)) layouts))
1229 (flet ((sorter (x y)
1230 (let ((xpn (package-name (symbol-package-for-target-symbol x)))
1231 (ypn (package-name (symbol-package-for-target-symbol y))))
1233 ((string= x y) (string< xpn ypn))
1234 (t (string< x y))))))
1235 (setq layouts (sort layouts #'sorter :key #'car)))
1236 (dolist (layout layouts result)
1237 (cold-push (cold-cons (cold-intern (car layout)) (cdr layout))
1240 ;;; Establish initial values for magic symbols.
1242 ;;; Scan over all the symbols referenced in each package in
1243 ;;; *COLD-PACKAGE-SYMBOLS* making that for each one there's an
1244 ;;; appropriate entry in the *!INITIAL-SYMBOLS* data structure to
1246 (defun finish-symbols ()
1248 ;; I think the point of setting these functions into SYMBOL-VALUEs
1249 ;; here, instead of using SYMBOL-FUNCTION, is that in CMU CL
1250 ;; SYMBOL-FUNCTION reduces to FDEFINITION, which is a pretty
1251 ;; hairy operation (involving globaldb.lisp etc.) which we don't
1252 ;; want to invoke early in cold init. -- WHN 2001-12-05
1254 ;; FIXME: So OK, that's a reasonable reason to do something weird like
1255 ;; this, but this is still a weird thing to do, and we should change
1256 ;; the names to highlight that something weird is going on. Perhaps
1257 ;; *MAYBE-GC-FUN*, *INTERNAL-ERROR-FUN*, *HANDLE-BREAKPOINT-FUN*,
1258 ;; and *HANDLE-FUN-END-BREAKPOINT-FUN*...
1259 (dolist (symbol sb!vm::*c-callable-static-symbols*)
1260 (cold-set symbol (cold-fdefinition-object (cold-intern symbol))))
1262 (cold-set 'sb!vm::*current-catch-block* (make-fixnum-descriptor 0))
1263 (cold-set 'sb!vm::*current-unwind-protect-block* (make-fixnum-descriptor 0))
1265 (cold-set '*free-interrupt-context-index* (make-fixnum-descriptor 0))
1267 (cold-set '*!initial-layouts* (cold-list-all-layouts))
1269 (/show "dumping packages" (mapcar #'car *cold-package-symbols*))
1270 (let ((initial-symbols *nil-descriptor*))
1271 (dolist (cold-package-symbols-entry *cold-package-symbols*)
1272 (let* ((cold-package (car cold-package-symbols-entry))
1273 (symbols (cdr cold-package-symbols-entry))
1274 (shadows (package-shadowing-symbols cold-package))
1275 (documentation (base-string-to-core
1276 ;; KLUDGE: NIL punned as 0-length string.
1278 ;; don't propagate the arbitrary
1279 ;; docstring from host packages into
1281 (or (eql cold-package *cl-package*)
1282 (eql cold-package *keyword-package*))
1283 (documentation cold-package t))))
1286 (internal *nil-descriptor*)
1287 (external *nil-descriptor*)
1288 (imported-internal *nil-descriptor*)
1289 (imported-external *nil-descriptor*)
1290 (shadowing *nil-descriptor*))
1291 (declare (type package cold-package)) ; i.e. not a target descriptor
1292 (/show "dumping" cold-package symbols)
1294 ;; FIXME: Add assertions here to make sure that inappropriate stuff
1295 ;; isn't being dumped:
1296 ;; * the CL-USER package
1297 ;; * the SB-COLD package
1298 ;; * any internal symbols in the CL package
1299 ;; * basically any package other than CL, KEYWORD, or the packages
1300 ;; in package-data-list.lisp-expr
1301 ;; and that the structure of the KEYWORD package (e.g. whether
1302 ;; any symbols are internal to it) matches what we want in the
1305 ;; FIXME: It seems possible that by looking at the contents of
1306 ;; packages in the target SBCL we could find which symbols in
1307 ;; package-data-lisp.lisp-expr are now obsolete. (If I
1308 ;; understand correctly, only symbols which actually have
1309 ;; definitions or which are otherwise referred to actually end
1310 ;; up in the target packages.)
1312 (dolist (symbol symbols)
1313 (let ((handle (car (get symbol 'cold-intern-info)))
1314 (imported-p (not (eq (symbol-package-for-target-symbol symbol)
1316 (multiple-value-bind (found where)
1317 (find-symbol (symbol-name symbol) cold-package)
1318 (unless (and where (eq found symbol))
1319 (error "The symbol ~S is not available in ~S."
1322 (when (memq symbol shadows)
1323 (cold-push handle shadowing))
1325 (:internal (if imported-p
1326 (cold-push handle imported-internal)
1328 (cold-push handle internal)
1329 (incf internal-count))))
1330 (:external (if imported-p
1331 (cold-push handle imported-external)
1333 (cold-push handle external)
1334 (incf external-count))))))))
1335 (let ((r *nil-descriptor*))
1336 (cold-push documentation r)
1337 (cold-push shadowing r)
1338 (cold-push imported-external r)
1339 (cold-push imported-internal r)
1340 (cold-push external r)
1341 (cold-push internal r)
1342 (cold-push (make-make-package-args cold-package
1346 ;; FIXME: It would be more space-efficient to use vectors
1347 ;; instead of lists here, and space-efficiency here would be
1348 ;; nice, since it would reduce the peak memory usage in
1349 ;; genesis and cold init.
1350 (cold-push r initial-symbols))))
1351 (cold-set '*!initial-symbols* initial-symbols))
1353 (cold-set '*!initial-fdefn-objects* (list-all-fdefn-objects))
1355 (cold-set '*!reversed-cold-toplevels* *current-reversed-cold-toplevels*)
1356 (cold-set '*!initial-debug-sources* *current-debug-sources*)
1360 (cold-set 'sb!vm::*fp-constant-0d0* (number-to-core 0d0))
1361 (cold-set 'sb!vm::*fp-constant-1d0* (number-to-core 1d0))
1362 (cold-set 'sb!vm::*fp-constant-0f0* (number-to-core 0f0))
1363 (cold-set 'sb!vm::*fp-constant-1f0* (number-to-core 1f0))))
1365 ;;; Make a cold list that can be used as the arg list to MAKE-PACKAGE in
1366 ;;; order to make a package that is similar to PKG.
1367 (defun make-make-package-args (pkg internal-count external-count)
1368 (let* ((use *nil-descriptor*)
1369 (cold-nicknames *nil-descriptor*)
1370 (res *nil-descriptor*))
1371 (dolist (u (package-use-list pkg))
1372 (when (assoc u *cold-package-symbols*)
1373 (cold-push (base-string-to-core (package-name u)) use)))
1374 (let* ((pkg-name (package-name pkg))
1375 ;; Make the package nickname lists for the standard packages
1376 ;; be the minimum specified by ANSI, regardless of what value
1377 ;; the cross-compilation host happens to use.
1378 (warm-nicknames (cond ((string= pkg-name "COMMON-LISP")
1380 ((string= pkg-name "COMMON-LISP-USER")
1382 ((string= pkg-name "KEYWORD")
1384 ;; For packages other than the
1385 ;; standard packages, the nickname
1386 ;; list was specified by our package
1387 ;; setup code, not by properties of
1388 ;; what cross-compilation host we
1389 ;; happened to use, and we can just
1390 ;; propagate it into the target.
1392 (package-nicknames pkg)))))
1393 (dolist (warm-nickname warm-nicknames)
1394 (cold-push (base-string-to-core warm-nickname) cold-nicknames)))
1396 ;; INTERNAL-COUNT and EXTERNAL-COUNT are the number of symbols that
1397 ;; the package contains in the core. We arrange for the package
1398 ;; symbol tables to be created somewhat larger so that they don't
1399 ;; need to be rehashed so easily when additional symbols are
1400 ;; interned during the warm build.
1401 (cold-push (number-to-core (truncate internal-count 0.8)) res)
1402 (cold-push (cold-intern :internal-symbols) res)
1403 (cold-push (number-to-core (truncate external-count 0.8)) res)
1404 (cold-push (cold-intern :external-symbols) res)
1406 (cold-push cold-nicknames res)
1407 (cold-push (cold-intern :nicknames) res)
1410 (cold-push (cold-intern :use) res)
1412 (cold-push (base-string-to-core (package-name pkg)) res)
1415 ;;;; functions and fdefinition objects
1417 ;;; a hash table mapping from fdefinition names to descriptors of cold
1420 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1421 ;;; we want to have only one entry per name, this must be an 'EQUAL
1422 ;;; hash table, not the default 'EQL.
1423 (defvar *cold-fdefn-objects*)
1425 (defvar *cold-fdefn-gspace* nil)
1427 ;;; Given a cold representation of a symbol, return a warm
1429 (defun warm-symbol (des)
1430 ;; Note that COLD-INTERN is responsible for keeping the
1431 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1432 ;; uninterned symbol, the code below will fail. But as long as we
1433 ;; don't need to look up uninterned symbols during bootstrapping,
1435 (multiple-value-bind (symbol found-p)
1436 (gethash (descriptor-bits des) *cold-symbols*)
1437 (declare (type symbol symbol))
1439 (error "no warm symbol"))
1442 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1443 (defun cold-car (des)
1444 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1445 (read-wordindexed des sb!vm:cons-car-slot))
1446 (defun cold-cdr (des)
1447 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1448 (read-wordindexed des sb!vm:cons-cdr-slot))
1449 (defun cold-null (des)
1450 (= (descriptor-bits des)
1451 (descriptor-bits *nil-descriptor*)))
1453 ;;; Given a cold representation of a function name, return a warm
1455 (declaim (ftype (function ((or descriptor symbol)) (or symbol list)) warm-fun-name))
1456 (defun warm-fun-name (des)
1459 ;; This parallels the logic at the start of COLD-INTERN
1460 ;; which re-homes symbols in SB-XC to COMMON-LISP.
1461 (if (eq (symbol-package des) (find-package "SB-XC"))
1462 (intern (symbol-name des) *cl-package*)
1464 (ecase (descriptor-lowtag des)
1465 (#.sb!vm:list-pointer-lowtag
1466 (aver (not (cold-null des))) ; function named NIL? please no..
1467 ;; Do cold (DESTRUCTURING-BIND (COLD-CAR COLD-CADR) DES ..).
1468 (let* ((car-des (cold-car des))
1469 (cdr-des (cold-cdr des))
1470 (cadr-des (cold-car cdr-des))
1471 (cddr-des (cold-cdr cdr-des)))
1472 (aver (cold-null cddr-des))
1473 (list (warm-symbol car-des)
1474 (warm-symbol cadr-des))))
1475 (#.sb!vm:other-pointer-lowtag
1476 (warm-symbol des))))))
1477 (legal-fun-name-or-type-error result)
1480 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw)
1481 (declare (type (or descriptor symbol) cold-name))
1482 (/show0 "/cold-fdefinition-object")
1483 (let ((warm-name (warm-fun-name cold-name)))
1484 (or (gethash warm-name *cold-fdefn-objects*)
1485 (let ((fdefn (allocate-boxed-object (or *cold-fdefn-gspace* *dynamic*)
1486 (1- sb!vm:fdefn-size)
1487 sb!vm:other-pointer-lowtag)))
1489 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
1490 (write-memory fdefn (make-other-immediate-descriptor
1491 (1- sb!vm:fdefn-size) sb!vm:fdefn-widetag))
1492 (write-wordindexed fdefn sb!vm:fdefn-name-slot cold-name)
1493 (unless leave-fn-raw
1494 (write-wordindexed fdefn sb!vm:fdefn-fun-slot
1496 (write-wordindexed fdefn
1497 sb!vm:fdefn-raw-addr-slot
1498 (make-random-descriptor
1499 (cold-foreign-symbol-address "undefined_tramp"))))
1502 ;;; Handle the at-cold-init-time, fset-for-static-linkage operation
1503 ;;; requested by FOP-FSET.
1504 (defun static-fset (cold-name defn)
1505 (declare (type (or descriptor symbol) cold-name))
1506 (let ((fdefn (cold-fdefinition-object cold-name t))
1507 (type (logand (descriptor-low (read-memory defn)) sb!vm:widetag-mask)))
1508 (write-wordindexed fdefn sb!vm:fdefn-fun-slot defn)
1509 (write-wordindexed fdefn
1510 sb!vm:fdefn-raw-addr-slot
1512 (#.sb!vm:simple-fun-header-widetag
1513 (/show0 "static-fset (simple-fun)")
1517 (make-random-descriptor
1518 (+ (logandc2 (descriptor-bits defn)
1520 (ash sb!vm:simple-fun-code-offset
1521 sb!vm:word-shift))))
1522 (#.sb!vm:closure-header-widetag
1523 (/show0 "/static-fset (closure)")
1524 (make-random-descriptor
1525 (cold-foreign-symbol-address "closure_tramp")))))
1528 (defun initialize-static-fns ()
1529 (let ((*cold-fdefn-gspace* *static*))
1530 (dolist (sym sb!vm:*static-funs*)
1531 (let* ((fdefn (cold-fdefinition-object (cold-intern sym)))
1532 (offset (- (+ (- (descriptor-low fdefn)
1533 sb!vm:other-pointer-lowtag)
1534 (* sb!vm:fdefn-raw-addr-slot sb!vm:n-word-bytes))
1535 (descriptor-low *nil-descriptor*)))
1536 (desired (sb!vm:static-fun-offset sym)))
1537 (unless (= offset desired)
1538 ;; FIXME: should be fatal
1539 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1540 sym nil offset desired))))))
1542 (defun list-all-fdefn-objects ()
1544 (result *nil-descriptor*))
1545 (maphash (lambda (key value)
1546 (push (cons key value) fdefns))
1547 *cold-fdefn-objects*)
1548 (flet ((sorter (x y)
1549 (let* ((xbn (fun-name-block-name x))
1550 (ybn (fun-name-block-name y))
1551 (xbnpn (package-name (symbol-package-for-target-symbol xbn)))
1552 (ybnpn (package-name (symbol-package-for-target-symbol ybn))))
1554 ((eql xbn ybn) (consp x))
1555 ((string= xbn ybn) (string< xbnpn ybnpn))
1556 (t (string< xbn ybn))))))
1557 (setq fdefns (sort fdefns #'sorter :key #'car)))
1558 (dolist (fdefn fdefns result)
1559 (cold-push (cdr fdefn) result))))
1561 ;;;; fixups and related stuff
1563 ;;; an EQUAL hash table
1564 (defvar *cold-foreign-symbol-table*)
1565 (declaim (type hash-table *cold-foreign-symbol-table*))
1567 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1569 (defun load-cold-foreign-symbol-table (filename)
1570 (/show "load-cold-foreign-symbol-table" filename)
1571 (with-open-file (file filename)
1572 (loop for line = (read-line file nil nil)
1574 ;; UNIX symbol tables might have tabs in them, and tabs are
1575 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1576 ;; nice portable way to deal with them within Lisp, alas.
1577 ;; Fortunately, it's easy to use UNIX command line tools like
1578 ;; sed to remove the problem, so it's not too painful for us
1579 ;; to push responsibility for converting tabs to spaces out to
1582 ;; Other non-STANDARD-CHARs are problematic for the same reason.
1583 ;; Make sure that there aren't any..
1584 (let ((ch (find-if (lambda (char)
1585 (not (typep char 'standard-char)))
1588 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1591 (setf line (string-trim '(#\space) line))
1592 (let ((p1 (position #\space line :from-end nil))
1593 (p2 (position #\space line :from-end t)))
1594 (if (not (and p1 p2 (< p1 p2)))
1595 ;; KLUDGE: It's too messy to try to understand all
1596 ;; possible output from nm, so we just punt the lines we
1597 ;; don't recognize. We realize that there's some chance
1598 ;; that might get us in trouble someday, so we warn
1600 (warn "ignoring unrecognized line ~S in ~A" line filename)
1601 (multiple-value-bind (value name)
1602 (if (string= "0x" line :end2 2)
1603 (values (parse-integer line :start 2 :end p1 :radix 16)
1604 (subseq line (1+ p2)))
1605 (values (parse-integer line :end p1 :radix 16)
1606 (subseq line (1+ p2))))
1607 ;; KLUDGE CLH 2010-05-31: on darwin, nm gives us
1608 ;; _function but dlsym expects us to look up
1609 ;; function, without the leading _ . Therefore, we
1610 ;; strip it off here.
1612 (when (equal (char name 0) #\_)
1613 (setf name (subseq name 1)))
1614 (multiple-value-bind (old-value found)
1615 (gethash name *cold-foreign-symbol-table*)
1617 (not (= old-value value)))
1618 (warn "redefining ~S from #X~X to #X~X"
1619 name old-value value)))
1620 (/show "adding to *cold-foreign-symbol-table*:" name value)
1621 (setf (gethash name *cold-foreign-symbol-table*) value)
1623 (let ((at-position (position #\@ name)))
1625 (let ((name (subseq name 0 at-position)))
1626 (multiple-value-bind (old-value found)
1627 (gethash name *cold-foreign-symbol-table*)
1629 (not (= old-value value)))
1630 (warn "redefining ~S from #X~X to #X~X"
1631 name old-value value)))
1632 (setf (gethash name *cold-foreign-symbol-table*)
1636 (defun cold-foreign-symbol-address (name)
1637 (or (find-foreign-symbol-in-table name *cold-foreign-symbol-table*)
1638 *foreign-symbol-placeholder-value*
1640 (format *error-output* "~&The foreign symbol table is:~%")
1641 (maphash (lambda (k v)
1642 (format *error-output* "~&~S = #X~8X~%" k v))
1643 *cold-foreign-symbol-table*)
1644 (error "The foreign symbol ~S is undefined." name))))
1646 (defvar *cold-assembler-routines*)
1648 (defvar *cold-assembler-fixups*)
1650 (defun record-cold-assembler-routine (name address)
1651 (/xhow "in RECORD-COLD-ASSEMBLER-ROUTINE" name address)
1652 (push (cons name address)
1653 *cold-assembler-routines*))
1655 (defun record-cold-assembler-fixup (routine
1660 (push (list routine code-object offset kind)
1661 *cold-assembler-fixups*))
1663 (defun lookup-assembler-reference (symbol)
1664 (let ((value (cdr (assoc symbol *cold-assembler-routines*))))
1665 ;; FIXME: Should this be ERROR instead of WARN?
1667 (warn "Assembler routine ~S not defined." symbol))
1670 ;;; The x86 port needs to store code fixups along with code objects if
1671 ;;; they are to be moved, so fixups for code objects in the dynamic
1672 ;;; heap need to be noted.
1674 (defvar *load-time-code-fixups*)
1677 (defun note-load-time-code-fixup (code-object offset)
1678 ;; If CODE-OBJECT might be moved
1679 (when (= (gspace-identifier (descriptor-intuit-gspace code-object))
1680 dynamic-core-space-id)
1681 (push offset (gethash (descriptor-bits code-object)
1682 *load-time-code-fixups*
1687 (defun output-load-time-code-fixups ()
1688 (let ((fixup-infos nil))
1690 (lambda (code-object-address fixup-offsets)
1691 (push (cons code-object-address fixup-offsets) fixup-infos))
1692 *load-time-code-fixups*)
1693 (setq fixup-infos (sort fixup-infos #'< :key #'car))
1694 (dolist (fixup-info fixup-infos)
1695 (let ((code-object-address (car fixup-info))
1696 (fixup-offsets (cdr fixup-info)))
1698 (allocate-vector-object
1699 *dynamic* sb!vm:n-word-bits (length fixup-offsets)
1700 sb!vm:simple-array-unsigned-byte-32-widetag)))
1701 (do ((index sb!vm:vector-data-offset (1+ index))
1702 (fixups fixup-offsets (cdr fixups)))
1704 (write-wordindexed fixup-vector index
1705 (make-random-descriptor (car fixups))))
1706 ;; KLUDGE: The fixup vector is stored as the first constant,
1707 ;; not as a separately-named slot.
1708 (write-wordindexed (make-random-descriptor code-object-address)
1709 sb!vm:code-constants-offset
1712 ;;; Given a pointer to a code object and an offset relative to the
1713 ;;; tail of the code object's header, return an offset relative to the
1714 ;;; (beginning of the) code object.
1716 ;;; FIXME: It might be clearer to reexpress
1717 ;;; (LET ((X (CALC-OFFSET CODE-OBJECT OFFSET0))) ..)
1719 ;;; (LET ((X (+ OFFSET0 (CODE-OBJECT-HEADER-N-BYTES CODE-OBJECT)))) ..).
1720 (declaim (ftype (function (descriptor sb!vm:word)) calc-offset))
1721 (defun calc-offset (code-object offset-from-tail-of-header)
1722 (let* ((header (read-memory code-object))
1723 (header-n-words (ash (descriptor-bits header)
1724 (- sb!vm:n-widetag-bits)))
1725 (header-n-bytes (ash header-n-words sb!vm:word-shift))
1726 (result (+ offset-from-tail-of-header header-n-bytes)))
1729 (declaim (ftype (function (descriptor sb!vm:word sb!vm:word keyword))
1731 (defun do-cold-fixup (code-object after-header value kind)
1732 (let* ((offset-within-code-object (calc-offset code-object after-header))
1733 (gspace-bytes (descriptor-bytes code-object))
1734 (gspace-byte-offset (+ (descriptor-byte-offset code-object)
1735 offset-within-code-object))
1736 (gspace-byte-address (gspace-byte-address
1737 (descriptor-gspace code-object))))
1738 (ecase +backend-fasl-file-implementation+
1739 ;; See CMU CL source for other formerly-supported architectures
1740 ;; (and note that you have to rewrite them to use BVREF-X
1741 ;; instead of SAP-REF).
1745 (assert (zerop (ldb (byte 2 0) value))))
1747 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1748 (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
1749 (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
1750 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1751 (ldb (byte 8 48) value)
1752 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1753 (ldb (byte 8 56) value))))
1755 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1756 (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
1757 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1758 (ldb (byte 8 32) value)
1759 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1760 (ldb (byte 8 40) value))))
1762 (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
1763 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1764 (ldb (byte 8 16) value)
1765 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1766 (ldb (byte 8 24) value))))
1768 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1769 (ldb (byte 8 0) value)
1770 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1771 (ldb (byte 8 8) value)))))
1775 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1776 (logior (mask-field (byte 18 14)
1777 (bvref-32 gspace-bytes gspace-byte-offset))
1779 (1+ (ash (ldb (byte 13 0) value) 1))
1780 (ash (ldb (byte 13 0) value) 1)))))
1782 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1783 (logior (mask-field (byte 18 14)
1784 (bvref-32 gspace-bytes gspace-byte-offset))
1786 (1+ (ash (ldb (byte 10 0) value) 1))
1787 (ash (ldb (byte 11 0) value) 1)))))
1789 (let ((low-bits (ldb (byte 11 0) value)))
1790 (assert (<= 0 low-bits (1- (ash 1 4)))))
1791 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1792 (logior (ash (dpb (ldb (byte 4 0) value)
1794 (ldb (byte 1 4) value)) 17)
1795 (logand (bvref-32 gspace-bytes gspace-byte-offset)
1798 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1799 (logior (mask-field (byte 11 21)
1800 (bvref-32 gspace-bytes gspace-byte-offset))
1801 (ash (ldb (byte 5 13) value) 16)
1802 (ash (ldb (byte 2 18) value) 14)
1803 (ash (ldb (byte 2 11) value) 12)
1804 (ash (ldb (byte 11 20) value) 1)
1805 (ldb (byte 1 31) value))))
1807 (let ((bits (ldb (byte 9 2) value)))
1808 (assert (zerop (ldb (byte 2 0) value)))
1809 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1810 (logior (ash bits 3)
1811 (mask-field (byte 1 1) (bvref-32 gspace-bytes gspace-byte-offset))
1812 (mask-field (byte 3 13) (bvref-32 gspace-bytes gspace-byte-offset))
1813 (mask-field (byte 11 21) (bvref-32 gspace-bytes gspace-byte-offset))))))))
1817 (assert (zerop (ash value -28)))
1818 (setf (ldb (byte 26 0)
1819 (bvref-32 gspace-bytes gspace-byte-offset))
1822 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1823 (logior (mask-field (byte 16 16)
1824 (bvref-32 gspace-bytes gspace-byte-offset))
1825 (ash (1+ (ldb (byte 17 15) value)) -1))))
1827 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1828 (logior (mask-field (byte 16 16)
1829 (bvref-32 gspace-bytes gspace-byte-offset))
1830 (ldb (byte 16 0) value))))))
1831 ;; FIXME: PowerPC Fixups are not fully implemented. The bit
1832 ;; here starts to set things up to work properly, but there
1833 ;; needs to be corresponding code in ppc-vm.lisp
1837 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1838 (dpb (ash value -2) (byte 24 2)
1839 (bvref-32 gspace-bytes gspace-byte-offset))))
1841 (let* ((un-fixed-up (bvref-16 gspace-bytes
1842 (+ gspace-byte-offset 2)))
1843 (fixed-up (+ un-fixed-up value))
1844 (h (ldb (byte 16 16) fixed-up))
1845 (l (ldb (byte 16 0) fixed-up)))
1846 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1847 (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
1849 (let* ((un-fixed-up (bvref-16 gspace-bytes
1850 (+ gspace-byte-offset 2)))
1851 (fixed-up (+ un-fixed-up value)))
1852 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1853 (ldb (byte 16 0) fixed-up))))))
1857 (error "can't deal with call fixups yet"))
1859 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1860 (dpb (ldb (byte 22 10) value)
1862 (bvref-32 gspace-bytes gspace-byte-offset))))
1864 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1865 (dpb (ldb (byte 10 0) value)
1867 (bvref-32 gspace-bytes gspace-byte-offset))))))
1869 ;; XXX: Note that un-fixed-up is read via bvref-word, which is
1870 ;; 64 bits wide on x86-64, but the fixed-up value is written
1871 ;; via bvref-32. This would make more sense if we supported
1872 ;; :absolute64 fixups, but apparently the cross-compiler
1873 ;; doesn't dump them.
1874 (let* ((un-fixed-up (bvref-word gspace-bytes
1875 gspace-byte-offset))
1876 (code-object-start-addr (logandc2 (descriptor-bits code-object)
1877 sb!vm:lowtag-mask)))
1878 (assert (= code-object-start-addr
1879 (+ gspace-byte-address
1880 (descriptor-byte-offset code-object))))
1883 (let ((fixed-up (+ value un-fixed-up)))
1884 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1886 ;; comment from CMU CL sources:
1888 ;; Note absolute fixups that point within the object.
1889 ;; KLUDGE: There seems to be an implicit assumption in
1890 ;; the old CMU CL code here, that if it doesn't point
1891 ;; before the object, it must point within the object
1892 ;; (not beyond it). It would be good to add an
1893 ;; explanation of why that's true, or an assertion that
1894 ;; it's really true, or both.
1896 ;; One possible explanation is that all absolute fixups
1897 ;; point either within the code object, within the
1898 ;; runtime, within read-only or static-space, or within
1899 ;; the linkage-table space. In all x86 configurations,
1900 ;; these areas are prior to the start of dynamic space,
1901 ;; where all the code-objects are loaded.
1903 (unless (< fixed-up code-object-start-addr)
1904 (note-load-time-code-fixup code-object
1906 (:relative ; (used for arguments to X86 relative CALL instruction)
1907 (let ((fixed-up (- (+ value un-fixed-up)
1910 4))) ; "length of CALL argument"
1911 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1913 ;; Note relative fixups that point outside the code
1914 ;; object, which is to say all relative fixups, since
1915 ;; relative addressing within a code object never needs
1918 (note-load-time-code-fixup code-object
1919 after-header))))))))
1922 (defun resolve-assembler-fixups ()
1923 (dolist (fixup *cold-assembler-fixups*)
1924 (let* ((routine (car fixup))
1925 (value (lookup-assembler-reference routine)))
1927 (do-cold-fixup (second fixup) (third fixup) value (fourth fixup))))))
1929 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
1930 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
1931 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
1932 ;;; target-load.lisp refers to.
1933 (defun foreign-symbols-to-core ()
1935 (result *nil-descriptor*))
1936 (maphash (lambda (symbol value)
1937 (push (cons symbol value) symbols))
1938 *cold-foreign-symbol-table*)
1939 (setq symbols (sort symbols #'string< :key #'car))
1940 (dolist (symbol symbols)
1941 (cold-push (cold-cons (base-string-to-core (car symbol))
1942 (number-to-core (cdr symbol)))
1944 (cold-set (cold-intern 'sb!kernel:*!initial-foreign-symbols*) result))
1945 (let ((result *nil-descriptor*))
1946 (dolist (rtn (sort (copy-list *cold-assembler-routines*) #'string< :key #'car))
1947 (cold-push (cold-cons (cold-intern (car rtn))
1948 (number-to-core (cdr rtn)))
1950 (cold-set (cold-intern '*!initial-assembler-routines*) result)))
1953 ;;;; general machinery for cold-loading FASL files
1955 ;;; FOP functions for cold loading
1956 (defvar *cold-fop-funs*
1957 ;; We start out with a copy of the ordinary *FOP-FUNS*. The ones
1958 ;; which aren't appropriate for cold load will be destructively
1960 (copy-seq *fop-funs*))
1962 ;;; Cause a fop to have a special definition for cold load.
1964 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
1965 ;;; (1) looks up the code for this name (created by a previous
1966 ;; DEFINE-FOP) instead of creating a code, and
1967 ;;; (2) stores its definition in the *COLD-FOP-FUNS* vector,
1968 ;;; instead of storing in the *FOP-FUNS* vector.
1969 (defmacro define-cold-fop ((name &key (pushp t) (stackp t)) &rest forms)
1970 (aver (member pushp '(nil t)))
1971 (aver (member stackp '(nil t)))
1972 (let ((code (get name 'fop-code))
1973 (fname (symbolicate "COLD-" name)))
1975 (error "~S is not a defined FOP." name))
1979 `((with-fop-stack ,pushp ,@forms))
1981 (setf (svref *cold-fop-funs* ,code) #',fname))))
1983 (defmacro clone-cold-fop ((name &key (pushp t) (stackp t))
1986 (aver (member pushp '(nil t)))
1987 (aver (member stackp '(nil t)))
1989 (macrolet ((clone-arg () '(read-word-arg)))
1990 (define-cold-fop (,name :pushp ,pushp :stackp ,stackp) ,@forms))
1991 (macrolet ((clone-arg () '(read-byte-arg)))
1992 (define-cold-fop (,small-name :pushp ,pushp :stackp ,stackp) ,@forms))))
1994 ;;; Cause a fop to be undefined in cold load.
1995 (defmacro not-cold-fop (name)
1996 `(define-cold-fop (,name)
1997 (error "The fop ~S is not supported in cold load." ',name)))
1999 ;;; COLD-LOAD loads stuff into the core image being built by calling
2000 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
2001 ;;; loading functions.
2002 (defun cold-load (filename)
2004 "Load the file named by FILENAME into the cold load image being built."
2005 (let* ((*fop-funs* *cold-fop-funs*)
2006 (*cold-load-filename* (etypecase filename
2008 (pathname (namestring filename)))))
2009 (with-open-file (s filename :element-type '(unsigned-byte 8))
2010 (load-as-fasl s nil nil))))
2012 ;;;; miscellaneous cold fops
2014 (define-cold-fop (fop-misc-trap) *unbound-marker*)
2016 (define-cold-fop (fop-short-character)
2017 (make-character-descriptor (read-byte-arg)))
2019 (define-cold-fop (fop-empty-list) nil)
2020 (define-cold-fop (fop-truth) t)
2022 (clone-cold-fop (fop-struct)
2024 (let* ((size (clone-arg))
2025 (result (allocate-boxed-object *dynamic*
2027 sb!vm:instance-pointer-lowtag))
2028 (layout (pop-stack))
2033 (+ sb!vm:instance-slots-offset
2034 (target-layout-index 'n-untagged-slots)))))
2035 (ntagged (- size nuntagged)))
2036 (write-memory result (make-other-immediate-descriptor
2037 size sb!vm:instance-header-widetag))
2038 (write-wordindexed result sb!vm:instance-slots-offset layout)
2039 (do ((index 1 (1+ index)))
2041 (declare (fixnum index))
2042 (write-wordindexed result
2043 (+ index sb!vm:instance-slots-offset)
2044 (if (>= index ntagged)
2045 (descriptor-word-sized-integer (pop-stack))
2049 (define-cold-fop (fop-layout)
2050 (let* ((nuntagged-des (pop-stack))
2051 (length-des (pop-stack))
2052 (depthoid-des (pop-stack))
2053 (cold-inherits (pop-stack))
2055 (old (gethash name *cold-layouts*)))
2056 (declare (type descriptor length-des depthoid-des cold-inherits))
2057 (declare (type symbol name))
2058 ;; If a layout of this name has been defined already
2060 ;; Enforce consistency between the previous definition and the
2061 ;; current definition, then return the previous definition.
2063 ;; FIXME: This would be more maintainable if we used
2064 ;; DEFSTRUCT (:TYPE LIST) to define COLD-LAYOUT. -- WHN 19990825
2065 (old-layout-descriptor
2072 (declare (type descriptor old-layout-descriptor))
2073 (declare (type index old-length old-nuntagged))
2074 (declare (type fixnum old-depthoid))
2075 (declare (type list old-inherits-list))
2076 (aver (eq name old-name))
2077 (let ((length (descriptor-fixnum length-des))
2078 (inherits-list (listify-cold-inherits cold-inherits))
2079 (depthoid (descriptor-fixnum depthoid-des))
2080 (nuntagged (descriptor-fixnum nuntagged-des)))
2081 (unless (= length old-length)
2082 (error "cold loading a reference to class ~S when the compile~%~
2083 time length was ~S and current length is ~S"
2087 (unless (equal inherits-list old-inherits-list)
2088 (error "cold loading a reference to class ~S when the compile~%~
2089 time inherits were ~S~%~
2090 and current inherits are ~S"
2094 (unless (= depthoid old-depthoid)
2095 (error "cold loading a reference to class ~S when the compile~%~
2096 time inheritance depthoid was ~S and current inheritance~%~
2101 (unless (= nuntagged old-nuntagged)
2102 (error "cold loading a reference to class ~S when the compile~%~
2103 time number of untagged slots was ~S and is currently ~S"
2107 old-layout-descriptor)
2108 ;; Make a new definition from scratch.
2109 (make-cold-layout name length-des cold-inherits depthoid-des
2112 ;;;; cold fops for loading symbols
2114 ;;; Load a symbol SIZE characters long from *FASL-INPUT-STREAM* and
2115 ;;; intern that symbol in PACKAGE.
2116 (defun cold-load-symbol (size package)
2117 (let ((string (make-string size)))
2118 (read-string-as-bytes *fasl-input-stream* string)
2119 (intern string package)))
2121 (macrolet ((frob (name pname-len package-len)
2122 `(define-cold-fop (,name)
2123 (let ((index (read-arg ,package-len)))
2125 (cold-load-symbol (read-arg ,pname-len)
2126 (ref-fop-table index)))))))
2127 (frob fop-symbol-in-package-save #.sb!vm:n-word-bytes #.sb!vm:n-word-bytes)
2128 (frob fop-small-symbol-in-package-save 1 #.sb!vm:n-word-bytes)
2129 (frob fop-symbol-in-byte-package-save #.sb!vm:n-word-bytes 1)
2130 (frob fop-small-symbol-in-byte-package-save 1 1))
2132 (clone-cold-fop (fop-lisp-symbol-save)
2133 (fop-lisp-small-symbol-save)
2134 (push-fop-table (cold-load-symbol (clone-arg) *cl-package*)))
2136 (clone-cold-fop (fop-keyword-symbol-save)
2137 (fop-keyword-small-symbol-save)
2138 (push-fop-table (cold-load-symbol (clone-arg) *keyword-package*)))
2140 (clone-cold-fop (fop-uninterned-symbol-save)
2141 (fop-uninterned-small-symbol-save)
2142 (let* ((size (clone-arg))
2143 (name (make-string size)))
2144 (read-string-as-bytes *fasl-input-stream* name)
2145 (let ((symbol-des (allocate-symbol name)))
2146 (push-fop-table symbol-des))))
2148 ;;;; cold fops for loading packages
2150 (clone-cold-fop (fop-named-package-save :stackp nil)
2151 (fop-small-named-package-save)
2152 (let* ((size (clone-arg))
2153 (name (make-string size)))
2154 (read-string-as-bytes *fasl-input-stream* name)
2155 (push-fop-table (find-package name))))
2157 ;;;; cold fops for loading lists
2159 ;;; Make a list of the top LENGTH things on the fop stack. The last
2160 ;;; cdr of the list is set to LAST.
2161 (defmacro cold-stack-list (length last)
2162 `(do* ((index ,length (1- index))
2163 (result ,last (cold-cons (pop-stack) result)))
2164 ((= index 0) result)
2165 (declare (fixnum index))))
2167 (define-cold-fop (fop-list)
2168 (cold-stack-list (read-byte-arg) *nil-descriptor*))
2169 (define-cold-fop (fop-list*)
2170 (cold-stack-list (read-byte-arg) (pop-stack)))
2171 (define-cold-fop (fop-list-1)
2172 (cold-stack-list 1 *nil-descriptor*))
2173 (define-cold-fop (fop-list-2)
2174 (cold-stack-list 2 *nil-descriptor*))
2175 (define-cold-fop (fop-list-3)
2176 (cold-stack-list 3 *nil-descriptor*))
2177 (define-cold-fop (fop-list-4)
2178 (cold-stack-list 4 *nil-descriptor*))
2179 (define-cold-fop (fop-list-5)
2180 (cold-stack-list 5 *nil-descriptor*))
2181 (define-cold-fop (fop-list-6)
2182 (cold-stack-list 6 *nil-descriptor*))
2183 (define-cold-fop (fop-list-7)
2184 (cold-stack-list 7 *nil-descriptor*))
2185 (define-cold-fop (fop-list-8)
2186 (cold-stack-list 8 *nil-descriptor*))
2187 (define-cold-fop (fop-list*-1)
2188 (cold-stack-list 1 (pop-stack)))
2189 (define-cold-fop (fop-list*-2)
2190 (cold-stack-list 2 (pop-stack)))
2191 (define-cold-fop (fop-list*-3)
2192 (cold-stack-list 3 (pop-stack)))
2193 (define-cold-fop (fop-list*-4)
2194 (cold-stack-list 4 (pop-stack)))
2195 (define-cold-fop (fop-list*-5)
2196 (cold-stack-list 5 (pop-stack)))
2197 (define-cold-fop (fop-list*-6)
2198 (cold-stack-list 6 (pop-stack)))
2199 (define-cold-fop (fop-list*-7)
2200 (cold-stack-list 7 (pop-stack)))
2201 (define-cold-fop (fop-list*-8)
2202 (cold-stack-list 8 (pop-stack)))
2204 ;;;; cold fops for loading vectors
2206 (clone-cold-fop (fop-base-string)
2207 (fop-small-base-string)
2208 (let* ((len (clone-arg))
2209 (string (make-string len)))
2210 (read-string-as-bytes *fasl-input-stream* string)
2211 (base-string-to-core string)))
2214 (clone-cold-fop (fop-character-string)
2215 (fop-small-character-string)
2216 (bug "CHARACTER-STRING dumped by cross-compiler."))
2218 (clone-cold-fop (fop-vector)
2220 (let* ((size (clone-arg))
2221 (result (allocate-vector-object *dynamic*
2224 sb!vm:simple-vector-widetag)))
2225 (do ((index (1- size) (1- index)))
2227 (declare (fixnum index))
2228 (write-wordindexed result
2229 (+ index sb!vm:vector-data-offset)
2233 (define-cold-fop (fop-int-vector)
2234 (let* ((len (read-word-arg))
2235 (sizebits (read-byte-arg))
2236 (type (case sizebits
2237 (0 sb!vm:simple-array-nil-widetag)
2238 (1 sb!vm:simple-bit-vector-widetag)
2239 (2 sb!vm:simple-array-unsigned-byte-2-widetag)
2240 (4 sb!vm:simple-array-unsigned-byte-4-widetag)
2241 (7 (prog1 sb!vm:simple-array-unsigned-byte-7-widetag
2243 (8 sb!vm:simple-array-unsigned-byte-8-widetag)
2244 (15 (prog1 sb!vm:simple-array-unsigned-byte-15-widetag
2245 (setf sizebits 16)))
2246 (16 sb!vm:simple-array-unsigned-byte-16-widetag)
2247 (31 (prog1 sb!vm:simple-array-unsigned-byte-31-widetag
2248 (setf sizebits 32)))
2249 (32 sb!vm:simple-array-unsigned-byte-32-widetag)
2250 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
2251 (63 (prog1 sb!vm:simple-array-unsigned-byte-63-widetag
2252 (setf sizebits 64)))
2253 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
2254 (64 sb!vm:simple-array-unsigned-byte-64-widetag)
2255 (t (error "losing element size: ~W" sizebits))))
2256 (result (allocate-vector-object *dynamic* sizebits len type))
2257 (start (+ (descriptor-byte-offset result)
2258 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2260 (ceiling (* len sizebits)
2261 sb!vm:n-byte-bits))))
2262 (read-bigvec-as-sequence-or-die (descriptor-bytes result)
2268 (define-cold-fop (fop-single-float-vector)
2269 (let* ((len (read-word-arg))
2270 (result (allocate-vector-object
2274 sb!vm:simple-array-single-float-widetag))
2275 (start (+ (descriptor-byte-offset result)
2276 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2277 (end (+ start (* len 4))))
2278 (read-bigvec-as-sequence-or-die (descriptor-bytes result)
2284 (not-cold-fop fop-double-float-vector)
2285 #!+long-float (not-cold-fop fop-long-float-vector)
2286 (not-cold-fop fop-complex-single-float-vector)
2287 (not-cold-fop fop-complex-double-float-vector)
2288 #!+long-float (not-cold-fop fop-complex-long-float-vector)
2290 (define-cold-fop (fop-array)
2291 (let* ((rank (read-word-arg))
2292 (data-vector (pop-stack))
2293 (result (allocate-boxed-object *dynamic*
2294 (+ sb!vm:array-dimensions-offset rank)
2295 sb!vm:other-pointer-lowtag)))
2296 (write-memory result
2297 (make-other-immediate-descriptor rank
2298 sb!vm:simple-array-widetag))
2299 (write-wordindexed result sb!vm:array-fill-pointer-slot *nil-descriptor*)
2300 (write-wordindexed result sb!vm:array-data-slot data-vector)
2301 (write-wordindexed result sb!vm:array-displacement-slot *nil-descriptor*)
2302 (write-wordindexed result sb!vm:array-displaced-p-slot *nil-descriptor*)
2303 (write-wordindexed result sb!vm:array-displaced-from-slot *nil-descriptor*)
2304 (let ((total-elements 1))
2305 (dotimes (axis rank)
2306 (let ((dim (pop-stack)))
2307 (unless (is-fixnum-lowtag (descriptor-lowtag dim))
2308 (error "non-fixnum dimension? (~S)" dim))
2309 (setf total-elements
2311 (logior (ash (descriptor-high dim)
2312 (- descriptor-low-bits
2313 sb!vm:n-fixnum-tag-bits))
2314 (ash (descriptor-low dim)
2315 sb!vm:n-fixnum-tag-bits))))
2316 (write-wordindexed result
2317 (+ sb!vm:array-dimensions-offset axis)
2319 (write-wordindexed result
2320 sb!vm:array-elements-slot
2321 (make-fixnum-descriptor total-elements)))
2325 ;;;; cold fops for loading numbers
2327 (defmacro define-cold-number-fop (fop)
2328 `(define-cold-fop (,fop :stackp nil)
2329 ;; Invoke the ordinary warm version of this fop to push the
2332 ;; Replace the warm fop result with the cold image of the warm
2335 (let ((number (pop-stack)))
2336 (number-to-core number)))))
2338 (define-cold-number-fop fop-single-float)
2339 (define-cold-number-fop fop-double-float)
2340 (define-cold-number-fop fop-integer)
2341 (define-cold-number-fop fop-small-integer)
2342 (define-cold-number-fop fop-word-integer)
2343 (define-cold-number-fop fop-byte-integer)
2344 (define-cold-number-fop fop-complex-single-float)
2345 (define-cold-number-fop fop-complex-double-float)
2347 (define-cold-fop (fop-ratio)
2348 (let ((den (pop-stack)))
2349 (number-pair-to-core (pop-stack) den sb!vm:ratio-widetag)))
2351 (define-cold-fop (fop-complex)
2352 (let ((im (pop-stack)))
2353 (number-pair-to-core (pop-stack) im sb!vm:complex-widetag)))
2355 ;;;; cold fops for calling (or not calling)
2357 (not-cold-fop fop-eval)
2358 (not-cold-fop fop-eval-for-effect)
2360 (defvar *load-time-value-counter*)
2362 (define-cold-fop (fop-funcall)
2363 (unless (= (read-byte-arg) 0)
2364 (error "You can't FOP-FUNCALL arbitrary stuff in cold load."))
2365 (let ((counter *load-time-value-counter*))
2366 (cold-push (cold-cons
2367 (cold-intern :load-time-value)
2371 (number-to-core counter)
2373 *current-reversed-cold-toplevels*)
2374 (setf *load-time-value-counter* (1+ counter))
2375 (make-descriptor 0 0 :load-time-value counter)))
2377 (defun finalize-load-time-value-noise ()
2378 (cold-set (cold-intern '*!load-time-values*)
2379 (allocate-vector-object *dynamic*
2381 *load-time-value-counter*
2382 sb!vm:simple-vector-widetag)))
2384 (define-cold-fop (fop-funcall-for-effect :pushp nil)
2385 (if (= (read-byte-arg) 0)
2386 (cold-push (pop-stack)
2387 *current-reversed-cold-toplevels*)
2388 (error "You can't FOP-FUNCALL arbitrary stuff in cold load.")))
2390 ;;;; cold fops for fixing up circularities
2392 (define-cold-fop (fop-rplaca :pushp nil)
2393 (let ((obj (ref-fop-table (read-word-arg)))
2394 (idx (read-word-arg)))
2395 (write-memory (cold-nthcdr idx obj) (pop-stack))))
2397 (define-cold-fop (fop-rplacd :pushp nil)
2398 (let ((obj (ref-fop-table (read-word-arg)))
2399 (idx (read-word-arg)))
2400 (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2402 (define-cold-fop (fop-svset :pushp nil)
2403 (let ((obj (ref-fop-table (read-word-arg)))
2404 (idx (read-word-arg)))
2405 (write-wordindexed obj
2407 (ecase (descriptor-lowtag obj)
2408 (#.sb!vm:instance-pointer-lowtag 1)
2409 (#.sb!vm:other-pointer-lowtag 2)))
2412 (define-cold-fop (fop-structset :pushp nil)
2413 (let ((obj (ref-fop-table (read-word-arg)))
2414 (idx (read-word-arg)))
2415 (write-wordindexed obj (1+ idx) (pop-stack))))
2417 ;;; In the original CMUCL code, this actually explicitly declared PUSHP
2418 ;;; to be T, even though that's what it defaults to in DEFINE-COLD-FOP.
2419 (define-cold-fop (fop-nthcdr)
2420 (cold-nthcdr (read-word-arg) (pop-stack)))
2422 (defun cold-nthcdr (index obj)
2424 (setq obj (read-wordindexed obj 1)))
2427 ;;;; cold fops for loading code objects and functions
2429 ;;; the names of things which have had COLD-FSET used on them already
2430 ;;; (used to make sure that we don't try to statically link a name to
2431 ;;; more than one definition)
2432 (defparameter *cold-fset-warm-names*
2433 ;; This can't be an EQL hash table because names can be conses, e.g.
2435 (make-hash-table :test 'equal))
2437 (define-cold-fop (fop-fset :pushp nil)
2438 (let* ((fn (pop-stack))
2439 (cold-name (pop-stack))
2440 (warm-name (warm-fun-name cold-name)))
2441 (if (gethash warm-name *cold-fset-warm-names*)
2442 (error "duplicate COLD-FSET for ~S" warm-name)
2443 (setf (gethash warm-name *cold-fset-warm-names*) t))
2444 (static-fset cold-name fn)))
2446 (define-cold-fop (fop-note-debug-source :pushp nil)
2447 (let ((debug-source (pop-stack)))
2448 (cold-push debug-source *current-debug-sources*)))
2450 (define-cold-fop (fop-fdefinition)
2451 (cold-fdefinition-object (pop-stack)))
2453 (define-cold-fop (fop-sanctify-for-execution)
2456 ;;; Setting this variable shows what code looks like before any
2457 ;;; fixups (or function headers) are applied.
2458 #!+sb-show (defvar *show-pre-fixup-code-p* nil)
2460 ;;; FIXME: The logic here should be converted into a function
2461 ;;; COLD-CODE-FOP-GUTS (NCONST CODE-SIZE) called by DEFINE-COLD-FOP
2462 ;;; FOP-CODE and DEFINE-COLD-FOP FOP-SMALL-CODE, so that
2463 ;;; variable-capture nastiness like (LET ((NCONST ,NCONST) ..) ..)
2464 ;;; doesn't keep me awake at night.
2465 (defmacro define-cold-code-fop (name nconst code-size)
2466 `(define-cold-fop (,name)
2467 (let* ((nconst ,nconst)
2468 (code-size ,code-size)
2469 (raw-header-n-words (+ sb!vm:code-trace-table-offset-slot nconst))
2471 ;; Note: we round the number of constants up to ensure
2472 ;; that the code vector will be properly aligned.
2473 (round-up raw-header-n-words 2))
2474 (des (allocate-cold-descriptor *dynamic*
2475 (+ (ash header-n-words
2478 sb!vm:other-pointer-lowtag)))
2480 (make-other-immediate-descriptor
2481 header-n-words sb!vm:code-header-widetag))
2482 (write-wordindexed des
2483 sb!vm:code-code-size-slot
2484 (make-fixnum-descriptor
2485 (ash (+ code-size (1- (ash 1 sb!vm:word-shift)))
2486 (- sb!vm:word-shift))))
2487 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2488 (write-wordindexed des sb!vm:code-debug-info-slot (pop-stack))
2489 (when (oddp raw-header-n-words)
2490 (write-wordindexed des
2492 (make-random-descriptor 0)))
2493 (do ((index (1- raw-header-n-words) (1- index)))
2494 ((< index sb!vm:code-trace-table-offset-slot))
2495 (write-wordindexed des index (pop-stack)))
2496 (let* ((start (+ (descriptor-byte-offset des)
2497 (ash header-n-words sb!vm:word-shift)))
2498 (end (+ start code-size)))
2499 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2504 (when *show-pre-fixup-code-p*
2505 (format *trace-output*
2506 "~&/raw code from code-fop ~W ~W:~%"
2509 (do ((i start (+ i sb!vm:n-word-bytes)))
2511 (format *trace-output*
2512 "/#X~8,'0x: #X~8,'0x~%"
2513 (+ i (gspace-byte-address (descriptor-gspace des)))
2514 (bvref-32 (descriptor-bytes des) i)))))
2517 (define-cold-code-fop fop-code (read-word-arg) (read-word-arg))
2519 (define-cold-code-fop fop-small-code (read-byte-arg) (read-halfword-arg))
2521 (clone-cold-fop (fop-alter-code :pushp nil)
2522 (fop-byte-alter-code)
2523 (let ((slot (clone-arg))
2526 (write-wordindexed code slot value)))
2528 (define-cold-fop (fop-fun-entry)
2529 (let* ((info (pop-stack))
2531 (arglist (pop-stack))
2533 (code-object (pop-stack))
2534 (offset (calc-offset code-object (read-word-arg)))
2535 (fn (descriptor-beyond code-object
2537 sb!vm:fun-pointer-lowtag))
2538 (next (read-wordindexed code-object sb!vm:code-entry-points-slot)))
2539 (unless (zerop (logand offset sb!vm:lowtag-mask))
2540 (error "unaligned function entry: ~S at #X~X" name offset))
2541 (write-wordindexed code-object sb!vm:code-entry-points-slot fn)
2543 (make-other-immediate-descriptor
2544 (ash offset (- sb!vm:word-shift))
2545 sb!vm:simple-fun-header-widetag))
2546 (write-wordindexed fn
2547 sb!vm:simple-fun-self-slot
2548 ;; KLUDGE: Wiring decisions like this in at
2549 ;; this level ("if it's an x86") instead of a
2550 ;; higher level of abstraction ("if it has such
2551 ;; and such relocation peculiarities (which
2552 ;; happen to be confined to the x86)") is bad.
2553 ;; It would be nice if the code were instead
2554 ;; conditional on some more descriptive
2555 ;; feature, :STICKY-CODE or
2556 ;; :LOAD-GC-INTERACTION or something.
2558 ;; FIXME: The X86 definition of the function
2559 ;; self slot breaks everything object.tex says
2560 ;; about it. (As far as I can tell, the X86
2561 ;; definition makes it a pointer to the actual
2562 ;; code instead of a pointer back to the object
2563 ;; itself.) Ask on the mailing list whether
2564 ;; this is documented somewhere, and if not,
2565 ;; try to reverse engineer some documentation.
2567 ;; a pointer back to the function object, as
2568 ;; described in CMU CL
2569 ;; src/docs/internals/object.tex
2572 ;; KLUDGE: a pointer to the actual code of the
2573 ;; object, as described nowhere that I can find
2575 (make-random-descriptor
2576 (+ (descriptor-bits fn)
2577 (- (ash sb!vm:simple-fun-code-offset
2579 ;; FIXME: We should mask out the type
2580 ;; bits, not assume we know what they
2581 ;; are and subtract them out this way.
2582 sb!vm:fun-pointer-lowtag))))
2583 (write-wordindexed fn sb!vm:simple-fun-next-slot next)
2584 (write-wordindexed fn sb!vm:simple-fun-name-slot name)
2585 (write-wordindexed fn sb!vm:simple-fun-arglist-slot arglist)
2586 (write-wordindexed fn sb!vm:simple-fun-type-slot type)
2587 (write-wordindexed fn sb!vm::simple-fun-info-slot info)
2590 (define-cold-fop (fop-foreign-fixup)
2591 (let* ((kind (pop-stack))
2592 (code-object (pop-stack))
2593 (len (read-byte-arg))
2594 (sym (make-string len)))
2595 (read-string-as-bytes *fasl-input-stream* sym)
2596 (let ((offset (read-word-arg))
2597 (value (cold-foreign-symbol-address sym)))
2598 (do-cold-fixup code-object offset value kind))
2602 (define-cold-fop (fop-foreign-dataref-fixup)
2603 (let* ((kind (pop-stack))
2604 (code-object (pop-stack))
2605 (len (read-byte-arg))
2606 (sym (make-string len)))
2607 (read-string-as-bytes *fasl-input-stream* sym)
2608 (maphash (lambda (k v)
2609 (format *error-output* "~&~S = #X~8X~%" k v))
2610 *cold-foreign-symbol-table*)
2611 (error "shared foreign symbol in cold load: ~S (~S)" sym kind)))
2613 (define-cold-fop (fop-assembler-code)
2614 (let* ((length (read-word-arg))
2616 ;; Note: we round the number of constants up to ensure that
2617 ;; the code vector will be properly aligned.
2618 (round-up sb!vm:code-constants-offset 2))
2619 (des (allocate-cold-descriptor *read-only*
2620 (+ (ash header-n-words
2623 sb!vm:other-pointer-lowtag)))
2625 (make-other-immediate-descriptor
2626 header-n-words sb!vm:code-header-widetag))
2627 (write-wordindexed des
2628 sb!vm:code-code-size-slot
2629 (make-fixnum-descriptor
2630 (ash (+ length (1- (ash 1 sb!vm:word-shift)))
2631 (- sb!vm:word-shift))))
2632 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2633 (write-wordindexed des sb!vm:code-debug-info-slot *nil-descriptor*)
2635 (let* ((start (+ (descriptor-byte-offset des)
2636 (ash header-n-words sb!vm:word-shift)))
2637 (end (+ start length)))
2638 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2644 (define-cold-fop (fop-assembler-routine)
2645 (let* ((routine (pop-stack))
2647 (offset (calc-offset des (read-word-arg))))
2648 (record-cold-assembler-routine
2650 (+ (logandc2 (descriptor-bits des) sb!vm:lowtag-mask) offset))
2653 (define-cold-fop (fop-assembler-fixup)
2654 (let* ((routine (pop-stack))
2656 (code-object (pop-stack))
2657 (offset (read-word-arg)))
2658 (record-cold-assembler-fixup routine code-object offset kind)
2661 (define-cold-fop (fop-code-object-fixup)
2662 (let* ((kind (pop-stack))
2663 (code-object (pop-stack))
2664 (offset (read-word-arg))
2665 (value (descriptor-bits code-object)))
2666 (do-cold-fixup code-object offset value kind)
2669 ;;;; sanity checking space layouts
2671 (defun check-spaces ()
2672 ;;; Co-opt type machinery to check for intersections...
2674 (flet ((check (start end space)
2675 (unless (< start end)
2676 (error "Bogus space: ~A" space))
2677 (let ((type (specifier-type `(integer ,start ,end))))
2678 (dolist (other types)
2679 (unless (eq *empty-type* (type-intersection (cdr other) type))
2680 (error "Space overlap: ~A with ~A" space (car other))))
2681 (push (cons space type) types))))
2682 (check sb!vm:read-only-space-start sb!vm:read-only-space-end :read-only)
2683 (check sb!vm:static-space-start sb!vm:static-space-end :static)
2685 (check sb!vm:dynamic-space-start sb!vm:dynamic-space-end :dynamic)
2688 (check sb!vm:dynamic-0-space-start sb!vm:dynamic-0-space-end :dynamic-0)
2689 (check sb!vm:dynamic-1-space-start sb!vm:dynamic-1-space-end :dynamic-1))
2691 (check sb!vm:linkage-table-space-start sb!vm:linkage-table-space-end :linkage-table))))
2693 ;;;; emitting C header file
2695 (defun tailwise-equal (string tail)
2696 (and (>= (length string) (length tail))
2697 (string= string tail :start1 (- (length string) (length tail)))))
2699 (defun write-boilerplate ()
2702 '("This is a machine-generated file. Please do not edit it by hand."
2703 "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
2705 "This file contains low-level information about the"
2706 "internals of a particular version and configuration"
2707 "of SBCL. It is used by the C compiler to create a runtime"
2708 "support environment, an executable program in the host"
2709 "operating system's native format, which can then be used to"
2710 "load and run 'core' files, which are basically programs"
2711 "in SBCL's own format."))
2712 (format t " *~@[ ~A~]~%" line))
2715 (defun c-name (string &optional strip)
2717 (substitute-if #\_ (lambda (c) (member c '(#\- #\/ #\%)))
2718 (remove-if (lambda (c) (position c strip))
2721 (defun c-symbol-name (symbol &optional strip)
2722 (c-name (symbol-name symbol) strip))
2724 (defun write-makefile-features ()
2725 ;; propagating *SHEBANG-FEATURES* into the Makefiles
2726 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2727 sb-cold:*shebang-features*)
2729 (format t "LISP_FEATURE_~A=1~%" shebang-feature-name)))
2731 (defun write-config-h ()
2732 ;; propagating *SHEBANG-FEATURES* into C-level #define's
2733 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2734 sb-cold:*shebang-features*)
2736 (format t "#define LISP_FEATURE_~A~%" shebang-feature-name))
2738 ;; and miscellaneous constants
2739 (format t "#define SBCL_CORE_VERSION_INTEGER ~D~%" sbcl-core-version-integer)
2741 "#define SBCL_VERSION_STRING ~S~%"
2742 (sb!xc:lisp-implementation-version))
2743 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
2744 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2745 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
2746 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2747 (format t "#define LISPOBJ(thing) thing~2%")
2748 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")
2751 (defun write-constants-h ()
2752 ;; writing entire families of named constants
2753 (let ((constants nil))
2754 (dolist (package-name '( ;; Even in CMU CL, constants from VM
2755 ;; were automatically propagated
2756 ;; into the runtime.
2758 ;; In SBCL, we also propagate various
2759 ;; magic numbers related to file format,
2760 ;; which live here instead of SB!VM.
2762 (do-external-symbols (symbol (find-package package-name))
2763 (when (constantp symbol)
2764 (let ((name (symbol-name symbol)))
2765 (labels ( ;; shared machinery
2766 (record (string priority suffix)
2769 (symbol-value symbol)
2771 (documentation symbol 'variable))
2773 ;; machinery for old-style CMU CL Lisp-to-C
2774 ;; arbitrary renaming, being phased out in favor of
2775 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
2777 (record-with-munged-name (prefix string priority)
2778 (record (concatenate
2781 (delete #\- (string-capitalize string)))
2784 (maybe-record-with-munged-name (tail prefix priority)
2785 (when (tailwise-equal name tail)
2786 (record-with-munged-name prefix
2791 ;; machinery for new-style SBCL Lisp-to-C naming
2792 (record-with-translated-name (priority large)
2793 (record (c-name name) priority (if large "LU" "")))
2794 (maybe-record-with-translated-name (suffixes priority &key large)
2795 (when (some (lambda (suffix)
2796 (tailwise-equal name suffix))
2798 (record-with-translated-name priority large))))
2799 (maybe-record-with-translated-name '("-LOWTAG") 0)
2800 (maybe-record-with-translated-name '("-WIDETAG" "-SHIFT") 1)
2801 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
2802 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
2803 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
2804 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
2805 (maybe-record-with-translated-name '("-SIZE") 6)
2806 (maybe-record-with-translated-name '("-START" "-END" "-PAGE-BYTES"
2807 "-CARD-BYTES" "-GRANULARITY")
2809 (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 8)
2810 (maybe-record-with-translated-name '("-CORE-SPACE-ID") 9)
2811 (maybe-record-with-translated-name '("-CORE-SPACE-ID-FLAG") 9)
2812 (maybe-record-with-translated-name '("-GENERATION+") 10))))))
2813 ;; KLUDGE: these constants are sort of important, but there's no
2814 ;; pleasing way to inform the code above about them. So we fake
2815 ;; it for now. nikodemus on #lisp (2004-08-09) suggested simply
2816 ;; exporting every numeric constant from SB!VM; that would work,
2817 ;; but the C runtime would have to be altered to use Lisp-like names
2818 ;; rather than the munged names currently exported. --njf, 2004-08-09
2819 (dolist (c '(sb!vm:n-word-bits sb!vm:n-word-bytes
2820 sb!vm:n-lowtag-bits sb!vm:lowtag-mask
2821 sb!vm:n-widetag-bits sb!vm:widetag-mask
2822 sb!vm:n-fixnum-tag-bits sb!vm:fixnum-tag-mask))
2823 (push (list (c-symbol-name c)
2824 -1 ; invent a new priority
2829 ;; One more symbol that doesn't fit into the code above.
2830 (let ((c 'sb!impl::+magic-hash-vector-value+))
2831 (push (list (c-symbol-name c)
2839 (lambda (const1 const2)
2840 (if (= (second const1) (second const2))
2841 (if (= (third const1) (third const2))
2842 (string< (first const1) (first const2))
2843 (< (third const1) (third const2)))
2844 (< (second const1) (second const2))))))
2845 (let ((prev-priority (second (car constants))))
2846 (dolist (const constants)
2847 (destructuring-bind (name priority value suffix doc) const
2848 (unless (= prev-priority priority)
2850 (setf prev-priority priority))
2851 (when (minusp value)
2852 (error "stub: negative values unsupported"))
2853 (format t "#define ~A ~A~A /* 0x~X ~@[ -- ~A ~]*/~%" name value suffix value doc))))
2856 ;; writing information about internal errors
2857 (let ((internal-errors sb!c:*backend-internal-errors*))
2858 (dotimes (i (length internal-errors))
2859 (let ((current-error (aref internal-errors i)))
2860 ;; FIXME: this UNLESS should go away (see also FIXME in
2861 ;; interr.lisp) -- APD, 2002-03-05
2862 (unless (eq nil (car current-error))
2863 (format t "#define ~A ~D~%"
2864 (c-symbol-name (car current-error))
2866 (format t "#define INTERNAL_ERROR_NAMES \\~%~{~S~#[~:;, \\~%~]~}~%"
2867 (map 'list #'cdr internal-errors)))
2870 ;; I'm not really sure why this is in SB!C, since it seems
2871 ;; conceptually like something that belongs to SB!VM. In any case,
2872 ;; it's needed C-side.
2873 (format t "#define BACKEND_PAGE_BYTES ~DLU~%" sb!c:*backend-page-bytes*)
2877 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
2878 ;; platforms. If we export this from the SB!VM package, it gets
2879 ;; written out as #define trap_PseudoAtomic, which is confusing as
2880 ;; the runtime treats trap_ as the prefix for illegal instruction
2881 ;; type things. We therefore don't export it, but instead do
2883 (when (boundp 'sb!vm::pseudo-atomic-trap)
2885 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
2886 sb!vm::pseudo-atomic-trap)
2888 ;; possibly this is another candidate for a rename (to
2889 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
2890 ;; [possibly applicable to other platforms])
2892 (dolist (symbol '(sb!vm::float-traps-byte
2893 sb!vm::float-exceptions-byte
2894 sb!vm::float-sticky-bits
2895 sb!vm::float-rounding-mode))
2896 (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
2897 (c-symbol-name symbol)
2898 (sb!xc:byte-position (symbol-value symbol)))
2899 (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
2900 (c-symbol-name symbol)
2901 (sb!xc:mask-field (symbol-value symbol) -1))))
2904 (defun write-tagnames-h (&optional (out *standard-output*))
2906 ((pretty-name (symbol strip)
2907 (let ((name (string-downcase symbol)))
2908 (substitute #\Space #\-
2909 (subseq name 0 (- (length name) (length strip))))))
2910 (list-sorted-tags (tail)
2911 (loop for symbol being the external-symbols of "SB!VM"
2912 when (and (constantp symbol)
2913 (tailwise-equal (string symbol) tail))
2914 collect symbol into tags
2915 finally (return (sort tags #'< :key #'symbol-value))))
2916 (write-tags (kind limit ash-count)
2917 (format out "~%static const char *~(~A~)_names[] = {~%"
2919 (let ((tags (list-sorted-tags kind)))
2921 (if (eql i (ash (or (symbol-value (first tags)) -1) ash-count))
2922 (format out " \"~A\"" (pretty-name (pop tags) kind))
2923 (format out " \"unknown [~D]\"" i))
2924 (unless (eql i (1- limit))
2925 (write-string "," out))
2927 (write-line "};" out)))
2928 (write-tags "-LOWTAG" sb!vm:lowtag-limit 0)
2929 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
2930 ;; ending with the same 2 bits. (#b10)
2931 (write-tags "-WIDETAG" (ash (1+ sb!vm:widetag-mask) -2) -2))
2934 (defun write-primitive-object (obj)
2935 ;; writing primitive object layouts
2936 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2939 (c-name (string-downcase (string (sb!vm:primitive-object-name obj)))))
2940 (when (sb!vm:primitive-object-widetag obj)
2941 (format t " lispobj header;~%"))
2942 (dolist (slot (sb!vm:primitive-object-slots obj))
2943 (format t " ~A ~A~@[[1]~];~%"
2944 (getf (sb!vm:slot-options slot) :c-type "lispobj")
2945 (c-name (string-downcase (string (sb!vm:slot-name slot))))
2946 (sb!vm:slot-rest-p slot)))
2948 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2949 (format t "/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
2950 (format t " * so they work directly on tagged addresses. */~2%")
2951 (let ((name (sb!vm:primitive-object-name obj))
2952 (lowtag (or (symbol-value (sb!vm:primitive-object-lowtag obj))
2954 (dolist (slot (sb!vm:primitive-object-slots obj))
2955 (format t "#define ~A_~A_OFFSET ~D~%"
2956 (c-symbol-name name)
2957 (c-symbol-name (sb!vm:slot-name slot))
2958 (- (* (sb!vm:slot-offset slot) sb!vm:n-word-bytes) lowtag)))
2960 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%"))
2962 (defun write-structure-object (dd)
2963 (flet ((cstring (designator)
2964 (c-name (string-downcase (string designator)))))
2965 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2966 (format t "struct ~A {~%" (cstring (dd-name dd)))
2967 (format t " lispobj header;~%")
2968 (format t " lispobj layout;~%")
2969 (dolist (slot (dd-slots dd))
2970 (when (eq t (dsd-raw-type slot))
2971 (format t " lispobj ~A;~%" (cstring (dsd-name slot)))))
2972 (unless (oddp (+ (dd-length dd) (dd-raw-length dd)))
2973 (format t " lispobj raw_slot_padding;~%"))
2974 (dotimes (n (dd-raw-length dd))
2975 (format t " lispobj raw~D;~%" (- (dd-raw-length dd) n 1)))
2977 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")))
2979 (defun write-static-symbols ()
2980 (dolist (symbol (cons nil sb!vm:*static-symbols*))
2981 ;; FIXME: It would be nice to use longer names than NIL and
2982 ;; (particularly) T in #define statements.
2983 (format t "#define ~A LISPOBJ(0x~X)~%"
2984 ;; FIXME: It would be nice not to need to strip anything
2985 ;; that doesn't get stripped always by C-SYMBOL-NAME.
2986 (c-symbol-name symbol "%*.!")
2987 (if *static* ; if we ran GENESIS
2988 ;; We actually ran GENESIS, use the real value.
2989 (descriptor-bits (cold-intern symbol))
2990 ;; We didn't run GENESIS, so guess at the address.
2991 (+ sb!vm:static-space-start
2993 sb!vm:other-pointer-lowtag
2994 (if symbol (sb!vm:static-symbol-offset symbol) 0))))))
2997 ;;;; writing map file
2999 ;;; Write a map file describing the cold load. Some of this
3000 ;;; information is subject to change due to relocating GC, but even so
3001 ;;; it can be very handy when attempting to troubleshoot the early
3002 ;;; stages of cold load.
3004 (let ((*print-pretty* nil)
3005 (*print-case* :upcase))
3006 (format t "assembler routines defined in core image:~2%")
3007 (dolist (routine (sort (copy-list *cold-assembler-routines*) #'<
3009 (format t "#X~8,'0X: ~S~%" (cdr routine) (car routine)))
3012 (maphash (lambda (name fdefn)
3013 (let ((fun (read-wordindexed fdefn
3014 sb!vm:fdefn-fun-slot)))
3015 (if (= (descriptor-bits fun)
3016 (descriptor-bits *nil-descriptor*))
3018 (let ((addr (read-wordindexed
3019 fdefn sb!vm:fdefn-raw-addr-slot)))
3020 (push (cons name (descriptor-bits addr))
3022 *cold-fdefn-objects*)
3023 (format t "~%~|~%initially defined functions:~2%")
3024 (setf funs (sort funs #'< :key #'cdr))
3026 (format t "0x~8,'0X: ~S #X~8,'0X~%" (cdr info) (car info)
3027 (- (cdr info) #x17)))
3030 (a note about initially undefined function references: These functions
3031 are referred to by code which is installed by GENESIS, but they are not
3032 installed by GENESIS. This is not necessarily a problem; functions can
3033 be defined later, by cold init toplevel forms, or in files compiled and
3034 loaded at warm init, or elsewhere. As long as they are defined before
3035 they are called, everything should be OK. Things are also OK if the
3036 cross-compiler knew their inline definition and used that everywhere
3037 that they were called before the out-of-line definition is installed,
3038 as is fairly common for structure accessors.)
3039 initially undefined function references:~2%")
3041 (setf undefs (sort undefs #'string< :key #'fun-name-block-name))
3042 (dolist (name undefs)
3043 (format t "~S~%" name)))
3045 (format t "~%~|~%layout names:~2%")
3047 (maphash (lambda (name gorp)
3048 (declare (ignore name))
3049 (stuff (cons (descriptor-bits (car gorp))
3052 (dolist (x (sort (stuff) #'< :key #'car))
3053 (apply #'format t "~8,'0X: ~S[~D]~%~10T~S~%" x))))
3057 ;;;; writing core file
3059 (defvar *core-file*)
3060 (defvar *data-page*)
3062 ;;; magic numbers to identify entries in a core file
3064 ;;; (In case you were wondering: No, AFAIK there's no special magic about
3065 ;;; these which requires them to be in the 38xx range. They're just
3066 ;;; arbitrary words, tested not for being in a particular range but just
3067 ;;; for equality. However, if you ever need to look at a .core file and
3068 ;;; figure out what's going on, it's slightly convenient that they're
3069 ;;; all in an easily recognizable range, and displacing the range away from
3070 ;;; zero seems likely to reduce the chance that random garbage will be
3071 ;;; misinterpreted as a .core file.)
3072 (defconstant version-core-entry-type-code 3860)
3073 (defconstant build-id-core-entry-type-code 3899)
3074 (defconstant new-directory-core-entry-type-code 3861)
3075 (defconstant initial-fun-core-entry-type-code 3863)
3076 (defconstant page-table-core-entry-type-code 3880)
3077 (defconstant end-core-entry-type-code 3840)
3079 (declaim (ftype (function (sb!vm:word) sb!vm:word) write-word))
3080 (defun write-word (num)
3081 (ecase sb!c:*backend-byte-order*
3083 (dotimes (i sb!vm:n-word-bytes)
3084 (write-byte (ldb (byte 8 (* i 8)) num) *core-file*)))
3086 (dotimes (i sb!vm:n-word-bytes)
3087 (write-byte (ldb (byte 8 (* (- (1- sb!vm:n-word-bytes) i) 8)) num)
3091 (defun advance-to-page ()
3092 (force-output *core-file*)
3093 (file-position *core-file*
3094 (round-up (file-position *core-file*)
3095 sb!c:*backend-page-bytes*)))
3097 (defun output-gspace (gspace)
3098 (force-output *core-file*)
3099 (let* ((posn (file-position *core-file*))
3100 (bytes (* (gspace-free-word-index gspace) sb!vm:n-word-bytes))
3101 (pages (ceiling bytes sb!c:*backend-page-bytes*))
3102 (total-bytes (* pages sb!c:*backend-page-bytes*)))
3104 (file-position *core-file*
3105 (* sb!c:*backend-page-bytes* (1+ *data-page*)))
3107 "writing ~S byte~:P [~S page~:P] from ~S~%"
3113 ;; Note: It is assumed that the GSPACE allocation routines always
3114 ;; allocate whole pages (of size *target-page-size*) and that any
3115 ;; empty gspace between the free pointer and the end of page will
3116 ;; be zero-filled. This will always be true under Mach on machines
3117 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3119 (write-bigvec-as-sequence (gspace-bytes gspace)
3122 (force-output *core-file*)
3123 (file-position *core-file* posn)
3125 ;; Write part of a (new) directory entry which looks like this:
3126 ;; GSPACE IDENTIFIER
3131 (write-word (gspace-identifier gspace))
3132 (write-word (gspace-free-word-index gspace))
3133 (write-word *data-page*)
3134 (multiple-value-bind (floor rem)
3135 (floor (gspace-byte-address gspace) sb!c:*backend-page-bytes*)
3140 (incf *data-page* pages)))
3142 ;;; Create a core file created from the cold loaded image. (This is
3143 ;;; the "initial core file" because core files could be created later
3144 ;;; by executing SAVE-LISP in a running system, perhaps after we've
3145 ;;; added some functionality to the system.)
3146 (declaim (ftype (function (string)) write-initial-core-file))
3147 (defun write-initial-core-file (filename)
3149 (let ((filenamestring (namestring filename))
3153 "[building initial core file in ~S: ~%"
3157 (with-open-file (*core-file* filenamestring
3159 :element-type '(unsigned-byte 8)
3160 :if-exists :rename-and-delete)
3162 ;; Write the magic number.
3163 (write-word core-magic)
3165 ;; Write the Version entry.
3166 (write-word version-core-entry-type-code)
3168 (write-word sbcl-core-version-integer)
3170 ;; Write the build ID.
3171 (write-word build-id-core-entry-type-code)
3172 (let ((build-id (with-open-file (s "output/build-id.tmp"
3175 (declare (type simple-string build-id))
3176 (/show build-id (length build-id))
3177 ;; Write length of build ID record: BUILD-ID-CORE-ENTRY-TYPE-CODE
3178 ;; word, this length word, and one word for each char of BUILD-ID.
3179 (write-word (+ 2 (length build-id)))
3180 (dovector (char build-id)
3181 ;; (We write each character as a word in order to avoid
3182 ;; having to think about word alignment issues in the
3183 ;; sbcl-0.7.8 version of coreparse.c.)
3184 (write-word (sb!xc:char-code char))))
3186 ;; Write the New Directory entry header.
3187 (write-word new-directory-core-entry-type-code)
3188 (write-word 17) ; length = (5 words/space) * 3 spaces + 2 for header.
3190 (output-gspace *read-only*)
3191 (output-gspace *static*)
3192 (output-gspace *dynamic*)
3194 ;; Write the initial function.
3195 (write-word initial-fun-core-entry-type-code)
3197 (let* ((cold-name (cold-intern '!cold-init))
3198 (cold-fdefn (cold-fdefinition-object cold-name))
3199 (initial-fun (read-wordindexed cold-fdefn
3200 sb!vm:fdefn-fun-slot)))
3202 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
3203 (descriptor-bits initial-fun))
3204 (write-word (descriptor-bits initial-fun)))
3206 ;; Write the End entry.
3207 (write-word end-core-entry-type-code)
3210 (format t "done]~%")
3212 (/show "leaving WRITE-INITIAL-CORE-FILE")
3215 ;;;; the actual GENESIS function
3217 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3218 ;;; and/or information about a Lisp core, therefrom.
3220 ;;; input file arguments:
3221 ;;; SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
3222 ;;; *tab* *characters* *converted* *to* *spaces*. (We push
3223 ;;; responsibility for removing tabs out to the caller it's
3224 ;;; trivial to remove them using UNIX command line tools like
3225 ;;; sed, whereas it's a headache to do it portably in Lisp because
3226 ;;; #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
3227 ;;; a core file cannot be built (but a C header file can be).
3229 ;;; output files arguments (any of which may be NIL to suppress output):
3230 ;;; CORE-FILE-NAME gets a Lisp core.
3231 ;;; C-HEADER-FILE-NAME gets a C header file, traditionally called
3232 ;;; internals.h, which is used by the C compiler when constructing
3233 ;;; the executable which will load the core.
3234 ;;; MAP-FILE-NAME gets (?) a map file. (dunno about this -- WHN 19990815)
3236 ;;; FIXME: GENESIS doesn't belong in SB!VM. Perhaps in %KERNEL for now,
3237 ;;; perhaps eventually in SB-LD or SB-BOOT.
3238 (defun sb!vm:genesis (&key
3240 symbol-table-file-name
3246 "~&beginning GENESIS, ~A~%"
3248 ;; Note: This output summarizing what we're doing is
3249 ;; somewhat telegraphic in style, not meant to imply that
3250 ;; we're not e.g. also creating a header file when we
3252 (format nil "creating core ~S" core-file-name)
3253 (format nil "creating headers in ~S" c-header-dir-name)))
3255 (let ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
3257 (when core-file-name
3258 (if symbol-table-file-name
3259 (load-cold-foreign-symbol-table symbol-table-file-name)
3260 (error "can't output a core file without symbol table file input")))
3262 ;; Now that we've successfully read our only input file (by
3263 ;; loading the symbol table, if any), it's a good time to ensure
3264 ;; that there'll be someplace for our output files to go when
3266 (flet ((frob (filename)
3268 (ensure-directories-exist filename :verbose t))))
3269 (frob core-file-name)
3270 (frob map-file-name))
3272 ;; (This shouldn't matter in normal use, since GENESIS normally
3273 ;; only runs once in any given Lisp image, but it could reduce
3274 ;; confusion if we ever experiment with running, tweaking, and
3275 ;; rerunning genesis interactively.)
3276 (do-all-symbols (sym)
3277 (remprop sym 'cold-intern-info))
3281 (let* ((*foreign-symbol-placeholder-value* (if core-file-name nil 0))
3282 (*load-time-value-counter* 0)
3283 (*cold-fdefn-objects* (make-hash-table :test 'equal))
3284 (*cold-symbols* (make-hash-table :test 'equal))
3285 (*cold-package-symbols* nil)
3286 (*read-only* (make-gspace :read-only
3287 read-only-core-space-id
3288 sb!vm:read-only-space-start))
3289 (*static* (make-gspace :static
3290 static-core-space-id
3291 sb!vm:static-space-start))
3292 (*dynamic* (make-gspace :dynamic
3293 dynamic-core-space-id
3294 #!+gencgc sb!vm:dynamic-space-start
3295 #!-gencgc sb!vm:dynamic-0-space-start))
3296 (*nil-descriptor* (make-nil-descriptor))
3297 (*current-reversed-cold-toplevels* *nil-descriptor*)
3298 (*current-debug-sources* *nil-descriptor*)
3299 (*unbound-marker* (make-other-immediate-descriptor
3301 sb!vm:unbound-marker-widetag))
3302 *cold-assembler-fixups*
3303 *cold-assembler-routines*
3304 #!+x86 (*load-time-code-fixups* (make-hash-table)))
3306 ;; Prepare for cold load.
3307 (initialize-non-nil-symbols)
3308 (initialize-layouts)
3309 (initialize-static-fns)
3311 ;; Initialize the *COLD-SYMBOLS* system with the information
3312 ;; from package-data-list.lisp-expr and
3313 ;; common-lisp-exports.lisp-expr.
3315 ;; Why do things this way? Historically, the *COLD-SYMBOLS*
3316 ;; machinery was designed and implemented in CMU CL long before
3317 ;; I (WHN) ever heard of CMU CL. It dumped symbols and packages
3318 ;; iff they were used in the cold image. When I added the
3319 ;; package-data-list.lisp-expr mechanism, the idea was to
3320 ;; centralize all information about packages and exports. Thus,
3321 ;; it was the natural place for information even about packages
3322 ;; (such as SB!PCL and SB!WALKER) which aren't used much until
3323 ;; after cold load. This didn't quite match the CMU CL approach
3324 ;; of filling *COLD-SYMBOLS* with symbols which appear in the
3325 ;; cold image and then dumping only those symbols. By explicitly
3326 ;; putting all the symbols from package-data-list.lisp-expr and
3327 ;; from common-lisp-exports.lisp-expr into *COLD-SYMBOLS* here,
3328 ;; we feed our centralized symbol information into the old CMU
3329 ;; CL code without having to change the old CMU CL code too
3330 ;; much. (And the old CMU CL code is still useful for making
3331 ;; sure that the appropriate keywords and internal symbols end
3332 ;; up interned in the target Lisp, which is good, e.g. in order
3333 ;; to make &KEY arguments work right and in order to make
3334 ;; BACKTRACEs into target Lisp system code be legible.)
3335 (dolist (exported-name
3336 (sb-cold:read-from-file "common-lisp-exports.lisp-expr"))
3337 (cold-intern (intern exported-name *cl-package*)))
3338 (dolist (pd (sb-cold:read-from-file "package-data-list.lisp-expr"))
3339 (declare (type sb-cold:package-data pd))
3340 (let ((package (find-package (sb-cold:package-data-name pd))))
3341 (labels (;; Call FN on every node of the TREE.
3342 (mapc-on-tree (fn tree)
3343 (declare (type function fn))
3345 (cons (mapc-on-tree fn (car tree))
3346 (mapc-on-tree fn (cdr tree)))
3347 (t (funcall fn tree)
3349 ;; Make sure that information about the association
3350 ;; between PACKAGE and the symbol named NAME gets
3351 ;; recorded in the cold-intern system or (as a
3352 ;; convenience when dealing with the tree structure
3353 ;; allowed in the PACKAGE-DATA-EXPORTS slot) do
3354 ;; nothing if NAME is NIL.
3357 (cold-intern (intern name package) :package package))))
3358 (mapc-on-tree #'chill (sb-cold:package-data-export pd))
3359 (mapc #'chill (sb-cold:package-data-reexport pd))
3360 (dolist (sublist (sb-cold:package-data-import-from pd))
3361 (destructuring-bind (package-name &rest symbol-names) sublist
3362 (declare (ignore package-name))
3363 (mapc #'chill symbol-names))))))
3366 (dolist (file-name object-file-names)
3367 (write-line (namestring file-name))
3368 (cold-load file-name))
3370 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3371 (resolve-assembler-fixups)
3372 #!+x86 (output-load-time-code-fixups)
3373 (foreign-symbols-to-core)
3375 (/show "back from FINISH-SYMBOLS")
3376 (finalize-load-time-value-noise)
3378 ;; Tell the target Lisp how much stuff we've allocated.
3379 (cold-set 'sb!vm:*read-only-space-free-pointer*
3380 (allocate-cold-descriptor *read-only*
3382 sb!vm:even-fixnum-lowtag))
3383 (cold-set 'sb!vm:*static-space-free-pointer*
3384 (allocate-cold-descriptor *static*
3386 sb!vm:even-fixnum-lowtag))
3387 (/show "done setting free pointers")
3389 ;; Write results to files.
3391 ;; FIXME: I dislike this approach of redefining
3392 ;; *STANDARD-OUTPUT* instead of putting the new stream in a
3393 ;; lexical variable, and it's annoying to have WRITE-MAP (to
3394 ;; *STANDARD-OUTPUT*) not be parallel to WRITE-INITIAL-CORE-FILE
3395 ;; (to a stream explicitly passed as an argument).
3396 (macrolet ((out-to (name &body body)
3397 `(let ((fn (format nil "~A/~A.h" c-header-dir-name ,name)))
3398 (ensure-directories-exist fn)
3399 (with-open-file (*standard-output* fn
3400 :if-exists :supersede :direction :output)
3402 (let ((n (c-name (string-upcase ,name))))
3405 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~A 1~%"
3409 "#endif /* SBCL_GENESIS_~A */~%"
3410 (string-upcase ,name))))))
3412 (with-open-file (*standard-output* map-file-name
3414 :if-exists :supersede)
3416 (out-to "config" (write-config-h))
3417 (out-to "constants" (write-constants-h))
3419 (out-to "tagnames" (write-tagnames-h))
3420 (let ((structs (sort (copy-list sb!vm:*primitive-objects*) #'string<
3423 (sb!vm:primitive-object-name obj))))))
3424 (dolist (obj structs)
3426 (string-downcase (string (sb!vm:primitive-object-name obj)))
3427 (write-primitive-object obj)))
3428 (out-to "primitive-objects"
3429 (dolist (obj structs)
3430 (format t "~&#include \"~A.h\"~%"
3432 (string (sb!vm:primitive-object-name obj)))))))
3433 (dolist (class '(hash-table
3435 sb!c::compiled-debug-info
3436 sb!c::compiled-debug-fun
3439 (string-downcase (string class))
3440 (write-structure-object
3441 (sb!kernel:layout-info (sb!kernel:find-layout class)))))
3442 (out-to "static-symbols" (write-static-symbols))
3444 (let ((fn (format nil "~A/Makefile.features" c-header-dir-name)))
3445 (ensure-directories-exist fn)
3446 (with-open-file (*standard-output* fn :if-exists :supersede
3448 (write-makefile-features)))
3450 (when core-file-name
3451 (write-initial-core-file core-file-name))))))