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 (char-code #\S) 24)
38 (ash (char-code #\B) 16)
39 (ash (char-code #\C) 8)
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.
52 ;;; 0: inherited from CMU CL
53 ;;; 1: rearranged static symbols for sbcl-0.6.8
54 ;;; 2: eliminated non-ANSI %DEFCONSTANT/%%DEFCONSTANT support,
55 ;;; deleted a slot from DEBUG-SOURCE structure
56 (defconstant sbcl-core-version-integer 2)
58 (defun round-up (number size)
60 "Round NUMBER up to be an integral multiple of SIZE."
61 (* size (ceiling number size)))
63 ;;;; representation of spaces in the core
65 ;;; If there is more than one dynamic space in memory (i.e., if a
66 ;;; copying GC is in use), then only the active dynamic space gets
69 (defconstant dynamic-space-id 1)
72 (defconstant static-space-id 2)
75 (defconstant read-only-space-id 3)
77 (defconstant descriptor-low-bits 16
78 "the number of bits in the low half of the descriptor")
79 (defconstant target-space-alignment (ash 1 descriptor-low-bits)
80 "the alignment requirement for spaces in the target.
81 Must be at least (ASH 1 DESCRIPTOR-LOW-BITS)")
83 ;;; a GENESIS-time representation of a memory space (e.g. read-only
84 ;;; space, dynamic space, or static space)
85 (defstruct (gspace (:constructor %make-gspace)
87 ;; name and identifier for this GSPACE
88 (name (missing-arg) :type symbol :read-only t)
89 (identifier (missing-arg) :type fixnum :read-only t)
90 ;; the word address where the data will be loaded
91 (word-address (missing-arg) :type unsigned-byte :read-only t)
92 ;; the data themselves. (Note that in CMU CL this was a pair
93 ;; of fields SAP and WORDS-ALLOCATED, but that wasn't very portable.)
94 (bytes (make-array target-space-alignment :element-type '(unsigned-byte 8))
95 :type (simple-array (unsigned-byte 8) 1))
96 ;; the index of the next unwritten word (i.e. chunk of
97 ;; SB!VM:N-WORD-BYTES bytes) in BYTES, or equivalently the number of
98 ;; words actually written in BYTES. In order to convert to an actual
99 ;; index into BYTES, thus must be multiplied by SB!VM:N-WORD-BYTES.
102 (defun gspace-byte-address (gspace)
103 (ash (gspace-word-address gspace) sb!vm:word-shift))
105 (def!method print-object ((gspace gspace) stream)
106 (print-unreadable-object (gspace stream :type t)
107 (format stream "~S" (gspace-name gspace))))
109 (defun make-gspace (name identifier byte-address)
110 (unless (zerop (rem byte-address target-space-alignment))
111 (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
113 target-space-alignment))
114 (%make-gspace :name name
115 :identifier identifier
116 :word-address (ash byte-address (- sb!vm:word-shift))))
118 ;;; KLUDGE: Doing it this way seems to partly replicate the
119 ;;; functionality of Common Lisp adjustable arrays. Is there any way
120 ;;; to do this stuff in one line of code by using standard Common Lisp
121 ;;; stuff? -- WHN 19990816
122 (defun expand-gspace-bytes (gspace)
123 (let* ((old-bytes (gspace-bytes gspace))
124 (old-length (length old-bytes))
125 (new-length (* 2 old-length))
126 (new-bytes (make-array new-length :element-type '(unsigned-byte 8))))
127 (replace new-bytes old-bytes :end1 old-length)
128 (setf (gspace-bytes gspace)
132 ;;;; representation of descriptors
134 (defstruct (descriptor
135 (:constructor make-descriptor
136 (high low &optional gspace word-offset))
138 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
139 (gspace nil :type (or gspace null))
140 ;; the offset in words from the start of GSPACE, or NIL if not set yet
141 (word-offset nil :type (or (unsigned-byte #.sb!vm:n-word-bits) null))
142 ;; the high and low halves of the descriptor
144 ;; KLUDGE: Judging from the comments in genesis.lisp of the CMU CL
145 ;; old-rt compiler, this split dates back from a very early version
146 ;; of genesis where 32-bit integers were represented as conses of
147 ;; two 16-bit integers. In any system with nice (UNSIGNED-BYTE 32)
148 ;; structure slots, like CMU CL >= 17 or any version of SBCL, there
149 ;; seems to be no reason to persist in this. -- WHN 19990917
152 (def!method print-object ((des descriptor) stream)
153 (let ((lowtag (descriptor-lowtag des)))
154 (print-unreadable-object (des stream :type t)
155 (cond ((or (= lowtag sb!vm:even-fixnum-lowtag)
156 (= lowtag sb!vm:odd-fixnum-lowtag))
157 (let ((unsigned (logior (ash (descriptor-high des)
158 (1+ (- descriptor-low-bits
159 sb!vm:n-lowtag-bits)))
160 (ash (descriptor-low des)
161 (- 1 sb!vm:n-lowtag-bits)))))
164 (if (> unsigned #x1FFFFFFF)
165 (- unsigned #x40000000)
167 ((or (= lowtag sb!vm:other-immediate-0-lowtag)
168 (= lowtag sb!vm:other-immediate-1-lowtag))
170 "for other immediate: #X~X, type #b~8,'0B"
171 (ash (descriptor-bits des) (- sb!vm:n-widetag-bits))
172 (logand (descriptor-low des) sb!vm:widetag-mask)))
175 "for pointer: #X~X, lowtag #b~3,'0B, ~A"
176 (logior (ash (descriptor-high des) descriptor-low-bits)
177 (logandc2 (descriptor-low des) sb!vm:lowtag-mask))
179 (let ((gspace (descriptor-gspace des)))
184 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
185 ;;; free word index is boosted as necessary, and if additional memory
186 ;;; is needed, we grow the GSPACE. The descriptor returned is a
187 ;;; pointer of type LOWTAG.
188 (defun allocate-cold-descriptor (gspace length lowtag)
189 (let* ((bytes (round-up length (ash 1 sb!vm:n-lowtag-bits)))
190 (old-free-word-index (gspace-free-word-index gspace))
191 (new-free-word-index (+ old-free-word-index
192 (ash bytes (- sb!vm:word-shift)))))
193 ;; Grow GSPACE as necessary until it's big enough to handle
194 ;; NEW-FREE-WORD-INDEX.
196 ((>= (length (gspace-bytes gspace))
197 (* new-free-word-index sb!vm:n-word-bytes)))
198 (expand-gspace-bytes gspace))
199 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
200 (setf (gspace-free-word-index gspace) new-free-word-index)
201 (let ((ptr (+ (gspace-word-address gspace) old-free-word-index)))
202 (make-descriptor (ash ptr (- sb!vm:word-shift descriptor-low-bits))
203 (logior (ash (logand ptr
205 (- descriptor-low-bits
210 old-free-word-index))))
212 (defun descriptor-lowtag (des)
214 "the lowtag bits for DES"
215 (logand (descriptor-low des) sb!vm:lowtag-mask))
217 (defun descriptor-bits (des)
218 (logior (ash (descriptor-high des) descriptor-low-bits)
219 (descriptor-low des)))
221 (defun descriptor-fixnum (des)
222 (let ((bits (descriptor-bits des)))
223 (if (logbitp (1- sb!vm:n-word-bits) bits)
224 ;; KLUDGE: The (- SB!VM:N-WORD-BITS 2) term here looks right to
225 ;; me, and it works, but in CMU CL it was (1- SB!VM:N-WORD-BITS),
226 ;; and although that doesn't make sense for me, or work for me,
227 ;; it's hard to see how it could have been wrong, since CMU CL
228 ;; genesis worked. It would be nice to understand how this came
229 ;; to be.. -- WHN 19990901
230 (logior (ash bits -2) (ash -1 (- sb!vm:n-word-bits 2)))
234 (defun descriptor-bytes (des)
235 (gspace-bytes (descriptor-intuit-gspace des)))
236 (defun descriptor-byte-offset (des)
237 (ash (descriptor-word-offset des) sb!vm:word-shift))
239 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
240 ;;; figure out a GSPACE which corresponds to DES, set it into
241 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
242 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
243 (declaim (ftype (function (descriptor) gspace) descriptor-intuit-gspace))
244 (defun descriptor-intuit-gspace (des)
245 (if (descriptor-gspace des)
246 (descriptor-gspace des)
247 ;; KLUDGE: It's not completely clear to me what's going on here;
248 ;; this is a literal translation from of some rather mysterious
249 ;; code from CMU CL's DESCRIPTOR-SAP function. Some explanation
250 ;; would be nice. -- WHN 19990817
251 (let ((lowtag (descriptor-lowtag des))
252 (high (descriptor-high des))
253 (low (descriptor-low des)))
254 (if (or (eql lowtag sb!vm:fun-pointer-lowtag)
255 (eql lowtag sb!vm:instance-pointer-lowtag)
256 (eql lowtag sb!vm:list-pointer-lowtag)
257 (eql lowtag sb!vm:other-pointer-lowtag))
258 (dolist (gspace (list *dynamic* *static* *read-only*)
259 (error "couldn't find a GSPACE for ~S" des))
260 ;; This code relies on the fact that GSPACEs are aligned
261 ;; such that the descriptor-low-bits low bits are zero.
262 (when (and (>= high (ash (gspace-word-address gspace)
263 (- sb!vm:word-shift descriptor-low-bits)))
264 (<= high (ash (+ (gspace-word-address gspace)
265 (gspace-free-word-index gspace))
266 (- sb!vm:word-shift descriptor-low-bits))))
267 (setf (descriptor-gspace des) gspace)
268 (setf (descriptor-word-offset des)
269 (+ (ash (- high (ash (gspace-word-address gspace)
271 descriptor-low-bits)))
272 (- descriptor-low-bits sb!vm:word-shift))
273 (ash (logandc2 low sb!vm:lowtag-mask)
274 (- sb!vm:word-shift))))
276 (error "don't even know how to look for a GSPACE for ~S" des)))))
278 (defun make-random-descriptor (value)
279 (make-descriptor (logand (ash value (- descriptor-low-bits))
282 descriptor-low-bits))))
283 (logand value (1- (ash 1 descriptor-low-bits)))))
285 (defun make-fixnum-descriptor (num)
286 (when (>= (integer-length num)
287 (1+ (- sb!vm:n-word-bits sb!vm:n-lowtag-bits)))
288 (error "~W is too big for a fixnum." num))
289 (make-random-descriptor (ash num (1- sb!vm:n-lowtag-bits))))
291 (defun make-other-immediate-descriptor (data type)
292 (make-descriptor (ash data (- sb!vm:n-widetag-bits descriptor-low-bits))
293 (logior (logand (ash data (- descriptor-low-bits
294 sb!vm:n-widetag-bits))
295 (1- (ash 1 descriptor-low-bits)))
298 (defun make-character-descriptor (data)
299 (make-other-immediate-descriptor data sb!vm:base-char-widetag))
301 (defun descriptor-beyond (des offset type)
302 (let* ((low (logior (+ (logandc2 (descriptor-low des) sb!vm:lowtag-mask)
305 (high (+ (descriptor-high des)
306 (ash low (- descriptor-low-bits)))))
307 (make-descriptor high (logand low (1- (ash 1 descriptor-low-bits))))))
309 ;;;; miscellaneous variables and other noise
311 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
312 ;;; undefined foreign symbols are to be treated as an error.
313 ;;; (In the first pass of GENESIS, needed to create a header file before
314 ;;; the C runtime can be built, various foreign symbols will necessarily
315 ;;; be undefined, but we don't need actual values for them anyway, and
316 ;;; we can just use 0 or some other placeholder. In the second pass of
317 ;;; GENESIS, all foreign symbols should be defined, so any undefined
318 ;;; foreign symbol is a problem.)
320 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
321 ;;; never tries to look up foreign symbols in the first place unless
322 ;;; it's actually creating a core file (as in the second pass) instead
323 ;;; of using this hack to allow it to go through the motions without
324 ;;; causing an error. -- WHN 20000825
325 (defvar *foreign-symbol-placeholder-value*)
327 ;;; a handle on the trap object
328 (defvar *unbound-marker*)
329 ;; was: (make-other-immediate-descriptor 0 sb!vm:unbound-marker-widetag)
331 ;;; a handle on the NIL object
332 (defvar *nil-descriptor*)
334 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
335 ;;; when the target Lisp starts up
337 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
338 ;;; loadtime value, represented by (CONS KEYWORD ..). The FILENAME
339 ;;; tells which fasl file each list element came from, for debugging
341 (defvar *current-reversed-cold-toplevels*)
343 ;;; the name of the object file currently being cold loaded (as a string, not a
344 ;;; pathname), or NIL if we're not currently cold loading any object file
345 (defvar *cold-load-filename* nil)
346 (declaim (type (or string null) *cold-load-filename*))
348 ;;;; miscellaneous stuff to read and write the core memory
350 ;;; FIXME: should be DEFINE-MODIFY-MACRO
351 (defmacro cold-push (thing list)
353 "Push THING onto the given cold-load LIST."
354 `(setq ,list (cold-cons ,thing ,list)))
356 ;;; BYTE-VECTOR-REF-32 and friends. These are like SAP-REF-n, except
357 ;;; that instead of a SAP we use a byte vector
358 (macrolet ((make-byte-vector-ref-n
360 (let* ((name (intern (format nil "BYTE-VECTOR-REF-~A" n)))
361 (number-octets (/ n 8))
363 (loop for i from 0 to (1- number-octets)
364 collect `(ash (aref byte-vector (+ byte-index ,i))
367 (loop for i from 0 to (1- number-octets)
368 collect `(ash (aref byte-vector
370 ,(- number-octets 1 i)))
373 (loop for i from 0 to (1- number-octets)
375 `((aref byte-vector (+ byte-index ,i))
376 (ldb (byte 8 ,(* i 8)) new-value))))
378 (loop for i from 0 to (1- number-octets)
380 `((aref byte-vector (+ byte-index ,i))
381 (ldb (byte 8 ,(- n 8 (* i 8))) new-value)))))
383 (defun ,name (byte-vector byte-index)
384 (aver (= sb!vm:n-word-bits 32))
385 (aver (= sb!vm:n-byte-bits 8))
386 (logior ,@(ecase sb!c:*backend-byte-order*
387 (:little-endian ash-list-le)
388 (:big-endian ash-list-be))))
389 (defun (setf ,name) (new-value byte-vector byte-index)
390 (aver (= sb!vm:n-word-bits 32))
391 (aver (= sb!vm:n-byte-bits 8))
392 (setf ,@(ecase sb!c:*backend-byte-order*
393 (:little-endian setf-list-le)
394 (:big-endian setf-list-be))))))))
395 (make-byte-vector-ref-n 8)
396 (make-byte-vector-ref-n 16)
397 (make-byte-vector-ref-n 32))
399 (declaim (ftype (function (descriptor sb!vm:word) descriptor) read-wordindexed))
400 (defun read-wordindexed (address index)
402 "Return the value which is displaced by INDEX words from ADDRESS."
403 (let* ((gspace (descriptor-intuit-gspace address))
404 (bytes (gspace-bytes gspace))
405 (byte-index (ash (+ index (descriptor-word-offset address))
407 (value (byte-vector-ref-32 bytes byte-index)))
408 (make-random-descriptor value)))
410 (declaim (ftype (function (descriptor) descriptor) read-memory))
411 (defun read-memory (address)
413 "Return the value at ADDRESS."
414 (read-wordindexed address 0))
416 ;;; (Note: In CMU CL, this function expected a SAP-typed ADDRESS
417 ;;; value, instead of the SAP-INT we use here.)
418 (declaim (ftype (function (sb!vm:word descriptor) (values))
419 note-load-time-value-reference))
420 (defun note-load-time-value-reference (address marker)
421 (cold-push (cold-cons
422 (cold-intern :load-time-value-fixup)
423 (cold-cons (sap-int-to-core address)
425 (number-to-core (descriptor-word-offset marker))
427 *current-reversed-cold-toplevels*)
430 (declaim (ftype (function (descriptor sb!vm:word descriptor)) write-wordindexed))
431 (defun write-wordindexed (address index value)
433 "Write VALUE displaced INDEX words from ADDRESS."
434 ;; KLUDGE: There is an algorithm (used in DESCRIPTOR-INTUIT-GSPACE)
435 ;; for calculating the value of the GSPACE slot from scratch. It
436 ;; doesn't work for all values, only some of them, but mightn't it
437 ;; be reasonable to see whether it works on VALUE before we give up
438 ;; because (DESCRIPTOR-GSPACE VALUE) isn't set? (Or failing that,
439 ;; perhaps write a comment somewhere explaining why it's not a good
440 ;; idea?) -- WHN 19990817
441 (if (and (null (descriptor-gspace value))
442 (not (null (descriptor-word-offset value))))
443 (note-load-time-value-reference (+ (logandc2 (descriptor-bits address)
445 (ash index sb!vm:word-shift))
447 (let* ((bytes (gspace-bytes (descriptor-intuit-gspace address)))
448 (byte-index (ash (+ index (descriptor-word-offset address))
450 (setf (byte-vector-ref-32 bytes byte-index)
451 (descriptor-bits value)))))
453 (declaim (ftype (function (descriptor descriptor)) write-memory))
454 (defun write-memory (address value)
456 "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
457 (write-wordindexed address 0 value))
459 ;;;; allocating images of primitive objects in the cold core
461 ;;; There are three kinds of blocks of memory in the type system:
462 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
463 ;;; header as all slots are descriptors.
464 ;;; * Unboxed objects (bignums): There is a single header word that contains
466 ;;; * Vector objects: There is a header word with the type, then a word for
467 ;;; the length, then the data.
468 (defun allocate-boxed-object (gspace length lowtag)
470 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
472 (allocate-cold-descriptor gspace (ash length sb!vm:word-shift) lowtag))
473 (defun allocate-unboxed-object (gspace element-bits length type)
475 "Allocate LENGTH units of ELEMENT-BITS bits plus a header word in GSPACE and
476 return an ``other-pointer'' descriptor to them. Initialize the header word
477 with the resultant length and TYPE."
478 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
479 (des (allocate-cold-descriptor gspace
480 (+ bytes sb!vm:n-word-bytes)
481 sb!vm:other-pointer-lowtag)))
483 (make-other-immediate-descriptor (ash bytes
484 (- sb!vm:word-shift))
487 (defun allocate-vector-object (gspace element-bits length type)
489 "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
490 GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
491 header word with TYPE and the length slot with LENGTH."
492 ;; FIXME: Here and in ALLOCATE-UNBOXED-OBJECT, BYTES is calculated using
493 ;; #'/ instead of #'CEILING, which seems wrong.
494 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
495 (des (allocate-cold-descriptor gspace
496 (+ bytes (* 2 sb!vm:n-word-bytes))
497 sb!vm:other-pointer-lowtag)))
498 (write-memory des (make-other-immediate-descriptor 0 type))
499 (write-wordindexed des
500 sb!vm:vector-length-slot
501 (make-fixnum-descriptor length))
504 ;;;; copying simple objects into the cold core
506 (defun string-to-core (string &optional (gspace *dynamic*))
508 "Copy string into the cold core and return a descriptor to it."
509 ;; (Remember that the system convention for storage of strings leaves an
510 ;; extra null byte at the end to aid in call-out to C.)
511 (let* ((length (length string))
512 (des (allocate-vector-object gspace
515 sb!vm:simple-string-widetag))
516 (bytes (gspace-bytes gspace))
517 (offset (+ (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
518 (descriptor-byte-offset des))))
519 (write-wordindexed des
520 sb!vm:vector-length-slot
521 (make-fixnum-descriptor length))
523 (setf (aref bytes (+ offset i))
524 ;; KLUDGE: There's no guarantee that the character
525 ;; encoding here will be the same as the character
526 ;; encoding on the target machine, so using CHAR-CODE as
527 ;; we do, or a bitwise copy as CMU CL code did, is sleazy.
528 ;; (To make this more portable, perhaps we could use
529 ;; indices into the sequence which is used to test whether
530 ;; a character is a STANDARD-CHAR?) -- WHN 19990817
531 (char-code (aref string i))))
532 (setf (aref bytes (+ offset length))
533 0) ; null string-termination character for C
536 (defun bignum-to-core (n)
538 "Copy a bignum to the cold core."
539 (let* ((words (ceiling (1+ (integer-length n)) sb!vm:n-word-bits))
540 (handle (allocate-unboxed-object *dynamic*
543 sb!vm:bignum-widetag)))
544 (declare (fixnum words))
545 (do ((index 1 (1+ index))
546 (remainder n (ash remainder (- sb!vm:n-word-bits))))
548 (unless (zerop (integer-length remainder))
549 ;; FIXME: Shouldn't this be a fatal error?
550 (warn "~W words of ~W were written, but ~W bits were left over."
552 (let ((word (ldb (byte sb!vm:n-word-bits 0) remainder)))
553 (write-wordindexed handle index
554 (make-descriptor (ash word (- descriptor-low-bits))
555 (ldb (byte descriptor-low-bits 0)
559 (defun number-pair-to-core (first second type)
561 "Makes a number pair of TYPE (ratio or complex) and fills it in."
562 (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits 2 type)))
563 (write-wordindexed des 1 first)
564 (write-wordindexed des 2 second)
567 (defun float-to-core (x)
570 (let ((des (allocate-unboxed-object *dynamic*
572 (1- sb!vm:single-float-size)
573 sb!vm:single-float-widetag)))
574 (write-wordindexed des
575 sb!vm:single-float-value-slot
576 (make-random-descriptor (single-float-bits x)))
579 (let ((des (allocate-unboxed-object *dynamic*
581 (1- sb!vm:double-float-size)
582 sb!vm:double-float-widetag))
583 (high-bits (make-random-descriptor (double-float-high-bits x)))
584 (low-bits (make-random-descriptor (double-float-low-bits x))))
585 (ecase sb!c:*backend-byte-order*
587 (write-wordindexed des sb!vm:double-float-value-slot low-bits)
588 (write-wordindexed des (1+ sb!vm:double-float-value-slot) high-bits))
590 (write-wordindexed des sb!vm:double-float-value-slot high-bits)
591 (write-wordindexed des (1+ sb!vm:double-float-value-slot) low-bits)))
593 #!+(and long-float x86)
595 (let ((des (allocate-unboxed-object *dynamic*
597 (1- sb!vm:long-float-size)
598 sb!vm:long-float-widetag))
599 (exp-bits (make-random-descriptor (long-float-exp-bits x)))
600 (high-bits (make-random-descriptor (long-float-high-bits x)))
601 (low-bits (make-random-descriptor (long-float-low-bits x))))
602 (ecase sb!c:*backend-byte-order*
604 (write-wordindexed des sb!vm:long-float-value-slot low-bits)
605 (write-wordindexed des (1+ sb!vm:long-float-value-slot) high-bits)
606 (write-wordindexed des (+ 2 sb!vm:long-float-value-slot) exp-bits))
608 (error "LONG-FLOAT is not supported for big-endian byte order.")))
611 (defun complex-single-float-to-core (num)
612 (declare (type (complex single-float) num))
613 (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
614 (1- sb!vm:complex-single-float-size)
615 sb!vm:complex-single-float-widetag)))
616 (write-wordindexed des sb!vm:complex-single-float-real-slot
617 (make-random-descriptor (single-float-bits (realpart num))))
618 (write-wordindexed des sb!vm:complex-single-float-imag-slot
619 (make-random-descriptor (single-float-bits (imagpart num))))
622 (defun complex-double-float-to-core (num)
623 (declare (type (complex double-float) num))
624 (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
625 (1- sb!vm:complex-double-float-size)
626 sb!vm:complex-double-float-widetag)))
627 (let* ((real (realpart num))
628 (high-bits (make-random-descriptor (double-float-high-bits real)))
629 (low-bits (make-random-descriptor (double-float-low-bits real))))
630 (ecase sb!c:*backend-byte-order*
632 (write-wordindexed des sb!vm:complex-double-float-real-slot low-bits)
633 (write-wordindexed des (1+ sb!vm:complex-double-float-real-slot) high-bits))
635 (write-wordindexed des sb!vm:complex-double-float-real-slot high-bits)
636 (write-wordindexed des (1+ sb!vm:complex-double-float-real-slot) low-bits))))
637 (let* ((imag (imagpart num))
638 (high-bits (make-random-descriptor (double-float-high-bits imag)))
639 (low-bits (make-random-descriptor (double-float-low-bits imag))))
640 (ecase sb!c:*backend-byte-order*
642 (write-wordindexed des sb!vm:complex-double-float-imag-slot low-bits)
643 (write-wordindexed des (1+ sb!vm:complex-double-float-imag-slot) high-bits))
645 (write-wordindexed des sb!vm:complex-double-float-imag-slot high-bits)
646 (write-wordindexed des (1+ sb!vm:complex-double-float-imag-slot) low-bits))))
649 ;;; Copy the given number to the core.
650 (defun number-to-core (number)
652 (integer (if (< (integer-length number) 30)
653 (make-fixnum-descriptor number)
654 (bignum-to-core number)))
655 (ratio (number-pair-to-core (number-to-core (numerator number))
656 (number-to-core (denominator number))
657 sb!vm:ratio-widetag))
658 ((complex single-float) (complex-single-float-to-core number))
659 ((complex double-float) (complex-double-float-to-core number))
661 ((complex long-float)
662 (error "~S isn't a cold-loadable number at all!" number))
663 (complex (number-pair-to-core (number-to-core (realpart number))
664 (number-to-core (imagpart number))
665 sb!vm:complex-widetag))
666 (float (float-to-core number))
667 (t (error "~S isn't a cold-loadable number at all!" number))))
669 (declaim (ftype (function (sb!vm:word) descriptor) sap-int-to-core))
670 (defun sap-int-to-core (sap-int)
671 (let ((des (allocate-unboxed-object *dynamic*
675 (write-wordindexed des
676 sb!vm:sap-pointer-slot
677 (make-random-descriptor sap-int))
680 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
681 (defun cold-cons (car cdr &optional (gspace *dynamic*))
682 (let ((dest (allocate-boxed-object gspace 2 sb!vm:list-pointer-lowtag)))
683 (write-memory dest car)
684 (write-wordindexed dest 1 cdr)
687 ;;; Make a simple-vector on the target that holds the specified
688 ;;; OBJECTS, and return its descriptor.
689 (defun vector-in-core (&rest objects)
690 (let* ((size (length objects))
691 (result (allocate-vector-object *dynamic* sb!vm:n-word-bits size
692 sb!vm:simple-vector-widetag)))
693 (dotimes (index size)
694 (write-wordindexed result (+ index sb!vm:vector-data-offset)
700 ;;; FIXME: This should be a &KEY argument of ALLOCATE-SYMBOL.
701 (defvar *cold-symbol-allocation-gspace* nil)
703 ;;; Allocate (and initialize) a symbol.
704 (defun allocate-symbol (name)
705 (declare (simple-string name))
706 (let ((symbol (allocate-unboxed-object (or *cold-symbol-allocation-gspace*
709 (1- sb!vm:symbol-size)
710 sb!vm:symbol-header-widetag)))
711 (write-wordindexed symbol sb!vm:symbol-value-slot *unbound-marker*)
713 (write-wordindexed symbol
714 sb!vm:symbol-hash-slot
715 (make-fixnum-descriptor
716 (1+ (random sb!xc:most-positive-fixnum))))
717 (write-wordindexed symbol sb!vm:symbol-plist-slot *nil-descriptor*)
718 (write-wordindexed symbol sb!vm:symbol-name-slot
719 (string-to-core name *dynamic*))
720 (write-wordindexed symbol sb!vm:symbol-package-slot *nil-descriptor*)
723 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
724 ;;; descriptor of a cold symbol or (in an abbreviation for the
725 ;;; most common usage pattern) an ordinary symbol, which will be
726 ;;; automatically cold-interned.
727 (declaim (ftype (function ((or descriptor symbol) descriptor)) cold-set))
728 (defun cold-set (symbol-or-symbol-des value)
729 (let ((symbol-des (etypecase symbol-or-symbol-des
730 (descriptor symbol-or-symbol-des)
731 (symbol (cold-intern symbol-or-symbol-des)))))
732 (write-wordindexed symbol-des sb!vm:symbol-value-slot value)))
734 ;;;; layouts and type system pre-initialization
736 ;;; Since we want to be able to dump structure constants and
737 ;;; predicates with reference layouts, we need to create layouts at
738 ;;; cold-load time. We use the name to intern layouts by, and dump a
739 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
740 ;;; initialization can find them. The only thing that's tricky [sic --
741 ;;; WHN 19990816] is initializing layout's layout, which must point to
744 ;;; a map from class names to lists of
745 ;;; `(,descriptor ,name ,length ,inherits ,depth)
746 ;;; KLUDGE: It would be more understandable and maintainable to use
747 ;;; DEFSTRUCT (:TYPE LIST) here. -- WHN 19990823
748 (defvar *cold-layouts* (make-hash-table :test 'equal))
750 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
752 (defvar *cold-layout-names* (make-hash-table :test 'eql))
754 ;;; FIXME: *COLD-LAYOUTS* and *COLD-LAYOUT-NAMES* should be
755 ;;; initialized by binding in GENESIS.
757 ;;; the descriptor for layout's layout (needed when making layouts)
758 (defvar *layout-layout*)
760 ;;; FIXME: This information should probably be pulled out of the
761 ;;; cross-compiler's tables at genesis time instead of inserted by
762 ;;; hand here as a bare numeric constant.
763 (defconstant target-layout-length 16)
765 ;;; Return a list of names created from the cold layout INHERITS data
767 (defun listify-cold-inherits (x)
768 (let ((len (descriptor-fixnum (read-wordindexed x
769 sb!vm:vector-length-slot))))
772 (let* ((des (read-wordindexed x (+ sb!vm:vector-data-offset index)))
773 (found (gethash (descriptor-bits des) *cold-layout-names*)))
776 (error "unknown descriptor at index ~S (bits = ~8,'0X)"
778 (descriptor-bits des)))))
781 (declaim (ftype (function (symbol descriptor descriptor descriptor) descriptor)
783 (defun make-cold-layout (name length inherits depthoid)
784 (let ((result (allocate-boxed-object *dynamic*
785 ;; KLUDGE: Why 1+? -- WHN 19990901
786 (1+ target-layout-length)
787 sb!vm:instance-pointer-lowtag)))
789 (make-other-immediate-descriptor
790 target-layout-length sb!vm:instance-header-widetag))
792 ;; KLUDGE: The offsets into LAYOUT below should probably be pulled out
793 ;; of the cross-compiler's tables at genesis time instead of inserted
794 ;; by hand as bare numeric constants. -- WHN ca. 19990901
796 ;; Set slot 0 = the layout of the layout.
797 (write-wordindexed result sb!vm:instance-slots-offset *layout-layout*)
799 ;; Set the immediately following slots = CLOS hash values.
801 ;; Note: CMU CL didn't set these in genesis, but instead arranged
802 ;; for them to be set at cold init time. That resulted in slightly
803 ;; kludgy-looking code, but there were at least two things to be
805 ;; 1. It put the hash values under the control of the target Lisp's
806 ;; RANDOM function, so that CLOS behavior would be nearly
807 ;; deterministic (instead of depending on the implementation of
808 ;; RANDOM in the cross-compilation host, and the state of its
809 ;; RNG when genesis begins).
810 ;; 2. It automatically ensured that all hash values in the target Lisp
811 ;; were part of the same sequence, so that we didn't have to worry
812 ;; about the possibility of the first hash value set in genesis
813 ;; being precisely equal to the some hash value set in cold init time
814 ;; (because the target Lisp RNG has advanced to precisely the same
815 ;; state that the host Lisp RNG was in earlier).
816 ;; Point 1 should not be an issue in practice because of the way we do our
817 ;; build procedure in two steps, so that the SBCL that we end up with has
818 ;; been created by another SBCL (whose RNG is under our control).
819 ;; Point 2 is more of an issue. If ANSI had provided a way to feed
820 ;; entropy into an RNG, we would have no problem: we'd just feed
821 ;; some specialized genesis-time-only pattern into the RNG state
822 ;; before using it. However, they didn't, so we have a slight
823 ;; problem. We address it by generating the hash values using a
824 ;; different algorithm than we use in ordinary operation.
825 (dotimes (i sb!kernel:layout-clos-hash-length)
826 (let (;; The expression here is pretty arbitrary, we just want
827 ;; to make sure that it's not something which is (1)
828 ;; evenly distributed and (2) not foreordained to arise in
829 ;; the target Lisp's (RANDOM-LAYOUT-CLOS-HASH) sequence
830 ;; and show up as the CLOS-HASH value of some other
833 ;; FIXME: This expression here can generate a zero value,
834 ;; and the CMU CL code goes out of its way to generate
835 ;; strictly positive values (even though the field is
836 ;; declared as an INDEX). Check that it's really OK to
837 ;; have zero values in the CLOS-HASH slots.
838 (hash-value (mod (logxor (logand (random-layout-clos-hash) 15253)
839 (logandc2 (random-layout-clos-hash) 15253)
841 ;; (The MOD here is defensive programming
842 ;; to make sure we never write an
843 ;; out-of-range value even if some joker
844 ;; sets LAYOUT-CLOS-HASH-MAX to other
845 ;; than 2^n-1 at some time in the
847 (1+ sb!kernel:layout-clos-hash-max))))
848 (write-wordindexed result
849 (+ i sb!vm:instance-slots-offset 1)
850 (make-fixnum-descriptor hash-value))))
852 ;; Set other slot values.
853 (let ((base (+ sb!vm:instance-slots-offset
854 sb!kernel:layout-clos-hash-length
856 ;; (Offset 0 is CLASS, "the class this is a layout for", which
857 ;; is uninitialized at this point.)
858 (write-wordindexed result (+ base 1) *nil-descriptor*) ; marked invalid
859 (write-wordindexed result (+ base 2) inherits)
860 (write-wordindexed result (+ base 3) depthoid)
861 (write-wordindexed result (+ base 4) length)
862 (write-wordindexed result (+ base 5) *nil-descriptor*) ; info
863 (write-wordindexed result (+ base 6) *nil-descriptor*)) ; pure
865 (setf (gethash name *cold-layouts*)
868 (descriptor-fixnum length)
869 (listify-cold-inherits inherits)
870 (descriptor-fixnum depthoid)))
871 (setf (gethash (descriptor-bits result) *cold-layout-names*) name)
875 (defun initialize-layouts ()
877 (clrhash *cold-layouts*)
879 ;; We initially create the layout of LAYOUT itself with NIL as the LAYOUT and
881 (setq *layout-layout* *nil-descriptor*)
882 (setq *layout-layout*
883 (make-cold-layout 'layout
884 (number-to-core target-layout-length)
886 ;; FIXME: hard-coded LAYOUT-DEPTHOID of LAYOUT..
888 (write-wordindexed *layout-layout*
889 sb!vm:instance-slots-offset
892 ;; Then we create the layouts that we'll need to make a correct INHERITS
893 ;; vector for the layout of LAYOUT itself..
895 ;; FIXME: The various LENGTH and DEPTHOID numbers should be taken from
896 ;; the compiler's tables, not set by hand.
903 (make-cold-layout 'instance
905 (vector-in-core t-layout)
908 (make-cold-layout 'structure-object
910 (vector-in-core t-layout i-layout)
913 (make-cold-layout 'structure!object
915 (vector-in-core t-layout i-layout so-layout)
917 (layout-inherits (vector-in-core t-layout
922 ;; ..and return to backpatch the layout of LAYOUT.
923 (setf (fourth (gethash 'layout *cold-layouts*))
924 (listify-cold-inherits layout-inherits))
925 (write-wordindexed *layout-layout*
926 ;; FIXME: hardcoded offset into layout struct
927 (+ sb!vm:instance-slots-offset
928 layout-clos-hash-length
933 ;;;; interning symbols in the cold image
935 ;;; In order to avoid having to know about the package format, we
936 ;;; build a data structure in *COLD-PACKAGE-SYMBOLS* that holds all
937 ;;; interned symbols along with info about their packages. The data
938 ;;; structure is a list of sublists, where the sublists have the
939 ;;; following format:
940 ;;; (<make-package-arglist>
941 ;;; <internal-symbols>
942 ;;; <external-symbols>
943 ;;; <imported-internal-symbols>
944 ;;; <imported-external-symbols>
945 ;;; <shadowing-symbols>)
947 ;;; KLUDGE: It would be nice to implement the sublists as instances of
948 ;;; a DEFSTRUCT (:TYPE LIST). (They'd still be lists, but at least we'd be
949 ;;; using mnemonically-named operators to access them, instead of trying
950 ;;; to remember what THIRD and FIFTH mean, and hoping that we never
951 ;;; need to change the list layout..) -- WHN 19990825
953 ;;; an alist from packages to lists of that package's symbols to be dumped
954 (defvar *cold-package-symbols*)
955 (declaim (type list *cold-package-symbols*))
957 ;;; a map from descriptors to symbols, so that we can back up. The key
958 ;;; is the address in the target core.
959 (defvar *cold-symbols*)
960 (declaim (type hash-table *cold-symbols*))
962 ;;; sanity check for a symbol we're about to create on the target
964 ;;; Make sure that the symbol has an appropriate package. In
965 ;;; particular, catch the so-easy-to-make error of typing something
966 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
967 ;;; need is SB!KERNEL:%BYTE-BLT.
968 (defun package-ok-for-target-symbol-p (package)
969 (let ((package-name (package-name package)))
971 ;; Cold interning things in these standard packages is OK. (Cold
972 ;; interning things in the other standard package, CL-USER, isn't
973 ;; OK. We just use CL-USER to expose symbols whose homes are in
974 ;; other packages. Thus, trying to cold intern a symbol whose
975 ;; home package is CL-USER probably means that a coding error has
976 ;; been made somewhere.)
977 (find package-name '("COMMON-LISP" "KEYWORD") :test #'string=)
978 ;; Cold interning something in one of our target-code packages,
979 ;; which are ever-so-rigorously-and-elegantly distinguished by
980 ;; this prefix on their names, is OK too.
981 (string= package-name "SB!" :end1 3 :end2 3)
982 ;; This one is OK too, since it ends up being COMMON-LISP on the
984 (string= package-name "SB-XC")
985 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
986 ;; package in the xc host? something we can't think of
987 ;; a valid reason to cold intern, anyway...)
990 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
992 ;;; Most host symbols we dump onto the target are created by SBCL
993 ;;; itself, so that as long as we avoid gratuitously
994 ;;; cross-compilation-unfriendly hacks, it just happens that their
995 ;;; SYMBOL-PACKAGE in the host system corresponds to their
996 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
997 ;;; in the COMMON-LISP package, where we don't get to create the
998 ;;; symbols but instead have to use the ones that the xc host created.
999 ;;; In particular, while ANSI specifies which symbols are exported
1000 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1001 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1002 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1003 ;;; symbols in the CLOS package).
1004 (defun symbol-package-for-target-symbol (symbol)
1005 ;; We want to catch weird symbols like CLISP's
1006 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1007 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1008 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1009 (multiple-value-bind (cl-symbol cl-status)
1010 (find-symbol (symbol-name symbol) *cl-package*)
1011 (if (and (eq symbol cl-symbol)
1012 (eq cl-status :external))
1013 ;; special case, to work around possible xc host weirdness
1014 ;; in COMMON-LISP package
1017 (let ((result (symbol-package symbol)))
1018 (aver (package-ok-for-target-symbol-p result))
1021 ;;; Return a handle on an interned symbol. If necessary allocate the
1022 ;;; symbol and record which package the symbol was referenced in. When
1023 ;;; we allocate the symbol, make sure we record a reference to the
1024 ;;; symbol in the home package so that the package gets set.
1025 (defun cold-intern (symbol
1027 (package (symbol-package-for-target-symbol symbol)))
1029 (aver (package-ok-for-target-symbol-p package))
1031 ;; Anything on the cross-compilation host which refers to the target
1032 ;; machinery through the host SB-XC package should be translated to
1033 ;; something on the target which refers to the same machinery
1034 ;; through the target COMMON-LISP package.
1035 (let ((p (find-package "SB-XC")))
1036 (when (eq package p)
1037 (setf package *cl-package*))
1038 (when (eq (symbol-package symbol) p)
1039 (setf symbol (intern (symbol-name symbol) *cl-package*))))
1041 (let (;; Information about each cold-interned symbol is stored
1042 ;; in COLD-INTERN-INFO.
1043 ;; (CAR COLD-INTERN-INFO) = descriptor of symbol
1044 ;; (CDR COLD-INTERN-INFO) = list of packages, other than symbol's
1045 ;; own package, referring to symbol
1046 ;; (*COLD-PACKAGE-SYMBOLS* and *COLD-SYMBOLS* store basically the
1047 ;; same information, but with the mapping running the opposite way.)
1048 (cold-intern-info (get symbol 'cold-intern-info)))
1049 (unless cold-intern-info
1050 (cond ((eq (symbol-package-for-target-symbol symbol) package)
1051 (let ((handle (allocate-symbol (symbol-name symbol))))
1052 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1053 (when (eq package *keyword-package*)
1054 (cold-set handle handle))
1055 (setq cold-intern-info
1056 (setf (get symbol 'cold-intern-info) (cons handle nil)))))
1058 (cold-intern symbol)
1059 (setq cold-intern-info (get symbol 'cold-intern-info)))))
1060 (unless (or (null package)
1061 (member package (cdr cold-intern-info)))
1062 (push package (cdr cold-intern-info))
1063 (let* ((old-cps-entry (assoc package *cold-package-symbols*))
1064 (cps-entry (or old-cps-entry
1065 (car (push (list package)
1066 *cold-package-symbols*)))))
1067 (unless old-cps-entry
1068 (/show "created *COLD-PACKAGE-SYMBOLS* entry for" package symbol))
1069 (push symbol (rest cps-entry))))
1070 (car cold-intern-info)))
1072 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1073 (defun make-nil-descriptor ()
1074 (let* ((des (allocate-unboxed-object
1079 (result (make-descriptor (descriptor-high des)
1080 (+ (descriptor-low des)
1081 (* 2 sb!vm:n-word-bytes)
1082 (- sb!vm:list-pointer-lowtag
1083 sb!vm:other-pointer-lowtag)))))
1084 (write-wordindexed des
1086 (make-other-immediate-descriptor
1088 sb!vm:symbol-header-widetag))
1089 (write-wordindexed des
1090 (+ 1 sb!vm:symbol-value-slot)
1092 (write-wordindexed des
1093 (+ 2 sb!vm:symbol-value-slot)
1095 (write-wordindexed des
1096 (+ 1 sb!vm:symbol-plist-slot)
1098 (write-wordindexed des
1099 (+ 1 sb!vm:symbol-name-slot)
1100 ;; This is *DYNAMIC*, and DES is *STATIC*,
1101 ;; because that's the way CMU CL did it; I'm
1102 ;; not sure whether there's an underlying
1103 ;; reason. -- WHN 1990826
1104 (string-to-core "NIL" *dynamic*))
1105 (write-wordindexed des
1106 (+ 1 sb!vm:symbol-package-slot)
1108 (setf (get nil 'cold-intern-info)
1113 ;;; Since the initial symbols must be allocated before we can intern
1114 ;;; anything else, we intern those here. We also set the value of T.
1115 (defun initialize-non-nil-symbols ()
1117 "Initialize the cold load symbol-hacking data structures."
1118 (let ((*cold-symbol-allocation-gspace* *static*))
1119 ;; Intern the others.
1120 (dolist (symbol sb!vm:*static-symbols*)
1121 (let* ((des (cold-intern symbol))
1122 (offset-wanted (sb!vm:static-symbol-offset symbol))
1123 (offset-found (- (descriptor-low des)
1124 (descriptor-low *nil-descriptor*))))
1125 (unless (= offset-wanted offset-found)
1126 ;; FIXME: should be fatal
1127 (warn "Offset from ~S to ~S is ~W, not ~W"
1132 ;; Establish the value of T.
1133 (let ((t-symbol (cold-intern t)))
1134 (cold-set t-symbol t-symbol))))
1136 ;;; a helper function for FINISH-SYMBOLS: Return a cold alist suitable
1137 ;;; to be stored in *!INITIAL-LAYOUTS*.
1138 (defun cold-list-all-layouts ()
1139 (let ((result *nil-descriptor*))
1140 (maphash (lambda (key stuff)
1141 (cold-push (cold-cons (cold-intern key)
1147 ;;; Establish initial values for magic symbols.
1149 ;;; Scan over all the symbols referenced in each package in
1150 ;;; *COLD-PACKAGE-SYMBOLS* making that for each one there's an
1151 ;;; appropriate entry in the *!INITIAL-SYMBOLS* data structure to
1153 (defun finish-symbols ()
1155 ;; I think the point of setting these functions into SYMBOL-VALUEs
1156 ;; here, instead of using SYMBOL-FUNCTION, is that in CMU CL
1157 ;; SYMBOL-FUNCTION reduces to FDEFINITION, which is a pretty
1158 ;; hairy operation (involving globaldb.lisp etc.) which we don't
1159 ;; want to invoke early in cold init. -- WHN 2001-12-05
1161 ;; FIXME: So OK, that's a reasonable reason to do something weird like
1162 ;; this, but this is still a weird thing to do, and we should change
1163 ;; the names to highlight that something weird is going on. Perhaps
1164 ;; *MAYBE-GC-FUN*, *INTERNAL-ERROR-FUN*, *HANDLE-BREAKPOINT-FUN*,
1165 ;; and *HANDLE-FUN-END-BREAKPOINT-FUN*...
1166 (macrolet ((frob (symbol)
1168 (cold-fdefinition-object (cold-intern ',symbol)))))
1170 (frob internal-error)
1171 (frob sb!di::handle-breakpoint)
1172 (frob sb!di::handle-fun-end-breakpoint))
1174 (cold-set '*current-catch-block* (make-fixnum-descriptor 0))
1175 (cold-set '*current-unwind-protect-block* (make-fixnum-descriptor 0))
1177 (cold-set '*free-interrupt-context-index* (make-fixnum-descriptor 0))
1179 (cold-set '*!initial-layouts* (cold-list-all-layouts))
1181 (/show "dumping packages" (mapcar #'car *cold-package-symbols*))
1182 (let ((initial-symbols *nil-descriptor*))
1183 (dolist (cold-package-symbols-entry *cold-package-symbols*)
1184 (let* ((cold-package (car cold-package-symbols-entry))
1185 (symbols (cdr cold-package-symbols-entry))
1186 (shadows (package-shadowing-symbols cold-package))
1187 (internal *nil-descriptor*)
1188 (external *nil-descriptor*)
1189 (imported-internal *nil-descriptor*)
1190 (imported-external *nil-descriptor*)
1191 (shadowing *nil-descriptor*))
1192 (declare (type package cold-package)) ; i.e. not a target descriptor
1193 (/show "dumping" cold-package symbols)
1195 ;; FIXME: Add assertions here to make sure that inappropriate stuff
1196 ;; isn't being dumped:
1197 ;; * the CL-USER package
1198 ;; * the SB-COLD package
1199 ;; * any internal symbols in the CL package
1200 ;; * basically any package other than CL, KEYWORD, or the packages
1201 ;; in package-data-list.lisp-expr
1202 ;; and that the structure of the KEYWORD package (e.g. whether
1203 ;; any symbols are internal to it) matches what we want in the
1206 ;; FIXME: It seems possible that by looking at the contents of
1207 ;; packages in the target SBCL we could find which symbols in
1208 ;; package-data-lisp.lisp-expr are now obsolete. (If I
1209 ;; understand correctly, only symbols which actually have
1210 ;; definitions or which are otherwise referred to actually end
1211 ;; up in the target packages.)
1213 (dolist (symbol symbols)
1214 (let ((handle (car (get symbol 'cold-intern-info)))
1215 (imported-p (not (eq (symbol-package-for-target-symbol symbol)
1217 (multiple-value-bind (found where)
1218 (find-symbol (symbol-name symbol) cold-package)
1219 (unless (and where (eq found symbol))
1220 (error "The symbol ~S is not available in ~S."
1223 (when (memq symbol shadows)
1224 (cold-push handle shadowing))
1226 (:internal (if imported-p
1227 (cold-push handle imported-internal)
1228 (cold-push handle internal)))
1229 (:external (if imported-p
1230 (cold-push handle imported-external)
1231 (cold-push handle external)))))))
1232 (let ((r *nil-descriptor*))
1233 (cold-push shadowing r)
1234 (cold-push imported-external r)
1235 (cold-push imported-internal r)
1236 (cold-push external r)
1237 (cold-push internal r)
1238 (cold-push (make-make-package-args cold-package) r)
1239 ;; FIXME: It would be more space-efficient to use vectors
1240 ;; instead of lists here, and space-efficiency here would be
1241 ;; nice, since it would reduce the peak memory usage in
1242 ;; genesis and cold init.
1243 (cold-push r initial-symbols))))
1244 (cold-set '*!initial-symbols* initial-symbols))
1246 (cold-set '*!initial-fdefn-objects* (list-all-fdefn-objects))
1248 (cold-set '*!reversed-cold-toplevels* *current-reversed-cold-toplevels*)
1252 (cold-set 'sb!vm::*fp-constant-0d0* (number-to-core 0d0))
1253 (cold-set 'sb!vm::*fp-constant-1d0* (number-to-core 1d0))
1254 (cold-set 'sb!vm::*fp-constant-0f0* (number-to-core 0f0))
1255 (cold-set 'sb!vm::*fp-constant-1f0* (number-to-core 1f0))
1258 (cold-set 'sb!vm::*fp-constant-0l0* (number-to-core 0L0))
1259 (cold-set 'sb!vm::*fp-constant-1l0* (number-to-core 1L0))
1260 ;; FIXME: Why is initialization of PI conditional on LONG-FLOAT?
1261 ;; (ditto LG2, LN2, L2E, etc.)
1262 (cold-set 'sb!vm::*fp-constant-pi* (number-to-core pi))
1263 (cold-set 'sb!vm::*fp-constant-l2t* (number-to-core (log 10L0 2L0)))
1264 (cold-set 'sb!vm::*fp-constant-l2e*
1265 (number-to-core (log 2.718281828459045235360287471352662L0 2L0)))
1266 (cold-set 'sb!vm::*fp-constant-lg2* (number-to-core (log 2L0 10L0)))
1267 (cold-set 'sb!vm::*fp-constant-ln2*
1269 (log 2L0 2.718281828459045235360287471352662L0))))))
1271 ;;; Make a cold list that can be used as the arg list to MAKE-PACKAGE in order
1272 ;;; to make a package that is similar to PKG.
1273 (defun make-make-package-args (pkg)
1274 (let* ((use *nil-descriptor*)
1275 (cold-nicknames *nil-descriptor*)
1276 (res *nil-descriptor*))
1277 (dolist (u (package-use-list pkg))
1278 (when (assoc u *cold-package-symbols*)
1279 (cold-push (string-to-core (package-name u)) use)))
1280 (let* ((pkg-name (package-name pkg))
1281 ;; Make the package nickname lists for the standard packages
1282 ;; be the minimum specified by ANSI, regardless of what value
1283 ;; the cross-compilation host happens to use.
1284 (warm-nicknames (cond ((string= pkg-name "COMMON-LISP")
1286 ((string= pkg-name "COMMON-LISP-USER")
1288 ((string= pkg-name "KEYWORD")
1290 ;; For packages other than the
1291 ;; standard packages, the nickname
1292 ;; list was specified by our package
1293 ;; setup code, not by properties of
1294 ;; what cross-compilation host we
1295 ;; happened to use, and we can just
1296 ;; propagate it into the target.
1298 (package-nicknames pkg)))))
1299 (dolist (warm-nickname warm-nicknames)
1300 (cold-push (string-to-core warm-nickname) cold-nicknames)))
1302 (cold-push (number-to-core (truncate (package-internal-symbol-count pkg)
1305 (cold-push (cold-intern :internal-symbols) res)
1306 (cold-push (number-to-core (truncate (package-external-symbol-count pkg)
1309 (cold-push (cold-intern :external-symbols) res)
1311 (cold-push cold-nicknames res)
1312 (cold-push (cold-intern :nicknames) res)
1315 (cold-push (cold-intern :use) res)
1317 (cold-push (string-to-core (package-name pkg)) res)
1320 ;;;; functions and fdefinition objects
1322 ;;; a hash table mapping from fdefinition names to descriptors of cold
1325 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1326 ;;; we want to have only one entry per name, this must be an 'EQUAL
1327 ;;; hash table, not the default 'EQL.
1328 (defvar *cold-fdefn-objects*)
1330 (defvar *cold-fdefn-gspace* nil)
1332 ;;; Given a cold representation of a symbol, return a warm
1334 (defun warm-symbol (des)
1335 ;; Note that COLD-INTERN is responsible for keeping the
1336 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1337 ;; uninterned symbol, the code below will fail. But as long as we
1338 ;; don't need to look up uninterned symbols during bootstrapping,
1340 (multiple-value-bind (symbol found-p)
1341 (gethash (descriptor-bits des) *cold-symbols*)
1342 (declare (type symbol symbol))
1344 (error "no warm symbol"))
1347 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1348 (defun cold-car (des)
1349 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1350 (read-wordindexed des sb!vm:cons-car-slot))
1351 (defun cold-cdr (des)
1352 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1353 (read-wordindexed des sb!vm:cons-cdr-slot))
1354 (defun cold-null (des)
1355 (= (descriptor-bits des)
1356 (descriptor-bits *nil-descriptor*)))
1358 ;;; Given a cold representation of a function name, return a warm
1360 (declaim (ftype (function (descriptor) (or symbol list)) warm-fun-name))
1361 (defun warm-fun-name (des)
1363 (ecase (descriptor-lowtag des)
1364 (#.sb!vm:list-pointer-lowtag
1365 (aver (not (cold-null des))) ; function named NIL? please no..
1366 ;; Do cold (DESTRUCTURING-BIND (COLD-CAR COLD-CADR) DES ..).
1367 (let* ((car-des (cold-car des))
1368 (cdr-des (cold-cdr des))
1369 (cadr-des (cold-car cdr-des))
1370 (cddr-des (cold-cdr cdr-des)))
1371 (aver (cold-null cddr-des))
1372 (list (warm-symbol car-des)
1373 (warm-symbol cadr-des))))
1374 (#.sb!vm:other-pointer-lowtag
1375 (warm-symbol des)))))
1376 (unless (legal-fun-name-p result)
1377 (error "not a legal function name: ~S" result))
1380 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw)
1381 (declare (type descriptor cold-name))
1382 (let ((warm-name (warm-fun-name cold-name)))
1383 (or (gethash warm-name *cold-fdefn-objects*)
1384 (let ((fdefn (allocate-boxed-object (or *cold-fdefn-gspace* *dynamic*)
1385 (1- sb!vm:fdefn-size)
1386 sb!vm:other-pointer-lowtag)))
1388 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
1389 (write-memory fdefn (make-other-immediate-descriptor
1390 (1- sb!vm:fdefn-size) sb!vm:fdefn-widetag))
1391 (write-wordindexed fdefn sb!vm:fdefn-name-slot cold-name)
1392 (unless leave-fn-raw
1393 (write-wordindexed fdefn sb!vm:fdefn-fun-slot
1395 (write-wordindexed fdefn
1396 sb!vm:fdefn-raw-addr-slot
1397 (make-random-descriptor
1398 (cold-foreign-symbol-address-as-integer
1399 (sb!vm:extern-alien-name "undefined_tramp")))))
1402 ;;; Handle the at-cold-init-time, fset-for-static-linkage operation
1403 ;;; requested by FOP-FSET.
1404 (defun static-fset (cold-name defn)
1405 (declare (type descriptor cold-name))
1406 (let ((fdefn (cold-fdefinition-object cold-name t))
1407 (type (logand (descriptor-low (read-memory defn)) sb!vm:widetag-mask)))
1408 (write-wordindexed fdefn sb!vm:fdefn-fun-slot defn)
1409 (write-wordindexed fdefn
1410 sb!vm:fdefn-raw-addr-slot
1412 (#.sb!vm:simple-fun-header-widetag
1416 (make-random-descriptor
1417 (+ (logandc2 (descriptor-bits defn)
1419 (ash sb!vm:simple-fun-code-offset
1420 sb!vm:word-shift))))
1421 (#.sb!vm:closure-header-widetag
1422 (make-random-descriptor
1423 (cold-foreign-symbol-address-as-integer
1424 (sb!vm:extern-alien-name "closure_tramp"))))))
1427 (defun initialize-static-fns ()
1428 (let ((*cold-fdefn-gspace* *static*))
1429 (dolist (sym sb!vm:*static-funs*)
1430 (let* ((fdefn (cold-fdefinition-object (cold-intern sym)))
1431 (offset (- (+ (- (descriptor-low fdefn)
1432 sb!vm:other-pointer-lowtag)
1433 (* sb!vm:fdefn-raw-addr-slot sb!vm:n-word-bytes))
1434 (descriptor-low *nil-descriptor*)))
1435 (desired (sb!vm:static-fun-offset sym)))
1436 (unless (= offset desired)
1437 ;; FIXME: should be fatal
1438 (warn "Offset from FDEFN ~S to ~S is ~W, not ~W."
1439 sym nil offset desired))))))
1441 (defun list-all-fdefn-objects ()
1442 (let ((result *nil-descriptor*))
1443 (maphash (lambda (key value)
1444 (declare (ignore key))
1445 (cold-push value result))
1446 *cold-fdefn-objects*)
1449 ;;;; fixups and related stuff
1451 ;;; an EQUAL hash table
1452 (defvar *cold-foreign-symbol-table*)
1453 (declaim (type hash-table *cold-foreign-symbol-table*))
1455 ;;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1457 (defun load-cold-foreign-symbol-table (filename)
1458 (with-open-file (file filename)
1460 (let ((line (read-line file nil nil)))
1463 ;; UNIX symbol tables might have tabs in them, and tabs are
1464 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1465 ;; nice portable way to deal with them within Lisp, alas.
1466 ;; Fortunately, it's easy to use UNIX command line tools like
1467 ;; sed to remove the problem, so it's not too painful for us
1468 ;; to push responsibility for converting tabs to spaces out to
1471 ;; Other non-STANDARD-CHARs are problematic for the same reason.
1472 ;; Make sure that there aren't any..
1473 (let ((ch (find-if (lambda (char)
1474 (not (typep char 'standard-char)))
1477 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1480 (setf line (string-trim '(#\space) line))
1481 (let ((p1 (position #\space line :from-end nil))
1482 (p2 (position #\space line :from-end t)))
1483 (if (not (and p1 p2 (< p1 p2)))
1484 ;; KLUDGE: It's too messy to try to understand all
1485 ;; possible output from nm, so we just punt the lines we
1486 ;; don't recognize. We realize that there's some chance
1487 ;; that might get us in trouble someday, so we warn
1489 (warn "ignoring unrecognized line ~S in ~A" line filename)
1490 (multiple-value-bind (value name)
1491 (if (string= "0x" line :end2 2)
1492 (values (parse-integer line :start 2 :end p1 :radix 16)
1493 (subseq line (1+ p2)))
1494 (values (parse-integer line :end p1 :radix 16)
1495 (subseq line (1+ p2))))
1496 (multiple-value-bind (old-value found)
1497 (gethash name *cold-foreign-symbol-table*)
1499 (not (= old-value value)))
1500 (warn "redefining ~S from #X~X to #X~X"
1501 name old-value value)))
1502 (setf (gethash name *cold-foreign-symbol-table*) value))))))
1505 (defun cold-foreign-symbol-address-as-integer (name)
1506 (or (find-foreign-symbol-in-table name *cold-foreign-symbol-table*)
1507 *foreign-symbol-placeholder-value*
1509 (format *error-output* "~&The foreign symbol table is:~%")
1510 (maphash (lambda (k v)
1511 (format *error-output* "~&~S = #X~8X~%" k v))
1512 *cold-foreign-symbol-table*)
1513 (error "The foreign symbol ~S is undefined." name))))
1515 (defvar *cold-assembler-routines*)
1517 (defvar *cold-assembler-fixups*)
1519 (defun record-cold-assembler-routine (name address)
1520 (/xhow "in RECORD-COLD-ASSEMBLER-ROUTINE" name address)
1521 (push (cons name address)
1522 *cold-assembler-routines*))
1524 (defun record-cold-assembler-fixup (routine
1529 (push (list routine code-object offset kind)
1530 *cold-assembler-fixups*))
1532 (defun lookup-assembler-reference (symbol)
1533 (let ((value (cdr (assoc symbol *cold-assembler-routines*))))
1534 ;; FIXME: Should this be ERROR instead of WARN?
1536 (warn "Assembler routine ~S not defined." symbol))
1539 ;;; The x86 port needs to store code fixups along with code objects if
1540 ;;; they are to be moved, so fixups for code objects in the dynamic
1541 ;;; heap need to be noted.
1543 (defvar *load-time-code-fixups*)
1546 (defun note-load-time-code-fixup (code-object offset value kind)
1547 ;; If CODE-OBJECT might be moved
1548 (when (= (gspace-identifier (descriptor-intuit-gspace code-object))
1550 ;; FIXME: pushed thing should be a structure, not just a list
1551 (push (list code-object offset value kind) *load-time-code-fixups*))
1555 (defun output-load-time-code-fixups ()
1556 (dolist (fixups *load-time-code-fixups*)
1557 (let ((code-object (first fixups))
1558 (offset (second fixups))
1559 (value (third fixups))
1560 (kind (fourth fixups)))
1561 (cold-push (cold-cons
1562 (cold-intern :load-time-code-fixup)
1566 (number-to-core offset)
1568 (number-to-core value)
1571 *nil-descriptor*)))))
1572 *current-reversed-cold-toplevels*))))
1574 ;;; Given a pointer to a code object and an offset relative to the
1575 ;;; tail of the code object's header, return an offset relative to the
1576 ;;; (beginning of the) code object.
1578 ;;; FIXME: It might be clearer to reexpress
1579 ;;; (LET ((X (CALC-OFFSET CODE-OBJECT OFFSET0))) ..)
1581 ;;; (LET ((X (+ OFFSET0 (CODE-OBJECT-HEADER-N-BYTES CODE-OBJECT)))) ..).
1582 (declaim (ftype (function (descriptor sb!vm:word)) calc-offset))
1583 (defun calc-offset (code-object offset-from-tail-of-header)
1584 (let* ((header (read-memory code-object))
1585 (header-n-words (ash (descriptor-bits header)
1586 (- sb!vm:n-widetag-bits)))
1587 (header-n-bytes (ash header-n-words sb!vm:word-shift))
1588 (result (+ offset-from-tail-of-header header-n-bytes)))
1591 (declaim (ftype (function (descriptor sb!vm:word sb!vm:word keyword))
1593 (defun do-cold-fixup (code-object after-header value kind)
1594 (let* ((offset-within-code-object (calc-offset code-object after-header))
1595 (gspace-bytes (descriptor-bytes code-object))
1596 (gspace-byte-offset (+ (descriptor-byte-offset code-object)
1597 offset-within-code-object))
1598 (gspace-byte-address (gspace-byte-address
1599 (descriptor-gspace code-object))))
1600 (ecase +backend-fasl-file-implementation+
1601 ;; See CMU CL source for other formerly-supported architectures
1602 ;; (and note that you have to rewrite them to use VECTOR-REF
1603 ;; unstead of SAP-REF).
1607 (assert (zerop (ldb (byte 2 0) value))))
1609 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1610 (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
1611 (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
1612 (setf (byte-vector-ref-8 gspace-bytes gspace-byte-offset)
1613 (ldb (byte 8 48) value)
1614 (byte-vector-ref-8 gspace-bytes (1+ gspace-byte-offset))
1615 (ldb (byte 8 56) value))))
1617 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1618 (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
1619 (setf (byte-vector-ref-8 gspace-bytes gspace-byte-offset)
1620 (ldb (byte 8 32) value)
1621 (byte-vector-ref-8 gspace-bytes (1+ gspace-byte-offset))
1622 (ldb (byte 8 40) value))))
1624 (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
1625 (setf (byte-vector-ref-8 gspace-bytes gspace-byte-offset)
1626 (ldb (byte 8 16) value)
1627 (byte-vector-ref-8 gspace-bytes (1+ gspace-byte-offset))
1628 (ldb (byte 8 24) value))))
1630 (setf (byte-vector-ref-8 gspace-bytes gspace-byte-offset)
1631 (ldb (byte 8 0) value)
1632 (byte-vector-ref-8 gspace-bytes (1+ gspace-byte-offset))
1633 (ldb (byte 8 8) value)))))
1637 (setf (byte-vector-ref-32 gspace-bytes gspace-byte-offset)
1638 (dpb (ash value -2) (byte 24 2)
1639 (byte-vector-ref-32 gspace-bytes gspace-byte-offset))))
1641 (let* ((h (ldb (byte 16 16) value))
1642 (l (ldb (byte 16 0) value)))
1643 (setf (byte-vector-ref-16 gspace-bytes (+ gspace-byte-offset 2))
1644 (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
1646 (setf (byte-vector-ref-16 gspace-bytes (+ gspace-byte-offset 2))
1647 (ldb (byte 16 0) value)))))
1651 (error "Can't deal with call fixups yet."))
1653 (setf (byte-vector-ref-32 gspace-bytes gspace-byte-offset)
1654 (dpb (ldb (byte 22 10) value)
1656 (byte-vector-ref-32 gspace-bytes gspace-byte-offset))))
1658 (setf (byte-vector-ref-32 gspace-bytes gspace-byte-offset)
1659 (dpb (ldb (byte 10 0) value)
1661 (byte-vector-ref-32 gspace-bytes gspace-byte-offset))))))
1663 (let* ((un-fixed-up (byte-vector-ref-32 gspace-bytes
1664 gspace-byte-offset))
1665 (code-object-start-addr (logandc2 (descriptor-bits code-object)
1666 sb!vm:lowtag-mask)))
1667 (assert (= code-object-start-addr
1668 (+ gspace-byte-address
1669 (descriptor-byte-offset code-object))))
1672 (let ((fixed-up (+ value un-fixed-up)))
1673 (setf (byte-vector-ref-32 gspace-bytes gspace-byte-offset)
1675 ;; comment from CMU CL sources:
1677 ;; Note absolute fixups that point within the object.
1678 ;; KLUDGE: There seems to be an implicit assumption in
1679 ;; the old CMU CL code here, that if it doesn't point
1680 ;; before the object, it must point within the object
1681 ;; (not beyond it). It would be good to add an
1682 ;; explanation of why that's true, or an assertion that
1683 ;; it's really true, or both.
1684 (unless (< fixed-up code-object-start-addr)
1685 (note-load-time-code-fixup code-object
1689 (:relative ; (used for arguments to X86 relative CALL instruction)
1690 (let ((fixed-up (- (+ value un-fixed-up)
1693 sb!vm:n-word-bytes))) ; length of CALL argument
1694 (setf (byte-vector-ref-32 gspace-bytes gspace-byte-offset)
1696 ;; Note relative fixups that point outside the code
1697 ;; object, which is to say all relative fixups, since
1698 ;; relative addressing within a code object never needs
1700 (note-load-time-code-fixup code-object
1706 (defun resolve-assembler-fixups ()
1707 (dolist (fixup *cold-assembler-fixups*)
1708 (let* ((routine (car fixup))
1709 (value (lookup-assembler-reference routine)))
1711 (do-cold-fixup (second fixup) (third fixup) value (fourth fixup))))))
1713 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
1714 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
1715 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
1716 ;;; target-load.lisp refers to.
1717 (defun linkage-info-to-core ()
1718 (let ((result *nil-descriptor*))
1719 (maphash (lambda (symbol value)
1720 (cold-push (cold-cons (string-to-core symbol)
1721 (number-to-core value))
1723 *cold-foreign-symbol-table*)
1724 (cold-set (cold-intern '*!initial-foreign-symbols*) result))
1725 (let ((result *nil-descriptor*))
1726 (dolist (rtn *cold-assembler-routines*)
1727 (cold-push (cold-cons (cold-intern (car rtn))
1728 (number-to-core (cdr rtn)))
1730 (cold-set (cold-intern '*!initial-assembler-routines*) result)))
1732 ;;;; general machinery for cold-loading FASL files
1734 ;;; FOP functions for cold loading
1735 (defvar *cold-fop-funs*
1736 ;; We start out with a copy of the ordinary *FOP-FUNS*. The ones
1737 ;; which aren't appropriate for cold load will be destructively
1739 (copy-seq *fop-funs*))
1741 (defvar *normal-fop-funs*)
1743 ;;; Cause a fop to have a special definition for cold load.
1745 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
1746 ;;; (1) looks up the code for this name (created by a previous
1747 ;; DEFINE-FOP) instead of creating a code, and
1748 ;;; (2) stores its definition in the *COLD-FOP-FUNS* vector,
1749 ;;; instead of storing in the *FOP-FUNS* vector.
1750 (defmacro define-cold-fop ((name &key (pushp t) (stackp t)) &rest forms)
1751 (aver (member pushp '(nil t)))
1752 (aver (member stackp '(nil t)))
1753 (let ((code (get name 'fop-code))
1754 (fname (symbolicate "COLD-" name)))
1756 (error "~S is not a defined FOP." name))
1760 `((with-fop-stack ,pushp ,@forms))
1762 (setf (svref *cold-fop-funs* ,code) #',fname))))
1764 (defmacro clone-cold-fop ((name &key (pushp t) (stackp t)) (small-name) &rest forms)
1765 (aver (member pushp '(nil t)))
1766 (aver (member stackp '(nil t)))
1768 (macrolet ((clone-arg () '(read-arg 4)))
1769 (define-cold-fop (,name :pushp ,pushp :stackp ,stackp) ,@forms))
1770 (macrolet ((clone-arg () '(read-arg 1)))
1771 (define-cold-fop (,small-name :pushp ,pushp :stackp ,stackp) ,@forms))))
1773 ;;; Cause a fop to be undefined in cold load.
1774 (defmacro not-cold-fop (name)
1775 `(define-cold-fop (,name)
1776 (error "The fop ~S is not supported in cold load." ',name)))
1778 ;;; COLD-LOAD loads stuff into the core image being built by calling
1779 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
1780 ;;; loading functions.
1781 (defun cold-load (filename)
1783 "Load the file named by FILENAME into the cold load image being built."
1784 (let* ((*normal-fop-funs* *fop-funs*)
1785 (*fop-funs* *cold-fop-funs*)
1786 (*cold-load-filename* (etypecase filename
1788 (pathname (namestring filename)))))
1789 (with-open-file (s filename :element-type '(unsigned-byte 8))
1790 (load-as-fasl s nil nil))))
1792 ;;;; miscellaneous cold fops
1794 (define-cold-fop (fop-misc-trap) *unbound-marker*)
1796 (define-cold-fop (fop-character)
1797 (make-character-descriptor (read-arg 3)))
1798 (define-cold-fop (fop-short-character)
1799 (make-character-descriptor (read-arg 1)))
1801 (define-cold-fop (fop-empty-list) *nil-descriptor*)
1802 (define-cold-fop (fop-truth) (cold-intern t))
1804 (define-cold-fop (fop-normal-load :stackp nil)
1805 (setq *fop-funs* *normal-fop-funs*))
1807 (define-fop (fop-maybe-cold-load 82 :stackp nil)
1808 (when *cold-load-filename*
1809 (setq *fop-funs* *cold-fop-funs*)))
1811 (define-cold-fop (fop-maybe-cold-load :stackp nil))
1813 (clone-cold-fop (fop-struct)
1815 (let* ((size (clone-arg))
1816 (result (allocate-boxed-object *dynamic*
1818 sb!vm:instance-pointer-lowtag)))
1819 (write-memory result (make-other-immediate-descriptor
1820 size sb!vm:instance-header-widetag))
1821 (do ((index (1- size) (1- index)))
1823 (declare (fixnum index))
1824 (write-wordindexed result
1825 (+ index sb!vm:instance-slots-offset)
1829 (define-cold-fop (fop-layout)
1830 (let* ((length-des (pop-stack))
1831 (depthoid-des (pop-stack))
1832 (cold-inherits (pop-stack))
1834 (old (gethash name *cold-layouts*)))
1835 (declare (type descriptor length-des depthoid-des cold-inherits))
1836 (declare (type symbol name))
1837 ;; If a layout of this name has been defined already
1839 ;; Enforce consistency between the previous definition and the
1840 ;; current definition, then return the previous definition.
1842 ;; FIXME: This would be more maintainable if we used
1843 ;; DEFSTRUCT (:TYPE LIST) to define COLD-LAYOUT. -- WHN 19990825
1844 (old-layout-descriptor
1850 (declare (type descriptor old-layout-descriptor))
1851 (declare (type index old-length))
1852 (declare (type fixnum old-depthoid))
1853 (declare (type list old-inherits-list))
1854 (aver (eq name old-name))
1855 (let ((length (descriptor-fixnum length-des))
1856 (inherits-list (listify-cold-inherits cold-inherits))
1857 (depthoid (descriptor-fixnum depthoid-des)))
1858 (unless (= length old-length)
1859 (error "cold loading a reference to class ~S when the compile~%~
1860 time length was ~S and current length is ~S"
1864 (unless (equal inherits-list old-inherits-list)
1865 (error "cold loading a reference to class ~S when the compile~%~
1866 time inherits were ~S~%~
1867 and current inherits are ~S"
1871 (unless (= depthoid old-depthoid)
1872 (error "cold loading a reference to class ~S when the compile~%~
1873 time inheritance depthoid was ~S and current inheritance~%~
1878 old-layout-descriptor)
1879 ;; Make a new definition from scratch.
1880 (make-cold-layout name length-des cold-inherits depthoid-des))))
1882 ;;;; cold fops for loading symbols
1884 ;;; Load a symbol SIZE characters long from *FASL-INPUT-STREAM* and
1885 ;;; intern that symbol in PACKAGE.
1886 (defun cold-load-symbol (size package)
1887 (let ((string (make-string size)))
1888 (read-string-as-bytes *fasl-input-stream* string)
1889 (cold-intern (intern string package) package)))
1891 (macrolet ((frob (name pname-len package-len)
1892 `(define-cold-fop (,name)
1893 (let ((index (read-arg ,package-len)))
1895 (cold-load-symbol (read-arg ,pname-len)
1896 (svref *current-fop-table* index)))))))
1897 (frob fop-symbol-in-package-save 4 4)
1898 (frob fop-small-symbol-in-package-save 1 4)
1899 (frob fop-symbol-in-byte-package-save 4 1)
1900 (frob fop-small-symbol-in-byte-package-save 1 1))
1902 (clone-cold-fop (fop-lisp-symbol-save)
1903 (fop-lisp-small-symbol-save)
1904 (push-fop-table (cold-load-symbol (clone-arg) *cl-package*)))
1906 (clone-cold-fop (fop-keyword-symbol-save)
1907 (fop-keyword-small-symbol-save)
1908 (push-fop-table (cold-load-symbol (clone-arg) *keyword-package*)))
1910 (clone-cold-fop (fop-uninterned-symbol-save)
1911 (fop-uninterned-small-symbol-save)
1912 (let* ((size (clone-arg))
1913 (name (make-string size)))
1914 (read-string-as-bytes *fasl-input-stream* name)
1915 (let ((symbol-des (allocate-symbol name)))
1916 (push-fop-table symbol-des))))
1918 ;;;; cold fops for loading lists
1920 ;;; Make a list of the top LENGTH things on the fop stack. The last
1921 ;;; cdr of the list is set to LAST.
1922 (defmacro cold-stack-list (length last)
1923 `(do* ((index ,length (1- index))
1924 (result ,last (cold-cons (pop-stack) result)))
1925 ((= index 0) result)
1926 (declare (fixnum index))))
1928 (define-cold-fop (fop-list)
1929 (cold-stack-list (read-arg 1) *nil-descriptor*))
1930 (define-cold-fop (fop-list*)
1931 (cold-stack-list (read-arg 1) (pop-stack)))
1932 (define-cold-fop (fop-list-1)
1933 (cold-stack-list 1 *nil-descriptor*))
1934 (define-cold-fop (fop-list-2)
1935 (cold-stack-list 2 *nil-descriptor*))
1936 (define-cold-fop (fop-list-3)
1937 (cold-stack-list 3 *nil-descriptor*))
1938 (define-cold-fop (fop-list-4)
1939 (cold-stack-list 4 *nil-descriptor*))
1940 (define-cold-fop (fop-list-5)
1941 (cold-stack-list 5 *nil-descriptor*))
1942 (define-cold-fop (fop-list-6)
1943 (cold-stack-list 6 *nil-descriptor*))
1944 (define-cold-fop (fop-list-7)
1945 (cold-stack-list 7 *nil-descriptor*))
1946 (define-cold-fop (fop-list-8)
1947 (cold-stack-list 8 *nil-descriptor*))
1948 (define-cold-fop (fop-list*-1)
1949 (cold-stack-list 1 (pop-stack)))
1950 (define-cold-fop (fop-list*-2)
1951 (cold-stack-list 2 (pop-stack)))
1952 (define-cold-fop (fop-list*-3)
1953 (cold-stack-list 3 (pop-stack)))
1954 (define-cold-fop (fop-list*-4)
1955 (cold-stack-list 4 (pop-stack)))
1956 (define-cold-fop (fop-list*-5)
1957 (cold-stack-list 5 (pop-stack)))
1958 (define-cold-fop (fop-list*-6)
1959 (cold-stack-list 6 (pop-stack)))
1960 (define-cold-fop (fop-list*-7)
1961 (cold-stack-list 7 (pop-stack)))
1962 (define-cold-fop (fop-list*-8)
1963 (cold-stack-list 8 (pop-stack)))
1965 ;;;; cold fops for loading vectors
1967 (clone-cold-fop (fop-string)
1969 (let* ((len (clone-arg))
1970 (string (make-string len)))
1971 (read-string-as-bytes *fasl-input-stream* string)
1972 (string-to-core string)))
1974 (clone-cold-fop (fop-vector)
1976 (let* ((size (clone-arg))
1977 (result (allocate-vector-object *dynamic*
1980 sb!vm:simple-vector-widetag)))
1981 (do ((index (1- size) (1- index)))
1983 (declare (fixnum index))
1984 (write-wordindexed result
1985 (+ index sb!vm:vector-data-offset)
1989 (define-cold-fop (fop-int-vector)
1990 (let* ((len (read-arg 4))
1991 (sizebits (read-arg 1))
1992 (type (case sizebits
1993 (1 sb!vm:simple-bit-vector-widetag)
1994 (2 sb!vm:simple-array-unsigned-byte-2-widetag)
1995 (4 sb!vm:simple-array-unsigned-byte-4-widetag)
1996 (8 sb!vm:simple-array-unsigned-byte-8-widetag)
1997 (16 sb!vm:simple-array-unsigned-byte-16-widetag)
1998 (32 sb!vm:simple-array-unsigned-byte-32-widetag)
1999 (t (error "losing element size: ~W" sizebits))))
2000 (result (allocate-vector-object *dynamic* sizebits len type))
2001 (start (+ (descriptor-byte-offset result)
2002 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2004 (ceiling (* len sizebits)
2005 sb!vm:n-byte-bits))))
2006 (read-sequence-or-die (descriptor-bytes result)
2012 (define-cold-fop (fop-single-float-vector)
2013 (let* ((len (read-arg 4))
2014 (result (allocate-vector-object
2018 sb!vm:simple-array-single-float-widetag))
2019 (start (+ (descriptor-byte-offset result)
2020 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2021 (end (+ start (* len sb!vm:n-word-bytes))))
2022 (read-sequence-or-die (descriptor-bytes result)
2028 (not-cold-fop fop-double-float-vector)
2029 #!+long-float (not-cold-fop fop-long-float-vector)
2030 (not-cold-fop fop-complex-single-float-vector)
2031 (not-cold-fop fop-complex-double-float-vector)
2032 #!+long-float (not-cold-fop fop-complex-long-float-vector)
2034 (define-cold-fop (fop-array)
2035 (let* ((rank (read-arg 4))
2036 (data-vector (pop-stack))
2037 (result (allocate-boxed-object *dynamic*
2038 (+ sb!vm:array-dimensions-offset rank)
2039 sb!vm:other-pointer-lowtag)))
2040 (write-memory result
2041 (make-other-immediate-descriptor rank
2042 sb!vm:simple-array-widetag))
2043 (write-wordindexed result sb!vm:array-fill-pointer-slot *nil-descriptor*)
2044 (write-wordindexed result sb!vm:array-data-slot data-vector)
2045 (write-wordindexed result sb!vm:array-displacement-slot *nil-descriptor*)
2046 (write-wordindexed result sb!vm:array-displaced-p-slot *nil-descriptor*)
2047 (let ((total-elements 1))
2048 (dotimes (axis rank)
2049 (let ((dim (pop-stack)))
2050 (unless (or (= (descriptor-lowtag dim) sb!vm:even-fixnum-lowtag)
2051 (= (descriptor-lowtag dim) sb!vm:odd-fixnum-lowtag))
2052 (error "non-fixnum dimension? (~S)" dim))
2053 (setf total-elements
2055 (logior (ash (descriptor-high dim)
2056 (- descriptor-low-bits
2057 (1- sb!vm:n-lowtag-bits)))
2058 (ash (descriptor-low dim)
2059 (- 1 sb!vm:n-lowtag-bits)))))
2060 (write-wordindexed result
2061 (+ sb!vm:array-dimensions-offset axis)
2063 (write-wordindexed result
2064 sb!vm:array-elements-slot
2065 (make-fixnum-descriptor total-elements)))
2068 ;;;; cold fops for loading numbers
2070 (defmacro define-cold-number-fop (fop)
2071 `(define-cold-fop (,fop :stackp nil)
2072 ;; Invoke the ordinary warm version of this fop to push the
2075 ;; Replace the warm fop result with the cold image of the warm
2078 (let ((number (pop-stack)))
2079 (number-to-core number)))))
2081 (define-cold-number-fop fop-single-float)
2082 (define-cold-number-fop fop-double-float)
2083 (define-cold-number-fop fop-integer)
2084 (define-cold-number-fop fop-small-integer)
2085 (define-cold-number-fop fop-word-integer)
2086 (define-cold-number-fop fop-byte-integer)
2087 (define-cold-number-fop fop-complex-single-float)
2088 (define-cold-number-fop fop-complex-double-float)
2091 (define-cold-fop (fop-long-float)
2092 (ecase +backend-fasl-file-implementation+
2093 (:x86 ; (which has 80-bit long-float format)
2094 (prepare-for-fast-read-byte *fasl-input-stream*
2095 (let* ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
2096 (1- sb!vm:long-float-size)
2097 sb!vm:long-float-widetag))
2098 (low-bits (make-random-descriptor (fast-read-u-integer 4)))
2099 (high-bits (make-random-descriptor (fast-read-u-integer 4)))
2100 (exp-bits (make-random-descriptor (fast-read-s-integer 2))))
2101 (done-with-fast-read-byte)
2102 (write-wordindexed des sb!vm:long-float-value-slot low-bits)
2103 (write-wordindexed des (1+ sb!vm:long-float-value-slot) high-bits)
2104 (write-wordindexed des (+ 2 sb!vm:long-float-value-slot) exp-bits)
2106 ;; This was supported in CMU CL, but isn't currently supported in
2109 (#.sb!c:sparc-fasl-file-implementation ; 128 bit long-float format
2110 (prepare-for-fast-read-byte *fasl-input-stream*
2111 (let* ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
2112 (1- sb!vm:long-float-size)
2113 sb!vm:long-float-widetag))
2114 (low-bits (make-random-descriptor (fast-read-u-integer 4)))
2115 (mid-bits (make-random-descriptor (fast-read-u-integer 4)))
2116 (high-bits (make-random-descriptor (fast-read-u-integer 4)))
2117 (exp-bits (make-random-descriptor (fast-read-s-integer 4))))
2118 (done-with-fast-read-byte)
2119 (write-wordindexed des sb!vm:long-float-value-slot exp-bits)
2120 (write-wordindexed des (1+ sb!vm:long-float-value-slot) high-bits)
2121 (write-wordindexed des (+ 2 sb!vm:long-float-value-slot) mid-bits)
2122 (write-wordindexed des (+ 3 sb!vm:long-float-value-slot) low-bits)
2126 (define-cold-fop (fop-complex-long-float)
2127 (ecase +backend-fasl-file-implementation+
2128 (:x86 ; (which has 80-bit long-float format)
2129 (prepare-for-fast-read-byte *fasl-input-stream*
2130 (let* ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
2131 (1- sb!vm:complex-long-float-size)
2132 sb!vm:complex-long-float-widetag))
2133 (real-low-bits (make-random-descriptor (fast-read-u-integer 4)))
2134 (real-high-bits (make-random-descriptor (fast-read-u-integer 4)))
2135 (real-exp-bits (make-random-descriptor (fast-read-s-integer 2)))
2136 (imag-low-bits (make-random-descriptor (fast-read-u-integer 4)))
2137 (imag-high-bits (make-random-descriptor (fast-read-u-integer 4)))
2138 (imag-exp-bits (make-random-descriptor (fast-read-s-integer 2))))
2139 (done-with-fast-read-byte)
2140 (write-wordindexed des
2141 sb!vm:complex-long-float-real-slot
2143 (write-wordindexed des
2144 (1+ sb!vm:complex-long-float-real-slot)
2146 (write-wordindexed des
2147 (+ 2 sb!vm:complex-long-float-real-slot)
2149 (write-wordindexed des
2150 sb!vm:complex-long-float-imag-slot
2152 (write-wordindexed des
2153 (1+ sb!vm:complex-long-float-imag-slot)
2155 (write-wordindexed des
2156 (+ 2 sb!vm:complex-long-float-imag-slot)
2159 ;; This was supported in CMU CL, but isn't currently supported in SBCL.
2161 (#.sb!c:sparc-fasl-file-implementation ; 128 bit long-float format
2162 (prepare-for-fast-read-byte *fasl-input-stream*
2163 (let* ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
2164 (1- sb!vm:complex-long-float-size)
2165 sb!vm:complex-long-float-widetag))
2166 (real-low-bits (make-random-descriptor (fast-read-u-integer 4)))
2167 (real-mid-bits (make-random-descriptor (fast-read-u-integer 4)))
2168 (real-high-bits (make-random-descriptor (fast-read-u-integer 4)))
2169 (real-exp-bits (make-random-descriptor (fast-read-s-integer 4)))
2170 (imag-low-bits (make-random-descriptor (fast-read-u-integer 4)))
2171 (imag-mid-bits (make-random-descriptor (fast-read-u-integer 4)))
2172 (imag-high-bits (make-random-descriptor (fast-read-u-integer 4)))
2173 (imag-exp-bits (make-random-descriptor (fast-read-s-integer 4))))
2174 (done-with-fast-read-byte)
2175 (write-wordindexed des
2176 sb!vm:complex-long-float-real-slot
2178 (write-wordindexed des
2179 (1+ sb!vm:complex-long-float-real-slot)
2181 (write-wordindexed des
2182 (+ 2 sb!vm:complex-long-float-real-slot)
2184 (write-wordindexed des
2185 (+ 3 sb!vm:complex-long-float-real-slot)
2187 (write-wordindexed des
2188 sb!vm:complex-long-float-real-slot
2190 (write-wordindexed des
2191 (1+ sb!vm:complex-long-float-real-slot)
2193 (write-wordindexed des
2194 (+ 2 sb!vm:complex-long-float-real-slot)
2196 (write-wordindexed des
2197 (+ 3 sb!vm:complex-long-float-real-slot)
2201 (define-cold-fop (fop-ratio)
2202 (let ((den (pop-stack)))
2203 (number-pair-to-core (pop-stack) den sb!vm:ratio-widetag)))
2205 (define-cold-fop (fop-complex)
2206 (let ((im (pop-stack)))
2207 (number-pair-to-core (pop-stack) im sb!vm:complex-widetag)))
2209 ;;;; cold fops for calling (or not calling)
2211 (not-cold-fop fop-eval)
2212 (not-cold-fop fop-eval-for-effect)
2214 (defvar *load-time-value-counter*)
2216 (define-cold-fop (fop-funcall)
2217 (unless (= (read-arg 1) 0)
2218 (error "You can't FOP-FUNCALL arbitrary stuff in cold load."))
2219 (let ((counter *load-time-value-counter*))
2220 (cold-push (cold-cons
2221 (cold-intern :load-time-value)
2225 (number-to-core counter)
2227 *current-reversed-cold-toplevels*)
2228 (setf *load-time-value-counter* (1+ counter))
2229 (make-descriptor 0 0 nil counter)))
2231 (defun finalize-load-time-value-noise ()
2232 (cold-set (cold-intern '*!load-time-values*)
2233 (allocate-vector-object *dynamic*
2235 *load-time-value-counter*
2236 sb!vm:simple-vector-widetag)))
2238 (define-cold-fop (fop-funcall-for-effect :pushp nil)
2239 (if (= (read-arg 1) 0)
2240 (cold-push (pop-stack)
2241 *current-reversed-cold-toplevels*)
2242 (error "You can't FOP-FUNCALL arbitrary stuff in cold load.")))
2244 ;;;; cold fops for fixing up circularities
2246 (define-cold-fop (fop-rplaca :pushp nil)
2247 (let ((obj (svref *current-fop-table* (read-arg 4)))
2249 (write-memory (cold-nthcdr idx obj) (pop-stack))))
2251 (define-cold-fop (fop-rplacd :pushp nil)
2252 (let ((obj (svref *current-fop-table* (read-arg 4)))
2254 (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2256 (define-cold-fop (fop-svset :pushp nil)
2257 (let ((obj (svref *current-fop-table* (read-arg 4)))
2259 (write-wordindexed obj
2261 (ecase (descriptor-lowtag obj)
2262 (#.sb!vm:instance-pointer-lowtag 1)
2263 (#.sb!vm:other-pointer-lowtag 2)))
2266 (define-cold-fop (fop-structset :pushp nil)
2267 (let ((obj (svref *current-fop-table* (read-arg 4)))
2269 (write-wordindexed obj (1+ idx) (pop-stack))))
2271 ;;; In the original CMUCL code, this actually explicitly declared PUSHP
2272 ;;; to be T, even though that's what it defaults to in DEFINE-COLD-FOP.
2273 (define-cold-fop (fop-nthcdr)
2274 (cold-nthcdr (read-arg 4) (pop-stack)))
2276 (defun cold-nthcdr (index obj)
2278 (setq obj (read-wordindexed obj 1)))
2281 ;;;; cold fops for loading code objects and functions
2283 ;;; the names of things which have had COLD-FSET used on them already
2284 ;;; (used to make sure that we don't try to statically link a name to
2285 ;;; more than one definition)
2286 (defparameter *cold-fset-warm-names*
2287 ;; This can't be an EQL hash table because names can be conses, e.g.
2289 (make-hash-table :test 'equal))
2291 (define-cold-fop (fop-fset :pushp nil)
2292 (let* ((fn (pop-stack))
2293 (cold-name (pop-stack))
2294 (warm-name (warm-fun-name cold-name)))
2295 (if (gethash warm-name *cold-fset-warm-names*)
2296 (error "duplicate COLD-FSET for ~S" warm-name)
2297 (setf (gethash warm-name *cold-fset-warm-names*) t))
2298 (static-fset cold-name fn)))
2300 (define-cold-fop (fop-fdefinition)
2301 (cold-fdefinition-object (pop-stack)))
2303 (define-cold-fop (fop-sanctify-for-execution)
2306 ;;; Setting this variable shows what code looks like before any
2307 ;;; fixups (or function headers) are applied.
2308 #!+sb-show (defvar *show-pre-fixup-code-p* nil)
2310 ;;; FIXME: The logic here should be converted into a function
2311 ;;; COLD-CODE-FOP-GUTS (NCONST CODE-SIZE) called by DEFINE-COLD-FOP
2312 ;;; FOP-CODE and DEFINE-COLD-FOP FOP-SMALL-CODE, so that
2313 ;;; variable-capture nastiness like (LET ((NCONST ,NCONST) ..) ..)
2314 ;;; doesn't keep me awake at night.
2315 (defmacro define-cold-code-fop (name nconst code-size)
2316 `(define-cold-fop (,name)
2317 (let* ((nconst ,nconst)
2318 (code-size ,code-size)
2319 (raw-header-n-words (+ sb!vm:code-trace-table-offset-slot nconst))
2321 ;; Note: we round the number of constants up to ensure
2322 ;; that the code vector will be properly aligned.
2323 (round-up raw-header-n-words 2))
2324 (des (allocate-cold-descriptor *dynamic*
2325 (+ (ash header-n-words
2328 sb!vm:other-pointer-lowtag)))
2330 (make-other-immediate-descriptor
2331 header-n-words sb!vm:code-header-widetag))
2332 (write-wordindexed des
2333 sb!vm:code-code-size-slot
2334 (make-fixnum-descriptor
2335 (ash (+ code-size (1- (ash 1 sb!vm:word-shift)))
2336 (- sb!vm:word-shift))))
2337 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2338 (write-wordindexed des sb!vm:code-debug-info-slot (pop-stack))
2339 (when (oddp raw-header-n-words)
2340 (write-wordindexed des
2342 (make-random-descriptor 0)))
2343 (do ((index (1- raw-header-n-words) (1- index)))
2344 ((< index sb!vm:code-trace-table-offset-slot))
2345 (write-wordindexed des index (pop-stack)))
2346 (let* ((start (+ (descriptor-byte-offset des)
2347 (ash header-n-words sb!vm:word-shift)))
2348 (end (+ start code-size)))
2349 (read-sequence-or-die (descriptor-bytes des)
2354 (when *show-pre-fixup-code-p*
2355 (format *trace-output*
2356 "~&/raw code from code-fop ~W ~W:~%"
2359 (do ((i start (+ i sb!vm:n-word-bytes)))
2361 (format *trace-output*
2362 "/#X~8,'0x: #X~8,'0x~%"
2363 (+ i (gspace-byte-address (descriptor-gspace des)))
2364 (byte-vector-ref-32 (descriptor-bytes des) i)))))
2367 (define-cold-code-fop fop-code (read-arg 4) (read-arg 4))
2369 (define-cold-code-fop fop-small-code (read-arg 1) (read-arg 2))
2371 (clone-cold-fop (fop-alter-code :pushp nil)
2372 (fop-byte-alter-code)
2373 (let ((slot (clone-arg))
2376 (write-wordindexed code slot value)))
2378 (define-cold-fop (fop-fun-entry)
2379 (let* ((type (pop-stack))
2380 (arglist (pop-stack))
2382 (code-object (pop-stack))
2383 (offset (calc-offset code-object (read-arg 4)))
2384 (fn (descriptor-beyond code-object
2386 sb!vm:fun-pointer-lowtag))
2387 (next (read-wordindexed code-object sb!vm:code-entry-points-slot)))
2388 (unless (zerop (logand offset sb!vm:lowtag-mask))
2389 (error "unaligned function entry: ~S at #X~X" name offset))
2390 (write-wordindexed code-object sb!vm:code-entry-points-slot fn)
2392 (make-other-immediate-descriptor
2393 (ash offset (- sb!vm:word-shift))
2394 sb!vm:simple-fun-header-widetag))
2395 (write-wordindexed fn
2396 sb!vm:simple-fun-self-slot
2397 ;; KLUDGE: Wiring decisions like this in at
2398 ;; this level ("if it's an x86") instead of a
2399 ;; higher level of abstraction ("if it has such
2400 ;; and such relocation peculiarities (which
2401 ;; happen to be confined to the x86)") is bad.
2402 ;; It would be nice if the code were instead
2403 ;; conditional on some more descriptive
2404 ;; feature, :STICKY-CODE or
2405 ;; :LOAD-GC-INTERACTION or something.
2407 ;; FIXME: The X86 definition of the function
2408 ;; self slot breaks everything object.tex says
2409 ;; about it. (As far as I can tell, the X86
2410 ;; definition makes it a pointer to the actual
2411 ;; code instead of a pointer back to the object
2412 ;; itself.) Ask on the mailing list whether
2413 ;; this is documented somewhere, and if not,
2414 ;; try to reverse engineer some documentation.
2416 ;; a pointer back to the function object, as
2417 ;; described in CMU CL
2418 ;; src/docs/internals/object.tex
2421 ;; KLUDGE: a pointer to the actual code of the
2422 ;; object, as described nowhere that I can find
2424 (make-random-descriptor
2425 (+ (descriptor-bits fn)
2426 (- (ash sb!vm:simple-fun-code-offset
2428 ;; FIXME: We should mask out the type
2429 ;; bits, not assume we know what they
2430 ;; are and subtract them out this way.
2431 sb!vm:fun-pointer-lowtag))))
2432 (write-wordindexed fn sb!vm:simple-fun-next-slot next)
2433 (write-wordindexed fn sb!vm:simple-fun-name-slot name)
2434 (write-wordindexed fn sb!vm:simple-fun-arglist-slot arglist)
2435 (write-wordindexed fn sb!vm:simple-fun-type-slot type)
2438 (define-cold-fop (fop-foreign-fixup)
2439 (let* ((kind (pop-stack))
2440 (code-object (pop-stack))
2442 (sym (make-string len)))
2443 (read-string-as-bytes *fasl-input-stream* sym)
2444 (let ((offset (read-arg 4))
2445 (value (cold-foreign-symbol-address-as-integer sym)))
2446 (do-cold-fixup code-object offset value kind))
2449 (define-cold-fop (fop-assembler-code)
2450 (let* ((length (read-arg 4))
2452 ;; Note: we round the number of constants up to ensure that
2453 ;; the code vector will be properly aligned.
2454 (round-up sb!vm:code-constants-offset 2))
2455 (des (allocate-cold-descriptor *read-only*
2456 (+ (ash header-n-words
2459 sb!vm:other-pointer-lowtag)))
2461 (make-other-immediate-descriptor
2462 header-n-words sb!vm:code-header-widetag))
2463 (write-wordindexed des
2464 sb!vm:code-code-size-slot
2465 (make-fixnum-descriptor
2466 (ash (+ length (1- (ash 1 sb!vm:word-shift)))
2467 (- sb!vm:word-shift))))
2468 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2469 (write-wordindexed des sb!vm:code-debug-info-slot *nil-descriptor*)
2471 (let* ((start (+ (descriptor-byte-offset des)
2472 (ash header-n-words sb!vm:word-shift)))
2473 (end (+ start length)))
2474 (read-sequence-or-die (descriptor-bytes des)
2480 (define-cold-fop (fop-assembler-routine)
2481 (let* ((routine (pop-stack))
2483 (offset (calc-offset des (read-arg 4))))
2484 (record-cold-assembler-routine
2486 (+ (logandc2 (descriptor-bits des) sb!vm:lowtag-mask) offset))
2489 (define-cold-fop (fop-assembler-fixup)
2490 (let* ((routine (pop-stack))
2492 (code-object (pop-stack))
2493 (offset (read-arg 4)))
2494 (record-cold-assembler-fixup routine code-object offset kind)
2497 (define-cold-fop (fop-code-object-fixup)
2498 (let* ((kind (pop-stack))
2499 (code-object (pop-stack))
2500 (offset (read-arg 4))
2501 (value (descriptor-bits code-object)))
2502 (do-cold-fixup code-object offset value kind)
2505 ;;;; emitting C header file
2507 (defun tailwise-equal (string tail)
2508 (and (>= (length string) (length tail))
2509 (string= string tail :start1 (- (length string) (length tail)))))
2511 (defun write-c-header ()
2513 ;; writing beginning boilerplate
2516 '("This is a machine-generated file. Please do not edit it by hand."
2518 "This file contains low-level information about the"
2519 "internals of a particular version and configuration"
2520 "of SBCL. It is used by the C compiler to create a runtime"
2521 "support environment, an executable program in the host"
2522 "operating system's native format, which can then be used to"
2523 "load and run 'core' files, which are basically programs"
2524 "in SBCL's own format."))
2525 (format t " * ~A~%" line))
2528 (format t "#ifndef _SBCL_H_~%#define _SBCL_H_~%")
2531 ;; propagating *SHEBANG-FEATURES* into C-level #define's
2532 (dolist (shebang-feature-name (sort (mapcar #'symbol-name
2533 sb-cold:*shebang-features*)
2536 "#define LISP_FEATURE_~A~%"
2537 (substitute #\_ #\- shebang-feature-name)))
2540 ;; writing miscellaneous constants
2541 (format t "#define SBCL_CORE_VERSION_INTEGER ~D~%" sbcl-core-version-integer)
2543 "#define SBCL_VERSION_STRING ~S~%"
2544 (sb!xc:lisp-implementation-version))
2545 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
2547 ;; FIXME: Other things from core.h should be defined here too:
2548 ;; #define CORE_END 3840
2549 ;; #define CORE_NDIRECTORY 3861
2550 ;; #define CORE_VALIDATE 3845
2551 ;; #define CORE_VERSION 3860
2552 ;; #define CORE_MACHINE_STATE 3862
2553 ;; (Except that some of them are obsolete and should be deleted instead.)
2555 ;; #define DYNAMIC_SPACE_ID (1)
2556 ;; #define STATIC_SPACE_ID (2)
2557 ;; #define READ_ONLY_SPACE_ID (3)
2559 ;; writing entire families of named constants from SB!VM
2560 (let ((constants nil))
2561 (do-external-symbols (symbol (find-package "SB!VM"))
2562 (when (constantp symbol)
2563 (let ((name (symbol-name symbol)))
2564 (labels (;; shared machinery
2565 (record (string priority)
2568 (symbol-value symbol)
2569 (documentation symbol 'variable))
2571 ;; machinery for old-style CMU CL Lisp-to-C
2572 ;; arbitrary renaming, being phased out in favor of
2573 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
2575 (record-with-munged-name (prefix string priority)
2576 (record (concatenate
2579 (delete #\- (string-capitalize string)))
2581 (maybe-record-with-munged-name (tail prefix priority)
2582 (when (tailwise-equal name tail)
2583 (record-with-munged-name prefix
2588 ;; machinery for new-style SBCL Lisp-to-C naming
2589 (record-with-translated-name (priority)
2590 (record (substitute #\_ #\- name)
2592 (maybe-record-with-translated-name (suffixes priority)
2593 (when (some (lambda (suffix)
2594 (tailwise-equal name suffix))
2596 (record-with-translated-name priority))))
2598 (maybe-record-with-translated-name '("-LOWTAG") 0)
2599 (maybe-record-with-translated-name '("-WIDETAG") 1)
2600 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
2601 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
2602 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
2603 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
2604 (maybe-record-with-translated-name '("-START" "-END") 6)))))
2607 (lambda (const1 const2)
2608 (if (= (second const1) (second const2))
2609 (< (third const1) (third const2))
2610 (< (second const1) (second const2))))))
2611 (let ((prev-priority (second (car constants))))
2612 (dolist (const constants)
2613 (destructuring-bind (name priority value doc) const
2614 (unless (= prev-priority priority)
2616 (setf prev-priority priority))
2617 (format t "#define ~A " name)
2619 ;; KLUDGE: As of sbcl-0.6.7.14, we're dumping two
2620 ;; different kinds of values here, (1) small codes
2621 ;; and (2) machine addresses. The small codes can be
2622 ;; dumped as bare integer values. The large machine
2623 ;; addresses might cause problems if they're large
2624 ;; and represented as (signed) C integers, so we
2625 ;; want to force them to be unsigned. We do that by
2626 ;; wrapping them in the LISPOBJ macro. (We could do
2627 ;; it with a bare "(unsigned)" cast, except that
2628 ;; this header file is used not only in C files, but
2629 ;; also in assembly files, which don't understand
2630 ;; the cast syntax. The LISPOBJ macro goes away in
2631 ;; assembly files, but that shouldn't matter because
2632 ;; we don't do arithmetic on address constants in
2633 ;; assembly files. See? It really is a kludge..) --
2635 (let (;; cutoff for treatment as a small code
2636 (cutoff (expt 2 16)))
2637 (cond ((minusp value)
2638 (error "stub: negative values unsupported"))
2644 (format t " /* 0x~X */~@[ /* ~A */~]~%" value doc))))
2647 ;; writing information about internal errors
2648 (let ((internal-errors sb!c:*backend-internal-errors*))
2649 (dotimes (i (length internal-errors))
2650 (let ((current-error (aref internal-errors i)))
2651 ;; FIXME: this UNLESS should go away (see also FIXME in
2652 ;; interr.lisp) -- APD, 2002-03-05
2653 (unless (eq nil (car current-error))
2654 (format t "#define ~A ~D~%"
2655 (substitute #\_ #\- (symbol-name (car current-error)))
2659 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
2660 ;; platforms. If we export this from the SB!VM package, it gets
2661 ;; written out as #define trap_PseudoAtomic, which is confusing as
2662 ;; the runtime treats trap_ as the prefix for illegal instruction
2663 ;; type things. We therefore don't export it, but instead do
2665 (when (boundp 'sb!vm::pseudo-atomic-trap)
2666 (format t "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%" sb!vm::pseudo-atomic-trap)
2668 ;; possibly this is another candidate for a rename (to
2669 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
2670 ;; [possibly applicable to other platforms])
2672 ;; writing primitive object layouts
2673 (let ((structs (sort (copy-list sb!vm:*primitive-objects*) #'string<
2676 (sb!vm:primitive-object-name obj))))))
2677 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2678 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
2679 (dolist (obj structs)
2683 (string-downcase (string (sb!vm:primitive-object-name obj)))))
2684 (when (sb!vm:primitive-object-widetag obj)
2685 (format t " lispobj header;~%"))
2686 (dolist (slot (sb!vm:primitive-object-slots obj))
2687 (format t " ~A ~A~@[[1]~];~%"
2688 (getf (sb!vm:slot-options slot) :c-type "lispobj")
2690 (string-downcase (string (sb!vm:slot-name slot))))
2691 (sb!vm:slot-rest-p slot)))
2693 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2694 (format t "#define LISPOBJ(thing) thing~2%")
2695 (dolist (obj structs)
2696 (let ((name (sb!vm:primitive-object-name obj))
2697 (lowtag (eval (sb!vm:primitive-object-lowtag obj))))
2699 (dolist (slot (sb!vm:primitive-object-slots obj))
2700 (format t "#define ~A_~A_OFFSET ~D~%"
2701 (substitute #\_ #\- (string name))
2702 (substitute #\_ #\- (string (sb!vm:slot-name slot)))
2703 (- (* (sb!vm:slot-offset slot) sb!vm:n-word-bytes) lowtag)))
2705 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%"))
2707 ;; writing static symbol offsets
2708 (dolist (symbol (cons nil sb!vm:*static-symbols*))
2709 ;; FIXME: It would be nice to use longer names than NIL and
2710 ;; (particularly) T in #define statements.
2711 (format t "#define ~A LISPOBJ(0x~X)~%"
2713 (remove-if (lambda (char)
2714 (member char '(#\% #\* #\. #\!)))
2715 (symbol-name symbol)))
2716 (if *static* ; if we ran GENESIS
2717 ;; We actually ran GENESIS, use the real value.
2718 (descriptor-bits (cold-intern symbol))
2719 ;; We didn't run GENESIS, so guess at the address.
2720 (+ sb!vm:static-space-start
2722 sb!vm:other-pointer-lowtag
2723 (if symbol (sb!vm:static-symbol-offset symbol) 0)))))
2726 (format t "~%#endif~%"))
2728 ;;;; writing map file
2730 ;;; Write a map file describing the cold load. Some of this
2731 ;;; information is subject to change due to relocating GC, but even so
2732 ;;; it can be very handy when attempting to troubleshoot the early
2733 ;;; stages of cold load.
2735 (let ((*print-pretty* nil)
2736 (*print-case* :upcase))
2737 (format t "assembler routines defined in core image:~2%")
2738 (dolist (routine (sort (copy-list *cold-assembler-routines*) #'<
2740 (format t "#X~8,'0X: ~S~%" (cdr routine) (car routine)))
2743 (maphash (lambda (name fdefn)
2744 (let ((fun (read-wordindexed fdefn
2745 sb!vm:fdefn-fun-slot)))
2746 (if (= (descriptor-bits fun)
2747 (descriptor-bits *nil-descriptor*))
2749 (let ((addr (read-wordindexed
2750 fdefn sb!vm:fdefn-raw-addr-slot)))
2751 (push (cons name (descriptor-bits addr))
2753 *cold-fdefn-objects*)
2754 (format t "~%~|~%initially defined functions:~2%")
2755 (setf funs (sort funs #'< :key #'cdr))
2757 (format t "0x~8,'0X: ~S #X~8,'0X~%" (cdr info) (car info)
2758 (- (cdr info) #x17)))
2761 (a note about initially undefined function references: These functions
2762 are referred to by code which is installed by GENESIS, but they are not
2763 installed by GENESIS. This is not necessarily a problem; functions can
2764 be defined later, by cold init toplevel forms, or in files compiled and
2765 loaded at warm init, or elsewhere. As long as they are defined before
2766 they are called, everything should be OK. Things are also OK if the
2767 cross-compiler knew their inline definition and used that everywhere
2768 that they were called before the out-of-line definition is installed,
2769 as is fairly common for structure accessors.)
2770 initially undefined function references:~2%")
2772 (setf undefs (sort undefs #'string< :key #'fun-name-block-name))
2773 (dolist (name undefs)
2774 (format t "~S~%" name)))
2776 (format t "~%~|~%layout names:~2%")
2778 (maphash (lambda (name gorp)
2779 (declare (ignore name))
2780 (stuff (cons (descriptor-bits (car gorp))
2783 (dolist (x (sort (stuff) #'< :key #'car))
2784 (apply #'format t "~8,'0X: ~S[~D]~%~10T~S~%" x))))
2788 ;;;; writing core file
2790 (defvar *core-file*)
2791 (defvar *data-page*)
2793 ;;; KLUDGE: These numbers correspond to values in core.h. If they're
2794 ;;; documented anywhere, I haven't found it. (I haven't tried very
2795 ;;; hard yet.) -- WHN 19990826
2796 (defparameter version-entry-type-code 3860)
2797 (defparameter validate-entry-type-code 3845)
2798 (defparameter directory-entry-type-code 3841)
2799 (defparameter new-directory-entry-type-code 3861)
2800 (defparameter initial-fun-entry-type-code 3863)
2801 (defparameter end-entry-type-code 3840)
2803 (declaim (ftype (function (sb!vm:word) sb!vm:word) write-word))
2804 (defun write-word (num)
2805 (ecase sb!c:*backend-byte-order*
2808 (write-byte (ldb (byte 8 (* i 8)) num) *core-file*)))
2811 (write-byte (ldb (byte 8 (* (- 3 i) 8)) num) *core-file*))))
2814 (defun advance-to-page ()
2815 (force-output *core-file*)
2816 (file-position *core-file*
2817 (round-up (file-position *core-file*)
2818 sb!c:*backend-page-size*)))
2820 (defun output-gspace (gspace)
2821 (force-output *core-file*)
2822 (let* ((posn (file-position *core-file*))
2823 (bytes (* (gspace-free-word-index gspace) sb!vm:n-word-bytes))
2824 (pages (ceiling bytes sb!c:*backend-page-size*))
2825 (total-bytes (* pages sb!c:*backend-page-size*)))
2827 (file-position *core-file*
2828 (* sb!c:*backend-page-size* (1+ *data-page*)))
2830 "writing ~S byte~:P [~S page~:P] from ~S~%"
2836 ;; Note: It is assumed that the GSPACE allocation routines always
2837 ;; allocate whole pages (of size *target-page-size*) and that any
2838 ;; empty gspace between the free pointer and the end of page will
2839 ;; be zero-filled. This will always be true under Mach on machines
2840 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
2842 (write-sequence (gspace-bytes gspace) *core-file* :end total-bytes)
2843 (force-output *core-file*)
2844 (file-position *core-file* posn)
2846 ;; Write part of a (new) directory entry which looks like this:
2847 ;; GSPACE IDENTIFIER
2852 (write-word (gspace-identifier gspace))
2853 (write-word (gspace-free-word-index gspace))
2854 (write-word *data-page*)
2855 (multiple-value-bind (floor rem)
2856 (floor (gspace-byte-address gspace) sb!c:*backend-page-size*)
2861 (incf *data-page* pages)))
2863 ;;; Create a core file created from the cold loaded image. (This is
2864 ;;; the "initial core file" because core files could be created later
2865 ;;; by executing SAVE-LISP in a running system, perhaps after we've
2866 ;;; added some functionality to the system.)
2867 (declaim (ftype (function (string)) write-initial-core-file))
2868 (defun write-initial-core-file (filename)
2870 (let ((filenamestring (namestring filename))
2874 "[building initial core file in ~S: ~%"
2878 (with-open-file (*core-file* filenamestring
2880 :element-type '(unsigned-byte 8)
2881 :if-exists :rename-and-delete)
2883 ;; Write the magic number.
2884 (write-word core-magic)
2886 ;; Write the Version entry.
2887 (write-word version-entry-type-code)
2889 (write-word sbcl-core-version-integer)
2891 ;; Write the New Directory entry header.
2892 (write-word new-directory-entry-type-code)
2893 (write-word 17) ; length = (5 words/space) * 3 spaces + 2 for header.
2895 (output-gspace *read-only*)
2896 (output-gspace *static*)
2897 (output-gspace *dynamic*)
2899 ;; Write the initial function.
2900 (write-word initial-fun-entry-type-code)
2902 (let* ((cold-name (cold-intern '!cold-init))
2903 (cold-fdefn (cold-fdefinition-object cold-name))
2904 (initial-fun (read-wordindexed cold-fdefn
2905 sb!vm:fdefn-fun-slot)))
2907 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
2908 (descriptor-bits initial-fun))
2909 (write-word (descriptor-bits initial-fun)))
2911 ;; Write the End entry.
2912 (write-word end-entry-type-code)
2915 (format t "done]~%")
2917 (/show "leaving WRITE-INITIAL-CORE-FILE")
2920 ;;;; the actual GENESIS function
2922 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
2923 ;;; and/or information about a Lisp core, therefrom.
2925 ;;; input file arguments:
2926 ;;; SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
2927 ;;; *tab* *characters* *converted* *to* *spaces*. (We push
2928 ;;; responsibility for removing tabs out to the caller it's
2929 ;;; trivial to remove them using UNIX command line tools like
2930 ;;; sed, whereas it's a headache to do it portably in Lisp because
2931 ;;; #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
2932 ;;; a core file cannot be built (but a C header file can be).
2934 ;;; output files arguments (any of which may be NIL to suppress output):
2935 ;;; CORE-FILE-NAME gets a Lisp core.
2936 ;;; C-HEADER-FILE-NAME gets a C header file, traditionally called
2937 ;;; internals.h, which is used by the C compiler when constructing
2938 ;;; the executable which will load the core.
2939 ;;; MAP-FILE-NAME gets (?) a map file. (dunno about this -- WHN 19990815)
2941 ;;; FIXME: GENESIS doesn't belong in SB!VM. Perhaps in %KERNEL for now,
2942 ;;; perhaps eventually in SB-LD or SB-BOOT.
2943 (defun sb!vm:genesis (&key
2945 symbol-table-file-name
2950 (when (and core-file-name
2951 (not symbol-table-file-name))
2952 (error "can't output a core file without symbol table file input"))
2955 "~&beginning GENESIS, ~A~%"
2957 ;; Note: This output summarizing what we're doing is
2958 ;; somewhat telegraphic in style, not meant to imply that
2959 ;; we're not e.g. also creating a header file when we
2961 (format nil "creating core ~S" core-file-name)
2962 (format nil "creating header ~S" c-header-file-name)))
2964 (let* ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
2966 ;; Read symbol table, if any.
2967 (when symbol-table-file-name
2968 (load-cold-foreign-symbol-table symbol-table-file-name))
2970 ;; Now that we've successfully read our only input file (by
2971 ;; loading the symbol table, if any), it's a good time to ensure
2972 ;; that there'll be someplace for our output files to go when
2974 (flet ((frob (filename)
2976 (ensure-directories-exist filename :verbose t))))
2977 (frob core-file-name)
2978 (frob map-file-name)
2979 (frob c-header-file-name))
2981 ;; (This shouldn't matter in normal use, since GENESIS normally
2982 ;; only runs once in any given Lisp image, but it could reduce
2983 ;; confusion if we ever experiment with running, tweaking, and
2984 ;; rerunning genesis interactively.)
2985 (do-all-symbols (sym)
2986 (remprop sym 'cold-intern-info))
2988 (let* ((*foreign-symbol-placeholder-value* (if core-file-name nil 0))
2989 (*load-time-value-counter* 0)
2990 (*cold-fdefn-objects* (make-hash-table :test 'equal))
2991 (*cold-symbols* (make-hash-table :test 'equal))
2992 (*cold-package-symbols* nil)
2993 (*read-only* (make-gspace :read-only
2995 sb!vm:read-only-space-start))
2996 (*static* (make-gspace :static
2998 sb!vm:static-space-start))
2999 (*dynamic* (make-gspace :dynamic
3001 #!+gencgc sb!vm:dynamic-space-start
3002 #!-gencgc sb!vm:dynamic-0-space-start))
3003 (*nil-descriptor* (make-nil-descriptor))
3004 (*current-reversed-cold-toplevels* *nil-descriptor*)
3005 (*unbound-marker* (make-other-immediate-descriptor
3007 sb!vm:unbound-marker-widetag))
3008 *cold-assembler-fixups*
3009 *cold-assembler-routines*
3010 #!+x86 *load-time-code-fixups*)
3012 ;; Prepare for cold load.
3013 (initialize-non-nil-symbols)
3014 (initialize-layouts)
3015 (initialize-static-fns)
3017 ;; Initialize the *COLD-SYMBOLS* system with the information
3018 ;; from package-data-list.lisp-expr and
3019 ;; common-lisp-exports.lisp-expr.
3021 ;; Why do things this way? Historically, the *COLD-SYMBOLS*
3022 ;; machinery was designed and implemented in CMU CL long before
3023 ;; I (WHN) ever heard of CMU CL. It dumped symbols and packages
3024 ;; iff they were used in the cold image. When I added the
3025 ;; package-data-list.lisp-expr mechanism, the idea was to
3026 ;; centralize all information about packages and exports. Thus,
3027 ;; it was the natural place for information even about packages
3028 ;; (such as SB!PCL and SB!WALKER) which aren't used much until
3029 ;; after cold load. This didn't quite match the CMU CL approach
3030 ;; of filling *COLD-SYMBOLS* with symbols which appear in the
3031 ;; cold image and then dumping only those symbols. By explicitly
3032 ;; putting all the symbols from package-data-list.lisp-expr and
3033 ;; from common-lisp-exports.lisp-expr into *COLD-SYMBOLS* here,
3034 ;; we feed our centralized symbol information into the old CMU
3035 ;; CL code without having to change the old CMU CL code too
3036 ;; much. (And the old CMU CL code is still useful for making
3037 ;; sure that the appropriate keywords and internal symbols end
3038 ;; up interned in the target Lisp, which is good, e.g. in order
3039 ;; to make &KEY arguments work right and in order to make
3040 ;; BACKTRACEs into target Lisp system code be legible.)
3041 (dolist (exported-name
3042 (sb-cold:read-from-file "common-lisp-exports.lisp-expr"))
3043 (cold-intern (intern exported-name *cl-package*)))
3044 (dolist (pd (sb-cold:read-from-file "package-data-list.lisp-expr"))
3045 (declare (type sb-cold:package-data pd))
3046 (let ((package (find-package (sb-cold:package-data-name pd))))
3047 (labels (;; Call FN on every node of the TREE.
3048 (mapc-on-tree (fn tree)
3050 (cons (mapc-on-tree fn (car tree))
3051 (mapc-on-tree fn (cdr tree)))
3052 (t (funcall fn tree)
3054 ;; Make sure that information about the association
3055 ;; between PACKAGE and the symbol named NAME gets
3056 ;; recorded in the cold-intern system or (as a
3057 ;; convenience when dealing with the tree structure
3058 ;; allowed in the PACKAGE-DATA-EXPORTS slot) do
3059 ;; nothing if NAME is NIL.
3062 (cold-intern (intern name package) package))))
3063 (mapc-on-tree #'chill (sb-cold:package-data-export pd))
3064 (mapc #'chill (sb-cold:package-data-reexport pd))
3065 (dolist (sublist (sb-cold:package-data-import-from pd))
3066 (destructuring-bind (package-name &rest symbol-names) sublist
3067 (declare (ignore package-name))
3068 (mapc #'chill symbol-names))))))
3071 (dolist (file-name object-file-names)
3072 (write-line (namestring file-name))
3073 (cold-load file-name))
3075 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3076 (resolve-assembler-fixups)
3077 #!+x86 (output-load-time-code-fixups)
3078 (linkage-info-to-core)
3080 (/show "back from FINISH-SYMBOLS")
3081 (finalize-load-time-value-noise)
3083 ;; Tell the target Lisp how much stuff we've allocated.
3084 (cold-set 'sb!vm:*read-only-space-free-pointer*
3085 (allocate-cold-descriptor *read-only*
3087 sb!vm:even-fixnum-lowtag))
3088 (cold-set 'sb!vm:*static-space-free-pointer*
3089 (allocate-cold-descriptor *static*
3091 sb!vm:even-fixnum-lowtag))
3092 (cold-set 'sb!vm:*initial-dynamic-space-free-pointer*
3093 (allocate-cold-descriptor *dynamic*
3095 sb!vm:even-fixnum-lowtag))
3096 (/show "done setting free pointers")
3098 ;; Write results to files.
3100 ;; FIXME: I dislike this approach of redefining
3101 ;; *STANDARD-OUTPUT* instead of putting the new stream in a
3102 ;; lexical variable, and it's annoying to have WRITE-MAP (to
3103 ;; *STANDARD-OUTPUT*) not be parallel to WRITE-INITIAL-CORE-FILE
3104 ;; (to a stream explicitly passed as an argument).
3106 (with-open-file (*standard-output* map-file-name
3108 :if-exists :supersede)
3110 (when c-header-file-name
3111 (with-open-file (*standard-output* c-header-file-name
3113 :if-exists :supersede)
3115 (when core-file-name
3116 (write-initial-core-file core-file-name)))))