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