Put assembly routines in immobile space if it exists
[sbcl.git] / src / compiler / generic / genesis.lisp
blobc26685ba13a243327265690abd4a1708aedb663a
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 (defvar *cold-package-gspace*
1119 (or #!+(and immobile-space 64-bit) '*immobile-fixedobj* '*dynamic*))
1120 (declaim (ftype (function (symbol descriptor descriptor descriptor descriptor)
1121 descriptor)
1122 make-cold-layout))
1123 (defun make-cold-layout (name length inherits depthoid bitmap)
1124 (let ((result (allocate-struct (symbol-value *cold-layout-gspace*) *layout-layout*
1125 target-layout-length t)))
1126 ;; Don't set the CLOS hash value: done in cold-init instead.
1128 ;; Set other slot values.
1130 ;; leave CLASSOID uninitialized for now
1131 (multiple-value-call
1132 #'write-slots result *host-layout-of-layout*
1133 :invalid *nil-descriptor*
1134 :inherits inherits
1135 :depthoid depthoid
1136 :length length
1137 :%flags (let* ((inherit-names (listify-cold-inherits inherits))
1138 (second (second inherit-names)))
1139 (make-fixnum-descriptor
1140 ;; Note similarity to FOP-LAYOUT here, but with extra
1141 ;; test for the subtree roots.
1142 (cond ((or (eq second 'structure-object) (eq name 'structure-object))
1143 +structure-layout-flag+)
1144 ((or (eq second 'condition) (eq name 'condition))
1145 +condition-layout-flag+)
1146 (t 0))))
1147 :info *nil-descriptor*
1148 :bitmap bitmap
1149 ;; Nothing in cold-init needs to call EQUALP on a structure with raw slots,
1150 ;; but for type-correctness this slot needs to be a simple-vector.
1151 :equalp-tests *simple-vector-0-descriptor*
1152 :source-location *nil-descriptor*
1153 :slot-list *nil-descriptor*
1154 (if (member name '(null list symbol))
1155 ;; Assign an empty slot-table. Why this is done only for three
1156 ;; classoids is ... too complicated to explain here in a few words,
1157 ;; but revision 18c239205d9349abc017b07e7894a710835c5205 broke it.
1158 ;; Keep this in sync with MAKE-SLOT-TABLE in pcl/slots-boot.
1159 (values :slot-table (if (boundp '*vacuous-slot-table*)
1160 *vacuous-slot-table*
1161 (setq *vacuous-slot-table*
1162 (host-constant-to-core '#(1 nil)))))
1163 (values)))
1165 (setf (gethash (descriptor-bits result) *cold-layout-names*) name
1166 (gethash name *cold-layouts*) result)))
1168 (defun predicate-for-specializer (type-name)
1169 (let ((classoid (find-classoid type-name nil)))
1170 (typecase classoid
1171 (structure-classoid
1172 (cond ((dd-predicate-name (layout-info (classoid-layout classoid))))
1173 ;; All early INSTANCEs should be STRUCTURE-OBJECTs.
1174 ;; Except: see hack for CONDITIONs in CLASS-DEPTHOID.
1175 ((eq type-name 'structure-object) 'sb!kernel:%instancep)))
1176 (built-in-classoid
1177 (let ((translation (specifier-type type-name)))
1178 (aver (not (contains-unknown-type-p translation)))
1179 (let ((predicate (find translation sb!c::*backend-type-predicates*
1180 :test #'type= :key #'car)))
1181 (cond (predicate (cdr predicate))
1182 ((eq type-name 'stream) 'streamp)
1183 ((eq type-name 't) 'sb!int:constantly-t)
1184 (t (error "No predicate for builtin: ~S" type-name))))))
1185 (null
1186 #+nil (format t "~&; PREDICATE-FOR-SPECIALIZER: no classoid for ~S~%"
1187 type-name)
1188 (case type-name
1189 (condition 'sb!kernel::!condition-p))))))
1191 ;;; Convert SPECIFIER (equivalently OBJ) to its representation as a ctype
1192 ;;; in the cold core.
1193 (defvar *ctype-cache*)
1195 (defvar *ctype-nullified-slots* nil)
1196 (defvar *built-in-classoid-nullified-slots* nil)
1198 ;; This function is memoized because it's essentially a constant,
1199 ;; but *nil-descriptor* isn't initialized by the time it's defined.
1200 (defun get-exceptional-slots (obj-type)
1201 (flet ((index (classoid-name slot-name)
1202 (dsd-index (find slot-name
1203 (dd-slots (find-defstruct-description classoid-name))
1204 :key #'dsd-name))))
1205 (case obj-type
1206 (built-in-classoid
1207 (or *built-in-classoid-nullified-slots*
1208 (setq *built-in-classoid-nullified-slots*
1209 (append (get-exceptional-slots 'ctype)
1210 (list (cons (index 'built-in-classoid 'sb!kernel::subclasses)
1211 *nil-descriptor*)
1212 (cons (index 'built-in-classoid 'layout)
1213 *nil-descriptor*))))))
1215 (or *ctype-nullified-slots*
1216 (setq *ctype-nullified-slots*
1217 (list (cons (index 'ctype 'sb!kernel::class-info)
1218 *nil-descriptor*))))))))
1220 (defun ctype-to-core (specifier obj)
1221 (declare (type ctype obj))
1222 (if (classoid-p obj)
1223 (let* ((cell (cold-find-classoid-cell (classoid-name obj) :create t))
1224 (cold-classoid
1225 (read-slot cell (find-layout 'sb!kernel::classoid-cell) :classoid)))
1226 (unless (cold-null cold-classoid)
1227 (return-from ctype-to-core cold-classoid)))
1228 ;; CTYPEs can't be TYPE=-hashed, but specifiers can be EQUAL-hashed.
1229 ;; Don't check the cache for classoids though; that would be wrong.
1230 ;; e.g. named-type T and classoid T both unparse to T.
1231 (awhen (gethash specifier *ctype-cache*)
1232 (return-from ctype-to-core it)))
1233 (let ((result
1234 (ctype-to-core-helper
1236 (lambda (obj)
1237 (typecase obj
1238 (xset (ctype-to-core-helper obj nil nil))
1239 (ctype (ctype-to-core (type-specifier obj) obj))))
1240 (get-exceptional-slots (type-of obj)))))
1241 (let ((type-class-vector
1242 (cold-symbol-value 'sb!kernel::*type-classes*))
1243 (index (position (sb!kernel::type-class-info obj)
1244 sb!kernel::*type-classes*)))
1245 ;; Push this instance into the list of fixups for its type class
1246 (cold-svset type-class-vector index
1247 (cold-cons result (cold-svref type-class-vector index))))
1248 (if (classoid-p obj)
1249 ;; Place this classoid into its clasoid-cell.
1250 (let ((cell (cold-find-classoid-cell (classoid-name obj) :create t)))
1251 (write-slots cell (find-layout 'sb!kernel::classoid-cell)
1252 :classoid result))
1253 ;; Otherwise put it in the general cache
1254 (setf (gethash specifier *ctype-cache*) result))
1255 result))
1257 (defun ctype-to-core-helper (obj obj-to-core-helper exceptional-slots)
1258 (let* ((host-type (type-of obj))
1259 (target-layout (or (gethash host-type *cold-layouts*)
1260 (error "No target layout for ~S" obj)))
1261 (result (allocate-struct *dynamic* target-layout))
1262 (cold-dd-slots (dd-slots-from-core host-type)))
1263 (aver (eql (layout-bitmap (find-layout host-type))
1264 sb!kernel::+layout-all-tagged+))
1265 ;; Dump the slots.
1266 (do ((len (cold-layout-length target-layout))
1267 (index sb!vm:instance-data-start (1+ index)))
1268 ((= index len) result)
1269 (write-wordindexed
1270 result
1271 (+ sb!vm:instance-slots-offset index)
1272 (acond ((assq index exceptional-slots) (cdr it))
1273 (t (host-constant-to-core
1274 (funcall (dsd-accessor-from-cold-slots cold-dd-slots index)
1275 obj)
1276 obj-to-core-helper)))))))
1278 ;; This is called to backpatch two small sets of objects:
1279 ;; - layouts created before layout-of-layout is made (3 counting LAYOUT itself)
1280 ;; - a small number of classoid-cells (~ 4).
1281 (defun set-instance-layout (thing layout)
1282 #!+compact-instance-header
1283 ;; High half of the header points to the layout
1284 (write-wordindexed/raw thing 0 (logior (ash (descriptor-bits layout) 32)
1285 (read-bits-wordindexed thing 0)))
1286 #!-compact-instance-header
1287 ;; Word following the header is the layout
1288 (write-wordindexed thing sb!vm:instance-slots-offset layout))
1290 (defun cold-layout-of (cold-struct)
1291 #!+compact-instance-header
1292 (let ((bits (ash (read-bits-wordindexed cold-struct 0) -32)))
1293 (if (zerop bits) *nil-descriptor* (make-random-descriptor bits)))
1294 #!-compact-instance-header
1295 (read-wordindexed cold-struct sb!vm:instance-slots-offset))
1297 (defun initialize-layouts ()
1298 (clrhash *cold-layouts*)
1299 ;; This assertion is due to the fact that MAKE-COLD-LAYOUT does not
1300 ;; know how to set any raw slots.
1301 (aver (eql (layout-bitmap *host-layout-of-layout*)
1302 sb!kernel::+layout-all-tagged+))
1303 (setq *layout-layout* (make-fixnum-descriptor 0))
1304 (flet ((chill-layout (name &rest inherits)
1305 ;; Check that the number of specified INHERITS matches
1306 ;; the length of the layout's inherits in the cross-compiler.
1307 (let ((warm-layout (classoid-layout (find-classoid name))))
1308 (assert (eql (length (layout-inherits warm-layout))
1309 (length inherits)))
1310 (make-cold-layout
1311 name
1312 (number-to-core (layout-length warm-layout))
1313 (vector-in-core inherits)
1314 (number-to-core (layout-depthoid warm-layout))
1315 (number-to-core (layout-bitmap warm-layout))))))
1316 (let* ((t-layout (chill-layout 't))
1317 (s-o-layout (chill-layout 'structure-object t-layout)))
1318 (setf *layout-layout* (chill-layout 'layout t-layout s-o-layout))
1319 (dolist (layout (list t-layout s-o-layout *layout-layout*))
1320 (set-instance-layout layout *layout-layout*))
1321 (chill-layout 'function t-layout)
1322 (let* ((sequence (chill-layout 'sequence t-layout))
1323 (list (chill-layout 'list t-layout sequence))
1324 (symbol (chill-layout 'symbol t-layout)))
1325 (chill-layout 'null t-layout sequence list symbol))
1326 (chill-layout 'package t-layout s-o-layout))))
1328 ;;;; interning symbols in the cold image
1330 ;;; a map from package name as a host string to
1331 ;;; ((external-symbols . internal-symbols) . cold-package-descriptor)
1332 (defvar *cold-package-symbols*)
1333 (declaim (type hash-table *cold-package-symbols*))
1335 (setf (get 'find-package :sb-cold-funcall-handler/for-value)
1336 (lambda (descriptor &aux (name (base-string-from-core descriptor)))
1337 (or (cdr (gethash name *cold-package-symbols*))
1338 (error "Genesis could not find a target package named ~S" name))))
1340 (defvar *classoid-cells*)
1341 (defun cold-find-classoid-cell (name &key create)
1342 (aver (eq create t))
1343 (or (gethash name *classoid-cells*)
1344 (let ((layout (gethash 'sb!kernel::classoid-cell *cold-layouts*)) ; ok if nil
1345 (host-layout (find-layout 'sb!kernel::classoid-cell)))
1346 (setf (gethash name *classoid-cells*)
1347 (write-slots (allocate-struct *dynamic* layout
1348 (layout-length host-layout))
1349 host-layout
1350 :name name
1351 :pcl-class *nil-descriptor*
1352 :classoid *nil-descriptor*)))))
1354 (setf (get 'find-classoid-cell :sb-cold-funcall-handler/for-value)
1355 #'cold-find-classoid-cell)
1357 ;;; a map from descriptors to symbols, so that we can back up. The key
1358 ;;; is the address in the target core.
1359 (defvar *cold-symbols*)
1360 (declaim (type hash-table *cold-symbols*))
1362 (defun set-readonly (string) (set-header-data string sb!vm:+vector-shareable+))
1364 (defun initialize-packages ()
1365 (let ((package-data-list
1366 ;; docstrings are set in src/cold/warm. It would work to do it here,
1367 ;; but seems preferable not to saddle Genesis with such responsibility.
1368 (list* (sb-cold:make-package-data :name "COMMON-LISP" :doc nil)
1369 (sb-cold:make-package-data :name "KEYWORD" :doc nil)
1370 ;; ANSI encourages us to put extension packages
1371 ;; in the USE list of COMMON-LISP-USER.
1372 (sb-cold:make-package-data
1373 :name "COMMON-LISP-USER" :doc nil
1374 :use '("COMMON-LISP" "SB!ALIEN" "SB!DEBUG" "SB!EXT" "SB!GRAY" "SB!PROFILE"))
1375 (sb-cold::package-list-for-genesis)))
1376 (package-layout (find-layout 'package))
1377 (target-pkg-list nil))
1378 (labels ((init-cold-package (name &optional docstring)
1379 (let ((cold-package (allocate-struct (symbol-value *cold-package-gspace*)
1380 (gethash 'package *cold-layouts*))))
1381 (setf (gethash name *cold-package-symbols*)
1382 (cons (cons nil nil) cold-package))
1383 ;; Initialize string slots
1384 (write-slots cold-package package-layout
1385 :%name (set-readonly
1386 (base-string-to-core
1387 (target-package-name name)))
1388 :%nicknames (chill-nicknames name)
1389 :doc-string (if docstring
1390 (base-string-to-core docstring)
1391 *nil-descriptor*)
1392 :%use-list *nil-descriptor*)
1393 ;; the cddr of this will accumulate the 'used-by' package list
1394 (push (list name cold-package) target-pkg-list)))
1395 (target-package-name (string)
1396 (if (eql (mismatch string "SB!") 3)
1397 (concatenate 'string "SB-" (subseq string 3))
1398 string))
1399 (chill-nicknames (pkg-name)
1400 ;; Make the package nickname lists for the standard packages
1401 ;; be the minimum specified by ANSI, regardless of what value
1402 ;; the cross-compilation host happens to use.
1403 ;; For packages other than the standard packages, the nickname
1404 ;; list was specified by our package setup code, and we can just
1405 ;; propagate the current state into the target.
1406 (list-to-core
1407 (mapcar #'base-string-to-core
1408 (cond ((string= pkg-name "COMMON-LISP") '("CL"))
1409 ((string= pkg-name "COMMON-LISP-USER")
1410 '("CL-USER"))
1411 ((string= pkg-name "KEYWORD") '())
1413 ;; 'package-data-list' contains no nicknames.
1414 ;; (See comment in 'set-up-cold-packages')
1415 (aver (null (package-nicknames
1416 (find-package pkg-name))))
1417 nil)))))
1418 (find-cold-package (name)
1419 (cadr (find-package-cell name)))
1420 (find-package-cell (name)
1421 (or (assoc (if (string= name "CL") "COMMON-LISP" name)
1422 target-pkg-list :test #'string=)
1423 (error "No cold package named ~S" name))))
1424 ;; pass 1: make all proto-packages
1425 (dolist (pd package-data-list)
1426 (init-cold-package (sb-cold:package-data-name pd)
1427 #!+sb-doc(sb-cold::package-data-doc pd)))
1428 ;; pass 2: set the 'use' lists and collect the 'used-by' lists
1429 (dolist (pd package-data-list)
1430 (let ((this (find-cold-package (sb-cold:package-data-name pd)))
1431 (use nil))
1432 (dolist (that (sb-cold:package-data-use pd))
1433 (let ((cell (find-package-cell that)))
1434 (push (cadr cell) use)
1435 (push this (cddr cell))))
1436 (write-slots this package-layout
1437 :%use-list (list-to-core (nreverse use)))))
1438 ;; pass 3: set the 'used-by' lists
1439 (dolist (cell target-pkg-list)
1440 (write-slots (cadr cell) package-layout
1441 :%used-by-list (list-to-core (cddr cell)))))))
1443 ;;; sanity check for a symbol we're about to create on the target
1445 ;;; Make sure that the symbol has an appropriate package. In
1446 ;;; particular, catch the so-easy-to-make error of typing something
1447 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1448 ;;; need is SB!KERNEL:%BYTE-BLT.
1449 (defun package-ok-for-target-symbol-p (package)
1450 (let ((package-name (package-name package)))
1452 ;; Cold interning things in these standard packages is OK. (Cold
1453 ;; interning things in the other standard package, CL-USER, isn't
1454 ;; OK. We just use CL-USER to expose symbols whose homes are in
1455 ;; other packages. Thus, trying to cold intern a symbol whose
1456 ;; home package is CL-USER probably means that a coding error has
1457 ;; been made somewhere.)
1458 (find package-name '("COMMON-LISP" "KEYWORD") :test #'string=)
1459 ;; Cold interning something in one of our target-code packages,
1460 ;; which are ever-so-rigorously-and-elegantly distinguished by
1461 ;; this prefix on their names, is OK too.
1462 (string= package-name "SB!" :end1 3 :end2 3)
1463 ;; This one is OK too, since it ends up being COMMON-LISP on the
1464 ;; target.
1465 (string= package-name "SB-XC")
1466 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1467 ;; package in the xc host? something we can't think of
1468 ;; a valid reason to cold intern, anyway...)
1471 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1473 ;;; Most host symbols we dump onto the target are created by SBCL
1474 ;;; itself, so that as long as we avoid gratuitously
1475 ;;; cross-compilation-unfriendly hacks, it just happens that their
1476 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1477 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1478 ;;; in the COMMON-LISP package, where we don't get to create the
1479 ;;; symbols but instead have to use the ones that the xc host created.
1480 ;;; In particular, while ANSI specifies which symbols are exported
1481 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1482 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1483 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1484 ;;; symbols in the CLOS package).
1485 (defun symbol-package-for-target-symbol (symbol)
1486 ;; We want to catch weird symbols like CLISP's
1487 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1488 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1489 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1490 (multiple-value-bind (cl-symbol cl-status)
1491 (find-symbol (symbol-name symbol) *cl-package*)
1492 (if (and (eq symbol cl-symbol)
1493 (eq cl-status :external))
1494 ;; special case, to work around possible xc host weirdness
1495 ;; in COMMON-LISP package
1496 *cl-package*
1497 ;; ordinary case
1498 (let ((result (symbol-package symbol)))
1499 (unless (package-ok-for-target-symbol-p result)
1500 (bug "~A in bad package for target: ~A" symbol result))
1501 result))))
1503 (defvar *uninterned-symbol-table* (make-hash-table :test #'equal))
1504 ;; This coalesces references to uninterned symbols, which is allowed because
1505 ;; "similar-as-constant" is defined by string comparison, and since we only have
1506 ;; base-strings during Genesis, there is no concern about upgraded array type.
1507 ;; There is a subtlety of whether coalescing may occur across files
1508 ;; - the target compiler doesn't and couldn't - but here it doesn't matter.
1509 (defun get-uninterned-symbol (name)
1510 (ensure-gethash name *uninterned-symbol-table* (allocate-symbol name)))
1512 ;;; Dump the target representation of HOST-VALUE,
1513 ;;; the type of which is in a restrictive set.
1514 (defun host-constant-to-core (host-value &optional helper)
1515 (let ((visited (make-hash-table :test #'eq)))
1516 (named-let target-representation ((value host-value))
1517 (unless (typep value '(or symbol number descriptor))
1518 (let ((found (gethash value visited)))
1519 (cond ((eq found :pending)
1520 (bug "circular constant?")) ; Circularity not permitted
1521 (found
1522 (return-from target-representation found))))
1523 (setf (gethash value visited) :pending))
1524 (setf (gethash value visited)
1525 (typecase value
1526 (descriptor value)
1527 (symbol (if (symbol-package value)
1528 (cold-intern value)
1529 (get-uninterned-symbol (string value))))
1530 (number (number-to-core value))
1531 (string (base-string-to-core value))
1532 (cons (cold-cons (target-representation (car value))
1533 (target-representation (cdr value))))
1534 (simple-vector
1535 (vector-in-core (map 'list #'target-representation value)))
1537 (or (and helper (funcall helper value))
1538 (error "host-constant-to-core: can't convert ~S"
1539 value))))))))
1541 ;; Look up the target's descriptor for #'FUN where FUN is a host symbol.
1542 (defun target-symbol-function (symbol)
1543 (let ((f (cold-fdefn-fun (cold-fdefinition-object symbol))))
1544 ;; It works only if DEFUN F was seen first.
1545 (aver (not (cold-null f)))
1548 ;;; Return a handle on an interned symbol. If necessary allocate the
1549 ;;; symbol and record its home package.
1550 (defun cold-intern (symbol
1551 &key (access nil)
1552 (gspace (symbol-value *cold-symbol-gspace*))
1553 &aux (package (symbol-package-for-target-symbol symbol)))
1555 ;; Anything on the cross-compilation host which refers to the target
1556 ;; machinery through the host SB-XC package should be translated to
1557 ;; something on the target which refers to the same machinery
1558 ;; through the target COMMON-LISP package.
1559 (let ((p (find-package "SB-XC")))
1560 (when (eq package p)
1561 (setf package *cl-package*))
1562 (when (eq (symbol-package symbol) p)
1563 (setf symbol (intern (symbol-name symbol) *cl-package*))))
1565 (or (get symbol 'cold-intern-info)
1566 (let ((handle (allocate-symbol (symbol-name symbol) :gspace gspace)))
1567 (setf (get symbol 'cold-intern-info) handle)
1568 ;; maintain reverse map from target descriptor to host symbol
1569 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1570 (let ((pkg-info (or (gethash (package-name package) *cold-package-symbols*)
1571 (error "No target package descriptor for ~S" package))))
1572 (write-wordindexed handle sb!vm:symbol-package-slot (cdr pkg-info))
1573 (record-accessibility
1574 (or access (nth-value 1 (find-symbol (symbol-name symbol) package)))
1575 pkg-info handle package symbol))
1576 #!+sb-thread (assign-tls-index-if-needed symbol handle)
1577 (when (eq package *keyword-package*)
1578 (cold-set handle handle))
1579 handle)))
1581 (defun record-accessibility (accessibility target-pkg-info symbol-descriptor
1582 &optional host-package host-symbol)
1583 (let ((access-lists (car target-pkg-info)))
1584 (case accessibility
1585 (:external (push symbol-descriptor (car access-lists)))
1586 (:internal (push symbol-descriptor (cdr access-lists)))
1587 (t (error "~S inaccessible in package ~S" host-symbol host-package)))))
1589 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1590 ;;; It might be nice to put NIL on a readonly page by itself to prevent unsafe
1591 ;;; code from destroying the world with (RPLACx nil 'kablooey)
1592 (defun make-nil-descriptor ()
1593 (let* ((des (allocate-header+object *static* sb!vm:symbol-size 0))
1594 (result (make-descriptor (+ (descriptor-bits des)
1595 (* 2 sb!vm:n-word-bytes)
1596 (- sb!vm:list-pointer-lowtag
1597 sb!vm:other-pointer-lowtag)))))
1598 (write-wordindexed des
1600 (make-other-immediate-descriptor
1602 sb!vm:symbol-widetag))
1603 (write-wordindexed des
1604 (+ 1 sb!vm:symbol-value-slot)
1605 result)
1606 (write-wordindexed des
1607 (+ 2 sb!vm:symbol-value-slot) ; = 1 + symbol-hash-slot
1608 result)
1609 (write-wordindexed des
1610 (+ 1 sb!vm:symbol-info-slot)
1611 (cold-cons result result)) ; NIL's info is (nil . nil)
1612 (write-wordindexed des
1613 (+ 1 sb!vm:symbol-name-slot)
1614 ;; NIL's name is in dynamic space because any extra
1615 ;; bytes allocated in static space would need to
1616 ;; be accounted for by STATIC-SYMBOL-OFFSET.
1617 (set-readonly (base-string-to-core "NIL" *dynamic*)))
1618 (setf (gethash (descriptor-bits result) *cold-symbols*) nil
1619 (get nil 'cold-intern-info) result)))
1621 ;;; Since the initial symbols must be allocated before we can intern
1622 ;;; anything else, we intern those here. We also set the value of T.
1623 (defun initialize-static-space ()
1624 "Initialize the cold load symbol-hacking data structures."
1625 ;; NIL did not have its package assigned. Do that now.
1626 (let ((target-cl-pkg-info (gethash "COMMON-LISP" *cold-package-symbols*)))
1627 ;; -1 is magic having to do with nil-as-cons vs. nil-as-symbol
1628 (write-wordindexed *nil-descriptor* (- sb!vm:symbol-package-slot 1)
1629 (cdr target-cl-pkg-info))
1630 (record-accessibility :external target-cl-pkg-info *nil-descriptor*))
1631 ;; Intern the others.
1632 (dovector (symbol sb!vm:+static-symbols+)
1633 (let* ((des (cold-intern symbol :gspace *static*))
1634 (offset-wanted (sb!vm:static-symbol-offset symbol))
1635 (offset-found (- (descriptor-bits des)
1636 (descriptor-bits *nil-descriptor*))))
1637 (unless (= offset-wanted offset-found)
1638 (error "Offset from ~S to ~S is ~W, not ~W"
1639 symbol
1641 offset-found
1642 offset-wanted))))
1643 ;; Establish the value of T.
1644 (let ((t-symbol (cold-intern t :gspace *static*)))
1645 (cold-set t-symbol t-symbol))
1646 (dolist (sym sb!vm::+c-callable-fdefns+)
1647 (cold-fdefinition-object (cold-intern sym) nil *static*))
1648 (dovector (sym sb!vm:+static-fdefns+)
1649 (let* ((fdefn (cold-fdefinition-object (cold-intern sym) nil *static*))
1650 (offset (- (+ (- (descriptor-bits fdefn)
1651 sb!vm:other-pointer-lowtag)
1652 (* sb!vm:fdefn-raw-addr-slot sb!vm:n-word-bytes))
1653 (descriptor-bits *nil-descriptor*)))
1654 (desired (sb!vm:static-fun-offset sym)))
1655 (unless (= offset desired)
1656 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1657 sym nil offset desired)))))
1659 ;;; Sort *COLD-LAYOUTS* to return them in a deterministic order.
1660 (defun sort-cold-layouts ()
1661 (sort (%hash-table-alist *cold-layouts*) #'<
1662 :key (lambda (x) (descriptor-bits (cdr x)))))
1664 ;;; Establish initial values for magic symbols.
1666 (defun finish-symbols ()
1667 (cold-set 'sb!vm::*current-catch-block* (make-fixnum-descriptor 0))
1668 (cold-set 'sb!vm::*current-unwind-protect-block* (make-fixnum-descriptor 0))
1670 (cold-set '*free-interrupt-context-index* (make-fixnum-descriptor 0))
1672 (cold-set '*!initial-layouts*
1673 (vector-in-core
1674 (mapcar (lambda (layout)
1675 (cold-cons (cold-intern (car layout)) (cdr layout)))
1676 (sort-cold-layouts))))
1678 #!+sb-thread
1679 (cold-set 'sb!vm::*free-tls-index*
1680 (make-descriptor (ash *genesis-tls-counter* sb!vm:word-shift)))
1682 (dolist (symbol sb!impl::*cache-vector-symbols*)
1683 (cold-set symbol *nil-descriptor*))
1685 ;; Symbols for which no call to COLD-INTERN would occur - due to not being
1686 ;; referenced until warm init - must be artificially cold-interned.
1687 ;; Inasmuch as the "offending" things are compiled by ordinary target code
1688 ;; and not cold-init, I think we should use an ordinary DEFPACKAGE for
1689 ;; the added-on bits. What I've done is somewhat of a fragile kludge.
1690 (let (syms)
1691 (with-package-iterator (iter '("SB!PCL" "SB!MOP" "SB!GRAY" "SB!SEQUENCE"
1692 "SB!PROFILE" "SB!EXT" "SB!VM"
1693 "SB!C" "SB!FASL" "SB!DEBUG")
1694 :external)
1695 (loop
1696 (multiple-value-bind (foundp sym accessibility package) (iter)
1697 (declare (ignore accessibility))
1698 (cond ((not foundp) (return))
1699 ((eq (symbol-package sym) package) (push sym syms))))))
1700 (setf syms (stable-sort syms #'string<))
1701 (dolist (sym syms)
1702 (cold-intern sym)))
1704 (cold-set
1705 'sb!impl::*!initial-symbols*
1706 (list-to-core
1707 (mapcar
1708 (lambda (pkgcons)
1709 (destructuring-bind (pkg-name . pkg-info) pkgcons
1710 (let ((shadow
1711 ;; Record shadowing symbols (except from SB-XC) in SB! packages.
1712 (when (eql (mismatch pkg-name "SB!") 3)
1713 ;; Be insensitive to the host's ordering.
1714 (sort (remove (find-package "SB-XC")
1715 (package-shadowing-symbols (find-package pkg-name))
1716 :key #'symbol-package) #'string<))))
1717 (write-slots (cdr pkg-info) ; package
1718 (find-layout 'package)
1719 :%shadowing-symbols (list-to-core
1720 (mapcar 'cold-intern shadow))))
1721 (unless (member pkg-name '("COMMON-LISP" "KEYWORD") :test 'string=)
1722 (let ((host-pkg (find-package pkg-name))
1723 (sb-xc-pkg (find-package "SB-XC"))
1724 syms)
1725 ;; Now for each symbol directly present in this host-pkg,
1726 ;; i.e. accessible but not :INHERITED, figure out if the symbol
1727 ;; came from a different package, and if so, make a note of it.
1728 (with-package-iterator (iter host-pkg :internal :external)
1729 (loop (multiple-value-bind (foundp sym accessibility) (iter)
1730 (unless foundp (return))
1731 (unless (or (eq (symbol-package sym) host-pkg)
1732 (eq (symbol-package sym) sb-xc-pkg))
1733 (push (cons sym accessibility) syms)))))
1734 (dolist (symcons (sort syms #'string< :key #'car))
1735 (destructuring-bind (sym . accessibility) symcons
1736 (record-accessibility accessibility pkg-info (cold-intern sym)
1737 host-pkg sym)))))
1738 (cold-list (cdr pkg-info)
1739 (vector-in-core (caar pkg-info))
1740 (vector-in-core (cdar pkg-info)))))
1741 (sort (%hash-table-alist *cold-package-symbols*)
1742 #'string< :key #'car)))) ; Sort by package-name
1744 (dump-symbol-info-vectors
1745 (attach-fdefinitions-to-symbols
1746 (attach-classoid-cells-to-symbols (make-hash-table :test #'eq))))
1748 #!+x86
1749 (progn
1750 (cold-set 'sb!vm::*fp-constant-0d0* (number-to-core 0d0))
1751 (cold-set 'sb!vm::*fp-constant-1d0* (number-to-core 1d0))
1752 (cold-set 'sb!vm::*fp-constant-0f0* (number-to-core 0f0))
1753 (cold-set 'sb!vm::*fp-constant-1f0* (number-to-core 1f0))))
1755 ;;;; functions and fdefinition objects
1757 ;;; a hash table mapping from fdefinition names to descriptors of cold
1758 ;;; objects
1760 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1761 ;;; we want to have only one entry per name, this must be an 'EQUAL
1762 ;;; hash table, not the default 'EQL.
1763 (defvar *cold-fdefn-objects*)
1765 ;;; Given a cold representation of a symbol, return a warm
1766 ;;; representation.
1767 (defun warm-symbol (des)
1768 ;; Note that COLD-INTERN is responsible for keeping the
1769 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1770 ;; uninterned symbol, the code below will fail. But as long as we
1771 ;; don't need to look up uninterned symbols during bootstrapping,
1772 ;; that's OK..
1773 (multiple-value-bind (symbol found-p)
1774 (gethash (descriptor-bits des) *cold-symbols*)
1775 (declare (type symbol symbol))
1776 (unless found-p
1777 (error "no warm symbol"))
1778 symbol))
1780 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1781 (defun cold-car (des)
1782 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1783 (read-wordindexed des sb!vm:cons-car-slot))
1784 (defun cold-cdr (des)
1785 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1786 (read-wordindexed des sb!vm:cons-cdr-slot))
1787 (defun cold-rplacd (des newval)
1788 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1789 (write-wordindexed des sb!vm:cons-cdr-slot newval)
1790 des)
1791 (defun cold-null (des) (descriptor= des *nil-descriptor*))
1793 ;;; Given a cold representation of a function name, return a warm
1794 ;;; representation.
1795 (declaim (ftype (function ((or symbol descriptor)) (or symbol list)) warm-fun-name))
1796 (defun warm-fun-name (des)
1797 (let ((result
1798 (if (symbolp des)
1799 ;; This parallels the logic at the start of COLD-INTERN
1800 ;; which re-homes symbols in SB-XC to COMMON-LISP.
1801 (if (eq (symbol-package des) (find-package "SB-XC"))
1802 (intern (symbol-name des) *cl-package*)
1803 des)
1804 (ecase (descriptor-lowtag des)
1805 (#.sb!vm:list-pointer-lowtag
1806 (aver (not (cold-null des))) ; function named NIL? please no..
1807 (let ((rest (cold-cdr des)))
1808 (aver (cold-null (cold-cdr rest)))
1809 (list (warm-symbol (cold-car des))
1810 (warm-symbol (cold-car rest)))))
1811 (#.sb!vm:other-pointer-lowtag
1812 (warm-symbol des))))))
1813 (legal-fun-name-or-type-error result)
1814 result))
1816 #!+x86-64
1817 (defun encode-fdefn-raw-addr (fdefn jump-target opcode)
1818 (let ((disp (- jump-target
1819 (+ (descriptor-bits fdefn)
1820 (- sb!vm:other-pointer-lowtag)
1821 (ash sb!vm:fdefn-raw-addr-slot sb!vm:word-shift)
1822 5))))
1823 (logior (ash (ldb (byte 32 0) (the (signed-byte 32) disp)) 8) opcode)))
1825 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw
1826 (gspace #!+immobile-space *immobile-fixedobj*
1827 #!-immobile-space *dynamic*))
1828 (declare (type (or symbol descriptor) cold-name))
1829 (declare (special core-file-name))
1830 (let ((warm-name (warm-fun-name cold-name)))
1831 (or (gethash warm-name *cold-fdefn-objects*)
1832 (let ((fdefn (allocate-header+object gspace (1- sb!vm:fdefn-size) sb!vm:fdefn-widetag)))
1833 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
1834 (write-wordindexed fdefn sb!vm:fdefn-name-slot cold-name)
1835 (unless leave-fn-raw
1836 (write-wordindexed fdefn sb!vm:fdefn-fun-slot *nil-descriptor*)
1837 (let ((tramp
1838 (or (lookup-assembler-reference 'sb!vm::undefined-tramp core-file-name)
1839 ;; Our preload for the tramps doesn't happen during host-1,
1840 ;; so substitute a usable value.
1841 0)))
1842 (write-wordindexed/raw fdefn sb!vm:fdefn-raw-addr-slot
1843 #!+(and immobile-code x86-64)
1844 (encode-fdefn-raw-addr fdefn tramp #xE8)
1845 #!-immobile-code tramp)))
1846 fdefn))))
1848 (defun cold-functionp (descriptor)
1849 (eql (descriptor-lowtag descriptor) sb!vm:fun-pointer-lowtag))
1851 (defun cold-fun-entry-addr (fun)
1852 (aver (= (descriptor-lowtag fun) sb!vm:fun-pointer-lowtag))
1853 (+ (descriptor-bits fun)
1854 (- sb!vm:fun-pointer-lowtag)
1855 (ash sb!vm:simple-fun-code-offset sb!vm:word-shift)))
1857 ;;; Handle a DEFUN in cold-load.
1858 (defun cold-fset (name defn source-loc &optional inline-expansion)
1859 ;; SOURCE-LOC can be ignored, because functions intrinsically store
1860 ;; their location as part of the code component.
1861 ;; The argument is supplied here only to provide context for
1862 ;; a redefinition warning, which can't happen in cold load.
1863 (declare (ignore source-loc))
1864 (sb!int:binding* (((cold-name warm-name)
1865 ;; (SETF f) was descriptorized when dumped, symbols were not.
1866 (if (symbolp name)
1867 (values (cold-intern name) name)
1868 (values name (warm-fun-name name))))
1869 (fdefn (cold-fdefinition-object cold-name t)))
1870 (when (cold-functionp (cold-fdefn-fun fdefn))
1871 (error "Duplicate DEFUN for ~S" warm-name))
1872 ;; There can't be any closures or funcallable instances.
1873 (aver (= (logand (descriptor-bits (read-memory defn)) sb!vm:widetag-mask)
1874 sb!vm:simple-fun-widetag))
1875 (push (cold-cons cold-name inline-expansion) *!cold-defuns*)
1876 (write-wordindexed fdefn sb!vm:fdefn-fun-slot defn)
1877 (let ((fun-entry-addr
1878 (+ (logandc2 (descriptor-bits defn) sb!vm:lowtag-mask)
1879 (ash sb!vm:simple-fun-code-offset sb!vm:word-shift))))
1880 (declare (ignorable fun-entry-addr)) ; sparc and arm don't need
1881 #!+(and immobile-code x86-64)
1882 (write-wordindexed/raw fdefn sb!vm:fdefn-raw-addr-slot
1883 (encode-fdefn-raw-addr fdefn fun-entry-addr #xE9))
1884 #!-immobile-code
1885 (progn
1886 #!+(or sparc arm) (write-wordindexed fdefn sb!vm:fdefn-raw-addr-slot defn)
1887 #!-(or sparc arm) (write-wordindexed/raw fdefn sb!vm:fdefn-raw-addr-slot
1888 fun-entry-addr)))
1889 fdefn))
1891 ;;; Handle a DEFMETHOD in cold-load. "Very easily done". Right.
1892 (defun cold-defmethod (name &rest stuff)
1893 (let ((gf (assoc name *cold-methods*)))
1894 (unless gf
1895 (setq gf (cons name nil))
1896 (push gf *cold-methods*))
1897 (push stuff (cdr gf))))
1899 (defun attach-classoid-cells-to-symbols (hashtable)
1900 (let ((num (sb!c::meta-info-number (sb!c::meta-info :type :classoid-cell)))
1901 (layout (gethash 'sb!kernel::classoid-cell *cold-layouts*)))
1902 (when (plusp (hash-table-count *classoid-cells*))
1903 (aver layout))
1904 ;; Iteration order is immaterial. The symbols will get sorted later.
1905 (maphash (lambda (symbol cold-classoid-cell)
1906 ;; Some classoid-cells are dumped before the cold layout
1907 ;; of classoid-cell has been made, so fix those cases now.
1908 ;; Obviously it would be better if, in general, ALLOCATE-STRUCT
1909 ;; knew when something later must backpatch a cold layout
1910 ;; so that it could make a note to itself to do those ASAP
1911 ;; after the cold layout became known.
1912 (when (cold-null (cold-layout-of cold-classoid-cell))
1913 (set-instance-layout cold-classoid-cell layout))
1914 (setf (gethash symbol hashtable)
1915 (packed-info-insert
1916 (gethash symbol hashtable +nil-packed-infos+)
1917 sb!impl::+no-auxilliary-key+ num cold-classoid-cell)))
1918 *classoid-cells*))
1919 hashtable)
1921 ;; Create pointer from SYMBOL and/or (SETF SYMBOL) to respective fdefinition
1923 (defun attach-fdefinitions-to-symbols (hashtable)
1924 ;; Collect fdefinitions that go with one symbol, e.g. CAR and (SETF CAR),
1925 ;; using the host's code for manipulating a packed info-vector.
1926 (maphash (lambda (warm-name cold-fdefn)
1927 (with-globaldb-name (key1 key2) warm-name
1928 :hairy (error "Hairy fdefn name in genesis: ~S" warm-name)
1929 :simple
1930 (setf (gethash key1 hashtable)
1931 (packed-info-insert
1932 (gethash key1 hashtable +nil-packed-infos+)
1933 key2 +fdefn-info-num+ cold-fdefn))))
1934 *cold-fdefn-objects*)
1935 hashtable)
1937 (defun dump-symbol-info-vectors (hashtable)
1938 ;; Emit in the same order symbols reside in core to avoid
1939 ;; sensitivity to the iteration order of host's maphash.
1940 (loop for (warm-sym . info)
1941 in (sort (%hash-table-alist hashtable) #'<
1942 :key (lambda (x) (descriptor-bits (cold-intern (car x)))))
1943 do (write-wordindexed
1944 (cold-intern warm-sym) sb!vm:symbol-info-slot
1945 ;; Each vector will have one fixnum, possibly the symbol SETF,
1946 ;; and one or two #<fdefn> objects in it, and/or a classoid-cell.
1947 (vector-in-core
1948 (map 'list (lambda (elt)
1949 (etypecase elt
1950 (symbol (cold-intern elt))
1951 (fixnum (make-fixnum-descriptor elt))
1952 (descriptor elt)))
1953 info)))))
1956 ;;;; fixups and related stuff
1958 ;;; an EQUAL hash table
1959 (defvar *cold-foreign-symbol-table*)
1960 (declaim (type hash-table *cold-foreign-symbol-table*))
1962 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1963 ;; the C runtime.
1964 (defun load-cold-foreign-symbol-table (filename)
1965 (/show "load-cold-foreign-symbol-table" filename)
1966 (with-open-file (file filename)
1967 (loop for line = (read-line file nil nil)
1968 while line do
1969 ;; UNIX symbol tables might have tabs in them, and tabs are
1970 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1971 ;; nice portable way to deal with them within Lisp, alas.
1972 ;; Fortunately, it's easy to use UNIX command line tools like
1973 ;; sed to remove the problem, so it's not too painful for us
1974 ;; to push responsibility for converting tabs to spaces out to
1975 ;; the caller.
1977 ;; Other non-STANDARD-CHARs are problematic for the same reason.
1978 ;; Make sure that there aren't any..
1979 (let ((ch (find-if (lambda (char)
1980 (not (typep char 'standard-char)))
1981 line)))
1982 (when ch
1983 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1985 line)))
1986 (setf line (string-trim '(#\space) line))
1987 (let ((p1 (position #\space line :from-end nil))
1988 (p2 (position #\space line :from-end t)))
1989 (if (not (and p1 p2 (< p1 p2)))
1990 ;; KLUDGE: It's too messy to try to understand all
1991 ;; possible output from nm, so we just punt the lines we
1992 ;; don't recognize. We realize that there's some chance
1993 ;; that might get us in trouble someday, so we warn
1994 ;; about it.
1995 (warn "ignoring unrecognized line ~S in ~A" line filename)
1996 (multiple-value-bind (value name)
1997 (if (string= "0x" line :end2 2)
1998 (values (parse-integer line :start 2 :end p1 :radix 16)
1999 (subseq line (1+ p2)))
2000 (values (parse-integer line :end p1 :radix 16)
2001 (subseq line (1+ p2))))
2002 ;; KLUDGE CLH 2010-05-31: on darwin, nm gives us
2003 ;; _function but dlsym expects us to look up
2004 ;; function, without the leading _ . Therefore, we
2005 ;; strip it off here.
2006 #!+darwin
2007 (when (equal (char name 0) #\_)
2008 (setf name (subseq name 1)))
2009 (multiple-value-bind (old-value found)
2010 (gethash name *cold-foreign-symbol-table*)
2011 (when (and found
2012 (not (= old-value value)))
2013 (warn "redefining ~S from #X~X to #X~X"
2014 name old-value value)))
2015 (/show "adding to *cold-foreign-symbol-table*:" name value)
2016 (setf (gethash name *cold-foreign-symbol-table*) value)
2017 #!+win32
2018 (let ((at-position (position #\@ name)))
2019 (when at-position
2020 (let ((name (subseq name 0 at-position)))
2021 (multiple-value-bind (old-value found)
2022 (gethash name *cold-foreign-symbol-table*)
2023 (when (and found
2024 (not (= old-value value)))
2025 (warn "redefining ~S from #X~X to #X~X"
2026 name old-value value)))
2027 (setf (gethash name *cold-foreign-symbol-table*)
2028 value)))))))))
2029 (values)) ;; PROGN
2031 (defun cold-foreign-symbol-address (name)
2032 (declare (ignorable name))
2033 #!+crossbuild-test #xf00fa8 ; any random 4-octet-aligned value should do
2034 #!-crossbuild-test
2035 (or (find-foreign-symbol-in-table name *cold-foreign-symbol-table*)
2036 *foreign-symbol-placeholder-value*
2037 (progn
2038 (format *error-output* "~&The foreign symbol table is:~%")
2039 (maphash (lambda (k v)
2040 (format *error-output* "~&~S = #X~8X~%" k v))
2041 *cold-foreign-symbol-table*)
2042 (error "The foreign symbol ~S is undefined." name))))
2044 (defvar *cold-assembler-objects*) ; list of the code components
2045 (defvar *cold-assembler-routines*)
2047 (defvar *cold-assembler-fixups*)
2048 (defvar *cold-static-call-fixups*)
2050 (defun record-cold-assembler-routine (name address)
2051 (/xhow "in RECORD-COLD-ASSEMBLER-ROUTINE" name address)
2052 (push (cons name address)
2053 *cold-assembler-routines*))
2055 (defun lookup-assembler-reference (symbol &optional (errorp t))
2056 (let ((value (cdr (assoc symbol *cold-assembler-routines*))))
2057 (unless value
2058 (when errorp
2059 (error "Assembler routine ~S not defined." symbol)))
2060 value))
2062 ;;; Unlike in the target, FOP-KNOWN-FUN sometimes has to backpatch.
2063 (defvar *deferred-known-fun-refs*)
2065 ;;; In case we need to store code fixups in code objects.
2066 ;;; At present only the x86 backends use this
2067 (defvar *code-fixup-notes*)
2069 ;;; Given a pointer to a code object and a byte offset relative to the
2070 ;;; tail of the code object's header, return a byte offset relative to the
2071 ;;; (beginning of the) code object.
2073 (declaim (ftype (function (descriptor sb!vm:word)) calc-offset))
2074 (defun calc-offset (code-object insts-offset-bytes)
2075 (+ (ash (logand (get-header-data code-object) sb!vm:short-header-max-words)
2076 sb!vm:word-shift)
2077 insts-offset-bytes))
2079 (declaim (ftype (function (descriptor sb!vm:word sb!vm:word
2080 keyword &optional keyword) descriptor)
2081 cold-fixup))
2082 (defun cold-fixup (code-object after-header value kind &optional flavor)
2083 (declare (ignorable flavor))
2084 (let* ((offset-within-code-object (calc-offset code-object after-header))
2085 (gspace-byte-offset (+ (descriptor-byte-offset code-object)
2086 offset-within-code-object)))
2087 #!-(or x86 x86-64)
2088 (sb!vm::fixup-code-object code-object gspace-byte-offset value kind)
2090 #!+(or x86 x86-64)
2091 (let* ((gspace-data (descriptor-mem code-object))
2092 (obj-start-addr (logandc2 (descriptor-bits code-object) sb!vm:lowtag-mask))
2093 (code-end-addr
2094 (+ obj-start-addr
2095 (ash (logand (get-header-data code-object)
2096 sb!vm:short-header-max-words) sb!vm:word-shift)
2097 (descriptor-fixnum
2098 (read-wordindexed code-object sb!vm:code-code-size-slot))))
2099 (gspace-base (gspace-byte-address (descriptor-gspace code-object)))
2100 (in-dynamic-space
2101 (= (gspace-identifier (descriptor-intuit-gspace code-object))
2102 dynamic-core-space-id))
2103 (addr (+ value
2104 (sb!vm::sign-extend (bvref-32 gspace-data gspace-byte-offset)
2105 32))))
2107 (declare (ignorable code-end-addr in-dynamic-space))
2108 (assert (= obj-start-addr
2109 (+ gspace-base (descriptor-byte-offset code-object))))
2111 ;; See FIXUP-CODE-OBJECT in x86-vm.lisp and x86-64-vm.lisp.
2112 ;; Except for the use of saps, this is basically identical.
2113 (when (ecase kind
2114 (:absolute
2115 (setf (bvref-32 gspace-data gspace-byte-offset)
2116 (the (unsigned-byte 32) addr))
2117 ;; Absolute fixups are recorded if within the object for x86.
2118 #!+x86 (and in-dynamic-space
2119 (< obj-start-addr addr code-end-addr))
2120 ;; Absolute fixups on x86-64 do not refer to this code component,
2121 ;; because we have RIP-relative addressing, but references to
2122 ;; other immobile-space objects must be recorded.
2123 ;; :STATIC-CALL absolute fixups are not saved because static space
2124 ;; won't move, but :NAMED-CALL fixups are saved.
2125 #!+x86-64
2126 (member flavor '(:named-call :layout :immobile-object)))
2127 (:relative ; (used for arguments to X86 relative CALL instruction)
2128 (setf (bvref-32 gspace-data gspace-byte-offset)
2129 (the (signed-byte 32)
2130 (- addr (+ gspace-base gspace-byte-offset 4)))) ; 4 = size of rel32off
2131 ;; Relative fixups are recorded if without the object.
2132 ;; Except that read-only space contains calls to asm routines,
2133 ;; and we don't record those fixups.
2134 #!+x86 (and in-dynamic-space
2135 (not (< obj-start-addr addr code-end-addr)))
2136 #!+x86-64 nil))
2137 (push after-header (gethash (descriptor-bits code-object)
2138 *code-fixup-notes*)))))
2139 code-object)
2141 (defun resolve-assembler-fixups ()
2142 (dolist (fixup *cold-assembler-fixups*)
2143 (let* ((routine (car fixup))
2144 (value (lookup-assembler-reference routine)))
2145 (when value
2146 (cold-fixup (second fixup) (third fixup) value (fourth fixup)
2147 :assembly-routine))))
2148 ;; Static calls are very similar to assembler routine calls,
2149 ;; so take care of those too.
2150 (dolist (fixup *cold-static-call-fixups*)
2151 (destructuring-bind (name kind code offset) fixup
2152 (cold-fixup code offset
2153 (cold-fun-entry-addr
2154 (cold-fdefn-fun (cold-fdefinition-object name)))
2155 kind :static-call))))
2157 #!+sb-dynamic-core
2158 (progn
2159 (defparameter *dyncore-address* sb!vm::linkage-table-space-start)
2160 (defparameter *dyncore-linkage-keys* nil)
2161 (defparameter *dyncore-table* (make-hash-table :test 'equal))
2163 (defun dyncore-note-symbol (symbol-name datap)
2164 "Register a symbol and return its address in proto-linkage-table."
2165 (let ((key (cons symbol-name datap)))
2166 (symbol-macrolet ((entry (gethash key *dyncore-table*)))
2167 (or entry
2168 (setf entry
2169 (prog1 *dyncore-address*
2170 (push key *dyncore-linkage-keys*)
2171 (incf *dyncore-address* sb!vm::linkage-table-entry-size))))))))
2173 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
2174 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
2175 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
2176 ;;; target-load.lisp refers to.
2177 (defun foreign-symbols-to-core ()
2178 (flet ((to-core (list transducer target-symbol)
2179 (cold-set target-symbol (vector-in-core (mapcar transducer list)))))
2180 #!-sb-dynamic-core
2181 (to-core (sort (%hash-table-alist *cold-foreign-symbol-table*) #'string< :key #'car)
2182 (lambda (symbol)
2183 (cold-cons (set-readonly (base-string-to-core (car symbol)))
2184 (number-to-core (cdr symbol))))
2185 '*!initial-foreign-symbols*)
2186 #!+sb-dynamic-core
2187 ;; Linkage table is recomputed by Lisp, so foreign symbols have to be listed
2188 ;; in the proper order, which is the reverse of the currently stored order.
2189 (to-core (nreverse *dyncore-linkage-keys*)
2190 (lambda (symbol)
2191 (cold-cons (set-readonly (base-string-to-core (car symbol)))
2192 (cdr symbol)))
2193 'sb!vm::+required-runtime-c-symbols+)
2194 (to-core (sort (copy-list *cold-assembler-routines*) #'string< :key #'car)
2195 (lambda (rtn)
2196 (cold-cons (cold-intern (car rtn)) (number-to-core (cdr rtn))))
2197 '*!initial-assembler-routines*)))
2200 ;;;; general machinery for cold-loading FASL files
2202 (defun pop-fop-stack (stack)
2203 (let ((top (svref stack 0)))
2204 (declare (type index top))
2205 (when (eql 0 top)
2206 (error "FOP stack empty"))
2207 (setf (svref stack 0) (1- top))
2208 (svref stack top)))
2210 ;;; Cause a fop to have a special definition for cold load.
2212 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
2213 ;;; looks up the encoding for this name (created by a previous DEFINE-FOP)
2214 ;;; instead of creating a new encoding.
2215 (defmacro define-cold-fop ((name &optional arglist) &rest forms)
2216 (let* ((code (get name 'opcode))
2217 (argc (aref (car **fop-signatures**) code))
2218 (fname (symbolicate "COLD-" name)))
2219 (unless code
2220 (error "~S is not a defined FOP." name))
2221 (when (and (plusp argc) (not (singleton-p arglist)))
2222 (error "~S must take one argument" name))
2223 `(progn
2224 (defun ,fname (.fasl-input. ,@arglist)
2225 (declare (ignorable .fasl-input.))
2226 (macrolet ((fasl-input () '(the fasl-input .fasl-input.))
2227 (fasl-input-stream () '(%fasl-input-stream (fasl-input)))
2228 (pop-stack ()
2229 '(pop-fop-stack (%fasl-input-stack (fasl-input)))))
2230 ,@forms))
2231 ;; We simply overwrite elements of **FOP-FUNS** since the contents
2232 ;; of the host are never propagated directly into the target core.
2233 ,@(loop for i from code to (logior code (if (plusp argc) 3 0))
2234 collect `(setf (svref **fop-funs** ,i) #',fname)))))
2236 ;;; Cause a fop to be undefined in cold load.
2237 (defmacro not-cold-fop (name)
2238 `(define-cold-fop (,name)
2239 (error "The fop ~S is not supported in cold load." ',name)))
2241 ;;; COLD-LOAD loads stuff into the core image being built by calling
2242 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
2243 ;;; loading functions.
2244 (defun cold-load (filename)
2245 "Load the file named by FILENAME into the cold load image being built."
2246 (write-line (namestring filename))
2247 (with-open-file (s filename :element-type '(unsigned-byte 8))
2248 (load-as-fasl s nil nil)))
2250 ;;;; miscellaneous cold fops
2252 (define-cold-fop (fop-misc-trap) *unbound-marker*)
2254 (define-cold-fop (fop-character (c))
2255 (make-character-descriptor c))
2257 (define-cold-fop (fop-empty-list) nil)
2258 (define-cold-fop (fop-truth) t)
2260 (define-cold-fop (fop-struct (size)) ; n-words incl. layout, excluding header
2261 (let* ((layout (pop-stack))
2262 (result (allocate-struct *dynamic* layout size))
2263 (bitmap (descriptor-fixnum
2264 (read-slot layout *host-layout-of-layout* :bitmap))))
2265 ;; Raw slots can not possibly work because dump-struct uses
2266 ;; %RAW-INSTANCE-REF/WORD which does not exist in the cross-compiler.
2267 ;; Remove this assertion if that problem is somehow circumvented.
2268 (unless (eql bitmap sb!kernel::+layout-all-tagged+)
2269 (error "Raw slots not working in genesis."))
2270 (loop for index downfrom (1- size) to sb!vm:instance-data-start
2271 for val = (pop-stack) then (pop-stack)
2272 do (write-wordindexed result
2273 (+ index sb!vm:instance-slots-offset)
2274 (if (logbitp index bitmap)
2276 (descriptor-word-sized-integer val))))
2277 result))
2279 (define-cold-fop (fop-layout)
2280 (let* ((bitmap-des (pop-stack))
2281 (length-des (pop-stack))
2282 (depthoid-des (pop-stack))
2283 (cold-inherits (pop-stack))
2284 (name (pop-stack))
2285 (old-layout-descriptor (gethash name *cold-layouts*)))
2286 (declare (type descriptor length-des depthoid-des cold-inherits))
2287 (declare (type symbol name))
2288 ;; If a layout of this name has been defined already
2289 (if old-layout-descriptor
2290 ;; Enforce consistency between the previous definition and the
2291 ;; current definition, then return the previous definition.
2292 (flet ((get-slot (keyword)
2293 (read-slot old-layout-descriptor *host-layout-of-layout* keyword)))
2294 (let ((old-length (descriptor-fixnum (get-slot :length)))
2295 (old-depthoid (descriptor-fixnum (get-slot :depthoid)))
2296 (old-bitmap (host-object-from-core (get-slot :bitmap)))
2297 (length (descriptor-fixnum length-des))
2298 (depthoid (descriptor-fixnum depthoid-des))
2299 (bitmap (host-object-from-core bitmap-des)))
2300 (unless (= length old-length)
2301 (error "cold loading a reference to class ~S when the compile~%~
2302 time length was ~S and current length is ~S"
2303 name
2304 length
2305 old-length))
2306 (unless (cold-vector-elements-eq cold-inherits (get-slot :inherits))
2307 (error "cold loading a reference to class ~S when the compile~%~
2308 time inherits were ~S~%~
2309 and current inherits are ~S"
2310 name
2311 (listify-cold-inherits cold-inherits)
2312 (listify-cold-inherits (get-slot :inherits))))
2313 (unless (= depthoid old-depthoid)
2314 (error "cold loading a reference to class ~S when the compile~%~
2315 time inheritance depthoid was ~S and current inheritance~%~
2316 depthoid is ~S"
2317 name
2318 depthoid
2319 old-depthoid))
2320 (unless (= bitmap old-bitmap)
2321 (error "cold loading a reference to class ~S when the compile~%~
2322 time raw-slot-bitmap was ~S and is currently ~S"
2323 name bitmap old-bitmap)))
2324 old-layout-descriptor)
2325 ;; Make a new definition from scratch.
2326 (make-cold-layout name length-des cold-inherits depthoid-des bitmap-des))))
2328 ;;;; cold fops for loading symbols
2330 ;;; Load a symbol SIZE characters long from FASL-INPUT, and
2331 ;;; intern that symbol in PACKAGE.
2332 (defun cold-load-symbol (length+flag package fasl-input)
2333 (let ((string (make-string (ash length+flag -1))))
2334 (read-string-as-bytes (%fasl-input-stream fasl-input) string)
2335 (push-fop-table (intern string package) fasl-input)))
2337 ;; I don't feel like hacking up DEFINE-COLD-FOP any more than necessary,
2338 ;; so this code is handcrafted to accept two operands.
2339 (flet ((fop-cold-symbol-in-package-save (fasl-input length+flag pkg-index)
2340 (cold-load-symbol length+flag (ref-fop-table fasl-input pkg-index)
2341 fasl-input)))
2342 (let ((i (get 'fop-symbol-in-package-save 'opcode)))
2343 (fill **fop-funs** #'fop-cold-symbol-in-package-save :start i :end (+ i 4))))
2345 (define-cold-fop (fop-lisp-symbol-save (length+flag))
2346 (cold-load-symbol length+flag *cl-package* (fasl-input)))
2348 (define-cold-fop (fop-keyword-symbol-save (length+flag))
2349 (cold-load-symbol length+flag *keyword-package* (fasl-input)))
2351 (define-cold-fop (fop-uninterned-symbol-save (length+flag))
2352 (let ((name (make-string (ash length+flag -1))))
2353 (read-string-as-bytes (fasl-input-stream) name)
2354 (push-fop-table (get-uninterned-symbol name) (fasl-input))))
2356 (define-cold-fop (fop-copy-symbol-save (index))
2357 (let* ((symbol (ref-fop-table (fasl-input) index))
2358 (name
2359 (if (symbolp symbol)
2360 (symbol-name symbol)
2361 (base-string-from-core
2362 (read-wordindexed symbol sb!vm:symbol-name-slot)))))
2363 ;; Genesis performs additional coalescing of uninterned symbols
2364 (push-fop-table (get-uninterned-symbol name) (fasl-input))))
2366 ;;;; cold fops for loading packages
2368 (define-cold-fop (fop-named-package-save (namelen))
2369 (let ((name (make-string namelen)))
2370 (read-string-as-bytes (fasl-input-stream) name)
2371 (push-fop-table (find-package name) (fasl-input))))
2373 ;;;; cold fops for loading lists
2375 ;;; Make a list of the top LENGTH things on the fop stack. The last
2376 ;;; cdr of the list is set to LAST.
2377 (defmacro cold-stack-list (length last)
2378 `(do* ((index ,length (1- index))
2379 (result ,last (cold-cons (pop-stack) result)))
2380 ((= index 0) result)
2381 (declare (fixnum index))))
2383 (define-cold-fop (fop-list)
2384 (cold-stack-list (read-byte-arg (fasl-input-stream)) *nil-descriptor*))
2385 (define-cold-fop (fop-list*)
2386 (cold-stack-list (read-byte-arg (fasl-input-stream)) (pop-stack)))
2387 (define-cold-fop (fop-list-1)
2388 (cold-stack-list 1 *nil-descriptor*))
2389 (define-cold-fop (fop-list-2)
2390 (cold-stack-list 2 *nil-descriptor*))
2391 (define-cold-fop (fop-list-3)
2392 (cold-stack-list 3 *nil-descriptor*))
2393 (define-cold-fop (fop-list-4)
2394 (cold-stack-list 4 *nil-descriptor*))
2395 (define-cold-fop (fop-list-5)
2396 (cold-stack-list 5 *nil-descriptor*))
2397 (define-cold-fop (fop-list-6)
2398 (cold-stack-list 6 *nil-descriptor*))
2399 (define-cold-fop (fop-list-7)
2400 (cold-stack-list 7 *nil-descriptor*))
2401 (define-cold-fop (fop-list-8)
2402 (cold-stack-list 8 *nil-descriptor*))
2403 (define-cold-fop (fop-list*-1)
2404 (cold-stack-list 1 (pop-stack)))
2405 (define-cold-fop (fop-list*-2)
2406 (cold-stack-list 2 (pop-stack)))
2407 (define-cold-fop (fop-list*-3)
2408 (cold-stack-list 3 (pop-stack)))
2409 (define-cold-fop (fop-list*-4)
2410 (cold-stack-list 4 (pop-stack)))
2411 (define-cold-fop (fop-list*-5)
2412 (cold-stack-list 5 (pop-stack)))
2413 (define-cold-fop (fop-list*-6)
2414 (cold-stack-list 6 (pop-stack)))
2415 (define-cold-fop (fop-list*-7)
2416 (cold-stack-list 7 (pop-stack)))
2417 (define-cold-fop (fop-list*-8)
2418 (cold-stack-list 8 (pop-stack)))
2420 ;;;; cold fops for loading vectors
2422 (define-cold-fop (fop-base-string (len))
2423 (let ((string (make-string len)))
2424 (read-string-as-bytes (fasl-input-stream) string)
2425 (set-readonly (base-string-to-core string))))
2427 #!+sb-unicode
2428 (define-cold-fop (fop-character-string (len))
2429 (bug "CHARACTER-STRING[~D] dumped by cross-compiler." len))
2431 (define-cold-fop (fop-vector (size))
2432 (if (zerop size)
2433 *simple-vector-0-descriptor*
2434 (let ((result (allocate-vector-object *dynamic*
2435 sb!vm:n-word-bits
2436 size
2437 sb!vm:simple-vector-widetag)))
2438 (do ((index (1- size) (1- index)))
2439 ((minusp index))
2440 (declare (fixnum index))
2441 (write-wordindexed result
2442 (+ index sb!vm:vector-data-offset)
2443 (pop-stack)))
2444 (set-readonly result))))
2446 (define-cold-fop (fop-spec-vector)
2447 (let* ((len (read-word-arg (fasl-input-stream)))
2448 (type (read-byte-arg (fasl-input-stream)))
2449 (sizebits (aref **saetp-bits-per-length** type))
2450 (result (progn (aver (< sizebits 255))
2451 (allocate-vector-object *dynamic* sizebits len type)))
2452 (start (+ (descriptor-byte-offset result)
2453 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2454 (end (+ start
2455 (ceiling (* len sizebits)
2456 sb!vm:n-byte-bits))))
2457 (read-bigvec-as-sequence-or-die (descriptor-mem result)
2458 (fasl-input-stream)
2459 :start start
2460 :end end)
2461 (set-readonly result)))
2463 (not-cold-fop fop-array)
2464 #+nil
2465 ;; This code is unexercised. The only use of FOP-ARRAY is from target-dump.
2466 ;; It would be a shame to delete it though, as it might come in handy.
2467 (define-cold-fop (fop-array)
2468 (let* ((rank (read-word-arg (fasl-input-stream)))
2469 (data-vector (pop-stack))
2470 (result (allocate-object *dynamic*
2471 (+ sb!vm:array-dimensions-offset rank)
2472 sb!vm:other-pointer-lowtag)))
2473 (write-header-word result rank sb!vm:simple-array-widetag)
2474 (write-wordindexed result sb!vm:array-fill-pointer-slot *nil-descriptor*)
2475 (write-wordindexed result sb!vm:array-data-slot data-vector)
2476 (write-wordindexed result sb!vm:array-displacement-slot *nil-descriptor*)
2477 (write-wordindexed result sb!vm:array-displaced-p-slot *nil-descriptor*)
2478 (write-wordindexed result sb!vm:array-displaced-from-slot *nil-descriptor*)
2479 (let ((total-elements 1))
2480 (dotimes (axis rank)
2481 (let ((dim (pop-stack)))
2482 (unless (is-fixnum-lowtag (descriptor-lowtag dim))
2483 (error "non-fixnum dimension? (~S)" dim))
2484 (setf total-elements (* total-elements (descriptor-fixnum dim)))
2485 (write-wordindexed result
2486 (+ sb!vm:array-dimensions-offset axis)
2487 dim)))
2488 (write-wordindexed result
2489 sb!vm:array-elements-slot
2490 (make-fixnum-descriptor total-elements)))
2491 result))
2494 ;;;; cold fops for loading numbers
2496 (defmacro define-cold-number-fop (fop &optional arglist)
2497 ;; Invoke the ordinary warm version of this fop to cons the number.
2498 `(define-cold-fop (,fop ,arglist)
2499 (number-to-core (,fop (fasl-input) ,@arglist))))
2501 (define-cold-number-fop fop-single-float)
2502 (define-cold-number-fop fop-double-float)
2503 (define-cold-number-fop fop-word-integer)
2504 (define-cold-number-fop fop-byte-integer)
2505 (define-cold-number-fop fop-complex-single-float)
2506 (define-cold-number-fop fop-complex-double-float)
2507 (define-cold-number-fop fop-integer (n-bytes))
2509 (define-cold-fop (fop-ratio)
2510 (let ((den (pop-stack)))
2511 (number-pair-to-core (pop-stack) den sb!vm:ratio-widetag)))
2513 (define-cold-fop (fop-complex)
2514 (let ((im (pop-stack)))
2515 (number-pair-to-core (pop-stack) im sb!vm:complex-widetag)))
2517 ;;;; cold fops for calling (or not calling)
2519 (not-cold-fop fop-eval)
2520 (not-cold-fop fop-eval-for-effect)
2522 (defvar *load-time-value-counter*)
2524 (flet ((pop-args (fasl-input)
2525 (let ((args)
2526 (stack (%fasl-input-stack fasl-input)))
2527 (dotimes (i (read-byte-arg (%fasl-input-stream fasl-input))
2528 (values (pop-fop-stack stack) args))
2529 (push (pop-fop-stack stack) args))))
2530 (call (fun-name handler-name args)
2531 (acond ((get fun-name handler-name) (apply it args))
2532 (t (error "Can't ~S ~S in cold load" handler-name fun-name)))))
2534 (define-cold-fop (fop-funcall)
2535 (multiple-value-bind (fun args) (pop-args (fasl-input))
2536 (if args
2537 (case fun
2538 (fdefinition
2539 ;; Special form #'F fopcompiles into `(FDEFINITION ,f)
2540 (aver (and (singleton-p args) (symbolp (car args))))
2541 (target-symbol-function (car args)))
2542 (cons (cold-cons (first args) (second args)))
2543 (symbol-global-value (cold-symbol-value (first args)))
2544 (t (call fun :sb-cold-funcall-handler/for-value args)))
2545 (let ((counter *load-time-value-counter*))
2546 (push (cold-list (cold-intern :load-time-value) fun
2547 (number-to-core counter)) *!cold-toplevels*)
2548 (setf *load-time-value-counter* (1+ counter))
2549 (make-descriptor 0 :load-time-value counter)))))
2551 (define-cold-fop (fop-funcall-for-effect)
2552 (multiple-value-bind (fun args) (pop-args (fasl-input))
2553 (if (not args)
2554 (push fun *!cold-toplevels*)
2555 (case fun
2556 (sb!impl::%defun (apply #'cold-fset args))
2557 (sb!pcl::!trivial-defmethod (apply #'cold-defmethod args))
2558 (sb!kernel::%defstruct
2559 (push args *known-structure-classoids*)
2560 (push (apply #'cold-list (cold-intern 'defstruct) args)
2561 *!cold-toplevels*))
2562 (sb!c::%defconstant
2563 (destructuring-bind (name val . rest) args
2564 (cold-set name (if (symbolp val) (cold-intern val) val))
2565 (push (cold-cons (cold-intern name) (list-to-core rest))
2566 *!cold-defconstants*)))
2567 (set
2568 (aver (= (length args) 2))
2569 (cold-set (first args)
2570 (let ((val (second args)))
2571 (if (symbolp val) (cold-intern val) val))))
2572 (%svset (apply 'cold-svset args))
2573 (t (call fun :sb-cold-funcall-handler/for-effect args)))))))
2575 (defun finalize-load-time-value-noise ()
2576 (cold-set '*!load-time-values*
2577 (allocate-vector-object *dynamic*
2578 sb!vm:n-word-bits
2579 *load-time-value-counter*
2580 sb!vm:simple-vector-widetag)))
2583 ;;;; cold fops for fixing up circularities
2585 (define-cold-fop (fop-rplaca)
2586 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2587 (idx (read-word-arg (fasl-input-stream))))
2588 (write-memory (cold-nthcdr idx obj) (pop-stack))))
2590 (define-cold-fop (fop-rplacd)
2591 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2592 (idx (read-word-arg (fasl-input-stream))))
2593 (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2595 (define-cold-fop (fop-svset)
2596 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2597 (idx (read-word-arg (fasl-input-stream))))
2598 (write-wordindexed obj
2599 (+ idx
2600 (ecase (descriptor-lowtag obj)
2601 (#.sb!vm:instance-pointer-lowtag 1)
2602 (#.sb!vm:other-pointer-lowtag 2)))
2603 (pop-stack))))
2605 (define-cold-fop (fop-structset)
2606 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
2607 (idx (read-word-arg (fasl-input-stream))))
2608 (write-wordindexed obj (+ idx sb!vm:instance-slots-offset) (pop-stack))))
2610 (define-cold-fop (fop-nthcdr)
2611 (cold-nthcdr (read-word-arg (fasl-input-stream)) (pop-stack)))
2613 (defun cold-nthcdr (index obj)
2614 (dotimes (i index)
2615 (setq obj (read-wordindexed obj sb!vm:cons-cdr-slot)))
2616 obj)
2618 ;;;; cold fops for loading code objects and functions
2620 (define-cold-fop (fop-fdefn)
2621 (cold-fdefinition-object (pop-stack)))
2623 (define-cold-fop (fop-known-fun)
2624 (let* ((name (pop-stack))
2625 (fun (cold-fdefn-fun (cold-fdefinition-object name))))
2626 (if (cold-null fun) `(:known-fun . ,name) fun)))
2628 #!-(or x86 (and x86-64 (not immobile-space)))
2629 (define-cold-fop (fop-sanctify-for-execution)
2630 (pop-stack))
2632 ;;; Setting this variable shows what code looks like before any
2633 ;;; fixups (or function headers) are applied.
2634 #!+sb-show (defvar *show-pre-fixup-code-p* nil)
2636 (defun cold-load-code (fasl-input code-size nconst nfuns)
2637 (macrolet ((pop-stack () '(pop-fop-stack (%fasl-input-stack fasl-input))))
2638 (let* ((raw-header-n-words (+ sb!vm:code-constants-offset nconst))
2639 ;; Note that the number of constants is rounded up to ensure
2640 ;; that the code vector will be properly aligned.
2641 (header-n-words (round-up raw-header-n-words 2))
2642 (toplevel-p (pop-stack))
2643 (debug-info (pop-stack))
2644 (des (allocate-cold-descriptor
2645 #!-immobile-code *dynamic*
2646 ;; toplevel-p is an indicator of whether the code will
2647 ;; will become garbage. If so, put it in dynamic space,
2648 ;; otherwise immobile space.
2649 #!+immobile-code
2650 (if toplevel-p *dynamic* *immobile-varyobj*)
2651 (+ (ash header-n-words sb!vm:word-shift) code-size)
2652 sb!vm:other-pointer-lowtag)))
2653 (declare (ignorable toplevel-p))
2654 (write-header-word des header-n-words sb!vm:code-header-widetag)
2655 (write-wordindexed des sb!vm:code-code-size-slot
2656 (make-fixnum-descriptor code-size))
2657 (write-wordindexed des sb!vm:code-debug-info-slot debug-info)
2658 (do ((index (1- raw-header-n-words) (1- index)))
2659 ((< index sb!vm:code-constants-offset))
2660 (let ((obj (pop-stack)))
2661 (if (and (consp obj) (eq (car obj) :known-fun))
2662 (push (list* (cdr obj) des index) *deferred-known-fun-refs*)
2663 (write-wordindexed des index obj))))
2664 (let* ((start (+ (descriptor-byte-offset des)
2665 (ash header-n-words sb!vm:word-shift)))
2666 (end (+ start code-size)))
2667 (read-bigvec-as-sequence-or-die (descriptor-mem des)
2668 (%fasl-input-stream fasl-input)
2669 :start start
2670 :end end)
2672 ;; Emulate NEW-SIMPLE-FUN in target-core
2673 (loop for fun-index from (1- nfuns) downto 0
2674 do (let ((offset (read-varint-arg fasl-input)))
2675 (if (> fun-index 0)
2676 (let ((bytes (descriptor-mem des))
2677 (index (+ (descriptor-byte-offset des)
2678 (calc-offset des (ash (1- fun-index) 2)))))
2679 (aver (eql (bvref-32 bytes index) 0))
2680 (setf (bvref-32 bytes index) offset))
2681 #!-64-bit
2682 (write-wordindexed/raw
2684 sb!vm::code-n-entries-slot
2685 (logior (ash offset 16)
2686 (ash nfuns sb!vm:n-fixnum-tag-bits)))
2687 #!+64-bit
2688 (write-wordindexed/raw
2689 des 0
2690 (logior (ash (logior (ash offset 16) nfuns) 32)
2691 (read-bits-wordindexed des 0))))))
2693 #!+sb-show
2694 (when *show-pre-fixup-code-p*
2695 (format *trace-output*
2696 "~&/raw code from code-fop ~W ~W:~%"
2697 nconst
2698 code-size)
2699 (do ((i start (+ i sb!vm:n-word-bytes)))
2700 ((>= i end))
2701 (format *trace-output*
2702 "/#X~8,'0x: #X~8,'0x~%"
2703 (+ i (gspace-byte-address (descriptor-gspace des)))
2704 (bvref-32 (descriptor-mem des) i)))))
2705 des)))
2707 (let ((i (get 'fop-code 'opcode)))
2708 (fill **fop-funs** #'cold-load-code :start i :end (+ i 4)))
2710 (defun resolve-deferred-known-funs ()
2711 (dolist (item *deferred-known-fun-refs*)
2712 (let ((fun (cold-fdefn-fun (cold-fdefinition-object (car item)))))
2713 (aver (not (cold-null fun)))
2714 (let ((place (cdr item)))
2715 (write-wordindexed (car place) (cdr place) fun)))))
2717 (define-cold-fop (fop-alter-code (slot))
2718 (let ((value (pop-stack))
2719 (code (pop-stack)))
2720 (write-wordindexed code slot value)))
2722 (defun fun-offset (code-object fun-index)
2723 (if (> fun-index 0)
2724 (bvref-32 (descriptor-mem code-object)
2725 (+ (descriptor-byte-offset code-object)
2726 (calc-offset code-object (ash (1- fun-index) 2))))
2727 (ldb (byte 16 16)
2728 #!-64-bit (read-bits-wordindexed code-object sb!vm::code-n-entries-slot)
2729 #!+64-bit (ldb (byte 32 32) (read-bits-wordindexed code-object 0)))))
2731 (defun compute-fun (code-object fun-index)
2732 (let* ((offset-from-insns-start (fun-offset code-object fun-index))
2733 (offset-from-code-start (calc-offset code-object offset-from-insns-start)))
2734 (unless (zerop (logand offset-from-code-start sb!vm:lowtag-mask))
2735 (error "unaligned function entry ~S ~S" code-object fun-index))
2736 (make-descriptor (logior (+ (logandc2 (descriptor-bits code-object) sb!vm:lowtag-mask)
2737 offset-from-code-start)
2738 sb!vm:fun-pointer-lowtag))))
2740 (defun cold-fop-fun-entry (fasl-input fun-index)
2741 (binding* (((info type arglist name code-object)
2742 (macrolet ((pop-stack ()
2743 '(pop-fop-stack (%fasl-input-stack fasl-input))))
2744 (values (pop-stack) (pop-stack) (pop-stack) (pop-stack) (pop-stack))))
2745 (fn (compute-fun code-object fun-index)))
2746 #!+(or x86 x86-64) ; store a machine-native pointer to the function entry
2747 ;; note that the bit pattern looks like fixnum due to alignment
2748 (write-wordindexed/raw fn sb!vm:simple-fun-self-slot
2749 (+ (- (descriptor-bits fn) sb!vm:fun-pointer-lowtag)
2750 (ash sb!vm:simple-fun-code-offset sb!vm:word-shift)))
2751 #!-(or x86 x86-64) ; store a pointer back to the function itself in 'self'
2752 (write-wordindexed fn sb!vm:simple-fun-self-slot fn)
2753 (write-wordindexed fn sb!vm:simple-fun-name-slot name)
2754 (write-wordindexed fn sb!vm:simple-fun-arglist-slot arglist)
2755 (write-wordindexed fn sb!vm:simple-fun-type-slot type)
2756 (write-wordindexed fn sb!vm::simple-fun-info-slot info)
2757 fn))
2759 (let ((i (get 'fop-fun-entry 'opcode)))
2760 (fill **fop-funs** #'cold-fop-fun-entry :start i :end (+ i 4)))
2762 #!+sb-thread
2763 (define-cold-fop (fop-symbol-tls-fixup)
2764 (let* ((symbol (pop-stack))
2765 (kind (pop-stack))
2766 (code-object (pop-stack)))
2767 (cold-fixup code-object
2768 (read-word-arg (fasl-input-stream))
2769 (ensure-symbol-tls-index symbol)
2770 kind))) ; and re-push code-object
2772 (define-cold-fop (fop-foreign-fixup)
2773 (let* ((kind (pop-stack))
2774 (code-object (pop-stack))
2775 (len (read-byte-arg (fasl-input-stream)))
2776 (sym (make-string len)))
2777 (read-string-as-bytes (fasl-input-stream) sym)
2778 #!+sb-dynamic-core
2779 (let ((offset (read-word-arg (fasl-input-stream)))
2780 (value (dyncore-note-symbol sym nil)))
2781 (cold-fixup code-object offset value kind :foreign)) ; and re-push code-object
2782 #!- (and) (format t "Bad non-plt fixup: ~S~S~%" sym code-object)
2783 #!-sb-dynamic-core
2784 (let ((offset (read-word-arg (fasl-input-stream)))
2785 (value (cold-foreign-symbol-address sym)))
2786 (cold-fixup code-object offset value kind :foreign)))) ; and re-push code-object
2788 #!+linkage-table
2789 (define-cold-fop (fop-foreign-dataref-fixup)
2790 (let* ((kind (pop-stack))
2791 (code-object (pop-stack))
2792 (len (read-byte-arg (fasl-input-stream)))
2793 (sym (make-string len)))
2794 #!-sb-dynamic-core (declare (ignore code-object))
2795 (read-string-as-bytes (fasl-input-stream) sym)
2796 #!+sb-dynamic-core
2797 (let ((offset (read-word-arg (fasl-input-stream)))
2798 (value (dyncore-note-symbol sym t)))
2799 (cold-fixup code-object offset value kind :foreign-dataref)) ; and re-push code-object
2800 #!-sb-dynamic-core
2801 (progn
2802 (maphash (lambda (k v)
2803 (format *error-output* "~&~S = #X~8X~%" k v))
2804 *cold-foreign-symbol-table*)
2805 (error "shared foreign symbol in cold load: ~S (~S)" sym kind))))
2807 (define-cold-fop (fop-assembler-code)
2808 (let* ((length (read-word-arg (fasl-input-stream)))
2809 (header-n-words
2810 ;; Note: we round the number of constants up to ensure that
2811 ;; the code vector will be properly aligned.
2812 (round-up sb!vm:code-constants-offset 2))
2813 (des (allocate-cold-descriptor (or #!+immobile-space *immobile-varyobj*
2814 *read-only*)
2815 (+ (ash header-n-words
2816 sb!vm:word-shift)
2817 length)
2818 sb!vm:other-pointer-lowtag)))
2819 (push des *cold-assembler-objects*)
2820 (write-header-word des header-n-words sb!vm:code-header-widetag)
2821 (write-wordindexed des
2822 sb!vm:code-code-size-slot
2823 (make-fixnum-descriptor length))
2824 (write-wordindexed des sb!vm:code-debug-info-slot *nil-descriptor*)
2826 (let* ((start (+ (descriptor-byte-offset des)
2827 (ash header-n-words sb!vm:word-shift)))
2828 (end (+ start length)))
2829 (read-bigvec-as-sequence-or-die (descriptor-mem des)
2830 (fasl-input-stream)
2831 :start start
2832 :end end))
2833 des))
2835 (define-cold-fop (fop-assembler-routine)
2836 (let* ((routine (pop-stack))
2837 (des (pop-stack))
2838 (offset (calc-offset des (read-word-arg (fasl-input-stream)))))
2839 (record-cold-assembler-routine
2840 routine
2841 (+ (logandc2 (descriptor-bits des) sb!vm:lowtag-mask) offset))
2842 des))
2844 (define-cold-fop (fop-assembler-fixup)
2845 (let* ((routine (pop-stack))
2846 (kind (pop-stack))
2847 (code-object (pop-stack))
2848 (offset (read-word-arg (fasl-input-stream))))
2849 (push (list routine code-object offset kind) *cold-assembler-fixups*)
2850 code-object))
2852 (define-cold-fop (fop-code-object-fixup)
2853 (let* ((kind (pop-stack))
2854 (code-object (pop-stack))
2855 (offset (read-word-arg (fasl-input-stream)))
2856 (value (descriptor-bits code-object)))
2857 (cold-fixup code-object offset value kind))) ; and re-push code-object
2859 #!+immobile-space
2860 (progn
2861 (define-cold-fop (fop-layout-fixup)
2862 (let* ((obj (pop-stack))
2863 (kind (pop-stack))
2864 (code-object (pop-stack))
2865 (offset (read-word-arg (fasl-input-stream)))
2866 (cold-layout (or (gethash obj *cold-layouts*)
2867 (error "No cold-layout for ~S~%" obj))))
2868 (cold-fixup code-object offset
2869 (descriptor-bits cold-layout)
2870 kind :layout)))
2871 (define-cold-fop (fop-immobile-obj-fixup)
2872 (let ((obj (pop-stack))
2873 (kind (pop-stack))
2874 (code-object (pop-stack))
2875 (offset (read-word-arg (fasl-input-stream))))
2876 (cold-fixup code-object offset
2877 (descriptor-bits (if (symbolp obj) (cold-intern obj) obj))
2878 kind :immobile-object))))
2880 #!+immobile-code
2881 (define-cold-fop (fop-named-call-fixup)
2882 (let* ((name (pop-stack))
2883 (fdefn (cold-fdefinition-object name))
2884 (kind (pop-stack))
2885 (code-object (pop-stack))
2886 (offset (read-word-arg (fasl-input-stream))))
2887 (cold-fixup code-object offset
2888 (+ (descriptor-bits fdefn)
2889 (ash sb!vm:fdefn-raw-addr-slot sb!vm:word-shift)
2890 (- sb!vm:other-pointer-lowtag))
2891 kind :named-call)))
2893 #!+immobile-code
2894 (define-cold-fop (fop-static-call-fixup)
2895 (let ((name (pop-stack))
2896 (kind (pop-stack))
2897 (code-object (pop-stack))
2898 (offset (read-word-arg (fasl-input-stream))))
2899 (push (list name kind code-object offset) *cold-static-call-fixups*)
2900 code-object))
2903 ;;;; sanity checking space layouts
2905 (defun check-spaces ()
2906 ;;; Co-opt type machinery to check for intersections...
2907 (let (types)
2908 (flet ((check (start end space)
2909 (unless (< start end)
2910 (error "Bogus space: ~A" space))
2911 (let ((type (specifier-type `(integer ,start ,end))))
2912 (dolist (other types)
2913 (unless (eq *empty-type* (type-intersection (cdr other) type))
2914 (error "Space overlap: ~A with ~A" space (car other))))
2915 (push (cons space type) types))))
2916 (check sb!vm:read-only-space-start sb!vm:read-only-space-end :read-only)
2917 (check sb!vm:static-space-start sb!vm:static-space-end :static)
2918 #!+gencgc
2919 (check sb!vm:default-dynamic-space-start
2920 (+ sb!vm:default-dynamic-space-start sb!vm:default-dynamic-space-size)
2921 :dynamic)
2922 #!+immobile-space
2923 ;; Must be a multiple of 32 because it makes the math a nicer
2924 ;; when computing word and bit index into the 'touched' bitmap.
2925 (assert (zerop (rem sb!vm:immobile-fixedobj-subspace-size
2926 (* 32 sb!vm:immobile-card-bytes))))
2927 #!-gencgc
2928 (progn
2929 (check sb!vm:dynamic-0-space-start sb!vm:dynamic-0-space-end :dynamic-0)
2930 (check sb!vm:dynamic-1-space-start sb!vm:dynamic-1-space-end :dynamic-1))
2931 #!+linkage-table
2932 (check sb!vm:linkage-table-space-start sb!vm:linkage-table-space-end :linkage-table))))
2934 ;;;; emitting C header file
2936 (defun tailwise-equal (string tail)
2937 (and (>= (length string) (length tail))
2938 (string= string tail :start1 (- (length string) (length tail)))))
2940 (defun write-boilerplate (*standard-output*)
2941 (format t "/*~%")
2942 (dolist (line
2943 '("This is a machine-generated file. Please do not edit it by hand."
2944 "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
2946 "This file contains low-level information about the"
2947 "internals of a particular version and configuration"
2948 "of SBCL. It is used by the C compiler to create a runtime"
2949 "support environment, an executable program in the host"
2950 "operating system's native format, which can then be used to"
2951 "load and run 'core' files, which are basically programs"
2952 "in SBCL's own format."))
2953 (format t " *~@[ ~A~]~%" line))
2954 (format t " */~%"))
2956 (defun c-name (string &optional strip)
2957 (delete #\+
2958 (substitute-if #\_ (lambda (c) (member c '(#\- #\/ #\%)))
2959 (remove-if (lambda (c) (position c strip))
2960 string))))
2962 (defun c-symbol-name (symbol &optional strip)
2963 (c-name (symbol-name symbol) strip))
2965 (defun write-makefile-features (*standard-output*)
2966 ;; propagating *SHEBANG-FEATURES* into the Makefiles
2967 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2968 sb-cold:*shebang-features*)
2969 #'string<))
2970 (format t "LISP_FEATURE_~A=1~%" shebang-feature-name)))
2972 (defun write-config-h (*standard-output*)
2973 ;; propagating *SHEBANG-FEATURES* into C-level #define's
2974 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2975 sb-cold:*shebang-features*)
2976 #'string<))
2977 (format t "#define LISP_FEATURE_~A~%" shebang-feature-name))
2978 (terpri)
2979 ;; and miscellaneous constants
2980 (format t "#define SBCL_VERSION_STRING ~S~%"
2981 (sb!xc:lisp-implementation-version))
2982 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
2983 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2984 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
2985 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2986 (format t "#define LISPOBJ(thing) thing~2%")
2987 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")
2988 (terpri))
2990 (defun write-constants-h (*standard-output*)
2991 ;; writing entire families of named constants
2992 (let ((constants nil))
2993 (dolist (package-name '("SB!VM"
2994 ;; We also propagate magic numbers
2995 ;; related to file format,
2996 ;; which live here instead of SB!VM.
2997 "SB!FASL"))
2998 (do-external-symbols (symbol (find-package package-name))
2999 (when (constantp symbol)
3000 (let ((name (symbol-name symbol)))
3001 (labels ( ;; shared machinery
3002 (record (string priority suffix)
3003 (push (list string
3004 priority
3005 (symbol-value symbol)
3006 suffix
3007 (documentation symbol 'variable))
3008 constants))
3009 ;; machinery for old-style CMU CL Lisp-to-C
3010 ;; arbitrary renaming, being phased out in favor of
3011 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
3012 ;; renaming
3013 (record-with-munged-name (prefix string priority)
3014 (record (concatenate
3015 'simple-string
3016 prefix
3017 (delete #\- (string-capitalize string)))
3018 priority
3019 ""))
3020 (maybe-record-with-munged-name (tail prefix priority)
3021 (when (tailwise-equal name tail)
3022 (record-with-munged-name prefix
3023 (subseq name 0
3024 (- (length name)
3025 (length tail)))
3026 priority)))
3027 ;; machinery for new-style SBCL Lisp-to-C naming
3028 (record-with-translated-name (priority large)
3029 (record (c-name name) priority
3030 (if large
3031 #!+(and win32 x86-64) "LLU"
3032 #!-(and win32 x86-64) "LU"
3033 "")))
3034 (maybe-record-with-translated-name (suffixes priority &key large)
3035 (when (some (lambda (suffix)
3036 (tailwise-equal name suffix))
3037 suffixes)
3038 (record-with-translated-name priority large))))
3039 (maybe-record-with-translated-name '("-LOWTAG") 0)
3040 (maybe-record-with-translated-name '("-WIDETAG" "-SHIFT") 1)
3041 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
3042 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
3043 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
3044 (maybe-record-with-translated-name '("SHAREABLE+" "SHAREABLE-NONSTD+") 4)
3045 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
3046 (maybe-record-with-translated-name '("-SIZE" "-INTERRUPTS") 6)
3047 (maybe-record-with-translated-name '("-START" "-END" "-PAGE-BYTES"
3048 "-CARD-BYTES" "-GRANULARITY")
3049 7 :large t)
3050 (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 8)
3051 (maybe-record-with-translated-name '("-CORE-SPACE-ID") 9)
3052 (maybe-record-with-translated-name '("-CORE-SPACE-ID-FLAG") 9)
3053 (maybe-record-with-translated-name '("-GENERATION+") 10))))))
3054 ;; KLUDGE: these constants are sort of important, but there's no
3055 ;; pleasing way to inform the code above about them. So we fake
3056 ;; it for now. nikodemus on #lisp (2004-08-09) suggested simply
3057 ;; exporting every numeric constant from SB!VM; that would work,
3058 ;; but the C runtime would have to be altered to use Lisp-like names
3059 ;; rather than the munged names currently exported. --njf, 2004-08-09
3060 (dolist (c '(sb!vm:n-word-bits sb!vm:n-word-bytes
3061 sb!vm:n-lowtag-bits sb!vm:lowtag-mask
3062 sb!vm:n-widetag-bits sb!vm:widetag-mask
3063 sb!vm:n-fixnum-tag-bits sb!vm:fixnum-tag-mask
3064 sb!vm:short-header-max-words))
3065 (push (list (c-symbol-name c)
3066 -1 ; invent a new priority
3067 (symbol-value c)
3069 nil)
3070 constants))
3071 ;; One more symbol that doesn't fit into the code above.
3072 (let ((c 'sb!impl::+magic-hash-vector-value+))
3073 (push (list (c-symbol-name c)
3075 (symbol-value c)
3076 #!+(and win32 x86-64) "LLU"
3077 #!-(and win32 x86-64) "LU"
3078 nil)
3079 constants))
3080 ;; And still one more
3081 #!+64-bit
3082 (let ((c 'sb!vm::immediate-widetags-mask))
3083 (push (list (c-symbol-name c)
3085 (logior (ash 1 (ash sb!vm:character-widetag -2))
3086 (ash 1 (ash sb!vm:single-float-widetag -2))
3087 (ash 1 (ash sb!vm:unbound-marker-widetag -2)))
3088 "LU"
3089 nil)
3090 constants))
3091 (setf constants
3092 (sort constants
3093 (lambda (const1 const2)
3094 (if (= (second const1) (second const2))
3095 (if (= (third const1) (third const2))
3096 (string< (first const1) (first const2))
3097 (< (third const1) (third const2)))
3098 (< (second const1) (second const2))))))
3099 (let ((prev-priority (second (car constants))))
3100 (dolist (const constants)
3101 (destructuring-bind (name priority value suffix doc) const
3102 (unless (= prev-priority priority)
3103 (terpri)
3104 (setf prev-priority priority))
3105 (when (minusp value)
3106 (error "stub: negative values unsupported"))
3107 (format t "#define ~A ~A~A /* 0x~X ~@[ -- ~A ~]*/~%" name value suffix value doc))))
3108 (terpri))
3110 ;; writing information about internal errors
3111 ;; Assembly code needs only the constants for UNDEFINED_[ALIEN_]FUN_ERROR
3112 ;; but to avoid imparting that knowledge here, we'll expose all error
3113 ;; number constants except for OBJECT-NOT-<x>-ERROR ones.
3114 (loop for (description name) across sb!c:+backend-internal-errors+
3115 for i from 0
3116 when (stringp description)
3117 do (format t "#define ~A ~D~%" (c-symbol-name name) i))
3119 ;; I'm not really sure why this is in SB!C, since it seems
3120 ;; conceptually like something that belongs to SB!VM. In any case,
3121 ;; it's needed C-side.
3122 (format t "#define BACKEND_PAGE_BYTES ~DLU~%" sb!c:*backend-page-bytes*)
3124 (terpri)
3126 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
3127 ;; platforms. If we export this from the SB!VM package, it gets
3128 ;; written out as #define trap_PseudoAtomic, which is confusing as
3129 ;; the runtime treats trap_ as the prefix for illegal instruction
3130 ;; type things. We therefore don't export it, but instead do
3131 #!+sparc
3132 (when (boundp 'sb!vm::pseudo-atomic-trap)
3133 (format t
3134 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
3135 sb!vm::pseudo-atomic-trap)
3136 (terpri))
3137 ;; possibly this is another candidate for a rename (to
3138 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
3139 ;; [possibly applicable to other platforms])
3141 #!+sb-safepoint
3142 (format t "#define GC_SAFEPOINT_PAGE_ADDR ((void*)0x~XUL) /* ~:*~A */~%"
3143 sb!vm:gc-safepoint-page-addr)
3145 (dolist (symbol '(sb!vm::float-traps-byte
3146 sb!vm::float-exceptions-byte
3147 sb!vm::float-sticky-bits
3148 sb!vm::float-rounding-mode))
3149 (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
3150 (c-symbol-name symbol)
3151 (sb!xc:byte-position (symbol-value symbol)))
3152 (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
3153 (c-symbol-name symbol)
3154 (sb!xc:mask-field (symbol-value symbol) -1))))
3156 (defun write-errnames-h (stream)
3157 ;; C code needs strings for describe_internal_error()
3158 (format stream "#define INTERNAL_ERROR_NAMES ~{\\~%~S~^, ~}~2%"
3159 (map 'list 'sb!kernel::!c-stringify-internal-error
3160 sb!c:+backend-internal-errors+))
3161 (format stream "#define INTERNAL_ERROR_NARGS {~{~S~^, ~}}~2%"
3162 (map 'list #'cddr sb!c:+backend-internal-errors+)))
3164 #!+sb-ldb
3165 (defun write-tagnames-h (out)
3166 (labels
3167 ((pretty-name (symbol strip)
3168 (let ((name (string-downcase symbol)))
3169 (substitute #\Space #\-
3170 (subseq name 0 (- (length name) (length strip))))))
3171 (list-sorted-tags (tail)
3172 (loop for symbol being the external-symbols of "SB!VM"
3173 when (and (constantp symbol)
3174 (tailwise-equal (string symbol) tail)
3175 ;; FIXME: these symbols are obsolete
3176 (not (member symbol
3177 '(sb!vm:simple-fun-header-widetag
3178 sb!vm:closure-header-widetag))))
3179 collect symbol into tags
3180 finally (return (sort tags #'< :key #'symbol-value))))
3181 (write-tags (visibility kind limit ash-count)
3182 (format out "~%~Aconst char *~(~A~)_names[] = {~%"
3183 visibility (subseq kind 1))
3184 (let ((tags (list-sorted-tags kind)))
3185 (dotimes (i limit)
3186 (if (eql i (ash (or (symbol-value (first tags)) -1) ash-count))
3187 (format out " \"~A\"" (pretty-name (pop tags) kind))
3188 (format out " \"unknown [~D]\"" i))
3189 (unless (eql i (1- limit))
3190 (write-string "," out))
3191 (terpri out)))
3192 (write-line "};" out)))
3193 (write-tags "static " "-LOWTAG" sb!vm:lowtag-limit 0)
3194 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
3195 ;; ending with the same 2 bits. (#b10)
3196 (write-tags "" "-WIDETAG" (ash (1+ sb!vm:widetag-mask) -2) -2))
3197 ;; Inform print_otherptr() of all array types that it's too dumb to print
3198 (let ((array-type-bits (make-array 32 :initial-element 0)))
3199 (flet ((toggle (b)
3200 (multiple-value-bind (ofs bit) (floor b 8)
3201 (setf (aref array-type-bits ofs) (ash 1 bit)))))
3202 (dovector (saetp sb!vm:*specialized-array-element-type-properties*)
3203 (unless (or (typep (sb!vm:saetp-ctype saetp) 'character-set-type)
3204 (eq (sb!vm:saetp-specifier saetp) t))
3205 (toggle (sb!vm:saetp-typecode saetp))
3206 (awhen (sb!vm:saetp-complex-typecode saetp) (toggle it)))))
3207 (format out
3208 "~%static unsigned char unprintable_array_types[32] =~% {~{~d~^,~}};~%"
3209 (coerce array-type-bits 'list)))
3210 (dolist (prim-obj '(symbol ratio complex sb!vm::code simple-fun
3211 closure funcallable-instance
3212 weak-pointer fdefn sb!vm::value-cell))
3213 (format out "static char *~A_slots[] = {~%~{ \"~A: \",~} NULL~%};~%"
3214 (c-name (string-downcase prim-obj))
3215 (mapcar (lambda (x) (c-name (string-downcase (sb!vm:slot-name x))))
3216 (remove-if 'sb!vm:slot-rest-p
3217 (sb!vm::primitive-object-slots
3218 (find prim-obj sb!vm:*primitive-objects*
3219 :key 'sb!vm:primitive-object-name))))))
3220 (values))
3222 (defun write-cast-operator (name c-name lowtag)
3223 (format t "static inline struct ~A* ~A(lispobj obj) {
3224 return (struct ~A*)(obj - ~D);~%}~%" c-name name c-name lowtag))
3226 (defun write-primitive-object (obj *standard-output*)
3227 (let* ((name (sb!vm:primitive-object-name obj))
3228 (c-name (c-name (string-downcase name)))
3229 (slots (sb!vm:primitive-object-slots obj))
3230 (lowtag (or (symbol-value (sb!vm:primitive-object-lowtag obj)) 0)))
3231 ;; writing primitive object layouts
3232 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3233 (format t "struct ~A {~%" c-name)
3234 (when (sb!vm:primitive-object-widetag obj)
3235 (format t " lispobj header;~%"))
3236 (dolist (slot slots)
3237 (format t " ~A ~A~@[[1]~];~%"
3238 (getf (sb!vm:slot-options slot) :c-type "lispobj")
3239 (c-name (string-downcase (sb!vm:slot-name slot)))
3240 (sb!vm:slot-rest-p slot)))
3241 (format t "};~%")
3242 (when (member name '(cons vector symbol fdefn))
3243 (write-cast-operator name c-name lowtag))
3244 (format t "~%#else /* LANGUAGE_ASSEMBLY */~2%")
3245 (format t "/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
3246 (format t " * so they work directly on tagged addresses. */~2%")
3247 (dolist (slot slots)
3248 (format t "#define ~A_~A_OFFSET ~D~%"
3249 (c-symbol-name name)
3250 (c-symbol-name (sb!vm:slot-name slot))
3251 (- (* (sb!vm:slot-offset slot) sb!vm:n-word-bytes) lowtag))))
3252 (format t "~%#endif /* LANGUAGE_ASSEMBLY */~2%"))
3254 (defun write-structure-object (dd *standard-output*)
3255 (flet ((cstring (designator) (c-name (string-downcase designator))))
3256 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3257 (format t "struct ~A {~%" (cstring (dd-name dd)))
3258 (format t " lispobj header; // = word_0_~%")
3259 ;; "self layout" slots are named '_layout' instead of 'layout' so that
3260 ;; classoid's expressly declared layout isn't renamed as a special-case.
3261 #!-compact-instance-header (format t " lispobj _layout;~%")
3262 ;; Output exactly the number of Lisp words consumed by the structure,
3263 ;; no more, no less. C code can always compute the padded length from
3264 ;; the precise length, but the other way doesn't work.
3265 (let ((names
3266 (coerce (loop for i from sb!vm:instance-data-start below (dd-length dd)
3267 collect (list (format nil "word_~D_" (1+ i))))
3268 'vector)))
3269 (dolist (slot (dd-slots dd))
3270 (let ((cell (aref names (- (dsd-index slot) sb!vm:instance-data-start)))
3271 (name (cstring (dsd-name slot))))
3272 (if (eq (dsd-raw-type slot) t)
3273 (rplaca cell name)
3274 (rplacd cell name))))
3275 (loop for slot across names
3276 do (format t " lispobj ~A;~@[ // ~A~]~%" (car slot) (cdr slot))))
3277 (format t "};~%")
3278 (when (member (dd-name dd) '(layout))
3279 (write-cast-operator (dd-name dd) (cstring (dd-name dd))
3280 sb!vm:instance-pointer-lowtag))
3281 (format t "~%#endif /* LANGUAGE_ASSEMBLY */~2%")))
3283 (defun write-static-symbols (stream)
3284 (dolist (symbol (cons nil (coerce sb!vm:+static-symbols+ 'list)))
3285 ;; FIXME: It would be nice to use longer names than NIL and
3286 ;; (particularly) T in #define statements.
3287 (format stream "#define ~A LISPOBJ(0x~X)~%"
3288 ;; FIXME: It would be nice not to need to strip anything
3289 ;; that doesn't get stripped always by C-SYMBOL-NAME.
3290 (c-symbol-name symbol "%*.!")
3291 (if *static* ; if we ran GENESIS
3292 ;; We actually ran GENESIS, use the real value.
3293 (descriptor-bits (cold-intern symbol))
3294 ;; We didn't run GENESIS, so guess at the address.
3295 (+ sb!vm:static-space-start
3296 sb!vm:n-word-bytes
3297 sb!vm:other-pointer-lowtag
3298 (if symbol (sb!vm:static-symbol-offset symbol) 0)))))
3299 (loop for symbol in sb!vm::+c-callable-fdefns+
3300 for index from 0
3302 (format stream "#define ~A_FDEFN LISPOBJ(0x~X)~%"
3303 (c-symbol-name symbol)
3304 (if *static* ; if we ran GENESIS
3305 ;; We actually ran GENESIS, use the real value.
3306 (descriptor-bits (cold-fdefinition-object symbol))
3307 ;; We didn't run GENESIS, so guess at the address.
3308 (+ sb!vm:static-space-start
3309 sb!vm:n-word-bytes
3310 sb!vm:other-pointer-lowtag
3311 (* (length sb!vm:+static-symbols+)
3312 (sb!vm:pad-data-block sb!vm:symbol-size))
3313 (* index (sb!vm:pad-data-block sb!vm:fdefn-size)))))))
3315 (defun write-sc-offset-coding (stream)
3316 (flet ((write-array (name bytes)
3317 (format stream "static struct sc_offset_byte ~A[] = {~@
3318 ~{ {~{ ~2D, ~2D ~}}~^,~%~}~@
3319 };~2%"
3320 name
3321 (mapcar (lambda (byte)
3322 (list (byte-size byte) (byte-position byte)))
3323 bytes))))
3324 (format stream "struct sc_offset_byte {
3325 int size;
3326 int position;
3327 };~2%")
3328 (write-array "sc_offset_sc_number_bytes" sb!c::+sc-offset-scn-bytes+)
3329 (write-array "sc_offset_offset_bytes" sb!c::+sc-offset-offset-bytes+)))
3331 ;;;; writing map file
3333 ;;; Write a map file describing the cold load. Some of this
3334 ;;; information is subject to change due to relocating GC, but even so
3335 ;;; it can be very handy when attempting to troubleshoot the early
3336 ;;; stages of cold load.
3337 (defun write-map (*standard-output*)
3338 (let ((*print-pretty* nil)
3339 (*print-case* :upcase))
3340 (format t "assembler routines defined in core image:~2%")
3341 (dolist (routine (sort (copy-list *cold-assembler-routines*) #'<
3342 :key #'cdr))
3343 (format t "~8,'0X: ~S~%" (cdr routine) (car routine)))
3344 (let ((fdefns nil)
3345 (funs nil)
3346 (undefs nil))
3347 (maphash (lambda (name fdefn &aux (fun (cold-fdefn-fun fdefn)))
3348 (push (list (- (descriptor-bits fdefn) (descriptor-lowtag fdefn))
3349 name) fdefns)
3350 (if (cold-null fun)
3351 (push name undefs)
3352 (push (list (- (descriptor-bits fun) (descriptor-lowtag fun))
3353 name) funs)))
3354 *cold-fdefn-objects*)
3355 (format t "~%~|~%fdefns (native pointer):
3356 ~:{~%~8,'0X: ~S~}~%" (sort fdefns #'< :key #'car))
3357 (format t "~%~|~%initially defined functions (native pointer):
3358 ~:{~%~8,'0X: ~S~}~%" (sort funs #'< :key #'car))
3359 (format t
3360 "~%~|
3361 (a note about initially undefined function references: These functions
3362 are referred to by code which is installed by GENESIS, but they are not
3363 installed by GENESIS. This is not necessarily a problem; functions can
3364 be defined later, by cold init toplevel forms, or in files compiled and
3365 loaded at warm init, or elsewhere. As long as they are defined before
3366 they are called, everything should be OK. Things are also OK if the
3367 cross-compiler knew their inline definition and used that everywhere
3368 that they were called before the out-of-line definition is installed,
3369 as is fairly common for structure accessors.)
3370 initially undefined function references:~2%")
3372 (setf undefs (sort undefs #'string< :key #'fun-name-block-name))
3373 (dolist (name undefs)
3374 (format t "~8,'0X: ~S~%"
3375 (descriptor-bits (gethash name *cold-fdefn-objects*))
3376 name)))
3378 (format t "~%~|~%layout names:~2%")
3379 (dolist (x (sort-cold-layouts))
3380 (let* ((des (cdr x))
3381 (inherits (read-slot des *host-layout-of-layout* :inherits)))
3382 (format t "~8,'0X: ~S[~D]~%~10T~:S~%" (descriptor-bits des) (car x)
3383 (cold-layout-length des) (listify-cold-inherits inherits))))
3385 (format t "~%~|~%parsed type specifiers:~2%")
3386 (mapc (lambda (cell)
3387 (format t "~X: ~S~%" (descriptor-bits (cdr cell)) (car cell)))
3388 (sort (%hash-table-alist *ctype-cache*) #'<
3389 :key (lambda (x) (descriptor-bits (cdr x))))))
3390 (values))
3392 ;;;; writing core file
3394 (defvar *core-file*)
3395 (defvar *data-page*)
3397 ;;; magic numbers to identify entries in a core file
3399 ;;; (In case you were wondering: No, AFAIK there's no special magic about
3400 ;;; these which requires them to be in the 38xx range. They're just
3401 ;;; arbitrary words, tested not for being in a particular range but just
3402 ;;; for equality. However, if you ever need to look at a .core file and
3403 ;;; figure out what's going on, it's slightly convenient that they're
3404 ;;; all in an easily recognizable range, and displacing the range away from
3405 ;;; zero seems likely to reduce the chance that random garbage will be
3406 ;;; misinterpreted as a .core file.)
3407 (defconstant build-id-core-entry-type-code 3860)
3408 (defconstant new-directory-core-entry-type-code 3861)
3409 (defconstant initial-fun-core-entry-type-code 3863)
3410 (defconstant page-table-core-entry-type-code 3880)
3411 (defconstant end-core-entry-type-code 3840)
3413 (declaim (ftype (function (sb!vm:word) sb!vm:word) write-word))
3414 (defun write-word (num)
3415 (ecase sb!c:*backend-byte-order*
3416 (:little-endian
3417 (dotimes (i sb!vm:n-word-bytes)
3418 (write-byte (ldb (byte 8 (* i 8)) num) *core-file*)))
3419 (:big-endian
3420 (dotimes (i sb!vm:n-word-bytes)
3421 (write-byte (ldb (byte 8 (* (- (1- sb!vm:n-word-bytes) i) 8)) num)
3422 *core-file*))))
3423 num)
3425 (defun output-gspace (gspace)
3426 (force-output *core-file*)
3427 (let* ((posn (file-position *core-file*))
3428 (bytes (* (gspace-free-word-index gspace) sb!vm:n-word-bytes))
3429 (pages (ceiling bytes sb!c:*backend-page-bytes*))
3430 (total-bytes (* pages sb!c:*backend-page-bytes*)))
3432 (file-position *core-file*
3433 (* sb!c:*backend-page-bytes* (1+ *data-page*)))
3434 (format t
3435 "writing ~S byte~:P [~S page~:P] from ~S~%"
3436 total-bytes
3437 pages
3438 gspace)
3439 (force-output)
3441 ;; Note: It is assumed that the GSPACE allocation routines always
3442 ;; allocate whole pages (of size *target-page-size*) and that any
3443 ;; empty gspace between the free pointer and the end of page will
3444 ;; be zero-filled. This will always be true under Mach on machines
3445 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3446 ;; 8K).
3447 (write-bigvec-as-sequence (gspace-data gspace)
3448 *core-file*
3449 :end total-bytes
3450 :pad-with-zeros t)
3451 (force-output *core-file*)
3452 (file-position *core-file* posn)
3454 ;; Write part of a (new) directory entry which looks like this:
3455 ;; GSPACE IDENTIFIER
3456 ;; WORD COUNT
3457 ;; DATA PAGE
3458 ;; ADDRESS
3459 ;; PAGE COUNT
3460 (write-word (gspace-identifier gspace))
3461 (write-word (gspace-free-word-index gspace))
3462 (write-word *data-page*)
3463 (multiple-value-bind (floor rem)
3464 (floor (gspace-byte-address gspace) sb!c:*backend-page-bytes*)
3465 (aver (zerop rem))
3466 (write-word floor))
3467 (write-word pages)
3469 (incf *data-page* pages)))
3471 ;;; Create a core file created from the cold loaded image. (This is
3472 ;;; the "initial core file" because core files could be created later
3473 ;;; by executing SAVE-LISP in a running system, perhaps after we've
3474 ;;; added some functionality to the system.)
3475 (declaim (ftype (function (string)) write-initial-core-file))
3476 (defun write-initial-core-file (filename)
3478 (let ((filenamestring (namestring filename))
3479 (*data-page* 0))
3481 (format t "[building initial core file in ~S: ~%" filenamestring)
3482 (force-output)
3484 (with-open-file (*core-file* filenamestring
3485 :direction :output
3486 :element-type '(unsigned-byte 8)
3487 :if-exists :rename-and-delete)
3489 ;; Write the magic number.
3490 (write-word core-magic)
3492 ;; Write the build ID.
3493 (write-word build-id-core-entry-type-code)
3494 (let ((build-id (with-open-file (s "output/build-id.tmp")
3495 (read s))))
3496 (declare (type simple-string build-id))
3497 (/show build-id (length build-id))
3498 ;; Write length of build ID record: BUILD-ID-CORE-ENTRY-TYPE-CODE
3499 ;; word, this length word, and one word for each char of BUILD-ID.
3500 (write-word (+ 2 (length build-id)))
3501 (dovector (char build-id)
3502 ;; (We write each character as a word in order to avoid
3503 ;; having to think about word alignment issues in the
3504 ;; sbcl-0.7.8 version of coreparse.c.)
3505 (write-word (sb!xc:char-code char))))
3507 ;; Write the New Directory entry header.
3508 (write-word new-directory-core-entry-type-code)
3509 (let ((spaces (nconc (list *read-only* *static*)
3510 #!+immobile-space
3511 (list *immobile-fixedobj* *immobile-varyobj*)
3512 (list *dynamic*))))
3513 ;; length = (5 words/space) * N spaces + 2 for header.
3514 (write-word (+ (* (length spaces) 5) 2))
3515 (mapc #'output-gspace spaces))
3517 ;; Write the initial function.
3518 (write-word initial-fun-core-entry-type-code)
3519 (write-word 3)
3520 (let* ((cold-name (cold-intern '!cold-init))
3521 (initial-fun
3522 (cold-fdefn-fun (cold-fdefinition-object cold-name))))
3523 (format t
3524 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
3525 (descriptor-bits initial-fun))
3526 (write-word (descriptor-bits initial-fun)))
3528 ;; Write the End entry.
3529 (write-word end-core-entry-type-code)
3530 (write-word 2)))
3532 (format t "done]~%")
3533 (force-output)
3534 (/show "leaving WRITE-INITIAL-CORE-FILE")
3535 (values))
3537 ;;;; the actual GENESIS function
3539 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3540 ;;; and/or information about a Lisp core, therefrom.
3542 ;;; input file arguments:
3543 ;;; SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
3544 ;;; *tab* *characters* *converted* *to* *spaces*. (We push
3545 ;;; responsibility for removing tabs out to the caller it's
3546 ;;; trivial to remove them using UNIX command line tools like
3547 ;;; sed, whereas it's a headache to do it portably in Lisp because
3548 ;;; #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
3549 ;;; a core file cannot be built (but a C header file can be).
3551 ;;; output files arguments (any of which may be NIL to suppress output):
3552 ;;; CORE-FILE-NAME gets a Lisp core.
3553 ;;; C-HEADER-DIR-NAME gets the path in which to place generated headers
3554 ;;; MAP-FILE-NAME gets the name of the textual 'cold-sbcl.map' file
3555 (defun sb-cold:genesis (&key object-file-names preload-file
3556 core-file-name c-header-dir-name map-file-name
3557 symbol-table-file-name (verbose t))
3558 (declare (ignorable symbol-table-file-name))
3559 (declare (special core-file-name))
3561 (when verbose
3562 (format t
3563 "~&beginning GENESIS, ~A~%"
3564 (if core-file-name
3565 ;; Note: This output summarizing what we're doing is
3566 ;; somewhat telegraphic in style, not meant to imply that
3567 ;; we're not e.g. also creating a header file when we
3568 ;; create a core.
3569 (format nil "creating core ~S" core-file-name)
3570 (format nil "creating headers in ~S" c-header-dir-name))))
3572 (let ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
3574 #!-(or sb-dynamic-core crossbuild-test)
3575 (when core-file-name
3576 (if symbol-table-file-name
3577 (load-cold-foreign-symbol-table symbol-table-file-name)
3578 (error "can't output a core file without symbol table file input")))
3580 ;; Now that we've successfully read our only input file (by
3581 ;; loading the symbol table, if any), it's a good time to ensure
3582 ;; that there'll be someplace for our output files to go when
3583 ;; we're done.
3584 (flet ((frob (filename)
3585 (when filename
3586 (ensure-directories-exist filename :verbose t))))
3587 (frob core-file-name)
3588 (frob map-file-name))
3590 ;; (This shouldn't matter in normal use, since GENESIS normally
3591 ;; only runs once in any given Lisp image, but it could reduce
3592 ;; confusion if we ever experiment with running, tweaking, and
3593 ;; rerunning genesis interactively.)
3594 (do-all-symbols (sym)
3595 (remprop sym 'cold-intern-info))
3597 (check-spaces)
3599 (let ((*foreign-symbol-placeholder-value* (if core-file-name nil 0))
3600 (*load-time-value-counter* 0)
3601 (*cold-fdefn-objects* (make-hash-table :test 'equal))
3602 (*cold-symbols* (make-hash-table :test 'eql)) ; integer keys
3603 (*cold-package-symbols* (make-hash-table :test 'equal)) ; string keys
3604 (*read-only* (make-gspace :read-only
3605 read-only-core-space-id
3606 sb!vm:read-only-space-start))
3607 (*static* (make-gspace :static
3608 static-core-space-id
3609 sb!vm:static-space-start))
3610 #!+immobile-space
3611 (*immobile-fixedobj* (make-gspace :immobile-fixedobj
3612 immobile-fixedobj-core-space-id
3613 sb!vm:immobile-space-start))
3614 #!+immobile-space
3615 (*immobile-varyobj* (make-gspace :immobile-varyobj
3616 immobile-varyobj-core-space-id
3617 (+ sb!vm:immobile-space-start
3618 sb!vm:immobile-fixedobj-subspace-size)))
3619 (*dynamic* (make-gspace :dynamic
3620 dynamic-core-space-id
3621 #!+gencgc sb!vm:default-dynamic-space-start
3622 #!-gencgc sb!vm:dynamic-0-space-start))
3623 (*nil-descriptor*)
3624 (*simple-vector-0-descriptor*)
3625 (*known-structure-classoids* nil)
3626 (*classoid-cells* (make-hash-table :test 'eq))
3627 (*ctype-cache* (make-hash-table :test 'equal))
3628 (*cold-layouts* (make-hash-table :test 'eq)) ; symbol -> cold-layout
3629 (*cold-layout-names* (make-hash-table :test 'eql)) ; addr -> symbol
3630 (*!cold-defconstants* nil)
3631 (*!cold-defuns* nil)
3632 ;; '*COLD-METHODS* is never seen in the target, so does not need
3633 ;; to adhere to the #\! convention for automatic uninterning.
3634 (*cold-methods* nil)
3635 (*!cold-toplevels* nil)
3636 *cold-static-call-fixups*
3637 *cold-assembler-fixups*
3638 *cold-assembler-routines*
3639 *cold-assembler-objects*
3640 (*code-fixup-notes* (make-hash-table))
3641 (*deferred-known-fun-refs* nil))
3643 (setf *nil-descriptor* (make-nil-descriptor)
3644 *simple-vector-0-descriptor* (vector-in-core nil))
3646 ;; If we're given a preload file, it contains tramps and whatnot
3647 ;; that must be loaded before we create any FDEFNs. It can in
3648 ;; theory be loaded any time between binding
3649 ;; *COLD-ASSEMBLER-ROUTINES* above and calling
3650 ;; INITIALIZE-STATIC-SPACE below.
3651 (when preload-file
3652 (cold-load preload-file))
3654 ;; Prepare for cold load.
3655 (initialize-layouts)
3656 (initialize-packages)
3657 (initialize-static-space)
3659 ;; Initialize the *COLD-SYMBOLS* system with the information
3660 ;; from common-lisp-exports.lisp-expr.
3661 ;; Packages whose names match SB!THING were set up on the host according
3662 ;; to "package-data-list.lisp-expr" which expresses the desired target
3663 ;; package configuration, so we can just mirror the host into the target.
3664 ;; But by waiting to observe calls to COLD-INTERN that occur during the
3665 ;; loading of the cross-compiler's outputs, it is possible to rid the
3666 ;; target of accidental leftover symbols, not that it wouldn't also be
3667 ;; a good idea to clean up package-data-list once in a while.
3668 (dolist (exported-name
3669 (sb-cold:read-from-file "common-lisp-exports.lisp-expr"))
3670 (cold-intern (intern exported-name *cl-package*) :access :external))
3672 ;; Create SB!KERNEL::*TYPE-CLASSES* as an array of NIL
3673 (cold-set (cold-intern 'sb!kernel::*type-classes*)
3674 (vector-in-core (make-list (length sb!kernel::*type-classes*))))
3676 ;; Load the remaining assembler code into immobile space
3677 #!+immobile-space
3678 (flet ((assembler-file-p (name) (tailwise-equal (namestring name) ".assem-obj")))
3679 (dolist (file-name (remove-if-not #'assembler-file-p object-file-names))
3680 (cold-load file-name))
3681 (setf object-file-names (remove-if #'assembler-file-p object-file-names)))
3683 ;; Cold load.
3684 (dolist (file-name object-file-names)
3685 (cold-load file-name))
3687 (when *known-structure-classoids*
3688 (let ((dd-layout (find-layout 'defstruct-description)))
3689 (dolist (defstruct-args *known-structure-classoids*)
3690 (let* ((dd (first defstruct-args))
3691 (name (warm-symbol (read-slot dd dd-layout :name)))
3692 (layout (gethash name *cold-layouts*)))
3693 (aver layout)
3694 (write-slots layout *host-layout-of-layout* :info dd))))
3695 (format t "~&; SB!Loader: (~D~@{+~D~}) structs/consts/funs/methods/other~%"
3696 (length *known-structure-classoids*)
3697 (length *!cold-defconstants*)
3698 (length *!cold-defuns*)
3699 (reduce #'+ *cold-methods* :key (lambda (x) (length (cdr x))))
3700 (length *!cold-toplevels*)))
3702 (dolist (symbol '(*!cold-defconstants* *!cold-defuns* *!cold-toplevels*))
3703 (cold-set symbol (list-to-core (nreverse (symbol-value symbol))))
3704 (makunbound symbol)) ; so no further PUSHes can be done
3706 (cold-set
3707 'sb!pcl::*!trivial-methods*
3708 (list-to-core
3709 (loop for (gf-name . methods) in *cold-methods*
3710 collect
3711 (cold-cons
3712 (cold-intern gf-name)
3713 (vector-in-core
3714 (loop for (class qual lambda-list fun source-loc)
3715 ;; Methods must be sorted because we invoke
3716 ;; only the first applicable one.
3717 in (stable-sort methods #'> ; highest depthoid first
3718 :key (lambda (method)
3719 (class-depthoid (car method))))
3720 collect
3721 (cold-list (cold-intern
3722 (and (null qual) (predicate-for-specializer class)))
3724 (cold-intern class)
3725 (cold-intern qual)
3726 lambda-list source-loc)))))))
3728 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3729 (resolve-deferred-known-funs)
3730 (resolve-assembler-fixups)
3731 (foreign-symbols-to-core)
3732 #!+(or x86 immobile-space)
3733 (dolist (pair (sort (%hash-table-alist *code-fixup-notes*) #'< :key #'car))
3734 (write-wordindexed (make-random-descriptor (car pair))
3735 sb!vm::code-fixups-slot
3736 #!+x86 (ub32-vector-in-core (cdr pair))
3737 #!+x86-64 (number-to-core
3738 (sb!c::pack-code-fixup-locs
3739 (sort (cdr pair) #'<)))))
3740 (when *cold-assembler-objects*
3741 (unless (eq *read-only* ; "Stayin' Alive"
3742 (descriptor-gspace (car *cold-assembler-objects*)))
3743 (cold-set (cold-intern '*assembler-objects*)
3744 (vector-in-core (nreverse *cold-assembler-objects*)))))
3745 (finish-symbols)
3746 (/show "back from FINISH-SYMBOLS")
3747 (finalize-load-time-value-noise)
3749 ;; Tell the target Lisp how much stuff we've allocated.
3750 (flet ((free-pointer (space)
3751 (make-random-descriptor
3752 (+ (ash (gspace-free-word-index space) sb!vm:word-shift)
3753 (gspace-byte-address space)))))
3754 (cold-set 'sb!vm:*read-only-space-free-pointer* (free-pointer *read-only*))
3755 (cold-set 'sb!vm:*static-space-free-pointer* (free-pointer *static*))
3756 #!+immobile-space
3757 (progn (cold-set 'sb!vm:*immobile-fixedobj-free-pointer*
3758 (free-pointer *immobile-fixedobj*))
3759 (cold-set 'sb!vm:*immobile-space-free-pointer*
3760 (free-pointer *immobile-varyobj*))))
3762 (/show "done setting free pointers")
3764 ;; Write results to files.
3765 (when map-file-name
3766 (with-open-file (stream map-file-name :direction :output :if-exists :supersede)
3767 (write-map stream)))
3768 (let ((filename (format nil "~A/Makefile.features" c-header-dir-name)))
3769 (ensure-directories-exist filename)
3770 (with-open-file (stream filename :direction :output :if-exists :supersede)
3771 (write-makefile-features stream)))
3773 (macrolet ((out-to (name &body body) ; write boilerplate and inclusion guard
3774 `(with-open-file (stream (format nil "~A/~A.h" c-header-dir-name ,name)
3775 :direction :output :if-exists :supersede)
3776 (write-boilerplate stream)
3777 (format stream
3778 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~:*~A~%"
3779 (c-name (string-upcase ,name)))
3780 ,@body
3781 (format stream "#endif~%"))))
3782 (out-to "config" (write-config-h stream))
3783 (out-to "constants" (write-constants-h stream))
3784 (out-to "errnames" (write-errnames-h stream))
3785 (out-to "gc-tables" (sb!vm::write-gc-tables stream))
3786 #!+sb-ldb
3787 (out-to "tagnames" (write-tagnames-h stream))
3788 (let ((structs (sort (copy-list sb!vm:*primitive-objects*) #'string<
3789 :key #'sb!vm:primitive-object-name)))
3790 (dolist (obj structs)
3791 (out-to (string-downcase (sb!vm:primitive-object-name obj))
3792 (write-primitive-object obj stream)))
3793 (out-to "primitive-objects"
3794 (dolist (obj structs)
3795 (format stream "~&#include \"~A.h\"~%"
3796 (string-downcase (sb!vm:primitive-object-name obj))))))
3797 (dolist (class '(classoid defstruct-description hash-table layout package
3798 sb!c::compiled-debug-info sb!c::compiled-debug-fun))
3799 (out-to (string-downcase class)
3800 (write-structure-object (layout-info (find-layout class)) stream)))
3801 (out-to "static-symbols" (write-static-symbols stream))
3802 (out-to "sc-offset" (write-sc-offset-coding stream)))
3804 (when core-file-name
3805 (write-initial-core-file core-file-name)))))
3807 ;;; Invert the action of HOST-CONSTANT-TO-CORE. If STRICTP is given as NIL,
3808 ;;; then we can produce a host object even if it is not a faithful rendition.
3809 (defun host-object-from-core (descriptor &optional (strictp t))
3810 (named-let recurse ((x descriptor))
3811 (when (cold-null x)
3812 (return-from recurse nil))
3813 (when (eq (descriptor-gspace x) :load-time-value)
3814 (error "Can't warm a deferred LTV placeholder"))
3815 (when (is-fixnum-lowtag (descriptor-lowtag x))
3816 (return-from recurse (descriptor-fixnum x)))
3817 (ecase (descriptor-lowtag x)
3818 (#.sb!vm:instance-pointer-lowtag
3819 (if strictp (error "Can't invert INSTANCE type") "#<instance>"))
3820 (#.sb!vm:list-pointer-lowtag
3821 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
3822 (#.sb!vm:fun-pointer-lowtag
3823 (if strictp
3824 (error "Can't map cold-fun -> warm-fun")
3825 (let ((name (read-wordindexed x sb!vm:simple-fun-name-slot)))
3826 `(function ,(recurse name)))))
3827 (#.sb!vm:other-pointer-lowtag
3828 (let ((widetag (logand (descriptor-bits (read-memory x))
3829 sb!vm:widetag-mask)))
3830 (ecase widetag
3831 (#.sb!vm:symbol-widetag
3832 (if strictp
3833 (warm-symbol x)
3834 (or (gethash (descriptor-bits x) *cold-symbols*) ; first try
3835 (make-symbol
3836 (recurse (read-wordindexed x sb!vm:symbol-name-slot))))))
3837 (#.sb!vm:simple-base-string-widetag (base-string-from-core x))
3838 (#.sb!vm:simple-vector-widetag (vector-from-core x #'recurse))
3839 (#.sb!vm:bignum-widetag (bignum-from-core x))))))))