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