Don't require GC barriers for move-from-fixnum+1.
[sbcl.git] / src / compiler / generic / genesis.lisp
blob68f5a021a3858dae8a467771fbf81d1d1411277e
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 *lflist-tail-atom*)
679 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
680 ;;; when the target Lisp starts up
682 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
683 ;;; loadtime value, represented by (CONS KEYWORD ..).
684 (declaim (special *!cold-toplevels* *cold-methods*))
687 ;;;; miscellaneous stuff to read and write the core memory
688 (declaim (ftype (function (descriptor sb-vm:word) descriptor) read-wordindexed))
689 (macrolet ((read-bits ()
690 `(bvref-word (descriptor-mem address)
691 (+ (descriptor-byte-offset address)
692 (ash index sb-vm:word-shift)))))
693 (defun read-bits-wordindexed (address index)
694 (read-bits))
695 (defun read-wordindexed (address index)
696 "Return the value which is displaced by INDEX words from ADDRESS."
697 (make-random-descriptor (read-bits))))
699 (defstruct (ltv-patch (:copier nil) (:constructor make-ltv-patch (index)))
700 (index 0 :read-only t))
701 (declaim (ftype (function (descriptor sb-vm:word (or symbol package descriptor ltv-patch)))
702 write-wordindexed))
703 (macrolet ((write-bits (bits)
704 `(setf (bvref-word (descriptor-mem address)
705 (+ (descriptor-byte-offset address)
706 (ash index sb-vm:word-shift)))
707 ,bits)))
708 (defun write-wordindexed (address index value)
709 "Write VALUE displaced INDEX words from ADDRESS."
710 (write-bits
711 (cond ((ltv-patch-p value)
712 (if (or (= (descriptor-lowtag address) sb-vm:list-pointer-lowtag)
713 (= (descriptor-widetag address) sb-vm:code-header-widetag))
714 (push (cold-list (cold-intern :load-time-value-fixup)
715 address
716 (number-to-core index)
717 (number-to-core (ltv-patch-index value)))
718 *!cold-toplevels*)
719 (bug "Can't patch load-time-value into ~S" address))
720 sb-vm:unbound-marker-widetag)
722 (descriptor-bits
723 ;; If we're passed a symbol as a value then it needs to be interned.
724 (cond ((symbolp value) (cold-intern value))
725 ((packagep value) (cdr (cold-find-package-info (sb-xc:package-name value))))
726 (t value)))))))
728 (defun write-wordindexed/raw (address index bits)
729 (declare (type descriptor address) (type sb-vm:word index)
730 (type (or sb-vm:word sb-vm:signed-word) bits))
731 (write-bits (logand bits sb-ext:most-positive-word))))
733 ;;;; allocating images of primitive objects in the cold core
735 (defun write-header-word (des header-word)
736 ;; In immobile space, all objects start life as pseudo-static as if by 'save'.
737 ;; Refer to depiction of "Immobile object header word" in immobile-space.h
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+))
743 0)))
744 (write-wordindexed/raw des 0 (logior (ash gen 24) header-word))))
746 (defun write-code-header-words (descriptor boxed unboxed n-fdefns)
747 (declare (ignorable n-fdefns))
748 (let ((total-words (align-up (+ boxed (ceiling unboxed sb-vm:n-word-bytes)) 2)))
749 (write-header-word descriptor
750 (logior (ash total-words sb-vm:code-header-size-shift)
751 sb-vm:code-header-widetag)))
752 (write-wordindexed/raw
753 descriptor 1
754 (logior #+64-bit (ash n-fdefns 32) (* boxed sb-vm:n-word-bytes))))
756 (defun write-header-data+tag (des header-data widetag)
757 (write-header-word des (logior (ash header-data sb-vm:n-widetag-bits)
758 widetag)))
760 (defun get-header-data (object)
761 (ash (read-bits-wordindexed object 0) (- sb-vm:n-widetag-bits)))
763 ;;; There are three kinds of blocks of memory in the type system:
764 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
765 ;;; header as all slots, or almost all slots, are descriptors.
766 ;;; This also includes code objects, which are mostly non-descriptors.
767 ;;; * Unboxed objects (bignums): There is a single header word that contains
768 ;;; the length.
769 ;;; * Vector objects: There is a header word with the type, then a word for
770 ;;; the length, then the data.
771 (defun allocate-object (gspace length lowtag)
772 "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
773 pointing to them."
774 (allocate-cold-descriptor gspace (ash length sb-vm:word-shift) lowtag))
775 (defun allocate-otherptr (gspace length widetag)
776 "Allocate LENGTH words in GSPACE and return an ``other-pointer'' descriptor.
777 LENGTH must count the header word itself as 1 word. The header word is
778 initialized with the payload size as (1- LENGTH), and WIDETAG."
779 (let ((des (allocate-cold-descriptor gspace (ash length sb-vm:word-shift)
780 sb-vm:other-pointer-lowtag))
781 (header-word (sb-vm::compute-object-header length widetag)))
782 #+permgen
783 (when (and (= widetag sb-vm:symbol-widetag) (eq gspace *static*))
784 ;; Set the "in-remset" bit so rutime won't call REMEMBER-OBJECT on static symbols
785 (setf header-word (logior header-word (ash 1 31))))
786 (write-header-word des header-word)
787 des))
788 (defvar *simple-vector-0-descriptor*)
789 (defun allocate-vector (widetag length words &optional (gspace *dynamic*))
790 ;; Allocate a vector with WORDS payload words (excluding the header+length).
791 ;; WORDS may be an odd number.
792 ;; Store WIDETAG in the header and LENGTH in the length slot.
793 (when (and (= widetag sb-vm:simple-vector-widetag)
794 (= length 0)
795 (eq gspace *dynamic*)
796 *simple-vector-0-descriptor*)
797 (return-from allocate-vector *simple-vector-0-descriptor*))
798 (emplace-vector (allocate-cold-descriptor
799 gspace
800 (sb-vm:pad-data-block (+ words sb-vm:vector-data-offset))
801 sb-vm:other-pointer-lowtag)
802 widetag length))
803 (defun emplace-vector (des widetag length)
804 #+ubsan
805 (write-header-word des (logior (ash length (+ 32 sb-vm:n-fixnum-tag-bits))
806 widetag))
807 #-ubsan
808 (progn (write-header-data+tag des 0 widetag)
809 (write-wordindexed des sb-vm:vector-length-slot (make-fixnum-descriptor length)))
810 des)
812 ;;; The COLD-LAYOUT is a reflection of or proxy for the words stored
813 ;;; in the core for a cold layout, so that we don't have to extract
814 ;;; them out of the core to compare cold layouts for validity.
815 (defstruct (cold-layout (:constructor %make-cold-layout))
816 id name depthoid length bitmap flags inherits descriptor)
818 ;;; a map from name as a host symbol to the descriptor of its target layout
819 (defvar *cold-layouts*)
820 (defun cold-layout-descriptor-bits (name)
821 (descriptor-bits (cold-layout-descriptor (gethash name *cold-layouts*))))
823 #+compact-instance-header
824 (progn
825 (defun set-simple-fun-layout (fn)
826 (let ((bits (ash (cold-layout-descriptor-bits 'function) 32)))
827 (write-wordindexed/raw fn 0 (logior (read-bits-wordindexed fn 0) bits))))
828 ;; This is called to backpatch layout-of-layout into the primordial layouts.
829 (defun set-instance-layout (thing layout)
830 ;; High half of the header points to the layout
831 (write-wordindexed/raw thing 0 (logior (ash (descriptor-bits layout) 32)
832 (read-bits-wordindexed thing 0))))
833 (defun get-instance-layout (thing)
834 (make-random-descriptor (ash (read-bits-wordindexed thing 0) -32))))
835 #-compact-instance-header
836 (progn
837 (defun set-simple-fun-layout (fn) (declare (ignore fn)))
838 (defun set-instance-layout (thing layout)
839 ;; Word following the header is the layout
840 (write-wordindexed thing sb-vm:instance-slots-offset layout))
841 (defun get-instance-layout (thing)
842 (read-wordindexed thing sb-vm:instance-slots-offset)))
844 ;; Make a structure and set the header word and layout.
845 ;; NWORDS is the payload length (= DD-LENGTH = LAYOUT-LENGTH)
846 (defun allocate-struct (nwords layout &optional (gspace *dynamic*))
847 ;; Add +1 for the header word when allocating.
848 (let ((object (allocate-object gspace (1+ nwords) sb-vm:instance-pointer-lowtag)))
849 ;; Length as stored in the header is the exact number of useful words
850 ;; that follow, as is customary. A padding word, if any is not "useful"
851 (write-header-word object (logior (ash nwords sb-vm:instance-length-shift)
852 sb-vm:instance-widetag))
853 (set-instance-layout object layout)
854 object))
855 (defun type-dd-slots-or-lose (type)
856 (or (car (get type 'dd-proxy)) (error "NO DD-SLOTS: ~S" type)))
857 ;;; Return the value to supply as the first argument to ALLOCATE-STRUCT
858 (defun struct-size (thing)
859 ;; ASSUMPTION: all slots consume 1 storage word
860 (+ sb-vm:instance-data-start (length (type-dd-slots-or-lose thing))))
861 (defun allocate-struct-of-type (type &optional (gspace *dynamic*))
862 (allocate-struct (struct-size type)
863 (cold-layout-descriptor (gethash type *cold-layouts*))
864 gspace))
866 ;;;; copying simple objects into the cold core
868 (defun cold-simple-vector-p (obj)
869 (and (= (descriptor-lowtag obj) sb-vm:other-pointer-lowtag)
870 (= (descriptor-widetag obj) sb-vm:simple-vector-widetag)))
872 (declaim (inline cold-vector-len))
873 (defun cold-vector-len (vector)
874 #+ubsan (ash (read-bits-wordindexed vector 0) (- -32 sb-vm:n-fixnum-tag-bits))
875 #-ubsan (descriptor-fixnum (read-wordindexed vector sb-vm:vector-length-slot)))
877 (macrolet ((vector-data (vector-descriptor)
878 `(+ (descriptor-byte-offset ,vector-descriptor)
879 (* sb-vm:vector-data-offset sb-vm:n-word-bytes))))
880 (defun base-string-to-core (string)
881 "Copy STRING (which must only contain STANDARD-CHARs) into the cold
882 core and return a descriptor to it."
883 ;; (Remember that the system convention for storage of strings leaves an
884 ;; extra null byte at the end to aid in call-out to C.)
885 (let* ((length (length string))
886 (des (allocate-vector sb-vm:simple-base-string-widetag
887 ;; add SAETP-N-PAD-ELEMENT
888 length (ceiling (1+ length) sb-vm:n-word-bytes)
889 *dynamic*))
890 (mem (descriptor-mem des))
891 (byte-base (vector-data des)))
892 (dotimes (i length des) ; was prezeroed, so automatically null-terminated
893 (setf (bvref mem (+ byte-base i)) (char-code (aref string i))))))
895 (defun base-string-from-core (descriptor)
896 (let* ((mem (descriptor-mem descriptor))
897 (byte-base (vector-data descriptor))
898 (len (cold-vector-len descriptor))
899 (str (make-string len)))
900 (dotimes (i len str)
901 (setf (aref str i) (code-char (bvref mem (+ byte-base i)))))))
903 (defun bit-vector-to-core (bit-vector &optional (gspace *dynamic*))
904 (let* ((length (length bit-vector))
905 (nwords (ceiling length sb-vm:n-word-bits))
906 (des (allocate-vector sb-vm:simple-bit-vector-widetag length nwords gspace))
907 (mem (descriptor-mem des))
908 (base (vector-data des)))
909 (let ((byte 0))
910 (dotimes (i length)
911 (let ((byte-bit (rem i 8)))
912 (setf (ldb (byte 1 byte-bit) byte) (bit bit-vector i))
913 (when (= byte-bit 7)
914 (setf (bvref mem (+ base (floor i 8))) byte))))
915 (when (/= 0 (rem length 8))
916 (setf (bvref mem (+ base (floor length 8))) byte))
917 des))))
919 ;;; I would think that all strings we dump are readonly. Maybe not?
920 (defun string-literal-to-core (s) (set-readonly (base-string-to-core s)))
922 ;;; Write the bits of INT to core as if a bignum, i.e. words are ordered from
923 ;;; least to most significant regardless of machine endianness.
924 (defun integer-bits-to-core (int descriptor start nwords)
925 (declare (fixnum nwords))
926 (do ((index 0 (1+ index))
927 (remainder int (ash remainder (- sb-vm:n-word-bits))))
928 ((>= index nwords)
929 (unless (zerop (integer-length remainder))
930 (error "Nonzero remainder after writing ~D using ~D words" int nwords)))
931 (write-wordindexed/raw descriptor
932 (+ start index)
933 (logand remainder sb-ext:most-positive-word))))
935 (defun bignum-to-core (n &optional (space *dynamic*))
936 "Copy a bignum to the cold core."
937 (let* ((words (ceiling (1+ (integer-length n)) sb-vm:n-word-bits))
938 (handle
939 #-bignum-assertions (allocate-otherptr space (1+ words) sb-vm:bignum-widetag)
940 #+bignum-assertions
941 (let* ((aligned-words (1+ (logior words 1))) ; round to odd, slap on a header
942 (physical-words (* aligned-words 2))
943 (handle (allocate-otherptr space physical-words sb-vm:bignum-widetag)))
944 ;; rewrite the header to indicate the logical size
945 (write-wordindexed/raw handle 0 (logior (ash words 8) sb-vm:bignum-widetag))
946 handle)))
947 (integer-bits-to-core n handle sb-vm:bignum-digits-offset words)
948 (aver (= (bignum-from-core handle) n))
949 handle))
951 (defun bignum-from-core (descriptor)
952 (let ((n-words (logand (get-header-data descriptor) #x7fffff))
953 (val 0))
954 (dotimes (i n-words val)
955 (let ((bits (read-bits-wordindexed descriptor
956 (+ i sb-vm:bignum-digits-offset))))
957 ;; sign-extend the highest word
958 (when (= i (1- n-words))
959 (setq bits (sb-vm::sign-extend bits sb-vm:n-word-bits)))
960 (setq val (logior (ash bits (* i sb-vm:n-word-bits)) val))))))
962 (defun number-pair-to-core (first second type)
963 "Makes a number pair of TYPE (ratio or complex) and fills it in."
964 (let ((des (allocate-otherptr *dynamic* 3 type)))
965 (write-wordindexed des 1 first)
966 (write-wordindexed des 2 second)
967 des))
969 (defun write-double-float-bits (address index x)
970 #-64-bit
971 (let ((high-bits (double-float-high-bits x))
972 (low-bits (double-float-low-bits x)))
973 #+little-endian
974 (progn (write-wordindexed/raw address index low-bits)
975 (write-wordindexed/raw address (1+ index) high-bits))
976 #+big-endian
977 (progn (write-wordindexed/raw address index high-bits)
978 (write-wordindexed/raw address (1+ index) low-bits)))
979 #+64-bit
980 (write-wordindexed/raw address index (double-float-bits x))
981 address)
983 (defun float-to-core (x)
984 (etypecase x
985 (single-float
986 (let ((bits (single-float-bits x)))
987 #+64-bit ; 64-bit platforms have immediate single-floats
988 (make-random-descriptor (logior (ash bits 32) sb-vm:single-float-widetag))
989 #-64-bit
990 (let ((des (allocate-otherptr *dynamic* sb-vm:single-float-size
991 sb-vm:single-float-widetag)))
992 (write-wordindexed/raw des sb-vm:single-float-value-slot bits)
993 des)))
994 (double-float
995 (let ((des (allocate-otherptr *dynamic* sb-vm:double-float-size
996 sb-vm:double-float-widetag)))
997 (write-double-float-bits des sb-vm:double-float-value-slot x)))))
999 (defun unsigned-bits-to-single-float (bits)
1000 (make-single-float (sb-vm::sign-extend bits 32)))
1001 (defun double-float-from-core (des)
1002 (let ((bits
1003 #+64-bit (read-bits-wordindexed des 1)
1004 #-64-bit (let* ((word0 (read-bits-wordindexed
1005 des sb-vm:double-float-value-slot))
1006 (word1 (read-bits-wordindexed
1007 des (1+ sb-vm:double-float-value-slot))))
1008 #+little-endian (logior (ash word1 32) word0)
1009 #+big-endian (logior (ash word0 32) word1))))
1010 (sb-impl::%make-double-float (sb-vm::sign-extend bits 64))))
1012 (defun complex-single-float-to-core (num)
1013 (declare (type (complex single-float) num))
1014 (let* ((des (allocate-otherptr *dynamic* sb-vm:complex-single-float-size
1015 sb-vm:complex-single-float-widetag))
1016 (where (+ (descriptor-byte-offset des)
1017 (ash #+64-bit sb-vm:complex-single-float-data-slot
1018 #-64-bit sb-vm:complex-single-float-real-slot
1019 sb-vm:word-shift))))
1020 (setf (bvref-s32 (descriptor-mem des) where) (single-float-bits (realpart num))
1021 (bvref-s32 (descriptor-mem des) (+ where 4)) (single-float-bits (imagpart num)))
1022 des))
1024 (defun complex-double-float-to-core (num)
1025 (declare (type (complex double-float) num))
1026 (let ((des (allocate-otherptr *dynamic* sb-vm:complex-double-float-size
1027 sb-vm:complex-double-float-widetag)))
1028 (write-double-float-bits des sb-vm:complex-double-float-real-slot
1029 (realpart num))
1030 (write-double-float-bits des sb-vm:complex-double-float-imag-slot
1031 (imagpart num))
1032 des))
1034 ;;; Copy the given number to the core.
1035 (defun number-to-core (number)
1036 (typecase number
1037 (integer (or (%fixnum-descriptor-if-possible number)
1038 (bignum-to-core number)))
1039 (ratio (number-pair-to-core (number-to-core (numerator number))
1040 (number-to-core (denominator number))
1041 sb-vm:ratio-widetag))
1042 ((complex single-float) (complex-single-float-to-core number))
1043 ((complex double-float) (complex-double-float-to-core number))
1044 #+long-float
1045 ((complex long-float)
1046 (error "~S isn't a cold-loadable number at all!" number))
1047 (complex (number-pair-to-core (number-to-core (realpart number))
1048 (number-to-core (imagpart number))
1049 sb-vm:complex-rational-widetag))
1050 (float (float-to-core number))
1051 (t (error "~S isn't a cold-loadable number at all!" number))))
1053 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
1054 (defun cold-cons (car cdr &optional (gspace *dynamic*))
1055 (let ((cons (allocate-cold-descriptor gspace (ash 2 sb-vm:word-shift)
1056 sb-vm:list-pointer-lowtag :list)))
1057 (let* ((objs (gspace-objects gspace))
1058 (n (1- (length objs))))
1059 (when objs
1060 (setf (aref objs n) (list (aref objs n)))))
1061 (write-wordindexed cons sb-vm:cons-car-slot car)
1062 (write-wordindexed cons sb-vm:cons-cdr-slot cdr)
1063 cons))
1064 (defun list-to-core (list)
1065 (let ((head *nil-descriptor*)
1066 (tail nil))
1067 ;; A recursive algorithm would have the first cons at the highest
1068 ;; address. This way looks nicer when viewed in ldb.
1069 (loop
1070 (unless list (return head))
1071 (let ((cons (cold-cons (pop list) *nil-descriptor*)))
1072 (if tail (cold-rplacd tail cons) (setq head cons))
1073 (setq tail cons)))))
1074 (defun cold-list (&rest args) (list-to-core args))
1075 (defun cold-list-length (list) ; but no circularity detection
1076 ;; a recursive implementation uses too much stack for some Lisps
1077 (let ((n 0))
1078 (loop (if (cold-null list) (return n))
1079 (incf n)
1080 (setq list (cold-cdr list)))))
1081 (defun cold-push (item symbol)
1082 (cold-set symbol (cold-cons item (cold-symbol-value symbol))))
1084 ;;; Make a simple-vector on the target that holds the specified
1085 ;;; OBJECTS, and return its descriptor.
1086 ;;; This is really "vectorify-list-into-core" but that's too wordy,
1087 ;;; so historically it was "vector-in-core" which is a fine name.
1088 (defun vector-in-core (objects &optional (gspace *dynamic*))
1089 (let* ((size (length objects))
1090 (result (allocate-vector sb-vm:simple-vector-widetag size size gspace)))
1091 (dotimes (index size result)
1092 (write-wordindexed result (+ index sb-vm:vector-data-offset)
1093 (pop objects)))))
1095 (defun word-vector (objects &optional (gspace *dynamic*))
1096 (let* ((size (length objects))
1097 (result (allocate-vector #+64-bit sb-vm:simple-array-unsigned-byte-64-widetag
1098 #-64-bit sb-vm:simple-array-unsigned-byte-32-widetag
1099 size size gspace)))
1100 (dotimes (index size result)
1101 (write-wordindexed/raw result (+ index sb-vm:vector-data-offset) (pop objects)))))
1103 (defun cold-svset (vector index value)
1104 (let ((i (if (integerp index) index (descriptor-fixnum index))))
1105 (write-wordindexed vector (+ i sb-vm:vector-data-offset) value))
1106 value)
1108 (declaim (inline cold-svref))
1109 (defun cold-svref (vector i)
1110 (declare (type index i))
1111 (aver (< i (cold-vector-len vector)))
1112 (read-wordindexed vector (+ i sb-vm:vector-data-offset)))
1113 (defun vector-from-core (descriptor &optional (transform #'identity))
1114 (let* ((len (cold-vector-len descriptor))
1115 (vector (make-array len)))
1116 (dotimes (i len vector)
1117 (setf (aref vector i) (funcall transform (cold-svref descriptor i))))))
1119 ;;;; symbol magic
1121 (defvar *tls-index-to-symbol*)
1122 #+sb-thread
1123 (progn
1124 ;; Simulate *FREE-TLS-INDEX*. This is a word count, not a displacement.
1125 (defvar *genesis-tls-counter* sb-vm::primitive-thread-object-length)
1126 ;; Assign SYMBOL the tls-index INDEX. SYMBOL must be a descriptor.
1127 ;; This is a backend support routine, but the style within this file
1128 ;; is to conditionalize by the target features.
1129 (defun cold-assign-tls-index (symbol index)
1130 (push (list index (warm-symbol symbol)) *tls-index-to-symbol*)
1131 #+64-bit
1132 (write-wordindexed/raw
1133 symbol 0 (logior (ash index 32) (read-bits-wordindexed symbol 0)))
1134 #-64-bit
1135 (write-wordindexed/raw symbol sb-vm:symbol-tls-index-slot index))
1137 ;; Return SYMBOL's tls-index,
1138 ;; choosing a new index if it doesn't have one yet.
1139 (defun ensure-symbol-tls-index (symbol)
1140 (let* ((cold-sym (cold-intern symbol))
1141 (tls-index #+64-bit (ldb (byte 32 32) (read-bits-wordindexed cold-sym 0))
1142 #-64-bit (read-bits-wordindexed cold-sym sb-vm:symbol-tls-index-slot)))
1143 (unless (plusp tls-index)
1144 (let ((next (prog1 *genesis-tls-counter* (incf *genesis-tls-counter*))))
1145 (setq tls-index (ash next sb-vm:word-shift))
1146 (cold-assign-tls-index cold-sym tls-index)))
1147 tls-index)))
1149 (defvar *cold-symbol-gspace* (or #+permgen '*permgen*
1150 #+immobile-space '*immobile-fixedobj*
1151 '*dynamic*))
1152 (defun assign-symbol-hash (descriptor wordindex name)
1153 ;; "why not just call sb-c::symbol-name-hash?" you ask? because: no symbol.
1154 (let ((name-hash (sb-c::calc-symbol-name-hash name (length name))))
1155 #-salted-symbol-hash
1156 (write-wordindexed descriptor wordindex (make-fixnum-descriptor name-hash))
1157 #+salted-symbol-hash
1158 (let* ((salt (sb-impl::murmur3-fmix-word (descriptor-bits descriptor)))
1159 (prng-byte sb-impl::symbol-hash-prng-byte)
1160 ;; 64-bit: Low 4 bytes to high 4 bytes of slot
1161 ;; 32-bit: name-hash to high 29 bits
1162 ;; plus salt the hash any way you want as long as the build is reproducible.
1163 (name-hash-pos (+ (byte-size prng-byte) (byte-position prng-byte)))
1164 (hash (logior (ash name-hash name-hash-pos) (mask-field prng-byte salt))))
1165 (write-wordindexed/raw descriptor wordindex hash))))
1167 (defun set-symbol-pkgid (symbol pkg &optional (nil-slots-magic 0))
1168 (let ((wordindex (+ #-64-bit sb-vm:symbol-package-id-slot nil-slots-magic)))
1169 (write-wordindexed/raw
1170 symbol wordindex
1171 #+64-bit (logior (read-bits-wordindexed symbol wordindex)
1172 (ash pkg #+x86-64 8 ; unaligned uint16_t
1173 #-x86-64 16)) ; naturally-aligned uint16_t
1174 #-64-bit (ash pkg sb-vm:n-fixnum-tag-bits))))
1176 ;;; Allocate (and initialize) a symbol.
1177 ;;; Even though all symbols are the same size now, I still envision the possibility
1178 ;;; of reducing gensyms to 4 words, though I'm not sure what to do if information
1179 ;;; is later attached (function, value, plist)
1180 (defun allocate-symbol (size cold-package name &key (gspace (symbol-value *cold-symbol-gspace*)))
1181 (declare (simple-string name))
1182 (let ((symbol (allocate-otherptr gspace size sb-vm:symbol-widetag)))
1183 (when core-file-name
1184 (let* ((cold-name (string-literal-to-core name))
1185 (pkg-id (if cold-package
1186 (descriptor-fixnum (read-slot cold-package :id))
1187 sb-impl::+package-id-none+)))
1188 (assign-symbol-hash symbol sb-vm:symbol-hash-slot name)
1189 (write-wordindexed symbol sb-vm:symbol-value-slot *unbound-marker*)
1190 (write-wordindexed symbol sb-vm:symbol-info-slot *nil-descriptor*)
1191 (set-symbol-pkgid symbol pkg-id)
1192 (write-wordindexed symbol sb-vm:symbol-name-slot cold-name)))
1193 symbol))
1195 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
1196 ;;; descriptor of a cold symbol or (in an abbreviation for the
1197 ;;; most common usage pattern) an ordinary symbol, which will be
1198 ;;; automatically cold-interned.
1199 (defun cold-set (symbol-or-symbol-des value)
1200 (let ((symbol-des (etypecase symbol-or-symbol-des
1201 (descriptor symbol-or-symbol-des)
1202 (symbol (cold-intern symbol-or-symbol-des)))))
1203 (write-wordindexed symbol-des sb-vm:symbol-value-slot value)))
1204 (defun cold-symbol-value (symbol)
1205 (let ((val (read-wordindexed (cold-intern symbol) sb-vm:symbol-value-slot)))
1206 (if (= (descriptor-bits val) sb-vm:unbound-marker-widetag)
1207 (error "Symbol value of ~a is unbound." symbol)
1208 val)))
1209 (defun cold-fdefn-fun (cold-fdefn)
1210 (let ((fun (read-wordindexed cold-fdefn sb-vm:fdefn-fun-slot)))
1211 (if (zerop (descriptor-bits fun)) *nil-descriptor* fun)))
1213 #+linkage-space
1214 (progn
1215 (defvar *fname-table*
1216 (make-array 6000 :initial-element 0 :fill-pointer 1 :adjustable nil))
1218 (defun coerce-to-cold-fname (fname)
1219 (cond ((symbolp fname) (cold-intern fname))
1220 ((= (descriptor-lowtag fname) sb-vm:list-pointer-lowtag)
1221 (ensure-cold-fdefn fname))
1222 ((member (descriptor-widetag fname) `(,sb-vm:symbol-widetag ,sb-vm:fdefn-widetag))
1223 fname)
1224 (t (bug "coerce-to-cold-fname ~s" fname))))
1226 (defun fname-linkage-index (fname) ; modeled on the code in 'src/code/linkage-space'
1227 (let ((des (coerce-to-cold-fname fname)))
1228 (cond ((cold-null des) 0)
1229 ((= (descriptor-widetag des) sb-vm:fdefn-widetag)
1230 (ldb (byte sb-vm:n-linkage-index-bits 16) (read-bits-wordindexed des 0)))
1232 (ldb (byte sb-vm:n-linkage-index-bits 0)
1233 (read-bits-wordindexed des sb-vm:symbol-hash-slot))))))
1235 (defun ensure-linkage-index (fname)
1236 (let* ((des (coerce-to-cold-fname fname))
1237 (index (fname-linkage-index des)))
1238 (when (zerop index)
1239 (setq index (vector-push-extend des *fname-table*))
1240 (if (= (descriptor-widetag des) sb-vm:fdefn-widetag)
1241 (let ((header (read-bits-wordindexed des 0))) ; store to fdefn header
1242 (write-wordindexed/raw des 0 (logior (ash index 16) header)))
1243 (let ((hash (read-bits-wordindexed des sb-vm:symbol-hash-slot)))
1244 (write-wordindexed/raw des sb-vm:symbol-hash-slot (logior hash index))))
1245 (assert (= (fname-linkage-index fname) index)))
1246 index)))
1248 ;;;; layouts and type system pre-initialization
1250 ;;; Since we want to be able to dump structure constants and
1251 ;;; predicates with reference layouts, we need to create layouts at
1252 ;;; cold-load time. We use the name to intern layouts by, and dump a
1253 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
1254 ;;; initialization can find them. The only thing that's tricky [sic --
1255 ;;; WHN 19990816] is initializing layout's layout, which must point to
1256 ;;; itself.
1258 ;;; a map from DESCRIPTOR-BITS of cold layouts (as descriptors)
1259 ;;; to the host's COLD-LAYOUT proxy for that layout.
1260 (defvar *cold-layout-by-addr*)
1262 ;;; Initial methods require that we sort possible methods by the depthoid.
1263 ;;; Most of the objects printed in cold-init are ordered hierarchically in our
1264 ;;; type lattice; the major exceptions are ARRAY and VECTOR at depthoid -1.
1265 ;;; Of course we need to print VECTORs because a STRING is a vector,
1266 ;;; and vector has to precede ARRAY. Kludge it for now.
1267 (defun class-depthoid (class-name) ; DEPTHOID-ish thing, any which way you can
1268 (case class-name
1269 (vector 1/2)
1270 (array 1/4)
1271 ;; The depthoid of CONDITION has to be faked. The proper value is 1.
1272 ;; But STRUCTURE-OBJECT is also at depthoid 1, and its predicate
1273 ;; is %INSTANCEP (which is too weak), so to select the correct method
1274 ;; we have to make CONDITION more specific.
1275 ;; In reality it is type disjoint from structure-object.
1276 (condition 2)
1278 (acond ((gethash class-name *cold-layouts*)
1279 (cold-layout-depthoid it))
1280 ((info :type :compiler-layout class-name)
1281 (layout-depthoid it))
1283 (error "Unknown depthoid for ~S" class-name))))))
1285 (declaim (ftype function read-slot %write-slots write-slots))
1286 (flet ((infer-metadata (x)
1287 (type-dd-slots-or-lose
1288 (cold-layout-name (gethash (descriptor-bits (get-instance-layout x))
1289 *cold-layout-by-addr*))))
1290 (find-slot (slots initarg)
1291 (let ((dsd (or (find initarg slots
1292 :test (lambda (x y) (eq x (keywordicate (dsd-name y)))))
1293 (error "No slot for ~S in ~S" initarg slots))))
1294 (values (+ sb-vm:instance-slots-offset (dsd-index dsd))
1295 (dsd-raw-type dsd)))))
1297 (defun %write-slots (metadata cold-object &rest assignments)
1298 (aver (evenp (length assignments)))
1299 (loop for (initarg value) on assignments by #'cddr
1300 do (multiple-value-bind (index repr) (find-slot metadata initarg)
1301 (ecase repr
1302 ((t) (write-wordindexed cold-object index value))
1303 ((word sb-vm:signed-word)
1304 (write-wordindexed/raw cold-object index value)))))
1305 cold-object)
1307 (defun write-slots (cold-object &rest assignments)
1308 (apply #'%write-slots (infer-metadata cold-object) cold-object assignments))
1310 ;; For symmetry, the reader takes an initarg, not a slot name.
1311 (defun read-slot (cold-object slot-initarg)
1312 (multiple-value-bind (index repr)
1313 (find-slot (infer-metadata cold-object) slot-initarg)
1314 (ecase repr
1315 ((t) (read-wordindexed cold-object index))
1316 (word (read-bits-wordindexed cold-object index))
1317 (sb-vm:signed-word
1318 (sb-vm::sign-extend (read-bits-wordindexed cold-object index)
1319 sb-vm:n-word-bits))))))
1321 (defun read-structure-definitions (pathname)
1322 (with-open-file (stream pathname)
1323 (loop
1324 (let ((ch (peek-char t stream)))
1325 (when (char= ch #\;)
1326 (return))
1327 (let* ((classoid-name (read stream))
1328 (*package* (find-package (cl:symbol-package classoid-name)))
1329 (flags+depthoid+inherits (read stream)))
1330 (setf (get classoid-name 'dd-proxy)
1331 (list (map 'vector
1332 (lambda (x)
1333 (destructuring-bind (bits name acc) x
1334 (sb-kernel::make-dsd name nil acc bits nil)))
1335 (read stream))
1336 :flags (car flags+depthoid+inherits)
1337 :depthoid (cadr flags+depthoid+inherits)
1338 :inherits (cddr flags+depthoid+inherits))))))))
1340 (defvar *vacuous-slot-table*)
1341 (defun cold-layout-gspace ()
1342 (cond ((boundp '*permgen*) *permgen*)
1343 ((boundp '*immobile-fixedobj*) *immobile-fixedobj*)
1344 (t *dynamic*)))
1345 (declaim (ftype (function (symbol layout-depthoid integer index integer descriptor)
1346 descriptor)
1347 make-cold-layout))
1349 (defun make-cold-layout (name depthoid flags length bitmap inherits)
1350 ;; Layouts created in genesis can't vary in length due to the number of ancestor
1351 ;; types in the IS-A vector. They may vary in length due to the bitmap word count.
1352 ;; But we can at least assert that there is one less thing to worry about.
1353 (aver (<= depthoid sb-kernel::layout-id-vector-fixed-capacity))
1354 (aver (cold-simple-vector-p inherits))
1355 (let* ((fixed-words (sb-kernel::type-dd-length layout))
1356 (bitmap-words (ceiling (1+ (integer-length bitmap)) sb-vm:n-word-bits))
1357 (result (allocate-struct (+ fixed-words bitmap-words)
1358 (or (awhen (gethash 'layout *cold-layouts*)
1359 (cold-layout-descriptor it))
1360 (make-fixnum-descriptor 0))
1361 (cold-layout-gspace)))
1362 (this-id (sb-kernel::choose-layout-id name (logtest flags +condition-layout-flag+)))
1363 (hash (make-fixnum-descriptor (sb-impl::hash-layout-name name))))
1365 (let ((proxy (%make-cold-layout :id this-id
1366 :name name
1367 :depthoid depthoid
1368 :length length
1369 :bitmap bitmap
1370 :flags flags
1371 :inherits inherits
1372 :descriptor result)))
1373 ;; Make two different ways to look up the proxy object -
1374 ;; by name or by descriptor-bits.
1375 (setf (gethash (descriptor-bits result) *cold-layout-by-addr*) proxy
1376 (gethash name *cold-layouts*) proxy))
1377 (unless core-file-name (return-from make-cold-layout result))
1379 ;; Can't use the easier WRITE-SLOTS unfortunately because bootstrapping is hard
1380 (let ((layout-metadata (type-dd-slots-or-lose 'layout)))
1382 #+64-bit
1383 (%write-slots layout-metadata result
1384 :flags (sb-kernel::pack-layout-flags depthoid length flags))
1385 #-64-bit
1386 (%write-slots layout-metadata result
1387 :depthoid (make-fixnum-descriptor depthoid)
1388 :length (make-fixnum-descriptor length)
1389 :flags flags)
1391 (%write-slots layout-metadata result
1392 :clos-hash hash
1393 :invalid *nil-descriptor*
1394 :inherits inherits
1395 :%info *nil-descriptor*)
1397 (when (member name '(null list symbol pathname))
1398 ;; Assign an empty slot-table. Why this is done only for four
1399 ;; classoids is ... too complicated to explain here in a few words,
1400 ;; but revision 18c239205d9349abc017b07e7894a710835c5205 broke it.
1401 ;; Keep this in sync with MAKE-SLOT-TABLE in pcl/slots-boot.
1402 (%write-slots layout-metadata result
1403 :slot-table (if (boundp '*vacuous-slot-table*)
1404 *vacuous-slot-table*
1405 (setq *vacuous-slot-table*
1406 (host-constant-to-core '#(1 nil))))))
1408 (let ((byte-offset (+ (descriptor-byte-offset result) (sb-vm::id-bits-offset))))
1409 (when (logtest flags +structure-layout-flag+)
1410 (loop for i from 2 below (cold-vector-len inherits)
1411 do (setf (bvref-s32 (descriptor-mem result) byte-offset)
1412 (cold-layout-id (gethash (descriptor-bits (cold-svref inherits i))
1413 *cold-layout-by-addr*)))
1414 (incf byte-offset 4)))
1415 (setf (bvref-s32 (descriptor-mem result) byte-offset) this-id)))
1417 (integer-bits-to-core bitmap result (1+ fixed-words) bitmap-words)
1419 result))
1421 (defun predicate-for-specializer (type-name)
1422 (let ((classoid (find-classoid type-name nil)))
1423 (typecase classoid
1424 (structure-classoid
1425 (dd-predicate-name (sb-kernel::layout-%info (classoid-layout classoid))))
1426 (built-in-classoid
1427 (let ((translation (specifier-type type-name)))
1428 (aver (not (contains-unknown-type-p translation)))
1429 (let ((predicate (find translation sb-c::*backend-type-predicates*
1430 :test #'type= :key #'car)))
1431 (cond (predicate (cdr predicate))
1432 ((eq type-name 'stream) 'streamp)
1433 ((eq type-name 'pathname) 'pathnamep)
1434 ((eq type-name 't) 'constantly-t)
1435 (t (error "No predicate for builtin: ~S" type-name)))))))))
1437 ;;; Map from host object to target object
1438 (defvar *host->cold-ctype*)
1440 ;;; NUMTYPE-ASPECTS are stored in a fixed-size vector.
1441 ;;; During genesis they are created on demand.
1442 ;;; (I'm not sure whether all or only some are created)
1443 (defun numtype-aspects-to-core (val)
1444 (let* ((index (sb-kernel::numtype-aspects-id val))
1445 (vector (cold-symbol-value 'sb-kernel::*numeric-aspects-v*))
1446 (cold-obj (cold-svref vector index)))
1447 (if (eql (descriptor-bits cold-obj) 0)
1448 (write-slots (cold-svset vector index
1449 (allocate-struct-of-type (type-of val)))
1450 :id (make-fixnum-descriptor (sb-kernel::numtype-aspects-id val))
1451 :complexp (sb-kernel::numtype-aspects-complexp val)
1452 :class (sb-kernel::numtype-aspects-class val)
1453 :precision (sb-kernel::numtype-aspects-precision val))
1454 cold-obj)))
1456 (defvar *dsd-index-cache* nil)
1457 (defun dsd-index-cached (type-name slot-name)
1458 (let ((cell (find-if (lambda (x)
1459 (and (eq (caar x) type-name) (eq (cdar x) slot-name)))
1460 *dsd-index-cache*)))
1461 (if cell
1462 (cdr cell)
1463 (let* ((dd-slots (car (get type-name 'dd-proxy)))
1464 (dsd (find slot-name dd-slots :key #'dsd-name))
1465 (index (dsd-index dsd)))
1466 (push (cons (cons type-name slot-name) index) *dsd-index-cache*)
1467 index))))
1469 (defun ctype-to-core (obj)
1470 (declare (type (or ctype xset list) obj))
1471 (cond
1472 ((null obj) *nil-descriptor*)
1473 ((gethash obj *host->cold-ctype*))
1474 ((listp obj)
1475 (if (and (proper-list-p obj) (every #'sb-kernel:ctype-p obj))
1476 ;; Be sure to preserving shared substructure.
1477 ;; There is no circularity, so inserting into the map after copying works fine
1478 (setf (gethash obj *host->cold-ctype*) (list-to-core (mapcar #'ctype-to-core obj)))
1479 (host-constant-to-core obj))) ; numeric bound, array dimension, etc
1481 (when (classoid-p obj) (aver (not (sb-kernel::undefined-classoid-p obj))))
1482 (let* ((host-type (type-of obj))
1483 ;; Precompute a list of slots that should be initialized to a
1484 ;; trivially dumpable constant in lieu of whatever complicated
1485 ;; substructure it currently holds.
1486 (overrides
1487 (typecase obj
1488 (classoid
1489 (let ((slots-to-omit
1490 `(;; :predicate will be patched in during cold init.
1491 (,(dsd-index-cached 'built-in-classoid 'sb-kernel::predicate) .
1492 ,(make-random-descriptor sb-vm:unbound-marker-widetag))
1493 (,(dsd-index-cached 'classoid 'sb-kernel::subclasses) . nil)
1494 ;; Even though (gethash (classoid-name obj) *cold-layouts*) may exist,
1495 ;; we nonetheless must set LAYOUT to NIL or else warm build fails
1496 ;; in the twisty maze of class initializations.
1497 (,(dsd-index-cached 'classoid 'layout) . nil))))
1498 (if (typep obj 'built-in-classoid)
1499 slots-to-omit
1500 ;; :predicate is not a slot. Don't mess up the object
1501 ;; by omitting a slot at the same index as it.
1502 (cdr slots-to-omit))))))
1503 (dd-slots (type-dd-slots-or-lose host-type))
1504 ;; ASSUMPTION: all slots consume 1 storage word
1505 (dd-len (+ sb-vm:instance-data-start (length dd-slots)))
1506 (result (allocate-struct-of-type host-type)))
1507 (setf (gethash obj *host->cold-ctype*) result) ; record it
1508 ;; Dump the slots.
1509 (do ((index sb-vm:instance-data-start (1+ index)))
1510 ((= index dd-len) result)
1511 (let* ((dsd (find index dd-slots :key #'dsd-index))
1512 (override (assq index overrides))
1513 (reader (dsd-accessor-name dsd)))
1514 (ecase (dsd-raw-type dsd)
1515 ((t)
1516 (write-wordindexed
1517 result
1518 (+ sb-vm:instance-slots-offset index)
1519 (if override
1520 (or (cdr override) *nil-descriptor*)
1521 (let ((val (funcall reader obj)))
1522 (funcall (typecase val
1523 ((or ctype xset list) #'ctype-to-core)
1524 (sb-kernel::numtype-aspects #'numtype-aspects-to-core)
1525 (t #'host-constant-to-core))
1526 val)))))
1527 ((word sb-vm:signed-word)
1528 (write-wordindexed/raw result (+ sb-vm:instance-slots-offset index)
1529 (or (cdr override) (funcall reader obj)))))))
1530 (cond ((classoid-p obj) ; Place classoid into its classoid-cell.
1531 (let ((cell (cold-find-classoid-cell (classoid-name obj) :create t)))
1532 (write-slots cell :classoid result)))
1533 ((ctype-p obj)
1534 ;; If OBJ belongs in a hash container, then deduce which
1535 (let* ((hashset (sb-kernel::ctype->hashset-sym obj))
1536 (preload
1537 (cond ((and hashset (hashset-find (symbol-value hashset) obj))
1538 hashset)
1539 ((and (member-type-p obj)
1540 ;; NULL is a hardwired case in the MEMBER type constructor
1541 (neq obj (specifier-type 'null))
1542 (type-singleton-p obj))
1543 'sb-kernel::*eql-type-cache*))))
1544 (when preload ; Record it
1545 (cold-push (cold-cons result preload) 'sb-kernel::*!initial-ctypes*)))))
1546 result))))
1548 (defun initialize-layouts ()
1549 (flet ((chill-layout (name &rest inherits)
1550 ;; Check that the number of specified INHERITS matches
1551 ;; the length of the layout's inherits in the cross-compiler.
1552 (let ((warm-layout (info :type :compiler-layout name)))
1553 (assert (eql (length (layout-inherits warm-layout))
1554 (length inherits)))
1555 (make-cold-layout name
1556 (layout-depthoid warm-layout)
1557 (layout-flags warm-layout)
1558 (layout-length warm-layout)
1559 (layout-bitmap warm-layout)
1560 (vector-in-core inherits)))))
1561 (let* ((t-layout (chill-layout 't))
1562 (s-o-layout (chill-layout 'structure-object t-layout))
1563 (layout-layout (chill-layout 'layout t-layout s-o-layout)))
1564 (when core-file-name
1565 (dolist (instance (list t-layout s-o-layout layout-layout))
1566 (set-instance-layout instance layout-layout)))
1567 (chill-layout 'function t-layout)
1568 (chill-layout 'package t-layout s-o-layout)
1569 (let* ((sequence (chill-layout 'sequence t-layout))
1570 (list (chill-layout 'list t-layout sequence))
1571 (symbol (chill-layout 'symbol t-layout)))
1572 (chill-layout 'null t-layout sequence list symbol))
1573 (chill-layout 'sb-lockless::list-node t-layout s-o-layout)
1574 (chill-layout 'stream t-layout))))
1576 ;;;; interning symbols in the cold image
1578 ;;; a map from package name as a host string to
1579 ;;; ((external-symbols . internal-symbols) . cold-package-descriptor)
1580 (defvar *cold-package-symbols*)
1581 (declaim (type hash-table *cold-package-symbols*))
1582 (defvar *package-graph*)
1584 ;;; preincrement on use. the first non-preassigned ID is 5
1585 (defvar *package-id-count* 4)
1587 ;;; Initialize the cold package named by NAME. The information is
1588 ;;; usually derived from the host package of the same name, except
1589 ;;; where the host package does not reflect the target package
1590 ;;; information, as for COMMON-LISP, KEYWORD, and COMMON-LISP-USER.
1591 (defun initialize-cold-package (cold-package name)
1592 (multiple-value-bind (nicknames docstring id shadow use-list)
1593 (cond ((string= name "COMMON-LISP")
1594 (values '("CL")
1595 "public: home of symbols defined by the ANSI language specification"
1596 sb-impl::+package-id-lisp+
1598 '()))
1599 ((string= name "KEYWORD")
1600 (values '()
1601 "public: home of keywords"
1602 sb-impl::+package-id-keyword+
1604 '()))
1605 ((string= name "COMMON-LISP-USER")
1606 (values '("CL-USER")
1607 "public: the default package for user code and data"
1608 sb-impl::+package-id-user+
1610 ;; ANSI encourages us to put extension packages in
1611 ;; the USE list of COMMON-LISP-USER.
1612 '("COMMON-LISP" "SB-ALIEN" "SB-DEBUG"
1613 "SB-EXT" "SB-GRAY" "SB-PROFILE")))
1615 (let ((package (find-package name)))
1616 (values (package-nicknames package)
1617 (documentation package t)
1618 (if (string= name "SB-KERNEL")
1619 sb-impl::+package-id-kernel+
1620 (incf *package-id-count*))
1621 (sort (package-shadowing-symbols package) #'string<)
1622 ;; SB-COREFILE is not actually part of
1623 ;; the use list for SB-FASL. It's
1624 ;; just needed for Genesis.
1625 (if (string= name "SB-FASL")
1626 (remove (find-package "SB-COREFILE")
1627 (package-use-list package))
1628 (package-use-list package))))))
1629 (let ((strings (mapcar #'string-literal-to-core (cons name nicknames))))
1630 (write-slots cold-package
1631 :id (make-fixnum-descriptor id)
1632 :keys (vector-in-core (list (list-to-core strings)))
1633 :%name *nil-descriptor*
1634 :%bits (make-fixnum-descriptor
1635 (if (system-package-p name)
1636 sb-impl::+initial-package-bits+
1638 :doc-string (if (and docstring #-sb-doc nil)
1639 (string-literal-to-core docstring)
1640 *nil-descriptor*)))
1641 (push (cons name (sort (mapcar 'sb-xc:package-name use-list) #'string<)) *package-graph*)
1642 ;; COLD-INTERN AVERs that the package has an ID, so delay writing
1643 ;; the shadowing-symbols until the package is ready.
1644 (write-slots cold-package
1645 :%shadowing-symbols (list-to-core
1646 (mapcar 'cold-intern shadow)))))
1648 (defun cold-find-package-info (package-name)
1649 ;; Create package info on demand.
1650 (or (gethash package-name *cold-package-symbols*)
1651 (let* ((cold-package (allocate-struct-of-type 'package))
1652 (info (cons (cons nil nil) cold-package)))
1653 (write-slots cold-package :%used-by *nil-descriptor*)
1654 (setf (gethash package-name *cold-package-symbols*) info)
1655 (initialize-cold-package cold-package package-name)
1656 info)))
1658 (defvar *classoid-cells*)
1659 (defun cold-find-classoid-cell (name &key create)
1660 (aver (eq create t))
1661 (or (gethash name *classoid-cells*)
1662 (setf (gethash name *classoid-cells*)
1663 (write-slots (allocate-struct-of-type 'sb-kernel::classoid-cell)
1664 :name name
1665 :pcl-class *nil-descriptor*
1666 :classoid *nil-descriptor*))))
1668 ;;; a map from descriptors to symbols, so that we can back up. The key
1669 ;;; is the address in the target core.
1670 (defvar *cold-symbols*)
1671 (declaim (type hash-table *cold-symbols*))
1673 (defun set-readonly (vector)
1674 (write-wordindexed/raw vector 0 (logior (read-bits-wordindexed vector 0)
1675 (ash sb-vm:+vector-shareable+
1676 sb-vm:array-flags-position)))
1677 vector)
1679 (defvar *uninterned-symbol-table* (make-hash-table :test #'equal))
1680 ;; This coalesces references to uninterned symbols, which is allowed because
1681 ;; "similar-as-constant" is defined by string comparison, and since we only have
1682 ;; base-strings during Genesis, there is no concern about upgraded array type.
1683 ;; There is a subtlety of whether coalescing may occur across files
1684 ;; - the target compiler doesn't and couldn't - but here it doesn't matter.
1685 (defun get-uninterned-symbol (name)
1686 (ensure-gethash name *uninterned-symbol-table*
1687 (allocate-symbol sb-vm:symbol-size nil name)))
1689 ;;; Dump the target representation of HOST-VALUE,
1690 ;;; the type of which is in a restrictive set.
1691 (defun host-constant-to-core (host-value &optional helper)
1692 (let ((visited (make-hash-table :test #'eq)))
1693 (named-let target-representation ((value host-value))
1694 (unless (typep value '(or symbol number descriptor))
1695 (let ((found (gethash value visited)))
1696 (cond ((eq found :pending)
1697 (bug "circular constant?")) ; Circularity not permitted
1698 (found
1699 (return-from target-representation found))))
1700 (setf (gethash value visited) :pending))
1701 (setf (gethash value visited)
1702 (typecase value
1703 (descriptor value)
1704 (symbol (if (cl:symbol-package value)
1705 (cold-intern value)
1706 (get-uninterned-symbol (string value))))
1707 (number (number-to-core value))
1708 (string (base-string-to-core value))
1709 (simple-bit-vector (bit-vector-to-core value))
1710 (cons (cold-cons (target-representation (car value))
1711 (target-representation (cdr value))))
1712 (simple-vector
1713 (vector-in-core (map 'list #'target-representation value)))
1715 (or (and helper (funcall helper value))
1716 (error "host-constant-to-core: can't convert ~S"
1717 value))))))))
1719 ;; Look up the target's descriptor for #'FUN where FUN is a host or cold symbol.
1720 (defun cold-symbol-function (symbol &optional (errorp t))
1721 (let* ((symbol (if (symbolp symbol) symbol (warm-symbol symbol)))
1722 (f #+linkage-space (read-wordindexed (cold-intern symbol) sb-vm:symbol-fdefn-slot)
1723 #-linkage-space (cold-fdefn-fun (ensure-cold-fdefn symbol))))
1724 (cond ((and (not (cold-null f)) (/= (descriptor-bits f) 0)) f)
1725 (errorp (error "Expected a definition for ~S in cold load" symbol))
1726 (t nil))))
1728 ;;; Return a handle on an interned symbol. If necessary allocate the
1729 ;;; symbol and record its home package.
1730 (defun cold-intern (symbol
1731 &key (access nil)
1732 (gspace (symbol-value *cold-symbol-gspace*))
1733 &aux (name (symbol-name symbol))
1734 (package (sb-xc:symbol-package symbol)))
1735 ;; Symbols that are logically in COMMON-LISP but accessed through the SB-XC package
1736 ;; need to be re-interned since the cold-intern-info must be associated with
1737 ;; exactly one of the possible lookalikes, not both. The re-interned symbol
1738 ;; is usually homed in CL:, but might be homed in SB-XC. When the symbols identity
1739 ;; matters to the type system (floating-point specifiers), we never want to see the
1740 ;; host's symbol; the canonical package shall be SB-XC. We can figure out the
1741 ;; canonical home package by finding the symbol via the XC-STRICT-CL package.
1742 (cond ((eq package *cl-package*)
1743 (setq symbol (find-symbol name (canonical-home-package name))))
1744 ((not (or (eq package *keyword-package*)
1745 (= (mismatch (cl:package-name package) "SB-") 3)))
1746 (bug "~S in bad package for target: ~A" symbol package)))
1748 (or (get symbol 'cold-intern-info)
1749 (let* ((pkg-info
1750 (when core-file-name (cold-find-package-info (sb-xc:package-name package))))
1751 (handle (allocate-symbol sb-vm:symbol-size
1752 (cdr pkg-info) name :gspace gspace)))
1753 (when pkg-info
1754 (aver (not (zerop (descriptor-fixnum (read-slot (cdr pkg-info) :id))))))
1755 (setf (get symbol 'cold-intern-info) handle)
1756 ;; maintain reverse map from target descriptor to host symbol
1757 (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1758 #+sb-thread
1759 (let ((index (info :variable :wired-tls symbol)))
1760 (when (integerp index) ; thread slot or known TLS
1761 (cold-assign-tls-index handle index)))
1762 ;; Steps that only make sense when writing a core file
1763 (when core-file-name
1764 (record-accessibility (or access (nth-value 1 (find-symbol name package)))
1765 pkg-info handle package symbol)
1766 (when (eq package *keyword-package*)
1767 (cold-set handle handle)))
1768 handle)))
1770 (defun record-accessibility (accessibility target-pkg-info symbol-descriptor
1771 &optional host-package host-symbol)
1772 (let ((access-lists (car target-pkg-info)))
1773 (case accessibility
1774 (:external (push symbol-descriptor (car access-lists)))
1775 (:internal (push symbol-descriptor (cdr access-lists)))
1776 (t (error "~S inaccessible in package ~S" host-symbol host-package)))))
1778 ;;; a hash table mapping from fdefinition names to descriptors of cold
1779 ;;; objects
1781 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1782 ;;; we want to have only one entry per name, this must be an 'EQUAL
1783 ;;; hash table, not the default 'EQL.
1784 (defvar *cold-fdefn-objects*)
1786 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1787 ;;; It might be nice to put NIL on a readonly page by itself to prevent unsafe
1788 ;;; code from destroying the world with (RPLACx nil 'kablooey)
1789 (defun make-nil-descriptor ()
1790 (gspace-claim-n-words *static* (/ (- sb-vm::nil-value-offset
1791 (* 2 sb-vm:n-word-bytes)
1792 sb-vm:list-pointer-lowtag)
1793 sb-vm:n-word-bytes))
1794 (let* ((des (allocate-otherptr *static* (1+ sb-vm:symbol-size) 0))
1795 (nil-val (make-descriptor (+ (descriptor-bits des)
1796 (* 2 sb-vm:n-word-bytes)
1797 (- sb-vm:list-pointer-lowtag
1798 ;; ALLOCATE-OTHERPTR always adds in
1799 ;; OTHER-POINTER-LOWTAG, so subtract it.
1800 sb-vm:other-pointer-lowtag))))
1801 (initial-info (cold-cons nil-val nil-val)))
1802 (aver (= (descriptor-bits nil-val) sb-vm:nil-value))
1804 (setf *nil-descriptor* nil-val
1805 (gethash (descriptor-bits nil-val) *cold-symbols*) nil
1806 (get nil 'cold-intern-info) nil-val)
1808 ;; Alter the first word to 0 instead of the symbol size. It reads as a fixnum,
1809 ;; but is meaningless. In practice, Lisp code can not utilize the fact that NIL
1810 ;; has a widetag; any use of NIL-as-symbol must pre-check for NIL. Consider:
1811 ;; 50100100: 0000000000000000 = 0
1812 ;; 50100108: 000000000000002D <- widetag = #x2D
1813 ;; 50100110: 0000000050100117
1814 ;; 50100118: 0000000050100117
1815 ;; 50100120: 0000001000000007 = (NIL . #<SB-INT:PACKED-INFO len=3 {1000002FF3}>)
1816 ;; 50100128: 000100100000400F
1817 ;; 50100130: 0000000000000000 = 0
1819 ;; Indeed *(char*)(NIL-0xf) = #x2D, /* if little-endian */
1820 ;; so why can't we exploit this to improve SYMBOLP? Hypothetically:
1821 ;; if (((ptr & 7) == 7) && *(char*)(ptr-15) == SYMBOL_WIDETAG) { }
1822 ;; which is true of NIL and all other symbols, but wrong, because it assumes
1823 ;; that _any_ cons cell could be accessed at a negative displacement from its
1824 ;; base address. Only NIL (viewed as a cons) has this property.
1825 ;; Otherwise we would be reading random bytes or inaccessible memory. Finally,
1826 ;; the above sequence would not necessarily decrease the instruction count!
1827 ;; Those points aside, gencgc correctly calls scav_symbol() on NIL.
1829 (when core-file-name
1830 (let ((name (string-literal-to-core "NIL")))
1831 (write-wordindexed/raw des 0 0)
1832 ;; The header-word for NIL "as a symbol" contains a length + widetag.
1833 (write-wordindexed/raw des 1 sb-vm:symbol-widetag)
1834 ;; Write the CAR and CDR of nil-as-cons
1835 (let* ((nil-cons-base-addr (- sb-vm:nil-value sb-vm:list-pointer-lowtag))
1836 (nil-cons-car-offs (- nil-cons-base-addr (gspace-byte-address *static*)))
1837 (nil-cons-cdr-offs (+ nil-cons-car-offs sb-vm:n-word-bytes)))
1838 (setf (bvref-word (descriptor-mem des) nil-cons-car-offs) sb-vm:nil-value
1839 (bvref-word (descriptor-mem des) nil-cons-cdr-offs) sb-vm:nil-value))
1840 ;; Assign HASH if and only if NIL's hash is address-insensitive
1841 #+(or relocatable-static-space (not 64-bit))
1842 (assign-symbol-hash des (+ 1 sb-vm:symbol-hash-slot) "NIL")
1843 (write-wordindexed des (+ 1 sb-vm:symbol-info-slot) initial-info)
1844 (set-symbol-pkgid des sb-impl::+package-id-lisp+ 1)
1845 (write-wordindexed des (+ 1 sb-vm:symbol-name-slot) name)))
1846 nil))
1848 ;;; Since the initial symbols must be allocated before we can intern
1849 ;;; anything else, we intern those here. We also set the value of T.
1850 (defun initialize-static-space (tls-init)
1851 "Initialize the cold load symbol-hacking data structures."
1852 (declare (ignorable tls-init))
1853 ;; -1 is magic having to do with nil-as-cons vs. nil-as-symbol
1854 #-compact-symbol
1855 (write-wordindexed *nil-descriptor* (- sb-vm:symbol-package-id-slot 1)
1856 (make-fixnum-descriptor sb-impl::+package-id-lisp+))
1857 (when core-file-name
1858 ;; NIL did not have its package assigned. Do that now.
1859 (record-accessibility :external (cold-find-package-info "COMMON-LISP")
1860 *nil-descriptor*))
1861 ;; Intern the others.
1862 (dovector (symbol sb-vm:+static-symbols+)
1863 (let* ((des (cold-intern symbol :gspace *static*))
1864 (offset-wanted (sb-vm:static-symbol-offset symbol))
1865 (offset-found (- (descriptor-bits des)
1866 (descriptor-bits *nil-descriptor*))))
1867 (unless (= offset-wanted offset-found)
1868 (error "Offset from ~S to ~S is ~W, not ~W"
1869 symbol
1871 offset-found
1872 offset-wanted))))
1873 ;; Reserve space for SB-LOCKLESS:+TAIL+ which is conceptually like NIL
1874 ;; but tagged with INSTANCE-POINTER-LOWTAG.
1875 (setq *lflist-tail-atom*
1876 (if core-file-name
1877 (write-slots (allocate-struct-of-type 'sb-lockless::list-node *static*)
1878 :%node-next nil)
1879 (allocate-struct (1+ sb-vm:instance-data-start)
1880 (make-fixnum-descriptor 0) *static*)))
1882 ;; Assign TLS indices of C interface symbols
1883 #+sb-thread
1884 (progn
1885 (dolist (binding sb-vm::per-thread-c-interface-symbols)
1886 (ensure-symbol-tls-index (car (ensure-list binding))))
1887 ;; Assign other known TLS indices
1888 (dolist (pair tls-init)
1889 (destructuring-bind (tls-index . symbol) pair
1890 (aver (eql tls-index (ensure-symbol-tls-index symbol))))))
1892 ;; Establish the value of T.
1893 (let ((t-symbol (cold-intern t :gspace *static*)))
1894 (cold-set t-symbol t-symbol))
1896 ;; Establish the value of SB-VM:FUNCTION-LAYOUT and **PRIMITIVE-OBJECT-LAYOUTS**
1897 #+compact-instance-header
1898 (progn
1899 (write-wordindexed/raw (cold-intern 'sb-vm:function-layout)
1900 sb-vm:symbol-value-slot
1901 (ash (cold-layout-descriptor-bits 'function) 32))
1902 (cold-set '**primitive-object-layouts**
1903 #+permgen
1904 (emplace-vector (make-random-descriptor
1905 (logior (gspace-byte-address *permgen*)
1906 sb-vm:other-pointer-lowtag))
1907 sb-vm:simple-vector-widetag 256)
1908 #+immobile-space
1909 (let ((filler
1910 (make-random-descriptor
1911 (logior (gspace-byte-address *immobile-fixedobj*)
1912 sb-vm:other-pointer-lowtag)))
1913 (vector
1914 (make-random-descriptor
1915 (logior (+ (gspace-byte-address *immobile-fixedobj*)
1916 sb-vm:immobile-card-bytes
1917 (* (+ 2 256) (- sb-vm:n-word-bytes)))
1918 sb-vm:other-pointer-lowtag))))
1919 (emplace-vector filler sb-vm:simple-array-fixnum-widetag
1920 (- (/ sb-vm:immobile-card-bytes sb-vm:n-word-bytes)
1921 ;; subtract 2 object headers + 256 words
1922 (+ 4 256)))
1923 (emplace-vector vector sb-vm:simple-vector-widetag 256))))
1925 ;; Dynamic-space code can't use "call rel32" to reach the assembly code
1926 ;; in a single instruction if too far away. The solution is to have a static-space
1927 ;; array of entrypoints addressable using "call [EA]"
1928 #+(and x86-64 immobile-code)
1929 (setf *asm-routine-vector* (word-vector (make-list 256 :initial-element 0)
1930 *static*))
1932 #+linkage-space (mapc 'ensure-linkage-index sb-vm::+c-callable-fdefns+)
1933 #-linkage-space
1934 (progn
1935 (dolist (sym sb-vm::+c-callable-fdefns+)
1936 (ensure-cold-fdefn sym *static*))
1938 (dovector (sym sb-vm:+static-fdefns+)
1939 (let* ((fdefn (ensure-cold-fdefn sym *static*))
1940 (offset (- (+ (- (descriptor-bits fdefn)
1941 sb-vm:other-pointer-lowtag)
1942 (* sb-vm:fdefn-raw-addr-slot sb-vm:n-word-bytes))
1943 (descriptor-bits *nil-descriptor*)))
1944 (desired (sb-vm:static-fun-offset sym)))
1945 (unless (= offset desired)
1946 (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1947 sym nil offset desired))))))
1949 ;;; Sort *COLD-LAYOUTS* to return them in a deterministic order.
1950 (defun sort-cold-layouts ()
1951 (sort (%hash-table-alist *cold-layouts*) #'<
1952 :key (lambda (x) (descriptor-bits (cold-layout-descriptor (cdr x))))))
1954 ;;; Establish initial values for magic symbols.
1956 (defun finish-symbols ()
1957 (cold-set 'sb-kernel::*!initial-layouts*
1958 (vector-in-core
1959 (mapcar (lambda (pair)
1960 (cold-cons (cold-intern (car pair))
1961 (cold-layout-descriptor (cdr pair))))
1962 (sort-cold-layouts))))
1963 ;; MAKE-LAYOUT uses ATOMIC-INCF which returns the value in the cell prior to
1964 ;; increment, so we need to add 1 to get to the next value for it because
1965 ;; we always pre-increment *general-layout-uniqueid-counter* when reading it.
1966 (cold-set 'sb-kernel::*layout-id-generator*
1967 (cold-list (make-fixnum-descriptor
1968 (1+ sb-kernel::*general-layout-uniqueid-counter*))))
1970 #+sb-thread
1971 (cold-set 'sb-vm::*free-tls-index*
1972 (make-descriptor (ash *genesis-tls-counter* sb-vm:word-shift)))
1974 (cold-set 'sb-c::*code-serialno* (make-fixnum-descriptor (1+ sb-c::*code-serialno*)))
1976 (cold-set 'sb-impl::*setf-fdefinition-hook* *nil-descriptor*)
1977 (cold-set 'sb-impl::*user-hash-table-tests* *nil-descriptor*)
1978 (cold-set 'sb-lockless:+tail+ *lflist-tail-atom*)
1980 #+immobile-code
1981 (let* ((space *immobile-text*)
1982 (wordindex (gspace-free-word-index space))
1983 (words-per-page (/ sb-vm:immobile-card-bytes sb-vm:n-word-bytes)))
1984 (cold-set 'sb-fasl::*asm-routine-vector* *asm-routine-vector*)
1985 (let* ((objects (gspace-objects space))
1986 (count (length objects)))
1987 (let ((remainder (rem wordindex words-per-page)))
1988 (unless (zerop remainder)
1989 (let* ((fill-nwords (- words-per-page remainder))
1990 (des
1991 ;; technically FILLER_WIDETAG has no valid lowtag because it's not an object
1992 ;; that lisp can address. But WRITE-WORDINDEXED requires a pointer descriptor
1993 (allocate-cold-descriptor space (* fill-nwords sb-vm:n-word-bytes)
1994 sb-vm:other-pointer-lowtag)))
1995 (aver (zerop (rem (gspace-free-word-index space) words-per-page)))
1996 (write-header-word des (logior (ash fill-nwords 32) sb-vm:filler-widetag)))))
1997 ;; Construct a ub32 array of object offsets.
1998 (let* ((n-data-words (ceiling count 2)) ; lispword = 2 ub32s
1999 (vect (allocate-vector sb-vm:simple-array-unsigned-byte-32-widetag
2000 count n-data-words))
2001 (data-ptr (+ (descriptor-byte-offset vect)
2002 (ash sb-vm:vector-data-offset sb-vm:word-shift))))
2003 (dotimes (i count)
2004 (setf (bvref-32 (descriptor-mem vect) data-ptr)
2005 (descriptor-byte-offset (aref objects i)))
2006 (incf data-ptr 4))
2007 (cold-set 'sb-vm::*immobile-codeblob-vector* vect))))
2009 ;; Symbols for which no call to COLD-INTERN would occur - due to not being
2010 ;; referenced until warm init - must be artificially cold-interned.
2011 ;; Inasmuch as the "offending" things are compiled by ordinary target code
2012 ;; and not cold-init, I think we should use an ordinary DEFPACKAGE for
2013 ;; the added-on bits. What I've done is somewhat of a fragile kludge.
2014 (let (syms)
2015 (with-package-iterator (iter '("SB-PCL" "SB-MOP" "SB-GRAY" "SB-SEQUENCE"
2016 "SB-PROFILE" "SB-EXT" "SB-VM"
2017 "SB-C" "SB-FASL" "SB-DEBUG")
2018 :external)
2019 (loop
2020 (multiple-value-bind (foundp sym accessibility package) (iter)
2021 (declare (ignore accessibility))
2022 (cond ((not foundp) (return))
2023 ((eq (cl:symbol-package sym) package) (push sym syms))))))
2024 (setf syms (stable-sort syms #'string<))
2025 (dolist (sym syms)
2026 (cold-intern sym)))
2028 (cold-set 'sb-impl::*!initial-package-graph*
2029 (list-to-core
2030 (mapcar (lambda (x) (list-to-core (mapcar #'string-literal-to-core x)))
2031 *package-graph*)))
2033 (cold-set
2034 'sb-impl::*!initial-symbols*
2035 (cold-cons
2036 (let (uninterned)
2037 (maphash (lambda (key val) (declare (ignore key)) (push val uninterned))
2038 *uninterned-symbol-table*)
2039 (vector-in-core (sort uninterned #'< :key #'descriptor-bits)))
2040 (list-to-core
2041 (mapcar
2042 (lambda (pkgcons)
2043 (destructuring-bind (pkg-name . pkg-info) pkgcons
2044 (unless (member pkg-name '("COMMON-LISP" "COMMON-LISP-USER" "KEYWORD")
2045 :test 'string=)
2046 (let ((host-pkg (find-package pkg-name))
2047 syms)
2048 ;; Now for each symbol directly present in this host-pkg,
2049 ;; i.e. accessible but not :INHERITED, figure out if the symbol
2050 ;; came from a different package, and if so, make a note of it.
2051 (with-package-iterator (iter host-pkg :internal :external)
2052 (loop (multiple-value-bind (foundp sym accessibility) (iter)
2053 (unless foundp (return))
2054 (unless (eq (cl:symbol-package sym) host-pkg)
2055 (push (cons sym accessibility) syms)))))
2056 (dolist (symcons (sort syms #'string< :key #'car))
2057 (destructuring-bind (sym . accessibility) symcons
2058 (record-accessibility accessibility pkg-info (cold-intern sym)
2059 host-pkg sym)))))
2060 (cold-list (cdr pkg-info)
2061 (vector-in-core (caar pkg-info))
2062 (vector-in-core (cdar pkg-info)))))
2063 (sort (%hash-table-alist *cold-package-symbols*)
2064 #'string< :key #'car))))) ; Sort by package-name
2066 ;; assign *PACKAGE* since it supposed to be always-bound
2067 ;; and various things assume that it is. e.g. FIND-PACKAGE has an
2068 ;; (IF (BOUNDP '*PACKAGE*)) test which the compiler elides.
2069 (cold-set '*package* (cdr (cold-find-package-info "COMMON-LISP-USER")))
2071 #+linkage-space ; element 0 is 0, not a descriptor, so don't write it
2072 (cold-set 'sb-vm::*!initial-linkage-table*
2073 (vector-in-core (cdr (coerce *fname-table* 'list))))
2074 #-linkage-space
2075 (loop with ud-tramp = (lookup-assembler-reference 'sb-vm::undefined-tramp)
2076 for fdefn being each hash-value of *cold-fdefn-objects*
2077 when (cold-null (cold-fdefn-fun fdefn))
2078 do (write-wordindexed/raw fdefn sb-vm:fdefn-raw-addr-slot ud-tramp))
2080 (dump-symbol-infos
2081 (attach-fdefinitions-to-symbols
2082 (attach-classoid-cells-to-symbols (make-hash-table :test #'eq))))
2084 #+x86-64 ; Dump a popular constant
2085 (let ((array
2086 ;; Embed the constant in an unboxed array. This shouldn't be necessary,
2087 ;; because the start of the scanned space is STATIC_SPACE_OBJECTS_START,
2088 ;; but not all uses strictly follow that rule. (They should though)
2089 ;; This must not conflict with the alloc regions at the start of the space.
2090 (make-random-descriptor (logior (- sb-vm::non-negative-fixnum-mask-constant-wired-address
2091 (* 2 sb-vm:n-word-bytes))
2092 sb-vm:other-pointer-lowtag))))
2093 (write-wordindexed/raw array 0 sb-vm:simple-array-unsigned-byte-64-widetag)
2094 (write-wordindexed array 1 (make-fixnum-descriptor 1))
2095 (write-wordindexed/raw array 2 sb-vm::non-negative-fixnum-mask-constant))
2097 #+x86
2098 (progn
2099 (cold-set 'sb-vm::*fp-constant-0d0* (number-to-core 0d0))
2100 (cold-set 'sb-vm::*fp-constant-1d0* (number-to-core 1d0))
2101 (cold-set 'sb-vm::*fp-constant-0f0* (number-to-core 0f0))
2102 (cold-set 'sb-vm::*fp-constant-1f0* (number-to-core 1f0))))
2104 ;;;; functions and fdefinition objects
2106 ;;; Given a cold representation of a symbol, return a warm
2107 ;;; representation.
2108 (defun warm-symbol (des)
2109 ;; Note that COLD-INTERN is responsible for keeping the
2110 ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
2111 ;; uninterned symbol, the code below will fail. But as long as we
2112 ;; don't need to look up uninterned symbols during bootstrapping,
2113 ;; that's OK..
2114 (multiple-value-bind (symbol found-p)
2115 (gethash (descriptor-bits des) *cold-symbols*)
2116 (declare (type symbol symbol))
2117 (unless found-p
2118 (error "no warm symbol"))
2119 symbol))
2121 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
2122 (defun cold-car (des)
2123 (aver (= (descriptor-lowtag des) sb-vm:list-pointer-lowtag))
2124 (read-wordindexed des sb-vm:cons-car-slot))
2125 (defun cold-cdr (des)
2126 (aver (= (descriptor-lowtag des) sb-vm:list-pointer-lowtag))
2127 (read-wordindexed des sb-vm:cons-cdr-slot))
2128 (defun cold-rplacd (des newval)
2129 (aver (= (descriptor-lowtag des) sb-vm:list-pointer-lowtag))
2130 (write-wordindexed des sb-vm:cons-cdr-slot newval)
2131 des)
2132 (defun cold-null (des) (descriptor= des *nil-descriptor*))
2134 ;;; Given a cold representation of a function name, return a warm
2135 ;;; representation.
2136 (declaim (ftype (function ((or symbol descriptor)) (or symbol list)) warm-fun-name))
2137 (defun warm-fun-name (des)
2138 (let ((result
2139 (if (symbolp des)
2140 ;; This parallels the logic at the start of COLD-INTERN
2141 ;; which re-homes symbols in SB-XC to COMMON-LISP.
2142 (if (eq (cl:symbol-package des) (find-package "SB-XC"))
2143 (intern (symbol-name des) (canonical-home-package (string des)))
2144 des)
2145 (ecase (descriptor-lowtag des)
2146 (#.sb-vm:list-pointer-lowtag
2147 (aver (not (cold-null des))) ; function named NIL? please no..
2148 (let ((rest (cold-cdr des)))
2149 (aver (cold-null (cold-cdr rest)))
2150 (list (warm-symbol (cold-car des))
2151 (warm-symbol (cold-car rest)))))
2152 (#.sb-vm:other-pointer-lowtag
2153 (warm-symbol des))))))
2154 (legal-fun-name-or-type-error result)
2155 result))
2157 (defvar *assembler-routines*) ; descriptor
2158 (defun ensure-cold-fdefn (cold-name &optional (gspace *dynamic*))
2159 (declare (type (or symbol descriptor) cold-name))
2160 (let ((warm-name (warm-fun-name cold-name)))
2161 #+linkage-space (aver (not (symbolp warm-name)))
2162 (or (gethash warm-name *cold-fdefn-objects*)
2163 (let ((fdefn (allocate-otherptr gspace sb-vm:fdefn-size sb-vm:fdefn-widetag)))
2164 (when core-file-name
2165 (write-wordindexed fdefn sb-vm:fdefn-name-slot cold-name)
2166 #-linkage-space
2167 (progn
2168 (write-wordindexed fdefn sb-vm:fdefn-fun-slot *nil-descriptor*)
2169 (when (typep warm-name '(and symbol (not null)))
2170 (write-wordindexed (cold-intern warm-name) sb-vm:symbol-fdefn-slot fdefn))))
2171 (setf (gethash warm-name *cold-fdefn-objects*) fdefn)))))
2173 (defun cold-fun-entry-addr (fun)
2174 (aver (= (descriptor-lowtag fun) sb-vm:fun-pointer-lowtag))
2175 (+ (descriptor-bits fun)
2176 (- sb-vm:fun-pointer-lowtag)
2177 (ash sb-vm:simple-fun-insts-offset sb-vm:word-shift)))
2179 (defun cold-fset (name function)
2180 (aver (= (descriptor-widetag function) sb-vm:simple-fun-widetag))
2181 #-linkage-space
2182 (let ((fdefn (ensure-cold-fdefn
2183 ;; (SETF f) was descriptorized when dumped, symbols were not.
2184 (if (symbolp name) (cold-intern name) name))))
2185 (let ((existing (read-wordindexed fdefn sb-vm:fdefn-fun-slot)))
2186 (unless (or (cold-null existing) (descriptor= existing function))
2187 (error "Function multiply defined: ~S. Was ~x is ~x" name
2188 (descriptor-bits existing)
2189 (descriptor-bits function))))
2190 (write-wordindexed fdefn sb-vm:fdefn-fun-slot function)
2191 (write-wordindexed/raw
2192 fdefn sb-vm:fdefn-raw-addr-slot
2193 (or #+(or sparc arm riscv) ; raw addr is the function descriptor
2194 (descriptor-bits function)
2195 ;; For all others raw addr is the starting address
2196 (+ (logandc2 (descriptor-bits function) sb-vm:lowtag-mask)
2197 (ash sb-vm:simple-fun-insts-offset sb-vm:word-shift))))
2198 fdefn)
2199 #+linkage-space
2200 (let ((fname (if (symbolp name) (cold-intern name) (ensure-cold-fdefn name))))
2201 (write-wordindexed fname sb-vm:fdefn-fun-slot function)
2202 fname))
2204 (defun attach-classoid-cells-to-symbols (hashtable)
2205 (when (plusp (hash-table-count *classoid-cells*))
2206 (aver (gethash 'sb-kernel::classoid-cell *cold-layouts*))
2207 (let ((type-classoid-cell-info
2208 (sb-c::meta-info-number (sb-c::meta-info :type :classoid-cell)))
2209 (type-kind-info
2210 (sb-c::meta-info-number (sb-c::meta-info :type :kind))))
2211 ;; Iteration order is immaterial. The symbols will get sorted later.
2212 (maphash (lambda (symbol cold-classoid-cell)
2213 (let ((packed-info
2214 (packed-info-insert
2215 (gethash symbol hashtable +nil-packed-infos+)
2216 sb-impl::+no-auxiliary-key+
2217 type-classoid-cell-info cold-classoid-cell)))
2218 ;; an instance classoid won't be returned from %PARSE-TYPE
2219 ;; unless the :KIND is set, but we can't set the kind
2220 ;; to :INSTANCE unless the classoid is present in the cell.
2221 (when (and (eq (info :type :kind symbol) :instance)
2222 (not (cold-null (read-slot cold-classoid-cell :classoid))))
2223 (setf packed-info
2224 (packed-info-insert
2225 packed-info sb-impl::+no-auxiliary-key+
2226 type-kind-info (cold-intern :instance))))
2227 (setf (gethash symbol hashtable) packed-info)))
2228 *classoid-cells*)))
2229 hashtable)
2231 ;; Create pointer from SYMBOL and/or (SETF SYMBOL) to respective fdefinition
2233 (defun attach-fdefinitions-to-symbols (hashtable)
2234 ;; Collect fdefinitions that go with one symbol, e.g. (SETF CAR) and (CAS CAR)
2235 ;; using the host's code for manipulating a packed-info.
2236 ;; Do not add fdefns for symbols to the info. It goes in a slot.
2237 (maphash (lambda (warm-name cold-fdefn)
2238 (unless (symbolp warm-name)
2239 (with-globaldb-name (key1 key2) warm-name
2240 :hairy (error "Hairy fdefn name in genesis: ~S" warm-name)
2241 :simple (setf (gethash key1 hashtable)
2242 (packed-info-insert
2243 (gethash key1 hashtable +nil-packed-infos+)
2244 key2 +fdefn-info-num+ cold-fdefn)))))
2245 *cold-fdefn-objects*)
2246 hashtable)
2248 (defun dump-packed-info (list)
2249 ;; Payload length is the element count + LAYOUT slot if necessary.
2250 ;; Header word is added automatically by ALLOCATE-STRUCT
2251 (let ((s (allocate-struct (+ sb-vm:instance-data-start (length list))
2252 (cold-layout-descriptor (gethash 'packed-info *cold-layouts*)))))
2253 (loop for i from (+ sb-vm:instance-slots-offset sb-vm:instance-data-start)
2254 for elt in list do (write-wordindexed s i elt))
2256 (defun dump-symbol-infos (hashtable)
2257 (cold-set 'sb-impl::+nil-packed-infos+
2258 (dump-packed-info (list (make-fixnum-descriptor 0))))
2259 ;; Emit in the same order symbols reside in core to avoid
2260 ;; sensitivity to the iteration order of host's maphash.
2261 (loop for (warm-sym . info)
2262 in (sort (%hash-table-alist hashtable) #'<
2263 :key (lambda (x) (descriptor-bits (cold-intern (car x)))))
2264 do (aver warm-sym) ; enforce that NIL was specially dealt with already
2265 (aver (> (sb-impl::packed-info-len info) 1))
2266 (write-wordindexed
2267 (cold-intern warm-sym)
2268 sb-vm:symbol-info-slot
2269 (dump-packed-info
2270 ;; Each packed-info will have one fixnum, possibly the symbol SETF,
2271 ;; and zero, one, or two #<fdefn>, and/or a classoid-cell.
2272 (map 'list (lambda (elt)
2273 (etypecase elt
2274 (symbol (cold-intern elt))
2275 (sb-xc:fixnum (make-fixnum-descriptor elt))
2276 (descriptor elt)))
2277 (sb-impl::packed-info-cells info))))))
2279 ;;;; fixups and related stuff
2281 ;;; an EQUAL hash table
2282 (defvar *cold-foreign-symbol-table*)
2283 (declaim (type hash-table *cold-foreign-symbol-table*))
2285 (defvar *asm-routine-alist*)
2287 ;;: See picture in 'objdef'
2288 (defun code-object-size (code-object) ; Return total size in bytes
2289 (* (ash (get-header-data code-object) (+ #+64-bit -24))
2290 sb-vm:n-word-bytes))
2292 ;; Boxed header length is stored directly in bytes, not words
2293 (defun code-header-bytes (code-object)
2294 (ldb (byte 32 0) (read-bits-wordindexed code-object sb-vm:code-boxed-size-slot)))
2295 (defun code-header-words (code-object) ; same, but expressed in words
2296 (ash (code-header-bytes code-object) (- sb-vm:word-shift)))
2298 (defun code-instructions (code)
2299 (make-model-sap (- (+ (descriptor-bits code) (code-header-bytes code))
2300 sb-vm:other-pointer-lowtag)
2301 (descriptor-gspace code)))
2303 ;;; These are fairly straightforward translations of the similarly named accessor
2304 ;;; from src/code/simple-fun.lisp
2305 (defun code-trailer-ref (code offset)
2306 "Reference a uint_32 relative to the end of code at byte offset OFFSET.
2307 Legal values for OFFSET are -4, -8, -12, ..."
2308 (bvref-32 (descriptor-mem code)
2309 (+ (descriptor-byte-offset code) (code-object-size code) offset)))
2310 (defun code-fun-table-count (code)
2311 "Return the COUNT trailer word in CODE. The COUNT is a packed integer containing
2312 the number of embedded SIMPLE-FUNs and the number of padding bytes in the
2313 instructions prior to the start of the simple-fun offset list"
2314 ;; The case of trailer-len = 0 (no trailer payload) can't happen during genesis,
2315 ;; so we don't check for it.
2316 (let ((word (code-trailer-ref code -4)))
2317 ;; TRAILER-REF returns 4-byte quantities. Extract a two-byte quantity.
2318 #+little-endian (ldb (byte 16 0) word)
2319 #+big-endian (ldb (byte 16 16) word)))
2321 ;;; These are literally identical between cross-compiler and target.
2322 ;;; TODO: Maybe put them somewhere that gets defined for both?
2323 ;;; (Minor problem of CODE-COMPONENT not being a primitive type though)
2324 (defun code-n-entries (code)
2325 (ash (code-fun-table-count code) -5))
2326 (defun %code-fun-offset (code fun-index)
2327 ;; The 4-byte quantity at "END" - 4 is the trailer count, the word at -8 is
2328 ;; the offset to the 0th simple-fun, -12 is the next, etc...
2329 (code-trailer-ref code (* -4 (+ fun-index 2))))
2331 (defun assembler-code-insts-start ()
2332 (let ((code-component *assembler-routines*))
2333 (+ (logandc2 (descriptor-bits code-component) sb-vm:lowtag-mask)
2334 (code-header-bytes code-component))))
2336 (defun lookup-assembler-reference (symbol)
2337 (let ((cell (or (assq symbol *asm-routine-alist*)
2338 (error "Unknown asm routine ~S" symbol))))
2339 (+ (assembler-code-insts-start) (cdr cell)))) ; compute the starting address
2341 (defun asm-routine-index-from-addr (address)
2342 (let ((relative-start (- address (assembler-code-insts-start))))
2343 (1+ (position relative-start *asm-routine-alist* :key #'cdr))))
2345 ;;; Unlike in the target, FOP-KNOWN-FUN sometimes has to backpatch.
2346 (defvar *deferred-known-fun-refs*)
2348 (defun code-jump-table-words (code)
2349 (ldb (byte 14 0) (read-bits-wordindexed code (code-header-words code))))
2351 (declaim (ftype (sfunction (descriptor sb-vm:word (or sb-vm:word
2352 sb-vm:signed-word)
2353 keyword keyword)
2354 descriptor)
2355 cold-fixup))
2356 (defun cold-fixup (code-object after-header value kind flavor)
2357 (sb-vm:fixup-code-object code-object after-header value kind flavor)
2358 code-object)
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 alien 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*) *assembler-routines*)
2378 (to-core *asm-routine-alist*
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 (let ((name (pop-stack)))
2739 (if (or #+linkage-space (symbolp name)) (cold-intern name) (ensure-cold-fdefn name))))
2741 (define-cold-fop (fop-known-fun)
2742 (let ((name (pop-stack)))
2743 (or (cold-symbol-function name nil) ; no error if undefined
2744 `(:known-fun . ,name))))
2746 ;;; Setting this variable shows what code looks like before any
2747 ;;; fixups (or function headers) are applied.
2748 (defvar *show-pre-fixup-code-p* nil)
2750 (defun store-named-call-fdefn (code index fdefn)
2751 #+untagged-fdefns
2752 (write-wordindexed/raw code index (- (descriptor-bits fdefn)
2753 sb-vm:other-pointer-lowtag))
2754 #-untagged-fdefns (write-wordindexed code index fdefn))
2756 (define-cold-fop (fop-load-code (header n-code-bytes n-fixup-elts))
2757 (let* ((n-simple-funs (read-unsigned-byte-32-arg (fasl-input-stream)))
2758 (n-fdefns (read-unsigned-byte-32-arg (fasl-input-stream)))
2759 (n-boxed-words (ash header -1))
2760 (n-constants (- n-boxed-words sb-vm:code-constants-offset))
2761 (stack-elts-consumed (+ n-constants 1 n-fixup-elts))
2762 (immobile (oddp header)) ; decode the representation used by dump
2763 ;; The number of constants is rounded up to even (if required)
2764 ;; to ensure that the code vector will be properly aligned.
2765 (aligned-n-boxed-words (align-up n-boxed-words sb-c::code-boxed-words-align))
2766 (stack (%fasl-input-stack (fasl-input)))
2767 (stack-index (fop-stack-pop-n stack stack-elts-consumed))
2768 (des (allocate-cold-descriptor
2769 (or #+immobile-code (and immobile *immobile-text*)
2770 *dynamic*)
2771 (+ (ash aligned-n-boxed-words sb-vm:word-shift) n-code-bytes)
2772 sb-vm:other-pointer-lowtag :code)))
2773 (declare (ignorable immobile))
2774 (write-code-header-words des aligned-n-boxed-words n-code-bytes n-fdefns)
2775 (write-wordindexed des sb-vm:code-debug-info-slot
2776 (svref stack (+ stack-index n-constants)))
2778 (let ((start (+ (descriptor-byte-offset des)
2779 (ash aligned-n-boxed-words sb-vm:word-shift))))
2780 (read-into-bigvec (descriptor-mem des) (fasl-input-stream) start n-code-bytes)
2781 (aver (= (code-n-entries des) n-simple-funs))
2782 (let ((jumptable-word (read-bits-wordindexed des aligned-n-boxed-words)))
2783 (aver (zerop (ash jumptable-word -14)))
2784 ;; assign serialno
2785 (write-wordindexed/raw
2786 des aligned-n-boxed-words
2787 (logior (ash (incf sb-c::*code-serialno*) (byte-position sb-vm::code-serialno-byte))
2788 jumptable-word)))
2789 (when *show-pre-fixup-code-p*
2790 (format *trace-output*
2791 "~&LOAD-CODE: ~d header words, ~d code bytes.~%"
2792 n-boxed-words n-code-bytes)
2793 (do ((i start (+ i sb-vm:n-word-bytes))
2794 (count (floor n-code-bytes sb-vm:n-word-bytes) (1- count)))
2795 ((zerop count))
2796 (format *trace-output*
2797 " ~X: ~V,'.X~%"
2798 (+ i (gspace-byte-address (descriptor-gspace des)))
2799 (* 2 sb-vm:n-word-bytes)
2800 (bvref-word (descriptor-mem des) i)))))
2802 (apply-fixups des stack (+ stack-index (1+ n-constants)) n-fixup-elts)
2803 (let ((header-index sb-vm:code-constants-offset))
2804 (declare (type index header-index stack-index))
2805 (dotimes (fun-index (code-n-entries des))
2806 (let ((fn (%code-entry-point des fun-index)))
2807 (set-simple-fun-layout fn)
2808 #+(or x86 x86-64 arm64) ; store a machine-native pointer to the function entry
2809 ;; note that the bit pattern looks like fixnum due to alignment
2810 (write-wordindexed/raw fn sb-vm:simple-fun-self-slot
2811 (+ (- (descriptor-bits fn) sb-vm:fun-pointer-lowtag)
2812 (ash sb-vm:simple-fun-insts-offset sb-vm:word-shift)))
2813 #-(or x86 x86-64 arm64) ; store a pointer back to the function itself in 'self'
2814 (write-wordindexed fn sb-vm:simple-fun-self-slot fn))
2815 (dotimes (i sb-vm:code-slots-per-simple-fun)
2816 (write-wordindexed des header-index (svref stack stack-index))
2817 (incf header-index)
2818 (incf stack-index)))
2819 (dotimes (i n-fdefns)
2820 (store-named-call-fdefn des header-index (svref stack stack-index))
2821 (incf header-index)
2822 (incf stack-index))
2823 (do () ((>= header-index n-boxed-words))
2824 (let ((constant (svref stack stack-index)))
2825 (cond ((and (consp constant) (eq (car constant) :known-fun))
2826 (push (list* (cdr constant) des header-index) *deferred-known-fun-refs*))
2828 (write-wordindexed des header-index constant))))
2829 (incf header-index)
2830 (incf stack-index)))
2831 des))
2833 (defun resolve-deferred-known-funs ()
2834 (dolist (item *deferred-known-fun-refs*)
2835 (let ((fun (cold-symbol-function (car item)))
2836 (place (cdr item)))
2837 (write-wordindexed (car place) (cdr place) fun))))
2839 (defun %code-entry-point (code-object fun-index)
2840 (let ((fun (sap-int (sap+ (code-instructions code-object)
2841 (%code-fun-offset code-object fun-index)))))
2842 (unless (zerop (logand fun sb-vm:lowtag-mask))
2843 (error "unaligned function entry ~S ~S" code-object fun-index))
2844 (make-descriptor (logior fun sb-vm:fun-pointer-lowtag))))
2846 (define-cold-fop (fop-assembler-code)
2847 (aver (not *assembler-routines*))
2848 (let* ((n-routines (read-word-arg (fasl-input-stream)))
2849 (length (read-word-arg (fasl-input-stream)))
2850 (n-fixup-elts (read-word-arg (fasl-input-stream)))
2851 (rounded-length (round-up length (* 2 sb-vm:n-word-bytes)))
2852 (header-n-words (sb-c::asm-routines-boxed-header-nwords))
2853 (space (or #+immobile-code *immobile-text*
2854 ;; If there is a read-only space, use it, else use static space.
2855 (if (> sb-vm:read-only-space-end
2856 #-darwin-jit sb-vm:read-only-space-start
2857 ;; Always use read-only space on darwin-jit.
2858 #+darwin-jit 0)
2859 *read-only*
2860 *static*)))
2861 (asm-code
2862 (allocate-cold-descriptor
2863 space
2864 (+ (ash header-n-words sb-vm:word-shift) rounded-length)
2865 sb-vm:other-pointer-lowtag)))
2866 (setf *assembler-routines* asm-code)
2867 (write-code-header-words asm-code header-n-words rounded-length 0)
2868 (let ((start (+ (descriptor-byte-offset asm-code)
2869 (ash header-n-words sb-vm:word-shift))))
2870 (read-into-bigvec (descriptor-mem asm-code) (fasl-input-stream) start length))
2871 ;; Write a bignum reference into the boxed constants.
2872 ;; All the backends should do this, as its avoids consing in GENERIC-NEGATE
2873 ;; when the argument is MOST-NEGATIVE-FIXNUM.
2874 #+x86-64 (write-wordindexed asm-code sb-vm:code-constants-offset
2875 (bignum-to-core (- most-negative-fixnum)
2876 #-immobile-space *static*))
2877 ;; Update the name -> address table.
2878 (let (table)
2879 (dotimes (i n-routines)
2880 (let ((offset (descriptor-fixnum (pop-stack)))
2881 (name (pop-stack)))
2882 (push (cons name offset) table)))
2883 ;; Now that we combine all assembler routines into a single code object
2884 ;; at assembly time, they can all be sorted at this point.
2885 ;; We used to combine them with some magic in genesis.
2886 (setq *asm-routine-alist* (sort table #'< :key #'cdr)))
2887 (let ((stack (%fasl-input-stack (fasl-input))))
2888 (apply-fixups asm-code stack (fop-stack-pop-n stack n-fixup-elts) n-fixup-elts))
2889 #+(or x86 x86-64) ; fill in the indirect call table
2890 (let ((base (code-header-words asm-code))
2891 (index 0))
2892 (dolist (item *asm-routine-alist*)
2893 ;; Word 0 of code-instructions is the jump table count (the asm routine entrypoints
2894 ;; look to GC exactly like a jump table in any other codeblob)
2895 (let ((entrypoint (lookup-assembler-reference (car item))))
2896 (write-wordindexed/raw asm-code (+ base index 1) entrypoint)
2897 #+immobile-space
2898 (progn
2899 (aver (< index (cold-vector-len *asm-routine-vector*)))
2900 (write-wordindexed/raw *asm-routine-vector*
2901 (+ sb-vm:vector-data-offset index) entrypoint)))
2902 (incf index)))))
2904 ;; The partial source info is not needed during the cold load, since
2905 ;; it can't be interrupted.
2906 (define-cold-fop (fop-note-partial-source-info)
2907 (pop-stack)
2908 (pop-stack)
2909 (pop-stack)
2910 (values))
2912 (define-cold-fop (fop-note-full-calls)
2913 (sb-c::accumulate-full-calls (host-object-from-core (pop-stack)))
2914 (values))
2916 ;;; Target variant of this is defined in 'target-load'
2917 (defun apply-fixups (code-obj fixups index count
2918 &aux (end (1- (+ index count))) callees)
2919 (declare (ignorable callees))
2920 (let ((retained-fixups (svref fixups index)))
2921 (write-wordindexed code-obj sb-vm::code-fixups-slot retained-fixups)
2922 (incf index))
2923 (binding* ((alloc-points (svref fixups index) :exit-if-null))
2924 (cold-set 'sb-c::*!cold-allocation-patch-point*
2925 (cold-cons (cold-cons code-obj alloc-points)
2926 (cold-symbol-value 'sb-c::*!cold-allocation-patch-point*))))
2927 (loop
2928 (when (>= index end) (return))
2929 (binding* (((offset kind flavor-id)
2930 (!unpack-fixup-info (descriptor-integer (svref fixups (incf index)))))
2931 (flavor (aref sb-c::+fixup-flavors+ flavor-id))
2932 (name (cond ((member flavor '(:code-object :card-table-index-mask)) nil)
2933 (t (svref fixups (incf index)))))
2934 (string
2935 (when (and (descriptor-p name)
2936 (= (descriptor-widetag name) sb-vm:simple-base-string-widetag))
2937 (base-string-from-core name))))
2938 (cold-fixup
2939 code-obj offset
2940 (ecase flavor
2941 #+linkage-space
2942 (:linkage-cell
2943 (let ((i (ensure-linkage-index name)))
2944 (unless (permanent-fname-p (warm-fun-name name))
2945 (pushnew i callees))
2947 (:assembly-routine (lookup-assembler-reference name))
2948 (:foreign (alien-linkage-table-note-symbol string nil))
2949 (:foreign-dataref (alien-linkage-table-note-symbol string t))
2950 (:code-object (descriptor-bits code-obj))
2951 #+sb-thread ; ENSURE-SYMBOL-TLS-INDEX isn't defined otherwise
2952 (:symbol-tls-index (ensure-symbol-tls-index name))
2953 (:layout (cold-layout-descriptor-bits name))
2954 (:layout-id ; SYM is a #<LAYOUT>
2955 (cold-layout-id (gethash (descriptor-bits name)
2956 *cold-layout-by-addr*)))
2957 ;; The machine-dependent code decides how to patch in 'nbits'
2958 (:card-table-index-mask sb-vm::gencgc-card-table-index-nbits)
2959 (:immobile-symbol
2960 ;; an interned symbol is represented by its host symbol,
2961 ;; but an uninterned symbol is a descriptor.
2962 (descriptor-bits (if (symbolp name) (cold-intern name) name)))
2963 (:symbol-value (descriptor-bits (cold-symbol-value name))))
2964 kind flavor)))
2965 #+linkage-space
2966 (write-wordindexed code-obj sb-vm:code-linkage-elts-slot
2967 (number-to-core (sb-c:pack-code-fixup-locs callees nil nil)))
2968 code-obj)
2970 ;;;; sanity checking space layouts
2972 (defun check-spaces ()
2973 ;;; Co-opt type machinery to check for intersections...
2974 (let (types)
2975 (flet ((check (start end space)
2976 (when (= start end) ; 0 size is allowed
2977 (return-from check))
2978 (unless (< start end)
2979 (error "Space bounds look bad: ~A = ~X..~X" space start end))
2980 (let ((type (specifier-type `(integer ,start (,end)))))
2981 (dolist (other types)
2982 (unless (eq *empty-type* (type-intersection (cdr other) type))
2983 (error "Space overlap: ~A with ~A" space (car other))))
2984 (push (cons space type) types))))
2985 (check sb-vm:read-only-space-start sb-vm:read-only-space-end :read-only)
2986 #-relocatable-static-space
2987 (check sb-vm:static-space-start sb-vm:static-space-end :static)
2988 #+relocatable-static-space
2989 (check sb-vm:static-space-start (+ sb-vm:static-space-start sb-vm::static-space-size) :static)
2990 (check sb-vm:dynamic-space-start
2991 (+ sb-vm:dynamic-space-start sb-vm::default-dynamic-space-size)
2992 :dynamic)
2993 #+immobile-space
2994 ;; Must be a multiple of 32 because it makes the math a nicer
2995 ;; when computing word and bit index into the 'touched' bitmap.
2996 (aver (zerop (rem sb-vm:fixedobj-space-size (* 32 sb-vm:immobile-card-bytes))))
2997 #+cheneygc
2998 (check sb-vm:dynamic-0-space-start sb-vm:dynamic-0-space-end :dynamic-0)
2999 #-immobile-space
3000 (let ((end (+ sb-vm:alien-linkage-space-start sb-vm:alien-linkage-space-size)))
3001 (check sb-vm:alien-linkage-space-start end :linkage-table)))))
3003 ;;;; emitting C header file
3005 (defun tailwise-equal (string tail)
3006 (and (>= (length string) (length tail))
3007 (string= string tail :start1 (- (length string) (length tail)))))
3009 (defun write-boilerplate (*standard-output*)
3010 (format t "/*~%")
3011 (dolist (line
3012 '("This is a machine-generated file. Please do not edit it by hand."
3014 "This file contains low-level information about the"
3015 "internals of a particular version and configuration"
3016 "of SBCL. It is used by the C compiler to create a runtime"
3017 "support environment, an executable program in the host"
3018 "operating system's native format, which can then be used to"
3019 "load and run 'core' files, which are basically programs"
3020 "in SBCL's own format."))
3021 (format t " *~@[ ~A~]~%" line))
3022 (format t " */~%"))
3024 (defun c-name (string &optional strip)
3025 (delete #\+
3026 (substitute-if #\_ (lambda (c) (member c '(#\- #\/ #\%)))
3027 (remove-if (lambda (c) (position c strip))
3028 string))))
3030 (defun c-symbol-name (symbol &optional strip)
3031 (c-name (symbol-name symbol) strip))
3033 (defun write-makefile-features (*standard-output*)
3034 ;; propagating SB-XC:*FEATURES* into the Makefiles
3035 (dolist (target-feature-name (sort (mapcar #'c-symbol-name sb-xc:*features*)
3036 #'string<))
3037 (format t "LISP_FEATURE_~A=1~%" target-feature-name)))
3039 (defun write-config-h (*standard-output*)
3040 ;; propagating SB-XC:*FEATURES* into C-level #define's
3041 (dolist (target-feature-name (sort (mapcar #'c-symbol-name sb-xc:*features*)
3042 #'string<))
3043 (format t "#define LISP_FEATURE_~A~%" target-feature-name))
3044 (terpri)
3045 ;; and miscellaneous constants
3046 (format t "#define SBCL_TARGET_ARCHITECTURE_STRING ~S~%"
3047 (substitute #\_ #\- (string-downcase (sb-cold::target-platform-keyword))))
3048 (format t "#define SBCL_VERSION_STRING ~S~%"
3049 (sb-xc:lisp-implementation-version))
3050 (format t "#define CORE_MAGIC 0x~X~%" core-magic)
3051 (format t "#ifndef __ASSEMBLER__~2%")
3052 (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
3053 (format t "#else /* __ASSEMBLER__ */~2%")
3054 (format t "#define LISPOBJ(thing) thing~2%")
3055 (format t "#endif /* __ASSEMBLER__ */~2%")
3056 (terpri))
3058 (defvar +c-literal-64bit+
3059 #+(and win32 x86-64) "LLU" ; "long" is 32 bits, "long long" is 64 bits
3060 #-(and win32 x86-64) "LU") ; "long" is 64 bits
3062 (defun write-constants-h (*standard-output*)
3063 (let ((constants nil))
3064 (flet ((record (string priority symbol suffix)
3065 (push (list string priority (symbol-value symbol) suffix)
3066 constants)))
3067 ;; writing entire families of named constants
3068 (dolist (package-name '("SB-VM"
3069 ;; We also propagate magic numbers
3070 ;; related to file format,
3071 ;; which live here instead of SB-VM.
3072 "SB-FASL"
3073 ;; Home package of some constants which aren't
3074 ;; in the target Lisp but are propagated to C.
3075 "SB-COREFILE"))
3076 (do-external-symbols (symbol (find-package package-name))
3077 (when (cl:constantp symbol)
3078 (let ((name (symbol-name symbol)))
3079 ;; Older naming convention
3080 (labels ((record-camelcased (prefix string priority)
3081 (record (concatenate 'simple-string
3082 prefix
3083 (delete #\- (string-capitalize string)))
3084 priority symbol ""))
3085 (maybe-record (tail prefix priority)
3086 (when (tailwise-equal name tail)
3087 (record-camelcased prefix
3088 (subseq name 0
3089 (- (length name) (length tail)))
3090 priority))))
3091 (maybe-record "-FLAG" "flag_" 2)
3092 (maybe-record "-TRAP" "trap_" 3)
3093 (maybe-record "-SC-NUMBER" "sc_" 5))
3094 ;; Newer naming convention
3095 (labels ((record-translated (priority large)
3096 (record (c-name name) priority symbol
3097 (if large +c-literal-64bit+ "")))
3098 (maybe-record (suffixes priority &key large)
3099 (when (some (lambda (suffix) (tailwise-equal name suffix))
3100 suffixes)
3101 (record-translated priority large))))
3102 (maybe-record '("-LOWTAG" "-ALIGN") 0)
3103 (maybe-record '("-WIDETAG" "-SHIFT") 1)
3104 (maybe-record '("SHAREABLE+" "SHAREABLE-NONSTD+") 4)
3105 (maybe-record '("-SIZE" "-INTERRUPTS") 6)
3106 (maybe-record '("-START" "-END" "-PAGE-BYTES"
3107 "-CARD-BYTES" "-GRANULARITY")
3108 7 :large t)
3109 (maybe-record '("-CORE-ENTRY-TYPE-CODE") 8)
3110 (maybe-record '("-CORE-SPACE-ID") 9)
3111 (maybe-record '("-CORE-SPACE-ID-FLAG") 9)
3112 (maybe-record '("-GENERATION+") 10))))))
3113 (do-symbols (symbol (find-package "SB-C"))
3114 (when (cl:constantp symbol)
3115 (let ((name (symbol-name symbol))
3116 (prefix "PACKED-DEBUG-FUN-"))
3117 (when (> (length name) (length prefix))
3118 (when (string= prefix name :end2 (length prefix))
3119 (let ((value (symbol-value symbol)))
3120 (when (integerp value)
3121 (record (c-symbol-name symbol) 4/5 symbol ""))))))))
3122 (dolist (c '(sb-impl::+package-id-none+
3123 sb-impl::+package-id-keyword+
3124 sb-impl::+package-id-lisp+
3125 sb-impl::+package-id-user+
3126 sb-impl::+package-id-kernel+))
3127 (record (c-symbol-name c) 3/2 #| arb |# c ""))
3128 ;; Other constants that aren't necessarily grouped into families.
3129 (dolist (c '(sb-bignum:maximum-bignum-length
3130 sb-vm:n-word-bits sb-vm:n-word-bytes
3131 sb-vm:n-lowtag-bits sb-vm:lowtag-mask
3132 sb-vm:n-widetag-bits sb-vm:widetag-mask
3133 sb-vm:n-fixnum-tag-bits sb-vm:fixnum-tag-mask
3134 sb-vm:instance-length-mask
3135 #+linkage-space sb-vm:n-linkage-index-bits
3136 sb-vm:dsd-raw-type-mask
3137 sb-vm:short-header-max-words
3138 sb-vm:array-flags-position
3139 sb-vm:array-rank-position
3140 sb-vm::nil-value-offset))
3141 (record (c-symbol-name c) -1 c ""))
3142 ;; More symbols that doesn't fit into the pattern above.
3143 (dolist (c '(sb-impl::+magic-hash-vector-value+
3144 ;; These next two flags bits use different naming conventions unfortunately,
3145 ;; but one's a vector header bit, the other a layout flag bit.
3146 sb-vm::+vector-alloc-mixed-region-bit+
3147 sb-kernel::+strictly-boxed-flag+
3148 #-sb-thread sb-vm::mixed-region-offset
3149 #-sb-thread sb-vm::cons-region-offset
3150 #-sb-thread sb-vm::boxed-region-offset
3151 sb-vm::nil-symbol-slots-offset
3152 sb-vm::nil-symbol-slots-end-offset
3153 sb-vm::static-space-objects-offset))
3154 (record (c-symbol-name c) 7 #| arb |# c +c-literal-64bit+)))
3155 ;; Sort by <priority, value, alpha> which is TOO COMPLICATED imho.
3156 ;; Priority and then alphabetical would suffice.
3157 (setf constants
3158 (sort constants
3159 (lambda (const1 const2)
3160 (if (= (second const1) (second const2)) ; priority
3161 (if (= (third const1) (third const2)) ; value
3162 (string< (first const1) (first const2))
3163 (< (third const1) (third const2)))
3164 (< (second const1) (second const2))))))
3165 (let ((prev-priority (second (car constants))))
3166 (dolist (const constants)
3167 (destructuring-bind (name priority value suffix) const
3168 (unless (= prev-priority priority)
3169 (terpri)
3170 (setf prev-priority priority))
3171 (when (minusp value)
3172 (error "stub: negative values unsupported"))
3173 (format t "#define ~A ~A~A /* 0x~X */~%" name value suffix value))))
3174 (terpri))
3176 ;; backend-page-bytes doesn't really mean much any more.
3177 ;; It's the granularity at which we can map the core file pages.
3178 (format t "#define BACKEND_PAGE_BYTES ~D~%" sb-c:+backend-page-bytes+)
3179 ;; values never needed in Lisp, so therefore not a defconstant
3180 (format t "~:{#define ~A ~D~%~}"
3181 `(("MAX_CONSES_PER_PAGE" ,sb-vm::max-conses-per-page)
3182 ("GENCGC_PAGE_SHIFT" ,(1- (integer-length sb-vm:gencgc-page-bytes)))
3183 ("GENCGC_CARD_SHIFT" ,sb-vm::gencgc-card-shift)
3184 ("CARDS_PER_PAGE" ,sb-vm::cards-per-page)))
3186 (let ((size sb-vm::default-dynamic-space-size))
3187 ;; "-DDEFAULT_DYNAMIC_SPACE_SIZE=n" in CFLAGS will override this.
3188 (format t "#ifndef DEFAULT_DYNAMIC_SPACE_SIZE
3189 #define DEFAULT_DYNAMIC_SPACE_SIZE ~D /* ~:*0x~X */
3190 #endif~2%" size))
3192 ;; writing information about internal errors
3193 ;; Assembly code needs only the constants for UNDEFINED_[ALIEN_]FUN_ERROR
3194 ;; but to avoid imparting that knowledge here, we'll expose all error
3195 ;; number constants except for OBJECT-NOT-<x>-ERROR ones.
3196 (loop for (description name) across sb-c:+backend-internal-errors+
3197 for i from 0
3198 when (stringp description)
3199 do (format t "#define ~A ~D~%" (c-symbol-name name) i))
3201 (terpri)
3203 #+(and win32 x86-64)
3204 (format t "#define WIN64_SEH_DATA_ADDR ((void*)~DUL) /* ~:*0x~X */~%"
3205 sb-vm:win64-seh-data-addr)
3207 ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
3208 ;; platforms. If we export this from the SB-VM package, it gets
3209 ;; written out as #define trap_PseudoAtomic, which is confusing as
3210 ;; the runtime treats trap_ as the prefix for illegal instruction
3211 ;; type things. We therefore don't export it, but instead do
3212 #+sparc
3213 (when (boundp 'sb-vm::pseudo-atomic-trap)
3214 (format t
3215 "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
3216 sb-vm::pseudo-atomic-trap)
3217 (terpri))
3218 #+(and sb-safepoint (not x86-64))
3219 (progn
3220 (format t "#define GC_SAFEPOINT_PAGE_ADDR (void*)((char*)STATIC_SPACE_START - ~d)~%"
3221 sb-c:+backend-page-bytes+)
3222 (format t "#define GC_SAFEPOINT_TRAP_ADDR (void*)((char*)STATIC_SPACE_START - ~d)~%"
3223 sb-vm:gc-safepoint-trap-offset))
3225 (dolist (symbol '(sb-vm:float-traps-byte
3226 sb-vm::float-exceptions-byte
3227 sb-vm:float-sticky-bits
3228 sb-vm::float-rounding-mode
3229 sb-c::packed-debug-fun-returns-byte))
3230 (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
3231 (c-symbol-name symbol)
3232 (sb-xc:byte-position (symbol-value symbol)))
3233 (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
3234 (c-symbol-name symbol)
3235 (sb-xc:mask-field (symbol-value symbol) -1))))
3237 (defun write-regnames-h (stream)
3238 (declare (ignorable stream))
3239 #-x86 ;; too weird - "UESP" (user-mode register ESP) is only
3240 ;; visible in a ucontext, so not a lisp register.
3241 (flet ((prettify (macro list &optional trailing-slash)
3242 (aver (not (member nil list)))
3243 (format stream "#define ~a " macro)
3244 (let ((linelen 100) ; force a line break
3245 (delim nil))
3246 (dolist (item list)
3247 (cond ((> linelen 70)
3248 (format stream "~:[~;,~]\\~% " delim)
3249 (setq delim nil linelen 4)) ; four leading spaces
3250 (delim
3251 (write-string ", " stream)
3252 (incf linelen 2)))
3253 (write-string item stream)
3254 (incf linelen (length item))
3255 (setq delim t))
3256 (when trailing-slash (write-char #\\ stream))
3257 (terpri stream))))
3258 (let ((names sb-vm::*register-names*))
3259 (prettify "REGNAMES" (map 'list (lambda (x) (format nil "~s" x)) names))
3260 (when (boundp 'sb-vm::boxed-regs)
3261 (prettify "BOXED_REGISTERS {"
3262 (mapcar (lambda (i) (format nil "reg_~A" (aref names i)))
3263 (symbol-value 'sb-vm::boxed-regs))
3265 (format stream "}~%")))))
3267 (defun write-errnames-h (stream)
3268 ;; C code needs strings for describe_internal_error()
3269 (format stream "#define INTERNAL_ERROR_NAMES ~{\\~%~S~^, ~}~2%"
3270 (map 'list 'sb-kernel::!c-stringify-internal-error
3271 sb-c:+backend-internal-errors+))
3272 (format stream "#define INTERNAL_ERROR_NARGS {~{~S~^, ~}}~2%"
3273 (map 'list #'cddr sb-c:+backend-internal-errors+)))
3275 (eval-when (:compile-toplevel :load-toplevel :execute)
3276 (import 'sb-vm::primitive-object-variable-length-p))
3278 (defun write-tagnames-h (out)
3279 (labels
3280 ((pretty-name (symbol strip)
3281 (let ((name (string-downcase symbol)))
3282 (substitute #\Space #\-
3283 (subseq name 0 (- (length name) (length strip))))))
3284 (list-sorted-tags (tail)
3285 (loop for symbol being the external-symbols of "SB-VM"
3286 when (and (cl:constantp symbol)
3287 (tailwise-equal (string symbol) tail))
3288 collect symbol into tags
3289 finally (return (sort tags #'< :key #'symbol-value))))
3290 (write-tags (visibility kind limit ash-count)
3291 ;; KIND is the string "-LOWTAG" or "-WIDETAG"
3292 (format out "~%~Aconst char *~(~A~)_names[] = {~%"
3293 visibility (subseq kind 1))
3294 (let ((tags (list-sorted-tags kind)))
3295 (dotimes (i limit)
3296 (let ((known (eql i (ash (or (symbol-value (first tags)) -1) ash-count))))
3297 (if known
3298 (if (string= kind "-WIDETAG")
3299 (format out " ~S" (sb-vm::widetag-string-name (pop tags)))
3300 (format out " \"~A\"" (pretty-name (pop tags) kind)))
3301 (format out " \"unknown [~D]\"" i)))
3302 (unless (eql i (1- limit))
3303 (write-string "," out))
3304 (terpri out)))
3305 (write-line "};" out)))
3306 (format out "#include <stddef.h>~%") ; for NULL
3307 (write-tags "static " "-LOWTAG" sb-vm:lowtag-limit 0)
3308 ;; this -2 shift depends on every OTHER-IMMEDIATE-?-LOWTAG
3309 ;; ending with the same 2 bits. (#b10)
3310 (write-tags "" "-WIDETAG" (ash (1+ sb-vm:widetag-mask) -2) -2))
3311 (dolist (name '(symbol ratio complex sb-vm::code simple-fun
3312 closure funcallable-instance
3313 weak-pointer fdefn sb-vm::value-cell))
3314 (format out "static char *~A_slots[] = {~%~{ \"~A: \",~} NULL~%};~%"
3315 (c-name (string-downcase name))
3316 (map 'list (lambda (x) (c-name (string-downcase (sb-vm:slot-name x))))
3317 (let* ((obj (sb-vm::primitive-object name))
3318 (slots (coerce (sb-vm:primitive-object-slots obj) 'list)))
3319 (butlast slots
3320 (if (primitive-object-variable-length-p obj) 1 0))))))
3321 (values))
3323 (defun write-c-print-dispatch (out)
3324 (dolist (flavor '("print" "brief"))
3325 (let ((a (make-array (1+ sb-vm:lowtag-mask))))
3326 (dotimes (i (length a))
3327 (setf (aref a i)
3328 (format nil "~a_~a" flavor
3329 (if (logtest i sb-vm:fixnum-tag-mask) "otherimm" "fixnum"))))
3330 (setf (aref a sb-vm:instance-pointer-lowtag) (format nil "~a_struct" flavor)
3331 (aref a sb-vm:list-pointer-lowtag) (format nil "~a_list" flavor)
3332 (aref a sb-vm:fun-pointer-lowtag) (format nil "~a_fun_or_otherptr" flavor)
3333 (aref a sb-vm:other-pointer-lowtag) (format nil "~a_fun_or_otherptr" flavor))
3334 (format out "static void (*~a_fns[])(lispobj obj) = {~
3335 ~{~% ~a, ~a, ~a, ~a~^,~}~%};~%" flavor (coerce a 'list)))))
3337 (defun write-cast-operator (operator-name c-type-name lowtag stream)
3338 (format stream "static inline struct ~A* ~A(lispobj obj) {
3339 return (struct ~A*)(obj - ~D);~%}~%" c-type-name operator-name c-type-name lowtag)
3340 (case operator-name
3341 (symbol
3342 (format stream "#include ~S~%"
3343 (namestring (merge-pathnames "symbol-tls.inc" (lispobj-dot-h)))))))
3345 (defun write-genesis-thread-h-requisites ()
3346 (write-structure-type (layout-info (find-layout 'sb-thread::thread))
3347 *standard-output* "thread_instance")
3348 (write-structure-type (layout-info (find-layout 'sb-thread::mutex))
3349 *standard-output* "lispmutex")
3350 ;; The os_thread field is either pthread_t or lispobj.
3351 ;; If no threads, then it's lispobj. #+win32 uses lispobj too
3352 ;; but it gets cast to HANDLE upon use.
3353 #+(and unix sb-thread) (format t "#include <pthread.h>~%")
3354 (format t "#include ~S
3356 #define N_HISTOGRAM_BINS_LARGE 32
3357 #define N_HISTOGRAM_BINS_SMALL 32
3358 typedef lispobj size_histogram[2*N_HISTOGRAM_BINS_LARGE+N_HISTOGRAM_BINS_SMALL];
3360 struct thread_state_word {
3361 // - control_stack_guard_page_protected is referenced from
3362 // hand-written assembly code. (grep 'THREAD_STATE_WORD_OFFSET')
3363 // - sprof_enable is referenced with SAPs.
3364 // (grep 'sb-vm:thread-state-word-slot')
3365 char control_stack_guard_page_protected;
3366 char sprof_enable; // statistical CPU profiler switch
3367 char state;
3368 char user_thread_p; // opposite of lisp's ephemeral-p
3370 };~%"
3371 ;; autogenerated files can use full paths to other inclusions
3372 ;; (in case your build system disfavors use of -I compiler options)
3373 (namestring (merge-pathnames "gencgc-alloc-region.h" (lispobj-dot-h)))
3374 #+64-bit " char padding[4];" #-64-bit ""))
3376 (defun write-weak-pointer-manipulators ()
3377 (format t "extern struct weak_pointer *weak_pointer_chain;~%")
3378 ;; weak pointer with no payload size in the header instead has a vector length slot
3379 (format t "static inline int weakptr_vectorp(struct weak_pointer* wp) { ~
3380 return !(wp->header & 0x~X); }~%"
3381 (ash (1- sb-vm:weak-pointer-size) sb-vm:n-widetag-bits))
3382 #+64-bit
3383 (format t "static inline void set_weak_pointer_next(struct weak_pointer *wp, void *next) {
3384 wp->header = ((uword_t)next << 16) | (wp->header & 0xffff);
3386 static inline struct weak_pointer *get_weak_pointer_next(struct weak_pointer *wp) {
3387 // extract a 48-bit pointer from the header
3388 return (void*)(wp->header >> 16);
3389 }~%")
3390 #-64-bit
3391 (format t "#define set_weak_pointer_next(wp, x) wp->next = x
3392 #define get_weak_pointer_next(wp) wp->next~%")
3393 (format t "#define WEAK_POINTER_CHAIN_END (void*)(intptr_t)1
3394 #define reset_weak_pointer_next(wp) set_weak_pointer_next(wp,0)
3395 #define in_weak_pointer_list(wp) (get_weak_pointer_next(wp)!=0)~%"))
3397 (defun write-vector-sap-helpers ()
3398 (format t "static inline char* vector_sap(lispobj v) { return (char*)VECTOR(v)->data; }
3399 static inline unsigned int schar(struct vector* string, int index) {
3400 return (widetag_of(&string->header) == SIMPLE_BASE_STRING_WIDETAG) ?
3401 ((unsigned char*)string->data)[index] :
3402 ((unsigned int*)string->data)[index];
3403 }~%"))
3405 (defun write-sap-initializer ()
3406 (let ((sap-align #+riscv 32 ; not sure why this is larger than normal
3407 #-riscv (* 2 sb-vm:n-word-bytes)))
3408 (format t "
3409 #define DX_ALLOC_SAP(var_name, ptr) \\
3410 lispobj var_name; \\
3411 struct sap _dx_##var_name __attribute__ ((aligned (~D))); \\
3412 do { \\
3413 _dx_##var_name.header = (1 << 8) | SAP_WIDETAG; \\
3414 _dx_##var_name.pointer = (char *)(ptr); \\
3415 var_name = make_lispobj(&_dx_##var_name, OTHER_POINTER_LOWTAG); \\
3416 } while (0)~%"
3417 sap-align)))
3419 (defun get-primitive-obj (x)
3420 (find x sb-vm:*primitive-objects* :key #'sb-vm:primitive-object-name
3421 :test #'string=))
3423 (defun output-c-primitive-obj (obj &aux (name (sb-vm:primitive-object-name obj))
3424 (slots (sb-vm:primitive-object-slots obj))
3425 (rest-slot
3426 (if (primitive-object-variable-length-p obj)
3427 (aref slots (1- (length slots))))))
3428 (format t "struct ~A {~%" (c-name (string-downcase name)))
3429 (when (sb-vm:primitive-object-widetag obj)
3430 (format t " lispobj header;~%"))
3431 ;; For data hiding purposes, change the name of vector->length to vector->length_.
3432 ;; This helped catch C code that made assumptions about the length being stored at
3433 ;; 1 word beyond the header as a fixnum, which it isn't if #+ubsan is enabled.
3434 (flet ((mangle-c-slot-name (slot-name)
3435 (if (and (eq name 'vector) (eq slot-name 'length))
3436 "length_"
3437 (c-name (string-downcase slot-name)))))
3438 (dovector (slot slots)
3439 (format t " ~A ~A~@[[1]~];~%"
3440 (getf (cddr slot) :c-type "lispobj")
3441 (mangle-c-slot-name (sb-vm:slot-name slot))
3442 (eq slot rest-slot))))
3443 (format t "};~%"))
3445 (defun sub-write-primitive-object (obj lang)
3446 (let* ((name (sb-vm:primitive-object-name obj))
3447 (c-name (c-name (string-downcase name)))
3448 (slots (sb-vm:primitive-object-slots obj))
3449 (lowtag (or (symbol-value (sb-vm:primitive-object-lowtag obj)) 0)))
3450 (when (eq name 'symbol)
3451 (sub-write-primitive-object (get-primitive-obj 'fdefn) lang))
3452 (ecase lang
3454 (when (eq name 'sb-vm::thread)
3455 (write-genesis-thread-h-requisites)
3456 (format t "#define INIT_THREAD_REGIONS(x) \\~%")
3457 (let ((tlabs (map 'list
3458 (lambda (x) (c-name (string-downcase (second x))))
3459 (remove-if-not (lambda (x)
3460 (tailwise-equal (string (second x)) "-TLAB"))
3461 slots))))
3462 (format t "~{ gc_init_region(&x->~A)~^,\\~%~}~2%" tlabs))
3463 (when (find 'sb-vm::pseudo-atomic-bits slots :key #'sb-vm:slot-name)
3464 (format t "#define HAVE_THREAD_PSEUDO_ATOMIC_BITS_SLOT 1~2%")
3465 #+(or sparc ppc ppc64) (format t "typedef char pa_bits_t[~d];~2%" sb-vm:n-word-bytes)
3466 #-(or sparc ppc ppc64) (format t "typedef lispobj pa_bits_t;~2%"))
3467 (format t "extern struct thread *all_threads;~%"))
3469 (output-c-primitive-obj obj)
3471 (when (eq name 'sb-vm::code)
3472 (format t "#define CODE_SLOTS_PER_SIMPLE_FUN ~d
3473 static inline struct code* fun_code_header(struct simple_fun* fun) {
3474 return (struct code*)((lispobj*)fun - ((uint32_t)fun->header >> 8));
3475 }~%" sb-vm:code-slots-per-simple-fun)
3476 (write-cast-operator 'function "simple_fun" sb-vm:fun-pointer-lowtag
3477 *standard-output*))
3479 (when (eq name 'vector)
3480 (output-c-primitive-obj (get-primitive-obj 'array))
3481 ;; This is 'sword_t' because we formerly would call fixnum_value() which
3482 ;; is a signed int, but it isn't really; except that I made all C vars
3483 ;; signed to avoid comparison mismatch, and don't want to change back.
3484 (format t "static inline sword_t vector_len(struct vector* v) {")
3485 #+ubsan (format t " return v->header >> ~d; }~%"
3486 (+ 32 sb-vm:n-fixnum-tag-bits))
3487 #-ubsan (format t " return v->length_ >> ~d; }~%"
3488 sb-vm:n-fixnum-tag-bits))
3489 (when (eq name 'weak-pointer)
3490 (write-weak-pointer-manipulators))
3491 (when (eq name 'sb-vm::sap)
3492 (write-sap-initializer))
3493 (when (member name '(cons vector symbol fdefn instance))
3494 (write-cast-operator name c-name lowtag *standard-output*))
3495 (when (eq name 'vector)
3496 (write-vector-sap-helpers)))
3498 (:asm
3499 (dovector (slot slots)
3500 (format t "#define ~A_~A_OFFSET ~D~%"
3501 (c-symbol-name name)
3502 (c-symbol-name (sb-vm:slot-name slot))
3503 (- (* (sb-vm:slot-offset slot) sb-vm:n-word-bytes) lowtag)))
3504 (format t "#define ~A_SIZE ~d~%"
3505 (string-upcase c-name) (sb-vm:primitive-object-length obj)))
3507 (:language-agnostic
3508 (when (eq name 'sb-vm::thread)
3509 (format t "~%#define THREAD_HEADER_SLOTS ~d~%" sb-vm::thread-header-slots)
3510 (dovector (x sb-vm::+thread-header-slot-names+)
3511 (let ((s (package-symbolicate "SB-VM" "THREAD-" x "-SLOT")))
3512 (format t "#define ~a ~d~%" (c-name (string s)) (symbol-value s))))
3513 (terpri))))
3514 (case name
3515 (sb-vm::unwind-block
3516 (sub-write-primitive-object (get-primitive-obj 'catch-block) lang))
3517 (sb-kernel:closure
3518 (sub-write-primitive-object (get-primitive-obj 'simple-fun) lang)
3519 (sub-write-primitive-object (get-primitive-obj 'code) lang))
3520 (instance
3521 (sub-write-primitive-object (get-primitive-obj 'funcallable-instance) lang)
3522 (when (eq lang :c)
3523 (write-wired-layout-ids *standard-output*)
3524 (write-structure-type (layout-info (find-layout 'layout)) *standard-output*
3525 "layout")
3526 (write-cast-operator 'layout "layout" sb-vm:instance-pointer-lowtag
3527 *standard-output*)
3528 (format t "#include ~S~%"
3529 (namestring (merge-pathnames "instance.inc" (lispobj-dot-h)))))))))
3531 (defvar included-lispobj-h)
3532 (defun write-primitive-object (obj *standard-output*)
3533 (sub-write-primitive-object obj :language-agnostic)
3534 (format t "#ifdef __ASSEMBLER__~2%")
3535 (format t "/* These offsets are SLOT-OFFSET * N-WORD-BYTES - LOWTAG~%")
3536 (format t " * so they work directly on tagged addresses. */~2%")
3537 (sub-write-primitive-object obj :asm)
3538 (format t "~%#else /* __ASSEMBLER__ */~2%")
3539 (format t "#include ~S~%" (lispobj-dot-h))
3540 (setq included-lispobj-h t)
3541 (sub-write-primitive-object obj :c)
3542 (format t "~%#endif /* __ASSEMBLER__ */~%"))
3544 (defun write-hash-table-flag-extractors ()
3545 ;; 'flags' is a packed integer.
3546 ;; See PACK-HT-FLAGS-WEAKNESS and PACK-HT-FLAGS-KIND in hash-table.lisp
3547 (format t "
3548 static inline int hashtable_kind(struct hash_table* ht) { return (ht->uw_flags >> 4) & 3; }
3549 static inline int hashtable_weakp(struct hash_table* ht) { return ht->uw_flags & 8; }
3550 static inline int hashtable_weakness(struct hash_table* ht) { return ht->uw_flags >> 6; }
3551 #define HASHTABLE_KIND_EQL 1~%"))
3553 (defun write-structure-type (dd *standard-output* &optional structure-tag)
3554 (labels
3555 ((cstring (designator) (c-name (string-downcase designator)))
3556 (output (dd structure-tag)
3557 (format t "struct ~A {~%" structure-tag)
3558 (format t " lispobj header; // = word_0_~%")
3559 ;; If the user's data starts at slot index 1, then index 0 is the layout.
3560 (when (= sb-vm:instance-data-start 1)
3561 (format t " lispobj _layout;~%")) ; Avoid name clash with CLASSOID-LAYOUT
3562 ;; Output exactly the number of Lisp words consumed by the structure,
3563 ;; no more, no less. C code can always compute the padded length from
3564 ;; the precise length, but the other way doesn't work.
3565 (let ((names
3566 (coerce (loop for i from sb-vm:instance-data-start below (dd-length dd)
3567 collect (list (format nil "word_~D_" (1+ i))))
3568 'vector)))
3569 (dolist (slot (dd-slots dd))
3570 (let ((cell (aref names (- (dsd-index slot) sb-vm:instance-data-start)))
3571 (name (cstring (dsd-name slot))))
3572 (case (dsd-raw-type slot)
3573 ((t) (rplaca cell name))
3574 ;; remind C programmers which slots are untagged
3575 (sb-vm:signed-word (rplaca cell (format nil "sw_~a" name)))
3576 (sb-vm:word (rplaca cell (format nil "uw_~a" name)))
3577 (t (rplacd cell name)))))
3578 ;; The reason this loops over NAMES instead of DD-SLOTS is that one slot
3579 ;; could output more than one lispword. This would happen with a DOUBLE-FLOAT
3580 ;; on 32-bit machines.
3581 (loop for slot across names
3583 (format t " ~A ~A;~@[ // ~A~]~%"
3584 (cond ((string= (car slot) "next_weak_hash_table")
3585 "struct hash_table*")
3586 (t "lispobj"))
3587 ;; reserved word
3588 (if (string= (car slot) "default") "_default" (car slot))
3589 (cdr slot))))
3590 (format t "};~%")))
3591 (unless included-lispobj-h ; looks better without redundant inclusions
3592 (setq included-lispobj-h t)
3593 (format t "#include ~S~%" (lispobj-dot-h)))
3594 (output dd (or structure-tag (cstring (dd-name dd))))
3595 (when (eq (dd-name dd) 'sb-impl::general-hash-table)
3596 (write-hash-table-flag-extractors))
3597 (when (eq (dd-name dd) 'sb-lockless::split-ordered-list)
3598 (terpri)
3599 (output (layout-info (find-layout 'sb-lockless::list-node)) "list_node")
3600 (terpri)
3601 (output (layout-info (find-layout 'sb-lockless::so-data-node)) "solist_node")
3602 (format t "static inline int so_dummy_node_p(struct solist_node* n) {
3603 return !(n->node_hash & ~D);~%}~%" (sb-vm:fixnumize 1)))))
3605 (defun write-thread-init (stream)
3606 (dolist (binding sb-vm::per-thread-c-interface-symbols)
3607 (format stream "INITIALIZE_TLS(~A, ~A);~%"
3608 (c-symbol-name (if (listp binding) (car binding) binding) "*")
3609 (let ((val (if (listp binding) (second binding))))
3610 (if (eq val 't) "LISP_T" val)))))
3612 (defun maybe-relativize (value)
3613 #-relocatable-static-space value
3614 #+relocatable-static-space (- value sb-vm:static-space-start))
3616 (defun write-static-symbols (stream)
3617 (dolist (symbol (cons nil (coerce sb-vm:+static-symbols+ 'list)))
3618 (format stream "#define ~A LISPOBJ(~:[~;STATIC_SPACE_START + ~]0x~X)~%"
3619 ;; FIXME: It would be nice not to need to strip anything
3620 ;; that doesn't get stripped always by C-SYMBOL-NAME.
3621 (if (eq symbol 't) "LISP_T" (c-symbol-name symbol "%*.!"))
3622 #-relocatable-static-space nil
3623 #+relocatable-static-space t
3624 (maybe-relativize
3625 (if *static* ; if we ran GENESIS
3626 ;; We actually ran GENESIS, use the real value.
3627 (descriptor-bits (cold-intern symbol))
3628 (+ sb-vm:nil-value
3629 (if symbol (sb-vm:static-symbol-offset symbol) 0))))))
3630 (format stream "#define LFLIST_TAIL_ATOM LISPOBJ(~:[~;STATIC_SPACE_START + ~]0x~X)~%"
3631 #-relocatable-static-space nil
3632 #+relocatable-static-space t
3633 (maybe-relativize (descriptor-bits *lflist-tail-atom*)))
3634 #+sb-thread
3635 (dolist (binding sb-vm::per-thread-c-interface-symbols)
3636 (let* ((symbol (car (ensure-list binding)))
3637 (c-symbol (c-symbol-name symbol "*")))
3638 (unless (member symbol sb-vm::+common-static-symbols+)
3639 ;; So that "#ifdef thing" works, but not as a C expression
3640 (format stream "#define ~A (*)~%" c-symbol))
3641 (format stream "#define ~A_tlsindex 0x~X~%"
3642 c-symbol (ensure-symbol-tls-index symbol))))
3643 ;; This #define is relative to the start of the fixedobj space to allow heap relocation.
3644 #+compact-instance-header
3645 (format stream "~@{#define LAYOUT_OF_~A (lispobj)(~A_SPACE_START+0x~x)~%~}"
3646 "FUNCTION"
3647 #+permgen "PERMGEN" #-permgen "FIXEDOBJ"
3648 (- (cold-layout-descriptor-bits 'function)
3649 (gspace-byte-address (cold-layout-gspace))))
3651 ;; C can call via the lisp linkage table for the known indices
3652 #+linkage-space
3653 (loop for symbol in sb-vm::+c-callable-fdefns+
3654 do (format stream "#define ~A_fname_index ~d~%"
3655 (c-symbol-name symbol) (ensure-linkage-index symbol)))
3657 ;; Everybody else can address each fdefn directly.
3658 #-linkage-space
3659 (loop for symbol in sb-vm::+c-callable-fdefns+
3660 for index from 0
3662 (format stream "#define ~A_FDEFN LISPOBJ(~:[~;STATIC_SPACE_START + ~]0x~X)~%"
3663 (c-symbol-name symbol)
3664 #-relocatable-static-space nil
3665 #+relocatable-static-space t
3666 (maybe-relativize
3667 (if *static* ; if we ran GENESIS
3668 ;; We actually ran GENESIS, use the real value.
3669 (descriptor-bits (ensure-cold-fdefn symbol))
3670 ;; We didn't run GENESIS, so guess at the address.
3671 (+ sb-vm:nil-value
3672 (* (length sb-vm:+static-symbols+)
3673 (sb-vm:pad-data-block sb-vm:symbol-size))
3674 (* index (sb-vm:pad-data-block sb-vm:fdefn-size))))))))
3676 (defun init-runtime-routines ()
3677 (dolist (symbol sb-vm::*runtime-asm-routines*)
3678 (let* ((des (cold-intern symbol :gspace *static*)))
3679 (cold-set des (make-descriptor (lookup-assembler-reference symbol))))))
3681 (defun write-sc+offset-coding (stream)
3682 (flet ((write-array (name bytes)
3683 (format stream "static struct sc_and_offset_byte ~A[] = {~@
3684 ~{ {~{ ~2D, ~2D ~}}~^,~%~}~@
3685 };~2%"
3686 name
3687 (mapcar (lambda (byte)
3688 (list (byte-size byte) (byte-position byte)))
3689 bytes))))
3690 (format stream "struct sc_and_offset_byte {
3691 int size;
3692 int position;
3693 };~2%")
3694 (write-array "sc_and_offset_sc_number_bytes" sb-c::+sc+offset-scn-bytes+)
3695 (write-array "sc_and_offset_offset_bytes" sb-c::+sc+offset-offset-bytes+)))
3697 ;;;; writing map file
3699 ;;; Write a map file describing the cold load. Some of this
3700 ;;; information is subject to change due to relocating GC, but even so
3701 ;;; it can be very handy when attempting to troubleshoot the early
3702 ;;; stages of cold load.
3703 (defparameter *boilerplate-text* "
3704 (a note about initially undefined function references: These functions
3705 are referred to by code which is installed by GENESIS, but they are not
3706 installed by GENESIS. This is not necessarily a problem; functions can
3707 be defined later, by cold init toplevel forms, or in files compiled and
3708 loaded at warm init, or elsewhere. As long as they are defined before
3709 they are called, everything should be OK. Things are also OK if the
3710 cross-compiler knew their inline definition and used that everywhere
3711 that they were called before the out-of-line definition is installed,
3712 as is fairly common for structure accessors.)")
3714 (defun write-map (*standard-output* &aux (*print-pretty* nil)
3715 (*print-case* :upcase))
3716 (format t "Table of contents~%")
3717 (format t "=================~%")
3718 (let ((sections '("assembler routines" "defined functions" "undefined functions"
3719 "classoids" "layouts"
3720 "packages" "symbols"
3721 "type specifiers"
3722 "alien linkage table" #+sb-thread "TLS map")))
3723 (dotimes (i (length sections))
3724 (format t "~4<~@R~>. ~A~%" (1+ i) (nth i sections))))
3725 (format t "=================~2%")
3727 (format t "I. assembler routines defined in core image: (base=~x)~2%"
3728 (descriptor-bits *assembler-routines*))
3729 (dolist (routine *asm-routine-alist*)
3730 (let ((name (car routine)))
3731 (format t "~8,'0X: ~S~%" (lookup-assembler-reference name) name)))
3733 #+linkage-space (print-lisp-linkage-space-map)
3734 #-linkage-space
3735 (let ((funs nil) (undefs nil))
3736 (maphash (lambda (name fdefn &aux (fun (cold-fdefn-fun fdefn)))
3737 (let ((fdefn-bits (descriptor-bits fdefn)))
3738 (if (cold-null fun)
3739 (push `(,fdefn-bits ,name) undefs)
3740 (push `(,fdefn-bits ,(descriptor-bits fun) ,name) funs))))
3741 *cold-fdefn-objects*)
3742 (format t "~%~|~%II.A. defined functions (alphabetically):
3744 FDEFN FUNCTION NAME
3745 ========== ========== ====~:{~%~10,'0X ~10,'0X ~S~}~%"
3746 (sort (copy-list funs) #'string<
3747 :key (lambda (x) (fun-name-block-name (caddr x)))))
3748 (format t "~%~|~%II.B. defined functions (numerically):
3750 FDEFN FUNCTION NAME
3751 ========== ========== ====~:{~%~10,'0X ~10,'0X ~S~}~%"
3752 (sort (copy-list funs) #'< :key #'second))
3754 (format t "~%~|~A~%
3755 III. initially undefined function references (alphabetically):
3757 FDEFN NAME
3758 ========== ====~:{~%~10,'0X ~S~}~%"
3759 *boilerplate-text*
3760 (sort undefs
3761 (lambda (a b &aux (pkg-a (sb-xc:package-name (sb-xc:symbol-package a)))
3762 (pkg-b (sb-xc:package-name (sb-xc:symbol-package b))))
3763 (cond ((string< pkg-a pkg-b) t)
3764 ((string> pkg-a pkg-b) nil)
3765 (t (string< a b))))
3766 :key (lambda (x) (fun-name-block-name (cadr x))))))
3768 (format t "~%~|~%IV. classoids:
3770 CELL CLASSOID NAME
3771 ========== ========== ====~%")
3772 (let ((dumped-classoids))
3773 (dolist (x (sort (%hash-table-alist *classoid-cells*) #'string< :key #'car))
3774 (destructuring-bind (name . cell) x
3775 (format t "~10,'0x ~:[ ~;~:*~10,'0X~] ~S~%"
3776 (descriptor-bits cell)
3777 (let ((classoid (read-slot cell :classoid)))
3778 (unless (cold-null classoid)
3779 (push classoid dumped-classoids)
3780 (descriptor-bits classoid)))
3781 name)))
3782 ;; Things sometimes go wrong with dumped classoids, so show a memory dump too
3783 (terpri)
3784 (dolist (classoid dumped-classoids)
3785 (let ((nwords (logand (ash (read-bits-wordindexed classoid 0)
3786 (- sb-vm:instance-length-shift))
3787 sb-vm:instance-length-mask)))
3788 (format t "Classoid @ ~x, ~d words:~%" (descriptor-bits classoid) (1+ nwords))
3789 (dotimes (i (1+ nwords)) ; include the header word in output
3790 (format t "~2d: ~10x~%" i (read-bits-wordindexed classoid i)))
3791 (terpri))))
3793 (format t "~%~|~%V. layout names:~2%")
3794 (format t "~28tBitmap Depth ID Name [Length]~%")
3795 (dolist (pair (sort-cold-layouts))
3796 (let* ((proxy (cdr pair))
3797 (descriptor (cold-layout-descriptor proxy))
3798 (addr (descriptor-bits descriptor)))
3799 (format t "~10,'0X -> ~10,'0X: ~8d ~2D ~5D ~S [~D]~%"
3800 addr
3802 (cold-layout-bitmap proxy)
3803 (cold-layout-depthoid proxy)
3804 (cold-layout-id proxy)
3805 (car pair)
3806 (cold-layout-length proxy))))
3808 (format t "~%~|~%VI. packages:~2%")
3809 (dolist (pair (sort (%hash-table-alist *cold-package-symbols*) #'<
3810 :key (lambda (x) (descriptor-bits (cddr x)))))
3811 (let ((pkg (cddr pair)))
3812 (format t "~x = ~a (ID=~d)~%" (descriptor-bits pkg) (car pair)
3813 (descriptor-fixnum (read-slot pkg :id)))))
3815 (format t "~%~|~%VII. symbols (numerically):~2%")
3816 (mapc (lambda (cell)
3817 (let* ((addr (car cell))
3818 (host-sym (cdr cell))
3819 (val
3820 (unless (or (keywordp host-sym) (null host-sym))
3821 (read-bits-wordindexed (cold-intern host-sym)
3822 sb-vm:symbol-value-slot))))
3823 (format t "~X: ~S~@[ = ~X~]~%" addr host-sym
3824 (unless (eql val sb-vm:unbound-marker-widetag) val))))
3825 (sort (%hash-table-alist *cold-symbols*) #'< :key #'car))
3827 (format t "~%~|~%VIII. parsed type specifiers:~2%")
3828 (format t " [Hash]~%")
3829 (let ((sorted
3830 (sort (%hash-table-alist *host->cold-ctype*) #'<
3831 :key (lambda (x) (descriptor-bits (cdr x))))))
3832 (mapc (lambda (cell &aux (host-obj (car cell)) (addr (descriptor-bits (cdr cell))))
3833 (when (ctype-p host-obj)
3834 (format t "~X: [~vx] ~A = ~S~%"
3835 addr (* 2 sb-vm:n-word-bytes)
3836 (descriptor-fixnum (read-slot (cdr cell) :%bits))
3837 (type-of host-obj) (type-specifier host-obj))))
3838 sorted)
3839 (format t "Lists:~%")
3840 (mapc (lambda (cell &aux (host-obj (car cell)) (addr (descriptor-bits (cdr cell))))
3841 (when (listp host-obj)
3842 (format t "~X: (~{#x~X~^ ~})~%" addr
3843 (mapcar (lambda (x) (descriptor-bits (gethash x *host->cold-ctype*)))
3844 host-obj))))
3845 sorted))
3847 (format t "~%~|~%IX. alien linkage table:~2%")
3848 (dolist (entry (sort (sb-int:%hash-table-alist *cold-foreign-symbol-table*)
3849 #'< :key #'cdr))
3850 (let ((name (car entry)))
3851 (format t " ~:[ ~;(D)~] ~8x = ~a~%"
3852 (listp name)
3853 (sb-vm::alien-linkage-table-entry-address (cdr entry))
3854 (car (ensure-list name)))))
3856 #+sb-thread
3857 (format t "~%~|~%X. TLS map:~2%~:{~4x ~s~%~}"
3858 (sort *tls-index-to-symbol* #'< :key #'car))
3860 (values))
3862 #+linkage-space
3863 (defun print-lisp-linkage-space-map ()
3864 (flet ((output (list)
3865 (format t "
3866 INDEX LINK-ADDR FNAME FUNCTION NAME
3867 ===== ========== ========== ========== ====
3868 ~:{~[ ~:;~:*~5D~] ~:[ ~;~:*~10x~] ~10,'0X ~10,'0X ~S~%~}~%"
3869 list)))
3870 (let* ((names
3871 (nconc (sb-int:%hash-table-alist *cold-fdefn-objects*) ; name -> descriptor
3872 ;; Non-nil symbols having a function def or linkage index
3873 (loop for symbol being each hash-value of *cold-symbols*
3874 using (hash-key bits)
3875 when (and symbol
3876 (let ((des (make-random-descriptor bits)))
3877 (or (plusp (fname-linkage-index des))
3878 (cold-symbol-function des nil))))
3879 collect (cons symbol (make-random-descriptor bits)))))
3880 (lines
3881 (mapcar (lambda (pair &aux (spelling (car pair)) ; symbol or (SETF symbol)
3882 (fname (cdr pair))
3883 (index (fname-linkage-index fname)))
3884 (list index
3885 (unless (eql index 0)
3886 (+ sb-vm::lisp-linkage-space-addr (ash index sb-vm:word-shift)))
3887 (descriptor-bits fname)
3888 (read-bits-wordindexed fname sb-vm:fdefn-fun-slot)
3889 spelling))
3890 names)))
3891 ;; Sort by name
3892 (format t "~%~|~%II.A. defined functions (alphabetically):")
3893 (output (sort (copy-list lines) #'string<
3894 :key (lambda (x) (fun-name-block-name (fifth x)))))
3895 ;; Sort by address
3896 (format t "~|~%II.B. defined functions (numerically):")
3897 (output (sort (copy-list lines) #'< :key (lambda (x) (fourth x)))))))
3899 ;;;; writing core file
3901 #+linkage-space
3902 (defun output-linkage-table (data-page core-file)
3903 (let* ((table *fname-table*)
3904 (n-table-entries (length table))
3905 (n-data-bytes (* n-table-entries sb-vm:n-word-bytes))
3906 (data (make-bigvec)))
3907 (expand-bigvec data n-data-bytes)
3908 (loop for i from 1 below n-table-entries ; table index 0 isn't used
3909 for offset from sb-vm:n-word-bytes by sb-vm:n-word-bytes
3910 do (let* ((fname (the descriptor (aref table i)))
3911 (fun (read-wordindexed fname sb-vm:fdefn-fun-slot)))
3912 (unless (zerop (descriptor-bits fun))
3913 (setf (bvref-word data offset)
3914 (read-bits-wordindexed fun sb-vm:simple-fun-self-slot)))))
3915 (force-output core-file) ; not sure if this does anything
3916 (let ((posn (file-position core-file)))
3917 (file-position core-file (* sb-c:+backend-page-bytes+ (1+ data-page)))
3918 (write-bigvec-as-sequence data core-file :end n-data-bytes)
3919 (force-output core-file)
3920 (file-position core-file posn))
3921 (format t "~&lisp linkage table: page=~D n-entries=~D~%" data-page n-table-entries)
3922 (write-words core-file
3923 ;; 5 = number of words in this core header entry
3924 lisp-linkage-space-core-entry-type-code 5
3925 n-table-entries data-page
3926 0) ; 0 = ELFcore linkage cell base address (not present)
3927 (+ data-page (ceiling n-data-bytes sb-vm:gencgc-page-bytes))))
3929 (defun output-gspace (gspace data-page core-file verbose)
3930 (force-output core-file)
3931 (let* ((posn (file-position core-file))
3932 (bytes (* (gspace-free-word-index gspace) sb-vm:n-word-bytes))
3933 (page-count (ceiling bytes sb-c:+backend-page-bytes+))
3934 (total-bytes (* page-count sb-c:+backend-page-bytes+)))
3936 (file-position core-file (* sb-c:+backend-page-bytes+ (1+ data-page)))
3937 (when verbose
3938 (format t "writing ~S byte~:P [~S page~:P] from ~S~%"
3939 total-bytes page-count gspace))
3941 ;; Note: It is assumed that the GSPACE allocation routines always
3942 ;; allocate whole pages (of size +backend-page-bytes+) and that any
3943 ;; empty gspace between the free pointer and the end of page will
3944 ;; be zero-filled. This will always be true under Mach on machines
3945 ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
3946 ;; 8K).
3947 (write-bigvec-as-sequence (gspace-data gspace)
3948 core-file
3949 :end total-bytes
3950 :pad-with-zeros t)
3951 (force-output core-file)
3952 (file-position core-file posn)
3954 ;; Write the directory entry.
3955 (write-words core-file (gspace-identifier gspace) (gspace-free-word-index gspace)
3956 data-page (gspace-byte-address gspace) page-count)
3958 (+ data-page page-count)))
3960 (defconstant bitmap-bytes-per-page
3961 (or #-mark-region-gc 0
3962 (/ sb-vm:gencgc-page-bytes (* sb-vm:cons-size sb-vm:n-word-bytes)
3963 sb-vm:n-byte-bits)))
3965 (defun output-page-table (gspace data-page core-file verbose)
3966 (force-output core-file)
3967 (let* ((data-bytes (* (gspace-free-word-index gspace) sb-vm:n-word-bytes))
3968 (n-ptes (ceiling data-bytes sb-vm:gencgc-page-bytes))
3969 (sizeof-corefile-pte (+ sb-vm:n-word-bytes 2))
3970 (pte-bytes (round-up (* sizeof-corefile-pte n-ptes) sb-vm:n-word-bytes))
3971 (n-code 0)
3972 (n-cons 0)
3973 (n-mixed 0)
3974 (posn (file-position core-file))
3975 (ptes (make-bigvec)))
3976 (file-position core-file (* sb-c:+backend-page-bytes+ (1+ data-page)))
3977 ;; Bitmap, if relevant, precedes the PTEs and consumes a whole number of words
3978 #+mark-region-gc
3979 (dotimes (page-index n-ptes)
3980 (write-words core-file
3981 (page-allocation-bitmap (aref (gspace-page-table gspace) page-index)))
3982 (let ((pte (aref (gspace-page-table gspace) page-index)))
3983 (unless (page-single-object-p pte) ; ordinary pages must be 100% full
3984 (setf (page-words-used pte) sb-vm::gencgc-page-words))))
3985 ;; Write as many PTEs as there are pages used.
3986 ;; A corefile PTE is { uword_t scan_start_offset; page_words_t words_used; }
3987 (expand-bigvec ptes pte-bytes)
3988 (dotimes (page-index n-ptes)
3989 (let* ((pte-offset (* page-index sizeof-corefile-pte))
3990 (pte (aref (gspace-page-table gspace) page-index))
3991 (usage (page-words-used pte))
3992 (sso (if (plusp usage)
3993 (- (* page-index sb-vm:gencgc-page-bytes)
3994 (* (page-scan-start pte) sb-vm:n-word-bytes))
3996 (type-bits (if (plusp usage)
3997 (ecase (page-type pte)
3998 (:code (incf n-code) #b111)
3999 (:list (incf n-cons) #b101)
4000 (:mixed (incf n-mixed) #b011))
4001 0)))
4002 (setf (bvref-word-unaligned ptes pte-offset) (logior sso type-bits))
4003 (setf (bvref-16 ptes (+ pte-offset sb-vm:n-word-bytes))
4004 (logior usage (if (page-single-object-p pte) 1 0)))))
4005 (when verbose
4006 (format t "movable dynamic space: ~d + ~d + ~d cons/code/mixed pages~%"
4007 n-cons n-code n-mixed))
4008 (write-bigvec-as-sequence ptes core-file :end pte-bytes)
4009 (force-output core-file)
4010 (file-position core-file posn)
4011 (write-words core-file
4012 page-table-core-entry-type-code
4013 6 ; = number of words in this core header entry
4014 sb-vm::gencgc-card-table-index-nbits
4015 n-ptes (+ (* n-ptes bitmap-bytes-per-page) pte-bytes) data-page)))
4017 ;;; Create a core file created from the cold loaded image. (This is
4018 ;;; the "initial core file" because core files could be created later
4019 ;;; by executing SAVE-LISP-AND-DIE in a running system, perhaps after we've
4020 ;;; added some functionality to the system.)
4021 (defun write-initial-core-file (filename build-id verbose)
4022 (when verbose
4023 (let ((*print-length* nil)
4024 (*print-level* nil))
4025 (format t "~&SB-XC:*FEATURES* =~&~S~%" sb-xc:*features*))
4026 (format t "[building initial core file in ~S: ~%" filename))
4028 (with-open-file (core-file filename :direction :output
4029 :element-type '(unsigned-byte 8)
4030 :if-exists :rename-and-delete)
4031 (let ((data-page 0))
4032 ;; Write the magic number.
4033 (write-words core-file core-magic)
4035 ;; Write the build ID, which contains a generated string
4036 ;; plus a suffix identifying a certain configuration of the C compiler.
4037 (binding* ((build-id (concatenate
4038 'string
4039 (or build-id
4040 (with-open-file (s "output/build-id.inc") (read s)))
4041 (if (member :msan sb-xc:*features*) "-msan" "")))
4042 ((nwords padding) (ceiling (length build-id) sb-vm:n-word-bytes)))
4043 (declare (type simple-string build-id))
4044 ;; Write BUILD-ID-CORE-ENTRY-TYPE-CODE, the length of the header,
4045 ;; length of the string, then base string chars + maybe padding.
4046 (write-words core-file build-id-core-entry-type-code
4047 (+ 3 nwords) ; 3 = fixed overhead including this word
4048 (length build-id))
4049 (dovector (char build-id) (write-byte (char-code char) core-file))
4050 (dotimes (j (- padding)) (write-byte #xff core-file)))
4052 ;; Write the function linkage table first. If present it'll be utilized when
4053 ;; loading the directory. It's not in the directory because it doesn't allocate
4054 ;; a space in the usual way: it's either random or contiguous with text space.
4055 #+linkage-space (setq data-page (output-linkage-table data-page core-file))
4056 ;; Write the Directory entry header.
4057 (write-words core-file directory-core-entry-type-code)
4058 (let ((spaces `(,*static*
4059 #+permgen ,*permgen*
4060 #+immobile-space ,@`(,*immobile-fixedobj* ,*immobile-text*)
4061 ,*dynamic* ,*read-only*)))
4062 ;; length = (5 words/space) * N spaces + 2 for header.
4063 (write-words core-file (+ (* (length spaces) 5) 2))
4064 (dolist (space spaces)
4065 (setq data-page (output-gspace space data-page core-file verbose))))
4066 (output-page-table *dynamic* data-page core-file verbose)
4068 ;; Write the initial function.
4069 (let ((initial-fun (descriptor-bits (cold-symbol-function '!cold-init))))
4070 (when verbose (format t "~&/INITIAL-FUN=#X~X~%" initial-fun))
4071 (write-words core-file initial-fun-core-entry-type-code 3 initial-fun))
4073 ;; Write the End entry.
4074 (write-words core-file end-core-entry-type-code 2)))
4076 (when verbose
4077 (format t "done]~%")
4078 (force-output))
4079 (values))
4081 ;;;; the actual GENESIS function
4083 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
4084 ;;; and/or information about a Lisp core, therefrom.
4086 ;;; output files arguments (any of which may be NIL to suppress output):
4087 ;;; CORE-FILE-NAME gets a Lisp core.
4088 ;;; C-HEADER-DIR-NAME gets the path in which to place generated headers
4089 ;;; MAP-FILE-NAME gets the name of the textual 'cold-sbcl.map' file
4090 (defun sb-cold:genesis (&key object-file-names foptrace-file-names tls-init
4091 defstruct-descriptions
4092 build-id
4093 core-file-name c-header-dir-name map-file-name
4094 (verbose t))
4096 (when verbose
4097 (format t
4098 "~&beginning GENESIS, ~A~%"
4099 (if core-file-name
4100 ;; Note: This output summarizing what we're doing is
4101 ;; somewhat telegraphic in style, not meant to imply that
4102 ;; we're not e.g. also creating a header file when we
4103 ;; create a core.
4104 (format nil "creating core ~S" core-file-name)
4105 (format nil "creating headers in ~S" c-header-dir-name))))
4107 (let ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
4109 ;; Prefill some linkage table entries perhaps
4110 (loop for (name datap) in sb-vm::*alien-linkage-table-predefined-entries*
4111 do (alien-linkage-table-note-symbol name datap))
4113 ;; Now that we've successfully read our only input file (by
4114 ;; loading the symbol table, if any), it's a good time to ensure
4115 ;; that there'll be someplace for our output files to go when
4116 ;; we're done.
4117 (flet ((frob (filename)
4118 (when filename
4119 (ensure-directories-exist filename :verbose t))))
4120 (frob core-file-name)
4121 (frob map-file-name))
4123 ;; (This shouldn't matter in normal use, since GENESIS normally
4124 ;; only runs once in any given Lisp image, but it could reduce
4125 ;; confusion if we ever experiment with running, tweaking, and
4126 ;; rerunning genesis interactively.)
4127 (do-all-symbols (sym)
4128 (remprop sym 'cold-intern-info))
4130 (check-spaces)
4132 (let ((*load-time-value-counter* 0)
4133 (*cold-fdefn-objects* (make-hash-table :test 'equal))
4134 (*cold-symbols* (make-hash-table :test 'eql)) ; integer keys
4135 (*cold-package-symbols* (make-hash-table :test 'equal)) ; string keys
4136 (*package-graph* nil) ; list of (string . list-of-string)
4137 (*read-only* (make-gspace :read-only
4138 read-only-core-space-id
4139 sb-vm:read-only-space-start))
4140 (*static* (make-gspace :static
4141 static-core-space-id
4142 sb-vm:static-space-start))
4143 #+immobile-space
4144 (*immobile-fixedobj*
4145 ;; Primordial layouts (from INITIALIZE-LAYOUTS) are made before anything else,
4146 ;; but they don't allocate starting from word index 0, because page 0 is reserved
4147 ;; for the **PRIMITIVE-OBJECT-LAYOUTS** vector.
4148 (make-gspace :immobile-fixedobj immobile-fixedobj-core-space-id
4149 sb-vm:fixedobj-space-start
4150 :free-word-index (/ sb-vm:immobile-card-bytes sb-vm:n-word-bytes)))
4151 #+immobile-space
4152 (*immobile-text*
4153 (make-gspace :immobile-text immobile-text-core-space-id sb-vm:text-space-start
4154 :objects (make-array 20000 :fill-pointer 0 :adjustable t)))
4155 #+permgen
4156 (*permgen*
4157 (make-gspace :permgen permgen-core-space-id sb-vm:permgen-space-start
4158 :free-word-index (+ sb-vm:vector-data-offset 256)))
4159 (*dynamic*
4160 (make-gspace :dynamic dynamic-core-space-id sb-vm:dynamic-space-start
4161 :page-table (make-array 100 :adjustable t :initial-element nil)))
4162 (*nil-descriptor*)
4163 (*simple-vector-0-descriptor*)
4164 (*classoid-cells* (make-hash-table :test 'eq))
4165 (*host->cold-ctype* (make-hash-table))
4166 (*cold-layouts* (make-hash-table :test 'eq)) ; symbol -> cold-layout
4167 (*cold-layout-by-addr* (make-hash-table :test 'eql)) ; addr -> cold-layout
4168 (*tls-index-to-symbol* nil)
4169 ;; '*COLD-METHODS* is never seen in the target, so does not need
4170 ;; to adhere to the #\! convention for automatic uninterning.
4171 (*cold-methods* nil)
4172 (*!cold-toplevels* nil)
4173 *asm-routine-alist*
4174 *assembler-routines*
4175 (*deferred-known-fun-refs* nil))
4177 (make-nil-descriptor)
4178 (setf *simple-vector-0-descriptor* (vector-in-core nil))
4180 (when core-file-name
4181 (read-structure-definitions defstruct-descriptions))
4182 ;; Prepare for cold load.
4183 (initialize-layouts)
4184 (initialize-static-space tls-init)
4185 (cold-set 'sb-c::*!cold-allocation-patch-point* *nil-descriptor*)
4186 (let ((n (length sb-kernel::*numeric-aspects-v*)))
4187 (cold-set 'sb-kernel::*numeric-aspects-v*
4188 (allocate-vector sb-vm:simple-vector-widetag n n)))
4189 (cold-set 'sb-kernel::*!initial-ctypes* *nil-descriptor*)
4191 ;; Load all assembler code
4192 (flet ((assembler-file-p (name) (tailwise-equal (namestring name) ".assem-obj")))
4193 (let ((files (remove-if-not #'assembler-file-p object-file-names)))
4194 ;; There should be exactly 1 assembler file, and 1 code object in it.
4195 (when files ; But it's present only in 2nd genesis.
4196 (aver (singleton-p files))
4197 (cold-load (car files) verbose nil)))
4198 (setf object-file-names (remove-if #'assembler-file-p object-file-names)))
4200 (when *assembler-routines*
4201 ;; code-debug-info stores the name->addr hashtable.
4202 ;; It's wrapped in a cons so that read-only space points to static-space
4203 ;; and not to dynamic space. #-darwin-jit doesn't need this hack.
4204 #+darwin-jit
4205 (write-wordindexed *assembler-routines* sb-vm:code-debug-info-slot
4206 (let ((z (make-fixnum-descriptor 0)))
4207 (cold-cons z z *static*)))
4208 (init-runtime-routines))
4210 ;; Initialize the *COLD-SYMBOLS* system with the information
4211 ;; from XC-STRICT-CL.
4212 (let (symbols)
4213 (do-external-symbols (symbol (find-package "XC-STRICT-CL"))
4214 (push symbol symbols))
4215 (setf symbols (sort symbols #'string<))
4216 (dolist (symbol symbols)
4217 (cold-intern (intern (symbol-name symbol) *cl-package*)
4218 :access :external)))
4220 ;; Make LOGICALLY-READONLYIZE no longer a no-op
4221 (setf (symbol-function 'logically-readonlyize)
4222 (symbol-function 'set-readonly))
4224 ;; Cold load.
4225 (dolist (file-name object-file-names)
4226 (push (cold-cons :begin-file (string-literal-to-core file-name))
4227 *!cold-toplevels*)
4228 (cold-load file-name verbose (find file-name foptrace-file-names :test 'equal)))
4230 (sb-cold::check-no-new-cl-symbols)
4232 (when (and verbose core-file-name)
4233 (format t "~&; SB-Loader: (~D~@{+~D~}) methods/other~%"
4234 (reduce #'+ *cold-methods* :key (lambda (x) (length (cdr x))))
4235 (length *!cold-toplevels*)))
4237 (cold-set '*!cold-toplevels* (list-to-core (nreverse *!cold-toplevels*)))
4238 (makunbound '*!cold-toplevels*) ; so no further PUSHes can be done
4240 ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
4241 (when core-file-name
4242 (sort-initial-methods)
4243 (resolve-deferred-known-funs)
4244 (foreign-symbols-to-core)
4245 (finish-symbols)
4246 (finalize-load-time-value-noise))
4248 ;; Write results to files.
4249 (when map-file-name
4250 (let ((all-objects (gspace-objects *dynamic*)))
4251 (when all-objects
4252 (with-open-file (stream "output/cold-sbcl.fullmap"
4253 :direction :output
4254 :if-exists :supersede)
4255 (format t "~&Headered objects: ~d, Conses: ~d~%"
4256 (count-if-not #'consp all-objects)
4257 (count-if #'consp all-objects))
4258 ;; Code/data separation causes nonlinear allocations
4259 (dovector (x (sort all-objects #'<
4260 :key (lambda (x)
4261 (descriptor-bits
4262 (if (consp x) (car x) x)))))
4263 (let* ((des (if (consp x) (car x) x))
4264 (word (read-bits-wordindexed des 0)))
4265 (format stream "~x: ~x~@[ ~x~]~%"
4266 (logandc2 (descriptor-bits des) sb-vm:lowtag-mask)
4267 word
4268 (when (and (not (consp x))
4269 (>= (logand word sb-vm:widetag-mask) #x80))
4270 (read-bits-wordindexed x 1))))))))
4271 (with-open-file (stream map-file-name :direction :output :if-exists :supersede)
4272 (write-map stream)))
4273 (when core-file-name
4274 (write-initial-core-file core-file-name build-id verbose))
4275 (unless c-header-dir-name
4276 (return-from sb-cold:genesis))
4277 (let ((filename (format nil "~A/Makefile.features" c-header-dir-name)))
4278 (ensure-directories-exist filename)
4279 (with-open-file (stream filename :direction :output :if-exists :supersede)
4280 (write-makefile-features stream)))
4281 (write-c-headers c-header-dir-name))))
4283 (defun write-mark-array-operators (stream &optional (ncards sb-vm::cards-per-page))
4284 #+host-quirks-sbcl (declare (host-sb-ext:muffle-conditions host-sb-ext:compiler-note))
4285 (format stream "#include ~S
4286 extern unsigned char *gc_card_mark;~%" (lispobj-dot-h))
4288 #-soft-card-marks
4289 (progn
4290 (aver (= ncards 1))
4291 #+nil ; these are in gencgc-impl
4292 (progn
4293 (format stream "static inline int cardseq_all_marked_nonsticky(long card) {
4294 return gc_card_mark[card] == CARD_MARKED;~%}~%")
4295 (format stream "static inline int cardseq_any_marked(long card) {
4296 return gc_card_mark[card] != CARD_UNMARKED;~%}~%")
4297 (format stream "static inline int cardseq_any_sticky_mark(long card) {
4298 return gc_card_mark[card] == STICKY_MARK;~%}~%"))
4299 (return-from write-mark-array-operators))
4301 ;; This string has a ~s and ~w so don't use FORMAT on it
4302 (write-string "
4303 /* SIMD-within-a-register algorithms
4305 * from https://graphics.stanford.edu/~seander/bithacks.html
4307 static inline uword_t word_haszero(uword_t word) {
4308 return ((word - 0x0101010101010101LL) & ~word & 0x8080808080808080LL) != 0;
4310 static inline uword_t word_has_stickymark(uword_t word) {
4311 return word_haszero(word ^ 0x0202020202020202LL);
4313 " stream)
4314 ;; In general we have to be wary of wraparound of the card index bits
4315 ;; - see example in comment above the definition of addr_to_card_index() -
4316 ;; but it's OK to treat marks as linearly addressable within a page.
4317 ;; The 'card' argument as supplied to these predicates will be
4318 ;; a page-aligned card, i.e. the first card for its page.
4319 (let* ((n-markwords
4320 ;; This is how many words (of N_WORD_BYTES) of marks there are for the
4321 ;; cards on a page.
4322 (cond ((and (= sb-vm:n-word-bytes 8) (= ncards 32)) 4)
4323 ((and (= sb-vm:n-word-bytes 8) (= ncards 16)) 2)
4324 ((and (= sb-vm:n-word-bytes 8) (= ncards 8)) 1)
4325 ((and (= sb-vm:n-word-bytes 4) (= ncards 8)) 2)
4326 (t (/ ncards sb-vm:n-word-bytes))))
4327 (indices (progn (assert (integerp ncards)) (loop for i below n-markwords collect i))))
4328 (format stream "static inline int cardseq_all_marked_nonsticky(long card) {
4329 uword_t* mark = (uword_t*)&gc_card_mark[card];
4330 return (~{mark[~d]~^ | ~}) == 0;~%}~%" indices)
4331 (format stream "static inline int cardseq_any_marked(long card) {
4332 uword_t* mark = (uword_t*)&gc_card_mark[card];
4333 return (~{mark[~d]~^ & ~}) != (uword_t)-1;~%}~%" indices)
4334 (format stream "static inline int cardseq_any_sticky_mark(long card) {
4335 uword_t* mark = (uword_t*)&gc_card_mark[card];
4336 return ~{word_has_stickymark(mark[~d])~^ || ~};~%}~%" indices)))
4338 (defun write-wired-layout-ids (stream)
4339 (terpri stream)
4340 (dolist (x '((layout "LAYOUT")
4341 (sb-impl::robinhood-hashset "HASHSET")
4342 (sb-impl::robinhood-hashset-storage "HASHSET_STORAGE")
4343 (sb-lockless::list-node "LFLIST_NODE")
4344 (sb-lockless::finalizer-node "FINALIZER_NODE")
4345 (sb-brothertree::unary-node "BROTHERTREE_UNARY_NODE")
4346 (package "PACKAGE")
4347 (hash-table "HASH_TABLE")))
4348 (destructuring-bind (type c-const) x
4349 (format stream "#define ~A_LAYOUT_ID ~D~%"
4350 c-const (sb-kernel::choose-layout-id type nil))))
4351 (terpri stream))
4353 (defparameter numeric-primitive-objects
4354 (remove nil ; SINGLE-FLOAT and/or the SIMD-PACKs might not exist
4355 (mapcar #'get-primitive-obj
4356 '(bignum ratio single-float double-float
4357 complex complex-single-float complex-double-float
4358 simd-pack simd-pack-256))))
4360 (defun write-c-headers (c-header-dir-name)
4361 (macrolet ((out-to (name &body body) ; write boilerplate and inclusion guard
4362 `(actually-out-to ,name (lambda (stream) ,@body))))
4363 (flet ((actually-out-to (name lambda)
4364 ;; A file gets a '.inc' extension, not '.h' for either or both
4365 ;; of two reasons:
4366 ;; - if it isn't self-contained, meaning that in order to #include it,
4367 ;; the consumer of it has to know something about which other headers
4368 ;; need to be #included first.
4369 ;; - it is not intended to be directly consumed because any use would
4370 ;; typically need to wrap each slot in some small calculation
4371 ;; such as native_pointer(), but we don't want to embed the layout
4372 ;; accessors into the autogenerated header. So there would instead be
4373 ;; a "src/runtime/foo.h" which includes "src/runtime/genesis/foo.inc"
4374 ;; 'thread.h' and 'gc-tables.h' violate the naming convention
4375 ;; by being non-self-contained.
4376 (let* ((extension
4377 (cond ((and (stringp name) (position #\. name)) nil)
4378 (t ".h")))
4379 (included-lispobj-h nil)
4380 (inclusion-guardp
4381 (string= extension ".h")))
4382 (with-open-file (stream (format nil "~A/~A~@[~A~]"
4383 c-header-dir-name name extension)
4384 :direction :output :if-exists :supersede)
4385 (write-boilerplate stream)
4386 (when inclusion-guardp
4387 (format stream
4388 "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~:*~A~%"
4389 (c-name (string-upcase name))))
4390 (funcall lambda stream)
4391 (when inclusion-guardp
4392 (format stream "#endif~%"))))))
4393 (out-to "sbcl" (write-config-h stream) (write-constants-h stream))
4394 (out-to "regnames" (write-regnames-h stream))
4395 (out-to "errnames" (write-errnames-h stream))
4396 (out-to "gc-tables" (sb-vm::write-gc-tables stream))
4397 (out-to "cardmarks" (write-mark-array-operators stream))
4398 (out-to "tagnames" (write-tagnames-h stream))
4399 (out-to "print.inc" (write-c-print-dispatch stream))
4400 (let* ((skip `(,(get-primitive-obj 'funcallable-instance)
4401 ,(get-primitive-obj 'catch-block)
4402 ,(get-primitive-obj 'code)
4403 ,(get-primitive-obj 'simple-fun)
4404 ,(get-primitive-obj 'fdefn)
4405 ,(get-primitive-obj 'array)
4406 ,@numeric-primitive-objects))
4407 (structs (sort (set-difference sb-vm:*primitive-objects* skip) #'string<
4408 :key #'sb-vm:primitive-object-name)))
4409 (out-to "number-types"
4410 (format stream "#include ~S~%" (lispobj-dot-h))
4411 (let ((*standard-output* stream))
4412 (mapc 'output-c-primitive-obj numeric-primitive-objects)))
4413 (dolist (obj structs)
4414 (out-to (string-downcase (sb-vm:primitive-object-name obj))
4415 (write-primitive-object obj stream)))
4416 (out-to "primitive-objects"
4417 (format stream "~&#include \"number-types.h\"~%")
4418 (dolist (obj structs)
4419 ;; exclude some not-really-object types
4420 (unless (member (sb-vm:primitive-object-name obj)
4421 '(sb-vm::unwind-block sb-vm::binding))
4422 (format stream "~&#include \"~A.h\"~%"
4423 (string-downcase (sb-vm:primitive-object-name obj)))))))
4424 ;; For purposes of the C code, cast all hash tables as general_hash_table
4425 ;; even if they lack the slots for weak tables.
4426 (out-to "hash-table"
4427 (write-structure-type (layout-info (find-layout 'sb-impl::general-hash-table))
4428 stream "hash_table"))
4429 (out-to "brothertree"
4430 (write-structure-type (layout-info (find-layout 'sb-brothertree::unary-node))
4431 stream "unary_node")
4432 (write-structure-type (layout-info (find-layout 'sb-brothertree::binary-node))
4433 stream "binary_node")
4434 (format stream "extern uword_t brothertree_find_lesseql(uword_t key, lispobj tree);~%"))
4435 (dolist (class '(defstruct-description package
4436 ;; FIXME: probably these should be external?
4437 sb-lockless::split-ordered-list
4438 sb-vm::arena
4439 sb-c::compiled-debug-info))
4440 (out-to (string-downcase class)
4441 ;; parent/child structs like to be output as one header, child first
4442 (let ((child (case class
4443 (defstruct-description 'defstruct-slot-description)
4444 (package 'sb-impl::symbol-table))))
4445 (when child
4446 (write-structure-type (layout-info (find-layout child)) stream)))
4447 (write-structure-type (layout-info (find-layout class)) stream)))
4448 (with-open-file (stream (format nil "~A/thread-init.inc" c-header-dir-name)
4449 :direction :output :if-exists :supersede)
4450 (write-boilerplate stream) ; no inclusion guard, it's not a ".h" file
4451 (write-thread-init stream))
4452 (out-to "static-symbols" (write-static-symbols stream))
4453 (out-to "sc-offset" (write-sc+offset-coding stream)))))
4455 ;;; Invert the action of HOST-CONSTANT-TO-CORE. If STRICTP is given as NIL,
4456 ;;; then we can produce a host object even if it is not a faithful rendition.
4457 (defun host-object-from-core (descriptor &optional (strictp t))
4458 (named-let recurse ((x descriptor))
4459 (when (symbolp x)
4460 (return-from recurse x))
4461 (when (cold-null x)
4462 (return-from recurse nil))
4463 (when (is-fixnum-lowtag (descriptor-lowtag x))
4464 (return-from recurse (descriptor-fixnum x)))
4465 #+64-bit
4466 (when (is-other-immediate-lowtag (descriptor-lowtag x))
4467 (ecase (logand (descriptor-bits x) sb-vm:widetag-mask)
4468 (#.sb-vm:single-float-widetag
4469 (return-from recurse
4470 (unsigned-bits-to-single-float (ash (descriptor-bits x) -32))))))
4471 (ecase (descriptor-lowtag x)
4472 (#.sb-vm:instance-pointer-lowtag
4473 (if strictp (error "Can't invert INSTANCE type") "#<instance>"))
4474 (#.sb-vm:list-pointer-lowtag
4475 (cons (recurse (cold-car x)) (recurse (cold-cdr x))))
4476 (#.sb-vm:fun-pointer-lowtag
4477 (if strictp
4478 (error "Can't map cold-fun -> warm-fun")
4479 #+nil ; FIXME: not done, but only needed for debugging genesis
4480 (let ((name (read-wordindexed x sb-vm:simple-fun-name-slot)))
4481 `(function ,(recurse name)))))
4482 (#.sb-vm:other-pointer-lowtag
4483 (let ((widetag (descriptor-widetag x)))
4484 (ecase widetag
4485 (#.sb-vm:symbol-widetag
4486 (if strictp
4487 (warm-symbol x)
4488 (or (gethash (descriptor-bits x) *cold-symbols*) ; first try
4489 (make-symbol (read-cold-symbol-name x)))))
4490 (#.sb-vm:simple-base-string-widetag (base-string-from-core x))
4491 (#.sb-vm:simple-vector-widetag (vector-from-core x #'recurse))
4492 #-64-bit
4493 (#.sb-vm:single-float-widetag
4494 (unsigned-bits-to-single-float (read-bits-wordindexed x 1)))
4495 (#.sb-vm:double-float-widetag
4496 (double-float-from-core x))
4497 (#.sb-vm:bignum-widetag (bignum-from-core x))))))))
4499 ;;; This is for FOP-SPEC-VECTOR which always supplies 0 for the start
4500 (defun read-n-bytes (stream vector start nbytes)
4501 (aver (zerop start))
4502 (let ((start (+ (descriptor-byte-offset vector)
4503 (ash sb-vm:vector-data-offset sb-vm:word-shift))))
4504 (read-into-bigvec (descriptor-mem vector) stream start nbytes)))