Enforce consistency between DEFINE-COLD-FOP and DEFINE-FOP.
[sbcl.git] / src / compiler / generic / genesis.lisp
blobd74db27b678bf04e1772cf04daede5fd81dac4a9
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. In particular,
19 ;;;; structure slot accessors are not set up. Slot accessors are
20 ;;;; available at cold init time because they're usually compiled
21 ;;;; inline. They're not available as out-of-line functions until the
22 ;;;; toplevel forms installing them have run.)
24 ;;;; This software is part of the SBCL system. See the README file for
25 ;;;; more information.
26 ;;;;
27 ;;;; This software is derived from the CMU CL system, which was
28 ;;;; written at Carnegie Mellon University and released into the
29 ;;;; public domain. The software is in the public domain and is
30 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
31 ;;;; files for more information.
33 (in-package "SB!FASL")
35 ;;; a magic number used to identify our core files
36 (defconstant core-magic
37 (logior (ash (sb!xc:char-code #\S) 24)
38 (ash (sb!xc:char-code #\B) 16)
39 (ash (sb!xc:char-code #\C) 8)
40 (sb!xc:char-code #\L)))
42 (defun round-up (number size)
43 #!+sb-doc
44 "Round NUMBER up to be an integral multiple of SIZE."
45 (* size (ceiling number size)))
47 ;;;; implementing the concept of "vector" in (almost) portable
48 ;;;; Common Lisp
49 ;;;;
50 ;;;; "If you only need to do such simple things, it doesn't really
51 ;;;; matter which language you use." -- _ANSI Common Lisp_, p. 1, Paul
52 ;;;; Graham (evidently not considering the abstraction "vector" to be
53 ;;;; such a simple thing:-)
55 (eval-when (:compile-toplevel :load-toplevel :execute)
56 (defconstant +smallvec-length+
57 (expt 2 16)))
59 ;;; an element of a BIGVEC -- a vector small enough that we have
60 ;;; a good chance of it being portable to other Common Lisps
61 (deftype smallvec ()
62 `(simple-array (unsigned-byte 8) (,+smallvec-length+)))
64 (defun make-smallvec ()
65 (make-array +smallvec-length+ :element-type '(unsigned-byte 8)
66 :initial-element 0))
68 ;;; a big vector, implemented as a vector of SMALLVECs
69 ;;;
70 ;;; KLUDGE: This implementation seems portable enough for our
71 ;;; purposes, since realistically every modern implementation is
72 ;;; likely to support vectors of at least 2^16 elements. But if you're
73 ;;; masochistic enough to read this far into the contortions imposed
74 ;;; on us by ANSI and the Lisp community, for daring to use the
75 ;;; abstraction of a large linearly addressable memory space, which is
76 ;;; after all only directly supported by the underlying hardware of at
77 ;;; least 99% of the general-purpose computers in use today, then you
78 ;;; may be titillated to hear that in fact this code isn't really
79 ;;; portable, because as of sbcl-0.7.4 we need somewhat more than
80 ;;; 16Mbytes to represent a core, and ANSI only guarantees that
81 ;;; ARRAY-DIMENSION-LIMIT is not less than 1024. -- WHN 2002-06-13
82 (defstruct bigvec
83 (outer-vector (vector (make-smallvec)) :type (vector smallvec)))
85 ;;; analogous to SVREF, but into a BIGVEC
86 (defun bvref (bigvec index)
87 (multiple-value-bind (outer-index inner-index)
88 (floor index +smallvec-length+)
89 (aref (the smallvec
90 (svref (bigvec-outer-vector bigvec) outer-index))
91 inner-index)))
92 (defun (setf bvref) (new-value bigvec index)
93 (multiple-value-bind (outer-index inner-index)
94 (floor index +smallvec-length+)
95 (setf (aref (the smallvec
96 (svref (bigvec-outer-vector bigvec) outer-index))
97 inner-index)
98 new-value)))
100 ;;; analogous to LENGTH, but for a BIGVEC
102 ;;; the length of BIGVEC, measured in the number of BVREFable bytes it
103 ;;; can hold
104 (defun bvlength (bigvec)
105 (* (length (bigvec-outer-vector bigvec))
106 +smallvec-length+))
108 ;;; analogous to WRITE-SEQUENCE, but for a BIGVEC
109 (defun write-bigvec-as-sequence (bigvec stream &key (start 0) end pad-with-zeros)
110 (let* ((bvlength (bvlength bigvec))
111 (data-length (min (or end bvlength) bvlength)))
112 (loop for i of-type index from start below data-length do
113 (write-byte (bvref bigvec i)
114 stream))
115 (when (and pad-with-zeros (< bvlength data-length))
116 (loop repeat (- data-length bvlength) do (write-byte 0 stream)))))
118 ;;; analogous to READ-SEQUENCE-OR-DIE, but for a BIGVEC
119 (defun read-bigvec-as-sequence-or-die (bigvec stream &key (start 0) end)
120 (loop for i of-type index from start below (or end (bvlength bigvec)) do
121 (setf (bvref bigvec i)
122 (read-byte stream))))
124 ;;; Grow BIGVEC (exponentially, so that large increases in size have
125 ;;; asymptotic logarithmic cost per byte).
126 (defun expand-bigvec (bigvec)
127 (let* ((old-outer-vector (bigvec-outer-vector bigvec))
128 (length-old-outer-vector (length old-outer-vector))
129 (new-outer-vector (make-array (* 2 length-old-outer-vector))))
130 (dotimes (i length-old-outer-vector)
131 (setf (svref new-outer-vector i)
132 (svref old-outer-vector i)))
133 (loop for i from length-old-outer-vector below (length new-outer-vector) do
134 (setf (svref new-outer-vector i)
135 (make-smallvec)))
136 (setf (bigvec-outer-vector bigvec)
137 new-outer-vector))
138 bigvec)
140 ;;;; looking up bytes and multi-byte values in a BIGVEC (considering
141 ;;;; it as an image of machine memory on the cross-compilation target)
143 ;;; BVREF-32 and friends. These are like SAP-REF-n, except that
144 ;;; instead of a SAP we use a BIGVEC.
145 (macrolet ((make-bvref-n
147 (let* ((name (intern (format nil "BVREF-~A" n)))
148 (number-octets (/ n 8))
149 (ash-list-le
150 (loop for i from 0 to (1- number-octets)
151 collect `(ash (bvref bigvec (+ byte-index ,i))
152 ,(* i 8))))
153 (ash-list-be
154 (loop for i from 0 to (1- number-octets)
155 collect `(ash (bvref bigvec
156 (+ byte-index
157 ,(- number-octets 1 i)))
158 ,(* i 8))))
159 (setf-list-le
160 (loop for i from 0 to (1- number-octets)
161 append
162 `((bvref bigvec (+ byte-index ,i))
163 (ldb (byte 8 ,(* i 8)) new-value))))
164 (setf-list-be
165 (loop for i from 0 to (1- number-octets)
166 append
167 `((bvref bigvec (+ byte-index ,i))
168 (ldb (byte 8 ,(- n 8 (* i 8))) new-value)))))
169 `(progn
170 (defun ,name (bigvec byte-index)
171 (logior ,@(ecase sb!c:*backend-byte-order*
172 (:little-endian ash-list-le)
173 (:big-endian ash-list-be))))
174 (defun (setf ,name) (new-value bigvec byte-index)
175 (setf ,@(ecase sb!c:*backend-byte-order*
176 (:little-endian setf-list-le)
177 (:big-endian setf-list-be))))))))
178 (make-bvref-n 8)
179 (make-bvref-n 16)
180 (make-bvref-n 32)
181 (make-bvref-n 64))
183 ;; lispobj-sized word, whatever that may be
184 ;; hopefully nobody ever wants a 128-bit SBCL...
185 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
186 (progn
187 (defun bvref-word (bytes index)
188 (bvref-64 bytes index))
189 (defun (setf bvref-word) (new-val bytes index)
190 (setf (bvref-64 bytes index) new-val)))
192 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
193 (progn
194 (defun bvref-word (bytes index)
195 (bvref-32 bytes index))
196 (defun (setf bvref-word) (new-val bytes index)
197 (setf (bvref-32 bytes index) new-val)))
200 ;;;; representation of spaces in the core
202 ;;; If there is more than one dynamic space in memory (i.e., if a
203 ;;; copying GC is in use), then only the active dynamic space gets
204 ;;; dumped to core.
205 (defvar *dynamic*)
206 (defconstant dynamic-core-space-id 1)
208 (defvar *static*)
209 (defconstant static-core-space-id 2)
211 (defvar *read-only*)
212 (defconstant read-only-core-space-id 3)
214 (defconstant max-core-space-id 3)
215 (defconstant deflated-core-space-id-flag 4)
217 (defconstant descriptor-low-bits 16
218 "the number of bits in the low half of the descriptor")
219 (defconstant target-space-alignment (ash 1 descriptor-low-bits)
220 "the alignment requirement for spaces in the target.
221 Must be at least (ASH 1 DESCRIPTOR-LOW-BITS)")
223 ;;; a GENESIS-time representation of a memory space (e.g. read-only
224 ;;; space, dynamic space, or static space)
225 (defstruct (gspace (:constructor %make-gspace)
226 (:copier nil))
227 ;; name and identifier for this GSPACE
228 (name (missing-arg) :type symbol :read-only t)
229 (identifier (missing-arg) :type fixnum :read-only t)
230 ;; the word address where the data will be loaded
231 (word-address (missing-arg) :type unsigned-byte :read-only t)
232 ;; the data themselves. (Note that in CMU CL this was a pair of
233 ;; fields SAP and WORDS-ALLOCATED, but that wasn't very portable.)
234 ;; (And then in SBCL this was a VECTOR, but turned out to be
235 ;; unportable too, since ANSI doesn't think that arrays longer than
236 ;; 1024 (!) should needed by portable CL code...)
237 (bytes (make-bigvec) :read-only t)
238 ;; the index of the next unwritten word (i.e. chunk of
239 ;; SB!VM:N-WORD-BYTES bytes) in BYTES, or equivalently the number of
240 ;; words actually written in BYTES. In order to convert to an actual
241 ;; index into BYTES, thus must be multiplied by SB!VM:N-WORD-BYTES.
242 (free-word-index 0))
244 (defun gspace-byte-address (gspace)
245 (ash (gspace-word-address gspace) sb!vm:word-shift))
247 (def!method print-object ((gspace gspace) stream)
248 (print-unreadable-object (gspace stream :type t)
249 (format stream "~S" (gspace-name gspace))))
251 (defun make-gspace (name identifier byte-address)
252 (unless (zerop (rem byte-address target-space-alignment))
253 (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
254 byte-address
255 target-space-alignment))
256 (%make-gspace :name name
257 :identifier identifier
258 :word-address (ash byte-address (- sb!vm:word-shift))))
260 ;;;; representation of descriptors
262 (defun is-fixnum-lowtag (lowtag)
263 (zerop (logand lowtag sb!vm:fixnum-tag-mask)))
265 (defun is-other-immediate-lowtag (lowtag)
266 ;; The other-immediate lowtags are similar to the fixnum lowtags, in
267 ;; that they have an "effective length" that is shorter than is used
268 ;; for the pointer lowtags. Unlike the fixnum lowtags, however, the
269 ;; other-immediate lowtags are always effectively two bits wide.
270 (= (logand lowtag 3) sb!vm:other-immediate-0-lowtag))
272 (defstruct (descriptor
273 (:constructor make-descriptor
274 (high low &optional gspace word-offset))
275 (:copier nil))
276 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
277 (gspace nil :type (or gspace (eql :load-time-value) null))
278 ;; the offset in words from the start of GSPACE, or NIL if not set yet
279 (word-offset nil :type (or sb!vm:word null))
280 ;; the high and low halves of the descriptor
282 ;; KLUDGE: Judging from the comments in genesis.lisp of the CMU CL
283 ;; old-rt compiler, this split dates back from a very early version
284 ;; of genesis where 32-bit integers were represented as conses of
285 ;; two 16-bit integers. In any system with nice (UNSIGNED-BYTE 32)
286 ;; structure slots, like CMU CL >= 17 or any version of SBCL, there
287 ;; seems to be no reason to persist in this. -- WHN 19990917
288 high
289 low)
290 (def!method print-object ((des descriptor) stream)
291 (let ((lowtag (descriptor-lowtag des)))
292 (print-unreadable-object (des stream :type t)
293 (cond ((is-fixnum-lowtag lowtag)
294 (let ((unsigned (logior (ash (descriptor-high des)
295 (1+ (- descriptor-low-bits
296 sb!vm:n-lowtag-bits)))
297 (ash (descriptor-low des)
298 (- 1 sb!vm:n-lowtag-bits)))))
299 (format stream
300 "for fixnum: ~W"
301 (if (> unsigned #x1FFFFFFF)
302 (- unsigned #x40000000)
303 unsigned))))
304 ((is-other-immediate-lowtag lowtag)
305 (format stream
306 "for other immediate: #X~X, type #b~8,'0B"
307 (ash (descriptor-bits des) (- sb!vm:n-widetag-bits))
308 (logand (descriptor-low des) sb!vm:widetag-mask)))
310 (format stream
311 "for pointer: #X~X, lowtag #b~3,'0B, ~A"
312 (logior (ash (descriptor-high des) descriptor-low-bits)
313 (logandc2 (descriptor-low des) sb!vm:lowtag-mask))
314 lowtag
315 (let ((gspace (descriptor-gspace des)))
316 (if gspace
317 (gspace-name gspace)
318 "unknown"))))))))
320 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
321 ;;; free word index is boosted as necessary, and if additional memory
322 ;;; is needed, we grow the GSPACE. The descriptor returned is a
323 ;;; pointer of type LOWTAG.
324 (defun allocate-cold-descriptor (gspace length lowtag)
325 (let* ((bytes (round-up length (ash 1 sb!vm:n-lowtag-bits)))
326 (old-free-word-index (gspace-free-word-index gspace))
327 (new-free-word-index (+ old-free-word-index
328 (ash bytes (- sb!vm:word-shift)))))
329 ;; Grow GSPACE as necessary until it's big enough to handle
330 ;; NEW-FREE-WORD-INDEX.
331 (do ()
332 ((>= (bvlength (gspace-bytes gspace))
333 (* new-free-word-index sb!vm:n-word-bytes)))
334 (expand-bigvec (gspace-bytes gspace)))
335 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
336 (setf (gspace-free-word-index gspace) new-free-word-index)
337 (let ((ptr (+ (gspace-word-address gspace) old-free-word-index)))
338 (make-descriptor (ash ptr (- sb!vm:word-shift descriptor-low-bits))
339 (logior (ash (logand ptr
340 (1- (ash 1
341 (- descriptor-low-bits
342 sb!vm:word-shift))))
343 sb!vm:word-shift)
344 lowtag)
345 gspace
346 old-free-word-index))))
348 (defun descriptor-lowtag (des)
349 #!+sb-doc
350 "the lowtag bits for DES"
351 (logand (descriptor-low des) sb!vm:lowtag-mask))
353 (defun descriptor-bits (des)
354 (logior (ash (descriptor-high des) descriptor-low-bits)
355 (descriptor-low des)))
357 (defun descriptor-fixnum (des)
358 (let ((bits (descriptor-bits des)))
359 (if (logbitp (1- sb!vm:n-word-bits) bits)
360 ;; KLUDGE: The (- SB!VM:N-WORD-BITS 2) term here looks right to
361 ;; me, and it works, but in CMU CL it was (1- SB!VM:N-WORD-BITS),
362 ;; and although that doesn't make sense for me, or work for me,
363 ;; it's hard to see how it could have been wrong, since CMU CL
364 ;; genesis worked. It would be nice to understand how this came
365 ;; to be.. -- WHN 19990901
366 (logior (ash bits (- sb!vm:n-fixnum-tag-bits))
367 (ash -1 (1+ sb!vm:n-positive-fixnum-bits)))
368 (ash bits (- sb!vm:n-fixnum-tag-bits)))))
370 (defun descriptor-word-sized-integer (des)
371 ;; Extract an (unsigned-byte 32), from either its fixnum or bignum
372 ;; representation.
373 (let ((lowtag (descriptor-lowtag des)))
374 (if (is-fixnum-lowtag lowtag)
375 (make-random-descriptor (descriptor-fixnum des))
376 (read-wordindexed des 1))))
378 ;;; common idioms
379 (defun descriptor-bytes (des)
380 (gspace-bytes (descriptor-intuit-gspace des)))
381 (defun descriptor-byte-offset (des)
382 (ash (descriptor-word-offset des) sb!vm:word-shift))
384 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
385 ;;; figure out a GSPACE which corresponds to DES, set it into
386 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
387 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
388 (declaim (ftype (function (descriptor) gspace) descriptor-intuit-gspace))
389 (defun descriptor-intuit-gspace (des)
390 (or (descriptor-gspace des)
392 ;; gspace wasn't set, now we have to search for it.
393 (let ((lowtag (descriptor-lowtag des))
394 (high (descriptor-high des))
395 (low (descriptor-low des)))
397 ;; Non-pointer objects don't have a gspace.
398 (unless (or (eql lowtag sb!vm:fun-pointer-lowtag)
399 (eql lowtag sb!vm:instance-pointer-lowtag)
400 (eql lowtag sb!vm:list-pointer-lowtag)
401 (eql lowtag sb!vm:other-pointer-lowtag))
402 (error "don't even know how to look for a GSPACE for ~S" des))
404 (dolist (gspace (list *dynamic* *static* *read-only*)
405 (error "couldn't find a GSPACE for ~S" des))
406 ;; Bounds-check the descriptor against the allocated area
407 ;; within each gspace.
409 ;; Most of the faffing around in here involving ash and
410 ;; various computed shift counts is due to the high/low
411 ;; split representation of the descriptor bits and an
412 ;; apparent disinclination to create intermediate values
413 ;; larger than a target fixnum.
415 ;; This code relies on the fact that GSPACEs are aligned
416 ;; such that the descriptor-low-bits low bits are zero.
417 (when (and (>= high (ash (gspace-word-address gspace)
418 (- sb!vm:word-shift descriptor-low-bits)))
419 (<= high (ash (+ (gspace-word-address gspace)
420 (gspace-free-word-index gspace))
421 (- sb!vm:word-shift descriptor-low-bits))))
422 ;; Update the descriptor with the correct gspace and the
423 ;; offset within the gspace and return the gspace.
424 (setf (descriptor-gspace des) gspace)
425 (setf (descriptor-word-offset des)
426 (+ (ash (- high (ash (gspace-word-address gspace)
427 (- sb!vm:word-shift
428 descriptor-low-bits)))
429 (- descriptor-low-bits sb!vm:word-shift))
430 (ash (logandc2 low sb!vm:lowtag-mask)
431 (- sb!vm:word-shift))))
432 (return gspace))))))
434 (defun make-random-descriptor (value)
435 (make-descriptor (logand (ash value (- descriptor-low-bits))
436 (1- (ash 1
437 (- sb!vm:n-word-bits
438 descriptor-low-bits))))
439 (logand value (1- (ash 1 descriptor-low-bits)))))
441 (defun make-fixnum-descriptor (num)
442 (when (>= (integer-length num)
443 (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits))
444 (error "~W is too big for a fixnum." num))
445 (make-random-descriptor (ash num sb!vm:n-fixnum-tag-bits)))
447 (defun make-other-immediate-descriptor (data type)
448 (make-descriptor (ash data (- sb!vm:n-widetag-bits descriptor-low-bits))
449 (logior (logand (ash data (- descriptor-low-bits
450 sb!vm:n-widetag-bits))
451 (1- (ash 1 descriptor-low-bits)))
452 type)))
454 (defun make-character-descriptor (data)
455 (make-other-immediate-descriptor data sb!vm:character-widetag))
457 (defun descriptor-beyond (des offset type)
458 (let* ((low (logior (+ (logandc2 (descriptor-low des) sb!vm:lowtag-mask)
459 offset)
460 type))
461 (high (+ (descriptor-high des)
462 (ash low (- descriptor-low-bits)))))
463 (make-descriptor high (logand low (1- (ash 1 descriptor-low-bits))))))
465 ;;;; miscellaneous variables and other noise
467 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
468 ;;; undefined foreign symbols are to be treated as an error.
469 ;;; (In the first pass of GENESIS, needed to create a header file before
470 ;;; the C runtime can be built, various foreign symbols will necessarily
471 ;;; be undefined, but we don't need actual values for them anyway, and
472 ;;; we can just use 0 or some other placeholder. In the second pass of
473 ;;; GENESIS, all foreign symbols should be defined, so any undefined
474 ;;; foreign symbol is a problem.)
476 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
477 ;;; never tries to look up foreign symbols in the first place unless
478 ;;; it's actually creating a core file (as in the second pass) instead
479 ;;; of using this hack to allow it to go through the motions without
480 ;;; causing an error. -- WHN 20000825
481 (defvar *foreign-symbol-placeholder-value*)
483 ;;; a handle on the trap object
484 (defvar *unbound-marker*)
485 ;; was: (make-other-immediate-descriptor 0 sb!vm:unbound-marker-widetag)
487 ;;; a handle on the NIL object
488 (defvar *nil-descriptor*)
490 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
491 ;;; when the target Lisp starts up
493 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
494 ;;; loadtime value, represented by (CONS KEYWORD ..). The FILENAME
495 ;;; tells which fasl file each list element came from, for debugging
496 ;;; purposes.
497 (defvar *current-reversed-cold-toplevels*)
499 ;;; the head of a list of DEBUG-SOURCEs which need to be patched when
500 ;;; the cold core starts up
501 (defvar *current-debug-sources*)
503 ;;; foreign symbol references
504 (defparameter *cold-foreign-undefined-symbols* nil)
506 ;;; the name of the object file currently being cold loaded (as a string, not a
507 ;;; pathname), or NIL if we're not currently cold loading any object file
508 (defvar *cold-load-filename* nil)
509 (declaim (type (or string null) *cold-load-filename*))
511 ;;;; miscellaneous stuff to read and write the core memory
513 ;;; FIXME: should be DEFINE-MODIFY-MACRO
514 (defmacro cold-push (thing list)
515 #!+sb-doc
516 "Push THING onto the given cold-load LIST."
517 `(setq ,list (cold-cons ,thing ,list)))
519 (declaim (ftype (function (descriptor sb!vm:word) descriptor) read-wordindexed))
520 (defun read-wordindexed (address index)
521 #!+sb-doc
522 "Return the value which is displaced by INDEX words from ADDRESS."
523 (let* ((gspace (descriptor-intuit-gspace address))
524 (bytes (gspace-bytes gspace))
525 (byte-index (ash (+ index (descriptor-word-offset address))
526 sb!vm:word-shift))
527 (value (bvref-word bytes byte-index)))
528 (make-random-descriptor value)))
530 (declaim (ftype (function (descriptor) descriptor) read-memory))
531 (defun read-memory (address)
532 #!+sb-doc
533 "Return the value at ADDRESS."
534 (read-wordindexed address 0))
536 ;;; (Note: In CMU CL, this function expected a SAP-typed ADDRESS
537 ;;; value, instead of the object-and-offset we use here.)
538 (declaim (ftype (function (descriptor sb!vm:word descriptor) (values))
539 note-load-time-value-reference))
540 (defun note-load-time-value-reference (address offset marker)
541 (cold-push (cold-cons
542 (cold-intern :load-time-value-fixup)
543 (cold-cons address
544 (cold-cons (number-to-core offset)
545 (cold-cons
546 (number-to-core (descriptor-word-offset marker))
547 *nil-descriptor*))))
548 *current-reversed-cold-toplevels*)
549 (values))
551 (declaim (ftype (function (descriptor sb!vm:word (or symbol descriptor))) write-wordindexed))
552 (defun write-wordindexed (address index value)
553 #!+sb-doc
554 "Write VALUE displaced INDEX words from ADDRESS."
555 ;; If we're passed a symbol as a value then it needs to be interned.
556 (let ((value (cond ((symbolp value) (cold-intern value))
557 (t value))))
558 (if (eql (descriptor-gspace value) :load-time-value)
559 (note-load-time-value-reference address
560 (- (ash index sb!vm:word-shift)
561 (logand (descriptor-bits address)
562 sb!vm:lowtag-mask))
563 value)
564 (let* ((bytes (gspace-bytes (descriptor-intuit-gspace address)))
565 (byte-index (ash (+ index (descriptor-word-offset address))
566 sb!vm:word-shift)))
567 (setf (bvref-word bytes byte-index) (descriptor-bits value))))))
569 (declaim (ftype (function (descriptor (or symbol descriptor))) write-memory))
570 (defun write-memory (address value)
571 #!+sb-doc
572 "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
573 (write-wordindexed address 0 value))
575 ;;;; allocating images of primitive objects in the cold core
577 (defun write-header-word (des header-data widetag)
578 (write-memory des (make-other-immediate-descriptor header-data widetag)))
580 ;;; There are three kinds of blocks of memory in the type system:
581 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
582 ;;; header as all slots are descriptors.
583 ;;; * Unboxed objects (bignums): There is a single header word that contains
584 ;;; the length.
585 ;;; * Vector objects: There is a header word with the type, then a word for
586 ;;; the length, then the data.
587 (defun allocate-object (gspace length lowtag)
588 #!+sb-doc
589 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
590 pointing to them."
591 (allocate-cold-descriptor gspace (ash length sb!vm:word-shift) lowtag))
592 (defun allocate-header+object (gspace length widetag)
593 #!+sb-doc
594 "Allocate LENGTH words plus a header word in GSPACE and
595 return an ``other-pointer'' descriptor to them. Initialize the header word
596 with the resultant length and WIDETAG."
597 (let ((des (allocate-cold-descriptor gspace
598 (ash (1+ length) sb!vm:word-shift)
599 sb!vm:other-pointer-lowtag)))
600 (write-header-word des length widetag)
601 des))
602 (defun allocate-vector-object (gspace element-bits length widetag)
603 #!+sb-doc
604 "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
605 GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
606 header word with WIDETAG and the length slot with LENGTH."
607 ;; ALLOCATE-COLD-DESCRIPTOR will take any rational number of bytes
608 ;; and round up to a double-word. This doesn't need to use CEILING.
609 (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
610 (des (allocate-cold-descriptor gspace
611 (+ bytes (* 2 sb!vm:n-word-bytes))
612 sb!vm:other-pointer-lowtag)))
613 (write-header-word des 0 widetag)
614 (write-wordindexed des
615 sb!vm:vector-length-slot
616 (make-fixnum-descriptor length))
617 des))
619 ;; Make a structure and set the header word and layout.
620 ;; LAYOUT-LENGTH is as returned by the like-named function.
621 (defun allocate-structure-object (gspace layout-length layout)
622 ;; The math in here is best illustrated by two examples:
623 ;; even: size 4 => request to allocate 5 => rounds up to 6, logior => 5
624 ;; odd : size 5 => request to allocate 6 => no rounding up, logior => 5
625 ;; In each case, the length of the memory block is even.
626 ;; ALLOCATE-OBJECT performs the rounding. It must be supplied
627 ;; the number of words minimally needed, counting the header itself.
628 ;; The number written into the header (%INSTANCE-LENGTH) is always odd.
629 (let ((des (allocate-object gspace (1+ layout-length)
630 sb!vm:instance-pointer-lowtag)))
631 (write-header-word des (logior layout-length 1)
632 sb!vm:instance-header-widetag)
633 (write-wordindexed des sb!vm:instance-slots-offset layout)
634 des))
636 ;;;; copying simple objects into the cold core
638 (defun base-string-to-core (string &optional (gspace *dynamic*))
639 #!+sb-doc
640 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
641 core and return a descriptor to it."
642 ;; (Remember that the system convention for storage of strings leaves an
643 ;; extra null byte at the end to aid in call-out to C.)
644 (let* ((length (length string))
645 (des (allocate-vector-object gspace
646 sb!vm:n-byte-bits
647 (1+ length)
648 sb!vm:simple-base-string-widetag))
649 (bytes (gspace-bytes gspace))
650 (offset (+ (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
651 (descriptor-byte-offset des))))
652 (write-wordindexed des
653 sb!vm:vector-length-slot
654 (make-fixnum-descriptor length))
655 (dotimes (i length)
656 (setf (bvref bytes (+ offset i))
657 (sb!xc:char-code (aref string i))))
658 (setf (bvref bytes (+ offset length))
659 0) ; null string-termination character for C
660 des))
662 (defun bignum-to-core (n)
663 #!+sb-doc
664 "Copy a bignum to the cold core."
665 (let* ((words (ceiling (1+ (integer-length n)) sb!vm:n-word-bits))
666 (handle
667 (allocate-header+object *dynamic* words sb!vm:bignum-widetag)))
668 (declare (fixnum words))
669 (do ((index 1 (1+ index))
670 (remainder n (ash remainder (- sb!vm:n-word-bits))))
671 ((> index words)
672 (unless (zerop (integer-length remainder))
673 ;; FIXME: Shouldn't this be a fatal error?
674 (warn "~W words of ~W were written, but ~W bits were left over."
675 words n remainder)))
676 (let ((word (ldb (byte sb!vm:n-word-bits 0) remainder)))
677 (write-wordindexed handle index
678 (make-descriptor (ash word (- descriptor-low-bits))
679 (ldb (byte descriptor-low-bits 0)
680 word)))))
681 handle))
683 (defun number-pair-to-core (first second type)
684 #!+sb-doc
685 "Makes a number pair of TYPE (ratio or complex) and fills it in."
686 (let ((des (allocate-header+object *dynamic* 2 type)))
687 (write-wordindexed des 1 first)
688 (write-wordindexed des 2 second)
689 des))
691 (defun write-double-float-bits (address index x)
692 (let ((hi (double-float-high-bits x))
693 (lo (double-float-low-bits x)))
694 (ecase sb!vm::n-word-bits
696 (let ((high-bits (make-random-descriptor hi))
697 (low-bits (make-random-descriptor lo)))
698 (ecase sb!c:*backend-byte-order*
699 (:little-endian
700 (write-wordindexed address index low-bits)
701 (write-wordindexed address (1+ index) high-bits))
702 (:big-endian
703 (write-wordindexed address index high-bits)
704 (write-wordindexed address (1+ index) low-bits)))))
706 (let ((bits (make-random-descriptor
707 (ecase sb!c:*backend-byte-order*
708 (:little-endian (logior lo (ash hi 32)))
709 ;; Just guessing.
710 #+nil (:big-endian (logior (logand hi #xffffffff)
711 (ash lo 32)))))))
712 (write-wordindexed address index bits))))
713 address))
715 (defun float-to-core (x)
716 (etypecase x
717 (single-float
718 ;; 64-bit platforms have immediate single-floats.
719 #!+#.(cl:if (cl:= sb!vm:n-word-bits 64) '(and) '(or))
720 (make-random-descriptor (logior (ash (single-float-bits x) 32)
721 sb!vm::single-float-widetag))
722 #!-#.(cl:if (cl:= sb!vm:n-word-bits 64) '(and) '(or))
723 (let ((des (allocate-header+object *dynamic*
724 (1- sb!vm:single-float-size)
725 sb!vm:single-float-widetag)))
726 (write-wordindexed des
727 sb!vm:single-float-value-slot
728 (make-random-descriptor (single-float-bits x)))
729 des))
730 (double-float
731 (let ((des (allocate-header+object *dynamic*
732 (1- sb!vm:double-float-size)
733 sb!vm:double-float-widetag)))
734 (write-double-float-bits des sb!vm:double-float-value-slot x)))))
736 (defun complex-single-float-to-core (num)
737 (declare (type (complex single-float) num))
738 (let ((des (allocate-header+object *dynamic*
739 (1- sb!vm:complex-single-float-size)
740 sb!vm:complex-single-float-widetag)))
741 #!-x86-64
742 (progn
743 (write-wordindexed des sb!vm:complex-single-float-real-slot
744 (make-random-descriptor (single-float-bits (realpart num))))
745 (write-wordindexed des sb!vm:complex-single-float-imag-slot
746 (make-random-descriptor (single-float-bits (imagpart num)))))
747 #!+x86-64
748 (write-wordindexed des sb!vm:complex-single-float-data-slot
749 (make-random-descriptor
750 (logior (ldb (byte 32 0) (single-float-bits (realpart num)))
751 (ash (single-float-bits (imagpart num)) 32))))
752 des))
754 (defun complex-double-float-to-core (num)
755 (declare (type (complex double-float) num))
756 (let ((des (allocate-header+object *dynamic*
757 (1- sb!vm:complex-double-float-size)
758 sb!vm:complex-double-float-widetag)))
759 (write-double-float-bits des sb!vm:complex-double-float-real-slot
760 (realpart num))
761 (write-double-float-bits des sb!vm:complex-double-float-imag-slot
762 (imagpart num))))
764 ;;; Copy the given number to the core.
765 (defun number-to-core (number)
766 (typecase number
767 (integer (if (< (integer-length number)
768 (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits))
769 (make-fixnum-descriptor number)
770 (bignum-to-core number)))
771 (ratio (number-pair-to-core (number-to-core (numerator number))
772 (number-to-core (denominator number))
773 sb!vm:ratio-widetag))
774 ((complex single-float) (complex-single-float-to-core number))
775 ((complex double-float) (complex-double-float-to-core number))
776 #!+long-float
777 ((complex long-float)
778 (error "~S isn't a cold-loadable number at all!" number))
779 (complex (number-pair-to-core (number-to-core (realpart number))
780 (number-to-core (imagpart number))
781 sb!vm:complex-widetag))
782 (float (float-to-core number))
783 (t (error "~S isn't a cold-loadable number at all!" number))))
785 (declaim (ftype (function (sb!vm:word) descriptor) sap-int-to-core))
786 (defun sap-int-to-core (sap-int)
787 (let ((des (allocate-header+object *dynamic* (1- sb!vm:sap-size)
788 sb!vm:sap-widetag)))
789 (write-wordindexed des
790 sb!vm:sap-pointer-slot
791 (make-random-descriptor sap-int))
792 des))
794 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
795 (defun cold-cons (car cdr &optional (gspace *dynamic*))
796 (let ((dest (allocate-object gspace 2 sb!vm:list-pointer-lowtag)))
797 (write-memory dest car)
798 (write-wordindexed dest 1 cdr)
799 dest))
801 ;;; Make a simple-vector on the target that holds the specified
802 ;;; OBJECTS, and return its descriptor.
803 ;;; This is really "vectorify-list-into-core" but that's too wordy,
804 ;;; so historically it was "vector-in-core" which is a fine name.
805 (defun vector-in-core (objects &optional (gspace *dynamic*))
806 (let* ((size (length objects))
807 (result (allocate-vector-object gspace sb!vm:n-word-bits size
808 sb!vm:simple-vector-widetag)))
809 (dotimes (index size)
810 (write-wordindexed result (+ index sb!vm:vector-data-offset)
811 (pop objects)))
812 result))
814 ;;;; symbol magic
816 ;; Simulate *FREE-TLS-INDEX*. This is a count, not a displacement.
817 ;; In C, sizeof counts 1 word for the variable-length interrupt_contexts[]
818 ;; but primitive-object-size counts 0, so add 1, though in fact the C code
819 ;; implies that it might have overcounted by 1. We could make this agnostic
820 ;; of MAX-INTERRUPTS by moving the thread base register up by TLS-SIZE words,
821 ;; using negative offsets for all dynamically assigned indices.
822 (defvar *genesis-tls-counter*
823 (+ 1 sb!vm::max-interrupts
824 (sb!vm:primitive-object-size
825 (find 'sb!vm::thread sb!vm:*primitive-objects*
826 :key #'sb!vm:primitive-object-name))))
828 #!+sb-thread
829 (progn
830 ;; Assign SYMBOL the tls-index INDEX. SYMBOL must be a descriptor.
831 ;; This is a backend support routine, but the style within this file
832 ;; is to conditionalize by the target features.
833 (defun cold-assign-tls-index (symbol index)
834 #!+x86-64
835 (let ((header-word
836 (logior (ash index 32)
837 (descriptor-bits (read-wordindexed symbol 0)))))
838 (write-wordindexed symbol 0 (make-random-descriptor header-word)))
839 #!-x86-64
840 (write-wordindexed symbol sb!vm:symbol-tls-index-slot
841 (make-random-descriptor index)))
843 ;; Return SYMBOL's tls-index,
844 ;; choosing a new index if it doesn't have one yet.
845 (defun ensure-symbol-tls-index (symbol)
846 (let* ((cold-sym (cold-intern symbol))
847 (tls-index
848 #!+x86-64
849 (ldb (byte 32 32) (descriptor-bits (read-wordindexed cold-sym 0)))
850 #!-x86-64
851 (descriptor-bits
852 (read-wordindexed cold-sym sb!vm:symbol-tls-index-slot))))
853 (unless (plusp tls-index)
854 (let ((next (prog1 *genesis-tls-counter* (incf *genesis-tls-counter*))))
855 (setq tls-index (ash next sb!vm:word-shift))
856 (cold-assign-tls-index cold-sym tls-index)))
857 tls-index)))
859 ;; A table of special variable names which get known TLS indices.
860 ;; Some of them are mapped onto 'struct thread' and have pre-determined offsets.
861 ;; Others are static symbols used with bind_variable() in the C runtime,
862 ;; and might not, in the absence of this table, get an index assigned by genesis
863 ;; depending on whether the cross-compiler used the BIND vop on them.
864 ;; Indices for those static symbols can be chosen arbitrarily, which is to say
865 ;; the value doesn't matter but must update the tls-counter correctly.
866 ;; All symbols other than the ones in this table get the indices assigned
867 ;; by the fasloader on demand.
868 #!+sb-thread
869 (defvar *known-tls-symbols*
870 ;; FIXME: no mechanism exists to determine which static symbols C code will
871 ;; dynamically bind. TLS is a finite resource, and wasting indices for all
872 ;; static symbols isn't the best idea. This list was hand-made with 'grep'.
873 '(sb!vm:*alloc-signal*
874 sb!sys:*allow-with-interrupts*
875 sb!vm:*current-catch-block*
876 sb!vm::*current-unwind-protect-block*
877 sb!kernel:*free-interrupt-context-index*
878 sb!kernel:*gc-inhibit*
879 sb!kernel:*gc-pending*
880 sb!impl::*gc-safe*
881 sb!impl::*in-safepoint*
882 sb!sys:*interrupt-pending*
883 sb!sys:*interrupts-enabled*
884 sb!vm::*pinned-objects*
885 sb!kernel:*restart-clusters*
886 sb!kernel:*stop-for-gc-pending*
887 #!+sb-thruption
888 sb!sys:*thruption-pending*))
890 ;;; Allocate (and initialize) a symbol.
891 (defun allocate-symbol (name &key (gspace *dynamic*))
892 (declare (simple-string name))
893 (let ((symbol (allocate-header+object gspace (1- sb!vm:symbol-size)
894 sb!vm:symbol-header-widetag)))
895 (write-wordindexed symbol sb!vm:symbol-value-slot *unbound-marker*)
896 (write-wordindexed symbol sb!vm:symbol-hash-slot (make-fixnum-descriptor 0))
897 (write-wordindexed symbol sb!vm:symbol-info-slot *nil-descriptor*)
898 (write-wordindexed symbol sb!vm:symbol-name-slot
899 (base-string-to-core name *dynamic*))
900 (write-wordindexed symbol sb!vm:symbol-package-slot *nil-descriptor*)
901 symbol))
903 #!+sb-thread
904 (defun assign-tls-index (symbol cold-symbol)
905 (let ((index (info :variable :wired-tls symbol)))
906 (cond ((integerp index) ; thread slot
907 (cold-assign-tls-index cold-symbol index))
908 ((memq symbol *known-tls-symbols*)
909 ;; symbols without which the C runtime could not start
910 (shiftf index *genesis-tls-counter* (1+ *genesis-tls-counter*))
911 (cold-assign-tls-index cold-symbol (ash index sb!vm:word-shift))))))
913 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
914 ;;; descriptor of a cold symbol or (in an abbreviation for the
915 ;;; most common usage pattern) an ordinary symbol, which will be
916 ;;; automatically cold-interned.
917 (declaim (ftype (function ((or symbol descriptor) descriptor)) cold-set))
918 (defun cold-set (symbol-or-symbol-des value)
919 (let ((symbol-des (etypecase symbol-or-symbol-des
920 (descriptor symbol-or-symbol-des)
921 (symbol (cold-intern symbol-or-symbol-des)))))
922 (write-wordindexed symbol-des sb!vm:symbol-value-slot value)))
924 ;;;; layouts and type system pre-initialization
926 ;;; Since we want to be able to dump structure constants and
927 ;;; predicates with reference layouts, we need to create layouts at
928 ;;; cold-load time. We use the name to intern layouts by, and dump a
929 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
930 ;;; initialization can find them. The only thing that's tricky [sic --
931 ;;; WHN 19990816] is initializing layout's layout, which must point to
932 ;;; itself.
934 ;;; a map from class names to lists of
935 ;;; `(,descriptor ,name ,length ,inherits ,depth)
936 ;;; KLUDGE: It would be more understandable and maintainable to use
937 ;;; DEFSTRUCT (:TYPE LIST) here. -- WHN 19990823
938 (defvar *cold-layouts* (make-hash-table :test 'equal))
940 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
941 ;;; mapping
942 (defvar *cold-layout-names* (make-hash-table :test 'eql))
944 ;;; FIXME: *COLD-LAYOUTS* and *COLD-LAYOUT-NAMES* should be
945 ;;; initialized by binding in GENESIS.
947 ;;; the descriptor for layout's layout (needed when making layouts)
948 (defvar *layout-layout*)
949 ;;; the descriptor for PACKAGE's layout (needed when making packages)
950 (defvar *package-layout*)
952 (defconstant target-layout-length
953 ;; LAYOUT-LENGTH counts the number of words in an instance,
954 ;; including the layout itself as 1 word
955 (layout-length (find-layout 'layout)))
957 (defun target-layout-index (slot-name)
958 ;; KLUDGE: this is a little bit sleazy, but the tricky thing is that
959 ;; structure slots don't have a terribly firm idea of their names.
960 ;; At least here if we change LAYOUT's package of definition, we
961 ;; only have to change one thing...
962 (let* ((name (find-symbol (symbol-name slot-name) "SB!KERNEL"))
963 (layout (find-layout 'layout))
964 (dd (layout-info layout))
965 (slots (dd-slots dd))
966 (dsd (find name slots :key #'dsd-name)))
967 (aver dsd)
968 (dsd-index dsd)))
970 (defun cold-set-layout-slot (cold-layout slot-name value)
971 (write-wordindexed
972 cold-layout
973 (+ sb!vm:instance-slots-offset (target-layout-index slot-name))
974 value))
976 ;;; Return a list of names created from the cold layout INHERITS data
977 ;;; in X.
978 (defun listify-cold-inherits (x)
979 (let ((len (descriptor-fixnum (read-wordindexed x
980 sb!vm:vector-length-slot))))
981 (collect ((res))
982 (dotimes (index len)
983 (let* ((des (read-wordindexed x (+ sb!vm:vector-data-offset index)))
984 (found (gethash (descriptor-bits des) *cold-layout-names*)))
985 (if found
986 (res found)
987 (error "unknown descriptor at index ~S (bits = ~8,'0X)"
988 index
989 (descriptor-bits des)))))
990 (res))))
992 (defvar *simple-vector-0-descriptor*)
993 (declaim (ftype (function (symbol descriptor descriptor descriptor descriptor)
994 descriptor)
995 make-cold-layout))
996 (defun make-cold-layout (name length inherits depthoid metadata)
997 (let ((result (allocate-structure-object *dynamic*
998 target-layout-length
999 *layout-layout*)))
1000 ;; Don't set the CLOS hash value: done in cold-init instead.
1002 ;; Set other slot values.
1004 ;; leave CLASSOID uninitialized for now
1005 (cold-set-layout-slot result 'invalid *nil-descriptor*)
1006 (cold-set-layout-slot result 'inherits inherits)
1007 (cold-set-layout-slot result 'depthoid depthoid)
1008 (cold-set-layout-slot result 'length length)
1009 (cold-set-layout-slot result 'info *nil-descriptor*)
1010 (cold-set-layout-slot result 'pure *nil-descriptor*)
1011 #!-interleaved-raw-slots
1012 (cold-set-layout-slot result 'n-untagged-slots metadata)
1013 #!+interleaved-raw-slots
1014 (progn
1015 (cold-set-layout-slot result 'untagged-bitmap metadata)
1016 ;; Nothing in cold-init needs to call EQUALP on a structure with raw slots,
1017 ;; but for type-correctness this slot needs to be a simple-vector.
1018 (unless (boundp '*simple-vector-0-descriptor*)
1019 (setq *simple-vector-0-descriptor* (vector-in-core nil)))
1020 (cold-set-layout-slot result 'equalp-tests *simple-vector-0-descriptor*))
1021 (cold-set-layout-slot result 'source-location *nil-descriptor*)
1022 (cold-set-layout-slot result '%for-std-class-b (make-fixnum-descriptor 0))
1023 (cold-set-layout-slot result 'slot-list *nil-descriptor*)
1025 (setf (gethash name *cold-layouts*)
1026 (list result
1027 name
1028 (descriptor-fixnum length)
1029 (listify-cold-inherits inherits)
1030 (descriptor-fixnum depthoid)
1031 (descriptor-fixnum metadata)))
1032 (setf (gethash (descriptor-bits result) *cold-layout-names*) name)
1034 result))
1036 ;; This is called to backpatch two small sets of objects:
1037 ;; - layouts which are made before layout-of-layout is made (4 of them)
1038 ;; - packages, which are made before layout-of-package is made (all of them)
1039 (defun patch-instance-layout (thing layout)
1040 ;; Layout pointer is in the word following the header
1041 (write-wordindexed thing sb!vm:instance-slots-offset layout))
1043 (defun initialize-layouts ()
1044 (clrhash *cold-layouts*)
1045 ;; This assertion is due to the fact that MAKE-COLD-LAYOUT does not
1046 ;; know how to set any raw slots.
1047 (aver (= 0 (layout-raw-slot-metadata (find-layout 'layout))))
1048 (setq *layout-layout* (make-fixnum-descriptor 0))
1049 (flet ((chill-layout (name &rest inherits)
1050 ;; Check that the number of specified INHERITS matches
1051 ;; the length of the layout's inherits in the cross-compiler.
1052 (let ((warm-layout (classoid-layout (find-classoid name))))
1053 (assert (eql (length (layout-inherits warm-layout))
1054 (length inherits)))
1055 (make-cold-layout
1056 name
1057 (number-to-core (layout-length warm-layout))
1058 (vector-in-core inherits)
1059 (number-to-core (layout-depthoid warm-layout))
1060 (number-to-core (layout-raw-slot-metadata warm-layout))))))
1061 (let* ((t-layout (chill-layout 't))
1062 (s-o-layout (chill-layout 'structure-object t-layout))
1063 (s!o-layout (chill-layout 'structure!object t-layout s-o-layout)))
1064 (setf *layout-layout*
1065 (chill-layout 'layout t-layout s-o-layout s!o-layout))
1066 (dolist (layout (list t-layout s-o-layout s!o-layout *layout-layout*))
1067 (patch-instance-layout layout *layout-layout*))
1068 (setf *package-layout*
1069 (chill-layout 'package ; *NOT* SB!XC:PACKAGE, or you lose
1070 t-layout s-o-layout s!o-layout)))))
1072 ;;;; interning symbols in the cold image
1074 ;;; a map from package name as a host string to
1075 ;;; (cold-package-descriptor . (external-symbols . internal-symbols))
1076 (defvar *cold-package-symbols*)
1077 (declaim (type hash-table *cold-package-symbols*))
1079 ;;; a map from descriptors to symbols, so that we can back up. The key
1080 ;;; is the address in the target core.
1081 (defvar *cold-symbols*)
1082 (declaim (type hash-table *cold-symbols*))
1084 (defun initialize-packages (package-data-list)
1085 (let ((slots (dd-slots (layout-info (find-layout 'package))))
1086 (target-pkg-list nil))
1087 (labels ((set-slot (obj slot-name value)
1088 (write-wordindexed obj (slot-index slot-name) value))
1089 (slot-index (slot-name)
1090 (+ sb!vm:instance-slots-offset
1091 (dsd-index
1092 (find slot-name slots :key #'dsd-name :test #'string=))))
1093 (init-cold-package (name &optional docstring)
1094 (let ((cold-package (car (gethash name *cold-package-symbols*))))
1095 ;; patch in the layout
1096 (patch-instance-layout cold-package *package-layout*)
1097 ;; Initialize string slots
1098 (set-slot cold-package '%name (base-string-to-core name))
1099 (set-slot cold-package '%nicknames (chill-nicknames name))
1100 (set-slot cold-package 'doc-string
1101 (if docstring
1102 (base-string-to-core docstring)
1103 *nil-descriptor*))
1104 (set-slot cold-package '%use-list *nil-descriptor*)
1105 ;; the cddr of this will accumulate the 'used-by' package list
1106 (push (list name cold-package) target-pkg-list)))
1107 (chill-nicknames (pkg-name)
1108 (let ((result *nil-descriptor*))
1109 ;; Make the package nickname lists for the standard packages
1110 ;; be the minimum specified by ANSI, regardless of what value
1111 ;; the cross-compilation host happens to use.
1112 ;; For packages other than the standard packages, the nickname
1113 ;; list was specified by our package setup code, and we can just
1114 ;; propagate the current state into the target.
1115 (dolist (nickname
1116 (cond ((string= pkg-name "COMMON-LISP") '("CL"))
1117 ((string= pkg-name "COMMON-LISP-USER")
1118 '("CL-USER"))
1119 ((string= pkg-name "KEYWORD") '())
1120 (t (package-nicknames (find-package pkg-name))))
1121 result)
1122 (cold-push (base-string-to-core nickname) result))))
1123 (find-cold-package (name)
1124 (cadr (find-package-cell name)))
1125 (find-package-cell (name)
1126 (or (assoc (if (string= name "CL") "COMMON-LISP" name)
1127 target-pkg-list :test #'string=)
1128 (error "No cold package named ~S" name)))
1129 (list-to-core (list)
1130 (let ((res *nil-descriptor*))
1131 (dolist (x list res) (cold-push x res)))))
1132 ;; pass 1: make all proto-packages
1133 (dolist (pd package-data-list)
1134 (init-cold-package (sb-cold:package-data-name pd)
1135 #!+sb-doc(sb-cold::package-data-doc pd)))
1136 ;; MISMATCH needs !HAIRY-DATA-VECTOR-REFFER-INIT to have been done,
1137 ;; and FIND-PACKAGE calls MISMATCH - which it shouldn't - but until
1138 ;; that is fixed, doing this in genesis allows packages to be
1139 ;; completely sane, modulo the naming, extremely early in cold-init.
1140 (cold-set '*keyword-package* (find-cold-package "KEYWORD"))
1141 (cold-set '*cl-package* (find-cold-package "COMMON-LISP"))
1142 ;; pass 2: set the 'use' lists and collect the 'used-by' lists
1143 (dolist (pd package-data-list)
1144 (let ((this (find-cold-package (sb-cold:package-data-name pd)))
1145 (use nil))
1146 (dolist (that (sb-cold:package-data-use pd))
1147 (let ((cell (find-package-cell that)))
1148 (push (cadr cell) use)
1149 (push this (cddr cell))))
1150 (set-slot this '%use-list (list-to-core (nreverse use)))))
1151 ;; pass 3: set the 'used-by' lists
1152 (dolist (cell target-pkg-list)
1153 (set-slot (cadr cell) '%used-by-list (list-to-core (cddr cell)))))))
1155 ;;; sanity check for a symbol we're about to create on the target
1157 ;;; Make sure that the symbol has an appropriate package. In
1158 ;;; particular, catch the so-easy-to-make error of typing something
1159 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1160 ;;; need is SB!KERNEL:%BYTE-BLT.
1161 (defun package-ok-for-target-symbol-p (package)
1162 (let ((package-name (package-name package)))
1164 ;; Cold interning things in these standard packages is OK. (Cold
1165 ;; interning things in the other standard package, CL-USER, isn't
1166 ;; OK. We just use CL-USER to expose symbols whose homes are in
1167 ;; other packages. Thus, trying to cold intern a symbol whose
1168 ;; home package is CL-USER probably means that a coding error has
1169 ;; been made somewhere.)
1170 (find package-name '("COMMON-LISP" "KEYWORD") :test #'string=)
1171 ;; Cold interning something in one of our target-code packages,
1172 ;; which are ever-so-rigorously-and-elegantly distinguished by
1173 ;; this prefix on their names, is OK too.
1174 (string= package-name "SB!" :end1 3 :end2 3)
1175 ;; This one is OK too, since it ends up being COMMON-LISP on the
1176 ;; target.
1177 (string= package-name "SB-XC")
1178 ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1179 ;; package in the xc host? something we can't think of
1180 ;; a valid reason to cold intern, anyway...)
1183 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1185 ;;; Most host symbols we dump onto the target are created by SBCL
1186 ;;; itself, so that as long as we avoid gratuitously
1187 ;;; cross-compilation-unfriendly hacks, it just happens that their
1188 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1189 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1190 ;;; in the COMMON-LISP package, where we don't get to create the
1191 ;;; symbols but instead have to use the ones that the xc host created.
1192 ;;; In particular, while ANSI specifies which symbols are exported
1193 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1194 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1195 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1196 ;;; symbols in the CLOS package).
1197 (defun symbol-package-for-target-symbol (symbol)
1198 ;; We want to catch weird symbols like CLISP's
1199 ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1200 ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1201 ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1202 (multiple-value-bind (cl-symbol cl-status)
1203 (find-symbol (symbol-name symbol) *cl-package*)
1204 (if (and (eq symbol cl-symbol)
1205 (eq cl-status :external))
1206 ;; special case, to work around possible xc host weirdness
1207 ;; in COMMON-LISP package
1208 *cl-package*
1209 ;; ordinary case
1210 (let ((result (symbol-package symbol)))
1211 (unless (package-ok-for-target-symbol-p result)
1212 (bug "~A in bad package for target: ~A" symbol result))
1213 result))))
1215 (defvar *uninterned-symbol-table* (make-hash-table :test #'equal))
1216 ;; This coalesces references to uninterned symbols, which is allowed because
1217 ;; "similar-as-constant" is defined by string comparison, and since we only have
1218 ;; base-strings during Genesis, there is no concern about upgraded array type.
1219 ;; There is a subtlety of whether coalescing may occur across files
1220 ;; - the target compiler doesn't and couldn't - but here it doesn't matter.
1221 (defun get-uninterned-symbol (name)
1222 (or (gethash name *uninterned-symbol-table*)
1223 (let ((cold-symbol (allocate-symbol name)))
1224 (setf (gethash name *uninterned-symbol-table*) cold-symbol))))
1226 ;;; Dump the target representation of HOST-VALUE,
1227 ;;; the type of which is in a restrictive set.
1228 (defun host-constant-to-core (host-value)
1229 ;; rough check for no shared substructure and/or circularity.
1230 ;; of course this would be wrong if it were a string containing "#1="
1231 (when (search "#1=" (write-to-string host-value :circle t :readably t))
1232 (warn "Strange constant to core from Genesis: ~S" host-value))
1233 (labels ((target-representation (value)
1234 (etypecase value
1235 (symbol (if (symbol-package value)
1236 (cold-intern value)
1237 (get-uninterned-symbol (string value))))
1238 (number (number-to-core value))
1239 (string (base-string-to-core value))
1240 (cons (cold-cons (target-representation (car value))
1241 (target-representation (cdr value))))
1242 (simple-vector
1243 (vector-in-core (map 'list #'target-representation value))))))
1244 (target-representation host-value)))
1246 ;;; Return a handle on an interned symbol. If necessary allocate the
1247 ;;; symbol and record its home package.
1248 (defun cold-intern (symbol
1249 &key (access nil)
1250 (gspace *dynamic*)
1251 &aux (package (symbol-package-for-target-symbol symbol)))
1252 (aver (package-ok-for-target-symbol-p package))
1254 ;; Anything on the cross-compilation host which refers to the target
1255 ;; machinery through the host SB-XC package should be translated to
1256 ;; something on the target which refers to the same machinery
1257 ;; through the target COMMON-LISP package.
1258 (let ((p (find-package "SB-XC")))
1259 (when (eq package p)
1260 (setf package *cl-package*))
1261 (when (eq (symbol-package symbol) p)
1262 (setf symbol (intern (symbol-name symbol) *cl-package*))))
1264 (or (get symbol 'cold-intern-info)
1265 (let ((pkg-info (gethash (package-name package) *cold-package-symbols*))
1266 (handle (allocate-symbol (symbol-name symbol) :gspace gspace)))
1267 ;; maintain reverse map from target descriptor to host symbol
1268 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1269 (unless pkg-info
1270 (error "No target package descriptor for ~S" package))
1271 (record-accessibility
1272 (or access (nth-value 1 (find-symbol (symbol-name symbol) package)))
1273 handle pkg-info symbol package t)
1274 #!+sb-thread
1275 (assign-tls-index symbol handle)
1276 (acond ((eq package *keyword-package*)
1277 (setq access :external)
1278 (cold-set handle handle))
1279 ((assoc symbol sb-cold:*symbol-values-for-genesis*)
1280 (cold-set handle
1281 (host-constant-to-core
1282 (let ((*package* (find-package (cddr it))))
1283 (eval (cadr it)))))))
1284 (setf (get symbol 'cold-intern-info) handle))))
1286 (defun record-accessibility (accessibility symbol-descriptor target-pkg-info
1287 host-symbol host-package &optional set-home-p)
1288 (when set-home-p
1289 (write-wordindexed symbol-descriptor sb!vm:symbol-package-slot
1290 (car target-pkg-info)))
1291 (when (member host-symbol (package-shadowing-symbols host-package))
1292 ;; Fail in an obvious way if target shadowing symbols exist.
1293 ;; (This is simply not an important use-case during system bootstrap.)
1294 (error "Genesis doesn't like shadowing symbol ~S, sorry." host-symbol))
1295 (let ((access-lists (cdr target-pkg-info)))
1296 (case accessibility
1297 (:external (push symbol-descriptor (car access-lists)))
1298 (:internal (push symbol-descriptor (cdr access-lists)))
1299 (t (error "~S inaccessible in package ~S" host-symbol host-package)))))
1301 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1302 ;;; It might be nice to put NIL on a readonly page by itself to prevent unsafe
1303 ;;; code from destroying the world with (RPLACx nil 'kablooey)
1304 (defun make-nil-descriptor (target-cl-pkg-info)
1305 (let* ((des (allocate-header+object *static* sb!vm:symbol-size 0))
1306 (result (make-descriptor (descriptor-high des)
1307 (+ (descriptor-low des)
1308 (* 2 sb!vm:n-word-bytes)
1309 (- sb!vm:list-pointer-lowtag
1310 sb!vm:other-pointer-lowtag)))))
1311 (write-wordindexed des
1313 (make-other-immediate-descriptor
1315 sb!vm:symbol-header-widetag))
1316 (write-wordindexed des
1317 (+ 1 sb!vm:symbol-value-slot)
1318 result)
1319 (write-wordindexed des
1320 (+ 2 sb!vm:symbol-value-slot) ; = 1 + symbol-hash-slot
1321 result)
1322 (write-wordindexed des
1323 (+ 1 sb!vm:symbol-info-slot)
1324 (cold-cons result result)) ; NIL's info is (nil . nil)
1325 (write-wordindexed des
1326 (+ 1 sb!vm:symbol-name-slot)
1327 ;; NIL's name is in dynamic space because any extra
1328 ;; bytes allocated in static space would need to
1329 ;; be accounted for by STATIC-SYMBOL-OFFSET.
1330 (base-string-to-core "NIL" *dynamic*))
1331 ;; RECORD-ACCESSIBILITY can't assign to the package slot
1332 ;; due to NIL's base address and lowtag being nonstandard.
1333 (write-wordindexed des
1334 (+ 1 sb!vm:symbol-package-slot)
1335 (car target-cl-pkg-info))
1336 (record-accessibility :external result target-cl-pkg-info nil *cl-package*)
1337 (setf (gethash (descriptor-bits result) *cold-symbols*) nil
1338 (get nil 'cold-intern-info) result)))
1340 ;;; Since the initial symbols must be allocated before we can intern
1341 ;;; anything else, we intern those here. We also set the value of T.
1342 (defun initialize-non-nil-symbols ()
1343 #!+sb-doc
1344 "Initialize the cold load symbol-hacking data structures."
1345 ;; Intern the others.
1346 (dolist (symbol sb!vm:*static-symbols*)
1347 (let* ((des (cold-intern symbol :gspace *static*))
1348 (offset-wanted (sb!vm:static-symbol-offset symbol))
1349 (offset-found (- (descriptor-low des)
1350 (descriptor-low *nil-descriptor*))))
1351 (unless (= offset-wanted offset-found)
1352 ;; FIXME: should be fatal
1353 (warn "Offset from ~S to ~S is ~W, not ~W"
1354 symbol
1356 offset-found
1357 offset-wanted))))
1358 ;; Establish the value of T.
1359 (let ((t-symbol (cold-intern t :gspace *static*)))
1360 (cold-set t-symbol t-symbol))
1361 ;; Establish the value of *PSEUDO-ATOMIC-BITS* so that the
1362 ;; allocation sequences that expect it to be zero upon entrance
1363 ;; actually find it to be so.
1364 #!+(or x86-64 x86)
1365 (let ((p-a-a-symbol (cold-intern '*pseudo-atomic-bits*
1366 :gspace *static*)))
1367 (cold-set p-a-a-symbol (make-fixnum-descriptor 0))))
1369 ;;; a helper function for FINISH-SYMBOLS: Return a cold alist suitable
1370 ;;; to be stored in *!INITIAL-LAYOUTS*.
1371 (defun cold-list-all-layouts ()
1372 (let ((layouts nil)
1373 (result *nil-descriptor*))
1374 (maphash (lambda (key stuff)
1375 (push (cons key (first stuff)) layouts))
1376 *cold-layouts*)
1377 (flet ((sorter (x y)
1378 (let ((xpn (package-name (symbol-package-for-target-symbol x)))
1379 (ypn (package-name (symbol-package-for-target-symbol y))))
1380 (cond
1381 ((string= x y) (string< xpn ypn))
1382 (t (string< x y))))))
1383 (setq layouts (sort layouts #'sorter :key #'car)))
1384 (dolist (layout layouts result)
1385 (cold-push (cold-cons (cold-intern (car layout)) (cdr layout))
1386 result))))
1388 ;;; Establish initial values for magic symbols.
1390 (defun finish-symbols ()
1392 ;; Everything between this preserved-for-posterity comment down to
1393 ;; the assignment of *CURRENT-CATCH-BLOCK* could be entirely deleted,
1394 ;; including the list of *C-CALLABLE-STATIC-SYMBOLS* itself,
1395 ;; if it is GC-safe for the C runtime to have its own implementation
1396 ;; of the INFO-VECTOR-FDEFN function in a multi-threaded build.
1398 ;; "I think the point of setting these functions into SYMBOL-VALUEs
1399 ;; here, instead of using SYMBOL-FUNCTION, is that in CMU CL
1400 ;; SYMBOL-FUNCTION reduces to FDEFINITION, which is a pretty
1401 ;; hairy operation (involving globaldb.lisp etc.) which we don't
1402 ;; want to invoke early in cold init. -- WHN 2001-12-05"
1404 ;; So... that's no longer true. We _do_ associate symbol -> fdefn in genesis.
1405 ;; Additionally, the INFO-VECTOR-FDEFN function is extremely simple and could
1406 ;; easily be implemented in C. However, info-vectors are inevitably
1407 ;; reallocated when new info is attached to a symbol, so the vectors can't be
1408 ;; in static space; they'd gradually become permanent garbage if they did.
1409 ;; That's the real reason for preserving the approach of storing an #<fdefn>
1410 ;; in a symbol's value cell - that location is static, the symbol-info is not.
1412 ;; FIXME: So OK, that's a reasonable reason to do something weird like
1413 ;; this, but this is still a weird thing to do, and we should change
1414 ;; the names to highlight that something weird is going on. Perhaps
1415 ;; *MAYBE-GC-FUN*, *INTERNAL-ERROR-FUN*, *HANDLE-BREAKPOINT-FUN*,
1416 ;; and *HANDLE-FUN-END-BREAKPOINT-FUN*...
1417 (dolist (symbol sb!vm::*c-callable-static-symbols*)
1418 (cold-set symbol (cold-fdefinition-object (cold-intern symbol))))
1420 (cold-set 'sb!vm::*current-catch-block* (make-fixnum-descriptor 0))
1421 (cold-set 'sb!vm::*current-unwind-protect-block* (make-fixnum-descriptor 0))
1423 (cold-set '*free-interrupt-context-index* (make-fixnum-descriptor 0))
1425 (cold-set '*!initial-layouts* (cold-list-all-layouts))
1427 #!+sb-thread
1428 (progn
1429 (cold-set 'sb!vm::*free-tls-index*
1430 (make-random-descriptor (ash *genesis-tls-counter*
1431 sb!vm:word-shift)))
1432 (cold-set 'sb!vm::*tls-index-lock* (make-fixnum-descriptor 0)))
1434 (dolist (symbol sb!impl::*cache-vector-symbols*)
1435 (cold-set symbol *nil-descriptor*))
1437 ;; Symbols for which no call to COLD-INTERN would occur - due to not being
1438 ;; referenced until warm init - must be artificially cold-interned.
1439 ;; Inasmuch as the "offending" things are compiled by ordinary target code
1440 ;; and not cold-init, I think we should use an ordinary DEFPACKAGE for
1441 ;; the added-on bits. What I've done is somewhat of a fragile kludge.
1442 (let (syms)
1443 (with-package-iterator (iter '("SB!PCL" "SB!MOP" "SB!GRAY" "SB!SEQUENCE"
1444 "SB!PROFILE" "SB!EXT" "SB!VM"
1445 "SB!C" "SB!FASL" "SB!DEBUG")
1446 :external)
1447 (loop
1448 (multiple-value-bind (foundp sym accessibility package) (iter)
1449 (declare (ignore accessibility))
1450 (cond ((not foundp) (return))
1451 ((eq (symbol-package sym) package) (push sym syms))))))
1452 (setf syms (stable-sort syms #'string<))
1453 (dolist (sym syms)
1454 (cold-intern sym)))
1456 (let ((cold-pkg-inits *nil-descriptor*)
1457 cold-package-symbols-list)
1458 (maphash (lambda (name info)
1459 (push (cons name info) cold-package-symbols-list))
1460 *cold-package-symbols*)
1461 (setf cold-package-symbols-list
1462 (sort cold-package-symbols-list #'string< :key #'car))
1463 (dolist (pkgcons cold-package-symbols-list)
1464 (destructuring-bind (pkg-name . pkg-info) pkgcons
1465 (unless (member pkg-name '("COMMON-LISP" "KEYWORD") :test 'string=)
1466 (let ((host-pkg (find-package pkg-name))
1467 (sb-xc-pkg (find-package "SB-XC"))
1468 syms)
1469 (with-package-iterator (iter host-pkg :internal :external)
1470 (loop (multiple-value-bind (foundp sym accessibility) (iter)
1471 (unless foundp (return))
1472 (unless (or (eq (symbol-package sym) host-pkg)
1473 (eq (symbol-package sym) sb-xc-pkg))
1474 (push (cons sym accessibility) syms)))))
1475 (setq syms (sort syms #'string< :key #'car))
1476 (dolist (symcons syms)
1477 (destructuring-bind (sym . accessibility) symcons
1478 (record-accessibility accessibility (cold-intern sym)
1479 pkg-info sym host-pkg)))))
1480 (cold-push (cold-cons (car pkg-info)
1481 (cold-cons (vector-in-core (cadr pkg-info))
1482 (vector-in-core (cddr pkg-info))))
1483 cold-pkg-inits)))
1484 (cold-set 'sb!impl::*!initial-symbols* cold-pkg-inits))
1486 (attach-fdefinitions-to-symbols)
1488 (cold-set '*!reversed-cold-toplevels* *current-reversed-cold-toplevels*)
1489 (cold-set '*!initial-debug-sources* *current-debug-sources*)
1491 #!+(or x86 x86-64)
1492 (progn
1493 (cold-set 'sb!vm::*fp-constant-0d0* (number-to-core 0d0))
1494 (cold-set 'sb!vm::*fp-constant-1d0* (number-to-core 1d0))
1495 (cold-set 'sb!vm::*fp-constant-0f0* (number-to-core 0f0))
1496 (cold-set 'sb!vm::*fp-constant-1f0* (number-to-core 1f0))))
1498 ;;;; functions and fdefinition objects
1500 ;;; a hash table mapping from fdefinition names to descriptors of cold
1501 ;;; objects
1503 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1504 ;;; we want to have only one entry per name, this must be an 'EQUAL
1505 ;;; hash table, not the default 'EQL.
1506 (defvar *cold-fdefn-objects*)
1508 (defvar *cold-fdefn-gspace* nil)
1510 ;;; Given a cold representation of a symbol, return a warm
1511 ;;; representation.
1512 (defun warm-symbol (des)
1513 ;; Note that COLD-INTERN is responsible for keeping the
1514 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1515 ;; uninterned symbol, the code below will fail. But as long as we
1516 ;; don't need to look up uninterned symbols during bootstrapping,
1517 ;; that's OK..
1518 (multiple-value-bind (symbol found-p)
1519 (gethash (descriptor-bits des) *cold-symbols*)
1520 (declare (type symbol symbol))
1521 (unless found-p
1522 (error "no warm symbol"))
1523 symbol))
1525 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1526 (defun cold-car (des)
1527 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1528 (read-wordindexed des sb!vm:cons-car-slot))
1529 (defun cold-cdr (des)
1530 (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1531 (read-wordindexed des sb!vm:cons-cdr-slot))
1532 (defun cold-null (des)
1533 (= (descriptor-bits des)
1534 (descriptor-bits *nil-descriptor*)))
1536 ;;; Given a cold representation of a function name, return a warm
1537 ;;; representation.
1538 (declaim (ftype (function ((or symbol descriptor)) (or symbol list)) warm-fun-name))
1539 (defun warm-fun-name (des)
1540 (let ((result
1541 (if (symbolp des)
1542 ;; This parallels the logic at the start of COLD-INTERN
1543 ;; which re-homes symbols in SB-XC to COMMON-LISP.
1544 (if (eq (symbol-package des) (find-package "SB-XC"))
1545 (intern (symbol-name des) *cl-package*)
1546 des)
1547 (ecase (descriptor-lowtag des)
1548 (#.sb!vm:list-pointer-lowtag
1549 (aver (not (cold-null des))) ; function named NIL? please no..
1550 ;; Do cold (DESTRUCTURING-BIND (COLD-CAR COLD-CADR) DES ..).
1551 (let* ((car-des (cold-car des))
1552 (cdr-des (cold-cdr des))
1553 (cadr-des (cold-car cdr-des))
1554 (cddr-des (cold-cdr cdr-des)))
1555 (aver (cold-null cddr-des))
1556 (list (warm-symbol car-des)
1557 (warm-symbol cadr-des))))
1558 (#.sb!vm:other-pointer-lowtag
1559 (warm-symbol des))))))
1560 (legal-fun-name-or-type-error result)
1561 result))
1563 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw)
1564 (declare (type (or symbol descriptor) cold-name))
1565 (/noshow0 "/cold-fdefinition-object")
1566 (let ((warm-name (warm-fun-name cold-name)))
1567 (or (gethash warm-name *cold-fdefn-objects*)
1568 (let ((fdefn (allocate-header+object (or *cold-fdefn-gspace* *dynamic*)
1569 (1- sb!vm:fdefn-size)
1570 sb!vm:fdefn-widetag)))
1571 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
1572 (write-wordindexed fdefn sb!vm:fdefn-name-slot cold-name)
1573 (unless leave-fn-raw
1574 (write-wordindexed fdefn sb!vm:fdefn-fun-slot
1575 *nil-descriptor*)
1576 (write-wordindexed fdefn
1577 sb!vm:fdefn-raw-addr-slot
1578 (make-random-descriptor
1579 (cold-foreign-symbol-address "undefined_tramp"))))
1580 fdefn))))
1582 ;;; Handle the at-cold-init-time, fset-for-static-linkage operation
1583 ;;; requested by FOP-FSET.
1584 (defun static-fset (cold-name defn)
1585 (declare (type (or symbol descriptor) cold-name))
1586 (let ((fdefn (cold-fdefinition-object cold-name t))
1587 (type (logand (descriptor-low (read-memory defn)) sb!vm:widetag-mask)))
1588 (write-wordindexed fdefn sb!vm:fdefn-fun-slot defn)
1589 (write-wordindexed fdefn
1590 sb!vm:fdefn-raw-addr-slot
1591 (ecase type
1592 (#.sb!vm:simple-fun-header-widetag
1593 (/noshow0 "static-fset (simple-fun)")
1594 #!+(or sparc arm)
1595 defn
1596 #!-(or sparc arm)
1597 (make-random-descriptor
1598 (+ (logandc2 (descriptor-bits defn)
1599 sb!vm:lowtag-mask)
1600 (ash sb!vm:simple-fun-code-offset
1601 sb!vm:word-shift))))
1602 (#.sb!vm:closure-header-widetag
1603 (/show0 "/static-fset (closure)")
1604 (make-random-descriptor
1605 (cold-foreign-symbol-address "closure_tramp")))))
1606 fdefn))
1608 (defun initialize-static-fns ()
1609 (let ((*cold-fdefn-gspace* *static*))
1610 (dolist (sym sb!vm:*static-funs*)
1611 (let* ((fdefn (cold-fdefinition-object (cold-intern sym)))
1612 (offset (- (+ (- (descriptor-low fdefn)
1613 sb!vm:other-pointer-lowtag)
1614 (* sb!vm:fdefn-raw-addr-slot sb!vm:n-word-bytes))
1615 (descriptor-low *nil-descriptor*)))
1616 (desired (sb!vm:static-fun-offset sym)))
1617 (unless (= offset desired)
1618 ;; FIXME: should be fatal
1619 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1620 sym nil offset desired))))))
1622 ;; Create pointer from SYMBOL and/or (SETF SYMBOL) to respective fdefinition
1624 (defun attach-fdefinitions-to-symbols ()
1625 (let ((hashtable (make-hash-table :test #'eq)))
1626 ;; Collect fdefinitions that go with one symbol, e.g. CAR and (SETF CAR),
1627 ;; using the host's code for manipulating a packed info-vector.
1628 (maphash (lambda (warm-name cold-fdefn)
1629 (sb!c::with-globaldb-name (key1 key2) warm-name
1630 :hairy (error "Hairy fdefn name in genesis: ~S" warm-name)
1631 :simple
1632 (setf (gethash key1 hashtable)
1633 (sb!c::packed-info-insert
1634 (gethash key1 hashtable sb!c::+nil-packed-infos+)
1635 key2 sb!c::+fdefn-type-num+ cold-fdefn))))
1636 *cold-fdefn-objects*)
1637 ;; Emit in the same order symbols reside in core to avoid
1638 ;; sensitivity to the iteration order of host's maphash.
1639 (loop for (warm-sym . info)
1640 in (sort (sb!impl::%hash-table-alist hashtable) #'<
1641 :key (lambda (x) (descriptor-bits (cold-intern (car x)))))
1642 do (write-wordindexed
1643 (cold-intern warm-sym) sb!vm:symbol-info-slot
1644 ;; Each vector will have one fixnum, possibly the symbol SETF,
1645 ;; and one or two #<fdefn> objects in it.
1646 (vector-in-core
1647 (map 'list (lambda (elt)
1648 (etypecase elt
1649 (symbol (cold-intern elt))
1650 (fixnum (make-fixnum-descriptor elt))
1651 (descriptor elt)))
1652 info))))))
1655 ;;;; fixups and related stuff
1657 ;;; an EQUAL hash table
1658 (defvar *cold-foreign-symbol-table*)
1659 (declaim (type hash-table *cold-foreign-symbol-table*))
1661 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1662 ;; the C runtime.
1663 (defun load-cold-foreign-symbol-table (filename)
1664 (/show "load-cold-foreign-symbol-table" filename)
1665 (with-open-file (file filename)
1666 (loop for line = (read-line file nil nil)
1667 while line do
1668 ;; UNIX symbol tables might have tabs in them, and tabs are
1669 ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1670 ;; nice portable way to deal with them within Lisp, alas.
1671 ;; Fortunately, it's easy to use UNIX command line tools like
1672 ;; sed to remove the problem, so it's not too painful for us
1673 ;; to push responsibility for converting tabs to spaces out to
1674 ;; the caller.
1676 ;; Other non-STANDARD-CHARs are problematic for the same reason.
1677 ;; Make sure that there aren't any..
1678 (let ((ch (find-if (lambda (char)
1679 (not (typep char 'standard-char)))
1680 line)))
1681 (when ch
1682 (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1684 line)))
1685 (setf line (string-trim '(#\space) line))
1686 (let ((p1 (position #\space line :from-end nil))
1687 (p2 (position #\space line :from-end t)))
1688 (if (not (and p1 p2 (< p1 p2)))
1689 ;; KLUDGE: It's too messy to try to understand all
1690 ;; possible output from nm, so we just punt the lines we
1691 ;; don't recognize. We realize that there's some chance
1692 ;; that might get us in trouble someday, so we warn
1693 ;; about it.
1694 (warn "ignoring unrecognized line ~S in ~A" line filename)
1695 (multiple-value-bind (value name)
1696 (if (string= "0x" line :end2 2)
1697 (values (parse-integer line :start 2 :end p1 :radix 16)
1698 (subseq line (1+ p2)))
1699 (values (parse-integer line :end p1 :radix 16)
1700 (subseq line (1+ p2))))
1701 ;; KLUDGE CLH 2010-05-31: on darwin, nm gives us
1702 ;; _function but dlsym expects us to look up
1703 ;; function, without the leading _ . Therefore, we
1704 ;; strip it off here.
1705 #!+darwin
1706 (when (equal (char name 0) #\_)
1707 (setf name (subseq name 1)))
1708 (multiple-value-bind (old-value found)
1709 (gethash name *cold-foreign-symbol-table*)
1710 (when (and found
1711 (not (= old-value value)))
1712 (warn "redefining ~S from #X~X to #X~X"
1713 name old-value value)))
1714 (/show "adding to *cold-foreign-symbol-table*:" name value)
1715 (setf (gethash name *cold-foreign-symbol-table*) value)
1716 #!+win32
1717 (let ((at-position (position #\@ name)))
1718 (when at-position
1719 (let ((name (subseq name 0 at-position)))
1720 (multiple-value-bind (old-value found)
1721 (gethash name *cold-foreign-symbol-table*)
1722 (when (and found
1723 (not (= old-value value)))
1724 (warn "redefining ~S from #X~X to #X~X"
1725 name old-value value)))
1726 (setf (gethash name *cold-foreign-symbol-table*)
1727 value)))))))))
1728 (values)) ;; PROGN
1730 (defun cold-foreign-symbol-address (name)
1731 (or (find-foreign-symbol-in-table name *cold-foreign-symbol-table*)
1732 *foreign-symbol-placeholder-value*
1733 (progn
1734 (format *error-output* "~&The foreign symbol table is:~%")
1735 (maphash (lambda (k v)
1736 (format *error-output* "~&~S = #X~8X~%" k v))
1737 *cold-foreign-symbol-table*)
1738 (error "The foreign symbol ~S is undefined." name))))
1740 (defvar *cold-assembler-routines*)
1742 (defvar *cold-assembler-fixups*)
1744 (defun record-cold-assembler-routine (name address)
1745 (/xhow "in RECORD-COLD-ASSEMBLER-ROUTINE" name address)
1746 (push (cons name address)
1747 *cold-assembler-routines*))
1749 (defun record-cold-assembler-fixup (routine
1750 code-object
1751 offset
1752 &optional
1753 (kind :both))
1754 (push (list routine code-object offset kind)
1755 *cold-assembler-fixups*))
1757 (defun lookup-assembler-reference (symbol)
1758 (let ((value (cdr (assoc symbol *cold-assembler-routines*))))
1759 ;; FIXME: Should this be ERROR instead of WARN?
1760 (unless value
1761 (warn "Assembler routine ~S not defined." symbol))
1762 value))
1764 ;;; The x86 port needs to store code fixups along with code objects if
1765 ;;; they are to be moved, so fixups for code objects in the dynamic
1766 ;;; heap need to be noted.
1767 #!+x86
1768 (defvar *load-time-code-fixups*)
1770 #!+x86
1771 (defun note-load-time-code-fixup (code-object offset)
1772 ;; If CODE-OBJECT might be moved
1773 (when (= (gspace-identifier (descriptor-intuit-gspace code-object))
1774 dynamic-core-space-id)
1775 (push offset (gethash (descriptor-bits code-object)
1776 *load-time-code-fixups*
1777 nil)))
1778 (values))
1780 #!+x86
1781 (defun output-load-time-code-fixups ()
1782 (let ((fixup-infos nil))
1783 (maphash
1784 (lambda (code-object-address fixup-offsets)
1785 (push (cons code-object-address fixup-offsets) fixup-infos))
1786 *load-time-code-fixups*)
1787 (setq fixup-infos (sort fixup-infos #'< :key #'car))
1788 (dolist (fixup-info fixup-infos)
1789 (let ((code-object-address (car fixup-info))
1790 (fixup-offsets (cdr fixup-info)))
1791 (let ((fixup-vector
1792 (allocate-vector-object
1793 *dynamic* sb!vm:n-word-bits (length fixup-offsets)
1794 sb!vm:simple-array-unsigned-byte-32-widetag)))
1795 (do ((index sb!vm:vector-data-offset (1+ index))
1796 (fixups fixup-offsets (cdr fixups)))
1797 ((null fixups))
1798 (write-wordindexed fixup-vector index
1799 (make-random-descriptor (car fixups))))
1800 ;; KLUDGE: The fixup vector is stored as the first constant,
1801 ;; not as a separately-named slot.
1802 (write-wordindexed (make-random-descriptor code-object-address)
1803 sb!vm:code-constants-offset
1804 fixup-vector))))))
1806 ;;; Given a pointer to a code object and an offset relative to the
1807 ;;; tail of the code object's header, return an offset relative to the
1808 ;;; (beginning of the) code object.
1810 ;;; FIXME: It might be clearer to reexpress
1811 ;;; (LET ((X (CALC-OFFSET CODE-OBJECT OFFSET0))) ..)
1812 ;;; as
1813 ;;; (LET ((X (+ OFFSET0 (CODE-OBJECT-HEADER-N-BYTES CODE-OBJECT)))) ..).
1814 (declaim (ftype (function (descriptor sb!vm:word)) calc-offset))
1815 (defun calc-offset (code-object offset-from-tail-of-header)
1816 (let* ((header (read-memory code-object))
1817 (header-n-words (ash (descriptor-bits header)
1818 (- sb!vm:n-widetag-bits)))
1819 (header-n-bytes (ash header-n-words sb!vm:word-shift))
1820 (result (+ offset-from-tail-of-header header-n-bytes)))
1821 result))
1823 (declaim (ftype (function (descriptor sb!vm:word sb!vm:word keyword))
1824 do-cold-fixup))
1825 (defun do-cold-fixup (code-object after-header value kind)
1826 (let* ((offset-within-code-object (calc-offset code-object after-header))
1827 (gspace-bytes (descriptor-bytes code-object))
1828 (gspace-byte-offset (+ (descriptor-byte-offset code-object)
1829 offset-within-code-object))
1830 (gspace-byte-address (gspace-byte-address
1831 (descriptor-gspace code-object))))
1832 ;; There's just a ton of code here that gets deleted,
1833 ;; inhibiting the view of the the forest through the trees.
1834 ;; Use of #+sbcl would say "probable bug in read-time conditional"
1835 #+#.(cl:if (cl:member :sbcl cl:*features*) '(and) '(or))
1836 (declare (sb-ext:muffle-conditions sb-ext:code-deletion-note))
1837 (ecase +backend-fasl-file-implementation+
1838 ;; See CMU CL source for other formerly-supported architectures
1839 ;; (and note that you have to rewrite them to use BVREF-X
1840 ;; instead of SAP-REF).
1841 (:alpha
1842 (ecase kind
1843 (:jmp-hint
1844 (assert (zerop (ldb (byte 2 0) value))))
1845 (:bits-63-48
1846 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1847 (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
1848 (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
1849 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1850 (ldb (byte 8 48) value)
1851 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1852 (ldb (byte 8 56) value))))
1853 (:bits-47-32
1854 (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1855 (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
1856 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1857 (ldb (byte 8 32) value)
1858 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1859 (ldb (byte 8 40) value))))
1860 (:ldah
1861 (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
1862 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1863 (ldb (byte 8 16) value)
1864 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1865 (ldb (byte 8 24) value))))
1866 (:lda
1867 (setf (bvref-8 gspace-bytes gspace-byte-offset)
1868 (ldb (byte 8 0) value)
1869 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1870 (ldb (byte 8 8) value)))))
1871 (:arm
1872 (ecase kind
1873 (:absolute
1874 (setf (bvref-32 gspace-bytes gspace-byte-offset) value))))
1875 (:hppa
1876 (ecase kind
1877 (:load
1878 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1879 (logior (mask-field (byte 18 14)
1880 (bvref-32 gspace-bytes gspace-byte-offset))
1881 (if (< value 0)
1882 (1+ (ash (ldb (byte 13 0) value) 1))
1883 (ash (ldb (byte 13 0) value) 1)))))
1884 (:load11u
1885 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1886 (logior (mask-field (byte 18 14)
1887 (bvref-32 gspace-bytes gspace-byte-offset))
1888 (if (< value 0)
1889 (1+ (ash (ldb (byte 10 0) value) 1))
1890 (ash (ldb (byte 11 0) value) 1)))))
1891 (:load-short
1892 (let ((low-bits (ldb (byte 11 0) value)))
1893 (assert (<= 0 low-bits (1- (ash 1 4)))))
1894 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1895 (logior (ash (dpb (ldb (byte 4 0) value)
1896 (byte 4 1)
1897 (ldb (byte 1 4) value)) 17)
1898 (logand (bvref-32 gspace-bytes gspace-byte-offset)
1899 #xffe0ffff))))
1900 (:hi
1901 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1902 (logior (mask-field (byte 11 21)
1903 (bvref-32 gspace-bytes gspace-byte-offset))
1904 (ash (ldb (byte 5 13) value) 16)
1905 (ash (ldb (byte 2 18) value) 14)
1906 (ash (ldb (byte 2 11) value) 12)
1907 (ash (ldb (byte 11 20) value) 1)
1908 (ldb (byte 1 31) value))))
1909 (:branch
1910 (let ((bits (ldb (byte 9 2) value)))
1911 (assert (zerop (ldb (byte 2 0) value)))
1912 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1913 (logior (ash bits 3)
1914 (mask-field (byte 1 1) (bvref-32 gspace-bytes gspace-byte-offset))
1915 (mask-field (byte 3 13) (bvref-32 gspace-bytes gspace-byte-offset))
1916 (mask-field (byte 11 21) (bvref-32 gspace-bytes gspace-byte-offset))))))))
1917 (:mips
1918 (ecase kind
1919 (:jump
1920 (assert (zerop (ash value -28)))
1921 (setf (ldb (byte 26 0)
1922 (bvref-32 gspace-bytes gspace-byte-offset))
1923 (ash value -2)))
1924 (:lui
1925 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1926 (logior (mask-field (byte 16 16)
1927 (bvref-32 gspace-bytes gspace-byte-offset))
1928 (ash (1+ (ldb (byte 17 15) value)) -1))))
1929 (:addi
1930 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1931 (logior (mask-field (byte 16 16)
1932 (bvref-32 gspace-bytes gspace-byte-offset))
1933 (ldb (byte 16 0) value))))))
1934 ;; FIXME: PowerPC Fixups are not fully implemented. The bit
1935 ;; here starts to set things up to work properly, but there
1936 ;; needs to be corresponding code in ppc-vm.lisp
1937 (:ppc
1938 (ecase kind
1939 (:ba
1940 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1941 (dpb (ash value -2) (byte 24 2)
1942 (bvref-32 gspace-bytes gspace-byte-offset))))
1943 (:ha
1944 (let* ((un-fixed-up (bvref-16 gspace-bytes
1945 (+ gspace-byte-offset 2)))
1946 (fixed-up (+ un-fixed-up value))
1947 (h (ldb (byte 16 16) fixed-up))
1948 (l (ldb (byte 16 0) fixed-up)))
1949 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1950 (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
1952 (let* ((un-fixed-up (bvref-16 gspace-bytes
1953 (+ gspace-byte-offset 2)))
1954 (fixed-up (+ un-fixed-up value)))
1955 (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1956 (ldb (byte 16 0) fixed-up))))))
1957 (:sparc
1958 (ecase kind
1959 (:call
1960 (error "can't deal with call fixups yet"))
1961 (:sethi
1962 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1963 (dpb (ldb (byte 22 10) value)
1964 (byte 22 0)
1965 (bvref-32 gspace-bytes gspace-byte-offset))))
1966 (:add
1967 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1968 (dpb (ldb (byte 10 0) value)
1969 (byte 10 0)
1970 (bvref-32 gspace-bytes gspace-byte-offset))))))
1971 ((:x86 :x86-64)
1972 ;; XXX: Note that un-fixed-up is read via bvref-word, which is
1973 ;; 64 bits wide on x86-64, but the fixed-up value is written
1974 ;; via bvref-32. This would make more sense if we supported
1975 ;; :absolute64 fixups, but apparently the cross-compiler
1976 ;; doesn't dump them.
1977 (let* ((un-fixed-up (bvref-word gspace-bytes
1978 gspace-byte-offset))
1979 (code-object-start-addr (logandc2 (descriptor-bits code-object)
1980 sb!vm:lowtag-mask)))
1981 (assert (= code-object-start-addr
1982 (+ gspace-byte-address
1983 (descriptor-byte-offset code-object))))
1984 (ecase kind
1985 (:absolute
1986 (let ((fixed-up (+ value un-fixed-up)))
1987 (setf (bvref-32 gspace-bytes gspace-byte-offset)
1988 fixed-up)
1989 ;; comment from CMU CL sources:
1991 ;; Note absolute fixups that point within the object.
1992 ;; KLUDGE: There seems to be an implicit assumption in
1993 ;; the old CMU CL code here, that if it doesn't point
1994 ;; before the object, it must point within the object
1995 ;; (not beyond it). It would be good to add an
1996 ;; explanation of why that's true, or an assertion that
1997 ;; it's really true, or both.
1999 ;; One possible explanation is that all absolute fixups
2000 ;; point either within the code object, within the
2001 ;; runtime, within read-only or static-space, or within
2002 ;; the linkage-table space. In all x86 configurations,
2003 ;; these areas are prior to the start of dynamic space,
2004 ;; where all the code-objects are loaded.
2005 #!+x86
2006 (unless (< fixed-up code-object-start-addr)
2007 (note-load-time-code-fixup code-object
2008 after-header))))
2009 (:relative ; (used for arguments to X86 relative CALL instruction)
2010 (let ((fixed-up (- (+ value un-fixed-up)
2011 gspace-byte-address
2012 gspace-byte-offset
2013 4))) ; "length of CALL argument"
2014 (setf (bvref-32 gspace-bytes gspace-byte-offset)
2015 fixed-up)
2016 ;; Note relative fixups that point outside the code
2017 ;; object, which is to say all relative fixups, since
2018 ;; relative addressing within a code object never needs
2019 ;; a fixup.
2020 #!+x86
2021 (note-load-time-code-fixup code-object
2022 after-header))))))))
2023 (values))
2025 (defun resolve-assembler-fixups ()
2026 (dolist (fixup *cold-assembler-fixups*)
2027 (let* ((routine (car fixup))
2028 (value (lookup-assembler-reference routine)))
2029 (when value
2030 (do-cold-fixup (second fixup) (third fixup) value (fourth fixup))))))
2032 #!+sb-dynamic-core
2033 (progn
2034 (defparameter *dyncore-address* sb!vm::linkage-table-space-start)
2035 (defparameter *dyncore-linkage-keys* nil)
2036 (defparameter *dyncore-table* (make-hash-table :test 'equal))
2038 (defun dyncore-note-symbol (symbol-name datap)
2039 "Register a symbol and return its address in proto-linkage-table."
2040 (let ((key (cons symbol-name datap)))
2041 (symbol-macrolet ((entry (gethash key *dyncore-table*)))
2042 (or entry
2043 (setf entry
2044 (prog1 *dyncore-address*
2045 (push key *dyncore-linkage-keys*)
2046 (incf *dyncore-address* sb!vm::linkage-table-entry-size))))))))
2048 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
2049 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
2050 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
2051 ;;; target-load.lisp refers to.
2052 (defun foreign-symbols-to-core ()
2053 (let ((symbols nil)
2054 (result *nil-descriptor*))
2055 #!-sb-dynamic-core
2056 (progn
2057 (maphash (lambda (symbol value)
2058 (push (cons symbol value) symbols))
2059 *cold-foreign-symbol-table*)
2060 (setq symbols (sort symbols #'string< :key #'car))
2061 (dolist (symbol symbols)
2062 (cold-push (cold-cons (base-string-to-core (car symbol))
2063 (number-to-core (cdr symbol)))
2064 result)))
2065 (cold-set '*!initial-foreign-symbols* result)
2066 #!+sb-dynamic-core
2067 (let ((runtime-linking-list *nil-descriptor*))
2068 (dolist (symbol *dyncore-linkage-keys*)
2069 (cold-push (cold-cons (base-string-to-core (car symbol))
2070 (cdr symbol))
2071 runtime-linking-list))
2072 (cold-set 'sb!vm::*required-runtime-c-symbols*
2073 runtime-linking-list)))
2074 (let ((result *nil-descriptor*))
2075 (dolist (rtn (sort (copy-list *cold-assembler-routines*) #'string< :key #'car))
2076 (cold-push (cold-cons (cold-intern (car rtn))
2077 (number-to-core (cdr rtn)))
2078 result))
2079 (cold-set '*!initial-assembler-routines* result)))
2082 ;;;; general machinery for cold-loading FASL files
2084 ;;; FOP functions for cold loading
2085 (defvar *cold-fop-funs*
2086 ;; We start out with a copy of the ordinary *FOP-FUNS*. The ones
2087 ;; which aren't appropriate for cold load will be destructively
2088 ;; modified.
2089 (copy-seq *fop-funs*))
2091 (defun pop-fop-stack ()
2092 (let* ((stack *fop-stack*)
2093 (top (svref stack 0)))
2094 (declare (type index top))
2095 (when (eql 0 top)
2096 (error "FOP stack empty"))
2097 (setf (svref stack 0) (1- top))
2098 (svref stack top)))
2100 ;;; Cause a fop to have a special definition for cold load.
2102 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
2103 ;;; (1) looks up the code for this name (created by a previous
2104 ;;; DEFINE-FOP) instead of creating a code, and
2105 ;;; (2) stores its definition in the *COLD-FOP-FUNS* vector,
2106 ;;; instead of storing in the *FOP-FUNS* vector.
2107 (defmacro define-cold-fop ((name &optional arglist) &rest forms)
2108 (let* ((code (get name 'opcode))
2109 (argp (plusp (sbit (car *fop-signatures*) (ash code -2))))
2110 (fname (symbolicate "COLD-" name)))
2111 (unless code
2112 (error "~S is not a defined FOP." name))
2113 (when (and argp (not (singleton-p arglist)))
2114 (error "~S must take one argument" name))
2115 `(progn
2116 (defun ,fname ,arglist
2117 (macrolet ((pop-stack () `(pop-fop-stack))) ,@forms))
2118 ,@(loop for i from code to (logior code (if argp 3 0))
2119 collect `(setf (svref *cold-fop-funs* ,i) #',fname)))))
2121 ;;; Cause a fop to be undefined in cold load.
2122 (defmacro not-cold-fop (name)
2123 `(define-cold-fop (,name)
2124 (error "The fop ~S is not supported in cold load." ',name)))
2126 ;;; COLD-LOAD loads stuff into the core image being built by calling
2127 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
2128 ;;; loading functions.
2129 (defun cold-load (filename)
2130 #!+sb-doc
2131 "Load the file named by FILENAME into the cold load image being built."
2132 (let* ((*fop-funs* *cold-fop-funs*)
2133 (*cold-load-filename* (etypecase filename
2134 (string filename)
2135 (pathname (namestring filename)))))
2136 (with-open-file (s filename :element-type '(unsigned-byte 8))
2137 (load-as-fasl s nil nil))))
2139 ;;;; miscellaneous cold fops
2141 (define-cold-fop (fop-misc-trap) *unbound-marker*)
2143 (define-cold-fop (fop-character (c))
2144 (make-character-descriptor c))
2146 (define-cold-fop (fop-empty-list) nil)
2147 (define-cold-fop (fop-truth) t)
2149 (define-cold-fop (fop-struct (size)) ; n-words incl. layout, excluding header
2150 (let* ((layout (pop-stack))
2151 (result (allocate-structure-object *dynamic* size layout))
2152 (metadata
2153 (descriptor-fixnum
2154 (read-wordindexed
2155 layout
2156 (+ sb!vm:instance-slots-offset
2157 (target-layout-index
2158 #!-interleaved-raw-slots 'n-untagged-slots
2159 #!+interleaved-raw-slots 'untagged-bitmap)))))
2160 #!-interleaved-raw-slots (ntagged (- size metadata))
2162 #!+interleaved-raw-slots
2163 (unless (= metadata 0)
2164 (error "Interleaved raw slots not (yet) known to work in genesis."))
2166 (do ((index 1 (1+ index)))
2167 ((eql index size))
2168 (declare (fixnum index))
2169 (write-wordindexed result
2170 (+ index sb!vm:instance-slots-offset)
2171 (if #!-interleaved-raw-slots (>= index ntagged)
2172 #!+interleaved-raw-slots (logbitp index metadata)
2173 (descriptor-word-sized-integer (pop-stack))
2174 (pop-stack))))
2175 result))
2177 (define-cold-fop (fop-layout)
2178 (let* ((metadata-des (pop-stack))
2179 (length-des (pop-stack))
2180 (depthoid-des (pop-stack))
2181 (cold-inherits (pop-stack))
2182 (name (pop-stack))
2183 (old (gethash name *cold-layouts*)))
2184 (declare (type descriptor length-des depthoid-des cold-inherits))
2185 (declare (type symbol name))
2186 ;; If a layout of this name has been defined already
2187 (if old
2188 ;; Enforce consistency between the previous definition and the
2189 ;; current definition, then return the previous definition.
2190 (destructuring-bind
2191 ;; FIXME: This would be more maintainable if we used
2192 ;; DEFSTRUCT (:TYPE LIST) to define COLD-LAYOUT. -- WHN 19990825
2193 (old-layout-descriptor
2194 old-name
2195 old-length
2196 old-inherits-list
2197 old-depthoid
2198 old-metadata)
2200 (declare (type descriptor old-layout-descriptor))
2201 (declare (type index old-length))
2202 (declare (type list old-inherits-list))
2203 (declare (type fixnum old-depthoid))
2204 (declare (type unsigned-byte old-metadata))
2205 (aver (eq name old-name))
2206 (let ((length (descriptor-fixnum length-des))
2207 (inherits-list (listify-cold-inherits cold-inherits))
2208 (depthoid (descriptor-fixnum depthoid-des))
2209 (metadata (descriptor-fixnum metadata-des)))
2210 (unless (= length old-length)
2211 (error "cold loading a reference to class ~S when the compile~%~
2212 time length was ~S and current length is ~S"
2213 name
2214 length
2215 old-length))
2216 (unless (equal inherits-list old-inherits-list)
2217 (error "cold loading a reference to class ~S when the compile~%~
2218 time inherits were ~S~%~
2219 and current inherits are ~S"
2220 name
2221 inherits-list
2222 old-inherits-list))
2223 (unless (= depthoid old-depthoid)
2224 (error "cold loading a reference to class ~S when the compile~%~
2225 time inheritance depthoid was ~S and current inheritance~%~
2226 depthoid is ~S"
2227 name
2228 depthoid
2229 old-depthoid))
2230 (unless (= metadata old-metadata)
2231 (error "cold loading a reference to class ~S when the compile~%~
2232 time raw-slot-metadata was ~S and is currently ~S"
2233 name
2234 metadata
2235 old-metadata)))
2236 old-layout-descriptor)
2237 ;; Make a new definition from scratch.
2238 (make-cold-layout name length-des cold-inherits depthoid-des
2239 metadata-des))))
2241 ;;;; cold fops for loading symbols
2243 ;;; Load a symbol SIZE characters long from *FASL-INPUT-STREAM* and
2244 ;;; intern that symbol in PACKAGE.
2245 (defun cold-load-symbol (size package)
2246 (let ((string (make-string size)))
2247 (read-string-as-bytes *fasl-input-stream* string)
2248 (push-fop-table (intern string package))))
2250 ;; I don't feel like hacking up DEFINE-COLD-FOP any more than necessary,
2251 ;; so this code is handcrafted to accept two operands.
2252 (flet ((fop-cold-symbol-in-package-save (index pname-len)
2253 (cold-load-symbol pname-len (ref-fop-table index))))
2254 (dotimes (i 16) ; occupies 16 cells in the dispatch table
2255 (setf (svref *cold-fop-funs* (+ (get 'fop-symbol-in-package-save 'opcode) i))
2256 #'fop-cold-symbol-in-package-save)))
2258 (define-cold-fop (fop-lisp-symbol-save (namelen))
2259 (cold-load-symbol namelen *cl-package*))
2261 (define-cold-fop (fop-keyword-symbol-save (namelen))
2262 (cold-load-symbol namelen *keyword-package*))
2264 (define-cold-fop (fop-uninterned-symbol-save (namelen))
2265 (let ((name (make-string namelen)))
2266 (read-string-as-bytes *fasl-input-stream* name)
2267 (push-fop-table (get-uninterned-symbol name))))
2269 ;;;; cold fops for loading packages
2271 (define-cold-fop (fop-named-package-save (namelen))
2272 (let ((name (make-string namelen)))
2273 (read-string-as-bytes *fasl-input-stream* name)
2274 (push-fop-table (find-package name))))
2276 ;;;; cold fops for loading lists
2278 ;;; Make a list of the top LENGTH things on the fop stack. The last
2279 ;;; cdr of the list is set to LAST.
2280 (defmacro cold-stack-list (length last)
2281 `(do* ((index ,length (1- index))
2282 (result ,last (cold-cons (pop-stack) result)))
2283 ((= index 0) result)
2284 (declare (fixnum index))))
2286 (define-cold-fop (fop-list)
2287 (cold-stack-list (read-byte-arg) *nil-descriptor*))
2288 (define-cold-fop (fop-list*)
2289 (cold-stack-list (read-byte-arg) (pop-stack)))
2290 (define-cold-fop (fop-list-1)
2291 (cold-stack-list 1 *nil-descriptor*))
2292 (define-cold-fop (fop-list-2)
2293 (cold-stack-list 2 *nil-descriptor*))
2294 (define-cold-fop (fop-list-3)
2295 (cold-stack-list 3 *nil-descriptor*))
2296 (define-cold-fop (fop-list-4)
2297 (cold-stack-list 4 *nil-descriptor*))
2298 (define-cold-fop (fop-list-5)
2299 (cold-stack-list 5 *nil-descriptor*))
2300 (define-cold-fop (fop-list-6)
2301 (cold-stack-list 6 *nil-descriptor*))
2302 (define-cold-fop (fop-list-7)
2303 (cold-stack-list 7 *nil-descriptor*))
2304 (define-cold-fop (fop-list-8)
2305 (cold-stack-list 8 *nil-descriptor*))
2306 (define-cold-fop (fop-list*-1)
2307 (cold-stack-list 1 (pop-stack)))
2308 (define-cold-fop (fop-list*-2)
2309 (cold-stack-list 2 (pop-stack)))
2310 (define-cold-fop (fop-list*-3)
2311 (cold-stack-list 3 (pop-stack)))
2312 (define-cold-fop (fop-list*-4)
2313 (cold-stack-list 4 (pop-stack)))
2314 (define-cold-fop (fop-list*-5)
2315 (cold-stack-list 5 (pop-stack)))
2316 (define-cold-fop (fop-list*-6)
2317 (cold-stack-list 6 (pop-stack)))
2318 (define-cold-fop (fop-list*-7)
2319 (cold-stack-list 7 (pop-stack)))
2320 (define-cold-fop (fop-list*-8)
2321 (cold-stack-list 8 (pop-stack)))
2323 ;;;; cold fops for loading vectors
2325 (define-cold-fop (fop-base-string (len))
2326 (let ((string (make-string len)))
2327 (read-string-as-bytes *fasl-input-stream* string)
2328 (base-string-to-core string)))
2330 #!+sb-unicode
2331 (define-cold-fop (fop-character-string (len))
2332 (bug "CHARACTER-STRING[~D] dumped by cross-compiler." len))
2334 (define-cold-fop (fop-vector (size))
2335 (let* ((result (allocate-vector-object *dynamic*
2336 sb!vm:n-word-bits
2337 size
2338 sb!vm:simple-vector-widetag)))
2339 (do ((index (1- size) (1- index)))
2340 ((minusp index))
2341 (declare (fixnum index))
2342 (write-wordindexed result
2343 (+ index sb!vm:vector-data-offset)
2344 (pop-stack)))
2345 result))
2347 (define-cold-fop (fop-spec-vector)
2348 (let* ((len (read-word-arg))
2349 (type (read-byte-arg))
2350 (sizebits (aref **saetp-bits-per-length** type))
2351 (result (progn (aver (< sizebits 255))
2352 (allocate-vector-object *dynamic* sizebits len type)))
2353 (start (+ (descriptor-byte-offset result)
2354 (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2355 (end (+ start
2356 (ceiling (* len sizebits)
2357 sb!vm:n-byte-bits))))
2358 (read-bigvec-as-sequence-or-die (descriptor-bytes result)
2359 *fasl-input-stream*
2360 :start start
2361 :end end)
2362 result))
2364 (define-cold-fop (fop-array)
2365 (let* ((rank (read-word-arg))
2366 (data-vector (pop-stack))
2367 (result (allocate-object *dynamic*
2368 (+ sb!vm:array-dimensions-offset rank)
2369 sb!vm:other-pointer-lowtag)))
2370 (write-header-word result rank sb!vm:simple-array-widetag)
2371 (write-wordindexed result sb!vm:array-fill-pointer-slot *nil-descriptor*)
2372 (write-wordindexed result sb!vm:array-data-slot data-vector)
2373 (write-wordindexed result sb!vm:array-displacement-slot *nil-descriptor*)
2374 (write-wordindexed result sb!vm:array-displaced-p-slot *nil-descriptor*)
2375 (write-wordindexed result sb!vm:array-displaced-from-slot *nil-descriptor*)
2376 (let ((total-elements 1))
2377 (dotimes (axis rank)
2378 (let ((dim (pop-stack)))
2379 (unless (is-fixnum-lowtag (descriptor-lowtag dim))
2380 (error "non-fixnum dimension? (~S)" dim))
2381 (setf total-elements
2382 (* total-elements
2383 (logior (ash (descriptor-high dim)
2384 (- descriptor-low-bits
2385 sb!vm:n-fixnum-tag-bits))
2386 (ash (descriptor-low dim)
2387 sb!vm:n-fixnum-tag-bits))))
2388 (write-wordindexed result
2389 (+ sb!vm:array-dimensions-offset axis)
2390 dim)))
2391 (write-wordindexed result
2392 sb!vm:array-elements-slot
2393 (make-fixnum-descriptor total-elements)))
2394 result))
2397 ;;;; cold fops for loading numbers
2399 (defmacro define-cold-number-fop (fop &optional arglist)
2400 ;; Invoke the ordinary warm version of this fop to cons the number.
2401 `(define-cold-fop (,fop ,arglist) (number-to-core (,fop ,@arglist))))
2403 (define-cold-number-fop fop-single-float)
2404 (define-cold-number-fop fop-double-float)
2405 (define-cold-number-fop fop-word-integer)
2406 (define-cold-number-fop fop-byte-integer)
2407 (define-cold-number-fop fop-complex-single-float)
2408 (define-cold-number-fop fop-complex-double-float)
2409 (define-cold-number-fop fop-integer (n-bytes))
2411 (define-cold-fop (fop-ratio)
2412 (let ((den (pop-stack)))
2413 (number-pair-to-core (pop-stack) den sb!vm:ratio-widetag)))
2415 (define-cold-fop (fop-complex)
2416 (let ((im (pop-stack)))
2417 (number-pair-to-core (pop-stack) im sb!vm:complex-widetag)))
2419 ;;;; cold fops for calling (or not calling)
2421 (not-cold-fop fop-eval)
2422 (not-cold-fop fop-eval-for-effect)
2424 (defvar *load-time-value-counter*)
2426 (define-cold-fop (fop-funcall)
2427 (unless (= (read-byte-arg) 0)
2428 (error "You can't FOP-FUNCALL arbitrary stuff in cold load."))
2429 (let ((counter *load-time-value-counter*))
2430 (cold-push (cold-cons
2431 (cold-intern :load-time-value)
2432 (cold-cons
2433 (pop-stack)
2434 (cold-cons
2435 (number-to-core counter)
2436 *nil-descriptor*)))
2437 *current-reversed-cold-toplevels*)
2438 (setf *load-time-value-counter* (1+ counter))
2439 (make-descriptor 0 0 :load-time-value counter)))
2441 (defun finalize-load-time-value-noise ()
2442 (cold-set '*!load-time-values*
2443 (allocate-vector-object *dynamic*
2444 sb!vm:n-word-bits
2445 *load-time-value-counter*
2446 sb!vm:simple-vector-widetag)))
2448 (define-cold-fop (fop-funcall-for-effect)
2449 (if (= (read-byte-arg) 0)
2450 (cold-push (pop-stack)
2451 *current-reversed-cold-toplevels*)
2452 (error "You can't FOP-FUNCALL arbitrary stuff in cold load.")))
2454 ;;;; cold fops for fixing up circularities
2456 (define-cold-fop (fop-rplaca)
2457 (let ((obj (ref-fop-table (read-word-arg)))
2458 (idx (read-word-arg)))
2459 (write-memory (cold-nthcdr idx obj) (pop-stack))))
2461 (define-cold-fop (fop-rplacd)
2462 (let ((obj (ref-fop-table (read-word-arg)))
2463 (idx (read-word-arg)))
2464 (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2466 (define-cold-fop (fop-svset)
2467 (let ((obj (ref-fop-table (read-word-arg)))
2468 (idx (read-word-arg)))
2469 (write-wordindexed obj
2470 (+ idx
2471 (ecase (descriptor-lowtag obj)
2472 (#.sb!vm:instance-pointer-lowtag 1)
2473 (#.sb!vm:other-pointer-lowtag 2)))
2474 (pop-stack))))
2476 (define-cold-fop (fop-structset)
2477 (let ((obj (ref-fop-table (read-word-arg)))
2478 (idx (read-word-arg)))
2479 (write-wordindexed obj (1+ idx) (pop-stack))))
2481 (define-cold-fop (fop-nthcdr)
2482 (cold-nthcdr (read-word-arg) (pop-stack)))
2484 (defun cold-nthcdr (index obj)
2485 (dotimes (i index)
2486 (setq obj (read-wordindexed obj 1)))
2487 obj)
2489 ;;;; cold fops for loading code objects and functions
2491 ;;; the names of things which have had COLD-FSET used on them already
2492 ;;; (used to make sure that we don't try to statically link a name to
2493 ;;; more than one definition)
2494 (defparameter *cold-fset-warm-names*
2495 ;; This can't be an EQL hash table because names can be conses, e.g.
2496 ;; (SETF CAR).
2497 (make-hash-table :test 'equal))
2499 (define-cold-fop (fop-fset)
2500 (let* ((fn (pop-stack))
2501 (cold-name (pop-stack))
2502 (warm-name (warm-fun-name cold-name)))
2503 (if (gethash warm-name *cold-fset-warm-names*)
2504 (error "duplicate COLD-FSET for ~S" warm-name)
2505 (setf (gethash warm-name *cold-fset-warm-names*) t))
2506 (static-fset cold-name fn)))
2508 (define-cold-fop (fop-note-debug-source)
2509 (let ((debug-source (pop-stack)))
2510 (cold-push debug-source *current-debug-sources*)))
2512 (define-cold-fop (fop-fdefn)
2513 (cold-fdefinition-object (pop-stack)))
2515 #!-(or x86 x86-64)
2516 (define-cold-fop (fop-sanctify-for-execution)
2517 (pop-stack))
2519 ;;; Setting this variable shows what code looks like before any
2520 ;;; fixups (or function headers) are applied.
2521 #!+sb-show (defvar *show-pre-fixup-code-p* nil)
2523 (defun cold-load-code (nconst code-size)
2524 (macrolet ((pop-stack () '(pop-fop-stack)))
2525 (let* ((raw-header-n-words (+ sb!vm:code-constants-offset nconst))
2526 (header-n-words
2527 ;; Note: we round the number of constants up to ensure
2528 ;; that the code vector will be properly aligned.
2529 (round-up raw-header-n-words 2))
2530 (des (allocate-cold-descriptor *dynamic*
2531 (+ (ash header-n-words
2532 sb!vm:word-shift)
2533 code-size)
2534 sb!vm:other-pointer-lowtag)))
2535 (write-header-word des header-n-words sb!vm:code-header-widetag)
2536 (write-wordindexed des
2537 sb!vm:code-code-size-slot
2538 (make-fixnum-descriptor code-size))
2539 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2540 (write-wordindexed des sb!vm:code-debug-info-slot (pop-stack))
2541 (when (oddp raw-header-n-words)
2542 (write-wordindexed des
2543 raw-header-n-words
2544 (make-random-descriptor 0)))
2545 (do ((index (1- raw-header-n-words) (1- index)))
2546 ((< index sb!vm:code-constants-offset))
2547 (write-wordindexed des index (pop-stack)))
2548 (let* ((start (+ (descriptor-byte-offset des)
2549 (ash header-n-words sb!vm:word-shift)))
2550 (end (+ start code-size)))
2551 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2552 *fasl-input-stream*
2553 :start start
2554 :end end)
2555 #!+sb-show
2556 (when *show-pre-fixup-code-p*
2557 (format *trace-output*
2558 "~&/raw code from code-fop ~W ~W:~%"
2559 nconst
2560 code-size)
2561 (do ((i start (+ i sb!vm:n-word-bytes)))
2562 ((>= i end))
2563 (format *trace-output*
2564 "/#X~8,'0x: #X~8,'0x~%"
2565 (+ i (gspace-byte-address (descriptor-gspace des)))
2566 (bvref-32 (descriptor-bytes des) i)))))
2567 des)))
2569 (dotimes (i 16) ; occupies 16 cells in the dispatch table
2570 (setf (svref *cold-fop-funs* (+ (get 'fop-code 'opcode) i))
2571 #'cold-load-code))
2573 (define-cold-fop (fop-alter-code (slot))
2574 (let ((value (pop-stack))
2575 (code (pop-stack)))
2576 (write-wordindexed code slot value)))
2578 (define-cold-fop (fop-fun-entry)
2579 (let* ((info (pop-stack))
2580 (type (pop-stack))
2581 (arglist (pop-stack))
2582 (name (pop-stack))
2583 (code-object (pop-stack))
2584 (offset (calc-offset code-object (read-word-arg)))
2585 (fn (descriptor-beyond code-object
2586 offset
2587 sb!vm:fun-pointer-lowtag))
2588 (next (read-wordindexed code-object sb!vm:code-entry-points-slot)))
2589 (unless (zerop (logand offset sb!vm:lowtag-mask))
2590 (error "unaligned function entry: ~S at #X~X" name offset))
2591 (write-wordindexed code-object sb!vm:code-entry-points-slot fn)
2592 (write-memory fn
2593 (make-other-immediate-descriptor
2594 (ash offset (- sb!vm:word-shift))
2595 sb!vm:simple-fun-header-widetag))
2596 (write-wordindexed fn
2597 sb!vm:simple-fun-self-slot
2598 ;; KLUDGE: Wiring decisions like this in at
2599 ;; this level ("if it's an x86") instead of a
2600 ;; higher level of abstraction ("if it has such
2601 ;; and such relocation peculiarities (which
2602 ;; happen to be confined to the x86)") is bad.
2603 ;; It would be nice if the code were instead
2604 ;; conditional on some more descriptive
2605 ;; feature, :STICKY-CODE or
2606 ;; :LOAD-GC-INTERACTION or something.
2608 ;; FIXME: The X86 definition of the function
2609 ;; self slot breaks everything object.tex says
2610 ;; about it. (As far as I can tell, the X86
2611 ;; definition makes it a pointer to the actual
2612 ;; code instead of a pointer back to the object
2613 ;; itself.) Ask on the mailing list whether
2614 ;; this is documented somewhere, and if not,
2615 ;; try to reverse engineer some documentation.
2616 #!-(or x86 x86-64)
2617 ;; a pointer back to the function object, as
2618 ;; described in CMU CL
2619 ;; src/docs/internals/object.tex
2621 #!+(or x86 x86-64)
2622 ;; KLUDGE: a pointer to the actual code of the
2623 ;; object, as described nowhere that I can find
2624 ;; -- WHN 19990907
2625 (make-random-descriptor
2626 (+ (descriptor-bits fn)
2627 (- (ash sb!vm:simple-fun-code-offset
2628 sb!vm:word-shift)
2629 ;; FIXME: We should mask out the type
2630 ;; bits, not assume we know what they
2631 ;; are and subtract them out this way.
2632 sb!vm:fun-pointer-lowtag))))
2633 (write-wordindexed fn sb!vm:simple-fun-next-slot next)
2634 (write-wordindexed fn sb!vm:simple-fun-name-slot name)
2635 (write-wordindexed fn sb!vm:simple-fun-arglist-slot arglist)
2636 (write-wordindexed fn sb!vm:simple-fun-type-slot type)
2637 (write-wordindexed fn sb!vm::simple-fun-info-slot info)
2638 fn))
2640 #!+sb-thread
2641 (define-cold-fop (fop-symbol-tls-fixup)
2642 (let* ((symbol (pop-stack))
2643 (kind (pop-stack))
2644 (code-object (pop-stack)))
2645 (do-cold-fixup code-object (read-word-arg) (ensure-symbol-tls-index symbol)
2646 kind)
2647 code-object))
2649 (define-cold-fop (fop-foreign-fixup)
2650 (let* ((kind (pop-stack))
2651 (code-object (pop-stack))
2652 (len (read-byte-arg))
2653 (sym (make-string len)))
2654 (read-string-as-bytes *fasl-input-stream* sym)
2655 #!+sb-dynamic-core
2656 (let ((offset (read-word-arg))
2657 (value (dyncore-note-symbol sym nil)))
2658 (do-cold-fixup code-object offset value kind))
2659 #!- (and) (format t "Bad non-plt fixup: ~S~S~%" sym code-object)
2660 #!-sb-dynamic-core
2661 (let ((offset (read-word-arg))
2662 (value (cold-foreign-symbol-address sym)))
2663 (do-cold-fixup code-object offset value kind))
2664 code-object))
2666 #!+linkage-table
2667 (define-cold-fop (fop-foreign-dataref-fixup)
2668 (let* ((kind (pop-stack))
2669 (code-object (pop-stack))
2670 (len (read-byte-arg))
2671 (sym (make-string len)))
2672 #!-sb-dynamic-core (declare (ignore code-object))
2673 (read-string-as-bytes *fasl-input-stream* sym)
2674 #!+sb-dynamic-core
2675 (let ((offset (read-word-arg))
2676 (value (dyncore-note-symbol sym t)))
2677 (do-cold-fixup code-object offset value kind)
2678 code-object)
2679 #!-sb-dynamic-core
2680 (progn
2681 (maphash (lambda (k v)
2682 (format *error-output* "~&~S = #X~8X~%" k v))
2683 *cold-foreign-symbol-table*)
2684 (error "shared foreign symbol in cold load: ~S (~S)" sym kind))))
2686 (define-cold-fop (fop-assembler-code)
2687 (let* ((length (read-word-arg))
2688 (header-n-words
2689 ;; Note: we round the number of constants up to ensure that
2690 ;; the code vector will be properly aligned.
2691 (round-up sb!vm:code-constants-offset 2))
2692 (des (allocate-cold-descriptor *read-only*
2693 (+ (ash header-n-words
2694 sb!vm:word-shift)
2695 length)
2696 sb!vm:other-pointer-lowtag)))
2697 (write-header-word des header-n-words sb!vm:code-header-widetag)
2698 (write-wordindexed des
2699 sb!vm:code-code-size-slot
2700 (make-fixnum-descriptor length))
2701 (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2702 (write-wordindexed des sb!vm:code-debug-info-slot *nil-descriptor*)
2704 (let* ((start (+ (descriptor-byte-offset des)
2705 (ash header-n-words sb!vm:word-shift)))
2706 (end (+ start length)))
2707 (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2708 *fasl-input-stream*
2709 :start start
2710 :end end))
2711 des))
2713 (define-cold-fop (fop-assembler-routine)
2714 (let* ((routine (pop-stack))
2715 (des (pop-stack))
2716 (offset (calc-offset des (read-word-arg))))
2717 (record-cold-assembler-routine
2718 routine
2719 (+ (logandc2 (descriptor-bits des) sb!vm:lowtag-mask) offset))
2720 des))
2722 (define-cold-fop (fop-assembler-fixup)
2723 (let* ((routine (pop-stack))
2724 (kind (pop-stack))
2725 (code-object (pop-stack))
2726 (offset (read-word-arg)))
2727 (record-cold-assembler-fixup routine code-object offset kind)
2728 code-object))
2730 (define-cold-fop (fop-code-object-fixup)
2731 (let* ((kind (pop-stack))
2732 (code-object (pop-stack))
2733 (offset (read-word-arg))
2734 (value (descriptor-bits code-object)))
2735 (do-cold-fixup code-object offset value kind)
2736 code-object))
2738 ;;;; sanity checking space layouts
2740 (defun check-spaces ()
2741 ;;; Co-opt type machinery to check for intersections...
2742 (let (types)
2743 (flet ((check (start end space)
2744 (unless (< start end)
2745 (error "Bogus space: ~A" space))
2746 (let ((type (specifier-type `(integer ,start ,end))))
2747 (dolist (other types)
2748 (unless (eq *empty-type* (type-intersection (cdr other) type))
2749 (error "Space overlap: ~A with ~A" space (car other))))
2750 (push (cons space type) types))))
2751 (check sb!vm:read-only-space-start sb!vm:read-only-space-end :read-only)
2752 (check sb!vm:static-space-start sb!vm:static-space-end :static)
2753 #!+gencgc
2754 (check sb!vm:dynamic-space-start sb!vm:dynamic-space-end :dynamic)
2755 #!-gencgc
2756 (progn
2757 (check sb!vm:dynamic-0-space-start sb!vm:dynamic-0-space-end :dynamic-0)
2758 (check sb!vm:dynamic-1-space-start sb!vm:dynamic-1-space-end :dynamic-1))
2759 #!+linkage-table
2760 (check sb!vm:linkage-table-space-start sb!vm:linkage-table-space-end :linkage-table))))
2762 ;;;; emitting C header file
2764 (defun tailwise-equal (string tail)
2765 (and (>= (length string) (length tail))
2766 (string= string tail :start1 (- (length string) (length tail)))))
2768 (defun write-boilerplate ()
2769 (format t "/*~%")
2770 (dolist (line
2771 '("This is a machine-generated file. Please do not edit it by hand."
2772 "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
2774 "This file contains low-level information about the"
2775 "internals of a particular version and configuration"
2776 "of SBCL. It is used by the C compiler to create a runtime"
2777 "support environment, an executable program in the host"
2778 "operating system's native format, which can then be used to"
2779 "load and run 'core' files, which are basically programs"
2780 "in SBCL's own format."))
2781 (format t " *~@[ ~A~]~%" line))
2782 (format t " */~%"))
2784 (defun c-name (string &optional strip)
2785 (delete #\+
2786 (substitute-if #\_ (lambda (c) (member c '(#\- #\/ #\%)))
2787 (remove-if (lambda (c) (position c strip))
2788 string))))
2790 (defun c-symbol-name (symbol &optional strip)
2791 (c-name (symbol-name symbol) strip))
2793 (defun write-makefile-features ()
2794 ;; propagating *SHEBANG-FEATURES* into the Makefiles
2795 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2796 sb-cold:*shebang-features*)
2797 #'string<))
2798 (format t "LISP_FEATURE_~A=1~%" shebang-feature-name)))
2800 (defun write-config-h ()
2801 ;; propagating *SHEBANG-FEATURES* into C-level #define's
2802 (dolist (shebang-feature-name (sort (mapcar #'c-symbol-name
2803 sb-cold:*shebang-features*)
2804 #'string<))
2805 (format t "#define LISP_FEATURE_~A~%" shebang-feature-name))
2806 (terpri)
2807 ;; and miscellaneous constants
2808 (format t "#define SBCL_VERSION_STRING ~S~%"
2809 (sb!xc:lisp-implementation-version))
2810 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
2811 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2812 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
2813 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2814 (format t "#define LISPOBJ(thing) thing~2%")
2815 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")
2816 (terpri))
2818 (defun write-constants-h ()
2819 ;; writing entire families of named constants
2820 (let ((constants nil))
2821 (dolist (package-name '( ;; Even in CMU CL, constants from VM
2822 ;; were automatically propagated
2823 ;; into the runtime.
2824 "SB!VM"
2825 ;; In SBCL, we also propagate various
2826 ;; magic numbers related to file format,
2827 ;; which live here instead of SB!VM.
2828 "SB!FASL"))
2829 (do-external-symbols (symbol (find-package package-name))
2830 (when (constantp symbol)
2831 (let ((name (symbol-name symbol)))
2832 (labels ( ;; shared machinery
2833 (record (string priority suffix)
2834 (push (list string
2835 priority
2836 (symbol-value symbol)
2837 suffix
2838 (documentation symbol 'variable))
2839 constants))
2840 ;; machinery for old-style CMU CL Lisp-to-C
2841 ;; arbitrary renaming, being phased out in favor of
2842 ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
2843 ;; renaming
2844 (record-with-munged-name (prefix string priority)
2845 (record (concatenate
2846 'simple-string
2847 prefix
2848 (delete #\- (string-capitalize string)))
2849 priority
2850 ""))
2851 (maybe-record-with-munged-name (tail prefix priority)
2852 (when (tailwise-equal name tail)
2853 (record-with-munged-name prefix
2854 (subseq name 0
2855 (- (length name)
2856 (length tail)))
2857 priority)))
2858 ;; machinery for new-style SBCL Lisp-to-C naming
2859 (record-with-translated-name (priority large)
2860 (record (c-name name) priority
2861 (if large
2862 #!+(and win32 x86-64) "LLU"
2863 #!-(and win32 x86-64) "LU"
2864 "")))
2865 (maybe-record-with-translated-name (suffixes priority &key large)
2866 (when (some (lambda (suffix)
2867 (tailwise-equal name suffix))
2868 suffixes)
2869 (record-with-translated-name priority large))))
2870 (maybe-record-with-translated-name '("-LOWTAG") 0)
2871 (maybe-record-with-translated-name '("-WIDETAG" "-SHIFT") 1)
2872 (maybe-record-with-munged-name "-FLAG" "flag_" 2)
2873 (maybe-record-with-munged-name "-TRAP" "trap_" 3)
2874 (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
2875 (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
2876 (maybe-record-with-translated-name '("-SIZE" "-INTERRUPTS") 6)
2877 (maybe-record-with-translated-name '("-START" "-END" "-PAGE-BYTES"
2878 "-CARD-BYTES" "-GRANULARITY")
2879 7 :large t)
2880 (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 8)
2881 (maybe-record-with-translated-name '("-CORE-SPACE-ID") 9)
2882 (maybe-record-with-translated-name '("-CORE-SPACE-ID-FLAG") 9)
2883 (maybe-record-with-translated-name '("-GENERATION+") 10))))))
2884 ;; KLUDGE: these constants are sort of important, but there's no
2885 ;; pleasing way to inform the code above about them. So we fake
2886 ;; it for now. nikodemus on #lisp (2004-08-09) suggested simply
2887 ;; exporting every numeric constant from SB!VM; that would work,
2888 ;; but the C runtime would have to be altered to use Lisp-like names
2889 ;; rather than the munged names currently exported. --njf, 2004-08-09
2890 (dolist (c '(sb!vm:n-word-bits sb!vm:n-word-bytes
2891 sb!vm:n-lowtag-bits sb!vm:lowtag-mask
2892 sb!vm:n-widetag-bits sb!vm:widetag-mask
2893 sb!vm:n-fixnum-tag-bits sb!vm:fixnum-tag-mask))
2894 (push (list (c-symbol-name c)
2895 -1 ; invent a new priority
2896 (symbol-value c)
2898 nil)
2899 constants))
2900 ;; One more symbol that doesn't fit into the code above.
2901 (let ((c 'sb!impl::+magic-hash-vector-value+))
2902 (push (list (c-symbol-name c)
2904 (symbol-value c)
2905 #!+(and win32 x86-64) "LLU"
2906 #!-(and win32 x86-64) "LU"
2907 nil)
2908 constants))
2909 (setf constants
2910 (sort constants
2911 (lambda (const1 const2)
2912 (if (= (second const1) (second const2))
2913 (if (= (third const1) (third const2))
2914 (string< (first const1) (first const2))
2915 (< (third const1) (third const2)))
2916 (< (second const1) (second const2))))))
2917 (let ((prev-priority (second (car constants))))
2918 (dolist (const constants)
2919 (destructuring-bind (name priority value suffix doc) const
2920 (unless (= prev-priority priority)
2921 (terpri)
2922 (setf prev-priority priority))
2923 (when (minusp value)
2924 (error "stub: negative values unsupported"))
2925 (format t "#define ~A ~A~A /* 0x~X ~@[ -- ~A ~]*/~%" name value suffix value doc))))
2926 (terpri))
2928 ;; writing information about internal errors
2929 ;; Assembly code needs only the constants for UNDEFINED_[ALIEN_]FUN_ERROR
2930 ;; but to avoid imparting that knowledge here, we'll expose all error
2931 ;; number constants except for OBJECT-NOT-<x>-ERROR ones.
2932 (loop for interr across sb!c:*backend-internal-errors*
2933 for i from 0
2934 when (stringp (car interr))
2935 do (format t "#define ~A ~D~%" (c-symbol-name (cdr interr)) i))
2936 ;; C code needs strings for describe_internal_error()
2937 (format t "#define INTERNAL_ERROR_NAMES ~{\\~%~S~^, ~}~2%"
2938 (map 'list 'sb!kernel::!c-stringify-internal-error
2939 sb!c:*backend-internal-errors*))
2941 ;; I'm not really sure why this is in SB!C, since it seems
2942 ;; conceptually like something that belongs to SB!VM. In any case,
2943 ;; it's needed C-side.
2944 (format t "#define BACKEND_PAGE_BYTES ~DLU~%" sb!c:*backend-page-bytes*)
2946 (terpri)
2948 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
2949 ;; platforms. If we export this from the SB!VM package, it gets
2950 ;; written out as #define trap_PseudoAtomic, which is confusing as
2951 ;; the runtime treats trap_ as the prefix for illegal instruction
2952 ;; type things. We therefore don't export it, but instead do
2953 #!+sparc
2954 (when (boundp 'sb!vm::pseudo-atomic-trap)
2955 (format t
2956 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
2957 sb!vm::pseudo-atomic-trap)
2958 (terpri))
2959 ;; possibly this is another candidate for a rename (to
2960 ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
2961 ;; [possibly applicable to other platforms])
2963 #!+sb-safepoint
2964 (format t "#define GC_SAFEPOINT_PAGE_ADDR ((void*)0x~XUL) /* ~:*~A */~%"
2965 sb!vm:gc-safepoint-page-addr)
2967 (dolist (symbol '(sb!vm::float-traps-byte
2968 sb!vm::float-exceptions-byte
2969 sb!vm::float-sticky-bits
2970 sb!vm::float-rounding-mode))
2971 (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
2972 (c-symbol-name symbol)
2973 (sb!xc:byte-position (symbol-value symbol)))
2974 (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
2975 (c-symbol-name symbol)
2976 (sb!xc:mask-field (symbol-value symbol) -1))))
2978 #!+sb-ldb
2979 (defun write-tagnames-h (&optional (out *standard-output*))
2980 (labels
2981 ((pretty-name (symbol strip)
2982 (let ((name (string-downcase symbol)))
2983 (substitute #\Space #\-
2984 (subseq name 0 (- (length name) (length strip))))))
2985 (list-sorted-tags (tail)
2986 (loop for symbol being the external-symbols of "SB!VM"
2987 when (and (constantp symbol)
2988 (tailwise-equal (string symbol) tail))
2989 collect symbol into tags
2990 finally (return (sort tags #'< :key #'symbol-value))))
2991 (write-tags (kind limit ash-count)
2992 (format out "~%static const char *~(~A~)_names[] = {~%"
2993 (subseq kind 1))
2994 (let ((tags (list-sorted-tags kind)))
2995 (dotimes (i limit)
2996 (if (eql i (ash (or (symbol-value (first tags)) -1) ash-count))
2997 (format out " \"~A\"" (pretty-name (pop tags) kind))
2998 (format out " \"unknown [~D]\"" i))
2999 (unless (eql i (1- limit))
3000 (write-string "," out))
3001 (terpri out)))
3002 (write-line "};" out)))
3003 (write-tags "-LOWTAG" sb!vm:lowtag-limit 0)
3004 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
3005 ;; ending with the same 2 bits. (#b10)
3006 (write-tags "-WIDETAG" (ash (1+ sb!vm:widetag-mask) -2) -2))
3007 ;; Inform print_otherptr() of all array types that it's too dumb to print
3008 (let ((array-type-bits (make-array 32 :initial-element 0)))
3009 (flet ((toggle (b)
3010 (multiple-value-bind (ofs bit) (floor b 8)
3011 (setf (aref array-type-bits ofs) (ash 1 bit)))))
3012 (dovector (saetp sb!vm:*specialized-array-element-type-properties*)
3013 (unless (or (typep (sb!vm:saetp-ctype saetp) 'character-set-type)
3014 (eq (sb!vm:saetp-specifier saetp) t))
3015 (toggle (sb!vm:saetp-typecode saetp))
3016 (awhen (sb!vm:saetp-complex-typecode saetp) (toggle it)))))
3017 (format out
3018 "~%static unsigned char unprintable_array_types[32] = ~% {~{~d~^,~}};~%"
3019 (coerce array-type-bits 'list)))
3020 (values))
3022 (defun write-primitive-object (obj)
3023 ;; writing primitive object layouts
3024 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3025 (format t
3026 "struct ~A {~%"
3027 (c-name (string-downcase (string (sb!vm:primitive-object-name obj)))))
3028 (when (sb!vm:primitive-object-widetag obj)
3029 (format t " lispobj header;~%"))
3030 (dolist (slot (sb!vm:primitive-object-slots obj))
3031 (format t " ~A ~A~@[[1]~];~%"
3032 (getf (sb!vm:slot-options slot) :c-type "lispobj")
3033 (c-name (string-downcase (string (sb!vm:slot-name slot))))
3034 (sb!vm:slot-rest-p slot)))
3035 (format t "};~2%")
3036 (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
3037 (format t "/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
3038 (format t " * so they work directly on tagged addresses. */~2%")
3039 (let ((name (sb!vm:primitive-object-name obj))
3040 (lowtag (or (symbol-value (sb!vm:primitive-object-lowtag obj))
3041 0)))
3042 (dolist (slot (sb!vm:primitive-object-slots obj))
3043 (format t "#define ~A_~A_OFFSET ~D~%"
3044 (c-symbol-name name)
3045 (c-symbol-name (sb!vm:slot-name slot))
3046 (- (* (sb!vm:slot-offset slot) sb!vm:n-word-bytes) lowtag)))
3047 (terpri))
3048 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%"))
3050 (defun write-structure-object (dd)
3051 (flet ((cstring (designator)
3052 (c-name (string-downcase (string designator)))))
3053 (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
3054 (format t "struct ~A {~%" (cstring (dd-name dd)))
3055 (format t " lispobj header;~%")
3056 ;; "self layout" slots are named '_layout' instead of 'layout' so that
3057 ;; classoid's expressly declared layout isn't renamed as a special-case.
3058 (format t " lispobj _layout;~%")
3059 #!-interleaved-raw-slots
3060 (progn
3061 ;; Note: if the structure has no raw slots, but has an even number of
3062 ;; ordinary slots (incl. layout, sans header), then the last slot gets
3063 ;; named 'raw_slot_paddingN' (not 'paddingN')
3064 ;; The choice of name is mildly disturbing, but harmless.
3065 (dolist (slot (dd-slots dd))
3066 (when (eq t (dsd-raw-type slot))
3067 (format t " lispobj ~A;~%" (cstring (dsd-name slot)))))
3068 (unless (oddp (+ (dd-length dd) (dd-raw-length dd)))
3069 (format t " lispobj raw_slot_padding;~%"))
3070 (dotimes (n (dd-raw-length dd))
3071 (format t " lispobj raw~D;~%" (- (dd-raw-length dd) n 1))))
3072 #!+interleaved-raw-slots
3073 (let ((index 1))
3074 (dolist (slot (dd-slots dd))
3075 (cond ((eq t (dsd-raw-type slot))
3076 (loop while (< index (dsd-index slot))
3078 (format t " lispobj raw_slot_padding~A;~%" index)
3079 (incf index))
3080 (format t " lispobj ~A;~%" (cstring (dsd-name slot)))
3081 (incf index))))
3082 (unless (oddp (dd-length dd))
3083 (format t " lispobj end_padding;~%")))
3084 (format t "};~2%")
3085 (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")))
3087 (defun write-static-symbols ()
3088 (dolist (symbol (cons nil sb!vm:*static-symbols*))
3089 ;; FIXME: It would be nice to use longer names than NIL and
3090 ;; (particularly) T in #define statements.
3091 (format t "#define ~A LISPOBJ(0x~X)~%"
3092 ;; FIXME: It would be nice not to need to strip anything
3093 ;; that doesn't get stripped always by C-SYMBOL-NAME.
3094 (c-symbol-name symbol "%*.!")
3095 (if *static* ; if we ran GENESIS
3096 ;; We actually ran GENESIS, use the real value.
3097 (descriptor-bits (cold-intern symbol))
3098 ;; We didn't run GENESIS, so guess at the address.
3099 (+ sb!vm:static-space-start
3100 sb!vm:n-word-bytes
3101 sb!vm:other-pointer-lowtag
3102 (if symbol (sb!vm:static-symbol-offset symbol) 0))))))
3105 ;;;; writing map file
3107 ;;; Write a map file describing the cold load. Some of this
3108 ;;; information is subject to change due to relocating GC, but even so
3109 ;;; it can be very handy when attempting to troubleshoot the early
3110 ;;; stages of cold load.
3111 (defun write-map ()
3112 (let ((*print-pretty* nil)
3113 (*print-case* :upcase))
3114 (format t "assembler routines defined in core image:~2%")
3115 (dolist (routine (sort (copy-list *cold-assembler-routines*) #'<
3116 :key #'cdr))
3117 (format t "#X~8,'0X: ~S~%" (cdr routine) (car routine)))
3118 (let ((funs nil)
3119 (undefs nil))
3120 (maphash (lambda (name fdefn)
3121 (let ((fun (read-wordindexed fdefn
3122 sb!vm:fdefn-fun-slot)))
3123 (if (= (descriptor-bits fun)
3124 (descriptor-bits *nil-descriptor*))
3125 (push name undefs)
3126 (let ((addr (read-wordindexed
3127 fdefn sb!vm:fdefn-raw-addr-slot)))
3128 (push (cons name (descriptor-bits addr))
3129 funs)))))
3130 *cold-fdefn-objects*)
3131 (format t "~%~|~%initially defined functions:~2%")
3132 (setf funs (sort funs #'< :key #'cdr))
3133 (dolist (info funs)
3134 (format t "0x~8,'0X: ~S #X~8,'0X~%" (cdr info) (car info)
3135 (- (cdr info) #x17)))
3136 (format t
3137 "~%~|
3138 (a note about initially undefined function references: These functions
3139 are referred to by code which is installed by GENESIS, but they are not
3140 installed by GENESIS. This is not necessarily a problem; functions can
3141 be defined later, by cold init toplevel forms, or in files compiled and
3142 loaded at warm init, or elsewhere. As long as they are defined before
3143 they are called, everything should be OK. Things are also OK if the
3144 cross-compiler knew their inline definition and used that everywhere
3145 that they were called before the out-of-line definition is installed,
3146 as is fairly common for structure accessors.)
3147 initially undefined function references:~2%")
3149 (setf undefs (sort undefs #'string< :key #'fun-name-block-name))
3150 (dolist (name undefs)
3151 (format t "~8,'0X: ~S~%"
3152 (descriptor-bits (gethash name *cold-fdefn-objects*))
3153 name)))
3155 (format t "~%~|~%layout names:~2%")
3156 (collect ((stuff))
3157 (maphash (lambda (name gorp)
3158 (declare (ignore name))
3159 (stuff (cons (descriptor-bits (car gorp))
3160 (cdr gorp))))
3161 *cold-layouts*)
3162 (dolist (x (sort (stuff) #'< :key #'car))
3163 (apply #'format t "~8,'0X: ~S[~D]~%~10T~S~%" x))))
3165 (values))
3167 ;;;; writing core file
3169 (defvar *core-file*)
3170 (defvar *data-page*)
3172 ;;; magic numbers to identify entries in a core file
3174 ;;; (In case you were wondering: No, AFAIK there's no special magic about
3175 ;;; these which requires them to be in the 38xx range. They're just
3176 ;;; arbitrary words, tested not for being in a particular range but just
3177 ;;; for equality. However, if you ever need to look at a .core file and
3178 ;;; figure out what's going on, it's slightly convenient that they're
3179 ;;; all in an easily recognizable range, and displacing the range away from
3180 ;;; zero seems likely to reduce the chance that random garbage will be
3181 ;;; misinterpreted as a .core file.)
3182 (defconstant build-id-core-entry-type-code 3860)
3183 (defconstant new-directory-core-entry-type-code 3861)
3184 (defconstant initial-fun-core-entry-type-code 3863)
3185 (defconstant page-table-core-entry-type-code 3880)
3186 (defconstant end-core-entry-type-code 3840)
3188 (declaim (ftype (function (sb!vm:word) sb!vm:word) write-word))
3189 (defun write-word (num)
3190 (ecase sb!c:*backend-byte-order*
3191 (:little-endian
3192 (dotimes (i sb!vm:n-word-bytes)
3193 (write-byte (ldb (byte 8 (* i 8)) num) *core-file*)))
3194 (:big-endian
3195 (dotimes (i sb!vm:n-word-bytes)
3196 (write-byte (ldb (byte 8 (* (- (1- sb!vm:n-word-bytes) i) 8)) num)
3197 *core-file*))))
3198 num)
3200 (defun advance-to-page ()
3201 (force-output *core-file*)
3202 (file-position *core-file*
3203 (round-up (file-position *core-file*)
3204 sb!c:*backend-page-bytes*)))
3206 (defun output-gspace (gspace)
3207 (force-output *core-file*)
3208 (let* ((posn (file-position *core-file*))
3209 (bytes (* (gspace-free-word-index gspace) sb!vm:n-word-bytes))
3210 (pages (ceiling bytes sb!c:*backend-page-bytes*))
3211 (total-bytes (* pages sb!c:*backend-page-bytes*)))
3213 (file-position *core-file*
3214 (* sb!c:*backend-page-bytes* (1+ *data-page*)))
3215 (format t
3216 "writing ~S byte~:P [~S page~:P] from ~S~%"
3217 total-bytes
3218 pages
3219 gspace)
3220 (force-output)
3222 ;; Note: It is assumed that the GSPACE allocation routines always
3223 ;; allocate whole pages (of size *target-page-size*) and that any
3224 ;; empty gspace between the free pointer and the end of page will
3225 ;; be zero-filled. This will always be true under Mach on machines
3226 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3227 ;; 8K).
3228 (write-bigvec-as-sequence (gspace-bytes gspace)
3229 *core-file*
3230 :end total-bytes
3231 :pad-with-zeros t)
3232 (force-output *core-file*)
3233 (file-position *core-file* posn)
3235 ;; Write part of a (new) directory entry which looks like this:
3236 ;; GSPACE IDENTIFIER
3237 ;; WORD COUNT
3238 ;; DATA PAGE
3239 ;; ADDRESS
3240 ;; PAGE COUNT
3241 (write-word (gspace-identifier gspace))
3242 (write-word (gspace-free-word-index gspace))
3243 (write-word *data-page*)
3244 (multiple-value-bind (floor rem)
3245 (floor (gspace-byte-address gspace) sb!c:*backend-page-bytes*)
3246 (aver (zerop rem))
3247 (write-word floor))
3248 (write-word pages)
3250 (incf *data-page* pages)))
3252 ;;; Create a core file created from the cold loaded image. (This is
3253 ;;; the "initial core file" because core files could be created later
3254 ;;; by executing SAVE-LISP in a running system, perhaps after we've
3255 ;;; added some functionality to the system.)
3256 (declaim (ftype (function (string)) write-initial-core-file))
3257 (defun write-initial-core-file (filename)
3259 (let ((filenamestring (namestring filename))
3260 (*data-page* 0))
3262 (format t
3263 "[building initial core file in ~S: ~%"
3264 filenamestring)
3265 (force-output)
3267 (with-open-file (*core-file* filenamestring
3268 :direction :output
3269 :element-type '(unsigned-byte 8)
3270 :if-exists :rename-and-delete)
3272 ;; Write the magic number.
3273 (write-word core-magic)
3275 ;; Write the build ID.
3276 (write-word build-id-core-entry-type-code)
3277 (let ((build-id (with-open-file (s "output/build-id.tmp")
3278 (read s))))
3279 (declare (type simple-string build-id))
3280 (/show build-id (length build-id))
3281 ;; Write length of build ID record: BUILD-ID-CORE-ENTRY-TYPE-CODE
3282 ;; word, this length word, and one word for each char of BUILD-ID.
3283 (write-word (+ 2 (length build-id)))
3284 (dovector (char build-id)
3285 ;; (We write each character as a word in order to avoid
3286 ;; having to think about word alignment issues in the
3287 ;; sbcl-0.7.8 version of coreparse.c.)
3288 (write-word (sb!xc:char-code char))))
3290 ;; Write the New Directory entry header.
3291 (write-word new-directory-core-entry-type-code)
3292 (write-word 17) ; length = (5 words/space) * 3 spaces + 2 for header.
3294 (output-gspace *read-only*)
3295 (output-gspace *static*)
3296 (output-gspace *dynamic*)
3298 ;; Write the initial function.
3299 (write-word initial-fun-core-entry-type-code)
3300 (write-word 3)
3301 (let* ((cold-name (cold-intern '!cold-init))
3302 (cold-fdefn (cold-fdefinition-object cold-name))
3303 (initial-fun (read-wordindexed cold-fdefn
3304 sb!vm:fdefn-fun-slot)))
3305 (format t
3306 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
3307 (descriptor-bits initial-fun))
3308 (write-word (descriptor-bits initial-fun)))
3310 ;; Write the End entry.
3311 (write-word end-core-entry-type-code)
3312 (write-word 2)))
3314 (format t "done]~%")
3315 (force-output)
3316 (/show "leaving WRITE-INITIAL-CORE-FILE")
3317 (values))
3319 ;;;; the actual GENESIS function
3321 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3322 ;;; and/or information about a Lisp core, therefrom.
3324 ;;; input file arguments:
3325 ;;; SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
3326 ;;; *tab* *characters* *converted* *to* *spaces*. (We push
3327 ;;; responsibility for removing tabs out to the caller it's
3328 ;;; trivial to remove them using UNIX command line tools like
3329 ;;; sed, whereas it's a headache to do it portably in Lisp because
3330 ;;; #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
3331 ;;; a core file cannot be built (but a C header file can be).
3333 ;;; output files arguments (any of which may be NIL to suppress output):
3334 ;;; CORE-FILE-NAME gets a Lisp core.
3335 ;;; C-HEADER-FILE-NAME gets a C header file, traditionally called
3336 ;;; internals.h, which is used by the C compiler when constructing
3337 ;;; the executable which will load the core.
3338 ;;; MAP-FILE-NAME gets (?) a map file. (dunno about this -- WHN 19990815)
3340 ;;; FIXME: GENESIS doesn't belong in SB!VM. Perhaps in %KERNEL for now,
3341 ;;; perhaps eventually in SB-LD or SB-BOOT.
3342 (defun sb!vm:genesis (&key
3343 object-file-names
3344 symbol-table-file-name
3345 core-file-name
3346 map-file-name
3347 c-header-dir-name
3348 #+nil (list-objects t))
3349 #!+sb-dynamic-core
3350 (declare (ignorable symbol-table-file-name))
3352 (format t
3353 "~&beginning GENESIS, ~A~%"
3354 (if core-file-name
3355 ;; Note: This output summarizing what we're doing is
3356 ;; somewhat telegraphic in style, not meant to imply that
3357 ;; we're not e.g. also creating a header file when we
3358 ;; create a core.
3359 (format nil "creating core ~S" core-file-name)
3360 (format nil "creating headers in ~S" c-header-dir-name)))
3362 (let ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
3364 #!-sb-dynamic-core
3365 (when core-file-name
3366 (if symbol-table-file-name
3367 (load-cold-foreign-symbol-table symbol-table-file-name)
3368 (error "can't output a core file without symbol table file input")))
3370 #!+sb-dynamic-core
3371 (progn
3372 (setf (gethash (extern-alien-name "undefined_tramp")
3373 *cold-foreign-symbol-table*)
3374 (dyncore-note-symbol "undefined_tramp" nil))
3375 (dyncore-note-symbol "undefined_alien_function" nil))
3377 ;; Now that we've successfully read our only input file (by
3378 ;; loading the symbol table, if any), it's a good time to ensure
3379 ;; that there'll be someplace for our output files to go when
3380 ;; we're done.
3381 (flet ((frob (filename)
3382 (when filename
3383 (ensure-directories-exist filename :verbose t))))
3384 (frob core-file-name)
3385 (frob map-file-name))
3387 ;; (This shouldn't matter in normal use, since GENESIS normally
3388 ;; only runs once in any given Lisp image, but it could reduce
3389 ;; confusion if we ever experiment with running, tweaking, and
3390 ;; rerunning genesis interactively.)
3391 (do-all-symbols (sym)
3392 (remprop sym 'cold-intern-info))
3394 (check-spaces)
3396 (let* ((*foreign-symbol-placeholder-value* (if core-file-name nil 0))
3397 (*load-time-value-counter* 0)
3398 (*cold-fdefn-objects* (make-hash-table :test 'equal))
3399 (*cold-symbols* (make-hash-table :test 'eql)) ; integer keys
3400 (*cold-package-symbols* (make-hash-table :test 'equal)) ; string keys
3401 (pkg-metadata (sb-cold:read-from-file "package-data-list.lisp-expr"))
3402 (*read-only* (make-gspace :read-only
3403 read-only-core-space-id
3404 sb!vm:read-only-space-start))
3405 (*static* (make-gspace :static
3406 static-core-space-id
3407 sb!vm:static-space-start))
3408 (*dynamic* (make-gspace :dynamic
3409 dynamic-core-space-id
3410 #!+gencgc sb!vm:dynamic-space-start
3411 #!-gencgc sb!vm:dynamic-0-space-start))
3412 ;; There's a cyclic dependency here: NIL refers to a package;
3413 ;; a package needs its layout which needs others layouts
3414 ;; which refer to NIL, which refers to a package ...
3415 ;; Break the cycle by preallocating packages without a layout.
3416 ;; This avoids having to track any symbols created prior to
3417 ;; creation of packages, since packages are primordial.
3418 (target-cl-pkg-info
3419 (dolist (name (list* "COMMON-LISP" "COMMON-LISP-USER" "KEYWORD"
3420 (mapcar #'sb-cold:package-data-name
3421 pkg-metadata))
3422 (gethash "COMMON-LISP" *cold-package-symbols*))
3423 (setf (gethash name *cold-package-symbols*)
3424 (cons (allocate-structure-object
3425 *dynamic* (layout-length (find-layout 'package))
3426 (make-fixnum-descriptor 0))
3427 (cons nil nil))))) ; (externals . internals)
3428 (*nil-descriptor* (make-nil-descriptor target-cl-pkg-info))
3429 (*current-reversed-cold-toplevels* *nil-descriptor*)
3430 (*current-debug-sources* *nil-descriptor*)
3431 (*unbound-marker* (make-other-immediate-descriptor
3433 sb!vm:unbound-marker-widetag))
3434 *cold-assembler-fixups*
3435 *cold-assembler-routines*
3436 #!+x86 (*load-time-code-fixups* (make-hash-table)))
3438 ;; Prepare for cold load.
3439 (initialize-non-nil-symbols)
3440 (initialize-layouts)
3441 (initialize-packages
3442 ;; docstrings are set in src/cold/warm. It would work to do it here,
3443 ;; but seems preferable not to saddle Genesis with such responsibility.
3444 (list* (sb-cold:make-package-data :name "COMMON-LISP" :doc nil)
3445 (sb-cold:make-package-data :name "KEYWORD" :doc nil)
3446 (sb-cold:make-package-data :name "COMMON-LISP-USER" :doc nil
3447 :use '("COMMON-LISP"
3448 ;; ANSI encourages us to put extension packages
3449 ;; in the USE list of COMMON-LISP-USER.
3450 "SB!ALIEN" "SB!DEBUG" "SB!EXT" "SB!GRAY" "SB!PROFILE"))
3451 pkg-metadata))
3452 (initialize-static-fns)
3454 ;; Initialize the *COLD-SYMBOLS* system with the information
3455 ;; from common-lisp-exports.lisp-expr.
3456 ;; Packages whose names match SB!THING were set up on the host according
3457 ;; to "package-data-list.lisp-expr" which expresses the desired target
3458 ;; package configuration, so we can just mirror the host into the target.
3459 ;; But by waiting to observe calls to COLD-INTERN that occur during the
3460 ;; loading of the cross-compiler's outputs, it is possible to rid the
3461 ;; target of accidental leftover symbols, not that it wouldn't also be
3462 ;; a good idea to clean up package-data-list once in a while.
3463 (dolist (exported-name
3464 (sb-cold:read-from-file "common-lisp-exports.lisp-expr"))
3465 (cold-intern (intern exported-name *cl-package*) :access :external))
3467 ;; Cold load.
3468 (dolist (file-name object-file-names)
3469 (write-line (namestring file-name))
3470 (cold-load file-name))
3472 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3473 (resolve-assembler-fixups)
3474 #!+x86 (output-load-time-code-fixups)
3475 (foreign-symbols-to-core)
3476 (finish-symbols)
3477 (/show "back from FINISH-SYMBOLS")
3478 (finalize-load-time-value-noise)
3480 ;; Tell the target Lisp how much stuff we've allocated.
3481 (cold-set 'sb!vm:*read-only-space-free-pointer*
3482 (allocate-cold-descriptor *read-only*
3484 sb!vm:even-fixnum-lowtag))
3485 (cold-set 'sb!vm:*static-space-free-pointer*
3486 (allocate-cold-descriptor *static*
3488 sb!vm:even-fixnum-lowtag))
3489 (/show "done setting free pointers")
3491 ;; Write results to files.
3493 ;; FIXME: I dislike this approach of redefining
3494 ;; *STANDARD-OUTPUT* instead of putting the new stream in a
3495 ;; lexical variable, and it's annoying to have WRITE-MAP (to
3496 ;; *STANDARD-OUTPUT*) not be parallel to WRITE-INITIAL-CORE-FILE
3497 ;; (to a stream explicitly passed as an argument).
3498 (macrolet ((out-to (name &body body)
3499 `(let ((fn (format nil "~A/~A.h" c-header-dir-name ,name)))
3500 (ensure-directories-exist fn)
3501 (with-open-file (*standard-output* fn
3502 :if-exists :supersede :direction :output)
3503 (write-boilerplate)
3504 (let ((n (c-name (string-upcase ,name))))
3505 (format
3507 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~A 1~%"
3508 n n))
3509 ,@body
3510 (format t
3511 "#endif /* SBCL_GENESIS_~A */~%"
3512 (string-upcase ,name))))))
3513 (when map-file-name
3514 (with-open-file (*standard-output* map-file-name
3515 :direction :output
3516 :if-exists :supersede)
3517 (write-map)))
3518 (out-to "config" (write-config-h))
3519 (out-to "constants" (write-constants-h))
3520 #!+sb-ldb
3521 (out-to "tagnames" (write-tagnames-h))
3522 (let ((structs (sort (copy-list sb!vm:*primitive-objects*) #'string<
3523 :key (lambda (obj)
3524 (symbol-name
3525 (sb!vm:primitive-object-name obj))))))
3526 (dolist (obj structs)
3527 (out-to
3528 (string-downcase (string (sb!vm:primitive-object-name obj)))
3529 (write-primitive-object obj)))
3530 (out-to "primitive-objects"
3531 (dolist (obj structs)
3532 (format t "~&#include \"~A.h\"~%"
3533 (string-downcase
3534 (string (sb!vm:primitive-object-name obj)))))))
3535 (dolist (class '(hash-table
3536 classoid
3537 layout
3538 sb!c::compiled-debug-info
3539 sb!c::compiled-debug-fun
3540 sb!xc:package))
3541 (out-to
3542 (string-downcase (string class))
3543 (write-structure-object
3544 (layout-info (find-layout class)))))
3545 (out-to "static-symbols" (write-static-symbols))
3547 (let ((fn (format nil "~A/Makefile.features" c-header-dir-name)))
3548 (ensure-directories-exist fn)
3549 (with-open-file (*standard-output* fn :if-exists :supersede
3550 :direction :output)
3551 (write-makefile-features)))
3553 (when core-file-name
3554 (write-initial-core-file core-file-name))))))
3556 ;; This generalization of WARM-FUN-NAME will do in a pinch
3557 ;; to view strings and things in a gspace.
3558 (defun warmelize (descriptor)
3559 (labels ((recurse (x)
3560 (when (eql (descriptor-bits x) (descriptor-bits *nil-descriptor*))
3561 (return-from recurse nil))
3562 (ecase (descriptor-lowtag x)
3563 (#.sb!vm:list-pointer-lowtag
3564 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
3565 (#.sb!vm:fun-pointer-lowtag
3566 (let ((name (read-wordindexed x sb!vm:simple-fun-name-slot)))
3567 `(function ,(recurse name))))
3568 (#.sb!vm:other-pointer-lowtag
3569 (let ((widetag (logand (descriptor-bits (read-wordindexed x 0))
3570 sb!vm:widetag-mask)))
3571 (ecase widetag
3572 (#.sb!vm:symbol-header-widetag
3573 ;; this is only approximate, as it disregards package
3574 (intern (recurse (read-wordindexed x sb!vm:symbol-name-slot))))
3575 (#.sb!vm:simple-base-string-widetag
3576 (let* ((len (descriptor-fixnum (read-wordindexed x 1)))
3577 (str (make-string len))
3578 (bytes (gspace-bytes (descriptor-gspace x))))
3579 (dotimes (i len str)
3580 (setf (aref str i)
3581 (code-char
3582 (bvref bytes
3583 (+ (descriptor-byte-offset x)
3584 (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
3585 i)))))))))))))
3586 (recurse descriptor)))