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