1 ;;;; "cold" core image builder: This is how we create a target Lisp
2 ;;;; system from scratch, by converting from fasl files to an image
3 ;;;; file in the cross-compilation host, without the help of the
4 ;;;; target Lisp system.
6 ;;;; As explained by Rob MacLachlan on the CMU CL mailing list Wed, 06
7 ;;;; Jan 1999 11:05:02 -0500, this cold load generator more or less
8 ;;;; fakes up static function linking. I.e. it makes sure that all the
9 ;;;; DEFUN-defined functions in the fasl files it reads are bound to the
10 ;;;; corresponding symbols before execution starts. It doesn't do
11 ;;;; anything to initialize variable values; instead it just arranges
12 ;;;; for !COLD-INIT to be called at cold load time. !COLD-INIT is
13 ;;;; responsible for explicitly initializing anything which has to be
14 ;;;; initialized early before it transfers control to the ordinary
17 ;;;; (In CMU CL, and in SBCL as of 0.6.9 anyway, functions not defined
18 ;;;; by DEFUN aren't set up specially by GENESIS. In particular,
19 ;;;; structure slot accessors are not set up. Slot accessors are
20 ;;;; available at cold init time because they're usually compiled
21 ;;;; inline. They're not available as out-of-line functions until the
22 ;;;; toplevel forms installing them have run.)
24 ;;;; This software is part of the SBCL system. See the README file for
25 ;;;; more information.
27 ;;;; This software is derived from the CMU CL system, which was
28 ;;;; written at Carnegie Mellon University and released into the
29 ;;;; public domain. The software is in the public domain and is
30 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
31 ;;;; files for more information.
33 (in-package "SB!FASL")
35 ;;; a magic number used to identify our core files
36 (defconstant core-magic
37 (logior (ash (sb!xc
:char-code
#\S
) 24)
38 (ash (sb!xc
:char-code
#\B
) 16)
39 (ash (sb!xc
:char-code
#\C
) 8)
40 (sb!xc
:char-code
#\L
)))
42 (defun round-up (number size
)
43 "Round NUMBER up to be an integral multiple of SIZE."
44 (* size
(ceiling number size
)))
46 ;;;; implementing the concept of "vector" in (almost) portable
49 ;;;; "If you only need to do such simple things, it doesn't really
50 ;;;; matter which language you use." -- _ANSI Common Lisp_, p. 1, Paul
51 ;;;; Graham (evidently not considering the abstraction "vector" to be
52 ;;;; such a simple thing:-)
54 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
55 (defconstant +smallvec-length
+
58 ;;; an element of a BIGVEC -- a vector small enough that we have
59 ;;; a good chance of it being portable to other Common Lisps
61 `(simple-array (unsigned-byte 8) (,+smallvec-length
+)))
63 (defun make-smallvec ()
64 (make-array +smallvec-length
+ :element-type
'(unsigned-byte 8)
67 ;;; a big vector, implemented as a vector of SMALLVECs
69 ;;; KLUDGE: This implementation seems portable enough for our
70 ;;; purposes, since realistically every modern implementation is
71 ;;; likely to support vectors of at least 2^16 elements. But if you're
72 ;;; masochistic enough to read this far into the contortions imposed
73 ;;; on us by ANSI and the Lisp community, for daring to use the
74 ;;; abstraction of a large linearly addressable memory space, which is
75 ;;; after all only directly supported by the underlying hardware of at
76 ;;; least 99% of the general-purpose computers in use today, then you
77 ;;; may be titillated to hear that in fact this code isn't really
78 ;;; portable, because as of sbcl-0.7.4 we need somewhat more than
79 ;;; 16Mbytes to represent a core, and ANSI only guarantees that
80 ;;; ARRAY-DIMENSION-LIMIT is not less than 1024. -- WHN 2002-06-13
82 (outer-vector (vector (make-smallvec)) :type
(vector smallvec
)))
84 ;;; analogous to SVREF, but into a BIGVEC
85 (defun bvref (bigvec index
)
86 (multiple-value-bind (outer-index inner-index
)
87 (floor index
+smallvec-length
+)
89 (svref (bigvec-outer-vector bigvec
) outer-index
))
91 (defun (setf bvref
) (new-value bigvec index
)
92 (multiple-value-bind (outer-index inner-index
)
93 (floor index
+smallvec-length
+)
94 (setf (aref (the smallvec
95 (svref (bigvec-outer-vector bigvec
) outer-index
))
99 ;;; analogous to LENGTH, but for a BIGVEC
101 ;;; the length of BIGVEC, measured in the number of BVREFable bytes it
103 (defun bvlength (bigvec)
104 (* (length (bigvec-outer-vector bigvec
))
107 ;;; analogous to WRITE-SEQUENCE, but for a BIGVEC
108 (defun write-bigvec-as-sequence (bigvec stream
&key
(start 0) end pad-with-zeros
)
109 (let* ((bvlength (bvlength bigvec
))
110 (data-length (min (or end bvlength
) bvlength
)))
111 (loop for i of-type index from start below data-length do
112 (write-byte (bvref bigvec i
)
114 (when (and pad-with-zeros
(< bvlength data-length
))
115 (loop repeat
(- data-length bvlength
) do
(write-byte 0 stream
)))))
117 ;;; analogous to READ-SEQUENCE-OR-DIE, but for a BIGVEC
118 (defun read-bigvec-as-sequence-or-die (bigvec stream
&key
(start 0) end
)
119 (loop for i of-type index from start below
(or end
(bvlength bigvec
)) do
120 (setf (bvref bigvec i
)
121 (read-byte stream
))))
123 ;;; Grow BIGVEC (exponentially, so that large increases in size have
124 ;;; asymptotic logarithmic cost per byte).
125 (defun expand-bigvec (bigvec)
126 (let* ((old-outer-vector (bigvec-outer-vector bigvec
))
127 (length-old-outer-vector (length old-outer-vector
))
128 (new-outer-vector (make-array (* 2 length-old-outer-vector
))))
129 (dotimes (i length-old-outer-vector
)
130 (setf (svref new-outer-vector i
)
131 (svref old-outer-vector i
)))
132 (loop for i from length-old-outer-vector below
(length new-outer-vector
) do
133 (setf (svref new-outer-vector i
)
135 (setf (bigvec-outer-vector bigvec
)
139 ;;;; looking up bytes and multi-byte values in a BIGVEC (considering
140 ;;;; it as an image of machine memory on the cross-compilation target)
142 ;;; BVREF-32 and friends. These are like SAP-REF-n, except that
143 ;;; instead of a SAP we use a BIGVEC.
144 (macrolet ((make-bvref-n
146 (let* ((name (intern (format nil
"BVREF-~A" n
)))
147 (number-octets (/ n
8))
149 (loop for i from
0 to
(1- number-octets
)
150 collect
`(ash (bvref bigvec
(+ byte-index
,i
))
153 (loop for i from
0 to
(1- number-octets
)
154 collect
`(ash (bvref bigvec
156 ,(- number-octets
1 i
)))
159 (loop for i from
0 to
(1- number-octets
)
161 `((bvref bigvec
(+ byte-index
,i
))
162 (ldb (byte 8 ,(* i
8)) new-value
))))
164 (loop for i from
0 to
(1- number-octets
)
166 `((bvref bigvec
(+ byte-index
,i
))
167 (ldb (byte 8 ,(- n
8 (* i
8))) new-value
)))))
169 (defun ,name
(bigvec byte-index
)
170 (logior ,@(ecase sb
!c
:*backend-byte-order
*
171 (:little-endian ash-list-le
)
172 (:big-endian ash-list-be
))))
173 (defun (setf ,name
) (new-value bigvec byte-index
)
174 (setf ,@(ecase sb
!c
:*backend-byte-order
*
175 (:little-endian setf-list-le
)
176 (:big-endian setf-list-be
))))))))
182 ;; lispobj-sized word, whatever that may be
183 ;; hopefully nobody ever wants a 128-bit SBCL...
184 #!+#.
(cl:if
(cl:= 64 sb
!vm
:n-word-bits
) '(and) '(or))
186 (defun bvref-word (bytes index
)
187 (bvref-64 bytes index
))
188 (defun (setf bvref-word
) (new-val bytes index
)
189 (setf (bvref-64 bytes index
) new-val
)))
191 #!+#.
(cl:if
(cl:= 32 sb
!vm
:n-word-bits
) '(and) '(or))
193 (defun bvref-word (bytes index
)
194 (bvref-32 bytes index
))
195 (defun (setf bvref-word
) (new-val bytes index
)
196 (setf (bvref-32 bytes index
) new-val
)))
199 ;;;; representation of spaces in the core
201 ;;; If there is more than one dynamic space in memory (i.e., if a
202 ;;; copying GC is in use), then only the active dynamic space gets
205 (defconstant dynamic-core-space-id
1)
208 (defconstant static-core-space-id
2)
211 (defconstant read-only-core-space-id
3)
213 (defconstant max-core-space-id
3)
214 (defconstant deflated-core-space-id-flag
4)
216 (defconstant descriptor-low-bits
16
217 "the number of bits in the low half of the descriptor")
218 (defconstant target-space-alignment
(ash 1 descriptor-low-bits
)
219 "the alignment requirement for spaces in the target.
220 Must be at least (ASH 1 DESCRIPTOR-LOW-BITS)")
222 ;;; a GENESIS-time representation of a memory space (e.g. read-only
223 ;;; space, dynamic space, or static space)
224 (defstruct (gspace (:constructor %make-gspace
)
226 ;; name and identifier for this GSPACE
227 (name (missing-arg) :type symbol
:read-only t
)
228 (identifier (missing-arg) :type fixnum
:read-only t
)
229 ;; the word address where the data will be loaded
230 (word-address (missing-arg) :type unsigned-byte
:read-only t
)
231 ;; the data themselves. (Note that in CMU CL this was a pair of
232 ;; fields SAP and WORDS-ALLOCATED, but that wasn't very portable.)
233 ;; (And then in SBCL this was a VECTOR, but turned out to be
234 ;; unportable too, since ANSI doesn't think that arrays longer than
235 ;; 1024 (!) should needed by portable CL code...)
236 (bytes (make-bigvec) :read-only t
)
237 ;; the index of the next unwritten word (i.e. chunk of
238 ;; SB!VM:N-WORD-BYTES bytes) in BYTES, or equivalently the number of
239 ;; words actually written in BYTES. In order to convert to an actual
240 ;; index into BYTES, thus must be multiplied by SB!VM:N-WORD-BYTES.
243 (defun gspace-byte-address (gspace)
244 (ash (gspace-word-address gspace
) sb
!vm
:word-shift
))
246 (def!method print-object
((gspace gspace
) stream
)
247 (print-unreadable-object (gspace stream
:type t
)
248 (format stream
"~S" (gspace-name gspace
))))
250 (defun make-gspace (name identifier byte-address
)
251 (unless (zerop (rem byte-address target-space-alignment
))
252 (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
254 target-space-alignment
))
255 (%make-gspace
:name name
256 :identifier identifier
257 :word-address
(ash byte-address
(- sb
!vm
:word-shift
))))
259 ;;;; representation of descriptors
261 (defun is-fixnum-lowtag (lowtag)
262 (zerop (logand lowtag sb
!vm
:fixnum-tag-mask
)))
264 (defun is-other-immediate-lowtag (lowtag)
265 ;; The other-immediate lowtags are similar to the fixnum lowtags, in
266 ;; that they have an "effective length" that is shorter than is used
267 ;; for the pointer lowtags. Unlike the fixnum lowtags, however, the
268 ;; other-immediate lowtags are always effectively two bits wide.
269 (= (logand lowtag
3) sb
!vm
:other-immediate-0-lowtag
))
271 (defstruct (descriptor
272 (:constructor make-descriptor
273 (high low
&optional gspace word-offset
))
275 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
276 (gspace nil
:type
(or gspace
(eql :load-time-value
) null
))
277 ;; the offset in words from the start of GSPACE, or NIL if not set yet
278 (word-offset nil
:type
(or sb
!vm
:word null
))
279 ;; the high and low halves of the descriptor
281 ;; KLUDGE: Judging from the comments in genesis.lisp of the CMU CL
282 ;; old-rt compiler, this split dates back from a very early version
283 ;; of genesis where 32-bit integers were represented as conses of
284 ;; two 16-bit integers. In any system with nice (UNSIGNED-BYTE 32)
285 ;; structure slots, like CMU CL >= 17 or any version of SBCL, there
286 ;; seems to be no reason to persist in this. -- WHN 19990917
289 (def!method print-object
((des descriptor
) stream
)
290 (let ((lowtag (descriptor-lowtag des
)))
291 (print-unreadable-object (des stream
:type t
)
292 (cond ((is-fixnum-lowtag lowtag
)
293 (let ((unsigned (logior (ash (descriptor-high des
)
294 (1+ (- descriptor-low-bits
295 sb
!vm
:n-lowtag-bits
)))
296 (ash (descriptor-low des
)
297 (- 1 sb
!vm
:n-lowtag-bits
)))))
300 (if (> unsigned
#x1FFFFFFF
)
301 (- unsigned
#x40000000
)
303 ((is-other-immediate-lowtag lowtag
)
305 "for other immediate: #X~X, type #b~8,'0B"
306 (ash (descriptor-bits des
) (- sb
!vm
:n-widetag-bits
))
307 (logand (descriptor-low des
) sb
!vm
:widetag-mask
)))
310 "for pointer: #X~X, lowtag #b~3,'0B, ~A"
311 (logior (ash (descriptor-high des
) descriptor-low-bits
)
312 (logandc2 (descriptor-low des
) sb
!vm
:lowtag-mask
))
314 (let ((gspace (descriptor-gspace des
)))
319 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
320 ;;; free word index is boosted as necessary, and if additional memory
321 ;;; is needed, we grow the GSPACE. The descriptor returned is a
322 ;;; pointer of type LOWTAG.
323 (defun allocate-cold-descriptor (gspace length lowtag
)
324 (let* ((bytes (round-up length
(ash 1 sb
!vm
:n-lowtag-bits
)))
325 (old-free-word-index (gspace-free-word-index gspace
))
326 (new-free-word-index (+ old-free-word-index
327 (ash bytes
(- sb
!vm
:word-shift
)))))
328 ;; Grow GSPACE as necessary until it's big enough to handle
329 ;; NEW-FREE-WORD-INDEX.
331 ((>= (bvlength (gspace-bytes gspace
))
332 (* new-free-word-index sb
!vm
:n-word-bytes
)))
333 (expand-bigvec (gspace-bytes gspace
)))
334 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
335 (setf (gspace-free-word-index gspace
) new-free-word-index
)
336 (let ((ptr (+ (gspace-word-address gspace
) old-free-word-index
)))
337 (make-descriptor (ash ptr
(- sb
!vm
:word-shift descriptor-low-bits
))
338 (logior (ash (logand ptr
340 (- descriptor-low-bits
345 old-free-word-index
))))
347 (defun descriptor-lowtag (des)
348 "the lowtag bits for DES"
349 (logand (descriptor-low des
) sb
!vm
:lowtag-mask
))
351 (defun descriptor-bits (des)
352 (logior (ash (descriptor-high des
) descriptor-low-bits
)
353 (descriptor-low des
)))
355 (defun descriptor-fixnum (des)
356 (let ((bits (descriptor-bits des
)))
357 (if (logbitp (1- sb
!vm
:n-word-bits
) bits
)
358 ;; KLUDGE: The (- SB!VM:N-WORD-BITS 2) term here looks right to
359 ;; me, and it works, but in CMU CL it was (1- SB!VM:N-WORD-BITS),
360 ;; and although that doesn't make sense for me, or work for me,
361 ;; it's hard to see how it could have been wrong, since CMU CL
362 ;; genesis worked. It would be nice to understand how this came
363 ;; to be.. -- WHN 19990901
364 (logior (ash bits
(- sb
!vm
:n-fixnum-tag-bits
))
365 (ash -
1 (1+ sb
!vm
:n-positive-fixnum-bits
)))
366 (ash bits
(- sb
!vm
:n-fixnum-tag-bits
)))))
368 (defun descriptor-word-sized-integer (des)
369 ;; Extract an (unsigned-byte 32), from either its fixnum or bignum
371 (let ((lowtag (descriptor-lowtag des
)))
372 (if (is-fixnum-lowtag lowtag
)
373 (make-random-descriptor (descriptor-fixnum des
))
374 (read-wordindexed des
1))))
377 (defun descriptor-bytes (des)
378 (gspace-bytes (descriptor-intuit-gspace des
)))
379 (defun descriptor-byte-offset (des)
380 (ash (descriptor-word-offset des
) sb
!vm
:word-shift
))
382 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
383 ;;; figure out a GSPACE which corresponds to DES, set it into
384 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
385 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
386 (declaim (ftype (function (descriptor) gspace
) descriptor-intuit-gspace
))
387 (defun descriptor-intuit-gspace (des)
388 (or (descriptor-gspace des
)
390 ;; gspace wasn't set, now we have to search for it.
391 (let ((lowtag (descriptor-lowtag des
))
392 (high (descriptor-high des
))
393 (low (descriptor-low des
)))
395 ;; Non-pointer objects don't have a gspace.
396 (unless (or (eql lowtag sb
!vm
:fun-pointer-lowtag
)
397 (eql lowtag sb
!vm
:instance-pointer-lowtag
)
398 (eql lowtag sb
!vm
:list-pointer-lowtag
)
399 (eql lowtag sb
!vm
:other-pointer-lowtag
))
400 (error "don't even know how to look for a GSPACE for ~S" des
))
402 (dolist (gspace (list *dynamic
* *static
* *read-only
*)
403 (error "couldn't find a GSPACE for ~S" des
))
404 ;; Bounds-check the descriptor against the allocated area
405 ;; within each gspace.
407 ;; Most of the faffing around in here involving ash and
408 ;; various computed shift counts is due to the high/low
409 ;; split representation of the descriptor bits and an
410 ;; apparent disinclination to create intermediate values
411 ;; larger than a target fixnum.
413 ;; This code relies on the fact that GSPACEs are aligned
414 ;; such that the descriptor-low-bits low bits are zero.
415 (when (and (>= high
(ash (gspace-word-address gspace
)
416 (- sb
!vm
:word-shift descriptor-low-bits
)))
417 (<= high
(ash (+ (gspace-word-address gspace
)
418 (gspace-free-word-index gspace
))
419 (- sb
!vm
:word-shift descriptor-low-bits
))))
420 ;; Update the descriptor with the correct gspace and the
421 ;; offset within the gspace and return the gspace.
422 (setf (descriptor-gspace des
) gspace
)
423 (setf (descriptor-word-offset des
)
424 (+ (ash (- high
(ash (gspace-word-address gspace
)
426 descriptor-low-bits
)))
427 (- descriptor-low-bits sb
!vm
:word-shift
))
428 (ash (logandc2 low sb
!vm
:lowtag-mask
)
429 (- sb
!vm
:word-shift
))))
432 (defun make-random-descriptor (value)
433 (make-descriptor (logand (ash value
(- descriptor-low-bits
))
436 descriptor-low-bits
))))
437 (logand value
(1- (ash 1 descriptor-low-bits
)))))
439 (defun make-fixnum-descriptor (num)
440 (when (>= (integer-length num
)
441 (- sb
!vm
:n-word-bits sb
!vm
:n-fixnum-tag-bits
))
442 (error "~W is too big for a fixnum." num
))
443 (make-random-descriptor (ash num sb
!vm
:n-fixnum-tag-bits
)))
445 (defun make-other-immediate-descriptor (data type
)
446 (make-descriptor (ash data
(- sb
!vm
:n-widetag-bits descriptor-low-bits
))
447 (logior (logand (ash data
(- descriptor-low-bits
448 sb
!vm
:n-widetag-bits
))
449 (1- (ash 1 descriptor-low-bits
)))
452 (defun make-character-descriptor (data)
453 (make-other-immediate-descriptor data sb
!vm
:character-widetag
))
455 (defun descriptor-beyond (des offset type
)
456 (let* ((low (logior (+ (logandc2 (descriptor-low des
) sb
!vm
:lowtag-mask
)
459 (high (+ (descriptor-high des
)
460 (ash low
(- descriptor-low-bits
)))))
461 (make-descriptor high
(logand low
(1- (ash 1 descriptor-low-bits
))))))
463 ;;;; miscellaneous variables and other noise
465 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
466 ;;; undefined foreign symbols are to be treated as an error.
467 ;;; (In the first pass of GENESIS, needed to create a header file before
468 ;;; the C runtime can be built, various foreign symbols will necessarily
469 ;;; be undefined, but we don't need actual values for them anyway, and
470 ;;; we can just use 0 or some other placeholder. In the second pass of
471 ;;; GENESIS, all foreign symbols should be defined, so any undefined
472 ;;; foreign symbol is a problem.)
474 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
475 ;;; never tries to look up foreign symbols in the first place unless
476 ;;; it's actually creating a core file (as in the second pass) instead
477 ;;; of using this hack to allow it to go through the motions without
478 ;;; causing an error. -- WHN 20000825
479 (defvar *foreign-symbol-placeholder-value
*)
481 ;;; a handle on the trap object
482 (defvar *unbound-marker
*)
483 ;; was: (make-other-immediate-descriptor 0 sb!vm:unbound-marker-widetag)
485 ;;; a handle on the NIL object
486 (defvar *nil-descriptor
*)
488 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
489 ;;; when the target Lisp starts up
491 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
492 ;;; loadtime value, represented by (CONS KEYWORD ..). The FILENAME
493 ;;; tells which fasl file each list element came from, for debugging
495 (defvar *current-reversed-cold-toplevels
*)
497 ;;; the head of a list of DEBUG-SOURCEs which need to be patched when
498 ;;; the cold core starts up
499 (defvar *current-debug-sources
*)
501 ;;; foreign symbol references
502 (defparameter *cold-foreign-undefined-symbols
* nil
)
504 ;;; the name of the object file currently being cold loaded (as a string, not a
505 ;;; pathname), or NIL if we're not currently cold loading any object file
506 (defvar *cold-load-filename
* nil
)
507 (declaim (type (or string null
) *cold-load-filename
*))
509 ;;;; miscellaneous stuff to read and write the core memory
511 ;;; FIXME: should be DEFINE-MODIFY-MACRO
512 (defmacro cold-push
(thing list
)
513 "Push THING onto the given cold-load LIST."
514 `(setq ,list
(cold-cons ,thing
,list
)))
516 (declaim (ftype (function (descriptor sb
!vm
:word
) descriptor
) read-wordindexed
))
517 (macrolet ((read-bits ()
518 `(let ((gspace (descriptor-intuit-gspace address
)))
519 (bvref-word (gspace-bytes gspace
)
520 (ash (+ index
(descriptor-word-offset address
))
521 sb
!vm
:word-shift
)))))
522 (defun read-bits-wordindexed (address index
)
524 (defun read-wordindexed (address index
)
525 "Return the value which is displaced by INDEX words from ADDRESS."
526 (make-random-descriptor (read-bits))))
528 (declaim (ftype (function (descriptor) descriptor
) read-memory
))
529 (defun read-memory (address)
530 "Return the value at ADDRESS."
531 (read-wordindexed address
0))
533 ;;; (Note: In CMU CL, this function expected a SAP-typed ADDRESS
534 ;;; value, instead of the object-and-offset we use here.)
535 (declaim (ftype (function (descriptor sb
!vm
:word descriptor
) (values))
536 note-load-time-value-reference
))
537 (defun note-load-time-value-reference (address offset marker
)
538 (cold-push (cold-cons
539 (cold-intern :load-time-value-fixup
)
541 (cold-cons (number-to-core offset
)
543 (number-to-core (descriptor-word-offset marker
))
545 *current-reversed-cold-toplevels
*)
548 (declaim (ftype (function (descriptor sb
!vm
:word
(or symbol descriptor
))) write-wordindexed
))
549 (defun write-wordindexed (address index value
)
550 "Write VALUE displaced INDEX words from ADDRESS."
551 ;; If we're passed a symbol as a value then it needs to be interned.
552 (let ((value (cond ((symbolp value
) (cold-intern value
))
554 (if (eql (descriptor-gspace value
) :load-time-value
)
555 (note-load-time-value-reference address
556 (- (ash index sb
!vm
:word-shift
)
557 (logand (descriptor-bits address
)
560 (let* ((bytes (gspace-bytes (descriptor-intuit-gspace address
)))
561 (byte-index (ash (+ index
(descriptor-word-offset address
))
563 (setf (bvref-word bytes byte-index
) (descriptor-bits value
))))))
565 (declaim (ftype (function (descriptor (or symbol descriptor
))) write-memory
))
566 (defun write-memory (address value
)
567 "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
568 (write-wordindexed address
0 value
))
570 ;;;; allocating images of primitive objects in the cold core
572 (defun write-header-word (des header-data widetag
)
573 (write-memory des
(make-other-immediate-descriptor header-data widetag
)))
575 ;;; There are three kinds of blocks of memory in the type system:
576 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
577 ;;; header as all slots are descriptors.
578 ;;; * Unboxed objects (bignums): There is a single header word that contains
580 ;;; * Vector objects: There is a header word with the type, then a word for
581 ;;; the length, then the data.
582 (defun allocate-object (gspace length lowtag
)
583 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
585 (allocate-cold-descriptor gspace
(ash length sb
!vm
:word-shift
) lowtag
))
586 (defun allocate-header+object
(gspace length widetag
)
587 "Allocate LENGTH words plus a header word in GSPACE and
588 return an ``other-pointer'' descriptor to them. Initialize the header word
589 with the resultant length and WIDETAG."
590 (let ((des (allocate-cold-descriptor gspace
591 (ash (1+ length
) sb
!vm
:word-shift
)
592 sb
!vm
:other-pointer-lowtag
)))
593 (write-header-word des length widetag
)
595 (defun allocate-vector-object (gspace element-bits length widetag
)
596 "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
597 GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
598 header word with WIDETAG and the length slot with LENGTH."
599 ;; ALLOCATE-COLD-DESCRIPTOR will take any rational number of bytes
600 ;; and round up to a double-word. This doesn't need to use CEILING.
601 (let* ((bytes (/ (* element-bits length
) sb
!vm
:n-byte-bits
))
602 (des (allocate-cold-descriptor gspace
603 (+ bytes
(* 2 sb
!vm
:n-word-bytes
))
604 sb
!vm
:other-pointer-lowtag
)))
605 (write-header-word des
0 widetag
)
606 (write-wordindexed des
607 sb
!vm
:vector-length-slot
608 (make-fixnum-descriptor length
))
611 ;; Make a structure and set the header word and layout.
612 ;; LAYOUT-LENGTH is as returned by the like-named function.
613 (defun allocate-structure-object (gspace layout-length layout
)
614 ;; The math in here is best illustrated by two examples:
615 ;; even: size 4 => request to allocate 5 => rounds up to 6, logior => 5
616 ;; odd : size 5 => request to allocate 6 => no rounding up, logior => 5
617 ;; In each case, the length of the memory block is even.
618 ;; ALLOCATE-OBJECT performs the rounding. It must be supplied
619 ;; the number of words minimally needed, counting the header itself.
620 ;; The number written into the header (%INSTANCE-LENGTH) is always odd.
621 (let ((des (allocate-object gspace
(1+ layout-length
)
622 sb
!vm
:instance-pointer-lowtag
)))
623 (write-header-word des
(logior layout-length
1)
624 sb
!vm
:instance-header-widetag
)
625 (write-wordindexed des sb
!vm
:instance-slots-offset layout
)
628 ;;;; copying simple objects into the cold core
630 (defun base-string-to-core (string &optional
(gspace *dynamic
*))
631 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
632 core and return a descriptor to it."
633 ;; (Remember that the system convention for storage of strings leaves an
634 ;; extra null byte at the end to aid in call-out to C.)
635 (let* ((length (length string
))
636 (des (allocate-vector-object gspace
639 sb
!vm
:simple-base-string-widetag
))
640 (bytes (gspace-bytes gspace
))
641 (offset (+ (* sb
!vm
:vector-data-offset sb
!vm
:n-word-bytes
)
642 (descriptor-byte-offset des
))))
643 (write-wordindexed des
644 sb
!vm
:vector-length-slot
645 (make-fixnum-descriptor length
))
647 (setf (bvref bytes
(+ offset i
))
648 (sb!xc
:char-code
(aref string i
))))
649 (setf (bvref bytes
(+ offset length
))
650 0) ; null string-termination character for C
653 (defun base-string-from-core (descriptor)
654 (let* ((len (descriptor-fixnum
655 (read-wordindexed descriptor sb
!vm
:vector-length-slot
)))
656 (str (make-string len
))
657 (bytes (gspace-bytes (descriptor-gspace descriptor
))))
660 (code-char (bvref bytes
661 (+ (descriptor-byte-offset descriptor
)
662 (* sb
!vm
:vector-data-offset sb
!vm
:n-word-bytes
)
665 (defun bignum-to-core (n)
666 "Copy a bignum to the cold core."
667 (let* ((words (ceiling (1+ (integer-length n
)) sb
!vm
:n-word-bits
))
669 (allocate-header+object
*dynamic
* words sb
!vm
:bignum-widetag
)))
670 (declare (fixnum words
))
671 (do ((index 1 (1+ index
))
672 (remainder n
(ash remainder
(- sb
!vm
:n-word-bits
))))
674 (unless (zerop (integer-length remainder
))
675 ;; FIXME: Shouldn't this be a fatal error?
676 (warn "~W words of ~W were written, but ~W bits were left over."
678 (let ((word (ldb (byte sb
!vm
:n-word-bits
0) remainder
)))
679 ;; FIXME: this is disgusting. there should be WRITE-BITS-WORDINDEXED.
680 (write-wordindexed handle index
681 (make-descriptor (ash word
(- descriptor-low-bits
))
682 (ldb (byte descriptor-low-bits
0)
686 (defun bignum-from-core (descriptor)
687 (let ((n-words (ash (descriptor-bits (read-memory descriptor
))
688 (- sb
!vm
:n-widetag-bits
)))
690 (dotimes (i n-words val
)
691 (let ((bits (read-bits-wordindexed descriptor
692 (+ i sb
!vm
:bignum-digits-offset
))))
693 ;; sign-extend the highest word
694 (when (and (= i
(1- n-words
)) (logbitp (1- sb
!vm
:n-word-bits
) bits
))
695 (setq bits
(dpb bits
(byte sb
!vm
:n-word-bits
0) -
1)))
696 (setq val
(logior (ash bits
(* i sb
!vm
:n-word-bits
)) val
))))))
698 (defun number-pair-to-core (first second type
)
699 "Makes a number pair of TYPE (ratio or complex) and fills it in."
700 (let ((des (allocate-header+object
*dynamic
* 2 type
)))
701 (write-wordindexed des
1 first
)
702 (write-wordindexed des
2 second
)
705 (defun write-double-float-bits (address index x
)
706 (let ((hi (double-float-high-bits x
))
707 (lo (double-float-low-bits x
)))
708 (ecase sb
!vm
::n-word-bits
710 (let ((high-bits (make-random-descriptor hi
))
711 (low-bits (make-random-descriptor lo
)))
712 (ecase sb
!c
:*backend-byte-order
*
714 (write-wordindexed address index low-bits
)
715 (write-wordindexed address
(1+ index
) high-bits
))
717 (write-wordindexed address index high-bits
)
718 (write-wordindexed address
(1+ index
) low-bits
)))))
720 (let ((bits (make-random-descriptor
721 (ecase sb
!c
:*backend-byte-order
*
722 (:little-endian
(logior lo
(ash hi
32)))
724 #+nil
(:big-endian
(logior (logand hi
#xffffffff
)
726 (write-wordindexed address index bits
))))
729 (defun float-to-core (x)
732 ;; 64-bit platforms have immediate single-floats.
733 #!+#.
(cl:if
(cl:= sb
!vm
:n-word-bits
64) '(and) '(or))
734 (make-random-descriptor (logior (ash (single-float-bits x
) 32)
735 sb
!vm
::single-float-widetag
))
736 #!-
#.
(cl:if
(cl:= sb
!vm
:n-word-bits
64) '(and) '(or))
737 (let ((des (allocate-header+object
*dynamic
*
738 (1- sb
!vm
:single-float-size
)
739 sb
!vm
:single-float-widetag
)))
740 (write-wordindexed des
741 sb
!vm
:single-float-value-slot
742 (make-random-descriptor (single-float-bits x
)))
745 (let ((des (allocate-header+object
*dynamic
*
746 (1- sb
!vm
:double-float-size
)
747 sb
!vm
:double-float-widetag
)))
748 (write-double-float-bits des sb
!vm
:double-float-value-slot x
)))))
750 (defun complex-single-float-to-core (num)
751 (declare (type (complex single-float
) num
))
752 (let ((des (allocate-header+object
*dynamic
*
753 (1- sb
!vm
:complex-single-float-size
)
754 sb
!vm
:complex-single-float-widetag
)))
757 (write-wordindexed des sb
!vm
:complex-single-float-real-slot
758 (make-random-descriptor (single-float-bits (realpart num
))))
759 (write-wordindexed des sb
!vm
:complex-single-float-imag-slot
760 (make-random-descriptor (single-float-bits (imagpart num
)))))
762 (write-wordindexed des sb
!vm
:complex-single-float-data-slot
763 (make-random-descriptor
764 (logior (ldb (byte 32 0) (single-float-bits (realpart num
)))
765 (ash (single-float-bits (imagpart num
)) 32))))
768 (defun complex-double-float-to-core (num)
769 (declare (type (complex double-float
) num
))
770 (let ((des (allocate-header+object
*dynamic
*
771 (1- sb
!vm
:complex-double-float-size
)
772 sb
!vm
:complex-double-float-widetag
)))
773 (write-double-float-bits des sb
!vm
:complex-double-float-real-slot
775 (write-double-float-bits des sb
!vm
:complex-double-float-imag-slot
778 ;;; Copy the given number to the core.
779 (defun number-to-core (number)
781 (integer (if (< (integer-length number
)
782 (- sb
!vm
:n-word-bits sb
!vm
:n-fixnum-tag-bits
))
783 (make-fixnum-descriptor number
)
784 (bignum-to-core number
)))
785 (ratio (number-pair-to-core (number-to-core (numerator number
))
786 (number-to-core (denominator number
))
787 sb
!vm
:ratio-widetag
))
788 ((complex single-float
) (complex-single-float-to-core number
))
789 ((complex double-float
) (complex-double-float-to-core number
))
791 ((complex long-float
)
792 (error "~S isn't a cold-loadable number at all!" number
))
793 (complex (number-pair-to-core (number-to-core (realpart number
))
794 (number-to-core (imagpart number
))
795 sb
!vm
:complex-widetag
))
796 (float (float-to-core number
))
797 (t (error "~S isn't a cold-loadable number at all!" number
))))
799 (declaim (ftype (function (sb!vm
:word
) descriptor
) sap-int-to-core
))
800 (defun sap-int-to-core (sap-int)
801 (let ((des (allocate-header+object
*dynamic
* (1- sb
!vm
:sap-size
)
803 (write-wordindexed des
804 sb
!vm
:sap-pointer-slot
805 (make-random-descriptor sap-int
))
808 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
809 (defun cold-cons (car cdr
&optional
(gspace *dynamic
*))
810 (let ((dest (allocate-object gspace
2 sb
!vm
:list-pointer-lowtag
)))
811 (write-memory dest car
)
812 (write-wordindexed dest
1 cdr
)
815 ;;; Make a simple-vector on the target that holds the specified
816 ;;; OBJECTS, and return its descriptor.
817 ;;; This is really "vectorify-list-into-core" but that's too wordy,
818 ;;; so historically it was "vector-in-core" which is a fine name.
819 (defun vector-in-core (objects &optional
(gspace *dynamic
*))
820 (let* ((size (length objects
))
821 (result (allocate-vector-object gspace sb
!vm
:n-word-bits size
822 sb
!vm
:simple-vector-widetag
)))
823 (dotimes (index size
)
824 (write-wordindexed result
(+ index sb
!vm
:vector-data-offset
)
828 (defun vector-from-core (descriptor transform
)
829 (let* ((len (descriptor-fixnum
830 (read-wordindexed descriptor sb
!vm
:vector-length-slot
)))
831 (vector (make-array len
)))
832 (dotimes (i len vector
)
833 (setf (aref vector i
)
835 (read-wordindexed descriptor
836 (+ sb
!vm
:vector-data-offset i
)))))))
840 ;; Simulate *FREE-TLS-INDEX*. This is a count, not a displacement.
841 ;; In C, sizeof counts 1 word for the variable-length interrupt_contexts[]
842 ;; but primitive-object-size counts 0, so add 1, though in fact the C code
843 ;; implies that it might have overcounted by 1. We could make this agnostic
844 ;; of MAX-INTERRUPTS by moving the thread base register up by TLS-SIZE words,
845 ;; using negative offsets for all dynamically assigned indices.
846 (defvar *genesis-tls-counter
*
847 (+ 1 sb
!vm
::max-interrupts
848 (sb!vm
:primitive-object-size
849 (find 'sb
!vm
::thread sb
!vm
:*primitive-objects
*
850 :key
#'sb
!vm
:primitive-object-name
))))
854 ;; Assign SYMBOL the tls-index INDEX. SYMBOL must be a descriptor.
855 ;; This is a backend support routine, but the style within this file
856 ;; is to conditionalize by the target features.
857 (defun cold-assign-tls-index (symbol index
)
860 (logior (ash index
32)
861 (descriptor-bits (read-wordindexed symbol
0)))))
862 (write-wordindexed symbol
0 (make-random-descriptor header-word
)))
864 (write-wordindexed symbol sb
!vm
:symbol-tls-index-slot
865 (make-random-descriptor index
)))
867 ;; Return SYMBOL's tls-index,
868 ;; choosing a new index if it doesn't have one yet.
869 (defun ensure-symbol-tls-index (symbol)
870 (let* ((cold-sym (cold-intern symbol
))
873 (ldb (byte 32 32) (descriptor-bits (read-wordindexed cold-sym
0)))
876 (read-wordindexed cold-sym sb
!vm
:symbol-tls-index-slot
))))
877 (unless (plusp tls-index
)
878 (let ((next (prog1 *genesis-tls-counter
* (incf *genesis-tls-counter
*))))
879 (setq tls-index
(ash next sb
!vm
:word-shift
))
880 (cold-assign-tls-index cold-sym tls-index
)))
883 ;; A table of special variable names which get known TLS indices.
884 ;; Some of them are mapped onto 'struct thread' and have pre-determined offsets.
885 ;; Others are static symbols used with bind_variable() in the C runtime,
886 ;; and might not, in the absence of this table, get an index assigned by genesis
887 ;; depending on whether the cross-compiler used the BIND vop on them.
888 ;; Indices for those static symbols can be chosen arbitrarily, which is to say
889 ;; the value doesn't matter but must update the tls-counter correctly.
890 ;; All symbols other than the ones in this table get the indices assigned
891 ;; by the fasloader on demand.
893 (defvar *known-tls-symbols
*
894 ;; FIXME: no mechanism exists to determine which static symbols C code will
895 ;; dynamically bind. TLS is a finite resource, and wasting indices for all
896 ;; static symbols isn't the best idea. This list was hand-made with 'grep'.
897 '(sb!vm
:*alloc-signal
*
898 sb
!sys
:*allow-with-interrupts
*
899 sb
!vm
:*current-catch-block
*
900 sb
!vm
::*current-unwind-protect-block
*
901 sb
!kernel
:*free-interrupt-context-index
*
902 sb
!kernel
:*gc-inhibit
*
903 sb
!kernel
:*gc-pending
*
905 sb
!impl
::*in-safepoint
*
906 sb
!sys
:*interrupt-pending
*
907 sb
!sys
:*interrupts-enabled
*
908 sb
!vm
::*pinned-objects
*
909 sb
!kernel
:*restart-clusters
*
910 sb
!kernel
:*stop-for-gc-pending
*
912 sb
!sys
:*thruption-pending
*))
914 ;;; Allocate (and initialize) a symbol.
915 (defun allocate-symbol (name &key
(gspace *dynamic
*))
916 (declare (simple-string name
))
917 (let ((symbol (allocate-header+object gspace
(1- sb
!vm
:symbol-size
)
918 sb
!vm
:symbol-header-widetag
)))
919 (write-wordindexed symbol sb
!vm
:symbol-value-slot
*unbound-marker
*)
920 (write-wordindexed symbol sb
!vm
:symbol-hash-slot
(make-fixnum-descriptor 0))
921 (write-wordindexed symbol sb
!vm
:symbol-info-slot
*nil-descriptor
*)
922 (write-wordindexed symbol sb
!vm
:symbol-name-slot
923 (base-string-to-core name
*dynamic
*))
924 (write-wordindexed symbol sb
!vm
:symbol-package-slot
*nil-descriptor
*)
928 (defun assign-tls-index (symbol cold-symbol
)
929 (let ((index (info :variable
:wired-tls symbol
)))
930 (cond ((integerp index
) ; thread slot
931 (cold-assign-tls-index cold-symbol index
))
932 ((memq symbol
*known-tls-symbols
*)
933 ;; symbols without which the C runtime could not start
934 (shiftf index
*genesis-tls-counter
* (1+ *genesis-tls-counter
*))
935 (cold-assign-tls-index cold-symbol
(ash index sb
!vm
:word-shift
))))))
937 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
938 ;;; descriptor of a cold symbol or (in an abbreviation for the
939 ;;; most common usage pattern) an ordinary symbol, which will be
940 ;;; automatically cold-interned.
941 (declaim (ftype (function ((or symbol descriptor
) descriptor
)) cold-set
))
942 (defun cold-set (symbol-or-symbol-des value
)
943 (let ((symbol-des (etypecase symbol-or-symbol-des
944 (descriptor symbol-or-symbol-des
)
945 (symbol (cold-intern symbol-or-symbol-des
)))))
946 (write-wordindexed symbol-des sb
!vm
:symbol-value-slot value
)))
948 ;;;; layouts and type system pre-initialization
950 ;;; Since we want to be able to dump structure constants and
951 ;;; predicates with reference layouts, we need to create layouts at
952 ;;; cold-load time. We use the name to intern layouts by, and dump a
953 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
954 ;;; initialization can find them. The only thing that's tricky [sic --
955 ;;; WHN 19990816] is initializing layout's layout, which must point to
958 ;;; a map from class names to lists of
959 ;;; `(,descriptor ,name ,length ,inherits ,depth)
960 ;;; KLUDGE: It would be more understandable and maintainable to use
961 ;;; DEFSTRUCT (:TYPE LIST) here. -- WHN 19990823
962 (defvar *cold-layouts
* (make-hash-table :test
'equal
))
964 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
966 (defvar *cold-layout-names
* (make-hash-table :test
'eql
))
968 ;;; FIXME: *COLD-LAYOUTS* and *COLD-LAYOUT-NAMES* should be
969 ;;; initialized by binding in GENESIS.
971 ;;; the descriptor for layout's layout (needed when making layouts)
972 (defvar *layout-layout
*)
973 ;;; the descriptor for PACKAGE's layout (needed when making packages)
974 (defvar *package-layout
*)
976 (defconstant target-layout-length
977 ;; LAYOUT-LENGTH counts the number of words in an instance,
978 ;; including the layout itself as 1 word
979 (layout-length (find-layout 'layout
)))
981 (defun target-layout-index (slot-name)
982 ;; KLUDGE: this is a little bit sleazy, but the tricky thing is that
983 ;; structure slots don't have a terribly firm idea of their names.
984 ;; At least here if we change LAYOUT's package of definition, we
985 ;; only have to change one thing...
986 (let* ((name (find-symbol (symbol-name slot-name
) "SB!KERNEL"))
987 (layout (find-layout 'layout
))
988 (dd (layout-info layout
))
989 (slots (dd-slots dd
))
990 (dsd (find name slots
:key
#'dsd-name
)))
994 (defun cold-set-layout-slot (cold-layout slot-name value
)
997 (+ sb
!vm
:instance-slots-offset
(target-layout-index slot-name
))
1000 ;;; Return a list of names created from the cold layout INHERITS data
1002 (defun listify-cold-inherits (x)
1003 (let ((len (descriptor-fixnum (read-wordindexed x
1004 sb
!vm
:vector-length-slot
))))
1006 (dotimes (index len
)
1007 (let* ((des (read-wordindexed x
(+ sb
!vm
:vector-data-offset index
)))
1008 (found (gethash (descriptor-bits des
) *cold-layout-names
*)))
1011 (error "unknown descriptor at index ~S (bits = ~8,'0X)"
1013 (descriptor-bits des
)))))
1016 (defvar *simple-vector-0-descriptor
*)
1017 (defvar *vacuous-slot-table
*)
1018 (declaim (ftype (function (symbol descriptor descriptor descriptor descriptor
)
1021 (defun make-cold-layout (name length inherits depthoid metadata
)
1022 (let ((result (allocate-structure-object *dynamic
*
1023 target-layout-length
1025 ;; Don't set the CLOS hash value: done in cold-init instead.
1027 ;; Set other slot values.
1029 ;; leave CLASSOID uninitialized for now
1030 (cold-set-layout-slot result
'invalid
*nil-descriptor
*)
1031 (cold-set-layout-slot result
'inherits inherits
)
1032 (cold-set-layout-slot result
'depthoid depthoid
)
1033 (cold-set-layout-slot result
'length length
)
1034 (cold-set-layout-slot result
'info
*nil-descriptor
*)
1035 (cold-set-layout-slot result
'pure
*nil-descriptor
*)
1036 #!-interleaved-raw-slots
1037 (cold-set-layout-slot result
'n-untagged-slots metadata
)
1038 #!+interleaved-raw-slots
1040 (cold-set-layout-slot result
'untagged-bitmap metadata
)
1041 ;; Nothing in cold-init needs to call EQUALP on a structure with raw slots,
1042 ;; but for type-correctness this slot needs to be a simple-vector.
1043 (unless (boundp '*simple-vector-0-descriptor
*)
1044 (setq *simple-vector-0-descriptor
* (vector-in-core nil
)))
1045 (cold-set-layout-slot result
'equalp-tests
*simple-vector-0-descriptor
*))
1046 (cold-set-layout-slot result
'source-location
*nil-descriptor
*)
1047 (cold-set-layout-slot result
'%for-std-class-b
(make-fixnum-descriptor 0))
1048 (cold-set-layout-slot result
'slot-list
*nil-descriptor
*)
1050 (when (member name
'(null list symbol
))
1051 ;; Assign an empty slot-table. Why this is done only for three
1052 ;; classoids is ... too complicated to explain here in a few words,
1053 ;; but revision 18c239205d9349abc017b07e7894a710835c5205 broke it.
1054 ;; Keep this in sync with MAKE-SLOT-TABLE in pcl/slots-boot.
1055 (unless (boundp '*vacuous-slot-table
*)
1056 (setq *vacuous-slot-table
*
1057 (host-constant-to-core '#(1 nil
))))
1058 (cold-set-layout-slot result
'slot-table
*vacuous-slot-table
*))
1060 (setf (gethash name
*cold-layouts
*)
1063 (descriptor-fixnum length
)
1064 (listify-cold-inherits inherits
)
1065 (descriptor-fixnum depthoid
)
1066 (descriptor-fixnum metadata
)))
1067 (setf (gethash (descriptor-bits result
) *cold-layout-names
*) name
)
1071 ;; This is called to backpatch two small sets of objects:
1072 ;; - layouts which are made before layout-of-layout is made (4 of them)
1073 ;; - packages, which are made before layout-of-package is made (all of them)
1074 (defun patch-instance-layout (thing layout
)
1075 ;; Layout pointer is in the word following the header
1076 (write-wordindexed thing sb
!vm
:instance-slots-offset layout
))
1078 (defun initialize-layouts ()
1079 (clrhash *cold-layouts
*)
1080 ;; This assertion is due to the fact that MAKE-COLD-LAYOUT does not
1081 ;; know how to set any raw slots.
1082 (aver (= 0 (layout-raw-slot-metadata (find-layout 'layout
))))
1083 (setq *layout-layout
* (make-fixnum-descriptor 0))
1084 (flet ((chill-layout (name &rest inherits
)
1085 ;; Check that the number of specified INHERITS matches
1086 ;; the length of the layout's inherits in the cross-compiler.
1087 (let ((warm-layout (classoid-layout (find-classoid name
))))
1088 (assert (eql (length (layout-inherits warm-layout
))
1092 (number-to-core (layout-length warm-layout
))
1093 (vector-in-core inherits
)
1094 (number-to-core (layout-depthoid warm-layout
))
1095 (number-to-core (layout-raw-slot-metadata warm-layout
))))))
1096 (let* ((t-layout (chill-layout 't
))
1097 (s-o-layout (chill-layout 'structure-object t-layout
))
1098 (s!o-layout
(chill-layout 'structure
!object t-layout s-o-layout
)))
1099 (setf *layout-layout
*
1100 (chill-layout 'layout t-layout s-o-layout s
!o-layout
))
1101 (dolist (layout (list t-layout s-o-layout s
!o-layout
*layout-layout
*))
1102 (patch-instance-layout layout
*layout-layout
*))
1103 (setf *package-layout
*
1104 (chill-layout 'package
; *NOT* SB!XC:PACKAGE, or you lose
1105 t-layout s-o-layout s
!o-layout
)))))
1107 ;;;; interning symbols in the cold image
1109 ;;; a map from package name as a host string to
1110 ;;; (cold-package-descriptor . (external-symbols . internal-symbols))
1111 (defvar *cold-package-symbols
*)
1112 (declaim (type hash-table
*cold-package-symbols
*))
1114 ;;; a map from descriptors to symbols, so that we can back up. The key
1115 ;;; is the address in the target core.
1116 (defvar *cold-symbols
*)
1117 (declaim (type hash-table
*cold-symbols
*))
1119 (defun initialize-packages (package-data-list)
1120 (let ((slots (dd-slots (layout-info (find-layout 'package
))))
1121 (target-pkg-list nil
))
1122 (labels ((set-slot (obj slot-name value
)
1123 (write-wordindexed obj
(slot-index slot-name
) value
))
1124 (slot-index (slot-name)
1125 (+ sb
!vm
:instance-slots-offset
1127 (find slot-name slots
:key
#'dsd-name
:test
#'string
=))))
1128 (init-cold-package (name &optional docstring
)
1129 (let ((cold-package (car (gethash name
*cold-package-symbols
*))))
1130 ;; patch in the layout
1131 (patch-instance-layout cold-package
*package-layout
*)
1132 ;; Initialize string slots
1133 (set-slot cold-package
'%name
(base-string-to-core name
))
1134 (set-slot cold-package
'%nicknames
(chill-nicknames name
))
1135 (set-slot cold-package
'doc-string
1137 (base-string-to-core docstring
)
1139 (set-slot cold-package
'%use-list
*nil-descriptor
*)
1140 ;; the cddr of this will accumulate the 'used-by' package list
1141 (push (list name cold-package
) target-pkg-list
)))
1142 (chill-nicknames (pkg-name)
1143 (let ((result *nil-descriptor
*))
1144 ;; Make the package nickname lists for the standard packages
1145 ;; be the minimum specified by ANSI, regardless of what value
1146 ;; the cross-compilation host happens to use.
1147 ;; For packages other than the standard packages, the nickname
1148 ;; list was specified by our package setup code, and we can just
1149 ;; propagate the current state into the target.
1151 (cond ((string= pkg-name
"COMMON-LISP") '("CL"))
1152 ((string= pkg-name
"COMMON-LISP-USER")
1154 ((string= pkg-name
"KEYWORD") '())
1155 (t (package-nicknames (find-package pkg-name
))))
1157 (cold-push (base-string-to-core nickname
) result
))))
1158 (find-cold-package (name)
1159 (cadr (find-package-cell name
)))
1160 (find-package-cell (name)
1161 (or (assoc (if (string= name
"CL") "COMMON-LISP" name
)
1162 target-pkg-list
:test
#'string
=)
1163 (error "No cold package named ~S" name
)))
1164 (list-to-core (list)
1165 (let ((res *nil-descriptor
*))
1166 (dolist (x list res
) (cold-push x res
)))))
1167 ;; pass 1: make all proto-packages
1168 (dolist (pd package-data-list
)
1169 (init-cold-package (sb-cold:package-data-name pd
)
1170 #!+sb-doc
(sb-cold::package-data-doc pd
)))
1171 ;; MISMATCH needs !HAIRY-DATA-VECTOR-REFFER-INIT to have been done,
1172 ;; and FIND-PACKAGE calls MISMATCH - which it shouldn't - but until
1173 ;; that is fixed, doing this in genesis allows packages to be
1174 ;; completely sane, modulo the naming, extremely early in cold-init.
1175 (cold-set '*keyword-package
* (find-cold-package "KEYWORD"))
1176 (cold-set '*cl-package
* (find-cold-package "COMMON-LISP"))
1177 ;; pass 2: set the 'use' lists and collect the 'used-by' lists
1178 (dolist (pd package-data-list
)
1179 (let ((this (find-cold-package (sb-cold:package-data-name pd
)))
1181 (dolist (that (sb-cold:package-data-use pd
))
1182 (let ((cell (find-package-cell that
)))
1183 (push (cadr cell
) use
)
1184 (push this
(cddr cell
))))
1185 (set-slot this
'%use-list
(list-to-core (nreverse use
)))))
1186 ;; pass 3: set the 'used-by' lists
1187 (dolist (cell target-pkg-list
)
1188 (set-slot (cadr cell
) '%used-by-list
(list-to-core (cddr cell
)))))))
1190 ;;; sanity check for a symbol we're about to create on the target
1192 ;;; Make sure that the symbol has an appropriate package. In
1193 ;;; particular, catch the so-easy-to-make error of typing something
1194 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1195 ;;; need is SB!KERNEL:%BYTE-BLT.
1196 (defun package-ok-for-target-symbol-p (package)
1197 (let ((package-name (package-name package
)))
1199 ;; Cold interning things in these standard packages is OK. (Cold
1200 ;; interning things in the other standard package, CL-USER, isn't
1201 ;; OK. We just use CL-USER to expose symbols whose homes are in
1202 ;; other packages. Thus, trying to cold intern a symbol whose
1203 ;; home package is CL-USER probably means that a coding error has
1204 ;; been made somewhere.)
1205 (find package-name
'("COMMON-LISP" "KEYWORD") :test
#'string
=)
1206 ;; Cold interning something in one of our target-code packages,
1207 ;; which are ever-so-rigorously-and-elegantly distinguished by
1208 ;; this prefix on their names, is OK too.
1209 (string= package-name
"SB!" :end1
3 :end2
3)
1210 ;; This one is OK too, since it ends up being COMMON-LISP on the
1212 (string= package-name
"SB-XC")
1213 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1214 ;; package in the xc host? something we can't think of
1215 ;; a valid reason to cold intern, anyway...)
1218 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1220 ;;; Most host symbols we dump onto the target are created by SBCL
1221 ;;; itself, so that as long as we avoid gratuitously
1222 ;;; cross-compilation-unfriendly hacks, it just happens that their
1223 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1224 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1225 ;;; in the COMMON-LISP package, where we don't get to create the
1226 ;;; symbols but instead have to use the ones that the xc host created.
1227 ;;; In particular, while ANSI specifies which symbols are exported
1228 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1229 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1230 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1231 ;;; symbols in the CLOS package).
1232 (defun symbol-package-for-target-symbol (symbol)
1233 ;; We want to catch weird symbols like CLISP's
1234 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1235 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1236 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1237 (multiple-value-bind (cl-symbol cl-status
)
1238 (find-symbol (symbol-name symbol
) *cl-package
*)
1239 (if (and (eq symbol cl-symbol
)
1240 (eq cl-status
:external
))
1241 ;; special case, to work around possible xc host weirdness
1242 ;; in COMMON-LISP package
1245 (let ((result (symbol-package symbol
)))
1246 (unless (package-ok-for-target-symbol-p result
)
1247 (bug "~A in bad package for target: ~A" symbol result
))
1250 (defvar *uninterned-symbol-table
* (make-hash-table :test
#'equal
))
1251 ;; This coalesces references to uninterned symbols, which is allowed because
1252 ;; "similar-as-constant" is defined by string comparison, and since we only have
1253 ;; base-strings during Genesis, there is no concern about upgraded array type.
1254 ;; There is a subtlety of whether coalescing may occur across files
1255 ;; - the target compiler doesn't and couldn't - but here it doesn't matter.
1256 (defun get-uninterned-symbol (name)
1257 (or (gethash name
*uninterned-symbol-table
*)
1258 (let ((cold-symbol (allocate-symbol name
)))
1259 (setf (gethash name
*uninterned-symbol-table
*) cold-symbol
))))
1261 ;;; Dump the target representation of HOST-VALUE,
1262 ;;; the type of which is in a restrictive set.
1263 (defun host-constant-to-core (host-value)
1264 ;; rough check for no shared substructure and/or circularity.
1265 ;; of course this would be wrong if it were a string containing "#1="
1266 (when (search "#1=" (write-to-string host-value
:circle t
:readably t
))
1267 (warn "Strange constant to core from Genesis: ~S" host-value
))
1268 (labels ((target-representation (value)
1270 (symbol (if (symbol-package value
)
1272 (get-uninterned-symbol (string value
))))
1273 (number (number-to-core value
))
1274 (string (base-string-to-core value
))
1275 (cons (cold-cons (target-representation (car value
))
1276 (target-representation (cdr value
))))
1278 (vector-in-core (map 'list
#'target-representation value
))))))
1279 (target-representation host-value
)))
1281 ;;; Return a handle on an interned symbol. If necessary allocate the
1282 ;;; symbol and record its home package.
1283 (defun cold-intern (symbol
1286 &aux
(package (symbol-package-for-target-symbol symbol
)))
1287 (aver (package-ok-for-target-symbol-p package
))
1289 ;; Anything on the cross-compilation host which refers to the target
1290 ;; machinery through the host SB-XC package should be translated to
1291 ;; something on the target which refers to the same machinery
1292 ;; through the target COMMON-LISP package.
1293 (let ((p (find-package "SB-XC")))
1294 (when (eq package p
)
1295 (setf package
*cl-package
*))
1296 (when (eq (symbol-package symbol
) p
)
1297 (setf symbol
(intern (symbol-name symbol
) *cl-package
*))))
1299 (or (get symbol
'cold-intern-info
)
1300 (let ((pkg-info (gethash (package-name package
) *cold-package-symbols
*))
1301 (handle (allocate-symbol (symbol-name symbol
) :gspace gspace
)))
1302 ;; maintain reverse map from target descriptor to host symbol
1303 (setf (gethash (descriptor-bits handle
) *cold-symbols
*) symbol
)
1305 (error "No target package descriptor for ~S" package
))
1306 (record-accessibility
1307 (or access
(nth-value 1 (find-symbol (symbol-name symbol
) package
)))
1308 handle pkg-info symbol package t
)
1310 (assign-tls-index symbol handle
)
1311 (acond ((eq package
*keyword-package
*)
1312 (setq access
:external
)
1313 (cold-set handle handle
))
1314 ((assoc symbol sb-cold
:*symbol-values-for-genesis
*)
1316 (host-constant-to-core
1317 (let ((*package
* (find-package (cddr it
))))
1318 (eval (cadr it
)))))))
1319 (setf (get symbol
'cold-intern-info
) handle
))))
1321 (defun record-accessibility (accessibility symbol-descriptor target-pkg-info
1322 host-symbol host-package
&optional set-home-p
)
1324 (write-wordindexed symbol-descriptor sb
!vm
:symbol-package-slot
1325 (car target-pkg-info
)))
1326 (when (member host-symbol
(package-shadowing-symbols host-package
))
1327 ;; Fail in an obvious way if target shadowing symbols exist.
1328 ;; (This is simply not an important use-case during system bootstrap.)
1329 (error "Genesis doesn't like shadowing symbol ~S, sorry." host-symbol
))
1330 (let ((access-lists (cdr target-pkg-info
)))
1332 (:external
(push symbol-descriptor
(car access-lists
)))
1333 (:internal
(push symbol-descriptor
(cdr access-lists
)))
1334 (t (error "~S inaccessible in package ~S" host-symbol host-package
)))))
1336 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1337 ;;; It might be nice to put NIL on a readonly page by itself to prevent unsafe
1338 ;;; code from destroying the world with (RPLACx nil 'kablooey)
1339 (defun make-nil-descriptor (target-cl-pkg-info)
1340 (let* ((des (allocate-header+object
*static
* sb
!vm
:symbol-size
0))
1341 (result (make-descriptor (descriptor-high des
)
1342 (+ (descriptor-low des
)
1343 (* 2 sb
!vm
:n-word-bytes
)
1344 (- sb
!vm
:list-pointer-lowtag
1345 sb
!vm
:other-pointer-lowtag
)))))
1346 (write-wordindexed des
1348 (make-other-immediate-descriptor
1350 sb
!vm
:symbol-header-widetag
))
1351 (write-wordindexed des
1352 (+ 1 sb
!vm
:symbol-value-slot
)
1354 (write-wordindexed des
1355 (+ 2 sb
!vm
:symbol-value-slot
) ; = 1 + symbol-hash-slot
1357 (write-wordindexed des
1358 (+ 1 sb
!vm
:symbol-info-slot
)
1359 (cold-cons result result
)) ; NIL's info is (nil . nil)
1360 (write-wordindexed des
1361 (+ 1 sb
!vm
:symbol-name-slot
)
1362 ;; NIL's name is in dynamic space because any extra
1363 ;; bytes allocated in static space would need to
1364 ;; be accounted for by STATIC-SYMBOL-OFFSET.
1365 (base-string-to-core "NIL" *dynamic
*))
1366 ;; RECORD-ACCESSIBILITY can't assign to the package slot
1367 ;; due to NIL's base address and lowtag being nonstandard.
1368 (write-wordindexed des
1369 (+ 1 sb
!vm
:symbol-package-slot
)
1370 (car target-cl-pkg-info
))
1371 (record-accessibility :external result target-cl-pkg-info nil
*cl-package
*)
1372 (setf (gethash (descriptor-bits result
) *cold-symbols
*) nil
1373 (get nil
'cold-intern-info
) result
)))
1375 ;;; Since the initial symbols must be allocated before we can intern
1376 ;;; anything else, we intern those here. We also set the value of T.
1377 (defun initialize-non-nil-symbols ()
1378 "Initialize the cold load symbol-hacking data structures."
1379 ;; Intern the others.
1380 (dolist (symbol sb
!vm
:*static-symbols
*)
1381 (let* ((des (cold-intern symbol
:gspace
*static
*))
1382 (offset-wanted (sb!vm
:static-symbol-offset symbol
))
1383 (offset-found (- (descriptor-low des
)
1384 (descriptor-low *nil-descriptor
*))))
1385 (unless (= offset-wanted offset-found
)
1386 ;; FIXME: should be fatal
1387 (warn "Offset from ~S to ~S is ~W, not ~W"
1392 ;; Establish the value of T.
1393 (let ((t-symbol (cold-intern t
:gspace
*static
*)))
1394 (cold-set t-symbol t-symbol
))
1395 ;; Establish the value of *PSEUDO-ATOMIC-BITS* so that the
1396 ;; allocation sequences that expect it to be zero upon entrance
1397 ;; actually find it to be so.
1399 (let ((p-a-a-symbol (cold-intern '*pseudo-atomic-bits
*
1401 (cold-set p-a-a-symbol
(make-fixnum-descriptor 0))))
1403 ;;; a helper function for FINISH-SYMBOLS: Return a cold alist suitable
1404 ;;; to be stored in *!INITIAL-LAYOUTS*.
1405 (defun cold-list-all-layouts ()
1407 (result *nil-descriptor
*))
1408 (maphash (lambda (key stuff
)
1409 (push (cons key
(first stuff
)) layouts
))
1411 (flet ((sorter (x y
)
1412 (let ((xpn (package-name (symbol-package-for-target-symbol x
)))
1413 (ypn (package-name (symbol-package-for-target-symbol y
))))
1415 ((string= x y
) (string< xpn ypn
))
1416 (t (string< x y
))))))
1417 (setq layouts
(sort layouts
#'sorter
:key
#'car
)))
1418 (dolist (layout layouts result
)
1419 (cold-push (cold-cons (cold-intern (car layout
)) (cdr layout
))
1422 ;;; Establish initial values for magic symbols.
1424 (defun finish-symbols ()
1426 ;; Everything between this preserved-for-posterity comment down to
1427 ;; the assignment of *CURRENT-CATCH-BLOCK* could be entirely deleted,
1428 ;; including the list of *C-CALLABLE-STATIC-SYMBOLS* itself,
1429 ;; if it is GC-safe for the C runtime to have its own implementation
1430 ;; of the INFO-VECTOR-FDEFN function in a multi-threaded build.
1432 ;; "I think the point of setting these functions into SYMBOL-VALUEs
1433 ;; here, instead of using SYMBOL-FUNCTION, is that in CMU CL
1434 ;; SYMBOL-FUNCTION reduces to FDEFINITION, which is a pretty
1435 ;; hairy operation (involving globaldb.lisp etc.) which we don't
1436 ;; want to invoke early in cold init. -- WHN 2001-12-05"
1438 ;; So... that's no longer true. We _do_ associate symbol -> fdefn in genesis.
1439 ;; Additionally, the INFO-VECTOR-FDEFN function is extremely simple and could
1440 ;; easily be implemented in C. However, info-vectors are inevitably
1441 ;; reallocated when new info is attached to a symbol, so the vectors can't be
1442 ;; in static space; they'd gradually become permanent garbage if they did.
1443 ;; That's the real reason for preserving the approach of storing an #<fdefn>
1444 ;; in a symbol's value cell - that location is static, the symbol-info is not.
1446 ;; FIXME: So OK, that's a reasonable reason to do something weird like
1447 ;; this, but this is still a weird thing to do, and we should change
1448 ;; the names to highlight that something weird is going on. Perhaps
1449 ;; *MAYBE-GC-FUN*, *INTERNAL-ERROR-FUN*, *HANDLE-BREAKPOINT-FUN*,
1450 ;; and *HANDLE-FUN-END-BREAKPOINT-FUN*...
1451 (dolist (symbol sb
!vm
::*c-callable-static-symbols
*)
1452 (cold-set symbol
(cold-fdefinition-object (cold-intern symbol
))))
1454 (cold-set 'sb
!vm
::*current-catch-block
* (make-fixnum-descriptor 0))
1455 (cold-set 'sb
!vm
::*current-unwind-protect-block
* (make-fixnum-descriptor 0))
1457 (cold-set '*free-interrupt-context-index
* (make-fixnum-descriptor 0))
1459 (cold-set '*!initial-layouts
* (cold-list-all-layouts))
1463 (cold-set 'sb
!vm
::*free-tls-index
*
1464 (make-random-descriptor (ash *genesis-tls-counter
*
1466 (cold-set 'sb
!vm
::*tls-index-lock
* (make-fixnum-descriptor 0)))
1468 (dolist (symbol sb
!impl
::*cache-vector-symbols
*)
1469 (cold-set symbol
*nil-descriptor
*))
1471 ;; Symbols for which no call to COLD-INTERN would occur - due to not being
1472 ;; referenced until warm init - must be artificially cold-interned.
1473 ;; Inasmuch as the "offending" things are compiled by ordinary target code
1474 ;; and not cold-init, I think we should use an ordinary DEFPACKAGE for
1475 ;; the added-on bits. What I've done is somewhat of a fragile kludge.
1477 (with-package-iterator (iter '("SB!PCL" "SB!MOP" "SB!GRAY" "SB!SEQUENCE"
1478 "SB!PROFILE" "SB!EXT" "SB!VM"
1479 "SB!C" "SB!FASL" "SB!DEBUG")
1482 (multiple-value-bind (foundp sym accessibility package
) (iter)
1483 (declare (ignore accessibility
))
1484 (cond ((not foundp
) (return))
1485 ((eq (symbol-package sym
) package
) (push sym syms
))))))
1486 (setf syms
(stable-sort syms
#'string
<))
1490 (let ((cold-pkg-inits *nil-descriptor
*)
1491 cold-package-symbols-list
)
1492 (maphash (lambda (name info
)
1493 (push (cons name info
) cold-package-symbols-list
))
1494 *cold-package-symbols
*)
1495 (setf cold-package-symbols-list
1496 (sort cold-package-symbols-list
#'string
< :key
#'car
))
1497 (dolist (pkgcons cold-package-symbols-list
)
1498 (destructuring-bind (pkg-name . pkg-info
) pkgcons
1499 (unless (member pkg-name
'("COMMON-LISP" "KEYWORD") :test
'string
=)
1500 (let ((host-pkg (find-package pkg-name
))
1501 (sb-xc-pkg (find-package "SB-XC"))
1503 (with-package-iterator (iter host-pkg
:internal
:external
)
1504 (loop (multiple-value-bind (foundp sym accessibility
) (iter)
1505 (unless foundp
(return))
1506 (unless (or (eq (symbol-package sym
) host-pkg
)
1507 (eq (symbol-package sym
) sb-xc-pkg
))
1508 (push (cons sym accessibility
) syms
)))))
1509 (setq syms
(sort syms
#'string
< :key
#'car
))
1510 (dolist (symcons syms
)
1511 (destructuring-bind (sym . accessibility
) symcons
1512 (record-accessibility accessibility
(cold-intern sym
)
1513 pkg-info sym host-pkg
)))))
1514 (cold-push (cold-cons (car pkg-info
)
1515 (cold-cons (vector-in-core (cadr pkg-info
))
1516 (vector-in-core (cddr pkg-info
))))
1518 (cold-set 'sb
!impl
::*!initial-symbols
* cold-pkg-inits
))
1520 (attach-fdefinitions-to-symbols)
1522 (cold-set '*!reversed-cold-toplevels
* *current-reversed-cold-toplevels
*)
1523 (cold-set '*!initial-debug-sources
* *current-debug-sources
*)
1527 (cold-set 'sb
!vm
::*fp-constant-0d0
* (number-to-core 0d0
))
1528 (cold-set 'sb
!vm
::*fp-constant-1d0
* (number-to-core 1d0
))
1529 (cold-set 'sb
!vm
::*fp-constant-0f0
* (number-to-core 0f0
))
1530 (cold-set 'sb
!vm
::*fp-constant-1f0
* (number-to-core 1f0
))))
1532 ;;;; functions and fdefinition objects
1534 ;;; a hash table mapping from fdefinition names to descriptors of cold
1537 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1538 ;;; we want to have only one entry per name, this must be an 'EQUAL
1539 ;;; hash table, not the default 'EQL.
1540 (defvar *cold-fdefn-objects
*)
1542 (defvar *cold-fdefn-gspace
* nil
)
1544 ;;; Given a cold representation of a symbol, return a warm
1546 (defun warm-symbol (des)
1547 ;; Note that COLD-INTERN is responsible for keeping the
1548 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1549 ;; uninterned symbol, the code below will fail. But as long as we
1550 ;; don't need to look up uninterned symbols during bootstrapping,
1552 (multiple-value-bind (symbol found-p
)
1553 (gethash (descriptor-bits des
) *cold-symbols
*)
1554 (declare (type symbol symbol
))
1556 (error "no warm symbol"))
1559 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1560 (defun cold-car (des)
1561 (aver (= (descriptor-lowtag des
) sb
!vm
:list-pointer-lowtag
))
1562 (read-wordindexed des sb
!vm
:cons-car-slot
))
1563 (defun cold-cdr (des)
1564 (aver (= (descriptor-lowtag des
) sb
!vm
:list-pointer-lowtag
))
1565 (read-wordindexed des sb
!vm
:cons-cdr-slot
))
1566 (defun cold-null (des)
1567 (= (descriptor-bits des
)
1568 (descriptor-bits *nil-descriptor
*)))
1570 ;;; Given a cold representation of a function name, return a warm
1572 (declaim (ftype (function ((or symbol descriptor
)) (or symbol list
)) warm-fun-name
))
1573 (defun warm-fun-name (des)
1576 ;; This parallels the logic at the start of COLD-INTERN
1577 ;; which re-homes symbols in SB-XC to COMMON-LISP.
1578 (if (eq (symbol-package des
) (find-package "SB-XC"))
1579 (intern (symbol-name des
) *cl-package
*)
1581 (ecase (descriptor-lowtag des
)
1582 (#.sb
!vm
:list-pointer-lowtag
1583 (aver (not (cold-null des
))) ; function named NIL? please no..
1584 ;; Do cold (DESTRUCTURING-BIND (COLD-CAR COLD-CADR) DES ..).
1585 (let* ((car-des (cold-car des
))
1586 (cdr-des (cold-cdr des
))
1587 (cadr-des (cold-car cdr-des
))
1588 (cddr-des (cold-cdr cdr-des
)))
1589 (aver (cold-null cddr-des
))
1590 (list (warm-symbol car-des
)
1591 (warm-symbol cadr-des
))))
1592 (#.sb
!vm
:other-pointer-lowtag
1593 (warm-symbol des
))))))
1594 (legal-fun-name-or-type-error result
)
1597 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw
)
1598 (declare (type (or symbol descriptor
) cold-name
))
1599 (/noshow0
"/cold-fdefinition-object")
1600 (let ((warm-name (warm-fun-name cold-name
)))
1601 (or (gethash warm-name
*cold-fdefn-objects
*)
1602 (let ((fdefn (allocate-header+object
(or *cold-fdefn-gspace
* *dynamic
*)
1603 (1- sb
!vm
:fdefn-size
)
1604 sb
!vm
:fdefn-widetag
)))
1605 (setf (gethash warm-name
*cold-fdefn-objects
*) fdefn
)
1606 (write-wordindexed fdefn sb
!vm
:fdefn-name-slot cold-name
)
1607 (unless leave-fn-raw
1608 (write-wordindexed fdefn sb
!vm
:fdefn-fun-slot
1610 (write-wordindexed fdefn
1611 sb
!vm
:fdefn-raw-addr-slot
1612 (make-random-descriptor
1613 (cold-foreign-symbol-address "undefined_tramp"))))
1616 ;;; Handle the at-cold-init-time, fset-for-static-linkage operation
1617 ;;; requested by FOP-FSET.
1618 (defun static-fset (cold-name defn
)
1619 (declare (type (or symbol descriptor
) cold-name
))
1620 (let ((fdefn (cold-fdefinition-object cold-name t
))
1621 (type (logand (descriptor-low (read-memory defn
)) sb
!vm
:widetag-mask
)))
1622 (write-wordindexed fdefn sb
!vm
:fdefn-fun-slot defn
)
1623 (write-wordindexed fdefn
1624 sb
!vm
:fdefn-raw-addr-slot
1626 (#.sb
!vm
:simple-fun-header-widetag
1627 (/noshow0
"static-fset (simple-fun)")
1631 (make-random-descriptor
1632 (+ (logandc2 (descriptor-bits defn
)
1634 (ash sb
!vm
:simple-fun-code-offset
1635 sb
!vm
:word-shift
))))
1636 (#.sb
!vm
:closure-header-widetag
1637 (/show0
"/static-fset (closure)")
1638 (make-random-descriptor
1639 (cold-foreign-symbol-address "closure_tramp")))))
1642 (defun initialize-static-fns ()
1643 (let ((*cold-fdefn-gspace
* *static
*))
1644 (dolist (sym sb
!vm
:*static-funs
*)
1645 (let* ((fdefn (cold-fdefinition-object (cold-intern sym
)))
1646 (offset (- (+ (- (descriptor-low fdefn
)
1647 sb
!vm
:other-pointer-lowtag
)
1648 (* sb
!vm
:fdefn-raw-addr-slot sb
!vm
:n-word-bytes
))
1649 (descriptor-low *nil-descriptor
*)))
1650 (desired (sb!vm
:static-fun-offset sym
)))
1651 (unless (= offset desired
)
1652 ;; FIXME: should be fatal
1653 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1654 sym nil offset desired
))))))
1656 ;; Create pointer from SYMBOL and/or (SETF SYMBOL) to respective fdefinition
1658 (defun attach-fdefinitions-to-symbols ()
1659 (let ((hashtable (make-hash-table :test
#'eq
)))
1660 ;; Collect fdefinitions that go with one symbol, e.g. CAR and (SETF CAR),
1661 ;; using the host's code for manipulating a packed info-vector.
1662 (maphash (lambda (warm-name cold-fdefn
)
1663 (sb!c
::with-globaldb-name
(key1 key2
) warm-name
1664 :hairy
(error "Hairy fdefn name in genesis: ~S" warm-name
)
1666 (setf (gethash key1 hashtable
)
1667 (sb!c
::packed-info-insert
1668 (gethash key1 hashtable sb
!c
::+nil-packed-infos
+)
1669 key2 sb
!c
::+fdefn-type-num
+ cold-fdefn
))))
1670 *cold-fdefn-objects
*)
1671 ;; Emit in the same order symbols reside in core to avoid
1672 ;; sensitivity to the iteration order of host's maphash.
1673 (loop for
(warm-sym . info
)
1674 in
(sort (sb!impl
::%hash-table-alist hashtable
) #'<
1675 :key
(lambda (x) (descriptor-bits (cold-intern (car x
)))))
1676 do
(write-wordindexed
1677 (cold-intern warm-sym
) sb
!vm
:symbol-info-slot
1678 ;; Each vector will have one fixnum, possibly the symbol SETF,
1679 ;; and one or two #<fdefn> objects in it.
1681 (map 'list
(lambda (elt)
1683 (symbol (cold-intern elt
))
1684 (fixnum (make-fixnum-descriptor elt
))
1689 ;;;; fixups and related stuff
1691 ;;; an EQUAL hash table
1692 (defvar *cold-foreign-symbol-table
*)
1693 (declaim (type hash-table
*cold-foreign-symbol-table
*))
1695 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1697 (defun load-cold-foreign-symbol-table (filename)
1698 (/show
"load-cold-foreign-symbol-table" filename
)
1699 (with-open-file (file filename
)
1700 (loop for line
= (read-line file nil nil
)
1702 ;; UNIX symbol tables might have tabs in them, and tabs are
1703 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1704 ;; nice portable way to deal with them within Lisp, alas.
1705 ;; Fortunately, it's easy to use UNIX command line tools like
1706 ;; sed to remove the problem, so it's not too painful for us
1707 ;; to push responsibility for converting tabs to spaces out to
1710 ;; Other non-STANDARD-CHARs are problematic for the same reason.
1711 ;; Make sure that there aren't any..
1712 (let ((ch (find-if (lambda (char)
1713 (not (typep char
'standard-char
)))
1716 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1719 (setf line
(string-trim '(#\space
) line
))
1720 (let ((p1 (position #\space line
:from-end nil
))
1721 (p2 (position #\space line
:from-end t
)))
1722 (if (not (and p1 p2
(< p1 p2
)))
1723 ;; KLUDGE: It's too messy to try to understand all
1724 ;; possible output from nm, so we just punt the lines we
1725 ;; don't recognize. We realize that there's some chance
1726 ;; that might get us in trouble someday, so we warn
1728 (warn "ignoring unrecognized line ~S in ~A" line filename
)
1729 (multiple-value-bind (value name
)
1730 (if (string= "0x" line
:end2
2)
1731 (values (parse-integer line
:start
2 :end p1
:radix
16)
1732 (subseq line
(1+ p2
)))
1733 (values (parse-integer line
:end p1
:radix
16)
1734 (subseq line
(1+ p2
))))
1735 ;; KLUDGE CLH 2010-05-31: on darwin, nm gives us
1736 ;; _function but dlsym expects us to look up
1737 ;; function, without the leading _ . Therefore, we
1738 ;; strip it off here.
1740 (when (equal (char name
0) #\_
)
1741 (setf name
(subseq name
1)))
1742 (multiple-value-bind (old-value found
)
1743 (gethash name
*cold-foreign-symbol-table
*)
1745 (not (= old-value value
)))
1746 (warn "redefining ~S from #X~X to #X~X"
1747 name old-value value
)))
1748 (/show
"adding to *cold-foreign-symbol-table*:" name value
)
1749 (setf (gethash name
*cold-foreign-symbol-table
*) value
)
1751 (let ((at-position (position #\
@ name
)))
1753 (let ((name (subseq name
0 at-position
)))
1754 (multiple-value-bind (old-value found
)
1755 (gethash name
*cold-foreign-symbol-table
*)
1757 (not (= old-value value
)))
1758 (warn "redefining ~S from #X~X to #X~X"
1759 name old-value value
)))
1760 (setf (gethash name
*cold-foreign-symbol-table
*)
1764 (defun cold-foreign-symbol-address (name)
1765 (or (find-foreign-symbol-in-table name
*cold-foreign-symbol-table
*)
1766 *foreign-symbol-placeholder-value
*
1768 (format *error-output
* "~&The foreign symbol table is:~%")
1769 (maphash (lambda (k v
)
1770 (format *error-output
* "~&~S = #X~8X~%" k v
))
1771 *cold-foreign-symbol-table
*)
1772 (error "The foreign symbol ~S is undefined." name
))))
1774 (defvar *cold-assembler-routines
*)
1776 (defvar *cold-assembler-fixups
*)
1778 (defun record-cold-assembler-routine (name address
)
1779 (/xhow
"in RECORD-COLD-ASSEMBLER-ROUTINE" name address
)
1780 (push (cons name address
)
1781 *cold-assembler-routines
*))
1783 (defun record-cold-assembler-fixup (routine
1788 (push (list routine code-object offset kind
)
1789 *cold-assembler-fixups
*))
1791 (defun lookup-assembler-reference (symbol)
1792 (let ((value (cdr (assoc symbol
*cold-assembler-routines
*))))
1793 ;; FIXME: Should this be ERROR instead of WARN?
1795 (warn "Assembler routine ~S not defined." symbol
))
1798 ;;; The x86 port needs to store code fixups along with code objects if
1799 ;;; they are to be moved, so fixups for code objects in the dynamic
1800 ;;; heap need to be noted.
1802 (defvar *load-time-code-fixups
*)
1805 (defun note-load-time-code-fixup (code-object offset
)
1806 ;; If CODE-OBJECT might be moved
1807 (when (= (gspace-identifier (descriptor-intuit-gspace code-object
))
1808 dynamic-core-space-id
)
1809 (push offset
(gethash (descriptor-bits code-object
)
1810 *load-time-code-fixups
*
1815 (defun output-load-time-code-fixups ()
1816 (let ((fixup-infos nil
))
1818 (lambda (code-object-address fixup-offsets
)
1819 (push (cons code-object-address fixup-offsets
) fixup-infos
))
1820 *load-time-code-fixups
*)
1821 (setq fixup-infos
(sort fixup-infos
#'< :key
#'car
))
1822 (dolist (fixup-info fixup-infos
)
1823 (let ((code-object-address (car fixup-info
))
1824 (fixup-offsets (cdr fixup-info
)))
1826 (allocate-vector-object
1827 *dynamic
* sb
!vm
:n-word-bits
(length fixup-offsets
)
1828 sb
!vm
:simple-array-unsigned-byte-32-widetag
)))
1829 (do ((index sb
!vm
:vector-data-offset
(1+ index
))
1830 (fixups fixup-offsets
(cdr fixups
)))
1832 (write-wordindexed fixup-vector index
1833 (make-random-descriptor (car fixups
))))
1834 ;; KLUDGE: The fixup vector is stored as the first constant,
1835 ;; not as a separately-named slot.
1836 (write-wordindexed (make-random-descriptor code-object-address
)
1837 sb
!vm
:code-constants-offset
1840 ;;; Given a pointer to a code object and an offset relative to the
1841 ;;; tail of the code object's header, return an offset relative to the
1842 ;;; (beginning of the) code object.
1844 ;;; FIXME: It might be clearer to reexpress
1845 ;;; (LET ((X (CALC-OFFSET CODE-OBJECT OFFSET0))) ..)
1847 ;;; (LET ((X (+ OFFSET0 (CODE-OBJECT-HEADER-N-BYTES CODE-OBJECT)))) ..).
1848 (declaim (ftype (function (descriptor sb
!vm
:word
)) calc-offset
))
1849 (defun calc-offset (code-object offset-from-tail-of-header
)
1850 (let* ((header (read-memory code-object
))
1851 (header-n-words (ash (descriptor-bits header
)
1852 (- sb
!vm
:n-widetag-bits
)))
1853 (header-n-bytes (ash header-n-words sb
!vm
:word-shift
))
1854 (result (+ offset-from-tail-of-header header-n-bytes
)))
1857 (declaim (ftype (function (descriptor sb
!vm
:word sb
!vm
:word keyword
))
1859 (defun do-cold-fixup (code-object after-header value kind
)
1860 (let* ((offset-within-code-object (calc-offset code-object after-header
))
1861 (gspace-bytes (descriptor-bytes code-object
))
1862 (gspace-byte-offset (+ (descriptor-byte-offset code-object
)
1863 offset-within-code-object
))
1864 (gspace-byte-address (gspace-byte-address
1865 (descriptor-gspace code-object
))))
1866 ;; There's just a ton of code here that gets deleted,
1867 ;; inhibiting the view of the the forest through the trees.
1868 ;; Use of #+sbcl would say "probable bug in read-time conditional"
1869 #+#.
(cl:if
(cl:member
:sbcl cl
:*features
*) '(and) '(or))
1870 (declare (sb-ext:muffle-conditions sb-ext
:code-deletion-note
))
1871 (ecase +backend-fasl-file-implementation
+
1872 ;; See CMU CL source for other formerly-supported architectures
1873 ;; (and note that you have to rewrite them to use BVREF-X
1874 ;; instead of SAP-REF).
1878 (assert (zerop (ldb (byte 2 0) value
))))
1880 (let* ((value (if (logbitp 15 value
) (+ value
(ash 1 16)) value
))
1881 (value (if (logbitp 31 value
) (+ value
(ash 1 32)) value
))
1882 (value (if (logbitp 47 value
) (+ value
(ash 1 48)) value
)))
1883 (setf (bvref-8 gspace-bytes gspace-byte-offset
)
1884 (ldb (byte 8 48) value
)
1885 (bvref-8 gspace-bytes
(1+ gspace-byte-offset
))
1886 (ldb (byte 8 56) value
))))
1888 (let* ((value (if (logbitp 15 value
) (+ value
(ash 1 16)) value
))
1889 (value (if (logbitp 31 value
) (+ value
(ash 1 32)) value
)))
1890 (setf (bvref-8 gspace-bytes gspace-byte-offset
)
1891 (ldb (byte 8 32) value
)
1892 (bvref-8 gspace-bytes
(1+ gspace-byte-offset
))
1893 (ldb (byte 8 40) value
))))
1895 (let ((value (if (logbitp 15 value
) (+ value
(ash 1 16)) value
)))
1896 (setf (bvref-8 gspace-bytes gspace-byte-offset
)
1897 (ldb (byte 8 16) value
)
1898 (bvref-8 gspace-bytes
(1+ gspace-byte-offset
))
1899 (ldb (byte 8 24) value
))))
1901 (setf (bvref-8 gspace-bytes gspace-byte-offset
)
1902 (ldb (byte 8 0) value
)
1903 (bvref-8 gspace-bytes
(1+ gspace-byte-offset
))
1904 (ldb (byte 8 8) value
)))))
1908 (setf (bvref-32 gspace-bytes gspace-byte-offset
) value
))))
1912 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
1913 (logior (mask-field (byte 18 14)
1914 (bvref-32 gspace-bytes gspace-byte-offset
))
1916 (1+ (ash (ldb (byte 13 0) value
) 1))
1917 (ash (ldb (byte 13 0) value
) 1)))))
1919 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
1920 (logior (mask-field (byte 18 14)
1921 (bvref-32 gspace-bytes gspace-byte-offset
))
1923 (1+ (ash (ldb (byte 10 0) value
) 1))
1924 (ash (ldb (byte 11 0) value
) 1)))))
1926 (let ((low-bits (ldb (byte 11 0) value
)))
1927 (assert (<= 0 low-bits
(1- (ash 1 4)))))
1928 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
1929 (logior (ash (dpb (ldb (byte 4 0) value
)
1931 (ldb (byte 1 4) value
)) 17)
1932 (logand (bvref-32 gspace-bytes gspace-byte-offset
)
1935 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
1936 (logior (mask-field (byte 11 21)
1937 (bvref-32 gspace-bytes gspace-byte-offset
))
1938 (ash (ldb (byte 5 13) value
) 16)
1939 (ash (ldb (byte 2 18) value
) 14)
1940 (ash (ldb (byte 2 11) value
) 12)
1941 (ash (ldb (byte 11 20) value
) 1)
1942 (ldb (byte 1 31) value
))))
1944 (let ((bits (ldb (byte 9 2) value
)))
1945 (assert (zerop (ldb (byte 2 0) value
)))
1946 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
1947 (logior (ash bits
3)
1948 (mask-field (byte 1 1) (bvref-32 gspace-bytes gspace-byte-offset
))
1949 (mask-field (byte 3 13) (bvref-32 gspace-bytes gspace-byte-offset
))
1950 (mask-field (byte 11 21) (bvref-32 gspace-bytes gspace-byte-offset
))))))))
1954 (assert (zerop (ash value -
28)))
1955 (setf (ldb (byte 26 0)
1956 (bvref-32 gspace-bytes gspace-byte-offset
))
1959 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
1960 (logior (mask-field (byte 16 16)
1961 (bvref-32 gspace-bytes gspace-byte-offset
))
1962 (ash (1+ (ldb (byte 17 15) value
)) -
1))))
1964 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
1965 (logior (mask-field (byte 16 16)
1966 (bvref-32 gspace-bytes gspace-byte-offset
))
1967 (ldb (byte 16 0) value
))))))
1968 ;; FIXME: PowerPC Fixups are not fully implemented. The bit
1969 ;; here starts to set things up to work properly, but there
1970 ;; needs to be corresponding code in ppc-vm.lisp
1974 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
1975 (dpb (ash value -
2) (byte 24 2)
1976 (bvref-32 gspace-bytes gspace-byte-offset
))))
1978 (let* ((un-fixed-up (bvref-16 gspace-bytes
1979 (+ gspace-byte-offset
2)))
1980 (fixed-up (+ un-fixed-up value
))
1981 (h (ldb (byte 16 16) fixed-up
))
1982 (l (ldb (byte 16 0) fixed-up
)))
1983 (setf (bvref-16 gspace-bytes
(+ gspace-byte-offset
2))
1984 (if (logbitp 15 l
) (ldb (byte 16 0) (1+ h
)) h
))))
1986 (let* ((un-fixed-up (bvref-16 gspace-bytes
1987 (+ gspace-byte-offset
2)))
1988 (fixed-up (+ un-fixed-up value
)))
1989 (setf (bvref-16 gspace-bytes
(+ gspace-byte-offset
2))
1990 (ldb (byte 16 0) fixed-up
))))))
1994 (error "can't deal with call fixups yet"))
1996 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
1997 (dpb (ldb (byte 22 10) value
)
1999 (bvref-32 gspace-bytes gspace-byte-offset
))))
2001 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
2002 (dpb (ldb (byte 10 0) value
)
2004 (bvref-32 gspace-bytes gspace-byte-offset
))))))
2006 ;; XXX: Note that un-fixed-up is read via bvref-word, which is
2007 ;; 64 bits wide on x86-64, but the fixed-up value is written
2008 ;; via bvref-32. This would make more sense if we supported
2009 ;; :absolute64 fixups, but apparently the cross-compiler
2010 ;; doesn't dump them.
2011 (let* ((un-fixed-up (bvref-word gspace-bytes
2012 gspace-byte-offset
))
2013 (code-object-start-addr (logandc2 (descriptor-bits code-object
)
2014 sb
!vm
:lowtag-mask
)))
2015 (assert (= code-object-start-addr
2016 (+ gspace-byte-address
2017 (descriptor-byte-offset code-object
))))
2020 (let ((fixed-up (+ value un-fixed-up
)))
2021 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
2023 ;; comment from CMU CL sources:
2025 ;; Note absolute fixups that point within the object.
2026 ;; KLUDGE: There seems to be an implicit assumption in
2027 ;; the old CMU CL code here, that if it doesn't point
2028 ;; before the object, it must point within the object
2029 ;; (not beyond it). It would be good to add an
2030 ;; explanation of why that's true, or an assertion that
2031 ;; it's really true, or both.
2033 ;; One possible explanation is that all absolute fixups
2034 ;; point either within the code object, within the
2035 ;; runtime, within read-only or static-space, or within
2036 ;; the linkage-table space. In all x86 configurations,
2037 ;; these areas are prior to the start of dynamic space,
2038 ;; where all the code-objects are loaded.
2040 (unless (< fixed-up code-object-start-addr
)
2041 (note-load-time-code-fixup code-object
2043 (:relative
; (used for arguments to X86 relative CALL instruction)
2044 (let ((fixed-up (- (+ value un-fixed-up
)
2047 4))) ; "length of CALL argument"
2048 (setf (bvref-32 gspace-bytes gspace-byte-offset
)
2050 ;; Note relative fixups that point outside the code
2051 ;; object, which is to say all relative fixups, since
2052 ;; relative addressing within a code object never needs
2055 (note-load-time-code-fixup code-object
2056 after-header
))))))))
2059 (defun resolve-assembler-fixups ()
2060 (dolist (fixup *cold-assembler-fixups
*)
2061 (let* ((routine (car fixup
))
2062 (value (lookup-assembler-reference routine
)))
2064 (do-cold-fixup (second fixup
) (third fixup
) value
(fourth fixup
))))))
2068 (defparameter *dyncore-address
* sb
!vm
::linkage-table-space-start
)
2069 (defparameter *dyncore-linkage-keys
* nil
)
2070 (defparameter *dyncore-table
* (make-hash-table :test
'equal
))
2072 (defun dyncore-note-symbol (symbol-name datap
)
2073 "Register a symbol and return its address in proto-linkage-table."
2074 (let ((key (cons symbol-name datap
)))
2075 (symbol-macrolet ((entry (gethash key
*dyncore-table
*)))
2078 (prog1 *dyncore-address
*
2079 (push key
*dyncore-linkage-keys
*)
2080 (incf *dyncore-address
* sb
!vm
::linkage-table-entry-size
))))))))
2082 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
2083 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
2084 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
2085 ;;; target-load.lisp refers to.
2086 (defun foreign-symbols-to-core ()
2088 (result *nil-descriptor
*))
2091 (maphash (lambda (symbol value
)
2092 (push (cons symbol value
) symbols
))
2093 *cold-foreign-symbol-table
*)
2094 (setq symbols
(sort symbols
#'string
< :key
#'car
))
2095 (dolist (symbol symbols
)
2096 (cold-push (cold-cons (base-string-to-core (car symbol
))
2097 (number-to-core (cdr symbol
)))
2099 (cold-set '*!initial-foreign-symbols
* result
)
2101 (let ((runtime-linking-list *nil-descriptor
*))
2102 (dolist (symbol *dyncore-linkage-keys
*)
2103 (cold-push (cold-cons (base-string-to-core (car symbol
))
2105 runtime-linking-list
))
2106 (cold-set 'sb
!vm
::*required-runtime-c-symbols
*
2107 runtime-linking-list
)))
2108 (let ((result *nil-descriptor
*))
2109 (dolist (rtn (sort (copy-list *cold-assembler-routines
*) #'string
< :key
#'car
))
2110 (cold-push (cold-cons (cold-intern (car rtn
))
2111 (number-to-core (cdr rtn
)))
2113 (cold-set '*!initial-assembler-routines
* result
)))
2116 ;;;; general machinery for cold-loading FASL files
2118 ;;; FOP functions for cold loading
2119 (defvar *cold-fop-funs
*
2120 ;; We start out with a copy of the ordinary *FOP-FUNS*. The ones
2121 ;; which aren't appropriate for cold load will be destructively
2123 (copy-seq *fop-funs
*))
2125 (defun pop-fop-stack ()
2126 (let* ((stack *fop-stack
*)
2127 (top (svref stack
0)))
2128 (declare (type index top
))
2130 (error "FOP stack empty"))
2131 (setf (svref stack
0) (1- top
))
2134 ;;; Cause a fop to have a special definition for cold load.
2136 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
2137 ;;; (1) looks up the code for this name (created by a previous
2138 ;;; DEFINE-FOP) instead of creating a code, and
2139 ;;; (2) stores its definition in the *COLD-FOP-FUNS* vector,
2140 ;;; instead of storing in the *FOP-FUNS* vector.
2141 (defmacro define-cold-fop
((name &optional arglist
) &rest forms
)
2142 (let* ((code (get name
'opcode
))
2143 (argp (plusp (sbit (car *fop-signatures
*) (ash code -
2))))
2144 (fname (symbolicate "COLD-" name
)))
2146 (error "~S is not a defined FOP." name
))
2147 (when (and argp
(not (singleton-p arglist
)))
2148 (error "~S must take one argument" name
))
2150 (defun ,fname
,arglist
2151 (macrolet ((pop-stack () `(pop-fop-stack))) ,@forms
))
2152 ,@(loop for i from code to
(logior code
(if argp
3 0))
2153 collect
`(setf (svref *cold-fop-funs
* ,i
) #',fname
)))))
2155 ;;; Cause a fop to be undefined in cold load.
2156 (defmacro not-cold-fop
(name)
2157 `(define-cold-fop (,name
)
2158 (error "The fop ~S is not supported in cold load." ',name
)))
2160 ;;; COLD-LOAD loads stuff into the core image being built by calling
2161 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
2162 ;;; loading functions.
2163 (defun cold-load (filename)
2164 "Load the file named by FILENAME into the cold load image being built."
2165 (let* ((*fop-funs
* *cold-fop-funs
*)
2166 (*cold-load-filename
* (etypecase filename
2168 (pathname (namestring filename
)))))
2169 (with-open-file (s filename
:element-type
'(unsigned-byte 8))
2170 (load-as-fasl s nil nil
))))
2172 ;;;; miscellaneous cold fops
2174 (define-cold-fop (fop-misc-trap) *unbound-marker
*)
2176 (define-cold-fop (fop-character (c))
2177 (make-character-descriptor c
))
2179 (define-cold-fop (fop-empty-list) nil
)
2180 (define-cold-fop (fop-truth) t
)
2182 (define-cold-fop (fop-struct (size)) ; n-words incl. layout, excluding header
2183 (let* ((layout (pop-stack))
2184 (result (allocate-structure-object *dynamic
* size layout
))
2189 (+ sb
!vm
:instance-slots-offset
2190 (target-layout-index
2191 #!-interleaved-raw-slots
'n-untagged-slots
2192 #!+interleaved-raw-slots
'untagged-bitmap
)))))
2193 #!-interleaved-raw-slots
(ntagged (- size metadata
))
2195 #!+interleaved-raw-slots
2196 (unless (= metadata
0)
2197 (error "Interleaved raw slots not (yet) known to work in genesis."))
2199 (do ((index 1 (1+ index
)))
2201 (declare (fixnum index
))
2202 (write-wordindexed result
2203 (+ index sb
!vm
:instance-slots-offset
)
2204 (if #!-interleaved-raw-slots
(>= index ntagged
)
2205 #!+interleaved-raw-slots
(logbitp index metadata
)
2206 (descriptor-word-sized-integer (pop-stack))
2210 (define-cold-fop (fop-layout)
2211 (let* ((metadata-des (pop-stack))
2212 (length-des (pop-stack))
2213 (depthoid-des (pop-stack))
2214 (cold-inherits (pop-stack))
2216 (old (gethash name
*cold-layouts
*)))
2217 (declare (type descriptor length-des depthoid-des cold-inherits
))
2218 (declare (type symbol name
))
2219 ;; If a layout of this name has been defined already
2221 ;; Enforce consistency between the previous definition and the
2222 ;; current definition, then return the previous definition.
2224 ;; FIXME: This would be more maintainable if we used
2225 ;; DEFSTRUCT (:TYPE LIST) to define COLD-LAYOUT. -- WHN 19990825
2226 (old-layout-descriptor
2233 (declare (type descriptor old-layout-descriptor
))
2234 (declare (type index old-length
))
2235 (declare (type list old-inherits-list
))
2236 (declare (type fixnum old-depthoid
))
2237 (declare (type unsigned-byte old-metadata
))
2238 (aver (eq name old-name
))
2239 (let ((length (descriptor-fixnum length-des
))
2240 (inherits-list (listify-cold-inherits cold-inherits
))
2241 (depthoid (descriptor-fixnum depthoid-des
))
2242 (metadata (descriptor-fixnum metadata-des
)))
2243 (unless (= length old-length
)
2244 (error "cold loading a reference to class ~S when the compile~%~
2245 time length was ~S and current length is ~S"
2249 (unless (equal inherits-list old-inherits-list
)
2250 (error "cold loading a reference to class ~S when the compile~%~
2251 time inherits were ~S~%~
2252 and current inherits are ~S"
2256 (unless (= depthoid old-depthoid
)
2257 (error "cold loading a reference to class ~S when the compile~%~
2258 time inheritance depthoid was ~S and current inheritance~%~
2263 (unless (= metadata old-metadata
)
2264 (error "cold loading a reference to class ~S when the compile~%~
2265 time raw-slot-metadata was ~S and is currently ~S"
2269 old-layout-descriptor
)
2270 ;; Make a new definition from scratch.
2271 (make-cold-layout name length-des cold-inherits depthoid-des
2274 ;;;; cold fops for loading symbols
2276 ;;; Load a symbol SIZE characters long from *FASL-INPUT-STREAM* and
2277 ;;; intern that symbol in PACKAGE.
2278 (defun cold-load-symbol (size package
)
2279 (let ((string (make-string size
)))
2280 (read-string-as-bytes *fasl-input-stream
* string
)
2281 (push-fop-table (intern string package
))))
2283 ;; I don't feel like hacking up DEFINE-COLD-FOP any more than necessary,
2284 ;; so this code is handcrafted to accept two operands.
2285 (flet ((fop-cold-symbol-in-package-save (index pname-len
)
2286 (cold-load-symbol pname-len
(ref-fop-table index
))))
2287 (dotimes (i 16) ; occupies 16 cells in the dispatch table
2288 (setf (svref *cold-fop-funs
* (+ (get 'fop-symbol-in-package-save
'opcode
) i
))
2289 #'fop-cold-symbol-in-package-save
)))
2291 (define-cold-fop (fop-lisp-symbol-save (namelen))
2292 (cold-load-symbol namelen
*cl-package
*))
2294 (define-cold-fop (fop-keyword-symbol-save (namelen))
2295 (cold-load-symbol namelen
*keyword-package
*))
2297 (define-cold-fop (fop-uninterned-symbol-save (namelen))
2298 (let ((name (make-string namelen
)))
2299 (read-string-as-bytes *fasl-input-stream
* name
)
2300 (push-fop-table (get-uninterned-symbol name
))))
2302 (define-cold-fop (fop-copy-symbol-save (index))
2303 (let* ((symbol (ref-fop-table index
))
2305 (if (symbolp symbol
)
2306 (symbol-name symbol
)
2307 (base-string-from-core
2308 (read-wordindexed symbol sb
!vm
:symbol-name-slot
)))))
2309 ;; Genesis performs additional coalescing of uninterned symbols
2310 (push-fop-table (get-uninterned-symbol name
))))
2312 ;;;; cold fops for loading packages
2314 (define-cold-fop (fop-named-package-save (namelen))
2315 (let ((name (make-string namelen
)))
2316 (read-string-as-bytes *fasl-input-stream
* name
)
2317 (push-fop-table (find-package name
))))
2319 ;;;; cold fops for loading lists
2321 ;;; Make a list of the top LENGTH things on the fop stack. The last
2322 ;;; cdr of the list is set to LAST.
2323 (defmacro cold-stack-list
(length last
)
2324 `(do* ((index ,length
(1- index
))
2325 (result ,last
(cold-cons (pop-stack) result
)))
2326 ((= index
0) result
)
2327 (declare (fixnum index
))))
2329 (define-cold-fop (fop-list)
2330 (cold-stack-list (read-byte-arg) *nil-descriptor
*))
2331 (define-cold-fop (fop-list*)
2332 (cold-stack-list (read-byte-arg) (pop-stack)))
2333 (define-cold-fop (fop-list-1)
2334 (cold-stack-list 1 *nil-descriptor
*))
2335 (define-cold-fop (fop-list-2)
2336 (cold-stack-list 2 *nil-descriptor
*))
2337 (define-cold-fop (fop-list-3)
2338 (cold-stack-list 3 *nil-descriptor
*))
2339 (define-cold-fop (fop-list-4)
2340 (cold-stack-list 4 *nil-descriptor
*))
2341 (define-cold-fop (fop-list-5)
2342 (cold-stack-list 5 *nil-descriptor
*))
2343 (define-cold-fop (fop-list-6)
2344 (cold-stack-list 6 *nil-descriptor
*))
2345 (define-cold-fop (fop-list-7)
2346 (cold-stack-list 7 *nil-descriptor
*))
2347 (define-cold-fop (fop-list-8)
2348 (cold-stack-list 8 *nil-descriptor
*))
2349 (define-cold-fop (fop-list*-
1)
2350 (cold-stack-list 1 (pop-stack)))
2351 (define-cold-fop (fop-list*-
2)
2352 (cold-stack-list 2 (pop-stack)))
2353 (define-cold-fop (fop-list*-
3)
2354 (cold-stack-list 3 (pop-stack)))
2355 (define-cold-fop (fop-list*-
4)
2356 (cold-stack-list 4 (pop-stack)))
2357 (define-cold-fop (fop-list*-
5)
2358 (cold-stack-list 5 (pop-stack)))
2359 (define-cold-fop (fop-list*-
6)
2360 (cold-stack-list 6 (pop-stack)))
2361 (define-cold-fop (fop-list*-
7)
2362 (cold-stack-list 7 (pop-stack)))
2363 (define-cold-fop (fop-list*-
8)
2364 (cold-stack-list 8 (pop-stack)))
2366 ;;;; cold fops for loading vectors
2368 (define-cold-fop (fop-base-string (len))
2369 (let ((string (make-string len
)))
2370 (read-string-as-bytes *fasl-input-stream
* string
)
2371 (base-string-to-core string
)))
2374 (define-cold-fop (fop-character-string (len))
2375 (bug "CHARACTER-STRING[~D] dumped by cross-compiler." len
))
2377 (define-cold-fop (fop-vector (size))
2378 (let* ((result (allocate-vector-object *dynamic
*
2381 sb
!vm
:simple-vector-widetag
)))
2382 (do ((index (1- size
) (1- index
)))
2384 (declare (fixnum index
))
2385 (write-wordindexed result
2386 (+ index sb
!vm
:vector-data-offset
)
2390 (define-cold-fop (fop-spec-vector)
2391 (let* ((len (read-word-arg))
2392 (type (read-byte-arg))
2393 (sizebits (aref **saetp-bits-per-length
** type
))
2394 (result (progn (aver (< sizebits
255))
2395 (allocate-vector-object *dynamic
* sizebits len type
)))
2396 (start (+ (descriptor-byte-offset result
)
2397 (ash sb
!vm
:vector-data-offset sb
!vm
:word-shift
)))
2399 (ceiling (* len sizebits
)
2400 sb
!vm
:n-byte-bits
))))
2401 (read-bigvec-as-sequence-or-die (descriptor-bytes result
)
2407 (define-cold-fop (fop-array)
2408 (let* ((rank (read-word-arg))
2409 (data-vector (pop-stack))
2410 (result (allocate-object *dynamic
*
2411 (+ sb
!vm
:array-dimensions-offset rank
)
2412 sb
!vm
:other-pointer-lowtag
)))
2413 (write-header-word result rank sb
!vm
:simple-array-widetag
)
2414 (write-wordindexed result sb
!vm
:array-fill-pointer-slot
*nil-descriptor
*)
2415 (write-wordindexed result sb
!vm
:array-data-slot data-vector
)
2416 (write-wordindexed result sb
!vm
:array-displacement-slot
*nil-descriptor
*)
2417 (write-wordindexed result sb
!vm
:array-displaced-p-slot
*nil-descriptor
*)
2418 (write-wordindexed result sb
!vm
:array-displaced-from-slot
*nil-descriptor
*)
2419 (let ((total-elements 1))
2420 (dotimes (axis rank
)
2421 (let ((dim (pop-stack)))
2422 (unless (is-fixnum-lowtag (descriptor-lowtag dim
))
2423 (error "non-fixnum dimension? (~S)" dim
))
2424 (setf total-elements
2426 (logior (ash (descriptor-high dim
)
2427 (- descriptor-low-bits
2428 sb
!vm
:n-fixnum-tag-bits
))
2429 (ash (descriptor-low dim
)
2430 sb
!vm
:n-fixnum-tag-bits
))))
2431 (write-wordindexed result
2432 (+ sb
!vm
:array-dimensions-offset axis
)
2434 (write-wordindexed result
2435 sb
!vm
:array-elements-slot
2436 (make-fixnum-descriptor total-elements
)))
2440 ;;;; cold fops for loading numbers
2442 (defmacro define-cold-number-fop
(fop &optional arglist
)
2443 ;; Invoke the ordinary warm version of this fop to cons the number.
2444 `(define-cold-fop (,fop
,arglist
) (number-to-core (,fop
,@arglist
))))
2446 (define-cold-number-fop fop-single-float
)
2447 (define-cold-number-fop fop-double-float
)
2448 (define-cold-number-fop fop-word-integer
)
2449 (define-cold-number-fop fop-byte-integer
)
2450 (define-cold-number-fop fop-complex-single-float
)
2451 (define-cold-number-fop fop-complex-double-float
)
2452 (define-cold-number-fop fop-integer
(n-bytes))
2454 (define-cold-fop (fop-ratio)
2455 (let ((den (pop-stack)))
2456 (number-pair-to-core (pop-stack) den sb
!vm
:ratio-widetag
)))
2458 (define-cold-fop (fop-complex)
2459 (let ((im (pop-stack)))
2460 (number-pair-to-core (pop-stack) im sb
!vm
:complex-widetag
)))
2462 ;;;; cold fops for calling (or not calling)
2464 (not-cold-fop fop-eval
)
2465 (not-cold-fop fop-eval-for-effect
)
2467 (defvar *load-time-value-counter
*)
2469 (define-cold-fop (fop-funcall)
2470 (unless (= (read-byte-arg) 0)
2471 (error "You can't FOP-FUNCALL arbitrary stuff in cold load."))
2472 (let ((counter *load-time-value-counter
*))
2473 (cold-push (cold-cons
2474 (cold-intern :load-time-value
)
2478 (number-to-core counter
)
2480 *current-reversed-cold-toplevels
*)
2481 (setf *load-time-value-counter
* (1+ counter
))
2482 (make-descriptor 0 0 :load-time-value counter
)))
2484 (defun finalize-load-time-value-noise ()
2485 (cold-set '*!load-time-values
*
2486 (allocate-vector-object *dynamic
*
2488 *load-time-value-counter
*
2489 sb
!vm
:simple-vector-widetag
)))
2491 (define-cold-fop (fop-funcall-for-effect)
2492 (if (= (read-byte-arg) 0)
2493 (cold-push (pop-stack)
2494 *current-reversed-cold-toplevels
*)
2495 (error "You can't FOP-FUNCALL arbitrary stuff in cold load.")))
2497 ;;;; cold fops for fixing up circularities
2499 (define-cold-fop (fop-rplaca)
2500 (let ((obj (ref-fop-table (read-word-arg)))
2501 (idx (read-word-arg)))
2502 (write-memory (cold-nthcdr idx obj
) (pop-stack))))
2504 (define-cold-fop (fop-rplacd)
2505 (let ((obj (ref-fop-table (read-word-arg)))
2506 (idx (read-word-arg)))
2507 (write-wordindexed (cold-nthcdr idx obj
) 1 (pop-stack))))
2509 (define-cold-fop (fop-svset)
2510 (let ((obj (ref-fop-table (read-word-arg)))
2511 (idx (read-word-arg)))
2512 (write-wordindexed obj
2514 (ecase (descriptor-lowtag obj
)
2515 (#.sb
!vm
:instance-pointer-lowtag
1)
2516 (#.sb
!vm
:other-pointer-lowtag
2)))
2519 (define-cold-fop (fop-structset)
2520 (let ((obj (ref-fop-table (read-word-arg)))
2521 (idx (read-word-arg)))
2522 (write-wordindexed obj
(1+ idx
) (pop-stack))))
2524 (define-cold-fop (fop-nthcdr)
2525 (cold-nthcdr (read-word-arg) (pop-stack)))
2527 (defun cold-nthcdr (index obj
)
2529 (setq obj
(read-wordindexed obj
1)))
2532 ;;;; cold fops for loading code objects and functions
2534 ;;; the names of things which have had COLD-FSET used on them already
2535 ;;; (used to make sure that we don't try to statically link a name to
2536 ;;; more than one definition)
2537 (defparameter *cold-fset-warm-names
*
2538 ;; This can't be an EQL hash table because names can be conses, e.g.
2540 (make-hash-table :test
'equal
))
2542 (define-cold-fop (fop-fset)
2543 (let* ((fn (pop-stack))
2544 (cold-name (pop-stack))
2545 (warm-name (warm-fun-name cold-name
)))
2546 (if (gethash warm-name
*cold-fset-warm-names
*)
2547 (error "duplicate COLD-FSET for ~S" warm-name
)
2548 (setf (gethash warm-name
*cold-fset-warm-names
*) t
))
2549 (static-fset cold-name fn
)))
2551 (define-cold-fop (fop-note-debug-source)
2552 (let ((debug-source (pop-stack)))
2553 (cold-push debug-source
*current-debug-sources
*)))
2555 (define-cold-fop (fop-fdefn)
2556 (cold-fdefinition-object (pop-stack)))
2559 (define-cold-fop (fop-sanctify-for-execution)
2562 ;;; Setting this variable shows what code looks like before any
2563 ;;; fixups (or function headers) are applied.
2564 #!+sb-show
(defvar *show-pre-fixup-code-p
* nil
)
2566 (defun cold-load-code (nconst code-size
)
2567 (macrolet ((pop-stack () '(pop-fop-stack)))
2568 (let* ((raw-header-n-words (+ sb
!vm
:code-constants-offset nconst
))
2570 ;; Note: we round the number of constants up to ensure
2571 ;; that the code vector will be properly aligned.
2572 (round-up raw-header-n-words
2))
2573 (des (allocate-cold-descriptor *dynamic
*
2574 (+ (ash header-n-words
2577 sb
!vm
:other-pointer-lowtag
)))
2578 (write-header-word des header-n-words sb
!vm
:code-header-widetag
)
2579 (write-wordindexed des
2580 sb
!vm
:code-code-size-slot
2581 (make-fixnum-descriptor code-size
))
2582 (write-wordindexed des sb
!vm
:code-entry-points-slot
*nil-descriptor
*)
2583 (write-wordindexed des sb
!vm
:code-debug-info-slot
(pop-stack))
2584 (when (oddp raw-header-n-words
)
2585 (write-wordindexed des
2587 (make-random-descriptor 0)))
2588 (do ((index (1- raw-header-n-words
) (1- index
)))
2589 ((< index sb
!vm
:code-constants-offset
))
2590 (write-wordindexed des index
(pop-stack)))
2591 (let* ((start (+ (descriptor-byte-offset des
)
2592 (ash header-n-words sb
!vm
:word-shift
)))
2593 (end (+ start code-size
)))
2594 (read-bigvec-as-sequence-or-die (descriptor-bytes des
)
2599 (when *show-pre-fixup-code-p
*
2600 (format *trace-output
*
2601 "~&/raw code from code-fop ~W ~W:~%"
2604 (do ((i start
(+ i sb
!vm
:n-word-bytes
)))
2606 (format *trace-output
*
2607 "/#X~8,'0x: #X~8,'0x~%"
2608 (+ i
(gspace-byte-address (descriptor-gspace des
)))
2609 (bvref-32 (descriptor-bytes des
) i
)))))
2612 (dotimes (i 16) ; occupies 16 cells in the dispatch table
2613 (setf (svref *cold-fop-funs
* (+ (get 'fop-code
'opcode
) i
))
2616 (define-cold-fop (fop-alter-code (slot))
2617 (let ((value (pop-stack))
2619 (write-wordindexed code slot value
)))
2621 (defvar *simple-fun-metadata
* (make-hash-table :test
'equalp
))
2623 ;; Return an expression that can be used to coalesce type-specifiers
2624 ;; and lambda lists attached to simple-funs. It doesn't have to be
2625 ;; a "correct" host representation, just something that preserves EQUAL-ness.
2626 (defun make-equal-comparable-thing (descriptor)
2627 (labels ((recurse (x)
2628 (cond ((cold-null x
) (return-from recurse nil
))
2629 ((is-fixnum-lowtag (descriptor-lowtag x
))
2630 (return-from recurse
(descriptor-fixnum x
)))
2631 #!+#.
(cl:if
(cl:= sb
!vm
:n-word-bits
64) '(and) '(or))
2632 ((is-other-immediate-lowtag (descriptor-lowtag x
))
2633 (let ((bits (descriptor-bits x
)))
2634 (when (= (logand bits sb
!vm
:widetag-mask
)
2635 sb
!vm
:single-float-widetag
)
2636 (return-from recurse
`(:ffloat-bits
,bits
))))))
2637 (ecase (descriptor-lowtag x
)
2638 (#.sb
!vm
:list-pointer-lowtag
2639 (cons (recurse (cold-car x
)) (recurse (cold-cdr x
))))
2640 (#.sb
!vm
:other-pointer-lowtag
2641 (ecase (logand (descriptor-bits (read-memory x
)) sb
!vm
:widetag-mask
)
2642 (#.sb
!vm
:symbol-header-widetag
2643 (if (cold-null (read-wordindexed x sb
!vm
:symbol-package-slot
))
2644 (get-or-make-uninterned-symbol
2645 (base-string-from-core
2646 (read-wordindexed x sb
!vm
:symbol-name-slot
)))
2648 #!+#.
(cl:if
(cl:= sb
!vm
:n-word-bits
32) '(and) '(or))
2649 (#.sb
!vm
:single-float-widetag
2651 ,(read-bits-wordindexed x sb
!vm
:single-float-value-slot
)))
2652 (#.sb
!vm
:double-float-widetag
2654 ,(read-bits-wordindexed x sb
!vm
:double-float-value-slot
)
2655 #!+#.
(cl:if
(cl:= sb
!vm
:n-word-bits
32) '(and) '(or))
2656 ,(read-bits-wordindexed
2657 x
(1+ sb
!vm
:double-float-value-slot
))))
2658 (#.sb
!vm
:bignum-widetag
2659 (bignum-from-core x
))
2660 (#.sb
!vm
:simple-base-string-widetag
2661 (base-string-from-core x
))
2662 ;; Why do function lambda lists have simple-vectors in them?
2663 ;; Because we expose all &OPTIONAL and &KEY default forms.
2664 ;; I think this is abstraction leakage, except possibly for
2665 ;; advertised constant defaults of NIL and such.
2666 ;; How one expresses a value as a sexpr should otherwise
2667 ;; be of no concern to a user of the code.
2668 (#.sb
!vm
:simple-vector-widetag
2669 (vector-from-core x
#'recurse
))))))
2670 ;; Return a warm symbol whose name is similar to NAME, coaelescing
2671 ;; all occurrences of #:.WHOLE. across all files, e.g.
2672 (get-or-make-uninterned-symbol (name)
2673 (let ((key `(:uninterned-symbol
,name
)))
2674 (or (gethash key
*simple-fun-metadata
*)
2675 (let ((symbol (make-symbol name
)))
2676 (setf (gethash key
*simple-fun-metadata
*) symbol
))))))
2677 (recurse descriptor
)))
2679 (define-cold-fop (fop-fun-entry)
2680 (let* ((info (pop-stack))
2682 (arglist (pop-stack))
2684 (code-object (pop-stack))
2685 (offset (calc-offset code-object
(read-word-arg)))
2686 (fn (descriptor-beyond code-object
2688 sb
!vm
:fun-pointer-lowtag
))
2689 (next (read-wordindexed code-object sb
!vm
:code-entry-points-slot
)))
2690 (unless (zerop (logand offset sb
!vm
:lowtag-mask
))
2691 (error "unaligned function entry: ~S at #X~X" name offset
))
2692 (write-wordindexed code-object sb
!vm
:code-entry-points-slot fn
)
2694 (make-other-immediate-descriptor
2695 (ash offset
(- sb
!vm
:word-shift
))
2696 sb
!vm
:simple-fun-header-widetag
))
2697 (write-wordindexed fn
2698 sb
!vm
:simple-fun-self-slot
2699 ;; KLUDGE: Wiring decisions like this in at
2700 ;; this level ("if it's an x86") instead of a
2701 ;; higher level of abstraction ("if it has such
2702 ;; and such relocation peculiarities (which
2703 ;; happen to be confined to the x86)") is bad.
2704 ;; It would be nice if the code were instead
2705 ;; conditional on some more descriptive
2706 ;; feature, :STICKY-CODE or
2707 ;; :LOAD-GC-INTERACTION or something.
2709 ;; FIXME: The X86 definition of the function
2710 ;; self slot breaks everything object.tex says
2711 ;; about it. (As far as I can tell, the X86
2712 ;; definition makes it a pointer to the actual
2713 ;; code instead of a pointer back to the object
2714 ;; itself.) Ask on the mailing list whether
2715 ;; this is documented somewhere, and if not,
2716 ;; try to reverse engineer some documentation.
2718 ;; a pointer back to the function object, as
2719 ;; described in CMU CL
2720 ;; src/docs/internals/object.tex
2723 ;; KLUDGE: a pointer to the actual code of the
2724 ;; object, as described nowhere that I can find
2726 (make-random-descriptor
2727 (+ (descriptor-bits fn
)
2728 (- (ash sb
!vm
:simple-fun-code-offset
2730 ;; FIXME: We should mask out the type
2731 ;; bits, not assume we know what they
2732 ;; are and subtract them out this way.
2733 sb
!vm
:fun-pointer-lowtag
))))
2734 (write-wordindexed fn sb
!vm
:simple-fun-next-slot next
)
2735 (write-wordindexed fn sb
!vm
:simple-fun-name-slot name
)
2736 (flet ((coalesce (sexpr) ; a warm symbol or a cold cons tree
2737 (if (symbolp sexpr
) ; will be cold-interned automatically
2739 (let ((representation (make-equal-comparable-thing sexpr
)))
2740 (or (gethash representation
*simple-fun-metadata
*)
2741 (setf (gethash representation
*simple-fun-metadata
*)
2743 (write-wordindexed fn sb
!vm
:simple-fun-arglist-slot
(coalesce arglist
))
2744 (write-wordindexed fn sb
!vm
:simple-fun-type-slot
(coalesce type
)))
2745 (write-wordindexed fn sb
!vm
::simple-fun-info-slot info
)
2749 (define-cold-fop (fop-symbol-tls-fixup)
2750 (let* ((symbol (pop-stack))
2752 (code-object (pop-stack)))
2753 (do-cold-fixup code-object
(read-word-arg) (ensure-symbol-tls-index symbol
)
2757 (define-cold-fop (fop-foreign-fixup)
2758 (let* ((kind (pop-stack))
2759 (code-object (pop-stack))
2760 (len (read-byte-arg))
2761 (sym (make-string len
)))
2762 (read-string-as-bytes *fasl-input-stream
* sym
)
2764 (let ((offset (read-word-arg))
2765 (value (dyncore-note-symbol sym nil
)))
2766 (do-cold-fixup code-object offset value kind
))
2767 #!-
(and) (format t
"Bad non-plt fixup: ~S~S~%" sym code-object
)
2769 (let ((offset (read-word-arg))
2770 (value (cold-foreign-symbol-address sym
)))
2771 (do-cold-fixup code-object offset value kind
))
2775 (define-cold-fop (fop-foreign-dataref-fixup)
2776 (let* ((kind (pop-stack))
2777 (code-object (pop-stack))
2778 (len (read-byte-arg))
2779 (sym (make-string len
)))
2780 #!-sb-dynamic-core
(declare (ignore code-object
))
2781 (read-string-as-bytes *fasl-input-stream
* sym
)
2783 (let ((offset (read-word-arg))
2784 (value (dyncore-note-symbol sym t
)))
2785 (do-cold-fixup code-object offset value kind
)
2789 (maphash (lambda (k v
)
2790 (format *error-output
* "~&~S = #X~8X~%" k v
))
2791 *cold-foreign-symbol-table
*)
2792 (error "shared foreign symbol in cold load: ~S (~S)" sym kind
))))
2794 (define-cold-fop (fop-assembler-code)
2795 (let* ((length (read-word-arg))
2797 ;; Note: we round the number of constants up to ensure that
2798 ;; the code vector will be properly aligned.
2799 (round-up sb
!vm
:code-constants-offset
2))
2800 (des (allocate-cold-descriptor *read-only
*
2801 (+ (ash header-n-words
2804 sb
!vm
:other-pointer-lowtag
)))
2805 (write-header-word des header-n-words sb
!vm
:code-header-widetag
)
2806 (write-wordindexed des
2807 sb
!vm
:code-code-size-slot
2808 (make-fixnum-descriptor length
))
2809 (write-wordindexed des sb
!vm
:code-entry-points-slot
*nil-descriptor
*)
2810 (write-wordindexed des sb
!vm
:code-debug-info-slot
*nil-descriptor
*)
2812 (let* ((start (+ (descriptor-byte-offset des
)
2813 (ash header-n-words sb
!vm
:word-shift
)))
2814 (end (+ start length
)))
2815 (read-bigvec-as-sequence-or-die (descriptor-bytes des
)
2821 (define-cold-fop (fop-assembler-routine)
2822 (let* ((routine (pop-stack))
2824 (offset (calc-offset des
(read-word-arg))))
2825 (record-cold-assembler-routine
2827 (+ (logandc2 (descriptor-bits des
) sb
!vm
:lowtag-mask
) offset
))
2830 (define-cold-fop (fop-assembler-fixup)
2831 (let* ((routine (pop-stack))
2833 (code-object (pop-stack))
2834 (offset (read-word-arg)))
2835 (record-cold-assembler-fixup routine code-object offset kind
)
2838 (define-cold-fop (fop-code-object-fixup)
2839 (let* ((kind (pop-stack))
2840 (code-object (pop-stack))
2841 (offset (read-word-arg))
2842 (value (descriptor-bits code-object
)))
2843 (do-cold-fixup code-object offset value kind
)
2846 ;;;; sanity checking space layouts
2848 (defun check-spaces ()
2849 ;;; Co-opt type machinery to check for intersections...
2851 (flet ((check (start end space
)
2852 (unless (< start end
)
2853 (error "Bogus space: ~A" space
))
2854 (let ((type (specifier-type `(integer ,start
,end
))))
2855 (dolist (other types
)
2856 (unless (eq *empty-type
* (type-intersection (cdr other
) type
))
2857 (error "Space overlap: ~A with ~A" space
(car other
))))
2858 (push (cons space type
) types
))))
2859 (check sb
!vm
:read-only-space-start sb
!vm
:read-only-space-end
:read-only
)
2860 (check sb
!vm
:static-space-start sb
!vm
:static-space-end
:static
)
2862 (check sb
!vm
:dynamic-space-start sb
!vm
:dynamic-space-end
:dynamic
)
2865 (check sb
!vm
:dynamic-0-space-start sb
!vm
:dynamic-0-space-end
:dynamic-0
)
2866 (check sb
!vm
:dynamic-1-space-start sb
!vm
:dynamic-1-space-end
:dynamic-1
))
2868 (check sb
!vm
:linkage-table-space-start sb
!vm
:linkage-table-space-end
:linkage-table
))))
2870 ;;;; emitting C header file
2872 (defun tailwise-equal (string tail
)
2873 (and (>= (length string
) (length tail
))
2874 (string= string tail
:start1
(- (length string
) (length tail
)))))
2876 (defun write-boilerplate ()
2879 '("This is a machine-generated file. Please do not edit it by hand."
2880 "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
2882 "This file contains low-level information about the"
2883 "internals of a particular version and configuration"
2884 "of SBCL. It is used by the C compiler to create a runtime"
2885 "support environment, an executable program in the host"
2886 "operating system's native format, which can then be used to"
2887 "load and run 'core' files, which are basically programs"
2888 "in SBCL's own format."))
2889 (format t
" *~@[ ~A~]~%" line
))
2892 (defun c-name (string &optional strip
)
2894 (substitute-if #\_
(lambda (c) (member c
'(#\-
#\
/ #\%
)))
2895 (remove-if (lambda (c) (position c strip
))
2898 (defun c-symbol-name (symbol &optional strip
)
2899 (c-name (symbol-name symbol
) strip
))
2901 (defun write-makefile-features ()
2902 ;; propagating *SHEBANG-FEATURES* into the Makefiles
2903 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2904 sb-cold
:*shebang-features
*)
2906 (format t
"LISP_FEATURE_~A=1~%" shebang-feature-name
)))
2908 (defun write-config-h ()
2909 ;; propagating *SHEBANG-FEATURES* into C-level #define's
2910 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2911 sb-cold
:*shebang-features
*)
2913 (format t
"#define LISP_FEATURE_~A~%" shebang-feature-name
))
2915 ;; and miscellaneous constants
2916 (format t
"#define SBCL_VERSION_STRING ~S~%"
2917 (sb!xc
:lisp-implementation-version
))
2918 (format t
"#define CORE_MAGIC 0x~X~%" core-magic
)
2919 (format t
"#ifndef LANGUAGE_ASSEMBLY~2%")
2920 (format t
"#define LISPOBJ(x) ((lispobj)x)~2%")
2921 (format t
"#else /* LANGUAGE_ASSEMBLY */~2%")
2922 (format t
"#define LISPOBJ(thing) thing~2%")
2923 (format t
"#endif /* LANGUAGE_ASSEMBLY */~2%")
2926 (defun write-constants-h ()
2927 ;; writing entire families of named constants
2928 (let ((constants nil
))
2929 (dolist (package-name '( ;; Even in CMU CL, constants from VM
2930 ;; were automatically propagated
2931 ;; into the runtime.
2933 ;; In SBCL, we also propagate various
2934 ;; magic numbers related to file format,
2935 ;; which live here instead of SB!VM.
2937 (do-external-symbols (symbol (find-package package-name
))
2938 (when (constantp symbol
)
2939 (let ((name (symbol-name symbol
)))
2940 (labels ( ;; shared machinery
2941 (record (string priority suffix
)
2944 (symbol-value symbol
)
2946 (documentation symbol
'variable
))
2948 ;; machinery for old-style CMU CL Lisp-to-C
2949 ;; arbitrary renaming, being phased out in favor of
2950 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
2952 (record-with-munged-name (prefix string priority
)
2953 (record (concatenate
2956 (delete #\-
(string-capitalize string
)))
2959 (maybe-record-with-munged-name (tail prefix priority
)
2960 (when (tailwise-equal name tail
)
2961 (record-with-munged-name prefix
2966 ;; machinery for new-style SBCL Lisp-to-C naming
2967 (record-with-translated-name (priority large
)
2968 (record (c-name name
) priority
2970 #!+(and win32 x86-64
) "LLU"
2971 #!-
(and win32 x86-64
) "LU"
2973 (maybe-record-with-translated-name (suffixes priority
&key large
)
2974 (when (some (lambda (suffix)
2975 (tailwise-equal name suffix
))
2977 (record-with-translated-name priority large
))))
2978 (maybe-record-with-translated-name '("-LOWTAG") 0)
2979 (maybe-record-with-translated-name '("-WIDETAG" "-SHIFT") 1)
2980 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
2981 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
2982 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
2983 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
2984 (maybe-record-with-translated-name '("-SIZE" "-INTERRUPTS") 6)
2985 (maybe-record-with-translated-name '("-START" "-END" "-PAGE-BYTES"
2986 "-CARD-BYTES" "-GRANULARITY")
2988 (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 8)
2989 (maybe-record-with-translated-name '("-CORE-SPACE-ID") 9)
2990 (maybe-record-with-translated-name '("-CORE-SPACE-ID-FLAG") 9)
2991 (maybe-record-with-translated-name '("-GENERATION+") 10))))))
2992 ;; KLUDGE: these constants are sort of important, but there's no
2993 ;; pleasing way to inform the code above about them. So we fake
2994 ;; it for now. nikodemus on #lisp (2004-08-09) suggested simply
2995 ;; exporting every numeric constant from SB!VM; that would work,
2996 ;; but the C runtime would have to be altered to use Lisp-like names
2997 ;; rather than the munged names currently exported. --njf, 2004-08-09
2998 (dolist (c '(sb!vm
:n-word-bits sb
!vm
:n-word-bytes
2999 sb
!vm
:n-lowtag-bits sb
!vm
:lowtag-mask
3000 sb
!vm
:n-widetag-bits sb
!vm
:widetag-mask
3001 sb
!vm
:n-fixnum-tag-bits sb
!vm
:fixnum-tag-mask
))
3002 (push (list (c-symbol-name c
)
3003 -
1 ; invent a new priority
3008 ;; One more symbol that doesn't fit into the code above.
3009 (let ((c 'sb
!impl
::+magic-hash-vector-value
+))
3010 (push (list (c-symbol-name c
)
3013 #!+(and win32 x86-64
) "LLU"
3014 #!-
(and win32 x86-64
) "LU"
3019 (lambda (const1 const2
)
3020 (if (= (second const1
) (second const2
))
3021 (if (= (third const1
) (third const2
))
3022 (string< (first const1
) (first const2
))
3023 (< (third const1
) (third const2
)))
3024 (< (second const1
) (second const2
))))))
3025 (let ((prev-priority (second (car constants
))))
3026 (dolist (const constants
)
3027 (destructuring-bind (name priority value suffix doc
) const
3028 (unless (= prev-priority priority
)
3030 (setf prev-priority priority
))
3031 (when (minusp value
)
3032 (error "stub: negative values unsupported"))
3033 (format t
"#define ~A ~A~A /* 0x~X ~@[ -- ~A ~]*/~%" name value suffix value doc
))))
3036 ;; writing information about internal errors
3037 ;; Assembly code needs only the constants for UNDEFINED_[ALIEN_]FUN_ERROR
3038 ;; but to avoid imparting that knowledge here, we'll expose all error
3039 ;; number constants except for OBJECT-NOT-<x>-ERROR ones.
3040 (loop for interr across sb
!c
:*backend-internal-errors
*
3042 when
(stringp (car interr
))
3043 do
(format t
"#define ~A ~D~%" (c-symbol-name (cdr interr
)) i
))
3044 ;; C code needs strings for describe_internal_error()
3045 (format t
"#define INTERNAL_ERROR_NAMES ~{\\~%~S~^, ~}~2%"
3046 (map 'list
'sb
!kernel
::!c-stringify-internal-error
3047 sb
!c
:*backend-internal-errors
*))
3049 ;; I'm not really sure why this is in SB!C, since it seems
3050 ;; conceptually like something that belongs to SB!VM. In any case,
3051 ;; it's needed C-side.
3052 (format t
"#define BACKEND_PAGE_BYTES ~DLU~%" sb
!c
:*backend-page-bytes
*)
3056 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
3057 ;; platforms. If we export this from the SB!VM package, it gets
3058 ;; written out as #define trap_PseudoAtomic, which is confusing as
3059 ;; the runtime treats trap_ as the prefix for illegal instruction
3060 ;; type things. We therefore don't export it, but instead do
3062 (when (boundp 'sb
!vm
::pseudo-atomic-trap
)
3064 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
3065 sb
!vm
::pseudo-atomic-trap
)
3067 ;; possibly this is another candidate for a rename (to
3068 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
3069 ;; [possibly applicable to other platforms])
3072 (format t
"#define GC_SAFEPOINT_PAGE_ADDR ((void*)0x~XUL) /* ~:*~A */~%"
3073 sb
!vm
:gc-safepoint-page-addr
)
3075 (dolist (symbol '(sb!vm
::float-traps-byte
3076 sb
!vm
::float-exceptions-byte
3077 sb
!vm
::float-sticky-bits
3078 sb
!vm
::float-rounding-mode
))
3079 (format t
"#define ~A_POSITION ~A /* ~:*0x~X */~%"
3080 (c-symbol-name symbol
)
3081 (sb!xc
:byte-position
(symbol-value symbol
)))
3082 (format t
"#define ~A_MASK 0x~X /* ~:*~A */~%"
3083 (c-symbol-name symbol
)
3084 (sb!xc
:mask-field
(symbol-value symbol
) -
1))))
3087 (defun write-tagnames-h (&optional
(out *standard-output
*))
3089 ((pretty-name (symbol strip
)
3090 (let ((name (string-downcase symbol
)))
3091 (substitute #\Space
#\-
3092 (subseq name
0 (- (length name
) (length strip
))))))
3093 (list-sorted-tags (tail)
3094 (loop for symbol being the external-symbols of
"SB!VM"
3095 when
(and (constantp symbol
)
3096 (tailwise-equal (string symbol
) tail
))
3097 collect symbol into tags
3098 finally
(return (sort tags
#'< :key
#'symbol-value
))))
3099 (write-tags (kind limit ash-count
)
3100 (format out
"~%static const char *~(~A~)_names[] = {~%"
3102 (let ((tags (list-sorted-tags kind
)))
3104 (if (eql i
(ash (or (symbol-value (first tags
)) -
1) ash-count
))
3105 (format out
" \"~A\"" (pretty-name (pop tags
) kind
))
3106 (format out
" \"unknown [~D]\"" i
))
3107 (unless (eql i
(1- limit
))
3108 (write-string "," out
))
3110 (write-line "};" out
)))
3111 (write-tags "-LOWTAG" sb
!vm
:lowtag-limit
0)
3112 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
3113 ;; ending with the same 2 bits. (#b10)
3114 (write-tags "-WIDETAG" (ash (1+ sb
!vm
:widetag-mask
) -
2) -
2))
3115 ;; Inform print_otherptr() of all array types that it's too dumb to print
3116 (let ((array-type-bits (make-array 32 :initial-element
0)))
3118 (multiple-value-bind (ofs bit
) (floor b
8)
3119 (setf (aref array-type-bits ofs
) (ash 1 bit
)))))
3120 (dovector (saetp sb
!vm
:*specialized-array-element-type-properties
*)
3121 (unless (or (typep (sb!vm
:saetp-ctype saetp
) 'character-set-type
)
3122 (eq (sb!vm
:saetp-specifier saetp
) t
))
3123 (toggle (sb!vm
:saetp-typecode saetp
))
3124 (awhen (sb!vm
:saetp-complex-typecode saetp
) (toggle it
)))))
3126 "~%static unsigned char unprintable_array_types[32] = ~% {~{~d~^,~}};~%"
3127 (coerce array-type-bits
'list
)))
3130 (defun write-primitive-object (obj)
3131 ;; writing primitive object layouts
3132 (format t
"#ifndef LANGUAGE_ASSEMBLY~2%")
3135 (c-name (string-downcase (string (sb!vm
:primitive-object-name obj
)))))
3136 (when (sb!vm
:primitive-object-widetag obj
)
3137 (format t
" lispobj header;~%"))
3138 (dolist (slot (sb!vm
:primitive-object-slots obj
))
3139 (format t
" ~A ~A~@[[1]~];~%"
3140 (getf (sb!vm
:slot-options slot
) :c-type
"lispobj")
3141 (c-name (string-downcase (string (sb!vm
:slot-name slot
))))
3142 (sb!vm
:slot-rest-p slot
)))
3144 (format t
"#else /* LANGUAGE_ASSEMBLY */~2%")
3145 (format t
"/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
3146 (format t
" * so they work directly on tagged addresses. */~2%")
3147 (let ((name (sb!vm
:primitive-object-name obj
))
3148 (lowtag (or (symbol-value (sb!vm
:primitive-object-lowtag obj
))
3150 (dolist (slot (sb!vm
:primitive-object-slots obj
))
3151 (format t
"#define ~A_~A_OFFSET ~D~%"
3152 (c-symbol-name name
)
3153 (c-symbol-name (sb!vm
:slot-name slot
))
3154 (- (* (sb!vm
:slot-offset slot
) sb
!vm
:n-word-bytes
) lowtag
)))
3156 (format t
"#endif /* LANGUAGE_ASSEMBLY */~2%"))
3158 (defun write-structure-object (dd)
3159 (flet ((cstring (designator)
3160 (c-name (string-downcase (string designator
)))))
3161 (format t
"#ifndef LANGUAGE_ASSEMBLY~2%")
3162 (format t
"struct ~A {~%" (cstring (dd-name dd
)))
3163 (format t
" lispobj header;~%")
3164 ;; "self layout" slots are named '_layout' instead of 'layout' so that
3165 ;; classoid's expressly declared layout isn't renamed as a special-case.
3166 (format t
" lispobj _layout;~%")
3167 #!-interleaved-raw-slots
3169 ;; Note: if the structure has no raw slots, but has an even number of
3170 ;; ordinary slots (incl. layout, sans header), then the last slot gets
3171 ;; named 'raw_slot_paddingN' (not 'paddingN')
3172 ;; The choice of name is mildly disturbing, but harmless.
3173 (dolist (slot (dd-slots dd
))
3174 (when (eq t
(dsd-raw-type slot
))
3175 (format t
" lispobj ~A;~%" (cstring (dsd-name slot
)))))
3176 (unless (oddp (+ (dd-length dd
) (dd-raw-length dd
)))
3177 (format t
" lispobj raw_slot_padding;~%"))
3178 (dotimes (n (dd-raw-length dd
))
3179 (format t
" lispobj raw~D;~%" (- (dd-raw-length dd
) n
1))))
3180 #!+interleaved-raw-slots
3182 (dolist (slot (dd-slots dd
))
3183 (cond ((eq t
(dsd-raw-type slot
))
3184 (loop while
(< index
(dsd-index slot
))
3186 (format t
" lispobj raw_slot_padding~A;~%" index
)
3188 (format t
" lispobj ~A;~%" (cstring (dsd-name slot
)))
3190 (unless (oddp (dd-length dd
))
3191 (format t
" lispobj end_padding;~%")))
3193 (format t
"#endif /* LANGUAGE_ASSEMBLY */~2%")))
3195 (defun write-static-symbols ()
3196 (dolist (symbol (cons nil sb
!vm
:*static-symbols
*))
3197 ;; FIXME: It would be nice to use longer names than NIL and
3198 ;; (particularly) T in #define statements.
3199 (format t
"#define ~A LISPOBJ(0x~X)~%"
3200 ;; FIXME: It would be nice not to need to strip anything
3201 ;; that doesn't get stripped always by C-SYMBOL-NAME.
3202 (c-symbol-name symbol
"%*.!")
3203 (if *static
* ; if we ran GENESIS
3204 ;; We actually ran GENESIS, use the real value.
3205 (descriptor-bits (cold-intern symbol
))
3206 ;; We didn't run GENESIS, so guess at the address.
3207 (+ sb
!vm
:static-space-start
3209 sb
!vm
:other-pointer-lowtag
3210 (if symbol
(sb!vm
:static-symbol-offset symbol
) 0))))))
3213 ;;;; writing map file
3215 ;;; Write a map file describing the cold load. Some of this
3216 ;;; information is subject to change due to relocating GC, but even so
3217 ;;; it can be very handy when attempting to troubleshoot the early
3218 ;;; stages of cold load.
3220 (let ((*print-pretty
* nil
)
3221 (*print-case
* :upcase
))
3222 (format t
"assembler routines defined in core image:~2%")
3223 (dolist (routine (sort (copy-list *cold-assembler-routines
*) #'<
3225 (format t
"#X~8,'0X: ~S~%" (cdr routine
) (car routine
)))
3228 (maphash (lambda (name fdefn
)
3229 (let ((fun (read-wordindexed fdefn
3230 sb
!vm
:fdefn-fun-slot
)))
3231 (if (= (descriptor-bits fun
)
3232 (descriptor-bits *nil-descriptor
*))
3234 (let ((addr (read-wordindexed
3235 fdefn sb
!vm
:fdefn-raw-addr-slot
)))
3236 (push (cons name
(descriptor-bits addr
))
3238 *cold-fdefn-objects
*)
3239 (format t
"~%~|~%initially defined functions:~2%")
3240 (setf funs
(sort funs
#'< :key
#'cdr
))
3242 (format t
"0x~8,'0X: ~S #X~8,'0X~%" (cdr info
) (car info
)
3243 (- (cdr info
) #x17
)))
3246 (a note about initially undefined function references: These functions
3247 are referred to by code which is installed by GENESIS, but they are not
3248 installed by GENESIS. This is not necessarily a problem; functions can
3249 be defined later, by cold init toplevel forms, or in files compiled and
3250 loaded at warm init, or elsewhere. As long as they are defined before
3251 they are called, everything should be OK. Things are also OK if the
3252 cross-compiler knew their inline definition and used that everywhere
3253 that they were called before the out-of-line definition is installed,
3254 as is fairly common for structure accessors.)
3255 initially undefined function references:~2%")
3257 (setf undefs
(sort undefs
#'string
< :key
#'fun-name-block-name
))
3258 (dolist (name undefs
)
3259 (format t
"~8,'0X: ~S~%"
3260 (descriptor-bits (gethash name
*cold-fdefn-objects
*))
3263 (format t
"~%~|~%layout names:~2%")
3265 (maphash (lambda (name gorp
)
3266 (declare (ignore name
))
3267 (stuff (cons (descriptor-bits (car gorp
))
3270 (dolist (x (sort (stuff) #'< :key
#'car
))
3271 (apply #'format t
"~8,'0X: ~S[~D]~%~10T~S~%" x
))))
3275 ;;;; writing core file
3277 (defvar *core-file
*)
3278 (defvar *data-page
*)
3280 ;;; magic numbers to identify entries in a core file
3282 ;;; (In case you were wondering: No, AFAIK there's no special magic about
3283 ;;; these which requires them to be in the 38xx range. They're just
3284 ;;; arbitrary words, tested not for being in a particular range but just
3285 ;;; for equality. However, if you ever need to look at a .core file and
3286 ;;; figure out what's going on, it's slightly convenient that they're
3287 ;;; all in an easily recognizable range, and displacing the range away from
3288 ;;; zero seems likely to reduce the chance that random garbage will be
3289 ;;; misinterpreted as a .core file.)
3290 (defconstant build-id-core-entry-type-code
3860)
3291 (defconstant new-directory-core-entry-type-code
3861)
3292 (defconstant initial-fun-core-entry-type-code
3863)
3293 (defconstant page-table-core-entry-type-code
3880)
3294 (defconstant end-core-entry-type-code
3840)
3296 (declaim (ftype (function (sb!vm
:word
) sb
!vm
:word
) write-word
))
3297 (defun write-word (num)
3298 (ecase sb
!c
:*backend-byte-order
*
3300 (dotimes (i sb
!vm
:n-word-bytes
)
3301 (write-byte (ldb (byte 8 (* i
8)) num
) *core-file
*)))
3303 (dotimes (i sb
!vm
:n-word-bytes
)
3304 (write-byte (ldb (byte 8 (* (- (1- sb
!vm
:n-word-bytes
) i
) 8)) num
)
3308 (defun advance-to-page ()
3309 (force-output *core-file
*)
3310 (file-position *core-file
*
3311 (round-up (file-position *core-file
*)
3312 sb
!c
:*backend-page-bytes
*)))
3314 (defun output-gspace (gspace)
3315 (force-output *core-file
*)
3316 (let* ((posn (file-position *core-file
*))
3317 (bytes (* (gspace-free-word-index gspace
) sb
!vm
:n-word-bytes
))
3318 (pages (ceiling bytes sb
!c
:*backend-page-bytes
*))
3319 (total-bytes (* pages sb
!c
:*backend-page-bytes
*)))
3321 (file-position *core-file
*
3322 (* sb
!c
:*backend-page-bytes
* (1+ *data-page
*)))
3324 "writing ~S byte~:P [~S page~:P] from ~S~%"
3330 ;; Note: It is assumed that the GSPACE allocation routines always
3331 ;; allocate whole pages (of size *target-page-size*) and that any
3332 ;; empty gspace between the free pointer and the end of page will
3333 ;; be zero-filled. This will always be true under Mach on machines
3334 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3336 (write-bigvec-as-sequence (gspace-bytes gspace
)
3340 (force-output *core-file
*)
3341 (file-position *core-file
* posn
)
3343 ;; Write part of a (new) directory entry which looks like this:
3344 ;; GSPACE IDENTIFIER
3349 (write-word (gspace-identifier gspace
))
3350 (write-word (gspace-free-word-index gspace
))
3351 (write-word *data-page
*)
3352 (multiple-value-bind (floor rem
)
3353 (floor (gspace-byte-address gspace
) sb
!c
:*backend-page-bytes
*)
3358 (incf *data-page
* pages
)))
3360 ;;; Create a core file created from the cold loaded image. (This is
3361 ;;; the "initial core file" because core files could be created later
3362 ;;; by executing SAVE-LISP in a running system, perhaps after we've
3363 ;;; added some functionality to the system.)
3364 (declaim (ftype (function (string)) write-initial-core-file
))
3365 (defun write-initial-core-file (filename)
3367 (let ((filenamestring (namestring filename
))
3371 "[building initial core file in ~S: ~%"
3375 (with-open-file (*core-file
* filenamestring
3377 :element-type
'(unsigned-byte 8)
3378 :if-exists
:rename-and-delete
)
3380 ;; Write the magic number.
3381 (write-word core-magic
)
3383 ;; Write the build ID.
3384 (write-word build-id-core-entry-type-code
)
3385 (let ((build-id (with-open-file (s "output/build-id.tmp")
3387 (declare (type simple-string build-id
))
3388 (/show build-id
(length build-id
))
3389 ;; Write length of build ID record: BUILD-ID-CORE-ENTRY-TYPE-CODE
3390 ;; word, this length word, and one word for each char of BUILD-ID.
3391 (write-word (+ 2 (length build-id
)))
3392 (dovector (char build-id
)
3393 ;; (We write each character as a word in order to avoid
3394 ;; having to think about word alignment issues in the
3395 ;; sbcl-0.7.8 version of coreparse.c.)
3396 (write-word (sb!xc
:char-code char
))))
3398 ;; Write the New Directory entry header.
3399 (write-word new-directory-core-entry-type-code
)
3400 (write-word 17) ; length = (5 words/space) * 3 spaces + 2 for header.
3402 (output-gspace *read-only
*)
3403 (output-gspace *static
*)
3404 (output-gspace *dynamic
*)
3406 ;; Write the initial function.
3407 (write-word initial-fun-core-entry-type-code
)
3409 (let* ((cold-name (cold-intern '!cold-init
))
3410 (cold-fdefn (cold-fdefinition-object cold-name
))
3411 (initial-fun (read-wordindexed cold-fdefn
3412 sb
!vm
:fdefn-fun-slot
)))
3414 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
3415 (descriptor-bits initial-fun
))
3416 (write-word (descriptor-bits initial-fun
)))
3418 ;; Write the End entry.
3419 (write-word end-core-entry-type-code
)
3422 (format t
"done]~%")
3424 (/show
"leaving WRITE-INITIAL-CORE-FILE")
3427 ;;;; the actual GENESIS function
3429 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3430 ;;; and/or information about a Lisp core, therefrom.
3432 ;;; input file arguments:
3433 ;;; SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
3434 ;;; *tab* *characters* *converted* *to* *spaces*. (We push
3435 ;;; responsibility for removing tabs out to the caller it's
3436 ;;; trivial to remove them using UNIX command line tools like
3437 ;;; sed, whereas it's a headache to do it portably in Lisp because
3438 ;;; #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
3439 ;;; a core file cannot be built (but a C header file can be).
3441 ;;; output files arguments (any of which may be NIL to suppress output):
3442 ;;; CORE-FILE-NAME gets a Lisp core.
3443 ;;; C-HEADER-FILE-NAME gets a C header file, traditionally called
3444 ;;; internals.h, which is used by the C compiler when constructing
3445 ;;; the executable which will load the core.
3446 ;;; MAP-FILE-NAME gets (?) a map file. (dunno about this -- WHN 19990815)
3448 ;;; FIXME: GENESIS doesn't belong in SB!VM. Perhaps in %KERNEL for now,
3449 ;;; perhaps eventually in SB-LD or SB-BOOT.
3450 (defun sb!vm
:genesis
(&key
3452 symbol-table-file-name
3456 #+nil
(list-objects t
))
3458 (declare (ignorable symbol-table-file-name
))
3461 "~&beginning GENESIS, ~A~%"
3463 ;; Note: This output summarizing what we're doing is
3464 ;; somewhat telegraphic in style, not meant to imply that
3465 ;; we're not e.g. also creating a header file when we
3467 (format nil
"creating core ~S" core-file-name
)
3468 (format nil
"creating headers in ~S" c-header-dir-name
)))
3470 (let ((*cold-foreign-symbol-table
* (make-hash-table :test
'equal
)))
3473 (when core-file-name
3474 (if symbol-table-file-name
3475 (load-cold-foreign-symbol-table symbol-table-file-name
)
3476 (error "can't output a core file without symbol table file input")))
3480 (setf (gethash (extern-alien-name "undefined_tramp")
3481 *cold-foreign-symbol-table
*)
3482 (dyncore-note-symbol "undefined_tramp" nil
))
3483 (dyncore-note-symbol "undefined_alien_function" nil
))
3485 ;; Now that we've successfully read our only input file (by
3486 ;; loading the symbol table, if any), it's a good time to ensure
3487 ;; that there'll be someplace for our output files to go when
3489 (flet ((frob (filename)
3491 (ensure-directories-exist filename
:verbose t
))))
3492 (frob core-file-name
)
3493 (frob map-file-name
))
3495 ;; (This shouldn't matter in normal use, since GENESIS normally
3496 ;; only runs once in any given Lisp image, but it could reduce
3497 ;; confusion if we ever experiment with running, tweaking, and
3498 ;; rerunning genesis interactively.)
3499 (do-all-symbols (sym)
3500 (remprop sym
'cold-intern-info
))
3504 (let* ((*foreign-symbol-placeholder-value
* (if core-file-name nil
0))
3505 (*load-time-value-counter
* 0)
3506 (*cold-fdefn-objects
* (make-hash-table :test
'equal
))
3507 (*cold-symbols
* (make-hash-table :test
'eql
)) ; integer keys
3508 (*cold-package-symbols
* (make-hash-table :test
'equal
)) ; string keys
3509 (pkg-metadata (sb-cold:read-from-file
"package-data-list.lisp-expr"))
3510 (*read-only
* (make-gspace :read-only
3511 read-only-core-space-id
3512 sb
!vm
:read-only-space-start
))
3513 (*static
* (make-gspace :static
3514 static-core-space-id
3515 sb
!vm
:static-space-start
))
3516 (*dynamic
* (make-gspace :dynamic
3517 dynamic-core-space-id
3518 #!+gencgc sb
!vm
:dynamic-space-start
3519 #!-gencgc sb
!vm
:dynamic-0-space-start
))
3520 ;; There's a cyclic dependency here: NIL refers to a package;
3521 ;; a package needs its layout which needs others layouts
3522 ;; which refer to NIL, which refers to a package ...
3523 ;; Break the cycle by preallocating packages without a layout.
3524 ;; This avoids having to track any symbols created prior to
3525 ;; creation of packages, since packages are primordial.
3527 (dolist (name (list* "COMMON-LISP" "COMMON-LISP-USER" "KEYWORD"
3528 (mapcar #'sb-cold
:package-data-name
3530 (gethash "COMMON-LISP" *cold-package-symbols
*))
3531 (setf (gethash name
*cold-package-symbols
*)
3532 (cons (allocate-structure-object
3533 *dynamic
* (layout-length (find-layout 'package
))
3534 (make-fixnum-descriptor 0))
3535 (cons nil nil
))))) ; (externals . internals)
3536 (*nil-descriptor
* (make-nil-descriptor target-cl-pkg-info
))
3537 (*current-reversed-cold-toplevels
* *nil-descriptor
*)
3538 (*current-debug-sources
* *nil-descriptor
*)
3539 (*unbound-marker
* (make-other-immediate-descriptor
3541 sb
!vm
:unbound-marker-widetag
))
3542 *cold-assembler-fixups
*
3543 *cold-assembler-routines
*
3544 #!+x86
(*load-time-code-fixups
* (make-hash-table)))
3546 ;; Prepare for cold load.
3547 (initialize-non-nil-symbols)
3548 (initialize-layouts)
3549 (initialize-packages
3550 ;; docstrings are set in src/cold/warm. It would work to do it here,
3551 ;; but seems preferable not to saddle Genesis with such responsibility.
3552 (list* (sb-cold:make-package-data
:name
"COMMON-LISP" :doc nil
)
3553 (sb-cold:make-package-data
:name
"KEYWORD" :doc nil
)
3554 (sb-cold:make-package-data
:name
"COMMON-LISP-USER" :doc nil
3555 :use
'("COMMON-LISP"
3556 ;; ANSI encourages us to put extension packages
3557 ;; in the USE list of COMMON-LISP-USER.
3558 "SB!ALIEN" "SB!DEBUG" "SB!EXT" "SB!GRAY" "SB!PROFILE"))
3560 (initialize-static-fns)
3562 ;; Initialize the *COLD-SYMBOLS* system with the information
3563 ;; from common-lisp-exports.lisp-expr.
3564 ;; Packages whose names match SB!THING were set up on the host according
3565 ;; to "package-data-list.lisp-expr" which expresses the desired target
3566 ;; package configuration, so we can just mirror the host into the target.
3567 ;; But by waiting to observe calls to COLD-INTERN that occur during the
3568 ;; loading of the cross-compiler's outputs, it is possible to rid the
3569 ;; target of accidental leftover symbols, not that it wouldn't also be
3570 ;; a good idea to clean up package-data-list once in a while.
3571 (dolist (exported-name
3572 (sb-cold:read-from-file
"common-lisp-exports.lisp-expr"))
3573 (cold-intern (intern exported-name
*cl-package
*) :access
:external
))
3576 (dolist (file-name object-file-names
)
3577 (write-line (namestring file-name
))
3578 (cold-load file-name
))
3580 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3581 (resolve-assembler-fixups)
3582 #!+x86
(output-load-time-code-fixups)
3583 (foreign-symbols-to-core)
3585 (/show
"back from FINISH-SYMBOLS")
3586 (finalize-load-time-value-noise)
3588 ;; Tell the target Lisp how much stuff we've allocated.
3589 (cold-set 'sb
!vm
:*read-only-space-free-pointer
*
3590 (allocate-cold-descriptor *read-only
*
3592 sb
!vm
:even-fixnum-lowtag
))
3593 (cold-set 'sb
!vm
:*static-space-free-pointer
*
3594 (allocate-cold-descriptor *static
*
3596 sb
!vm
:even-fixnum-lowtag
))
3597 (/show
"done setting free pointers")
3599 ;; Write results to files.
3601 ;; FIXME: I dislike this approach of redefining
3602 ;; *STANDARD-OUTPUT* instead of putting the new stream in a
3603 ;; lexical variable, and it's annoying to have WRITE-MAP (to
3604 ;; *STANDARD-OUTPUT*) not be parallel to WRITE-INITIAL-CORE-FILE
3605 ;; (to a stream explicitly passed as an argument).
3606 (macrolet ((out-to (name &body body
)
3607 `(let ((fn (format nil
"~A/~A.h" c-header-dir-name
,name
)))
3608 (ensure-directories-exist fn
)
3609 (with-open-file (*standard-output
* fn
3610 :if-exists
:supersede
:direction
:output
)
3612 (let ((n (c-name (string-upcase ,name
))))
3615 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~A 1~%"
3619 "#endif /* SBCL_GENESIS_~A */~%"
3620 (string-upcase ,name
))))))
3622 (with-open-file (*standard-output
* map-file-name
3624 :if-exists
:supersede
)
3626 (out-to "config" (write-config-h))
3627 (out-to "constants" (write-constants-h))
3629 (out-to "tagnames" (write-tagnames-h))
3630 (let ((structs (sort (copy-list sb
!vm
:*primitive-objects
*) #'string
<
3633 (sb!vm
:primitive-object-name obj
))))))
3634 (dolist (obj structs
)
3636 (string-downcase (string (sb!vm
:primitive-object-name obj
)))
3637 (write-primitive-object obj
)))
3638 (out-to "primitive-objects"
3639 (dolist (obj structs
)
3640 (format t
"~&#include \"~A.h\"~%"
3642 (string (sb!vm
:primitive-object-name obj
)))))))
3643 (dolist (class '(hash-table
3646 sb
!c
::compiled-debug-info
3647 sb
!c
::compiled-debug-fun
3650 (string-downcase (string class
))
3651 (write-structure-object
3652 (layout-info (find-layout class
)))))
3653 (out-to "static-symbols" (write-static-symbols))
3655 (let ((fn (format nil
"~A/Makefile.features" c-header-dir-name
)))
3656 (ensure-directories-exist fn
)
3657 (with-open-file (*standard-output
* fn
:if-exists
:supersede
3659 (write-makefile-features)))
3661 (when core-file-name
3662 (write-initial-core-file core-file-name
))))))
3664 ;; This generalization of WARM-FUN-NAME will do in a pinch
3665 ;; to view strings and things in a gspace.
3666 (defun warmelize (descriptor)
3667 (labels ((recurse (x)
3669 (return-from recurse nil
))
3670 (ecase (descriptor-lowtag x
)
3671 (#.sb
!vm
:list-pointer-lowtag
3672 (cons (recurse (cold-car x
)) (recurse (cold-cdr x
))))
3673 (#.sb
!vm
:fun-pointer-lowtag
3674 (let ((name (read-wordindexed x sb
!vm
:simple-fun-name-slot
)))
3675 `(function ,(recurse name
))))
3676 (#.sb
!vm
:other-pointer-lowtag
3677 (let ((widetag (logand (descriptor-bits (read-wordindexed x
0))
3678 sb
!vm
:widetag-mask
)))
3680 (#.sb
!vm
:symbol-header-widetag
3681 ;; this is only approximate, as it disregards package
3682 (intern (recurse (read-wordindexed x sb
!vm
:symbol-name-slot
))))
3683 (#.sb
!vm
:simple-base-string-widetag
3684 (base-string-from-core x
))))))))
3685 (recurse descriptor
)))