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.)
20 ;;;; This software is part of the SBCL system. See the README file for
21 ;;;; more information.
23 ;;;; This software is derived from the CMU CL system, which was
24 ;;;; written at Carnegie Mellon University and released into the
25 ;;;; public domain. The software is in the public domain and is
26 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
27 ;;;; files for more information.
29 (in-package "SB!FASL")
31 ;;; a magic number used to identify our core files
32 (defconstant core-magic
33 (logior (ash (sb!xc
:char-code
#\S
) 24)
34 (ash (sb!xc
:char-code
#\B
) 16)
35 (ash (sb!xc
:char-code
#\C
) 8)
36 (sb!xc
:char-code
#\L
)))
38 (defun round-up (number size
)
39 "Round NUMBER up to be an integral multiple of SIZE."
40 (* size
(ceiling number size
)))
42 ;;;; implementing the concept of "vector" in (almost) portable
45 ;;;; "If you only need to do such simple things, it doesn't really
46 ;;;; matter which language you use." -- _ANSI Common Lisp_, p. 1, Paul
47 ;;;; Graham (evidently not considering the abstraction "vector" to be
48 ;;;; such a simple thing:-)
50 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
51 (defconstant +smallvec-length
+
54 ;;; an element of a BIGVEC -- a vector small enough that we have
55 ;;; a good chance of it being portable to other Common Lisps
57 `(simple-array (unsigned-byte 8) (,+smallvec-length
+)))
59 (defun make-smallvec ()
60 (make-array +smallvec-length
+ :element-type
'(unsigned-byte 8)
63 ;;; a big vector, implemented as a vector of SMALLVECs
65 ;;; KLUDGE: This implementation seems portable enough for our
66 ;;; purposes, since realistically every modern implementation is
67 ;;; likely to support vectors of at least 2^16 elements. But if you're
68 ;;; masochistic enough to read this far into the contortions imposed
69 ;;; on us by ANSI and the Lisp community, for daring to use the
70 ;;; abstraction of a large linearly addressable memory space, which is
71 ;;; after all only directly supported by the underlying hardware of at
72 ;;; least 99% of the general-purpose computers in use today, then you
73 ;;; may be titillated to hear that in fact this code isn't really
74 ;;; portable, because as of sbcl-0.7.4 we need somewhat more than
75 ;;; 16Mbytes to represent a core, and ANSI only guarantees that
76 ;;; ARRAY-DIMENSION-LIMIT is not less than 1024. -- WHN 2002-06-13
78 (outer-vector (vector (make-smallvec)) :type
(vector smallvec
)))
80 ;;; analogous to SVREF, but into a BIGVEC
81 (defun bvref (bigvec index
)
82 (multiple-value-bind (outer-index inner-index
)
83 (floor index
+smallvec-length
+)
85 (svref (bigvec-outer-vector bigvec
) outer-index
))
87 (defun (setf bvref
) (new-value bigvec index
)
88 (multiple-value-bind (outer-index inner-index
)
89 (floor index
+smallvec-length
+)
90 (setf (aref (the smallvec
91 (svref (bigvec-outer-vector bigvec
) outer-index
))
95 ;;; analogous to LENGTH, but for a BIGVEC
97 ;;; the length of BIGVEC, measured in the number of BVREFable bytes it
99 (defun bvlength (bigvec)
100 (* (length (bigvec-outer-vector bigvec
))
103 ;;; analogous to WRITE-SEQUENCE, but for a BIGVEC
104 (defun write-bigvec-as-sequence (bigvec stream
&key
(start 0) end pad-with-zeros
)
105 (let* ((bvlength (bvlength bigvec
))
106 (data-length (min (or end bvlength
) bvlength
)))
107 (loop for i of-type index from start below data-length do
108 (write-byte (bvref bigvec i
)
110 (when (and pad-with-zeros
(< bvlength data-length
))
111 (loop repeat
(- data-length bvlength
) do
(write-byte 0 stream
)))))
113 ;;; analogous to READ-SEQUENCE-OR-DIE, but for a BIGVEC
114 (defun read-bigvec-as-sequence-or-die (bigvec stream
&key
(start 0) end
)
115 (loop for i of-type index from start below
(or end
(bvlength bigvec
)) do
116 (setf (bvref bigvec i
)
117 (read-byte stream
))))
119 ;;; Grow BIGVEC (exponentially, so that large increases in size have
120 ;;; asymptotic logarithmic cost per byte).
121 (defun expand-bigvec (bigvec)
122 (let* ((old-outer-vector (bigvec-outer-vector bigvec
))
123 (length-old-outer-vector (length old-outer-vector
))
124 (new-outer-vector (make-array (* 2 length-old-outer-vector
))))
125 (replace new-outer-vector old-outer-vector
)
126 (loop for i from length-old-outer-vector below
(length new-outer-vector
) do
127 (setf (svref new-outer-vector i
)
129 (setf (bigvec-outer-vector bigvec
)
133 ;;;; looking up bytes and multi-byte values in a BIGVEC (considering
134 ;;;; it as an image of machine memory on the cross-compilation target)
136 ;;; BVREF-32 and friends. These are like SAP-REF-n, except that
137 ;;; instead of a SAP we use a BIGVEC.
138 (macrolet ((make-bvref-n (n)
139 (let ((name (intern (format nil
"BVREF-~A" n
)))
141 (loop with n-octets
= (/ n
8)
142 for i from
0 below n-octets
143 collect
`(+ byte-index
#!+big-endian
,(- n-octets i
1)
146 (defun ,name
(bigvec byte-index
)
147 (logior ,@(loop for index in le-octet-indices
149 collect
`(ash (bvref bigvec
,index
) ,(* i
8)))))
150 (defun (setf ,name
) (new-value bigvec byte-index
)
151 ;; We don't carefully distinguish between signed and unsigned,
152 ;; since there's only one setter function per byte size.
153 (declare (type (or (signed-byte ,n
) (unsigned-byte ,n
))
155 (setf ,@(loop for index in le-octet-indices
157 append
`((bvref bigvec
,index
)
158 (ldb (byte 8 ,(* i
8)) new-value
)))))))))
164 ;; lispobj-sized word, whatever that may be
165 ;; hopefully nobody ever wants a 128-bit SBCL...
166 (macrolet ((acc (bv index
) `(#!+64-bit bvref-64
#!-
64-bit bvref-32
,bv
,index
)))
167 (defun (setf bvref-word
) (new-val bytes index
) (setf (acc bytes index
) new-val
))
168 (defun bvref-word (bytes index
) (acc bytes index
)))
170 ;;;; representation of spaces in the core
172 ;;; If there is more than one dynamic space in memory (i.e., if a
173 ;;; copying GC is in use), then only the active dynamic space gets
176 (defconstant dynamic-core-space-id
1)
179 (defconstant static-core-space-id
2)
182 (defconstant read-only-core-space-id
3)
186 (defvar *immobile-fixedobj
*)
187 (defvar *immobile-varyobj
*)
188 (defconstant immobile-fixedobj-core-space-id
4)
189 (defconstant immobile-varyobj-core-space-id
5)
190 (defvar *immobile-space-map
* nil
))
192 (defconstant max-core-space-id
5)
193 (defconstant deflated-core-space-id-flag
8)
195 ;;; a GENESIS-time representation of a memory space (e.g. read-only
196 ;;; space, dynamic space, or static space)
197 (defstruct (gspace (:constructor %make-gspace
)
199 ;; name and identifier for this GSPACE
200 (name (missing-arg) :type symbol
:read-only t
)
201 (identifier (missing-arg) :type fixnum
:read-only t
)
202 ;; the word address where the data will be loaded
203 (word-address (missing-arg) :type unsigned-byte
:read-only t
)
204 ;; the gspace contents as a BIGVEC
205 (data (make-bigvec) :type bigvec
:read-only t
)
206 ;; the index of the next unwritten word (i.e. chunk of
207 ;; SB!VM:N-WORD-BYTES bytes) in DATA, or equivalently the number of
208 ;; words actually written in DATA. In order to convert to an actual
209 ;; index into DATA, thus must be multiplied by SB!VM:N-WORD-BYTES.
212 (defun gspace-byte-address (gspace)
213 (ash (gspace-word-address gspace
) sb
!vm
:word-shift
))
215 (cl:defmethod
print-object ((gspace gspace
) stream
)
216 (print-unreadable-object (gspace stream
:type t
)
217 (format stream
"@#x~X ~S" (gspace-byte-address gspace
) (gspace-name gspace
))))
219 (defun make-gspace (name identifier byte-address
)
220 ;; Genesis should be agnostic of space alignment except in so far as it must
221 ;; be a multiple of the backend page size. We used to care more, in that
222 ;; descriptor-bits were composed of a high half and low half for the
223 ;; questionable motive of caring about fixnum-ness of the halves,
224 ;; despite the wonderful abstraction INTEGER that transparently becomes
225 ;; a BIGNUM if the host's fixnum is limited in size.
226 ;; So it's not clear whether this test belongs here, because if we do need it,
227 ;; then it best belongs where we assign space addresses in the first place.
228 (let ((target-space-alignment (ash 1 16)))
229 (unless (zerop (rem byte-address target-space-alignment
))
230 (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
231 byte-address target-space-alignment
)))
232 (%make-gspace
:name name
233 :identifier identifier
234 :word-address
(ash byte-address
(- sb
!vm
:word-shift
))))
236 ;;;; representation of descriptors
238 (declaim (inline is-fixnum-lowtag
))
239 (defun is-fixnum-lowtag (lowtag)
240 (zerop (logand lowtag sb
!vm
:fixnum-tag-mask
)))
242 (defun is-other-immediate-lowtag (lowtag)
243 ;; The other-immediate lowtags are similar to the fixnum lowtags, in
244 ;; that they have an "effective length" that is shorter than is used
245 ;; for the pointer lowtags. Unlike the fixnum lowtags, however, the
246 ;; other-immediate lowtags are always effectively two bits wide.
247 (= (logand lowtag
3) sb
!vm
:other-immediate-0-lowtag
))
249 (defstruct (descriptor
250 (:constructor make-descriptor
(bits &optional gspace word-offset
))
252 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
253 (gspace nil
:type
(or gspace
(eql :load-time-value
) null
))
254 ;; the offset in words from the start of GSPACE, or NIL if not set yet
255 (word-offset nil
:type
(or sb
!vm
:word null
))
256 (bits 0 :read-only t
:type
(unsigned-byte #.sb
!vm
:n-machine-word-bits
)))
258 (declaim (inline descriptor
=))
259 (defun descriptor= (a b
) (eql (descriptor-bits a
) (descriptor-bits b
)))
261 (defun make-random-descriptor (bits)
262 (make-descriptor (logand bits sb
!ext
:most-positive-word
)))
264 (declaim (inline descriptor-lowtag
))
265 (defun descriptor-lowtag (des)
266 "the lowtag bits for DES"
267 (logand (descriptor-bits des
) sb
!vm
:lowtag-mask
))
269 (defmethod print-object ((des descriptor
) stream
)
270 (let ((gspace (descriptor-gspace des
))
271 (bits (descriptor-bits des
))
272 (lowtag (descriptor-lowtag des
)))
273 (print-unreadable-object (des stream
:type t
)
274 (cond ((eq gspace
:load-time-value
)
275 (format stream
"for LTV ~D" (descriptor-word-offset des
)))
276 ((is-fixnum-lowtag lowtag
)
277 (format stream
"for fixnum: ~W" (descriptor-fixnum des
)))
278 ((is-other-immediate-lowtag lowtag
)
280 "for other immediate: #X~X, type #b~8,'0B"
281 (ash bits
(- sb
!vm
:n-widetag-bits
))
282 (logand bits sb
!vm
:widetag-mask
)))
285 "for pointer: #X~X, lowtag #b~v,'0B, ~A"
286 (logandc2 bits sb
!vm
:lowtag-mask
)
287 sb
!vm
:n-lowtag-bits lowtag
288 (if gspace
(gspace-name gspace
) "unknown")))))))
290 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
291 ;;; free word index is boosted as necessary, and if additional memory
292 ;;; is needed, we grow the GSPACE. The descriptor returned is a
293 ;;; pointer of type LOWTAG.
294 (defun allocate-cold-descriptor (gspace length lowtag
&optional page-attributes
)
296 (gspace-claim-n-bytes gspace length page-attributes
))
297 (ptr (+ (gspace-word-address gspace
) word-index
)))
298 (make-descriptor (logior (ash ptr sb
!vm
:word-shift
) lowtag
)
302 (defun gspace-claim-n-words (gspace n-words
)
303 (let* ((old-free-word-index (gspace-free-word-index gspace
))
304 (new-free-word-index (+ old-free-word-index n-words
)))
305 ;; Grow GSPACE as necessary until it's big enough to handle
306 ;; NEW-FREE-WORD-INDEX.
308 ((>= (bvlength (gspace-data gspace
))
309 (* new-free-word-index sb
!vm
:n-word-bytes
)))
310 (expand-bigvec (gspace-data gspace
)))
311 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
312 (setf (gspace-free-word-index gspace
) new-free-word-index
)
313 old-free-word-index
))
315 ;; align256p is true if we need to force objects on this page to 256-byte
316 ;; boundaries. This doesn't need to be generalized - everything of type
317 ;; INSTANCE is either on its natural alignment, or 256-byte.
318 ;; [See doc/internals-notes/compact-instance for why you might want it at all]
319 ;; PAGE-KIND is a heuristic for placement of symbols
320 ;; based on being interned/uninterned/likely-special-variable.
321 (defun make-page-attributes (align256p page-kind
)
322 (declare (type (or null
(integer 0 3)) page-kind
))
323 (logior (ash (or page-kind
0) 1) (if align256p
1 0)))
324 (defun immobile-obj-spacing-words (page-attributes)
325 (if (logbitp 0 page-attributes
)
326 (/ 256 sb
!vm
:n-word-bytes
)))
328 (defun gspace-claim-n-bytes (gspace specified-n-bytes page-attributes
)
329 (declare (ignorable page-attributes
))
330 (let* ((n-bytes (round-up specified-n-bytes
(ash 1 sb
!vm
:n-lowtag-bits
)))
331 (n-words (ash n-bytes
(- sb
!vm
:word-shift
))))
332 (aver (evenp n-words
))
333 (cond #!+immobile-space
334 ((eq gspace
*immobile-fixedobj
*)
335 (aver page-attributes
)
336 ;; An immobile fixedobj page can only have one value of object-spacing
337 ;; and size for all objects on it. Different widetags are ok.
338 (let* ((key (cons specified-n-bytes page-attributes
))
339 (found (cdr (assoc key
*immobile-space-map
* :test
'equal
)))
340 (page-n-words (/ sb
!vm
:immobile-card-bytes sb
!vm
:n-word-bytes
)))
341 (unless found
; grab one whole GC page from immobile space
342 (let ((free-word-index
343 (gspace-claim-n-words gspace page-n-words
)))
344 (setf found
(cons 0 free-word-index
))
345 (push (cons key found
) *immobile-space-map
*)))
346 (destructuring-bind (page-word-index . page-base-index
) found
349 (or (immobile-obj-spacing-words page-attributes
)
351 (if (> next-word
(- page-n-words n-words
))
352 ;; no more objects fit on this page
353 (setf *immobile-space-map
*
354 (delete key
*immobile-space-map
* :key
'car
:test
'equal
))
355 (setf (car found
) next-word
)))
356 (+ page-word-index page-base-index
))))
358 (gspace-claim-n-words gspace n-words
)))))
360 (defun descriptor-fixnum (des)
361 (unless (is-fixnum-lowtag (descriptor-lowtag des
))
362 (error "descriptor-fixnum called on non-fixnum ~S" des
))
363 (let* ((descriptor-bits (descriptor-bits des
))
364 (bits (ash descriptor-bits
(- sb
!vm
:n-fixnum-tag-bits
))))
365 (if (logbitp (1- sb
!vm
:n-word-bits
) descriptor-bits
)
366 (logior bits
(ash -
1 (1+ sb
!vm
:n-positive-fixnum-bits
)))
369 (defun descriptor-word-sized-integer (des)
370 ;; Extract an (unsigned-byte 32), from either its fixnum or bignum
372 (let ((lowtag (descriptor-lowtag des
)))
373 (if (is-fixnum-lowtag lowtag
)
374 (make-random-descriptor (descriptor-fixnum des
))
375 (read-wordindexed des
1))))
378 (defun descriptor-mem (des)
379 (gspace-data (descriptor-intuit-gspace des
)))
380 (defun descriptor-byte-offset (des)
381 (ash (descriptor-word-offset des
) sb
!vm
:word-shift
))
383 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
384 ;;; figure out a GSPACE which corresponds to DES, set it into
385 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
386 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
387 (declaim (ftype (function (descriptor) gspace
) descriptor-intuit-gspace
))
388 (defun descriptor-intuit-gspace (des)
389 (or (descriptor-gspace des
)
391 ;; gspace wasn't set, now we have to search for it.
392 (let* ((lowtag (descriptor-lowtag des
))
393 (abs-word-addr (ash (- (descriptor-bits des
) lowtag
)
394 (- sb
!vm
:word-shift
))))
396 ;; Non-pointer objects don't have a gspace.
397 (unless (or (eql lowtag sb
!vm
:fun-pointer-lowtag
)
398 (eql lowtag sb
!vm
:instance-pointer-lowtag
)
399 (eql lowtag sb
!vm
:list-pointer-lowtag
)
400 (eql lowtag sb
!vm
:other-pointer-lowtag
))
401 (error "don't even know how to look for a GSPACE for ~S" des
))
403 (dolist (gspace (list *dynamic
* *static
* *read-only
*
404 #!+immobile-space
*immobile-fixedobj
*
405 #!+immobile-space
*immobile-varyobj
*)
406 (error "couldn't find a GSPACE for ~S" des
))
407 ;; Bounds-check the descriptor against the allocated area
408 ;; within each gspace.
409 (when (and (<= (gspace-word-address gspace
)
411 (+ (gspace-word-address gspace
)
412 (gspace-free-word-index gspace
))))
413 ;; Update the descriptor with the correct gspace and the
414 ;; offset within the gspace and return the gspace.
415 (setf (descriptor-word-offset des
)
416 (- abs-word-addr
(gspace-word-address gspace
)))
417 (return (setf (descriptor-gspace des
) gspace
)))))))
419 (defun %fixnum-descriptor-if-possible
(num)
420 (and (typep num
'(signed-byte #.sb
!vm
:n-fixnum-bits
))
421 (make-random-descriptor (ash num sb
!vm
:n-fixnum-tag-bits
))))
423 (defun make-fixnum-descriptor (num)
424 (or (%fixnum-descriptor-if-possible num
)
425 (error "~W is too big for a fixnum." num
)))
427 (defun make-other-immediate-descriptor (data type
)
428 (make-descriptor (logior (ash data sb
!vm
:n-widetag-bits
) type
)))
430 (defun make-character-descriptor (data)
431 (make-other-immediate-descriptor data sb
!vm
:character-widetag
))
434 ;;;; miscellaneous variables and other noise
436 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
437 ;;; undefined foreign symbols are to be treated as an error.
438 ;;; (In the first pass of GENESIS, needed to create a header file before
439 ;;; the C runtime can be built, various foreign symbols will necessarily
440 ;;; be undefined, but we don't need actual values for them anyway, and
441 ;;; we can just use 0 or some other placeholder. In the second pass of
442 ;;; GENESIS, all foreign symbols should be defined, so any undefined
443 ;;; foreign symbol is a problem.)
445 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
446 ;;; never tries to look up foreign symbols in the first place unless
447 ;;; it's actually creating a core file (as in the second pass) instead
448 ;;; of using this hack to allow it to go through the motions without
449 ;;; causing an error. -- WHN 20000825
450 (defvar *foreign-symbol-placeholder-value
*)
452 ;;; a handle on the trap object
453 (defvar *unbound-marker
*
454 (make-other-immediate-descriptor 0 sb
!vm
:unbound-marker-widetag
))
456 ;;; a handle on the NIL object
457 (defvar *nil-descriptor
*)
459 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
460 ;;; when the target Lisp starts up
462 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
463 ;;; loadtime value, represented by (CONS KEYWORD ..).
464 (declaim (special *!cold-toplevels
* *!cold-defconstants
*
465 *!cold-defuns
* *cold-methods
*))
467 ;;; the head of a list of DEBUG-SOURCEs which need to be patched when
468 ;;; the cold core starts up
469 (defvar *current-debug-sources
*)
471 ;;; foreign symbol references
472 (defparameter *cold-foreign-undefined-symbols
* nil
)
474 ;;;; miscellaneous stuff to read and write the core memory
476 ;; Like above, but the list is held in the target's image of the host symbol,
477 ;; not the host's value of the symbol.
478 (defun cold-target-push (cold-thing host-symbol
)
479 (cold-set host-symbol
(cold-cons cold-thing
(cold-symbol-value host-symbol
))))
481 (declaim (ftype (function (descriptor sb
!vm
:word
) descriptor
) read-wordindexed
))
482 (macrolet ((read-bits ()
483 `(bvref-word (descriptor-mem address
)
484 (ash (+ index
(descriptor-word-offset address
))
486 (defun read-bits-wordindexed (address index
)
488 (defun read-wordindexed (address index
)
489 "Return the value which is displaced by INDEX words from ADDRESS."
490 (make-random-descriptor (read-bits))))
492 (declaim (ftype (function (descriptor) descriptor
) read-memory
))
493 (defun read-memory (address)
494 "Return the value at ADDRESS."
495 (read-wordindexed address
0))
497 (declaim (ftype (function (descriptor
498 (integer #.
(- sb
!vm
:list-pointer-lowtag
)
499 #.sb
!ext
:most-positive-word
)
502 note-load-time-value-reference
))
503 (defun note-load-time-value-reference (address offset marker
)
504 (push (cold-list (cold-intern :load-time-value-fixup
)
506 (number-to-core offset
)
507 (number-to-core (descriptor-word-offset marker
)))
511 (declaim (ftype (function (descriptor sb
!vm
:word
(or symbol descriptor
))) write-wordindexed
))
512 (macrolet ((write-bits (bits)
513 `(setf (bvref-word (descriptor-mem address
)
514 (ash (+ index
(descriptor-word-offset address
))
517 (defun write-wordindexed (address index value
)
518 "Write VALUE displaced INDEX words from ADDRESS."
519 ;; If we're passed a symbol as a value then it needs to be interned.
520 (let ((value (cond ((symbolp value
) (cold-intern value
))
522 (if (eql (descriptor-gspace value
) :load-time-value
)
523 (note-load-time-value-reference address
524 (- (ash index sb
!vm
:word-shift
)
525 (logand (descriptor-bits address
)
528 (write-bits (descriptor-bits value
)))))
530 (defun write-wordindexed/raw
(address index bits
)
531 (declare (type descriptor address
) (type sb
!vm
:word index
)
532 (type (or sb
!vm
:word sb
!vm
:signed-word
) bits
))
533 (write-bits (logand bits sb
!ext
:most-positive-word
))))
535 (declaim (ftype (function (descriptor (or symbol descriptor
))) write-memory
))
536 (defun write-memory (address value
)
537 "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
538 (write-wordindexed address
0 value
))
540 ;;;; allocating images of primitive objects in the cold core
542 (defun write-header-word (des header-data widetag
)
543 ;; In immobile space, all objects start life as pseudo-static as if by 'save'.
544 (let ((gen #!+gencgc
(if (or #!+immobile-space
545 (let ((gspace (descriptor-gspace des
)))
546 (or (eq gspace
*immobile-fixedobj
*)
547 (eq gspace
*immobile-varyobj
*))))
548 sb
!vm
:+pseudo-static-generation
+
551 (write-wordindexed/raw des
0
552 (logior (ash (logior (ash gen
16) header-data
)
553 sb
!vm
:n-widetag-bits
) widetag
))))
555 (defun set-header-data (object data
)
556 (write-header-word object data
(ldb (byte sb
!vm
:n-widetag-bits
0)
557 (read-bits-wordindexed object
0)))
558 object
) ; return the object itself, like SB!KERNEL:SET-HEADER-DATA
560 (defun get-header-data (object)
561 (ash (read-bits-wordindexed object
0) (- sb
!vm
:n-widetag-bits
)))
563 ;;; There are three kinds of blocks of memory in the type system:
564 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
565 ;;; header as all slots are descriptors.
566 ;;; * Unboxed objects (bignums): There is a single header word that contains
568 ;;; * Vector objects: There is a header word with the type, then a word for
569 ;;; the length, then the data.
570 (defun allocate-object (gspace length lowtag
&optional align256p
)
571 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
573 (allocate-cold-descriptor gspace
(ash length sb
!vm
:word-shift
) lowtag
574 (make-page-attributes align256p
0)))
575 (defun allocate-header+object
(gspace length widetag
)
576 "Allocate LENGTH words plus a header word in GSPACE and
577 return an ``other-pointer'' descriptor to them. Initialize the header word
578 with the resultant length and WIDETAG."
579 (let ((des (allocate-cold-descriptor
580 gspace
(ash (1+ length
) sb
!vm
:word-shift
)
581 sb
!vm
:other-pointer-lowtag
582 (make-page-attributes nil
0))))
583 (write-header-word des length widetag
)
585 (defun allocate-vector-object (gspace element-bits length widetag
)
586 "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
587 GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
588 header word with WIDETAG and the length slot with LENGTH."
589 ;; ALLOCATE-COLD-DESCRIPTOR will take any rational number of bytes
590 ;; and round up to a double-word. This doesn't need to use CEILING.
591 (let* ((bytes (/ (* element-bits length
) sb
!vm
:n-byte-bits
))
592 (des (allocate-cold-descriptor gspace
593 (+ bytes
(* 2 sb
!vm
:n-word-bytes
))
594 sb
!vm
:other-pointer-lowtag
)))
595 (write-header-word des
0 widetag
)
596 (write-wordindexed des
597 sb
!vm
:vector-length-slot
598 (make-fixnum-descriptor length
))
601 ;;; the hosts's representation of LAYOUT-of-LAYOUT
602 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
603 (defvar *host-layout-of-layout
* (find-layout 'layout
)))
605 (defun cold-layout-length (layout)
606 (descriptor-fixnum (read-slot layout
*host-layout-of-layout
* :length
)))
607 (defun cold-layout-depthoid (layout)
608 (descriptor-fixnum (read-slot layout
*host-layout-of-layout
* :depthoid
)))
610 ;; Make a structure and set the header word and layout.
611 ;; LAYOUT-LENGTH is as returned by the like-named function.
612 (defun allocate-struct
613 (gspace layout
&optional
(layout-length (cold-layout-length layout
))
615 ;; Count +1 for the header word when allocating.
616 (let ((des (allocate-object gspace
(1+ layout-length
)
617 sb
!vm
:instance-pointer-lowtag is-layout
)))
618 ;; Length as stored in the header is the exact number of useful words
619 ;; that follow, as is customary. A padding word, if any is not "useful"
620 (write-header-word des
621 (logior layout-length
622 #!+compact-instance-header
623 (if layout
(ash (descriptor-bits layout
) 24) 0))
624 sb
!vm
:instance-widetag
)
625 #!-compact-instance-header
626 (write-wordindexed des sb
!vm
:instance-slots-offset layout
)
629 ;;;; copying simple objects into the cold core
631 (defun base-string-to-core (string &optional
(gspace *dynamic
*))
632 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
633 core and return a descriptor to it."
634 ;; (Remember that the system convention for storage of strings leaves an
635 ;; extra null byte at the end to aid in call-out to C.)
636 (let* ((length (length string
))
637 (des (allocate-vector-object gspace
640 sb
!vm
:simple-base-string-widetag
))
641 (bytes (gspace-data gspace
))
642 (offset (+ (* sb
!vm
:vector-data-offset sb
!vm
:n-word-bytes
)
643 (descriptor-byte-offset des
))))
644 (write-wordindexed des
645 sb
!vm
:vector-length-slot
646 (make-fixnum-descriptor length
))
648 (setf (bvref bytes
(+ offset i
))
649 (sb!xc
:char-code
(aref string i
))))
650 (setf (bvref bytes
(+ offset length
))
651 0) ; null string-termination character for C
654 (defun base-string-from-core (descriptor)
655 (let* ((len (descriptor-fixnum
656 (read-wordindexed descriptor sb
!vm
:vector-length-slot
)))
657 (str (make-string len
))
658 (bytes (descriptor-mem descriptor
)))
661 (code-char (bvref bytes
662 (+ (descriptor-byte-offset descriptor
)
663 (* sb
!vm
:vector-data-offset sb
!vm
:n-word-bytes
)
666 (defun bignum-to-core (n)
667 "Copy a bignum to the cold core."
668 (let* ((words (ceiling (1+ (integer-length n
)) sb
!vm
:n-word-bits
))
670 (allocate-header+object
*dynamic
* words sb
!vm
:bignum-widetag
)))
671 (declare (fixnum words
))
672 (do ((index 1 (1+ index
))
673 (remainder n
(ash remainder
(- sb
!vm
:n-word-bits
))))
675 (unless (zerop (integer-length remainder
))
676 ;; FIXME: Shouldn't this be a fatal error?
677 (warn "~W words of ~W were written, but ~W bits were left over."
679 (write-wordindexed/raw handle index
680 (ldb (byte sb
!vm
:n-word-bits
0) remainder
)))
683 (defun bignum-from-core (descriptor)
684 (let ((n-words (ash (descriptor-bits (read-memory descriptor
))
685 (- sb
!vm
:n-widetag-bits
)))
687 (dotimes (i n-words val
)
688 (let ((bits (read-bits-wordindexed descriptor
689 (+ i sb
!vm
:bignum-digits-offset
))))
690 ;; sign-extend the highest word
691 (when (and (= i
(1- n-words
)) (logbitp (1- sb
!vm
:n-word-bits
) bits
))
692 (setq bits
(dpb bits
(byte sb
!vm
:n-word-bits
0) -
1)))
693 (setq val
(logior (ash bits
(* i sb
!vm
:n-word-bits
)) val
))))))
695 (defun number-pair-to-core (first second type
)
696 "Makes a number pair of TYPE (ratio or complex) and fills it in."
697 (let ((des (allocate-header+object
*dynamic
* 2 type
)))
698 (write-wordindexed des
1 first
)
699 (write-wordindexed des
2 second
)
702 (defun write-double-float-bits (address index x
)
703 (let ((high-bits (double-float-high-bits x
))
704 (low-bits (double-float-low-bits x
)))
705 (ecase sb
!vm
::n-word-bits
707 (ecase sb
!c
:*backend-byte-order
*
709 (write-wordindexed/raw address index low-bits
)
710 (write-wordindexed/raw address
(1+ index
) high-bits
))
712 (write-wordindexed/raw address index high-bits
)
713 (write-wordindexed/raw address
(1+ index
) low-bits
))))
715 (let ((bits (ecase sb
!c
:*backend-byte-order
*
716 (:little-endian
(logior low-bits
(ash high-bits
32)))
718 #+nil
(:big-endian
(logior (logand high-bits
#xffffffff
)
719 (ash low-bits
32))))))
720 (write-wordindexed/raw address index bits
))))
724 (defun float-to-core (x)
727 ;; 64-bit platforms have immediate single-floats.
729 (make-random-descriptor (logior (ash (single-float-bits x
) 32)
730 sb
!vm
::single-float-widetag
))
732 (let ((des (allocate-header+object
*dynamic
*
733 (1- sb
!vm
:single-float-size
)
734 sb
!vm
:single-float-widetag
)))
735 (write-wordindexed/raw des sb
!vm
:single-float-value-slot
736 (single-float-bits x
))
739 (let ((des (allocate-header+object
*dynamic
*
740 (1- sb
!vm
:double-float-size
)
741 sb
!vm
:double-float-widetag
)))
742 (write-double-float-bits des sb
!vm
:double-float-value-slot x
)))))
744 (defun complex-single-float-to-core (num)
745 (declare (type (complex single-float
) num
))
746 (let ((des (allocate-header+object
*dynamic
*
747 (1- sb
!vm
:complex-single-float-size
)
748 sb
!vm
:complex-single-float-widetag
)))
751 (write-wordindexed/raw des sb
!vm
:complex-single-float-real-slot
752 (single-float-bits (realpart num
)))
753 (write-wordindexed/raw des sb
!vm
:complex-single-float-imag-slot
754 (single-float-bits (imagpart num
))))
756 (write-wordindexed/raw
757 des sb
!vm
:complex-single-float-data-slot
758 (logior (ldb (byte 32 0) (single-float-bits (realpart num
)))
759 (ash (single-float-bits (imagpart num
)) 32)))
762 (defun complex-double-float-to-core (num)
763 (declare (type (complex double-float
) num
))
764 (let ((des (allocate-header+object
*dynamic
*
765 (1- sb
!vm
:complex-double-float-size
)
766 sb
!vm
:complex-double-float-widetag
)))
767 (write-double-float-bits des sb
!vm
:complex-double-float-real-slot
769 (write-double-float-bits des sb
!vm
:complex-double-float-imag-slot
772 ;;; Copy the given number to the core.
773 (defun number-to-core (number)
775 (integer (or (%fixnum-descriptor-if-possible number
)
776 (bignum-to-core number
)))
777 (ratio (number-pair-to-core (number-to-core (numerator number
))
778 (number-to-core (denominator number
))
779 sb
!vm
:ratio-widetag
))
780 ((complex single-float
) (complex-single-float-to-core number
))
781 ((complex double-float
) (complex-double-float-to-core number
))
783 ((complex long-float
)
784 (error "~S isn't a cold-loadable number at all!" number
))
785 (complex (number-pair-to-core (number-to-core (realpart number
))
786 (number-to-core (imagpart number
))
787 sb
!vm
:complex-widetag
))
788 (float (float-to-core number
))
789 (t (error "~S isn't a cold-loadable number at all!" number
))))
791 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
792 (defun cold-cons (car cdr
&optional
(gspace *dynamic
*))
793 (let ((dest (allocate-object gspace
2 sb
!vm
:list-pointer-lowtag
)))
794 (write-wordindexed dest sb
!vm
:cons-car-slot car
)
795 (write-wordindexed dest sb
!vm
:cons-cdr-slot cdr
)
797 (defun list-to-core (list)
798 (let ((head *nil-descriptor
*)
800 ;; A recursive algorithm would have the first cons at the highest
801 ;; address. This way looks nicer when viewed in ldb.
803 (unless list
(return head
))
804 (let ((cons (cold-cons (pop list
) *nil-descriptor
*)))
805 (if tail
(cold-rplacd tail cons
) (setq head cons
))
807 (defun cold-list (&rest args
) (list-to-core args
))
808 (defun cold-list-length (list) ; but no circularity detection
809 ;; a recursive implementation uses too much stack for some Lisps
811 (loop (if (cold-null list
) (return n
))
813 (setq list
(cold-cdr list
)))))
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 result
)
824 (write-wordindexed result
(+ index sb
!vm
:vector-data-offset
)
827 (defun ub32-vector-in-core (objects)
828 (let* ((size (length objects
))
829 (result (allocate-vector-object *dynamic
* sb
!vm
:n-word-bits size
830 sb
!vm
:simple-array-unsigned-byte-32-widetag
)))
831 (dotimes (index size result
)
832 (write-wordindexed/raw result
(+ index sb
!vm
:vector-data-offset
)
834 (defun cold-svset (vector index value
)
835 (let ((i (if (integerp index
) index
(descriptor-fixnum index
))))
836 (write-wordindexed vector
(+ i sb
!vm
:vector-data-offset
) value
)))
838 (setf (get 'vector
:sb-cold-funcall-handler
/for-value
)
839 (lambda (&rest args
) (vector-in-core args
)))
841 (declaim (inline cold-vector-len cold-svref
))
842 (defun cold-vector-len (vector)
843 (descriptor-fixnum (read-wordindexed vector sb
!vm
:vector-length-slot
)))
844 (defun cold-svref (vector i
)
845 (read-wordindexed vector
(+ (if (integerp i
) i
(descriptor-fixnum i
))
846 sb
!vm
:vector-data-offset
)))
847 (defun cold-vector-elements-eq (a b
)
848 (and (eql (cold-vector-len a
) (cold-vector-len b
))
849 (dotimes (k (cold-vector-len a
) t
)
850 (unless (descriptor= (cold-svref a k
) (cold-svref b k
))
852 (defun vector-from-core (descriptor &optional
(transform #'identity
))
853 (let* ((len (cold-vector-len descriptor
))
854 (vector (make-array len
)))
855 (dotimes (i len vector
)
856 (setf (aref vector i
) (funcall transform
(cold-svref descriptor i
))))))
860 ;; Simulate *FREE-TLS-INDEX*. This is a count, not a displacement.
861 ;; In C, sizeof counts 1 word for the variable-length interrupt_contexts[]
862 ;; but primitive-object-size counts 0, so add 1, though in fact the C code
863 ;; implies that it might have overcounted by 1. We could make this agnostic
864 ;; of MAX-INTERRUPTS by moving the thread base register up by TLS-SIZE words,
865 ;; using negative offsets for all dynamically assigned indices.
866 (defvar *genesis-tls-counter
*
867 (+ 1 sb
!vm
::max-interrupts
868 (sb!vm
:primitive-object-size
869 (find 'sb
!vm
::thread sb
!vm
:*primitive-objects
*
870 :key
#'sb
!vm
:primitive-object-name
))))
874 ;; Assign SYMBOL the tls-index INDEX. SYMBOL must be a descriptor.
875 ;; This is a backend support routine, but the style within this file
876 ;; is to conditionalize by the target features.
877 (defun cold-assign-tls-index (symbol index
)
879 (write-wordindexed/raw
880 symbol
0 (logior (ash index
32) (read-bits-wordindexed symbol
0)))
882 (write-wordindexed/raw symbol sb
!vm
:symbol-tls-index-slot index
))
884 ;; Return SYMBOL's tls-index,
885 ;; choosing a new index if it doesn't have one yet.
886 (defun ensure-symbol-tls-index (symbol)
887 (let* ((cold-sym (cold-intern symbol
))
890 (ldb (byte 32 32) (read-bits-wordindexed cold-sym
0))
892 (read-bits-wordindexed cold-sym sb
!vm
:symbol-tls-index-slot
)))
893 (unless (plusp tls-index
)
894 (let ((next (prog1 *genesis-tls-counter
* (incf *genesis-tls-counter
*))))
895 (setq tls-index
(ash next sb
!vm
:word-shift
))
896 (cold-assign-tls-index cold-sym tls-index
)))
899 ;; A table of special variable names which get known TLS indices.
900 ;; Some of them are mapped onto 'struct thread' and have pre-determined offsets.
901 ;; Others are static symbols used with bind_variable() in the C runtime,
902 ;; and might not, in the absence of this table, get an index assigned by genesis
903 ;; depending on whether the cross-compiler used the BIND vop on them.
904 ;; Indices for those static symbols can be chosen arbitrarily, which is to say
905 ;; the value doesn't matter but must update the tls-counter correctly.
906 ;; All symbols other than the ones in this table get the indices assigned
907 ;; by the fasloader on demand.
909 (defvar *known-tls-symbols
*
910 ;; FIXME: no mechanism exists to determine which static symbols C code will
911 ;; dynamically bind. TLS is a finite resource, and wasting indices for all
912 ;; static symbols isn't the best idea. This list was hand-made with 'grep'.
913 '(sb!vm
:*alloc-signal
*
914 sb
!sys
:*allow-with-interrupts
*
915 sb
!vm
:*current-catch-block
*
916 sb
!vm
::*current-unwind-protect-block
*
917 sb
!kernel
:*free-interrupt-context-index
*
918 sb
!kernel
:*gc-inhibit
*
919 sb
!kernel
:*gc-pending
*
921 sb
!impl
::*in-safepoint
*
922 sb
!sys
:*interrupt-pending
*
923 sb
!sys
:*interrupts-enabled
*
924 sb
!vm
::*pinned-objects
*
925 sb
!kernel
:*restart-clusters
*
926 sb
!kernel
:*stop-for-gc-pending
*
928 sb
!sys
:*thruption-pending
*))
930 (defvar *cold-symbol-gspace
* (or #!+immobile-space
'*immobile-fixedobj
* '*dynamic
*))
932 ;;; Allocate (and initialize) a symbol.
933 (defun allocate-symbol (name &key
(gspace (symbol-value *cold-symbol-gspace
*)))
934 (declare (simple-string name
))
935 (let ((symbol (allocate-header+object gspace
(1- sb
!vm
:symbol-size
)
936 sb
!vm
:symbol-widetag
)))
937 (write-wordindexed symbol sb
!vm
:symbol-value-slot
*unbound-marker
*)
938 (write-wordindexed symbol sb
!vm
:symbol-hash-slot
(make-fixnum-descriptor 0))
939 (write-wordindexed symbol sb
!vm
:symbol-info-slot
*nil-descriptor
*)
940 (write-wordindexed symbol sb
!vm
:symbol-name-slot
941 (set-readonly (base-string-to-core name
*dynamic
*)))
942 (write-wordindexed symbol sb
!vm
:symbol-package-slot
*nil-descriptor
*)
946 (defun assign-tls-index-if-needed (symbol cold-symbol
)
947 (let ((index (info :variable
:wired-tls symbol
)))
948 (cond ((integerp index
) ; thread slot
949 (cold-assign-tls-index cold-symbol index
))
950 ((memq symbol
*known-tls-symbols
*)
951 ;; symbols without which the C runtime could not start
952 (shiftf index
*genesis-tls-counter
* (1+ *genesis-tls-counter
*))
953 (cold-assign-tls-index cold-symbol
(ash index sb
!vm
:word-shift
))))))
955 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
956 ;;; descriptor of a cold symbol or (in an abbreviation for the
957 ;;; most common usage pattern) an ordinary symbol, which will be
958 ;;; automatically cold-interned.
959 (defun cold-set (symbol-or-symbol-des value
)
960 (let ((symbol-des (etypecase symbol-or-symbol-des
961 (descriptor symbol-or-symbol-des
)
962 (symbol (cold-intern symbol-or-symbol-des
)))))
963 (write-wordindexed symbol-des sb
!vm
:symbol-value-slot value
)))
964 (defun cold-symbol-value (symbol)
965 (let ((val (read-wordindexed (cold-intern symbol
) sb
!vm
:symbol-value-slot
)))
966 (if (= (descriptor-bits val
) sb
!vm
:unbound-marker-widetag
)
967 (unbound-cold-symbol-handler symbol
)
969 (defun cold-fdefn-fun (cold-fdefn)
970 (read-wordindexed cold-fdefn sb
!vm
:fdefn-fun-slot
))
972 (defun unbound-cold-symbol-handler (symbol)
973 (let ((host-val (and (boundp symbol
) (symbol-value symbol
))))
974 (if (typep host-val
'sb
!kernel
:named-type
)
975 (let ((target-val (ctype-to-core (sb!kernel
:named-type-name host-val
)
977 ;; Though it looks complicated to assign cold symbols on demand,
978 ;; it avoids writing code to build the layout of NAMED-TYPE in the
979 ;; way we build other primordial stuff such as layout-of-layout.
980 (cold-set symbol target-val
)
982 (error "Taking Cold-symbol-value of unbound symbol ~S" symbol
))))
984 ;;;; layouts and type system pre-initialization
986 ;;; Since we want to be able to dump structure constants and
987 ;;; predicates with reference layouts, we need to create layouts at
988 ;;; cold-load time. We use the name to intern layouts by, and dump a
989 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
990 ;;; initialization can find them. The only thing that's tricky [sic --
991 ;;; WHN 19990816] is initializing layout's layout, which must point to
994 ;;; a map from name as a host symbol to the descriptor of its target layout
995 (defvar *cold-layouts
*)
997 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
999 (defvar *cold-layout-names
*)
1001 ;;; the descriptor for layout's layout (needed when making layouts)
1002 (defvar *layout-layout
*)
1004 (defvar *known-structure-classoids
*)
1006 (defconstant target-layout-length
1007 ;; LAYOUT-LENGTH counts the number of words in an instance,
1008 ;; including the layout itself as 1 word
1009 (layout-length *host-layout-of-layout
*))
1011 ;;; Trivial methods [sic] require that we sort possible methods by the depthoid.
1012 ;;; Most of the objects printed in cold-init are ordered hierarchically in our
1013 ;;; type lattice; the major exceptions are ARRAY and VECTOR at depthoid -1.
1014 ;;; Of course we need to print VECTORs because a STRING is a vector,
1015 ;;; and vector has to precede ARRAY. Kludge it for now.
1016 (defun class-depthoid (class-name) ; DEPTHOID-ish thing, any which way you can
1020 ;; The depthoid of CONDITION has to be faked. The proper value is 1.
1021 ;; But STRUCTURE-OBJECT is also at depthoid 1, and its predicate
1022 ;; is %INSTANCEP (which is too weak), so to select the correct method
1023 ;; we have to make CONDITION more specific.
1024 ;; In reality it is type disjoint from structure-object.
1027 (let ((target-layout (gethash class-name
*cold-layouts
*)))
1029 (cold-layout-depthoid target-layout
)
1030 (let ((host-layout (find-layout class-name
)))
1031 (if (layout-invalid host-layout
)
1032 (error "~S has neither a host not target layout" class-name
)
1033 (layout-depthoid host-layout
))))))))
1035 ;;; Return a list of names created from the cold layout INHERITS data
1037 (defun listify-cold-inherits (x)
1038 (map 'list
(lambda (cold-layout)
1039 (or (gethash (descriptor-bits cold-layout
) *cold-layout-names
*)
1040 (error "~S is not the descriptor of a cold-layout" cold-layout
)))
1041 (vector-from-core x
)))
1043 ;;; COLD-DD-SLOTS is a cold descriptor for the list of slots
1044 ;;; in a cold defstruct-description. INDEX is a DSD-INDEX.
1045 ;;; Return the host's accessor name for the host image of that slot.
1046 (defun dsd-accessor-from-cold-slots (cold-dd-slots desired-index
)
1047 (let* ((dsd-slots (dd-slots
1048 (find-defstruct-description 'defstruct-slot-description
)))
1050 (dsd-index (find 'sb
!kernel
::bits dsd-slots
:key
#'dsd-name
)))
1051 (accessor-fun-name-slot
1052 (dsd-index (find 'sb
!kernel
::accessor-name dsd-slots
:key
#'dsd-name
))))
1053 (do ((list cold-dd-slots
(cold-cdr list
)))
1055 (when (= (ash (descriptor-fixnum
1056 (read-wordindexed (cold-car list
)
1057 (+ sb
!vm
:instance-slots-offset bits-slot
)))
1058 (- sb
!kernel
::+dsd-index-shift
+))
1062 (read-wordindexed (cold-car list
)
1063 (+ sb
!vm
:instance-slots-offset
1064 accessor-fun-name-slot
))))))))
1066 (defun cold-dsd-index (cold-dsd dsd-layout
)
1067 (ash (descriptor-fixnum (read-slot cold-dsd dsd-layout
:bits
))
1068 (- sb
!kernel
::+dsd-index-shift
+)))
1070 (defun cold-dsd-raw-type (cold-dsd dsd-layout
)
1071 (1- (ldb (byte 3 0) (descriptor-fixnum (read-slot cold-dsd dsd-layout
:bits
)))))
1073 (flet ((get-slots (host-layout-or-type)
1074 (etypecase host-layout-or-type
1075 (layout (dd-slots (layout-info host-layout-or-type
)))
1076 (symbol (dd-slots-from-core host-layout-or-type
))))
1077 (get-slot-index (slots initarg
)
1078 (+ sb
!vm
:instance-slots-offset
1079 (if (descriptor-p slots
)
1080 (do ((dsd-layout (find-layout 'defstruct-slot-description
))
1081 (slots slots
(cold-cdr slots
)))
1082 ((cold-null slots
) (error "No slot for ~S" initarg
))
1083 (let* ((dsd (cold-car slots
))
1084 (slot-name (read-slot dsd dsd-layout
:name
)))
1085 (when (eq (keywordicate (warm-symbol slot-name
)) initarg
)
1086 ;; Untagged slots are not accessible during cold-load
1087 (aver (eql (cold-dsd-raw-type dsd dsd-layout
) -
1))
1088 (return (cold-dsd-index dsd dsd-layout
)))))
1089 (let ((dsd (find initarg slots
1091 (eq x
(keywordicate (dsd-name y
)))))))
1092 (aver (eq (dsd-raw-type dsd
) t
)) ; Same as above: no can do.
1093 (dsd-index dsd
))))))
1094 (defun write-slots (cold-object host-layout-or-type
&rest assignments
)
1095 (aver (evenp (length assignments
)))
1096 (let ((slots (get-slots host-layout-or-type
)))
1097 (loop for
(initarg value
) on assignments by
#'cddr
1098 do
(write-wordindexed
1099 cold-object
(get-slot-index slots initarg
) value
)))
1102 ;; For symmetry, the reader takes an initarg, not a slot name.
1103 (defun read-slot (cold-object host-layout-or-type slot-initarg
)
1104 (let ((slots (get-slots host-layout-or-type
)))
1105 (read-wordindexed cold-object
(get-slot-index slots slot-initarg
)))))
1107 ;; Given a TYPE-NAME of a structure-class, find its defstruct-description
1108 ;; as a target descriptor, and return the slot list as a target descriptor.
1109 (defun dd-slots-from-core (type-name)
1110 (let* ((host-dd-layout (find-layout 'defstruct-description
))
1112 ;; This is inefficient, but not enough so to worry about.
1113 (or (car (assoc (cold-intern type-name
) *known-structure-classoids
*
1114 :key
(lambda (x) (read-slot x host-dd-layout
:name
))
1115 :test
#'descriptor
=))
1116 (error "No known layout for ~S" type-name
))))
1117 (read-slot target-dd host-dd-layout
:slots
)))
1119 (defvar *simple-vector-0-descriptor
*)
1120 (defvar *vacuous-slot-table
*)
1121 (defvar *cold-layout-gspace
* (or #!+immobile-space
'*immobile-fixedobj
* '*dynamic
*))
1122 (declaim (ftype (function (symbol descriptor descriptor descriptor descriptor
)
1125 (defun make-cold-layout (name length inherits depthoid bitmap
)
1126 (let ((result (allocate-struct (symbol-value *cold-layout-gspace
*) *layout-layout
*
1127 target-layout-length t
)))
1128 ;; Don't set the CLOS hash value: done in cold-init instead.
1130 ;; Set other slot values.
1132 ;; leave CLASSOID uninitialized for now
1133 (multiple-value-call
1134 #'write-slots result
*host-layout-of-layout
*
1135 :invalid
*nil-descriptor
*
1139 :info
*nil-descriptor
*
1140 :pure
*nil-descriptor
*
1142 ;; Nothing in cold-init needs to call EQUALP on a structure with raw slots,
1143 ;; but for type-correctness this slot needs to be a simple-vector.
1144 :equalp-tests
*simple-vector-0-descriptor
*
1145 :source-location
*nil-descriptor
*
1146 :%for-std-class-b
(make-fixnum-descriptor 0)
1147 :slot-list
*nil-descriptor
*
1148 (if (member name
'(null list symbol
))
1149 ;; Assign an empty slot-table. Why this is done only for three
1150 ;; classoids is ... too complicated to explain here in a few words,
1151 ;; but revision 18c239205d9349abc017b07e7894a710835c5205 broke it.
1152 ;; Keep this in sync with MAKE-SLOT-TABLE in pcl/slots-boot.
1153 (values :slot-table
(if (boundp '*vacuous-slot-table
*)
1154 *vacuous-slot-table
*
1155 (setq *vacuous-slot-table
*
1156 (host-constant-to-core '#(1 nil
)))))
1159 (setf (gethash (descriptor-bits result
) *cold-layout-names
*) name
1160 (gethash name
*cold-layouts
*) result
)))
1162 (defun predicate-for-specializer (type-name)
1163 (let ((classoid (find-classoid type-name nil
)))
1166 (cond ((dd-predicate-name (layout-info (classoid-layout classoid
))))
1167 ;; All early INSTANCEs should be STRUCTURE-OBJECTs.
1168 ;; Except: see hack for CONDITIONs in CLASS-DEPTHOID.
1169 ((eq type-name
'structure-object
) 'sb
!kernel
:%instancep
)))
1171 (let ((translation (specifier-type type-name
)))
1172 (aver (not (contains-unknown-type-p translation
)))
1173 (let ((predicate (find translation sb
!c
::*backend-type-predicates
*
1174 :test
#'type
= :key
#'car
)))
1175 (cond (predicate (cdr predicate
))
1176 ((eq type-name
'stream
) 'streamp
)
1177 ((eq type-name
't
) 'sb
!int
:constantly-t
)
1178 (t (error "No predicate for builtin: ~S" type-name
))))))
1180 #+nil
(format t
"~&; PREDICATE-FOR-SPECIALIZER: no classoid for ~S~%"
1183 (condition 'sb
!kernel
::!condition-p
))))))
1185 ;;; Convert SPECIFIER (equivalently OBJ) to its representation as a ctype
1186 ;;; in the cold core.
1187 (defvar *ctype-cache
*)
1189 (defvar *ctype-nullified-slots
* nil
)
1190 (defvar *built-in-classoid-nullified-slots
* nil
)
1192 ;; This function is memoized because it's essentially a constant,
1193 ;; but *nil-descriptor* isn't initialized by the time it's defined.
1194 (defun get-exceptional-slots (obj-type)
1195 (flet ((index (classoid-name slot-name
)
1196 (dsd-index (find slot-name
1197 (dd-slots (find-defstruct-description classoid-name
))
1201 (or *built-in-classoid-nullified-slots
*
1202 (setq *built-in-classoid-nullified-slots
*
1203 (append (get-exceptional-slots 'ctype
)
1204 (list (cons (index 'built-in-classoid
'sb
!kernel
::subclasses
)
1206 (cons (index 'built-in-classoid
'layout
)
1207 *nil-descriptor
*))))))
1209 (or *ctype-nullified-slots
*
1210 (setq *ctype-nullified-slots
*
1211 (list (cons (index 'ctype
'sb
!kernel
::class-info
)
1212 *nil-descriptor
*))))))))
1214 (defun ctype-to-core (specifier obj
)
1215 (declare (type ctype obj
))
1216 (if (classoid-p obj
)
1217 (let* ((cell (cold-find-classoid-cell (classoid-name obj
) :create t
))
1219 (read-slot cell
(find-layout 'sb
!kernel
::classoid-cell
) :classoid
)))
1220 (unless (cold-null cold-classoid
)
1221 (return-from ctype-to-core cold-classoid
)))
1222 ;; CTYPEs can't be TYPE=-hashed, but specifiers can be EQUAL-hashed.
1223 ;; Don't check the cache for classoids though; that would be wrong.
1224 ;; e.g. named-type T and classoid T both unparse to T.
1225 (awhen (gethash specifier
*ctype-cache
*)
1226 (return-from ctype-to-core it
)))
1228 (ctype-to-core-helper
1232 (xset (ctype-to-core-helper obj nil nil
))
1233 (ctype (ctype-to-core (type-specifier obj
) obj
))))
1234 (get-exceptional-slots (type-of obj
)))))
1235 (let ((type-class-vector
1236 (cold-symbol-value 'sb
!kernel
::*type-classes
*))
1237 (index (position (sb!kernel
::type-class-info obj
)
1238 sb
!kernel
::*type-classes
*)))
1239 ;; Push this instance into the list of fixups for its type class
1240 (cold-svset type-class-vector index
1241 (cold-cons result
(cold-svref type-class-vector index
))))
1242 (if (classoid-p obj
)
1243 ;; Place this classoid into its clasoid-cell.
1244 (let ((cell (cold-find-classoid-cell (classoid-name obj
) :create t
)))
1245 (write-slots cell
(find-layout 'sb
!kernel
::classoid-cell
)
1247 ;; Otherwise put it in the general cache
1248 (setf (gethash specifier
*ctype-cache
*) result
))
1251 (defun ctype-to-core-helper (obj obj-to-core-helper exceptional-slots
)
1252 (let* ((host-type (type-of obj
))
1253 (target-layout (or (gethash host-type
*cold-layouts
*)
1254 (error "No target layout for ~S" obj
)))
1255 (result (allocate-struct *dynamic
* target-layout
))
1256 (cold-dd-slots (dd-slots-from-core host-type
)))
1257 (aver (eql (layout-bitmap (find-layout host-type
))
1258 sb
!kernel
::+layout-all-tagged
+))
1260 (do ((len (cold-layout-length target-layout
))
1261 (index sb
!vm
:instance-data-start
(1+ index
)))
1262 ((= index len
) result
)
1265 (+ sb
!vm
:instance-slots-offset index
)
1266 (acond ((assq index exceptional-slots
) (cdr it
))
1267 (t (host-constant-to-core
1268 (funcall (dsd-accessor-from-cold-slots cold-dd-slots index
)
1270 obj-to-core-helper
)))))))
1272 ;; This is called to backpatch two small sets of objects:
1273 ;; - layouts created before layout-of-layout is made (3 counting LAYOUT itself)
1274 ;; - a small number of classoid-cells (~ 4).
1275 (defun set-instance-layout (thing layout
)
1276 #!+compact-instance-header
1277 ;; High half of the header points to the layout
1278 (write-wordindexed/raw thing
0 (logior (ash (descriptor-bits layout
) 32)
1279 (read-bits-wordindexed thing
0)))
1280 #!-compact-instance-header
1281 ;; Word following the header is the layout
1282 (write-wordindexed thing sb
!vm
:instance-slots-offset layout
))
1284 (defun cold-layout-of (cold-struct)
1285 #!+compact-instance-header
1286 (let ((bits (ash (read-bits-wordindexed cold-struct
0) -
32)))
1287 (if (zerop bits
) *nil-descriptor
* (make-random-descriptor bits
)))
1288 #!-compact-instance-header
1289 (read-wordindexed cold-struct sb
!vm
:instance-slots-offset
))
1291 (defun initialize-layouts ()
1292 (clrhash *cold-layouts
*)
1293 ;; This assertion is due to the fact that MAKE-COLD-LAYOUT does not
1294 ;; know how to set any raw slots.
1295 (aver (eql (layout-bitmap *host-layout-of-layout
*)
1296 sb
!kernel
::+layout-all-tagged
+))
1297 (setq *layout-layout
* (make-fixnum-descriptor 0))
1298 (flet ((chill-layout (name &rest inherits
)
1299 ;; Check that the number of specified INHERITS matches
1300 ;; the length of the layout's inherits in the cross-compiler.
1301 (let ((warm-layout (classoid-layout (find-classoid name
))))
1302 (assert (eql (length (layout-inherits warm-layout
))
1306 (number-to-core (layout-length warm-layout
))
1307 (vector-in-core inherits
)
1308 (number-to-core (layout-depthoid warm-layout
))
1309 (number-to-core (layout-bitmap warm-layout
))))))
1310 (let* ((t-layout (chill-layout 't
))
1311 (s-o-layout (chill-layout 'structure-object t-layout
)))
1312 (setf *layout-layout
* (chill-layout 'layout t-layout s-o-layout
))
1313 (dolist (layout (list t-layout s-o-layout
*layout-layout
*))
1314 (set-instance-layout layout
*layout-layout
*))
1315 (chill-layout 'function t-layout
)
1316 (let* ((sequence (chill-layout 'sequence t-layout
))
1317 (list (chill-layout 'list t-layout sequence
))
1318 (symbol (chill-layout 'symbol t-layout
)))
1319 (chill-layout 'null t-layout sequence list symbol
))
1320 (chill-layout 'package t-layout s-o-layout
))))
1322 ;;;; interning symbols in the cold image
1324 ;;; a map from package name as a host string to
1325 ;;; ((external-symbols . internal-symbols) . cold-package-descriptor)
1326 (defvar *cold-package-symbols
*)
1327 (declaim (type hash-table
*cold-package-symbols
*))
1329 (setf (get 'find-package
:sb-cold-funcall-handler
/for-value
)
1330 (lambda (descriptor &aux
(name (base-string-from-core descriptor
)))
1331 (or (cdr (gethash name
*cold-package-symbols
*))
1332 (error "Genesis could not find a target package named ~S" name
))))
1334 (defvar *classoid-cells
*)
1335 (defun cold-find-classoid-cell (name &key create
)
1336 (aver (eq create t
))
1337 (or (gethash name
*classoid-cells
*)
1338 (let ((layout (gethash 'sb
!kernel
::classoid-cell
*cold-layouts
*)) ; ok if nil
1339 (host-layout (find-layout 'sb
!kernel
::classoid-cell
)))
1340 (setf (gethash name
*classoid-cells
*)
1341 (write-slots (allocate-struct *dynamic
* layout
1342 (layout-length host-layout
))
1345 :pcl-class
*nil-descriptor
*
1346 :classoid
*nil-descriptor
*)))))
1348 (setf (get 'find-classoid-cell
:sb-cold-funcall-handler
/for-value
)
1349 #'cold-find-classoid-cell
)
1351 ;;; a map from descriptors to symbols, so that we can back up. The key
1352 ;;; is the address in the target core.
1353 (defvar *cold-symbols
*)
1354 (declaim (type hash-table
*cold-symbols
*))
1356 (defun set-readonly (string) (set-header-data string sb
!vm
:+vector-shareable
+))
1358 (defun initialize-packages ()
1359 (let ((package-data-list
1360 ;; docstrings are set in src/cold/warm. It would work to do it here,
1361 ;; but seems preferable not to saddle Genesis with such responsibility.
1362 (list* (sb-cold:make-package-data
:name
"COMMON-LISP" :doc nil
)
1363 (sb-cold:make-package-data
:name
"KEYWORD" :doc nil
)
1364 ;; ANSI encourages us to put extension packages
1365 ;; in the USE list of COMMON-LISP-USER.
1366 (sb-cold:make-package-data
1367 :name
"COMMON-LISP-USER" :doc nil
1368 :use
'("COMMON-LISP" "SB!ALIEN" "SB!DEBUG" "SB!EXT" "SB!GRAY" "SB!PROFILE"))
1369 (sb-cold::package-list-for-genesis
)))
1370 (package-layout (find-layout 'package
))
1371 (target-pkg-list nil
))
1372 (labels ((init-cold-package (name &optional docstring
)
1373 (let ((cold-package (allocate-struct (symbol-value *cold-layout-gspace
*)
1374 (gethash 'package
*cold-layouts
*))))
1375 (setf (gethash name
*cold-package-symbols
*)
1376 (cons (cons nil nil
) cold-package
))
1377 ;; Initialize string slots
1378 (write-slots cold-package package-layout
1379 :%name
(set-readonly
1380 (base-string-to-core
1381 (target-package-name name
)))
1382 :%nicknames
(chill-nicknames name
)
1383 :doc-string
(if docstring
1384 (base-string-to-core docstring
)
1386 :%use-list
*nil-descriptor
*)
1387 ;; the cddr of this will accumulate the 'used-by' package list
1388 (push (list name cold-package
) target-pkg-list
)))
1389 (target-package-name (string)
1390 (if (eql (mismatch string
"SB!") 3)
1391 (concatenate 'string
"SB-" (subseq string
3))
1393 (chill-nicknames (pkg-name)
1394 ;; Make the package nickname lists for the standard packages
1395 ;; be the minimum specified by ANSI, regardless of what value
1396 ;; the cross-compilation host happens to use.
1397 ;; For packages other than the standard packages, the nickname
1398 ;; list was specified by our package setup code, and we can just
1399 ;; propagate the current state into the target.
1401 (mapcar #'base-string-to-core
1402 (cond ((string= pkg-name
"COMMON-LISP") '("CL"))
1403 ((string= pkg-name
"COMMON-LISP-USER")
1405 ((string= pkg-name
"KEYWORD") '())
1407 ;; 'package-data-list' contains no nicknames.
1408 ;; (See comment in 'set-up-cold-packages')
1409 (aver (null (package-nicknames
1410 (find-package pkg-name
))))
1412 (find-cold-package (name)
1413 (cadr (find-package-cell name
)))
1414 (find-package-cell (name)
1415 (or (assoc (if (string= name
"CL") "COMMON-LISP" name
)
1416 target-pkg-list
:test
#'string
=)
1417 (error "No cold package named ~S" name
))))
1418 ;; pass 1: make all proto-packages
1419 (dolist (pd package-data-list
)
1420 (init-cold-package (sb-cold:package-data-name pd
)
1421 #!+sb-doc
(sb-cold::package-data-doc pd
)))
1422 ;; pass 2: set the 'use' lists and collect the 'used-by' lists
1423 (dolist (pd package-data-list
)
1424 (let ((this (find-cold-package (sb-cold:package-data-name pd
)))
1426 (dolist (that (sb-cold:package-data-use pd
))
1427 (let ((cell (find-package-cell that
)))
1428 (push (cadr cell
) use
)
1429 (push this
(cddr cell
))))
1430 (write-slots this package-layout
1431 :%use-list
(list-to-core (nreverse use
)))))
1432 ;; pass 3: set the 'used-by' lists
1433 (dolist (cell target-pkg-list
)
1434 (write-slots (cadr cell
) package-layout
1435 :%used-by-list
(list-to-core (cddr cell
)))))))
1437 ;;; sanity check for a symbol we're about to create on the target
1439 ;;; Make sure that the symbol has an appropriate package. In
1440 ;;; particular, catch the so-easy-to-make error of typing something
1441 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1442 ;;; need is SB!KERNEL:%BYTE-BLT.
1443 (defun package-ok-for-target-symbol-p (package)
1444 (let ((package-name (package-name package
)))
1446 ;; Cold interning things in these standard packages is OK. (Cold
1447 ;; interning things in the other standard package, CL-USER, isn't
1448 ;; OK. We just use CL-USER to expose symbols whose homes are in
1449 ;; other packages. Thus, trying to cold intern a symbol whose
1450 ;; home package is CL-USER probably means that a coding error has
1451 ;; been made somewhere.)
1452 (find package-name
'("COMMON-LISP" "KEYWORD") :test
#'string
=)
1453 ;; Cold interning something in one of our target-code packages,
1454 ;; which are ever-so-rigorously-and-elegantly distinguished by
1455 ;; this prefix on their names, is OK too.
1456 (string= package-name
"SB!" :end1
3 :end2
3)
1457 ;; This one is OK too, since it ends up being COMMON-LISP on the
1459 (string= package-name
"SB-XC")
1460 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1461 ;; package in the xc host? something we can't think of
1462 ;; a valid reason to cold intern, anyway...)
1465 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1467 ;;; Most host symbols we dump onto the target are created by SBCL
1468 ;;; itself, so that as long as we avoid gratuitously
1469 ;;; cross-compilation-unfriendly hacks, it just happens that their
1470 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1471 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1472 ;;; in the COMMON-LISP package, where we don't get to create the
1473 ;;; symbols but instead have to use the ones that the xc host created.
1474 ;;; In particular, while ANSI specifies which symbols are exported
1475 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1476 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1477 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1478 ;;; symbols in the CLOS package).
1479 (defun symbol-package-for-target-symbol (symbol)
1480 ;; We want to catch weird symbols like CLISP's
1481 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1482 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1483 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1484 (multiple-value-bind (cl-symbol cl-status
)
1485 (find-symbol (symbol-name symbol
) *cl-package
*)
1486 (if (and (eq symbol cl-symbol
)
1487 (eq cl-status
:external
))
1488 ;; special case, to work around possible xc host weirdness
1489 ;; in COMMON-LISP package
1492 (let ((result (symbol-package symbol
)))
1493 (unless (package-ok-for-target-symbol-p result
)
1494 (bug "~A in bad package for target: ~A" symbol result
))
1497 (defvar *uninterned-symbol-table
* (make-hash-table :test
#'equal
))
1498 ;; This coalesces references to uninterned symbols, which is allowed because
1499 ;; "similar-as-constant" is defined by string comparison, and since we only have
1500 ;; base-strings during Genesis, there is no concern about upgraded array type.
1501 ;; There is a subtlety of whether coalescing may occur across files
1502 ;; - the target compiler doesn't and couldn't - but here it doesn't matter.
1503 (defun get-uninterned-symbol (name)
1504 (or (gethash name
*uninterned-symbol-table
*)
1505 (let ((cold-symbol (allocate-symbol name
)))
1506 (setf (gethash name
*uninterned-symbol-table
*) cold-symbol
))))
1508 ;;; Dump the target representation of HOST-VALUE,
1509 ;;; the type of which is in a restrictive set.
1510 (defun host-constant-to-core (host-value &optional helper
)
1511 (let ((visited (make-hash-table :test
#'eq
)))
1512 (named-let target-representation
((value host-value
))
1513 (unless (typep value
'(or symbol number descriptor
))
1514 (let ((found (gethash value visited
)))
1515 (cond ((eq found
:pending
)
1516 (bug "circular constant?")) ; Circularity not permitted
1518 (return-from target-representation found
))))
1519 (setf (gethash value visited
) :pending
))
1520 (setf (gethash value visited
)
1523 (symbol (if (symbol-package value
)
1525 (get-uninterned-symbol (string value
))))
1526 (number (number-to-core value
))
1527 (string (base-string-to-core value
))
1528 (cons (cold-cons (target-representation (car value
))
1529 (target-representation (cdr value
))))
1531 (vector-in-core (map 'list
#'target-representation value
)))
1533 (or (and helper
(funcall helper value
))
1534 (error "host-constant-to-core: can't convert ~S"
1537 ;; Look up the target's descriptor for #'FUN where FUN is a host symbol.
1538 (defun target-symbol-function (symbol)
1539 (let ((f (cold-fdefn-fun (cold-fdefinition-object symbol
))))
1540 ;; It works only if DEFUN F was seen first.
1541 (aver (not (cold-null f
)))
1544 ;;; Return a handle on an interned symbol. If necessary allocate the
1545 ;;; symbol and record its home package.
1546 (defun cold-intern (symbol
1548 (gspace (symbol-value *cold-symbol-gspace
*))
1549 &aux
(package (symbol-package-for-target-symbol symbol
)))
1551 ;; Anything on the cross-compilation host which refers to the target
1552 ;; machinery through the host SB-XC package should be translated to
1553 ;; something on the target which refers to the same machinery
1554 ;; through the target COMMON-LISP package.
1555 (let ((p (find-package "SB-XC")))
1556 (when (eq package p
)
1557 (setf package
*cl-package
*))
1558 (when (eq (symbol-package symbol
) p
)
1559 (setf symbol
(intern (symbol-name symbol
) *cl-package
*))))
1561 (or (get symbol
'cold-intern-info
)
1562 (let ((handle (allocate-symbol (symbol-name symbol
) :gspace gspace
)))
1563 (setf (get symbol
'cold-intern-info
) handle
)
1564 ;; maintain reverse map from target descriptor to host symbol
1565 (setf (gethash (descriptor-bits handle
) *cold-symbols
*) symbol
)
1566 (let ((pkg-info (or (gethash (package-name package
) *cold-package-symbols
*)
1567 (error "No target package descriptor for ~S" package
))))
1568 (write-wordindexed handle sb
!vm
:symbol-package-slot
(cdr pkg-info
))
1569 (record-accessibility
1570 (or access
(nth-value 1 (find-symbol (symbol-name symbol
) package
)))
1571 pkg-info handle package symbol
))
1572 #!+sb-thread
(assign-tls-index-if-needed symbol handle
)
1573 (when (eq package
*keyword-package
*)
1574 (cold-set handle handle
))
1577 (defun record-accessibility (accessibility target-pkg-info symbol-descriptor
1578 &optional host-package host-symbol
)
1579 (let ((access-lists (car target-pkg-info
)))
1581 (:external
(push symbol-descriptor
(car access-lists
)))
1582 (:internal
(push symbol-descriptor
(cdr access-lists
)))
1583 (t (error "~S inaccessible in package ~S" host-symbol host-package
)))))
1585 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1586 ;;; It might be nice to put NIL on a readonly page by itself to prevent unsafe
1587 ;;; code from destroying the world with (RPLACx nil 'kablooey)
1588 (defun make-nil-descriptor ()
1589 (let* ((des (allocate-header+object
*static
* sb
!vm
:symbol-size
0))
1590 (result (make-descriptor (+ (descriptor-bits des
)
1591 (* 2 sb
!vm
:n-word-bytes
)
1592 (- sb
!vm
:list-pointer-lowtag
1593 sb
!vm
:other-pointer-lowtag
)))))
1594 (write-wordindexed des
1596 (make-other-immediate-descriptor
1598 sb
!vm
:symbol-widetag
))
1599 (write-wordindexed des
1600 (+ 1 sb
!vm
:symbol-value-slot
)
1602 (write-wordindexed des
1603 (+ 2 sb
!vm
:symbol-value-slot
) ; = 1 + symbol-hash-slot
1605 (write-wordindexed des
1606 (+ 1 sb
!vm
:symbol-info-slot
)
1607 (cold-cons result result
)) ; NIL's info is (nil . nil)
1608 (write-wordindexed des
1609 (+ 1 sb
!vm
:symbol-name-slot
)
1610 ;; NIL's name is in dynamic space because any extra
1611 ;; bytes allocated in static space would need to
1612 ;; be accounted for by STATIC-SYMBOL-OFFSET.
1613 (set-readonly (base-string-to-core "NIL" *dynamic
*)))
1614 (setf (gethash (descriptor-bits result
) *cold-symbols
*) nil
1615 (get nil
'cold-intern-info
) result
)))
1617 ;;; Since the initial symbols must be allocated before we can intern
1618 ;;; anything else, we intern those here. We also set the value of T.
1619 (defun initialize-static-space ()
1620 "Initialize the cold load symbol-hacking data structures."
1621 ;; NIL did not have its package assigned. Do that now.
1622 (let ((target-cl-pkg-info (gethash "COMMON-LISP" *cold-package-symbols
*)))
1623 ;; -1 is magic having to do with nil-as-cons vs. nil-as-symbol
1624 (write-wordindexed *nil-descriptor
* (- sb
!vm
:symbol-package-slot
1)
1625 (cdr target-cl-pkg-info
))
1626 (record-accessibility :external target-cl-pkg-info
*nil-descriptor
*))
1627 ;; Intern the others.
1628 (dovector (symbol sb
!vm
:+static-symbols
+)
1629 (let* ((des (cold-intern symbol
:gspace
*static
*))
1630 (offset-wanted (sb!vm
:static-symbol-offset symbol
))
1631 (offset-found (- (descriptor-bits des
)
1632 (descriptor-bits *nil-descriptor
*))))
1633 (unless (= offset-wanted offset-found
)
1634 (error "Offset from ~S to ~S is ~W, not ~W"
1639 ;; Establish the value of T.
1640 (let ((t-symbol (cold-intern t
:gspace
*static
*)))
1641 (cold-set t-symbol t-symbol
))
1642 (dolist (sym sb
!vm
::+c-callable-fdefns
+)
1643 (cold-fdefinition-object (cold-intern sym
) nil
*static
*))
1644 (dovector (sym sb
!vm
:+static-fdefns
+)
1645 (let* ((fdefn (cold-fdefinition-object (cold-intern sym
) nil
*static
*))
1646 (offset (- (+ (- (descriptor-bits fdefn
)
1647 sb
!vm
:other-pointer-lowtag
)
1648 (* sb
!vm
:fdefn-raw-addr-slot sb
!vm
:n-word-bytes
))
1649 (descriptor-bits *nil-descriptor
*)))
1650 (desired (sb!vm
:static-fun-offset sym
)))
1651 (unless (= offset desired
)
1652 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1653 sym nil offset desired
)))))
1655 ;;; Sort *COLD-LAYOUTS* to return them in a deterministic order.
1656 (defun sort-cold-layouts ()
1657 (sort (%hash-table-alist
*cold-layouts
*) #'<
1658 :key
(lambda (x) (descriptor-bits (cdr x
)))))
1660 ;;; Establish initial values for magic symbols.
1662 (defun finish-symbols ()
1663 (cold-set 'sb
!vm
::*current-catch-block
* (make-fixnum-descriptor 0))
1664 (cold-set 'sb
!vm
::*current-unwind-protect-block
* (make-fixnum-descriptor 0))
1666 (cold-set '*free-interrupt-context-index
* (make-fixnum-descriptor 0))
1668 (cold-set '*!initial-layouts
*
1670 (mapcar (lambda (layout)
1671 (cold-cons (cold-intern (car layout
)) (cdr layout
)))
1672 (sort-cold-layouts))))
1675 (cold-set 'sb
!vm
::*free-tls-index
*
1676 (make-descriptor (ash *genesis-tls-counter
* sb
!vm
:word-shift
)))
1678 (dolist (symbol sb
!impl
::*cache-vector-symbols
*)
1679 (cold-set symbol
*nil-descriptor
*))
1681 ;; Symbols for which no call to COLD-INTERN would occur - due to not being
1682 ;; referenced until warm init - must be artificially cold-interned.
1683 ;; Inasmuch as the "offending" things are compiled by ordinary target code
1684 ;; and not cold-init, I think we should use an ordinary DEFPACKAGE for
1685 ;; the added-on bits. What I've done is somewhat of a fragile kludge.
1687 (with-package-iterator (iter '("SB!PCL" "SB!MOP" "SB!GRAY" "SB!SEQUENCE"
1688 "SB!PROFILE" "SB!EXT" "SB!VM"
1689 "SB!C" "SB!FASL" "SB!DEBUG")
1692 (multiple-value-bind (foundp sym accessibility package
) (iter)
1693 (declare (ignore accessibility
))
1694 (cond ((not foundp
) (return))
1695 ((eq (symbol-package sym
) package
) (push sym syms
))))))
1696 (setf syms
(stable-sort syms
#'string
<))
1701 'sb
!impl
::*!initial-symbols
*
1705 (destructuring-bind (pkg-name . pkg-info
) pkgcons
1707 ;; Record shadowing symbols (except from SB-XC) in SB! packages.
1708 (when (eql (mismatch pkg-name
"SB!") 3)
1709 ;; Be insensitive to the host's ordering.
1710 (sort (remove (find-package "SB-XC")
1711 (package-shadowing-symbols (find-package pkg-name
))
1712 :key
#'symbol-package
) #'string
<))))
1713 (write-slots (cdr pkg-info
) ; package
1714 (find-layout 'package
)
1715 :%shadowing-symbols
(list-to-core
1716 (mapcar 'cold-intern shadow
))))
1717 (unless (member pkg-name
'("COMMON-LISP" "KEYWORD") :test
'string
=)
1718 (let ((host-pkg (find-package pkg-name
))
1719 (sb-xc-pkg (find-package "SB-XC"))
1721 ;; Now for each symbol directly present in this host-pkg,
1722 ;; i.e. accessible but not :INHERITED, figure out if the symbol
1723 ;; came from a different package, and if so, make a note of it.
1724 (with-package-iterator (iter host-pkg
:internal
:external
)
1725 (loop (multiple-value-bind (foundp sym accessibility
) (iter)
1726 (unless foundp
(return))
1727 (unless (or (eq (symbol-package sym
) host-pkg
)
1728 (eq (symbol-package sym
) sb-xc-pkg
))
1729 (push (cons sym accessibility
) syms
)))))
1730 (dolist (symcons (sort syms
#'string
< :key
#'car
))
1731 (destructuring-bind (sym . accessibility
) symcons
1732 (record-accessibility accessibility pkg-info
(cold-intern sym
)
1734 (cold-list (cdr pkg-info
)
1735 (vector-in-core (caar pkg-info
))
1736 (vector-in-core (cdar pkg-info
)))))
1737 (sort (%hash-table-alist
*cold-package-symbols
*)
1738 #'string
< :key
#'car
)))) ; Sort by package-name
1740 (dump-symbol-info-vectors
1741 (attach-fdefinitions-to-symbols
1742 (attach-classoid-cells-to-symbols (make-hash-table :test
#'eq
))))
1744 (cold-set '*!initial-debug-sources
* *current-debug-sources
*)
1748 (cold-set 'sb
!vm
::*fp-constant-0d0
* (number-to-core 0d0
))
1749 (cold-set 'sb
!vm
::*fp-constant-1d0
* (number-to-core 1d0
))
1750 (cold-set 'sb
!vm
::*fp-constant-0f0
* (number-to-core 0f0
))
1751 (cold-set 'sb
!vm
::*fp-constant-1f0
* (number-to-core 1f0
))))
1753 ;;;; functions and fdefinition objects
1755 ;;; a hash table mapping from fdefinition names to descriptors of cold
1758 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1759 ;;; we want to have only one entry per name, this must be an 'EQUAL
1760 ;;; hash table, not the default 'EQL.
1761 (defvar *cold-fdefn-objects
*)
1763 ;;; Given a cold representation of a symbol, return a warm
1765 (defun warm-symbol (des)
1766 ;; Note that COLD-INTERN is responsible for keeping the
1767 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1768 ;; uninterned symbol, the code below will fail. But as long as we
1769 ;; don't need to look up uninterned symbols during bootstrapping,
1771 (multiple-value-bind (symbol found-p
)
1772 (gethash (descriptor-bits des
) *cold-symbols
*)
1773 (declare (type symbol symbol
))
1775 (error "no warm symbol"))
1778 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1779 (defun cold-car (des)
1780 (aver (= (descriptor-lowtag des
) sb
!vm
:list-pointer-lowtag
))
1781 (read-wordindexed des sb
!vm
:cons-car-slot
))
1782 (defun cold-cdr (des)
1783 (aver (= (descriptor-lowtag des
) sb
!vm
:list-pointer-lowtag
))
1784 (read-wordindexed des sb
!vm
:cons-cdr-slot
))
1785 (defun cold-rplacd (des newval
)
1786 (aver (= (descriptor-lowtag des
) sb
!vm
:list-pointer-lowtag
))
1787 (write-wordindexed des sb
!vm
:cons-cdr-slot newval
)
1789 (defun cold-null (des) (descriptor= des
*nil-descriptor
*))
1791 ;;; Given a cold representation of a function name, return a warm
1793 (declaim (ftype (function ((or symbol descriptor
)) (or symbol list
)) warm-fun-name
))
1794 (defun warm-fun-name (des)
1797 ;; This parallels the logic at the start of COLD-INTERN
1798 ;; which re-homes symbols in SB-XC to COMMON-LISP.
1799 (if (eq (symbol-package des
) (find-package "SB-XC"))
1800 (intern (symbol-name des
) *cl-package
*)
1802 (ecase (descriptor-lowtag des
)
1803 (#.sb
!vm
:list-pointer-lowtag
1804 (aver (not (cold-null des
))) ; function named NIL? please no..
1805 (let ((rest (cold-cdr des
)))
1806 (aver (cold-null (cold-cdr rest
)))
1807 (list (warm-symbol (cold-car des
))
1808 (warm-symbol (cold-car rest
)))))
1809 (#.sb
!vm
:other-pointer-lowtag
1810 (warm-symbol des
))))))
1811 (legal-fun-name-or-type-error result
)
1815 (defun encode-fdefn-raw-addr (fdefn jump-target opcode
)
1816 (let ((disp (- jump-target
1817 (+ (descriptor-bits fdefn
)
1818 (- sb
!vm
:other-pointer-lowtag
)
1819 (ash sb
!vm
:fdefn-raw-addr-slot sb
!vm
:word-shift
)
1821 (logior (ash (ldb (byte 32 0) (the (signed-byte 32) disp
)) 8) opcode
)))
1823 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw
1824 (gspace #!+immobile-space
*immobile-fixedobj
*
1825 #!-immobile-space
*dynamic
*))
1826 (declare (type (or symbol descriptor
) cold-name
))
1827 (declare (special core-file-name
))
1828 (let ((warm-name (warm-fun-name cold-name
)))
1829 (or (gethash warm-name
*cold-fdefn-objects
*)
1830 (let ((fdefn (allocate-header+object gspace
(1- sb
!vm
:fdefn-size
) sb
!vm
:fdefn-widetag
)))
1831 (setf (gethash warm-name
*cold-fdefn-objects
*) fdefn
)
1832 (write-wordindexed fdefn sb
!vm
:fdefn-name-slot cold-name
)
1833 (unless leave-fn-raw
1834 (write-wordindexed fdefn sb
!vm
:fdefn-fun-slot
*nil-descriptor
*)
1836 (or (lookup-assembler-reference 'sb
!vm
::undefined-tramp core-file-name
)
1837 ;; Our preload for the tramps doesn't happen during host-1,
1838 ;; so substitute a usable value.
1840 (write-wordindexed/raw fdefn sb
!vm
:fdefn-raw-addr-slot
1841 #!+(and immobile-code x86-64
)
1842 (encode-fdefn-raw-addr fdefn tramp
#xE8
)
1843 #!-immobile-code tramp
)))
1846 (defun cold-functionp (descriptor)
1847 (eql (descriptor-lowtag descriptor
) sb
!vm
:fun-pointer-lowtag
))
1849 (defun cold-fun-entry-addr (fun)
1850 (aver (= (descriptor-lowtag fun
) sb
!vm
:fun-pointer-lowtag
))
1851 (+ (descriptor-bits fun
)
1852 (- sb
!vm
:fun-pointer-lowtag
)
1853 (ash sb
!vm
:simple-fun-code-offset sb
!vm
:word-shift
)))
1855 ;;; Handle a DEFUN in cold-load.
1856 (defun cold-fset (name defn source-loc
&optional inline-expansion
)
1857 ;; SOURCE-LOC can be ignored, because functions intrinsically store
1858 ;; their location as part of the code component.
1859 ;; The argument is supplied here only to provide context for
1860 ;; a redefinition warning, which can't happen in cold load.
1861 (declare (ignore source-loc
))
1862 (sb!int
:binding
* (((cold-name warm-name
)
1863 ;; (SETF f) was descriptorized when dumped, symbols were not.
1865 (values (cold-intern name
) name
)
1866 (values name
(warm-fun-name name
))))
1867 (fdefn (cold-fdefinition-object cold-name t
)))
1868 (when (cold-functionp (cold-fdefn-fun fdefn
))
1869 (error "Duplicate DEFUN for ~S" warm-name
))
1870 ;; There can't be any closures or funcallable instances.
1871 (aver (= (logand (descriptor-bits (read-memory defn
)) sb
!vm
:widetag-mask
)
1872 sb
!vm
:simple-fun-widetag
))
1873 (push (cold-cons cold-name inline-expansion
) *!cold-defuns
*)
1874 (write-wordindexed fdefn sb
!vm
:fdefn-fun-slot defn
)
1875 (let ((fun-entry-addr
1876 (+ (logandc2 (descriptor-bits defn
) sb
!vm
:lowtag-mask
)
1877 (ash sb
!vm
:simple-fun-code-offset sb
!vm
:word-shift
))))
1878 (declare (ignorable fun-entry-addr
)) ; sparc and arm don't need
1879 #!+(and immobile-code x86-64
)
1880 (write-wordindexed/raw fdefn sb
!vm
:fdefn-raw-addr-slot
1881 (encode-fdefn-raw-addr fdefn fun-entry-addr
#xE9
))
1884 #!+(or sparc arm
) (write-wordindexed fdefn sb
!vm
:fdefn-raw-addr-slot defn
)
1885 #!-
(or sparc arm
) (write-wordindexed/raw fdefn sb
!vm
:fdefn-raw-addr-slot
1889 ;;; Handle a DEFMETHOD in cold-load. "Very easily done". Right.
1890 (defun cold-defmethod (name &rest stuff
)
1891 (let ((gf (assoc name
*cold-methods
*)))
1893 (setq gf
(cons name nil
))
1894 (push gf
*cold-methods
*))
1895 (push stuff
(cdr gf
))))
1897 (defun attach-classoid-cells-to-symbols (hashtable)
1898 (let ((num (sb!c
::meta-info-number
(sb!c
::meta-info
:type
:classoid-cell
)))
1899 (layout (gethash 'sb
!kernel
::classoid-cell
*cold-layouts
*)))
1900 (when (plusp (hash-table-count *classoid-cells
*))
1902 ;; Iteration order is immaterial. The symbols will get sorted later.
1903 (maphash (lambda (symbol cold-classoid-cell
)
1904 ;; Some classoid-cells are dumped before the cold layout
1905 ;; of classoid-cell has been made, so fix those cases now.
1906 ;; Obviously it would be better if, in general, ALLOCATE-STRUCT
1907 ;; knew when something later must backpatch a cold layout
1908 ;; so that it could make a note to itself to do those ASAP
1909 ;; after the cold layout became known.
1910 (when (cold-null (cold-layout-of cold-classoid-cell
))
1911 (set-instance-layout cold-classoid-cell layout
))
1912 (setf (gethash symbol hashtable
)
1914 (gethash symbol hashtable
+nil-packed-infos
+)
1915 sb
!impl
::+no-auxilliary-key
+ num cold-classoid-cell
)))
1919 ;; Create pointer from SYMBOL and/or (SETF SYMBOL) to respective fdefinition
1921 (defun attach-fdefinitions-to-symbols (hashtable)
1922 ;; Collect fdefinitions that go with one symbol, e.g. CAR and (SETF CAR),
1923 ;; using the host's code for manipulating a packed info-vector.
1924 (maphash (lambda (warm-name cold-fdefn
)
1925 (with-globaldb-name (key1 key2
) warm-name
1926 :hairy
(error "Hairy fdefn name in genesis: ~S" warm-name
)
1928 (setf (gethash key1 hashtable
)
1930 (gethash key1 hashtable
+nil-packed-infos
+)
1931 key2
+fdefn-info-num
+ cold-fdefn
))))
1932 *cold-fdefn-objects
*)
1935 (defun dump-symbol-info-vectors (hashtable)
1936 ;; Emit in the same order symbols reside in core to avoid
1937 ;; sensitivity to the iteration order of host's maphash.
1938 (loop for
(warm-sym . info
)
1939 in
(sort (%hash-table-alist hashtable
) #'<
1940 :key
(lambda (x) (descriptor-bits (cold-intern (car x
)))))
1941 do
(write-wordindexed
1942 (cold-intern warm-sym
) sb
!vm
:symbol-info-slot
1943 ;; Each vector will have one fixnum, possibly the symbol SETF,
1944 ;; and one or two #<fdefn> objects in it, and/or a classoid-cell.
1946 (map 'list
(lambda (elt)
1948 (symbol (cold-intern elt
))
1949 (fixnum (make-fixnum-descriptor elt
))
1954 ;;;; fixups and related stuff
1956 ;;; an EQUAL hash table
1957 (defvar *cold-foreign-symbol-table
*)
1958 (declaim (type hash-table
*cold-foreign-symbol-table
*))
1960 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1962 (defun load-cold-foreign-symbol-table (filename)
1963 (/show
"load-cold-foreign-symbol-table" filename
)
1964 (with-open-file (file filename
)
1965 (loop for line
= (read-line file nil nil
)
1967 ;; UNIX symbol tables might have tabs in them, and tabs are
1968 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1969 ;; nice portable way to deal with them within Lisp, alas.
1970 ;; Fortunately, it's easy to use UNIX command line tools like
1971 ;; sed to remove the problem, so it's not too painful for us
1972 ;; to push responsibility for converting tabs to spaces out to
1975 ;; Other non-STANDARD-CHARs are problematic for the same reason.
1976 ;; Make sure that there aren't any..
1977 (let ((ch (find-if (lambda (char)
1978 (not (typep char
'standard-char
)))
1981 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1984 (setf line
(string-trim '(#\space
) line
))
1985 (let ((p1 (position #\space line
:from-end nil
))
1986 (p2 (position #\space line
:from-end t
)))
1987 (if (not (and p1 p2
(< p1 p2
)))
1988 ;; KLUDGE: It's too messy to try to understand all
1989 ;; possible output from nm, so we just punt the lines we
1990 ;; don't recognize. We realize that there's some chance
1991 ;; that might get us in trouble someday, so we warn
1993 (warn "ignoring unrecognized line ~S in ~A" line filename
)
1994 (multiple-value-bind (value name
)
1995 (if (string= "0x" line
:end2
2)
1996 (values (parse-integer line
:start
2 :end p1
:radix
16)
1997 (subseq line
(1+ p2
)))
1998 (values (parse-integer line
:end p1
:radix
16)
1999 (subseq line
(1+ p2
))))
2000 ;; KLUDGE CLH 2010-05-31: on darwin, nm gives us
2001 ;; _function but dlsym expects us to look up
2002 ;; function, without the leading _ . Therefore, we
2003 ;; strip it off here.
2005 (when (equal (char name
0) #\_
)
2006 (setf name
(subseq name
1)))
2007 (multiple-value-bind (old-value found
)
2008 (gethash name
*cold-foreign-symbol-table
*)
2010 (not (= old-value value
)))
2011 (warn "redefining ~S from #X~X to #X~X"
2012 name old-value value
)))
2013 (/show
"adding to *cold-foreign-symbol-table*:" name value
)
2014 (setf (gethash name
*cold-foreign-symbol-table
*) value
)
2016 (let ((at-position (position #\
@ name
)))
2018 (let ((name (subseq name
0 at-position
)))
2019 (multiple-value-bind (old-value found
)
2020 (gethash name
*cold-foreign-symbol-table
*)
2022 (not (= old-value value
)))
2023 (warn "redefining ~S from #X~X to #X~X"
2024 name old-value value
)))
2025 (setf (gethash name
*cold-foreign-symbol-table
*)
2029 (defun cold-foreign-symbol-address (name)
2030 (declare (ignorable name
))
2031 #!+crossbuild-test
#xf00fa8
; any random 4-octet-aligned value should do
2033 (or (find-foreign-symbol-in-table name
*cold-foreign-symbol-table
*)
2034 *foreign-symbol-placeholder-value
*
2036 (format *error-output
* "~&The foreign symbol table is:~%")
2037 (maphash (lambda (k v
)
2038 (format *error-output
* "~&~S = #X~8X~%" k v
))
2039 *cold-foreign-symbol-table
*)
2040 (error "The foreign symbol ~S is undefined." name
))))
2042 (defvar *cold-assembler-routines
*)
2044 (defvar *cold-assembler-fixups
*)
2045 (defvar *cold-static-call-fixups
*)
2047 (defun record-cold-assembler-routine (name address
)
2048 (/xhow
"in RECORD-COLD-ASSEMBLER-ROUTINE" name address
)
2049 (push (cons name address
)
2050 *cold-assembler-routines
*))
2052 (defun lookup-assembler-reference (symbol &optional
(errorp t
))
2053 (let ((value (cdr (assoc symbol
*cold-assembler-routines
*))))
2056 (error "Assembler routine ~S not defined." symbol
)))
2059 ;;; Unlike in the target, FOP-KNOWN-FUN sometimes has to backpatch.
2060 (defvar *deferred-known-fun-refs
*)
2062 ;;; In case we need to store code fixups in code objects.
2063 ;;; At present only the x86 backends use this
2064 (defvar *code-fixup-notes
*)
2066 ;;; Given a pointer to a code object and a byte offset relative to the
2067 ;;; tail of the code object's header, return a byte offset relative to the
2068 ;;; (beginning of the) code object.
2070 (declaim (ftype (function (descriptor sb
!vm
:word
)) calc-offset
))
2071 (defun calc-offset (code-object insts-offset-bytes
)
2072 (+ (ash (logand (get-header-data code-object
) sb
!vm
:short-header-max-words
)
2074 insts-offset-bytes
))
2076 (declaim (ftype (function (descriptor sb
!vm
:word sb
!vm
:word
2077 keyword
&optional keyword
) descriptor
)
2079 (defun do-cold-fixup (code-object after-header value kind
&optional flavor
)
2080 (declare (ignorable flavor
))
2081 (let* ((offset-within-code-object (calc-offset code-object after-header
))
2082 (gspace-byte-offset (+ (descriptor-byte-offset code-object
)
2083 offset-within-code-object
)))
2085 (sb!vm
::fixup-code-object code-object gspace-byte-offset value kind
)
2088 (let* ((gspace-data (descriptor-mem code-object
))
2089 (obj-start-addr (logandc2 (descriptor-bits code-object
) sb
!vm
:lowtag-mask
))
2092 (ash (logand (get-header-data code-object
)
2093 sb
!vm
:short-header-max-words
) sb
!vm
:word-shift
)
2095 (read-wordindexed code-object sb
!vm
:code-code-size-slot
))))
2096 (gspace-base (gspace-byte-address (descriptor-gspace code-object
)))
2098 (= (gspace-identifier (descriptor-intuit-gspace code-object
))
2099 dynamic-core-space-id
))
2101 (sb!vm
::sign-extend
(bvref-32 gspace-data gspace-byte-offset
)
2104 (declare (ignorable code-end-addr in-dynamic-space
))
2105 (assert (= obj-start-addr
2106 (+ gspace-base
(descriptor-byte-offset code-object
))))
2108 ;; See FIXUP-CODE-OBJECT in x86-vm.lisp and x86-64-vm.lisp.
2109 ;; Except for the use of saps, this is basically identical.
2112 (setf (bvref-32 gspace-data gspace-byte-offset
)
2113 (the (unsigned-byte 32) addr
))
2114 ;; Absolute fixups are recorded if within the object for x86.
2115 #!+x86
(and in-dynamic-space
2116 (< obj-start-addr addr code-end-addr
))
2117 ;; Absolute :immobile-object fixups are recorded for x86-64.
2118 #!+x86-64
(eq flavor
:immobile-object
))
2119 (:relative
; (used for arguments to X86 relative CALL instruction)
2120 (setf (bvref-32 gspace-data gspace-byte-offset
)
2121 (the (signed-byte 32)
2122 (- addr
(+ gspace-base gspace-byte-offset
4)))) ; 4 = size of rel32off
2123 ;; Relative fixups are recorded if without the object.
2124 ;; Except that read-only space contains calls to asm routines,
2125 ;; and we don't record those fixups.
2126 #!+x86
(and in-dynamic-space
2127 (not (< obj-start-addr addr code-end-addr
)))
2129 (push after-header
(gethash (descriptor-bits code-object
)
2130 *code-fixup-notes
*)))))
2133 (defun resolve-assembler-fixups ()
2134 (dolist (fixup *cold-assembler-fixups
*)
2135 (let* ((routine (car fixup
))
2136 (value (lookup-assembler-reference routine
)))
2138 (do-cold-fixup (second fixup
) (third fixup
) value
(fourth fixup
)))))
2139 ;; Static calls are very similar to assembler routine calls,
2140 ;; so take care of those too.
2141 (dolist (fixup *cold-static-call-fixups
*)
2142 (destructuring-bind (name kind code offset
) fixup
2143 (do-cold-fixup code offset
2144 (cold-fun-entry-addr
2145 (cold-fdefn-fun (cold-fdefinition-object name
)))
2150 (defparameter *dyncore-address
* sb
!vm
::linkage-table-space-start
)
2151 (defparameter *dyncore-linkage-keys
* nil
)
2152 (defparameter *dyncore-table
* (make-hash-table :test
'equal
))
2154 (defun dyncore-note-symbol (symbol-name datap
)
2155 "Register a symbol and return its address in proto-linkage-table."
2156 (let ((key (cons symbol-name datap
)))
2157 (symbol-macrolet ((entry (gethash key
*dyncore-table
*)))
2160 (prog1 *dyncore-address
*
2161 (push key
*dyncore-linkage-keys
*)
2162 (incf *dyncore-address
* sb
!vm
::linkage-table-entry-size
))))))))
2164 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
2165 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
2166 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
2167 ;;; target-load.lisp refers to.
2168 (defun foreign-symbols-to-core ()
2169 (flet ((to-core (list transducer target-symbol
)
2170 (cold-set target-symbol
(vector-in-core (mapcar transducer list
)))))
2172 (to-core (sort (%hash-table-alist
*cold-foreign-symbol-table
*) #'string
< :key
#'car
)
2174 (cold-cons (set-readonly (base-string-to-core (car symbol
)))
2175 (number-to-core (cdr symbol
))))
2176 '*!initial-foreign-symbols
*)
2178 ;; Linkage table is recomputed by Lisp, so foreign symbols have to be listed
2179 ;; in the proper order, which is the reverse of the currently stored order.
2180 (to-core (nreverse *dyncore-linkage-keys
*)
2182 (cold-cons (set-readonly (base-string-to-core (car symbol
)))
2184 'sb
!vm
::+required-runtime-c-symbols
+)
2185 (to-core (sort (copy-list *cold-assembler-routines
*) #'string
< :key
#'car
)
2187 (cold-cons (cold-intern (car rtn
)) (number-to-core (cdr rtn
))))
2188 '*!initial-assembler-routines
*)))
2191 ;;;; general machinery for cold-loading FASL files
2193 (defun pop-fop-stack (stack)
2194 (let ((top (svref stack
0)))
2195 (declare (type index top
))
2197 (error "FOP stack empty"))
2198 (setf (svref stack
0) (1- top
))
2201 ;;; Cause a fop to have a special definition for cold load.
2203 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
2204 ;;; looks up the encoding for this name (created by a previous DEFINE-FOP)
2205 ;;; instead of creating a new encoding.
2206 (defmacro define-cold-fop
((name &optional arglist
) &rest forms
)
2207 (let* ((code (get name
'opcode
))
2208 (argc (aref (car **fop-signatures
**) code
))
2209 (fname (symbolicate "COLD-" name
)))
2211 (error "~S is not a defined FOP." name
))
2212 (when (and (plusp argc
) (not (singleton-p arglist
)))
2213 (error "~S must take one argument" name
))
2215 (defun ,fname
(.fasl-input.
,@arglist
)
2216 (declare (ignorable .fasl-input.
))
2217 (macrolet ((fasl-input () '(the fasl-input .fasl-input.
))
2218 (fasl-input-stream () '(%fasl-input-stream
(fasl-input)))
2220 '(pop-fop-stack (%fasl-input-stack
(fasl-input)))))
2222 ;; We simply overwrite elements of **FOP-FUNS** since the contents
2223 ;; of the host are never propagated directly into the target core.
2224 ,@(loop for i from code to
(logior code
(if (plusp argc
) 3 0))
2225 collect
`(setf (svref **fop-funs
** ,i
) #',fname
)))))
2227 ;;; Cause a fop to be undefined in cold load.
2228 (defmacro not-cold-fop
(name)
2229 `(define-cold-fop (,name
)
2230 (error "The fop ~S is not supported in cold load." ',name
)))
2232 ;;; COLD-LOAD loads stuff into the core image being built by calling
2233 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
2234 ;;; loading functions.
2235 (defun cold-load (filename)
2236 "Load the file named by FILENAME into the cold load image being built."
2237 (write-line (namestring filename
))
2238 (with-open-file (s filename
:element-type
'(unsigned-byte 8))
2239 (load-as-fasl s nil nil
)))
2241 ;;;; miscellaneous cold fops
2243 (define-cold-fop (fop-misc-trap) *unbound-marker
*)
2245 (define-cold-fop (fop-character (c))
2246 (make-character-descriptor c
))
2248 (define-cold-fop (fop-empty-list) nil
)
2249 (define-cold-fop (fop-truth) t
)
2251 (define-cold-fop (fop-struct (size)) ; n-words incl. layout, excluding header
2252 (let* ((layout (pop-stack))
2253 (result (allocate-struct *dynamic
* layout size
))
2254 (bitmap (descriptor-fixnum
2255 (read-slot layout
*host-layout-of-layout
* :bitmap
))))
2256 ;; Raw slots can not possibly work because dump-struct uses
2257 ;; %RAW-INSTANCE-REF/WORD which does not exist in the cross-compiler.
2258 ;; Remove this assertion if that problem is somehow circumvented.
2259 (unless (eql bitmap sb
!kernel
::+layout-all-tagged
+)
2260 (error "Raw slots not working in genesis."))
2261 (loop for index downfrom
(1- size
) to sb
!vm
:instance-data-start
2262 for val
= (pop-stack) then
(pop-stack)
2263 do
(write-wordindexed result
2264 (+ index sb
!vm
:instance-slots-offset
)
2265 (if (logbitp index bitmap
)
2267 (descriptor-word-sized-integer val
))))
2270 (define-cold-fop (fop-layout)
2271 (let* ((bitmap-des (pop-stack))
2272 (length-des (pop-stack))
2273 (depthoid-des (pop-stack))
2274 (cold-inherits (pop-stack))
2276 (old-layout-descriptor (gethash name
*cold-layouts
*)))
2277 (declare (type descriptor length-des depthoid-des cold-inherits
))
2278 (declare (type symbol name
))
2279 ;; If a layout of this name has been defined already
2280 (if old-layout-descriptor
2281 ;; Enforce consistency between the previous definition and the
2282 ;; current definition, then return the previous definition.
2283 (flet ((get-slot (keyword)
2284 (read-slot old-layout-descriptor
*host-layout-of-layout
* keyword
)))
2285 (let ((old-length (descriptor-fixnum (get-slot :length
)))
2286 (old-depthoid (descriptor-fixnum (get-slot :depthoid
)))
2287 (old-bitmap (host-object-from-core (get-slot :bitmap
)))
2288 (length (descriptor-fixnum length-des
))
2289 (depthoid (descriptor-fixnum depthoid-des
))
2290 (bitmap (host-object-from-core bitmap-des
)))
2291 (unless (= length old-length
)
2292 (error "cold loading a reference to class ~S when the compile~%~
2293 time length was ~S and current length is ~S"
2297 (unless (cold-vector-elements-eq cold-inherits
(get-slot :inherits
))
2298 (error "cold loading a reference to class ~S when the compile~%~
2299 time inherits were ~S~%~
2300 and current inherits are ~S"
2302 (listify-cold-inherits cold-inherits
)
2303 (listify-cold-inherits (get-slot :inherits
))))
2304 (unless (= depthoid old-depthoid
)
2305 (error "cold loading a reference to class ~S when the compile~%~
2306 time inheritance depthoid was ~S and current inheritance~%~
2311 (unless (= bitmap old-bitmap
)
2312 (error "cold loading a reference to class ~S when the compile~%~
2313 time raw-slot-bitmap was ~S and is currently ~S"
2314 name bitmap old-bitmap
)))
2315 old-layout-descriptor
)
2316 ;; Make a new definition from scratch.
2317 (make-cold-layout name length-des cold-inherits depthoid-des bitmap-des
))))
2319 ;;;; cold fops for loading symbols
2321 ;;; Load a symbol SIZE characters long from FASL-INPUT, and
2322 ;;; intern that symbol in PACKAGE.
2323 (defun cold-load-symbol (length+flag package fasl-input
)
2324 (let ((string (make-string (ash length
+flag -
1))))
2325 (read-string-as-bytes (%fasl-input-stream fasl-input
) string
)
2326 (push-fop-table (intern string package
) fasl-input
)))
2328 ;; I don't feel like hacking up DEFINE-COLD-FOP any more than necessary,
2329 ;; so this code is handcrafted to accept two operands.
2330 (flet ((fop-cold-symbol-in-package-save (fasl-input length
+flag pkg-index
)
2331 (cold-load-symbol length
+flag
(ref-fop-table fasl-input pkg-index
)
2333 (let ((i (get 'fop-symbol-in-package-save
'opcode
)))
2334 (fill **fop-funs
** #'fop-cold-symbol-in-package-save
:start i
:end
(+ i
4))))
2336 (define-cold-fop (fop-lisp-symbol-save (length+flag
))
2337 (cold-load-symbol length
+flag
*cl-package
* (fasl-input)))
2339 (define-cold-fop (fop-keyword-symbol-save (length+flag
))
2340 (cold-load-symbol length
+flag
*keyword-package
* (fasl-input)))
2342 (define-cold-fop (fop-uninterned-symbol-save (length+flag
))
2343 (let ((name (make-string (ash length
+flag -
1))))
2344 (read-string-as-bytes (fasl-input-stream) name
)
2345 (push-fop-table (get-uninterned-symbol name
) (fasl-input))))
2347 (define-cold-fop (fop-copy-symbol-save (index))
2348 (let* ((symbol (ref-fop-table (fasl-input) index
))
2350 (if (symbolp symbol
)
2351 (symbol-name symbol
)
2352 (base-string-from-core
2353 (read-wordindexed symbol sb
!vm
:symbol-name-slot
)))))
2354 ;; Genesis performs additional coalescing of uninterned symbols
2355 (push-fop-table (get-uninterned-symbol name
) (fasl-input))))
2357 ;;;; cold fops for loading packages
2359 (define-cold-fop (fop-named-package-save (namelen))
2360 (let ((name (make-string namelen
)))
2361 (read-string-as-bytes (fasl-input-stream) name
)
2362 (push-fop-table (find-package name
) (fasl-input))))
2364 ;;;; cold fops for loading lists
2366 ;;; Make a list of the top LENGTH things on the fop stack. The last
2367 ;;; cdr of the list is set to LAST.
2368 (defmacro cold-stack-list
(length last
)
2369 `(do* ((index ,length
(1- index
))
2370 (result ,last
(cold-cons (pop-stack) result
)))
2371 ((= index
0) result
)
2372 (declare (fixnum index
))))
2374 (define-cold-fop (fop-list)
2375 (cold-stack-list (read-byte-arg (fasl-input-stream)) *nil-descriptor
*))
2376 (define-cold-fop (fop-list*)
2377 (cold-stack-list (read-byte-arg (fasl-input-stream)) (pop-stack)))
2378 (define-cold-fop (fop-list-1)
2379 (cold-stack-list 1 *nil-descriptor
*))
2380 (define-cold-fop (fop-list-2)
2381 (cold-stack-list 2 *nil-descriptor
*))
2382 (define-cold-fop (fop-list-3)
2383 (cold-stack-list 3 *nil-descriptor
*))
2384 (define-cold-fop (fop-list-4)
2385 (cold-stack-list 4 *nil-descriptor
*))
2386 (define-cold-fop (fop-list-5)
2387 (cold-stack-list 5 *nil-descriptor
*))
2388 (define-cold-fop (fop-list-6)
2389 (cold-stack-list 6 *nil-descriptor
*))
2390 (define-cold-fop (fop-list-7)
2391 (cold-stack-list 7 *nil-descriptor
*))
2392 (define-cold-fop (fop-list-8)
2393 (cold-stack-list 8 *nil-descriptor
*))
2394 (define-cold-fop (fop-list*-
1)
2395 (cold-stack-list 1 (pop-stack)))
2396 (define-cold-fop (fop-list*-
2)
2397 (cold-stack-list 2 (pop-stack)))
2398 (define-cold-fop (fop-list*-
3)
2399 (cold-stack-list 3 (pop-stack)))
2400 (define-cold-fop (fop-list*-
4)
2401 (cold-stack-list 4 (pop-stack)))
2402 (define-cold-fop (fop-list*-
5)
2403 (cold-stack-list 5 (pop-stack)))
2404 (define-cold-fop (fop-list*-
6)
2405 (cold-stack-list 6 (pop-stack)))
2406 (define-cold-fop (fop-list*-
7)
2407 (cold-stack-list 7 (pop-stack)))
2408 (define-cold-fop (fop-list*-
8)
2409 (cold-stack-list 8 (pop-stack)))
2411 ;;;; cold fops for loading vectors
2413 (define-cold-fop (fop-base-string (len))
2414 (let ((string (make-string len
)))
2415 (read-string-as-bytes (fasl-input-stream) string
)
2416 (set-readonly (base-string-to-core string
))))
2419 (define-cold-fop (fop-character-string (len))
2420 (bug "CHARACTER-STRING[~D] dumped by cross-compiler." len
))
2422 (define-cold-fop (fop-vector (size))
2424 *simple-vector-0-descriptor
*
2425 (let ((result (allocate-vector-object *dynamic
*
2428 sb
!vm
:simple-vector-widetag
)))
2429 (do ((index (1- size
) (1- index
)))
2431 (declare (fixnum index
))
2432 (write-wordindexed result
2433 (+ index sb
!vm
:vector-data-offset
)
2435 (set-readonly result
))))
2437 (define-cold-fop (fop-spec-vector)
2438 (let* ((len (read-word-arg (fasl-input-stream)))
2439 (type (read-byte-arg (fasl-input-stream)))
2440 (sizebits (aref **saetp-bits-per-length
** type
))
2441 (result (progn (aver (< sizebits
255))
2442 (allocate-vector-object *dynamic
* sizebits len type
)))
2443 (start (+ (descriptor-byte-offset result
)
2444 (ash sb
!vm
:vector-data-offset sb
!vm
:word-shift
)))
2446 (ceiling (* len sizebits
)
2447 sb
!vm
:n-byte-bits
))))
2448 (read-bigvec-as-sequence-or-die (descriptor-mem result
)
2452 (set-readonly result
)))
2454 (not-cold-fop fop-array
)
2456 ;; This code is unexercised. The only use of FOP-ARRAY is from target-dump.
2457 ;; It would be a shame to delete it though, as it might come in handy.
2458 (define-cold-fop (fop-array)
2459 (let* ((rank (read-word-arg (fasl-input-stream)))
2460 (data-vector (pop-stack))
2461 (result (allocate-object *dynamic
*
2462 (+ sb
!vm
:array-dimensions-offset rank
)
2463 sb
!vm
:other-pointer-lowtag
)))
2464 (write-header-word result rank sb
!vm
:simple-array-widetag
)
2465 (write-wordindexed result sb
!vm
:array-fill-pointer-slot
*nil-descriptor
*)
2466 (write-wordindexed result sb
!vm
:array-data-slot data-vector
)
2467 (write-wordindexed result sb
!vm
:array-displacement-slot
*nil-descriptor
*)
2468 (write-wordindexed result sb
!vm
:array-displaced-p-slot
*nil-descriptor
*)
2469 (write-wordindexed result sb
!vm
:array-displaced-from-slot
*nil-descriptor
*)
2470 (let ((total-elements 1))
2471 (dotimes (axis rank
)
2472 (let ((dim (pop-stack)))
2473 (unless (is-fixnum-lowtag (descriptor-lowtag dim
))
2474 (error "non-fixnum dimension? (~S)" dim
))
2475 (setf total-elements
(* total-elements
(descriptor-fixnum dim
)))
2476 (write-wordindexed result
2477 (+ sb
!vm
:array-dimensions-offset axis
)
2479 (write-wordindexed result
2480 sb
!vm
:array-elements-slot
2481 (make-fixnum-descriptor total-elements
)))
2485 ;;;; cold fops for loading numbers
2487 (defmacro define-cold-number-fop
(fop &optional arglist
)
2488 ;; Invoke the ordinary warm version of this fop to cons the number.
2489 `(define-cold-fop (,fop
,arglist
)
2490 (number-to-core (,fop
(fasl-input) ,@arglist
))))
2492 (define-cold-number-fop fop-single-float
)
2493 (define-cold-number-fop fop-double-float
)
2494 (define-cold-number-fop fop-word-integer
)
2495 (define-cold-number-fop fop-byte-integer
)
2496 (define-cold-number-fop fop-complex-single-float
)
2497 (define-cold-number-fop fop-complex-double-float
)
2498 (define-cold-number-fop fop-integer
(n-bytes))
2500 (define-cold-fop (fop-ratio)
2501 (let ((den (pop-stack)))
2502 (number-pair-to-core (pop-stack) den sb
!vm
:ratio-widetag
)))
2504 (define-cold-fop (fop-complex)
2505 (let ((im (pop-stack)))
2506 (number-pair-to-core (pop-stack) im sb
!vm
:complex-widetag
)))
2508 ;;;; cold fops for calling (or not calling)
2510 (not-cold-fop fop-eval
)
2511 (not-cold-fop fop-eval-for-effect
)
2513 (defvar *load-time-value-counter
*)
2515 (flet ((pop-args (fasl-input)
2517 (stack (%fasl-input-stack fasl-input
)))
2518 (dotimes (i (read-byte-arg (%fasl-input-stream fasl-input
))
2519 (values (pop-fop-stack stack
) args
))
2520 (push (pop-fop-stack stack
) args
))))
2521 (call (fun-name handler-name args
)
2522 (acond ((get fun-name handler-name
) (apply it args
))
2523 (t (error "Can't ~S ~S in cold load" handler-name fun-name
)))))
2525 (define-cold-fop (fop-funcall)
2526 (multiple-value-bind (fun args
) (pop-args (fasl-input))
2530 ;; Special form #'F fopcompiles into `(FDEFINITION ,f)
2531 (aver (and (singleton-p args
) (symbolp (car args
))))
2532 (target-symbol-function (car args
)))
2533 (cons (cold-cons (first args
) (second args
)))
2534 (symbol-global-value (cold-symbol-value (first args
)))
2535 (t (call fun
:sb-cold-funcall-handler
/for-value args
)))
2536 (let ((counter *load-time-value-counter
*))
2537 (push (cold-list (cold-intern :load-time-value
) fun
2538 (number-to-core counter
)) *!cold-toplevels
*)
2539 (setf *load-time-value-counter
* (1+ counter
))
2540 (make-descriptor 0 :load-time-value counter
)))))
2542 (define-cold-fop (fop-funcall-for-effect)
2543 (multiple-value-bind (fun args
) (pop-args (fasl-input))
2545 (push fun
*!cold-toplevels
*)
2547 (sb!impl
::%defun
(apply #'cold-fset args
))
2548 (sb!pcl
::!trivial-defmethod
(apply #'cold-defmethod
args))
2549 (sb!kernel
::%defstruct
2550 (push args
*known-structure-classoids
*)
2551 (push (apply #'cold-list
(cold-intern 'defstruct
) args
)
2554 (destructuring-bind (name val . rest
) args
2555 (cold-set name
(if (symbolp val
) (cold-intern val
) val
))
2556 (push (cold-cons (cold-intern name
) (list-to-core rest
))
2557 *!cold-defconstants
*)))
2559 (aver (= (length args
) 2))
2560 (cold-set (first args
)
2561 (let ((val (second args
)))
2562 (if (symbolp val
) (cold-intern val
) val
))))
2563 (%svset
(apply 'cold-svset args
))
2564 (t (call fun
:sb-cold-funcall-handler
/for-effect args
)))))))
2566 (defun finalize-load-time-value-noise ()
2567 (cold-set '*!load-time-values
*
2568 (allocate-vector-object *dynamic
*
2570 *load-time-value-counter
*
2571 sb
!vm
:simple-vector-widetag
)))
2574 ;;;; cold fops for fixing up circularities
2576 (define-cold-fop (fop-rplaca)
2577 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2578 (idx (read-word-arg (fasl-input-stream))))
2579 (write-memory (cold-nthcdr idx obj
) (pop-stack))))
2581 (define-cold-fop (fop-rplacd)
2582 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2583 (idx (read-word-arg (fasl-input-stream))))
2584 (write-wordindexed (cold-nthcdr idx obj
) 1 (pop-stack))))
2586 (define-cold-fop (fop-svset)
2587 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2588 (idx (read-word-arg (fasl-input-stream))))
2589 (write-wordindexed obj
2591 (ecase (descriptor-lowtag obj
)
2592 (#.sb
!vm
:instance-pointer-lowtag
1)
2593 (#.sb
!vm
:other-pointer-lowtag
2)))
2596 (define-cold-fop (fop-structset)
2597 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2598 (idx (read-word-arg (fasl-input-stream))))
2599 (write-wordindexed obj
(+ idx sb
!vm
:instance-slots-offset
) (pop-stack))))
2601 (define-cold-fop (fop-nthcdr)
2602 (cold-nthcdr (read-word-arg (fasl-input-stream)) (pop-stack)))
2604 (defun cold-nthcdr (index obj
)
2606 (setq obj
(read-wordindexed obj sb
!vm
:cons-cdr-slot
)))
2609 ;;;; cold fops for loading code objects and functions
2611 (define-cold-fop (fop-fdefn)
2612 (cold-fdefinition-object (pop-stack)))
2614 (define-cold-fop (fop-known-fun)
2615 (let* ((name (pop-stack))
2616 (fun (cold-fdefn-fun (cold-fdefinition-object name
))))
2617 (if (cold-null fun
) `(:known-fun .
,name
) fun
)))
2619 #!-
(or x86
(and x86-64
(not immobile-space
)))
2620 (define-cold-fop (fop-sanctify-for-execution)
2623 ;;; Setting this variable shows what code looks like before any
2624 ;;; fixups (or function headers) are applied.
2625 #!+sb-show
(defvar *show-pre-fixup-code-p
* nil
)
2627 (defun cold-load-code (fasl-input code-size nconst nfuns
)
2628 (macrolet ((pop-stack () '(pop-fop-stack (%fasl-input-stack fasl-input
))))
2629 (let* ((raw-header-n-words (+ sb
!vm
:code-constants-offset nconst
))
2630 ;; Note that the number of constants is rounded up to ensure
2631 ;; that the code vector will be properly aligned.
2632 (header-n-words (round-up raw-header-n-words
2))
2633 (toplevel-p (pop-stack))
2634 (debug-info (pop-stack))
2635 (des (allocate-cold-descriptor
2636 #!-immobile-code
*dynamic
*
2637 ;; toplevel-p is an indicator of whether the code will
2638 ;; will become garbage. If so, put it in dynamic space,
2639 ;; otherwise immobile space.
2641 (if toplevel-p
*dynamic
* *immobile-varyobj
*)
2642 (+ (ash header-n-words sb
!vm
:word-shift
) code-size
)
2643 sb
!vm
:other-pointer-lowtag
)))
2644 (declare (ignorable toplevel-p
))
2645 (write-header-word des header-n-words sb
!vm
:code-header-widetag
)
2646 (write-wordindexed des sb
!vm
:code-code-size-slot
2647 (make-fixnum-descriptor code-size
))
2648 (write-wordindexed des sb
!vm
:code-debug-info-slot debug-info
)
2649 (do ((index (1- raw-header-n-words
) (1- index
)))
2650 ((< index sb
!vm
:code-constants-offset
))
2651 (let ((obj (pop-stack)))
2652 (if (and (consp obj
) (eq (car obj
) :known-fun
))
2653 (push (list* (cdr obj
) des index
) *deferred-known-fun-refs
*)
2654 (write-wordindexed des index obj
))))
2655 (let* ((start (+ (descriptor-byte-offset des
)
2656 (ash header-n-words sb
!vm
:word-shift
)))
2657 (end (+ start code-size
)))
2658 (read-bigvec-as-sequence-or-die (descriptor-mem des
)
2659 (%fasl-input-stream fasl-input
)
2663 ;; Emulate NEW-SIMPLE-FUN in target-core
2664 (loop for fun-index from
(1- nfuns
) downto
0
2665 do
(let ((offset (read-varint-arg fasl-input
)))
2667 (let ((bytes (descriptor-mem des
))
2668 (index (+ (descriptor-byte-offset des
)
2669 (calc-offset des
(ash (1- fun-index
) 2)))))
2670 (aver (eql (bvref-32 bytes index
) 0))
2671 (setf (bvref-32 bytes index
) offset
))
2673 (write-wordindexed/raw
2675 sb
!vm
::code-n-entries-slot
2676 (logior (ash offset
16)
2677 (ash nfuns sb
!vm
:n-fixnum-tag-bits
)))
2679 (write-wordindexed/raw
2681 (logior (ash (logior (ash offset
16) nfuns
) 32)
2682 (read-bits-wordindexed des
0))))))
2685 (when *show-pre-fixup-code-p
*
2686 (format *trace-output
*
2687 "~&/raw code from code-fop ~W ~W:~%"
2690 (do ((i start
(+ i sb
!vm
:n-word-bytes
)))
2692 (format *trace-output
*
2693 "/#X~8,'0x: #X~8,'0x~%"
2694 (+ i
(gspace-byte-address (descriptor-gspace des
)))
2695 (bvref-32 (descriptor-mem des
) i
)))))
2698 (let ((i (get 'fop-code
'opcode
)))
2699 (fill **fop-funs
** #'cold-load-code
:start i
:end
(+ i
4)))
2701 (defun resolve-deferred-known-funs ()
2702 (dolist (item *deferred-known-fun-refs
*)
2703 (let ((fun (cold-fdefn-fun (cold-fdefinition-object (car item
)))))
2704 (aver (not (cold-null fun
)))
2705 (let ((place (cdr item
)))
2706 (write-wordindexed (car place
) (cdr place
) fun
)))))
2708 (define-cold-fop (fop-alter-code (slot))
2709 (let ((value (pop-stack))
2711 (write-wordindexed code slot value
)))
2713 (defvar *simple-fun-metadata
* (make-hash-table :test
'equalp
))
2715 ;; Return an expression that can be used to coalesce type-specifiers
2716 ;; and lambda lists attached to simple-funs. It doesn't have to be
2717 ;; a "correct" host representation, just something that preserves EQUAL-ness.
2718 (defun make-equal-comparable-thing (descriptor)
2719 (labels ((recurse (x)
2720 (cond ((cold-null x
) (return-from recurse nil
))
2721 ((is-fixnum-lowtag (descriptor-lowtag x
))
2722 (return-from recurse
(descriptor-fixnum x
)))
2724 ((is-other-immediate-lowtag (descriptor-lowtag x
))
2725 (let ((bits (descriptor-bits x
)))
2726 (when (= (logand bits sb
!vm
:widetag-mask
)
2727 sb
!vm
:single-float-widetag
)
2728 (return-from recurse
`(:ffloat-bits
,bits
))))))
2729 (ecase (descriptor-lowtag x
)
2730 (#.sb
!vm
:list-pointer-lowtag
2731 (cons (recurse (cold-car x
)) (recurse (cold-cdr x
))))
2732 (#.sb
!vm
:other-pointer-lowtag
2733 (ecase (logand (descriptor-bits (read-memory x
)) sb
!vm
:widetag-mask
)
2734 (#.sb
!vm
:symbol-widetag
2735 (if (cold-null (read-wordindexed x sb
!vm
:symbol-package-slot
))
2736 (get-or-make-uninterned-symbol
2737 (base-string-from-core
2738 (read-wordindexed x sb
!vm
:symbol-name-slot
)))
2741 (#.sb
!vm
:single-float-widetag
2743 ,(read-bits-wordindexed x sb
!vm
:single-float-value-slot
)))
2744 (#.sb
!vm
:double-float-widetag
2746 ,(read-bits-wordindexed x sb
!vm
:double-float-value-slot
)
2748 ,(read-bits-wordindexed
2749 x
(1+ sb
!vm
:double-float-value-slot
))))
2750 (#.sb
!vm
:bignum-widetag
2751 (bignum-from-core x
))
2752 (#.sb
!vm
:simple-base-string-widetag
2753 (base-string-from-core x
))
2754 ;; Why do function lambda lists have simple-vectors in them?
2755 ;; Because we expose all &OPTIONAL and &KEY default forms.
2756 ;; I think this is abstraction leakage, except possibly for
2757 ;; advertised constant defaults of NIL and such.
2758 ;; How one expresses a value as a sexpr should otherwise
2759 ;; be of no concern to a user of the code.
2760 (#.sb
!vm
:simple-vector-widetag
2761 (vector-from-core x
#'recurse
))))))
2762 ;; Return a warm symbol whose name is similar to NAME, coaelescing
2763 ;; all occurrences of #:.WHOLE. across all files, e.g.
2764 (get-or-make-uninterned-symbol (name)
2765 (let ((key `(:uninterned-symbol
,name
)))
2766 (or (gethash key
*simple-fun-metadata
*)
2767 (let ((symbol (make-symbol name
)))
2768 (setf (gethash key
*simple-fun-metadata
*) symbol
))))))
2769 (recurse descriptor
)))
2771 (defun fun-offset (code-object fun-index
)
2773 (bvref-32 (descriptor-mem code-object
)
2774 (+ (descriptor-byte-offset code-object
)
2775 (calc-offset code-object
(ash (1- fun-index
) 2))))
2777 #!-
64-bit
(read-bits-wordindexed code-object sb
!vm
::code-n-entries-slot
)
2778 #!+64-bit
(ldb (byte 32 32) (read-bits-wordindexed code-object
0)))))
2780 (defun compute-fun (code-object fun-index
)
2781 (let* ((offset-from-insns-start (fun-offset code-object fun-index
))
2782 (offset-from-code-start (calc-offset code-object offset-from-insns-start
)))
2783 (unless (zerop (logand offset-from-code-start sb
!vm
:lowtag-mask
))
2784 (error "unaligned function entry ~S ~S" code-object fun-index
))
2785 (make-descriptor (logior (+ (logandc2 (descriptor-bits code-object
) sb
!vm
:lowtag-mask
)
2786 offset-from-code-start
)
2787 sb
!vm
:fun-pointer-lowtag
))))
2789 (defun cold-fop-fun-entry (fasl-input fun-index
)
2790 (binding* (((info type arglist name code-object
)
2791 (macrolet ((pop-stack ()
2792 '(pop-fop-stack (%fasl-input-stack fasl-input
))))
2793 (values (pop-stack) (pop-stack) (pop-stack) (pop-stack) (pop-stack))))
2794 (fn (compute-fun code-object fun-index
)))
2795 #!+(or x86 x86-64
) ; store a machine-native pointer to the function entry
2796 ;; note that the bit pattern looks like fixnum due to alignment
2797 (write-wordindexed/raw fn sb
!vm
:simple-fun-self-slot
2798 (+ (- (descriptor-bits fn
) sb
!vm
:fun-pointer-lowtag
)
2799 (ash sb
!vm
:simple-fun-code-offset sb
!vm
:word-shift
)))
2800 #!-
(or x86 x86-64
) ; store a pointer back to the function itself in 'self'
2801 (write-wordindexed fn sb
!vm
:simple-fun-self-slot fn
)
2802 (write-wordindexed fn sb
!vm
:simple-fun-name-slot name
)
2803 (flet ((coalesce (sexpr) ; a warm symbol or a cold cons tree
2804 (if (symbolp sexpr
) ; will be cold-interned automatically
2806 (let ((representation (make-equal-comparable-thing sexpr
)))
2807 (or (gethash representation
*simple-fun-metadata
*)
2808 (setf (gethash representation
*simple-fun-metadata
*)
2810 (write-wordindexed fn sb
!vm
:simple-fun-arglist-slot
(coalesce arglist
))
2811 (write-wordindexed fn sb
!vm
:simple-fun-type-slot
(coalesce type
)))
2812 (write-wordindexed fn sb
!vm
::simple-fun-info-slot info
)
2815 (let ((i (get 'fop-fun-entry
'opcode
)))
2816 (fill **fop-funs
** #'cold-fop-fun-entry
:start i
:end
(+ i
4)))
2819 (define-cold-fop (fop-symbol-tls-fixup)
2820 (let* ((symbol (pop-stack))
2822 (code-object (pop-stack)))
2823 (do-cold-fixup code-object
2824 (read-word-arg (fasl-input-stream))
2825 (ensure-symbol-tls-index symbol
)
2826 kind
))) ; and re-push code-object
2828 (define-cold-fop (fop-foreign-fixup)
2829 (let* ((kind (pop-stack))
2830 (code-object (pop-stack))
2831 (len (read-byte-arg (fasl-input-stream)))
2832 (sym (make-string len
)))
2833 (read-string-as-bytes (fasl-input-stream) sym
)
2835 (let ((offset (read-word-arg (fasl-input-stream)))
2836 (value (dyncore-note-symbol sym nil
)))
2837 (do-cold-fixup code-object offset value kind
)) ; and re-push code-object
2838 #!-
(and) (format t
"Bad non-plt fixup: ~S~S~%" sym code-object
)
2840 (let ((offset (read-word-arg (fasl-input-stream)))
2841 (value (cold-foreign-symbol-address sym
)))
2842 (do-cold-fixup code-object offset value kind
)))) ; and re-push code-object
2845 (define-cold-fop (fop-foreign-dataref-fixup)
2846 (let* ((kind (pop-stack))
2847 (code-object (pop-stack))
2848 (len (read-byte-arg (fasl-input-stream)))
2849 (sym (make-string len
)))
2850 #!-sb-dynamic-core
(declare (ignore code-object
))
2851 (read-string-as-bytes (fasl-input-stream) sym
)
2853 (let ((offset (read-word-arg (fasl-input-stream)))
2854 (value (dyncore-note-symbol sym t
)))
2855 (do-cold-fixup code-object offset value kind
)) ; and re-push code-object
2858 (maphash (lambda (k v
)
2859 (format *error-output
* "~&~S = #X~8X~%" k v
))
2860 *cold-foreign-symbol-table
*)
2861 (error "shared foreign symbol in cold load: ~S (~S)" sym kind
))))
2863 (define-cold-fop (fop-assembler-code)
2864 (let* ((length (read-word-arg (fasl-input-stream)))
2866 ;; Note: we round the number of constants up to ensure that
2867 ;; the code vector will be properly aligned.
2868 (round-up sb
!vm
:code-constants-offset
2))
2869 (des (allocate-cold-descriptor *read-only
*
2870 (+ (ash header-n-words
2873 sb
!vm
:other-pointer-lowtag
)))
2874 (write-header-word des header-n-words sb
!vm
:code-header-widetag
)
2875 (write-wordindexed des
2876 sb
!vm
:code-code-size-slot
2877 (make-fixnum-descriptor length
))
2878 (write-wordindexed des sb
!vm
:code-debug-info-slot
*nil-descriptor
*)
2880 (let* ((start (+ (descriptor-byte-offset des
)
2881 (ash header-n-words sb
!vm
:word-shift
)))
2882 (end (+ start length
)))
2883 (read-bigvec-as-sequence-or-die (descriptor-mem des
)
2889 (define-cold-fop (fop-assembler-routine)
2890 (let* ((routine (pop-stack))
2892 (offset (calc-offset des
(read-word-arg (fasl-input-stream)))))
2893 (record-cold-assembler-routine
2895 (+ (logandc2 (descriptor-bits des
) sb
!vm
:lowtag-mask
) offset
))
2898 (define-cold-fop (fop-assembler-fixup)
2899 (let* ((routine (pop-stack))
2901 (code-object (pop-stack))
2902 (offset (read-word-arg (fasl-input-stream))))
2903 (push (list routine code-object offset kind
) *cold-assembler-fixups
*)
2906 (define-cold-fop (fop-code-object-fixup)
2907 (let* ((kind (pop-stack))
2908 (code-object (pop-stack))
2909 (offset (read-word-arg (fasl-input-stream)))
2910 (value (descriptor-bits code-object
)))
2911 (do-cold-fixup code-object offset value kind
))) ; and re-push code-object
2915 (define-cold-fop (fop-layout-fixup)
2916 (let* ((obj (pop-stack))
2918 (code-object (pop-stack))
2919 (offset (read-word-arg (fasl-input-stream)))
2920 (cold-layout (or (gethash obj
*cold-layouts
*)
2921 (error "No cold-layout for ~S~%" obj
))))
2922 (do-cold-fixup code-object offset
2923 (descriptor-bits cold-layout
)
2924 kind
:immobile-object
)))
2925 (define-cold-fop (fop-immobile-obj-fixup)
2926 (let ((obj (pop-stack))
2928 (code-object (pop-stack))
2929 (offset (read-word-arg (fasl-input-stream))))
2930 (do-cold-fixup code-object offset
2931 (descriptor-bits (if (symbolp obj
) (cold-intern obj
) obj
))
2932 kind
:immobile-object
))))
2935 (define-cold-fop (fop-named-call-fixup)
2936 (let ((fdefn (cold-fdefinition-object (pop-stack)))
2938 (code-object (pop-stack))
2939 (offset (read-word-arg (fasl-input-stream))))
2940 (do-cold-fixup code-object offset
2941 (+ (descriptor-bits fdefn
)
2942 (ash sb
!vm
:fdefn-raw-addr-slot sb
!vm
:word-shift
)
2943 (- sb
!vm
:other-pointer-lowtag
))
2947 (define-cold-fop (fop-static-call-fixup)
2948 (let ((name (pop-stack))
2950 (code-object (pop-stack))
2951 (offset (read-word-arg (fasl-input-stream))))
2952 (push (list name kind code-object offset
) *cold-static-call-fixups
*)
2956 ;;;; sanity checking space layouts
2958 (defun check-spaces ()
2959 ;;; Co-opt type machinery to check for intersections...
2961 (flet ((check (start end space
)
2962 (unless (< start end
)
2963 (error "Bogus space: ~A" space
))
2964 (let ((type (specifier-type `(integer ,start
,end
))))
2965 (dolist (other types
)
2966 (unless (eq *empty-type
* (type-intersection (cdr other
) type
))
2967 (error "Space overlap: ~A with ~A" space
(car other
))))
2968 (push (cons space type
) types
))))
2969 (check sb
!vm
:read-only-space-start sb
!vm
:read-only-space-end
:read-only
)
2970 (check sb
!vm
:static-space-start sb
!vm
:static-space-end
:static
)
2972 (check sb
!vm
:default-dynamic-space-start
2973 (+ sb
!vm
:default-dynamic-space-start sb
!vm
:default-dynamic-space-size
)
2976 ;; Must be a multiple of 32 because it makes the math a nicer
2977 ;; when computing word and bit index into the 'touched' bitmap.
2978 (assert (zerop (rem sb
!vm
:immobile-fixedobj-subspace-size
2979 (* 32 sb
!vm
:immobile-card-bytes
))))
2982 (check sb
!vm
:dynamic-0-space-start sb
!vm
:dynamic-0-space-end
:dynamic-0
)
2983 (check sb
!vm
:dynamic-1-space-start sb
!vm
:dynamic-1-space-end
:dynamic-1
))
2985 (check sb
!vm
:linkage-table-space-start sb
!vm
:linkage-table-space-end
:linkage-table
))))
2987 ;;;; emitting C header file
2989 (defun tailwise-equal (string tail
)
2990 (and (>= (length string
) (length tail
))
2991 (string= string tail
:start1
(- (length string
) (length tail
)))))
2993 (defun write-boilerplate (*standard-output
*)
2996 '("This is a machine-generated file. Please do not edit it by hand."
2997 "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
2999 "This file contains low-level information about the"
3000 "internals of a particular version and configuration"
3001 "of SBCL. It is used by the C compiler to create a runtime"
3002 "support environment, an executable program in the host"
3003 "operating system's native format, which can then be used to"
3004 "load and run 'core' files, which are basically programs"
3005 "in SBCL's own format."))
3006 (format t
" *~@[ ~A~]~%" line
))
3009 (defun c-name (string &optional strip
)
3011 (substitute-if #\_
(lambda (c) (member c
'(#\-
#\
/ #\%
)))
3012 (remove-if (lambda (c) (position c strip
))
3015 (defun c-symbol-name (symbol &optional strip
)
3016 (c-name (symbol-name symbol
) strip
))
3018 (defun write-makefile-features (*standard-output
*)
3019 ;; propagating *SHEBANG-FEATURES* into the Makefiles
3020 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
3021 sb-cold
:*shebang-features
*)
3023 (format t
"LISP_FEATURE_~A=1~%" shebang-feature-name
)))
3025 (defun write-config-h (*standard-output
*)
3026 ;; propagating *SHEBANG-FEATURES* into C-level #define's
3027 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
3028 sb-cold
:*shebang-features
*)
3030 (format t
"#define LISP_FEATURE_~A~%" shebang-feature-name
))
3032 ;; and miscellaneous constants
3033 (format t
"#define SBCL_VERSION_STRING ~S~%"
3034 (sb!xc
:lisp-implementation-version
))
3035 (format t
"#define CORE_MAGIC 0x~X~%" core-magic
)
3036 (format t
"#ifndef LANGUAGE_ASSEMBLY~2%")
3037 (format t
"#define LISPOBJ(x) ((lispobj)x)~2%")
3038 (format t
"#else /* LANGUAGE_ASSEMBLY */~2%")
3039 (format t
"#define LISPOBJ(thing) thing~2%")
3040 (format t
"#endif /* LANGUAGE_ASSEMBLY */~2%")
3043 (defun write-constants-h (*standard-output
*)
3044 ;; writing entire families of named constants
3045 (let ((constants nil
))
3046 (dolist (package-name '("SB!VM"
3047 ;; We also propagate magic numbers
3048 ;; related to file format,
3049 ;; which live here instead of SB!VM.
3051 (do-external-symbols (symbol (find-package package-name
))
3052 (when (constantp symbol
)
3053 (let ((name (symbol-name symbol
)))
3054 (labels ( ;; shared machinery
3055 (record (string priority suffix
)
3058 (symbol-value symbol
)
3060 (documentation symbol
'variable
))
3062 ;; machinery for old-style CMU CL Lisp-to-C
3063 ;; arbitrary renaming, being phased out in favor of
3064 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
3066 (record-with-munged-name (prefix string priority
)
3067 (record (concatenate
3070 (delete #\-
(string-capitalize string
)))
3073 (maybe-record-with-munged-name (tail prefix priority
)
3074 (when (tailwise-equal name tail
)
3075 (record-with-munged-name prefix
3080 ;; machinery for new-style SBCL Lisp-to-C naming
3081 (record-with-translated-name (priority large
)
3082 (record (c-name name
) priority
3084 #!+(and win32 x86-64
) "LLU"
3085 #!-
(and win32 x86-64
) "LU"
3087 (maybe-record-with-translated-name (suffixes priority
&key large
)
3088 (when (some (lambda (suffix)
3089 (tailwise-equal name suffix
))
3091 (record-with-translated-name priority large
))))
3092 (maybe-record-with-translated-name '("-LOWTAG") 0)
3093 (maybe-record-with-translated-name '("-WIDETAG" "-SHIFT") 1)
3094 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
3095 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
3096 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
3097 (maybe-record-with-translated-name '("SHAREABLE+" "SHAREABLE-NONSTD+") 4)
3098 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
3099 (maybe-record-with-translated-name '("-SIZE" "-INTERRUPTS") 6)
3100 (maybe-record-with-translated-name '("-START" "-END" "-PAGE-BYTES"
3101 "-CARD-BYTES" "-GRANULARITY")
3103 (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 8)
3104 (maybe-record-with-translated-name '("-CORE-SPACE-ID") 9)
3105 (maybe-record-with-translated-name '("-CORE-SPACE-ID-FLAG") 9)
3106 (maybe-record-with-translated-name '("-GENERATION+") 10))))))
3107 ;; KLUDGE: these constants are sort of important, but there's no
3108 ;; pleasing way to inform the code above about them. So we fake
3109 ;; it for now. nikodemus on #lisp (2004-08-09) suggested simply
3110 ;; exporting every numeric constant from SB!VM; that would work,
3111 ;; but the C runtime would have to be altered to use Lisp-like names
3112 ;; rather than the munged names currently exported. --njf, 2004-08-09
3113 (dolist (c '(sb!vm
:n-word-bits sb
!vm
:n-word-bytes
3114 sb
!vm
:n-lowtag-bits sb
!vm
:lowtag-mask
3115 sb
!vm
:n-widetag-bits sb
!vm
:widetag-mask
3116 sb
!vm
:n-fixnum-tag-bits sb
!vm
:fixnum-tag-mask
3117 sb
!vm
:short-header-max-words
))
3118 (push (list (c-symbol-name c
)
3119 -
1 ; invent a new priority
3124 ;; One more symbol that doesn't fit into the code above.
3125 (let ((c 'sb
!impl
::+magic-hash-vector-value
+))
3126 (push (list (c-symbol-name c
)
3129 #!+(and win32 x86-64
) "LLU"
3130 #!-
(and win32 x86-64
) "LU"
3133 ;; And still one more
3135 (let ((c 'sb
!vm
::immediate-widetags-mask
))
3136 (push (list (c-symbol-name c
)
3138 (logior (ash 1 (ash sb
!vm
:character-widetag -
2))
3139 (ash 1 (ash sb
!vm
:single-float-widetag -
2))
3140 (ash 1 (ash sb
!vm
:unbound-marker-widetag -
2)))
3146 (lambda (const1 const2
)
3147 (if (= (second const1
) (second const2
))
3148 (if (= (third const1
) (third const2
))
3149 (string< (first const1
) (first const2
))
3150 (< (third const1
) (third const2
)))
3151 (< (second const1
) (second const2
))))))
3152 (let ((prev-priority (second (car constants
))))
3153 (dolist (const constants
)
3154 (destructuring-bind (name priority value suffix doc
) const
3155 (unless (= prev-priority priority
)
3157 (setf prev-priority priority
))
3158 (when (minusp value
)
3159 (error "stub: negative values unsupported"))
3160 (format t
"#define ~A ~A~A /* 0x~X ~@[ -- ~A ~]*/~%" name value suffix value doc
))))
3163 ;; writing information about internal errors
3164 ;; Assembly code needs only the constants for UNDEFINED_[ALIEN_]FUN_ERROR
3165 ;; but to avoid imparting that knowledge here, we'll expose all error
3166 ;; number constants except for OBJECT-NOT-<x>-ERROR ones.
3167 (loop for
(description name
) across sb
!c
:+backend-internal-errors
+
3169 when
(stringp description
)
3170 do
(format t
"#define ~A ~D~%" (c-symbol-name name
) i
))
3172 ;; I'm not really sure why this is in SB!C, since it seems
3173 ;; conceptually like something that belongs to SB!VM. In any case,
3174 ;; it's needed C-side.
3175 (format t
"#define BACKEND_PAGE_BYTES ~DLU~%" sb
!c
:*backend-page-bytes
*)
3179 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
3180 ;; platforms. If we export this from the SB!VM package, it gets
3181 ;; written out as #define trap_PseudoAtomic, which is confusing as
3182 ;; the runtime treats trap_ as the prefix for illegal instruction
3183 ;; type things. We therefore don't export it, but instead do
3185 (when (boundp 'sb
!vm
::pseudo-atomic-trap
)
3187 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
3188 sb
!vm
::pseudo-atomic-trap
)
3190 ;; possibly this is another candidate for a rename (to
3191 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
3192 ;; [possibly applicable to other platforms])
3195 (format t
"#define GC_SAFEPOINT_PAGE_ADDR ((void*)0x~XUL) /* ~:*~A */~%"
3196 sb
!vm
:gc-safepoint-page-addr
)
3198 (dolist (symbol '(sb!vm
::float-traps-byte
3199 sb
!vm
::float-exceptions-byte
3200 sb
!vm
::float-sticky-bits
3201 sb
!vm
::float-rounding-mode
))
3202 (format t
"#define ~A_POSITION ~A /* ~:*0x~X */~%"
3203 (c-symbol-name symbol
)
3204 (sb!xc
:byte-position
(symbol-value symbol
)))
3205 (format t
"#define ~A_MASK 0x~X /* ~:*~A */~%"
3206 (c-symbol-name symbol
)
3207 (sb!xc
:mask-field
(symbol-value symbol
) -
1))))
3209 (defun write-errnames-h (stream)
3210 ;; C code needs strings for describe_internal_error()
3211 (format stream
"#define INTERNAL_ERROR_NAMES ~{\\~%~S~^, ~}~2%"
3212 (map 'list
'sb
!kernel
::!c-stringify-internal-error
3213 sb
!c
:+backend-internal-errors
+))
3214 (format stream
"#define INTERNAL_ERROR_NARGS {~{~S~^, ~}}~2%"
3215 (map 'list
#'cddr sb
!c
:+backend-internal-errors
+)))
3218 (defun write-tagnames-h (out)
3220 ((pretty-name (symbol strip
)
3221 (let ((name (string-downcase symbol
)))
3222 (substitute #\Space
#\-
3223 (subseq name
0 (- (length name
) (length strip
))))))
3224 (list-sorted-tags (tail)
3225 (loop for symbol being the external-symbols of
"SB!VM"
3226 when
(and (constantp symbol
)
3227 (tailwise-equal (string symbol
) tail
)
3228 ;; FIXME: these symbols are obsolete
3230 '(sb!vm
:simple-fun-header-widetag
3231 sb
!vm
:closure-header-widetag
))))
3232 collect symbol into tags
3233 finally
(return (sort tags
#'< :key
#'symbol-value
))))
3234 (write-tags (visibility kind limit ash-count
)
3235 (format out
"~%~Aconst char *~(~A~)_names[] = {~%"
3236 visibility
(subseq kind
1))
3237 (let ((tags (list-sorted-tags kind
)))
3239 (if (eql i
(ash (or (symbol-value (first tags
)) -
1) ash-count
))
3240 (format out
" \"~A\"" (pretty-name (pop tags
) kind
))
3241 (format out
" \"unknown [~D]\"" i
))
3242 (unless (eql i
(1- limit
))
3243 (write-string "," out
))
3245 (write-line "};" out
)))
3246 (write-tags "static " "-LOWTAG" sb
!vm
:lowtag-limit
0)
3247 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
3248 ;; ending with the same 2 bits. (#b10)
3249 (write-tags "" "-WIDETAG" (ash (1+ sb
!vm
:widetag-mask
) -
2) -
2))
3250 ;; Inform print_otherptr() of all array types that it's too dumb to print
3251 (let ((array-type-bits (make-array 32 :initial-element
0)))
3253 (multiple-value-bind (ofs bit
) (floor b
8)
3254 (setf (aref array-type-bits ofs
) (ash 1 bit
)))))
3255 (dovector (saetp sb
!vm
:*specialized-array-element-type-properties
*)
3256 (unless (or (typep (sb!vm
:saetp-ctype saetp
) 'character-set-type
)
3257 (eq (sb!vm
:saetp-specifier saetp
) t
))
3258 (toggle (sb!vm
:saetp-typecode saetp
))
3259 (awhen (sb!vm
:saetp-complex-typecode saetp
) (toggle it
)))))
3261 "~%static unsigned char unprintable_array_types[32] =~% {~{~d~^,~}};~%"
3262 (coerce array-type-bits
'list
)))
3263 (dolist (prim-obj '(symbol ratio complex sb
!vm
::code simple-fun
3264 closure funcallable-instance
3265 weak-pointer fdefn sb
!vm
::value-cell
))
3266 (format out
"static char *~A_slots[] = {~%~{ \"~A: \",~} NULL~%};~%"
3267 (c-name (string-downcase prim-obj
))
3268 (mapcar (lambda (x) (c-name (string-downcase (sb!vm
:slot-name x
))))
3269 (remove-if 'sb
!vm
:slot-rest-p
3270 (sb!vm
::primitive-object-slots
3271 (find prim-obj sb
!vm
:*primitive-objects
*
3272 :key
'sb
!vm
:primitive-object-name
))))))
3275 (defun write-primitive-object (obj *standard-output
*)
3276 ;; writing primitive object layouts
3277 (format t
"#ifndef LANGUAGE_ASSEMBLY~2%")
3278 (format t
"struct ~A {~%" (c-name (string-downcase (sb!vm
:primitive-object-name obj
))))
3279 (when (sb!vm
:primitive-object-widetag obj
)
3280 (format t
" lispobj header;~%"))
3281 (dolist (slot (sb!vm
:primitive-object-slots obj
))
3282 (format t
" ~A ~A~@[[1]~];~%"
3283 (getf (sb!vm
:slot-options slot
) :c-type
"lispobj")
3284 (c-name (string-downcase (sb!vm
:slot-name slot
)))
3285 (sb!vm
:slot-rest-p slot
)))
3287 (format t
"#else /* LANGUAGE_ASSEMBLY */~2%")
3288 (format t
"/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
3289 (format t
" * so they work directly on tagged addresses. */~2%")
3290 (let ((name (sb!vm
:primitive-object-name obj
))
3291 (lowtag (or (symbol-value (sb!vm
:primitive-object-lowtag obj
))
3293 (dolist (slot (sb!vm
:primitive-object-slots obj
))
3294 (format t
"#define ~A_~A_OFFSET ~D~%"
3295 (c-symbol-name name
)
3296 (c-symbol-name (sb!vm
:slot-name slot
))
3297 (- (* (sb!vm
:slot-offset slot
) sb
!vm
:n-word-bytes
) lowtag
)))
3299 (format t
"#endif /* LANGUAGE_ASSEMBLY */~2%"))
3301 (defun write-structure-object (dd *standard-output
*)
3302 (flet ((cstring (designator) (c-name (string-downcase designator
))))
3303 (format t
"#ifndef LANGUAGE_ASSEMBLY~2%")
3304 (format t
"struct ~A {~%" (cstring (dd-name dd
)))
3305 (format t
" lispobj header; // = word_0_~%")
3306 ;; "self layout" slots are named '_layout' instead of 'layout' so that
3307 ;; classoid's expressly declared layout isn't renamed as a special-case.
3308 #!-compact-instance-header
(format t
" lispobj _layout;~%")
3309 ;; Output exactly the number of Lisp words consumed by the structure,
3310 ;; no more, no less. C code can always compute the padded length from
3311 ;; the precise length, but the other way doesn't work.
3313 (coerce (loop for i from sb
!vm
:instance-data-start below
(dd-length dd
)
3314 collect
(list (format nil
"word_~D_" (1+ i
))))
3316 (dolist (slot (dd-slots dd
))
3317 (let ((cell (aref names
(- (dsd-index slot
) sb
!vm
:instance-data-start
)))
3318 (name (cstring (dsd-name slot
))))
3319 (if (eq (dsd-raw-type slot
) t
)
3321 (rplacd cell name
))))
3322 (loop for slot across names
3323 do
(format t
" lispobj ~A;~@[ // ~A~]~%" (car slot
) (cdr slot
))))
3325 (format t
"#endif /* LANGUAGE_ASSEMBLY */~2%")))
3327 (defun write-static-symbols (stream)
3328 (dolist (symbol (cons nil
(coerce sb
!vm
:+static-symbols
+ 'list
)))
3329 ;; FIXME: It would be nice to use longer names than NIL and
3330 ;; (particularly) T in #define statements.
3331 (format stream
"#define ~A LISPOBJ(0x~X)~%"
3332 ;; FIXME: It would be nice not to need to strip anything
3333 ;; that doesn't get stripped always by C-SYMBOL-NAME.
3334 (c-symbol-name symbol
"%*.!")
3335 (if *static
* ; if we ran GENESIS
3336 ;; We actually ran GENESIS, use the real value.
3337 (descriptor-bits (cold-intern symbol
))
3338 ;; We didn't run GENESIS, so guess at the address.
3339 (+ sb
!vm
:static-space-start
3341 sb
!vm
:other-pointer-lowtag
3342 (if symbol
(sb!vm
:static-symbol-offset symbol
) 0)))))
3343 (loop for symbol in sb
!vm
::+c-callable-fdefns
+
3346 (format stream
"#define ~A_FDEFN LISPOBJ(0x~X)~%"
3347 (c-symbol-name symbol
)
3348 (if *static
* ; if we ran GENESIS
3349 ;; We actually ran GENESIS, use the real value.
3350 (descriptor-bits (cold-fdefinition-object symbol
))
3351 ;; We didn't run GENESIS, so guess at the address.
3352 (+ sb
!vm
:static-space-start
3354 sb
!vm
:other-pointer-lowtag
3355 (* (length sb
!vm
:+static-symbols
+)
3356 (sb!vm
:pad-data-block sb
!vm
:symbol-size
))
3357 (* index
(sb!vm
:pad-data-block sb
!vm
:fdefn-size
)))))))
3359 (defun write-sc-offset-coding (stream)
3360 (flet ((write-array (name bytes
)
3361 (format stream
"static struct sc_offset_byte ~A[] = {~@
3362 ~{ {~{ ~2D, ~2D ~}}~^,~%~}~@
3365 (mapcar (lambda (byte)
3366 (list (byte-size byte
) (byte-position byte
)))
3368 (format stream
"struct sc_offset_byte {
3372 (write-array "sc_offset_sc_number_bytes" sb
!c
::+sc-offset-scn-bytes
+)
3373 (write-array "sc_offset_offset_bytes" sb
!c
::+sc-offset-offset-bytes
+)))
3375 ;;;; writing map file
3377 ;;; Write a map file describing the cold load. Some of this
3378 ;;; information is subject to change due to relocating GC, but even so
3379 ;;; it can be very handy when attempting to troubleshoot the early
3380 ;;; stages of cold load.
3381 (defun write-map (*standard-output
*)
3382 (let ((*print-pretty
* nil
)
3383 (*print-case
* :upcase
))
3384 (format t
"assembler routines defined in core image:~2%")
3385 (dolist (routine (sort (copy-list *cold-assembler-routines
*) #'<
3387 (format t
"~8,'0X: ~S~%" (cdr routine
) (car routine
)))
3391 (maphash (lambda (name fdefn
&aux
(fun (cold-fdefn-fun fdefn
)))
3392 (push (list (- (descriptor-bits fdefn
) (descriptor-lowtag fdefn
))
3396 (push (list (- (descriptor-bits fun
) (descriptor-lowtag fun
))
3398 *cold-fdefn-objects
*)
3399 (format t
"~%~|~%fdefns (native pointer):
3400 ~:{~%~8,'0X: ~S~}~%" (sort fdefns
#'< :key
#'car
))
3401 (format t
"~%~|~%initially defined functions (native pointer):
3402 ~:{~%~8,'0X: ~S~}~%" (sort funs
#'< :key
#'car
))
3405 (a note about initially undefined function references: These functions
3406 are referred to by code which is installed by GENESIS, but they are not
3407 installed by GENESIS. This is not necessarily a problem; functions can
3408 be defined later, by cold init toplevel forms, or in files compiled and
3409 loaded at warm init, or elsewhere. As long as they are defined before
3410 they are called, everything should be OK. Things are also OK if the
3411 cross-compiler knew their inline definition and used that everywhere
3412 that they were called before the out-of-line definition is installed,
3413 as is fairly common for structure accessors.)
3414 initially undefined function references:~2%")
3416 (setf undefs
(sort undefs
#'string
< :key
#'fun-name-block-name
))
3417 (dolist (name undefs
)
3418 (format t
"~8,'0X: ~S~%"
3419 (descriptor-bits (gethash name
*cold-fdefn-objects
*))
3422 (format t
"~%~|~%layout names:~2%")
3423 (dolist (x (sort-cold-layouts))
3424 (let* ((des (cdr x
))
3425 (inherits (read-slot des
*host-layout-of-layout
* :inherits
)))
3426 (format t
"~8,'0X: ~S[~D]~%~10T~:S~%" (descriptor-bits des
) (car x
)
3427 (cold-layout-length des
) (listify-cold-inherits inherits
))))
3429 (format t
"~%~|~%parsed type specifiers:~2%")
3430 (mapc (lambda (cell)
3431 (format t
"~X: ~S~%" (descriptor-bits (cdr cell
)) (car cell
)))
3432 (sort (%hash-table-alist
*ctype-cache
*) #'<
3433 :key
(lambda (x) (descriptor-bits (cdr x
))))))
3436 ;;;; writing core file
3438 (defvar *core-file
*)
3439 (defvar *data-page
*)
3441 ;;; magic numbers to identify entries in a core file
3443 ;;; (In case you were wondering: No, AFAIK there's no special magic about
3444 ;;; these which requires them to be in the 38xx range. They're just
3445 ;;; arbitrary words, tested not for being in a particular range but just
3446 ;;; for equality. However, if you ever need to look at a .core file and
3447 ;;; figure out what's going on, it's slightly convenient that they're
3448 ;;; all in an easily recognizable range, and displacing the range away from
3449 ;;; zero seems likely to reduce the chance that random garbage will be
3450 ;;; misinterpreted as a .core file.)
3451 (defconstant build-id-core-entry-type-code
3860)
3452 (defconstant new-directory-core-entry-type-code
3861)
3453 (defconstant initial-fun-core-entry-type-code
3863)
3454 (defconstant page-table-core-entry-type-code
3880)
3455 (defconstant end-core-entry-type-code
3840)
3457 (declaim (ftype (function (sb!vm
:word
) sb
!vm
:word
) write-word
))
3458 (defun write-word (num)
3459 (ecase sb
!c
:*backend-byte-order
*
3461 (dotimes (i sb
!vm
:n-word-bytes
)
3462 (write-byte (ldb (byte 8 (* i
8)) num
) *core-file
*)))
3464 (dotimes (i sb
!vm
:n-word-bytes
)
3465 (write-byte (ldb (byte 8 (* (- (1- sb
!vm
:n-word-bytes
) i
) 8)) num
)
3469 (defun output-gspace (gspace)
3470 (force-output *core-file
*)
3471 (let* ((posn (file-position *core-file
*))
3472 (bytes (* (gspace-free-word-index gspace
) sb
!vm
:n-word-bytes
))
3473 (pages (ceiling bytes sb
!c
:*backend-page-bytes
*))
3474 (total-bytes (* pages sb
!c
:*backend-page-bytes
*)))
3476 (file-position *core-file
*
3477 (* sb
!c
:*backend-page-bytes
* (1+ *data-page
*)))
3479 "writing ~S byte~:P [~S page~:P] from ~S~%"
3485 ;; Note: It is assumed that the GSPACE allocation routines always
3486 ;; allocate whole pages (of size *target-page-size*) and that any
3487 ;; empty gspace between the free pointer and the end of page will
3488 ;; be zero-filled. This will always be true under Mach on machines
3489 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3491 (write-bigvec-as-sequence (gspace-data gspace
)
3495 (force-output *core-file
*)
3496 (file-position *core-file
* posn
)
3498 ;; Write part of a (new) directory entry which looks like this:
3499 ;; GSPACE IDENTIFIER
3504 (write-word (gspace-identifier gspace
))
3505 (write-word (gspace-free-word-index gspace
))
3506 (write-word *data-page
*)
3507 (multiple-value-bind (floor rem
)
3508 (floor (gspace-byte-address gspace
) sb
!c
:*backend-page-bytes
*)
3513 (incf *data-page
* pages
)))
3515 ;;; Create a core file created from the cold loaded image. (This is
3516 ;;; the "initial core file" because core files could be created later
3517 ;;; by executing SAVE-LISP in a running system, perhaps after we've
3518 ;;; added some functionality to the system.)
3519 (declaim (ftype (function (string)) write-initial-core-file
))
3520 (defun write-initial-core-file (filename)
3522 (let ((filenamestring (namestring filename
))
3525 (format t
"[building initial core file in ~S: ~%" filenamestring
)
3528 (with-open-file (*core-file
* filenamestring
3530 :element-type
'(unsigned-byte 8)
3531 :if-exists
:rename-and-delete
)
3533 ;; Write the magic number.
3534 (write-word core-magic
)
3536 ;; Write the build ID.
3537 (write-word build-id-core-entry-type-code
)
3538 (let ((build-id (with-open-file (s "output/build-id.tmp")
3540 (declare (type simple-string build-id
))
3541 (/show build-id
(length build-id
))
3542 ;; Write length of build ID record: BUILD-ID-CORE-ENTRY-TYPE-CODE
3543 ;; word, this length word, and one word for each char of BUILD-ID.
3544 (write-word (+ 2 (length build-id
)))
3545 (dovector (char build-id
)
3546 ;; (We write each character as a word in order to avoid
3547 ;; having to think about word alignment issues in the
3548 ;; sbcl-0.7.8 version of coreparse.c.)
3549 (write-word (sb!xc
:char-code char
))))
3551 ;; Write the New Directory entry header.
3552 (write-word new-directory-core-entry-type-code
)
3553 (let ((spaces (nconc (list *read-only
* *static
*)
3555 (list *immobile-fixedobj
* *immobile-varyobj
*)
3557 ;; length = (5 words/space) * N spaces + 2 for header.
3558 (write-word (+ (* (length spaces
) 5) 2))
3559 (mapc #'output-gspace spaces
))
3561 ;; Write the initial function.
3562 (write-word initial-fun-core-entry-type-code
)
3564 (let* ((cold-name (cold-intern '!cold-init
))
3566 (cold-fdefn-fun (cold-fdefinition-object cold-name
))))
3568 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
3569 (descriptor-bits initial-fun
))
3570 (write-word (descriptor-bits initial-fun
)))
3572 ;; Write the End entry.
3573 (write-word end-core-entry-type-code
)
3576 (format t
"done]~%")
3578 (/show
"leaving WRITE-INITIAL-CORE-FILE")
3581 ;;;; the actual GENESIS function
3583 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3584 ;;; and/or information about a Lisp core, therefrom.
3586 ;;; input file arguments:
3587 ;;; SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
3588 ;;; *tab* *characters* *converted* *to* *spaces*. (We push
3589 ;;; responsibility for removing tabs out to the caller it's
3590 ;;; trivial to remove them using UNIX command line tools like
3591 ;;; sed, whereas it's a headache to do it portably in Lisp because
3592 ;;; #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
3593 ;;; a core file cannot be built (but a C header file can be).
3595 ;;; output files arguments (any of which may be NIL to suppress output):
3596 ;;; CORE-FILE-NAME gets a Lisp core.
3597 ;;; C-HEADER-DIR-NAME gets the path in which to place generated headers
3598 ;;; MAP-FILE-NAME gets the name of the textual 'cold-sbcl.map' file
3599 (defun sb-cold:genesis
(&key object-file-names preload-file
3600 core-file-name c-header-dir-name map-file-name
3601 symbol-table-file-name
)
3602 (declare (ignorable symbol-table-file-name
))
3603 (declare (special core-file-name
))
3606 "~&beginning GENESIS, ~A~%"
3608 ;; Note: This output summarizing what we're doing is
3609 ;; somewhat telegraphic in style, not meant to imply that
3610 ;; we're not e.g. also creating a header file when we
3612 (format nil
"creating core ~S" core-file-name
)
3613 (format nil
"creating headers in ~S" c-header-dir-name
)))
3615 (let ((*cold-foreign-symbol-table
* (make-hash-table :test
'equal
)))
3617 #!-
(or sb-dynamic-core crossbuild-test
)
3618 (when core-file-name
3619 (if symbol-table-file-name
3620 (load-cold-foreign-symbol-table symbol-table-file-name
)
3621 (error "can't output a core file without symbol table file input")))
3623 ;; Now that we've successfully read our only input file (by
3624 ;; loading the symbol table, if any), it's a good time to ensure
3625 ;; that there'll be someplace for our output files to go when
3627 (flet ((frob (filename)
3629 (ensure-directories-exist filename
:verbose t
))))
3630 (frob core-file-name
)
3631 (frob map-file-name
))
3633 ;; (This shouldn't matter in normal use, since GENESIS normally
3634 ;; only runs once in any given Lisp image, but it could reduce
3635 ;; confusion if we ever experiment with running, tweaking, and
3636 ;; rerunning genesis interactively.)
3637 (do-all-symbols (sym)
3638 (remprop sym
'cold-intern-info
))
3642 (let* ((*foreign-symbol-placeholder-value
* (if core-file-name nil
0))
3643 (*load-time-value-counter
* 0)
3644 (*cold-fdefn-objects
* (make-hash-table :test
'equal
))
3645 (*cold-symbols
* (make-hash-table :test
'eql
)) ; integer keys
3646 (*cold-package-symbols
* (make-hash-table :test
'equal
)) ; string keys
3647 (*read-only
* (make-gspace :read-only
3648 read-only-core-space-id
3649 sb
!vm
:read-only-space-start
))
3650 (*static
* (make-gspace :static
3651 static-core-space-id
3652 sb
!vm
:static-space-start
))
3654 (*immobile-fixedobj
* (make-gspace :immobile-fixedobj
3655 immobile-fixedobj-core-space-id
3656 sb
!vm
:immobile-space-start
))
3658 (*immobile-varyobj
* (make-gspace :immobile-varyobj
3659 immobile-varyobj-core-space-id
3660 (+ sb
!vm
:immobile-space-start
3661 sb
!vm
:immobile-fixedobj-subspace-size
)))
3662 (*dynamic
* (make-gspace :dynamic
3663 dynamic-core-space-id
3664 #!+gencgc sb
!vm
:default-dynamic-space-start
3665 #!-gencgc sb
!vm
:dynamic-0-space-start
))
3666 (*nil-descriptor
* (make-nil-descriptor))
3667 (*simple-vector-0-descriptor
* (vector-in-core nil
))
3668 (*known-structure-classoids
* nil
)
3669 (*classoid-cells
* (make-hash-table :test
'eq
))
3670 (*ctype-cache
* (make-hash-table :test
'equal
))
3671 (*cold-layouts
* (make-hash-table :test
'eq
)) ; symbol -> cold-layout
3672 (*cold-layout-names
* (make-hash-table :test
'eql
)) ; addr -> symbol
3673 (*!cold-defconstants
* nil
)
3674 (*!cold-defuns
* nil
)
3675 ;; '*COLD-METHODS* is never seen in the target, so does not need
3676 ;; to adhere to the #\! convention for automatic uninterning.
3677 (*cold-methods
* nil
)
3678 (*!cold-toplevels
* nil
)
3679 (*current-debug-sources
* *nil-descriptor
*)
3680 *cold-static-call-fixups
*
3681 *cold-assembler-fixups
*
3682 *cold-assembler-routines
*
3683 (*code-fixup-notes
* (make-hash-table))
3684 (*deferred-known-fun-refs
* nil
))
3686 ;; If we're given a preload file, it contains tramps and whatnot
3687 ;; that must be loaded before we create any FDEFNs. It can in
3688 ;; theory be loaded any time between binding
3689 ;; *COLD-ASSEMBLER-ROUTINES* above and calling
3690 ;; INITIALIZE-STATIC-SPACE below.
3692 (cold-load preload-file
))
3694 ;; Prepare for cold load.
3695 (initialize-layouts)
3696 (initialize-packages)
3697 (initialize-static-space)
3699 ;; Initialize the *COLD-SYMBOLS* system with the information
3700 ;; from common-lisp-exports.lisp-expr.
3701 ;; Packages whose names match SB!THING were set up on the host according
3702 ;; to "package-data-list.lisp-expr" which expresses the desired target
3703 ;; package configuration, so we can just mirror the host into the target.
3704 ;; But by waiting to observe calls to COLD-INTERN that occur during the
3705 ;; loading of the cross-compiler's outputs, it is possible to rid the
3706 ;; target of accidental leftover symbols, not that it wouldn't also be
3707 ;; a good idea to clean up package-data-list once in a while.
3708 (dolist (exported-name
3709 (sb-cold:read-from-file
"common-lisp-exports.lisp-expr"))
3710 (cold-intern (intern exported-name
*cl-package
*) :access
:external
))
3712 ;; Create SB!KERNEL::*TYPE-CLASSES* as an array of NIL
3713 (cold-set (cold-intern 'sb
!kernel
::*type-classes
*)
3714 (vector-in-core (make-list (length sb
!kernel
::*type-classes
*))))
3717 (dolist (file-name object-file-names
)
3718 (cold-load file-name
))
3720 (when *known-structure-classoids
*
3721 (let ((dd-layout (find-layout 'defstruct-description
)))
3722 (dolist (defstruct-args *known-structure-classoids
*)
3723 (let* ((dd (first defstruct-args
))
3724 (name (warm-symbol (read-slot dd dd-layout
:name
)))
3725 (layout (gethash name
*cold-layouts
*)))
3727 (write-slots layout
*host-layout-of-layout
* :info dd
))))
3728 (format t
"~&; SB!Loader: (~D~@{+~D~}) structs/consts/funs/methods/other~%"
3729 (length *known-structure-classoids
*)
3730 (length *!cold-defconstants
*)
3731 (length *!cold-defuns
*)
3732 (reduce #'+ *cold-methods
* :key
(lambda (x) (length (cdr x
))))
3733 (length *!cold-toplevels
*)))
3735 (dolist (symbol '(*!cold-defconstants
* *!cold-defuns
* *!cold-toplevels
*))
3736 (cold-set symbol
(list-to-core (nreverse (symbol-value symbol
))))
3737 (makunbound symbol
)) ; so no further PUSHes can be done
3740 'sb
!pcl
::*!trivial-methods
*
3742 (loop for
(gf-name . methods
) in
*cold-methods
*
3745 (cold-intern gf-name
)
3747 (loop for
(class qual lambda-list fun source-loc
)
3748 ;; Methods must be sorted because we invoke
3749 ;; only the first applicable one.
3750 in
(stable-sort methods
#'> ; highest depthoid first
3751 :key
(lambda (method)
3752 (class-depthoid (car method
))))
3754 (cold-list (cold-intern
3755 (and (null qual
) (predicate-for-specializer class
)))
3759 lambda-list source-loc
)))))))
3761 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3762 (resolve-deferred-known-funs)
3763 (resolve-assembler-fixups)
3764 (foreign-symbols-to-core)
3765 #!+(or x86 immobile-space
)
3766 (dolist (pair (sort (%hash-table-alist
*code-fixup-notes
*) #'< :key
#'car
))
3767 (write-wordindexed (make-random-descriptor (car pair
))
3768 sb
!vm
::code-fixups-slot
3769 #!+x86
(ub32-vector-in-core (cdr pair
))
3770 #!+x86-64
(number-to-core
3771 (sb!c
::pack-code-fixup-locs
(cdr pair
)))))
3773 (/show
"back from FINISH-SYMBOLS")
3774 (finalize-load-time-value-noise)
3776 ;; Tell the target Lisp how much stuff we've allocated.
3777 ;; ALLOCATE-COLD-DESCRIPTOR is a weird trick to locate a space's end,
3778 ;; and it doesn't work on immobile space.
3779 (cold-set 'sb
!vm
:*read-only-space-free-pointer
*
3780 (allocate-cold-descriptor *read-only
*
3782 sb
!vm
:even-fixnum-lowtag
))
3783 (cold-set 'sb
!vm
:*static-space-free-pointer
*
3784 (allocate-cold-descriptor *static
*
3786 sb
!vm
:even-fixnum-lowtag
))
3789 (cold-set 'sb
!vm
:*immobile-fixedobj-free-pointer
*
3790 (make-random-descriptor
3791 (ash (+ (gspace-word-address *immobile-fixedobj
*)
3792 (gspace-free-word-index *immobile-fixedobj
*))
3794 ;; The upper bound of the varyobj subspace is delimited by
3795 ;; a structure with no layout and no slots.
3796 ;; This is necessary because 'coreparse' does not have the actual
3797 ;; value of the free pointer, but the space must not contain any
3798 ;; objects that look like conses (due to the tail of 0 words).
3799 (let ((des (allocate-object *immobile-varyobj
* 1 ; 1 word in total
3800 sb
!vm
:instance-pointer-lowtag nil
)))
3801 (write-wordindexed/raw des
0 sb
!vm
:instance-widetag
)
3802 (write-wordindexed/raw des sb
!vm
:instance-slots-offset
0))
3803 (cold-set 'sb
!vm
:*immobile-space-free-pointer
*
3804 (make-random-descriptor
3805 (ash (+ (gspace-word-address *immobile-varyobj
*)
3806 (gspace-free-word-index *immobile-varyobj
*))
3807 sb
!vm
:word-shift
))))
3809 (/show
"done setting free pointers")
3811 ;; Write results to files.
3813 (with-open-file (stream map-file-name
:direction
:output
:if-exists
:supersede
)
3814 (write-map stream
)))
3815 (let ((filename (format nil
"~A/Makefile.features" c-header-dir-name
)))
3816 (ensure-directories-exist filename
)
3817 (with-open-file (stream filename
:direction
:output
:if-exists
:supersede
)
3818 (write-makefile-features stream
)))
3820 (macrolet ((out-to (name &body body
) ; write boilerplate and inclusion guard
3821 `(with-open-file (stream (format nil
"~A/~A.h" c-header-dir-name
,name
)
3822 :direction
:output
:if-exists
:supersede
)
3823 (write-boilerplate stream
)
3825 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~:*~A~%"
3826 (c-name (string-upcase ,name
)))
3828 (format stream
"#endif~%"))))
3829 (out-to "config" (write-config-h stream
))
3830 (out-to "constants" (write-constants-h stream
))
3831 (out-to "errnames" (write-errnames-h stream
))
3832 (out-to "gc-tables" (sb!vm
::write-gc-tables stream
))
3834 (out-to "tagnames" (write-tagnames-h stream
))
3835 (let ((structs (sort (copy-list sb
!vm
:*primitive-objects
*) #'string
<
3836 :key
#'sb
!vm
:primitive-object-name
)))
3837 (dolist (obj structs
)
3838 (out-to (string-downcase (sb!vm
:primitive-object-name obj
))
3839 (write-primitive-object obj stream
)))
3840 (out-to "primitive-objects"
3841 (dolist (obj structs
)
3842 (format stream
"~&#include \"~A.h\"~%"
3843 (string-downcase (sb!vm
:primitive-object-name obj
))))))
3844 (dolist (class '(classoid hash-table layout package
3845 sb
!c
::compiled-debug-info sb
!c
::compiled-debug-fun
))
3846 (out-to (string-downcase class
)
3847 (write-structure-object (layout-info (find-layout class
)) stream
)))
3848 (out-to "static-symbols" (write-static-symbols stream
))
3849 (out-to "sc-offset" (write-sc-offset-coding stream
)))
3851 (when core-file-name
3852 (write-initial-core-file core-file-name
)))))
3854 ;;; Invert the action of HOST-CONSTANT-TO-CORE. If STRICTP is given as NIL,
3855 ;;; then we can produce a host object even if it is not a faithful rendition.
3856 (defun host-object-from-core (descriptor &optional
(strictp t
))
3857 (named-let recurse
((x descriptor
))
3859 (return-from recurse nil
))
3860 (when (eq (descriptor-gspace x
) :load-time-value
)
3861 (error "Can't warm a deferred LTV placeholder"))
3862 (when (is-fixnum-lowtag (descriptor-lowtag x
))
3863 (return-from recurse
(descriptor-fixnum x
)))
3864 (ecase (descriptor-lowtag x
)
3865 (#.sb
!vm
:instance-pointer-lowtag
3866 (if strictp
(error "Can't invert INSTANCE type") "#<instance>"))
3867 (#.sb
!vm
:list-pointer-lowtag
3868 (cons (recurse (cold-car x
)) (recurse (cold-cdr x
))))
3869 (#.sb
!vm
:fun-pointer-lowtag
3871 (error "Can't map cold-fun -> warm-fun")
3872 (let ((name (read-wordindexed x sb
!vm
:simple-fun-name-slot
)))
3873 `(function ,(recurse name
)))))
3874 (#.sb
!vm
:other-pointer-lowtag
3875 (let ((widetag (logand (descriptor-bits (read-memory x
))
3876 sb
!vm
:widetag-mask
)))
3878 (#.sb
!vm
:symbol-widetag
3881 (or (gethash (descriptor-bits x
) *cold-symbols
*) ; first try
3883 (recurse (read-wordindexed x sb
!vm
:symbol-name-slot
))))))
3884 (#.sb
!vm
:simple-base-string-widetag
(base-string-from-core x
))
3885 (#.sb
!vm
:simple-vector-widetag
(vector-from-core x
#'recurse
))
3886 (#.sb
!vm
:bignum-widetag
(bignum-from-core x
))))))))