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