Store fop symbol -> number map in a hash-table
[sbcl.git] / src / compiler / generic / genesis.lisp
blob0b8ac70d2eae0b616f9bd4605672d0717b571713
1 ;;;; "cold" core image builder: This is how we create a target Lisp
2 ;;;; system from scratch, by converting from fasl files to an image
3 ;;;; file in the cross-compilation host, without the help of the
4 ;;;; target Lisp system.
5 ;;;;
6 ;;;; As explained by Rob MacLachlan on the CMU CL mailing list Wed, 06
7 ;;;; Jan 1999 11:05:02 -0500, this cold load generator more or less
8 ;;;; fakes up static function linking. I.e. it makes sure that all the
9 ;;;; DEFUN-defined functions in the fasl files it reads are bound to the
10 ;;;; corresponding symbols before execution starts. It doesn't do
11 ;;;; anything to initialize variable values; instead it just arranges
12 ;;;; for !COLD-INIT to be called at cold load time. !COLD-INIT is
13 ;;;; responsible for explicitly initializing anything which has to be
14 ;;;; initialized early before it transfers control to the ordinary
15 ;;;; top level forms.
16 ;;;;
17 ;;;; (In CMU CL, and in SBCL as of 0.6.9 anyway, functions not defined
18 ;;;; by DEFUN aren't set up specially by GENESIS.)
20 ;;;; This software is part of the SBCL system. See the README file for
21 ;;;; more information.
22 ;;;;
23 ;;;; This software is derived from the CMU CL system, which was
24 ;;;; written at Carnegie Mellon University and released into the
25 ;;;; public domain. The software is in the public domain and is
26 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
27 ;;;; files for more information.
29 (in-package "SB-FASL")
31 ;;; Some build systems frown upon excessive use (or any use) of "-I" options
32 ;;; on the C compiler invocation. So depending on the current working directory
33 ;;; when generating headers and when building, the pathname where we produce
34 ;;; headers may differ from the string specified in #include lines.
35 ;;; The :C-HEADER-DIR-NAME keyword to genesis specifies the output path,
36 ;;; and this symbol (which is normally unbound) specifies the #include prefix.
37 ;;; The normal build is done within src/runtime and does not need
38 ;;; anything done to set this.
39 (defun genesis-header-prefix ()
40 (if (boundp 'cl-user::*genesis-header-prefix*)
41 (symbol-value 'cl-user::*genesis-header-prefix*)
42 "genesis"))
43 ;;; By the same reasoning as above, lispobj.h is either in "." or a relative path.
44 (defun lispobj-dot-h ()
45 (if (boundp 'cl-user::*lispobj-h-namestring*)
46 (symbol-value 'cl-user::*lispobj-h-namestring*)
47 "lispobj.h"))
49 (eval-when (:compile-toplevel :load-toplevel :execute)
50 (use-package "SB-COREFILE"))
52 (defun round-up (number size)
53 "Round NUMBER up to be an integral multiple of SIZE."
54 (* size (ceiling number size)))
56 ;;;; implementing the concept of "vector" in (almost) portable
57 ;;;; Common Lisp
58 ;;;;
59 ;;;; "If you only need to do such simple things, it doesn't really
60 ;;;; matter which language you use." -- _ANSI Common Lisp_, p. 1, Paul
61 ;;;; Graham (evidently not considering the abstraction "vector" to be
62 ;;;; such a simple thing:-)
64 (defconstant +smallvec-length+ (expt 2 16))
66 ;;; an element of a BIGVEC -- a vector small enough that we have
67 ;;; a good chance of it being portable to other Common Lisps
68 (deftype smallvec ()
69 `(simple-array (unsigned-byte 8) (,+smallvec-length+)))
71 (defun make-smallvec ()
72 (make-array +smallvec-length+ :element-type '(unsigned-byte 8)
73 :initial-element 0))
75 ;;; a big vector, implemented as a vector of SMALLVECs
76 ;;;
77 ;;; KLUDGE: This implementation seems portable enough for our
78 ;;; purposes, since realistically every modern implementation is
79 ;;; likely to support vectors of at least 2^16 elements. But if you're
80 ;;; masochistic enough to read this far into the contortions imposed
81 ;;; on us by ANSI and the Lisp community, for daring to use the
82 ;;; abstraction of a large linearly addressable memory space, which is
83 ;;; after all only directly supported by the underlying hardware of at
84 ;;; least 99% of the general-purpose computers in use today, then you
85 ;;; may be titillated to hear that in fact this code isn't really
86 ;;; portable, because as of sbcl-0.7.4 we need somewhat more than
87 ;;; 16Mbytes to represent a core, and ANSI only guarantees that
88 ;;; ARRAY-DIMENSION-LIMIT is not less than 1024. -- WHN 2002-06-13
89 (defstruct (bigvec (:constructor %make-bigvec ()))
90 (outer-vector (vector (make-smallvec)) :type (vector smallvec)))
91 (defun make-bigvec (&optional (min-size 0))
92 (expand-bigvec (%make-bigvec) min-size))
94 ;;; analogous to SVREF, but into a BIGVEC
95 (defun bvref (bigvec index)
96 (multiple-value-bind (outer-index inner-index)
97 (floor index +smallvec-length+)
98 (aref (the smallvec
99 (svref (bigvec-outer-vector bigvec) outer-index))
100 inner-index)))
101 (defun (setf bvref) (new-value bigvec index)
102 (multiple-value-bind (outer-index inner-index)
103 (floor index +smallvec-length+)
104 (setf (aref (the (simple-array (unsigned-byte 8) (*))
105 (svref (bigvec-outer-vector bigvec) outer-index))
106 inner-index)
107 new-value)))
109 ;;; analogous to LENGTH, but for a BIGVEC
111 ;;; the length of BIGVEC, measured in the number of BVREFable bytes it
112 ;;; can hold
113 (defun bvlength (bigvec)
114 (* (length (bigvec-outer-vector bigvec))
115 +smallvec-length+))
117 (defparameter *bigvec-for-write-words* (%make-bigvec))
118 (defun write-words (stream &rest words)
119 (let ((bigvec *bigvec-for-write-words*)
120 (offset 0))
121 (if (and (singleton-p words) (typep (first words) 'array))
122 (dovector (word (first words))
123 (setf (bvref-word bigvec offset) (the sb-vm:word word))
124 (incf offset sb-vm:n-word-bytes))
125 (dolist (word words)
126 (setf (bvref-word bigvec offset) (the sb-vm:word word))
127 (incf offset sb-vm:n-word-bytes)))
128 (write-sequence (elt (bigvec-outer-vector bigvec) 0) stream :end offset)))
130 ;;; analogous to WRITE-SEQUENCE, but for a BIGVEC
131 (defun write-bigvec-as-sequence (bigvec stream &key end pad-with-zeros)
132 (binding* ((bvlength (bvlength bigvec))
133 (data-length (min (or end bvlength) bvlength))
134 ;; Compute the coordinates of the final byte to be written
135 ((outer-index inner-index)
136 (if (zerop data-length)
137 (values 0 -1)
138 (floor (1- data-length) +smallvec-length+))))
139 ;; Each SMALLVEC prior to the one indexed by outer-index is written in its entirety
140 (dotimes (i outer-index)
141 (write-sequence (elt (bigvec-outer-vector bigvec) i) stream))
142 ;; The SMALLVEC at OUTER-INDEX is written up to and including INNER-INDEX
143 (write-sequence (elt (bigvec-outer-vector bigvec) outer-index) stream
144 :end (1+ inner-index))
145 ;; FIXME: This logic from rev 243d0f6f59 says it's needed if +SMALLVEC-LENGTH+ is
146 ;; less than backend page bytes, but if that were true (which it never is)
147 ;; we should just increase +SMALLVEC-LENGTH+. And how can could it be right even in
148 ;; that case? DATA-LENGTH is not larger than BVLENGTH, because it it were,
149 ;; you asked to write more than the vector holds. Istm this is garbage
150 ;; but I'm afraid to remove it.
151 (when (and pad-with-zeros (< bvlength data-length))
152 (loop repeat (- data-length bvlength) do (write-byte 0 stream)))))
154 ;;; analogous to READ-SEQUENCE-OR-DIE, but for a BIGVEC
155 ;;; FIXME: should signal error on EOF
156 (defun read-into-bigvec (bigvec stream start nbytes)
157 ;; compute the coordinates of the start and end
158 (binding* (((start-outer start-inner) (floor start +smallvec-length+))
159 ;; this the INCLUSIVE bound on the ending element
160 (end-outer (floor (+ start nbytes -1) +smallvec-length+)))
161 ;; if it's all into a single outer vector, take the quick route
162 (if (= start-outer end-outer)
163 (read-sequence (elt (bigvec-outer-vector bigvec) start-outer) stream
164 :start start-inner :end (+ start-inner nbytes))
165 ;; KISS - use the slow algorithm rather than any "partial read" cleverness
166 (loop for i of-type index from start repeat nbytes
167 do (setf (bvref bigvec i) (read-byte stream))))))
169 ;;; Grow BIGVEC (exponentially, so that large increases in size have
170 ;;; asymptotic logarithmic cost per byte).
171 (defun expand-bigvec (bigvec required-length)
172 (loop
173 (when (>= (bvlength bigvec) required-length)
174 (return bigvec))
175 (let* ((old-outer-vector (bigvec-outer-vector bigvec))
176 (length-old-outer-vector (length old-outer-vector))
177 (new-outer-vector (make-array (* 2 length-old-outer-vector))))
178 (replace new-outer-vector old-outer-vector)
179 (loop for i from length-old-outer-vector below (length new-outer-vector)
180 do (setf (svref new-outer-vector i) (make-smallvec)))
181 (setf (bigvec-outer-vector bigvec)
182 new-outer-vector))))
184 ;;;; looking up bytes and multi-byte values in a BIGVEC (considering
185 ;;;; it as an image of machine memory on the cross-compilation target)
187 ;;; BVREF-32 and friends. These are like SAP-REF-n, except that
188 ;;; instead of a SAP we use a BIGVEC.
189 (macrolet ((make-bvref-n (n)
190 (let ((name (intern (format nil "BVREF-~A" n)))
191 (le-octet-indices
192 (loop with n-octets = (/ n 8)
193 for i from 0 below n-octets
194 collect `(+ byte-index #+big-endian ,(- n-octets i 1)
195 #-big-endian ,i))))
196 `(progn
197 (defun ,name (bigvec byte-index)
198 (logior ,@(loop for index in le-octet-indices
199 for i from 0
200 collect `(ash (bvref bigvec ,index) ,(* i 8)))))
201 (defun (setf ,name) (new-value bigvec byte-index)
202 (declare (type (unsigned-byte ,n) new-value))
203 (setf ,@(loop for index in le-octet-indices
204 for i from 0
205 append `((bvref bigvec ,index)
206 (ldb (byte 8 ,(* i 8)) new-value))))
207 new-value)))))
208 (make-bvref-n 8)
209 (make-bvref-n 16)
210 (make-bvref-n 32)
211 (make-bvref-n 64))
213 (defun (setf bvref-s32) (newval bv index)
214 (setf (bvref-32 bv index) (ldb (byte 32 0) (the (signed-byte 32) newval)))
215 newval)
217 #+host-quirks-sbcl
218 (progn
219 (declaim (inline native-bvref-word (setf native-bvref-word)))
220 (defun native-bvref-word (bigvec byte-index)
221 (multiple-value-bind (outer-index inner-index) (floor byte-index +smallvec-length+)
222 (host-sb-kernel:%vector-raw-bits
223 (the smallvec (svref (bigvec-outer-vector bigvec) outer-index))
224 (ash inner-index (- sb-vm:word-shift)))))
225 (defun (setf native-bvref-word) (newval bigvec byte-index)
226 (multiple-value-bind (outer-index inner-index) (floor byte-index +smallvec-length+)
227 (setf (host-sb-kernel:%vector-raw-bits
228 (the smallvec (svref (bigvec-outer-vector bigvec) outer-index))
229 (ash inner-index (- sb-vm:word-shift)))
230 newval))))
232 ;; lispobj-sized word, whatever that may be
233 ;; hopefully nobody ever wants a 128-bit SBCL...
234 (macrolet ((access (bv index &optional alignedp)
235 (cond ((and alignedp
236 (and (member :sbcl cl:*features*)
237 (sb-cold::compatible-vector-raw-bits)))
238 `(native-bvref-word ,bv ,index))
240 `(#+64-bit bvref-64 #-64-bit bvref-32 ,bv ,index)))))
241 (defun (setf bvref-word-unaligned) (new-val bytes index)
242 (declare (type sb-xc:fixnum index))
243 (setf (access bytes index) new-val))
244 (defun (setf bvref-word) (new-val bytes index)
245 (declare (type sb-xc:fixnum index))
246 (aver (not (logtest index (ash sb-vm:lowtag-mask -1))))
247 (setf (access bytes index t) new-val))
248 (defun bvref-word (bytes index)
249 (declare (type sb-xc:fixnum index))
250 (aver (not (logtest index (ash sb-vm:lowtag-mask -1))))
251 (access bytes index t)))
253 ;;;; representation of spaces in the core
255 ;;; If there is more than one dynamic space in memory (i.e., if a
256 ;;; copying GC is in use), then only the active dynamic space gets
257 ;;; dumped to core.
258 (defvar *dynamic*)
259 (defvar *permgen*)
260 (defvar *static*)
261 (defvar *read-only*)
262 (defvar core-file-name)
264 (defvar *immobile-fixedobj*) ; always defined, we can test BOUNDP on it
265 #+immobile-space
266 (progn
267 (defvar *asm-routine-vector*)
268 (defvar *immobile-text*)
269 (defvar *immobile-space-map* nil))
271 (defstruct page
272 (type nil :type (member nil :code :list :mixed))
273 (words-used 0)
274 (allocation-bitmap
275 (make-array (/ sb-vm:gencgc-page-bytes
276 (ash 1 sb-vm:n-lowtag-bits)
277 sb-vm:n-word-bits)
278 :element-type 'sb-vm:word
279 :initial-element 0))
280 single-object-p
281 scan-start) ; byte offset from base of the space
283 ;;; a GENESIS-time representation of a memory space (e.g. read-only
284 ;;; space, dynamic space, or static space)
285 (defstruct (gspace (:constructor %make-gspace)
286 (:copier nil))
287 ;; name and identifier for this GSPACE
288 (name (missing-arg) :type symbol :read-only t)
289 (identifier (missing-arg) :type fixnum :read-only t)
290 ;; the address where the data will be loaded
291 (byte-address (missing-arg) :type unsigned-byte :read-only t)
292 ;; the gspace contents as a BIGVEC
293 (data (make-bigvec) :type bigvec :read-only t)
294 (page-table nil) ; for dynamic space
295 (cons-region) ; (word-index . limit)
296 ;; lists of holes created by the allocator to segregate code from data.
297 ;; Doesn't matter for cheneygc; does for gencgc.
298 ;; Each free-range is (START . LENGTH) in words.
299 (code-free-ranges (list nil))
300 (non-code-free-ranges (list nil))
301 ;; Address of every object created in this space.
302 (objects (or #+sb-devel (make-array 700000 :fill-pointer 0 :adjustable t)))
303 ;; the index of the next unwritten word (i.e. chunk of
304 ;; SB-VM:N-WORD-BYTES bytes) in DATA, or equivalently the number of
305 ;; words actually written in DATA. In order to convert to an actual
306 ;; index into DATA, thus must be multiplied by SB-VM:N-WORD-BYTES.
307 (free-word-index 0))
309 (defun gspace-upper-bound (gspace)
310 (+ (gspace-byte-address gspace)
311 (ash (gspace-free-word-index gspace) sb-vm:word-shift)))
313 (cl:defmethod print-object ((gspace gspace) stream)
314 (print-unreadable-object (gspace stream :type t)
315 (format stream "@#x~X ~S" (gspace-byte-address gspace) (gspace-name gspace))))
317 (defun make-gspace (name identifier byte-address &rest rest)
318 ;; Genesis should be agnostic of space alignment except in so far as it must
319 ;; be a multiple of the backend page size. We used to care more, in that
320 ;; descriptor-bits were composed of a high half and low half for the
321 ;; questionable motive of caring about fixnum-ness of the halves,
322 ;; despite the wonderful abstraction INTEGER that transparently becomes
323 ;; a BIGNUM if the host's fixnum is limited in size.
324 ;; So it's not clear whether this test belongs here, because if we do need it,
325 ;; then it best belongs where we assign space addresses in the first place.
326 (let ((target-space-alignment sb-c:+backend-page-bytes+))
327 (unless (zerop (rem byte-address target-space-alignment))
328 (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
329 byte-address target-space-alignment)))
330 (apply #'%make-gspace :name name :identifier identifier :byte-address byte-address rest))
332 (defstruct (model-sap (:constructor make-model-sap (address gspace)))
333 (address 0 :type sb-vm:word)
334 (gspace nil :type gspace))
335 (defun sap-int (x) (model-sap-address x))
336 (defun sap+ (sap x)
337 (make-model-sap (+ (model-sap-address sap) x)
338 (model-sap-gspace sap)))
339 (macrolet ((access (name)
340 `(,name (gspace-data (model-sap-gspace sap))
341 (- (+ (model-sap-address sap) offset)
342 (gspace-byte-address (model-sap-gspace sap))))))
343 (defun sap-ref-8 (sap offset) (access bvref-8))
344 (defun sap-ref-16 (sap offset) (access bvref-16))
345 (defun sap-ref-32 (sap offset) (access bvref-32))
346 (defun sap-ref-64 (sap offset) (access bvref-64))
347 (defun signed-sap-ref-32 (sap offset)
348 (sb-disassem:sign-extend (access bvref-32) 32))
349 (defun signed-sap-ref-64 (sap offset)
350 (sb-disassem:sign-extend (access bvref-64) 64))
351 (defun (setf sap-ref-16) (newval sap offset)
352 (setf (access bvref-16) newval))
353 (defun (setf sap-ref-32) (newval sap offset)
354 (setf (access bvref-32) newval))
355 (defun (setf signed-sap-ref-32) (newval sap offset)
356 (setf (access bvref-32) (ldb (byte 32 0) (the (signed-byte 32) newval))))
357 (defun (setf sap-ref-64) (newval sap offset)
358 (setf (access bvref-64) newval)))
360 ;;;; representation of descriptors
362 (declaim (inline is-fixnum-lowtag))
363 (defun is-fixnum-lowtag (lowtag)
364 (zerop (logand lowtag sb-vm:fixnum-tag-mask)))
366 (defun is-other-immediate-lowtag (lowtag)
367 ;; The other-immediate lowtags are similar to the fixnum lowtags, in
368 ;; that they have an "effective length" that is shorter than is used
369 ;; for the pointer lowtags. Unlike the fixnum lowtags, however, the
370 ;; other-immediate lowtags are always effectively two bits wide.
371 (= (logand lowtag 3) sb-vm:other-immediate-0-lowtag))
373 (defstruct (descriptor
374 (:constructor make-descriptor (bits &optional %gspace))
375 (:copier nil))
376 ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
377 (%gspace nil :type (or gspace null))
378 (bits 0 :read-only t :type (unsigned-byte #.sb-vm:n-machine-word-bits)))
380 (declaim (inline descriptor=))
381 (defun descriptor= (a b) (eql (descriptor-bits a) (descriptor-bits b)))
383 (defun make-random-descriptor (bits)
384 (make-descriptor (logand bits sb-ext:most-positive-word)))
386 (declaim (inline descriptor-lowtag descriptor-widetag))
387 (defun descriptor-lowtag (des)
388 "the lowtag bits for DES"
389 (logand (descriptor-bits des) sb-vm:lowtag-mask))
390 (defun descriptor-widetag (des)
391 (logand (read-bits-wordindexed des 0) sb-vm:widetag-mask))
393 (declaim (inline descriptor-base-address))
394 (defun descriptor-base-address (des)
395 (logandc2 (descriptor-bits des) sb-vm:lowtag-mask))
396 (defun descriptor-byte-offset (des)
397 (- (descriptor-base-address des) (gspace-byte-address (descriptor-gspace des))))
399 (defmethod print-object ((des descriptor) stream)
400 (print-unreadable-object (des stream :type t)
401 (let ((lowtag (descriptor-lowtag des))
402 (bits (descriptor-bits des)))
403 (multiple-value-call 'format stream
404 (cond ((is-fixnum-lowtag lowtag)
405 (values "for fixnum: ~W" (descriptor-fixnum des)))
406 ((is-other-immediate-lowtag lowtag)
407 (values "for other immediate: #X~X, type #b~8,'0B"
408 (ash bits (- sb-vm:n-widetag-bits))
409 (logand bits sb-vm:widetag-mask)))
410 ((descriptor-%gspace des)
411 (values "for pointer: #X~X, lowtag #b~v,'0B, ~A"
412 (descriptor-base-address des)
413 sb-vm:n-lowtag-bits lowtag
414 (gspace-name (descriptor-%gspace des))))
416 (values "bits: #X~X" bits)))))))
418 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
419 ;;; free word index is boosted as necessary, and if additional memory
420 ;;; is needed, we grow the GSPACE. The descriptor returned is a
421 ;;; pointer of type LOWTAG.
422 (defun allocate-cold-descriptor (gspace length lowtag &optional (page-type :mixed))
423 (let* ((relative-ptr (ash (gspace-claim-n-bytes gspace length page-type)
424 sb-vm:word-shift))
425 (ptr (+ (gspace-byte-address gspace) relative-ptr))
426 (des (make-descriptor (logior ptr lowtag) gspace)))
427 (awhen (gspace-objects gspace) (vector-push-extend des it))
428 des))
430 (defun gspace-claim-n-words (gspace n-words)
431 (let* ((old-free-word-index (gspace-free-word-index gspace))
432 (new-free-word-index (+ old-free-word-index n-words)))
433 ;; Grow GSPACE as necessary
434 (expand-bigvec (gspace-data gspace) (* new-free-word-index sb-vm:n-word-bytes))
435 ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
436 (setf (gspace-free-word-index gspace) new-free-word-index)
437 old-free-word-index))
439 (defconstant min-usable-hole-size 10) ; semi-arbitrary constant to speed up the allocator
440 ;; Place conses and code on their respective page type.
441 (defun dynamic-space-claim-n-words (gspace n-words page-type
442 &aux (words-per-page
443 (/ sb-vm:gencgc-page-bytes sb-vm:n-word-bytes)))
444 (labels ((alignedp (word-index) ; T if WORD-INDEX aligns to a GC page boundary
445 (not (logtest (* word-index sb-vm:n-word-bytes)
446 (1- sb-vm:gencgc-page-bytes))))
447 (page-index (word-index)
448 (values (floor word-index words-per-page)))
449 (pte (index) ; create on demand
450 (or (aref (gspace-page-table gspace) index)
451 (setf (aref (gspace-page-table gspace) index) (make-page))))
452 (assign-page-type (page-type start-word-index count)
453 ;; CMUCL incorrectly warns that the result of ADJUST-ARRAY
454 ;; must not be discarded.
455 #+host-quirks-cmu (declare (notinline adjust-array))
456 (let ((start-page (page-index start-word-index))
457 (end-page (page-index (+ start-word-index (1- count)))))
458 (unless (> (length (gspace-page-table gspace)) end-page)
459 (adjust-array (gspace-page-table gspace) (1+ end-page)
460 :initial-element nil))
461 #+mark-region-gc
462 (when (> end-page start-page)
463 (assert (alignedp start-word-index)))
464 (loop for page-index from start-page to end-page
465 for pte = (pte page-index)
466 do (if (null (page-type pte))
467 (setf (page-type pte) page-type)
468 (aver (eq (page-type pte) page-type))))))
469 (mark-allocation (start-word-index)
470 ;; Mark the start of the object for mark-region GC.
471 (let* ((start-page (page-index start-word-index))
472 (pte (pte start-page))
473 (word-in-page (mod start-word-index words-per-page)))
474 (multiple-value-bind (word-index bit-index)
475 (floor (floor word-in-page 2) sb-vm:n-word-bits)
476 (setf (ldb (byte 1 bit-index)
477 (aref (page-allocation-bitmap pte) word-index))
478 1))))
479 (note-words-used (start-word-index)
480 (let* ((start-page (page-index start-word-index))
481 (end-word-index (+ start-word-index n-words))
482 (end-page (page-index (1- end-word-index))))
483 ;; pages from start to end (exclusive) must be full
484 (loop for index from start-page below end-page
485 do (setf (page-words-used (pte index)) words-per-page))
486 ;; Compute the difference between the word-index at the start of
487 ;; end-page and the end-word.
488 (setf (page-words-used (pte end-page))
489 (- end-word-index (* end-page words-per-page)))
490 ;; update the scan start of any page without it set
491 (loop for index from start-page to end-page
492 do (let ((pte (pte index)))
493 (unless (page-scan-start pte)
494 (setf (page-scan-start pte) start-word-index)
495 ;; gencgc doesn't use single-object pages in genesis.
496 ;; mark-region does for all oversized objects.
497 #+mark-region-gc
498 (setf (page-single-object-p pte) (>= n-words words-per-page))))))
499 start-word-index)
500 (get-frontier-page-type ()
501 (page-type (pte (page-index (1- (gspace-free-word-index gspace))))))
502 (realign-frontier (&key (keep-hole t))
503 ;; Align the frontier to a page, putting the empty space onto a free list
504 (let* ((free-ptr (gspace-free-word-index gspace))
505 (avail (- (align-up free-ptr words-per-page) free-ptr))
506 (other-type (get-frontier-page-type)) ; before extending frontier
507 (word-index (gspace-claim-n-words gspace avail)))
508 ;; the space we got should be exactly what we thought it should be
509 (aver (= word-index free-ptr))
510 (aver (alignedp (gspace-free-word-index gspace)))
511 (aver (= (gspace-free-word-index gspace) (+ free-ptr avail)))
512 (when (and (>= avail min-usable-hole-size) keep-hole)
513 ;; allocator is first-fit; space goes to the tail of the other freelist.
514 (nconc (ecase other-type
515 (:code (gspace-code-free-ranges gspace))
516 (:mixed (gspace-non-code-free-ranges gspace)))
517 (list (cons word-index avail)))))))
518 (when (eq page-type :list) ; Claim whole pages at a time
519 (let* ((region
520 (or (gspace-cons-region gspace)
521 (progn
522 (unless (alignedp (gspace-free-word-index gspace))
523 (realign-frontier))
524 (let ((word-index (gspace-claim-n-words gspace words-per-page)))
525 (assign-page-type page-type word-index sb-vm:cons-size)
526 (let ((pte (pte (page-index word-index))))
527 (setf (page-scan-start pte) word-index))
528 (setf (gspace-cons-region gspace)
529 (cons word-index
530 (+ word-index (* (1- sb-vm::max-conses-per-page)
531 sb-vm:cons-size))))))))
532 (result (car region)))
533 (incf (page-words-used (pte (page-index result))) sb-vm:cons-size)
534 (mark-allocation result)
535 (when (= (incf (car region) sb-vm:cons-size) (cdr region))
536 (setf (gspace-cons-region gspace) nil))
537 (return-from dynamic-space-claim-n-words result)))
538 (let* ((holder (ecase page-type
539 (:code (gspace-code-free-ranges gspace))
540 (:mixed (gspace-non-code-free-ranges gspace))))
541 (found (find-if (lambda (x) (>= (cdr x) n-words))
542 (cdr holder)))) ; dummy cons cell simplifies writeback
543 (when found ; always try to backfill holes first if possible
544 (let ((word-index (car found)))
545 (if (< (decf (cdr found) n-words) min-usable-hole-size) ; discard this hole now?
546 (rplacd holder (delete found (cdr holder) :count 1)) ; yup
547 (incf (car found) n-words))
548 (mark-allocation word-index)
549 (return-from dynamic-space-claim-n-words (note-words-used word-index))))
550 ;; Avoid switching between :CODE and :MIXED on a page
551 (unless (or (alignedp (gspace-free-word-index gspace))
552 (eq (get-frontier-page-type) page-type))
553 (realign-frontier))
554 ;; The mark-region GC is stricter on what kind of heap it can work
555 ;; with. Notably: objects don't span pages,
556 #+mark-region-gc
557 (let* ((free-ptr (gspace-free-word-index gspace))
558 (avail (- (align-up free-ptr words-per-page) free-ptr)))
559 (when (< avail n-words)
560 (realign-frontier)))
561 ;; and large objects have their own pages,
562 #+mark-region-gc
563 (when (>= n-words words-per-page)
564 (realign-frontier))
565 (let ((word-index (gspace-claim-n-words gspace n-words)))
566 (assign-page-type page-type word-index n-words)
567 (mark-allocation word-index)
568 ;; so small objects can't be put at the end of large objects.
569 #+mark-region-gc
570 (when (>= n-words words-per-page)
571 (realign-frontier :keep-hole nil))
572 (note-words-used word-index)))))
574 (defun gspace-claim-n-bytes (gspace specified-n-bytes &optional (page-type :mixed))
575 (declare (ignorable page-type))
576 (let* ((n-bytes (round-up specified-n-bytes (ash 1 sb-vm:n-lowtag-bits)))
577 (n-words (ash n-bytes (- sb-vm:word-shift))))
578 (aver (evenp n-words))
579 (cond #+immobile-space
580 ((eq gspace *immobile-fixedobj*)
581 ;; There can be at most 1 page in progress for each distinct N-WORDS.
582 ;; Try to find the one which matches.
583 (let* ((found (cdr (assoc n-words *immobile-space-map*)))
584 (words-per-page (/ sb-vm:immobile-card-bytes sb-vm:n-word-bytes)))
585 (unless found ; grab one whole GC page from immobile space
586 (let ((free-word-index (gspace-claim-n-words gspace words-per-page)))
587 (setf found (cons 0 free-word-index))
588 (push (cons n-words found) *immobile-space-map*)))
589 (destructuring-bind (page-word-index . page-base-index) found
590 (let ((next-word (+ page-word-index n-words)))
591 (if (> next-word (- words-per-page n-words))
592 ;; no more objects will fit on this page
593 (setf *immobile-space-map*
594 (delete n-words *immobile-space-map* :key 'car))
595 (setf (car found) next-word)))
596 (+ page-word-index page-base-index))))
597 ((eq gspace *dynamic*)
598 (dynamic-space-claim-n-words gspace n-words page-type))
600 (gspace-claim-n-words gspace n-words)))))
602 (defun descriptor-fixnum (des)
603 (unless (is-fixnum-lowtag (descriptor-lowtag des))
604 (error "descriptor-fixnum called on non-fixnum ~S" des))
605 (let* ((descriptor-bits (descriptor-bits des))
606 (bits (ash descriptor-bits (- sb-vm:n-fixnum-tag-bits))))
607 (if (logbitp (1- sb-vm:n-word-bits) descriptor-bits)
608 (logior bits (ash -1 (1+ sb-vm:n-positive-fixnum-bits)))
609 bits)))
611 (defun descriptor-integer (des)
612 (cond ((is-fixnum-lowtag (descriptor-lowtag des))
613 (descriptor-fixnum des))
614 ((= (descriptor-widetag des) sb-vm:bignum-widetag)
615 (bignum-from-core des))))
617 ;;; common idioms
618 (defun descriptor-mem (des)
619 (gspace-data (descriptor-gspace des)))
621 ;;; If DESCRIPTOR-%GSPACE is already set, just return that. Otherwise,
622 ;;; figure out a GSPACE which corresponds to DES, and memoize and return it.
623 (declaim (ftype (function (descriptor) gspace) descriptor-gspace))
624 (defun descriptor-gspace (des)
625 (or (descriptor-%gspace des)
627 ;; gspace wasn't set, now we have to search for it.
628 (let* ((lowtag (descriptor-lowtag des))
629 (abs-addr (- (descriptor-bits des) lowtag)))
631 ;; Non-pointer objects don't have a gspace.
632 (unless (or (eql lowtag sb-vm:fun-pointer-lowtag)
633 (eql lowtag sb-vm:instance-pointer-lowtag)
634 (eql lowtag sb-vm:list-pointer-lowtag)
635 (eql lowtag sb-vm:other-pointer-lowtag))
636 (error "don't even know how to look for a GSPACE for ~S" des))
638 (dolist (gspace (list *dynamic* *static* *read-only*
639 #+permgen *permgen*
640 #+immobile-space *immobile-fixedobj*
641 #+immobile-space *immobile-text*)
642 (error "couldn't find a GSPACE for ~S" des))
643 ;; Bounds-check the descriptor against the allocated area
644 ;; within each gspace.
645 (when (or (<= (gspace-byte-address gspace) abs-addr (gspace-upper-bound gspace))
646 (and (eq gspace *read-only*) ; KLUDGE
647 (<= sb-vm:read-only-space-start abs-addr
648 sb-vm:read-only-space-end)))
649 (return (setf (descriptor-%gspace des) gspace)))))))
651 (defun descriptor-gspace-name (des)
652 (gspace-name (descriptor-gspace des)))
654 (defun %fixnum-descriptor-if-possible (num)
655 (and (typep num `(signed-byte ,sb-vm:n-fixnum-bits))
656 (make-random-descriptor (ash num sb-vm:n-fixnum-tag-bits))))
658 (defun make-fixnum-descriptor (num)
659 (or (%fixnum-descriptor-if-possible num)
660 (error "~W is too big for a fixnum." num)))
662 (defun make-other-immediate-descriptor (data type)
663 (make-descriptor (logior (ash data sb-vm:n-widetag-bits) type)))
665 (defun make-character-descriptor (data)
666 (make-other-immediate-descriptor data sb-vm:character-widetag))
669 ;;;; miscellaneous variables and other noise
671 ;;; a handle on the trap object
672 (defvar *unbound-marker*
673 (make-other-immediate-descriptor 0 sb-vm:unbound-marker-widetag))
675 ;;; a handle on the NIL object
676 (defvar *nil-descriptor*)
677 (defvar *c-callable-fdefn-vector*)
678 (defvar *lflist-tail-atom*)
680 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
681 ;;; when the target Lisp starts up
683 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
684 ;;; loadtime value, represented by (CONS KEYWORD ..).
685 (declaim (special *!cold-toplevels* *cold-methods*))
688 ;;;; miscellaneous stuff to read and write the core memory
689 (declaim (ftype (function (descriptor sb-vm:word) descriptor) read-wordindexed))
690 (macrolet ((read-bits ()
691 `(bvref-word (descriptor-mem address)
692 (+ (descriptor-byte-offset address)
693 (ash index sb-vm:word-shift)))))
694 (defun read-bits-wordindexed (address index)
695 (read-bits))
696 (defun read-wordindexed (address index)
697 "Return the value which is displaced by INDEX words from ADDRESS."
698 (make-random-descriptor (read-bits))))
700 (defstruct (ltv-patch (:copier nil) (:constructor make-ltv-patch (index)))
701 (index 0 :read-only t))
702 (declaim (ftype (function (descriptor sb-vm:word (or symbol package descriptor ltv-patch)))
703 write-wordindexed))
704 (macrolet ((write-bits (bits)
705 `(setf (bvref-word (descriptor-mem address)
706 (+ (descriptor-byte-offset address)
707 (ash index sb-vm:word-shift)))
708 ,bits)))
709 (defun write-wordindexed (address index value)
710 "Write VALUE displaced INDEX words from ADDRESS."
711 (write-bits
712 (cond ((ltv-patch-p value)
713 (if (or (= (descriptor-lowtag address) sb-vm:list-pointer-lowtag)
714 (= (descriptor-widetag address) sb-vm:code-header-widetag))
715 (push (cold-list (cold-intern :load-time-value-fixup)
716 address
717 (number-to-core index)
718 (number-to-core (ltv-patch-index value)))
719 *!cold-toplevels*)
720 (bug "Can't patch load-time-value into ~S" address))
721 sb-vm:unbound-marker-widetag)
723 (descriptor-bits
724 ;; If we're passed a symbol as a value then it needs to be interned.
725 (cond ((symbolp value) (cold-intern value))
726 ((packagep value) (cdr (cold-find-package-info (sb-xc:package-name value))))
727 (t value)))))))
729 (defun write-wordindexed/raw (address index bits)
730 (declare (type descriptor address) (type sb-vm:word index)
731 (type (or sb-vm:word sb-vm:signed-word) bits))
732 (write-bits (logand bits sb-ext:most-positive-word))))
734 ;;;; allocating images of primitive objects in the cold core
736 (defun write-header-word (des header-word)
737 ;; In immobile space, all objects start life as pseudo-static as if by 'save'.
738 (let* ((gen (or #+immobile-space
739 (let ((gspace (descriptor-gspace des)))
740 (when (or (eq gspace *immobile-fixedobj*)
741 (eq gspace *immobile-text*))
742 sb-vm:+pseudo-static-generation+))
744 (widetag (logand header-word sb-vm:widetag-mask))
745 ;; Refer to depiction of "Immobile object header word" in immobile-space.h
746 (gen-shift (if (= widetag sb-vm:fdefn-widetag) 8 24)))
747 (write-wordindexed/raw des 0 (logior (ash gen gen-shift) header-word))))
749 (defun write-code-header-words (descriptor boxed unboxed n-fdefns)
750 (declare (ignorable n-fdefns))
751 (let ((total-words (align-up (+ boxed (ceiling unboxed sb-vm:n-word-bytes)) 2)))
752 (write-header-word descriptor
753 (logior (ash total-words sb-vm:code-header-size-shift)
754 sb-vm:code-header-widetag)))
755 (write-wordindexed/raw
756 descriptor 1
757 (logior #+64-bit (ash n-fdefns 32) (* boxed sb-vm:n-word-bytes))))
759 (defun write-header-data+tag (des header-data widetag)
760 (write-header-word des (logior (ash header-data sb-vm:n-widetag-bits)
761 widetag)))
763 (defun get-header-data (object)
764 (ash (read-bits-wordindexed object 0) (- sb-vm:n-widetag-bits)))
766 ;;; There are three kinds of blocks of memory in the type system:
767 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
768 ;;; header as all slots, or almost all slots, are descriptors.
769 ;;; This also includes code objects, which are mostly non-descriptors.
770 ;;; * Unboxed objects (bignums): There is a single header word that contains
771 ;;; the length.
772 ;;; * Vector objects: There is a header word with the type, then a word for
773 ;;; the length, then the data.
774 (defun allocate-object (gspace length lowtag)
775 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
776 pointing to them."
777 (allocate-cold-descriptor gspace (ash length sb-vm:word-shift) lowtag))
778 (defun allocate-otherptr (gspace length widetag)
779 "Allocate LENGTH words in GSPACE and return an ``other-pointer'' descriptor.
780 LENGTH must count the header word itself as 1 word. The header word is
781 initialized with the payload size as (1- LENGTH), and WIDETAG."
782 (let ((des (allocate-cold-descriptor gspace (ash length sb-vm:word-shift)
783 sb-vm:other-pointer-lowtag)))
784 (write-header-word des (sb-vm::compute-object-header length widetag))
785 des))
786 (defvar *simple-vector-0-descriptor*)
787 (defun allocate-vector (widetag length words &optional (gspace *dynamic*))
788 ;; Allocate a vector with WORDS payload words (excluding the header+length).
789 ;; WORDS may be an odd number.
790 ;; Store WIDETAG in the header and LENGTH in the length slot.
791 (when (and (= widetag sb-vm:simple-vector-widetag)
792 (= length 0)
793 (eq gspace *dynamic*)
794 *simple-vector-0-descriptor*)
795 (return-from allocate-vector *simple-vector-0-descriptor*))
796 (emplace-vector (allocate-cold-descriptor
797 gspace
798 (sb-vm:pad-data-block (+ words sb-vm:vector-data-offset))
799 sb-vm:other-pointer-lowtag)
800 widetag length))
801 (defun emplace-vector (des widetag length)
802 #+ubsan
803 (write-header-word des (logior (ash length (+ 32 sb-vm:n-fixnum-tag-bits))
804 widetag))
805 #-ubsan
806 (progn (write-header-data+tag des 0 widetag)
807 (write-wordindexed des sb-vm:vector-length-slot (make-fixnum-descriptor length)))
808 des)
810 ;;; The COLD-LAYOUT is a reflection of or proxy for the words stored
811 ;;; in the core for a cold layout, so that we don't have to extract
812 ;;; them out of the core to compare cold layouts for validity.
813 (defstruct (cold-layout (:constructor %make-cold-layout))
814 id name depthoid length bitmap flags inherits descriptor)
816 ;;; a map from name as a host symbol to the descriptor of its target layout
817 (defvar *cold-layouts*)
818 (defun cold-layout-descriptor-bits (name)
819 (descriptor-bits (cold-layout-descriptor (gethash name *cold-layouts*))))
821 #+compact-instance-header
822 (progn
823 (defun set-simple-fun-layout (fn)
824 (let ((bits (ash (cold-layout-descriptor-bits 'function) 32)))
825 (write-wordindexed/raw fn 0 (logior (read-bits-wordindexed fn 0) bits))))
826 ;; This is called to backpatch layout-of-layout into the primordial layouts.
827 (defun set-instance-layout (thing layout)
828 ;; High half of the header points to the layout
829 (write-wordindexed/raw thing 0 (logior (ash (descriptor-bits layout) 32)
830 (read-bits-wordindexed thing 0))))
831 (defun get-instance-layout (thing)
832 (make-random-descriptor (ash (read-bits-wordindexed thing 0) -32))))
833 #-compact-instance-header
834 (progn
835 (defun set-simple-fun-layout (fn) (declare (ignore fn)))
836 (defun set-instance-layout (thing layout)
837 ;; Word following the header is the layout
838 (write-wordindexed thing sb-vm:instance-slots-offset layout))
839 (defun get-instance-layout (thing)
840 (read-wordindexed thing sb-vm:instance-slots-offset)))
842 ;; Make a structure and set the header word and layout.
843 ;; NWORDS is the payload length (= DD-LENGTH = LAYOUT-LENGTH)
844 (defun allocate-struct (nwords layout &optional (gspace *dynamic*))
845 ;; Add +1 for the header word when allocating.
846 (let ((object (allocate-object gspace (1+ nwords) sb-vm:instance-pointer-lowtag)))
847 ;; Length as stored in the header is the exact number of useful words
848 ;; that follow, as is customary. A padding word, if any is not "useful"
849 (write-header-word object (logior (ash nwords sb-vm:instance-length-shift)
850 sb-vm:instance-widetag))
851 (set-instance-layout object layout)
852 object))
853 (defun type-dd-slots-or-lose (type)
854 (or (car (get type 'dd-proxy)) (error "NO DD-SLOTS: ~S" type)))
855 ;;; Return the value to supply as the first argument to ALLOCATE-STRUCT
856 (defun struct-size (thing)
857 ;; ASSUMPTION: all slots consume 1 storage word
858 (+ sb-vm:instance-data-start (length (type-dd-slots-or-lose thing))))
859 (defun allocate-struct-of-type (type &optional (gspace *dynamic*))
860 (allocate-struct (struct-size type)
861 (cold-layout-descriptor (gethash type *cold-layouts*))
862 gspace))
864 ;;;; copying simple objects into the cold core
866 (defun cold-simple-vector-p (obj)
867 (and (= (descriptor-lowtag obj) sb-vm:other-pointer-lowtag)
868 (= (descriptor-widetag obj) sb-vm:simple-vector-widetag)))
870 (declaim (inline cold-vector-len))
871 (defun cold-vector-len (vector)
872 #+ubsan (ash (read-bits-wordindexed vector 0) (- -32 sb-vm:n-fixnum-tag-bits))
873 #-ubsan (descriptor-fixnum (read-wordindexed vector sb-vm:vector-length-slot)))
875 (macrolet ((vector-data (vector-descriptor)
876 `(+ (descriptor-byte-offset ,vector-descriptor)
877 (* sb-vm:vector-data-offset sb-vm:n-word-bytes))))
878 (defun base-string-to-core (string)
879 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
880 core and return a descriptor to it."
881 ;; (Remember that the system convention for storage of strings leaves an
882 ;; extra null byte at the end to aid in call-out to C.)
883 (let* ((length (length string))
884 (des (allocate-vector sb-vm:simple-base-string-widetag
885 ;; add SAETP-N-PAD-ELEMENT
886 length (ceiling (1+ length) sb-vm:n-word-bytes)
887 *dynamic*))
888 (mem (descriptor-mem des))
889 (byte-base (vector-data des)))
890 (dotimes (i length des) ; was prezeroed, so automatically null-terminated
891 (setf (bvref mem (+ byte-base i)) (char-code (aref string i))))))
893 (defun base-string-from-core (descriptor)
894 (let* ((mem (descriptor-mem descriptor))
895 (byte-base (vector-data descriptor))
896 (len (cold-vector-len descriptor))
897 (str (make-string len)))
898 (dotimes (i len str)
899 (setf (aref str i) (code-char (bvref mem (+ byte-base i)))))))
901 (defun bit-vector-to-core (bit-vector &optional (gspace *dynamic*))
902 (let* ((length (length bit-vector))
903 (nwords (ceiling length sb-vm:n-word-bits))
904 (des (allocate-vector sb-vm:simple-bit-vector-widetag length nwords gspace))
905 (mem (descriptor-mem des))
906 (base (vector-data des)))
907 (let ((byte 0))
908 (dotimes (i length)
909 (let ((byte-bit (rem i 8)))
910 (setf (ldb (byte 1 byte-bit) byte) (bit bit-vector i))
911 (when (= byte-bit 7)
912 (setf (bvref mem (+ base (floor i 8))) byte))))
913 (when (/= 0 (rem length 8))
914 (setf (bvref mem (+ base (floor length 8))) byte))
915 des))))
917 ;;; I would think that all strings we dump are readonly. Maybe not?
918 (defun string-literal-to-core (s) (set-readonly (base-string-to-core s)))
920 ;;; Write the bits of INT to core as if a bignum, i.e. words are ordered from
921 ;;; least to most significant regardless of machine endianness.
922 (defun integer-bits-to-core (int descriptor start nwords)
923 (declare (fixnum nwords))
924 (do ((index 0 (1+ index))
925 (remainder int (ash remainder (- sb-vm:n-word-bits))))
926 ((>= index nwords)
927 (unless (zerop (integer-length remainder))
928 (error "Nonzero remainder after writing ~D using ~D words" int nwords)))
929 (write-wordindexed/raw descriptor
930 (+ start index)
931 (logand remainder sb-ext:most-positive-word))))
933 (defun bignum-to-core (n &optional (space *dynamic*))
934 "Copy a bignum to the cold core."
935 (let* ((words (ceiling (1+ (integer-length n)) sb-vm:n-word-bits))
936 (handle
937 #-bignum-assertions (allocate-otherptr space (1+ words) sb-vm:bignum-widetag)
938 #+bignum-assertions
939 (let* ((aligned-words (1+ (logior words 1))) ; round to odd, slap on a header
940 (physical-words (* aligned-words 2))
941 (handle (allocate-otherptr space physical-words sb-vm:bignum-widetag)))
942 ;; rewrite the header to indicate the logical size
943 (write-wordindexed/raw handle 0 (logior (ash words 8) sb-vm:bignum-widetag))
944 handle)))
945 (integer-bits-to-core n handle sb-vm:bignum-digits-offset words)
946 (aver (= (bignum-from-core handle) n))
947 handle))
949 (defun bignum-from-core (descriptor)
950 (let ((n-words (logand (get-header-data descriptor) #x7fffff))
951 (val 0))
952 (dotimes (i n-words val)
953 (let ((bits (read-bits-wordindexed descriptor
954 (+ i sb-vm:bignum-digits-offset))))
955 ;; sign-extend the highest word
956 (when (= i (1- n-words))
957 (setq bits (sb-vm::sign-extend bits sb-vm:n-word-bits)))
958 (setq val (logior (ash bits (* i sb-vm:n-word-bits)) val))))))
960 (defun number-pair-to-core (first second type)
961 "Makes a number pair of TYPE (ratio or complex) and fills it in."
962 (let ((des (allocate-otherptr *dynamic* 3 type)))
963 (write-wordindexed des 1 first)
964 (write-wordindexed des 2 second)
965 des))
967 (defun write-double-float-bits (address index x)
968 #-64-bit
969 (let ((high-bits (double-float-high-bits x))
970 (low-bits (double-float-low-bits x)))
971 #+little-endian
972 (progn (write-wordindexed/raw address index low-bits)
973 (write-wordindexed/raw address (1+ index) high-bits))
974 #+big-endian
975 (progn (write-wordindexed/raw address index high-bits)
976 (write-wordindexed/raw address (1+ index) low-bits)))
977 #+64-bit
978 (write-wordindexed/raw address index (double-float-bits x))
979 address)
981 (defun float-to-core (x)
982 (ecase (sb-impl::flonum-format x)
983 (single-float
984 (let ((bits (single-float-bits x)))
985 #+64-bit ; 64-bit platforms have immediate single-floats
986 (make-random-descriptor (logior (ash bits 32) sb-vm:single-float-widetag))
987 #-64-bit
988 (let ((des (allocate-otherptr *dynamic* sb-vm:single-float-size
989 sb-vm:single-float-widetag)))
990 (write-wordindexed/raw des sb-vm:single-float-value-slot bits)
991 des)))
992 (double-float
993 (let ((des (allocate-otherptr *dynamic* sb-vm:double-float-size
994 sb-vm:double-float-widetag)))
995 (write-double-float-bits des sb-vm:double-float-value-slot x)))))
997 (defun unsigned-bits-to-single-float (bits)
998 (sb-impl::make-flonum (sb-vm::sign-extend bits 32) 'single-float))
999 (defun double-float-from-core (des)
1000 (let ((bits
1001 #+64-bit (read-bits-wordindexed des 1)
1002 #-64-bit (let* ((word0 (read-bits-wordindexed
1003 des sb-vm:double-float-value-slot))
1004 (word1 (read-bits-wordindexed
1005 des (1+ sb-vm:double-float-value-slot))))
1006 #+little-endian (logior (ash word1 32) word0)
1007 #+big-endian (logior (ash word0 32) word1))))
1008 (sb-impl::make-flonum (sb-vm::sign-extend bits 64) 'double-float)))
1010 (defun complexnum-to-core (num &aux (r (realpart num)) (i (imagpart num)))
1011 (if (rationalp r)
1012 (number-pair-to-core (number-to-core r) (number-to-core i) sb-vm:complex-rational-widetag)
1013 (ecase (sb-impl::flonum-format r)
1014 (single-float
1015 (let* ((des (allocate-otherptr *dynamic* sb-vm:complex-single-float-size
1016 sb-vm:complex-single-float-widetag))
1017 (where (+ (descriptor-byte-offset des)
1018 (ash #+64-bit sb-vm:complex-single-float-data-slot
1019 #-64-bit sb-vm:complex-single-float-real-slot
1020 sb-vm:word-shift))))
1021 (setf (bvref-s32 (descriptor-mem des) where) (single-float-bits r)
1022 (bvref-s32 (descriptor-mem des) (+ where 4)) (single-float-bits i))
1023 des))
1024 (double-float
1025 (let ((des (allocate-otherptr *dynamic* sb-vm:complex-double-float-size
1026 sb-vm:complex-double-float-widetag)))
1027 (write-double-float-bits des sb-vm:complex-double-float-real-slot r)
1028 (write-double-float-bits des sb-vm:complex-double-float-imag-slot i)
1029 des)))))
1031 ;;; Copy the given number to the core.
1032 (defun number-to-core (number)
1033 (typecase number
1034 (integer (or (%fixnum-descriptor-if-possible number)
1035 (bignum-to-core number)))
1036 (ratio (number-pair-to-core (number-to-core (numerator number))
1037 (number-to-core (denominator number))
1038 sb-vm:ratio-widetag))
1039 (float (float-to-core number))
1040 (complex (complexnum-to-core number))
1041 (t (error "~S isn't a cold-loadable number at all!" number))))
1043 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
1044 (defun cold-cons (car cdr &optional (gspace *dynamic*))
1045 (let ((cons (allocate-cold-descriptor gspace (ash 2 sb-vm:word-shift)
1046 sb-vm:list-pointer-lowtag :list)))
1047 (let* ((objs (gspace-objects gspace))
1048 (n (1- (length objs))))
1049 (when objs
1050 (setf (aref objs n) (list (aref objs n)))))
1051 (write-wordindexed cons sb-vm:cons-car-slot car)
1052 (write-wordindexed cons sb-vm:cons-cdr-slot cdr)
1053 cons))
1054 (defun list-to-core (list)
1055 (let ((head *nil-descriptor*)
1056 (tail nil))
1057 ;; A recursive algorithm would have the first cons at the highest
1058 ;; address. This way looks nicer when viewed in ldb.
1059 (loop
1060 (unless list (return head))
1061 (let ((cons (cold-cons (pop list) *nil-descriptor*)))
1062 (if tail (cold-rplacd tail cons) (setq head cons))
1063 (setq tail cons)))))
1064 (defun cold-list (&rest args) (list-to-core args))
1065 (defun cold-list-length (list) ; but no circularity detection
1066 ;; a recursive implementation uses too much stack for some Lisps
1067 (let ((n 0))
1068 (loop (if (cold-null list) (return n))
1069 (incf n)
1070 (setq list (cold-cdr list)))))
1071 (defun cold-push (item symbol)
1072 (cold-set symbol (cold-cons item (cold-symbol-value symbol))))
1074 ;;; Make a simple-vector on the target that holds the specified
1075 ;;; OBJECTS, and return its descriptor.
1076 ;;; This is really "vectorify-list-into-core" but that's too wordy,
1077 ;;; so historically it was "vector-in-core" which is a fine name.
1078 (defun vector-in-core (objects &optional (gspace *dynamic*))
1079 (let* ((size (length objects))
1080 (result (allocate-vector sb-vm:simple-vector-widetag size size gspace)))
1081 (dotimes (index size result)
1082 (write-wordindexed result (+ index sb-vm:vector-data-offset)
1083 (pop objects)))))
1085 (defun word-vector (objects &optional (gspace *dynamic*))
1086 (let* ((size (length objects))
1087 (result (allocate-vector #+64-bit sb-vm:simple-array-unsigned-byte-64-widetag
1088 #-64-bit sb-vm:simple-array-unsigned-byte-32-widetag
1089 size size gspace)))
1090 (dotimes (index size result)
1091 (write-wordindexed/raw result (+ index sb-vm:vector-data-offset) (pop objects)))))
1093 (defun cold-svset (vector index value)
1094 (let ((i (if (integerp index) index (descriptor-fixnum index))))
1095 (write-wordindexed vector (+ i sb-vm:vector-data-offset) value))
1096 value)
1098 (declaim (inline cold-svref))
1099 (defun cold-svref (vector i)
1100 (declare (type index i))
1101 (aver (< i (cold-vector-len vector)))
1102 (read-wordindexed vector (+ i sb-vm:vector-data-offset)))
1103 (defun vector-from-core (descriptor &optional (transform #'identity))
1104 (let* ((len (cold-vector-len descriptor))
1105 (vector (make-array len)))
1106 (dotimes (i len vector)
1107 (setf (aref vector i) (funcall transform (cold-svref descriptor i))))))
1109 ;;;; symbol magic
1111 (defvar *tls-index-to-symbol*)
1112 #+sb-thread
1113 (progn
1114 ;; Simulate *FREE-TLS-INDEX*. This is a word count, not a displacement.
1115 (defvar *genesis-tls-counter* sb-vm::primitive-thread-object-length)
1116 ;; Assign SYMBOL the tls-index INDEX. SYMBOL must be a descriptor.
1117 ;; This is a backend support routine, but the style within this file
1118 ;; is to conditionalize by the target features.
1119 (defun cold-assign-tls-index (symbol index)
1120 (push (list index (warm-symbol symbol)) *tls-index-to-symbol*)
1121 #+64-bit
1122 (write-wordindexed/raw
1123 symbol 0 (logior (ash index 32) (read-bits-wordindexed symbol 0)))
1124 #-64-bit
1125 (write-wordindexed/raw symbol sb-vm:symbol-tls-index-slot index))
1127 ;; Return SYMBOL's tls-index,
1128 ;; choosing a new index if it doesn't have one yet.
1129 (defun ensure-symbol-tls-index (symbol)
1130 (let* ((cold-sym (cold-intern symbol))
1131 (tls-index #+64-bit (ldb (byte 32 32) (read-bits-wordindexed cold-sym 0))
1132 #-64-bit (read-bits-wordindexed cold-sym sb-vm:symbol-tls-index-slot)))
1133 (unless (plusp tls-index)
1134 (let ((next (prog1 *genesis-tls-counter* (incf *genesis-tls-counter*))))
1135 (setq tls-index (ash next sb-vm:word-shift))
1136 (cold-assign-tls-index cold-sym tls-index)))
1137 tls-index)))
1139 (defvar *cold-symbol-gspace* (or #+immobile-space '*immobile-fixedobj* '*dynamic*))
1140 (defun encode-symbol-name (package-id name)
1141 (declare (ignorable package-id))
1142 (logior #+compact-symbol (ash package-id sb-impl::symbol-name-bits)
1143 (descriptor-bits name)))
1145 (defun assign-symbol-hash (descriptor wordindex name)
1146 ;; "why not just call sb-c::symbol-name-hash?" you ask? because: no symbol.
1147 (let ((name-hash (sb-c::calc-symbol-name-hash name (length name))))
1148 #-salted-symbol-hash
1149 (write-wordindexed descriptor wordindex (make-fixnum-descriptor name-hash))
1150 #+salted-symbol-hash
1151 (let* ((salt (sb-impl::murmur3-fmix-word (descriptor-bits descriptor)))
1152 (prng-byte sb-impl::symbol-hash-prng-byte)
1153 ;; 64-bit: Low 4 bytes to high 4 bytes of slot
1154 ;; 32-bit: name-hash to high 29 bits
1155 ;; plus salt the hash any way you want as long as the build is reproducible.
1156 (name-hash-pos (+ (byte-size prng-byte) (byte-position prng-byte)))
1157 (hash (logior (ash name-hash name-hash-pos) (mask-field prng-byte salt))))
1158 (write-wordindexed/raw descriptor wordindex hash))))
1160 ;;; Allocate (and initialize) a symbol.
1161 ;;; Even though all symbols are the same size now, I still envision the possibility
1162 ;;; of reducing gensyms to 4 words, though I'm not sure what to do if information
1163 ;;; is later attached (function, value, plist)
1164 (defun allocate-symbol (size cold-package name &key (gspace (symbol-value *cold-symbol-gspace*)))
1165 (declare (simple-string name))
1166 (let ((symbol (allocate-otherptr gspace size sb-vm:symbol-widetag)))
1167 (when core-file-name
1168 (let* ((cold-name (string-literal-to-core name))
1169 (pkg-id (if cold-package
1170 (descriptor-fixnum (read-slot cold-package :id))
1171 sb-impl::+package-id-none+)))
1172 (assign-symbol-hash symbol sb-vm:symbol-hash-slot name)
1173 (write-wordindexed symbol sb-vm:symbol-value-slot *unbound-marker*)
1174 (write-wordindexed symbol sb-vm:symbol-info-slot *nil-descriptor*)
1175 (write-wordindexed/raw symbol sb-vm:symbol-name-slot
1176 (encode-symbol-name pkg-id cold-name))
1177 #-compact-symbol (write-wordindexed symbol sb-vm:symbol-package-id-slot
1178 (make-fixnum-descriptor pkg-id))))
1179 symbol))
1181 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
1182 ;;; descriptor of a cold symbol or (in an abbreviation for the
1183 ;;; most common usage pattern) an ordinary symbol, which will be
1184 ;;; automatically cold-interned.
1185 (defun cold-set (symbol-or-symbol-des value)
1186 (let ((symbol-des (etypecase symbol-or-symbol-des
1187 (descriptor symbol-or-symbol-des)
1188 (symbol (cold-intern symbol-or-symbol-des)))))
1189 (write-wordindexed symbol-des sb-vm:symbol-value-slot value)))
1190 (defun cold-symbol-value (symbol)
1191 (let ((val (read-wordindexed (cold-intern symbol) sb-vm:symbol-value-slot)))
1192 (if (= (descriptor-bits val) sb-vm:unbound-marker-widetag)
1193 (error "Symbol value of ~a is unbound." symbol)
1194 val)))
1195 (defun cold-fdefn-fun (cold-fdefn)
1196 (read-wordindexed cold-fdefn sb-vm:fdefn-fun-slot))
1199 ;;;; layouts and type system pre-initialization
1201 ;;; Since we want to be able to dump structure constants and
1202 ;;; predicates with reference layouts, we need to create layouts at
1203 ;;; cold-load time. We use the name to intern layouts by, and dump a
1204 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
1205 ;;; initialization can find them. The only thing that's tricky [sic --
1206 ;;; WHN 19990816] is initializing layout's layout, which must point to
1207 ;;; itself.
1209 ;;; a map from DESCRIPTOR-BITS of cold layouts (as descriptors)
1210 ;;; to the host's COLD-LAYOUT proxy for that layout.
1211 (defvar *cold-layout-by-addr*)
1213 ;;; Initial methods require that we sort possible methods by the depthoid.
1214 ;;; Most of the objects printed in cold-init are ordered hierarchically in our
1215 ;;; type lattice; the major exceptions are ARRAY and VECTOR at depthoid -1.
1216 ;;; Of course we need to print VECTORs because a STRING is a vector,
1217 ;;; and vector has to precede ARRAY. Kludge it for now.
1218 (defun class-depthoid (class-name) ; DEPTHOID-ish thing, any which way you can
1219 (case class-name
1220 (vector 1/2)
1221 (array 1/4)
1222 ;; The depthoid of CONDITION has to be faked. The proper value is 1.
1223 ;; But STRUCTURE-OBJECT is also at depthoid 1, and its predicate
1224 ;; is %INSTANCEP (which is too weak), so to select the correct method
1225 ;; we have to make CONDITION more specific.
1226 ;; In reality it is type disjoint from structure-object.
1227 (condition 2)
1229 (acond ((gethash class-name *cold-layouts*)
1230 (cold-layout-depthoid it))
1231 ((info :type :compiler-layout class-name)
1232 (layout-depthoid it))
1234 (error "Unknown depthoid for ~S" class-name))))))
1236 (declaim (ftype function read-slot %write-slots write-slots))
1237 (flet ((infer-metadata (x)
1238 (type-dd-slots-or-lose
1239 (cold-layout-name (gethash (descriptor-bits (get-instance-layout x))
1240 *cold-layout-by-addr*))))
1241 (find-slot (slots initarg)
1242 (let ((dsd (or (find initarg slots
1243 :test (lambda (x y) (eq x (keywordicate (dsd-name y)))))
1244 (error "No slot for ~S in ~S" initarg slots))))
1245 (values (+ sb-vm:instance-slots-offset (dsd-index dsd))
1246 (dsd-raw-type dsd)))))
1248 (defun %write-slots (metadata cold-object &rest assignments)
1249 (aver (evenp (length assignments)))
1250 (loop for (initarg value) on assignments by #'cddr
1251 do (multiple-value-bind (index repr) (find-slot metadata initarg)
1252 (ecase repr
1253 ((t) (write-wordindexed cold-object index value))
1254 ((word sb-vm:signed-word)
1255 (write-wordindexed/raw cold-object index value)))))
1256 cold-object)
1258 (defun write-slots (cold-object &rest assignments)
1259 (apply #'%write-slots (infer-metadata cold-object) cold-object assignments))
1261 ;; For symmetry, the reader takes an initarg, not a slot name.
1262 (defun read-slot (cold-object slot-initarg)
1263 (multiple-value-bind (index repr)
1264 (find-slot (infer-metadata cold-object) slot-initarg)
1265 (ecase repr
1266 ((t) (read-wordindexed cold-object index))
1267 (word (read-bits-wordindexed cold-object index))
1268 (sb-vm:signed-word
1269 (sb-vm::sign-extend (read-bits-wordindexed cold-object index)
1270 sb-vm:n-word-bits))))))
1272 (defun read-structure-definitions (pathname)
1273 (with-open-file (stream pathname)
1274 (loop
1275 (let ((ch (peek-char t stream)))
1276 (when (char= ch #\;)
1277 (return))
1278 (let* ((classoid-name (read stream))
1279 (*package* (find-package (cl:symbol-package classoid-name)))
1280 (flags+depthoid+inherits (read stream)))
1281 (setf (get classoid-name 'dd-proxy)
1282 (list (map 'vector
1283 (lambda (x)
1284 (destructuring-bind (bits name acc) x
1285 (sb-kernel::make-dsd name nil acc bits nil)))
1286 (read stream))
1287 :flags (car flags+depthoid+inherits)
1288 :depthoid (cadr flags+depthoid+inherits)
1289 :inherits (cddr flags+depthoid+inherits))))))))
1291 (defvar *vacuous-slot-table*)
1292 (defun cold-layout-gspace ()
1293 (cond ((boundp '*permgen*) *permgen*)
1294 ((boundp '*immobile-fixedobj*) *immobile-fixedobj*)
1295 (t *dynamic*)))
1296 (declaim (ftype (function (symbol layout-depthoid integer index integer descriptor)
1297 descriptor)
1298 make-cold-layout))
1300 (defun make-cold-layout (name depthoid flags length bitmap inherits)
1301 ;; Layouts created in genesis can't vary in length due to the number of ancestor
1302 ;; types in the IS-A vector. They may vary in length due to the bitmap word count.
1303 ;; But we can at least assert that there is one less thing to worry about.
1304 (aver (<= depthoid sb-kernel::layout-id-vector-fixed-capacity))
1305 (aver (cold-simple-vector-p inherits))
1306 (let* ((fixed-words (sb-kernel::type-dd-length layout))
1307 (bitmap-words (ceiling (1+ (integer-length bitmap)) sb-vm:n-word-bits))
1308 (result (allocate-struct (+ fixed-words bitmap-words)
1309 (or (awhen (gethash 'layout *cold-layouts*)
1310 (cold-layout-descriptor it))
1311 (make-fixnum-descriptor 0))
1312 (cold-layout-gspace)))
1313 (this-id (sb-kernel::choose-layout-id name (logtest flags +condition-layout-flag+)))
1314 (hash (make-fixnum-descriptor (sb-impl::hash-layout-name name))))
1316 (let ((proxy (%make-cold-layout :id this-id
1317 :name name
1318 :depthoid depthoid
1319 :length length
1320 :bitmap bitmap
1321 :flags flags
1322 :inherits inherits
1323 :descriptor result)))
1324 ;; Make two different ways to look up the proxy object -
1325 ;; by name or by descriptor-bits.
1326 (setf (gethash (descriptor-bits result) *cold-layout-by-addr*) proxy
1327 (gethash name *cold-layouts*) proxy))
1328 (unless core-file-name (return-from make-cold-layout result))
1330 ;; Can't use the easier WRITE-SLOTS unfortunately because bootstrapping is hard
1331 (let ((layout-metadata (type-dd-slots-or-lose 'layout)))
1333 #+64-bit
1334 (%write-slots layout-metadata result
1335 :flags (sb-kernel::pack-layout-flags depthoid length flags))
1336 #-64-bit
1337 (%write-slots layout-metadata result
1338 :depthoid (make-fixnum-descriptor depthoid)
1339 :length (make-fixnum-descriptor length)
1340 :flags flags)
1342 (%write-slots layout-metadata result
1343 :clos-hash hash
1344 :invalid *nil-descriptor*
1345 :inherits inherits
1346 :%info *nil-descriptor*)
1348 (when (member name '(null list symbol pathname))
1349 ;; Assign an empty slot-table. Why this is done only for four
1350 ;; classoids is ... too complicated to explain here in a few words,
1351 ;; but revision 18c239205d9349abc017b07e7894a710835c5205 broke it.
1352 ;; Keep this in sync with MAKE-SLOT-TABLE in pcl/slots-boot.
1353 (%write-slots layout-metadata result
1354 :slot-table (if (boundp '*vacuous-slot-table*)
1355 *vacuous-slot-table*
1356 (setq *vacuous-slot-table*
1357 (host-constant-to-core '#(1 nil))))))
1359 (let ((byte-offset (+ (descriptor-byte-offset result) (sb-vm::id-bits-offset))))
1360 (when (logtest flags +structure-layout-flag+)
1361 (loop for i from 2 below (cold-vector-len inherits)
1362 do (setf (bvref-s32 (descriptor-mem result) byte-offset)
1363 (cold-layout-id (gethash (descriptor-bits (cold-svref inherits i))
1364 *cold-layout-by-addr*)))
1365 (incf byte-offset 4)))
1366 (setf (bvref-s32 (descriptor-mem result) byte-offset) this-id)))
1368 (integer-bits-to-core bitmap result (1+ fixed-words) bitmap-words)
1370 result))
1372 (defun predicate-for-specializer (type-name)
1373 (let ((classoid (find-classoid type-name nil)))
1374 (typecase classoid
1375 (structure-classoid
1376 (dd-predicate-name (sb-kernel::layout-%info (classoid-layout classoid))))
1377 (built-in-classoid
1378 (let ((translation (specifier-type type-name)))
1379 (aver (not (contains-unknown-type-p translation)))
1380 (let ((predicate (find translation sb-c::*backend-type-predicates*
1381 :test #'type= :key #'car)))
1382 (cond (predicate (cdr predicate))
1383 ((eq type-name 'stream) 'streamp)
1384 ((eq type-name 'pathname) 'pathnamep)
1385 ((eq type-name 't) 'constantly-t)
1386 (t (error "No predicate for builtin: ~S" type-name)))))))))
1388 ;;; Map from host object to target object
1389 (defvar *host->cold-ctype*)
1391 ;;; NUMTYPE-ASPECTS are stored in a fixed-size vector.
1392 ;;; During genesis they are created on demand.
1393 ;;; (I'm not sure whether all or only some are created)
1394 (defun numtype-aspects-to-core (val)
1395 (let* ((index (sb-kernel::numtype-aspects-id val))
1396 (vector (cold-symbol-value 'sb-kernel::*numeric-aspects-v*))
1397 (cold-obj (cold-svref vector index)))
1398 (if (eql (descriptor-bits cold-obj) 0)
1399 (write-slots (cold-svset vector index
1400 (allocate-struct-of-type (type-of val)))
1401 :id (make-fixnum-descriptor (sb-kernel::numtype-aspects-id val))
1402 :complexp (sb-kernel::numtype-aspects-complexp val)
1403 :class (sb-kernel::numtype-aspects-class val)
1404 :precision (sb-kernel::numtype-aspects-precision val))
1405 cold-obj)))
1407 (defvar *dsd-index-cache* nil)
1408 (defun dsd-index-cached (type-name slot-name)
1409 (let ((cell (find-if (lambda (x)
1410 (and (eq (caar x) type-name) (eq (cdar x) slot-name)))
1411 *dsd-index-cache*)))
1412 (if cell
1413 (cdr cell)
1414 (let* ((dd-slots (car (get type-name 'dd-proxy)))
1415 (dsd (find slot-name dd-slots :key #'dsd-name))
1416 (index (dsd-index dsd)))
1417 (push (cons (cons type-name slot-name) index) *dsd-index-cache*)
1418 index))))
1420 (defun ctype-to-core (obj)
1421 (declare (type (or ctype xset list) obj))
1422 (cond
1423 ((null obj) *nil-descriptor*)
1424 ((gethash obj *host->cold-ctype*))
1425 ((listp obj)
1426 (if (and (proper-list-p obj) (every #'sb-kernel:ctype-p obj))
1427 ;; Be sure to preserving shared substructure.
1428 ;; There is no circularity, so inserting into the map after copying works fine
1429 (setf (gethash obj *host->cold-ctype*) (list-to-core (mapcar #'ctype-to-core obj)))
1430 (host-constant-to-core obj))) ; numeric bound, array dimension, etc
1432 (when (classoid-p obj) (aver (not (sb-kernel::undefined-classoid-p obj))))
1433 (let* ((host-type (type-of obj))
1434 ;; Precompute a list of slots that should be initialized to a
1435 ;; trivially dumpable constant in lieu of whatever complicated
1436 ;; substructure it currently holds.
1437 (overrides
1438 (typecase obj
1439 (classoid
1440 (let ((slots-to-omit
1441 `(;; :predicate will be patched in during cold init.
1442 (,(dsd-index-cached 'built-in-classoid 'sb-kernel::predicate) .
1443 ,(make-random-descriptor sb-vm:unbound-marker-widetag))
1444 (,(dsd-index-cached 'classoid 'sb-kernel::subclasses) . nil)
1445 ;; Even though (gethash (classoid-name obj) *cold-layouts*) may exist,
1446 ;; we nonetheless must set LAYOUT to NIL or else warm build fails
1447 ;; in the twisty maze of class initializations.
1448 (,(dsd-index-cached 'classoid 'layout) . nil))))
1449 (if (typep obj 'built-in-classoid)
1450 slots-to-omit
1451 ;; :predicate is not a slot. Don't mess up the object
1452 ;; by omitting a slot at the same index as it.
1453 (cdr slots-to-omit))))))
1454 (dd-slots (type-dd-slots-or-lose host-type))
1455 ;; ASSUMPTION: all slots consume 1 storage word
1456 (dd-len (+ sb-vm:instance-data-start (length dd-slots)))
1457 (result (allocate-struct-of-type host-type)))
1458 (setf (gethash obj *host->cold-ctype*) result) ; record it
1459 ;; Dump the slots.
1460 (do ((index sb-vm:instance-data-start (1+ index)))
1461 ((= index dd-len) result)
1462 (let* ((dsd (find index dd-slots :key #'dsd-index))
1463 (override (assq index overrides))
1464 (reader (dsd-accessor-name dsd)))
1465 (ecase (dsd-raw-type dsd)
1466 ((t)
1467 (write-wordindexed
1468 result
1469 (+ sb-vm:instance-slots-offset index)
1470 (if override
1471 (or (cdr override) *nil-descriptor*)
1472 (let ((val (funcall reader obj)))
1473 (funcall (typecase val
1474 ((or ctype xset list) #'ctype-to-core)
1475 (sb-kernel::numtype-aspects #'numtype-aspects-to-core)
1476 (t #'host-constant-to-core))
1477 val)))))
1478 ((word sb-vm:signed-word)
1479 (write-wordindexed/raw result (+ sb-vm:instance-slots-offset index)
1480 (or (cdr override) (funcall reader obj)))))))
1481 (cond ((classoid-p obj) ; Place classoid into its classoid-cell.
1482 (let ((cell (cold-find-classoid-cell (classoid-name obj) :create t)))
1483 (write-slots cell :classoid result)))
1484 ((ctype-p obj)
1485 ;; If OBJ belongs in a hash container, then deduce which
1486 (let* ((hashset (sb-kernel::ctype->hashset-sym obj))
1487 (preload
1488 (cond ((and hashset (hashset-find (symbol-value hashset) obj))
1489 hashset)
1490 ((and (member-type-p obj)
1491 ;; NULL is a hardwired case in the MEMBER type constructor
1492 (neq obj (specifier-type 'null))
1493 (type-singleton-p obj))
1494 'sb-kernel::*eql-type-cache*))))
1495 (when preload ; Record it
1496 (cold-push (cold-cons result preload) 'sb-kernel::*!initial-ctypes*)))))
1497 result))))
1499 (defun initialize-layouts ()
1500 (flet ((chill-layout (name &rest inherits)
1501 ;; Check that the number of specified INHERITS matches
1502 ;; the length of the layout's inherits in the cross-compiler.
1503 (let ((warm-layout (info :type :compiler-layout name)))
1504 (assert (eql (length (layout-inherits warm-layout))
1505 (length inherits)))
1506 (make-cold-layout name
1507 (layout-depthoid warm-layout)
1508 (layout-flags warm-layout)
1509 (layout-length warm-layout)
1510 (layout-bitmap warm-layout)
1511 (vector-in-core inherits)))))
1512 (let* ((t-layout (chill-layout 't))
1513 (s-o-layout (chill-layout 'structure-object t-layout))
1514 (layout-layout (chill-layout 'layout t-layout s-o-layout)))
1515 (when core-file-name
1516 (dolist (instance (list t-layout s-o-layout layout-layout))
1517 (set-instance-layout instance layout-layout)))
1518 (chill-layout 'function t-layout)
1519 (chill-layout 'package t-layout s-o-layout)
1520 (let* ((sequence (chill-layout 'sequence t-layout))
1521 (list (chill-layout 'list t-layout sequence))
1522 (symbol (chill-layout 'symbol t-layout)))
1523 (chill-layout 'null t-layout sequence list symbol))
1524 (chill-layout 'sb-lockless::list-node t-layout s-o-layout)
1525 (chill-layout 'stream t-layout))))
1527 ;;;; interning symbols in the cold image
1529 ;;; a map from package name as a host string to
1530 ;;; ((external-symbols . internal-symbols) . cold-package-descriptor)
1531 (defvar *cold-package-symbols*)
1532 (declaim (type hash-table *cold-package-symbols*))
1533 (defvar *package-graph*)
1535 ;;; preincrement on use. the first non-preassigned ID is 5
1536 (defvar *package-id-count* 4)
1538 ;;; Initialize the cold package named by NAME. The information is
1539 ;;; usually derived from the host package of the same name, except
1540 ;;; where the host package does not reflect the target package
1541 ;;; information, as for COMMON-LISP, KEYWORD, and COMMON-LISP-USER.
1542 (defun initialize-cold-package (cold-package name)
1543 (multiple-value-bind (nicknames docstring id shadow use-list)
1544 (cond ((string= name "COMMON-LISP")
1545 (values '("CL")
1546 "public: home of symbols defined by the ANSI language specification"
1547 sb-impl::+package-id-lisp+
1549 '()))
1550 ((string= name "KEYWORD")
1551 (values '()
1552 "public: home of keywords"
1553 sb-impl::+package-id-keyword+
1555 '()))
1556 ((string= name "COMMON-LISP-USER")
1557 (values '("CL-USER")
1558 "public: the default package for user code and data"
1559 sb-impl::+package-id-user+
1561 ;; ANSI encourages us to put extension packages in
1562 ;; the USE list of COMMON-LISP-USER.
1563 '("COMMON-LISP" "SB-ALIEN" "SB-DEBUG"
1564 "SB-EXT" "SB-GRAY" "SB-PROFILE")))
1566 (let ((package (find-package name)))
1567 (values (package-nicknames package)
1568 (documentation package t)
1569 (if (string= name "SB-KERNEL")
1570 sb-impl::+package-id-kernel+
1571 (incf *package-id-count*))
1572 (sort (package-shadowing-symbols package) #'string<)
1573 ;; SB-COREFILE is not actually part of
1574 ;; the use list for SB-FASL. It's
1575 ;; just needed for Genesis.
1576 (if (string= name "SB-FASL")
1577 (remove (find-package "SB-COREFILE")
1578 (package-use-list package))
1579 (package-use-list package))))))
1580 (let ((strings (mapcar #'string-literal-to-core (cons name nicknames))))
1581 (write-slots cold-package
1582 :id (make-fixnum-descriptor id)
1583 :keys (vector-in-core (list (list-to-core strings)))
1584 :%name *nil-descriptor*
1585 :%bits (make-fixnum-descriptor
1586 (if (system-package-p name)
1587 sb-impl::+initial-package-bits+
1589 :doc-string (if (and docstring #-sb-doc nil)
1590 (string-literal-to-core docstring)
1591 *nil-descriptor*)))
1592 (push (cons name (sort (mapcar 'sb-xc:package-name use-list) #'string<)) *package-graph*)
1593 ;; COLD-INTERN AVERs that the package has an ID, so delay writing
1594 ;; the shadowing-symbols until the package is ready.
1595 (write-slots cold-package
1596 :%shadowing-symbols (list-to-core
1597 (mapcar 'cold-intern shadow)))))
1599 (defun cold-find-package-info (package-name)
1600 ;; Create package info on demand.
1601 (or (gethash package-name *cold-package-symbols*)
1602 (let* ((cold-package (allocate-struct-of-type 'package))
1603 (info (cons (cons nil nil) cold-package)))
1604 (write-slots cold-package :%used-by *nil-descriptor*)
1605 (setf (gethash package-name *cold-package-symbols*) info)
1606 (initialize-cold-package cold-package package-name)
1607 info)))
1609 (defvar *classoid-cells*)
1610 (defun cold-find-classoid-cell (name &key create)
1611 (aver (eq create t))
1612 (or (gethash name *classoid-cells*)
1613 (setf (gethash name *classoid-cells*)
1614 (write-slots (allocate-struct-of-type 'sb-kernel::classoid-cell)
1615 :name name
1616 :pcl-class *nil-descriptor*
1617 :classoid *nil-descriptor*))))
1619 ;;; a map from descriptors to symbols, so that we can back up. The key
1620 ;;; is the address in the target core.
1621 (defvar *cold-symbols*)
1622 (declaim (type hash-table *cold-symbols*))
1624 (defun set-readonly (vector)
1625 (write-wordindexed/raw vector 0 (logior (read-bits-wordindexed vector 0)
1626 (ash sb-vm:+vector-shareable+
1627 sb-vm:array-flags-position)))
1628 vector)
1630 (defvar *uninterned-symbol-table* (make-hash-table :test #'equal))
1631 ;; This coalesces references to uninterned symbols, which is allowed because
1632 ;; "similar-as-constant" is defined by string comparison, and since we only have
1633 ;; base-strings during Genesis, there is no concern about upgraded array type.
1634 ;; There is a subtlety of whether coalescing may occur across files
1635 ;; - the target compiler doesn't and couldn't - but here it doesn't matter.
1636 (defun get-uninterned-symbol (name)
1637 (ensure-gethash name *uninterned-symbol-table*
1638 (allocate-symbol sb-vm:symbol-size nil name)))
1640 ;;; Dump the target representation of HOST-VALUE,
1641 ;;; the type of which is in a restrictive set.
1642 (defun host-constant-to-core (host-value &optional helper)
1643 (let ((visited (make-hash-table :test #'eq)))
1644 (named-let target-representation ((value host-value))
1645 (unless (typep value '(or symbol number descriptor))
1646 (let ((found (gethash value visited)))
1647 (cond ((eq found :pending)
1648 (bug "circular constant?")) ; Circularity not permitted
1649 (found
1650 (return-from target-representation found))))
1651 (setf (gethash value visited) :pending))
1652 (setf (gethash value visited)
1653 (typecase value
1654 (descriptor value)
1655 (symbol (if (cl:symbol-package value)
1656 (cold-intern value)
1657 (get-uninterned-symbol (string value))))
1658 (number (number-to-core value))
1659 (string (base-string-to-core value))
1660 (simple-bit-vector (bit-vector-to-core value))
1661 (cons (cold-cons (target-representation (car value))
1662 (target-representation (cdr value))))
1663 (simple-vector
1664 (vector-in-core (map 'list #'target-representation value)))
1666 (or (and helper (funcall helper value))
1667 (error "host-constant-to-core: can't convert ~S"
1668 value))))))))
1670 ;; Look up the target's descriptor for #'FUN where FUN is a host symbol.
1671 (defun cold-symbol-function (symbol &optional (errorp t))
1672 (let* ((symbol (if (symbolp symbol)
1673 symbol
1674 (warm-symbol symbol)))
1675 (f (cold-fdefn-fun (ensure-cold-fdefn symbol))))
1676 (cond ((not (cold-null f)) f)
1677 (errorp (error "Expected a definition for ~S in cold load" symbol))
1678 (t nil))))
1680 ;;; Return a handle on an interned symbol. If necessary allocate the
1681 ;;; symbol and record its home package.
1682 (defun cold-intern (symbol
1683 &key (access nil)
1684 (gspace (symbol-value *cold-symbol-gspace*))
1685 &aux (name (symbol-name symbol))
1686 (package (sb-xc:symbol-package symbol)))
1687 ;; Symbols that are logically in COMMON-LISP but accessed through the SB-XC package
1688 ;; need to be re-interned since the cold-intern-info must be associated with
1689 ;; exactly one of the possible lookalikes, not both. The re-interned symbol
1690 ;; is usually homed in CL:, but might be homed in SB-XC. When the symbols identity
1691 ;; matters to the type system (floating-point specifiers), we never want to see the
1692 ;; host's symbol; the canonical package shall be SB-XC. We can figure out the
1693 ;; canonical home package by finding the symbol via the XC-STRICT-CL package.
1694 (cond ((eq package *cl-package*)
1695 (setq symbol (find-symbol name (canonical-home-package name))))
1696 ((not (or (eq package *keyword-package*)
1697 (= (mismatch (cl:package-name package) "SB-") 3)))
1698 (bug "~S in bad package for target: ~A" symbol package)))
1700 (or (get symbol 'cold-intern-info)
1701 ;; KLUDGE: there is no way to automatically know which macros are handled
1702 ;; by sb-fasteval as special forms. An extra slot should be created in
1703 ;; any symbol naming such a macro, though things still work if the slot
1704 ;; doesn't exist, as long as only a deferred interpreter processor is used
1705 ;; and not an immediate processor.
1706 (let* ((pkg-info
1707 (when core-file-name (cold-find-package-info (sb-xc:package-name package))))
1708 (handle (allocate-symbol sb-vm:symbol-size
1709 (cdr pkg-info) name :gspace gspace)))
1710 (when pkg-info
1711 (aver (not (zerop (descriptor-fixnum (read-slot (cdr pkg-info) :id))))))
1712 (setf (get symbol 'cold-intern-info) handle)
1713 ;; maintain reverse map from target descriptor to host symbol
1714 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1715 #+sb-thread
1716 (let ((index (info :variable :wired-tls symbol)))
1717 (when (integerp index) ; thread slot or known TLS
1718 (cold-assign-tls-index handle index)))
1719 ;; Steps that only make sense when writing a core file
1720 (when core-file-name
1721 (record-accessibility (or access (nth-value 1 (find-symbol name package)))
1722 pkg-info handle package symbol)
1723 (when (eq package *keyword-package*)
1724 (cold-set handle handle)))
1725 handle)))
1727 (defun record-accessibility (accessibility target-pkg-info symbol-descriptor
1728 &optional host-package host-symbol)
1729 (let ((access-lists (car target-pkg-info)))
1730 (case accessibility
1731 (:external (push symbol-descriptor (car access-lists)))
1732 (:internal (push symbol-descriptor (cdr access-lists)))
1733 (t (error "~S inaccessible in package ~S" host-symbol host-package)))))
1735 ;;; a hash table mapping from fdefinition names to descriptors of cold
1736 ;;; objects
1738 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1739 ;;; we want to have only one entry per name, this must be an 'EQUAL
1740 ;;; hash table, not the default 'EQL.
1741 (defvar *cold-fdefn-objects*)
1743 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1744 ;;; It might be nice to put NIL on a readonly page by itself to prevent unsafe
1745 ;;; code from destroying the world with (RPLACx nil 'kablooey)
1746 (defun make-nil-descriptor ()
1747 (gspace-claim-n-words *static* (/ (- sb-vm::nil-value-offset
1748 (* 2 sb-vm:n-word-bytes)
1749 sb-vm:list-pointer-lowtag)
1750 sb-vm:n-word-bytes))
1751 (let* ((des (allocate-otherptr *static* (1+ sb-vm:symbol-size) 0))
1752 (nil-val (make-descriptor (+ (descriptor-bits des)
1753 (* 2 sb-vm:n-word-bytes)
1754 (- sb-vm:list-pointer-lowtag
1755 ;; ALLOCATE-OTHERPTR always adds in
1756 ;; OTHER-POINTER-LOWTAG, so subtract it.
1757 sb-vm:other-pointer-lowtag))))
1758 (initial-info (cold-cons nil-val nil-val)))
1759 (aver (= (descriptor-bits nil-val) sb-vm:nil-value))
1761 (setf *nil-descriptor* nil-val
1762 (gethash (descriptor-bits nil-val) *cold-symbols*) nil
1763 (get nil 'cold-intern-info) nil-val)
1765 ;; Alter the first word to 0 instead of the symbol size. It reads as a fixnum,
1766 ;; but is meaningless. In practice, Lisp code can not utilize the fact that NIL
1767 ;; has a widetag; any use of NIL-as-symbol must pre-check for NIL. Consider:
1768 ;; 50100100: 0000000000000000 = 0
1769 ;; 50100108: 000000000000052D <- 5 words follow, widetag = #x2D
1770 ;; 50100110: 0000000050100117
1771 ;; 50100118: 0000000050100117
1772 ;; 50100120: 0000001000000007 = (NIL . #<SB-INT:PACKED-INFO len=3 {1000002FF3}>)
1773 ;; 50100128: 000100100000400F
1774 ;; 50100130: 0000000000000000 = 0
1776 ;; Indeed *(char*)(NIL-0xf) = #x2D, /* if little-endian */
1777 ;; so why can't we exploit this to improve SYMBOLP? Hypothetically:
1778 ;; if (((ptr & 7) == 7) && *(char*)(ptr-15) == SYMBOL_WIDETAG) { }
1779 ;; which is true of NIL and all other symbols, but wrong, because it assumes
1780 ;; that _any_ cons cell could be accessed at a negative displacement from its
1781 ;; base address. Only NIL (viewed as a cons) has this property.
1782 ;; Otherwise we would be reading random bytes or inaccessible memory. Finally,
1783 ;; the above sequence would not necessarily decrease the instruction count!
1784 ;; Those points aside, gencgc correctly calls scav_symbol() on NIL.
1786 (when core-file-name
1787 (let ((name (string-literal-to-core "NIL")))
1788 (write-wordindexed des 0 (make-fixnum-descriptor 0))
1789 ;; The header-word for NIL "as a symbol" contains a length + widetag.
1790 (write-wordindexed des 1 (make-other-immediate-descriptor (1- sb-vm:symbol-size)
1791 sb-vm:symbol-widetag))
1792 ;; Write the CAR and CDR of nil-as-cons
1793 (let* ((nil-cons-base-addr (- sb-vm:nil-value sb-vm:list-pointer-lowtag))
1794 (nil-cons-car-offs (- nil-cons-base-addr (gspace-byte-address *static*)))
1795 (nil-cons-cdr-offs (+ nil-cons-car-offs sb-vm:n-word-bytes)))
1796 (setf (bvref-word (descriptor-mem des) nil-cons-car-offs) sb-vm:nil-value
1797 (bvref-word (descriptor-mem des) nil-cons-cdr-offs) sb-vm:nil-value))
1798 ;; Assign HASH if and only if NIL's hash is address-insensitive
1799 #+(or relocatable-static-space (not 64-bit))
1800 (assign-symbol-hash des (+ 1 sb-vm:symbol-hash-slot) "NIL")
1801 (write-wordindexed des (+ 1 sb-vm:symbol-info-slot) initial-info)
1802 (write-wordindexed/raw des (+ 1 sb-vm:symbol-name-slot)
1803 (encode-symbol-name sb-impl::+package-id-lisp+ name))))
1804 nil))
1806 ;;; Since the initial symbols must be allocated before we can intern
1807 ;;; anything else, we intern those here. We also set the value of T.
1808 (defun initialize-static-space (tls-init)
1809 "Initialize the cold load symbol-hacking data structures."
1810 (declare (ignorable tls-init))
1811 ;; -1 is magic having to do with nil-as-cons vs. nil-as-symbol
1812 #-compact-symbol
1813 (write-wordindexed *nil-descriptor* (- sb-vm:symbol-package-id-slot 1)
1814 (make-fixnum-descriptor sb-impl::+package-id-lisp+))
1815 (when core-file-name
1816 ;; NIL did not have its package assigned. Do that now.
1817 (record-accessibility :external (cold-find-package-info "COMMON-LISP")
1818 *nil-descriptor*))
1819 ;; Intern the others.
1820 (dovector (symbol sb-vm:+static-symbols+)
1821 (let* ((des (cold-intern symbol :gspace *static*))
1822 (offset-wanted (sb-vm:static-symbol-offset symbol))
1823 (offset-found (- (descriptor-bits des)
1824 (descriptor-bits *nil-descriptor*))))
1825 (unless (= offset-wanted offset-found)
1826 (error "Offset from ~S to ~S is ~W, not ~W"
1827 symbol
1829 offset-found
1830 offset-wanted))))
1831 ;; Reserve space for SB-LOCKLESS:+TAIL+ which is conceptually like NIL
1832 ;; but tagged with INSTANCE-POINTER-LOWTAG.
1833 (setq *lflist-tail-atom*
1834 (if core-file-name
1835 (write-slots (allocate-struct-of-type 'sb-lockless::list-node *static*)
1836 :%node-next nil)
1837 (allocate-struct (1+ sb-vm:instance-data-start)
1838 (make-fixnum-descriptor 0) *static*)))
1840 ;; Assign TLS indices of C interface symbols
1841 #+sb-thread
1842 (progn
1843 (dolist (binding sb-vm::per-thread-c-interface-symbols)
1844 (ensure-symbol-tls-index (car (ensure-list binding))))
1845 ;; Assign other known TLS indices
1846 (dolist (pair tls-init)
1847 (destructuring-bind (tls-index . symbol) pair
1848 (aver (eql tls-index (ensure-symbol-tls-index symbol))))))
1850 ;; Establish the value of T.
1851 (let ((t-symbol (cold-intern t :gspace *static*)))
1852 (cold-set t-symbol t-symbol))
1854 ;; Establish the value of SB-VM:FUNCTION-LAYOUT and **PRIMITIVE-OBJECT-LAYOUTS**
1855 #+compact-instance-header
1856 (progn
1857 (write-wordindexed/raw (cold-intern 'sb-vm:function-layout)
1858 sb-vm:symbol-value-slot
1859 (ash (cold-layout-descriptor-bits 'function) 32))
1860 (cold-set '**primitive-object-layouts**
1861 #+permgen
1862 (emplace-vector (make-random-descriptor
1863 (logior (gspace-byte-address *permgen*)
1864 sb-vm:other-pointer-lowtag))
1865 sb-vm:simple-vector-widetag 256)
1866 #+immobile-space
1867 (let ((filler
1868 (make-random-descriptor
1869 (logior (gspace-byte-address *immobile-fixedobj*)
1870 sb-vm:other-pointer-lowtag)))
1871 (vector
1872 (make-random-descriptor
1873 (logior (+ (gspace-byte-address *immobile-fixedobj*)
1874 sb-vm:immobile-card-bytes
1875 (* (+ 2 256) (- sb-vm:n-word-bytes)))
1876 sb-vm:other-pointer-lowtag))))
1877 (emplace-vector filler sb-vm:simple-array-fixnum-widetag
1878 (- (/ sb-vm:immobile-card-bytes sb-vm:n-word-bytes)
1879 ;; subtract 2 object headers + 256 words
1880 (+ 4 256)))
1881 (emplace-vector vector sb-vm:simple-vector-widetag 256))))
1883 ;; Immobile code on x86-64 prefers all FDEFNs adjacent so that code
1884 ;; can be located anywhere in the addressable memory allowed by the
1885 ;; OS, as long as all FDEFNs are near enough all code (i.e. within a
1886 ;; 32-bit jmp offset). That fails if static fdefns are wired to an
1887 ;; address below 4GB and code resides above 4GB. But as the
1888 ;; Fundamental Theorem says: any problem can be solved by adding
1889 ;; another indirection.
1890 #+(and x86-64 immobile-code)
1891 (progn
1892 (setf *c-callable-fdefn-vector*
1893 (vector-in-core (make-list (length sb-vm::+c-callable-fdefns+)
1894 :initial-element *nil-descriptor*)
1895 *static*))
1896 ;; static-call entrypoint vector must be immediately adjacent to *asm-routine-vector*
1897 (word-vector (make-list (length sb-vm:+static-fdefns+) :initial-element 0) *static*)
1898 (setf *asm-routine-vector* (word-vector (make-list 256 :initial-element 0)
1899 *static*)))
1901 ;; With immobile-code on x86-64, static-fdefns as a concept are
1902 ;; useful - the implication is that the function's definition will
1903 ;; not change. But the fdefn per se is not useful - callers refer
1904 ;; to callees directly.
1905 #-(and x86-64 immobile-code)
1906 (progn
1907 (dolist (sym sb-vm::+c-callable-fdefns+)
1908 (ensure-cold-fdefn sym *static*))
1910 (dovector (sym sb-vm:+static-fdefns+)
1911 (let* ((fdefn (ensure-cold-fdefn sym *static*))
1912 (offset (- (+ (- (descriptor-bits fdefn)
1913 sb-vm:other-pointer-lowtag)
1914 (* sb-vm:fdefn-raw-addr-slot sb-vm:n-word-bytes))
1915 (descriptor-bits *nil-descriptor*)))
1916 (desired (sb-vm:static-fun-offset sym)))
1917 (unless (= offset desired)
1918 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1919 sym nil offset desired))))))
1921 ;;; Sort *COLD-LAYOUTS* to return them in a deterministic order.
1922 (defun sort-cold-layouts ()
1923 (sort (%hash-table-alist *cold-layouts*) #'<
1924 :key (lambda (x) (descriptor-bits (cold-layout-descriptor (cdr x))))))
1926 ;;; Establish initial values for magic symbols.
1928 (defun finish-symbols ()
1929 (cold-set 'sb-kernel::*!initial-layouts*
1930 (vector-in-core
1931 (mapcar (lambda (pair)
1932 (cold-cons (cold-intern (car pair))
1933 (cold-layout-descriptor (cdr pair))))
1934 (sort-cold-layouts))))
1935 ;; MAKE-LAYOUT uses ATOMIC-INCF which returns the value in the cell prior to
1936 ;; increment, so we need to add 1 to get to the next value for it because
1937 ;; we always pre-increment *general-layout-uniqueid-counter* when reading it.
1938 (cold-set 'sb-kernel::*layout-id-generator*
1939 (cold-list (make-fixnum-descriptor
1940 (1+ sb-kernel::*general-layout-uniqueid-counter*))))
1942 #+sb-thread
1943 (cold-set 'sb-vm::*free-tls-index*
1944 (make-descriptor (ash *genesis-tls-counter* sb-vm:word-shift)))
1946 (cold-set 'sb-c::*code-serialno* (make-fixnum-descriptor (1+ sb-c::*code-serialno*)))
1948 (cold-set 'sb-impl::*setf-fdefinition-hook* *nil-descriptor*)
1949 (cold-set 'sb-impl::*user-hash-table-tests* *nil-descriptor*)
1950 (cold-set 'sb-lockless:+tail+ *lflist-tail-atom*)
1952 #+immobile-code
1953 (let* ((space *immobile-text*)
1954 (wordindex (gspace-free-word-index space))
1955 (words-per-page (/ sb-vm:immobile-card-bytes sb-vm:n-word-bytes)))
1956 ;; Put the C-callable fdefns into the static-space vector of fdefns on x86-64
1957 #+x86-64
1958 (loop for i from 0 for sym in sb-vm::+c-callable-fdefns+
1959 do (cold-svset *c-callable-fdefn-vector* i (ensure-cold-fdefn sym)))
1960 (cold-set 'sb-fasl::*asm-routine-vector* *asm-routine-vector*)
1961 (let* ((objects (gspace-objects space))
1962 (count (length objects)))
1963 (let ((remainder (rem wordindex words-per-page)))
1964 (unless (zerop remainder)
1965 (let* ((fill-nwords (- words-per-page remainder))
1966 (des
1967 ;; technically FILLER_WIDETAG has no valid lowtag because it's not an object
1968 ;; that lisp can address. But WRITE-WORDINDEXED requires a pointer descriptor
1969 (allocate-cold-descriptor space (* fill-nwords sb-vm:n-word-bytes)
1970 sb-vm:other-pointer-lowtag)))
1971 (aver (zerop (rem (gspace-free-word-index space) words-per-page)))
1972 (write-header-word des (logior (ash fill-nwords 32) sb-vm:filler-widetag)))))
1973 ;; Construct a ub32 array of object offsets.
1974 (let* ((n-data-words (ceiling count 2)) ; lispword = 2 ub32s
1975 (vect (allocate-vector sb-vm:simple-array-unsigned-byte-32-widetag
1976 count n-data-words))
1977 (data-ptr (+ (descriptor-byte-offset vect)
1978 (ash sb-vm:vector-data-offset sb-vm:word-shift))))
1979 (dotimes (i count)
1980 (setf (bvref-32 (descriptor-mem vect) data-ptr)
1981 (descriptor-byte-offset (aref objects i)))
1982 (incf data-ptr 4))
1983 (cold-set 'sb-vm::*immobile-codeblob-vector* vect))))
1985 ;; Symbols for which no call to COLD-INTERN would occur - due to not being
1986 ;; referenced until warm init - must be artificially cold-interned.
1987 ;; Inasmuch as the "offending" things are compiled by ordinary target code
1988 ;; and not cold-init, I think we should use an ordinary DEFPACKAGE for
1989 ;; the added-on bits. What I've done is somewhat of a fragile kludge.
1990 (let (syms)
1991 (with-package-iterator (iter '("SB-PCL" "SB-MOP" "SB-GRAY" "SB-SEQUENCE"
1992 "SB-PROFILE" "SB-EXT" "SB-VM"
1993 "SB-C" "SB-FASL" "SB-DEBUG")
1994 :external)
1995 (loop
1996 (multiple-value-bind (foundp sym accessibility package) (iter)
1997 (declare (ignore accessibility))
1998 (cond ((not foundp) (return))
1999 ((eq (cl:symbol-package sym) package) (push sym syms))))))
2000 (setf syms (stable-sort syms #'string<))
2001 (dolist (sym syms)
2002 (cold-intern sym)))
2004 (cold-set 'sb-impl::*!initial-package-graph*
2005 (list-to-core
2006 (mapcar (lambda (x) (list-to-core (mapcar #'string-literal-to-core x)))
2007 *package-graph*)))
2009 (cold-set
2010 'sb-impl::*!initial-symbols*
2011 (cold-cons
2012 (let (uninterned)
2013 (maphash (lambda (key val) (declare (ignore key)) (push val uninterned))
2014 *uninterned-symbol-table*)
2015 (vector-in-core (sort uninterned #'< :key #'descriptor-bits)))
2016 (list-to-core
2017 (mapcar
2018 (lambda (pkgcons)
2019 (destructuring-bind (pkg-name . pkg-info) pkgcons
2020 (unless (member pkg-name '("COMMON-LISP" "COMMON-LISP-USER" "KEYWORD")
2021 :test 'string=)
2022 (let ((host-pkg (find-package pkg-name))
2023 syms)
2024 ;; Now for each symbol directly present in this host-pkg,
2025 ;; i.e. accessible but not :INHERITED, figure out if the symbol
2026 ;; came from a different package, and if so, make a note of it.
2027 (with-package-iterator (iter host-pkg :internal :external)
2028 (loop (multiple-value-bind (foundp sym accessibility) (iter)
2029 (unless foundp (return))
2030 (unless (eq (cl:symbol-package sym) host-pkg)
2031 (push (cons sym accessibility) syms)))))
2032 (dolist (symcons (sort syms #'string< :key #'car))
2033 (destructuring-bind (sym . accessibility) symcons
2034 (record-accessibility accessibility pkg-info (cold-intern sym)
2035 host-pkg sym)))))
2036 (cold-list (cdr pkg-info)
2037 (vector-in-core (caar pkg-info))
2038 (vector-in-core (cdar pkg-info)))))
2039 (sort (%hash-table-alist *cold-package-symbols*)
2040 #'string< :key #'car))))) ; Sort by package-name
2042 ;; assign *PACKAGE* since it supposed to be always-bound
2043 ;; and various things assume that it is. e.g. FIND-PACKAGE has an
2044 ;; (IF (BOUNDP '*PACKAGE*)) test which the compiler elides.
2045 (cold-set '*package* (cdr (cold-find-package-info "COMMON-LISP-USER")))
2047 (dump-symbol-infos
2048 (attach-fdefinitions-to-symbols
2049 (attach-classoid-cells-to-symbols (make-hash-table :test #'eq))))
2051 #+x86-64 ; Dump a popular constant
2052 (let ((array
2053 ;; Embed the constant in an unboxed array. This shouldn't be necessary,
2054 ;; because the start of the scanned space is STATIC_SPACE_OBJECTS_START,
2055 ;; but not all uses strictly follow that rule. (They should though)
2056 ;; This must not conflict with the alloc regions at the start of the space.
2057 (make-random-descriptor (logior (- sb-vm::non-negative-fixnum-mask-constant-wired-address
2058 (* 2 sb-vm:n-word-bytes))
2059 sb-vm:other-pointer-lowtag))))
2060 (write-wordindexed/raw array 0 sb-vm:simple-array-unsigned-byte-64-widetag)
2061 (write-wordindexed array 1 (make-fixnum-descriptor 1))
2062 (write-wordindexed/raw array 2 sb-vm::non-negative-fixnum-mask-constant))
2064 #+x86
2065 (progn
2066 (cold-set 'sb-vm::*fp-constant-0d0* (number-to-core $0d0))
2067 (cold-set 'sb-vm::*fp-constant-1d0* (number-to-core $1d0))
2068 (cold-set 'sb-vm::*fp-constant-0f0* (number-to-core $0f0))
2069 (cold-set 'sb-vm::*fp-constant-1f0* (number-to-core $1f0))))
2071 ;;;; functions and fdefinition objects
2073 ;;; Given a cold representation of a symbol, return a warm
2074 ;;; representation.
2075 (defun warm-symbol (des)
2076 ;; Note that COLD-INTERN is responsible for keeping the
2077 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
2078 ;; uninterned symbol, the code below will fail. But as long as we
2079 ;; don't need to look up uninterned symbols during bootstrapping,
2080 ;; that's OK..
2081 (multiple-value-bind (symbol found-p)
2082 (gethash (descriptor-bits des) *cold-symbols*)
2083 (declare (type symbol symbol))
2084 (unless found-p
2085 (error "no warm symbol"))
2086 symbol))
2088 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
2089 (defun cold-car (des)
2090 (aver (= (descriptor-lowtag des) sb-vm:list-pointer-lowtag))
2091 (read-wordindexed des sb-vm:cons-car-slot))
2092 (defun cold-cdr (des)
2093 (aver (= (descriptor-lowtag des) sb-vm:list-pointer-lowtag))
2094 (read-wordindexed des sb-vm:cons-cdr-slot))
2095 (defun cold-rplacd (des newval)
2096 (aver (= (descriptor-lowtag des) sb-vm:list-pointer-lowtag))
2097 (write-wordindexed des sb-vm:cons-cdr-slot newval)
2098 des)
2099 (defun cold-null (des) (descriptor= des *nil-descriptor*))
2101 ;;; Given a cold representation of a function name, return a warm
2102 ;;; representation.
2103 (declaim (ftype (function ((or symbol descriptor)) (or symbol list)) warm-fun-name))
2104 (defun warm-fun-name (des)
2105 (let ((result
2106 (if (symbolp des)
2107 ;; This parallels the logic at the start of COLD-INTERN
2108 ;; which re-homes symbols in SB-XC to COMMON-LISP.
2109 (if (eq (cl:symbol-package des) (find-package "SB-XC"))
2110 (intern (symbol-name des) (canonical-home-package (string des)))
2111 des)
2112 (ecase (descriptor-lowtag des)
2113 (#.sb-vm:list-pointer-lowtag
2114 (aver (not (cold-null des))) ; function named NIL? please no..
2115 (let ((rest (cold-cdr des)))
2116 (aver (cold-null (cold-cdr rest)))
2117 (list (warm-symbol (cold-car des))
2118 (warm-symbol (cold-car rest)))))
2119 (#.sb-vm:other-pointer-lowtag
2120 (warm-symbol des))))))
2121 (legal-fun-name-or-type-error result)
2122 result))
2124 (defvar *cold-assembler-obj*) ; a single code component
2125 ;;; Writing the address of the undefined trampoline into static fdefns
2126 ;;; has to occur after the asm routines are loaded, which occurs after
2127 ;;; the static fdefns are initialized.
2128 (defvar *deferred-undefined-tramp-refs*)
2129 (defun fdefn-makunbound (fdefn)
2130 (write-wordindexed fdefn sb-vm:fdefn-fun-slot *nil-descriptor*)
2131 (write-wordindexed/raw fdefn sb-vm:fdefn-raw-addr-slot
2132 (lookup-assembler-reference 'sb-vm::undefined-tramp :direct)))
2133 (defun ensure-cold-fdefn (cold-name &optional
2134 (gspace #+immobile-space *immobile-fixedobj*
2135 #-immobile-space *dynamic*))
2136 (declare (type (or symbol descriptor) cold-name))
2137 (let ((warm-name (warm-fun-name cold-name)))
2138 (or (gethash warm-name *cold-fdefn-objects*)
2139 (let ((fdefn (allocate-otherptr gspace sb-vm:fdefn-size sb-vm:fdefn-widetag)))
2140 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
2141 #+x86-64
2142 (write-wordindexed/raw ; write an INT instruction into the header
2143 fdefn 0 (logior (ash sb-vm::undefined-fdefn-header 16)
2144 (read-bits-wordindexed fdefn 0)))
2145 (write-wordindexed fdefn sb-vm:fdefn-name-slot cold-name)
2146 (when core-file-name
2147 (when (typep warm-name '(and symbol (not null)))
2148 (write-wordindexed (cold-intern warm-name) sb-vm:symbol-fdefn-slot fdefn))
2149 (if *cold-assembler-obj*
2150 (fdefn-makunbound fdefn)
2151 (push (lambda ()
2152 (when (zerop (read-bits-wordindexed fdefn sb-vm:fdefn-fun-slot))
2153 ;; This is probably irrelevant - it only occurs for static fdefns,
2154 ;; but every static fdefn will eventually get a definition.
2155 (fdefn-makunbound fdefn)))
2156 *deferred-undefined-tramp-refs*)))
2157 fdefn))))
2159 (defun cold-fun-entry-addr (fun)
2160 (aver (= (descriptor-lowtag fun) sb-vm:fun-pointer-lowtag))
2161 (+ (descriptor-bits fun)
2162 (- sb-vm:fun-pointer-lowtag)
2163 (ash sb-vm:simple-fun-insts-offset sb-vm:word-shift)))
2165 (defun cold-fset (name function)
2166 (aver (= (descriptor-widetag function) sb-vm:simple-fun-widetag))
2167 (let ((fdefn (ensure-cold-fdefn
2168 ;; (SETF f) was descriptorized when dumped, symbols were not.
2169 (if (symbolp name)
2170 (cold-intern name)
2171 name))))
2172 (let ((existing (read-wordindexed fdefn sb-vm:fdefn-fun-slot)))
2173 (unless (or (cold-null existing) (descriptor= existing function))
2174 (error "Function multiply defined: ~S. Was ~x is ~x" name
2175 (descriptor-bits existing)
2176 (descriptor-bits function))))
2177 (write-wordindexed fdefn sb-vm:fdefn-fun-slot function)
2178 #+x86-64
2179 (write-wordindexed/raw ; write a JMP instruction into the header
2180 fdefn 0 (dpb #x1025FF (byte 24 16) (read-bits-wordindexed fdefn 0)))
2181 (write-wordindexed/raw
2182 fdefn sb-vm:fdefn-raw-addr-slot
2183 (or #+(or sparc arm riscv) ; raw addr is the function descriptor
2184 (descriptor-bits function)
2185 ;; For all others raw addr is the starting address
2186 (+ (logandc2 (descriptor-bits function) sb-vm:lowtag-mask)
2187 (ash sb-vm:simple-fun-insts-offset sb-vm:word-shift))))
2188 fdefn))
2190 (defun attach-classoid-cells-to-symbols (hashtable)
2191 (when (plusp (hash-table-count *classoid-cells*))
2192 (aver (gethash 'sb-kernel::classoid-cell *cold-layouts*))
2193 (let ((type-classoid-cell-info
2194 (sb-c::meta-info-number (sb-c::meta-info :type :classoid-cell)))
2195 (type-kind-info
2196 (sb-c::meta-info-number (sb-c::meta-info :type :kind))))
2197 ;; Iteration order is immaterial. The symbols will get sorted later.
2198 (maphash (lambda (symbol cold-classoid-cell)
2199 (let ((packed-info
2200 (packed-info-insert
2201 (gethash symbol hashtable +nil-packed-infos+)
2202 sb-impl::+no-auxiliary-key+
2203 type-classoid-cell-info cold-classoid-cell)))
2204 ;; an instance classoid won't be returned from %PARSE-TYPE
2205 ;; unless the :KIND is set, but we can't set the kind
2206 ;; to :INSTANCE unless the classoid is present in the cell.
2207 (when (and (eq (info :type :kind symbol) :instance)
2208 (not (cold-null (read-slot cold-classoid-cell :classoid))))
2209 (setf packed-info
2210 (packed-info-insert
2211 packed-info sb-impl::+no-auxiliary-key+
2212 type-kind-info (cold-intern :instance))))
2213 (setf (gethash symbol hashtable) packed-info)))
2214 *classoid-cells*)))
2215 hashtable)
2217 ;; Create pointer from SYMBOL and/or (SETF SYMBOL) to respective fdefinition
2219 (defun attach-fdefinitions-to-symbols (hashtable)
2220 ;; Collect fdefinitions that go with one symbol, e.g. (SETF CAR) and (CAS CAR)
2221 ;; using the host's code for manipulating a packed-info.
2222 ;; Do not add fdefns for symbols to the info. It goes in a slot.
2223 (maphash (lambda (warm-name cold-fdefn)
2224 (unless (symbolp warm-name)
2225 (with-globaldb-name (key1 key2) warm-name
2226 :hairy (error "Hairy fdefn name in genesis: ~S" warm-name)
2227 :simple (setf (gethash key1 hashtable)
2228 (packed-info-insert
2229 (gethash key1 hashtable +nil-packed-infos+)
2230 key2 +fdefn-info-num+ cold-fdefn)))))
2231 *cold-fdefn-objects*)
2232 hashtable)
2234 (defun dump-packed-info (list)
2235 ;; Payload length is the element count + LAYOUT slot if necessary.
2236 ;; Header word is added automatically by ALLOCATE-STRUCT
2237 (let ((s (allocate-struct (+ sb-vm:instance-data-start (length list))
2238 (cold-layout-descriptor (gethash 'packed-info *cold-layouts*)))))
2239 (loop for i from (+ sb-vm:instance-slots-offset sb-vm:instance-data-start)
2240 for elt in list do (write-wordindexed s i elt))
2242 (defun dump-symbol-infos (hashtable)
2243 (cold-set 'sb-impl::+nil-packed-infos+
2244 (dump-packed-info (list (make-fixnum-descriptor 0))))
2245 ;; Emit in the same order symbols reside in core to avoid
2246 ;; sensitivity to the iteration order of host's maphash.
2247 (loop for (warm-sym . info)
2248 in (sort (%hash-table-alist hashtable) #'<
2249 :key (lambda (x) (descriptor-bits (cold-intern (car x)))))
2250 do (aver warm-sym) ; enforce that NIL was specially dealt with already
2251 (aver (> (sb-impl::packed-info-len info) 1))
2252 (write-wordindexed
2253 (cold-intern warm-sym)
2254 sb-vm:symbol-info-slot
2255 (dump-packed-info
2256 ;; Each packed-info will have one fixnum, possibly the symbol SETF,
2257 ;; and zero, one, or two #<fdefn>, and/or a classoid-cell.
2258 (map 'list (lambda (elt)
2259 (etypecase elt
2260 (symbol (cold-intern elt))
2261 (sb-xc:fixnum (make-fixnum-descriptor elt))
2262 (descriptor elt)))
2263 (sb-impl::packed-info-cells info))))))
2265 ;;;; fixups and related stuff
2267 ;;; an EQUAL hash table
2268 (defvar *cold-foreign-symbol-table*)
2269 (declaim (type hash-table *cold-foreign-symbol-table*))
2271 (defvar *cold-assembler-routines*)
2272 (defvar *cold-static-call-fixups*)
2274 ;;: See picture in 'objdef'
2275 (defun code-object-size (code-object) ; Return total size in bytes
2276 (* (ash (get-header-data code-object) (+ #+64-bit -24))
2277 sb-vm:n-word-bytes))
2279 ;; Boxed header length is stored directly in bytes, not words
2280 (defun code-header-bytes (code-object)
2281 (ldb (byte 32 0) (read-bits-wordindexed code-object sb-vm:code-boxed-size-slot)))
2282 (defun code-header-words (code-object) ; same, but expressed in words
2283 (ash (code-header-bytes code-object) (- sb-vm:word-shift)))
2285 (defun code-instructions (code)
2286 (make-model-sap (- (+ (descriptor-bits code) (code-header-bytes code))
2287 sb-vm:other-pointer-lowtag)
2288 (descriptor-gspace code)))
2290 ;;; These are fairly straightforward translations of the similarly named accessor
2291 ;;; from src/code/simple-fun.lisp
2292 (defun code-trailer-ref (code offset)
2293 "Reference a uint_32 relative to the end of code at byte offset OFFSET.
2294 Legal values for OFFSET are -4, -8, -12, ..."
2295 (bvref-32 (descriptor-mem code)
2296 (+ (descriptor-byte-offset code) (code-object-size code) offset)))
2297 (defun code-fun-table-count (code)
2298 "Return the COUNT trailer word in CODE. The COUNT is a packed integer containing
2299 the number of embedded SIMPLE-FUNs and the number of padding bytes in the
2300 instructions prior to the start of the simple-fun offset list"
2301 ;; The case of trailer-len = 0 (no trailer payload) can't happen during genesis,
2302 ;; so we don't check for it.
2303 (let ((word (code-trailer-ref code -4)))
2304 ;; TRAILER-REF returns 4-byte quantities. Extract a two-byte quantity.
2305 #+little-endian (ldb (byte 16 0) word)
2306 #+big-endian (ldb (byte 16 16) word)))
2308 ;;; These are literally identical between cross-compiler and target.
2309 ;;; TODO: Maybe put them somewhere that gets defined for both?
2310 ;;; (Minor problem of CODE-COMPONENT not being a primitive type though)
2311 (defun code-n-entries (code)
2312 (ash (code-fun-table-count code) -5))
2313 (defun %code-fun-offset (code fun-index)
2314 ;; The 4-byte quantity at "END" - 4 is the trailer count, the word at -8 is
2315 ;; the offset to the 0th simple-fun, -12 is the next, etc...
2316 (code-trailer-ref code (* -4 (+ fun-index 2))))
2318 (defun lookup-assembler-reference (symbol &optional (mode :direct))
2319 (let* ((code-component *cold-assembler-obj*)
2320 (list *cold-assembler-routines*)
2321 (insts (+ (logandc2 (descriptor-bits code-component) sb-vm:lowtag-mask)
2322 (code-header-bytes code-component)))
2323 (offset (or (cdr (assq symbol list))
2324 (error "Assembler routine ~S not defined." symbol)))
2325 (addr (+ insts offset)))
2326 (ecase mode
2327 (:direct addr)
2328 #+(or ppc ppc64) (:indirect (- addr sb-vm:nil-value))
2329 #+(or x86 x86-64)
2330 (:indirect
2331 (let ((index (count-if (lambda (x) (< (cdr x) offset)) list)))
2332 #-immobile-space
2333 (+ insts (ash (1+ index) sb-vm:word-shift)) ; add 1 for the jump table count
2334 #+immobile-space
2335 (+ (logandc2 (descriptor-bits *asm-routine-vector*) sb-vm:lowtag-mask)
2336 (ash (+ sb-vm:vector-data-offset index) sb-vm:word-shift)))))))
2338 ;;; Unlike in the target, FOP-KNOWN-FUN sometimes has to backpatch.
2339 (defvar *deferred-known-fun-refs*)
2341 (defun code-jump-table-words (code)
2342 (ldb (byte 14 0) (read-bits-wordindexed code (code-header-words code))))
2344 (declaim (ftype (sfunction (descriptor sb-vm:word (or sb-vm:word
2345 sb-vm:signed-word)
2346 keyword keyword)
2347 descriptor)
2348 cold-fixup))
2349 (defun cold-fixup (code-object after-header value kind flavor)
2350 (sb-vm:fixup-code-object code-object after-header value kind flavor)
2351 code-object)
2353 (defun resolve-static-call-fixups ()
2354 (dolist (fixup *cold-static-call-fixups*)
2355 (destructuring-bind (name kind code offset) fixup
2356 (cold-fixup code offset
2357 (cold-fun-entry-addr (cold-symbol-function name))
2358 kind :static-call))))
2360 (defun alien-linkage-table-note-symbol (symbol-name datap)
2361 "Register a symbol and return its address in proto-linkage-table."
2362 (sb-vm::alien-linkage-table-entry-address
2363 (ensure-gethash (if datap (list symbol-name) symbol-name)
2364 *cold-foreign-symbol-table*
2365 (hash-table-count *cold-foreign-symbol-table*))))
2367 (defun foreign-symbols-to-core ()
2368 (flet ((to-core (list transducer target-symbol)
2369 (cold-set target-symbol (vector-in-core (mapcar transducer list)))))
2370 ;; Sort by index into linkage table
2371 (to-core (sort (%hash-table-alist *cold-foreign-symbol-table*) #'< :key #'cdr)
2372 (lambda (pair &aux (key (car pair))
2373 (sym (string-literal-to-core
2374 (if (listp key) (car key) key))))
2375 (if (listp key) (cold-list sym) sym))
2376 'sb-vm::+required-foreign-symbols+)
2377 (cold-set (cold-intern '*assembler-routines*) *cold-assembler-obj*)
2378 (to-core *cold-assembler-routines*
2379 (lambda (rtn)
2380 (cold-cons (cold-intern (first rtn)) (make-fixnum-descriptor (cdr rtn))))
2381 '*!initial-assembler-routines*)))
2384 ;;;; general machinery for cold-loading FASL files
2386 (defun pop-fop-stack (stack)
2387 (let ((top (svref stack 0)))
2388 (declare (type index top))
2389 (when (eql 0 top)
2390 (error "FOP stack empty"))
2391 (setf (svref stack 0) (1- top))
2392 (svref stack top)))
2394 ;;; Cause a fop to have a special definition for cold load.
2396 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
2397 ;;; looks up the encoding for this name (created by a previous DEFINE-FOP)
2398 ;;; instead of creating a new encoding.
2399 (defmacro define-cold-fop ((name &optional arglist) &rest forms)
2400 #+c-headers-only (declare (ignore name arglist forms))
2401 #-c-headers-only
2402 (let* ((code (gethash name *fop-name-to-opcode*))
2403 (argc (aref (car **fop-signatures**)
2404 (or code
2405 (error "~S is not a defined FOP." name))))
2406 (fname (symbolicate "COLD-" name)))
2407 (aver (= (length arglist) argc))
2408 `(progn
2409 (defun ,fname (.fasl-input. ,@arglist)
2410 (declare (ignorable .fasl-input.))
2411 (macrolet ((fasl-input () '(the fasl-input .fasl-input.))
2412 (fasl-input-stream () '(%fasl-input-stream (fasl-input)))
2413 (pop-stack ()
2414 '(pop-fop-stack (%fasl-input-stack (fasl-input)))))
2415 ,@forms))
2416 ;; We simply overwrite elements of **FOP-FUNS** since the contents
2417 ;; of the host are never propagated directly into the target core.
2418 (setf (svref **fop-funs** ,code) #',fname))))
2420 ;;; Cause a fop to be undefined in cold load.
2421 (defmacro not-cold-fop (name)
2422 `(define-cold-fop (,name)
2423 (error "The fop ~S is not supported in cold load." ',name)))
2425 ;;; COLD-LOAD loads stuff into the core image being built by calling
2426 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
2427 ;;; loading functions.
2428 (defun cold-load (filename verbose show-fops-p)
2429 "Load the file named by FILENAME into the cold load image being built."
2430 (when verbose
2431 (write-line (namestring filename)))
2432 (with-open-file (s filename :element-type '(unsigned-byte 8))
2433 (if show-fops-p
2434 (with-open-file (f (make-pathname :type "foptrace" :defaults filename)
2435 :direction :output :if-exists :supersede)
2436 (let ((sb-fasl::*show-fops-p* t)
2437 (*trace-output* f))
2438 (load-as-fasl s nil nil)))
2439 (load-as-fasl s nil nil))))
2441 ;;;; miscellaneous cold fops
2443 (define-cold-fop (fop-misc-trap) *unbound-marker*)
2445 (define-cold-fop (fop-struct (size)) ; n-words incl. layout, excluding header
2446 (let* ((layout (pop-stack))
2447 (result (allocate-struct size layout))
2448 (bitmap (cold-layout-bitmap (gethash (descriptor-bits layout) *cold-layout-by-addr*)))
2449 (stack (%fasl-input-stack (fasl-input)))
2450 (n-data-words (- size sb-vm:instance-data-start)))
2451 (do ((stack-index (fop-stack-pop-n stack n-data-words) (1+ stack-index))
2452 (dsd-index sb-vm:instance-data-start (1+ dsd-index)))
2453 ((>= dsd-index size))
2454 (let ((val (svref stack stack-index)))
2455 (if (logbitp dsd-index bitmap)
2456 (write-wordindexed result (+ sb-vm:instance-slots-offset dsd-index) val)
2457 (write-wordindexed/raw result (+ sb-vm:instance-slots-offset dsd-index)
2458 (the sb-vm:word (descriptor-integer val))))))
2459 result))
2461 (defun find-in-inherits (typename inherits)
2462 (binding* ((proxy (gethash typename *cold-layouts*) :exit-if-null)
2463 (layout (cold-layout-descriptor proxy)))
2464 (dotimes (i (cold-vector-len inherits))
2465 (when (descriptor= (cold-svref inherits i) layout)
2466 (return t)))))
2468 (define-cold-fop (fop-layout (depthoid flags length))
2469 (decf depthoid) ; was bumped by 1 since non-stack args can't encode negatives
2470 (let* ((inherits (pop-stack))
2471 (bitmap-descriptor (pop-stack))
2472 (bitmap-value (descriptor-integer bitmap-descriptor))
2473 (name (pop-stack))
2474 (existing-layout (gethash name *cold-layouts*)))
2475 (declare (type descriptor bitmap-descriptor inherits))
2476 (declare (type symbol name))
2477 ;; parameters have to match an existing FOP-LAYOUT invocation if there was one
2478 (when existing-layout
2479 (let ((old-flags (cold-layout-flags existing-layout))
2480 (old-depthoid (cold-layout-depthoid existing-layout))
2481 (old-length (cold-layout-length existing-layout))
2482 (old-bitmap (cold-layout-bitmap existing-layout))
2483 (old-inherits (cold-layout-inherits existing-layout)))
2484 (unless (and (= flags old-flags)
2485 (= depthoid old-depthoid)
2486 (= length old-length)
2487 (= bitmap-value old-bitmap)
2488 (eql (cold-vector-len inherits) (cold-vector-len old-inherits))
2489 (dotimes (i (cold-vector-len inherits) t)
2490 (unless (descriptor= (cold-svref inherits i)
2491 (cold-svref old-inherits i))
2492 (return nil))))
2493 ;; Users will never see this.
2494 (format t "old=(flags=~d depthoid=~d length=~d bitmap=~d inherits=~s)~%"
2495 old-flags old-depthoid old-length old-bitmap
2496 (vector-from-core old-inherits))
2497 (format t "new=(flags=~d depthoid=~d length=~d bitmap=~d inherits=~s)~%"
2498 flags depthoid length bitmap-value
2499 (vector-from-core inherits))
2500 (bug "Messed up fop-layout for ~s" name))))
2501 (if existing-layout
2502 (cold-layout-descriptor existing-layout)
2503 (make-cold-layout name depthoid flags length bitmap-value inherits))))
2505 ;;;; cold fops for loading symbols
2507 ;;; Given STRING naming a symbol exported from COMMON-LISP, return either "SB-XC"
2508 ;;; or "COMMON-LISP" depending on which we consider canonical for the symbol's
2509 ;;; home package during genesis. If finding the symbol via XC-STRICT-CL finds a
2510 ;;; symbol in SB-XC, then that package is canonical. This is very important to
2511 ;;; get right for symbols whose identity matters (floating-point type specifiers),
2512 ;;; or else the interned ctype objects get all messed up.
2513 (defun canonical-home-package (string)
2514 (if (eq (cl:symbol-package (find-symbol string "XC-STRICT-CL"))
2515 (find-package "SB-XC"))
2516 "SB-XC"
2517 "COMMON-LISP"))
2519 ;;; Load a symbol SIZE characters long from FASL-INPUT, and
2520 ;;; intern that symbol in PACKAGE.
2521 (defun cold-load-symbol (length+flag package fasl-input)
2522 (let ((string (make-string (ash length+flag -1))))
2523 (read-string-as-bytes (%fasl-input-stream fasl-input) string)
2524 (push-fop-table (intern string (if (eq package *cl-package*)
2525 (canonical-home-package string)
2526 package))
2527 fasl-input)))
2529 (define-cold-fop (fop-symbol-in-package-save (length+flag pkg-index))
2530 (cold-load-symbol length+flag (ref-fop-table (fasl-input) pkg-index)
2531 (fasl-input)))
2533 (define-cold-fop (fop-symbol-in-package-internal-save (length+flag pkg-index))
2534 (cold-load-symbol length+flag (ref-fop-table (fasl-input) pkg-index)
2535 (fasl-input)))
2537 (define-cold-fop (fop-lisp-symbol-save (length+flag))
2538 (cold-load-symbol length+flag *cl-package* (fasl-input)))
2540 (define-cold-fop (fop-keyword-symbol-save (length+flag))
2541 (cold-load-symbol length+flag *keyword-package* (fasl-input)))
2543 (define-cold-fop (fop-uninterned-symbol-save (length+flag))
2544 (let ((name (make-string (ash length+flag -1))))
2545 (read-string-as-bytes (fasl-input-stream) name)
2546 (push-fop-table (get-uninterned-symbol name) (fasl-input))))
2548 (defun read-cold-symbol-name (symbol)
2549 (base-string-from-core (read-wordindexed symbol sb-vm:symbol-name-slot)))
2551 (define-cold-fop (fop-copy-symbol-save (index))
2552 (let* ((symbol (ref-fop-table (fasl-input) index))
2553 (name
2554 (if (symbolp symbol)
2555 (symbol-name symbol)
2556 (read-cold-symbol-name symbol))))
2557 ;; Genesis performs additional coalescing of uninterned symbols
2558 (push-fop-table (get-uninterned-symbol name) (fasl-input))))
2560 ;;;; cold fops for loading packages
2562 (define-cold-fop (fop-named-package-save (namelen))
2563 (let ((name (make-string namelen)))
2564 (read-string-as-bytes (fasl-input-stream) name)
2565 (push-fop-table (find-package name) (fasl-input))))
2567 ;;;; cold fops for loading vectors
2569 (define-cold-fop (fop-base-string (len))
2570 (let ((string (make-string len)))
2571 (read-string-as-bytes (fasl-input-stream) string)
2572 (string-literal-to-core string)))
2574 #+sb-unicode
2575 (define-cold-fop (fop-character-string (len))
2576 (bug "CHARACTER-STRING[~D] dumped by cross-compiler." len))
2578 (define-cold-fop (fop-vector (size))
2579 (do* ((stack (%fasl-input-stack (fasl-input)))
2580 (stackptr (fop-stack-pop-n stack size) (1+ stackptr))
2581 (result (allocate-vector sb-vm:simple-vector-widetag
2582 size size *dynamic*))
2583 (index sb-vm:vector-data-offset (1+ index))
2584 (end (+ sb-vm:vector-data-offset size)))
2585 ((= index end) (set-readonly result))
2586 (write-wordindexed result index (svref stack stackptr))))
2588 ; (not-cold-fop fop-array) ; the syntax doesn't work
2589 #+nil
2590 ;; This code is unexercised. The only use of FOP-ARRAY is from target-dump.
2591 ;; It would be a shame to delete it though, as it might come in handy.
2592 (define-cold-fop (fop-array)
2593 (let* ((rank (read-word-arg (fasl-input-stream)))
2594 (data-vector (pop-stack))
2595 (result (allocate-object *dynamic*
2596 (+ sb-vm:array-dimensions-offset rank)
2597 sb-vm:other-pointer-lowtag)))
2598 (write-header-data+tag result rank sb-vm:simple-array-widetag)
2599 (write-wordindexed result sb-vm:array-fill-pointer-slot *nil-descriptor*)
2600 (write-wordindexed result sb-vm:array-data-slot data-vector)
2601 (write-wordindexed result sb-vm:array-displacement-slot *nil-descriptor*)
2602 (write-wordindexed result sb-vm:array-displaced-p-slot *nil-descriptor*)
2603 (write-wordindexed result sb-vm:array-displaced-from-slot *nil-descriptor*)
2604 (let ((total-elements 1))
2605 (dotimes (axis rank)
2606 (let ((dim (pop-stack)))
2607 (unless (is-fixnum-lowtag (descriptor-lowtag dim))
2608 (error "non-fixnum dimension? (~S)" dim))
2609 (setf total-elements (* total-elements (descriptor-fixnum dim)))
2610 (write-wordindexed result
2611 (+ sb-vm:array-dimensions-offset axis)
2612 dim)))
2613 (write-wordindexed result
2614 sb-vm:array-elements-slot
2615 (make-fixnum-descriptor total-elements)))
2616 result))
2619 ;;;; cold fops for calling (or not calling)
2621 (defvar *load-time-value-counter*)
2623 (define-cold-fop (fop-funcall (n))
2624 (if (= n 0)
2625 (let ((counter *load-time-value-counter*))
2626 (push (cold-list (cold-intern :load-time-value)
2627 (pop-stack)
2628 (number-to-core counter)) *!cold-toplevels*)
2629 (setf *load-time-value-counter* (1+ counter))
2630 (make-ltv-patch counter))
2631 (let ((des (pop-stack)))
2632 (unless (and (= n 1)
2633 (eq (pop-stack) 'values-specifier-type))
2634 (error "Can't FOP-FUNCALL random stuff in cold load."))
2635 (let ((spec (if (descriptor-p des) (host-object-from-core des) des)))
2636 (ctype-to-core (if (eq spec '*)
2637 *wild-type*
2638 (values-specifier-type spec)))))))
2640 (defun finalize-load-time-value-noise ()
2641 (cold-set '*!load-time-values*
2642 (allocate-vector sb-vm:simple-vector-widetag
2643 *load-time-value-counter*
2644 *load-time-value-counter*)))
2646 (define-cold-fop (fop-funcall-for-effect (n))
2647 (if (= n 0)
2648 (push (pop-stack) *!cold-toplevels*)
2649 (error "Can't FOP-FUNCALL-FOR-EFFECT random stuff in cold load")))
2651 (define-cold-fop (fop-named-constant-set (index))
2652 (push (cold-list (cold-intern :named-constant)
2653 (pop-stack)
2654 (number-to-core index)
2655 (pop-stack))
2656 *!cold-toplevels*))
2659 ;;;; cold fops for fixing up circularities
2661 (define-cold-fop (fop-rplaca (tbl-slot idx))
2662 (let ((obj (ref-fop-table (fasl-input) tbl-slot)))
2663 (write-wordindexed (cold-nthcdr idx obj) 0 (pop-stack))))
2665 (define-cold-fop (fop-rplacd (tbl-slot idx))
2666 (let ((obj (ref-fop-table (fasl-input) tbl-slot)))
2667 (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2669 (define-cold-fop (fop-svset (tbl-slot idx))
2670 (let ((obj (ref-fop-table (fasl-input) tbl-slot)))
2671 (write-wordindexed obj (+ idx sb-vm:vector-data-offset) (pop-stack))))
2673 (define-cold-fop (fop-structset (tbl-slot idx))
2674 (let ((obj (ref-fop-table (fasl-input) tbl-slot)))
2675 (write-wordindexed obj (+ idx sb-vm:instance-slots-offset) (pop-stack))))
2677 (define-cold-fop (fop-nthcdr (n))
2678 (cold-nthcdr n (pop-stack)))
2680 (defun cold-nthcdr (index obj)
2681 (dotimes (i index)
2682 (setq obj (read-wordindexed obj sb-vm:cons-cdr-slot)))
2683 obj)
2685 ;;;; cold fops for loading code objects and functions
2687 (define-cold-fop (fop-fset)
2688 (let ((fn (pop-stack))
2689 (name (pop-stack)))
2690 (cold-fset name fn)))
2692 (define-cold-fop (fop-mset)
2693 (let ((fn (pop-stack))
2694 (specializers (pop-stack))
2695 (qualifiers (pop-stack))
2696 (name (pop-stack)))
2697 ;; Methods that are qualified or are specialized on more than
2698 ;; one argument do not work on start-up, since our start-up
2699 ;; implementation of method dispatch is single dispatch only.
2700 (when (and (null qualifiers)
2701 (= 1 (count-if-not (lambda (x) (eq x t)) (host-object-from-core specializers))))
2702 (push (list (cold-car specializers) fn)
2703 (cdr (or (assoc name *cold-methods*)
2704 (car (push (list name) *cold-methods*))))))))
2706 ;;; Order all initial methods so that the first one whose guard
2707 ;;; returns T is the most specific method. LAYOUT-DEPTHOID is a valid
2708 ;;; sort key for this because we don't have multiple inheritance in
2709 ;;; the system object type lattice.
2710 (defun sort-initial-methods ()
2711 (cold-set
2712 'sb-pcl::*!initial-methods*
2713 (list-to-core
2714 (loop for (gf-name . methods) in *cold-methods*
2715 collect
2716 (cold-cons
2717 (cold-intern gf-name)
2718 (vector-in-core
2719 (loop for (class fun)
2720 ;; Methods must be sorted because we invoke
2721 ;; only the first applicable one.
2722 in (stable-sort methods #'> ; highest depthoid first
2723 :key (lambda (method)
2724 (class-depthoid (warm-symbol (car method)))))
2725 collect
2726 (vector-in-core
2727 (let ((class-symbol (warm-symbol class)))
2728 (list (cold-intern
2729 (predicate-for-specializer class-symbol))
2730 (acond ((gethash class-symbol *cold-layouts*)
2731 (cold-layout-descriptor it))
2733 (aver (predicate-for-specializer class-symbol))
2734 class))
2735 fun))))))))))
2737 (define-cold-fop (fop-fdefn)
2738 (ensure-cold-fdefn (pop-stack)))
2740 (define-cold-fop (fop-known-fun)
2741 (let ((name (pop-stack)))
2742 (or (cold-symbol-function name nil) ; no error if undefined
2743 `(:known-fun . ,name))))
2745 ;;; Setting this variable shows what code looks like before any
2746 ;;; fixups (or function headers) are applied.
2747 (defvar *show-pre-fixup-code-p* nil)
2749 (defun store-named-call-fdefn (code index fdefn)
2750 #+untagged-fdefns
2751 (write-wordindexed/raw code index (- (descriptor-bits fdefn)
2752 sb-vm:other-pointer-lowtag))
2753 #-untagged-fdefns (write-wordindexed code index fdefn))
2755 (define-cold-fop (fop-load-code (header n-code-bytes n-fixup-elts))
2756 (let* ((n-simple-funs (read-unsigned-byte-32-arg (fasl-input-stream)))
2757 (n-fdefns (read-unsigned-byte-32-arg (fasl-input-stream)))
2758 (n-boxed-words (ash header -1))
2759 (n-constants (- n-boxed-words sb-vm:code-constants-offset))
2760 (stack-elts-consumed (+ n-constants 1 n-fixup-elts))
2761 (immobile (oddp header)) ; decode the representation used by dump
2762 ;; The number of constants is rounded up to even (if required)
2763 ;; to ensure that the code vector will be properly aligned.
2764 (aligned-n-boxed-words (align-up n-boxed-words sb-c::code-boxed-words-align))
2765 (stack (%fasl-input-stack (fasl-input)))
2766 (stack-index (fop-stack-pop-n stack stack-elts-consumed))
2767 (des (allocate-cold-descriptor
2768 (or #+immobile-code (and immobile *immobile-text*)
2769 *dynamic*)
2770 (+ (ash aligned-n-boxed-words sb-vm:word-shift) n-code-bytes)
2771 sb-vm:other-pointer-lowtag :code)))
2772 (declare (ignorable immobile))
2773 (write-code-header-words des aligned-n-boxed-words n-code-bytes n-fdefns)
2774 (write-wordindexed des sb-vm:code-debug-info-slot
2775 (svref stack (+ stack-index n-constants)))
2777 (let ((start (+ (descriptor-byte-offset des)
2778 (ash aligned-n-boxed-words sb-vm:word-shift))))
2779 (read-into-bigvec (descriptor-mem des) (fasl-input-stream) start n-code-bytes)
2780 (aver (= (code-n-entries des) n-simple-funs))
2781 (let ((jumptable-word (read-bits-wordindexed des aligned-n-boxed-words)))
2782 (aver (zerop (ash jumptable-word -14)))
2783 ;; assign serialno
2784 (write-wordindexed/raw
2785 des aligned-n-boxed-words
2786 (logior (ash (incf sb-c::*code-serialno*) (byte-position sb-vm::code-serialno-byte))
2787 jumptable-word)))
2788 (when *show-pre-fixup-code-p*
2789 (format *trace-output*
2790 "~&LOAD-CODE: ~d header words, ~d code bytes.~%"
2791 n-boxed-words n-code-bytes)
2792 (do ((i start (+ i sb-vm:n-word-bytes))
2793 (count (floor n-code-bytes sb-vm:n-word-bytes) (1- count)))
2794 ((zerop count))
2795 (format *trace-output*
2796 " ~X: ~V,'.X~%"
2797 (+ i (gspace-byte-address (descriptor-gspace des)))
2798 (* 2 sb-vm:n-word-bytes)
2799 (bvref-word (descriptor-mem des) i)))))
2801 (apply-fixups des stack (+ stack-index (1+ n-constants)) n-fixup-elts)
2802 (let ((header-index sb-vm:code-constants-offset))
2803 (declare (type index header-index stack-index))
2804 (dotimes (fun-index (code-n-entries des))
2805 (let ((fn (%code-entry-point des fun-index)))
2806 (set-simple-fun-layout fn)
2807 #+(or x86 x86-64 arm64) ; store a machine-native pointer to the function entry
2808 ;; note that the bit pattern looks like fixnum due to alignment
2809 (write-wordindexed/raw fn sb-vm:simple-fun-self-slot
2810 (+ (- (descriptor-bits fn) sb-vm:fun-pointer-lowtag)
2811 (ash sb-vm:simple-fun-insts-offset sb-vm:word-shift)))
2812 #-(or x86 x86-64 arm64) ; store a pointer back to the function itself in 'self'
2813 (write-wordindexed fn sb-vm:simple-fun-self-slot fn))
2814 (dotimes (i sb-vm:code-slots-per-simple-fun)
2815 (write-wordindexed des header-index (svref stack stack-index))
2816 (incf header-index)
2817 (incf stack-index)))
2818 (dotimes (i n-fdefns)
2819 (store-named-call-fdefn des header-index (svref stack stack-index))
2820 (incf header-index)
2821 (incf stack-index))
2822 (do () ((>= header-index n-boxed-words))
2823 (let ((constant (svref stack stack-index)))
2824 (cond ((and (consp constant) (eq (car constant) :known-fun))
2825 (push (list* (cdr constant) des header-index) *deferred-known-fun-refs*))
2827 (write-wordindexed des header-index constant))))
2828 (incf header-index)
2829 (incf stack-index)))
2830 des))
2832 (defun resolve-deferred-known-funs ()
2833 (dolist (item *deferred-known-fun-refs*)
2834 (let ((fun (cold-symbol-function (car item)))
2835 (place (cdr item)))
2836 (write-wordindexed (car place) (cdr place) fun))))
2838 (defun %code-entry-point (code-object fun-index)
2839 (let ((fun (sap-int (sap+ (code-instructions code-object)
2840 (%code-fun-offset code-object fun-index)))))
2841 (unless (zerop (logand fun sb-vm:lowtag-mask))
2842 (error "unaligned function entry ~S ~S" code-object fun-index))
2843 (make-descriptor (logior fun sb-vm:fun-pointer-lowtag))))
2845 (define-cold-fop (fop-assembler-code)
2846 (aver (not *cold-assembler-obj*))
2847 (let* ((n-routines (read-word-arg (fasl-input-stream)))
2848 (length (read-word-arg (fasl-input-stream)))
2849 (n-fixup-elts (read-word-arg (fasl-input-stream)))
2850 (rounded-length (round-up length (* 2 sb-vm:n-word-bytes)))
2851 (header-n-words (sb-c::asm-routines-boxed-header-nwords))
2852 (space (or #+immobile-code *immobile-text*
2853 ;; If there is a read-only space, use it, else use static space.
2854 (if (> sb-vm:read-only-space-end
2855 #-darwin-jit sb-vm:read-only-space-start
2856 ;; Always use read-only space on darwin-jit.
2857 #+darwin-jit 0)
2858 *read-only*
2859 *static*)))
2860 (asm-code
2861 (allocate-cold-descriptor
2862 space
2863 (+ (ash header-n-words sb-vm:word-shift) rounded-length)
2864 sb-vm:other-pointer-lowtag)))
2865 (setf *cold-assembler-obj* asm-code)
2866 (write-code-header-words asm-code header-n-words rounded-length 0)
2867 (let ((start (+ (descriptor-byte-offset asm-code)
2868 (ash header-n-words sb-vm:word-shift))))
2869 (read-into-bigvec (descriptor-mem asm-code) (fasl-input-stream) start length))
2870 ;; Write a bignum reference into the boxed constants.
2871 ;; All the backends should do this, as its avoids consing in GENERIC-NEGATE
2872 ;; when the argument is MOST-NEGATIVE-FIXNUM.
2873 #+x86-64 (write-wordindexed asm-code sb-vm:code-constants-offset
2874 (bignum-to-core (- most-negative-fixnum)
2875 #-immobile-space *static*))
2876 ;; Update the name -> address table.
2877 (let (table)
2878 (dotimes (i n-routines)
2879 (let ((offset (descriptor-fixnum (pop-stack)))
2880 (name (pop-stack)))
2881 (push (cons name offset) table)))
2882 ;; Now that we combine all assembler routines into a single code object
2883 ;; at assembly time, they can all be sorted at this point.
2884 ;; We used to combine them with some magic in genesis.
2885 (setq *cold-assembler-routines* (sort table #'< :key #'cdr)))
2886 (let ((stack (%fasl-input-stack (fasl-input))))
2887 (apply-fixups asm-code stack (fop-stack-pop-n stack n-fixup-elts) n-fixup-elts))
2888 #+(or x86 x86-64) ; fill in the indirect call table
2889 (let ((base (code-header-words asm-code))
2890 (index 0))
2891 (dolist (item *cold-assembler-routines*)
2892 ;; Word 0 of code-instructions is the jump table count (the asm routine entrypoints
2893 ;; look to GC exactly like a jump table in any other codeblob)
2894 (let ((entrypoint (lookup-assembler-reference (car item))))
2895 (write-wordindexed/raw asm-code (+ base index 1) entrypoint)
2896 #+immobile-space
2897 (progn
2898 (aver (< index (cold-vector-len *asm-routine-vector*)))
2899 (write-wordindexed/raw *asm-routine-vector*
2900 (+ sb-vm:vector-data-offset index) entrypoint)))
2901 (incf index)))))
2903 ;; The partial source info is not needed during the cold load, since
2904 ;; it can't be interrupted.
2905 (define-cold-fop (fop-note-partial-source-info)
2906 (pop-stack)
2907 (pop-stack)
2908 (pop-stack)
2909 (values))
2911 (define-cold-fop (fop-note-full-calls)
2912 (sb-c::accumulate-full-calls (host-object-from-core (pop-stack)))
2913 (values))
2915 ;;; Target variant of this is defined in 'target-load'
2916 (defun apply-fixups (code-obj fixups index count &aux (end (1- (+ index count))))
2917 (let ((retained-fixups (svref fixups index)))
2918 (write-wordindexed code-obj sb-vm::code-fixups-slot retained-fixups)
2919 (incf index))
2920 (binding* ((alloc-points (svref fixups index) :exit-if-null))
2921 (cold-set 'sb-c::*!cold-allocation-patch-point*
2922 (cold-cons (cold-cons code-obj alloc-points)
2923 (cold-symbol-value 'sb-c::*!cold-allocation-patch-point*))))
2924 (loop
2925 (when (>= index end) (return))
2926 (binding* (((offset kind flavor)
2927 (!unpack-fixup-info (descriptor-integer (svref fixups (incf index)))))
2928 (name (cond ((member flavor '(:code-object :card-table-index-mask)) nil)
2929 (t (svref fixups (incf index)))))
2930 (string
2931 (when (and (descriptor-p name)
2932 (= (descriptor-widetag name) sb-vm:simple-base-string-widetag))
2933 (base-string-from-core name))))
2934 (if (eq flavor :static-call)
2935 (push (list name kind code-obj offset) *cold-static-call-fixups*)
2936 (cold-fixup
2937 code-obj offset
2938 (ecase flavor
2939 (:assembly-routine (lookup-assembler-reference name))
2940 (:assembly-routine* (lookup-assembler-reference name :indirect))
2941 (:foreign (alien-linkage-table-note-symbol string nil))
2942 (:foreign-dataref (alien-linkage-table-note-symbol string t))
2943 (:code-object (descriptor-bits code-obj))
2944 #+sb-thread ; ENSURE-SYMBOL-TLS-INDEX isn't defined otherwise
2945 (:symbol-tls-index (ensure-symbol-tls-index name))
2946 (:layout (cold-layout-descriptor-bits name))
2947 (:layout-id ; SYM is a #<LAYOUT>
2948 (cold-layout-id (gethash (descriptor-bits name)
2949 *cold-layout-by-addr*)))
2950 ;; The machine-dependent code decides how to patch in 'nbits'
2951 (:card-table-index-mask sb-vm::gencgc-card-table-index-nbits)
2952 (:immobile-symbol
2953 ;; an interned symbol is represented by its host symbol,
2954 ;; but an uninterned symbol is a descriptor.
2955 (descriptor-bits (if (symbolp name) (cold-intern name) name)))
2956 (:symbol-value (descriptor-bits (cold-symbol-value name)))
2957 (:fdefn-call ; x86-64 only
2958 (+ (descriptor-bits (ensure-cold-fdefn name))
2959 ;; this jumps to the jump instruction embedded within an fdefn.
2960 ;; (It's a terrible technique which I plan to remove.)
2961 (- 2 sb-vm:other-pointer-lowtag))))
2962 kind flavor))))
2963 code-obj)
2965 ;;;; sanity checking space layouts
2967 (defun check-spaces ()
2968 ;;; Co-opt type machinery to check for intersections...
2969 (let (types)
2970 (flet ((check (start end space)
2971 (when (= start end) ; 0 size is allowed
2972 (return-from check))
2973 (unless (< start end)
2974 (error "Space bounds look bad: ~A = ~X..~X" space start end))
2975 (let ((type (specifier-type `(integer ,start (,end)))))
2976 (dolist (other types)
2977 (unless (eq *empty-type* (type-intersection (cdr other) type))
2978 (error "Space overlap: ~A with ~A" space (car other))))
2979 (push (cons space type) types))))
2980 (check sb-vm:read-only-space-start sb-vm:read-only-space-end :read-only)
2981 #-relocatable-static-space
2982 (check sb-vm:static-space-start sb-vm:static-space-end :static)
2983 #+relocatable-static-space
2984 (check sb-vm:static-space-start (+ sb-vm:static-space-start sb-vm::static-space-size) :static)
2985 (check sb-vm:dynamic-space-start
2986 (+ sb-vm:dynamic-space-start sb-vm::default-dynamic-space-size)
2987 :dynamic)
2988 #+immobile-space
2989 ;; Must be a multiple of 32 because it makes the math a nicer
2990 ;; when computing word and bit index into the 'touched' bitmap.
2991 (aver (zerop (rem sb-vm:fixedobj-space-size (* 32 sb-vm:immobile-card-bytes))))
2992 #+cheneygc
2993 (check sb-vm:dynamic-0-space-start sb-vm:dynamic-0-space-end :dynamic-0)
2994 #-immobile-space
2995 (let ((end (+ sb-vm:alien-linkage-table-space-start sb-vm:alien-linkage-table-space-size)))
2996 (check sb-vm:alien-linkage-table-space-start end :linkage-table)))))
2998 ;;;; emitting C header file
3000 (defun tailwise-equal (string tail)
3001 (and (>= (length string) (length tail))
3002 (string= string tail :start1 (- (length string) (length tail)))))
3004 (defun write-boilerplate (*standard-output*)
3005 (format t "/*~%")
3006 (dolist (line
3007 '("This is a machine-generated file. Please do not edit it by hand."
3009 "This file contains low-level information about the"
3010 "internals of a particular version and configuration"
3011 "of SBCL. It is used by the C compiler to create a runtime"
3012 "support environment, an executable program in the host"
3013 "operating system's native format, which can then be used to"
3014 "load and run 'core' files, which are basically programs"
3015 "in SBCL's own format."))
3016 (format t " *~@[ ~A~]~%" line))
3017 (format t " */~%"))
3019 (defun c-name (string &optional strip)
3020 (delete #\+
3021 (substitute-if #\_ (lambda (c) (member c '(#\- #\/ #\%)))
3022 (remove-if (lambda (c) (position c strip))
3023 string))))
3025 (defun c-symbol-name (symbol &optional strip)
3026 (c-name (symbol-name symbol) strip))
3028 (defun write-makefile-features (*standard-output*)
3029 ;; propagating SB-XC:*FEATURES* into the Makefiles
3030 (dolist (target-feature-name (sort (mapcar #'c-symbol-name sb-xc:*features*)
3031 #'string<))
3032 (format t "LISP_FEATURE_~A=1~%" target-feature-name)))
3034 (defun write-config-h (*standard-output*)
3035 ;; propagating SB-XC:*FEATURES* into C-level #define's
3036 (dolist (target-feature-name (sort (mapcar #'c-symbol-name sb-xc:*features*)
3037 #'string<))
3038 (format t "#define LISP_FEATURE_~A~%" target-feature-name))
3039 (terpri)
3040 ;; and miscellaneous constants
3041 (format t "#define SBCL_TARGET_ARCHITECTURE_STRING ~S~%"
3042 (substitute #\_ #\- (string-downcase (sb-cold::target-platform-keyword))))
3043 (format t "#define SBCL_VERSION_STRING ~S~%"
3044 (sb-xc:lisp-implementation-version))
3045 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
3046 (format t "#ifndef __ASSEMBLER__~2%")
3047 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
3048 (format t "#else /* __ASSEMBLER__ */~2%")
3049 (format t "#define LISPOBJ(thing) thing~2%")
3050 (format t "#endif /* __ASSEMBLER__ */~2%")
3051 (terpri))
3053 (defvar +c-literal-64bit+
3054 #+(and win32 x86-64) "LLU" ; "long" is 32 bits, "long long" is 64 bits
3055 #-(and win32 x86-64) "LU") ; "long" is 64 bits
3057 (defun write-constants-h (*standard-output*)
3058 (let ((constants nil))
3059 (flet ((record (string priority symbol suffix)
3060 (push (list string priority (symbol-value symbol) suffix)
3061 constants)))
3062 ;; writing entire families of named constants
3063 (dolist (package-name '("SB-VM"
3064 ;; We also propagate magic numbers
3065 ;; related to file format,
3066 ;; which live here instead of SB-VM.
3067 "SB-FASL"
3068 ;; Home package of some constants which aren't
3069 ;; in the target Lisp but are propagated to C.
3070 "SB-COREFILE"))
3071 (do-external-symbols (symbol (find-package package-name))
3072 (when (cl:constantp symbol)
3073 (let ((name (symbol-name symbol)))
3074 ;; Older naming convention
3075 (labels ((record-camelcased (prefix string priority)
3076 (record (concatenate 'simple-string
3077 prefix
3078 (delete #\- (string-capitalize string)))
3079 priority symbol ""))
3080 (maybe-record (tail prefix priority)
3081 (when (tailwise-equal name tail)
3082 (record-camelcased prefix
3083 (subseq name 0
3084 (- (length name) (length tail)))
3085 priority))))
3086 (maybe-record "-FLAG" "flag_" 2)
3087 (maybe-record "-TRAP" "trap_" 3)
3088 (maybe-record "-SC-NUMBER" "sc_" 5))
3089 ;; Newer naming convention
3090 (labels ((record-translated (priority large)
3091 (record (c-name name) priority symbol
3092 (if large +c-literal-64bit+ "")))
3093 (maybe-record (suffixes priority &key large)
3094 (when (some (lambda (suffix) (tailwise-equal name suffix))
3095 suffixes)
3096 (record-translated priority large))))
3097 (maybe-record '("-LOWTAG" "-ALIGN") 0)
3098 (maybe-record '("-WIDETAG" "-SHIFT") 1)
3099 (maybe-record '("SHAREABLE+" "SHAREABLE-NONSTD+") 4)
3100 (maybe-record '("-SIZE" "-INTERRUPTS") 6)
3101 (maybe-record '("-START" "-END" "-PAGE-BYTES"
3102 "-CARD-BYTES" "-GRANULARITY")
3103 7 :large t)
3104 (maybe-record '("-CORE-ENTRY-TYPE-CODE") 8)
3105 (maybe-record '("-CORE-SPACE-ID") 9)
3106 (maybe-record '("-CORE-SPACE-ID-FLAG") 9)
3107 (maybe-record '("-GENERATION+") 10))))))
3108 (dolist (c '(sb-impl::+package-id-none+
3109 sb-impl::+package-id-keyword+
3110 sb-impl::+package-id-lisp+
3111 sb-impl::+package-id-user+
3112 sb-impl::+package-id-kernel+
3113 sb-impl::symbol-name-bits))
3114 (record (c-symbol-name c) 3/2 #| arb |# c ""))
3115 ;; Other constants that aren't necessarily grouped into families.
3116 (dolist (c '(sb-kernel:maximum-bignum-length
3117 sb-vm:n-word-bits sb-vm:n-word-bytes
3118 sb-vm:n-lowtag-bits sb-vm:lowtag-mask
3119 sb-vm:n-widetag-bits sb-vm:widetag-mask
3120 sb-vm:n-fixnum-tag-bits sb-vm:fixnum-tag-mask
3121 sb-vm:instance-length-mask
3122 sb-vm:dsd-raw-type-mask
3123 sb-vm:short-header-max-words
3124 sb-vm:array-flags-position
3125 sb-vm:array-rank-position
3126 sb-vm::nil-value-offset))
3127 (record (c-symbol-name c) -1 c ""))
3128 ;; More symbols that doesn't fit into the pattern above.
3129 (dolist (c '(sb-impl::+magic-hash-vector-value+
3130 ;; These next two flags bits use different naming conventions unfortunately,
3131 ;; but one's a vector header bit, the other a layout flag bit.
3132 sb-vm::+vector-alloc-mixed-region-bit+
3133 sb-kernel::+strictly-boxed-flag+
3134 #-sb-thread sb-vm::mixed-region-offset
3135 #-sb-thread sb-vm::cons-region-offset
3136 #-sb-thread sb-vm::boxed-region-offset
3137 sb-vm::nil-symbol-slots-offset
3138 sb-vm::nil-symbol-slots-end-offset
3139 sb-vm::static-space-objects-offset))
3140 (record (c-symbol-name c) 7 #| arb |# c +c-literal-64bit+)))
3141 ;; Sort by <priority, value, alpha> which is TOO COMPLICATED imho.
3142 ;; Priority and then alphabetical would suffice.
3143 (setf constants
3144 (sort constants
3145 (lambda (const1 const2)
3146 (if (= (second const1) (second const2)) ; priority
3147 (if (= (third const1) (third const2)) ; value
3148 (string< (first const1) (first const2))
3149 (< (third const1) (third const2)))
3150 (< (second const1) (second const2))))))
3151 (let ((prev-priority (second (car constants))))
3152 (dolist (const constants)
3153 (destructuring-bind (name priority value suffix) const
3154 (unless (= prev-priority priority)
3155 (terpri)
3156 (setf prev-priority priority))
3157 (when (minusp value)
3158 (error "stub: negative values unsupported"))
3159 (format t "#define ~A ~A~A /* 0x~X */~%" name value suffix value))))
3160 (terpri))
3162 ;; backend-page-bytes doesn't really mean much any more.
3163 ;; It's the granularity at which we can map the core file pages.
3164 (format t "#define BACKEND_PAGE_BYTES ~D~%" sb-c:+backend-page-bytes+)
3165 ;; values never needed in Lisp, so therefore not a defconstant
3166 (progn
3167 (format t "#define MAX_CONSES_PER_PAGE ~D~%" sb-vm::max-conses-per-page)
3168 (format t "#define CARDS_PER_PAGE ~D~%#define GENCGC_CARD_SHIFT ~D~%"
3169 sb-vm::cards-per-page ; this is the "GC" page, not "backend" page
3170 sb-vm::gencgc-card-shift))
3172 (let ((size sb-vm::default-dynamic-space-size))
3173 ;; "-DDEFAULT_DYNAMIC_SPACE_SIZE=n" in CFLAGS will override this.
3174 (format t "#ifndef DEFAULT_DYNAMIC_SPACE_SIZE
3175 #define DEFAULT_DYNAMIC_SPACE_SIZE ~D /* ~:*0x~X */
3176 #endif~2%" size))
3178 ;; writing information about internal errors
3179 ;; Assembly code needs only the constants for UNDEFINED_[ALIEN_]FUN_ERROR
3180 ;; but to avoid imparting that knowledge here, we'll expose all error
3181 ;; number constants except for OBJECT-NOT-<x>-ERROR ones.
3182 (loop for (description name) across sb-c:+backend-internal-errors+
3183 for i from 0
3184 when (stringp description)
3185 do (format t "#define ~A ~D~%" (c-symbol-name name) i))
3187 (terpri)
3189 #+(and win32 x86-64)
3190 (format t "#define WIN64_SEH_DATA_ADDR ((void*)~DUL) /* ~:*0x~X */~%"
3191 sb-vm:win64-seh-data-addr)
3193 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
3194 ;; platforms. If we export this from the SB-VM package, it gets
3195 ;; written out as #define trap_PseudoAtomic, which is confusing as
3196 ;; the runtime treats trap_ as the prefix for illegal instruction
3197 ;; type things. We therefore don't export it, but instead do
3198 #+sparc
3199 (when (boundp 'sb-vm::pseudo-atomic-trap)
3200 (format t
3201 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
3202 sb-vm::pseudo-atomic-trap)
3203 (terpri))
3204 #+(and sb-safepoint (not x86-64))
3205 (progn
3206 (format t "#define GC_SAFEPOINT_PAGE_ADDR (void*)((char*)STATIC_SPACE_START - ~d)~%"
3207 +backend-page-bytes+)
3208 (format t "#define GC_SAFEPOINT_TRAP_ADDR (void*)((char*)STATIC_SPACE_START - ~d)~%"
3209 sb-vm:gc-safepoint-trap-offset))
3211 (dolist (symbol '(sb-vm:float-traps-byte
3212 sb-vm::float-exceptions-byte
3213 sb-vm:float-sticky-bits
3214 sb-vm::float-rounding-mode))
3215 (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
3216 (c-symbol-name symbol)
3217 (sb-xc:byte-position (symbol-value symbol)))
3218 (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
3219 (c-symbol-name symbol)
3220 (sb-xc:mask-field (symbol-value symbol) -1))))
3222 (defun write-regnames-h (stream)
3223 (declare (ignorable stream))
3224 #-x86 ;; too weird - "UESP" (user-mode register ESP) is only
3225 ;; visible in a ucontext, so not a lisp register.
3226 (flet ((prettify (macro list &optional trailing-slash)
3227 (aver (not (member nil list)))
3228 (format stream "#define ~a " macro)
3229 (let ((linelen 100) ; force a line break
3230 (delim nil))
3231 (dolist (item list)
3232 (cond ((> linelen 70)
3233 (format stream "~:[~;,~]\\~% " delim)
3234 (setq delim nil linelen 4)) ; four leading spaces
3235 (delim
3236 (write-string ", " stream)
3237 (incf linelen 2)))
3238 (write-string item stream)
3239 (incf linelen (length item))
3240 (setq delim t))
3241 (when trailing-slash (write-char #\\ stream))
3242 (terpri stream))))
3243 (let ((names sb-vm::*register-names*))
3244 (prettify "REGNAMES" (map 'list (lambda (x) (format nil "~s" x)) names))
3245 (when (boundp 'sb-vm::boxed-regs)
3246 (prettify "BOXED_REGISTERS {"
3247 (mapcar (lambda (i) (format nil "reg_~A" (aref names i)))
3248 (symbol-value 'sb-vm::boxed-regs))
3250 (format stream "}~%")))))
3252 (defun write-errnames-h (stream)
3253 ;; C code needs strings for describe_internal_error()
3254 (format stream "#define INTERNAL_ERROR_NAMES ~{\\~%~S~^, ~}~2%"
3255 (map 'list 'sb-kernel::!c-stringify-internal-error
3256 sb-c:+backend-internal-errors+))
3257 (format stream "#define INTERNAL_ERROR_NARGS {~{~S~^, ~}}~2%"
3258 (map 'list #'cddr sb-c:+backend-internal-errors+)))
3260 (eval-when (:compile-toplevel :load-toplevel :execute)
3261 (import 'sb-vm::primitive-object-variable-length-p))
3263 (defun write-tagnames-h (out)
3264 (labels
3265 ((pretty-name (symbol strip)
3266 (let ((name (string-downcase symbol)))
3267 (substitute #\Space #\-
3268 (subseq name 0 (- (length name) (length strip))))))
3269 (list-sorted-tags (tail)
3270 (loop for symbol being the external-symbols of "SB-VM"
3271 when (and (cl:constantp symbol)
3272 (tailwise-equal (string symbol) tail))
3273 collect symbol into tags
3274 finally (return (sort tags #'< :key #'symbol-value))))
3275 (write-tags (visibility kind limit ash-count)
3276 ;; KIND is the string "-LOWTAG" or "-WIDETAG"
3277 (format out "~%~Aconst char *~(~A~)_names[] = {~%"
3278 visibility (subseq kind 1))
3279 (let ((tags (list-sorted-tags kind)))
3280 (dotimes (i limit)
3281 (let ((known (eql i (ash (or (symbol-value (first tags)) -1) ash-count))))
3282 (if known
3283 (if (string= kind "-WIDETAG")
3284 (format out " ~S" (sb-vm::widetag-string-name (pop tags)))
3285 (format out " \"~A\"" (pretty-name (pop tags) kind)))
3286 (format out " \"unknown [~D]\"" i)))
3287 (unless (eql i (1- limit))
3288 (write-string "," out))
3289 (terpri out)))
3290 (write-line "};" out)))
3291 (format out "#include <stddef.h>~%") ; for NULL
3292 (write-tags "static " "-LOWTAG" sb-vm:lowtag-limit 0)
3293 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
3294 ;; ending with the same 2 bits. (#b10)
3295 (write-tags "" "-WIDETAG" (ash (1+ sb-vm:widetag-mask) -2) -2))
3296 (dolist (name '(symbol ratio complex sb-vm::code simple-fun
3297 closure funcallable-instance
3298 weak-pointer fdefn sb-vm::value-cell))
3299 (format out "static char *~A_slots[] = {~%~{ \"~A: \",~} NULL~%};~%"
3300 (c-name (string-downcase name))
3301 (map 'list (lambda (x) (c-name (string-downcase (sb-vm:slot-name x))))
3302 (let* ((obj (sb-vm::primitive-object name))
3303 (slots (coerce (sb-vm:primitive-object-slots obj) 'list)))
3304 (butlast slots
3305 (if (primitive-object-variable-length-p obj) 1 0))))))
3306 (values))
3308 (defun write-c-print-dispatch (out)
3309 (dolist (flavor '("print" "brief"))
3310 (let ((a (make-array (1+ sb-vm:lowtag-mask))))
3311 (dotimes (i (length a))
3312 (setf (aref a i)
3313 (format nil "~a_~a" flavor
3314 (if (logtest i sb-vm:fixnum-tag-mask) "otherimm" "fixnum"))))
3315 (setf (aref a sb-vm:instance-pointer-lowtag) (format nil "~a_struct" flavor)
3316 (aref a sb-vm:list-pointer-lowtag) (format nil "~a_list" flavor)
3317 (aref a sb-vm:fun-pointer-lowtag) (format nil "~a_fun_or_otherptr" flavor)
3318 (aref a sb-vm:other-pointer-lowtag) (format nil "~a_fun_or_otherptr" flavor))
3319 (format out "static void (*~a_fns[])(lispobj obj) = {~
3320 ~{~% ~a, ~a, ~a, ~a~^,~}~%};~%" flavor (coerce a 'list)))))
3322 (defun write-cast-operator (operator-name c-type-name lowtag stream)
3323 (format stream "static inline struct ~A* ~A(lispobj obj) {
3324 return (struct ~A*)(obj - ~D);~%}~%" c-type-name operator-name c-type-name lowtag)
3325 (case operator-name
3326 (fdefn
3327 (format stream "#define StaticSymbolFunction(x) FdefnFun(x##_FDEFN)
3328 /* Return 'fun' given a tagged pointer to an fdefn. */
3329 static inline lispobj FdefnFun(lispobj fdefn) { return FDEFN(fdefn)->fun; }
3330 extern lispobj decode_fdefn_rawfun(struct fdefn *fdefn);~%"))
3331 (symbol
3332 (format stream "
3333 lispobj symbol_function(struct symbol* symbol);
3334 #include \"~A/vector.h\"
3335 struct vector *symbol_name(struct symbol*);~%
3336 lispobj symbol_package(struct symbol*);~%" (genesis-header-prefix))
3337 (multiple-value-bind (package-id-getter name-bits-extractor name-assigner)
3338 #-compact-symbol
3339 (values (format nil "s->package_id >> ~D" sb-vm:n-fixnum-tag-bits)
3340 "ptr" ; no decoder
3341 "name") ; no encoder
3342 #+compact-symbol ; NAME slot is PACKAGE-ID [16 bits] | STRING [48 bits]
3343 (values (format nil "s->name >> ~D" sb-impl::symbol-name-bits)
3344 (format nil "(ptr & (uword_t)0x~X)"
3345 (mask-field (byte sb-impl::symbol-name-bits 0) -1))
3346 (format nil "(s->name & (uword_t)0x~X) | name"
3347 (mask-field (byte sb-impl::package-id-bits sb-impl::symbol-name-bits)
3348 -1)))
3349 (format stream "static inline int symbol_package_id(struct symbol* s) { return ~A; }~%"
3350 package-id-getter)
3351 (format stream "#define decode_symbol_name(ptr) ~A~%" name-bits-extractor)
3352 (format stream "static inline void set_symbol_name(struct symbol*s, lispobj name) {
3353 s->name = ~A;~%}~%#include ~S~%"
3354 name-assigner
3355 (namestring (merge-pathnames "symbol-tls.inc" (lispobj-dot-h))))))))
3357 (defun write-genesis-thread-h-requisites ()
3358 (write-structure-object (layout-info (find-layout 'sb-thread::thread))
3359 *standard-output* "thread_instance" nil)
3360 (write-structure-object (layout-info (find-layout 'sb-thread::mutex))
3361 *standard-output* "lispmutex" nil)
3362 ;; The os_thread field is either pthread_t or lispobj.
3363 ;; If no threads, then it's lispobj. #+win32 uses lispobj too
3364 ;; but it gets cast to HANDLE upon use.
3365 #+(and unix sb-thread) (format t "#include <pthread.h>~%")
3366 (format t "#include ~S
3368 #define N_HISTOGRAM_BINS_LARGE 32
3369 #define N_HISTOGRAM_BINS_SMALL 32
3370 typedef lispobj size_histogram[2*N_HISTOGRAM_BINS_LARGE+N_HISTOGRAM_BINS_SMALL];
3372 struct thread_state_word {
3373 // - control_stack_guard_page_protected is referenced from
3374 // hand-written assembly code. (grep 'THREAD_STATE_WORD_OFFSET')
3375 // - sprof_enable is referenced with SAPs.
3376 // (grep 'sb-vm:thread-state-word-slot')
3377 char control_stack_guard_page_protected;
3378 char sprof_enable; // statistical CPU profiler switch
3379 char state;
3380 char user_thread_p; // opposite of lisp's ephemeral-p
3382 };~%"
3383 ;; autogenerated files can use full paths to other inclusions
3384 ;; (in case your build system disfavors use of -I compiler options)
3385 (namestring (merge-pathnames "gencgc-alloc-region.h" (lispobj-dot-h)))
3386 #+64-bit " char padding[4];" #-64-bit ""))
3388 (defun write-weak-pointer-manipulators ()
3389 (format t "extern struct weak_pointer *weak_pointer_chain;~%")
3390 ;; weak pointer with no payload size in the header instead has a vector length slot
3391 (format t "static inline int weakptr_vectorp(struct weak_pointer* wp) { ~
3392 return !(wp->header & 0x~X); }~%"
3393 (ash (1- sb-vm:weak-pointer-size) sb-vm:n-widetag-bits))
3394 #+64-bit
3395 (format t "static inline void set_weak_pointer_next(struct weak_pointer *wp, void *next) {
3396 wp->header = ((uword_t)next << 16) | (wp->header & 0xffff);
3398 static inline struct weak_pointer *get_weak_pointer_next(struct weak_pointer *wp) {
3399 // extract a 48-bit pointer from the header
3400 return (void*)(wp->header >> 16);
3401 }~%")
3402 #-64-bit
3403 (format t "#define set_weak_pointer_next(wp, x) wp->next = x
3404 #define get_weak_pointer_next(wp) wp->next~%")
3405 (format t "#define WEAK_POINTER_CHAIN_END (void*)(intptr_t)1
3406 #define reset_weak_pointer_next(wp) set_weak_pointer_next(wp,0)
3407 #define in_weak_pointer_list(wp) (get_weak_pointer_next(wp)!=0)~%"))
3409 (defun write-vector-sap-helpers ()
3410 (format t "static inline char* vector_sap(lispobj v) { return (char*)VECTOR(v)->data; }
3411 static inline unsigned int schar(struct vector* string, int index) {
3412 return (widetag_of(&string->header) == SIMPLE_BASE_STRING_WIDETAG) ?
3413 ((unsigned char*)string->data)[index] :
3414 ((unsigned int*)string->data)[index];
3415 }~%"))
3417 (defun write-sap-initializer ()
3418 (let ((sap-align #+riscv 32 ; not sure why this is larger than normal
3419 #-riscv (* 2 sb-vm:n-word-bytes)))
3420 (format t "
3421 #define DX_ALLOC_SAP(var_name, ptr) \\
3422 lispobj var_name; \\
3423 struct sap _dx_##var_name __attribute__ ((aligned (~D))); \\
3424 do { \\
3425 _dx_##var_name.header = (1 << 8) | SAP_WIDETAG; \\
3426 _dx_##var_name.pointer = (char *)(ptr); \\
3427 var_name = make_lispobj(&_dx_##var_name, OTHER_POINTER_LOWTAG); \\
3428 } while (0)~%"
3429 sap-align)))
3431 (defun output-c-primitive-obj (obj &aux (name (sb-vm:primitive-object-name obj))
3432 (slots (sb-vm:primitive-object-slots obj))
3433 (rest-slot
3434 (if (primitive-object-variable-length-p obj)
3435 (aref slots (1- (length slots))))))
3436 (format t "struct ~A {~%" (c-name (string-downcase name)))
3437 (when (sb-vm:primitive-object-widetag obj)
3438 (format t " lispobj header;~%"))
3439 ;; For data hiding purposes, change the name of vector->length to vector->length_.
3440 ;; This helped catch C code that made assumptions about the length being stored at
3441 ;; 1 word beyond the header as a fixnum, which it isn't if #+ubsan is enabled.
3442 (flet ((mangle-c-slot-name (slot-name)
3443 (if (and (eq name 'vector) (eq slot-name 'length))
3444 "length_"
3445 (c-name (string-downcase slot-name)))))
3446 (dovector (slot slots)
3447 (format t " ~A ~A~@[[1]~];~%"
3448 (getf (cddr slot) :c-type "lispobj")
3449 (mangle-c-slot-name (sb-vm:slot-name slot))
3450 (eq slot rest-slot))))
3451 (format t "};~%"))
3453 (defun write-primitive-object (obj *standard-output*)
3454 (let* ((name (sb-vm:primitive-object-name obj))
3455 (c-name (c-name (string-downcase name)))
3456 (slots (sb-vm:primitive-object-slots obj))
3457 (lowtag (or (symbol-value (sb-vm:primitive-object-lowtag obj)) 0)))
3458 ;; writing primitive object layouts
3459 (flet ((output-c ()
3460 (when (eq name 'sb-vm::thread)
3461 (write-genesis-thread-h-requisites)
3462 (format t "#define INIT_THREAD_REGIONS(x) \\~%")
3463 (let ((tlabs (map 'list
3464 (lambda (x) (c-name (string-downcase (second x))))
3465 (remove-if-not (lambda (x)
3466 (tailwise-equal (string (second x)) "-TLAB"))
3467 slots))))
3468 (format t "~{ gc_init_region(&x->~A)~^,\\~%~}~2%" tlabs))
3469 (when (find 'sb-vm::pseudo-atomic-bits slots :key #'sb-vm:slot-name)
3470 (format t "#define HAVE_THREAD_PSEUDO_ATOMIC_BITS_SLOT 1~2%")
3471 #+(or sparc ppc ppc64) (format t "typedef char pa_bits_t[~d];~2%" sb-vm:n-word-bytes)
3472 #-(or sparc ppc ppc64) (format t "typedef lispobj pa_bits_t;~2%"))
3473 (format t "extern struct thread *all_threads;~%"))
3474 (output-c-primitive-obj obj)
3475 (when (eq name 'sb-vm::code)
3476 (format t "#define CODE_SLOTS_PER_SIMPLE_FUN ~d
3477 static inline struct code* fun_code_header(struct simple_fun* fun) {
3478 return (struct code*)((lispobj*)fun - ((uint32_t)fun->header >> 8));
3479 }~%" sb-vm:code-slots-per-simple-fun)
3480 (write-cast-operator 'function "simple_fun" sb-vm:fun-pointer-lowtag
3481 *standard-output*))
3482 (when (eq name 'vector)
3483 (output-c-primitive-obj (get-primitive-obj 'array))
3484 ;; This is 'sword_t' because we formerly would call fixnum_value() which
3485 ;; is a signed int, but it isn't really; except that I made all C vars
3486 ;; signed to avoid comparison mismatch, and don't want to change back.
3487 (format t "static inline sword_t vector_len(struct vector* v) {")
3488 #+ubsan (format t " return v->header >> ~d; }~%"
3489 (+ 32 sb-vm:n-fixnum-tag-bits))
3490 #-ubsan (format t " return v->length_ >> ~d; }~%"
3491 sb-vm:n-fixnum-tag-bits))
3492 (when (eq name 'weak-pointer)
3493 (write-weak-pointer-manipulators))
3494 (when (eq name 'sb-vm::sap)
3495 (write-sap-initializer))
3496 (when (member name '(cons vector symbol fdefn instance))
3497 (write-cast-operator name c-name lowtag *standard-output*))
3498 (when (eq name 'vector)
3499 (write-vector-sap-helpers)))
3500 (output-asm ()
3501 (format t "/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
3502 (format t " * so they work directly on tagged addresses. */~2%")
3503 (dovector (slot slots)
3504 (format t "#define ~A_~A_OFFSET ~D~%"
3505 (c-symbol-name name)
3506 (c-symbol-name (sb-vm:slot-name slot))
3507 (- (* (sb-vm:slot-offset slot) sb-vm:n-word-bytes) lowtag)))
3508 (format t "#define ~A_SIZE ~d~%"
3509 (string-upcase c-name) (sb-vm:primitive-object-length obj))))
3510 (when (eq name 'sb-vm::thread)
3511 (format t "#define THREAD_HEADER_SLOTS ~d~%" sb-vm::thread-header-slots)
3512 (dovector (x sb-vm::+thread-header-slot-names+)
3513 (let ((s (package-symbolicate "SB-VM" "THREAD-" x "-SLOT")))
3514 (format t "#define ~a ~d~%" (c-name (string s)) (symbol-value s))))
3515 (terpri))
3516 (format t "#ifdef __ASSEMBLER__~2%")
3517 (output-asm)
3518 (format t "~%#else /* __ASSEMBLER__ */~2%")
3519 (format t "#include ~S~%" (lispobj-dot-h))
3520 (output-c)
3521 (format t "~%#endif /* __ASSEMBLER__ */~%"))))
3523 (defun write-hash-table-flag-extractors ()
3524 ;; 'flags' is a packed integer.
3525 ;; See PACK-HT-FLAGS-WEAKNESS and PACK-HT-FLAGS-KIND in hash-table.lisp
3526 (format t "
3527 static inline int hashtable_kind(struct hash_table* ht) { return (ht->uw_flags >> 4) & 3; }
3528 static inline int hashtable_weakp(struct hash_table* ht) { return ht->uw_flags & 8; }
3529 static inline int hashtable_weakness(struct hash_table* ht) { return ht->uw_flags >> 6; }
3530 #define HASHTABLE_KIND_EQL 1~%"))
3532 (defun write-structure-object (dd *standard-output* &optional structure-tag
3533 (assembler-guard t))
3534 (labels
3535 ((cstring (designator) (c-name (string-downcase designator)))
3536 (output (dd structure-tag)
3537 (format t "struct ~A {~%" structure-tag)
3538 (format t " lispobj header; // = word_0_~%")
3539 ;; If the user's data starts at slot index 1, then index 0 is the layout.
3540 (when (= sb-vm:instance-data-start 1)
3541 (format t " lispobj _layout;~%")) ; Avoid name clash with CLASSOID-LAYOUT
3542 ;; Output exactly the number of Lisp words consumed by the structure,
3543 ;; no more, no less. C code can always compute the padded length from
3544 ;; the precise length, but the other way doesn't work.
3545 (let ((names
3546 (coerce (loop for i from sb-vm:instance-data-start below (dd-length dd)
3547 collect (list (format nil "word_~D_" (1+ i))))
3548 'vector)))
3549 (dolist (slot (dd-slots dd))
3550 (let ((cell (aref names (- (dsd-index slot) sb-vm:instance-data-start)))
3551 (name (cstring (dsd-name slot))))
3552 (case (dsd-raw-type slot)
3553 ((t) (rplaca cell name))
3554 ;; remind C programmers which slots are untagged
3555 (sb-vm:signed-word (rplaca cell (format nil "sw_~a" name)))
3556 (sb-vm:word (rplaca cell (format nil "uw_~a" name)))
3557 (t (rplacd cell name)))))
3558 ;; The reason this loops over NAMES instead of DD-SLOTS is that one slot
3559 ;; could output more than one lispword. This would happen with a DOUBLE-FLOAT
3560 ;; on 32-bit machines.
3561 (loop for slot across names
3563 (format t " ~A ~A;~@[ // ~A~]~%"
3564 (cond ((string= (car slot) "next_weak_hash_table")
3565 "struct hash_table*")
3566 (t "lispobj"))
3567 ;; reserved word
3568 (if (string= (car slot) "default") "_default" (car slot))
3569 (cdr slot))))
3570 (format t "};~%")))
3571 (when assembler-guard
3572 (format t "#ifndef __ASSEMBLER__~2%")
3573 (format t "#include ~S~%" (lispobj-dot-h)))
3574 (output dd (or structure-tag (cstring (dd-name dd))))
3575 (when (eq (dd-name dd) 'sb-impl::general-hash-table)
3576 (write-hash-table-flag-extractors))
3577 (when (eq (dd-name dd) 'sb-lockless::split-ordered-list)
3578 (terpri)
3579 (output (layout-info (find-layout 'sb-lockless::list-node)) "list_node")
3580 (terpri)
3581 (output (layout-info (find-layout 'sb-lockless::so-data-node)) "solist_node")
3582 (format t "static inline int so_dummy_node_p(struct solist_node* n) {
3583 return !(n->node_hash & ~D);~%}~%" (sb-vm:fixnumize 1)))
3584 (when assembler-guard
3585 (format t "~%#endif /* __ASSEMBLER__ */~2%"))))
3587 (defun write-thread-init (stream)
3588 (dolist (binding sb-vm::per-thread-c-interface-symbols)
3589 (format stream "INITIALIZE_TLS(~A, ~A);~%"
3590 (c-symbol-name (if (listp binding) (car binding) binding) "*")
3591 (let ((val (if (listp binding) (second binding))))
3592 (if (eq val 't) "LISP_T" val)))))
3594 (defun maybe-relativize (value)
3595 #-relocatable-static-space value
3596 #+relocatable-static-space (- value sb-vm:static-space-start))
3598 (defun write-static-symbols (stream)
3599 (dolist (symbol (cons nil (coerce sb-vm:+static-symbols+ 'list)))
3600 (format stream "#define ~A LISPOBJ(~:[~;STATIC_SPACE_START + ~]0x~X)~%"
3601 ;; FIXME: It would be nice not to need to strip anything
3602 ;; that doesn't get stripped always by C-SYMBOL-NAME.
3603 (if (eq symbol 't) "LISP_T" (c-symbol-name symbol "%*.!"))
3604 #-relocatable-static-space nil
3605 #+relocatable-static-space t
3606 (maybe-relativize
3607 (if *static* ; if we ran GENESIS
3608 ;; We actually ran GENESIS, use the real value.
3609 (descriptor-bits (cold-intern symbol))
3610 (+ sb-vm:nil-value
3611 (if symbol (sb-vm:static-symbol-offset symbol) 0))))))
3612 (format stream "#define LFLIST_TAIL_ATOM LISPOBJ(~:[~;STATIC_SPACE_START + ~]0x~X)~%"
3613 #-relocatable-static-space nil
3614 #+relocatable-static-space t
3615 (maybe-relativize (descriptor-bits *lflist-tail-atom*)))
3616 #+sb-thread
3617 (dolist (binding sb-vm::per-thread-c-interface-symbols)
3618 (let* ((symbol (car (ensure-list binding)))
3619 (c-symbol (c-symbol-name symbol "*")))
3620 (unless (member symbol sb-vm::+common-static-symbols+)
3621 ;; So that "#ifdef thing" works, but not as a C expression
3622 (format stream "#define ~A (*)~%" c-symbol))
3623 (format stream "#define ~A_tlsindex 0x~X~%"
3624 c-symbol (ensure-symbol-tls-index symbol))))
3625 ;; This #define is relative to the start of the fixedobj space to allow heap relocation.
3626 #+compact-instance-header
3627 (format stream "~@{#define LAYOUT_OF_~A (lispobj)(~A_SPACE_START+0x~x)~%~}"
3628 "FUNCTION"
3629 #+permgen "PERMGEN" #-permgen "FIXEDOBJ"
3630 (- (cold-layout-descriptor-bits 'function)
3631 (gspace-byte-address (cold-layout-gspace))))
3632 ;; For immobile code on x86-64, define a constant for the address of the vector of
3633 ;; C-callable fdefns, and then fdefns in terms of indices to that vector.
3634 #+(and x86-64 immobile-code)
3635 (progn
3636 (format stream "#define STATIC_FDEFNS LISPOBJ(0x~X)~%"
3637 (descriptor-bits *c-callable-fdefn-vector*))
3638 (loop for symbol in sb-vm::+c-callable-fdefns+
3639 for index from 0
3640 do (format stream "#define ~A_fdefn ~d~0@*
3641 #define ~A_FDEFN (VECTOR(STATIC_FDEFNS)->data[~d])~%"
3642 (c-symbol-name symbol) index)))
3643 ;; Everybody else can address each fdefn directly.
3644 #-(and x86-64 immobile-code)
3645 (loop for symbol in sb-vm::+c-callable-fdefns+
3646 for index from 0
3648 (format stream "#define ~A_FDEFN LISPOBJ(~:[~;STATIC_SPACE_START + ~]0x~X)~%"
3649 (c-symbol-name symbol)
3650 #-relocatable-static-space nil
3651 #+relocatable-static-space t
3652 (maybe-relativize
3653 (if *static* ; if we ran GENESIS
3654 ;; We actually ran GENESIS, use the real value.
3655 (descriptor-bits (ensure-cold-fdefn symbol))
3656 ;; We didn't run GENESIS, so guess at the address.
3657 (+ sb-vm:nil-value
3658 (* (length sb-vm:+static-symbols+)
3659 (sb-vm:pad-data-block sb-vm:symbol-size))
3660 (* index (sb-vm:pad-data-block sb-vm:fdefn-size))))))))
3662 (defun init-runtime-routines ()
3663 (dolist (symbol sb-vm::*runtime-asm-routines*)
3664 (let* ((des (cold-intern symbol :gspace *static*)))
3665 (cold-set des (make-descriptor (lookup-assembler-reference symbol))))))
3667 (defun write-sc+offset-coding (stream)
3668 (flet ((write-array (name bytes)
3669 (format stream "static struct sc_and_offset_byte ~A[] = {~@
3670 ~{ {~{ ~2D, ~2D ~}}~^,~%~}~@
3671 };~2%"
3672 name
3673 (mapcar (lambda (byte)
3674 (list (byte-size byte) (byte-position byte)))
3675 bytes))))
3676 (format stream "struct sc_and_offset_byte {
3677 int size;
3678 int position;
3679 };~2%")
3680 (write-array "sc_and_offset_sc_number_bytes" sb-c::+sc+offset-scn-bytes+)
3681 (write-array "sc_and_offset_offset_bytes" sb-c::+sc+offset-offset-bytes+)))
3683 ;;;; writing map file
3685 ;;; Write a map file describing the cold load. Some of this
3686 ;;; information is subject to change due to relocating GC, but even so
3687 ;;; it can be very handy when attempting to troubleshoot the early
3688 ;;; stages of cold load.
3689 (defparameter *boilerplate-text* "
3690 (a note about initially undefined function references: These functions
3691 are referred to by code which is installed by GENESIS, but they are not
3692 installed by GENESIS. This is not necessarily a problem; functions can
3693 be defined later, by cold init toplevel forms, or in files compiled and
3694 loaded at warm init, or elsewhere. As long as they are defined before
3695 they are called, everything should be OK. Things are also OK if the
3696 cross-compiler knew their inline definition and used that everywhere
3697 that they were called before the out-of-line definition is installed,
3698 as is fairly common for structure accessors.)")
3700 (defun write-map (*standard-output* &aux (*print-pretty* nil)
3701 (*print-case* :upcase))
3702 (format t "Table of contents~%")
3703 (format t "=================~%")
3704 (let ((sections '("assembler routines" "defined functions" "undefined functions"
3705 "classoids" "layouts"
3706 "packages" "symbols"
3707 "type specifiers"
3708 "linkage table" #+sb-thread "TLS map")))
3709 (dotimes (i (length sections))
3710 (format t "~4<~@R~>. ~A~%" (1+ i) (nth i sections))))
3711 (format t "=================~2%")
3713 (format t "I. assembler routines defined in core image: (base=~x)~2%"
3714 (descriptor-bits *cold-assembler-obj*))
3715 (dolist (routine *cold-assembler-routines*)
3716 (let ((name (car routine)))
3717 (format t "~8,'0X: ~S~%" (lookup-assembler-reference name) name)))
3719 (let ((funs nil) (undefs nil))
3720 (maphash (lambda (name fdefn &aux (fun (cold-fdefn-fun fdefn)))
3721 (let ((fdefn-bits (descriptor-bits fdefn)))
3722 (if (cold-null fun)
3723 (push `(,fdefn-bits ,name) undefs)
3724 (push `(,fdefn-bits ,(descriptor-bits fun) ,name) funs))))
3725 *cold-fdefn-objects*)
3726 (format t "~%~|~%II.A. defined functions (alphabetically):
3728 FDEFN FUNCTION NAME
3729 ========== ========== ====~:{~%~10,'0X ~10,'0X ~S~}~%"
3730 (sort (copy-list funs) #'string<
3731 :key (lambda (x) (fun-name-block-name (caddr x)))))
3732 (format t "~%~|~%II.B. defined functions (numerically):
3734 FDEFN FUNCTION NAME
3735 ========== ========== ====~:{~%~10,'0X ~10,'0X ~S~}~%"
3736 (sort (copy-list funs) #'< :key #'second))
3738 (format t "~%~|~A~%
3739 III. initially undefined function references (alphabetically):
3741 FDEFN NAME
3742 ========== ====~:{~%~10,'0X ~S~}~%"
3743 *boilerplate-text*
3744 (sort undefs
3745 (lambda (a b &aux (pkg-a (sb-xc:package-name (sb-xc:symbol-package a)))
3746 (pkg-b (sb-xc:package-name (sb-xc:symbol-package b))))
3747 (cond ((string< pkg-a pkg-b) t)
3748 ((string> pkg-a pkg-b) nil)
3749 (t (string< a b))))
3750 :key (lambda (x) (fun-name-block-name (cadr x))))))
3752 (format t "~%~|~%IV. classoids:
3754 CELL CLASSOID NAME
3755 ========== ========== ====~%")
3756 (let ((dumped-classoids))
3757 (dolist (x (sort (%hash-table-alist *classoid-cells*) #'string< :key #'car))
3758 (destructuring-bind (name . cell) x
3759 (format t "~10,'0x ~:[ ~;~:*~10,'0X~] ~S~%"
3760 (descriptor-bits cell)
3761 (let ((classoid (read-slot cell :classoid)))
3762 (unless (cold-null classoid)
3763 (push classoid dumped-classoids)
3764 (descriptor-bits classoid)))
3765 name)))
3766 ;; Things sometimes go wrong with dumped classoids, so show a memory dump too
3767 (terpri)
3768 (dolist (classoid dumped-classoids)
3769 (let ((nwords (logand (ash (read-bits-wordindexed classoid 0)
3770 (- sb-vm:instance-length-shift))
3771 sb-vm:instance-length-mask)))
3772 (format t "Classoid @ ~x, ~d words:~%" (descriptor-bits classoid) (1+ nwords))
3773 (dotimes (i (1+ nwords)) ; include the header word in output
3774 (format t "~2d: ~10x~%" i (read-bits-wordindexed classoid i)))
3775 (terpri))))
3777 (format t "~%~|~%V. layout names:~2%")
3778 (format t "~28tBitmap Depth ID Name [Length]~%")
3779 (dolist (pair (sort-cold-layouts))
3780 (let* ((proxy (cdr pair))
3781 (descriptor (cold-layout-descriptor proxy))
3782 (addr (descriptor-bits descriptor)))
3783 (format t "~10,'0X -> ~10,'0X: ~8d ~2D ~5D ~S [~D]~%"
3784 addr
3786 (cold-layout-bitmap proxy)
3787 (cold-layout-depthoid proxy)
3788 (cold-layout-id proxy)
3789 (car pair)
3790 (cold-layout-length proxy))))
3792 (format t "~%~|~%VI. packages:~2%")
3793 (dolist (pair (sort (%hash-table-alist *cold-package-symbols*) #'<
3794 :key (lambda (x) (descriptor-bits (cddr x)))))
3795 (let ((pkg (cddr pair)))
3796 (format t "~x = ~a (ID=~d)~%" (descriptor-bits pkg) (car pair)
3797 (descriptor-fixnum (read-slot pkg :id)))))
3799 (format t "~%~|~%VII. symbols (numerically):~2%")
3800 (mapc (lambda (cell)
3801 (let* ((addr (car cell))
3802 (host-sym (cdr cell))
3803 (val
3804 (unless (or (keywordp host-sym) (null host-sym))
3805 (read-bits-wordindexed (cold-intern host-sym)
3806 sb-vm:symbol-value-slot))))
3807 (format t "~X: ~S~@[ = ~X~]~%" addr host-sym
3808 (unless (eql val sb-vm:unbound-marker-widetag) val))))
3809 (sort (%hash-table-alist *cold-symbols*) #'< :key #'car))
3811 (format t "~%~|~%VIII. parsed type specifiers:~2%")
3812 (format t " [Hash]~%")
3813 (let ((sorted
3814 (sort (%hash-table-alist *host->cold-ctype*) #'<
3815 :key (lambda (x) (descriptor-bits (cdr x))))))
3816 (mapc (lambda (cell &aux (host-obj (car cell)) (addr (descriptor-bits (cdr cell))))
3817 (when (ctype-p host-obj)
3818 (format t "~X: [~vx] ~A = ~S~%"
3819 addr (* 2 sb-vm:n-word-bytes)
3820 (descriptor-fixnum (read-slot (cdr cell) :%bits))
3821 (type-of host-obj) (type-specifier host-obj))))
3822 sorted)
3823 (format t "Lists:~%")
3824 (mapc (lambda (cell &aux (host-obj (car cell)) (addr (descriptor-bits (cdr cell))))
3825 (when (listp host-obj)
3826 (format t "~X: (~{#x~X~^ ~})~%" addr
3827 (mapcar (lambda (x) (descriptor-bits (gethash x *host->cold-ctype*)))
3828 host-obj))))
3829 sorted))
3831 (format t "~%~|~%IX. linkage table:~2%")
3832 (dolist (entry (sort (sb-int:%hash-table-alist *cold-foreign-symbol-table*)
3833 #'< :key #'cdr))
3834 (let ((name (car entry)))
3835 (format t " ~:[ ~;(D)~] ~8x = ~a~%"
3836 (listp name)
3837 (sb-vm::alien-linkage-table-entry-address (cdr entry))
3838 (car (ensure-list name)))))
3840 #+sb-thread
3841 (format t "~%~|~%X. TLS map:~2%~:{~4x ~s~%~}"
3842 (sort *tls-index-to-symbol* #'< :key #'car))
3844 (values))
3846 ;;;; writing core file
3848 (defun output-gspace (gspace data-page core-file verbose)
3849 (force-output core-file)
3850 (let* ((posn (file-position core-file))
3851 (bytes (* (gspace-free-word-index gspace) sb-vm:n-word-bytes))
3852 (page-count (ceiling bytes sb-c:+backend-page-bytes+))
3853 (total-bytes (* page-count sb-c:+backend-page-bytes+)))
3855 (file-position core-file (* sb-c:+backend-page-bytes+ (1+ data-page)))
3856 (when verbose
3857 (format t "writing ~S byte~:P [~S page~:P] from ~S~%"
3858 total-bytes page-count gspace))
3860 ;; Note: It is assumed that the GSPACE allocation routines always
3861 ;; allocate whole pages (of size +backend-page-bytes+) and that any
3862 ;; empty gspace between the free pointer and the end of page will
3863 ;; be zero-filled. This will always be true under Mach on machines
3864 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3865 ;; 8K).
3866 (write-bigvec-as-sequence (gspace-data gspace)
3867 core-file
3868 :end total-bytes
3869 :pad-with-zeros t)
3870 (force-output core-file)
3871 (file-position core-file posn)
3873 ;; Write the directory entry.
3874 (write-words core-file (gspace-identifier gspace) (gspace-free-word-index gspace)
3875 data-page (gspace-byte-address gspace) page-count)
3877 (+ data-page page-count)))
3879 (defconstant bitmap-bytes-per-page
3880 (or #-mark-region-gc 0
3881 (/ sb-vm:gencgc-page-bytes (* sb-vm:cons-size sb-vm:n-word-bytes)
3882 sb-vm:n-byte-bits)))
3884 (defun output-page-table (gspace data-page core-file verbose)
3885 (force-output core-file)
3886 (let* ((data-bytes (* (gspace-free-word-index gspace) sb-vm:n-word-bytes))
3887 (n-ptes (ceiling data-bytes sb-vm:gencgc-page-bytes))
3888 (sizeof-corefile-pte (+ sb-vm:n-word-bytes 2))
3889 (pte-bytes (round-up (* sizeof-corefile-pte n-ptes) sb-vm:n-word-bytes))
3890 (n-code 0)
3891 (n-cons 0)
3892 (n-mixed 0)
3893 (posn (file-position core-file))
3894 (ptes (make-bigvec)))
3895 (file-position core-file (* sb-c:+backend-page-bytes+ (1+ data-page)))
3896 ;; Bitmap, if relevant, precedes the PTEs and consumes a whole number of words
3897 #+mark-region-gc
3898 (dotimes (page-index n-ptes)
3899 (write-words core-file
3900 (page-allocation-bitmap (aref (gspace-page-table gspace) page-index)))
3901 (let ((pte (aref (gspace-page-table gspace) page-index)))
3902 (unless (page-single-object-p pte) ; ordinary pages must be 100% full
3903 (setf (page-words-used pte) sb-vm::gencgc-page-words))))
3904 ;; Write as many PTEs as there are pages used.
3905 ;; A corefile PTE is { uword_t scan_start_offset; page_words_t words_used; }
3906 (expand-bigvec ptes pte-bytes)
3907 (dotimes (page-index n-ptes)
3908 (let* ((pte-offset (* page-index sizeof-corefile-pte))
3909 (pte (aref (gspace-page-table gspace) page-index))
3910 (usage (page-words-used pte))
3911 (sso (if (plusp usage)
3912 (- (* page-index sb-vm:gencgc-page-bytes)
3913 (* (page-scan-start pte) sb-vm:n-word-bytes))
3915 (type-bits (if (plusp usage)
3916 (ecase (page-type pte)
3917 (:code (incf n-code) #b111)
3918 (:list (incf n-cons) #b101)
3919 (:mixed (incf n-mixed) #b011))
3920 0)))
3921 (setf (bvref-word-unaligned ptes pte-offset) (logior sso type-bits))
3922 (setf (bvref-16 ptes (+ pte-offset sb-vm:n-word-bytes))
3923 (logior usage (if (page-single-object-p pte) 1 0)))))
3924 (when verbose
3925 (format t "movable dynamic space: ~d + ~d + ~d cons/code/mixed pages~%"
3926 n-cons n-code n-mixed))
3927 (write-bigvec-as-sequence ptes core-file :end pte-bytes)
3928 (force-output core-file)
3929 (file-position core-file posn)
3930 (write-words core-file
3931 page-table-core-entry-type-code
3932 6 ; = number of words in this core header entry
3933 sb-vm::gencgc-card-table-index-nbits
3934 n-ptes (+ (* n-ptes bitmap-bytes-per-page) pte-bytes) data-page)))
3936 ;;; Create a core file created from the cold loaded image. (This is
3937 ;;; the "initial core file" because core files could be created later
3938 ;;; by executing SAVE-LISP-AND-DIE in a running system, perhaps after we've
3939 ;;; added some functionality to the system.)
3940 (defun write-initial-core-file (filename build-id verbose)
3941 (when verbose
3942 (let ((*print-length* nil)
3943 (*print-level* nil))
3944 (format t "~&SB-XC:*FEATURES* =~&~S~%" sb-xc:*features*))
3945 (format t "[building initial core file in ~S: ~%" filename))
3947 (with-open-file (core-file filename :direction :output
3948 :element-type '(unsigned-byte 8)
3949 :if-exists :rename-and-delete)
3950 (let ((data-page 0))
3951 ;; Write the magic number.
3952 (write-words core-file core-magic)
3954 ;; Write the build ID, which contains a generated string
3955 ;; plus a suffix identifying a certain configuration of the C compiler.
3956 (binding* ((build-id (concatenate
3957 'string
3958 (or build-id
3959 (with-open-file (s "output/build-id.inc") (read s)))
3960 (if (member :msan sb-xc:*features*) "-msan" "")))
3961 ((nwords padding) (ceiling (length build-id) sb-vm:n-word-bytes)))
3962 (declare (type simple-string build-id))
3963 ;; Write BUILD-ID-CORE-ENTRY-TYPE-CODE, the length of the header,
3964 ;; length of the string, then base string chars + maybe padding.
3965 (write-words core-file build-id-core-entry-type-code
3966 (+ 3 nwords) ; 3 = fixed overhead including this word
3967 (length build-id))
3968 (dovector (char build-id) (write-byte (char-code char) core-file))
3969 (dotimes (j (- padding)) (write-byte #xff core-file)))
3971 ;; Write the Directory entry header.
3972 (write-words core-file directory-core-entry-type-code)
3973 (let ((spaces `(,*static*
3974 #+permgen ,*permgen*
3975 #+immobile-space ,@`(,*immobile-fixedobj* ,*immobile-text*)
3976 ,*dynamic* ,*read-only*)))
3977 ;; length = (5 words/space) * N spaces + 2 for header.
3978 (write-words core-file (+ (* (length spaces) 5) 2))
3979 (dolist (space spaces)
3980 (setq data-page (output-gspace space data-page core-file verbose))))
3981 (output-page-table *dynamic* data-page core-file verbose)
3983 ;; Write the initial function.
3984 (let ((initial-fun (descriptor-bits (cold-symbol-function '!cold-init))))
3985 (when verbose (format t "~&/INITIAL-FUN=#X~X~%" initial-fun))
3986 (write-words core-file initial-fun-core-entry-type-code 3 initial-fun))
3988 ;; Write the End entry.
3989 (write-words core-file end-core-entry-type-code 2)))
3991 (when verbose
3992 (format t "done]~%")
3993 (force-output))
3994 (values))
3996 ;;;; the actual GENESIS function
3998 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3999 ;;; and/or information about a Lisp core, therefrom.
4001 ;;; output files arguments (any of which may be NIL to suppress output):
4002 ;;; CORE-FILE-NAME gets a Lisp core.
4003 ;;; C-HEADER-DIR-NAME gets the path in which to place generated headers
4004 ;;; MAP-FILE-NAME gets the name of the textual 'cold-sbcl.map' file
4005 (defun sb-cold:genesis (&key object-file-names foptrace-file-names tls-init
4006 defstruct-descriptions
4007 build-id
4008 core-file-name c-header-dir-name map-file-name
4009 (verbose t))
4011 (when verbose
4012 (format t
4013 "~&beginning GENESIS, ~A~%"
4014 (if core-file-name
4015 ;; Note: This output summarizing what we're doing is
4016 ;; somewhat telegraphic in style, not meant to imply that
4017 ;; we're not e.g. also creating a header file when we
4018 ;; create a core.
4019 (format nil "creating core ~S" core-file-name)
4020 (format nil "creating headers in ~S" c-header-dir-name))))
4022 (let ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
4024 ;; Prefill some linkage table entries perhaps
4025 (loop for (name datap) in sb-vm::*alien-linkage-table-predefined-entries*
4026 do (alien-linkage-table-note-symbol name datap))
4028 ;; Now that we've successfully read our only input file (by
4029 ;; loading the symbol table, if any), it's a good time to ensure
4030 ;; that there'll be someplace for our output files to go when
4031 ;; we're done.
4032 (flet ((frob (filename)
4033 (when filename
4034 (ensure-directories-exist filename :verbose t))))
4035 (frob core-file-name)
4036 (frob map-file-name))
4038 ;; (This shouldn't matter in normal use, since GENESIS normally
4039 ;; only runs once in any given Lisp image, but it could reduce
4040 ;; confusion if we ever experiment with running, tweaking, and
4041 ;; rerunning genesis interactively.)
4042 (do-all-symbols (sym)
4043 (remprop sym 'cold-intern-info))
4045 (check-spaces)
4047 (let ((*load-time-value-counter* 0)
4048 (*cold-fdefn-objects* (make-hash-table :test 'equal))
4049 (*cold-symbols* (make-hash-table :test 'eql)) ; integer keys
4050 (*cold-package-symbols* (make-hash-table :test 'equal)) ; string keys
4051 (*package-graph* nil) ; list of (string . list-of-string)
4052 (*read-only* (make-gspace :read-only
4053 read-only-core-space-id
4054 sb-vm:read-only-space-start))
4055 (*static* (make-gspace :static
4056 static-core-space-id
4057 sb-vm:static-space-start))
4058 #+immobile-space
4059 (*immobile-fixedobj*
4060 ;; Primordial layouts (from INITIALIZE-LAYOUTS) are made before anything else,
4061 ;; but they don't allocate starting from word index 0, because page 0 is reserved
4062 ;; for the **PRIMITIVE-OBJECT-LAYOUTS** vector.
4063 (make-gspace :immobile-fixedobj immobile-fixedobj-core-space-id
4064 sb-vm:fixedobj-space-start
4065 :free-word-index (/ sb-vm:immobile-card-bytes sb-vm:n-word-bytes)))
4066 #+immobile-space
4067 (*immobile-text*
4068 (make-gspace :immobile-text immobile-text-core-space-id sb-vm:text-space-start
4069 :objects (make-array 20000 :fill-pointer 0 :adjustable t)))
4070 #+permgen
4071 (*permgen*
4072 (make-gspace :permgen permgen-core-space-id sb-vm:permgen-space-start
4073 :free-word-index (+ sb-vm:vector-data-offset 256)))
4074 (*dynamic*
4075 (make-gspace :dynamic dynamic-core-space-id sb-vm:dynamic-space-start
4076 :page-table (make-array 100 :adjustable t :initial-element nil)))
4077 (*nil-descriptor*)
4078 (*simple-vector-0-descriptor*)
4079 (*c-callable-fdefn-vector*)
4080 (*classoid-cells* (make-hash-table :test 'eq))
4081 (*host->cold-ctype* (make-hash-table))
4082 (*cold-layouts* (make-hash-table :test 'eq)) ; symbol -> cold-layout
4083 (*cold-layout-by-addr* (make-hash-table :test 'eql)) ; addr -> cold-layout
4084 (*tls-index-to-symbol* nil)
4085 ;; '*COLD-METHODS* is never seen in the target, so does not need
4086 ;; to adhere to the #\! convention for automatic uninterning.
4087 (*cold-methods* nil)
4088 (*!cold-toplevels* nil)
4089 *cold-static-call-fixups*
4090 *cold-assembler-routines*
4091 *cold-assembler-obj*
4092 *deferred-undefined-tramp-refs*
4093 (*deferred-known-fun-refs* nil))
4095 (make-nil-descriptor)
4096 (setf *simple-vector-0-descriptor* (vector-in-core nil))
4098 (when core-file-name
4099 (read-structure-definitions defstruct-descriptions))
4100 ;; Prepare for cold load.
4101 (initialize-layouts)
4102 (initialize-static-space tls-init)
4103 (cold-set 'sb-c::*!cold-allocation-patch-point* *nil-descriptor*)
4104 (let ((n (length sb-kernel::*numeric-aspects-v*)))
4105 (cold-set 'sb-kernel::*numeric-aspects-v*
4106 (allocate-vector sb-vm:simple-vector-widetag n n)))
4107 (cold-set 'sb-kernel::*!initial-ctypes* *nil-descriptor*)
4109 ;; Load all assembler code
4110 (flet ((assembler-file-p (name) (tailwise-equal (namestring name) ".assem-obj")))
4111 (let ((files (remove-if-not #'assembler-file-p object-file-names)))
4112 ;; There should be exactly 1 assembler file, and 1 code object in it.
4113 (when files ; But it's present only in 2nd genesis.
4114 (aver (singleton-p files))
4115 (cold-load (car files) verbose nil)))
4116 (setf object-file-names (remove-if #'assembler-file-p object-file-names)))
4117 (mapc 'funcall *deferred-undefined-tramp-refs*)
4118 (makunbound '*deferred-undefined-tramp-refs*)
4120 (when *cold-assembler-obj*
4121 ;; code-debug-info stores the name->addr hashtable.
4122 ;; It's wrapped in a cons so that read-only space points to static-space
4123 ;; and not to dynamic space. #-darwin-jit doesn't need this hack.
4124 #+darwin-jit
4125 (write-wordindexed *cold-assembler-obj* sb-vm:code-debug-info-slot
4126 (let ((z (make-fixnum-descriptor 0)))
4127 (cold-cons z z *static*)))
4128 (init-runtime-routines))
4130 ;; Initialize the *COLD-SYMBOLS* system with the information
4131 ;; from XC-STRICT-CL.
4132 (let (symbols)
4133 (do-external-symbols (symbol (find-package "XC-STRICT-CL"))
4134 (push symbol symbols))
4135 (setf symbols (sort symbols #'string<))
4136 (dolist (symbol symbols)
4137 (cold-intern (intern (symbol-name symbol) *cl-package*)
4138 :access :external)))
4140 ;; Make LOGICALLY-READONLYIZE no longer a no-op
4141 (setf (symbol-function 'logically-readonlyize)
4142 (symbol-function 'set-readonly))
4144 ;; Cold load.
4145 (dolist (file-name object-file-names)
4146 (push (cold-cons :begin-file (string-literal-to-core file-name))
4147 *!cold-toplevels*)
4148 (cold-load file-name verbose (find file-name foptrace-file-names :test 'equal)))
4150 (sb-cold::check-no-new-cl-symbols)
4152 (when (and verbose core-file-name)
4153 (format t "~&; SB-Loader: (~D~@{+~D~}) methods/other~%"
4154 (reduce #'+ *cold-methods* :key (lambda (x) (length (cdr x))))
4155 (length *!cold-toplevels*)))
4157 (cold-set '*!cold-toplevels* (list-to-core (nreverse *!cold-toplevels*)))
4158 (makunbound '*!cold-toplevels*) ; so no further PUSHes can be done
4160 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
4161 (sort-initial-methods)
4162 (resolve-deferred-known-funs)
4163 (resolve-static-call-fixups)
4164 (foreign-symbols-to-core)
4165 (when core-file-name
4166 (finish-symbols))
4167 (finalize-load-time-value-noise)
4169 ;; Write results to files.
4170 (when map-file-name
4171 (let ((all-objects (gspace-objects *dynamic*)))
4172 (when all-objects
4173 (with-open-file (stream "output/cold-sbcl.fullmap"
4174 :direction :output
4175 :if-exists :supersede)
4176 (format t "~&Headered objects: ~d, Conses: ~d~%"
4177 (count-if-not #'consp all-objects)
4178 (count-if #'consp all-objects))
4179 ;; Code/data separation causes nonlinear allocations
4180 (dovector (x (sort all-objects #'<
4181 :key (lambda (x)
4182 (descriptor-bits
4183 (if (consp x) (car x) x)))))
4184 (let* ((des (if (consp x) (car x) x))
4185 (word (read-bits-wordindexed des 0)))
4186 (format stream "~x: ~x~@[ ~x~]~%"
4187 (logandc2 (descriptor-bits des) sb-vm:lowtag-mask)
4188 word
4189 (when (and (not (consp x))
4190 (>= (logand word sb-vm:widetag-mask) #x80))
4191 (read-bits-wordindexed x 1))))))))
4192 (with-open-file (stream map-file-name :direction :output :if-exists :supersede)
4193 (write-map stream)))
4194 (when core-file-name
4195 (write-initial-core-file core-file-name build-id verbose))
4196 (unless c-header-dir-name
4197 (return-from sb-cold:genesis))
4198 (let ((filename (format nil "~A/Makefile.features" c-header-dir-name)))
4199 (ensure-directories-exist filename)
4200 (with-open-file (stream filename :direction :output :if-exists :supersede)
4201 (write-makefile-features stream)))
4202 (write-c-headers c-header-dir-name))))
4204 (defun write-mark-array-operators (stream &optional (ncards sb-vm::cards-per-page))
4205 #+host-quirks-sbcl (declare (host-sb-ext:muffle-conditions host-sb-ext:compiler-note))
4206 (format stream "#include ~S
4207 extern unsigned char *gc_card_mark;~%" (lispobj-dot-h))
4209 #-soft-card-marks
4210 (progn
4211 (aver (= ncards 1))
4212 #+nil ; these are in gencgc-impl
4213 (progn
4214 (format stream "static inline int cardseq_all_marked_nonsticky(long card) {
4215 return gc_card_mark[card] == CARD_MARKED;~%}~%")
4216 (format stream "static inline int cardseq_any_marked(long card) {
4217 return gc_card_mark[card] != CARD_UNMARKED;~%}~%")
4218 (format stream "static inline int cardseq_any_sticky_mark(long card) {
4219 return gc_card_mark[card] == STICKY_MARK;~%}~%"))
4220 (return-from write-mark-array-operators))
4222 ;; This string has a ~s and ~w so don't use FORMAT on it
4223 (write-string "
4224 /* SIMD-within-a-register algorithms
4226 * from https://graphics.stanford.edu/~seander/bithacks.html
4228 static inline uword_t word_haszero(uword_t word) {
4229 return ((word - 0x0101010101010101LL) & ~word & 0x8080808080808080LL) != 0;
4231 static inline uword_t word_has_stickymark(uword_t word) {
4232 return word_haszero(word ^ 0x0202020202020202LL);
4234 " stream)
4235 ;; In general we have to be wary of wraparound of the card index bits
4236 ;; - see example in comment above the definition of addr_to_card_index() -
4237 ;; but it's OK to treat marks as linearly addressable within a page.
4238 ;; The 'card' argument as supplied to these predicates will be
4239 ;; a page-aligned card, i.e. the first card for its page.
4240 (let* ((n-markwords
4241 ;; This is how many words (of N_WORD_BYTES) of marks there are for the
4242 ;; cards on a page.
4243 (cond ((and (= sb-vm:n-word-bytes 8) (= ncards 32)) 4)
4244 ((and (= sb-vm:n-word-bytes 8) (= ncards 16)) 2)
4245 ((and (= sb-vm:n-word-bytes 8) (= ncards 8)) 1)
4246 ((and (= sb-vm:n-word-bytes 4) (= ncards 8)) 2)
4247 (t (/ ncards sb-vm:n-word-bytes))))
4248 (indices (progn (assert (integerp ncards)) (loop for i below n-markwords collect i))))
4249 (format stream "static inline int cardseq_all_marked_nonsticky(long card) {
4250 uword_t* mark = (uword_t*)&gc_card_mark[card];
4251 return (~{mark[~d]~^ | ~}) == 0;~%}~%" indices)
4252 (format stream "static inline int cardseq_any_marked(long card) {
4253 uword_t* mark = (uword_t*)&gc_card_mark[card];
4254 return (~{mark[~d]~^ & ~}) != (uword_t)-1;~%}~%" indices)
4255 (format stream "static inline int cardseq_any_sticky_mark(long card) {
4256 uword_t* mark = (uword_t*)&gc_card_mark[card];
4257 return ~{word_has_stickymark(mark[~d])~^ || ~};~%}~%" indices)))
4259 (defun write-wired-layout-ids (stream)
4260 (terpri stream)
4261 (dolist (x '((layout "LAYOUT")
4262 (sb-impl::robinhood-hashset "HASHSET")
4263 (sb-impl::robinhood-hashset-storage "HASHSET_STORAGE")
4264 (sb-lockless::list-node "LFLIST_NODE")
4265 (sb-lockless::finalizer-node "FINALIZER_NODE")
4266 (sb-brothertree::unary-node "BROTHERTREE_UNARY_NODE")
4267 (package "PACKAGE")
4268 (hash-table "HASH_TABLE")))
4269 (destructuring-bind (type c-const) x
4270 (format stream "#define ~A_LAYOUT_ID ~D~%"
4271 c-const (sb-kernel::choose-layout-id type nil))))
4272 (terpri stream))
4274 (defun get-primitive-obj (x)
4275 (find x sb-vm:*primitive-objects* :key #'sb-vm:primitive-object-name))
4277 (defparameter numeric-primitive-objects
4278 (remove nil ; SINGLE-FLOAT and/or the SIMD-PACKs might not exist
4279 (mapcar #'get-primitive-obj
4280 '(bignum ratio single-float double-float
4281 complex complex-single-float complex-double-float
4282 simd-pack simd-pack-256))))
4284 (defun write-c-headers (c-header-dir-name)
4285 (macrolet ((out-to (name &body body) ; write boilerplate and inclusion guard
4286 `(actually-out-to ,name (lambda (stream) ,@body))))
4287 (flet ((actually-out-to (name lambda)
4288 ;; A file gets a '.inc' extension, not '.h' for either or both
4289 ;; of two reasons:
4290 ;; - if it isn't self-contained, meaning that in order to #include it,
4291 ;; the consumer of it has to know something about which other headers
4292 ;; need to be #included first.
4293 ;; - it is not intended to be directly consumed because any use would
4294 ;; typically need to wrap each slot in some small calculation
4295 ;; such as native_pointer(), but we don't want to embed the layout
4296 ;; accessors into the autogenerated header. So there would instead be
4297 ;; a "src/runtime/foo.h" which includes "src/runtime/genesis/foo.inc"
4298 ;; 'thread.h' and 'gc-tables.h' violate the naming convention
4299 ;; by being non-self-contained.
4300 (let* ((extension
4301 (cond ((and (stringp name) (position #\. name)) nil)
4302 (t ".h")))
4303 (inclusion-guardp
4304 (string= extension ".h")))
4305 (with-open-file (stream (format nil "~A/~A~@[~A~]"
4306 c-header-dir-name name extension)
4307 :direction :output :if-exists :supersede)
4308 (write-boilerplate stream)
4309 (when inclusion-guardp
4310 (format stream
4311 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~:*~A~%"
4312 (c-name (string-upcase name))))
4313 (funcall lambda stream)
4314 (when inclusion-guardp
4315 (format stream "#endif~%"))))))
4316 (out-to "sbcl" (write-config-h stream) (write-constants-h stream))
4317 (out-to "regnames" (write-regnames-h stream))
4318 (out-to "errnames" (write-errnames-h stream))
4319 (out-to "gc-tables" (sb-vm::write-gc-tables stream))
4320 (out-to "cardmarks" (write-mark-array-operators stream))
4321 (out-to "tagnames" (write-tagnames-h stream))
4322 (out-to "print.inc" (write-c-print-dispatch stream))
4323 (let* ((funinstance (get-primitive-obj 'funcallable-instance))
4324 (catch-block (get-primitive-obj 'sb-vm::catch-block))
4325 (code (get-primitive-obj 'sb-vm::code))
4326 (simple-fun (get-primitive-obj 'sb-kernel:simple-fun))
4327 (array (get-primitive-obj 'array))
4328 (skip `(,funinstance ,catch-block ,code ,simple-fun ,array
4329 ,@numeric-primitive-objects))
4330 (structs (sort (set-difference sb-vm:*primitive-objects* skip) #'string<
4331 :key #'sb-vm:primitive-object-name)))
4332 (out-to "number-types"
4333 (format stream "#include ~S~%" (lispobj-dot-h))
4334 (let ((*standard-output* stream))
4335 (mapc 'output-c-primitive-obj numeric-primitive-objects)))
4336 (dolist (obj structs)
4337 (out-to (string-downcase (sb-vm:primitive-object-name obj))
4338 (write-primitive-object obj stream)
4339 (case (sb-vm:primitive-object-name obj)
4340 (instance
4341 (write-primitive-object funinstance stream)
4342 (write-wired-layout-ids stream)
4343 (write-structure-object (layout-info (find-layout 'layout)) stream
4344 "layout")
4345 (write-cast-operator 'layout "layout"
4346 sb-vm:instance-pointer-lowtag stream)
4347 (format stream "#include ~S~%"
4348 (namestring (merge-pathnames "instance.inc" (lispobj-dot-h)))))
4349 (sb-vm::unwind-block
4350 (write-primitive-object catch-block stream))
4351 (sb-kernel:closure
4352 (write-primitive-object simple-fun stream)
4353 (write-primitive-object code stream)))))
4354 (out-to "primitive-objects"
4355 (format stream "~&#include \"number-types.h\"~%")
4356 (dolist (obj structs)
4357 ;; exclude some not-really-object types
4358 (unless (member (sb-vm:primitive-object-name obj)
4359 '(sb-vm::unwind-block sb-vm::binding))
4360 (format stream "~&#include \"~A.h\"~%"
4361 (string-downcase (sb-vm:primitive-object-name obj)))))))
4362 ;; For purposes of the C code, cast all hash tables as general_hash_table
4363 ;; even if they lack the slots for weak tables.
4364 (out-to "hash-table"
4365 (write-structure-object (layout-info (find-layout 'sb-impl::general-hash-table))
4366 stream "hash_table"))
4367 (out-to "brothertree"
4368 (write-structure-object (layout-info (find-layout 'sb-brothertree::unary-node))
4369 stream "unary_node")
4370 (write-structure-object (layout-info (find-layout 'sb-brothertree::binary-node))
4371 stream "binary_node")
4372 (format stream "extern uword_t brothertree_find_lesseql(uword_t key, lispobj tree);~%"))
4373 (dolist (class '(defstruct-description package
4374 ;; FIXME: probably these should be external?
4375 sb-lockless::split-ordered-list
4376 sb-vm::arena sb-thread::avlnode
4377 sb-c::compiled-debug-info))
4378 (out-to (string-downcase class)
4379 ;; parent/child structs like to be output as one header, child first
4380 (let ((child (case class
4381 (sb-c::compiled-debug-info 'sb-c::compiled-debug-fun)
4382 (defstruct-description 'defstruct-slot-description)
4383 (package 'sb-impl::symbol-table))))
4384 (when child
4385 (write-structure-object (layout-info (find-layout child)) stream)))
4386 (write-structure-object (layout-info (find-layout class))
4387 stream)))
4388 (with-open-file (stream (format nil "~A/thread-init.inc" c-header-dir-name)
4389 :direction :output :if-exists :supersede)
4390 (write-boilerplate stream) ; no inclusion guard, it's not a ".h" file
4391 (write-thread-init stream))
4392 (out-to "static-symbols" (write-static-symbols stream))
4393 (out-to "sc-offset" (write-sc+offset-coding stream)))))
4395 ;;; Invert the action of HOST-CONSTANT-TO-CORE. If STRICTP is given as NIL,
4396 ;;; then we can produce a host object even if it is not a faithful rendition.
4397 (defun host-object-from-core (descriptor &optional (strictp t))
4398 (named-let recurse ((x descriptor))
4399 (when (symbolp x)
4400 (return-from recurse x))
4401 (when (cold-null x)
4402 (return-from recurse nil))
4403 (when (is-fixnum-lowtag (descriptor-lowtag x))
4404 (return-from recurse (descriptor-fixnum x)))
4405 #+64-bit
4406 (when (is-other-immediate-lowtag (descriptor-lowtag x))
4407 (ecase (logand (descriptor-bits x) sb-vm:widetag-mask)
4408 (#.sb-vm:single-float-widetag
4409 (return-from recurse
4410 (unsigned-bits-to-single-float (ash (descriptor-bits x) -32))))))
4411 (ecase (descriptor-lowtag x)
4412 (#.sb-vm:instance-pointer-lowtag
4413 (if strictp (error "Can't invert INSTANCE type") "#<instance>"))
4414 (#.sb-vm:list-pointer-lowtag
4415 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
4416 (#.sb-vm:fun-pointer-lowtag
4417 (if strictp
4418 (error "Can't map cold-fun -> warm-fun")
4419 #+nil ; FIXME: not done, but only needed for debugging genesis
4420 (let ((name (read-wordindexed x sb-vm:simple-fun-name-slot)))
4421 `(function ,(recurse name)))))
4422 (#.sb-vm:other-pointer-lowtag
4423 (let ((widetag (descriptor-widetag x)))
4424 (ecase widetag
4425 (#.sb-vm:symbol-widetag
4426 (if strictp
4427 (warm-symbol x)
4428 (or (gethash (descriptor-bits x) *cold-symbols*) ; first try
4429 (make-symbol (read-cold-symbol-name x)))))
4430 (#.sb-vm:simple-base-string-widetag (base-string-from-core x))
4431 (#.sb-vm:simple-vector-widetag (vector-from-core x #'recurse))
4432 #-64-bit
4433 (#.sb-vm:single-float-widetag
4434 (unsigned-bits-to-single-float (read-bits-wordindexed x 1)))
4435 (#.sb-vm:double-float-widetag
4436 (double-float-from-core x))
4437 (#.sb-vm:bignum-widetag (bignum-from-core x))))))))
4439 ;;; This is for FOP-SPEC-VECTOR which always supplies 0 for the start
4440 (defun read-n-bytes (stream vector start nbytes)
4441 (aver (zerop start))
4442 (let ((start (+ (descriptor-byte-offset vector)
4443 (ash sb-vm:vector-data-offset sb-vm:word-shift))))
4444 (read-into-bigvec (descriptor-mem vector) stream start nbytes)))