Dump ctypes such as STRING, LIST, RATIO as constants.
[sbcl.git] / src / compiler / generic / genesis.lisp
blob5a6e11f69aece8a472d0fe834ac816a58b06a6f4
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.
5 ;;;;
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
15 ;;;; top level forms.
16 ;;;;
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.
22 ;;;;
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
43 ;;;; Common Lisp
44 ;;;;
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+
52 (expt 2 16)))
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
56 (deftype smallvec ()
57 `(simple-array (unsigned-byte 8) (,+smallvec-length+)))
59 (defun make-smallvec ()
60 (make-array +smallvec-length+ :element-type '(unsigned-byte 8)
61 :initial-element 0))
63 ;;; a big vector, implemented as a vector of SMALLVECs
64 ;;;
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
77 (defstruct bigvec
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+)
84 (aref (the smallvec
85 (svref (bigvec-outer-vector bigvec) outer-index))
86 inner-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))
92 inner-index)
93 new-value)))
95 ;;; analogous to LENGTH, but for a BIGVEC
96 ;;;
97 ;;; the length of BIGVEC, measured in the number of BVREFable bytes it
98 ;;; can hold
99 (defun bvlength (bigvec)
100 (* (length (bigvec-outer-vector bigvec))
101 +smallvec-length+))
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)
109 stream))
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 (dotimes (i length-old-outer-vector)
126 (setf (svref new-outer-vector i)
127 (svref old-outer-vector i)))
128 (loop for i from length-old-outer-vector below (length new-outer-vector) do
129 (setf (svref new-outer-vector i)
130 (make-smallvec)))
131 (setf (bigvec-outer-vector bigvec)
132 new-outer-vector))
133 bigvec)
135 ;;;; looking up bytes and multi-byte values in a BIGVEC (considering
136 ;;;; it as an image of machine memory on the cross-compilation target)
138 ;;; BVREF-32 and friends. These are like SAP-REF-n, except that
139 ;;; instead of a SAP we use a BIGVEC.
140 (macrolet ((make-bvref-n
142 (let* ((name (intern (format nil "BVREF-~A" n)))
143 (number-octets (/ n 8))
144 (ash-list-le
145 (loop for i from 0 to (1- number-octets)
146 collect `(ash (bvref bigvec (+ byte-index ,i))
147 ,(* i 8))))
148 (ash-list-be
149 (loop for i from 0 to (1- number-octets)
150 collect `(ash (bvref bigvec
151 (+ byte-index
152 ,(- number-octets 1 i)))
153 ,(* i 8))))
154 (setf-list-le
155 (loop for i from 0 to (1- number-octets)
156 append
157 `((bvref bigvec (+ byte-index ,i))
158 (ldb (byte 8 ,(* i 8)) new-value))))
159 (setf-list-be
160 (loop for i from 0 to (1- number-octets)
161 append
162 `((bvref bigvec (+ byte-index ,i))
163 (ldb (byte 8 ,(- n 8 (* i 8))) new-value)))))
164 `(progn
165 (defun ,name (bigvec byte-index)
166 (logior ,@(ecase sb!c:*backend-byte-order*
167 (:little-endian ash-list-le)
168 (:big-endian ash-list-be))))
169 (defun (setf ,name) (new-value bigvec byte-index)
170 (setf ,@(ecase sb!c:*backend-byte-order*
171 (:little-endian setf-list-le)
172 (:big-endian setf-list-be))))))))
173 (make-bvref-n 8)
174 (make-bvref-n 16)
175 (make-bvref-n 32)
176 (make-bvref-n 64))
178 ;; lispobj-sized word, whatever that may be
179 ;; hopefully nobody ever wants a 128-bit SBCL...
180 #!+64-bit
181 (progn
182 (defun bvref-word (bytes index)
183 (bvref-64 bytes index))
184 (defun (setf bvref-word) (new-val bytes index)
185 (setf (bvref-64 bytes index) new-val)))
187 #!-64-bit
188 (progn
189 (defun bvref-word (bytes index)
190 (bvref-32 bytes index))
191 (defun (setf bvref-word) (new-val bytes index)
192 (setf (bvref-32 bytes index) new-val)))
195 ;;;; representation of spaces in the core
197 ;;; If there is more than one dynamic space in memory (i.e., if a
198 ;;; copying GC is in use), then only the active dynamic space gets
199 ;;; dumped to core.
200 (defvar *dynamic*)
201 (defconstant dynamic-core-space-id 1)
203 (defvar *static*)
204 (defconstant static-core-space-id 2)
206 (defvar *read-only*)
207 (defconstant read-only-core-space-id 3)
209 (defconstant max-core-space-id 3)
210 (defconstant deflated-core-space-id-flag 4)
212 ;; This is somewhat arbitrary as there is no concept of the the
213 ;; number of bits in the "low" part of a descriptor any more.
214 (defconstant target-space-alignment (ash 1 16)
215 "the alignment requirement for spaces in the target.")
217 ;;; a GENESIS-time representation of a memory space (e.g. read-only
218 ;;; space, dynamic space, or static space)
219 (defstruct (gspace (:constructor %make-gspace)
220 (:copier nil))
221 ;; name and identifier for this GSPACE
222 (name (missing-arg) :type symbol :read-only t)
223 (identifier (missing-arg) :type fixnum :read-only t)
224 ;; the word address where the data will be loaded
225 (word-address (missing-arg) :type unsigned-byte :read-only t)
226 ;; the data themselves. (Note that in CMU CL this was a pair of
227 ;; fields SAP and WORDS-ALLOCATED, but that wasn't very portable.)
228 ;; (And then in SBCL this was a VECTOR, but turned out to be
229 ;; unportable too, since ANSI doesn't think that arrays longer than
230 ;; 1024 (!) should needed by portable CL code...)
231 (bytes (make-bigvec) :read-only t)
232 ;; the index of the next unwritten word (i.e. chunk of
233 ;; SB!VM:N-WORD-BYTES bytes) in BYTES, or equivalently the number of
234 ;; words actually written in BYTES. In order to convert to an actual
235 ;; index into BYTES, thus must be multiplied by SB!VM:N-WORD-BYTES.
236 (free-word-index 0))
238 (defun gspace-byte-address (gspace)
239 (ash (gspace-word-address gspace) sb!vm:word-shift))
241 (def!method print-object ((gspace gspace) stream)
242 (print-unreadable-object (gspace stream :type t)
243 (format stream "~S" (gspace-name gspace))))
245 (defun make-gspace (name identifier byte-address)
246 (unless (zerop (rem byte-address target-space-alignment))
247 (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
248 byte-address
249 target-space-alignment))
250 (%make-gspace :name name
251 :identifier identifier
252 :word-address (ash byte-address (- sb!vm:word-shift))))
254 ;;;; representation of descriptors
256 (defun is-fixnum-lowtag (lowtag)
257 (zerop (logand lowtag sb!vm:fixnum-tag-mask)))
259 (defun is-other-immediate-lowtag (lowtag)
260 ;; The other-immediate lowtags are similar to the fixnum lowtags, in
261 ;; that they have an "effective length" that is shorter than is used
262 ;; for the pointer lowtags. Unlike the fixnum lowtags, however, the
263 ;; other-immediate lowtags are always effectively two bits wide.
264 (= (logand lowtag 3) sb!vm:other-immediate-0-lowtag))
266 (defstruct (descriptor
267 (:constructor make-descriptor (bits &optional gspace word-offset))
268 (:copier nil))
269 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
270 (gspace nil :type (or gspace (eql :load-time-value) null))
271 ;; the offset in words from the start of GSPACE, or NIL if not set yet
272 (word-offset nil :type (or sb!vm:word null))
273 (bits 0 :read-only t :type (unsigned-byte #.sb!vm:n-machine-word-bits)))
275 (declaim (inline descriptor=))
276 (defun descriptor= (a b) (eql (descriptor-bits a) (descriptor-bits b)))
278 ;; FIXME: most uses of MAKE-RANDOM-DESCRIPTOR are abuses for writing a raw word
279 ;; into target memory as if it were a descriptor, because there is no variant
280 ;; of WRITE-WORDINDEXED taking a non-descriptor value.
281 ;; As an intermediary step, perhaps this should be renamed to MAKE-RAW-BITS.
282 (defun make-random-descriptor (bits)
283 (make-descriptor (logand bits sb!ext:most-positive-word)))
285 (def!method print-object ((des descriptor) stream)
286 (let ((lowtag (descriptor-lowtag des)))
287 (print-unreadable-object (des stream :type t)
288 (cond ((eq (descriptor-gspace des) :load-time-value)
289 (format stream "for LTV ~D" (descriptor-word-offset des)))
290 ((is-fixnum-lowtag lowtag)
291 (format stream "for fixnum: ~W" (descriptor-fixnum des)))
292 ((is-other-immediate-lowtag lowtag)
293 (format stream
294 "for other immediate: #X~X, type #b~8,'0B"
295 (ash (descriptor-bits des) (- sb!vm:n-widetag-bits))
296 (logand (descriptor-bits des) sb!vm:widetag-mask)))
298 (format stream
299 "for pointer: #X~X, lowtag #b~v,'0B, ~A"
300 (logandc2 (descriptor-bits des) sb!vm:lowtag-mask)
301 sb!vm:n-lowtag-bits lowtag
302 (let ((gspace (descriptor-gspace des)))
303 (if gspace
304 (gspace-name gspace)
305 "unknown"))))))))
307 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
308 ;;; free word index is boosted as necessary, and if additional memory
309 ;;; is needed, we grow the GSPACE. The descriptor returned is a
310 ;;; pointer of type LOWTAG.
311 (defun allocate-cold-descriptor (gspace length lowtag)
312 (let* ((bytes (round-up length (ash 1 sb!vm:n-lowtag-bits)))
313 (old-free-word-index (gspace-free-word-index gspace))
314 (new-free-word-index (+ old-free-word-index
315 (ash bytes (- sb!vm:word-shift)))))
316 ;; Grow GSPACE as necessary until it's big enough to handle
317 ;; NEW-FREE-WORD-INDEX.
318 (do ()
319 ((>= (bvlength (gspace-bytes gspace))
320 (* new-free-word-index sb!vm:n-word-bytes)))
321 (expand-bigvec (gspace-bytes gspace)))
322 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
323 (setf (gspace-free-word-index gspace) new-free-word-index)
324 (let ((ptr (+ (gspace-word-address gspace) old-free-word-index)))
325 (make-descriptor (logior (ash ptr sb!vm:word-shift) lowtag)
326 gspace
327 old-free-word-index))))
329 (defun descriptor-lowtag (des)
330 "the lowtag bits for DES"
331 (logand (descriptor-bits des) sb!vm:lowtag-mask))
333 (defun descriptor-fixnum (des)
334 (let ((bits (descriptor-bits des)))
335 (if (logbitp (1- sb!vm:n-word-bits) bits)
336 ;; KLUDGE: The (- SB!VM:N-WORD-BITS 2) term here looks right to
337 ;; me, and it works, but in CMU CL it was (1- SB!VM:N-WORD-BITS),
338 ;; and although that doesn't make sense for me, or work for me,
339 ;; it's hard to see how it could have been wrong, since CMU CL
340 ;; genesis worked. It would be nice to understand how this came
341 ;; to be.. -- WHN 19990901
342 (logior (ash bits (- sb!vm:n-fixnum-tag-bits))
343 (ash -1 (1+ sb!vm:n-positive-fixnum-bits)))
344 (ash bits (- sb!vm:n-fixnum-tag-bits)))))
346 (defun descriptor-word-sized-integer (des)
347 ;; Extract an (unsigned-byte 32), from either its fixnum or bignum
348 ;; representation.
349 (let ((lowtag (descriptor-lowtag des)))
350 (if (is-fixnum-lowtag lowtag)
351 (make-random-descriptor (descriptor-fixnum des))
352 (read-wordindexed des 1))))
354 ;;; common idioms
355 (defun descriptor-bytes (des)
356 (gspace-bytes (descriptor-intuit-gspace des)))
357 (defun descriptor-byte-offset (des)
358 (ash (descriptor-word-offset des) sb!vm:word-shift))
360 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
361 ;;; figure out a GSPACE which corresponds to DES, set it into
362 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
363 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
364 (declaim (ftype (function (descriptor) gspace) descriptor-intuit-gspace))
365 (defun descriptor-intuit-gspace (des)
366 (or (descriptor-gspace des)
368 ;; gspace wasn't set, now we have to search for it.
369 (let* ((lowtag (descriptor-lowtag des))
370 (abs-word-addr (ash (- (descriptor-bits des) lowtag)
371 (- sb!vm:word-shift))))
373 ;; Non-pointer objects don't have a gspace.
374 (unless (or (eql lowtag sb!vm:fun-pointer-lowtag)
375 (eql lowtag sb!vm:instance-pointer-lowtag)
376 (eql lowtag sb!vm:list-pointer-lowtag)
377 (eql lowtag sb!vm:other-pointer-lowtag))
378 (error "don't even know how to look for a GSPACE for ~S" des))
380 (dolist (gspace (list *dynamic* *static* *read-only*)
381 (error "couldn't find a GSPACE for ~S" des))
382 ;; Bounds-check the descriptor against the allocated area
383 ;; within each gspace.
384 (when (and (<= (gspace-word-address gspace)
385 abs-word-addr
386 (+ (gspace-word-address gspace)
387 (gspace-free-word-index gspace))))
388 ;; Update the descriptor with the correct gspace and the
389 ;; offset within the gspace and return the gspace.
390 (setf (descriptor-word-offset des)
391 (- abs-word-addr (gspace-word-address gspace)))
392 (return (setf (descriptor-gspace des) gspace)))))))
394 (defun %fixnum-descriptor-if-possible (num)
395 (and (typep num '(signed-byte #.sb!vm:n-fixnum-bits))
396 (make-random-descriptor (ash num sb!vm:n-fixnum-tag-bits))))
398 (defun make-fixnum-descriptor (num)
399 (or (%fixnum-descriptor-if-possible num)
400 (error "~W is too big for a fixnum." num)))
402 (defun make-other-immediate-descriptor (data type)
403 (make-descriptor (logior (ash data sb!vm:n-widetag-bits) type)))
405 (defun make-character-descriptor (data)
406 (make-other-immediate-descriptor data sb!vm:character-widetag))
408 (defun descriptor-beyond (des offset lowtag)
409 ;; OFFSET is in bytes and relative to the descriptor as a native pointer,
410 ;; not the tagged address. Then we slap on a new lowtag.
411 (make-descriptor
412 (logior (+ offset (logandc2 (descriptor-bits des) sb!vm:lowtag-mask))
413 lowtag)))
415 ;;;; miscellaneous variables and other noise
417 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
418 ;;; undefined foreign symbols are to be treated as an error.
419 ;;; (In the first pass of GENESIS, needed to create a header file before
420 ;;; the C runtime can be built, various foreign symbols will necessarily
421 ;;; be undefined, but we don't need actual values for them anyway, and
422 ;;; we can just use 0 or some other placeholder. In the second pass of
423 ;;; GENESIS, all foreign symbols should be defined, so any undefined
424 ;;; foreign symbol is a problem.)
426 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
427 ;;; never tries to look up foreign symbols in the first place unless
428 ;;; it's actually creating a core file (as in the second pass) instead
429 ;;; of using this hack to allow it to go through the motions without
430 ;;; causing an error. -- WHN 20000825
431 (defvar *foreign-symbol-placeholder-value*)
433 ;;; a handle on the trap object
434 (defvar *unbound-marker*)
435 ;; was: (make-other-immediate-descriptor 0 sb!vm:unbound-marker-widetag)
437 ;;; a handle on the NIL object
438 (defvar *nil-descriptor*)
440 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
441 ;;; when the target Lisp starts up
443 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
444 ;;; loadtime value, represented by (CONS KEYWORD ..).
445 (declaim (special *!cold-toplevels* *!cold-setf-macros*
446 *!cold-defconstants* *!cold-defuns*))
448 ;;; the head of a list of DEBUG-SOURCEs which need to be patched when
449 ;;; the cold core starts up
450 (defvar *current-debug-sources*)
452 ;;; foreign symbol references
453 (defparameter *cold-foreign-undefined-symbols* nil)
455 ;;;; miscellaneous stuff to read and write the core memory
457 ;;; FIXME: should be DEFINE-MODIFY-MACRO
458 (defmacro cold-push (thing list) ; for making a target list held in a host symbol
459 "Push THING onto the given cold-load LIST."
460 `(setq ,list (cold-cons ,thing ,list)))
462 ;; Like above, but the list is held in the target's image of the host symbol,
463 ;; not the host's value of the symbol.
464 (defun cold-target-push (cold-thing host-symbol)
465 (cold-set host-symbol (cold-cons cold-thing (cold-symbol-value host-symbol))))
467 (declaim (ftype (function (descriptor sb!vm:word) descriptor) read-wordindexed))
468 (macrolet ((read-bits ()
469 `(bvref-word (descriptor-bytes address)
470 (ash (+ index (descriptor-word-offset address))
471 sb!vm:word-shift))))
472 (defun read-bits-wordindexed (address index)
473 (read-bits))
474 (defun read-wordindexed (address index)
475 "Return the value which is displaced by INDEX words from ADDRESS."
476 (make-random-descriptor (read-bits))))
478 (declaim (ftype (function (descriptor) descriptor) read-memory))
479 (defun read-memory (address)
480 "Return the value at ADDRESS."
481 (read-wordindexed address 0))
483 (declaim (ftype (function (descriptor
484 (integer #.(- sb!vm:list-pointer-lowtag)
485 #.sb!ext:most-positive-word)
486 descriptor)
487 (values))
488 note-load-time-value-reference))
489 (defun note-load-time-value-reference (address offset marker)
490 (push (cold-list (cold-intern :load-time-value-fixup)
491 address
492 (number-to-core offset)
493 (number-to-core (descriptor-word-offset marker)))
494 *!cold-toplevels*)
495 (values))
497 (declaim (ftype (function (descriptor sb!vm:word (or symbol descriptor))) write-wordindexed))
498 (defun write-wordindexed (address index value)
499 "Write VALUE displaced INDEX words from ADDRESS."
500 ;; If we're passed a symbol as a value then it needs to be interned.
501 (let ((value (cond ((symbolp value) (cold-intern value))
502 (t value))))
503 (if (eql (descriptor-gspace value) :load-time-value)
504 (note-load-time-value-reference address
505 (- (ash index sb!vm:word-shift)
506 (logand (descriptor-bits address)
507 sb!vm:lowtag-mask))
508 value)
509 (let* ((bytes (descriptor-bytes address))
510 (byte-index (ash (+ index (descriptor-word-offset address))
511 sb!vm:word-shift)))
512 (setf (bvref-word bytes byte-index) (descriptor-bits value))))))
514 (declaim (ftype (function (descriptor (or symbol descriptor))) write-memory))
515 (defun write-memory (address value)
516 "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
517 (write-wordindexed address 0 value))
519 ;;;; allocating images of primitive objects in the cold core
521 (defun write-header-word (des header-data widetag)
522 (write-memory des (make-other-immediate-descriptor header-data widetag)))
524 ;;; There are three kinds of blocks of memory in the type system:
525 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
526 ;;; header as all slots are descriptors.
527 ;;; * Unboxed objects (bignums): There is a single header word that contains
528 ;;; the length.
529 ;;; * Vector objects: There is a header word with the type, then a word for
530 ;;; the length, then the data.
531 (defun allocate-object (gspace length lowtag)
532 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
533 pointing to them."
534 (allocate-cold-descriptor gspace (ash length sb!vm:word-shift) lowtag))
535 (defun allocate-header+object (gspace length widetag)
536 "Allocate LENGTH words plus a header word in GSPACE and
537 return an ``other-pointer'' descriptor to them. Initialize the header word
538 with the resultant length and WIDETAG."
539 (let ((des (allocate-cold-descriptor gspace
540 (ash (1+ length) sb!vm:word-shift)
541 sb!vm:other-pointer-lowtag)))
542 (write-header-word des length widetag)
543 des))
544 (defun allocate-vector-object (gspace element-bits length widetag)
545 "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
546 GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
547 header word with WIDETAG and the length slot with LENGTH."
548 ;; ALLOCATE-COLD-DESCRIPTOR will take any rational number of bytes
549 ;; and round up to a double-word. This doesn't need to use CEILING.
550 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
551 (des (allocate-cold-descriptor gspace
552 (+ bytes (* 2 sb!vm:n-word-bytes))
553 sb!vm:other-pointer-lowtag)))
554 (write-header-word des 0 widetag)
555 (write-wordindexed des
556 sb!vm:vector-length-slot
557 (make-fixnum-descriptor length))
558 des))
560 ;;; the hosts's representation of LAYOUT-of-LAYOUT
561 (eval-when (:compile-toplevel :load-toplevel :execute)
562 (defvar *host-layout-of-layout* (find-layout 'layout)))
564 (defun cold-layout-length (layout)
565 (descriptor-fixnum (read-slot layout *host-layout-of-layout* :length)))
567 ;; Make a structure and set the header word and layout.
568 ;; LAYOUT-LENGTH is as returned by the like-named function.
569 (defun allocate-struct
570 (gspace layout &optional (layout-length (cold-layout-length layout)))
571 ;; The math in here is best illustrated by two examples:
572 ;; even: size 4 => request to allocate 5 => rounds up to 6, logior => 5
573 ;; odd : size 5 => request to allocate 6 => no rounding up, logior => 5
574 ;; In each case, the length of the memory block is even.
575 ;; ALLOCATE-OBJECT performs the rounding. It must be supplied
576 ;; the number of words minimally needed, counting the header itself.
577 ;; The number written into the header (%INSTANCE-LENGTH) is always odd.
578 (let ((des (allocate-object gspace (1+ layout-length)
579 sb!vm:instance-pointer-lowtag)))
580 (write-header-word des (logior layout-length 1)
581 sb!vm:instance-header-widetag)
582 (write-wordindexed des sb!vm:instance-slots-offset layout)
583 des))
585 ;;;; copying simple objects into the cold core
587 (defun base-string-to-core (string &optional (gspace *dynamic*))
588 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
589 core and return a descriptor to it."
590 ;; (Remember that the system convention for storage of strings leaves an
591 ;; extra null byte at the end to aid in call-out to C.)
592 (let* ((length (length string))
593 (des (allocate-vector-object gspace
594 sb!vm:n-byte-bits
595 (1+ length)
596 sb!vm:simple-base-string-widetag))
597 (bytes (gspace-bytes gspace))
598 (offset (+ (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
599 (descriptor-byte-offset des))))
600 (write-wordindexed des
601 sb!vm:vector-length-slot
602 (make-fixnum-descriptor length))
603 (dotimes (i length)
604 (setf (bvref bytes (+ offset i))
605 (sb!xc:char-code (aref string i))))
606 (setf (bvref bytes (+ offset length))
607 0) ; null string-termination character for C
608 des))
610 (defun base-string-from-core (descriptor)
611 (let* ((len (descriptor-fixnum
612 (read-wordindexed descriptor sb!vm:vector-length-slot)))
613 (str (make-string len))
614 (bytes (descriptor-bytes descriptor)))
615 (dotimes (i len str)
616 (setf (aref str i)
617 (code-char (bvref bytes
618 (+ (descriptor-byte-offset descriptor)
619 (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
620 i)))))))
622 (defun bignum-to-core (n)
623 "Copy a bignum to the cold core."
624 (let* ((words (ceiling (1+ (integer-length n)) sb!vm:n-word-bits))
625 (handle
626 (allocate-header+object *dynamic* words sb!vm:bignum-widetag)))
627 (declare (fixnum words))
628 (do ((index 1 (1+ index))
629 (remainder n (ash remainder (- sb!vm:n-word-bits))))
630 ((> index words)
631 (unless (zerop (integer-length remainder))
632 ;; FIXME: Shouldn't this be a fatal error?
633 (warn "~W words of ~W were written, but ~W bits were left over."
634 words n remainder)))
635 ;; FIXME: this is disgusting. there should be WRITE-BITS-WORDINDEXED.
636 (write-wordindexed handle index
637 (make-random-descriptor
638 (ldb (byte sb!vm:n-word-bits 0) remainder))))
639 handle))
641 (defun bignum-from-core (descriptor)
642 (let ((n-words (ash (descriptor-bits (read-memory descriptor))
643 (- sb!vm:n-widetag-bits)))
644 (val 0))
645 (dotimes (i n-words val)
646 (let ((bits (read-bits-wordindexed descriptor
647 (+ i sb!vm:bignum-digits-offset))))
648 ;; sign-extend the highest word
649 (when (and (= i (1- n-words)) (logbitp (1- sb!vm:n-word-bits) bits))
650 (setq bits (dpb bits (byte sb!vm:n-word-bits 0) -1)))
651 (setq val (logior (ash bits (* i sb!vm:n-word-bits)) val))))))
653 (defun number-pair-to-core (first second type)
654 "Makes a number pair of TYPE (ratio or complex) and fills it in."
655 (let ((des (allocate-header+object *dynamic* 2 type)))
656 (write-wordindexed des 1 first)
657 (write-wordindexed des 2 second)
658 des))
660 (defun write-double-float-bits (address index x)
661 (let ((hi (double-float-high-bits x))
662 (lo (double-float-low-bits x)))
663 (ecase sb!vm::n-word-bits
665 ;; As noted in BIGNUM-TO-CORE, this idiom is unclear - the things
666 ;; aren't descriptors. Same for COMPLEX-foo-TO-CORE
667 (let ((high-bits (make-random-descriptor hi))
668 (low-bits (make-random-descriptor lo)))
669 (ecase sb!c:*backend-byte-order*
670 (:little-endian
671 (write-wordindexed address index low-bits)
672 (write-wordindexed address (1+ index) high-bits))
673 (:big-endian
674 (write-wordindexed address index high-bits)
675 (write-wordindexed address (1+ index) low-bits)))))
677 (let ((bits (make-random-descriptor
678 (ecase sb!c:*backend-byte-order*
679 (:little-endian (logior lo (ash hi 32)))
680 ;; Just guessing.
681 #+nil (:big-endian (logior (logand hi #xffffffff)
682 (ash lo 32)))))))
683 (write-wordindexed address index bits))))
684 address))
686 (defun float-to-core (x)
687 (etypecase x
688 (single-float
689 ;; 64-bit platforms have immediate single-floats.
690 #!+64-bit
691 (make-random-descriptor (logior (ash (single-float-bits x) 32)
692 sb!vm::single-float-widetag))
693 #!-64-bit
694 (let ((des (allocate-header+object *dynamic*
695 (1- sb!vm:single-float-size)
696 sb!vm:single-float-widetag)))
697 (write-wordindexed des
698 sb!vm:single-float-value-slot
699 (make-random-descriptor (single-float-bits x)))
700 des))
701 (double-float
702 (let ((des (allocate-header+object *dynamic*
703 (1- sb!vm:double-float-size)
704 sb!vm:double-float-widetag)))
705 (write-double-float-bits des sb!vm:double-float-value-slot x)))))
707 (defun complex-single-float-to-core (num)
708 (declare (type (complex single-float) num))
709 (let ((des (allocate-header+object *dynamic*
710 (1- sb!vm:complex-single-float-size)
711 sb!vm:complex-single-float-widetag)))
712 #!-64-bit
713 (progn
714 (write-wordindexed des sb!vm:complex-single-float-real-slot
715 (make-random-descriptor (single-float-bits (realpart num))))
716 (write-wordindexed des sb!vm:complex-single-float-imag-slot
717 (make-random-descriptor (single-float-bits (imagpart num)))))
718 #!+64-bit
719 (write-wordindexed des sb!vm:complex-single-float-data-slot
720 (make-random-descriptor
721 (logior (ldb (byte 32 0) (single-float-bits (realpart num)))
722 (ash (single-float-bits (imagpart num)) 32))))
723 des))
725 (defun complex-double-float-to-core (num)
726 (declare (type (complex double-float) num))
727 (let ((des (allocate-header+object *dynamic*
728 (1- sb!vm:complex-double-float-size)
729 sb!vm:complex-double-float-widetag)))
730 (write-double-float-bits des sb!vm:complex-double-float-real-slot
731 (realpart num))
732 (write-double-float-bits des sb!vm:complex-double-float-imag-slot
733 (imagpart num))))
735 ;;; Copy the given number to the core.
736 (defun number-to-core (number)
737 (typecase number
738 (integer (or (%fixnum-descriptor-if-possible number)
739 (bignum-to-core number)))
740 (ratio (number-pair-to-core (number-to-core (numerator number))
741 (number-to-core (denominator number))
742 sb!vm:ratio-widetag))
743 ((complex single-float) (complex-single-float-to-core number))
744 ((complex double-float) (complex-double-float-to-core number))
745 #!+long-float
746 ((complex long-float)
747 (error "~S isn't a cold-loadable number at all!" number))
748 (complex (number-pair-to-core (number-to-core (realpart number))
749 (number-to-core (imagpart number))
750 sb!vm:complex-widetag))
751 (float (float-to-core number))
752 (t (error "~S isn't a cold-loadable number at all!" number))))
754 (declaim (ftype (function (sb!vm:word) descriptor) sap-int-to-core))
755 (defun sap-int-to-core (sap-int)
756 (let ((des (allocate-header+object *dynamic* (1- sb!vm:sap-size)
757 sb!vm:sap-widetag)))
758 (write-wordindexed des
759 sb!vm:sap-pointer-slot
760 (make-random-descriptor sap-int))
761 des))
763 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
764 (defun cold-cons (car cdr &optional (gspace *dynamic*))
765 (let ((dest (allocate-object gspace 2 sb!vm:list-pointer-lowtag)))
766 (write-wordindexed dest sb!vm:cons-car-slot car)
767 (write-wordindexed dest sb!vm:cons-cdr-slot cdr)
768 dest))
769 (defun list-to-core (list)
770 (let ((head *nil-descriptor*)
771 (tail nil))
772 ;; A recursive algorithm would have the first cons at the highest
773 ;; address. This way looks nicer when viewed in ldb.
774 (loop
775 (unless list (return head))
776 (let ((cons (cold-cons (pop list) *nil-descriptor*)))
777 (if tail (cold-rplacd tail cons) (setq head cons))
778 (setq tail cons)))))
779 (defun cold-list (&rest args) (list-to-core args))
780 (defun cold-list-length (list) ; but no circularity detection
781 ;; a recursive implementation uses too much stack for some Lisps
782 (let ((n 0))
783 (loop (if (cold-null list) (return n))
784 (incf n)
785 (setq list (cold-cdr list)))))
787 ;;; Make a simple-vector on the target that holds the specified
788 ;;; OBJECTS, and return its descriptor.
789 ;;; This is really "vectorify-list-into-core" but that's too wordy,
790 ;;; so historically it was "vector-in-core" which is a fine name.
791 (defun vector-in-core (objects &optional (gspace *dynamic*))
792 (let* ((size (length objects))
793 (result (allocate-vector-object gspace sb!vm:n-word-bits size
794 sb!vm:simple-vector-widetag)))
795 (dotimes (index size)
796 (write-wordindexed result (+ index sb!vm:vector-data-offset)
797 (pop objects)))
798 result))
799 (defun cold-svset (vector index value)
800 (let ((i (if (integerp index) index (descriptor-fixnum index))))
801 (write-wordindexed vector (+ i sb!vm:vector-data-offset) value)))
803 (setf (get 'vector :sb-cold-funcall-handler/for-value)
804 (lambda (&rest args) (vector-in-core args)))
806 (declaim (inline cold-vector-len cold-svref))
807 (defun cold-vector-len (vector)
808 (descriptor-fixnum (read-wordindexed vector sb!vm:vector-length-slot)))
809 (defun cold-svref (vector i)
810 (read-wordindexed vector (+ (if (integerp i) i (descriptor-fixnum i))
811 sb!vm:vector-data-offset)))
812 (defun cold-vector-elements-eq (a b)
813 (and (eql (cold-vector-len a) (cold-vector-len b))
814 (dotimes (k (cold-vector-len a) t)
815 (unless (descriptor= (cold-svref a k) (cold-svref b k))
816 (return nil)))))
817 (defun vector-from-core (descriptor &optional (transform #'identity))
818 (let* ((len (cold-vector-len descriptor))
819 (vector (make-array len)))
820 (dotimes (i len vector)
821 (setf (aref vector i) (funcall transform (cold-svref descriptor i))))))
823 ;;;; symbol magic
825 ;; Simulate *FREE-TLS-INDEX*. This is a count, not a displacement.
826 ;; In C, sizeof counts 1 word for the variable-length interrupt_contexts[]
827 ;; but primitive-object-size counts 0, so add 1, though in fact the C code
828 ;; implies that it might have overcounted by 1. We could make this agnostic
829 ;; of MAX-INTERRUPTS by moving the thread base register up by TLS-SIZE words,
830 ;; using negative offsets for all dynamically assigned indices.
831 (defvar *genesis-tls-counter*
832 (+ 1 sb!vm::max-interrupts
833 (sb!vm:primitive-object-size
834 (find 'sb!vm::thread sb!vm:*primitive-objects*
835 :key #'sb!vm:primitive-object-name))))
837 #!+sb-thread
838 (progn
839 ;; Assign SYMBOL the tls-index INDEX. SYMBOL must be a descriptor.
840 ;; This is a backend support routine, but the style within this file
841 ;; is to conditionalize by the target features.
842 (defun cold-assign-tls-index (symbol index)
843 #!+64-bit
844 (let ((header-word
845 (logior (ash index 32)
846 (descriptor-bits (read-memory symbol)))))
847 (write-wordindexed symbol 0 (make-random-descriptor header-word)))
848 #!-64-bit
849 (write-wordindexed symbol sb!vm:symbol-tls-index-slot
850 (make-random-descriptor index)))
852 ;; Return SYMBOL's tls-index,
853 ;; choosing a new index if it doesn't have one yet.
854 (defun ensure-symbol-tls-index (symbol)
855 (let* ((cold-sym (cold-intern symbol))
856 (tls-index
857 #!+64-bit
858 (ldb (byte 32 32) (descriptor-bits (read-memory cold-sym)))
859 #!-64-bit
860 (descriptor-bits
861 (read-wordindexed cold-sym sb!vm:symbol-tls-index-slot))))
862 (unless (plusp tls-index)
863 (let ((next (prog1 *genesis-tls-counter* (incf *genesis-tls-counter*))))
864 (setq tls-index (ash next sb!vm:word-shift))
865 (cold-assign-tls-index cold-sym tls-index)))
866 tls-index)))
868 ;; A table of special variable names which get known TLS indices.
869 ;; Some of them are mapped onto 'struct thread' and have pre-determined offsets.
870 ;; Others are static symbols used with bind_variable() in the C runtime,
871 ;; and might not, in the absence of this table, get an index assigned by genesis
872 ;; depending on whether the cross-compiler used the BIND vop on them.
873 ;; Indices for those static symbols can be chosen arbitrarily, which is to say
874 ;; the value doesn't matter but must update the tls-counter correctly.
875 ;; All symbols other than the ones in this table get the indices assigned
876 ;; by the fasloader on demand.
877 #!+sb-thread
878 (defvar *known-tls-symbols*
879 ;; FIXME: no mechanism exists to determine which static symbols C code will
880 ;; dynamically bind. TLS is a finite resource, and wasting indices for all
881 ;; static symbols isn't the best idea. This list was hand-made with 'grep'.
882 '(sb!vm:*alloc-signal*
883 sb!sys:*allow-with-interrupts*
884 sb!vm:*current-catch-block*
885 sb!vm::*current-unwind-protect-block*
886 sb!kernel:*free-interrupt-context-index*
887 sb!kernel:*gc-inhibit*
888 sb!kernel:*gc-pending*
889 sb!impl::*gc-safe*
890 sb!impl::*in-safepoint*
891 sb!sys:*interrupt-pending*
892 sb!sys:*interrupts-enabled*
893 sb!vm::*pinned-objects*
894 sb!kernel:*restart-clusters*
895 sb!kernel:*stop-for-gc-pending*
896 #!+sb-thruption
897 sb!sys:*thruption-pending*))
899 ;;; Symbol print names are coalesced by string=.
900 ;;; This is valid because it is an error to modify a print name.
901 (defvar *symbol-name-strings* (make-hash-table :test 'equal))
903 ;;; Allocate (and initialize) a symbol.
904 (defun allocate-symbol (name &key (gspace *dynamic*))
905 (declare (simple-string name))
906 (let ((symbol (allocate-header+object gspace (1- sb!vm:symbol-size)
907 sb!vm:symbol-header-widetag)))
908 (write-wordindexed symbol sb!vm:symbol-value-slot *unbound-marker*)
909 (write-wordindexed symbol sb!vm:symbol-hash-slot (make-fixnum-descriptor 0))
910 (write-wordindexed symbol sb!vm:symbol-info-slot *nil-descriptor*)
911 (write-wordindexed symbol sb!vm:symbol-name-slot
912 (or (gethash name *symbol-name-strings*)
913 (setf (gethash name *symbol-name-strings*)
914 (base-string-to-core name *dynamic*))))
915 (write-wordindexed symbol sb!vm:symbol-package-slot *nil-descriptor*)
916 symbol))
918 #!+sb-thread
919 (defun assign-tls-index (symbol cold-symbol)
920 (let ((index (info :variable :wired-tls symbol)))
921 (cond ((integerp index) ; thread slot
922 (cold-assign-tls-index cold-symbol index))
923 ((memq symbol *known-tls-symbols*)
924 ;; symbols without which the C runtime could not start
925 (shiftf index *genesis-tls-counter* (1+ *genesis-tls-counter*))
926 (cold-assign-tls-index cold-symbol (ash index sb!vm:word-shift))))))
928 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
929 ;;; descriptor of a cold symbol or (in an abbreviation for the
930 ;;; most common usage pattern) an ordinary symbol, which will be
931 ;;; automatically cold-interned.
932 (declaim (ftype (function ((or symbol descriptor) descriptor)) cold-set))
933 (defun cold-set (symbol-or-symbol-des value)
934 (let ((symbol-des (etypecase symbol-or-symbol-des
935 (descriptor symbol-or-symbol-des)
936 (symbol (cold-intern symbol-or-symbol-des)))))
937 (write-wordindexed symbol-des sb!vm:symbol-value-slot value)))
938 (defun cold-symbol-value (symbol)
939 (let ((val (read-wordindexed (cold-intern symbol) sb!vm:symbol-value-slot)))
940 (if (= (descriptor-bits val) sb!vm:unbound-marker-widetag)
941 (unbound-cold-symbol-handler symbol)
942 val)))
943 (defun cold-fdefn-fun (cold-fdefn)
944 (read-wordindexed cold-fdefn sb!vm:fdefn-fun-slot))
946 (defun unbound-cold-symbol-handler (symbol)
947 (let ((host-val (and (boundp symbol) (symbol-value symbol))))
948 (if (typep host-val 'sb!kernel:named-type)
949 (let ((target-val (ctype-to-core (sb!kernel:named-type-name host-val)
950 host-val)))
951 ;; Though it looks complicated to assign cold symbols on demand,
952 ;; it avoids writing code to build the layout of NAMED-TYPE in the
953 ;; way we build other primordial stuff such as layout-of-layout.
954 (cold-set symbol target-val)
955 target-val)
956 (error "Taking Cold-symbol-value of unbound symbol ~S" symbol))))
958 ;;;; layouts and type system pre-initialization
960 ;;; Since we want to be able to dump structure constants and
961 ;;; predicates with reference layouts, we need to create layouts at
962 ;;; cold-load time. We use the name to intern layouts by, and dump a
963 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
964 ;;; initialization can find them. The only thing that's tricky [sic --
965 ;;; WHN 19990816] is initializing layout's layout, which must point to
966 ;;; itself.
968 ;;; a map from name as a host symbol to the descriptor of its target layout
969 (defvar *cold-layouts* (make-hash-table :test 'equal))
971 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
972 ;;; mapping
973 (defvar *cold-layout-names* (make-hash-table :test 'eql))
975 ;;; FIXME: *COLD-LAYOUTS* and *COLD-LAYOUT-NAMES* should be
976 ;;; initialized by binding in GENESIS.
978 ;;; the descriptor for layout's layout (needed when making layouts)
979 (defvar *layout-layout*)
980 ;;; the descriptor for PACKAGE's layout (needed when making packages)
981 (defvar *package-layout*)
983 (defvar *known-structure-classoids*)
985 (defconstant target-layout-length
986 ;; LAYOUT-LENGTH counts the number of words in an instance,
987 ;; including the layout itself as 1 word
988 (layout-length *host-layout-of-layout*))
990 ;;; Return a list of names created from the cold layout INHERITS data
991 ;;; in X.
992 (defun listify-cold-inherits (x)
993 (map 'list (lambda (des)
994 (or (gethash (descriptor-bits des) *cold-layout-names*)
995 (error "~S is not the descriptor of a cold-layout" des)))
996 (vector-from-core x)))
998 ;;; COLD-DD-SLOTS is a cold descriptor for the list of slots
999 ;;; in a cold defstruct-description. INDEX is a DSD-INDEX.
1000 ;;; Return the host's accessor name for the host image of that slot.
1001 (defun dsd-accessor-from-cold-slots (cold-dd-slots desired-index)
1002 (let* ((dsd-slots (dd-slots
1003 (find-defstruct-description 'defstruct-slot-description)))
1004 (index-slot
1005 (dsd-index (find 'sb!kernel::index dsd-slots :key #'dsd-name)))
1006 (accessor-fun-name-slot
1007 (dsd-index (find 'sb!kernel::accessor-name dsd-slots :key #'dsd-name))))
1008 (do ((list cold-dd-slots (cold-cdr list)))
1009 ((cold-null list))
1010 (when (= (descriptor-fixnum
1011 (read-wordindexed (cold-car list)
1012 (+ sb!vm:instance-slots-offset index-slot)))
1013 desired-index)
1014 (return
1015 (warm-symbol
1016 (read-wordindexed (cold-car list)
1017 (+ sb!vm:instance-slots-offset
1018 accessor-fun-name-slot))))))))
1020 (flet ((get-slots (host-layout-or-type)
1021 (etypecase host-layout-or-type
1022 (layout (dd-slots (layout-info host-layout-or-type)))
1023 (symbol (dd-slots-from-core host-layout-or-type))))
1024 (get-slot-index (slots initarg)
1025 (+ sb!vm:instance-slots-offset
1026 (if (descriptor-p slots)
1027 (do ((dsd-layout (find-layout 'defstruct-slot-description))
1028 (slots slots (cold-cdr slots)))
1029 ((cold-null slots) (error "No slot for ~S" initarg))
1030 (let* ((dsd (cold-car slots))
1031 (slot-name (read-slot dsd dsd-layout :name)))
1032 (when (eq (keywordicate (warm-symbol slot-name)) initarg)
1033 ;; Untagged slots are not accessible during cold-load
1034 (aver (eql (descriptor-fixnum
1035 (read-slot dsd dsd-layout :%raw-type)) -1))
1036 (return (descriptor-fixnum
1037 (read-slot dsd dsd-layout :index))))))
1038 (let ((dsd (find initarg slots
1039 :test (lambda (x y)
1040 (eq x (keywordicate (dsd-name y)))))))
1041 (aver (eq (dsd-raw-type dsd) t)) ; Same as above: no can do.
1042 (dsd-index dsd))))))
1043 (defun write-slots (cold-object host-layout-or-type &rest assignments)
1044 (aver (evenp (length assignments)))
1045 (let ((slots (get-slots host-layout-or-type)))
1046 (loop for (initarg value) on assignments by #'cddr
1047 do (write-wordindexed
1048 cold-object (get-slot-index slots initarg) value)))
1049 cold-object)
1051 ;; For symmetry, the reader takes an initarg, not a slot name.
1052 (defun read-slot (cold-object host-layout-or-type slot-initarg)
1053 (let ((slots (get-slots host-layout-or-type)))
1054 (read-wordindexed cold-object (get-slot-index slots slot-initarg)))))
1056 ;; Given a TYPE-NAME of a structure-class, find its defstruct-description
1057 ;; as a target descriptor, and return the slot list as a target descriptor.
1058 (defun dd-slots-from-core (type-name)
1059 (let* ((host-dd-layout (find-layout 'defstruct-description))
1060 (target-dd
1061 ;; This is inefficient, but not enough so to worry about.
1062 (or (car (assoc (cold-intern type-name) *known-structure-classoids*
1063 :key (lambda (x) (read-slot x host-dd-layout :name))
1064 :test #'descriptor=))
1065 (error "No known layout for ~S" type-name))))
1066 (read-slot target-dd host-dd-layout :slots)))
1068 (defvar *simple-vector-0-descriptor*)
1069 (defvar *vacuous-slot-table*)
1070 (declaim (ftype (function (symbol descriptor descriptor descriptor descriptor)
1071 descriptor)
1072 make-cold-layout))
1073 (defun make-cold-layout (name length inherits depthoid metadata)
1074 (let ((result (allocate-struct *dynamic* *layout-layout*
1075 target-layout-length)))
1076 ;; Don't set the CLOS hash value: done in cold-init instead.
1078 ;; Set other slot values.
1080 ;; leave CLASSOID uninitialized for now
1081 (multiple-value-call
1082 #'write-slots result *host-layout-of-layout*
1083 :invalid *nil-descriptor*
1084 :inherits inherits
1085 :depthoid depthoid
1086 :length length
1087 :info *nil-descriptor*
1088 :pure *nil-descriptor*
1089 #!-interleaved-raw-slots
1090 (values :n-untagged-slots metadata)
1091 #!+interleaved-raw-slots
1092 (values :untagged-bitmap metadata
1093 ;; Nothing in cold-init needs to call EQUALP on a structure with raw slots,
1094 ;; but for type-correctness this slot needs to be a simple-vector.
1095 :equalp-tests (if (boundp '*simple-vector-0-descriptor*)
1096 *simple-vector-0-descriptor*
1097 (setq *simple-vector-0-descriptor*
1098 (vector-in-core nil))))
1099 :source-location *nil-descriptor*
1100 :%for-std-class-b (make-fixnum-descriptor 0)
1101 :slot-list *nil-descriptor*
1102 (if (member name '(null list symbol))
1103 ;; Assign an empty slot-table. Why this is done only for three
1104 ;; classoids is ... too complicated to explain here in a few words,
1105 ;; but revision 18c239205d9349abc017b07e7894a710835c5205 broke it.
1106 ;; Keep this in sync with MAKE-SLOT-TABLE in pcl/slots-boot.
1107 (values :slot-table (if (boundp '*vacuous-slot-table*)
1108 *vacuous-slot-table*
1109 (setq *vacuous-slot-table*
1110 (host-constant-to-core '#(1 nil)))))
1111 (values)))
1113 (setf (gethash (descriptor-bits result) *cold-layout-names*) name
1114 (gethash name *cold-layouts*) result)))
1116 ;;; Convert SPECIFIER (equivalently OBJ) to its representation as a ctype
1117 ;;; in the cold core.
1118 (defvar *ctype-cache*)
1119 (defun ctype-to-core (specifier obj)
1120 (declare (type ctype obj))
1121 ;; CTYPEs can't be TYPE=-hashed, but specifiers can be EQUAL-hashed.
1122 (or (gethash specifier *ctype-cache*)
1123 (let* ((class-info-slot
1124 (dsd-index (find 'sb!kernel::class-info
1125 (dd-slots (find-defstruct-description 'ctype))
1126 :key #'dsd-name)))
1127 (result
1128 (ctype-to-core-helper
1130 (lambda (obj)
1131 (typecase obj
1132 (xset (ctype-to-core-helper obj nil))
1133 (ctype (ctype-to-core (type-specifier obj) obj))))
1134 (cons class-info-slot *nil-descriptor*))))
1135 (let ((type-class-vector
1136 (cold-symbol-value 'sb!kernel::*type-classes*))
1137 (index (position (sb!kernel::type-class-info obj)
1138 sb!kernel::*type-classes*)))
1139 ;; Push this instance into the list of fixups for its type class
1140 (cold-svset type-class-vector index
1141 (cold-cons result (cold-svref type-class-vector index))))
1142 (setf (gethash specifier *ctype-cache*) result))))
1144 (defun ctype-to-core-helper (obj obj-to-core-helper &rest exceptional-slots)
1145 (let* ((host-type (type-of obj))
1146 (target-layout (or (gethash host-type *cold-layouts*)
1147 (error "No target layout for ~S" obj)))
1148 (result (allocate-struct *dynamic* target-layout))
1149 (cold-dd-slots (dd-slots-from-core host-type)))
1150 (aver (zerop (layout-raw-slot-metadata (find-layout host-type))))
1151 ;; Dump the slots.
1152 (do ((len (cold-layout-length target-layout))
1153 (index 1 (1+ index)))
1154 ((= index len) result)
1155 (write-wordindexed
1156 result
1157 (+ sb!vm:instance-slots-offset index)
1158 (acond ((assq index exceptional-slots) (cdr it))
1159 (t (host-constant-to-core
1160 (funcall (dsd-accessor-from-cold-slots cold-dd-slots index)
1161 obj)
1162 obj-to-core-helper)))))))
1164 ;; This is called to backpatch three small sets of objects:
1165 ;; - layouts which are made before layout-of-layout is made (4 of them)
1166 ;; - packages, which are made before layout-of-package is made (all of them)
1167 ;; - a small number of classoid-cells (probably 3 or 4).
1168 (defun patch-instance-layout (thing layout)
1169 ;; Layout pointer is in the word following the header
1170 (write-wordindexed thing sb!vm:instance-slots-offset layout))
1172 (defun cold-layout-of (cold-struct)
1173 (read-wordindexed cold-struct sb!vm:instance-slots-offset))
1175 (defun initialize-layouts ()
1176 (clrhash *cold-layouts*)
1177 ;; This assertion is due to the fact that MAKE-COLD-LAYOUT does not
1178 ;; know how to set any raw slots.
1179 (aver (= 0 (layout-raw-slot-metadata *host-layout-of-layout*)))
1180 (setq *layout-layout* (make-fixnum-descriptor 0))
1181 (flet ((chill-layout (name &rest inherits)
1182 ;; Check that the number of specified INHERITS matches
1183 ;; the length of the layout's inherits in the cross-compiler.
1184 (let ((warm-layout (classoid-layout (find-classoid name))))
1185 (assert (eql (length (layout-inherits warm-layout))
1186 (length inherits)))
1187 (make-cold-layout
1188 name
1189 (number-to-core (layout-length warm-layout))
1190 (vector-in-core inherits)
1191 (number-to-core (layout-depthoid warm-layout))
1192 (number-to-core (layout-raw-slot-metadata warm-layout))))))
1193 (let* ((t-layout (chill-layout 't))
1194 (s-o-layout (chill-layout 'structure-object t-layout))
1195 (s!o-layout (chill-layout 'structure!object t-layout s-o-layout)))
1196 (setf *layout-layout*
1197 (chill-layout 'layout t-layout s-o-layout s!o-layout))
1198 (dolist (layout (list t-layout s-o-layout s!o-layout *layout-layout*))
1199 (patch-instance-layout layout *layout-layout*))
1200 (setf *package-layout*
1201 (chill-layout 'package t-layout s-o-layout)))))
1203 ;;;; interning symbols in the cold image
1205 ;;; a map from package name as a host string to
1206 ;;; (cold-package-descriptor . (external-symbols . internal-symbols))
1207 (defvar *cold-package-symbols*)
1208 (declaim (type hash-table *cold-package-symbols*))
1210 (setf (get 'find-package :sb-cold-funcall-handler/for-value)
1211 (lambda (descriptor &aux (name (base-string-from-core descriptor)))
1212 (or (car (gethash name *cold-package-symbols*))
1213 (error "Genesis could not find a target package named ~S" name))))
1215 (defvar *classoid-cells*)
1216 (setf (get 'find-classoid-cell :sb-cold-funcall-handler/for-value)
1217 (lambda (name &key create)
1218 (aver (eq create t))
1219 (or (gethash name *classoid-cells*)
1220 (let ((layout (gethash 'sb!kernel::classoid-cell *cold-layouts*))
1221 (host-layout (find-layout 'sb!kernel::classoid-cell)))
1222 (setf (gethash name *classoid-cells*)
1223 (write-slots (allocate-struct *dynamic* layout
1224 (layout-length host-layout))
1225 host-layout
1226 :name name
1227 :pcl-class *nil-descriptor*
1228 :classoid *nil-descriptor*))))))
1230 ;;; a map from descriptors to symbols, so that we can back up. The key
1231 ;;; is the address in the target core.
1232 (defvar *cold-symbols*)
1233 (declaim (type hash-table *cold-symbols*))
1235 (defun initialize-packages (package-data-list)
1236 (let ((package-layout (find-layout 'package))
1237 (target-pkg-list nil))
1238 (labels ((init-cold-package (name &optional docstring)
1239 (let ((cold-package (car (gethash name *cold-package-symbols*))))
1240 ;; patch in the layout
1241 (patch-instance-layout cold-package *package-layout*)
1242 ;; Initialize string slots
1243 (write-slots cold-package package-layout
1244 :%name (base-string-to-core
1245 (target-package-name name))
1246 :%nicknames (chill-nicknames name)
1247 :doc-string (if docstring
1248 (base-string-to-core docstring)
1249 *nil-descriptor*)
1250 :%use-list *nil-descriptor*)
1251 ;; the cddr of this will accumulate the 'used-by' package list
1252 (push (list name cold-package) target-pkg-list)))
1253 (target-package-name (string)
1254 (if (eql (mismatch string "SB!") 3)
1255 (concatenate 'string "SB-" (subseq string 3))
1256 string))
1257 (chill-nicknames (pkg-name)
1258 (let ((result *nil-descriptor*))
1259 ;; Make the package nickname lists for the standard packages
1260 ;; be the minimum specified by ANSI, regardless of what value
1261 ;; the cross-compilation host happens to use.
1262 ;; For packages other than the standard packages, the nickname
1263 ;; list was specified by our package setup code, and we can just
1264 ;; propagate the current state into the target.
1265 (dolist (nickname
1266 (cond ((string= pkg-name "COMMON-LISP") '("CL"))
1267 ((string= pkg-name "COMMON-LISP-USER")
1268 '("CL-USER"))
1269 ((string= pkg-name "KEYWORD") '())
1271 ;; 'package-data-list' contains no nicknames.
1272 ;; (See comment in 'set-up-cold-packages')
1273 (aver (null (package-nicknames
1274 (find-package pkg-name))))
1275 nil))
1276 result)
1277 (cold-push (base-string-to-core nickname) result))))
1278 (find-cold-package (name)
1279 (cadr (find-package-cell name)))
1280 (find-package-cell (name)
1281 (or (assoc (if (string= name "CL") "COMMON-LISP" name)
1282 target-pkg-list :test #'string=)
1283 (error "No cold package named ~S" name))))
1284 ;; pass 1: make all proto-packages
1285 (dolist (pd package-data-list)
1286 (init-cold-package (sb-cold:package-data-name pd)
1287 #!+sb-doc(sb-cold::package-data-doc pd)))
1288 ;; pass 2: set the 'use' lists and collect the 'used-by' lists
1289 (dolist (pd package-data-list)
1290 (let ((this (find-cold-package (sb-cold:package-data-name pd)))
1291 (use nil))
1292 (dolist (that (sb-cold:package-data-use pd))
1293 (let ((cell (find-package-cell that)))
1294 (push (cadr cell) use)
1295 (push this (cddr cell))))
1296 (write-slots this package-layout
1297 :%use-list (list-to-core (nreverse use)))))
1298 ;; pass 3: set the 'used-by' lists
1299 (dolist (cell target-pkg-list)
1300 (write-slots (cadr cell) package-layout
1301 :%used-by-list (list-to-core (cddr cell)))))))
1303 ;;; sanity check for a symbol we're about to create on the target
1305 ;;; Make sure that the symbol has an appropriate package. In
1306 ;;; particular, catch the so-easy-to-make error of typing something
1307 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1308 ;;; need is SB!KERNEL:%BYTE-BLT.
1309 (defun package-ok-for-target-symbol-p (package)
1310 (let ((package-name (package-name package)))
1312 ;; Cold interning things in these standard packages is OK. (Cold
1313 ;; interning things in the other standard package, CL-USER, isn't
1314 ;; OK. We just use CL-USER to expose symbols whose homes are in
1315 ;; other packages. Thus, trying to cold intern a symbol whose
1316 ;; home package is CL-USER probably means that a coding error has
1317 ;; been made somewhere.)
1318 (find package-name '("COMMON-LISP" "KEYWORD") :test #'string=)
1319 ;; Cold interning something in one of our target-code packages,
1320 ;; which are ever-so-rigorously-and-elegantly distinguished by
1321 ;; this prefix on their names, is OK too.
1322 (string= package-name "SB!" :end1 3 :end2 3)
1323 ;; This one is OK too, since it ends up being COMMON-LISP on the
1324 ;; target.
1325 (string= package-name "SB-XC")
1326 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1327 ;; package in the xc host? something we can't think of
1328 ;; a valid reason to cold intern, anyway...)
1331 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1333 ;;; Most host symbols we dump onto the target are created by SBCL
1334 ;;; itself, so that as long as we avoid gratuitously
1335 ;;; cross-compilation-unfriendly hacks, it just happens that their
1336 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1337 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1338 ;;; in the COMMON-LISP package, where we don't get to create the
1339 ;;; symbols but instead have to use the ones that the xc host created.
1340 ;;; In particular, while ANSI specifies which symbols are exported
1341 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1342 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1343 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1344 ;;; symbols in the CLOS package).
1345 (defun symbol-package-for-target-symbol (symbol)
1346 ;; We want to catch weird symbols like CLISP's
1347 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1348 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1349 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1350 (multiple-value-bind (cl-symbol cl-status)
1351 (find-symbol (symbol-name symbol) *cl-package*)
1352 (if (and (eq symbol cl-symbol)
1353 (eq cl-status :external))
1354 ;; special case, to work around possible xc host weirdness
1355 ;; in COMMON-LISP package
1356 *cl-package*
1357 ;; ordinary case
1358 (let ((result (symbol-package symbol)))
1359 (unless (package-ok-for-target-symbol-p result)
1360 (bug "~A in bad package for target: ~A" symbol result))
1361 result))))
1363 (defvar *uninterned-symbol-table* (make-hash-table :test #'equal))
1364 ;; This coalesces references to uninterned symbols, which is allowed because
1365 ;; "similar-as-constant" is defined by string comparison, and since we only have
1366 ;; base-strings during Genesis, there is no concern about upgraded array type.
1367 ;; There is a subtlety of whether coalescing may occur across files
1368 ;; - the target compiler doesn't and couldn't - but here it doesn't matter.
1369 (defun get-uninterned-symbol (name)
1370 (or (gethash name *uninterned-symbol-table*)
1371 (let ((cold-symbol (allocate-symbol name)))
1372 (setf (gethash name *uninterned-symbol-table*) cold-symbol))))
1374 ;;; Dump the target representation of HOST-VALUE,
1375 ;;; the type of which is in a restrictive set.
1376 (defun host-constant-to-core (host-value &optional helper)
1377 (let ((visited (make-hash-table :test #'eq)))
1378 (named-let target-representation ((value host-value))
1379 (unless (typep value '(or symbol number descriptor))
1380 (let ((found (gethash value visited)))
1381 (cond ((eq found :pending)
1382 (bug "circular constant?")) ; Circularity not permitted
1383 (found
1384 (return-from target-representation found))))
1385 (setf (gethash value visited) :pending))
1386 (setf (gethash value visited)
1387 (typecase value
1388 (descriptor value)
1389 (symbol (if (symbol-package value)
1390 (cold-intern value)
1391 (get-uninterned-symbol (string value))))
1392 (number (number-to-core value))
1393 (string (base-string-to-core value))
1394 (cons (cold-cons (target-representation (car value))
1395 (target-representation (cdr value))))
1396 (simple-vector
1397 (vector-in-core (map 'list #'target-representation value)))
1399 (or (and helper (funcall helper value))
1400 (error "host-constant-to-core: can't convert ~S"
1401 value))))))))
1403 ;; Look up the target's descriptor for #'FUN where FUN is a host symbol.
1404 (defun target-symbol-function (symbol)
1405 (let ((f (cold-fdefn-fun (cold-fdefinition-object symbol))))
1406 ;; It works only if DEFUN F was seen first.
1407 (aver (not (cold-null f)))
1410 ;;; Create the effect of executing a (MAKE-ARRAY) call on the target.
1411 ;;; This is for initializing a restricted set of vector constants
1412 ;;; whose contents are typically function pointers.
1413 (defun emulate-target-make-array (form)
1414 (destructuring-bind (size-expr &key initial-element) (cdr form)
1415 (let* ((size (eval size-expr))
1416 (result (allocate-vector-object *dynamic* sb!vm:n-word-bits size
1417 sb!vm:simple-vector-widetag)))
1418 (aver (integerp size))
1419 (unless (eql initial-element 0)
1420 (let ((target-initial-element
1421 (etypecase initial-element
1422 ((cons (eql function) (cons symbol null))
1423 (target-symbol-function (second initial-element)))
1424 (null *nil-descriptor*)
1425 ;; Insert more types here ...
1427 (dotimes (index size)
1428 (cold-svset result (make-fixnum-descriptor index)
1429 target-initial-element))))
1430 result)))
1432 ;; Return a target object produced by emulating evaluation of EXPR
1433 ;; with *package* set to ORIGINAL-PACKAGE.
1434 (defun emulate-target-eval (expr original-package)
1435 (let ((*package* (find-package original-package)))
1436 ;; For most things, just call EVAL and dump the host object's
1437 ;; target representation. But with MAKE-ARRAY we allow that the
1438 ;; initial-element might not be evaluable in the host.
1439 ;; Embedded MAKE-ARRAY is kept as-is because we don't "look into"
1440 ;; the EXPR, just hope that it works.
1441 (if (typep expr '(cons (eql make-array)))
1442 (emulate-target-make-array expr)
1443 (host-constant-to-core (eval expr)))))
1445 ;;; Return a handle on an interned symbol. If necessary allocate the
1446 ;;; symbol and record its home package.
1447 (defun cold-intern (symbol
1448 &key (access nil)
1449 (gspace *dynamic*)
1450 &aux (package (symbol-package-for-target-symbol symbol)))
1451 (aver (package-ok-for-target-symbol-p package))
1453 ;; Anything on the cross-compilation host which refers to the target
1454 ;; machinery through the host SB-XC package should be translated to
1455 ;; something on the target which refers to the same machinery
1456 ;; through the target COMMON-LISP package.
1457 (let ((p (find-package "SB-XC")))
1458 (when (eq package p)
1459 (setf package *cl-package*))
1460 (when (eq (symbol-package symbol) p)
1461 (setf symbol (intern (symbol-name symbol) *cl-package*))))
1463 (or (get symbol 'cold-intern-info)
1464 (let ((pkg-info (gethash (package-name package) *cold-package-symbols*))
1465 (handle (allocate-symbol (symbol-name symbol) :gspace gspace)))
1466 ;; maintain reverse map from target descriptor to host symbol
1467 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1468 (unless pkg-info
1469 (error "No target package descriptor for ~S" package))
1470 (record-accessibility
1471 (or access (nth-value 1 (find-symbol (symbol-name symbol) package)))
1472 handle pkg-info symbol package t)
1473 #!+sb-thread
1474 (assign-tls-index symbol handle)
1475 (acond ((eq package *keyword-package*)
1476 (setq access :external)
1477 (cold-set handle handle))
1478 ((assoc symbol sb-cold:*symbol-values-for-genesis*)
1479 (cold-set handle (destructuring-bind (expr . package) (cdr it)
1480 (emulate-target-eval expr package)))))
1481 (setf (get symbol 'cold-intern-info) handle))))
1483 (defun record-accessibility (accessibility symbol-descriptor target-pkg-info
1484 host-symbol host-package &optional set-home-p)
1485 (when set-home-p
1486 (write-wordindexed symbol-descriptor sb!vm:symbol-package-slot
1487 (car target-pkg-info)))
1488 (let ((access-lists (cdr target-pkg-info)))
1489 (case accessibility
1490 (:external (push symbol-descriptor (car access-lists)))
1491 (:internal (push symbol-descriptor (cdr access-lists)))
1492 (t (error "~S inaccessible in package ~S" host-symbol host-package)))))
1494 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1495 ;;; It might be nice to put NIL on a readonly page by itself to prevent unsafe
1496 ;;; code from destroying the world with (RPLACx nil 'kablooey)
1497 (defun make-nil-descriptor (target-cl-pkg-info)
1498 (let* ((des (allocate-header+object *static* sb!vm:symbol-size 0))
1499 (result (make-descriptor (+ (descriptor-bits des)
1500 (* 2 sb!vm:n-word-bytes)
1501 (- sb!vm:list-pointer-lowtag
1502 sb!vm:other-pointer-lowtag)))))
1503 (write-wordindexed des
1505 (make-other-immediate-descriptor
1507 sb!vm:symbol-header-widetag))
1508 (write-wordindexed des
1509 (+ 1 sb!vm:symbol-value-slot)
1510 result)
1511 (write-wordindexed des
1512 (+ 2 sb!vm:symbol-value-slot) ; = 1 + symbol-hash-slot
1513 result)
1514 (write-wordindexed des
1515 (+ 1 sb!vm:symbol-info-slot)
1516 (cold-cons result result)) ; NIL's info is (nil . nil)
1517 (write-wordindexed des
1518 (+ 1 sb!vm:symbol-name-slot)
1519 ;; NIL's name is in dynamic space because any extra
1520 ;; bytes allocated in static space would need to
1521 ;; be accounted for by STATIC-SYMBOL-OFFSET.
1522 (base-string-to-core "NIL" *dynamic*))
1523 ;; RECORD-ACCESSIBILITY can't assign to the package slot
1524 ;; due to NIL's base address and lowtag being nonstandard.
1525 (write-wordindexed des
1526 (+ 1 sb!vm:symbol-package-slot)
1527 (car target-cl-pkg-info))
1528 (record-accessibility :external result target-cl-pkg-info nil *cl-package*)
1529 (setf (gethash (descriptor-bits result) *cold-symbols*) nil
1530 (get nil 'cold-intern-info) result)))
1532 ;;; Since the initial symbols must be allocated before we can intern
1533 ;;; anything else, we intern those here. We also set the value of T.
1534 (defun initialize-non-nil-symbols ()
1535 "Initialize the cold load symbol-hacking data structures."
1536 ;; Intern the others.
1537 (dolist (symbol sb!vm:*static-symbols*)
1538 (let* ((des (cold-intern symbol :gspace *static*))
1539 (offset-wanted (sb!vm:static-symbol-offset symbol))
1540 (offset-found (- (descriptor-bits des)
1541 (descriptor-bits *nil-descriptor*))))
1542 (unless (= offset-wanted offset-found)
1543 ;; FIXME: should be fatal
1544 (warn "Offset from ~S to ~S is ~W, not ~W"
1545 symbol
1547 offset-found
1548 offset-wanted))))
1549 ;; Establish the value of T.
1550 (let ((t-symbol (cold-intern t :gspace *static*)))
1551 (cold-set t-symbol t-symbol))
1552 ;; Establish the value of *PSEUDO-ATOMIC-BITS* so that the
1553 ;; allocation sequences that expect it to be zero upon entrance
1554 ;; actually find it to be so.
1555 #!+(or x86-64 x86)
1556 (let ((p-a-a-symbol (cold-intern '*pseudo-atomic-bits*
1557 :gspace *static*)))
1558 (cold-set p-a-a-symbol (make-fixnum-descriptor 0))))
1560 ;;; Sort *COLD-LAYOUTS* to return them in a deterministic order.
1561 (defun sort-cold-layouts ()
1562 (sort (%hash-table-alist *cold-layouts*) #'<
1563 :key (lambda (x) (descriptor-bits (cdr x)))))
1565 ;;; a helper function for FINISH-SYMBOLS: Return a cold alist suitable
1566 ;;; to be stored in *!INITIAL-LAYOUTS*.
1567 (defun cold-list-all-layouts ()
1568 (let ((result *nil-descriptor*))
1569 (dolist (layout (sort-cold-layouts) result)
1570 (cold-push (cold-cons (cold-intern (car layout)) (cdr layout))
1571 result))))
1573 ;;; Establish initial values for magic symbols.
1575 (defun finish-symbols ()
1577 ;; Everything between this preserved-for-posterity comment down to
1578 ;; the assignment of *CURRENT-CATCH-BLOCK* could be entirely deleted,
1579 ;; including the list of *C-CALLABLE-STATIC-SYMBOLS* itself,
1580 ;; if it is GC-safe for the C runtime to have its own implementation
1581 ;; of the INFO-VECTOR-FDEFN function in a multi-threaded build.
1583 ;; "I think the point of setting these functions into SYMBOL-VALUEs
1584 ;; here, instead of using SYMBOL-FUNCTION, is that in CMU CL
1585 ;; SYMBOL-FUNCTION reduces to FDEFINITION, which is a pretty
1586 ;; hairy operation (involving globaldb.lisp etc.) which we don't
1587 ;; want to invoke early in cold init. -- WHN 2001-12-05"
1589 ;; So... that's no longer true. We _do_ associate symbol -> fdefn in genesis.
1590 ;; Additionally, the INFO-VECTOR-FDEFN function is extremely simple and could
1591 ;; easily be implemented in C. However, info-vectors are inevitably
1592 ;; reallocated when new info is attached to a symbol, so the vectors can't be
1593 ;; in static space; they'd gradually become permanent garbage if they did.
1594 ;; That's the real reason for preserving the approach of storing an #<fdefn>
1595 ;; in a symbol's value cell - that location is static, the symbol-info is not.
1597 ;; FIXME: So OK, that's a reasonable reason to do something weird like
1598 ;; this, but this is still a weird thing to do, and we should change
1599 ;; the names to highlight that something weird is going on. Perhaps
1600 ;; *MAYBE-GC-FUN*, *INTERNAL-ERROR-FUN*, *HANDLE-BREAKPOINT-FUN*,
1601 ;; and *HANDLE-FUN-END-BREAKPOINT-FUN*...
1602 (dolist (symbol sb!vm::*c-callable-static-symbols*)
1603 (cold-set symbol (cold-fdefinition-object (cold-intern symbol))))
1605 (cold-set 'sb!vm::*current-catch-block* (make-fixnum-descriptor 0))
1606 (cold-set 'sb!vm::*current-unwind-protect-block* (make-fixnum-descriptor 0))
1608 (cold-set '*free-interrupt-context-index* (make-fixnum-descriptor 0))
1610 (cold-set '*!initial-layouts* (cold-list-all-layouts))
1612 #!+sb-thread
1613 (cold-set 'sb!vm::*free-tls-index*
1614 (make-descriptor (ash *genesis-tls-counter* sb!vm:word-shift)))
1616 (dolist (symbol sb!impl::*cache-vector-symbols*)
1617 (cold-set symbol *nil-descriptor*))
1619 ;; Symbols for which no call to COLD-INTERN would occur - due to not being
1620 ;; referenced until warm init - must be artificially cold-interned.
1621 ;; Inasmuch as the "offending" things are compiled by ordinary target code
1622 ;; and not cold-init, I think we should use an ordinary DEFPACKAGE for
1623 ;; the added-on bits. What I've done is somewhat of a fragile kludge.
1624 (let (syms)
1625 (with-package-iterator (iter '("SB!PCL" "SB!MOP" "SB!GRAY" "SB!SEQUENCE"
1626 "SB!PROFILE" "SB!EXT" "SB!VM"
1627 "SB!C" "SB!FASL" "SB!DEBUG")
1628 :external)
1629 (loop
1630 (multiple-value-bind (foundp sym accessibility package) (iter)
1631 (declare (ignore accessibility))
1632 (cond ((not foundp) (return))
1633 ((eq (symbol-package sym) package) (push sym syms))))))
1634 (setf syms (stable-sort syms #'string<))
1635 (dolist (sym syms)
1636 (cold-intern sym)))
1638 (let ((cold-pkg-inits *nil-descriptor*)
1639 cold-package-symbols-list)
1640 (maphash (lambda (name info)
1641 (push (cons name info) cold-package-symbols-list))
1642 *cold-package-symbols*)
1643 (setf cold-package-symbols-list
1644 (sort cold-package-symbols-list #'string< :key #'car))
1645 (dolist (pkgcons cold-package-symbols-list)
1646 (destructuring-bind (pkg-name . pkg-info) pkgcons
1647 (let ((shadow
1648 ;; Record shadowing symbols (except from SB-XC) in SB! packages.
1649 (when (eql (mismatch pkg-name "SB!") 3)
1650 ;; Be insensitive to the host's ordering.
1651 (sort (remove (find-package "SB-XC")
1652 (package-shadowing-symbols (find-package pkg-name))
1653 :key #'symbol-package) #'string<))))
1654 (write-slots (car (gethash pkg-name *cold-package-symbols*)) ; package
1655 (find-layout 'package)
1656 :%shadowing-symbols (list-to-core
1657 (mapcar 'cold-intern shadow))))
1658 (unless (member pkg-name '("COMMON-LISP" "KEYWORD") :test 'string=)
1659 (let ((host-pkg (find-package pkg-name))
1660 (sb-xc-pkg (find-package "SB-XC"))
1661 syms)
1662 ;; Now for each symbol directly present in this host-pkg,
1663 ;; i.e. accessible but not :INHERITED, figure out if the symbol
1664 ;; came from a different package, and if so, make a note of it.
1665 (with-package-iterator (iter host-pkg :internal :external)
1666 (loop (multiple-value-bind (foundp sym accessibility) (iter)
1667 (unless foundp (return))
1668 (unless (or (eq (symbol-package sym) host-pkg)
1669 (eq (symbol-package sym) sb-xc-pkg))
1670 (push (cons sym accessibility) syms)))))
1671 (dolist (symcons (sort syms #'string< :key #'car))
1672 (destructuring-bind (sym . accessibility) symcons
1673 (record-accessibility accessibility (cold-intern sym)
1674 pkg-info sym host-pkg)))))
1675 (cold-push (cold-cons (car pkg-info)
1676 (cold-cons (vector-in-core (cadr pkg-info))
1677 (vector-in-core (cddr pkg-info))))
1678 cold-pkg-inits)))
1679 (cold-set 'sb!impl::*!initial-symbols* cold-pkg-inits))
1681 (dump-symbol-info-vectors
1682 (attach-fdefinitions-to-symbols
1683 (attach-classoid-cells-to-symbols (make-hash-table :test #'eq))))
1685 (cold-set '*!initial-debug-sources* *current-debug-sources*)
1687 #!+(or x86 x86-64)
1688 (progn
1689 (cold-set 'sb!vm::*fp-constant-0d0* (number-to-core 0d0))
1690 (cold-set 'sb!vm::*fp-constant-1d0* (number-to-core 1d0))
1691 (cold-set 'sb!vm::*fp-constant-0f0* (number-to-core 0f0))
1692 (cold-set 'sb!vm::*fp-constant-1f0* (number-to-core 1f0))))
1694 ;;;; functions and fdefinition objects
1696 ;;; a hash table mapping from fdefinition names to descriptors of cold
1697 ;;; objects
1699 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1700 ;;; we want to have only one entry per name, this must be an 'EQUAL
1701 ;;; hash table, not the default 'EQL.
1702 (defvar *cold-fdefn-objects*)
1704 (defvar *cold-fdefn-gspace* nil)
1706 ;;; Given a cold representation of a symbol, return a warm
1707 ;;; representation.
1708 (defun warm-symbol (des)
1709 ;; Note that COLD-INTERN is responsible for keeping the
1710 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1711 ;; uninterned symbol, the code below will fail. But as long as we
1712 ;; don't need to look up uninterned symbols during bootstrapping,
1713 ;; that's OK..
1714 (multiple-value-bind (symbol found-p)
1715 (gethash (descriptor-bits des) *cold-symbols*)
1716 (declare (type symbol symbol))
1717 (unless found-p
1718 (error "no warm symbol"))
1719 symbol))
1721 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1722 (defun cold-car (des)
1723 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1724 (read-wordindexed des sb!vm:cons-car-slot))
1725 (defun cold-cdr (des)
1726 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1727 (read-wordindexed des sb!vm:cons-cdr-slot))
1728 (defun cold-rplacd (des newval)
1729 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1730 (write-wordindexed des sb!vm:cons-cdr-slot newval)
1731 des)
1732 (defun cold-null (des)
1733 (= (descriptor-bits des)
1734 (descriptor-bits *nil-descriptor*)))
1736 ;;; Given a cold representation of a function name, return a warm
1737 ;;; representation.
1738 (declaim (ftype (function ((or symbol descriptor)) (or symbol list)) warm-fun-name))
1739 (defun warm-fun-name (des)
1740 (let ((result
1741 (if (symbolp des)
1742 ;; This parallels the logic at the start of COLD-INTERN
1743 ;; which re-homes symbols in SB-XC to COMMON-LISP.
1744 (if (eq (symbol-package des) (find-package "SB-XC"))
1745 (intern (symbol-name des) *cl-package*)
1746 des)
1747 (ecase (descriptor-lowtag des)
1748 (#.sb!vm:list-pointer-lowtag
1749 (aver (not (cold-null des))) ; function named NIL? please no..
1750 ;; Do cold (DESTRUCTURING-BIND (COLD-CAR COLD-CADR) DES ..).
1751 (let* ((car-des (cold-car des))
1752 (cdr-des (cold-cdr des))
1753 (cadr-des (cold-car cdr-des))
1754 (cddr-des (cold-cdr cdr-des)))
1755 (aver (cold-null cddr-des))
1756 (list (warm-symbol car-des)
1757 (warm-symbol cadr-des))))
1758 (#.sb!vm:other-pointer-lowtag
1759 (warm-symbol des))))))
1760 (legal-fun-name-or-type-error result)
1761 result))
1763 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw)
1764 (declare (type (or symbol descriptor) cold-name))
1765 (/noshow0 "/cold-fdefinition-object")
1766 (let ((warm-name (warm-fun-name cold-name)))
1767 (or (gethash warm-name *cold-fdefn-objects*)
1768 (let ((fdefn (allocate-header+object (or *cold-fdefn-gspace* *dynamic*)
1769 (1- sb!vm:fdefn-size)
1770 sb!vm:fdefn-widetag)))
1771 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
1772 (write-wordindexed fdefn sb!vm:fdefn-name-slot cold-name)
1773 (unless leave-fn-raw
1774 (write-wordindexed fdefn sb!vm:fdefn-fun-slot *nil-descriptor*)
1775 (write-wordindexed fdefn
1776 sb!vm:fdefn-raw-addr-slot
1777 (make-random-descriptor
1778 (cold-foreign-symbol-address "undefined_tramp"))))
1779 fdefn))))
1781 ;;; Handle the at-cold-init-time, fset-for-static-linkage operation.
1782 ;;; "static" is sort of a misnomer. It's just ordinary fdefinition linkage.
1783 (defun static-fset (cold-name defn)
1784 (declare (type (or symbol descriptor) cold-name))
1785 (let ((fdefn (cold-fdefinition-object cold-name t))
1786 (type (logand (descriptor-bits (read-memory defn)) sb!vm:widetag-mask)))
1787 (write-wordindexed fdefn sb!vm:fdefn-fun-slot defn)
1788 (write-wordindexed fdefn
1789 sb!vm:fdefn-raw-addr-slot
1790 (ecase type
1791 (#.sb!vm:simple-fun-header-widetag
1792 (/noshow0 "static-fset (simple-fun)")
1793 #!+(or sparc arm)
1794 defn
1795 #!-(or sparc arm)
1796 (make-random-descriptor
1797 (+ (logandc2 (descriptor-bits defn)
1798 sb!vm:lowtag-mask)
1799 (ash sb!vm:simple-fun-code-offset
1800 sb!vm:word-shift))))
1801 (#.sb!vm:closure-header-widetag
1802 ;; There's no way to create a closure.
1803 (bug "FSET got closure-header-widetag")
1804 (/show0 "/static-fset (closure)")
1805 (make-random-descriptor
1806 (cold-foreign-symbol-address "closure_tramp")))))
1807 fdefn))
1809 ;;; the names of things which have had COLD-FSET used on them already
1810 ;;; (used to make sure that we don't try to statically link a name to
1811 ;;; more than one definition)
1812 (defparameter *cold-fset-warm-names*
1813 (make-hash-table :test 'equal)) ; names can be conses, e.g. (SETF CAR)
1815 (defun cold-fset (name compiled-lambda source-loc &optional inline-expansion)
1816 ;; SOURCE-LOC can be ignored, because functions intrinsically store
1817 ;; their location as part of the code component.
1818 ;; The argument is supplied here only to provide context for
1819 ;; a redefinition warning, which can't happen in cold load.
1820 (declare (ignore source-loc))
1821 (multiple-value-bind (cold-name warm-name)
1822 ;; (SETF f) was descriptorized when dumped, symbols were not,
1823 ;; Figure out what kind of name we're looking at.
1824 (if (symbolp name)
1825 (values (cold-intern name) name)
1826 (values name (warm-fun-name name)))
1827 (when (gethash warm-name *cold-fset-warm-names*)
1828 (error "duplicate COLD-FSET for ~S" warm-name))
1829 (setf (gethash warm-name *cold-fset-warm-names*) t)
1830 (push (cold-cons cold-name inline-expansion) *!cold-defuns*)
1831 (static-fset cold-name compiled-lambda)))
1833 (defun initialize-static-fns ()
1834 (let ((*cold-fdefn-gspace* *static*))
1835 (dolist (sym sb!vm:*static-funs*)
1836 (let* ((fdefn (cold-fdefinition-object (cold-intern sym)))
1837 (offset (- (+ (- (descriptor-bits fdefn)
1838 sb!vm:other-pointer-lowtag)
1839 (* sb!vm:fdefn-raw-addr-slot sb!vm:n-word-bytes))
1840 (descriptor-bits *nil-descriptor*)))
1841 (desired (sb!vm:static-fun-offset sym)))
1842 (unless (= offset desired)
1843 ;; FIXME: should be fatal
1844 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1845 sym nil offset desired))))))
1847 (defun attach-classoid-cells-to-symbols (hashtable)
1848 (let ((num (sb!c::meta-info-number (sb!c::meta-info :type :classoid-cell)))
1849 (layout (gethash 'sb!kernel::classoid-cell *cold-layouts*)))
1850 (when (plusp (hash-table-count *classoid-cells*))
1851 (aver layout))
1852 ;; Iteration order is immaterial. The symbols will get sorted later.
1853 (maphash (lambda (symbol cold-classoid-cell)
1854 ;; Some classoid-cells are dumped before the cold layout
1855 ;; of classoid-cell has been made, so fix those cases now.
1856 ;; Obviously it would be better if, in general, ALLOCATE-STRUCT
1857 ;; knew when something later must backpatch a cold layout
1858 ;; so that it could make a note to itself to do those ASAP
1859 ;; after the cold layout became known.
1860 (when (cold-null (cold-layout-of cold-classoid-cell))
1861 (patch-instance-layout cold-classoid-cell layout))
1862 (setf (gethash symbol hashtable)
1863 (packed-info-insert
1864 (gethash symbol hashtable +nil-packed-infos+)
1865 sb!c::+no-auxilliary-key+ num cold-classoid-cell)))
1866 *classoid-cells*))
1867 hashtable)
1869 ;; Create pointer from SYMBOL and/or (SETF SYMBOL) to respective fdefinition
1871 (defun attach-fdefinitions-to-symbols (hashtable)
1872 ;; Collect fdefinitions that go with one symbol, e.g. CAR and (SETF CAR),
1873 ;; using the host's code for manipulating a packed info-vector.
1874 (maphash (lambda (warm-name cold-fdefn)
1875 (with-globaldb-name (key1 key2) warm-name
1876 :hairy (error "Hairy fdefn name in genesis: ~S" warm-name)
1877 :simple
1878 (setf (gethash key1 hashtable)
1879 (packed-info-insert
1880 (gethash key1 hashtable +nil-packed-infos+)
1881 key2 +fdefn-info-num+ cold-fdefn))))
1882 *cold-fdefn-objects*)
1883 hashtable)
1885 (defun dump-symbol-info-vectors (hashtable)
1886 ;; Emit in the same order symbols reside in core to avoid
1887 ;; sensitivity to the iteration order of host's maphash.
1888 (loop for (warm-sym . info)
1889 in (sort (%hash-table-alist hashtable) #'<
1890 :key (lambda (x) (descriptor-bits (cold-intern (car x)))))
1891 do (write-wordindexed
1892 (cold-intern warm-sym) sb!vm:symbol-info-slot
1893 ;; Each vector will have one fixnum, possibly the symbol SETF,
1894 ;; and one or two #<fdefn> objects in it, and/or a classoid-cell.
1895 (vector-in-core
1896 (map 'list (lambda (elt)
1897 (etypecase elt
1898 (symbol (cold-intern elt))
1899 (fixnum (make-fixnum-descriptor elt))
1900 (descriptor elt)))
1901 info)))))
1904 ;;;; fixups and related stuff
1906 ;;; an EQUAL hash table
1907 (defvar *cold-foreign-symbol-table*)
1908 (declaim (type hash-table *cold-foreign-symbol-table*))
1910 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1911 ;; the C runtime.
1912 (defun load-cold-foreign-symbol-table (filename)
1913 (/show "load-cold-foreign-symbol-table" filename)
1914 (with-open-file (file filename)
1915 (loop for line = (read-line file nil nil)
1916 while line do
1917 ;; UNIX symbol tables might have tabs in them, and tabs are
1918 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1919 ;; nice portable way to deal with them within Lisp, alas.
1920 ;; Fortunately, it's easy to use UNIX command line tools like
1921 ;; sed to remove the problem, so it's not too painful for us
1922 ;; to push responsibility for converting tabs to spaces out to
1923 ;; the caller.
1925 ;; Other non-STANDARD-CHARs are problematic for the same reason.
1926 ;; Make sure that there aren't any..
1927 (let ((ch (find-if (lambda (char)
1928 (not (typep char 'standard-char)))
1929 line)))
1930 (when ch
1931 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1933 line)))
1934 (setf line (string-trim '(#\space) line))
1935 (let ((p1 (position #\space line :from-end nil))
1936 (p2 (position #\space line :from-end t)))
1937 (if (not (and p1 p2 (< p1 p2)))
1938 ;; KLUDGE: It's too messy to try to understand all
1939 ;; possible output from nm, so we just punt the lines we
1940 ;; don't recognize. We realize that there's some chance
1941 ;; that might get us in trouble someday, so we warn
1942 ;; about it.
1943 (warn "ignoring unrecognized line ~S in ~A" line filename)
1944 (multiple-value-bind (value name)
1945 (if (string= "0x" line :end2 2)
1946 (values (parse-integer line :start 2 :end p1 :radix 16)
1947 (subseq line (1+ p2)))
1948 (values (parse-integer line :end p1 :radix 16)
1949 (subseq line (1+ p2))))
1950 ;; KLUDGE CLH 2010-05-31: on darwin, nm gives us
1951 ;; _function but dlsym expects us to look up
1952 ;; function, without the leading _ . Therefore, we
1953 ;; strip it off here.
1954 #!+darwin
1955 (when (equal (char name 0) #\_)
1956 (setf name (subseq name 1)))
1957 (multiple-value-bind (old-value found)
1958 (gethash name *cold-foreign-symbol-table*)
1959 (when (and found
1960 (not (= old-value value)))
1961 (warn "redefining ~S from #X~X to #X~X"
1962 name old-value value)))
1963 (/show "adding to *cold-foreign-symbol-table*:" name value)
1964 (setf (gethash name *cold-foreign-symbol-table*) value)
1965 #!+win32
1966 (let ((at-position (position #\@ name)))
1967 (when at-position
1968 (let ((name (subseq name 0 at-position)))
1969 (multiple-value-bind (old-value found)
1970 (gethash name *cold-foreign-symbol-table*)
1971 (when (and found
1972 (not (= old-value value)))
1973 (warn "redefining ~S from #X~X to #X~X"
1974 name old-value value)))
1975 (setf (gethash name *cold-foreign-symbol-table*)
1976 value)))))))))
1977 (values)) ;; PROGN
1979 (defun cold-foreign-symbol-address (name)
1980 (or (find-foreign-symbol-in-table name *cold-foreign-symbol-table*)
1981 *foreign-symbol-placeholder-value*
1982 (progn
1983 (format *error-output* "~&The foreign symbol table is:~%")
1984 (maphash (lambda (k v)
1985 (format *error-output* "~&~S = #X~8X~%" k v))
1986 *cold-foreign-symbol-table*)
1987 (error "The foreign symbol ~S is undefined." name))))
1989 (defvar *cold-assembler-routines*)
1991 (defvar *cold-assembler-fixups*)
1993 (defun record-cold-assembler-routine (name address)
1994 (/xhow "in RECORD-COLD-ASSEMBLER-ROUTINE" name address)
1995 (push (cons name address)
1996 *cold-assembler-routines*))
1998 (defun record-cold-assembler-fixup (routine
1999 code-object
2000 offset
2001 &optional
2002 (kind :both))
2003 (push (list routine code-object offset kind)
2004 *cold-assembler-fixups*))
2006 (defun lookup-assembler-reference (symbol)
2007 (let ((value (cdr (assoc symbol *cold-assembler-routines*))))
2008 ;; FIXME: Should this be ERROR instead of WARN?
2009 (unless value
2010 (warn "Assembler routine ~S not defined." symbol))
2011 value))
2013 ;;; Unlike in the target, FOP-KNOWN-FUN sometimes has to backpatch.
2014 (defvar *deferred-known-fun-refs*)
2016 ;;; The x86 port needs to store code fixups along with code objects if
2017 ;;; they are to be moved, so fixups for code objects in the dynamic
2018 ;;; heap need to be noted.
2019 #!+x86
2020 (defvar *load-time-code-fixups*)
2022 #!+x86
2023 (defun note-load-time-code-fixup (code-object offset)
2024 ;; If CODE-OBJECT might be moved
2025 (when (= (gspace-identifier (descriptor-intuit-gspace code-object))
2026 dynamic-core-space-id)
2027 (push offset (gethash (descriptor-bits code-object)
2028 *load-time-code-fixups*
2029 nil)))
2030 (values))
2032 #!+x86
2033 (defun output-load-time-code-fixups ()
2034 (let ((fixup-infos nil))
2035 (maphash
2036 (lambda (code-object-address fixup-offsets)
2037 (push (cons code-object-address fixup-offsets) fixup-infos))
2038 *load-time-code-fixups*)
2039 (setq fixup-infos (sort fixup-infos #'< :key #'car))
2040 (dolist (fixup-info fixup-infos)
2041 (let ((code-object-address (car fixup-info))
2042 (fixup-offsets (cdr fixup-info)))
2043 (let ((fixup-vector
2044 (allocate-vector-object
2045 *dynamic* sb!vm:n-word-bits (length fixup-offsets)
2046 sb!vm:simple-array-unsigned-byte-32-widetag)))
2047 (do ((index sb!vm:vector-data-offset (1+ index))
2048 (fixups fixup-offsets (cdr fixups)))
2049 ((null fixups))
2050 (write-wordindexed fixup-vector index
2051 (make-random-descriptor (car fixups))))
2052 ;; KLUDGE: The fixup vector is stored as the first constant,
2053 ;; not as a separately-named slot.
2054 (write-wordindexed (make-random-descriptor code-object-address)
2055 sb!vm:code-constants-offset
2056 fixup-vector))))))
2058 ;;; Given a pointer to a code object and an offset relative to the
2059 ;;; tail of the code object's header, return an offset relative to the
2060 ;;; (beginning of the) code object.
2062 ;;; FIXME: It might be clearer to reexpress
2063 ;;; (LET ((X (CALC-OFFSET CODE-OBJECT OFFSET0))) ..)
2064 ;;; as
2065 ;;; (LET ((X (+ OFFSET0 (CODE-OBJECT-HEADER-N-BYTES CODE-OBJECT)))) ..).
2066 (declaim (ftype (function (descriptor sb!vm:word)) calc-offset))
2067 (defun calc-offset (code-object offset-from-tail-of-header)
2068 (let* ((header (read-memory code-object))
2069 (header-n-words (ash (descriptor-bits header)
2070 (- sb!vm:n-widetag-bits)))
2071 (header-n-bytes (ash header-n-words sb!vm:word-shift))
2072 (result (+ offset-from-tail-of-header header-n-bytes)))
2073 result))
2075 (declaim (ftype (function (descriptor sb!vm:word sb!vm:word keyword))
2076 do-cold-fixup))
2077 (defun do-cold-fixup (code-object after-header value kind)
2078 (let* ((offset-within-code-object (calc-offset code-object after-header))
2079 (gspace-bytes (descriptor-bytes code-object))
2080 (gspace-byte-offset (+ (descriptor-byte-offset code-object)
2081 offset-within-code-object))
2082 (gspace-byte-address (gspace-byte-address
2083 (descriptor-gspace code-object))))
2084 ;; There's just a ton of code here that gets deleted,
2085 ;; inhibiting the view of the the forest through the trees.
2086 ;; Use of #+sbcl would say "probable bug in read-time conditional"
2087 #+#.(cl:if (cl:member :sbcl cl:*features*) '(and) '(or))
2088 (declare (sb-ext:muffle-conditions sb-ext:code-deletion-note))
2089 (ecase +backend-fasl-file-implementation+
2090 ;; See CMU CL source for other formerly-supported architectures
2091 ;; (and note that you have to rewrite them to use BVREF-X
2092 ;; instead of SAP-REF).
2093 (:alpha
2094 (ecase kind
2095 (:jmp-hint
2096 (assert (zerop (ldb (byte 2 0) value))))
2097 (:bits-63-48
2098 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
2099 (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
2100 (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
2101 (setf (bvref-8 gspace-bytes gspace-byte-offset)
2102 (ldb (byte 8 48) value)
2103 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
2104 (ldb (byte 8 56) value))))
2105 (:bits-47-32
2106 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
2107 (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
2108 (setf (bvref-8 gspace-bytes gspace-byte-offset)
2109 (ldb (byte 8 32) value)
2110 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
2111 (ldb (byte 8 40) value))))
2112 (:ldah
2113 (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
2114 (setf (bvref-8 gspace-bytes gspace-byte-offset)
2115 (ldb (byte 8 16) value)
2116 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
2117 (ldb (byte 8 24) value))))
2118 (:lda
2119 (setf (bvref-8 gspace-bytes gspace-byte-offset)
2120 (ldb (byte 8 0) value)
2121 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
2122 (ldb (byte 8 8) value)))))
2123 (:arm
2124 (ecase kind
2125 (:absolute
2126 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))))
2127 (:arm64
2128 (ecase kind
2129 (:absolute
2130 (setf (bvref-64 gspace-bytes gspace-byte-offset) value))
2131 (:cond-branch
2132 (setf (ldb (byte 19 5)
2133 (bvref-32 gspace-bytes gspace-byte-offset))
2134 (ash (- value (+ gspace-byte-address gspace-byte-offset))
2135 -2)))
2136 (:uncond-branch
2137 (setf (ldb (byte 26 0)
2138 (bvref-32 gspace-bytes gspace-byte-offset))
2139 (ash (- value (+ gspace-byte-address gspace-byte-offset))
2140 -2)))))
2141 (:hppa
2142 (ecase kind
2143 (:absolute
2144 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))
2145 (:load
2146 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2147 (logior (mask-field (byte 18 14)
2148 (bvref-32 gspace-bytes gspace-byte-offset))
2149 (if (< value 0)
2150 (1+ (ash (ldb (byte 13 0) value) 1))
2151 (ash (ldb (byte 13 0) value) 1)))))
2152 (:load11u
2153 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2154 (logior (mask-field (byte 18 14)
2155 (bvref-32 gspace-bytes gspace-byte-offset))
2156 (if (< value 0)
2157 (1+ (ash (ldb (byte 10 0) value) 1))
2158 (ash (ldb (byte 11 0) value) 1)))))
2159 (:load-short
2160 (let ((low-bits (ldb (byte 11 0) value)))
2161 (assert (<= 0 low-bits (1- (ash 1 4)))))
2162 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2163 (logior (ash (dpb (ldb (byte 4 0) value)
2164 (byte 4 1)
2165 (ldb (byte 1 4) value)) 17)
2166 (logand (bvref-32 gspace-bytes gspace-byte-offset)
2167 #xffe0ffff))))
2168 (:hi
2169 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2170 (logior (mask-field (byte 11 21)
2171 (bvref-32 gspace-bytes gspace-byte-offset))
2172 (ash (ldb (byte 5 13) value) 16)
2173 (ash (ldb (byte 2 18) value) 14)
2174 (ash (ldb (byte 2 11) value) 12)
2175 (ash (ldb (byte 11 20) value) 1)
2176 (ldb (byte 1 31) value))))
2177 (:branch
2178 (let ((bits (ldb (byte 9 2) value)))
2179 (assert (zerop (ldb (byte 2 0) value)))
2180 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2181 (logior (ash bits 3)
2182 (mask-field (byte 1 1) (bvref-32 gspace-bytes gspace-byte-offset))
2183 (mask-field (byte 3 13) (bvref-32 gspace-bytes gspace-byte-offset))
2184 (mask-field (byte 11 21) (bvref-32 gspace-bytes gspace-byte-offset))))))))
2185 (:mips
2186 (ecase kind
2187 (:jump
2188 (assert (zerop (ash value -28)))
2189 (setf (ldb (byte 26 0)
2190 (bvref-32 gspace-bytes gspace-byte-offset))
2191 (ash value -2)))
2192 (:lui
2193 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2194 (logior (mask-field (byte 16 16)
2195 (bvref-32 gspace-bytes gspace-byte-offset))
2196 (ash (1+ (ldb (byte 17 15) value)) -1))))
2197 (:addi
2198 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2199 (logior (mask-field (byte 16 16)
2200 (bvref-32 gspace-bytes gspace-byte-offset))
2201 (ldb (byte 16 0) value))))))
2202 ;; FIXME: PowerPC Fixups are not fully implemented. The bit
2203 ;; here starts to set things up to work properly, but there
2204 ;; needs to be corresponding code in ppc-vm.lisp
2205 (:ppc
2206 (ecase kind
2207 (:ba
2208 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2209 (dpb (ash value -2) (byte 24 2)
2210 (bvref-32 gspace-bytes gspace-byte-offset))))
2211 (:ha
2212 (let* ((un-fixed-up (bvref-16 gspace-bytes
2213 (+ gspace-byte-offset 2)))
2214 (fixed-up (+ un-fixed-up value))
2215 (h (ldb (byte 16 16) fixed-up))
2216 (l (ldb (byte 16 0) fixed-up)))
2217 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
2218 (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
2220 (let* ((un-fixed-up (bvref-16 gspace-bytes
2221 (+ gspace-byte-offset 2)))
2222 (fixed-up (+ un-fixed-up value)))
2223 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
2224 (ldb (byte 16 0) fixed-up))))))
2225 (:sparc
2226 (ecase kind
2227 (:call
2228 (error "can't deal with call fixups yet"))
2229 (:sethi
2230 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2231 (dpb (ldb (byte 22 10) value)
2232 (byte 22 0)
2233 (bvref-32 gspace-bytes gspace-byte-offset))))
2234 (:add
2235 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2236 (dpb (ldb (byte 10 0) value)
2237 (byte 10 0)
2238 (bvref-32 gspace-bytes gspace-byte-offset))))))
2239 ((:x86 :x86-64)
2240 ;; XXX: Note that un-fixed-up is read via bvref-word, which is
2241 ;; 64 bits wide on x86-64, but the fixed-up value is written
2242 ;; via bvref-32. This would make more sense if we supported
2243 ;; :absolute64 fixups, but apparently the cross-compiler
2244 ;; doesn't dump them.
2245 (let* ((un-fixed-up (bvref-word gspace-bytes
2246 gspace-byte-offset))
2247 (code-object-start-addr (logandc2 (descriptor-bits code-object)
2248 sb!vm:lowtag-mask)))
2249 (assert (= code-object-start-addr
2250 (+ gspace-byte-address
2251 (descriptor-byte-offset code-object))))
2252 (ecase kind
2253 (:absolute
2254 (let ((fixed-up (+ value un-fixed-up)))
2255 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2256 fixed-up)
2257 ;; comment from CMU CL sources:
2259 ;; Note absolute fixups that point within the object.
2260 ;; KLUDGE: There seems to be an implicit assumption in
2261 ;; the old CMU CL code here, that if it doesn't point
2262 ;; before the object, it must point within the object
2263 ;; (not beyond it). It would be good to add an
2264 ;; explanation of why that's true, or an assertion that
2265 ;; it's really true, or both.
2267 ;; One possible explanation is that all absolute fixups
2268 ;; point either within the code object, within the
2269 ;; runtime, within read-only or static-space, or within
2270 ;; the linkage-table space. In all x86 configurations,
2271 ;; these areas are prior to the start of dynamic space,
2272 ;; where all the code-objects are loaded.
2273 #!+x86
2274 (unless (< fixed-up code-object-start-addr)
2275 (note-load-time-code-fixup code-object
2276 after-header))))
2277 (:relative ; (used for arguments to X86 relative CALL instruction)
2278 (let ((fixed-up (- (+ value un-fixed-up)
2279 gspace-byte-address
2280 gspace-byte-offset
2281 4))) ; "length of CALL argument"
2282 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2283 fixed-up)
2284 ;; Note relative fixups that point outside the code
2285 ;; object, which is to say all relative fixups, since
2286 ;; relative addressing within a code object never needs
2287 ;; a fixup.
2288 #!+x86
2289 (note-load-time-code-fixup code-object
2290 after-header))))))))
2291 (values))
2293 (defun resolve-assembler-fixups ()
2294 (dolist (fixup *cold-assembler-fixups*)
2295 (let* ((routine (car fixup))
2296 (value (lookup-assembler-reference routine)))
2297 (when value
2298 (do-cold-fixup (second fixup) (third fixup) value (fourth fixup))))))
2300 #!+sb-dynamic-core
2301 (progn
2302 (defparameter *dyncore-address* sb!vm::linkage-table-space-start)
2303 (defparameter *dyncore-linkage-keys* nil)
2304 (defparameter *dyncore-table* (make-hash-table :test 'equal))
2306 (defun dyncore-note-symbol (symbol-name datap)
2307 "Register a symbol and return its address in proto-linkage-table."
2308 (let ((key (cons symbol-name datap)))
2309 (symbol-macrolet ((entry (gethash key *dyncore-table*)))
2310 (or entry
2311 (setf entry
2312 (prog1 *dyncore-address*
2313 (push key *dyncore-linkage-keys*)
2314 (incf *dyncore-address* sb!vm::linkage-table-entry-size))))))))
2316 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
2317 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
2318 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
2319 ;;; target-load.lisp refers to.
2320 (defun foreign-symbols-to-core ()
2321 (let ((result *nil-descriptor*))
2322 #!-sb-dynamic-core
2323 (dolist (symbol (sort (%hash-table-alist *cold-foreign-symbol-table*)
2324 #'string< :key #'car))
2325 (cold-push (cold-cons (base-string-to-core (car symbol))
2326 (number-to-core (cdr symbol)))
2327 result))
2328 (cold-set '*!initial-foreign-symbols* result)
2329 #!+sb-dynamic-core
2330 (let ((runtime-linking-list *nil-descriptor*))
2331 (dolist (symbol *dyncore-linkage-keys*)
2332 (cold-push (cold-cons (base-string-to-core (car symbol))
2333 (cdr symbol))
2334 runtime-linking-list))
2335 (cold-set 'sb!vm::*required-runtime-c-symbols*
2336 runtime-linking-list)))
2337 (let ((result *nil-descriptor*))
2338 (dolist (rtn (sort (copy-list *cold-assembler-routines*) #'string< :key #'car))
2339 (cold-push (cold-cons (cold-intern (car rtn))
2340 (number-to-core (cdr rtn)))
2341 result))
2342 (cold-set '*!initial-assembler-routines* result)))
2345 ;;;; general machinery for cold-loading FASL files
2347 (defun pop-fop-stack (stack)
2348 (let ((top (svref stack 0)))
2349 (declare (type index top))
2350 (when (eql 0 top)
2351 (error "FOP stack empty"))
2352 (setf (svref stack 0) (1- top))
2353 (svref stack top)))
2355 ;;; Cause a fop to have a special definition for cold load.
2357 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
2358 ;;; looks up the encoding for this name (created by a previous DEFINE-FOP)
2359 ;;; instead of creating a new encoding.
2360 (defmacro define-cold-fop ((name &optional arglist) &rest forms)
2361 (let* ((code (get name 'opcode))
2362 (argp (plusp (sbit (car **fop-signatures**) (ash code -2))))
2363 (fname (symbolicate "COLD-" name)))
2364 (unless code
2365 (error "~S is not a defined FOP." name))
2366 (when (and argp (not (singleton-p arglist)))
2367 (error "~S must take one argument" name))
2368 `(progn
2369 (defun ,fname (.fasl-input. ,@arglist)
2370 (declare (ignorable .fasl-input.))
2371 (macrolet ((fasl-input () '(the fasl-input .fasl-input.))
2372 (fasl-input-stream () '(%fasl-input-stream (fasl-input)))
2373 (pop-stack ()
2374 '(pop-fop-stack (%fasl-input-stack (fasl-input)))))
2375 ,@forms))
2376 ;; We simply overwrite elements of **FOP-FUNS** since the contents
2377 ;; of the host are never propagated directly into the target core.
2378 ,@(loop for i from code to (logior code (if argp 3 0))
2379 collect `(setf (svref **fop-funs** ,i) #',fname)))))
2381 ;;; Cause a fop to be undefined in cold load.
2382 (defmacro not-cold-fop (name)
2383 `(define-cold-fop (,name)
2384 (error "The fop ~S is not supported in cold load." ',name)))
2386 ;;; COLD-LOAD loads stuff into the core image being built by calling
2387 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
2388 ;;; loading functions.
2389 (defun cold-load (filename)
2390 "Load the file named by FILENAME into the cold load image being built."
2391 (with-open-file (s filename :element-type '(unsigned-byte 8))
2392 (load-as-fasl s nil nil)))
2394 ;;;; miscellaneous cold fops
2396 (define-cold-fop (fop-misc-trap) *unbound-marker*)
2398 (define-cold-fop (fop-character (c))
2399 (make-character-descriptor c))
2401 (define-cold-fop (fop-empty-list) nil)
2402 (define-cold-fop (fop-truth) t)
2404 (define-cold-fop (fop-struct (size)) ; n-words incl. layout, excluding header
2405 (let* ((layout (pop-stack))
2406 (result (allocate-struct *dynamic* layout size))
2407 (metadata
2408 (descriptor-fixnum
2409 (read-slot layout *host-layout-of-layout*
2410 #!-interleaved-raw-slots :n-untagged-slots
2411 #!+interleaved-raw-slots :untagged-bitmap)))
2412 #!-interleaved-raw-slots (ntagged (- size metadata))
2414 ;; Raw slots can not possibly work because dump-struct uses
2415 ;; %RAW-INSTANCE-REF/WORD which does not exist in the cross-compiler.
2416 ;; Remove this assertion if that problem is somehow circumvented.
2417 (unless (= metadata 0)
2418 (error "Raw slots not working in genesis."))
2420 (do ((index 1 (1+ index)))
2421 ((eql index size))
2422 (declare (fixnum index))
2423 (write-wordindexed result
2424 (+ index sb!vm:instance-slots-offset)
2425 (if #!-interleaved-raw-slots (>= index ntagged)
2426 #!+interleaved-raw-slots (logbitp index metadata)
2427 (descriptor-word-sized-integer (pop-stack))
2428 (pop-stack))))
2429 result))
2431 (define-cold-fop (fop-layout)
2432 (let* ((metadata-des (pop-stack))
2433 (length-des (pop-stack))
2434 (depthoid-des (pop-stack))
2435 (cold-inherits (pop-stack))
2436 (name (pop-stack))
2437 (old-layout-descriptor (gethash name *cold-layouts*)))
2438 (declare (type descriptor length-des depthoid-des cold-inherits))
2439 (declare (type symbol name))
2440 ;; If a layout of this name has been defined already
2441 (if old-layout-descriptor
2442 ;; Enforce consistency between the previous definition and the
2443 ;; current definition, then return the previous definition.
2444 (flet ((get-slot (keyword)
2445 (read-slot old-layout-descriptor *host-layout-of-layout* keyword)))
2446 (let ((old-length (descriptor-fixnum (get-slot :length)))
2447 (old-depthoid (descriptor-fixnum (get-slot :depthoid)))
2448 (old-metadata
2449 (descriptor-fixnum
2450 (get-slot #!-interleaved-raw-slots :n-untagged-slots
2451 #!+interleaved-raw-slots :untagged-bitmap)))
2452 (length (descriptor-fixnum length-des))
2453 (depthoid (descriptor-fixnum depthoid-des))
2454 (metadata (descriptor-fixnum metadata-des)))
2455 (unless (= length old-length)
2456 (error "cold loading a reference to class ~S when the compile~%~
2457 time length was ~S and current length is ~S"
2458 name
2459 length
2460 old-length))
2461 (unless (cold-vector-elements-eq cold-inherits (get-slot :inherits))
2462 (error "cold loading a reference to class ~S when the compile~%~
2463 time inherits were ~S~%~
2464 and current inherits are ~S"
2465 name
2466 (listify-cold-inherits cold-inherits)
2467 (listify-cold-inherits (get-slot :inherits))))
2468 (unless (= depthoid old-depthoid)
2469 (error "cold loading a reference to class ~S when the compile~%~
2470 time inheritance depthoid was ~S and current inheritance~%~
2471 depthoid is ~S"
2472 name
2473 depthoid
2474 old-depthoid))
2475 (unless (= metadata old-metadata)
2476 (error "cold loading a reference to class ~S when the compile~%~
2477 time raw-slot-metadata was ~S and is currently ~S"
2478 name
2479 metadata
2480 old-metadata)))
2481 old-layout-descriptor)
2482 ;; Make a new definition from scratch.
2483 (make-cold-layout name length-des cold-inherits depthoid-des
2484 metadata-des))))
2486 ;;;; cold fops for loading symbols
2488 ;;; Load a symbol SIZE characters long from FASL-INPUT, and
2489 ;;; intern that symbol in PACKAGE.
2490 (defun cold-load-symbol (size package fasl-input)
2491 (let ((string (make-string size)))
2492 (read-string-as-bytes (%fasl-input-stream fasl-input) string)
2493 (push-fop-table (intern string package) fasl-input)))
2495 ;; I don't feel like hacking up DEFINE-COLD-FOP any more than necessary,
2496 ;; so this code is handcrafted to accept two operands.
2497 (flet ((fop-cold-symbol-in-package-save (fasl-input index pname-len)
2498 (cold-load-symbol pname-len (ref-fop-table fasl-input index)
2499 fasl-input)))
2500 (dotimes (i 16) ; occupies 16 cells in the dispatch table
2501 (setf (svref **fop-funs** (+ (get 'fop-symbol-in-package-save 'opcode) i))
2502 #'fop-cold-symbol-in-package-save)))
2504 (define-cold-fop (fop-lisp-symbol-save (namelen))
2505 (cold-load-symbol namelen *cl-package* (fasl-input)))
2507 (define-cold-fop (fop-keyword-symbol-save (namelen))
2508 (cold-load-symbol namelen *keyword-package* (fasl-input)))
2510 (define-cold-fop (fop-uninterned-symbol-save (namelen))
2511 (let ((name (make-string namelen)))
2512 (read-string-as-bytes (fasl-input-stream) name)
2513 (push-fop-table (get-uninterned-symbol name) (fasl-input))))
2515 (define-cold-fop (fop-copy-symbol-save (index))
2516 (let* ((symbol (ref-fop-table (fasl-input) index))
2517 (name
2518 (if (symbolp symbol)
2519 (symbol-name symbol)
2520 (base-string-from-core
2521 (read-wordindexed symbol sb!vm:symbol-name-slot)))))
2522 ;; Genesis performs additional coalescing of uninterned symbols
2523 (push-fop-table (get-uninterned-symbol name) (fasl-input))))
2525 ;;;; cold fops for loading packages
2527 (define-cold-fop (fop-named-package-save (namelen))
2528 (let ((name (make-string namelen)))
2529 (read-string-as-bytes (fasl-input-stream) name)
2530 (push-fop-table (find-package name) (fasl-input))))
2532 ;;;; cold fops for loading lists
2534 ;;; Make a list of the top LENGTH things on the fop stack. The last
2535 ;;; cdr of the list is set to LAST.
2536 (defmacro cold-stack-list (length last)
2537 `(do* ((index ,length (1- index))
2538 (result ,last (cold-cons (pop-stack) result)))
2539 ((= index 0) result)
2540 (declare (fixnum index))))
2542 (define-cold-fop (fop-list)
2543 (cold-stack-list (read-byte-arg (fasl-input-stream)) *nil-descriptor*))
2544 (define-cold-fop (fop-list*)
2545 (cold-stack-list (read-byte-arg (fasl-input-stream)) (pop-stack)))
2546 (define-cold-fop (fop-list-1)
2547 (cold-stack-list 1 *nil-descriptor*))
2548 (define-cold-fop (fop-list-2)
2549 (cold-stack-list 2 *nil-descriptor*))
2550 (define-cold-fop (fop-list-3)
2551 (cold-stack-list 3 *nil-descriptor*))
2552 (define-cold-fop (fop-list-4)
2553 (cold-stack-list 4 *nil-descriptor*))
2554 (define-cold-fop (fop-list-5)
2555 (cold-stack-list 5 *nil-descriptor*))
2556 (define-cold-fop (fop-list-6)
2557 (cold-stack-list 6 *nil-descriptor*))
2558 (define-cold-fop (fop-list-7)
2559 (cold-stack-list 7 *nil-descriptor*))
2560 (define-cold-fop (fop-list-8)
2561 (cold-stack-list 8 *nil-descriptor*))
2562 (define-cold-fop (fop-list*-1)
2563 (cold-stack-list 1 (pop-stack)))
2564 (define-cold-fop (fop-list*-2)
2565 (cold-stack-list 2 (pop-stack)))
2566 (define-cold-fop (fop-list*-3)
2567 (cold-stack-list 3 (pop-stack)))
2568 (define-cold-fop (fop-list*-4)
2569 (cold-stack-list 4 (pop-stack)))
2570 (define-cold-fop (fop-list*-5)
2571 (cold-stack-list 5 (pop-stack)))
2572 (define-cold-fop (fop-list*-6)
2573 (cold-stack-list 6 (pop-stack)))
2574 (define-cold-fop (fop-list*-7)
2575 (cold-stack-list 7 (pop-stack)))
2576 (define-cold-fop (fop-list*-8)
2577 (cold-stack-list 8 (pop-stack)))
2579 ;;;; cold fops for loading vectors
2581 (define-cold-fop (fop-base-string (len))
2582 (let ((string (make-string len)))
2583 (read-string-as-bytes (fasl-input-stream) string)
2584 (base-string-to-core string)))
2586 #!+sb-unicode
2587 (define-cold-fop (fop-character-string (len))
2588 (bug "CHARACTER-STRING[~D] dumped by cross-compiler." len))
2590 (define-cold-fop (fop-vector (size))
2591 (let* ((result (allocate-vector-object *dynamic*
2592 sb!vm:n-word-bits
2593 size
2594 sb!vm:simple-vector-widetag)))
2595 (do ((index (1- size) (1- index)))
2596 ((minusp index))
2597 (declare (fixnum index))
2598 (write-wordindexed result
2599 (+ index sb!vm:vector-data-offset)
2600 (pop-stack)))
2601 result))
2603 (define-cold-fop (fop-spec-vector)
2604 (let* ((len (read-word-arg (fasl-input-stream)))
2605 (type (read-byte-arg (fasl-input-stream)))
2606 (sizebits (aref **saetp-bits-per-length** type))
2607 (result (progn (aver (< sizebits 255))
2608 (allocate-vector-object *dynamic* sizebits len type)))
2609 (start (+ (descriptor-byte-offset result)
2610 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2611 (end (+ start
2612 (ceiling (* len sizebits)
2613 sb!vm:n-byte-bits))))
2614 (read-bigvec-as-sequence-or-die (descriptor-bytes result)
2615 (fasl-input-stream)
2616 :start start
2617 :end end)
2618 result))
2620 (not-cold-fop fop-array)
2621 #+nil
2622 ;; This code is unexercised. The only use of FOP-ARRAY is from target-dump.
2623 ;; It would be a shame to delete it though, as it might come in handy.
2624 (define-cold-fop (fop-array)
2625 (let* ((rank (read-word-arg (fasl-input-stream)))
2626 (data-vector (pop-stack))
2627 (result (allocate-object *dynamic*
2628 (+ sb!vm:array-dimensions-offset rank)
2629 sb!vm:other-pointer-lowtag)))
2630 (write-header-word result rank sb!vm:simple-array-widetag)
2631 (write-wordindexed result sb!vm:array-fill-pointer-slot *nil-descriptor*)
2632 (write-wordindexed result sb!vm:array-data-slot data-vector)
2633 (write-wordindexed result sb!vm:array-displacement-slot *nil-descriptor*)
2634 (write-wordindexed result sb!vm:array-displaced-p-slot *nil-descriptor*)
2635 (write-wordindexed result sb!vm:array-displaced-from-slot *nil-descriptor*)
2636 (let ((total-elements 1))
2637 (dotimes (axis rank)
2638 (let ((dim (pop-stack)))
2639 (unless (is-fixnum-lowtag (descriptor-lowtag dim))
2640 (error "non-fixnum dimension? (~S)" dim))
2641 (setf total-elements (* total-elements (descriptor-fixnum dim)))
2642 (write-wordindexed result
2643 (+ sb!vm:array-dimensions-offset axis)
2644 dim)))
2645 (write-wordindexed result
2646 sb!vm:array-elements-slot
2647 (make-fixnum-descriptor total-elements)))
2648 result))
2651 ;;;; cold fops for loading numbers
2653 (defmacro define-cold-number-fop (fop &optional arglist)
2654 ;; Invoke the ordinary warm version of this fop to cons the number.
2655 `(define-cold-fop (,fop ,arglist)
2656 (number-to-core (,fop (fasl-input) ,@arglist))))
2658 (define-cold-number-fop fop-single-float)
2659 (define-cold-number-fop fop-double-float)
2660 (define-cold-number-fop fop-word-integer)
2661 (define-cold-number-fop fop-byte-integer)
2662 (define-cold-number-fop fop-complex-single-float)
2663 (define-cold-number-fop fop-complex-double-float)
2664 (define-cold-number-fop fop-integer (n-bytes))
2666 (define-cold-fop (fop-ratio)
2667 (let ((den (pop-stack)))
2668 (number-pair-to-core (pop-stack) den sb!vm:ratio-widetag)))
2670 (define-cold-fop (fop-complex)
2671 (let ((im (pop-stack)))
2672 (number-pair-to-core (pop-stack) im sb!vm:complex-widetag)))
2674 ;;;; cold fops for calling (or not calling)
2676 (not-cold-fop fop-eval)
2677 (not-cold-fop fop-eval-for-effect)
2679 (defvar *load-time-value-counter*)
2681 (flet ((pop-args (fasl-input)
2682 (let ((args)
2683 (stack (%fasl-input-stack fasl-input)))
2684 (dotimes (i (read-byte-arg (%fasl-input-stream fasl-input))
2685 (values (pop-fop-stack stack) args))
2686 (push (pop-fop-stack stack) args))))
2687 (call (fun-name handler-name args)
2688 (acond ((get fun-name handler-name) (apply it args))
2689 (t (error "Can't ~S ~S in cold load" handler-name fun-name)))))
2691 (define-cold-fop (fop-funcall)
2692 (multiple-value-bind (fun args) (pop-args (fasl-input))
2693 (if args
2694 (case fun
2695 (fdefinition
2696 ;; Special form #'F fopcompiles into `(FDEFINITION ,f)
2697 (aver (and (singleton-p args) (symbolp (car args))))
2698 (target-symbol-function (car args)))
2699 (cons (cold-cons (first args) (second args)))
2700 (symbol-global-value (cold-symbol-value (first args)))
2701 (t (call fun :sb-cold-funcall-handler/for-value args)))
2702 (let ((counter *load-time-value-counter*))
2703 (push (cold-list (cold-intern :load-time-value) fun
2704 (number-to-core counter)) *!cold-toplevels*)
2705 (setf *load-time-value-counter* (1+ counter))
2706 (make-descriptor 0 :load-time-value counter)))))
2708 (define-cold-fop (fop-funcall-for-effect)
2709 (multiple-value-bind (fun args) (pop-args (fasl-input))
2710 (if (not args)
2711 (push fun *!cold-toplevels*)
2712 (case fun
2713 (sb!impl::%defun (apply #'cold-fset args))
2714 (sb!kernel::%defstruct
2715 (push args *known-structure-classoids*)
2716 (push (apply #'cold-list (cold-intern 'defstruct) args)
2717 *!cold-toplevels*))
2718 (sb!impl::%defsetf
2719 (push (list-to-core args) *!cold-setf-macros*))
2720 (sb!c::%defconstant
2721 (destructuring-bind (name val . rest) args
2722 (cold-set name (if (symbolp val) (cold-intern val) val))
2723 (push (cold-cons (cold-intern name) (list-to-core rest))
2724 *!cold-defconstants*)))
2725 (set
2726 (aver (= (length args) 2))
2727 (cold-set (first args)
2728 (let ((val (second args)))
2729 (if (symbolp val) (cold-intern val) val))))
2730 (%svset (apply 'cold-svset args))
2731 (t (call fun :sb-cold-funcall-handler/for-effect args)))))))
2733 (defun finalize-load-time-value-noise ()
2734 (cold-set '*!load-time-values*
2735 (allocate-vector-object *dynamic*
2736 sb!vm:n-word-bits
2737 *load-time-value-counter*
2738 sb!vm:simple-vector-widetag)))
2741 ;;;; cold fops for fixing up circularities
2743 (define-cold-fop (fop-rplaca)
2744 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2745 (idx (read-word-arg (fasl-input-stream))))
2746 (write-memory (cold-nthcdr idx obj) (pop-stack))))
2748 (define-cold-fop (fop-rplacd)
2749 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2750 (idx (read-word-arg (fasl-input-stream))))
2751 (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2753 (define-cold-fop (fop-svset)
2754 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2755 (idx (read-word-arg (fasl-input-stream))))
2756 (write-wordindexed obj
2757 (+ idx
2758 (ecase (descriptor-lowtag obj)
2759 (#.sb!vm:instance-pointer-lowtag 1)
2760 (#.sb!vm:other-pointer-lowtag 2)))
2761 (pop-stack))))
2763 (define-cold-fop (fop-structset)
2764 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2765 (idx (read-word-arg (fasl-input-stream))))
2766 (write-wordindexed obj (+ idx sb!vm:instance-slots-offset) (pop-stack))))
2768 (define-cold-fop (fop-nthcdr)
2769 (cold-nthcdr (read-word-arg (fasl-input-stream)) (pop-stack)))
2771 (defun cold-nthcdr (index obj)
2772 (dotimes (i index)
2773 (setq obj (read-wordindexed obj sb!vm:cons-cdr-slot)))
2774 obj)
2776 ;;;; cold fops for loading code objects and functions
2778 (define-cold-fop (fop-note-debug-source)
2779 (let ((debug-source (pop-stack)))
2780 (cold-push debug-source *current-debug-sources*)))
2782 (define-cold-fop (fop-fdefn)
2783 (cold-fdefinition-object (pop-stack)))
2785 (define-cold-fop (fop-known-fun)
2786 (let* ((name (pop-stack))
2787 (fun (cold-fdefn-fun (cold-fdefinition-object name))))
2788 (if (cold-null fun) `(:known-fun . ,name) fun)))
2790 #!-(or x86 x86-64)
2791 (define-cold-fop (fop-sanctify-for-execution)
2792 (pop-stack))
2794 ;;; Setting this variable shows what code looks like before any
2795 ;;; fixups (or function headers) are applied.
2796 #!+sb-show (defvar *show-pre-fixup-code-p* nil)
2798 (defun cold-load-code (fasl-input nconst code-size)
2799 (macrolet ((pop-stack () '(pop-fop-stack (%fasl-input-stack fasl-input))))
2800 (let* ((raw-header-n-words (+ sb!vm:code-constants-offset nconst))
2801 (header-n-words
2802 ;; Note: we round the number of constants up to ensure
2803 ;; that the code vector will be properly aligned.
2804 (round-up raw-header-n-words 2))
2805 (des (allocate-cold-descriptor *dynamic*
2806 (+ (ash header-n-words
2807 sb!vm:word-shift)
2808 code-size)
2809 sb!vm:other-pointer-lowtag)))
2810 (write-header-word des header-n-words sb!vm:code-header-widetag)
2811 (write-wordindexed des
2812 sb!vm:code-code-size-slot
2813 (make-fixnum-descriptor code-size))
2814 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2815 (write-wordindexed des sb!vm:code-debug-info-slot (pop-stack))
2816 (when (oddp raw-header-n-words)
2817 (write-wordindexed des raw-header-n-words (make-descriptor 0)))
2818 (do ((index (1- raw-header-n-words) (1- index)))
2819 ((< index sb!vm:code-constants-offset))
2820 (let ((obj (pop-stack)))
2821 (if (and (consp obj) (eq (car obj) :known-fun))
2822 (push (list* (cdr obj) des index) *deferred-known-fun-refs*)
2823 (write-wordindexed des index obj))))
2824 (let* ((start (+ (descriptor-byte-offset des)
2825 (ash header-n-words sb!vm:word-shift)))
2826 (end (+ start code-size)))
2827 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2828 (%fasl-input-stream fasl-input)
2829 :start start
2830 :end end)
2831 #!+sb-show
2832 (when *show-pre-fixup-code-p*
2833 (format *trace-output*
2834 "~&/raw code from code-fop ~W ~W:~%"
2835 nconst
2836 code-size)
2837 (do ((i start (+ i sb!vm:n-word-bytes)))
2838 ((>= i end))
2839 (format *trace-output*
2840 "/#X~8,'0x: #X~8,'0x~%"
2841 (+ i (gspace-byte-address (descriptor-gspace des)))
2842 (bvref-32 (descriptor-bytes des) i)))))
2843 des)))
2845 (dotimes (i 16) ; occupies 16 cells in the dispatch table
2846 (setf (svref **fop-funs** (+ (get 'fop-code 'opcode) i))
2847 #'cold-load-code))
2849 (defun resolve-deferred-known-funs ()
2850 (dolist (item *deferred-known-fun-refs*)
2851 (let ((fun (cold-fdefn-fun (cold-fdefinition-object (car item)))))
2852 (aver (not (cold-null fun)))
2853 (let ((place (cdr item)))
2854 (write-wordindexed (car place) (cdr place) fun)))))
2856 (define-cold-fop (fop-alter-code (slot))
2857 (let ((value (pop-stack))
2858 (code (pop-stack)))
2859 (write-wordindexed code slot value)))
2861 (defvar *simple-fun-metadata* (make-hash-table :test 'equalp))
2863 ;; Return an expression that can be used to coalesce type-specifiers
2864 ;; and lambda lists attached to simple-funs. It doesn't have to be
2865 ;; a "correct" host representation, just something that preserves EQUAL-ness.
2866 (defun make-equal-comparable-thing (descriptor)
2867 (labels ((recurse (x)
2868 (cond ((cold-null x) (return-from recurse nil))
2869 ((is-fixnum-lowtag (descriptor-lowtag x))
2870 (return-from recurse (descriptor-fixnum x)))
2871 #!+64-bit
2872 ((is-other-immediate-lowtag (descriptor-lowtag x))
2873 (let ((bits (descriptor-bits x)))
2874 (when (= (logand bits sb!vm:widetag-mask)
2875 sb!vm:single-float-widetag)
2876 (return-from recurse `(:ffloat-bits ,bits))))))
2877 (ecase (descriptor-lowtag x)
2878 (#.sb!vm:list-pointer-lowtag
2879 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
2880 (#.sb!vm:other-pointer-lowtag
2881 (ecase (logand (descriptor-bits (read-memory x)) sb!vm:widetag-mask)
2882 (#.sb!vm:symbol-header-widetag
2883 (if (cold-null (read-wordindexed x sb!vm:symbol-package-slot))
2884 (get-or-make-uninterned-symbol
2885 (base-string-from-core
2886 (read-wordindexed x sb!vm:symbol-name-slot)))
2887 (warm-symbol x)))
2888 #!-64-bit
2889 (#.sb!vm:single-float-widetag
2890 `(:ffloat-bits
2891 ,(read-bits-wordindexed x sb!vm:single-float-value-slot)))
2892 (#.sb!vm:double-float-widetag
2893 `(:dfloat-bits
2894 ,(read-bits-wordindexed x sb!vm:double-float-value-slot)
2895 #!-64-bit
2896 ,(read-bits-wordindexed
2897 x (1+ sb!vm:double-float-value-slot))))
2898 (#.sb!vm:bignum-widetag
2899 (bignum-from-core x))
2900 (#.sb!vm:simple-base-string-widetag
2901 (base-string-from-core x))
2902 ;; Why do function lambda lists have simple-vectors in them?
2903 ;; Because we expose all &OPTIONAL and &KEY default forms.
2904 ;; I think this is abstraction leakage, except possibly for
2905 ;; advertised constant defaults of NIL and such.
2906 ;; How one expresses a value as a sexpr should otherwise
2907 ;; be of no concern to a user of the code.
2908 (#.sb!vm:simple-vector-widetag
2909 (vector-from-core x #'recurse))))))
2910 ;; Return a warm symbol whose name is similar to NAME, coaelescing
2911 ;; all occurrences of #:.WHOLE. across all files, e.g.
2912 (get-or-make-uninterned-symbol (name)
2913 (let ((key `(:uninterned-symbol ,name)))
2914 (or (gethash key *simple-fun-metadata*)
2915 (let ((symbol (make-symbol name)))
2916 (setf (gethash key *simple-fun-metadata*) symbol))))))
2917 (recurse descriptor)))
2919 (define-cold-fop (fop-fun-entry)
2920 (let* ((info (pop-stack))
2921 (type (pop-stack))
2922 (arglist (pop-stack))
2923 (name (pop-stack))
2924 (code-object (pop-stack))
2925 (offset (calc-offset code-object (read-word-arg (fasl-input-stream))))
2926 (fn (descriptor-beyond code-object
2927 offset
2928 sb!vm:fun-pointer-lowtag))
2929 (next (read-wordindexed code-object sb!vm:code-entry-points-slot)))
2930 (unless (zerop (logand offset sb!vm:lowtag-mask))
2931 (error "unaligned function entry: ~S at #X~X" name offset))
2932 (write-wordindexed code-object sb!vm:code-entry-points-slot fn)
2933 (write-memory fn
2934 (make-other-immediate-descriptor
2935 (ash offset (- sb!vm:word-shift))
2936 sb!vm:simple-fun-header-widetag))
2937 (write-wordindexed fn
2938 sb!vm:simple-fun-self-slot
2939 ;; KLUDGE: Wiring decisions like this in at
2940 ;; this level ("if it's an x86") instead of a
2941 ;; higher level of abstraction ("if it has such
2942 ;; and such relocation peculiarities (which
2943 ;; happen to be confined to the x86)") is bad.
2944 ;; It would be nice if the code were instead
2945 ;; conditional on some more descriptive
2946 ;; feature, :STICKY-CODE or
2947 ;; :LOAD-GC-INTERACTION or something.
2949 ;; FIXME: The X86 definition of the function
2950 ;; self slot breaks everything object.tex says
2951 ;; about it. (As far as I can tell, the X86
2952 ;; definition makes it a pointer to the actual
2953 ;; code instead of a pointer back to the object
2954 ;; itself.) Ask on the mailing list whether
2955 ;; this is documented somewhere, and if not,
2956 ;; try to reverse engineer some documentation.
2957 #!-(or x86 x86-64)
2958 ;; a pointer back to the function object, as
2959 ;; described in CMU CL
2960 ;; src/docs/internals/object.tex
2962 #!+(or x86 x86-64)
2963 ;; KLUDGE: a pointer to the actual code of the
2964 ;; object, as described nowhere that I can find
2965 ;; -- WHN 19990907
2966 (make-descriptor ; raw bits that look like fixnum
2967 (+ (descriptor-bits fn)
2968 (- (ash sb!vm:simple-fun-code-offset
2969 sb!vm:word-shift)
2970 ;; FIXME: We should mask out the type
2971 ;; bits, not assume we know what they
2972 ;; are and subtract them out this way.
2973 sb!vm:fun-pointer-lowtag))))
2974 (write-wordindexed fn sb!vm:simple-fun-next-slot next)
2975 (write-wordindexed fn sb!vm:simple-fun-name-slot name)
2976 (flet ((coalesce (sexpr) ; a warm symbol or a cold cons tree
2977 (if (symbolp sexpr) ; will be cold-interned automatically
2978 sexpr
2979 (let ((representation (make-equal-comparable-thing sexpr)))
2980 (or (gethash representation *simple-fun-metadata*)
2981 (setf (gethash representation *simple-fun-metadata*)
2982 sexpr))))))
2983 (write-wordindexed fn sb!vm:simple-fun-arglist-slot (coalesce arglist))
2984 (write-wordindexed fn sb!vm:simple-fun-type-slot (coalesce type)))
2985 (write-wordindexed fn sb!vm::simple-fun-info-slot info)
2986 fn))
2988 #!+sb-thread
2989 (define-cold-fop (fop-symbol-tls-fixup)
2990 (let* ((symbol (pop-stack))
2991 (kind (pop-stack))
2992 (code-object (pop-stack)))
2993 (do-cold-fixup code-object
2994 (read-word-arg (fasl-input-stream))
2995 (ensure-symbol-tls-index symbol) kind)
2996 code-object))
2998 (define-cold-fop (fop-foreign-fixup)
2999 (let* ((kind (pop-stack))
3000 (code-object (pop-stack))
3001 (len (read-byte-arg (fasl-input-stream)))
3002 (sym (make-string len)))
3003 (read-string-as-bytes (fasl-input-stream) sym)
3004 #!+sb-dynamic-core
3005 (let ((offset (read-word-arg (fasl-input-stream)))
3006 (value (dyncore-note-symbol sym nil)))
3007 (do-cold-fixup code-object offset value kind))
3008 #!- (and) (format t "Bad non-plt fixup: ~S~S~%" sym code-object)
3009 #!-sb-dynamic-core
3010 (let ((offset (read-word-arg (fasl-input-stream)))
3011 (value (cold-foreign-symbol-address sym)))
3012 (do-cold-fixup code-object offset value kind))
3013 code-object))
3015 #!+linkage-table
3016 (define-cold-fop (fop-foreign-dataref-fixup)
3017 (let* ((kind (pop-stack))
3018 (code-object (pop-stack))
3019 (len (read-byte-arg (fasl-input-stream)))
3020 (sym (make-string len)))
3021 #!-sb-dynamic-core (declare (ignore code-object))
3022 (read-string-as-bytes (fasl-input-stream) sym)
3023 #!+sb-dynamic-core
3024 (let ((offset (read-word-arg (fasl-input-stream)))
3025 (value (dyncore-note-symbol sym t)))
3026 (do-cold-fixup code-object offset value kind)
3027 code-object)
3028 #!-sb-dynamic-core
3029 (progn
3030 (maphash (lambda (k v)
3031 (format *error-output* "~&~S = #X~8X~%" k v))
3032 *cold-foreign-symbol-table*)
3033 (error "shared foreign symbol in cold load: ~S (~S)" sym kind))))
3035 (define-cold-fop (fop-assembler-code)
3036 (let* ((length (read-word-arg (fasl-input-stream)))
3037 (header-n-words
3038 ;; Note: we round the number of constants up to ensure that
3039 ;; the code vector will be properly aligned.
3040 (round-up sb!vm:code-constants-offset 2))
3041 (des (allocate-cold-descriptor *read-only*
3042 (+ (ash header-n-words
3043 sb!vm:word-shift)
3044 length)
3045 sb!vm:other-pointer-lowtag)))
3046 (write-header-word des header-n-words sb!vm:code-header-widetag)
3047 (write-wordindexed des
3048 sb!vm:code-code-size-slot
3049 (make-fixnum-descriptor length))
3050 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
3051 (write-wordindexed des sb!vm:code-debug-info-slot *nil-descriptor*)
3053 (let* ((start (+ (descriptor-byte-offset des)
3054 (ash header-n-words sb!vm:word-shift)))
3055 (end (+ start length)))
3056 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
3057 (fasl-input-stream)
3058 :start start
3059 :end end))
3060 des))
3062 (define-cold-fop (fop-assembler-routine)
3063 (let* ((routine (pop-stack))
3064 (des (pop-stack))
3065 (offset (calc-offset des (read-word-arg (fasl-input-stream)))))
3066 (record-cold-assembler-routine
3067 routine
3068 (+ (logandc2 (descriptor-bits des) sb!vm:lowtag-mask) offset))
3069 des))
3071 (define-cold-fop (fop-assembler-fixup)
3072 (let* ((routine (pop-stack))
3073 (kind (pop-stack))
3074 (code-object (pop-stack))
3075 (offset (read-word-arg (fasl-input-stream))))
3076 (record-cold-assembler-fixup routine code-object offset kind)
3077 code-object))
3079 (define-cold-fop (fop-code-object-fixup)
3080 (let* ((kind (pop-stack))
3081 (code-object (pop-stack))
3082 (offset (read-word-arg (fasl-input-stream)))
3083 (value (descriptor-bits code-object)))
3084 (do-cold-fixup code-object offset value kind)
3085 code-object))
3087 ;;;; sanity checking space layouts
3089 (defun check-spaces ()
3090 ;;; Co-opt type machinery to check for intersections...
3091 (let (types)
3092 (flet ((check (start end space)
3093 (unless (< start end)
3094 (error "Bogus space: ~A" space))
3095 (let ((type (specifier-type `(integer ,start ,end))))
3096 (dolist (other types)
3097 (unless (eq *empty-type* (type-intersection (cdr other) type))
3098 (error "Space overlap: ~A with ~A" space (car other))))
3099 (push (cons space type) types))))
3100 (check sb!vm:read-only-space-start sb!vm:read-only-space-end :read-only)
3101 (check sb!vm:static-space-start sb!vm:static-space-end :static)
3102 #!+gencgc
3103 (check sb!vm:dynamic-space-start sb!vm:dynamic-space-end :dynamic)
3104 #!-gencgc
3105 (progn
3106 (check sb!vm:dynamic-0-space-start sb!vm:dynamic-0-space-end :dynamic-0)
3107 (check sb!vm:dynamic-1-space-start sb!vm:dynamic-1-space-end :dynamic-1))
3108 #!+linkage-table
3109 (check sb!vm:linkage-table-space-start sb!vm:linkage-table-space-end :linkage-table))))
3111 ;;;; emitting C header file
3113 (defun tailwise-equal (string tail)
3114 (and (>= (length string) (length tail))
3115 (string= string tail :start1 (- (length string) (length tail)))))
3117 (defun write-boilerplate ()
3118 (format t "/*~%")
3119 (dolist (line
3120 '("This is a machine-generated file. Please do not edit it by hand."
3121 "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
3123 "This file contains low-level information about the"
3124 "internals of a particular version and configuration"
3125 "of SBCL. It is used by the C compiler to create a runtime"
3126 "support environment, an executable program in the host"
3127 "operating system's native format, which can then be used to"
3128 "load and run 'core' files, which are basically programs"
3129 "in SBCL's own format."))
3130 (format t " *~@[ ~A~]~%" line))
3131 (format t " */~%"))
3133 (defun c-name (string &optional strip)
3134 (delete #\+
3135 (substitute-if #\_ (lambda (c) (member c '(#\- #\/ #\%)))
3136 (remove-if (lambda (c) (position c strip))
3137 string))))
3139 (defun c-symbol-name (symbol &optional strip)
3140 (c-name (symbol-name symbol) strip))
3142 (defun write-makefile-features ()
3143 ;; propagating *SHEBANG-FEATURES* into the Makefiles
3144 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
3145 sb-cold:*shebang-features*)
3146 #'string<))
3147 (format t "LISP_FEATURE_~A=1~%" shebang-feature-name)))
3149 (defun write-config-h ()
3150 ;; propagating *SHEBANG-FEATURES* into C-level #define's
3151 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
3152 sb-cold:*shebang-features*)
3153 #'string<))
3154 (format t "#define LISP_FEATURE_~A~%" shebang-feature-name))
3155 (terpri)
3156 ;; and miscellaneous constants
3157 (format t "#define SBCL_VERSION_STRING ~S~%"
3158 (sb!xc:lisp-implementation-version))
3159 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
3160 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3161 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
3162 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
3163 (format t "#define LISPOBJ(thing) thing~2%")
3164 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")
3165 (terpri))
3167 (defun write-constants-h ()
3168 ;; writing entire families of named constants
3169 (let ((constants nil))
3170 (dolist (package-name '( ;; Even in CMU CL, constants from VM
3171 ;; were automatically propagated
3172 ;; into the runtime.
3173 "SB!VM"
3174 ;; In SBCL, we also propagate various
3175 ;; magic numbers related to file format,
3176 ;; which live here instead of SB!VM.
3177 "SB!FASL"))
3178 (do-external-symbols (symbol (find-package package-name))
3179 (when (constantp symbol)
3180 (let ((name (symbol-name symbol)))
3181 (labels ( ;; shared machinery
3182 (record (string priority suffix)
3183 (push (list string
3184 priority
3185 (symbol-value symbol)
3186 suffix
3187 (documentation symbol 'variable))
3188 constants))
3189 ;; machinery for old-style CMU CL Lisp-to-C
3190 ;; arbitrary renaming, being phased out in favor of
3191 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
3192 ;; renaming
3193 (record-with-munged-name (prefix string priority)
3194 (record (concatenate
3195 'simple-string
3196 prefix
3197 (delete #\- (string-capitalize string)))
3198 priority
3199 ""))
3200 (maybe-record-with-munged-name (tail prefix priority)
3201 (when (tailwise-equal name tail)
3202 (record-with-munged-name prefix
3203 (subseq name 0
3204 (- (length name)
3205 (length tail)))
3206 priority)))
3207 ;; machinery for new-style SBCL Lisp-to-C naming
3208 (record-with-translated-name (priority large)
3209 (record (c-name name) priority
3210 (if large
3211 #!+(and win32 x86-64) "LLU"
3212 #!-(and win32 x86-64) "LU"
3213 "")))
3214 (maybe-record-with-translated-name (suffixes priority &key large)
3215 (when (some (lambda (suffix)
3216 (tailwise-equal name suffix))
3217 suffixes)
3218 (record-with-translated-name priority large))))
3219 (maybe-record-with-translated-name '("-LOWTAG") 0)
3220 (maybe-record-with-translated-name '("-WIDETAG" "-SHIFT") 1)
3221 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
3222 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
3223 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
3224 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
3225 (maybe-record-with-translated-name '("-SIZE" "-INTERRUPTS") 6)
3226 (maybe-record-with-translated-name '("-START" "-END" "-PAGE-BYTES"
3227 "-CARD-BYTES" "-GRANULARITY")
3228 7 :large t)
3229 (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 8)
3230 (maybe-record-with-translated-name '("-CORE-SPACE-ID") 9)
3231 (maybe-record-with-translated-name '("-CORE-SPACE-ID-FLAG") 9)
3232 (maybe-record-with-translated-name '("-GENERATION+") 10))))))
3233 ;; KLUDGE: these constants are sort of important, but there's no
3234 ;; pleasing way to inform the code above about them. So we fake
3235 ;; it for now. nikodemus on #lisp (2004-08-09) suggested simply
3236 ;; exporting every numeric constant from SB!VM; that would work,
3237 ;; but the C runtime would have to be altered to use Lisp-like names
3238 ;; rather than the munged names currently exported. --njf, 2004-08-09
3239 (dolist (c '(sb!vm:n-word-bits sb!vm:n-word-bytes
3240 sb!vm:n-lowtag-bits sb!vm:lowtag-mask
3241 sb!vm:n-widetag-bits sb!vm:widetag-mask
3242 sb!vm:n-fixnum-tag-bits sb!vm:fixnum-tag-mask))
3243 (push (list (c-symbol-name c)
3244 -1 ; invent a new priority
3245 (symbol-value c)
3247 nil)
3248 constants))
3249 ;; One more symbol that doesn't fit into the code above.
3250 (let ((c 'sb!impl::+magic-hash-vector-value+))
3251 (push (list (c-symbol-name c)
3253 (symbol-value c)
3254 #!+(and win32 x86-64) "LLU"
3255 #!-(and win32 x86-64) "LU"
3256 nil)
3257 constants))
3258 (setf constants
3259 (sort constants
3260 (lambda (const1 const2)
3261 (if (= (second const1) (second const2))
3262 (if (= (third const1) (third const2))
3263 (string< (first const1) (first const2))
3264 (< (third const1) (third const2)))
3265 (< (second const1) (second const2))))))
3266 (let ((prev-priority (second (car constants))))
3267 (dolist (const constants)
3268 (destructuring-bind (name priority value suffix doc) const
3269 (unless (= prev-priority priority)
3270 (terpri)
3271 (setf prev-priority priority))
3272 (when (minusp value)
3273 (error "stub: negative values unsupported"))
3274 (format t "#define ~A ~A~A /* 0x~X ~@[ -- ~A ~]*/~%" name value suffix value doc))))
3275 (terpri))
3277 ;; writing information about internal errors
3278 ;; Assembly code needs only the constants for UNDEFINED_[ALIEN_]FUN_ERROR
3279 ;; but to avoid imparting that knowledge here, we'll expose all error
3280 ;; number constants except for OBJECT-NOT-<x>-ERROR ones.
3281 (loop for interr across sb!c:+backend-internal-errors+
3282 for i from 0
3283 when (stringp (car interr))
3284 do (format t "#define ~A ~D~%" (c-symbol-name (cdr interr)) i))
3285 ;; C code needs strings for describe_internal_error()
3286 (format t "#define INTERNAL_ERROR_NAMES ~{\\~%~S~^, ~}~2%"
3287 (map 'list 'sb!kernel::!c-stringify-internal-error
3288 sb!c:+backend-internal-errors+))
3290 ;; I'm not really sure why this is in SB!C, since it seems
3291 ;; conceptually like something that belongs to SB!VM. In any case,
3292 ;; it's needed C-side.
3293 (format t "#define BACKEND_PAGE_BYTES ~DLU~%" sb!c:*backend-page-bytes*)
3295 (terpri)
3297 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
3298 ;; platforms. If we export this from the SB!VM package, it gets
3299 ;; written out as #define trap_PseudoAtomic, which is confusing as
3300 ;; the runtime treats trap_ as the prefix for illegal instruction
3301 ;; type things. We therefore don't export it, but instead do
3302 #!+sparc
3303 (when (boundp 'sb!vm::pseudo-atomic-trap)
3304 (format t
3305 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
3306 sb!vm::pseudo-atomic-trap)
3307 (terpri))
3308 ;; possibly this is another candidate for a rename (to
3309 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
3310 ;; [possibly applicable to other platforms])
3312 #!+sb-safepoint
3313 (format t "#define GC_SAFEPOINT_PAGE_ADDR ((void*)0x~XUL) /* ~:*~A */~%"
3314 sb!vm:gc-safepoint-page-addr)
3316 (dolist (symbol '(sb!vm::float-traps-byte
3317 sb!vm::float-exceptions-byte
3318 sb!vm::float-sticky-bits
3319 sb!vm::float-rounding-mode))
3320 (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
3321 (c-symbol-name symbol)
3322 (sb!xc:byte-position (symbol-value symbol)))
3323 (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
3324 (c-symbol-name symbol)
3325 (sb!xc:mask-field (symbol-value symbol) -1))))
3327 #!+sb-ldb
3328 (defun write-tagnames-h (&optional (out *standard-output*))
3329 (labels
3330 ((pretty-name (symbol strip)
3331 (let ((name (string-downcase symbol)))
3332 (substitute #\Space #\-
3333 (subseq name 0 (- (length name) (length strip))))))
3334 (list-sorted-tags (tail)
3335 (loop for symbol being the external-symbols of "SB!VM"
3336 when (and (constantp symbol)
3337 (tailwise-equal (string symbol) tail))
3338 collect symbol into tags
3339 finally (return (sort tags #'< :key #'symbol-value))))
3340 (write-tags (kind limit ash-count)
3341 (format out "~%static const char *~(~A~)_names[] = {~%"
3342 (subseq kind 1))
3343 (let ((tags (list-sorted-tags kind)))
3344 (dotimes (i limit)
3345 (if (eql i (ash (or (symbol-value (first tags)) -1) ash-count))
3346 (format out " \"~A\"" (pretty-name (pop tags) kind))
3347 (format out " \"unknown [~D]\"" i))
3348 (unless (eql i (1- limit))
3349 (write-string "," out))
3350 (terpri out)))
3351 (write-line "};" out)))
3352 (write-tags "-LOWTAG" sb!vm:lowtag-limit 0)
3353 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
3354 ;; ending with the same 2 bits. (#b10)
3355 (write-tags "-WIDETAG" (ash (1+ sb!vm:widetag-mask) -2) -2))
3356 ;; Inform print_otherptr() of all array types that it's too dumb to print
3357 (let ((array-type-bits (make-array 32 :initial-element 0)))
3358 (flet ((toggle (b)
3359 (multiple-value-bind (ofs bit) (floor b 8)
3360 (setf (aref array-type-bits ofs) (ash 1 bit)))))
3361 (dovector (saetp sb!vm:*specialized-array-element-type-properties*)
3362 (unless (or (typep (sb!vm:saetp-ctype saetp) 'character-set-type)
3363 (eq (sb!vm:saetp-specifier saetp) t))
3364 (toggle (sb!vm:saetp-typecode saetp))
3365 (awhen (sb!vm:saetp-complex-typecode saetp) (toggle it)))))
3366 (format out
3367 "~%static unsigned char unprintable_array_types[32] =~% {~{~d~^,~}};~%"
3368 (coerce array-type-bits 'list)))
3369 (values))
3371 (defun write-primitive-object (obj)
3372 ;; writing primitive object layouts
3373 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3374 (format t
3375 "struct ~A {~%"
3376 (c-name (string-downcase (string (sb!vm:primitive-object-name obj)))))
3377 (when (sb!vm:primitive-object-widetag obj)
3378 (format t " lispobj header;~%"))
3379 (dolist (slot (sb!vm:primitive-object-slots obj))
3380 (format t " ~A ~A~@[[1]~];~%"
3381 (getf (sb!vm:slot-options slot) :c-type "lispobj")
3382 (c-name (string-downcase (string (sb!vm:slot-name slot))))
3383 (sb!vm:slot-rest-p slot)))
3384 (format t "};~2%")
3385 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
3386 (format t "/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
3387 (format t " * so they work directly on tagged addresses. */~2%")
3388 (let ((name (sb!vm:primitive-object-name obj))
3389 (lowtag (or (symbol-value (sb!vm:primitive-object-lowtag obj))
3390 0)))
3391 (dolist (slot (sb!vm:primitive-object-slots obj))
3392 (format t "#define ~A_~A_OFFSET ~D~%"
3393 (c-symbol-name name)
3394 (c-symbol-name (sb!vm:slot-name slot))
3395 (- (* (sb!vm:slot-offset slot) sb!vm:n-word-bytes) lowtag)))
3396 (terpri))
3397 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%"))
3399 (defun write-structure-object (dd)
3400 (flet ((cstring (designator)
3401 (c-name (string-downcase (string designator)))))
3402 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3403 (format t "struct ~A {~%" (cstring (dd-name dd)))
3404 (format t " lispobj header;~%")
3405 ;; "self layout" slots are named '_layout' instead of 'layout' so that
3406 ;; classoid's expressly declared layout isn't renamed as a special-case.
3407 (format t " lispobj _layout;~%")
3408 #!-interleaved-raw-slots
3409 (progn
3410 ;; Note: if the structure has no raw slots, but has an even number of
3411 ;; ordinary slots (incl. layout, sans header), then the last slot gets
3412 ;; named 'raw_slot_paddingN' (not 'paddingN')
3413 ;; The choice of name is mildly disturbing, but harmless.
3414 (dolist (slot (dd-slots dd))
3415 (when (eq t (dsd-raw-type slot))
3416 (format t " lispobj ~A;~%" (cstring (dsd-name slot)))))
3417 (unless (oddp (+ (dd-length dd) (dd-raw-length dd)))
3418 (format t " lispobj raw_slot_padding;~%"))
3419 (dotimes (n (dd-raw-length dd))
3420 (format t " lispobj raw~D;~%" (- (dd-raw-length dd) n 1))))
3421 #!+interleaved-raw-slots
3422 (let ((index 1))
3423 (dolist (slot (dd-slots dd))
3424 (cond ((eq t (dsd-raw-type slot))
3425 (loop while (< index (dsd-index slot))
3427 (format t " lispobj raw_slot_padding~A;~%" index)
3428 (incf index))
3429 (format t " lispobj ~A;~%" (cstring (dsd-name slot)))
3430 (incf index))))
3431 (unless (oddp (dd-length dd))
3432 (format t " lispobj end_padding;~%")))
3433 (format t "};~2%")
3434 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")))
3436 (defun write-static-symbols ()
3437 (dolist (symbol (cons nil sb!vm:*static-symbols*))
3438 ;; FIXME: It would be nice to use longer names than NIL and
3439 ;; (particularly) T in #define statements.
3440 (format t "#define ~A LISPOBJ(0x~X)~%"
3441 ;; FIXME: It would be nice not to need to strip anything
3442 ;; that doesn't get stripped always by C-SYMBOL-NAME.
3443 (c-symbol-name symbol "%*.!")
3444 (if *static* ; if we ran GENESIS
3445 ;; We actually ran GENESIS, use the real value.
3446 (descriptor-bits (cold-intern symbol))
3447 ;; We didn't run GENESIS, so guess at the address.
3448 (+ sb!vm:static-space-start
3449 sb!vm:n-word-bytes
3450 sb!vm:other-pointer-lowtag
3451 (if symbol (sb!vm:static-symbol-offset symbol) 0))))))
3454 ;;;; writing map file
3456 ;;; Write a map file describing the cold load. Some of this
3457 ;;; information is subject to change due to relocating GC, but even so
3458 ;;; it can be very handy when attempting to troubleshoot the early
3459 ;;; stages of cold load.
3460 (defun write-map ()
3461 (let ((*print-pretty* nil)
3462 (*print-case* :upcase))
3463 (format t "assembler routines defined in core image:~2%")
3464 (dolist (routine (sort (copy-list *cold-assembler-routines*) #'<
3465 :key #'cdr))
3466 (format t "~8,'0X: ~S~%" (cdr routine) (car routine)))
3467 (let ((funs nil)
3468 (undefs nil))
3469 (maphash (lambda (name fdefn)
3470 (let ((fun (cold-fdefn-fun fdefn)))
3471 (if (cold-null fun)
3472 (push name undefs)
3473 (let ((addr (read-wordindexed
3474 fdefn sb!vm:fdefn-raw-addr-slot)))
3475 (push (cons name (descriptor-bits addr))
3476 funs)))))
3477 *cold-fdefn-objects*)
3478 (format t "~%~|~%initially defined functions:~2%")
3479 (setf funs (sort funs #'< :key #'cdr))
3480 (dolist (info funs)
3481 (format t "~8,'0X: ~S #X~8,'0X~%" (cdr info) (car info)
3482 (- (cdr info) #x17)))
3483 (format t
3484 "~%~|
3485 (a note about initially undefined function references: These functions
3486 are referred to by code which is installed by GENESIS, but they are not
3487 installed by GENESIS. This is not necessarily a problem; functions can
3488 be defined later, by cold init toplevel forms, or in files compiled and
3489 loaded at warm init, or elsewhere. As long as they are defined before
3490 they are called, everything should be OK. Things are also OK if the
3491 cross-compiler knew their inline definition and used that everywhere
3492 that they were called before the out-of-line definition is installed,
3493 as is fairly common for structure accessors.)
3494 initially undefined function references:~2%")
3496 (setf undefs (sort undefs #'string< :key #'fun-name-block-name))
3497 (dolist (name undefs)
3498 (format t "~8,'0X: ~S~%"
3499 (descriptor-bits (gethash name *cold-fdefn-objects*))
3500 name)))
3502 (format t "~%~|~%layout names:~2%")
3503 (dolist (x (sort-cold-layouts))
3504 (let* ((des (cdr x))
3505 (inherits (read-slot des *host-layout-of-layout* :inherits)))
3506 (format t "~8,'0X: ~S[~D]~%~10T~:S~%" (descriptor-bits des) (car x)
3507 (cold-layout-length des) (listify-cold-inherits inherits))))
3509 (format t "~%~|~%parsed type specifiers:~2%")
3510 (mapc (lambda (cell)
3511 (format t "~X: ~S~%" (descriptor-bits (cdr cell)) (car cell)))
3512 (sort (%hash-table-alist *ctype-cache*) #'<
3513 :key (lambda (x) (descriptor-bits (cdr x))))))
3514 (values))
3516 ;;;; writing core file
3518 (defvar *core-file*)
3519 (defvar *data-page*)
3521 ;;; magic numbers to identify entries in a core file
3523 ;;; (In case you were wondering: No, AFAIK there's no special magic about
3524 ;;; these which requires them to be in the 38xx range. They're just
3525 ;;; arbitrary words, tested not for being in a particular range but just
3526 ;;; for equality. However, if you ever need to look at a .core file and
3527 ;;; figure out what's going on, it's slightly convenient that they're
3528 ;;; all in an easily recognizable range, and displacing the range away from
3529 ;;; zero seems likely to reduce the chance that random garbage will be
3530 ;;; misinterpreted as a .core file.)
3531 (defconstant build-id-core-entry-type-code 3860)
3532 (defconstant new-directory-core-entry-type-code 3861)
3533 (defconstant initial-fun-core-entry-type-code 3863)
3534 (defconstant page-table-core-entry-type-code 3880)
3535 (defconstant end-core-entry-type-code 3840)
3537 (declaim (ftype (function (sb!vm:word) sb!vm:word) write-word))
3538 (defun write-word (num)
3539 (ecase sb!c:*backend-byte-order*
3540 (:little-endian
3541 (dotimes (i sb!vm:n-word-bytes)
3542 (write-byte (ldb (byte 8 (* i 8)) num) *core-file*)))
3543 (:big-endian
3544 (dotimes (i sb!vm:n-word-bytes)
3545 (write-byte (ldb (byte 8 (* (- (1- sb!vm:n-word-bytes) i) 8)) num)
3546 *core-file*))))
3547 num)
3549 (defun advance-to-page ()
3550 (force-output *core-file*)
3551 (file-position *core-file*
3552 (round-up (file-position *core-file*)
3553 sb!c:*backend-page-bytes*)))
3555 (defun output-gspace (gspace)
3556 (force-output *core-file*)
3557 (let* ((posn (file-position *core-file*))
3558 (bytes (* (gspace-free-word-index gspace) sb!vm:n-word-bytes))
3559 (pages (ceiling bytes sb!c:*backend-page-bytes*))
3560 (total-bytes (* pages sb!c:*backend-page-bytes*)))
3562 (file-position *core-file*
3563 (* sb!c:*backend-page-bytes* (1+ *data-page*)))
3564 (format t
3565 "writing ~S byte~:P [~S page~:P] from ~S~%"
3566 total-bytes
3567 pages
3568 gspace)
3569 (force-output)
3571 ;; Note: It is assumed that the GSPACE allocation routines always
3572 ;; allocate whole pages (of size *target-page-size*) and that any
3573 ;; empty gspace between the free pointer and the end of page will
3574 ;; be zero-filled. This will always be true under Mach on machines
3575 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3576 ;; 8K).
3577 (write-bigvec-as-sequence (gspace-bytes gspace)
3578 *core-file*
3579 :end total-bytes
3580 :pad-with-zeros t)
3581 (force-output *core-file*)
3582 (file-position *core-file* posn)
3584 ;; Write part of a (new) directory entry which looks like this:
3585 ;; GSPACE IDENTIFIER
3586 ;; WORD COUNT
3587 ;; DATA PAGE
3588 ;; ADDRESS
3589 ;; PAGE COUNT
3590 (write-word (gspace-identifier gspace))
3591 (write-word (gspace-free-word-index gspace))
3592 (write-word *data-page*)
3593 (multiple-value-bind (floor rem)
3594 (floor (gspace-byte-address gspace) sb!c:*backend-page-bytes*)
3595 (aver (zerop rem))
3596 (write-word floor))
3597 (write-word pages)
3599 (incf *data-page* pages)))
3601 ;;; Create a core file created from the cold loaded image. (This is
3602 ;;; the "initial core file" because core files could be created later
3603 ;;; by executing SAVE-LISP in a running system, perhaps after we've
3604 ;;; added some functionality to the system.)
3605 (declaim (ftype (function (string)) write-initial-core-file))
3606 (defun write-initial-core-file (filename)
3608 (let ((filenamestring (namestring filename))
3609 (*data-page* 0))
3611 (format t
3612 "[building initial core file in ~S: ~%"
3613 filenamestring)
3614 (force-output)
3616 (with-open-file (*core-file* filenamestring
3617 :direction :output
3618 :element-type '(unsigned-byte 8)
3619 :if-exists :rename-and-delete)
3621 ;; Write the magic number.
3622 (write-word core-magic)
3624 ;; Write the build ID.
3625 (write-word build-id-core-entry-type-code)
3626 (let ((build-id (with-open-file (s "output/build-id.tmp")
3627 (read s))))
3628 (declare (type simple-string build-id))
3629 (/show build-id (length build-id))
3630 ;; Write length of build ID record: BUILD-ID-CORE-ENTRY-TYPE-CODE
3631 ;; word, this length word, and one word for each char of BUILD-ID.
3632 (write-word (+ 2 (length build-id)))
3633 (dovector (char build-id)
3634 ;; (We write each character as a word in order to avoid
3635 ;; having to think about word alignment issues in the
3636 ;; sbcl-0.7.8 version of coreparse.c.)
3637 (write-word (sb!xc:char-code char))))
3639 ;; Write the New Directory entry header.
3640 (write-word new-directory-core-entry-type-code)
3641 (write-word 17) ; length = (5 words/space) * 3 spaces + 2 for header.
3643 (output-gspace *read-only*)
3644 (output-gspace *static*)
3645 (output-gspace *dynamic*)
3647 ;; Write the initial function.
3648 (write-word initial-fun-core-entry-type-code)
3649 (write-word 3)
3650 (let* ((cold-name (cold-intern '!cold-init))
3651 (initial-fun
3652 (cold-fdefn-fun (cold-fdefinition-object cold-name))))
3653 (format t
3654 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
3655 (descriptor-bits initial-fun))
3656 (write-word (descriptor-bits initial-fun)))
3658 ;; Write the End entry.
3659 (write-word end-core-entry-type-code)
3660 (write-word 2)))
3662 (format t "done]~%")
3663 (force-output)
3664 (/show "leaving WRITE-INITIAL-CORE-FILE")
3665 (values))
3667 ;;;; the actual GENESIS function
3669 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3670 ;;; and/or information about a Lisp core, therefrom.
3672 ;;; input file arguments:
3673 ;;; SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
3674 ;;; *tab* *characters* *converted* *to* *spaces*. (We push
3675 ;;; responsibility for removing tabs out to the caller it's
3676 ;;; trivial to remove them using UNIX command line tools like
3677 ;;; sed, whereas it's a headache to do it portably in Lisp because
3678 ;;; #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
3679 ;;; a core file cannot be built (but a C header file can be).
3681 ;;; output files arguments (any of which may be NIL to suppress output):
3682 ;;; CORE-FILE-NAME gets a Lisp core.
3683 ;;; C-HEADER-FILE-NAME gets a C header file, traditionally called
3684 ;;; internals.h, which is used by the C compiler when constructing
3685 ;;; the executable which will load the core.
3686 ;;; MAP-FILE-NAME gets (?) a map file. (dunno about this -- WHN 19990815)
3688 ;;; FIXME: GENESIS doesn't belong in SB!VM. Perhaps in %KERNEL for now,
3689 ;;; perhaps eventually in SB-LD or SB-BOOT.
3690 (defun sb!vm:genesis (&key
3691 object-file-names
3692 symbol-table-file-name
3693 core-file-name
3694 map-file-name
3695 c-header-dir-name
3696 #+nil (list-objects t))
3697 #!+sb-dynamic-core
3698 (declare (ignorable symbol-table-file-name))
3700 (format t
3701 "~&beginning GENESIS, ~A~%"
3702 (if core-file-name
3703 ;; Note: This output summarizing what we're doing is
3704 ;; somewhat telegraphic in style, not meant to imply that
3705 ;; we're not e.g. also creating a header file when we
3706 ;; create a core.
3707 (format nil "creating core ~S" core-file-name)
3708 (format nil "creating headers in ~S" c-header-dir-name)))
3710 (let ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
3712 #!-sb-dynamic-core
3713 (when core-file-name
3714 (if symbol-table-file-name
3715 (load-cold-foreign-symbol-table symbol-table-file-name)
3716 (error "can't output a core file without symbol table file input")))
3718 #!+sb-dynamic-core
3719 (progn
3720 (setf (gethash (extern-alien-name "undefined_tramp")
3721 *cold-foreign-symbol-table*)
3722 (dyncore-note-symbol "undefined_tramp" nil))
3723 (dyncore-note-symbol "undefined_alien_function" nil))
3725 ;; Now that we've successfully read our only input file (by
3726 ;; loading the symbol table, if any), it's a good time to ensure
3727 ;; that there'll be someplace for our output files to go when
3728 ;; we're done.
3729 (flet ((frob (filename)
3730 (when filename
3731 (ensure-directories-exist filename :verbose t))))
3732 (frob core-file-name)
3733 (frob map-file-name))
3735 ;; (This shouldn't matter in normal use, since GENESIS normally
3736 ;; only runs once in any given Lisp image, but it could reduce
3737 ;; confusion if we ever experiment with running, tweaking, and
3738 ;; rerunning genesis interactively.)
3739 (do-all-symbols (sym)
3740 (remprop sym 'cold-intern-info))
3742 (check-spaces)
3744 (let* ((*foreign-symbol-placeholder-value* (if core-file-name nil 0))
3745 (*load-time-value-counter* 0)
3746 (*cold-fdefn-objects* (make-hash-table :test 'equal))
3747 (*cold-symbols* (make-hash-table :test 'eql)) ; integer keys
3748 (*cold-package-symbols* (make-hash-table :test 'equal)) ; string keys
3749 (pkg-metadata (sb-cold::package-list-for-genesis))
3750 (*read-only* (make-gspace :read-only
3751 read-only-core-space-id
3752 sb!vm:read-only-space-start))
3753 (*static* (make-gspace :static
3754 static-core-space-id
3755 sb!vm:static-space-start))
3756 (*dynamic* (make-gspace :dynamic
3757 dynamic-core-space-id
3758 #!+gencgc sb!vm:dynamic-space-start
3759 #!-gencgc sb!vm:dynamic-0-space-start))
3760 ;; There's a cyclic dependency here: NIL refers to a package;
3761 ;; a package needs its layout which needs others layouts
3762 ;; which refer to NIL, which refers to a package ...
3763 ;; Break the cycle by preallocating packages without a layout.
3764 ;; This avoids having to track any symbols created prior to
3765 ;; creation of packages, since packages are primordial.
3766 (target-cl-pkg-info
3767 (dolist (name (list* "COMMON-LISP" "COMMON-LISP-USER" "KEYWORD"
3768 (mapcar #'sb-cold:package-data-name
3769 pkg-metadata))
3770 (gethash "COMMON-LISP" *cold-package-symbols*))
3771 (setf (gethash name *cold-package-symbols*)
3772 (cons (allocate-struct
3773 *dynamic* (make-fixnum-descriptor 0)
3774 (layout-length (find-layout 'package)))
3775 (cons nil nil))))) ; (externals . internals)
3776 (*nil-descriptor* (make-nil-descriptor target-cl-pkg-info))
3777 (*known-structure-classoids* nil)
3778 (*classoid-cells* (make-hash-table :test 'eq))
3779 (*ctype-cache* (make-hash-table :test 'equal))
3780 (*!cold-defconstants* nil)
3781 (*!cold-defuns* nil)
3782 (*!cold-setf-macros* nil)
3783 (*!cold-toplevels* nil)
3784 (*current-debug-sources* *nil-descriptor*)
3785 (*unbound-marker* (make-other-immediate-descriptor
3787 sb!vm:unbound-marker-widetag))
3788 *cold-assembler-fixups*
3789 *cold-assembler-routines*
3790 (*deferred-known-fun-refs* nil)
3791 #!+x86 (*load-time-code-fixups* (make-hash-table)))
3793 ;; Prepare for cold load.
3794 (initialize-non-nil-symbols)
3795 (initialize-layouts)
3796 (initialize-packages
3797 ;; docstrings are set in src/cold/warm. It would work to do it here,
3798 ;; but seems preferable not to saddle Genesis with such responsibility.
3799 (list* (sb-cold:make-package-data :name "COMMON-LISP" :doc nil)
3800 (sb-cold:make-package-data :name "KEYWORD" :doc nil)
3801 (sb-cold:make-package-data :name "COMMON-LISP-USER" :doc nil
3802 :use '("COMMON-LISP"
3803 ;; ANSI encourages us to put extension packages
3804 ;; in the USE list of COMMON-LISP-USER.
3805 "SB!ALIEN" "SB!DEBUG" "SB!EXT" "SB!GRAY" "SB!PROFILE"))
3806 pkg-metadata))
3807 (initialize-static-fns)
3809 ;; Initialize the *COLD-SYMBOLS* system with the information
3810 ;; from common-lisp-exports.lisp-expr.
3811 ;; Packages whose names match SB!THING were set up on the host according
3812 ;; to "package-data-list.lisp-expr" which expresses the desired target
3813 ;; package configuration, so we can just mirror the host into the target.
3814 ;; But by waiting to observe calls to COLD-INTERN that occur during the
3815 ;; loading of the cross-compiler's outputs, it is possible to rid the
3816 ;; target of accidental leftover symbols, not that it wouldn't also be
3817 ;; a good idea to clean up package-data-list once in a while.
3818 (dolist (exported-name
3819 (sb-cold:read-from-file "common-lisp-exports.lisp-expr"))
3820 (cold-intern (intern exported-name *cl-package*) :access :external))
3822 ;; Create SB!KERNEL::*TYPE-CLASSES* as an array of NIL
3823 (cold-set (cold-intern 'sb!kernel::*type-classes*)
3824 (vector-in-core (make-list (length sb!kernel::*type-classes*))))
3826 ;; Cold load.
3827 (dolist (file-name object-file-names)
3828 (write-line (namestring file-name))
3829 (cold-load file-name))
3831 (when *known-structure-classoids*
3832 (let ((dd-layout (find-layout 'defstruct-description)))
3833 (dolist (defstruct-args *known-structure-classoids*)
3834 (let* ((dd (first defstruct-args))
3835 (name (warm-symbol (read-slot dd dd-layout :name)))
3836 (layout (gethash name *cold-layouts*)))
3837 (aver layout)
3838 (write-slots layout *host-layout-of-layout* :info dd))))
3839 (format t "~&; SB!Loader: (~D+~D+~D+~D+~D) ~
3840 structs/consts/funs/setf-macros/other~%"
3841 (length *known-structure-classoids*)
3842 (length *!cold-defconstants*)
3843 (length *!cold-defuns*)
3844 (length *!cold-setf-macros*)
3845 (length *!cold-toplevels*)))
3847 (dolist (symbol '(*!cold-defconstants* *!cold-defuns*
3848 *!cold-setf-macros* *!cold-toplevels*))
3849 (cold-set symbol (list-to-core (nreverse (symbol-value symbol))))
3850 (makunbound symbol)) ; so no further PUSHes can be done
3852 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3853 (resolve-deferred-known-funs)
3854 (resolve-assembler-fixups)
3855 #!+x86 (output-load-time-code-fixups)
3856 (foreign-symbols-to-core)
3857 (finish-symbols)
3858 (/show "back from FINISH-SYMBOLS")
3859 (finalize-load-time-value-noise)
3861 ;; Tell the target Lisp how much stuff we've allocated.
3862 (cold-set 'sb!vm:*read-only-space-free-pointer*
3863 (allocate-cold-descriptor *read-only*
3865 sb!vm:even-fixnum-lowtag))
3866 (cold-set 'sb!vm:*static-space-free-pointer*
3867 (allocate-cold-descriptor *static*
3869 sb!vm:even-fixnum-lowtag))
3870 (/show "done setting free pointers")
3872 ;; Write results to files.
3874 ;; FIXME: I dislike this approach of redefining
3875 ;; *STANDARD-OUTPUT* instead of putting the new stream in a
3876 ;; lexical variable, and it's annoying to have WRITE-MAP (to
3877 ;; *STANDARD-OUTPUT*) not be parallel to WRITE-INITIAL-CORE-FILE
3878 ;; (to a stream explicitly passed as an argument).
3879 (macrolet ((out-to (name &body body)
3880 `(let ((fn (format nil "~A/~A.h" c-header-dir-name ,name)))
3881 (ensure-directories-exist fn)
3882 (with-open-file (*standard-output* fn
3883 :if-exists :supersede :direction :output)
3884 (write-boilerplate)
3885 (let ((n (c-name (string-upcase ,name))))
3886 (format
3888 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~A 1~%"
3889 n n))
3890 ,@body
3891 (format t
3892 "#endif /* SBCL_GENESIS_~A */~%"
3893 (string-upcase ,name))))))
3894 (when map-file-name
3895 (with-open-file (*standard-output* map-file-name
3896 :direction :output
3897 :if-exists :supersede)
3898 (write-map)))
3899 (out-to "config" (write-config-h))
3900 (out-to "constants" (write-constants-h))
3901 #!+sb-ldb
3902 (out-to "tagnames" (write-tagnames-h))
3903 (let ((structs (sort (copy-list sb!vm:*primitive-objects*) #'string<
3904 :key (lambda (obj)
3905 (symbol-name
3906 (sb!vm:primitive-object-name obj))))))
3907 (dolist (obj structs)
3908 (out-to
3909 (string-downcase (string (sb!vm:primitive-object-name obj)))
3910 (write-primitive-object obj)))
3911 (out-to "primitive-objects"
3912 (dolist (obj structs)
3913 (format t "~&#include \"~A.h\"~%"
3914 (string-downcase
3915 (string (sb!vm:primitive-object-name obj)))))))
3916 (dolist (class '(hash-table
3917 classoid
3918 layout
3919 sb!c::compiled-debug-info
3920 sb!c::compiled-debug-fun
3921 package))
3922 (out-to
3923 (string-downcase (string class))
3924 (write-structure-object
3925 (layout-info (find-layout class)))))
3926 (out-to "static-symbols" (write-static-symbols))
3928 (let ((fn (format nil "~A/Makefile.features" c-header-dir-name)))
3929 (ensure-directories-exist fn)
3930 (with-open-file (*standard-output* fn :if-exists :supersede
3931 :direction :output)
3932 (write-makefile-features)))
3934 (when core-file-name
3935 (write-initial-core-file core-file-name))))))
3937 ;;; Invert the action of HOST-CONSTANT-TO-CORE. If STRICTP is given as NIL,
3938 ;;; then we can produce a host object even if it is not a faithful rendition.
3939 (defun host-object-from-core (descriptor &optional (strictp t))
3940 (named-let recurse ((x descriptor))
3941 (when (cold-null x)
3942 (return-from recurse nil))
3943 (when (eq (descriptor-gspace x) :load-time-value)
3944 (error "Can't warm a deferred LTV placeholder"))
3945 (when (is-fixnum-lowtag (descriptor-lowtag x))
3946 (return-from recurse (descriptor-fixnum x)))
3947 (ecase (descriptor-lowtag x)
3948 (#.sb!vm:list-pointer-lowtag
3949 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
3950 (#.sb!vm:fun-pointer-lowtag
3951 (if strictp
3952 (error "Can't map cold-fun -> warm-fun")
3953 (let ((name (read-wordindexed x sb!vm:simple-fun-name-slot)))
3954 `(function ,(recurse name)))))
3955 (#.sb!vm:other-pointer-lowtag
3956 (let ((widetag (logand (descriptor-bits (read-memory x))
3957 sb!vm:widetag-mask)))
3958 (ecase widetag
3959 (#.sb!vm:symbol-header-widetag
3960 (if strictp
3961 (warm-symbol x)
3962 (or (gethash (descriptor-bits x) *cold-symbols*) ; first try
3963 (make-symbol
3964 (recurse (read-wordindexed x sb!vm:symbol-name-slot))))))
3965 (#.sb!vm:simple-base-string-widetag (base-string-from-core x))
3966 (#.sb!vm:simple-vector-widetag (vector-from-core x #'recurse))
3967 (#.sb!vm:bignum-widetag (bignum-from-core x))))))))