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 (defstruct (descriptor
286 (:constructor make-descriptor
287 (high low &optional gspace word-offset))
289 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
290 (gspace nil :type (or gspace (eql :load-time-value) null))
291 ;; the offset in words from the start of GSPACE, or NIL if not set yet
292 (word-offset nil :type (or sb!vm:word null))
293 ;; the high and low halves of the descriptor
295 ;; KLUDGE: Judging from the comments in genesis.lisp of the CMU CL
296 ;; old-rt compiler, this split dates back from a very early version
297 ;; of genesis where 32-bit integers were represented as conses of
298 ;; two 16-bit integers. In any system with nice (UNSIGNED-BYTE 32)
299 ;; structure slots, like CMU CL >= 17 or any version of SBCL, there
300 ;; seems to be no reason to persist in this. -- WHN 19990917
303 (def!method print-object ((des descriptor) stream)
304 (let ((lowtag (descriptor-lowtag des)))
305 (print-unreadable-object (des stream :type t)
306 (cond ((is-fixnum-lowtag lowtag)
307 (let ((unsigned (logior (ash (descriptor-high des)
308 (1+ (- descriptor-low-bits
309 sb!vm:n-lowtag-bits)))
310 (ash (descriptor-low des)
311 (- 1 sb!vm:n-lowtag-bits)))))
314 (if (> unsigned #x1FFFFFFF)
315 (- unsigned #x40000000)
317 ((or (= lowtag sb!vm:other-immediate-0-lowtag)
318 (= lowtag sb!vm:other-immediate-1-lowtag)
319 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
320 (= lowtag sb!vm:other-immediate-2-lowtag)
321 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
322 (= lowtag sb!vm:other-immediate-3-lowtag))
324 "for other immediate: #X~X, type #b~8,'0B"
325 (ash (descriptor-bits des) (- sb!vm:n-widetag-bits))
326 (logand (descriptor-low des) sb!vm:widetag-mask)))
329 "for pointer: #X~X, lowtag #b~3,'0B, ~A"
330 (logior (ash (descriptor-high des) descriptor-low-bits)
331 (logandc2 (descriptor-low des) sb!vm:lowtag-mask))
333 (let ((gspace (descriptor-gspace des)))
338 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
339 ;;; free word index is boosted as necessary, and if additional memory
340 ;;; is needed, we grow the GSPACE. The descriptor returned is a
341 ;;; pointer of type LOWTAG.
342 (defun allocate-cold-descriptor (gspace length lowtag)
343 (let* ((bytes (round-up length (ash 1 sb!vm:n-lowtag-bits)))
344 (old-free-word-index (gspace-free-word-index gspace))
345 (new-free-word-index (+ old-free-word-index
346 (ash bytes (- sb!vm:word-shift)))))
347 ;; Grow GSPACE as necessary until it's big enough to handle
348 ;; NEW-FREE-WORD-INDEX.
350 ((>= (bvlength (gspace-bytes gspace))
351 (* new-free-word-index sb!vm:n-word-bytes)))
352 (expand-bigvec (gspace-bytes gspace)))
353 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
354 (setf (gspace-free-word-index gspace) new-free-word-index)
355 (let ((ptr (+ (gspace-word-address gspace) old-free-word-index)))
356 (make-descriptor (ash ptr (- sb!vm:word-shift descriptor-low-bits))
357 (logior (ash (logand ptr
359 (- descriptor-low-bits
364 old-free-word-index))))
366 (defun descriptor-lowtag (des)
368 "the lowtag bits for DES"
369 (logand (descriptor-low des) sb!vm:lowtag-mask))
371 (defun descriptor-bits (des)
372 (logior (ash (descriptor-high des) descriptor-low-bits)
373 (descriptor-low des)))
375 (defun descriptor-fixnum (des)
376 (let ((bits (descriptor-bits des)))
377 (if (logbitp (1- sb!vm:n-word-bits) bits)
378 ;; KLUDGE: The (- SB!VM:N-WORD-BITS 2) term here looks right to
379 ;; me, and it works, but in CMU CL it was (1- SB!VM:N-WORD-BITS),
380 ;; and although that doesn't make sense for me, or work for me,
381 ;; it's hard to see how it could have been wrong, since CMU CL
382 ;; genesis worked. It would be nice to understand how this came
383 ;; to be.. -- WHN 19990901
384 (logior (ash bits (- sb!vm:n-fixnum-tag-bits))
385 (ash -1 (1+ sb!vm:n-positive-fixnum-bits)))
386 (ash bits (- sb!vm:n-fixnum-tag-bits)))))
388 (defun descriptor-word-sized-integer (des)
389 ;; Extract an (unsigned-byte 32), from either its fixnum or bignum
391 (let ((lowtag (descriptor-lowtag des)))
392 (if (is-fixnum-lowtag lowtag)
393 (make-random-descriptor (descriptor-fixnum des))
394 (read-wordindexed des 1))))
397 (defun descriptor-bytes (des)
398 (gspace-bytes (descriptor-intuit-gspace des)))
399 (defun descriptor-byte-offset (des)
400 (ash (descriptor-word-offset des) sb!vm:word-shift))
402 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
403 ;;; figure out a GSPACE which corresponds to DES, set it into
404 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
405 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
406 (declaim (ftype (function (descriptor) gspace) descriptor-intuit-gspace))
407 (defun descriptor-intuit-gspace (des)
408 (or (descriptor-gspace des)
410 ;; gspace wasn't set, now we have to search for it.
411 (let ((lowtag (descriptor-lowtag des))
412 (high (descriptor-high des))
413 (low (descriptor-low des)))
415 ;; Non-pointer objects don't have a gspace.
416 (unless (or (eql lowtag sb!vm:fun-pointer-lowtag)
417 (eql lowtag sb!vm:instance-pointer-lowtag)
418 (eql lowtag sb!vm:list-pointer-lowtag)
419 (eql lowtag sb!vm:other-pointer-lowtag))
420 (error "don't even know how to look for a GSPACE for ~S" des))
422 (dolist (gspace (list *dynamic* *static* *read-only*)
423 (error "couldn't find a GSPACE for ~S" des))
424 ;; Bounds-check the descriptor against the allocated area
425 ;; within each gspace.
427 ;; Most of the faffing around in here involving ash and
428 ;; various computed shift counts is due to the high/low
429 ;; split representation of the descriptor bits and an
430 ;; apparent disinclination to create intermediate values
431 ;; larger than a target fixnum.
433 ;; This code relies on the fact that GSPACEs are aligned
434 ;; such that the descriptor-low-bits low bits are zero.
435 (when (and (>= high (ash (gspace-word-address gspace)
436 (- sb!vm:word-shift descriptor-low-bits)))
437 (<= high (ash (+ (gspace-word-address gspace)
438 (gspace-free-word-index gspace))
439 (- sb!vm:word-shift descriptor-low-bits))))
440 ;; Update the descriptor with the correct gspace and the
441 ;; offset within the gspace and return the gspace.
442 (setf (descriptor-gspace des) gspace)
443 (setf (descriptor-word-offset des)
444 (+ (ash (- high (ash (gspace-word-address gspace)
446 descriptor-low-bits)))
447 (- descriptor-low-bits sb!vm:word-shift))
448 (ash (logandc2 low sb!vm:lowtag-mask)
449 (- sb!vm:word-shift))))
452 (defun make-random-descriptor (value)
453 (make-descriptor (logand (ash value (- descriptor-low-bits))
456 descriptor-low-bits))))
457 (logand value (1- (ash 1 descriptor-low-bits)))))
459 (defun make-fixnum-descriptor (num)
460 (when (>= (integer-length num)
461 (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits))
462 (error "~W is too big for a fixnum." num))
463 (make-random-descriptor (ash num sb!vm:n-fixnum-tag-bits)))
465 (defun make-other-immediate-descriptor (data type)
466 (make-descriptor (ash data (- sb!vm:n-widetag-bits descriptor-low-bits))
467 (logior (logand (ash data (- descriptor-low-bits
468 sb!vm:n-widetag-bits))
469 (1- (ash 1 descriptor-low-bits)))
472 (defun make-character-descriptor (data)
473 (make-other-immediate-descriptor data sb!vm:character-widetag))
475 (defun descriptor-beyond (des offset type)
476 (let* ((low (logior (+ (logandc2 (descriptor-low des) sb!vm:lowtag-mask)
479 (high (+ (descriptor-high des)
480 (ash low (- descriptor-low-bits)))))
481 (make-descriptor high (logand low (1- (ash 1 descriptor-low-bits))))))
483 ;;;; miscellaneous variables and other noise
485 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
486 ;;; undefined foreign symbols are to be treated as an error.
487 ;;; (In the first pass of GENESIS, needed to create a header file before
488 ;;; the C runtime can be built, various foreign symbols will necessarily
489 ;;; be undefined, but we don't need actual values for them anyway, and
490 ;;; we can just use 0 or some other placeholder. In the second pass of
491 ;;; GENESIS, all foreign symbols should be defined, so any undefined
492 ;;; foreign symbol is a problem.)
494 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
495 ;;; never tries to look up foreign symbols in the first place unless
496 ;;; it's actually creating a core file (as in the second pass) instead
497 ;;; of using this hack to allow it to go through the motions without
498 ;;; causing an error. -- WHN 20000825
499 (defvar *foreign-symbol-placeholder-value*)
501 ;;; a handle on the trap object
502 (defvar *unbound-marker*)
503 ;; was: (make-other-immediate-descriptor 0 sb!vm:unbound-marker-widetag)
505 ;;; a handle on the NIL object
506 (defvar *nil-descriptor*)
508 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
509 ;;; when the target Lisp starts up
511 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
512 ;;; loadtime value, represented by (CONS KEYWORD ..). The FILENAME
513 ;;; tells which fasl file each list element came from, for debugging
515 (defvar *current-reversed-cold-toplevels*)
517 ;;; the head of a list of DEBUG-SOURCEs which need to be patched when
518 ;;; the cold core starts up
519 (defvar *current-debug-sources*)
521 ;;; the name of the object file currently being cold loaded (as a string, not a
522 ;;; pathname), or NIL if we're not currently cold loading any object file
523 (defvar *cold-load-filename* nil)
524 (declaim (type (or string null) *cold-load-filename*))
526 ;;;; miscellaneous stuff to read and write the core memory
528 ;;; FIXME: should be DEFINE-MODIFY-MACRO
529 (defmacro cold-push (thing list)
531 "Push THING onto the given cold-load LIST."
532 `(setq ,list (cold-cons ,thing ,list)))
534 (declaim (ftype (function (descriptor sb!vm:word) descriptor) read-wordindexed))
535 (defun read-wordindexed (address index)
537 "Return the value which is displaced by INDEX words from ADDRESS."
538 (let* ((gspace (descriptor-intuit-gspace address))
539 (bytes (gspace-bytes gspace))
540 (byte-index (ash (+ index (descriptor-word-offset address))
542 (value (bvref-word bytes byte-index)))
543 (make-random-descriptor value)))
545 (declaim (ftype (function (descriptor) descriptor) read-memory))
546 (defun read-memory (address)
548 "Return the value at ADDRESS."
549 (read-wordindexed address 0))
551 ;;; (Note: In CMU CL, this function expected a SAP-typed ADDRESS
552 ;;; value, instead of the object-and-offset we use here.)
553 (declaim (ftype (function (descriptor sb!vm:word descriptor) (values))
554 note-load-time-value-reference))
555 (defun note-load-time-value-reference (address offset marker)
556 (cold-push (cold-cons
557 (cold-intern :load-time-value-fixup)
559 (cold-cons (number-to-core offset)
561 (number-to-core (descriptor-word-offset marker))
563 *current-reversed-cold-toplevels*)
566 (declaim (ftype (function (descriptor sb!vm:word (or descriptor symbol))) write-wordindexed))
567 (defun write-wordindexed (address index value)
569 "Write VALUE displaced INDEX words from ADDRESS."
570 ;; If we're passed a symbol as a value then it needs to be interned.
571 (when (symbolp value) (setf value (cold-intern value)))
572 (if (eql (descriptor-gspace value) :load-time-value)
573 (note-load-time-value-reference address
574 (- (ash index sb!vm:word-shift)
575 (logand (descriptor-bits address)
578 (let* ((bytes (gspace-bytes (descriptor-intuit-gspace address)))
579 (byte-index (ash (+ index (descriptor-word-offset address))
581 (setf (bvref-word bytes byte-index)
582 (descriptor-bits value)))))
584 (declaim (ftype (function (descriptor (or descriptor symbol))) write-memory))
585 (defun write-memory (address value)
587 "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
588 (write-wordindexed address 0 value))
590 ;;;; allocating images of primitive objects in the cold core
592 ;;; There are three kinds of blocks of memory in the type system:
593 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
594 ;;; header as all slots are descriptors.
595 ;;; * Unboxed objects (bignums): There is a single header word that contains
597 ;;; * Vector objects: There is a header word with the type, then a word for
598 ;;; the length, then the data.
599 (defun allocate-boxed-object (gspace length lowtag)
601 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
603 (allocate-cold-descriptor gspace (ash length sb!vm:word-shift) lowtag))
604 (defun allocate-unboxed-object (gspace element-bits length type)
606 "Allocate LENGTH units of ELEMENT-BITS bits plus a header word in GSPACE and
607 return an ``other-pointer'' descriptor to them. Initialize the header word
608 with the resultant length and TYPE."
609 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
610 (des (allocate-cold-descriptor gspace
611 (+ bytes sb!vm:n-word-bytes)
612 sb!vm:other-pointer-lowtag)))
614 (make-other-immediate-descriptor (ash bytes
615 (- sb!vm:word-shift))
618 (defun allocate-vector-object (gspace element-bits length type)
620 "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
621 GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
622 header word with TYPE and the length slot with LENGTH."
623 ;; FIXME: Here and in ALLOCATE-UNBOXED-OBJECT, BYTES is calculated using
624 ;; #'/ instead of #'CEILING, which seems wrong.
625 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
626 (des (allocate-cold-descriptor gspace
627 (+ bytes (* 2 sb!vm:n-word-bytes))
628 sb!vm:other-pointer-lowtag)))
629 (write-memory des (make-other-immediate-descriptor 0 type))
630 (write-wordindexed des
631 sb!vm:vector-length-slot
632 (make-fixnum-descriptor length))
635 ;;;; copying simple objects into the cold core
637 (defun base-string-to-core (string &optional (gspace *dynamic*))
639 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
640 core and return a descriptor to it."
641 ;; (Remember that the system convention for storage of strings leaves an
642 ;; extra null byte at the end to aid in call-out to C.)
643 (let* ((length (length string))
644 (des (allocate-vector-object gspace
647 sb!vm:simple-base-string-widetag))
648 (bytes (gspace-bytes gspace))
649 (offset (+ (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
650 (descriptor-byte-offset des))))
651 (write-wordindexed des
652 sb!vm:vector-length-slot
653 (make-fixnum-descriptor length))
655 (setf (bvref bytes (+ offset i))
656 (sb!xc:char-code (aref string i))))
657 (setf (bvref bytes (+ offset length))
658 0) ; null string-termination character for C
661 (defun bignum-to-core (n)
663 "Copy a bignum to the cold core."
664 (let* ((words (ceiling (1+ (integer-length n)) sb!vm:n-word-bits))
665 (handle (allocate-unboxed-object *dynamic*
668 sb!vm:bignum-widetag)))
669 (declare (fixnum words))
670 (do ((index 1 (1+ index))
671 (remainder n (ash remainder (- sb!vm:n-word-bits))))
673 (unless (zerop (integer-length remainder))
674 ;; FIXME: Shouldn't this be a fatal error?
675 (warn "~W words of ~W were written, but ~W bits were left over."
677 (let ((word (ldb (byte sb!vm:n-word-bits 0) remainder)))
678 (write-wordindexed handle index
679 (make-descriptor (ash word (- descriptor-low-bits))
680 (ldb (byte descriptor-low-bits 0)
684 (defun number-pair-to-core (first second type)
686 "Makes a number pair of TYPE (ratio or complex) and fills it in."
687 (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits 2 type)))
688 (write-wordindexed des 1 first)
689 (write-wordindexed des 2 second)
692 (defun write-double-float-bits (address index x)
693 (let ((hi (double-float-high-bits x))
694 (lo (double-float-low-bits x)))
695 (ecase sb!vm::n-word-bits
697 (let ((high-bits (make-random-descriptor hi))
698 (low-bits (make-random-descriptor lo)))
699 (ecase sb!c:*backend-byte-order*
701 (write-wordindexed address index low-bits)
702 (write-wordindexed address (1+ index) high-bits))
704 (write-wordindexed address index high-bits)
705 (write-wordindexed address (1+ index) low-bits)))))
707 (let ((bits (make-random-descriptor
708 (ecase sb!c:*backend-byte-order*
709 (:little-endian (logior lo (ash hi 32)))
711 #+nil (:big-endian (logior (logand hi #xffffffff)
713 (write-wordindexed address index bits))))
716 (defun float-to-core (x)
719 ;; 64-bit platforms have immediate single-floats.
720 #!+#.(cl:if (cl:= sb!vm:n-word-bits 64) '(and) '(or))
721 (make-random-descriptor (logior (ash (single-float-bits x) 32)
722 sb!vm::single-float-widetag))
723 #!-#.(cl:if (cl:= sb!vm:n-word-bits 64) '(and) '(or))
724 (let ((des (allocate-unboxed-object *dynamic*
726 (1- sb!vm:single-float-size)
727 sb!vm:single-float-widetag)))
728 (write-wordindexed des
729 sb!vm:single-float-value-slot
730 (make-random-descriptor (single-float-bits x)))
733 (let ((des (allocate-unboxed-object *dynamic*
735 (1- sb!vm:double-float-size)
736 sb!vm:double-float-widetag)))
737 (write-double-float-bits des sb!vm:double-float-value-slot x)))))
739 (defun complex-single-float-to-core (num)
740 (declare (type (complex single-float) num))
741 (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
742 (1- sb!vm:complex-single-float-size)
743 sb!vm:complex-single-float-widetag)))
746 (write-wordindexed des sb!vm:complex-single-float-real-slot
747 (make-random-descriptor (single-float-bits (realpart num))))
748 (write-wordindexed des sb!vm:complex-single-float-imag-slot
749 (make-random-descriptor (single-float-bits (imagpart num)))))
751 (write-wordindexed des sb!vm:complex-single-float-data-slot
752 (make-random-descriptor
753 (logior (ldb (byte 32 0) (single-float-bits (realpart num)))
754 (ash (single-float-bits (imagpart num)) 32))))
757 (defun complex-double-float-to-core (num)
758 (declare (type (complex double-float) num))
759 (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
760 (1- sb!vm:complex-double-float-size)
761 sb!vm:complex-double-float-widetag)))
762 (write-double-float-bits des sb!vm:complex-double-float-real-slot
764 (write-double-float-bits des sb!vm:complex-double-float-imag-slot
767 ;;; Copy the given number to the core.
768 (defun number-to-core (number)
770 (integer (if (< (integer-length number)
771 (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits))
772 (make-fixnum-descriptor number)
773 (bignum-to-core number)))
774 (ratio (number-pair-to-core (number-to-core (numerator number))
775 (number-to-core (denominator number))
776 sb!vm:ratio-widetag))
777 ((complex single-float) (complex-single-float-to-core number))
778 ((complex double-float) (complex-double-float-to-core number))
780 ((complex long-float)
781 (error "~S isn't a cold-loadable number at all!" number))
782 (complex (number-pair-to-core (number-to-core (realpart number))
783 (number-to-core (imagpart number))
784 sb!vm:complex-widetag))
785 (float (float-to-core number))
786 (t (error "~S isn't a cold-loadable number at all!" number))))
788 (declaim (ftype (function (sb!vm:word) descriptor) sap-int-to-core))
789 (defun sap-int-to-core (sap-int)
790 (let ((des (allocate-unboxed-object *dynamic*
794 (write-wordindexed des
795 sb!vm:sap-pointer-slot
796 (make-random-descriptor sap-int))
799 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
800 (defun cold-cons (car cdr &optional (gspace *dynamic*))
801 (let ((dest (allocate-boxed-object gspace 2 sb!vm:list-pointer-lowtag)))
802 (write-memory dest car)
803 (write-wordindexed dest 1 cdr)
806 ;;; Make a simple-vector on the target that holds the specified
807 ;;; OBJECTS, and return its descriptor.
808 (defun vector-in-core (&rest objects)
809 (let* ((size (length objects))
810 (result (allocate-vector-object *dynamic* sb!vm:n-word-bits size
811 sb!vm:simple-vector-widetag)))
812 (dotimes (index size)
813 (write-wordindexed result (+ index sb!vm:vector-data-offset)
819 ;;; Allocate (and initialize) a symbol.
820 (defun allocate-symbol (name &key (gspace *dynamic*))
821 (declare (simple-string name))
822 (let ((symbol (allocate-unboxed-object gspace
824 (1- sb!vm:symbol-size)
825 sb!vm:symbol-header-widetag)))
826 (write-wordindexed symbol sb!vm:symbol-value-slot *unbound-marker*)
827 (write-wordindexed symbol
828 sb!vm:symbol-hash-slot
829 (make-fixnum-descriptor 0))
830 (write-wordindexed symbol sb!vm:symbol-plist-slot *nil-descriptor*)
831 (write-wordindexed symbol sb!vm:symbol-name-slot
832 (base-string-to-core name *dynamic*))
833 (write-wordindexed symbol sb!vm:symbol-package-slot *nil-descriptor*)
836 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
837 ;;; descriptor of a cold symbol or (in an abbreviation for the
838 ;;; most common usage pattern) an ordinary symbol, which will be
839 ;;; automatically cold-interned.
840 (declaim (ftype (function ((or descriptor symbol) descriptor)) cold-set))
841 (defun cold-set (symbol-or-symbol-des value)
842 (let ((symbol-des (etypecase symbol-or-symbol-des
843 (descriptor symbol-or-symbol-des)
844 (symbol (cold-intern symbol-or-symbol-des)))))
845 (write-wordindexed symbol-des sb!vm:symbol-value-slot value)))
847 ;;;; layouts and type system pre-initialization
849 ;;; Since we want to be able to dump structure constants and
850 ;;; predicates with reference layouts, we need to create layouts at
851 ;;; cold-load time. We use the name to intern layouts by, and dump a
852 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
853 ;;; initialization can find them. The only thing that's tricky [sic --
854 ;;; WHN 19990816] is initializing layout's layout, which must point to
857 ;;; a map from class names to lists of
858 ;;; `(,descriptor ,name ,length ,inherits ,depth)
859 ;;; KLUDGE: It would be more understandable and maintainable to use
860 ;;; DEFSTRUCT (:TYPE LIST) here. -- WHN 19990823
861 (defvar *cold-layouts* (make-hash-table :test 'equal))
863 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
865 (defvar *cold-layout-names* (make-hash-table :test 'eql))
867 ;;; FIXME: *COLD-LAYOUTS* and *COLD-LAYOUT-NAMES* should be
868 ;;; initialized by binding in GENESIS.
870 ;;; the descriptor for layout's layout (needed when making layouts)
871 (defvar *layout-layout*)
873 (defconstant target-layout-length
874 (layout-length (find-layout 'layout)))
876 (defun target-layout-index (slot-name)
877 ;; KLUDGE: this is a little bit sleazy, but the tricky thing is that
878 ;; structure slots don't have a terribly firm idea of their names.
879 ;; At least here if we change LAYOUT's package of definition, we
880 ;; only have to change one thing...
881 (let* ((name (find-symbol (symbol-name slot-name) "SB!KERNEL"))
882 (layout (find-layout 'layout))
883 (dd (layout-info layout))
884 (slots (dd-slots dd))
885 (dsd (find name slots :key #'dsd-name)))
889 (defun cold-set-layout-slot (cold-layout slot-name value)
892 (+ sb!vm:instance-slots-offset (target-layout-index slot-name))
895 ;;; Return a list of names created from the cold layout INHERITS data
897 (defun listify-cold-inherits (x)
898 (let ((len (descriptor-fixnum (read-wordindexed x
899 sb!vm:vector-length-slot))))
902 (let* ((des (read-wordindexed x (+ sb!vm:vector-data-offset index)))
903 (found (gethash (descriptor-bits des) *cold-layout-names*)))
906 (error "unknown descriptor at index ~S (bits = ~8,'0X)"
908 (descriptor-bits des)))))
911 (declaim (ftype (function (symbol descriptor descriptor descriptor descriptor)
914 (defun make-cold-layout (name length inherits depthoid nuntagged)
915 (let ((result (allocate-boxed-object *dynamic*
916 ;; KLUDGE: Why 1+? -- WHN 19990901
917 ;; header word? -- CSR 20051204
918 (1+ target-layout-length)
919 sb!vm:instance-pointer-lowtag)))
921 (make-other-immediate-descriptor
922 target-layout-length sb!vm:instance-header-widetag))
924 ;; KLUDGE: The offsets into LAYOUT below should probably be pulled out
925 ;; of the cross-compiler's tables at genesis time instead of inserted
926 ;; by hand as bare numeric constants. -- WHN ca. 19990901
928 ;; Set slot 0 = the layout of the layout.
929 (write-wordindexed result sb!vm:instance-slots-offset *layout-layout*)
931 ;; Don't set the CLOS hash value: done in cold-init instead.
933 ;; Set other slot values.
935 ;; leave CLASSOID uninitialized for now
936 (cold-set-layout-slot result 'invalid *nil-descriptor*)
937 (cold-set-layout-slot result 'inherits inherits)
938 (cold-set-layout-slot result 'depthoid depthoid)
939 (cold-set-layout-slot result 'length length)
940 (cold-set-layout-slot result 'info *nil-descriptor*)
941 (cold-set-layout-slot result 'pure *nil-descriptor*)
942 (cold-set-layout-slot result 'n-untagged-slots nuntagged)
943 (cold-set-layout-slot result 'source-location *nil-descriptor*)
944 (cold-set-layout-slot result 'for-std-class-p *nil-descriptor*)
946 (setf (gethash name *cold-layouts*)
949 (descriptor-fixnum length)
950 (listify-cold-inherits inherits)
951 (descriptor-fixnum depthoid)
952 (descriptor-fixnum nuntagged)))
953 (setf (gethash (descriptor-bits result) *cold-layout-names*) name)
957 (defun initialize-layouts ()
959 (clrhash *cold-layouts*)
961 ;; We initially create the layout of LAYOUT itself with NIL as the LAYOUT and
963 (setq *layout-layout* *nil-descriptor*)
964 (let ((xlayout-layout (find-layout 'layout)))
965 (aver (= 0 (layout-n-untagged-slots xlayout-layout)))
966 (setq *layout-layout*
967 (make-cold-layout 'layout
968 (number-to-core target-layout-length)
970 (number-to-core (layout-depthoid xlayout-layout))
973 *layout-layout* sb!vm:instance-slots-offset *layout-layout*)
975 ;; Then we create the layouts that we'll need to make a correct INHERITS
976 ;; vector for the layout of LAYOUT itself..
978 ;; FIXME: The various LENGTH and DEPTHOID numbers should be taken from
979 ;; the compiler's tables, not set by hand.
987 (make-cold-layout 'structure-object
989 (vector-in-core t-layout)
993 (make-cold-layout 'structure!object
995 (vector-in-core t-layout so-layout)
998 (layout-inherits (vector-in-core t-layout
1002 ;; ..and return to backpatch the layout of LAYOUT.
1003 (setf (fourth (gethash 'layout *cold-layouts*))
1004 (listify-cold-inherits layout-inherits))
1005 (cold-set-layout-slot *layout-layout* 'inherits layout-inherits))))
1007 ;;;; interning symbols in the cold image
1009 ;;; In order to avoid having to know about the package format, we
1010 ;;; build a data structure in *COLD-PACKAGE-SYMBOLS* that holds all
1011 ;;; interned symbols along with info about their packages. The data
1012 ;;; structure is a list of sublists, where the sublists have the
1013 ;;; following format:
1014 ;;; (<make-package-arglist>
1015 ;;; <internal-symbols>
1016 ;;; <external-symbols>
1017 ;;; <imported-internal-symbols>
1018 ;;; <imported-external-symbols>
1019 ;;; <shadowing-symbols>
1020 ;;; <package-documentation>)
1022 ;;; KLUDGE: It would be nice to implement the sublists as instances of
1023 ;;; a DEFSTRUCT (:TYPE LIST). (They'd still be lists, but at least we'd be
1024 ;;; using mnemonically-named operators to access them, instead of trying
1025 ;;; to remember what THIRD and FIFTH mean, and hoping that we never
1026 ;;; need to change the list layout..) -- WHN 19990825
1028 ;;; an alist from packages to lists of that package's symbols to be dumped
1029 (defvar *cold-package-symbols*)
1030 (declaim (type list *cold-package-symbols*))
1032 ;;; a map from descriptors to symbols, so that we can back up. The key
1033 ;;; is the address in the target core.
1034 (defvar *cold-symbols*)
1035 (declaim (type hash-table *cold-symbols*))
1037 ;;; sanity check for a symbol we're about to create on the target
1039 ;;; Make sure that the symbol has an appropriate package. In
1040 ;;; particular, catch the so-easy-to-make error of typing something
1041 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1042 ;;; need is SB!KERNEL:%BYTE-BLT.
1043 (defun package-ok-for-target-symbol-p (package)
1044 (let ((package-name (package-name package)))
1046 ;; Cold interning things in these standard packages is OK. (Cold
1047 ;; interning things in the other standard package, CL-USER, isn't
1048 ;; OK. We just use CL-USER to expose symbols whose homes are in
1049 ;; other packages. Thus, trying to cold intern a symbol whose
1050 ;; home package is CL-USER probably means that a coding error has
1051 ;; been made somewhere.)
1052 (find package-name '("COMMON-LISP" "KEYWORD") :test #'string=)
1053 ;; Cold interning something in one of our target-code packages,
1054 ;; which are ever-so-rigorously-and-elegantly distinguished by
1055 ;; this prefix on their names, is OK too.
1056 (string= package-name "SB!" :end1 3 :end2 3)
1057 ;; This one is OK too, since it ends up being COMMON-LISP on the
1059 (string= package-name "SB-XC")
1060 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1061 ;; package in the xc host? something we can't think of
1062 ;; a valid reason to cold intern, anyway...)
1065 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1067 ;;; Most host symbols we dump onto the target are created by SBCL
1068 ;;; itself, so that as long as we avoid gratuitously
1069 ;;; cross-compilation-unfriendly hacks, it just happens that their
1070 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1071 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1072 ;;; in the COMMON-LISP package, where we don't get to create the
1073 ;;; symbols but instead have to use the ones that the xc host created.
1074 ;;; In particular, while ANSI specifies which symbols are exported
1075 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1076 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1077 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1078 ;;; symbols in the CLOS package).
1079 (defun symbol-package-for-target-symbol (symbol)
1080 ;; We want to catch weird symbols like CLISP's
1081 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1082 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1083 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1084 (multiple-value-bind (cl-symbol cl-status)
1085 (find-symbol (symbol-name symbol) *cl-package*)
1086 (if (and (eq symbol cl-symbol)
1087 (eq cl-status :external))
1088 ;; special case, to work around possible xc host weirdness
1089 ;; in COMMON-LISP package
1092 (let ((result (symbol-package symbol)))
1093 (unless (package-ok-for-target-symbol-p result)
1094 (bug "~A in bad package for target: ~A" symbol result))
1097 ;;; Return a handle on an interned symbol. If necessary allocate the
1098 ;;; symbol and record which package the symbol was referenced in. When
1099 ;;; we allocate the symbol, make sure we record a reference to the
1100 ;;; symbol in the home package so that the package gets set.
1101 (defun cold-intern (symbol
1103 (package (symbol-package-for-target-symbol symbol))
1106 (aver (package-ok-for-target-symbol-p package))
1108 ;; Anything on the cross-compilation host which refers to the target
1109 ;; machinery through the host SB-XC package should be translated to
1110 ;; something on the target which refers to the same machinery
1111 ;; through the target COMMON-LISP package.
1112 (let ((p (find-package "SB-XC")))
1113 (when (eq package p)
1114 (setf package *cl-package*))
1115 (when (eq (symbol-package symbol) p)
1116 (setf symbol (intern (symbol-name symbol) *cl-package*))))
1118 (let (;; Information about each cold-interned symbol is stored
1119 ;; in COLD-INTERN-INFO.
1120 ;; (CAR COLD-INTERN-INFO) = descriptor of symbol
1121 ;; (CDR COLD-INTERN-INFO) = list of packages, other than symbol's
1122 ;; own package, referring to symbol
1123 ;; (*COLD-PACKAGE-SYMBOLS* and *COLD-SYMBOLS* store basically the
1124 ;; same information, but with the mapping running the opposite way.)
1125 (cold-intern-info (get symbol 'cold-intern-info)))
1126 (unless cold-intern-info
1127 (cond ((eq (symbol-package-for-target-symbol symbol) package)
1128 (let ((handle (allocate-symbol (symbol-name symbol) :gspace gspace)))
1129 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1130 (when (eq package *keyword-package*)
1131 (cold-set handle handle))
1132 (setq cold-intern-info
1133 (setf (get symbol 'cold-intern-info) (cons handle nil)))))
1135 (cold-intern symbol)
1136 (setq cold-intern-info (get symbol 'cold-intern-info)))))
1137 (unless (or (null package)
1138 (member package (cdr cold-intern-info)))
1139 (push package (cdr cold-intern-info))
1140 (let* ((old-cps-entry (assoc package *cold-package-symbols*))
1141 (cps-entry (or old-cps-entry
1142 (car (push (list package)
1143 *cold-package-symbols*)))))
1144 (unless old-cps-entry
1145 (/show "created *COLD-PACKAGE-SYMBOLS* entry for" package symbol))
1146 (push symbol (rest cps-entry))))
1147 (car cold-intern-info)))
1149 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1150 (defun make-nil-descriptor ()
1151 (let* ((des (allocate-unboxed-object
1156 (result (make-descriptor (descriptor-high des)
1157 (+ (descriptor-low des)
1158 (* 2 sb!vm:n-word-bytes)
1159 (- sb!vm:list-pointer-lowtag
1160 sb!vm:other-pointer-lowtag)))))
1161 (write-wordindexed des
1163 (make-other-immediate-descriptor
1165 sb!vm:symbol-header-widetag))
1166 (write-wordindexed des
1167 (+ 1 sb!vm:symbol-value-slot)
1169 (write-wordindexed des
1170 (+ 2 sb!vm:symbol-value-slot)
1172 (write-wordindexed des
1173 (+ 1 sb!vm:symbol-plist-slot)
1175 (write-wordindexed des
1176 (+ 1 sb!vm:symbol-name-slot)
1177 ;; This is *DYNAMIC*, and DES is *STATIC*,
1178 ;; because that's the way CMU CL did it; I'm
1179 ;; not sure whether there's an underlying
1180 ;; reason. -- WHN 1990826
1181 (base-string-to-core "NIL" *dynamic*))
1182 (write-wordindexed des
1183 (+ 1 sb!vm:symbol-package-slot)
1185 (setf (get nil 'cold-intern-info)
1190 ;;; Since the initial symbols must be allocated before we can intern
1191 ;;; anything else, we intern those here. We also set the value of T.
1192 (defun initialize-non-nil-symbols ()
1194 "Initialize the cold load symbol-hacking data structures."
1195 ;; Intern the others.
1196 (dolist (symbol sb!vm:*static-symbols*)
1197 (let* ((des (cold-intern symbol :gspace *static*))
1198 (offset-wanted (sb!vm:static-symbol-offset symbol))
1199 (offset-found (- (descriptor-low des)
1200 (descriptor-low *nil-descriptor*))))
1201 (unless (= offset-wanted offset-found)
1202 ;; FIXME: should be fatal
1203 (warn "Offset from ~S to ~S is ~W, not ~W"
1208 ;; Establish the value of T.
1209 (let ((t-symbol (cold-intern t :gspace *static*)))
1210 (cold-set t-symbol t-symbol))
1211 ;; Establish the value of *PSEUDO-ATOMIC-BITS* so that the
1212 ;; allocation sequences that expect it to be zero upon entrance
1213 ;; actually find it to be so.
1215 (let ((p-a-a-symbol (cold-intern 'sb!kernel:*pseudo-atomic-bits*
1217 (cold-set p-a-a-symbol (make-fixnum-descriptor 0))))
1219 ;;; a helper function for FINISH-SYMBOLS: Return a cold alist suitable
1220 ;;; to be stored in *!INITIAL-LAYOUTS*.
1221 (defun cold-list-all-layouts ()
1223 (result *nil-descriptor*))
1224 (maphash (lambda (key stuff)
1225 (push (cons key (first stuff)) layouts))
1227 (flet ((sorter (x y)
1228 (let ((xpn (package-name (symbol-package-for-target-symbol x)))
1229 (ypn (package-name (symbol-package-for-target-symbol y))))
1231 ((string= x y) (string< xpn ypn))
1232 (t (string< x y))))))
1233 (setq layouts (sort layouts #'sorter :key #'car)))
1234 (dolist (layout layouts result)
1235 (cold-push (cold-cons (cold-intern (car layout)) (cdr layout))
1238 ;;; Establish initial values for magic symbols.
1240 ;;; Scan over all the symbols referenced in each package in
1241 ;;; *COLD-PACKAGE-SYMBOLS* making that for each one there's an
1242 ;;; appropriate entry in the *!INITIAL-SYMBOLS* data structure to
1244 (defun finish-symbols ()
1246 ;; I think the point of setting these functions into SYMBOL-VALUEs
1247 ;; here, instead of using SYMBOL-FUNCTION, is that in CMU CL
1248 ;; SYMBOL-FUNCTION reduces to FDEFINITION, which is a pretty
1249 ;; hairy operation (involving globaldb.lisp etc.) which we don't
1250 ;; want to invoke early in cold init. -- WHN 2001-12-05
1252 ;; FIXME: So OK, that's a reasonable reason to do something weird like
1253 ;; this, but this is still a weird thing to do, and we should change
1254 ;; the names to highlight that something weird is going on. Perhaps
1255 ;; *MAYBE-GC-FUN*, *INTERNAL-ERROR-FUN*, *HANDLE-BREAKPOINT-FUN*,
1256 ;; and *HANDLE-FUN-END-BREAKPOINT-FUN*...
1257 (dolist (symbol sb!vm::*c-callable-static-symbols*)
1258 (cold-set symbol (cold-fdefinition-object (cold-intern symbol))))
1260 (cold-set 'sb!vm::*current-catch-block* (make-fixnum-descriptor 0))
1261 (cold-set 'sb!vm::*current-unwind-protect-block* (make-fixnum-descriptor 0))
1263 (cold-set '*free-interrupt-context-index* (make-fixnum-descriptor 0))
1265 (cold-set '*!initial-layouts* (cold-list-all-layouts))
1267 (/show "dumping packages" (mapcar #'car *cold-package-symbols*))
1268 (let ((initial-symbols *nil-descriptor*))
1269 (dolist (cold-package-symbols-entry *cold-package-symbols*)
1270 (let* ((cold-package (car cold-package-symbols-entry))
1271 (symbols (cdr cold-package-symbols-entry))
1272 (shadows (package-shadowing-symbols cold-package))
1273 (documentation (base-string-to-core
1274 ;; KLUDGE: NIL punned as 0-length string.
1276 ;; don't propagate the arbitrary
1277 ;; docstring from host packages into
1279 (or (eql cold-package *cl-package*)
1280 (eql cold-package *keyword-package*))
1281 (documentation cold-package t))))
1284 (internal *nil-descriptor*)
1285 (external *nil-descriptor*)
1286 (imported-internal *nil-descriptor*)
1287 (imported-external *nil-descriptor*)
1288 (shadowing *nil-descriptor*))
1289 (declare (type package cold-package)) ; i.e. not a target descriptor
1290 (/show "dumping" cold-package symbols)
1292 ;; FIXME: Add assertions here to make sure that inappropriate stuff
1293 ;; isn't being dumped:
1294 ;; * the CL-USER package
1295 ;; * the SB-COLD package
1296 ;; * any internal symbols in the CL package
1297 ;; * basically any package other than CL, KEYWORD, or the packages
1298 ;; in package-data-list.lisp-expr
1299 ;; and that the structure of the KEYWORD package (e.g. whether
1300 ;; any symbols are internal to it) matches what we want in the
1303 ;; FIXME: It seems possible that by looking at the contents of
1304 ;; packages in the target SBCL we could find which symbols in
1305 ;; package-data-lisp.lisp-expr are now obsolete. (If I
1306 ;; understand correctly, only symbols which actually have
1307 ;; definitions or which are otherwise referred to actually end
1308 ;; up in the target packages.)
1310 (dolist (symbol symbols)
1311 (let ((handle (car (get symbol 'cold-intern-info)))
1312 (imported-p (not (eq (symbol-package-for-target-symbol symbol)
1314 (multiple-value-bind (found where)
1315 (find-symbol (symbol-name symbol) cold-package)
1316 (unless (and where (eq found symbol))
1317 (error "The symbol ~S is not available in ~S."
1320 (when (memq symbol shadows)
1321 (cold-push handle shadowing))
1323 (:internal (if imported-p
1324 (cold-push handle imported-internal)
1326 (cold-push handle internal)
1327 (incf internal-count))))
1328 (:external (if imported-p
1329 (cold-push handle imported-external)
1331 (cold-push handle external)
1332 (incf external-count))))))))
1333 (let ((r *nil-descriptor*))
1334 (cold-push documentation r)
1335 (cold-push shadowing r)
1336 (cold-push imported-external r)
1337 (cold-push imported-internal r)
1338 (cold-push external r)
1339 (cold-push internal r)
1340 (cold-push (make-make-package-args cold-package
1344 ;; FIXME: It would be more space-efficient to use vectors
1345 ;; instead of lists here, and space-efficiency here would be
1346 ;; nice, since it would reduce the peak memory usage in
1347 ;; genesis and cold init.
1348 (cold-push r initial-symbols))))
1349 (cold-set '*!initial-symbols* initial-symbols))
1351 (cold-set '*!initial-fdefn-objects* (list-all-fdefn-objects))
1353 (cold-set '*!reversed-cold-toplevels* *current-reversed-cold-toplevels*)
1354 (cold-set '*!initial-debug-sources* *current-debug-sources*)
1358 (cold-set 'sb!vm::*fp-constant-0d0* (number-to-core 0d0))
1359 (cold-set 'sb!vm::*fp-constant-1d0* (number-to-core 1d0))
1360 (cold-set 'sb!vm::*fp-constant-0f0* (number-to-core 0f0))
1361 (cold-set 'sb!vm::*fp-constant-1f0* (number-to-core 1f0))))
1363 ;;; Make a cold list that can be used as the arg list to MAKE-PACKAGE in
1364 ;;; order to make a package that is similar to PKG.
1365 (defun make-make-package-args (pkg internal-count external-count)
1366 (let* ((use *nil-descriptor*)
1367 (cold-nicknames *nil-descriptor*)
1368 (res *nil-descriptor*))
1369 (dolist (u (package-use-list pkg))
1370 (when (assoc u *cold-package-symbols*)
1371 (cold-push (base-string-to-core (package-name u)) use)))
1372 (let* ((pkg-name (package-name pkg))
1373 ;; Make the package nickname lists for the standard packages
1374 ;; be the minimum specified by ANSI, regardless of what value
1375 ;; the cross-compilation host happens to use.
1376 (warm-nicknames (cond ((string= pkg-name "COMMON-LISP")
1378 ((string= pkg-name "COMMON-LISP-USER")
1380 ((string= pkg-name "KEYWORD")
1382 ;; For packages other than the
1383 ;; standard packages, the nickname
1384 ;; list was specified by our package
1385 ;; setup code, not by properties of
1386 ;; what cross-compilation host we
1387 ;; happened to use, and we can just
1388 ;; propagate it into the target.
1390 (package-nicknames pkg)))))
1391 (dolist (warm-nickname warm-nicknames)
1392 (cold-push (base-string-to-core warm-nickname) cold-nicknames)))
1394 ;; INTERNAL-COUNT and EXTERNAL-COUNT are the number of symbols that
1395 ;; the package contains in the core. We arrange for the package
1396 ;; symbol tables to be created somewhat larger so that they don't
1397 ;; need to be rehashed so easily when additional symbols are
1398 ;; interned during the warm build.
1399 (cold-push (number-to-core (truncate internal-count 0.8)) res)
1400 (cold-push (cold-intern :internal-symbols) res)
1401 (cold-push (number-to-core (truncate external-count 0.8)) res)
1402 (cold-push (cold-intern :external-symbols) res)
1404 (cold-push cold-nicknames res)
1405 (cold-push (cold-intern :nicknames) res)
1408 (cold-push (cold-intern :use) res)
1410 (cold-push (base-string-to-core (package-name pkg)) res)
1413 ;;;; functions and fdefinition objects
1415 ;;; a hash table mapping from fdefinition names to descriptors of cold
1418 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1419 ;;; we want to have only one entry per name, this must be an 'EQUAL
1420 ;;; hash table, not the default 'EQL.
1421 (defvar *cold-fdefn-objects*)
1423 (defvar *cold-fdefn-gspace* nil)
1425 ;;; Given a cold representation of a symbol, return a warm
1427 (defun warm-symbol (des)
1428 ;; Note that COLD-INTERN is responsible for keeping the
1429 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1430 ;; uninterned symbol, the code below will fail. But as long as we
1431 ;; don't need to look up uninterned symbols during bootstrapping,
1433 (multiple-value-bind (symbol found-p)
1434 (gethash (descriptor-bits des) *cold-symbols*)
1435 (declare (type symbol symbol))
1437 (error "no warm symbol"))
1440 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1441 (defun cold-car (des)
1442 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1443 (read-wordindexed des sb!vm:cons-car-slot))
1444 (defun cold-cdr (des)
1445 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1446 (read-wordindexed des sb!vm:cons-cdr-slot))
1447 (defun cold-null (des)
1448 (= (descriptor-bits des)
1449 (descriptor-bits *nil-descriptor*)))
1451 ;;; Given a cold representation of a function name, return a warm
1453 (declaim (ftype (function ((or descriptor symbol)) (or symbol list)) warm-fun-name))
1454 (defun warm-fun-name (des)
1457 ;; This parallels the logic at the start of COLD-INTERN
1458 ;; which re-homes symbols in SB-XC to COMMON-LISP.
1459 (if (eq (symbol-package des) (find-package "SB-XC"))
1460 (intern (symbol-name des) *cl-package*)
1462 (ecase (descriptor-lowtag des)
1463 (#.sb!vm:list-pointer-lowtag
1464 (aver (not (cold-null des))) ; function named NIL? please no..
1465 ;; Do cold (DESTRUCTURING-BIND (COLD-CAR COLD-CADR) DES ..).
1466 (let* ((car-des (cold-car des))
1467 (cdr-des (cold-cdr des))
1468 (cadr-des (cold-car cdr-des))
1469 (cddr-des (cold-cdr cdr-des)))
1470 (aver (cold-null cddr-des))
1471 (list (warm-symbol car-des)
1472 (warm-symbol cadr-des))))
1473 (#.sb!vm:other-pointer-lowtag
1474 (warm-symbol des))))))
1475 (legal-fun-name-or-type-error result)
1478 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw)
1479 (declare (type (or descriptor symbol) cold-name))
1480 (/show0 "/cold-fdefinition-object")
1481 (let ((warm-name (warm-fun-name cold-name)))
1482 (or (gethash warm-name *cold-fdefn-objects*)
1483 (let ((fdefn (allocate-boxed-object (or *cold-fdefn-gspace* *dynamic*)
1484 (1- sb!vm:fdefn-size)
1485 sb!vm:other-pointer-lowtag)))
1487 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
1488 (write-memory fdefn (make-other-immediate-descriptor
1489 (1- sb!vm:fdefn-size) sb!vm:fdefn-widetag))
1490 (write-wordindexed fdefn sb!vm:fdefn-name-slot cold-name)
1491 (unless leave-fn-raw
1492 (write-wordindexed fdefn sb!vm:fdefn-fun-slot
1494 (write-wordindexed fdefn
1495 sb!vm:fdefn-raw-addr-slot
1496 (make-random-descriptor
1497 (cold-foreign-symbol-address "undefined_tramp"))))
1500 ;;; Handle the at-cold-init-time, fset-for-static-linkage operation
1501 ;;; requested by FOP-FSET.
1502 (defun static-fset (cold-name defn)
1503 (declare (type (or descriptor symbol) cold-name))
1504 (let ((fdefn (cold-fdefinition-object cold-name t))
1505 (type (logand (descriptor-low (read-memory defn)) sb!vm:widetag-mask)))
1506 (write-wordindexed fdefn sb!vm:fdefn-fun-slot defn)
1507 (write-wordindexed fdefn
1508 sb!vm:fdefn-raw-addr-slot
1510 (#.sb!vm:simple-fun-header-widetag
1511 (/show0 "static-fset (simple-fun)")
1515 (make-random-descriptor
1516 (+ (logandc2 (descriptor-bits defn)
1518 (ash sb!vm:simple-fun-code-offset
1519 sb!vm:word-shift))))
1520 (#.sb!vm:closure-header-widetag
1521 (/show0 "/static-fset (closure)")
1522 (make-random-descriptor
1523 (cold-foreign-symbol-address "closure_tramp")))))
1526 (defun initialize-static-fns ()
1527 (let ((*cold-fdefn-gspace* *static*))
1528 (dolist (sym sb!vm:*static-funs*)
1529 (let* ((fdefn (cold-fdefinition-object (cold-intern sym)))
1530 (offset (- (+ (- (descriptor-low fdefn)
1531 sb!vm:other-pointer-lowtag)
1532 (* sb!vm:fdefn-raw-addr-slot sb!vm:n-word-bytes))
1533 (descriptor-low *nil-descriptor*)))
1534 (desired (sb!vm:static-fun-offset sym)))
1535 (unless (= offset desired)
1536 ;; FIXME: should be fatal
1537 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1538 sym nil offset desired))))))
1540 (defun list-all-fdefn-objects ()
1542 (result *nil-descriptor*))
1543 (maphash (lambda (key value)
1544 (push (cons key value) fdefns))
1545 *cold-fdefn-objects*)
1546 (flet ((sorter (x y)
1547 (let* ((xbn (fun-name-block-name x))
1548 (ybn (fun-name-block-name y))
1549 (xbnpn (package-name (symbol-package-for-target-symbol xbn)))
1550 (ybnpn (package-name (symbol-package-for-target-symbol ybn))))
1552 ((eql xbn ybn) (consp x))
1553 ((string= xbn ybn) (string< xbnpn ybnpn))
1554 (t (string< xbn ybn))))))
1555 (setq fdefns (sort fdefns #'sorter :key #'car)))
1556 (dolist (fdefn fdefns result)
1557 (cold-push (cdr fdefn) result))))
1559 ;;;; fixups and related stuff
1561 ;;; an EQUAL hash table
1562 (defvar *cold-foreign-symbol-table*)
1563 (declaim (type hash-table *cold-foreign-symbol-table*))
1565 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1567 (defun load-cold-foreign-symbol-table (filename)
1568 (/show "load-cold-foreign-symbol-table" filename)
1569 (with-open-file (file filename)
1570 (loop for line = (read-line file nil nil)
1572 ;; UNIX symbol tables might have tabs in them, and tabs are
1573 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1574 ;; nice portable way to deal with them within Lisp, alas.
1575 ;; Fortunately, it's easy to use UNIX command line tools like
1576 ;; sed to remove the problem, so it's not too painful for us
1577 ;; to push responsibility for converting tabs to spaces out to
1580 ;; Other non-STANDARD-CHARs are problematic for the same reason.
1581 ;; Make sure that there aren't any..
1582 (let ((ch (find-if (lambda (char)
1583 (not (typep char 'standard-char)))
1586 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1589 (setf line (string-trim '(#\space) line))
1590 (let ((p1 (position #\space line :from-end nil))
1591 (p2 (position #\space line :from-end t)))
1592 (if (not (and p1 p2 (< p1 p2)))
1593 ;; KLUDGE: It's too messy to try to understand all
1594 ;; possible output from nm, so we just punt the lines we
1595 ;; don't recognize. We realize that there's some chance
1596 ;; that might get us in trouble someday, so we warn
1598 (warn "ignoring unrecognized line ~S in ~A" line filename)
1599 (multiple-value-bind (value name)
1600 (if (string= "0x" line :end2 2)
1601 (values (parse-integer line :start 2 :end p1 :radix 16)
1602 (subseq line (1+ p2)))
1603 (values (parse-integer line :end p1 :radix 16)
1604 (subseq line (1+ p2))))
1605 ;; KLUDGE CLH 2010-05-31: on darwin, nm gives us
1606 ;; _function but dlsym expects us to look up
1607 ;; function, without the leading _ . Therefore, we
1608 ;; strip it off here.
1610 (when (equal (char name 0) #\_)
1611 (setf name (subseq name 1)))
1612 (multiple-value-bind (old-value found)
1613 (gethash name *cold-foreign-symbol-table*)
1615 (not (= old-value value)))
1616 (warn "redefining ~S from #X~X to #X~X"
1617 name old-value value)))
1618 (/show "adding to *cold-foreign-symbol-table*:" name value)
1619 (setf (gethash name *cold-foreign-symbol-table*) value)
1621 (let ((at-position (position #\@ name)))
1623 (let ((name (subseq name 0 at-position)))
1624 (multiple-value-bind (old-value found)
1625 (gethash name *cold-foreign-symbol-table*)
1627 (not (= old-value value)))
1628 (warn "redefining ~S from #X~X to #X~X"
1629 name old-value value)))
1630 (setf (gethash name *cold-foreign-symbol-table*)
1634 (defun cold-foreign-symbol-address (name)
1635 (or (find-foreign-symbol-in-table name *cold-foreign-symbol-table*)
1636 *foreign-symbol-placeholder-value*
1638 (format *error-output* "~&The foreign symbol table is:~%")
1639 (maphash (lambda (k v)
1640 (format *error-output* "~&~S = #X~8X~%" k v))
1641 *cold-foreign-symbol-table*)
1642 (error "The foreign symbol ~S is undefined." name))))
1644 (defvar *cold-assembler-routines*)
1646 (defvar *cold-assembler-fixups*)
1648 (defun record-cold-assembler-routine (name address)
1649 (/xhow "in RECORD-COLD-ASSEMBLER-ROUTINE" name address)
1650 (push (cons name address)
1651 *cold-assembler-routines*))
1653 (defun record-cold-assembler-fixup (routine
1658 (push (list routine code-object offset kind)
1659 *cold-assembler-fixups*))
1661 (defun lookup-assembler-reference (symbol)
1662 (let ((value (cdr (assoc symbol *cold-assembler-routines*))))
1663 ;; FIXME: Should this be ERROR instead of WARN?
1665 (warn "Assembler routine ~S not defined." symbol))
1668 ;;; The x86 port needs to store code fixups along with code objects if
1669 ;;; they are to be moved, so fixups for code objects in the dynamic
1670 ;;; heap need to be noted.
1672 (defvar *load-time-code-fixups*)
1675 (defun note-load-time-code-fixup (code-object offset)
1676 ;; If CODE-OBJECT might be moved
1677 (when (= (gspace-identifier (descriptor-intuit-gspace code-object))
1678 dynamic-core-space-id)
1679 (push offset (gethash (descriptor-bits code-object)
1680 *load-time-code-fixups*
1685 (defun output-load-time-code-fixups ()
1686 (let ((fixup-infos nil))
1688 (lambda (code-object-address fixup-offsets)
1689 (push (cons code-object-address fixup-offsets) fixup-infos))
1690 *load-time-code-fixups*)
1691 (setq fixup-infos (sort fixup-infos #'< :key #'car))
1692 (dolist (fixup-info fixup-infos)
1693 (let ((code-object-address (car fixup-info))
1694 (fixup-offsets (cdr fixup-info)))
1696 (allocate-vector-object
1697 *dynamic* sb!vm:n-word-bits (length fixup-offsets)
1698 sb!vm:simple-array-unsigned-byte-32-widetag)))
1699 (do ((index sb!vm:vector-data-offset (1+ index))
1700 (fixups fixup-offsets (cdr fixups)))
1702 (write-wordindexed fixup-vector index
1703 (make-random-descriptor (car fixups))))
1704 ;; KLUDGE: The fixup vector is stored as the first constant,
1705 ;; not as a separately-named slot.
1706 (write-wordindexed (make-random-descriptor code-object-address)
1707 sb!vm:code-constants-offset
1710 ;;; Given a pointer to a code object and an offset relative to the
1711 ;;; tail of the code object's header, return an offset relative to the
1712 ;;; (beginning of the) code object.
1714 ;;; FIXME: It might be clearer to reexpress
1715 ;;; (LET ((X (CALC-OFFSET CODE-OBJECT OFFSET0))) ..)
1717 ;;; (LET ((X (+ OFFSET0 (CODE-OBJECT-HEADER-N-BYTES CODE-OBJECT)))) ..).
1718 (declaim (ftype (function (descriptor sb!vm:word)) calc-offset))
1719 (defun calc-offset (code-object offset-from-tail-of-header)
1720 (let* ((header (read-memory code-object))
1721 (header-n-words (ash (descriptor-bits header)
1722 (- sb!vm:n-widetag-bits)))
1723 (header-n-bytes (ash header-n-words sb!vm:word-shift))
1724 (result (+ offset-from-tail-of-header header-n-bytes)))
1727 (declaim (ftype (function (descriptor sb!vm:word sb!vm:word keyword))
1729 (defun do-cold-fixup (code-object after-header value kind)
1730 (let* ((offset-within-code-object (calc-offset code-object after-header))
1731 (gspace-bytes (descriptor-bytes code-object))
1732 (gspace-byte-offset (+ (descriptor-byte-offset code-object)
1733 offset-within-code-object))
1734 (gspace-byte-address (gspace-byte-address
1735 (descriptor-gspace code-object))))
1736 (ecase +backend-fasl-file-implementation+
1737 ;; See CMU CL source for other formerly-supported architectures
1738 ;; (and note that you have to rewrite them to use BVREF-X
1739 ;; instead of SAP-REF).
1743 (assert (zerop (ldb (byte 2 0) value))))
1745 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1746 (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
1747 (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
1748 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1749 (ldb (byte 8 48) value)
1750 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1751 (ldb (byte 8 56) value))))
1753 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1754 (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
1755 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1756 (ldb (byte 8 32) value)
1757 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1758 (ldb (byte 8 40) value))))
1760 (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
1761 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1762 (ldb (byte 8 16) value)
1763 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1764 (ldb (byte 8 24) value))))
1766 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1767 (ldb (byte 8 0) value)
1768 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1769 (ldb (byte 8 8) value)))))
1773 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1774 (logior (mask-field (byte 18 14)
1775 (bvref-32 gspace-bytes gspace-byte-offset))
1777 (1+ (ash (ldb (byte 13 0) value) 1))
1778 (ash (ldb (byte 13 0) value) 1)))))
1780 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1781 (logior (mask-field (byte 18 14)
1782 (bvref-32 gspace-bytes gspace-byte-offset))
1784 (1+ (ash (ldb (byte 10 0) value) 1))
1785 (ash (ldb (byte 11 0) value) 1)))))
1787 (let ((low-bits (ldb (byte 11 0) value)))
1788 (assert (<= 0 low-bits (1- (ash 1 4)))))
1789 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1790 (logior (ash (dpb (ldb (byte 4 0) value)
1792 (ldb (byte 1 4) value)) 17)
1793 (logand (bvref-32 gspace-bytes gspace-byte-offset)
1796 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1797 (logior (mask-field (byte 11 21)
1798 (bvref-32 gspace-bytes gspace-byte-offset))
1799 (ash (ldb (byte 5 13) value) 16)
1800 (ash (ldb (byte 2 18) value) 14)
1801 (ash (ldb (byte 2 11) value) 12)
1802 (ash (ldb (byte 11 20) value) 1)
1803 (ldb (byte 1 31) value))))
1805 (let ((bits (ldb (byte 9 2) value)))
1806 (assert (zerop (ldb (byte 2 0) value)))
1807 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1808 (logior (ash bits 3)
1809 (mask-field (byte 1 1) (bvref-32 gspace-bytes gspace-byte-offset))
1810 (mask-field (byte 3 13) (bvref-32 gspace-bytes gspace-byte-offset))
1811 (mask-field (byte 11 21) (bvref-32 gspace-bytes gspace-byte-offset))))))))
1815 (assert (zerop (ash value -28)))
1816 (setf (ldb (byte 26 0)
1817 (bvref-32 gspace-bytes gspace-byte-offset))
1820 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1821 (logior (mask-field (byte 16 16)
1822 (bvref-32 gspace-bytes gspace-byte-offset))
1823 (ash (1+ (ldb (byte 17 15) value)) -1))))
1825 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1826 (logior (mask-field (byte 16 16)
1827 (bvref-32 gspace-bytes gspace-byte-offset))
1828 (ldb (byte 16 0) value))))))
1829 ;; FIXME: PowerPC Fixups are not fully implemented. The bit
1830 ;; here starts to set things up to work properly, but there
1831 ;; needs to be corresponding code in ppc-vm.lisp
1835 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1836 (dpb (ash value -2) (byte 24 2)
1837 (bvref-32 gspace-bytes gspace-byte-offset))))
1839 (let* ((un-fixed-up (bvref-16 gspace-bytes
1840 (+ gspace-byte-offset 2)))
1841 (fixed-up (+ un-fixed-up value))
1842 (h (ldb (byte 16 16) fixed-up))
1843 (l (ldb (byte 16 0) fixed-up)))
1844 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1845 (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
1847 (let* ((un-fixed-up (bvref-16 gspace-bytes
1848 (+ gspace-byte-offset 2)))
1849 (fixed-up (+ un-fixed-up value)))
1850 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1851 (ldb (byte 16 0) fixed-up))))))
1855 (error "can't deal with call fixups yet"))
1857 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1858 (dpb (ldb (byte 22 10) value)
1860 (bvref-32 gspace-bytes gspace-byte-offset))))
1862 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1863 (dpb (ldb (byte 10 0) value)
1865 (bvref-32 gspace-bytes gspace-byte-offset))))))
1867 ;; XXX: Note that un-fixed-up is read via bvref-word, which is
1868 ;; 64 bits wide on x86-64, but the fixed-up value is written
1869 ;; via bvref-32. This would make more sense if we supported
1870 ;; :absolute64 fixups, but apparently the cross-compiler
1871 ;; doesn't dump them.
1872 (let* ((un-fixed-up (bvref-word gspace-bytes
1873 gspace-byte-offset))
1874 (code-object-start-addr (logandc2 (descriptor-bits code-object)
1875 sb!vm:lowtag-mask)))
1876 (assert (= code-object-start-addr
1877 (+ gspace-byte-address
1878 (descriptor-byte-offset code-object))))
1881 (let ((fixed-up (+ value un-fixed-up)))
1882 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1884 ;; comment from CMU CL sources:
1886 ;; Note absolute fixups that point within the object.
1887 ;; KLUDGE: There seems to be an implicit assumption in
1888 ;; the old CMU CL code here, that if it doesn't point
1889 ;; before the object, it must point within the object
1890 ;; (not beyond it). It would be good to add an
1891 ;; explanation of why that's true, or an assertion that
1892 ;; it's really true, or both.
1894 ;; One possible explanation is that all absolute fixups
1895 ;; point either within the code object, within the
1896 ;; runtime, within read-only or static-space, or within
1897 ;; the linkage-table space. In all x86 configurations,
1898 ;; these areas are prior to the start of dynamic space,
1899 ;; where all the code-objects are loaded.
1901 (unless (< fixed-up code-object-start-addr)
1902 (note-load-time-code-fixup code-object
1904 (:relative ; (used for arguments to X86 relative CALL instruction)
1905 (let ((fixed-up (- (+ value un-fixed-up)
1908 4))) ; "length of CALL argument"
1909 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1911 ;; Note relative fixups that point outside the code
1912 ;; object, which is to say all relative fixups, since
1913 ;; relative addressing within a code object never needs
1916 (note-load-time-code-fixup code-object
1917 after-header))))))))
1920 (defun resolve-assembler-fixups ()
1921 (dolist (fixup *cold-assembler-fixups*)
1922 (let* ((routine (car fixup))
1923 (value (lookup-assembler-reference routine)))
1925 (do-cold-fixup (second fixup) (third fixup) value (fourth fixup))))))
1927 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
1928 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
1929 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
1930 ;;; target-load.lisp refers to.
1931 (defun foreign-symbols-to-core ()
1933 (result *nil-descriptor*))
1934 (maphash (lambda (symbol value)
1935 (push (cons symbol value) symbols))
1936 *cold-foreign-symbol-table*)
1937 (setq symbols (sort symbols #'string< :key #'car))
1938 (dolist (symbol symbols)
1939 (cold-push (cold-cons (base-string-to-core (car symbol))
1940 (number-to-core (cdr symbol)))
1942 (cold-set (cold-intern 'sb!kernel:*!initial-foreign-symbols*) result))
1943 (let ((result *nil-descriptor*))
1944 (dolist (rtn (sort (copy-list *cold-assembler-routines*) #'string< :key #'car))
1945 (cold-push (cold-cons (cold-intern (car rtn))
1946 (number-to-core (cdr rtn)))
1948 (cold-set (cold-intern '*!initial-assembler-routines*) result)))
1951 ;;;; general machinery for cold-loading FASL files
1953 ;;; FOP functions for cold loading
1954 (defvar *cold-fop-funs*
1955 ;; We start out with a copy of the ordinary *FOP-FUNS*. The ones
1956 ;; which aren't appropriate for cold load will be destructively
1958 (copy-seq *fop-funs*))
1960 ;;; Cause a fop to have a special definition for cold load.
1962 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
1963 ;;; (1) looks up the code for this name (created by a previous
1964 ;; DEFINE-FOP) instead of creating a code, and
1965 ;;; (2) stores its definition in the *COLD-FOP-FUNS* vector,
1966 ;;; instead of storing in the *FOP-FUNS* vector.
1967 (defmacro define-cold-fop ((name &key (pushp t) (stackp t)) &rest forms)
1968 (aver (member pushp '(nil t)))
1969 (aver (member stackp '(nil t)))
1970 (let ((code (get name 'fop-code))
1971 (fname (symbolicate "COLD-" name)))
1973 (error "~S is not a defined FOP." name))
1977 `((with-fop-stack ,pushp ,@forms))
1979 (setf (svref *cold-fop-funs* ,code) #',fname))))
1981 (defmacro clone-cold-fop ((name &key (pushp t) (stackp t))
1984 (aver (member pushp '(nil t)))
1985 (aver (member stackp '(nil t)))
1987 (macrolet ((clone-arg () '(read-word-arg)))
1988 (define-cold-fop (,name :pushp ,pushp :stackp ,stackp) ,@forms))
1989 (macrolet ((clone-arg () '(read-byte-arg)))
1990 (define-cold-fop (,small-name :pushp ,pushp :stackp ,stackp) ,@forms))))
1992 ;;; Cause a fop to be undefined in cold load.
1993 (defmacro not-cold-fop (name)
1994 `(define-cold-fop (,name)
1995 (error "The fop ~S is not supported in cold load." ',name)))
1997 ;;; COLD-LOAD loads stuff into the core image being built by calling
1998 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
1999 ;;; loading functions.
2000 (defun cold-load (filename)
2002 "Load the file named by FILENAME into the cold load image being built."
2003 (let* ((*fop-funs* *cold-fop-funs*)
2004 (*cold-load-filename* (etypecase filename
2006 (pathname (namestring filename)))))
2007 (with-open-file (s filename :element-type '(unsigned-byte 8))
2008 (load-as-fasl s nil nil))))
2010 ;;;; miscellaneous cold fops
2012 (define-cold-fop (fop-misc-trap) *unbound-marker*)
2014 (define-cold-fop (fop-short-character)
2015 (make-character-descriptor (read-byte-arg)))
2017 (define-cold-fop (fop-empty-list) nil)
2018 (define-cold-fop (fop-truth) t)
2020 (clone-cold-fop (fop-struct)
2022 (let* ((size (clone-arg))
2023 (result (allocate-boxed-object *dynamic*
2025 sb!vm:instance-pointer-lowtag))
2026 (layout (pop-stack))
2031 (+ sb!vm:instance-slots-offset
2032 (target-layout-index 'n-untagged-slots)))))
2033 (ntagged (- size nuntagged)))
2034 (write-memory result (make-other-immediate-descriptor
2035 size sb!vm:instance-header-widetag))
2036 (write-wordindexed result sb!vm:instance-slots-offset layout)
2037 (do ((index 1 (1+ index)))
2039 (declare (fixnum index))
2040 (write-wordindexed result
2041 (+ index sb!vm:instance-slots-offset)
2042 (if (>= index ntagged)
2043 (descriptor-word-sized-integer (pop-stack))
2047 (define-cold-fop (fop-layout)
2048 (let* ((nuntagged-des (pop-stack))
2049 (length-des (pop-stack))
2050 (depthoid-des (pop-stack))
2051 (cold-inherits (pop-stack))
2053 (old (gethash name *cold-layouts*)))
2054 (declare (type descriptor length-des depthoid-des cold-inherits))
2055 (declare (type symbol name))
2056 ;; If a layout of this name has been defined already
2058 ;; Enforce consistency between the previous definition and the
2059 ;; current definition, then return the previous definition.
2061 ;; FIXME: This would be more maintainable if we used
2062 ;; DEFSTRUCT (:TYPE LIST) to define COLD-LAYOUT. -- WHN 19990825
2063 (old-layout-descriptor
2070 (declare (type descriptor old-layout-descriptor))
2071 (declare (type index old-length old-nuntagged))
2072 (declare (type fixnum old-depthoid))
2073 (declare (type list old-inherits-list))
2074 (aver (eq name old-name))
2075 (let ((length (descriptor-fixnum length-des))
2076 (inherits-list (listify-cold-inherits cold-inherits))
2077 (depthoid (descriptor-fixnum depthoid-des))
2078 (nuntagged (descriptor-fixnum nuntagged-des)))
2079 (unless (= length old-length)
2080 (error "cold loading a reference to class ~S when the compile~%~
2081 time length was ~S and current length is ~S"
2085 (unless (equal inherits-list old-inherits-list)
2086 (error "cold loading a reference to class ~S when the compile~%~
2087 time inherits were ~S~%~
2088 and current inherits are ~S"
2092 (unless (= depthoid old-depthoid)
2093 (error "cold loading a reference to class ~S when the compile~%~
2094 time inheritance depthoid was ~S and current inheritance~%~
2099 (unless (= nuntagged old-nuntagged)
2100 (error "cold loading a reference to class ~S when the compile~%~
2101 time number of untagged slots was ~S and is currently ~S"
2105 old-layout-descriptor)
2106 ;; Make a new definition from scratch.
2107 (make-cold-layout name length-des cold-inherits depthoid-des
2110 ;;;; cold fops for loading symbols
2112 ;;; Load a symbol SIZE characters long from *FASL-INPUT-STREAM* and
2113 ;;; intern that symbol in PACKAGE.
2114 (defun cold-load-symbol (size package)
2115 (let ((string (make-string size)))
2116 (read-string-as-bytes *fasl-input-stream* string)
2117 (intern string package)))
2119 (macrolet ((frob (name pname-len package-len)
2120 `(define-cold-fop (,name)
2121 (let ((index (read-arg ,package-len)))
2123 (cold-load-symbol (read-arg ,pname-len)
2124 (svref *current-fop-table* index)))))))
2125 (frob fop-symbol-in-package-save #.sb!vm:n-word-bytes #.sb!vm:n-word-bytes)
2126 (frob fop-small-symbol-in-package-save 1 #.sb!vm:n-word-bytes)
2127 (frob fop-symbol-in-byte-package-save #.sb!vm:n-word-bytes 1)
2128 (frob fop-small-symbol-in-byte-package-save 1 1))
2130 (clone-cold-fop (fop-lisp-symbol-save)
2131 (fop-lisp-small-symbol-save)
2132 (push-fop-table (cold-load-symbol (clone-arg) *cl-package*)))
2134 (clone-cold-fop (fop-keyword-symbol-save)
2135 (fop-keyword-small-symbol-save)
2136 (push-fop-table (cold-load-symbol (clone-arg) *keyword-package*)))
2138 (clone-cold-fop (fop-uninterned-symbol-save)
2139 (fop-uninterned-small-symbol-save)
2140 (let* ((size (clone-arg))
2141 (name (make-string size)))
2142 (read-string-as-bytes *fasl-input-stream* name)
2143 (let ((symbol-des (allocate-symbol name)))
2144 (push-fop-table symbol-des))))
2146 ;;;; cold fops for loading packages
2148 (clone-cold-fop (fop-named-package-save :stackp nil)
2149 (fop-small-named-package-save)
2150 (let* ((size (clone-arg))
2151 (name (make-string size)))
2152 (read-string-as-bytes *fasl-input-stream* name)
2153 (push-fop-table (find-package name))))
2155 ;;;; cold fops for loading lists
2157 ;;; Make a list of the top LENGTH things on the fop stack. The last
2158 ;;; cdr of the list is set to LAST.
2159 (defmacro cold-stack-list (length last)
2160 `(do* ((index ,length (1- index))
2161 (result ,last (cold-cons (pop-stack) result)))
2162 ((= index 0) result)
2163 (declare (fixnum index))))
2165 (define-cold-fop (fop-list)
2166 (cold-stack-list (read-byte-arg) *nil-descriptor*))
2167 (define-cold-fop (fop-list*)
2168 (cold-stack-list (read-byte-arg) (pop-stack)))
2169 (define-cold-fop (fop-list-1)
2170 (cold-stack-list 1 *nil-descriptor*))
2171 (define-cold-fop (fop-list-2)
2172 (cold-stack-list 2 *nil-descriptor*))
2173 (define-cold-fop (fop-list-3)
2174 (cold-stack-list 3 *nil-descriptor*))
2175 (define-cold-fop (fop-list-4)
2176 (cold-stack-list 4 *nil-descriptor*))
2177 (define-cold-fop (fop-list-5)
2178 (cold-stack-list 5 *nil-descriptor*))
2179 (define-cold-fop (fop-list-6)
2180 (cold-stack-list 6 *nil-descriptor*))
2181 (define-cold-fop (fop-list-7)
2182 (cold-stack-list 7 *nil-descriptor*))
2183 (define-cold-fop (fop-list-8)
2184 (cold-stack-list 8 *nil-descriptor*))
2185 (define-cold-fop (fop-list*-1)
2186 (cold-stack-list 1 (pop-stack)))
2187 (define-cold-fop (fop-list*-2)
2188 (cold-stack-list 2 (pop-stack)))
2189 (define-cold-fop (fop-list*-3)
2190 (cold-stack-list 3 (pop-stack)))
2191 (define-cold-fop (fop-list*-4)
2192 (cold-stack-list 4 (pop-stack)))
2193 (define-cold-fop (fop-list*-5)
2194 (cold-stack-list 5 (pop-stack)))
2195 (define-cold-fop (fop-list*-6)
2196 (cold-stack-list 6 (pop-stack)))
2197 (define-cold-fop (fop-list*-7)
2198 (cold-stack-list 7 (pop-stack)))
2199 (define-cold-fop (fop-list*-8)
2200 (cold-stack-list 8 (pop-stack)))
2202 ;;;; cold fops for loading vectors
2204 (clone-cold-fop (fop-base-string)
2205 (fop-small-base-string)
2206 (let* ((len (clone-arg))
2207 (string (make-string len)))
2208 (read-string-as-bytes *fasl-input-stream* string)
2209 (base-string-to-core string)))
2212 (clone-cold-fop (fop-character-string)
2213 (fop-small-character-string)
2214 (bug "CHARACTER-STRING dumped by cross-compiler."))
2216 (clone-cold-fop (fop-vector)
2218 (let* ((size (clone-arg))
2219 (result (allocate-vector-object *dynamic*
2222 sb!vm:simple-vector-widetag)))
2223 (do ((index (1- size) (1- index)))
2225 (declare (fixnum index))
2226 (write-wordindexed result
2227 (+ index sb!vm:vector-data-offset)
2231 (define-cold-fop (fop-int-vector)
2232 (let* ((len (read-word-arg))
2233 (sizebits (read-byte-arg))
2234 (type (case sizebits
2235 (0 sb!vm:simple-array-nil-widetag)
2236 (1 sb!vm:simple-bit-vector-widetag)
2237 (2 sb!vm:simple-array-unsigned-byte-2-widetag)
2238 (4 sb!vm:simple-array-unsigned-byte-4-widetag)
2239 (7 (prog1 sb!vm:simple-array-unsigned-byte-7-widetag
2241 (8 sb!vm:simple-array-unsigned-byte-8-widetag)
2242 (15 (prog1 sb!vm:simple-array-unsigned-byte-15-widetag
2243 (setf sizebits 16)))
2244 (16 sb!vm:simple-array-unsigned-byte-16-widetag)
2245 (31 (prog1 sb!vm:simple-array-unsigned-byte-31-widetag
2246 (setf sizebits 32)))
2247 (32 sb!vm:simple-array-unsigned-byte-32-widetag)
2248 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
2249 (63 (prog1 sb!vm:simple-array-unsigned-byte-63-widetag
2250 (setf sizebits 64)))
2251 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
2252 (64 sb!vm:simple-array-unsigned-byte-64-widetag)
2253 (t (error "losing element size: ~W" sizebits))))
2254 (result (allocate-vector-object *dynamic* sizebits len type))
2255 (start (+ (descriptor-byte-offset result)
2256 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2258 (ceiling (* len sizebits)
2259 sb!vm:n-byte-bits))))
2260 (read-bigvec-as-sequence-or-die (descriptor-bytes result)
2266 (define-cold-fop (fop-single-float-vector)
2267 (let* ((len (read-word-arg))
2268 (result (allocate-vector-object
2272 sb!vm:simple-array-single-float-widetag))
2273 (start (+ (descriptor-byte-offset result)
2274 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2275 (end (+ start (* len 4))))
2276 (read-bigvec-as-sequence-or-die (descriptor-bytes result)
2282 (not-cold-fop fop-double-float-vector)
2283 #!+long-float (not-cold-fop fop-long-float-vector)
2284 (not-cold-fop fop-complex-single-float-vector)
2285 (not-cold-fop fop-complex-double-float-vector)
2286 #!+long-float (not-cold-fop fop-complex-long-float-vector)
2288 (define-cold-fop (fop-array)
2289 (let* ((rank (read-word-arg))
2290 (data-vector (pop-stack))
2291 (result (allocate-boxed-object *dynamic*
2292 (+ sb!vm:array-dimensions-offset rank)
2293 sb!vm:other-pointer-lowtag)))
2294 (write-memory result
2295 (make-other-immediate-descriptor rank
2296 sb!vm:simple-array-widetag))
2297 (write-wordindexed result sb!vm:array-fill-pointer-slot *nil-descriptor*)
2298 (write-wordindexed result sb!vm:array-data-slot data-vector)
2299 (write-wordindexed result sb!vm:array-displacement-slot *nil-descriptor*)
2300 (write-wordindexed result sb!vm:array-displaced-p-slot *nil-descriptor*)
2301 (write-wordindexed result sb!vm:array-displaced-from-slot *nil-descriptor*)
2302 (let ((total-elements 1))
2303 (dotimes (axis rank)
2304 (let ((dim (pop-stack)))
2305 (unless (is-fixnum-lowtag (descriptor-lowtag dim))
2306 (error "non-fixnum dimension? (~S)" dim))
2307 (setf total-elements
2309 (logior (ash (descriptor-high dim)
2310 (- descriptor-low-bits
2311 sb!vm:n-fixnum-tag-bits))
2312 (ash (descriptor-low dim)
2313 sb!vm:n-fixnum-tag-bits))))
2314 (write-wordindexed result
2315 (+ sb!vm:array-dimensions-offset axis)
2317 (write-wordindexed result
2318 sb!vm:array-elements-slot
2319 (make-fixnum-descriptor total-elements)))
2323 ;;;; cold fops for loading numbers
2325 (defmacro define-cold-number-fop (fop)
2326 `(define-cold-fop (,fop :stackp nil)
2327 ;; Invoke the ordinary warm version of this fop to push the
2330 ;; Replace the warm fop result with the cold image of the warm
2333 (let ((number (pop-stack)))
2334 (number-to-core number)))))
2336 (define-cold-number-fop fop-single-float)
2337 (define-cold-number-fop fop-double-float)
2338 (define-cold-number-fop fop-integer)
2339 (define-cold-number-fop fop-small-integer)
2340 (define-cold-number-fop fop-word-integer)
2341 (define-cold-number-fop fop-byte-integer)
2342 (define-cold-number-fop fop-complex-single-float)
2343 (define-cold-number-fop fop-complex-double-float)
2345 (define-cold-fop (fop-ratio)
2346 (let ((den (pop-stack)))
2347 (number-pair-to-core (pop-stack) den sb!vm:ratio-widetag)))
2349 (define-cold-fop (fop-complex)
2350 (let ((im (pop-stack)))
2351 (number-pair-to-core (pop-stack) im sb!vm:complex-widetag)))
2353 ;;;; cold fops for calling (or not calling)
2355 (not-cold-fop fop-eval)
2356 (not-cold-fop fop-eval-for-effect)
2358 (defvar *load-time-value-counter*)
2360 (define-cold-fop (fop-funcall)
2361 (unless (= (read-byte-arg) 0)
2362 (error "You can't FOP-FUNCALL arbitrary stuff in cold load."))
2363 (let ((counter *load-time-value-counter*))
2364 (cold-push (cold-cons
2365 (cold-intern :load-time-value)
2369 (number-to-core counter)
2371 *current-reversed-cold-toplevels*)
2372 (setf *load-time-value-counter* (1+ counter))
2373 (make-descriptor 0 0 :load-time-value counter)))
2375 (defun finalize-load-time-value-noise ()
2376 (cold-set (cold-intern '*!load-time-values*)
2377 (allocate-vector-object *dynamic*
2379 *load-time-value-counter*
2380 sb!vm:simple-vector-widetag)))
2382 (define-cold-fop (fop-funcall-for-effect :pushp nil)
2383 (if (= (read-byte-arg) 0)
2384 (cold-push (pop-stack)
2385 *current-reversed-cold-toplevels*)
2386 (error "You can't FOP-FUNCALL arbitrary stuff in cold load.")))
2388 ;;;; cold fops for fixing up circularities
2390 (define-cold-fop (fop-rplaca :pushp nil)
2391 (let ((obj (svref *current-fop-table* (read-word-arg)))
2392 (idx (read-word-arg)))
2393 (write-memory (cold-nthcdr idx obj) (pop-stack))))
2395 (define-cold-fop (fop-rplacd :pushp nil)
2396 (let ((obj (svref *current-fop-table* (read-word-arg)))
2397 (idx (read-word-arg)))
2398 (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2400 (define-cold-fop (fop-svset :pushp nil)
2401 (let ((obj (svref *current-fop-table* (read-word-arg)))
2402 (idx (read-word-arg)))
2403 (write-wordindexed obj
2405 (ecase (descriptor-lowtag obj)
2406 (#.sb!vm:instance-pointer-lowtag 1)
2407 (#.sb!vm:other-pointer-lowtag 2)))
2410 (define-cold-fop (fop-structset :pushp nil)
2411 (let ((obj (svref *current-fop-table* (read-word-arg)))
2412 (idx (read-word-arg)))
2413 (write-wordindexed obj (1+ idx) (pop-stack))))
2415 ;;; In the original CMUCL code, this actually explicitly declared PUSHP
2416 ;;; to be T, even though that's what it defaults to in DEFINE-COLD-FOP.
2417 (define-cold-fop (fop-nthcdr)
2418 (cold-nthcdr (read-word-arg) (pop-stack)))
2420 (defun cold-nthcdr (index obj)
2422 (setq obj (read-wordindexed obj 1)))
2425 ;;;; cold fops for loading code objects and functions
2427 ;;; the names of things which have had COLD-FSET used on them already
2428 ;;; (used to make sure that we don't try to statically link a name to
2429 ;;; more than one definition)
2430 (defparameter *cold-fset-warm-names*
2431 ;; This can't be an EQL hash table because names can be conses, e.g.
2433 (make-hash-table :test 'equal))
2435 (define-cold-fop (fop-fset :pushp nil)
2436 (let* ((fn (pop-stack))
2437 (cold-name (pop-stack))
2438 (warm-name (warm-fun-name cold-name)))
2439 (if (gethash warm-name *cold-fset-warm-names*)
2440 (error "duplicate COLD-FSET for ~S" warm-name)
2441 (setf (gethash warm-name *cold-fset-warm-names*) t))
2442 (static-fset cold-name fn)))
2444 (define-cold-fop (fop-note-debug-source :pushp nil)
2445 (let ((debug-source (pop-stack)))
2446 (cold-push debug-source *current-debug-sources*)))
2448 (define-cold-fop (fop-fdefinition)
2449 (cold-fdefinition-object (pop-stack)))
2451 (define-cold-fop (fop-sanctify-for-execution)
2454 ;;; Setting this variable shows what code looks like before any
2455 ;;; fixups (or function headers) are applied.
2456 #!+sb-show (defvar *show-pre-fixup-code-p* nil)
2458 ;;; FIXME: The logic here should be converted into a function
2459 ;;; COLD-CODE-FOP-GUTS (NCONST CODE-SIZE) called by DEFINE-COLD-FOP
2460 ;;; FOP-CODE and DEFINE-COLD-FOP FOP-SMALL-CODE, so that
2461 ;;; variable-capture nastiness like (LET ((NCONST ,NCONST) ..) ..)
2462 ;;; doesn't keep me awake at night.
2463 (defmacro define-cold-code-fop (name nconst code-size)
2464 `(define-cold-fop (,name)
2465 (let* ((nconst ,nconst)
2466 (code-size ,code-size)
2467 (raw-header-n-words (+ sb!vm:code-trace-table-offset-slot nconst))
2469 ;; Note: we round the number of constants up to ensure
2470 ;; that the code vector will be properly aligned.
2471 (round-up raw-header-n-words 2))
2472 (des (allocate-cold-descriptor *dynamic*
2473 (+ (ash header-n-words
2476 sb!vm:other-pointer-lowtag)))
2478 (make-other-immediate-descriptor
2479 header-n-words sb!vm:code-header-widetag))
2480 (write-wordindexed des
2481 sb!vm:code-code-size-slot
2482 (make-fixnum-descriptor
2483 (ash (+ code-size (1- (ash 1 sb!vm:word-shift)))
2484 (- sb!vm:word-shift))))
2485 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2486 (write-wordindexed des sb!vm:code-debug-info-slot (pop-stack))
2487 (when (oddp raw-header-n-words)
2488 (write-wordindexed des
2490 (make-random-descriptor 0)))
2491 (do ((index (1- raw-header-n-words) (1- index)))
2492 ((< index sb!vm:code-trace-table-offset-slot))
2493 (write-wordindexed des index (pop-stack)))
2494 (let* ((start (+ (descriptor-byte-offset des)
2495 (ash header-n-words sb!vm:word-shift)))
2496 (end (+ start code-size)))
2497 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2502 (when *show-pre-fixup-code-p*
2503 (format *trace-output*
2504 "~&/raw code from code-fop ~W ~W:~%"
2507 (do ((i start (+ i sb!vm:n-word-bytes)))
2509 (format *trace-output*
2510 "/#X~8,'0x: #X~8,'0x~%"
2511 (+ i (gspace-byte-address (descriptor-gspace des)))
2512 (bvref-32 (descriptor-bytes des) i)))))
2515 (define-cold-code-fop fop-code (read-word-arg) (read-word-arg))
2517 (define-cold-code-fop fop-small-code (read-byte-arg) (read-halfword-arg))
2519 (clone-cold-fop (fop-alter-code :pushp nil)
2520 (fop-byte-alter-code)
2521 (let ((slot (clone-arg))
2524 (write-wordindexed code slot value)))
2526 (define-cold-fop (fop-fun-entry)
2527 (let* ((info (pop-stack))
2529 (arglist (pop-stack))
2531 (code-object (pop-stack))
2532 (offset (calc-offset code-object (read-word-arg)))
2533 (fn (descriptor-beyond code-object
2535 sb!vm:fun-pointer-lowtag))
2536 (next (read-wordindexed code-object sb!vm:code-entry-points-slot)))
2537 (unless (zerop (logand offset sb!vm:lowtag-mask))
2538 (error "unaligned function entry: ~S at #X~X" name offset))
2539 (write-wordindexed code-object sb!vm:code-entry-points-slot fn)
2541 (make-other-immediate-descriptor
2542 (ash offset (- sb!vm:word-shift))
2543 sb!vm:simple-fun-header-widetag))
2544 (write-wordindexed fn
2545 sb!vm:simple-fun-self-slot
2546 ;; KLUDGE: Wiring decisions like this in at
2547 ;; this level ("if it's an x86") instead of a
2548 ;; higher level of abstraction ("if it has such
2549 ;; and such relocation peculiarities (which
2550 ;; happen to be confined to the x86)") is bad.
2551 ;; It would be nice if the code were instead
2552 ;; conditional on some more descriptive
2553 ;; feature, :STICKY-CODE or
2554 ;; :LOAD-GC-INTERACTION or something.
2556 ;; FIXME: The X86 definition of the function
2557 ;; self slot breaks everything object.tex says
2558 ;; about it. (As far as I can tell, the X86
2559 ;; definition makes it a pointer to the actual
2560 ;; code instead of a pointer back to the object
2561 ;; itself.) Ask on the mailing list whether
2562 ;; this is documented somewhere, and if not,
2563 ;; try to reverse engineer some documentation.
2565 ;; a pointer back to the function object, as
2566 ;; described in CMU CL
2567 ;; src/docs/internals/object.tex
2570 ;; KLUDGE: a pointer to the actual code of the
2571 ;; object, as described nowhere that I can find
2573 (make-random-descriptor
2574 (+ (descriptor-bits fn)
2575 (- (ash sb!vm:simple-fun-code-offset
2577 ;; FIXME: We should mask out the type
2578 ;; bits, not assume we know what they
2579 ;; are and subtract them out this way.
2580 sb!vm:fun-pointer-lowtag))))
2581 (write-wordindexed fn sb!vm:simple-fun-next-slot next)
2582 (write-wordindexed fn sb!vm:simple-fun-name-slot name)
2583 (write-wordindexed fn sb!vm:simple-fun-arglist-slot arglist)
2584 (write-wordindexed fn sb!vm:simple-fun-type-slot type)
2585 (write-wordindexed fn sb!vm::simple-fun-info-slot info)
2588 (define-cold-fop (fop-foreign-fixup)
2589 (let* ((kind (pop-stack))
2590 (code-object (pop-stack))
2591 (len (read-byte-arg))
2592 (sym (make-string len)))
2593 (read-string-as-bytes *fasl-input-stream* sym)
2594 (let ((offset (read-word-arg))
2595 (value (cold-foreign-symbol-address sym)))
2596 (do-cold-fixup code-object offset value kind))
2600 (define-cold-fop (fop-foreign-dataref-fixup)
2601 (let* ((kind (pop-stack))
2602 (code-object (pop-stack))
2603 (len (read-byte-arg))
2604 (sym (make-string len)))
2605 (read-string-as-bytes *fasl-input-stream* sym)
2606 (maphash (lambda (k v)
2607 (format *error-output* "~&~S = #X~8X~%" k v))
2608 *cold-foreign-symbol-table*)
2609 (error "shared foreign symbol in cold load: ~S (~S)" sym kind)))
2611 (define-cold-fop (fop-assembler-code)
2612 (let* ((length (read-word-arg))
2614 ;; Note: we round the number of constants up to ensure that
2615 ;; the code vector will be properly aligned.
2616 (round-up sb!vm:code-constants-offset 2))
2617 (des (allocate-cold-descriptor *read-only*
2618 (+ (ash header-n-words
2621 sb!vm:other-pointer-lowtag)))
2623 (make-other-immediate-descriptor
2624 header-n-words sb!vm:code-header-widetag))
2625 (write-wordindexed des
2626 sb!vm:code-code-size-slot
2627 (make-fixnum-descriptor
2628 (ash (+ length (1- (ash 1 sb!vm:word-shift)))
2629 (- sb!vm:word-shift))))
2630 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2631 (write-wordindexed des sb!vm:code-debug-info-slot *nil-descriptor*)
2633 (let* ((start (+ (descriptor-byte-offset des)
2634 (ash header-n-words sb!vm:word-shift)))
2635 (end (+ start length)))
2636 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2642 (define-cold-fop (fop-assembler-routine)
2643 (let* ((routine (pop-stack))
2645 (offset (calc-offset des (read-word-arg))))
2646 (record-cold-assembler-routine
2648 (+ (logandc2 (descriptor-bits des) sb!vm:lowtag-mask) offset))
2651 (define-cold-fop (fop-assembler-fixup)
2652 (let* ((routine (pop-stack))
2654 (code-object (pop-stack))
2655 (offset (read-word-arg)))
2656 (record-cold-assembler-fixup routine code-object offset kind)
2659 (define-cold-fop (fop-code-object-fixup)
2660 (let* ((kind (pop-stack))
2661 (code-object (pop-stack))
2662 (offset (read-word-arg))
2663 (value (descriptor-bits code-object)))
2664 (do-cold-fixup code-object offset value kind)
2667 ;;;; sanity checking space layouts
2669 (defun check-spaces ()
2670 ;;; Co-opt type machinery to check for intersections...
2672 (flet ((check (start end space)
2673 (unless (< start end)
2674 (error "Bogus space: ~A" space))
2675 (let ((type (specifier-type `(integer ,start ,end))))
2676 (dolist (other types)
2677 (unless (eq *empty-type* (type-intersection (cdr other) type))
2678 (error "Space overlap: ~A with ~A" space (car other))))
2679 (push (cons space type) types))))
2680 (check sb!vm:read-only-space-start sb!vm:read-only-space-end :read-only)
2681 (check sb!vm:static-space-start sb!vm:static-space-end :static)
2683 (check sb!vm:dynamic-space-start sb!vm:dynamic-space-end :dynamic)
2686 (check sb!vm:dynamic-0-space-start sb!vm:dynamic-0-space-end :dynamic-0)
2687 (check sb!vm:dynamic-1-space-start sb!vm:dynamic-1-space-end :dynamic-1))
2689 (check sb!vm:linkage-table-space-start sb!vm:linkage-table-space-end :linkage-table))))
2691 ;;;; emitting C header file
2693 (defun tailwise-equal (string tail)
2694 (and (>= (length string) (length tail))
2695 (string= string tail :start1 (- (length string) (length tail)))))
2697 (defun write-boilerplate ()
2700 '("This is a machine-generated file. Please do not edit it by hand."
2701 "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
2703 "This file contains low-level information about the"
2704 "internals of a particular version and configuration"
2705 "of SBCL. It is used by the C compiler to create a runtime"
2706 "support environment, an executable program in the host"
2707 "operating system's native format, which can then be used to"
2708 "load and run 'core' files, which are basically programs"
2709 "in SBCL's own format."))
2710 (format t " *~@[ ~A~]~%" line))
2713 (defun c-name (string &optional strip)
2715 (substitute-if #\_ (lambda (c) (member c '(#\- #\/ #\%)))
2716 (remove-if (lambda (c) (position c strip))
2719 (defun c-symbol-name (symbol &optional strip)
2720 (c-name (symbol-name symbol) strip))
2722 (defun write-makefile-features ()
2723 ;; propagating *SHEBANG-FEATURES* into the Makefiles
2724 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2725 sb-cold:*shebang-features*)
2727 (format t "LISP_FEATURE_~A=1~%" shebang-feature-name)))
2729 (defun write-config-h ()
2730 ;; propagating *SHEBANG-FEATURES* into C-level #define's
2731 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2732 sb-cold:*shebang-features*)
2734 (format t "#define LISP_FEATURE_~A~%" shebang-feature-name))
2736 ;; and miscellaneous constants
2737 (format t "#define SBCL_CORE_VERSION_INTEGER ~D~%" sbcl-core-version-integer)
2739 "#define SBCL_VERSION_STRING ~S~%"
2740 (sb!xc:lisp-implementation-version))
2741 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
2742 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2743 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
2744 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2745 (format t "#define LISPOBJ(thing) thing~2%")
2746 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")
2749 (defun write-constants-h ()
2750 ;; writing entire families of named constants
2751 (let ((constants nil))
2752 (dolist (package-name '( ;; Even in CMU CL, constants from VM
2753 ;; were automatically propagated
2754 ;; into the runtime.
2756 ;; In SBCL, we also propagate various
2757 ;; magic numbers related to file format,
2758 ;; which live here instead of SB!VM.
2760 (do-external-symbols (symbol (find-package package-name))
2761 (when (constantp symbol)
2762 (let ((name (symbol-name symbol)))
2763 (labels ( ;; shared machinery
2764 (record (string priority suffix)
2767 (symbol-value symbol)
2769 (documentation symbol 'variable))
2771 ;; machinery for old-style CMU CL Lisp-to-C
2772 ;; arbitrary renaming, being phased out in favor of
2773 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
2775 (record-with-munged-name (prefix string priority)
2776 (record (concatenate
2779 (delete #\- (string-capitalize string)))
2782 (maybe-record-with-munged-name (tail prefix priority)
2783 (when (tailwise-equal name tail)
2784 (record-with-munged-name prefix
2789 ;; machinery for new-style SBCL Lisp-to-C naming
2790 (record-with-translated-name (priority large)
2791 (record (c-name name) priority (if large "LU" "")))
2792 (maybe-record-with-translated-name (suffixes priority &key large)
2793 (when (some (lambda (suffix)
2794 (tailwise-equal name suffix))
2796 (record-with-translated-name priority large))))
2797 (maybe-record-with-translated-name '("-LOWTAG") 0)
2798 (maybe-record-with-translated-name '("-WIDETAG" "-SHIFT") 1)
2799 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
2800 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
2801 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
2802 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
2803 (maybe-record-with-translated-name '("-SIZE") 6)
2804 (maybe-record-with-translated-name '("-START" "-END" "-PAGE-BYTES"
2805 "-CARD-BYTES" "-GRANULARITY")
2807 (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 8)
2808 (maybe-record-with-translated-name '("-CORE-SPACE-ID") 9)
2809 (maybe-record-with-translated-name '("-CORE-SPACE-ID-FLAG") 9)
2810 (maybe-record-with-translated-name '("-GENERATION+") 10))))))
2811 ;; KLUDGE: these constants are sort of important, but there's no
2812 ;; pleasing way to inform the code above about them. So we fake
2813 ;; it for now. nikodemus on #lisp (2004-08-09) suggested simply
2814 ;; exporting every numeric constant from SB!VM; that would work,
2815 ;; but the C runtime would have to be altered to use Lisp-like names
2816 ;; rather than the munged names currently exported. --njf, 2004-08-09
2817 (dolist (c '(sb!vm:n-word-bits sb!vm:n-word-bytes
2818 sb!vm:n-lowtag-bits sb!vm:lowtag-mask
2819 sb!vm:n-widetag-bits sb!vm:widetag-mask
2820 sb!vm:n-fixnum-tag-bits sb!vm:fixnum-tag-mask))
2821 (push (list (c-symbol-name c)
2822 -1 ; invent a new priority
2827 ;; One more symbol that doesn't fit into the code above.
2828 (let ((c 'sb!impl::+magic-hash-vector-value+))
2829 (push (list (c-symbol-name c)
2837 (lambda (const1 const2)
2838 (if (= (second const1) (second const2))
2839 (if (= (third const1) (third const2))
2840 (string< (first const1) (first const2))
2841 (< (third const1) (third const2)))
2842 (< (second const1) (second const2))))))
2843 (let ((prev-priority (second (car constants))))
2844 (dolist (const constants)
2845 (destructuring-bind (name priority value suffix doc) const
2846 (unless (= prev-priority priority)
2848 (setf prev-priority priority))
2849 (when (minusp value)
2850 (error "stub: negative values unsupported"))
2851 (format t "#define ~A ~A~A /* 0x~X ~@[ -- ~A ~]*/~%" name value suffix value doc))))
2854 ;; writing information about internal errors
2855 (let ((internal-errors sb!c:*backend-internal-errors*))
2856 (dotimes (i (length internal-errors))
2857 (let ((current-error (aref internal-errors i)))
2858 ;; FIXME: this UNLESS should go away (see also FIXME in
2859 ;; interr.lisp) -- APD, 2002-03-05
2860 (unless (eq nil (car current-error))
2861 (format t "#define ~A ~D~%"
2862 (c-symbol-name (car current-error))
2864 (format t "#define INTERNAL_ERROR_NAMES \\~%~{~S~#[~:;, \\~%~]~}~%"
2865 (map 'list #'cdr internal-errors)))
2868 ;; I'm not really sure why this is in SB!C, since it seems
2869 ;; conceptually like something that belongs to SB!VM. In any case,
2870 ;; it's needed C-side.
2871 (format t "#define BACKEND_PAGE_BYTES ~DLU~%" sb!c:*backend-page-bytes*)
2875 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
2876 ;; platforms. If we export this from the SB!VM package, it gets
2877 ;; written out as #define trap_PseudoAtomic, which is confusing as
2878 ;; the runtime treats trap_ as the prefix for illegal instruction
2879 ;; type things. We therefore don't export it, but instead do
2881 (when (boundp 'sb!vm::pseudo-atomic-trap)
2883 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
2884 sb!vm::pseudo-atomic-trap)
2886 ;; possibly this is another candidate for a rename (to
2887 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
2888 ;; [possibly applicable to other platforms])
2890 (dolist (symbol '(sb!vm::float-traps-byte
2891 sb!vm::float-exceptions-byte
2892 sb!vm::float-sticky-bits
2893 sb!vm::float-rounding-mode))
2894 (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
2895 (c-symbol-name symbol)
2896 (sb!xc:byte-position (symbol-value symbol)))
2897 (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
2898 (c-symbol-name symbol)
2899 (sb!xc:mask-field (symbol-value symbol) -1))))
2902 (defun write-tagnames-h (&optional (out *standard-output*))
2904 ((pretty-name (symbol strip)
2905 (let ((name (string-downcase symbol)))
2906 (substitute #\Space #\-
2907 (subseq name 0 (- (length name) (length strip))))))
2908 (list-sorted-tags (tail)
2909 (loop for symbol being the external-symbols of "SB!VM"
2910 when (and (constantp symbol)
2911 (tailwise-equal (string symbol) tail))
2912 collect symbol into tags
2913 finally (return (sort tags #'< :key #'symbol-value))))
2914 (write-tags (kind limit ash-count)
2915 (format out "~%static const char *~(~A~)_names[] = {~%"
2917 (let ((tags (list-sorted-tags kind)))
2919 (if (eql i (ash (or (symbol-value (first tags)) -1) ash-count))
2920 (format out " \"~A\"" (pretty-name (pop tags) kind))
2921 (format out " \"unknown [~D]\"" i))
2922 (unless (eql i (1- limit))
2923 (write-string "," out))
2925 (write-line "};" out)))
2926 (write-tags "-LOWTAG" sb!vm:lowtag-limit 0)
2927 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
2928 ;; ending with the same 2 bits. (#b10)
2929 (write-tags "-WIDETAG" (ash (1+ sb!vm:widetag-mask) -2) -2))
2932 (defun write-primitive-object (obj)
2933 ;; writing primitive object layouts
2934 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2937 (c-name (string-downcase (string (sb!vm:primitive-object-name obj)))))
2938 (when (sb!vm:primitive-object-widetag obj)
2939 (format t " lispobj header;~%"))
2940 (dolist (slot (sb!vm:primitive-object-slots obj))
2941 (format t " ~A ~A~@[[1]~];~%"
2942 (getf (sb!vm:slot-options slot) :c-type "lispobj")
2943 (c-name (string-downcase (string (sb!vm:slot-name slot))))
2944 (sb!vm:slot-rest-p slot)))
2946 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2947 (format t "/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
2948 (format t " * so they work directly on tagged addresses. */~2%")
2949 (let ((name (sb!vm:primitive-object-name obj))
2950 (lowtag (eval (sb!vm:primitive-object-lowtag obj))))
2952 (dolist (slot (sb!vm:primitive-object-slots obj))
2953 (format t "#define ~A_~A_OFFSET ~D~%"
2954 (c-symbol-name name)
2955 (c-symbol-name (sb!vm:slot-name slot))
2956 (- (* (sb!vm:slot-offset slot) sb!vm:n-word-bytes) lowtag)))
2958 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%"))
2960 (defun write-structure-object (dd)
2961 (flet ((cstring (designator)
2962 (c-name (string-downcase (string designator)))))
2963 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2964 (format t "struct ~A {~%" (cstring (dd-name dd)))
2965 (format t " lispobj header;~%")
2966 (format t " lispobj layout;~%")
2967 (dolist (slot (dd-slots dd))
2968 (when (eq t (dsd-raw-type slot))
2969 (format t " lispobj ~A;~%" (cstring (dsd-name slot)))))
2970 (unless (oddp (+ (dd-length dd) (dd-raw-length dd)))
2971 (format t " lispobj raw_slot_padding;~%"))
2972 (dotimes (n (dd-raw-length dd))
2973 (format t " lispobj raw~D;~%" (- (dd-raw-length dd) n 1)))
2975 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")))
2977 (defun write-static-symbols ()
2978 (dolist (symbol (cons nil sb!vm:*static-symbols*))
2979 ;; FIXME: It would be nice to use longer names than NIL and
2980 ;; (particularly) T in #define statements.
2981 (format t "#define ~A LISPOBJ(0x~X)~%"
2982 ;; FIXME: It would be nice not to need to strip anything
2983 ;; that doesn't get stripped always by C-SYMBOL-NAME.
2984 (c-symbol-name symbol "%*.!")
2985 (if *static* ; if we ran GENESIS
2986 ;; We actually ran GENESIS, use the real value.
2987 (descriptor-bits (cold-intern symbol))
2988 ;; We didn't run GENESIS, so guess at the address.
2989 (+ sb!vm:static-space-start
2991 sb!vm:other-pointer-lowtag
2992 (if symbol (sb!vm:static-symbol-offset symbol) 0))))))
2995 ;;;; writing map file
2997 ;;; Write a map file describing the cold load. Some of this
2998 ;;; information is subject to change due to relocating GC, but even so
2999 ;;; it can be very handy when attempting to troubleshoot the early
3000 ;;; stages of cold load.
3002 (let ((*print-pretty* nil)
3003 (*print-case* :upcase))
3004 (format t "assembler routines defined in core image:~2%")
3005 (dolist (routine (sort (copy-list *cold-assembler-routines*) #'<
3007 (format t "#X~8,'0X: ~S~%" (cdr routine) (car routine)))
3010 (maphash (lambda (name fdefn)
3011 (let ((fun (read-wordindexed fdefn
3012 sb!vm:fdefn-fun-slot)))
3013 (if (= (descriptor-bits fun)
3014 (descriptor-bits *nil-descriptor*))
3016 (let ((addr (read-wordindexed
3017 fdefn sb!vm:fdefn-raw-addr-slot)))
3018 (push (cons name (descriptor-bits addr))
3020 *cold-fdefn-objects*)
3021 (format t "~%~|~%initially defined functions:~2%")
3022 (setf funs (sort funs #'< :key #'cdr))
3024 (format t "0x~8,'0X: ~S #X~8,'0X~%" (cdr info) (car info)
3025 (- (cdr info) #x17)))
3028 (a note about initially undefined function references: These functions
3029 are referred to by code which is installed by GENESIS, but they are not
3030 installed by GENESIS. This is not necessarily a problem; functions can
3031 be defined later, by cold init toplevel forms, or in files compiled and
3032 loaded at warm init, or elsewhere. As long as they are defined before
3033 they are called, everything should be OK. Things are also OK if the
3034 cross-compiler knew their inline definition and used that everywhere
3035 that they were called before the out-of-line definition is installed,
3036 as is fairly common for structure accessors.)
3037 initially undefined function references:~2%")
3039 (setf undefs (sort undefs #'string< :key #'fun-name-block-name))
3040 (dolist (name undefs)
3041 (format t "~S~%" name)))
3043 (format t "~%~|~%layout names:~2%")
3045 (maphash (lambda (name gorp)
3046 (declare (ignore name))
3047 (stuff (cons (descriptor-bits (car gorp))
3050 (dolist (x (sort (stuff) #'< :key #'car))
3051 (apply #'format t "~8,'0X: ~S[~D]~%~10T~S~%" x))))
3055 ;;;; writing core file
3057 (defvar *core-file*)
3058 (defvar *data-page*)
3060 ;;; magic numbers to identify entries in a core file
3062 ;;; (In case you were wondering: No, AFAIK there's no special magic about
3063 ;;; these which requires them to be in the 38xx range. They're just
3064 ;;; arbitrary words, tested not for being in a particular range but just
3065 ;;; for equality. However, if you ever need to look at a .core file and
3066 ;;; figure out what's going on, it's slightly convenient that they're
3067 ;;; all in an easily recognizable range, and displacing the range away from
3068 ;;; zero seems likely to reduce the chance that random garbage will be
3069 ;;; misinterpreted as a .core file.)
3070 (defconstant version-core-entry-type-code 3860)
3071 (defconstant build-id-core-entry-type-code 3899)
3072 (defconstant new-directory-core-entry-type-code 3861)
3073 (defconstant initial-fun-core-entry-type-code 3863)
3074 (defconstant page-table-core-entry-type-code 3880)
3075 #!+(and sb-lutex sb-thread)
3076 (defconstant lutex-table-core-entry-type-code 3887)
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))))))