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