3f1a909f9cb27f890f6ccb6279b6c72cd0214cb0
[sbcl.git] / src / compiler / generic / genesis.lisp
1 ;;;; "cold" core image builder: This is how we create a target Lisp
2 ;;;; system from scratch, by converting from fasl files to an image
3 ;;;; file in the cross-compilation host, without the help of the
4 ;;;; target Lisp system.
5 ;;;;
6 ;;;; As explained by Rob MacLachlan on the CMU CL mailing list Wed, 06
7 ;;;; Jan 1999 11:05:02 -0500, this cold load generator more or less
8 ;;;; fakes up static function linking. I.e. it makes sure that all the
9 ;;;; DEFUN-defined functions in the fasl files it reads are bound to the
10 ;;;; corresponding symbols before execution starts. It doesn't do
11 ;;;; anything to initialize variable values; instead it just arranges
12 ;;;; for !COLD-INIT to be called at cold load time. !COLD-INIT is
13 ;;;; responsible for explicitly initializing anything which has to be
14 ;;;; initialized early before it transfers control to the ordinary
15 ;;;; top level forms.
16 ;;;;
17 ;;;; (In CMU CL, and in SBCL as of 0.6.9 anyway, functions not defined
18 ;;;; by DEFUN aren't set up specially by GENESIS. In particular,
19 ;;;; structure slot accessors are not set up. Slot accessors are
20 ;;;; available at cold init time because they're usually compiled
21 ;;;; inline. They're not available as out-of-line functions until the
22 ;;;; toplevel forms installing them have run.)
23
24 ;;;; This software is part of the SBCL system. See the README file for
25 ;;;; more information.
26 ;;;;
27 ;;;; This software is derived from the CMU CL system, which was
28 ;;;; written at Carnegie Mellon University and released into the
29 ;;;; public domain. The software is in the public domain and is
30 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
31 ;;;; files for more information.
32
33 (in-package "SB!FASL")
34
35 ;;; a magic number used to identify our core files
36 (defconstant core-magic
37   (logior (ash (sb!xc:char-code #\S) 24)
38           (ash (sb!xc:char-code #\B) 16)
39           (ash (sb!xc:char-code #\C) 8)
40           (sb!xc:char-code #\L)))
41
42 ;;; the current version of SBCL core files
43 ;;;
44 ;;; FIXME: This is left over from CMU CL, and not well thought out.
45 ;;; It's good to make sure that the runtime doesn't try to run core
46 ;;; files from the wrong version, but a single number is not the ideal
47 ;;; way to do this in high level data like this (as opposed to e.g. in
48 ;;; IP packets), and in fact the CMU CL version number never ended up
49 ;;; being incremented past 0. A better approach might be to use a
50 ;;; string which is set from CVS data. (Though now as of sbcl-0.7.8 or
51 ;;; so, we have another problem that the core incompatibility
52 ;;; detection mechanisms are on such a hair trigger -- with even
53 ;;; different builds from the same sources being considered
54 ;;; incompatible -- that any coarser-grained versioning mechanisms
55 ;;; like this are largely irrelevant as long as the hair-triggering
56 ;;; persists.)
57 ;;;
58 ;;; 0: inherited from CMU CL
59 ;;; 1: rearranged static symbols for sbcl-0.6.8
60 ;;; 2: eliminated non-ANSI %DEFCONSTANT/%%DEFCONSTANT support,
61 ;;;    deleted a slot from DEBUG-SOURCE structure
62 ;;; 3: added build ID to cores to discourage sbcl/.core mismatch
63 (defconstant sbcl-core-version-integer 3)
64
65 (defun round-up (number size)
66   #!+sb-doc
67   "Round NUMBER up to be an integral multiple of SIZE."
68   (* size (ceiling number size)))
69 \f
70 ;;;; implementing the concept of "vector" in (almost) portable
71 ;;;; Common Lisp
72 ;;;;
73 ;;;; "If you only need to do such simple things, it doesn't really
74 ;;;; matter which language you use." -- _ANSI Common Lisp_, p. 1, Paul
75 ;;;; Graham (evidently not considering the abstraction "vector" to be
76 ;;;; such a simple thing:-)
77
78 (eval-when (:compile-toplevel :load-toplevel :execute)
79   (defconstant +smallvec-length+
80     (expt 2 16)))
81
82 ;;; an element of a BIGVEC -- a vector small enough that we have
83 ;;; a good chance of it being portable to other Common Lisps
84 (deftype smallvec ()
85   `(simple-array (unsigned-byte 8) (,+smallvec-length+)))
86
87 (defun make-smallvec ()
88   (make-array +smallvec-length+ :element-type '(unsigned-byte 8)))
89
90 ;;; a big vector, implemented as a vector of SMALLVECs
91 ;;;
92 ;;; KLUDGE: This implementation seems portable enough for our
93 ;;; purposes, since realistically every modern implementation is
94 ;;; likely to support vectors of at least 2^16 elements. But if you're
95 ;;; masochistic enough to read this far into the contortions imposed
96 ;;; on us by ANSI and the Lisp community, for daring to use the
97 ;;; abstraction of a large linearly addressable memory space, which is
98 ;;; after all only directly supported by the underlying hardware of at
99 ;;; least 99% of the general-purpose computers in use today, then you
100 ;;; may be titillated to hear that in fact this code isn't really
101 ;;; portable, because as of sbcl-0.7.4 we need somewhat more than
102 ;;; 16Mbytes to represent a core, and ANSI only guarantees that
103 ;;; ARRAY-DIMENSION-LIMIT is not less than 1024. -- WHN 2002-06-13
104 (defstruct bigvec
105   (outer-vector (vector (make-smallvec)) :type (vector smallvec)))
106
107 ;;; analogous to SVREF, but into a BIGVEC
108 (defun bvref (bigvec index)
109   (multiple-value-bind (outer-index inner-index)
110       (floor index +smallvec-length+)
111     (aref (the smallvec
112             (svref (bigvec-outer-vector bigvec) outer-index))
113           inner-index)))
114 (defun (setf bvref) (new-value bigvec index)
115   (multiple-value-bind (outer-index inner-index)
116       (floor index +smallvec-length+)
117     (setf (aref (the smallvec
118                   (svref (bigvec-outer-vector bigvec) outer-index))
119                 inner-index)
120           new-value)))
121
122 ;;; analogous to LENGTH, but for a BIGVEC
123 ;;;
124 ;;; the length of BIGVEC, measured in the number of BVREFable bytes it
125 ;;; can hold
126 (defun bvlength (bigvec)
127   (* (length (bigvec-outer-vector bigvec))
128      +smallvec-length+))
129
130 ;;; analogous to WRITE-SEQUENCE, but for a BIGVEC
131 (defun write-bigvec-as-sequence (bigvec stream &key (start 0) end)
132   (loop for i of-type index from start below (or end (bvlength bigvec)) do
133         (write-byte (bvref bigvec i)
134                     stream)))
135
136 ;;; analogous to READ-SEQUENCE-OR-DIE, but for a BIGVEC
137 (defun read-bigvec-as-sequence-or-die (bigvec stream &key (start 0) end)
138   (loop for i of-type index from start below (or end (bvlength bigvec)) do
139         (setf (bvref bigvec i)
140               (read-byte stream))))
141
142 ;;; Grow BIGVEC (exponentially, so that large increases in size have
143 ;;; asymptotic logarithmic cost per byte).
144 (defun expand-bigvec (bigvec)
145   (let* ((old-outer-vector (bigvec-outer-vector bigvec))
146          (length-old-outer-vector (length old-outer-vector))
147          (new-outer-vector (make-array (* 2 length-old-outer-vector))))
148     (dotimes (i length-old-outer-vector)
149       (setf (svref new-outer-vector i)
150             (svref old-outer-vector i)))
151     (loop for i from length-old-outer-vector below (length new-outer-vector) do
152           (setf (svref new-outer-vector i)
153                 (make-smallvec)))
154     (setf (bigvec-outer-vector bigvec)
155           new-outer-vector))
156   bigvec)
157 \f
158 ;;;; looking up bytes and multi-byte values in a BIGVEC (considering
159 ;;;; it as an image of machine memory on the cross-compilation target)
160
161 ;;; BVREF-32 and friends. These are like SAP-REF-n, except that
162 ;;; instead of a SAP we use a BIGVEC.
163 (macrolet ((make-bvref-n
164             (n)
165             (let* ((name (intern (format nil "BVREF-~A" n)))
166                    (number-octets (/ n 8))
167                    (ash-list-le
168                     (loop for i from 0 to (1- number-octets)
169                           collect `(ash (bvref bigvec (+ byte-index ,i))
170                                         ,(* i 8))))
171                    (ash-list-be
172                     (loop for i from 0 to (1- number-octets)
173                           collect `(ash (bvref bigvec
174                                                (+ byte-index
175                                                   ,(- number-octets 1 i)))
176                                         ,(* i 8))))
177                    (setf-list-le
178                     (loop for i from 0 to (1- number-octets)
179                           append
180                           `((bvref bigvec (+ byte-index ,i))
181                             (ldb (byte 8 ,(* i 8)) new-value))))
182                    (setf-list-be
183                     (loop for i from 0 to (1- number-octets)
184                           append
185                           `((bvref bigvec (+ byte-index ,i))
186                             (ldb (byte 8 ,(- n 8 (* i 8))) new-value)))))
187               `(progn
188                  (defun ,name (bigvec byte-index)
189                    (logior ,@(ecase sb!c:*backend-byte-order*
190                                (:little-endian ash-list-le)
191                                (:big-endian ash-list-be))))
192                  (defun (setf ,name) (new-value bigvec byte-index)
193                    (setf ,@(ecase sb!c:*backend-byte-order*
194                              (:little-endian setf-list-le)
195                              (:big-endian setf-list-be))))))))
196   (make-bvref-n 8)
197   (make-bvref-n 16)
198   (make-bvref-n 32)
199   (make-bvref-n 64))
200
201 ;; lispobj-sized word, whatever that may be
202 ;; hopefully nobody ever wants a 128-bit SBCL...
203 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
204 (progn
205 (defun bvref-word (bytes index)
206   (bvref-64 bytes index))
207 (defun (setf bvref-word) (new-val bytes index)
208   (setf (bvref-64 bytes index) new-val)))
209
210 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
211 (progn
212 (defun bvref-word (bytes index)
213   (bvref-32 bytes index))
214 (defun (setf bvref-word) (new-val bytes index)
215   (setf (bvref-32 bytes index) new-val)))
216
217 \f
218 ;;;; representation of spaces in the core
219
220 ;;; If there is more than one dynamic space in memory (i.e., if a
221 ;;; copying GC is in use), then only the active dynamic space gets
222 ;;; dumped to core.
223 (defvar *dynamic*)
224 (defconstant dynamic-core-space-id 1)
225
226 (defvar *static*)
227 (defconstant static-core-space-id 2)
228
229 (defvar *read-only*)
230 (defconstant read-only-core-space-id 3)
231
232 (defconstant descriptor-low-bits 16
233   "the number of bits in the low half of the descriptor")
234 (defconstant target-space-alignment (ash 1 descriptor-low-bits)
235   "the alignment requirement for spaces in the target.
236   Must be at least (ASH 1 DESCRIPTOR-LOW-BITS)")
237
238 ;;; a GENESIS-time representation of a memory space (e.g. read-only
239 ;;; space, dynamic space, or static space)
240 (defstruct (gspace (:constructor %make-gspace)
241                    (:copier nil))
242   ;; name and identifier for this GSPACE
243   (name (missing-arg) :type symbol :read-only t)
244   (identifier (missing-arg) :type fixnum :read-only t)
245   ;; the word address where the data will be loaded
246   (word-address (missing-arg) :type unsigned-byte :read-only t)
247   ;; the data themselves. (Note that in CMU CL this was a pair of
248   ;; fields SAP and WORDS-ALLOCATED, but that wasn't very portable.)
249   ;; (And then in SBCL this was a VECTOR, but turned out to be
250   ;; unportable too, since ANSI doesn't think that arrays longer than
251   ;; 1024 (!) should needed by portable CL code...)
252   (bytes (make-bigvec) :read-only t)
253   ;; the index of the next unwritten word (i.e. chunk of
254   ;; SB!VM:N-WORD-BYTES bytes) in BYTES, or equivalently the number of
255   ;; words actually written in BYTES. In order to convert to an actual
256   ;; index into BYTES, thus must be multiplied by SB!VM:N-WORD-BYTES.
257   (free-word-index 0))
258
259 (defun gspace-byte-address (gspace)
260   (ash (gspace-word-address gspace) sb!vm:word-shift))
261
262 (def!method print-object ((gspace gspace) stream)
263   (print-unreadable-object (gspace stream :type t)
264     (format stream "~S" (gspace-name gspace))))
265
266 (defun make-gspace (name identifier byte-address)
267   (unless (zerop (rem byte-address target-space-alignment))
268     (error "The byte address #X~X is not aligned on a #X~X-byte boundary."
269            byte-address
270            target-space-alignment))
271   (%make-gspace :name name
272                 :identifier identifier
273                 :word-address (ash byte-address (- sb!vm:word-shift))))
274 \f
275 ;;;; representation of descriptors
276
277 (defstruct (descriptor
278             (:constructor make-descriptor
279                           (high low &optional gspace word-offset))
280             (:copier nil))
281   ;; the GSPACE that this descriptor is allocated in, or NIL if not set yet.
282   (gspace nil :type (or gspace null))
283   ;; the offset in words from the start of GSPACE, or NIL if not set yet
284   (word-offset nil :type (or sb!vm:word null))
285   ;; the high and low halves of the descriptor
286   ;;
287   ;; KLUDGE: Judging from the comments in genesis.lisp of the CMU CL
288   ;; old-rt compiler, this split dates back from a very early version
289   ;; of genesis where 32-bit integers were represented as conses of
290   ;; two 16-bit integers. In any system with nice (UNSIGNED-BYTE 32)
291   ;; structure slots, like CMU CL >= 17 or any version of SBCL, there
292   ;; seems to be no reason to persist in this. -- WHN 19990917
293   high
294   low)
295 (def!method print-object ((des descriptor) stream)
296   (let ((lowtag (descriptor-lowtag des)))
297     (print-unreadable-object (des stream :type t)
298       (cond ((or (= lowtag sb!vm:even-fixnum-lowtag)
299                  (= lowtag sb!vm:odd-fixnum-lowtag))
300              (let ((unsigned (logior (ash (descriptor-high des)
301                                           (1+ (- descriptor-low-bits
302                                                  sb!vm:n-lowtag-bits)))
303                                      (ash (descriptor-low des)
304                                           (- 1 sb!vm:n-lowtag-bits)))))
305                (format stream
306                        "for fixnum: ~W"
307                        (if (> unsigned #x1FFFFFFF)
308                            (- unsigned #x40000000)
309                            unsigned))))
310             ((or (= lowtag sb!vm:other-immediate-0-lowtag)
311                  (= lowtag sb!vm:other-immediate-1-lowtag)
312                  #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
313                  (= lowtag sb!vm:other-immediate-2-lowtag)
314                  #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
315                  (= lowtag sb!vm:other-immediate-3-lowtag))
316              (format stream
317                      "for other immediate: #X~X, type #b~8,'0B"
318                      (ash (descriptor-bits des) (- sb!vm:n-widetag-bits))
319                      (logand (descriptor-low des) sb!vm:widetag-mask)))
320             (t
321              (format stream
322                      "for pointer: #X~X, lowtag #b~3,'0B, ~A"
323                      (logior (ash (descriptor-high des) descriptor-low-bits)
324                              (logandc2 (descriptor-low des) sb!vm:lowtag-mask))
325                      lowtag
326                      (let ((gspace (descriptor-gspace des)))
327                        (if gspace
328                            (gspace-name gspace)
329                            "unknown"))))))))
330
331 ;;; Return a descriptor for a block of LENGTH bytes out of GSPACE. The
332 ;;; free word index is boosted as necessary, and if additional memory
333 ;;; is needed, we grow the GSPACE. The descriptor returned is a
334 ;;; pointer of type LOWTAG.
335 (defun allocate-cold-descriptor (gspace length lowtag)
336   (let* ((bytes (round-up length (ash 1 sb!vm:n-lowtag-bits)))
337          (old-free-word-index (gspace-free-word-index gspace))
338          (new-free-word-index (+ old-free-word-index
339                                  (ash bytes (- sb!vm:word-shift)))))
340     ;; Grow GSPACE as necessary until it's big enough to handle
341     ;; NEW-FREE-WORD-INDEX.
342     (do ()
343         ((>= (bvlength (gspace-bytes gspace))
344              (* new-free-word-index sb!vm:n-word-bytes)))
345       (expand-bigvec (gspace-bytes gspace)))
346     ;; Now that GSPACE is big enough, we can meaningfully grab a chunk of it.
347     (setf (gspace-free-word-index gspace) new-free-word-index)
348     (let ((ptr (+ (gspace-word-address gspace) old-free-word-index)))
349       (make-descriptor (ash ptr (- sb!vm:word-shift descriptor-low-bits))
350                        (logior (ash (logand ptr
351                                             (1- (ash 1
352                                                      (- descriptor-low-bits
353                                                         sb!vm:word-shift))))
354                                     sb!vm:word-shift)
355                                lowtag)
356                        gspace
357                        old-free-word-index))))
358
359 (defun descriptor-lowtag (des)
360   #!+sb-doc
361   "the lowtag bits for DES"
362   (logand (descriptor-low des) sb!vm:lowtag-mask))
363
364 (defun descriptor-bits (des)
365   (logior (ash (descriptor-high des) descriptor-low-bits)
366           (descriptor-low des)))
367
368 (defun descriptor-fixnum (des)
369   (let ((bits (descriptor-bits des)))
370     (if (logbitp (1- sb!vm:n-word-bits) bits)
371         ;; KLUDGE: The (- SB!VM:N-WORD-BITS 2) term here looks right to
372         ;; me, and it works, but in CMU CL it was (1- SB!VM:N-WORD-BITS),
373         ;; and although that doesn't make sense for me, or work for me,
374         ;; it's hard to see how it could have been wrong, since CMU CL
375         ;; genesis worked. It would be nice to understand how this came
376         ;; to be.. -- WHN 19990901
377         (logior (ash bits (- 1 sb!vm:n-lowtag-bits))
378                 (ash -1 (1+ sb!vm:n-positive-fixnum-bits)))
379         (ash bits (- 1 sb!vm:n-lowtag-bits)))))
380
381 ;;; common idioms
382 (defun descriptor-bytes (des)
383   (gspace-bytes (descriptor-intuit-gspace des)))
384 (defun descriptor-byte-offset (des)
385   (ash (descriptor-word-offset des) sb!vm:word-shift))
386
387 ;;; If DESCRIPTOR-GSPACE is already set, just return that. Otherwise,
388 ;;; figure out a GSPACE which corresponds to DES, set it into
389 ;;; (DESCRIPTOR-GSPACE DES), set a consistent value into
390 ;;; (DESCRIPTOR-WORD-OFFSET DES), and return the GSPACE.
391 (declaim (ftype (function (descriptor) gspace) descriptor-intuit-gspace))
392 (defun descriptor-intuit-gspace (des)
393   (if (descriptor-gspace des)
394     (descriptor-gspace des)
395     ;; KLUDGE: It's not completely clear to me what's going on here;
396     ;; this is a literal translation from of some rather mysterious
397     ;; code from CMU CL's DESCRIPTOR-SAP function. Some explanation
398     ;; would be nice. -- WHN 19990817
399     (let ((lowtag (descriptor-lowtag des))
400           (high (descriptor-high des))
401           (low (descriptor-low des)))
402       (if (or (eql lowtag sb!vm:fun-pointer-lowtag)
403               (eql lowtag sb!vm:instance-pointer-lowtag)
404               (eql lowtag sb!vm:list-pointer-lowtag)
405               (eql lowtag sb!vm:other-pointer-lowtag))
406         (dolist (gspace (list *dynamic* *static* *read-only*)
407                         (error "couldn't find a GSPACE for ~S" des))
408           ;; This code relies on the fact that GSPACEs are aligned
409           ;; such that the descriptor-low-bits low bits are zero.
410           (when (and (>= high (ash (gspace-word-address gspace)
411                                    (- sb!vm:word-shift descriptor-low-bits)))
412                      (<= high (ash (+ (gspace-word-address gspace)
413                                       (gspace-free-word-index gspace))
414                                    (- sb!vm:word-shift descriptor-low-bits))))
415             (setf (descriptor-gspace des) gspace)
416             (setf (descriptor-word-offset des)
417                   (+ (ash (- high (ash (gspace-word-address gspace)
418                                        (- sb!vm:word-shift
419                                           descriptor-low-bits)))
420                           (- descriptor-low-bits sb!vm:word-shift))
421                      (ash (logandc2 low sb!vm:lowtag-mask)
422                           (- sb!vm:word-shift))))
423             (return gspace)))
424         (error "don't even know how to look for a GSPACE for ~S" des)))))
425
426 (defun make-random-descriptor (value)
427   (make-descriptor (logand (ash value (- descriptor-low-bits))
428                            (1- (ash 1
429                                     (- sb!vm:n-word-bits
430                                        descriptor-low-bits))))
431                    (logand value (1- (ash 1 descriptor-low-bits)))))
432
433 (defun make-fixnum-descriptor (num)
434   (when (>= (integer-length num)
435             (1+ (- sb!vm:n-word-bits sb!vm:n-lowtag-bits)))
436     (error "~W is too big for a fixnum." num))
437   (make-random-descriptor (ash num (1- sb!vm:n-lowtag-bits))))
438
439 (defun make-other-immediate-descriptor (data type)
440   (make-descriptor (ash data (- sb!vm:n-widetag-bits descriptor-low-bits))
441                    (logior (logand (ash data (- descriptor-low-bits
442                                                 sb!vm:n-widetag-bits))
443                                    (1- (ash 1 descriptor-low-bits)))
444                            type)))
445
446 (defun make-character-descriptor (data)
447   (make-other-immediate-descriptor data sb!vm:base-char-widetag))
448
449 (defun descriptor-beyond (des offset type)
450   (let* ((low (logior (+ (logandc2 (descriptor-low des) sb!vm:lowtag-mask)
451                          offset)
452                       type))
453          (high (+ (descriptor-high des)
454                   (ash low (- descriptor-low-bits)))))
455     (make-descriptor high (logand low (1- (ash 1 descriptor-low-bits))))))
456 \f
457 ;;;; miscellaneous variables and other noise
458
459 ;;; a numeric value to be returned for undefined foreign symbols, or NIL if
460 ;;; undefined foreign symbols are to be treated as an error.
461 ;;; (In the first pass of GENESIS, needed to create a header file before
462 ;;; the C runtime can be built, various foreign symbols will necessarily
463 ;;; be undefined, but we don't need actual values for them anyway, and
464 ;;; we can just use 0 or some other placeholder. In the second pass of
465 ;;; GENESIS, all foreign symbols should be defined, so any undefined
466 ;;; foreign symbol is a problem.)
467 ;;;
468 ;;; KLUDGE: It would probably be cleaner to rewrite GENESIS so that it
469 ;;; never tries to look up foreign symbols in the first place unless
470 ;;; it's actually creating a core file (as in the second pass) instead
471 ;;; of using this hack to allow it to go through the motions without
472 ;;; causing an error. -- WHN 20000825
473 (defvar *foreign-symbol-placeholder-value*)
474
475 ;;; a handle on the trap object
476 (defvar *unbound-marker*)
477 ;; was:  (make-other-immediate-descriptor 0 sb!vm:unbound-marker-widetag)
478
479 ;;; a handle on the NIL object
480 (defvar *nil-descriptor*)
481
482 ;;; the head of a list of TOPLEVEL-THINGs describing stuff to be done
483 ;;; when the target Lisp starts up
484 ;;;
485 ;;; Each TOPLEVEL-THING can be a function to be executed or a fixup or
486 ;;; loadtime value, represented by (CONS KEYWORD ..). The FILENAME
487 ;;; tells which fasl file each list element came from, for debugging
488 ;;; purposes.
489 (defvar *current-reversed-cold-toplevels*)
490
491 ;;; the name of the object file currently being cold loaded (as a string, not a
492 ;;; pathname), or NIL if we're not currently cold loading any object file
493 (defvar *cold-load-filename* nil)
494 (declaim (type (or string null) *cold-load-filename*))
495 \f
496 ;;;; miscellaneous stuff to read and write the core memory
497
498 ;;; FIXME: should be DEFINE-MODIFY-MACRO
499 (defmacro cold-push (thing list)
500   #!+sb-doc
501   "Push THING onto the given cold-load LIST."
502   `(setq ,list (cold-cons ,thing ,list)))
503
504 (declaim (ftype (function (descriptor sb!vm:word) descriptor) read-wordindexed))
505 (defun read-wordindexed (address index)
506   #!+sb-doc
507   "Return the value which is displaced by INDEX words from ADDRESS."
508   (let* ((gspace (descriptor-intuit-gspace address))
509          (bytes (gspace-bytes gspace))
510          (byte-index (ash (+ index (descriptor-word-offset address))
511                           sb!vm:word-shift))
512          (value (bvref-word bytes byte-index)))
513     (make-random-descriptor value)))
514
515 (declaim (ftype (function (descriptor) descriptor) read-memory))
516 (defun read-memory (address)
517   #!+sb-doc
518   "Return the value at ADDRESS."
519   (read-wordindexed address 0))
520
521 ;;; (Note: In CMU CL, this function expected a SAP-typed ADDRESS
522 ;;; value, instead of the SAP-INT we use here.)
523 (declaim (ftype (function (sb!vm:word descriptor) (values))
524                 note-load-time-value-reference))
525 (defun note-load-time-value-reference (address marker)
526   (cold-push (cold-cons
527               (cold-intern :load-time-value-fixup)
528               (cold-cons (sap-int-to-core address)
529                          (cold-cons
530                           (number-to-core (descriptor-word-offset marker))
531                           *nil-descriptor*)))
532              *current-reversed-cold-toplevels*)
533   (values))
534
535 (declaim (ftype (function (descriptor sb!vm:word descriptor)) write-wordindexed))
536 (defun write-wordindexed (address index value)
537   #!+sb-doc
538   "Write VALUE displaced INDEX words from ADDRESS."
539   ;; KLUDGE: There is an algorithm (used in DESCRIPTOR-INTUIT-GSPACE)
540   ;; for calculating the value of the GSPACE slot from scratch. It
541   ;; doesn't work for all values, only some of them, but mightn't it
542   ;; be reasonable to see whether it works on VALUE before we give up
543   ;; because (DESCRIPTOR-GSPACE VALUE) isn't set? (Or failing that,
544   ;; perhaps write a comment somewhere explaining why it's not a good
545   ;; idea?) -- WHN 19990817
546   (if (and (null (descriptor-gspace value))
547            (not (null (descriptor-word-offset value))))
548     (note-load-time-value-reference (+ (logandc2 (descriptor-bits address)
549                                                  sb!vm:lowtag-mask)
550                                        (ash index sb!vm:word-shift))
551                                     value)
552     (let* ((bytes (gspace-bytes (descriptor-intuit-gspace address)))
553            (byte-index (ash (+ index (descriptor-word-offset address))
554                                sb!vm:word-shift)))
555       (setf (bvref-word bytes byte-index)
556             (descriptor-bits value)))))
557
558 (declaim (ftype (function (descriptor descriptor)) write-memory))
559 (defun write-memory (address value)
560   #!+sb-doc
561   "Write VALUE (a DESCRIPTOR) at ADDRESS (also a DESCRIPTOR)."
562   (write-wordindexed address 0 value))
563 \f
564 ;;;; allocating images of primitive objects in the cold core
565
566 ;;; There are three kinds of blocks of memory in the type system:
567 ;;; * Boxed objects (cons cells, structures, etc): These objects have no
568 ;;;   header as all slots are descriptors.
569 ;;; * Unboxed objects (bignums): There is a single header word that contains
570 ;;;   the length.
571 ;;; * Vector objects: There is a header word with the type, then a word for
572 ;;;   the length, then the data.
573 (defun allocate-boxed-object (gspace length lowtag)
574   #!+sb-doc
575   "Allocate LENGTH words in GSPACE and return a new descriptor of type LOWTAG
576   pointing to them."
577   (allocate-cold-descriptor gspace (ash length sb!vm:word-shift) lowtag))
578 (defun allocate-unboxed-object (gspace element-bits length type)
579   #!+sb-doc
580   "Allocate LENGTH units of ELEMENT-BITS bits plus a header word in GSPACE and
581   return an ``other-pointer'' descriptor to them. Initialize the header word
582   with the resultant length and TYPE."
583   (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
584          (des (allocate-cold-descriptor gspace
585                                         (+ bytes sb!vm:n-word-bytes)
586                                         sb!vm:other-pointer-lowtag)))
587     (write-memory des
588                   (make-other-immediate-descriptor (ash bytes
589                                                         (- sb!vm:word-shift))
590                                                    type))
591     des))
592 (defun allocate-vector-object (gspace element-bits length type)
593   #!+sb-doc
594   "Allocate LENGTH units of ELEMENT-BITS size plus a header plus a length slot in
595   GSPACE and return an ``other-pointer'' descriptor to them. Initialize the
596   header word with TYPE and the length slot with LENGTH."
597   ;; FIXME: Here and in ALLOCATE-UNBOXED-OBJECT, BYTES is calculated using
598   ;; #'/ instead of #'CEILING, which seems wrong.
599   (let* ((bytes (/ (* element-bits length) sb!vm:n-byte-bits))
600          (des (allocate-cold-descriptor gspace
601                                         (+ bytes (* 2 sb!vm:n-word-bytes))
602                                         sb!vm:other-pointer-lowtag)))
603     (write-memory des (make-other-immediate-descriptor 0 type))
604     (write-wordindexed des
605                        sb!vm:vector-length-slot
606                        (make-fixnum-descriptor length))
607     des))
608 \f
609 ;;;; copying simple objects into the cold core
610
611 (defun string-to-core (string &optional (gspace *dynamic*))
612   #!+sb-doc
613   "Copy string into the cold core and return a descriptor to it."
614   ;; (Remember that the system convention for storage of strings leaves an
615   ;; extra null byte at the end to aid in call-out to C.)
616   (let* ((length (length string))
617          (des (allocate-vector-object gspace
618                                       sb!vm:n-byte-bits
619                                       (1+ length)
620                                       sb!vm:simple-base-string-widetag))
621          (bytes (gspace-bytes gspace))
622          (offset (+ (* sb!vm:vector-data-offset sb!vm:n-word-bytes)
623                     (descriptor-byte-offset des))))
624     (write-wordindexed des
625                        sb!vm:vector-length-slot
626                        (make-fixnum-descriptor length))
627     (dotimes (i length)
628       (setf (bvref bytes (+ offset i))
629             (sb!xc:char-code (aref string i))))
630     (setf (bvref bytes (+ offset length))
631           0) ; null string-termination character for C
632     des))
633
634 (defun bignum-to-core (n)
635   #!+sb-doc
636   "Copy a bignum to the cold core."
637   (let* ((words (ceiling (1+ (integer-length n)) sb!vm:n-word-bits))
638          (handle (allocate-unboxed-object *dynamic*
639                                           sb!vm:n-word-bits
640                                           words
641                                           sb!vm:bignum-widetag)))
642     (declare (fixnum words))
643     (do ((index 1 (1+ index))
644          (remainder n (ash remainder (- sb!vm:n-word-bits))))
645         ((> index words)
646          (unless (zerop (integer-length remainder))
647            ;; FIXME: Shouldn't this be a fatal error?
648            (warn "~W words of ~W were written, but ~W bits were left over."
649                  words n remainder)))
650       (let ((word (ldb (byte sb!vm:n-word-bits 0) remainder)))
651         (write-wordindexed handle index
652                            (make-descriptor (ash word (- descriptor-low-bits))
653                                             (ldb (byte descriptor-low-bits 0)
654                                                  word)))))
655     handle))
656
657 (defun number-pair-to-core (first second type)
658   #!+sb-doc
659   "Makes a number pair of TYPE (ratio or complex) and fills it in."
660   (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits 2 type)))
661     (write-wordindexed des 1 first)
662     (write-wordindexed des 2 second)
663     des))
664
665 (defun float-to-core (x)
666   (etypecase x
667     (single-float
668      (let ((des (allocate-unboxed-object *dynamic*
669                                          sb!vm:n-word-bits
670                                          (1- sb!vm:single-float-size)
671                                          sb!vm:single-float-widetag)))
672        (write-wordindexed des
673                           sb!vm:single-float-value-slot
674                           (make-random-descriptor (single-float-bits x)))
675        des))
676     (double-float
677      (let ((des (allocate-unboxed-object *dynamic*
678                                          sb!vm:n-word-bits
679                                          (1- sb!vm:double-float-size)
680                                          sb!vm:double-float-widetag))
681            (high-bits (make-random-descriptor (double-float-high-bits x)))
682            (low-bits (make-random-descriptor (double-float-low-bits x))))
683        (ecase sb!c:*backend-byte-order*
684          (:little-endian
685           (write-wordindexed des sb!vm:double-float-value-slot low-bits)
686           (write-wordindexed des (1+ sb!vm:double-float-value-slot) high-bits))
687          (:big-endian
688           (write-wordindexed des sb!vm:double-float-value-slot high-bits)
689           (write-wordindexed des (1+ sb!vm:double-float-value-slot) low-bits)))
690        des))))
691
692 (defun complex-single-float-to-core (num)
693   (declare (type (complex single-float) num))
694   (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
695                                       (1- sb!vm:complex-single-float-size)
696                                       sb!vm:complex-single-float-widetag)))
697     (write-wordindexed des sb!vm:complex-single-float-real-slot
698                    (make-random-descriptor (single-float-bits (realpart num))))
699     (write-wordindexed des sb!vm:complex-single-float-imag-slot
700                    (make-random-descriptor (single-float-bits (imagpart num))))
701     des))
702
703 (defun complex-double-float-to-core (num)
704   (declare (type (complex double-float) num))
705   (let ((des (allocate-unboxed-object *dynamic* sb!vm:n-word-bits
706                                       (1- sb!vm:complex-double-float-size)
707                                       sb!vm:complex-double-float-widetag)))
708     (let* ((real (realpart num))
709            (high-bits (make-random-descriptor (double-float-high-bits real)))
710            (low-bits (make-random-descriptor (double-float-low-bits real))))
711       (ecase sb!c:*backend-byte-order*
712         (:little-endian
713          (write-wordindexed des sb!vm:complex-double-float-real-slot low-bits)
714          (write-wordindexed des
715                             (1+ sb!vm:complex-double-float-real-slot)
716                             high-bits))
717         (:big-endian
718          (write-wordindexed des sb!vm:complex-double-float-real-slot high-bits)
719          (write-wordindexed des
720                             (1+ sb!vm:complex-double-float-real-slot)
721                             low-bits))))
722     (let* ((imag (imagpart num))
723            (high-bits (make-random-descriptor (double-float-high-bits imag)))
724            (low-bits (make-random-descriptor (double-float-low-bits imag))))
725       (ecase sb!c:*backend-byte-order*
726         (:little-endian
727          (write-wordindexed des
728                             sb!vm:complex-double-float-imag-slot
729                             low-bits)
730          (write-wordindexed des
731                             (1+ sb!vm:complex-double-float-imag-slot)
732                             high-bits))
733         (:big-endian
734          (write-wordindexed des
735                             sb!vm:complex-double-float-imag-slot
736                             high-bits)
737          (write-wordindexed des
738                             (1+ sb!vm:complex-double-float-imag-slot)
739                             low-bits))))
740     des))
741
742 ;;; Copy the given number to the core.
743 (defun number-to-core (number)
744   (typecase number
745     (integer (if (< (integer-length number) 
746                     (- (1+ sb!vm:n-word-bits) sb!vm:n-lowtag-bits))
747                  (make-fixnum-descriptor number)
748                  (bignum-to-core number)))
749     (ratio (number-pair-to-core (number-to-core (numerator number))
750                                 (number-to-core (denominator number))
751                                 sb!vm:ratio-widetag))
752     ((complex single-float) (complex-single-float-to-core number))
753     ((complex double-float) (complex-double-float-to-core number))
754     #!+long-float
755     ((complex long-float)
756      (error "~S isn't a cold-loadable number at all!" number))
757     (complex (number-pair-to-core (number-to-core (realpart number))
758                                   (number-to-core (imagpart number))
759                                   sb!vm:complex-widetag))
760     (float (float-to-core number))
761     (t (error "~S isn't a cold-loadable number at all!" number))))
762
763 (declaim (ftype (function (sb!vm:word) descriptor) sap-int-to-core))
764 (defun sap-int-to-core (sap-int)
765   (let ((des (allocate-unboxed-object *dynamic*
766                                       sb!vm:n-word-bits
767                                       (1- sb!vm:sap-size)
768                                       sb!vm:sap-widetag)))
769     (write-wordindexed des
770                        sb!vm:sap-pointer-slot
771                        (make-random-descriptor sap-int))
772     des))
773
774 ;;; Allocate a cons cell in GSPACE and fill it in with CAR and CDR.
775 (defun cold-cons (car cdr &optional (gspace *dynamic*))
776   (let ((dest (allocate-boxed-object gspace 2 sb!vm:list-pointer-lowtag)))
777     (write-memory dest car)
778     (write-wordindexed dest 1 cdr)
779     dest))
780
781 ;;; Make a simple-vector on the target that holds the specified
782 ;;; OBJECTS, and return its descriptor.
783 (defun vector-in-core (&rest objects)
784   (let* ((size (length objects))
785          (result (allocate-vector-object *dynamic* sb!vm:n-word-bits size
786                                          sb!vm:simple-vector-widetag)))
787     (dotimes (index size)
788       (write-wordindexed result (+ index sb!vm:vector-data-offset)
789                          (pop objects)))
790     result))
791 \f
792 ;;;; symbol magic
793
794 ;;; FIXME: This should be a &KEY argument of ALLOCATE-SYMBOL.
795 (defvar *cold-symbol-allocation-gspace* nil)
796
797 ;;; Allocate (and initialize) a symbol.
798 (defun allocate-symbol (name)
799   (declare (simple-string name))
800   (let ((symbol (allocate-unboxed-object (or *cold-symbol-allocation-gspace*
801                                              *dynamic*)
802                                          sb!vm:n-word-bits
803                                          (1- sb!vm:symbol-size)
804                                          sb!vm:symbol-header-widetag)))
805     (write-wordindexed symbol sb!vm:symbol-value-slot *unbound-marker*)
806     (write-wordindexed symbol
807                        sb!vm:symbol-hash-slot
808                        (make-fixnum-descriptor 0))
809     (write-wordindexed symbol sb!vm:symbol-plist-slot *nil-descriptor*)
810     (write-wordindexed symbol sb!vm:symbol-name-slot
811                        (string-to-core name *dynamic*))
812     (write-wordindexed symbol sb!vm:symbol-package-slot *nil-descriptor*)
813     symbol))
814
815 ;;; Set the cold symbol value of SYMBOL-OR-SYMBOL-DES, which can be either a
816 ;;; descriptor of a cold symbol or (in an abbreviation for the
817 ;;; most common usage pattern) an ordinary symbol, which will be
818 ;;; automatically cold-interned.
819 (declaim (ftype (function ((or descriptor symbol) descriptor)) cold-set))
820 (defun cold-set (symbol-or-symbol-des value)
821   (let ((symbol-des (etypecase symbol-or-symbol-des
822                       (descriptor symbol-or-symbol-des)
823                       (symbol (cold-intern symbol-or-symbol-des)))))
824     (write-wordindexed symbol-des sb!vm:symbol-value-slot value)))
825 \f
826 ;;;; layouts and type system pre-initialization
827
828 ;;; Since we want to be able to dump structure constants and
829 ;;; predicates with reference layouts, we need to create layouts at
830 ;;; cold-load time. We use the name to intern layouts by, and dump a
831 ;;; list of all cold layouts in *!INITIAL-LAYOUTS* so that type system
832 ;;; initialization can find them. The only thing that's tricky [sic --
833 ;;; WHN 19990816] is initializing layout's layout, which must point to
834 ;;; itself.
835
836 ;;; a map from class names to lists of
837 ;;;    `(,descriptor ,name ,length ,inherits ,depth)
838 ;;; KLUDGE: It would be more understandable and maintainable to use
839 ;;; DEFSTRUCT (:TYPE LIST) here. -- WHN 19990823
840 (defvar *cold-layouts* (make-hash-table :test 'equal))
841
842 ;;; a map from DESCRIPTOR-BITS of cold layouts to the name, for inverting
843 ;;; mapping
844 (defvar *cold-layout-names* (make-hash-table :test 'eql))
845
846 ;;; FIXME: *COLD-LAYOUTS* and *COLD-LAYOUT-NAMES* should be
847 ;;; initialized by binding in GENESIS.
848
849 ;;; the descriptor for layout's layout (needed when making layouts)
850 (defvar *layout-layout*)
851
852 ;;; FIXME: This information should probably be pulled out of the
853 ;;; cross-compiler's tables at genesis time instead of inserted by
854 ;;; hand here as a bare numeric constant.
855 (defconstant target-layout-length 16)
856
857 ;;; Return a list of names created from the cold layout INHERITS data
858 ;;; in X.
859 (defun listify-cold-inherits (x)
860   (let ((len (descriptor-fixnum (read-wordindexed x
861                                                   sb!vm:vector-length-slot))))
862     (collect ((res))
863       (dotimes (index len)
864         (let* ((des (read-wordindexed x (+ sb!vm:vector-data-offset index)))
865                (found (gethash (descriptor-bits des) *cold-layout-names*)))
866           (if found
867             (res found)
868             (error "unknown descriptor at index ~S (bits = ~8,'0X)"
869                    index
870                    (descriptor-bits des)))))
871       (res))))
872
873 (declaim (ftype (function (symbol descriptor descriptor descriptor) descriptor)
874                 make-cold-layout))
875 (defun make-cold-layout (name length inherits depthoid)
876   (let ((result (allocate-boxed-object *dynamic*
877                                        ;; KLUDGE: Why 1+? -- WHN 19990901
878                                        (1+ target-layout-length)
879                                        sb!vm:instance-pointer-lowtag)))
880     (write-memory result
881                   (make-other-immediate-descriptor
882                    target-layout-length sb!vm:instance-header-widetag))
883
884     ;; KLUDGE: The offsets into LAYOUT below should probably be pulled out
885     ;; of the cross-compiler's tables at genesis time instead of inserted
886     ;; by hand as bare numeric constants. -- WHN ca. 19990901
887
888     ;; Set slot 0 = the layout of the layout.
889     (write-wordindexed result sb!vm:instance-slots-offset *layout-layout*)
890
891     ;; Set the immediately following slots = CLOS hash values.
892     ;;
893     ;; Note: CMU CL didn't set these in genesis, but instead arranged
894     ;; for them to be set at cold init time. That resulted in slightly
895     ;; kludgy-looking code, but there were at least two things to be
896     ;; said for it:
897     ;;   1. It put the hash values under the control of the target Lisp's
898     ;;      RANDOM function, so that CLOS behavior would be nearly
899     ;;      deterministic (instead of depending on the implementation of
900     ;;      RANDOM in the cross-compilation host, and the state of its
901     ;;      RNG when genesis begins).
902     ;;   2. It automatically ensured that all hash values in the target Lisp
903     ;;      were part of the same sequence, so that we didn't have to worry
904     ;;      about the possibility of the first hash value set in genesis
905     ;;      being precisely equal to the some hash value set in cold init time
906     ;;      (because the target Lisp RNG has advanced to precisely the same
907     ;;      state that the host Lisp RNG was in earlier).
908     ;; Point 1 should not be an issue in practice because of the way we do our
909     ;; build procedure in two steps, so that the SBCL that we end up with has
910     ;; been created by another SBCL (whose RNG is under our control).
911     ;; Point 2 is more of an issue. If ANSI had provided a way to feed
912     ;; entropy into an RNG, we would have no problem: we'd just feed
913     ;; some specialized genesis-time-only pattern into the RNG state
914     ;; before using it. However, they didn't, so we have a slight
915     ;; problem. We address it by generating the hash values using a
916     ;; different algorithm than we use in ordinary operation.
917     (dotimes (i sb!kernel:layout-clos-hash-length)
918       (let (;; The expression here is pretty arbitrary, we just want
919             ;; to make sure that it's not something which is (1)
920             ;; evenly distributed and (2) not foreordained to arise in
921             ;; the target Lisp's (RANDOM-LAYOUT-CLOS-HASH) sequence
922             ;; and show up as the CLOS-HASH value of some other
923             ;; LAYOUT.
924             ;;
925             ;; FIXME: This expression here can generate a zero value,
926             ;; and the CMU CL code goes out of its way to generate
927             ;; strictly positive values (even though the field is
928             ;; declared as an INDEX). Check that it's really OK to
929             ;; have zero values in the CLOS-HASH slots.
930             (hash-value (mod (logxor (logand   (random-layout-clos-hash) 15253)
931                                      (logandc2 (random-layout-clos-hash) 15253)
932                                      1)
933                              ;; (The MOD here is defensive programming
934                              ;; to make sure we never write an
935                              ;; out-of-range value even if some joker
936                              ;; sets LAYOUT-CLOS-HASH-MAX to other
937                              ;; than 2^n-1 at some time in the
938                              ;; future.)
939                              (1+ sb!kernel:layout-clos-hash-max))))
940         (write-wordindexed result
941                            (+ i sb!vm:instance-slots-offset 1)
942                            (make-fixnum-descriptor hash-value))))
943
944     ;; Set other slot values.
945     (let ((base (+ sb!vm:instance-slots-offset
946                    sb!kernel:layout-clos-hash-length
947                    1)))
948       ;; (Offset 0 is CLASS, "the class this is a layout for", which
949       ;; is uninitialized at this point.)
950       (write-wordindexed result (+ base 1) *nil-descriptor*) ; marked invalid
951       (write-wordindexed result (+ base 2) inherits)
952       (write-wordindexed result (+ base 3) depthoid)
953       (write-wordindexed result (+ base 4) length)
954       (write-wordindexed result (+ base 5) *nil-descriptor*) ; info
955       (write-wordindexed result (+ base 6) *nil-descriptor*)) ; pure
956
957     (setf (gethash name *cold-layouts*)
958           (list result
959                 name
960                 (descriptor-fixnum length)
961                 (listify-cold-inherits inherits)
962                 (descriptor-fixnum depthoid)))
963     (setf (gethash (descriptor-bits result) *cold-layout-names*) name)
964
965     result))
966
967 (defun initialize-layouts ()
968
969   (clrhash *cold-layouts*)
970
971   ;; We initially create the layout of LAYOUT itself with NIL as the LAYOUT and
972   ;; #() as INHERITS,
973   (setq *layout-layout* *nil-descriptor*)
974   (setq *layout-layout*
975         (make-cold-layout 'layout
976                           (number-to-core target-layout-length)
977                           (vector-in-core)
978                           ;; FIXME: hard-coded LAYOUT-DEPTHOID of LAYOUT..
979                           (number-to-core 4)))
980   (write-wordindexed *layout-layout*
981                      sb!vm:instance-slots-offset
982                      *layout-layout*)
983
984   ;; Then we create the layouts that we'll need to make a correct INHERITS
985   ;; vector for the layout of LAYOUT itself..
986   ;;
987   ;; FIXME: The various LENGTH and DEPTHOID numbers should be taken from
988   ;; the compiler's tables, not set by hand.
989   (let* ((t-layout
990           (make-cold-layout 't
991                             (number-to-core 0)
992                             (vector-in-core)
993                             (number-to-core 0)))
994          (i-layout
995           (make-cold-layout 'instance
996                             (number-to-core 0)
997                             (vector-in-core t-layout)
998                             (number-to-core 1)))
999          (so-layout
1000           (make-cold-layout 'structure-object
1001                             (number-to-core 1)
1002                             (vector-in-core t-layout i-layout)
1003                             (number-to-core 2)))
1004          (bso-layout
1005           (make-cold-layout 'structure!object
1006                             (number-to-core 1)
1007                             (vector-in-core t-layout i-layout so-layout)
1008                             (number-to-core 3)))
1009          (layout-inherits (vector-in-core t-layout
1010                                           i-layout
1011                                           so-layout
1012                                           bso-layout)))
1013
1014     ;; ..and return to backpatch the layout of LAYOUT.
1015     (setf (fourth (gethash 'layout *cold-layouts*))
1016           (listify-cold-inherits layout-inherits))
1017     (write-wordindexed *layout-layout*
1018                        ;; FIXME: hardcoded offset into layout struct
1019                        (+ sb!vm:instance-slots-offset
1020                           layout-clos-hash-length
1021                           1
1022                           2)
1023                        layout-inherits)))
1024 \f
1025 ;;;; interning symbols in the cold image
1026
1027 ;;; In order to avoid having to know about the package format, we
1028 ;;; build a data structure in *COLD-PACKAGE-SYMBOLS* that holds all
1029 ;;; interned symbols along with info about their packages. The data
1030 ;;; structure is a list of sublists, where the sublists have the
1031 ;;; following format:
1032 ;;;   (<make-package-arglist>
1033 ;;;    <internal-symbols>
1034 ;;;    <external-symbols>
1035 ;;;    <imported-internal-symbols>
1036 ;;;    <imported-external-symbols>
1037 ;;;    <shadowing-symbols>
1038 ;;;    <package-documentation>)
1039 ;;;
1040 ;;; KLUDGE: It would be nice to implement the sublists as instances of
1041 ;;; a DEFSTRUCT (:TYPE LIST). (They'd still be lists, but at least we'd be
1042 ;;; using mnemonically-named operators to access them, instead of trying
1043 ;;; to remember what THIRD and FIFTH mean, and hoping that we never
1044 ;;; need to change the list layout..) -- WHN 19990825
1045
1046 ;;; an alist from packages to lists of that package's symbols to be dumped
1047 (defvar *cold-package-symbols*)
1048 (declaim (type list *cold-package-symbols*))
1049
1050 ;;; a map from descriptors to symbols, so that we can back up. The key
1051 ;;; is the address in the target core.
1052 (defvar *cold-symbols*)
1053 (declaim (type hash-table *cold-symbols*))
1054
1055 ;;; sanity check for a symbol we're about to create on the target
1056 ;;;
1057 ;;; Make sure that the symbol has an appropriate package. In
1058 ;;; particular, catch the so-easy-to-make error of typing something
1059 ;;; like SB-KERNEL:%BYTE-BLT in cold sources when what you really
1060 ;;; need is SB!KERNEL:%BYTE-BLT.
1061 (defun package-ok-for-target-symbol-p (package)
1062   (let ((package-name (package-name package)))
1063     (or
1064      ;; Cold interning things in these standard packages is OK. (Cold
1065      ;; interning things in the other standard package, CL-USER, isn't
1066      ;; OK. We just use CL-USER to expose symbols whose homes are in
1067      ;; other packages. Thus, trying to cold intern a symbol whose
1068      ;; home package is CL-USER probably means that a coding error has
1069      ;; been made somewhere.)
1070      (find package-name '("COMMON-LISP" "KEYWORD") :test #'string=)
1071      ;; Cold interning something in one of our target-code packages,
1072      ;; which are ever-so-rigorously-and-elegantly distinguished by
1073      ;; this prefix on their names, is OK too.
1074      (string= package-name "SB!" :end1 3 :end2 3)
1075      ;; This one is OK too, since it ends up being COMMON-LISP on the
1076      ;; target.
1077      (string= package-name "SB-XC")
1078      ;; Anything else looks bad. (maybe COMMON-LISP-USER? maybe an extension
1079      ;; package in the xc host? something we can't think of
1080      ;; a valid reason to cold intern, anyway...)
1081      )))
1082   
1083 ;;; like SYMBOL-PACKAGE, but safe for symbols which end up on the target
1084 ;;;
1085 ;;; Most host symbols we dump onto the target are created by SBCL
1086 ;;; itself, so that as long as we avoid gratuitously
1087 ;;; cross-compilation-unfriendly hacks, it just happens that their
1088 ;;; SYMBOL-PACKAGE in the host system corresponds to their
1089 ;;; SYMBOL-PACKAGE in the target system. However, that's not the case
1090 ;;; in the COMMON-LISP package, where we don't get to create the
1091 ;;; symbols but instead have to use the ones that the xc host created.
1092 ;;; In particular, while ANSI specifies which symbols are exported
1093 ;;; from COMMON-LISP, it doesn't specify that their home packages are
1094 ;;; COMMON-LISP, so the xc host can keep them in random packages which
1095 ;;; don't exist on the target (e.g. CLISP keeping some CL-exported
1096 ;;; symbols in the CLOS package).
1097 (defun symbol-package-for-target-symbol (symbol)
1098   ;; We want to catch weird symbols like CLISP's
1099   ;; CL:FIND-METHOD=CLOS::FIND-METHOD, but we don't want to get
1100   ;; sidetracked by ordinary symbols like :CHARACTER which happen to
1101   ;; have the same SYMBOL-NAME as exports from COMMON-LISP.
1102   (multiple-value-bind (cl-symbol cl-status)
1103       (find-symbol (symbol-name symbol) *cl-package*)
1104     (if (and (eq symbol cl-symbol)
1105              (eq cl-status :external))
1106         ;; special case, to work around possible xc host weirdness
1107         ;; in COMMON-LISP package
1108         *cl-package*
1109         ;; ordinary case
1110         (let ((result (symbol-package symbol)))
1111           (aver (package-ok-for-target-symbol-p result))
1112           result))))
1113
1114 ;;; Return a handle on an interned symbol. If necessary allocate the
1115 ;;; symbol and record which package the symbol was referenced in. When
1116 ;;; we allocate the symbol, make sure we record a reference to the
1117 ;;; symbol in the home package so that the package gets set.
1118 (defun cold-intern (symbol
1119                     &optional
1120                     (package (symbol-package-for-target-symbol symbol)))
1121
1122   (aver (package-ok-for-target-symbol-p package))
1123
1124   ;; Anything on the cross-compilation host which refers to the target
1125   ;; machinery through the host SB-XC package should be translated to
1126   ;; something on the target which refers to the same machinery
1127   ;; through the target COMMON-LISP package.
1128   (let ((p (find-package "SB-XC")))
1129     (when (eq package p)
1130       (setf package *cl-package*))
1131     (when (eq (symbol-package symbol) p)
1132       (setf symbol (intern (symbol-name symbol) *cl-package*))))
1133
1134   (let (;; Information about each cold-interned symbol is stored
1135         ;; in COLD-INTERN-INFO.
1136         ;;   (CAR COLD-INTERN-INFO) = descriptor of symbol
1137         ;;   (CDR COLD-INTERN-INFO) = list of packages, other than symbol's
1138         ;;                            own package, referring to symbol
1139         ;; (*COLD-PACKAGE-SYMBOLS* and *COLD-SYMBOLS* store basically the
1140         ;; same information, but with the mapping running the opposite way.)
1141         (cold-intern-info (get symbol 'cold-intern-info)))
1142     (unless cold-intern-info
1143       (cond ((eq (symbol-package-for-target-symbol symbol) package)
1144              (let ((handle (allocate-symbol (symbol-name symbol))))
1145                (setf (gethash (descriptor-bits handle) *cold-symbols*) symbol)
1146                (when (eq package *keyword-package*)
1147                  (cold-set handle handle))
1148                (setq cold-intern-info
1149                      (setf (get symbol 'cold-intern-info) (cons handle nil)))))
1150             (t
1151              (cold-intern symbol)
1152              (setq cold-intern-info (get symbol 'cold-intern-info)))))
1153     (unless (or (null package)
1154                 (member package (cdr cold-intern-info)))
1155       (push package (cdr cold-intern-info))
1156       (let* ((old-cps-entry (assoc package *cold-package-symbols*))
1157              (cps-entry (or old-cps-entry
1158                             (car (push (list package)
1159                                        *cold-package-symbols*)))))
1160         (unless old-cps-entry
1161           (/show "created *COLD-PACKAGE-SYMBOLS* entry for" package symbol))
1162         (push symbol (rest cps-entry))))
1163     (car cold-intern-info)))
1164
1165 ;;; Construct and return a value for use as *NIL-DESCRIPTOR*.
1166 (defun make-nil-descriptor ()
1167   (let* ((des (allocate-unboxed-object
1168                *static*
1169                sb!vm:n-word-bits
1170                sb!vm:symbol-size
1171                0))
1172          (result (make-descriptor (descriptor-high des)
1173                                   (+ (descriptor-low des)
1174                                      (* 2 sb!vm:n-word-bytes)
1175                                      (- sb!vm:list-pointer-lowtag
1176                                         sb!vm:other-pointer-lowtag)))))
1177     (write-wordindexed des
1178                        1
1179                        (make-other-immediate-descriptor
1180                         0
1181                         sb!vm:symbol-header-widetag))
1182     (write-wordindexed des
1183                        (+ 1 sb!vm:symbol-value-slot)
1184                        result)
1185     (write-wordindexed des
1186                        (+ 2 sb!vm:symbol-value-slot)
1187                        result)
1188     (write-wordindexed des
1189                        (+ 1 sb!vm:symbol-plist-slot)
1190                        result)
1191     (write-wordindexed des
1192                        (+ 1 sb!vm:symbol-name-slot)
1193                        ;; This is *DYNAMIC*, and DES is *STATIC*,
1194                        ;; because that's the way CMU CL did it; I'm
1195                        ;; not sure whether there's an underlying
1196                        ;; reason. -- WHN 1990826
1197                        (string-to-core "NIL" *dynamic*))
1198     (write-wordindexed des
1199                        (+ 1 sb!vm:symbol-package-slot)
1200                        result)
1201     (setf (get nil 'cold-intern-info)
1202           (cons result nil))
1203     (cold-intern nil)
1204     result))
1205
1206 ;;; Since the initial symbols must be allocated before we can intern
1207 ;;; anything else, we intern those here. We also set the value of T.
1208 (defun initialize-non-nil-symbols ()
1209   #!+sb-doc
1210   "Initialize the cold load symbol-hacking data structures."
1211   (let ((*cold-symbol-allocation-gspace* *static*))
1212     ;; Intern the others.
1213     (dolist (symbol sb!vm:*static-symbols*)
1214       (let* ((des (cold-intern symbol))
1215              (offset-wanted (sb!vm:static-symbol-offset symbol))
1216              (offset-found (- (descriptor-low des)
1217                               (descriptor-low *nil-descriptor*))))
1218         (unless (= offset-wanted offset-found)
1219           ;; FIXME: should be fatal
1220           (warn "Offset from ~S to ~S is ~W, not ~W"
1221                 symbol
1222                 nil
1223                 offset-found
1224                 offset-wanted))))
1225     ;; Establish the value of T.
1226     (let ((t-symbol (cold-intern t)))
1227       (cold-set t-symbol t-symbol))))
1228
1229 ;;; a helper function for FINISH-SYMBOLS: Return a cold alist suitable
1230 ;;; to be stored in *!INITIAL-LAYOUTS*.
1231 (defun cold-list-all-layouts ()
1232   (let ((result *nil-descriptor*))
1233     (maphash (lambda (key stuff)
1234                (cold-push (cold-cons (cold-intern key)
1235                                      (first stuff))
1236                           result))
1237              *cold-layouts*)
1238     result))
1239
1240 ;;; Establish initial values for magic symbols.
1241 ;;;
1242 ;;; Scan over all the symbols referenced in each package in
1243 ;;; *COLD-PACKAGE-SYMBOLS* making that for each one there's an
1244 ;;; appropriate entry in the *!INITIAL-SYMBOLS* data structure to
1245 ;;; intern it.
1246 (defun finish-symbols ()
1247
1248   ;; I think the point of setting these functions into SYMBOL-VALUEs
1249   ;; here, instead of using SYMBOL-FUNCTION, is that in CMU CL
1250   ;; SYMBOL-FUNCTION reduces to FDEFINITION, which is a pretty
1251   ;; hairy operation (involving globaldb.lisp etc.) which we don't
1252   ;; want to invoke early in cold init. -- WHN 2001-12-05
1253   ;;
1254   ;; FIXME: So OK, that's a reasonable reason to do something weird like
1255   ;; this, but this is still a weird thing to do, and we should change
1256   ;; the names to highlight that something weird is going on. Perhaps
1257   ;; *MAYBE-GC-FUN*, *INTERNAL-ERROR-FUN*, *HANDLE-BREAKPOINT-FUN*,
1258   ;; and *HANDLE-FUN-END-BREAKPOINT-FUN*...
1259   (macrolet ((frob (symbol)
1260                `(cold-set ',symbol
1261                           (cold-fdefinition-object (cold-intern ',symbol)))))
1262     (frob sub-gc)
1263     (frob internal-error)
1264     (frob sb!kernel::control-stack-exhausted-error)
1265     (frob sb!di::handle-breakpoint)
1266     (frob sb!di::handle-fun-end-breakpoint)
1267     (frob sb!thread::handle-thread-exit))
1268
1269   (cold-set 'sb!vm::*current-catch-block*          (make-fixnum-descriptor 0))
1270   (cold-set 'sb!vm::*current-unwind-protect-block* (make-fixnum-descriptor 0))
1271
1272   (cold-set '*free-interrupt-context-index* (make-fixnum-descriptor 0))
1273
1274   (cold-set '*!initial-layouts* (cold-list-all-layouts))
1275
1276   (/show "dumping packages" (mapcar #'car *cold-package-symbols*))
1277   (let ((initial-symbols *nil-descriptor*))
1278     (dolist (cold-package-symbols-entry *cold-package-symbols*)
1279       (let* ((cold-package (car cold-package-symbols-entry))
1280              (symbols (cdr cold-package-symbols-entry))
1281              (shadows (package-shadowing-symbols cold-package))
1282              (documentation (string-to-core (documentation cold-package t)))
1283              (internal *nil-descriptor*)
1284              (external *nil-descriptor*)
1285              (imported-internal *nil-descriptor*)
1286              (imported-external *nil-descriptor*)
1287              (shadowing *nil-descriptor*))
1288         (declare (type package cold-package)) ; i.e. not a target descriptor
1289         (/show "dumping" cold-package symbols)
1290
1291         ;; FIXME: Add assertions here to make sure that inappropriate stuff
1292         ;; isn't being dumped:
1293         ;;   * the CL-USER package
1294         ;;   * the SB-COLD package
1295         ;;   * any internal symbols in the CL package
1296         ;;   * basically any package other than CL, KEYWORD, or the packages
1297         ;;     in package-data-list.lisp-expr
1298         ;; and that the structure of the KEYWORD package (e.g. whether
1299         ;; any symbols are internal to it) matches what we want in the
1300         ;; target SBCL.
1301
1302         ;; FIXME: It seems possible that by looking at the contents of
1303         ;; packages in the target SBCL we could find which symbols in
1304         ;; package-data-lisp.lisp-expr are now obsolete. (If I
1305         ;; understand correctly, only symbols which actually have
1306         ;; definitions or which are otherwise referred to actually end
1307         ;; up in the target packages.)
1308
1309         (dolist (symbol symbols)
1310           (let ((handle (car (get symbol 'cold-intern-info)))
1311                 (imported-p (not (eq (symbol-package-for-target-symbol symbol)
1312                                      cold-package))))
1313             (multiple-value-bind (found where)
1314                 (find-symbol (symbol-name symbol) cold-package)
1315               (unless (and where (eq found symbol))
1316                 (error "The symbol ~S is not available in ~S."
1317                        symbol
1318                        cold-package))
1319               (when (memq symbol shadows)
1320                 (cold-push handle shadowing))
1321               (case where
1322                 (:internal (if imported-p
1323                                (cold-push handle imported-internal)
1324                                (cold-push handle internal)))
1325                 (:external (if imported-p
1326                                (cold-push handle imported-external)
1327                                (cold-push handle external)))))))
1328         (let ((r *nil-descriptor*))
1329           (cold-push documentation r)
1330           (cold-push shadowing r)
1331           (cold-push imported-external r)
1332           (cold-push imported-internal r)
1333           (cold-push external r)
1334           (cold-push internal r)
1335           (cold-push (make-make-package-args cold-package) r)
1336           ;; FIXME: It would be more space-efficient to use vectors
1337           ;; instead of lists here, and space-efficiency here would be
1338           ;; nice, since it would reduce the peak memory usage in
1339           ;; genesis and cold init.
1340           (cold-push r initial-symbols))))
1341     (cold-set '*!initial-symbols* initial-symbols))
1342
1343   (cold-set '*!initial-fdefn-objects* (list-all-fdefn-objects))
1344
1345   (cold-set '*!reversed-cold-toplevels* *current-reversed-cold-toplevels*)
1346
1347   #!+(or x86 x86-64)
1348   (progn
1349     (cold-set 'sb!vm::*fp-constant-0d0* (number-to-core 0d0))
1350     (cold-set 'sb!vm::*fp-constant-1d0* (number-to-core 1d0))
1351     (cold-set 'sb!vm::*fp-constant-0f0* (number-to-core 0f0))
1352     (cold-set 'sb!vm::*fp-constant-1f0* (number-to-core 1f0))))
1353
1354 ;;; Make a cold list that can be used as the arg list to MAKE-PACKAGE in order
1355 ;;; to make a package that is similar to PKG.
1356 (defun make-make-package-args (pkg)
1357   (let* ((use *nil-descriptor*)
1358          (cold-nicknames *nil-descriptor*)
1359          (res *nil-descriptor*))
1360     (dolist (u (package-use-list pkg))
1361       (when (assoc u *cold-package-symbols*)
1362         (cold-push (string-to-core (package-name u)) use)))
1363     (let* ((pkg-name (package-name pkg))
1364            ;; Make the package nickname lists for the standard packages
1365            ;; be the minimum specified by ANSI, regardless of what value
1366            ;; the cross-compilation host happens to use.
1367            (warm-nicknames (cond ((string= pkg-name "COMMON-LISP")
1368                                   '("CL"))
1369                                  ((string= pkg-name "COMMON-LISP-USER")
1370                                   '("CL-USER"))
1371                                  ((string= pkg-name "KEYWORD")
1372                                   '())
1373                                  ;; For packages other than the
1374                                  ;; standard packages, the nickname
1375                                  ;; list was specified by our package
1376                                  ;; setup code, not by properties of
1377                                  ;; what cross-compilation host we
1378                                  ;; happened to use, and we can just
1379                                  ;; propagate it into the target.
1380                                  (t
1381                                   (package-nicknames pkg)))))
1382       (dolist (warm-nickname warm-nicknames)
1383         (cold-push (string-to-core warm-nickname) cold-nicknames)))
1384
1385     (cold-push (number-to-core (truncate (package-internal-symbol-count pkg)
1386                                          0.8))
1387                res)
1388     (cold-push (cold-intern :internal-symbols) res)
1389     (cold-push (number-to-core (truncate (package-external-symbol-count pkg)
1390                                          0.8))
1391                res)
1392     (cold-push (cold-intern :external-symbols) res)
1393
1394     (cold-push cold-nicknames res)
1395     (cold-push (cold-intern :nicknames) res)
1396
1397     (cold-push use res)
1398     (cold-push (cold-intern :use) res)
1399
1400     (cold-push (string-to-core (package-name pkg)) res)
1401     res))
1402 \f
1403 ;;;; functions and fdefinition objects
1404
1405 ;;; a hash table mapping from fdefinition names to descriptors of cold
1406 ;;; objects
1407 ;;;
1408 ;;; Note: Since fdefinition names can be lists like '(SETF FOO), and
1409 ;;; we want to have only one entry per name, this must be an 'EQUAL
1410 ;;; hash table, not the default 'EQL.
1411 (defvar *cold-fdefn-objects*)
1412
1413 (defvar *cold-fdefn-gspace* nil)
1414
1415 ;;; Given a cold representation of a symbol, return a warm
1416 ;;; representation. 
1417 (defun warm-symbol (des)
1418   ;; Note that COLD-INTERN is responsible for keeping the
1419   ;; *COLD-SYMBOLS* table up to date, so if DES happens to refer to an
1420   ;; uninterned symbol, the code below will fail. But as long as we
1421   ;; don't need to look up uninterned symbols during bootstrapping,
1422   ;; that's OK..
1423   (multiple-value-bind (symbol found-p)
1424       (gethash (descriptor-bits des) *cold-symbols*)
1425     (declare (type symbol symbol))
1426     (unless found-p
1427       (error "no warm symbol"))
1428     symbol))
1429   
1430 ;;; like CL:CAR, CL:CDR, and CL:NULL but for cold values
1431 (defun cold-car (des)
1432   (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1433   (read-wordindexed des sb!vm:cons-car-slot))
1434 (defun cold-cdr (des)
1435   (aver (= (descriptor-lowtag des) sb!vm:list-pointer-lowtag))
1436   (read-wordindexed des sb!vm:cons-cdr-slot))
1437 (defun cold-null (des)
1438   (= (descriptor-bits des)
1439      (descriptor-bits *nil-descriptor*)))
1440   
1441 ;;; Given a cold representation of a function name, return a warm
1442 ;;; representation.
1443 (declaim (ftype (function (descriptor) (or symbol list)) warm-fun-name))
1444 (defun warm-fun-name (des)
1445   (let ((result
1446          (ecase (descriptor-lowtag des)
1447            (#.sb!vm:list-pointer-lowtag
1448             (aver (not (cold-null des))) ; function named NIL? please no..
1449             ;; Do cold (DESTRUCTURING-BIND (COLD-CAR COLD-CADR) DES ..).
1450             (let* ((car-des (cold-car des))
1451                    (cdr-des (cold-cdr des))
1452                    (cadr-des (cold-car cdr-des))
1453                    (cddr-des (cold-cdr cdr-des)))
1454               (aver (cold-null cddr-des))
1455               (list (warm-symbol car-des)
1456                     (warm-symbol cadr-des))))
1457            (#.sb!vm:other-pointer-lowtag
1458             (warm-symbol des)))))
1459     (legal-fun-name-or-type-error result)
1460     result))
1461
1462 (defun cold-fdefinition-object (cold-name &optional leave-fn-raw)
1463   (declare (type descriptor cold-name))
1464   (/show0 "/cold-fdefinition-object")
1465   (let ((warm-name (warm-fun-name cold-name)))
1466     (or (gethash warm-name *cold-fdefn-objects*)
1467         (let ((fdefn (allocate-boxed-object (or *cold-fdefn-gspace* *dynamic*)
1468                                             (1- sb!vm:fdefn-size)
1469                                             sb!vm:other-pointer-lowtag)))
1470
1471           (setf (gethash warm-name *cold-fdefn-objects*) fdefn)
1472           (write-memory fdefn (make-other-immediate-descriptor
1473                                (1- sb!vm:fdefn-size) sb!vm:fdefn-widetag))
1474           (write-wordindexed fdefn sb!vm:fdefn-name-slot cold-name)
1475           (unless leave-fn-raw
1476             (write-wordindexed fdefn sb!vm:fdefn-fun-slot
1477                                *nil-descriptor*)
1478             (write-wordindexed fdefn
1479                                sb!vm:fdefn-raw-addr-slot
1480                                (make-random-descriptor
1481                                 (cold-foreign-symbol-address-as-integer
1482                                  (sb!vm:extern-alien-name "undefined_tramp")))))
1483           fdefn))))
1484
1485 ;;; Handle the at-cold-init-time, fset-for-static-linkage operation
1486 ;;; requested by FOP-FSET.
1487 (defun static-fset (cold-name defn)
1488   (declare (type descriptor cold-name))
1489   (let ((fdefn (cold-fdefinition-object cold-name t))
1490         (type (logand (descriptor-low (read-memory defn)) sb!vm:widetag-mask)))
1491     (write-wordindexed fdefn sb!vm:fdefn-fun-slot defn)
1492     (write-wordindexed fdefn
1493                        sb!vm:fdefn-raw-addr-slot
1494                        (ecase type
1495                          (#.sb!vm:simple-fun-header-widetag
1496                           (/show0 "static-fset (simple-fun)")
1497                           #!+sparc
1498                           defn
1499                           #!-sparc
1500                           (make-random-descriptor
1501                            (+ (logandc2 (descriptor-bits defn)
1502                                         sb!vm:lowtag-mask)
1503                               (ash sb!vm:simple-fun-code-offset
1504                                    sb!vm:word-shift))))
1505                          (#.sb!vm:closure-header-widetag
1506                           (/show0 "/static-fset (closure)")
1507                           (make-random-descriptor
1508                            (cold-foreign-symbol-address-as-integer
1509                             (sb!vm:extern-alien-name "closure_tramp"))))))
1510     fdefn))
1511
1512 (defun initialize-static-fns ()
1513   (let ((*cold-fdefn-gspace* *static*))
1514     (dolist (sym sb!vm:*static-funs*)
1515       (let* ((fdefn (cold-fdefinition-object (cold-intern sym)))
1516              (offset (- (+ (- (descriptor-low fdefn)
1517                               sb!vm:other-pointer-lowtag)
1518                            (* sb!vm:fdefn-raw-addr-slot sb!vm:n-word-bytes))
1519                         (descriptor-low *nil-descriptor*)))
1520              (desired (sb!vm:static-fun-offset sym)))
1521         (unless (= offset desired)
1522           ;; FIXME: should be fatal
1523           (error "Offset from FDEFN ~S to ~S is ~W, not ~W."
1524                  sym nil offset desired))))))
1525
1526 (defun list-all-fdefn-objects ()
1527   (let ((result *nil-descriptor*))
1528     (maphash (lambda (key value)
1529                (declare (ignore key))
1530                (cold-push value result))
1531              *cold-fdefn-objects*)
1532     result))
1533 \f
1534 ;;;; fixups and related stuff
1535
1536 ;;; an EQUAL hash table
1537 (defvar *cold-foreign-symbol-table*)
1538 (declaim (type hash-table *cold-foreign-symbol-table*))
1539
1540 ;; Read the sbcl.nm file to find the addresses for foreign-symbols in
1541 ;; the C runtime.
1542 (defun load-cold-foreign-symbol-table (filename)
1543   (/show "load-cold-foreign-symbol-table" filename)
1544   (with-open-file (file filename)
1545     (loop for line = (read-line file nil nil)
1546           while line do   
1547           ;; UNIX symbol tables might have tabs in them, and tabs are
1548           ;; not in Common Lisp STANDARD-CHAR, so there seems to be no
1549           ;; nice portable way to deal with them within Lisp, alas.
1550           ;; Fortunately, it's easy to use UNIX command line tools like
1551           ;; sed to remove the problem, so it's not too painful for us
1552           ;; to push responsibility for converting tabs to spaces out to
1553           ;; the caller.
1554           ;;
1555           ;; Other non-STANDARD-CHARs are problematic for the same reason.
1556           ;; Make sure that there aren't any..
1557           (let ((ch (find-if (lambda (char)
1558                                (not (typep char 'standard-char)))
1559                              line)))
1560             (when ch
1561               (error "non-STANDARD-CHAR ~S found in foreign symbol table:~%~S"
1562                      ch
1563                      line)))
1564           (setf line (string-trim '(#\space) line))
1565           (let ((p1 (position #\space line :from-end nil))
1566                 (p2 (position #\space line :from-end t)))
1567             (if (not (and p1 p2 (< p1 p2)))
1568                 ;; KLUDGE: It's too messy to try to understand all
1569                 ;; possible output from nm, so we just punt the lines we
1570                 ;; don't recognize. We realize that there's some chance
1571                 ;; that might get us in trouble someday, so we warn
1572                 ;; about it.
1573                 (warn "ignoring unrecognized line ~S in ~A" line filename)
1574                 (multiple-value-bind (value name)
1575                     (if (string= "0x" line :end2 2)
1576                         (values (parse-integer line :start 2 :end p1 :radix 16)
1577                                 (subseq line (1+ p2)))
1578                         (values (parse-integer line :end p1 :radix 16)
1579                                 (subseq line (1+ p2))))
1580                   (multiple-value-bind (old-value found)
1581                       (gethash name *cold-foreign-symbol-table*)
1582                     (when (and found
1583                                (not (= old-value value)))
1584                       (warn "redefining ~S from #X~X to #X~X"
1585                             name old-value value)))
1586                   (/show "adding to *cold-foreign-symbol-table*:" name value)
1587                   (setf (gethash name *cold-foreign-symbol-table*) value))))))
1588   (values))     ;; PROGN
1589
1590 (defun cold-foreign-symbol-address-as-integer (name)
1591   (or (find-foreign-symbol-in-table name *cold-foreign-symbol-table*)
1592       *foreign-symbol-placeholder-value*
1593       (progn
1594         (format *error-output* "~&The foreign symbol table is:~%")
1595         (maphash (lambda (k v)
1596                    (format *error-output* "~&~S = #X~8X~%" k v))
1597                  *cold-foreign-symbol-table*)
1598         (error "The foreign symbol ~S is undefined." name))))
1599
1600 (defvar *cold-assembler-routines*)
1601
1602 (defvar *cold-assembler-fixups*)
1603
1604 (defun record-cold-assembler-routine (name address)
1605   (/xhow "in RECORD-COLD-ASSEMBLER-ROUTINE" name address)
1606   (push (cons name address)
1607         *cold-assembler-routines*))
1608
1609 (defun record-cold-assembler-fixup (routine
1610                                     code-object
1611                                     offset
1612                                     &optional
1613                                     (kind :both))
1614   (push (list routine code-object offset kind)
1615         *cold-assembler-fixups*))
1616
1617 (defun lookup-assembler-reference (symbol)
1618   (let ((value (cdr (assoc symbol *cold-assembler-routines*))))
1619     ;; FIXME: Should this be ERROR instead of WARN?
1620     (unless value
1621       (warn "Assembler routine ~S not defined." symbol))
1622     value))
1623
1624 ;;; The x86 port needs to store code fixups along with code objects if
1625 ;;; they are to be moved, so fixups for code objects in the dynamic
1626 ;;; heap need to be noted.
1627 #!+(or x86 x86-64)
1628 (defvar *load-time-code-fixups*)
1629
1630 #!+(or x86 x86-64)
1631 (defun note-load-time-code-fixup (code-object offset value kind)
1632   ;; If CODE-OBJECT might be moved
1633   (when (= (gspace-identifier (descriptor-intuit-gspace code-object))
1634            dynamic-core-space-id)
1635     ;; FIXME: pushed thing should be a structure, not just a list
1636     (push (list code-object offset value kind) *load-time-code-fixups*))
1637   (values))
1638
1639 #!+(or x86 x86-64)
1640 (defun output-load-time-code-fixups ()
1641   (dolist (fixups *load-time-code-fixups*)
1642     (let ((code-object (first fixups))
1643           (offset (second fixups))
1644           (value (third fixups))
1645           (kind (fourth fixups)))
1646       (cold-push (cold-cons
1647                   (cold-intern :load-time-code-fixup)
1648                   (cold-cons
1649                    code-object
1650                    (cold-cons
1651                     (number-to-core offset)
1652                     (cold-cons
1653                      (number-to-core value)
1654                      (cold-cons
1655                       (cold-intern kind)
1656                       *nil-descriptor*)))))
1657                  *current-reversed-cold-toplevels*))))
1658
1659 ;;; Given a pointer to a code object and an offset relative to the
1660 ;;; tail of the code object's header, return an offset relative to the
1661 ;;; (beginning of the) code object.
1662 ;;;
1663 ;;; FIXME: It might be clearer to reexpress
1664 ;;;    (LET ((X (CALC-OFFSET CODE-OBJECT OFFSET0))) ..)
1665 ;;; as
1666 ;;;    (LET ((X (+ OFFSET0 (CODE-OBJECT-HEADER-N-BYTES CODE-OBJECT)))) ..).
1667 (declaim (ftype (function (descriptor sb!vm:word)) calc-offset))
1668 (defun calc-offset (code-object offset-from-tail-of-header)
1669   (let* ((header (read-memory code-object))
1670          (header-n-words (ash (descriptor-bits header)
1671                               (- sb!vm:n-widetag-bits)))
1672          (header-n-bytes (ash header-n-words sb!vm:word-shift))
1673          (result (+ offset-from-tail-of-header header-n-bytes)))
1674     result))
1675
1676 (declaim (ftype (function (descriptor sb!vm:word sb!vm:word keyword))
1677                 do-cold-fixup))
1678 (defun do-cold-fixup (code-object after-header value kind)
1679   (let* ((offset-within-code-object (calc-offset code-object after-header))
1680          (gspace-bytes (descriptor-bytes code-object))
1681          (gspace-byte-offset (+ (descriptor-byte-offset code-object)
1682                                 offset-within-code-object))
1683          (gspace-byte-address (gspace-byte-address
1684                                (descriptor-gspace code-object))))
1685     (ecase +backend-fasl-file-implementation+
1686       ;; See CMU CL source for other formerly-supported architectures
1687       ;; (and note that you have to rewrite them to use BVREF-X
1688       ;; instead of SAP-REF).
1689       (:alpha
1690          (ecase kind
1691          (:jmp-hint
1692           (assert (zerop (ldb (byte 2 0) value))))
1693          (:bits-63-48
1694           (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1695                  (value (if (logbitp 31 value) (+ value (ash 1 32)) value))
1696                  (value (if (logbitp 47 value) (+ value (ash 1 48)) value)))
1697             (setf (bvref-8 gspace-bytes gspace-byte-offset)
1698                   (ldb (byte 8 48) value)
1699                   (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1700                   (ldb (byte 8 56) value))))
1701          (:bits-47-32
1702           (let* ((value (if (logbitp 15 value) (+ value (ash 1 16)) value))
1703                  (value (if (logbitp 31 value) (+ value (ash 1 32)) value)))
1704             (setf (bvref-8 gspace-bytes gspace-byte-offset)
1705                   (ldb (byte 8 32) value)
1706                   (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1707                   (ldb (byte 8 40) value))))
1708          (:ldah
1709           (let ((value (if (logbitp 15 value) (+ value (ash 1 16)) value)))
1710             (setf (bvref-8 gspace-bytes gspace-byte-offset)
1711                   (ldb (byte 8 16) value)
1712                   (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1713                   (ldb (byte 8 24) value))))
1714          (:lda
1715           (setf (bvref-8 gspace-bytes gspace-byte-offset)
1716                 (ldb (byte 8 0) value)
1717                 (bvref-8 gspace-bytes (1+ gspace-byte-offset))
1718                 (ldb (byte 8 8) value)))))
1719       (:hppa
1720        (ecase kind
1721          (:load
1722           (setf (bvref-32 gspace-bytes gspace-byte-offset)
1723                 (logior (ash (ldb (byte 11 0) value) 1)
1724                         (logand (bvref-32 gspace-bytes gspace-byte-offset) 
1725                                 #xffffc000))))
1726          (:load-short
1727           (let ((low-bits (ldb (byte 11 0) value)))
1728             (assert (<= 0 low-bits (1- (ash 1 4))))
1729             (setf (bvref-32 gspace-bytes gspace-byte-offset)
1730                   (logior (ash low-bits 17)
1731                           (logand (bvref-32 gspace-bytes gspace-byte-offset)
1732                                   #xffe0ffff)))))
1733          (:hi
1734           (setf (bvref-32 gspace-bytes gspace-byte-offset)
1735                 (logior (ash (ldb (byte 5 13) value) 16)
1736                         (ash (ldb (byte 2 18) value) 14)
1737                         (ash (ldb (byte 2 11) value) 12)
1738                         (ash (ldb (byte 11 20) value) 1)
1739                         (ldb (byte 1 31) value)
1740                         (logand (bvref-32 gspace-bytes gspace-byte-offset)
1741                                 #xffe00000))))
1742          (:branch
1743           (let ((bits (ldb (byte 9 2) value)))
1744             (assert (zerop (ldb (byte 2 0) value)))
1745             (setf (bvref-32 gspace-bytes gspace-byte-offset)
1746                   (logior (ash bits 3)
1747                           (logand (bvref-32 gspace-bytes gspace-byte-offset)
1748                                   #xffe0e002)))))))
1749       (:mips
1750        (ecase kind
1751          (:jump
1752           (assert (zerop (ash value -28)))
1753           (setf (ldb (byte 26 0) 
1754                      (bvref-32 gspace-bytes gspace-byte-offset))
1755                 (ash value -2)))
1756          (:lui
1757           (setf (bvref-32 gspace-bytes gspace-byte-offset)
1758                 (logior (mask-field (byte 16 16)
1759                                     (bvref-32 gspace-bytes gspace-byte-offset))
1760                         (+ (ash value -16)
1761                            (if (logbitp 15 value) 1 0)))))
1762          (:addi
1763           (setf (bvref-32 gspace-bytes gspace-byte-offset)
1764                 (logior (mask-field (byte 16 16)
1765                                     (bvref-32 gspace-bytes gspace-byte-offset))
1766                         (ldb (byte 16 0) value))))))
1767        (:ppc
1768        (ecase kind
1769          (:ba
1770           (setf (bvref-32 gspace-bytes gspace-byte-offset)
1771                 (dpb (ash value -2) (byte 24 2) 
1772                      (bvref-32 gspace-bytes gspace-byte-offset))))
1773          (:ha
1774           (let* ((h (ldb (byte 16 16) value))
1775                  (l (ldb (byte 16 0) value)))
1776             (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1777                   (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
1778          (:l
1779           (setf (bvref-16 gspace-bytes (+ gspace-byte-offset 2))
1780                 (ldb (byte 16 0) value)))))     
1781       (:sparc
1782        (ecase kind
1783          (:call
1784           (error "can't deal with call fixups yet"))
1785          (:sethi
1786           (setf (bvref-32 gspace-bytes gspace-byte-offset)
1787                 (dpb (ldb (byte 22 10) value)
1788                      (byte 22 0)
1789                      (bvref-32 gspace-bytes gspace-byte-offset))))
1790          (:add
1791           (setf (bvref-32 gspace-bytes gspace-byte-offset)
1792                 (dpb (ldb (byte 10 0) value)
1793                      (byte 10 0)
1794                      (bvref-32 gspace-bytes gspace-byte-offset))))))
1795       ((:x86 :x86-64)
1796        (let* ((un-fixed-up (bvref-word gspace-bytes
1797                                                gspace-byte-offset))
1798               (code-object-start-addr (logandc2 (descriptor-bits code-object)
1799                                                 sb!vm:lowtag-mask)))
1800          (assert (= code-object-start-addr
1801                   (+ gspace-byte-address
1802                      (descriptor-byte-offset code-object))))
1803          (ecase kind
1804            (:absolute
1805             (let ((fixed-up (+ value un-fixed-up)))
1806               (setf (bvref-32 gspace-bytes gspace-byte-offset)
1807                     fixed-up)
1808               ;; comment from CMU CL sources:
1809               ;;
1810               ;; Note absolute fixups that point within the object.
1811               ;; KLUDGE: There seems to be an implicit assumption in
1812               ;; the old CMU CL code here, that if it doesn't point
1813               ;; before the object, it must point within the object
1814               ;; (not beyond it). It would be good to add an
1815               ;; explanation of why that's true, or an assertion that
1816               ;; it's really true, or both.
1817               (unless (< fixed-up code-object-start-addr)
1818                 (note-load-time-code-fixup code-object
1819                                            after-header
1820                                            value
1821                                            kind))))
1822            (:relative ; (used for arguments to X86 relative CALL instruction)
1823             (let ((fixed-up (- (+ value un-fixed-up)
1824                                gspace-byte-address
1825                                gspace-byte-offset
1826                                4))) ; "length of CALL argument"
1827               (setf (bvref-32 gspace-bytes gspace-byte-offset)
1828                     fixed-up)
1829               ;; Note relative fixups that point outside the code
1830               ;; object, which is to say all relative fixups, since
1831               ;; relative addressing within a code object never needs
1832               ;; a fixup.
1833               (note-load-time-code-fixup code-object
1834                                          after-header
1835                                          value
1836                                          kind))))))))
1837   (values))
1838
1839 (defun resolve-assembler-fixups ()
1840   (dolist (fixup *cold-assembler-fixups*)
1841     (let* ((routine (car fixup))
1842            (value (lookup-assembler-reference routine)))
1843       (when value
1844         (do-cold-fixup (second fixup) (third fixup) value (fourth fixup))))))
1845
1846 ;;; *COLD-FOREIGN-SYMBOL-TABLE* becomes *!INITIAL-FOREIGN-SYMBOLS* in
1847 ;;; the core. When the core is loaded, !LOADER-COLD-INIT uses this to
1848 ;;; create *STATIC-FOREIGN-SYMBOLS*, which the code in
1849 ;;; target-load.lisp refers to.
1850 (defun foreign-symbols-to-core ()
1851   (let ((result *nil-descriptor*))
1852     (maphash (lambda (symbol value)
1853                (cold-push (cold-cons (string-to-core symbol)
1854                                      (number-to-core value))
1855                           result))
1856              *cold-foreign-symbol-table*)
1857     (cold-set (cold-intern 'sb!kernel:*!initial-foreign-symbols*) result))
1858   (let ((result *nil-descriptor*))
1859     (dolist (rtn *cold-assembler-routines*)
1860       (cold-push (cold-cons (cold-intern (car rtn))
1861                             (number-to-core (cdr rtn)))
1862                  result))
1863     (cold-set (cold-intern '*!initial-assembler-routines*) result)))
1864
1865 \f
1866 ;;;; general machinery for cold-loading FASL files
1867
1868 ;;; FOP functions for cold loading
1869 (defvar *cold-fop-funs*
1870   ;; We start out with a copy of the ordinary *FOP-FUNS*. The ones
1871   ;; which aren't appropriate for cold load will be destructively
1872   ;; modified.
1873   (copy-seq *fop-funs*))
1874
1875 (defvar *normal-fop-funs*)
1876
1877 ;;; Cause a fop to have a special definition for cold load.
1878 ;;; 
1879 ;;; This is similar to DEFINE-FOP, but unlike DEFINE-FOP, this version
1880 ;;;   (1) looks up the code for this name (created by a previous
1881 ;;        DEFINE-FOP) instead of creating a code, and
1882 ;;;   (2) stores its definition in the *COLD-FOP-FUNS* vector,
1883 ;;;       instead of storing in the *FOP-FUNS* vector.
1884 (defmacro define-cold-fop ((name &key (pushp t) (stackp t)) &rest forms)
1885   (aver (member pushp '(nil t)))
1886   (aver (member stackp '(nil t)))
1887   (let ((code (get name 'fop-code))
1888         (fname (symbolicate "COLD-" name)))
1889     (unless code
1890       (error "~S is not a defined FOP." name))
1891     `(progn
1892        (defun ,fname ()
1893          ,@(if stackp
1894                `((with-fop-stack ,pushp ,@forms))
1895                forms))
1896        (setf (svref *cold-fop-funs* ,code) #',fname))))
1897
1898 (defmacro clone-cold-fop ((name &key (pushp t) (stackp t))
1899                           (small-name)
1900                           &rest forms)
1901   (aver (member pushp '(nil t)))
1902   (aver (member stackp '(nil t)))
1903   `(progn
1904     (macrolet ((clone-arg () '(read-word-arg)))
1905       (define-cold-fop (,name :pushp ,pushp :stackp ,stackp) ,@forms))
1906     (macrolet ((clone-arg () '(read-byte-arg)))
1907       (define-cold-fop (,small-name :pushp ,pushp :stackp ,stackp) ,@forms))))
1908
1909 ;;; Cause a fop to be undefined in cold load.
1910 (defmacro not-cold-fop (name)
1911   `(define-cold-fop (,name)
1912      (error "The fop ~S is not supported in cold load." ',name)))
1913
1914 ;;; COLD-LOAD loads stuff into the core image being built by calling
1915 ;;; LOAD-AS-FASL with the fop function table rebound to a table of cold
1916 ;;; loading functions.
1917 (defun cold-load (filename)
1918   #!+sb-doc
1919   "Load the file named by FILENAME into the cold load image being built."
1920   (let* ((*normal-fop-funs* *fop-funs*)
1921          (*fop-funs* *cold-fop-funs*)
1922          (*cold-load-filename* (etypecase filename
1923                                  (string filename)
1924                                  (pathname (namestring filename)))))
1925     (with-open-file (s filename :element-type '(unsigned-byte 8))
1926       (load-as-fasl s nil nil))))
1927 \f
1928 ;;;; miscellaneous cold fops
1929
1930 (define-cold-fop (fop-misc-trap) *unbound-marker*)
1931
1932 (define-cold-fop (fop-short-character)
1933   (make-character-descriptor (read-byte-arg)))
1934
1935 (define-cold-fop (fop-empty-list) *nil-descriptor*)
1936 (define-cold-fop (fop-truth) (cold-intern t))
1937
1938 (define-cold-fop (fop-normal-load :stackp nil)
1939   (setq *fop-funs* *normal-fop-funs*))
1940
1941 (define-fop (fop-maybe-cold-load 82 :stackp nil)
1942   (when *cold-load-filename*
1943     (setq *fop-funs* *cold-fop-funs*)))
1944
1945 (define-cold-fop (fop-maybe-cold-load :stackp nil))
1946
1947 (clone-cold-fop (fop-struct)
1948                 (fop-small-struct)
1949   (let* ((size (clone-arg))
1950          (result (allocate-boxed-object *dynamic*
1951                                         (1+ size)
1952                                         sb!vm:instance-pointer-lowtag)))
1953     (write-memory result (make-other-immediate-descriptor
1954                           size sb!vm:instance-header-widetag))
1955     (do ((index (1- size) (1- index)))
1956         ((minusp index))
1957       (declare (fixnum index))
1958       (write-wordindexed result
1959                          (+ index sb!vm:instance-slots-offset)
1960                          (pop-stack)))
1961     result))
1962
1963 (define-cold-fop (fop-layout)
1964   (let* ((length-des (pop-stack))
1965          (depthoid-des (pop-stack))
1966          (cold-inherits (pop-stack))
1967          (name (pop-stack))
1968          (old (gethash name *cold-layouts*)))
1969     (declare (type descriptor length-des depthoid-des cold-inherits))
1970     (declare (type symbol name))
1971     ;; If a layout of this name has been defined already
1972     (if old
1973       ;; Enforce consistency between the previous definition and the
1974       ;; current definition, then return the previous definition.
1975       (destructuring-bind
1976           ;; FIXME: This would be more maintainable if we used
1977           ;; DEFSTRUCT (:TYPE LIST) to define COLD-LAYOUT. -- WHN 19990825
1978           (old-layout-descriptor
1979            old-name
1980            old-length
1981            old-inherits-list
1982            old-depthoid)
1983           old
1984         (declare (type descriptor old-layout-descriptor))
1985         (declare (type index old-length))
1986         (declare (type fixnum old-depthoid))
1987         (declare (type list old-inherits-list))
1988         (aver (eq name old-name))
1989         (let ((length (descriptor-fixnum length-des))
1990               (inherits-list (listify-cold-inherits cold-inherits))
1991               (depthoid (descriptor-fixnum depthoid-des)))
1992           (unless (= length old-length)
1993             (error "cold loading a reference to class ~S when the compile~%~
1994                     time length was ~S and current length is ~S"
1995                    name
1996                    length
1997                    old-length))
1998           (unless (equal inherits-list old-inherits-list)
1999             (error "cold loading a reference to class ~S when the compile~%~
2000                     time inherits were ~S~%~
2001                     and current inherits are ~S"
2002                    name
2003                    inherits-list
2004                    old-inherits-list))
2005           (unless (= depthoid old-depthoid)
2006             (error "cold loading a reference to class ~S when the compile~%~
2007                     time inheritance depthoid was ~S and current inheritance~%~
2008                     depthoid is ~S"
2009                    name
2010                    depthoid
2011                    old-depthoid)))
2012         old-layout-descriptor)
2013       ;; Make a new definition from scratch.
2014       (make-cold-layout name length-des cold-inherits depthoid-des))))
2015 \f
2016 ;;;; cold fops for loading symbols
2017
2018 ;;; Load a symbol SIZE characters long from *FASL-INPUT-STREAM* and
2019 ;;; intern that symbol in PACKAGE.
2020 (defun cold-load-symbol (size package)
2021   (let ((string (make-string size)))
2022     (read-string-as-bytes *fasl-input-stream* string)
2023     (cold-intern (intern string package))))
2024
2025 (macrolet ((frob (name pname-len package-len)
2026              `(define-cold-fop (,name)
2027                 (let ((index (read-arg ,package-len)))
2028                   (push-fop-table
2029                    (cold-load-symbol (read-arg ,pname-len)
2030                                      (svref *current-fop-table* index)))))))
2031   (frob fop-symbol-in-package-save #.sb!vm:n-word-bytes #.sb!vm:n-word-bytes)
2032   (frob fop-small-symbol-in-package-save 1 #.sb!vm:n-word-bytes)
2033   (frob fop-symbol-in-byte-package-save #.sb!vm:n-word-bytes 1)
2034   (frob fop-small-symbol-in-byte-package-save 1 1))
2035
2036 (clone-cold-fop (fop-lisp-symbol-save)
2037                 (fop-lisp-small-symbol-save)
2038   (push-fop-table (cold-load-symbol (clone-arg) *cl-package*)))
2039
2040 (clone-cold-fop (fop-keyword-symbol-save)
2041                 (fop-keyword-small-symbol-save)
2042   (push-fop-table (cold-load-symbol (clone-arg) *keyword-package*)))
2043
2044 (clone-cold-fop (fop-uninterned-symbol-save)
2045                 (fop-uninterned-small-symbol-save)
2046   (let* ((size (clone-arg))
2047          (name (make-string size)))
2048     (read-string-as-bytes *fasl-input-stream* name)
2049     (let ((symbol-des (allocate-symbol name)))
2050       (push-fop-table symbol-des))))
2051 \f
2052 ;;;; cold fops for loading lists
2053
2054 ;;; Make a list of the top LENGTH things on the fop stack. The last
2055 ;;; cdr of the list is set to LAST.
2056 (defmacro cold-stack-list (length last)
2057   `(do* ((index ,length (1- index))
2058          (result ,last (cold-cons (pop-stack) result)))
2059         ((= index 0) result)
2060      (declare (fixnum index))))
2061
2062 (define-cold-fop (fop-list)
2063   (cold-stack-list (read-byte-arg) *nil-descriptor*))
2064 (define-cold-fop (fop-list*)
2065   (cold-stack-list (read-byte-arg) (pop-stack)))
2066 (define-cold-fop (fop-list-1)
2067   (cold-stack-list 1 *nil-descriptor*))
2068 (define-cold-fop (fop-list-2)
2069   (cold-stack-list 2 *nil-descriptor*))
2070 (define-cold-fop (fop-list-3)
2071   (cold-stack-list 3 *nil-descriptor*))
2072 (define-cold-fop (fop-list-4)
2073   (cold-stack-list 4 *nil-descriptor*))
2074 (define-cold-fop (fop-list-5)
2075   (cold-stack-list 5 *nil-descriptor*))
2076 (define-cold-fop (fop-list-6)
2077   (cold-stack-list 6 *nil-descriptor*))
2078 (define-cold-fop (fop-list-7)
2079   (cold-stack-list 7 *nil-descriptor*))
2080 (define-cold-fop (fop-list-8)
2081   (cold-stack-list 8 *nil-descriptor*))
2082 (define-cold-fop (fop-list*-1)
2083   (cold-stack-list 1 (pop-stack)))
2084 (define-cold-fop (fop-list*-2)
2085   (cold-stack-list 2 (pop-stack)))
2086 (define-cold-fop (fop-list*-3)
2087   (cold-stack-list 3 (pop-stack)))
2088 (define-cold-fop (fop-list*-4)
2089   (cold-stack-list 4 (pop-stack)))
2090 (define-cold-fop (fop-list*-5)
2091   (cold-stack-list 5 (pop-stack)))
2092 (define-cold-fop (fop-list*-6)
2093   (cold-stack-list 6 (pop-stack)))
2094 (define-cold-fop (fop-list*-7)
2095   (cold-stack-list 7 (pop-stack)))
2096 (define-cold-fop (fop-list*-8)
2097   (cold-stack-list 8 (pop-stack)))
2098 \f
2099 ;;;; cold fops for loading vectors
2100
2101 (clone-cold-fop (fop-string)
2102                 (fop-small-string)
2103   (let* ((len (clone-arg))
2104          (string (make-string len)))
2105     (read-string-as-bytes *fasl-input-stream* string)
2106     (string-to-core string)))
2107
2108 (clone-cold-fop (fop-vector)
2109                 (fop-small-vector)
2110   (let* ((size (clone-arg))
2111          (result (allocate-vector-object *dynamic*
2112                                          sb!vm:n-word-bits
2113                                          size
2114                                          sb!vm:simple-vector-widetag)))
2115     (do ((index (1- size) (1- index)))
2116         ((minusp index))
2117       (declare (fixnum index))
2118       (write-wordindexed result
2119                          (+ index sb!vm:vector-data-offset)
2120                          (pop-stack)))
2121     result))
2122
2123 (define-cold-fop (fop-int-vector)
2124   (let* ((len (read-word-arg))
2125          (sizebits (read-byte-arg))
2126          (type (case sizebits
2127                  (0 sb!vm:simple-array-nil-widetag)
2128                  (1 sb!vm:simple-bit-vector-widetag)
2129                  (2 sb!vm:simple-array-unsigned-byte-2-widetag)
2130                  (4 sb!vm:simple-array-unsigned-byte-4-widetag)
2131                  (7 (prog1 sb!vm:simple-array-unsigned-byte-7-widetag
2132                       (setf sizebits 8)))
2133                  (8 sb!vm:simple-array-unsigned-byte-8-widetag)
2134                  (15 (prog1 sb!vm:simple-array-unsigned-byte-15-widetag
2135                        (setf sizebits 16)))
2136                  (16 sb!vm:simple-array-unsigned-byte-16-widetag)
2137                  (31 (prog1 sb!vm:simple-array-unsigned-byte-31-widetag
2138                        (setf sizebits 32)))
2139                  (32 sb!vm:simple-array-unsigned-byte-32-widetag)
2140                  #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
2141                  (63 (prog1 sb!vm:simple-array-unsigned-byte-63-widetag
2142                        (setf sizebits 64)))
2143                  #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
2144                  (64 sb!vm:simple-array-unsigned-byte-64-widetag)
2145                  (t (error "losing element size: ~W" sizebits))))
2146          (result (allocate-vector-object *dynamic* sizebits len type))
2147          (start (+ (descriptor-byte-offset result)
2148                    (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2149          (end (+ start
2150                  (ceiling (* len sizebits)
2151                           sb!vm:n-byte-bits))))
2152     (read-bigvec-as-sequence-or-die (descriptor-bytes result)
2153                                     *fasl-input-stream*
2154                                     :start start
2155                                     :end end)
2156     result))
2157
2158 (define-cold-fop (fop-single-float-vector)
2159   (let* ((len (read-word-arg))
2160          (result (allocate-vector-object
2161                   *dynamic*
2162                   sb!vm:n-word-bits
2163                   len
2164                   sb!vm:simple-array-single-float-widetag))
2165          (start (+ (descriptor-byte-offset result)
2166                    (ash sb!vm:vector-data-offset sb!vm:word-shift)))
2167          (end (+ start (* len 4))))
2168     (read-bigvec-as-sequence-or-die (descriptor-bytes result)
2169                                     *fasl-input-stream*
2170                                     :start start
2171                                     :end end)
2172     result))
2173
2174 (not-cold-fop fop-double-float-vector)
2175 #!+long-float (not-cold-fop fop-long-float-vector)
2176 (not-cold-fop fop-complex-single-float-vector)
2177 (not-cold-fop fop-complex-double-float-vector)
2178 #!+long-float (not-cold-fop fop-complex-long-float-vector)
2179
2180 (define-cold-fop (fop-array)
2181   (let* ((rank (read-word-arg))
2182          (data-vector (pop-stack))
2183          (result (allocate-boxed-object *dynamic*
2184                                         (+ sb!vm:array-dimensions-offset rank)
2185                                         sb!vm:other-pointer-lowtag)))
2186     (write-memory result
2187                   (make-other-immediate-descriptor rank
2188                                                    sb!vm:simple-array-widetag))
2189     (write-wordindexed result sb!vm:array-fill-pointer-slot *nil-descriptor*)
2190     (write-wordindexed result sb!vm:array-data-slot data-vector)
2191     (write-wordindexed result sb!vm:array-displacement-slot *nil-descriptor*)
2192     (write-wordindexed result sb!vm:array-displaced-p-slot *nil-descriptor*)
2193     (let ((total-elements 1))
2194       (dotimes (axis rank)
2195         (let ((dim (pop-stack)))
2196           (unless (or (= (descriptor-lowtag dim) sb!vm:even-fixnum-lowtag)
2197                       (= (descriptor-lowtag dim) sb!vm:odd-fixnum-lowtag))
2198             (error "non-fixnum dimension? (~S)" dim))
2199           (setf total-elements
2200                 (* total-elements
2201                    (logior (ash (descriptor-high dim)
2202                                 (- descriptor-low-bits
2203                                    (1- sb!vm:n-lowtag-bits)))
2204                            (ash (descriptor-low dim)
2205                                 (- 1 sb!vm:n-lowtag-bits)))))
2206           (write-wordindexed result
2207                              (+ sb!vm:array-dimensions-offset axis)
2208                              dim)))
2209       (write-wordindexed result
2210                          sb!vm:array-elements-slot
2211                          (make-fixnum-descriptor total-elements)))
2212     result))
2213
2214 \f
2215 ;;;; cold fops for loading numbers
2216
2217 (defmacro define-cold-number-fop (fop)
2218   `(define-cold-fop (,fop :stackp nil)
2219      ;; Invoke the ordinary warm version of this fop to push the
2220      ;; number.
2221      (,fop)
2222      ;; Replace the warm fop result with the cold image of the warm
2223      ;; fop result.
2224      (with-fop-stack t
2225        (let ((number (pop-stack)))
2226          (number-to-core number)))))
2227
2228 (define-cold-number-fop fop-single-float)
2229 (define-cold-number-fop fop-double-float)
2230 (define-cold-number-fop fop-integer)
2231 (define-cold-number-fop fop-small-integer)
2232 (define-cold-number-fop fop-word-integer)
2233 (define-cold-number-fop fop-byte-integer)
2234 (define-cold-number-fop fop-complex-single-float)
2235 (define-cold-number-fop fop-complex-double-float)
2236
2237 (define-cold-fop (fop-ratio)
2238   (let ((den (pop-stack)))
2239     (number-pair-to-core (pop-stack) den sb!vm:ratio-widetag)))
2240
2241 (define-cold-fop (fop-complex)
2242   (let ((im (pop-stack)))
2243     (number-pair-to-core (pop-stack) im sb!vm:complex-widetag)))
2244 \f
2245 ;;;; cold fops for calling (or not calling)
2246
2247 (not-cold-fop fop-eval)
2248 (not-cold-fop fop-eval-for-effect)
2249
2250 (defvar *load-time-value-counter*)
2251
2252 (define-cold-fop (fop-funcall)
2253   (unless (= (read-byte-arg) 0)
2254     (error "You can't FOP-FUNCALL arbitrary stuff in cold load."))
2255   (let ((counter *load-time-value-counter*))
2256     (cold-push (cold-cons
2257                 (cold-intern :load-time-value)
2258                 (cold-cons
2259                  (pop-stack)
2260                  (cold-cons
2261                   (number-to-core counter)
2262                   *nil-descriptor*)))
2263                *current-reversed-cold-toplevels*)
2264     (setf *load-time-value-counter* (1+ counter))
2265     (make-descriptor 0 0 nil counter)))
2266
2267 (defun finalize-load-time-value-noise ()
2268   (cold-set (cold-intern '*!load-time-values*)
2269             (allocate-vector-object *dynamic*
2270                                     sb!vm:n-word-bits
2271                                     *load-time-value-counter*
2272                                     sb!vm:simple-vector-widetag)))
2273
2274 (define-cold-fop (fop-funcall-for-effect :pushp nil)
2275   (if (= (read-byte-arg) 0)
2276       (cold-push (pop-stack)
2277                  *current-reversed-cold-toplevels*)
2278       (error "You can't FOP-FUNCALL arbitrary stuff in cold load.")))
2279 \f
2280 ;;;; cold fops for fixing up circularities
2281
2282 (define-cold-fop (fop-rplaca :pushp nil)
2283   (let ((obj (svref *current-fop-table* (read-word-arg)))
2284         (idx (read-word-arg)))
2285     (write-memory (cold-nthcdr idx obj) (pop-stack))))
2286
2287 (define-cold-fop (fop-rplacd :pushp nil)
2288   (let ((obj (svref *current-fop-table* (read-word-arg)))
2289         (idx (read-word-arg)))
2290     (write-wordindexed (cold-nthcdr idx obj) 1 (pop-stack))))
2291
2292 (define-cold-fop (fop-svset :pushp nil)
2293   (let ((obj (svref *current-fop-table* (read-word-arg)))
2294         (idx (read-word-arg)))
2295     (write-wordindexed obj
2296                    (+ idx
2297                       (ecase (descriptor-lowtag obj)
2298                         (#.sb!vm:instance-pointer-lowtag 1)
2299                         (#.sb!vm:other-pointer-lowtag 2)))
2300                    (pop-stack))))
2301
2302 (define-cold-fop (fop-structset :pushp nil)
2303   (let ((obj (svref *current-fop-table* (read-word-arg)))
2304         (idx (read-word-arg)))
2305     (write-wordindexed obj (1+ idx) (pop-stack))))
2306
2307 ;;; In the original CMUCL code, this actually explicitly declared PUSHP
2308 ;;; to be T, even though that's what it defaults to in DEFINE-COLD-FOP.
2309 (define-cold-fop (fop-nthcdr)
2310   (cold-nthcdr (read-word-arg) (pop-stack)))
2311
2312 (defun cold-nthcdr (index obj)
2313   (dotimes (i index)
2314     (setq obj (read-wordindexed obj 1)))
2315   obj)
2316 \f
2317 ;;;; cold fops for loading code objects and functions
2318
2319 ;;; the names of things which have had COLD-FSET used on them already
2320 ;;; (used to make sure that we don't try to statically link a name to
2321 ;;; more than one definition)
2322 (defparameter *cold-fset-warm-names*
2323   ;; This can't be an EQL hash table because names can be conses, e.g.
2324   ;; (SETF CAR).
2325   (make-hash-table :test 'equal))
2326
2327 (define-cold-fop (fop-fset :pushp nil)
2328   (let* ((fn (pop-stack))
2329          (cold-name (pop-stack))
2330          (warm-name (warm-fun-name cold-name)))
2331     (if (gethash warm-name *cold-fset-warm-names*)
2332         (error "duplicate COLD-FSET for ~S" warm-name)
2333         (setf (gethash warm-name *cold-fset-warm-names*) t))
2334     (static-fset cold-name fn)))
2335
2336 (define-cold-fop (fop-fdefinition)
2337   (cold-fdefinition-object (pop-stack)))
2338
2339 (define-cold-fop (fop-sanctify-for-execution)
2340   (pop-stack))
2341
2342 ;;; Setting this variable shows what code looks like before any
2343 ;;; fixups (or function headers) are applied.
2344 #!+sb-show (defvar *show-pre-fixup-code-p* nil)
2345
2346 ;;; FIXME: The logic here should be converted into a function
2347 ;;; COLD-CODE-FOP-GUTS (NCONST CODE-SIZE) called by DEFINE-COLD-FOP
2348 ;;; FOP-CODE and DEFINE-COLD-FOP FOP-SMALL-CODE, so that
2349 ;;; variable-capture nastiness like (LET ((NCONST ,NCONST) ..) ..)
2350 ;;; doesn't keep me awake at night.
2351 (defmacro define-cold-code-fop (name nconst code-size)
2352   `(define-cold-fop (,name)
2353      (let* ((nconst ,nconst)
2354             (code-size ,code-size)
2355             (raw-header-n-words (+ sb!vm:code-trace-table-offset-slot nconst))
2356             (header-n-words
2357              ;; Note: we round the number of constants up to ensure
2358              ;; that the code vector will be properly aligned.
2359              (round-up raw-header-n-words 2))
2360             (des (allocate-cold-descriptor *dynamic*
2361                                            (+ (ash header-n-words
2362                                                    sb!vm:word-shift)
2363                                               code-size)
2364                                            sb!vm:other-pointer-lowtag)))
2365        (write-memory des
2366                      (make-other-immediate-descriptor
2367                       header-n-words sb!vm:code-header-widetag))
2368        (write-wordindexed des
2369                           sb!vm:code-code-size-slot
2370                           (make-fixnum-descriptor
2371                            (ash (+ code-size (1- (ash 1 sb!vm:word-shift)))
2372                                 (- sb!vm:word-shift))))
2373        (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2374        (write-wordindexed des sb!vm:code-debug-info-slot (pop-stack))
2375        (when (oddp raw-header-n-words)
2376          (write-wordindexed des
2377                             raw-header-n-words
2378                             (make-random-descriptor 0)))
2379        (do ((index (1- raw-header-n-words) (1- index)))
2380            ((< index sb!vm:code-trace-table-offset-slot))
2381          (write-wordindexed des index (pop-stack)))
2382        (let* ((start (+ (descriptor-byte-offset des)
2383                         (ash header-n-words sb!vm:word-shift)))
2384               (end (+ start code-size)))
2385          (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2386                                          *fasl-input-stream*
2387                                          :start start
2388                                          :end end)
2389          #!+sb-show
2390          (when *show-pre-fixup-code-p*
2391            (format *trace-output*
2392                    "~&/raw code from code-fop ~W ~W:~%"
2393                    nconst
2394                    code-size)
2395            (do ((i start (+ i sb!vm:n-word-bytes)))
2396                ((>= i end))
2397              (format *trace-output*
2398                      "/#X~8,'0x: #X~8,'0x~%"
2399                      (+ i (gspace-byte-address (descriptor-gspace des)))
2400                      (bvref-32 (descriptor-bytes des) i)))))
2401        des)))
2402
2403 (define-cold-code-fop fop-code (read-word-arg) (read-word-arg))
2404
2405 (define-cold-code-fop fop-small-code (read-byte-arg) (read-halfword-arg))
2406
2407 (clone-cold-fop (fop-alter-code :pushp nil)
2408                 (fop-byte-alter-code)
2409   (let ((slot (clone-arg))
2410         (value (pop-stack))
2411         (code (pop-stack)))
2412     (write-wordindexed code slot value)))
2413
2414 (define-cold-fop (fop-fun-entry)
2415   (let* ((type (pop-stack))
2416          (arglist (pop-stack))
2417          (name (pop-stack))
2418          (code-object (pop-stack))
2419          (offset (calc-offset code-object (read-word-arg)))
2420          (fn (descriptor-beyond code-object
2421                                 offset
2422                                 sb!vm:fun-pointer-lowtag))
2423          (next (read-wordindexed code-object sb!vm:code-entry-points-slot)))
2424     (unless (zerop (logand offset sb!vm:lowtag-mask))
2425       (error "unaligned function entry: ~S at #X~X" name offset))
2426     (write-wordindexed code-object sb!vm:code-entry-points-slot fn)
2427     (write-memory fn
2428                   (make-other-immediate-descriptor
2429                    (ash offset (- sb!vm:word-shift))
2430                    sb!vm:simple-fun-header-widetag))
2431     (write-wordindexed fn
2432                        sb!vm:simple-fun-self-slot
2433                        ;; KLUDGE: Wiring decisions like this in at
2434                        ;; this level ("if it's an x86") instead of a
2435                        ;; higher level of abstraction ("if it has such
2436                        ;; and such relocation peculiarities (which
2437                        ;; happen to be confined to the x86)") is bad.
2438                        ;; It would be nice if the code were instead
2439                        ;; conditional on some more descriptive
2440                        ;; feature, :STICKY-CODE or
2441                        ;; :LOAD-GC-INTERACTION or something.
2442                        ;;
2443                        ;; FIXME: The X86 definition of the function
2444                        ;; self slot breaks everything object.tex says
2445                        ;; about it. (As far as I can tell, the X86
2446                        ;; definition makes it a pointer to the actual
2447                        ;; code instead of a pointer back to the object
2448                        ;; itself.) Ask on the mailing list whether
2449                        ;; this is documented somewhere, and if not,
2450                        ;; try to reverse engineer some documentation.
2451                        #!-x86
2452                        ;; a pointer back to the function object, as
2453                        ;; described in CMU CL
2454                        ;; src/docs/internals/object.tex
2455                        fn
2456                        #!+x86
2457                        ;; KLUDGE: a pointer to the actual code of the
2458                        ;; object, as described nowhere that I can find
2459                        ;; -- WHN 19990907
2460                        (make-random-descriptor
2461                         (+ (descriptor-bits fn)
2462                            (- (ash sb!vm:simple-fun-code-offset
2463                                    sb!vm:word-shift)
2464                               ;; FIXME: We should mask out the type
2465                               ;; bits, not assume we know what they
2466                               ;; are and subtract them out this way.
2467                               sb!vm:fun-pointer-lowtag))))
2468     (write-wordindexed fn sb!vm:simple-fun-next-slot next)
2469     (write-wordindexed fn sb!vm:simple-fun-name-slot name)
2470     (write-wordindexed fn sb!vm:simple-fun-arglist-slot arglist)
2471     (write-wordindexed fn sb!vm:simple-fun-type-slot type)
2472     fn))
2473
2474 (define-cold-fop (fop-foreign-fixup)
2475   (let* ((kind (pop-stack))
2476          (code-object (pop-stack))
2477          (len (read-byte-arg))
2478          (sym (make-string len)))
2479     (read-string-as-bytes *fasl-input-stream* sym)
2480     (let ((offset (read-word-arg))
2481           (value (cold-foreign-symbol-address-as-integer sym)))
2482       (do-cold-fixup code-object offset value kind))
2483    code-object))
2484
2485 #!+linkage-table
2486 (define-cold-fop (fop-foreign-dataref-fixup)
2487   (let* ((kind (pop-stack))
2488          (code-object (pop-stack))
2489          (len (read-byte-arg))
2490          (sym (make-string len)))
2491     (read-string-as-bytes *fasl-input-stream* sym)
2492     (maphash (lambda (k v)
2493                (format *error-output* "~&~S = #X~8X~%" k v))
2494              *cold-foreign-symbol-table*)
2495     (error "shared foreign symbol in cold load: ~S (~S)" sym kind)))
2496
2497 (define-cold-fop (fop-assembler-code)
2498   (let* ((length (read-word-arg))
2499          (header-n-words
2500           ;; Note: we round the number of constants up to ensure that
2501           ;; the code vector will be properly aligned.
2502           (round-up sb!vm:code-constants-offset 2))
2503          (des (allocate-cold-descriptor *read-only*
2504                                         (+ (ash header-n-words
2505                                                 sb!vm:word-shift)
2506                                            length)
2507                                         sb!vm:other-pointer-lowtag)))
2508     (write-memory des
2509                   (make-other-immediate-descriptor
2510                    header-n-words sb!vm:code-header-widetag))
2511     (write-wordindexed des
2512                        sb!vm:code-code-size-slot
2513                        (make-fixnum-descriptor
2514                         (ash (+ length (1- (ash 1 sb!vm:word-shift)))
2515                              (- sb!vm:word-shift))))
2516     (write-wordindexed des sb!vm:code-entry-points-slot *nil-descriptor*)
2517     (write-wordindexed des sb!vm:code-debug-info-slot *nil-descriptor*)
2518
2519     (let* ((start (+ (descriptor-byte-offset des)
2520                      (ash header-n-words sb!vm:word-shift)))
2521            (end (+ start length)))
2522       (read-bigvec-as-sequence-or-die (descriptor-bytes des)
2523                                       *fasl-input-stream*
2524                                       :start start
2525                                       :end end))
2526     des))
2527
2528 (define-cold-fop (fop-assembler-routine)
2529   (let* ((routine (pop-stack))
2530          (des (pop-stack))
2531          (offset (calc-offset des (read-word-arg))))
2532     (record-cold-assembler-routine
2533      routine
2534      (+ (logandc2 (descriptor-bits des) sb!vm:lowtag-mask) offset))
2535     des))
2536
2537 (define-cold-fop (fop-assembler-fixup)
2538   (let* ((routine (pop-stack))
2539          (kind (pop-stack))
2540          (code-object (pop-stack))
2541          (offset (read-word-arg)))
2542     (record-cold-assembler-fixup routine code-object offset kind)
2543     code-object))
2544
2545 (define-cold-fop (fop-code-object-fixup)
2546   (let* ((kind (pop-stack))
2547          (code-object (pop-stack))
2548          (offset (read-word-arg))
2549          (value (descriptor-bits code-object)))
2550     (do-cold-fixup code-object offset value kind)
2551     code-object))
2552 \f
2553 ;;;; emitting C header file
2554
2555 (defun tailwise-equal (string tail)
2556   (and (>= (length string) (length tail))
2557        (string= string tail :start1 (- (length string) (length tail)))))
2558
2559 (defun write-boilerplate ()
2560   (format t "/*~%")
2561   (dolist (line
2562            '("This is a machine-generated file. Please do not edit it by hand."
2563              "(As of sbcl-0.8.14, it came from WRITE-CONFIG-H in genesis.lisp.)"
2564              ""
2565              "This file contains low-level information about the"
2566              "internals of a particular version and configuration"
2567              "of SBCL. It is used by the C compiler to create a runtime"
2568              "support environment, an executable program in the host"
2569              "operating system's native format, which can then be used to"
2570              "load and run 'core' files, which are basically programs"
2571              "in SBCL's own format."))
2572     (format t " * ~A~%" line))
2573   (format t " */~%"))
2574
2575 (defun write-config-h ()
2576   ;; propagating *SHEBANG-FEATURES* into C-level #define's
2577   (dolist (shebang-feature-name (sort (mapcar #'symbol-name
2578                                               sb-cold:*shebang-features*)
2579                                       #'string<))
2580     (format t
2581             "#define LISP_FEATURE_~A~%"
2582             (substitute #\_ #\- shebang-feature-name)))
2583   (terpri)
2584   ;; and miscellaneous constants
2585   (format t "#define SBCL_CORE_VERSION_INTEGER ~D~%" sbcl-core-version-integer)
2586   (format t
2587           "#define SBCL_VERSION_STRING ~S~%"
2588           (sb!xc:lisp-implementation-version))
2589   (format t "#define CORE_MAGIC 0x~X~%" core-magic)
2590   (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2591   (format t "#define LISPOBJ(x) ((lispobj)x)~2%")
2592   (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2593   (format t "#define LISPOBJ(thing) thing~2%")
2594   (format t "#endif /* LANGUAGE_ASSEMBLY */~2%")
2595   (terpri))
2596
2597 (defun write-constants-h ()
2598   ;; writing entire families of named constants 
2599   (let ((constants nil))
2600     (dolist (package-name '(;; Even in CMU CL, constants from VM
2601                             ;; were automatically propagated
2602                             ;; into the runtime.
2603                             "SB!VM"
2604                             ;; In SBCL, we also propagate various
2605                             ;; magic numbers related to file format,
2606                             ;; which live here instead of SB!VM.
2607                             "SB!FASL"))
2608       (do-external-symbols (symbol (find-package package-name))
2609         (when (constantp symbol)
2610           (let ((name (symbol-name symbol)))
2611             (labels (;; shared machinery
2612                      (record (string priority)
2613                        (push (list string
2614                                    priority
2615                                    (symbol-value symbol)
2616                                    (documentation symbol 'variable))
2617                              constants))
2618                      ;; machinery for old-style CMU CL Lisp-to-C
2619                      ;; arbitrary renaming, being phased out in favor of
2620                      ;; the newer systematic RECORD-WITH-TRANSLATED-NAME
2621                      ;; renaming
2622                      (record-with-munged-name (prefix string priority)
2623                        (record (concatenate
2624                                 'simple-string
2625                                 prefix
2626                                 (delete #\- (string-capitalize string)))
2627                                priority))
2628                      (maybe-record-with-munged-name (tail prefix priority)
2629                        (when (tailwise-equal name tail)
2630                          (record-with-munged-name prefix
2631                                                   (subseq name 0
2632                                                           (- (length name)
2633                                                              (length tail)))
2634                                                   priority)))
2635                      ;; machinery for new-style SBCL Lisp-to-C naming
2636                      (record-with-translated-name (priority)
2637                        (record (substitute #\_ #\- name)
2638                                priority))
2639                      (maybe-record-with-translated-name (suffixes priority)
2640                        (when (some (lambda (suffix)
2641                                      (tailwise-equal name suffix))
2642                                    suffixes)
2643                          (record-with-translated-name priority))))
2644   
2645               (maybe-record-with-translated-name '("-LOWTAG") 0)
2646               (maybe-record-with-translated-name '("-WIDETAG") 1)
2647               (maybe-record-with-munged-name "-FLAG" "flag_" 2)
2648               (maybe-record-with-munged-name "-TRAP" "trap_" 3)
2649               (maybe-record-with-munged-name "-SUBTYPE" "subtype_" 4)
2650               (maybe-record-with-munged-name "-SC-NUMBER" "sc_" 5)
2651               (maybe-record-with-translated-name '("-START" "-END" "-SIZE") 6)
2652               (maybe-record-with-translated-name '("-CORE-ENTRY-TYPE-CODE") 7)
2653               (maybe-record-with-translated-name '("-CORE-SPACE-ID") 8))))))
2654     ;; KLUDGE: these constants are sort of important, but there's no
2655     ;; pleasing way to inform the code above about them.  So we fake
2656     ;; it for now.  nikodemus on #lisp (2004-08-09) suggested simply
2657     ;; exporting every numeric constant from SB!VM; that would work,
2658     ;; but the C runtime would have to be altered to use Lisp-like names
2659     ;; rather than the munged names currently exported.  --njf, 2004-08-09
2660     (dolist (c '(sb!vm:n-word-bits sb!vm:n-word-bytes
2661                  sb!vm:n-lowtag-bits sb!vm:lowtag-mask
2662                  sb!vm:n-widetag-bits sb!vm:widetag-mask
2663                  sb!vm:n-fixnum-tag-bits sb!vm:fixnum-tag-mask))
2664       (push (list (substitute #\_ #\- (symbol-name c))
2665                   -1                    ; invent a new priority
2666                   (symbol-value c)
2667                   nil)
2668             constants))
2669
2670     (setf constants
2671           (sort constants
2672                 (lambda (const1 const2)
2673                   (if (= (second const1) (second const2))
2674                       (< (third const1) (third const2))
2675                       (< (second const1) (second const2))))))
2676     (let ((prev-priority (second (car constants))))
2677       (dolist (const constants)
2678         (destructuring-bind (name priority value doc) const
2679           (unless (= prev-priority priority)
2680             (terpri)
2681             (setf prev-priority priority))
2682           (format t "#define ~A " name)
2683           (format t 
2684                   ;; KLUDGE: As of sbcl-0.6.7.14, we're dumping two
2685                   ;; different kinds of values here, (1) small codes
2686                   ;; and (2) machine addresses. The small codes can be
2687                   ;; dumped as bare integer values. The large machine
2688                   ;; addresses might cause problems if they're large
2689                   ;; and represented as (signed) C integers, so we
2690                   ;; want to force them to be unsigned. We do that by
2691                   ;; wrapping them in the LISPOBJ macro. (We could do
2692                   ;; it with a bare "(unsigned)" cast, except that
2693                   ;; this header file is used not only in C files, but
2694                   ;; also in assembly files, which don't understand
2695                   ;; the cast syntax. The LISPOBJ macro goes away in
2696                   ;; assembly files, but that shouldn't matter because
2697                   ;; we don't do arithmetic on address constants in
2698                   ;; assembly files. See? It really is a kludge..) --
2699                   ;; WHN 2000-10-18
2700                   (let (;; cutoff for treatment as a small code
2701                         (cutoff (expt 2 16)))
2702                     (cond ((minusp value)
2703                            (error "stub: negative values unsupported"))
2704                           ((< value cutoff)
2705                            "~D")
2706                           (t
2707                            "LISPOBJ(~D)")))
2708                   value)
2709           (format t " /* 0x~X */~@[  /* ~A */~]~%" value doc))))
2710     (terpri))
2711
2712   ;; writing information about internal errors
2713   (let ((internal-errors sb!c:*backend-internal-errors*))
2714     (dotimes (i (length internal-errors))
2715       (let ((current-error (aref internal-errors i)))
2716         ;; FIXME: this UNLESS should go away (see also FIXME in
2717         ;; interr.lisp) -- APD, 2002-03-05
2718         (unless (eq nil (car current-error))
2719           (format t "#define ~A ~D~%"
2720                   (substitute #\_ #\- (symbol-name (car current-error)))
2721                   i)))))
2722   (terpri)
2723
2724   ;; FIXME: The SPARC has a PSEUDO-ATOMIC-TRAP that differs between
2725   ;; platforms. If we export this from the SB!VM package, it gets
2726   ;; written out as #define trap_PseudoAtomic, which is confusing as
2727   ;; the runtime treats trap_ as the prefix for illegal instruction
2728   ;; type things. We therefore don't export it, but instead do
2729   #!+sparc
2730   (when (boundp 'sb!vm::pseudo-atomic-trap)
2731     (format t
2732             "#define PSEUDO_ATOMIC_TRAP ~D /* 0x~:*~X */~%"
2733             sb!vm::pseudo-atomic-trap)
2734     (terpri))
2735   ;; possibly this is another candidate for a rename (to
2736   ;; pseudo-atomic-trap-number or pseudo-atomic-magic-constant
2737   ;; [possibly applicable to other platforms])
2738
2739   (dolist (symbol '(sb!vm::float-traps-byte
2740                     sb!vm::float-exceptions-byte
2741                     sb!vm::float-sticky-bits
2742                     sb!vm::float-rounding-mode))
2743     (format t "#define ~A_POSITION ~A /* ~:*0x~X */~%"
2744             (substitute #\_ #\- (symbol-name symbol))
2745             (sb!xc:byte-position (symbol-value symbol)))
2746     (format t "#define ~A_MASK 0x~X /* ~:*~A */~%"
2747             (substitute #\_ #\- (symbol-name symbol))
2748             (sb!xc:mask-field (symbol-value symbol) -1))))
2749
2750
2751
2752 (defun write-primitive-object (obj)  
2753   ;; writing primitive object layouts
2754     (format t "#ifndef LANGUAGE_ASSEMBLY~2%")
2755       (format t
2756               "struct ~A {~%"
2757               (substitute #\_ #\-
2758               (string-downcase (string (sb!vm:primitive-object-name obj)))))
2759       (when (sb!vm:primitive-object-widetag obj)
2760         (format t "    lispobj header;~%"))
2761       (dolist (slot (sb!vm:primitive-object-slots obj))
2762         (format t "    ~A ~A~@[[1]~];~%"
2763         (getf (sb!vm:slot-options slot) :c-type "lispobj")
2764         (substitute #\_ #\-
2765                     (string-downcase (string (sb!vm:slot-name slot))))
2766         (sb!vm:slot-rest-p slot)))
2767   (format t "};~2%")
2768     (format t "#else /* LANGUAGE_ASSEMBLY */~2%")
2769       (let ((name (sb!vm:primitive-object-name obj))
2770       (lowtag (eval (sb!vm:primitive-object-lowtag obj))))
2771         (when lowtag
2772         (dolist (slot (sb!vm:primitive-object-slots obj))
2773           (format t "#define ~A_~A_OFFSET ~D~%"
2774                   (substitute #\_ #\- (string name))
2775                   (substitute #\_ #\- (string (sb!vm:slot-name slot)))
2776                   (- (* (sb!vm:slot-offset slot) sb!vm:n-word-bytes) lowtag)))
2777       (terpri)))
2778     (format t "#endif /* LANGUAGE_ASSEMBLY */~2%"))
2779
2780 (defun write-static-symbols ()
2781   (dolist (symbol (cons nil sb!vm:*static-symbols*))
2782     ;; FIXME: It would be nice to use longer names than NIL and
2783     ;; (particularly) T in #define statements.
2784     (format t "#define ~A LISPOBJ(0x~X)~%"
2785             (substitute #\_ #\-
2786                         (remove-if (lambda (char)
2787                                      (member char '(#\% #\* #\. #\!)))
2788                                    (symbol-name symbol)))
2789             (if *static*                ; if we ran GENESIS
2790               ;; We actually ran GENESIS, use the real value.
2791               (descriptor-bits (cold-intern symbol))
2792               ;; We didn't run GENESIS, so guess at the address.
2793               (+ sb!vm:static-space-start
2794                  sb!vm:n-word-bytes
2795                  sb!vm:other-pointer-lowtag
2796                    (if symbol (sb!vm:static-symbol-offset symbol) 0))))))
2797
2798 \f
2799 ;;;; writing map file
2800
2801 ;;; Write a map file describing the cold load. Some of this
2802 ;;; information is subject to change due to relocating GC, but even so
2803 ;;; it can be very handy when attempting to troubleshoot the early
2804 ;;; stages of cold load.
2805 (defun write-map ()
2806   (let ((*print-pretty* nil)
2807         (*print-case* :upcase))
2808     (format t "assembler routines defined in core image:~2%")
2809     (dolist (routine (sort (copy-list *cold-assembler-routines*) #'<
2810                            :key #'cdr))
2811       (format t "#X~8,'0X: ~S~%" (cdr routine) (car routine)))
2812     (let ((funs nil)
2813           (undefs nil))
2814       (maphash (lambda (name fdefn)
2815                  (let ((fun (read-wordindexed fdefn
2816                                               sb!vm:fdefn-fun-slot)))
2817                    (if (= (descriptor-bits fun)
2818                           (descriptor-bits *nil-descriptor*))
2819                        (push name undefs)
2820                        (let ((addr (read-wordindexed
2821                                     fdefn sb!vm:fdefn-raw-addr-slot)))
2822                          (push (cons name (descriptor-bits addr))
2823                                funs)))))
2824                *cold-fdefn-objects*)
2825       (format t "~%~|~%initially defined functions:~2%")
2826       (setf funs (sort funs #'< :key #'cdr))
2827       (dolist (info funs)
2828         (format t "0x~8,'0X: ~S   #X~8,'0X~%" (cdr info) (car info)
2829                 (- (cdr info) #x17)))
2830       (format t
2831 "~%~|
2832 (a note about initially undefined function references: These functions
2833 are referred to by code which is installed by GENESIS, but they are not
2834 installed by GENESIS. This is not necessarily a problem; functions can
2835 be defined later, by cold init toplevel forms, or in files compiled and
2836 loaded at warm init, or elsewhere. As long as they are defined before
2837 they are called, everything should be OK. Things are also OK if the
2838 cross-compiler knew their inline definition and used that everywhere
2839 that they were called before the out-of-line definition is installed,
2840 as is fairly common for structure accessors.)
2841 initially undefined function references:~2%")
2842
2843       (setf undefs (sort undefs #'string< :key #'fun-name-block-name))
2844       (dolist (name undefs)
2845         (format t "~S~%" name)))
2846
2847     (format t "~%~|~%layout names:~2%")
2848     (collect ((stuff))
2849       (maphash (lambda (name gorp)
2850                  (declare (ignore name))
2851                  (stuff (cons (descriptor-bits (car gorp))
2852                               (cdr gorp))))
2853                *cold-layouts*)
2854       (dolist (x (sort (stuff) #'< :key #'car))
2855         (apply #'format t "~8,'0X: ~S[~D]~%~10T~S~%" x))))
2856
2857   (values))
2858 \f
2859 ;;;; writing core file
2860
2861 (defvar *core-file*)
2862 (defvar *data-page*)
2863
2864 ;;; magic numbers to identify entries in a core file
2865 ;;;
2866 ;;; (In case you were wondering: No, AFAIK there's no special magic about
2867 ;;; these which requires them to be in the 38xx range. They're just
2868 ;;; arbitrary words, tested not for being in a particular range but just
2869 ;;; for equality. However, if you ever need to look at a .core file and
2870 ;;; figure out what's going on, it's slightly convenient that they're
2871 ;;; all in an easily recognizable range, and displacing the range away from
2872 ;;; zero seems likely to reduce the chance that random garbage will be
2873 ;;; misinterpreted as a .core file.)
2874 (defconstant version-core-entry-type-code 3860)
2875 (defconstant build-id-core-entry-type-code 3899)
2876 (defconstant new-directory-core-entry-type-code 3861)
2877 (defconstant initial-fun-core-entry-type-code 3863)
2878 (defconstant end-core-entry-type-code 3840)
2879
2880 (declaim (ftype (function (sb!vm:word) sb!vm:word) write-word))
2881 (defun write-word (num)
2882   (ecase sb!c:*backend-byte-order*
2883     (:little-endian
2884      (dotimes (i sb!vm:n-word-bytes)
2885        (write-byte (ldb (byte 8 (* i 8)) num) *core-file*)))
2886     (:big-endian
2887      (dotimes (i sb!vm:n-word-bytes)
2888        (write-byte (ldb (byte 8 (* (- (1- sb!vm:n-word-bytes) i) 8)) num) 
2889                    *core-file*))))
2890   num)
2891
2892 (defun advance-to-page ()
2893   (force-output *core-file*)
2894   (file-position *core-file*
2895                  (round-up (file-position *core-file*)
2896                            sb!c:*backend-page-size*)))
2897
2898 (defun output-gspace (gspace)
2899   (force-output *core-file*)
2900   (let* ((posn (file-position *core-file*))
2901          (bytes (* (gspace-free-word-index gspace) sb!vm:n-word-bytes))
2902          (pages (ceiling bytes sb!c:*backend-page-size*))
2903          (total-bytes (* pages sb!c:*backend-page-size*)))
2904
2905     (file-position *core-file*
2906                    (* sb!c:*backend-page-size* (1+ *data-page*)))
2907     (format t
2908             "writing ~S byte~:P [~S page~:P] from ~S~%"
2909             total-bytes
2910             pages
2911             gspace)
2912     (force-output)
2913
2914     ;; Note: It is assumed that the GSPACE allocation routines always
2915     ;; allocate whole pages (of size *target-page-size*) and that any
2916     ;; empty gspace between the free pointer and the end of page will
2917     ;; be zero-filled. This will always be true under Mach on machines
2918     ;; where the page size is equal. (RT is 4K, PMAX is 4K, Sun 3 is
2919     ;; 8K).
2920     (write-bigvec-as-sequence (gspace-bytes gspace)
2921                               *core-file*
2922                               :end total-bytes)
2923     (force-output *core-file*)
2924     (file-position *core-file* posn)
2925
2926     ;; Write part of a (new) directory entry which looks like this:
2927     ;;   GSPACE IDENTIFIER
2928     ;;   WORD COUNT
2929     ;;   DATA PAGE
2930     ;;   ADDRESS
2931     ;;   PAGE COUNT
2932     (write-word (gspace-identifier gspace))
2933     (write-word (gspace-free-word-index gspace))
2934     (write-word *data-page*)
2935     (multiple-value-bind (floor rem)
2936         (floor (gspace-byte-address gspace) sb!c:*backend-page-size*)
2937       (aver (zerop rem))
2938       (write-word floor))
2939     (write-word pages)
2940
2941     (incf *data-page* pages)))
2942
2943 ;;; Create a core file created from the cold loaded image. (This is
2944 ;;; the "initial core file" because core files could be created later
2945 ;;; by executing SAVE-LISP in a running system, perhaps after we've
2946 ;;; added some functionality to the system.)
2947 (declaim (ftype (function (string)) write-initial-core-file))
2948 (defun write-initial-core-file (filename)
2949
2950   (let ((filenamestring (namestring filename))
2951         (*data-page* 0))
2952
2953     (format t
2954             "[building initial core file in ~S: ~%"
2955             filenamestring)
2956     (force-output)
2957
2958     (with-open-file (*core-file* filenamestring
2959                                  :direction :output
2960                                  :element-type '(unsigned-byte 8)
2961                                  :if-exists :rename-and-delete)
2962
2963       ;; Write the magic number.
2964       (write-word core-magic)
2965
2966       ;; Write the Version entry.
2967       (write-word version-core-entry-type-code)
2968       (write-word 3)
2969       (write-word sbcl-core-version-integer)
2970
2971       ;; Write the build ID.
2972       (write-word build-id-core-entry-type-code)
2973       (let ((build-id (with-open-file (s "output/build-id.tmp"
2974                                          :direction :input)
2975                         (read s))))
2976         (declare (type simple-string build-id))
2977         (/show build-id (length build-id))
2978         ;; Write length of build ID record: BUILD-ID-CORE-ENTRY-TYPE-CODE
2979         ;; word, this length word, and one word for each char of BUILD-ID.
2980         (write-word (+ 2 (length build-id)))
2981         (dovector (char build-id)
2982           ;; (We write each character as a word in order to avoid
2983           ;; having to think about word alignment issues in the
2984           ;; sbcl-0.7.8 version of coreparse.c.)
2985           (write-word (sb!xc:char-code char))))
2986
2987       ;; Write the New Directory entry header.
2988       (write-word new-directory-core-entry-type-code)
2989       (write-word 17) ; length = (5 words/space) * 3 spaces + 2 for header.
2990
2991       (output-gspace *read-only*)
2992       (output-gspace *static*)
2993       (output-gspace *dynamic*)
2994
2995       ;; Write the initial function.
2996       (write-word initial-fun-core-entry-type-code)
2997       (write-word 3)
2998       (let* ((cold-name (cold-intern '!cold-init))
2999              (cold-fdefn (cold-fdefinition-object cold-name))
3000              (initial-fun (read-wordindexed cold-fdefn
3001                                             sb!vm:fdefn-fun-slot)))
3002         (format t
3003                 "~&/(DESCRIPTOR-BITS INITIAL-FUN)=#X~X~%"
3004                 (descriptor-bits initial-fun))
3005         (write-word (descriptor-bits initial-fun)))
3006
3007       ;; Write the End entry.
3008       (write-word end-core-entry-type-code)
3009       (write-word 2)))
3010
3011   (format t "done]~%")
3012   (force-output)
3013   (/show "leaving WRITE-INITIAL-CORE-FILE")
3014   (values))
3015 \f
3016 ;;;; the actual GENESIS function
3017
3018 ;;; Read the FASL files in OBJECT-FILE-NAMES and produce a Lisp core,
3019 ;;; and/or information about a Lisp core, therefrom.
3020 ;;;
3021 ;;; input file arguments:
3022 ;;;   SYMBOL-TABLE-FILE-NAME names a UNIX-style .nm file *with* *any*
3023 ;;;     *tab* *characters* *converted* *to* *spaces*. (We push
3024 ;;;     responsibility for removing tabs out to the caller it's
3025 ;;;     trivial to remove them using UNIX command line tools like
3026 ;;;     sed, whereas it's a headache to do it portably in Lisp because
3027 ;;;     #\TAB is not a STANDARD-CHAR.) If this file is not supplied,
3028 ;;;     a core file cannot be built (but a C header file can be).
3029 ;;;
3030 ;;; output files arguments (any of which may be NIL to suppress output):
3031 ;;;   CORE-FILE-NAME gets a Lisp core.
3032 ;;;   C-HEADER-FILE-NAME gets a C header file, traditionally called
3033 ;;;     internals.h, which is used by the C compiler when constructing
3034 ;;;     the executable which will load the core.
3035 ;;;   MAP-FILE-NAME gets (?) a map file. (dunno about this -- WHN 19990815)
3036 ;;;
3037 ;;; FIXME: GENESIS doesn't belong in SB!VM. Perhaps in %KERNEL for now,
3038 ;;; perhaps eventually in SB-LD or SB-BOOT.
3039 (defun sb!vm:genesis (&key
3040                       object-file-names
3041                       symbol-table-file-name
3042                       core-file-name
3043                       map-file-name
3044                       c-header-dir-name)
3045
3046   (format t
3047           "~&beginning GENESIS, ~A~%"
3048           (if core-file-name
3049             ;; Note: This output summarizing what we're doing is
3050             ;; somewhat telegraphic in style, not meant to imply that
3051             ;; we're not e.g. also creating a header file when we
3052             ;; create a core.
3053             (format nil "creating core ~S" core-file-name)
3054             (format nil "creating headers in ~S" c-header-dir-name)))
3055   
3056   (let ((*cold-foreign-symbol-table* (make-hash-table :test 'equal)))
3057
3058     (when core-file-name
3059       (if symbol-table-file-name
3060           (load-cold-foreign-symbol-table symbol-table-file-name)
3061           (error "can't output a core file without symbol table file input")))
3062
3063     ;; Now that we've successfully read our only input file (by
3064     ;; loading the symbol table, if any), it's a good time to ensure
3065     ;; that there'll be someplace for our output files to go when
3066     ;; we're done.
3067     (flet ((frob (filename)
3068              (when filename
3069                (ensure-directories-exist filename :verbose t))))
3070       (frob core-file-name)
3071       (frob map-file-name))
3072
3073     ;; (This shouldn't matter in normal use, since GENESIS normally
3074     ;; only runs once in any given Lisp image, but it could reduce
3075     ;; confusion if we ever experiment with running, tweaking, and
3076     ;; rerunning genesis interactively.)
3077     (do-all-symbols (sym)
3078       (remprop sym 'cold-intern-info))
3079
3080     (let* ((*foreign-symbol-placeholder-value* (if core-file-name nil 0))
3081            (*load-time-value-counter* 0)
3082            (*cold-fdefn-objects* (make-hash-table :test 'equal))
3083            (*cold-symbols* (make-hash-table :test 'equal))
3084            (*cold-package-symbols* nil)
3085            (*read-only* (make-gspace :read-only
3086                                      read-only-core-space-id
3087                                      sb!vm:read-only-space-start))
3088            (*static*    (make-gspace :static
3089                                      static-core-space-id
3090                                      sb!vm:static-space-start))
3091            (*dynamic*   (make-gspace :dynamic
3092                                      dynamic-core-space-id
3093                                      #!+gencgc sb!vm:dynamic-space-start
3094                                      #!-gencgc sb!vm:dynamic-0-space-start))
3095            (*nil-descriptor* (make-nil-descriptor))
3096            (*current-reversed-cold-toplevels* *nil-descriptor*)
3097            (*unbound-marker* (make-other-immediate-descriptor
3098                               0
3099                               sb!vm:unbound-marker-widetag))
3100            *cold-assembler-fixups*
3101            *cold-assembler-routines*
3102            #!+x86 *load-time-code-fixups*)
3103
3104       ;; Prepare for cold load.
3105       (initialize-non-nil-symbols)
3106       (initialize-layouts)
3107       (initialize-static-fns)
3108
3109       ;; Initialize the *COLD-SYMBOLS* system with the information
3110       ;; from package-data-list.lisp-expr and
3111       ;; common-lisp-exports.lisp-expr.
3112       ;;
3113       ;; Why do things this way? Historically, the *COLD-SYMBOLS*
3114       ;; machinery was designed and implemented in CMU CL long before
3115       ;; I (WHN) ever heard of CMU CL. It dumped symbols and packages
3116       ;; iff they were used in the cold image. When I added the
3117       ;; package-data-list.lisp-expr mechanism, the idea was to
3118       ;; centralize all information about packages and exports. Thus,
3119       ;; it was the natural place for information even about packages
3120       ;; (such as SB!PCL and SB!WALKER) which aren't used much until
3121       ;; after cold load. This didn't quite match the CMU CL approach
3122       ;; of filling *COLD-SYMBOLS* with symbols which appear in the
3123       ;; cold image and then dumping only those symbols. By explicitly
3124       ;; putting all the symbols from package-data-list.lisp-expr and
3125       ;; from common-lisp-exports.lisp-expr into *COLD-SYMBOLS* here,
3126       ;; we feed our centralized symbol information into the old CMU
3127       ;; CL code without having to change the old CMU CL code too
3128       ;; much. (And the old CMU CL code is still useful for making
3129       ;; sure that the appropriate keywords and internal symbols end
3130       ;; up interned in the target Lisp, which is good, e.g. in order
3131       ;; to make &KEY arguments work right and in order to make
3132       ;; BACKTRACEs into target Lisp system code be legible.)
3133       (dolist (exported-name
3134                (sb-cold:read-from-file "common-lisp-exports.lisp-expr"))
3135         (cold-intern (intern exported-name *cl-package*)))
3136       (dolist (pd (sb-cold:read-from-file "package-data-list.lisp-expr"))
3137         (declare (type sb-cold:package-data pd))
3138         (let ((package (find-package (sb-cold:package-data-name pd))))
3139           (labels (;; Call FN on every node of the TREE.
3140                    (mapc-on-tree (fn tree)
3141                                  (declare (type function fn))
3142                                  (typecase tree
3143                                    (cons (mapc-on-tree fn (car tree))
3144                                          (mapc-on-tree fn (cdr tree)))
3145                                    (t (funcall fn tree)
3146                                       (values))))
3147                    ;; Make sure that information about the association
3148                    ;; between PACKAGE and the symbol named NAME gets
3149                    ;; recorded in the cold-intern system or (as a
3150                    ;; convenience when dealing with the tree structure
3151                    ;; allowed in the PACKAGE-DATA-EXPORTS slot) do
3152                    ;; nothing if NAME is NIL.
3153                    (chill (name)
3154                      (when name
3155                        (cold-intern (intern name package) package))))
3156             (mapc-on-tree #'chill (sb-cold:package-data-export pd))
3157             (mapc #'chill (sb-cold:package-data-reexport pd))
3158             (dolist (sublist (sb-cold:package-data-import-from pd))
3159               (destructuring-bind (package-name &rest symbol-names) sublist
3160                 (declare (ignore package-name))
3161                 (mapc #'chill symbol-names))))))
3162
3163       ;; Cold load.
3164       (dolist (file-name object-file-names)
3165         (write-line (namestring file-name))
3166         (cold-load file-name))
3167
3168       ;; Tidy up loose ends left by cold loading. ("Postpare from cold load?")
3169       (resolve-assembler-fixups)
3170       #!+x86 (output-load-time-code-fixups)
3171       (foreign-symbols-to-core)
3172       (finish-symbols)
3173       (/show "back from FINISH-SYMBOLS")
3174       (finalize-load-time-value-noise)
3175
3176       ;; Tell the target Lisp how much stuff we've allocated.
3177       (cold-set 'sb!vm:*read-only-space-free-pointer*
3178                 (allocate-cold-descriptor *read-only*
3179                                           0
3180                                           sb!vm:even-fixnum-lowtag))
3181       (cold-set 'sb!vm:*static-space-free-pointer*
3182                 (allocate-cold-descriptor *static*
3183                                           0
3184                                           sb!vm:even-fixnum-lowtag))
3185       (cold-set 'sb!vm:*initial-dynamic-space-free-pointer*
3186                 (allocate-cold-descriptor *dynamic*
3187                                           0
3188                                           sb!vm:even-fixnum-lowtag))
3189       (/show "done setting free pointers")
3190
3191       ;; Write results to files.
3192       ;;
3193       ;; FIXME: I dislike this approach of redefining
3194       ;; *STANDARD-OUTPUT* instead of putting the new stream in a
3195       ;; lexical variable, and it's annoying to have WRITE-MAP (to
3196       ;; *STANDARD-OUTPUT*) not be parallel to WRITE-INITIAL-CORE-FILE
3197       ;; (to a stream explicitly passed as an argument).
3198       (macrolet ((out-to (name &body body)
3199                    `(let ((fn (format nil "~A/~A.h" c-header-dir-name ,name)))
3200                      (ensure-directories-exist fn)
3201                      (with-open-file (*standard-output* fn  
3202                                       :if-exists :supersede :direction :output)
3203                        (write-boilerplate)
3204                        (let ((n (substitute #\_ #\- (string-upcase ,name))))
3205                          (format 
3206                           t
3207                           "#ifndef SBCL_GENESIS_~A~%#define SBCL_GENESIS_~A 1~%"
3208                           n n))
3209                        ,@body
3210                        (format t
3211                         "#endif /* SBCL_GENESIS_~A */~%"
3212                         (string-upcase ,name))))))
3213       (when map-file-name
3214         (with-open-file (*standard-output* map-file-name
3215                                            :direction :output
3216                                            :if-exists :supersede)
3217           (write-map)))
3218         (out-to "config" (write-config-h))
3219         (out-to "constants" (write-constants-h))
3220         (let ((structs (sort (copy-list sb!vm:*primitive-objects*) #'string<
3221                              :key (lambda (obj)
3222                                     (symbol-name
3223                                      (sb!vm:primitive-object-name obj))))))
3224           (dolist (obj structs)
3225             (out-to
3226              (string-downcase (string (sb!vm:primitive-object-name obj)))
3227              (write-primitive-object obj)))
3228           (out-to "primitive-objects"
3229                   (dolist (obj structs)
3230                     (format t "~&#include \"~A.h\"~%"
3231                             (string-downcase 
3232                              (string (sb!vm:primitive-object-name obj)))))))
3233         (out-to "static-symbols" (write-static-symbols))
3234         
3235       (when core-file-name
3236           (write-initial-core-file core-file-name))))))