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