1.0.0.18:
[sbcl.git] / src / compiler / generic / target-core.lisp
1 ;;;; target-only code that knows how to load compiled code directly
2 ;;;; into core
3 ;;;;
4 ;;;; FIXME: The filename here is confusing because "core" here means
5 ;;;; "main memory", while elsewhere in the system it connotes a
6 ;;;; ".core" file dumping the contents of main memory.
7
8 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; more information.
10 ;;;;
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
16
17 (in-package "SB!C")
18
19 ;;; Make a function entry, filling in slots from the ENTRY-INFO.
20 (defun make-fun-entry (entry-info code-obj object)
21   (declare (type entry-info entry-info) (type core-object object))
22   (let ((offset (label-position (entry-info-offset entry-info))))
23     (declare (type index offset))
24     (unless (zerop (logand offset sb!vm:lowtag-mask))
25       (error "Unaligned function object, offset = #X~X." offset))
26     (let ((res (%primitive compute-fun code-obj offset)))
27       (setf (%simple-fun-self res) res)
28       (setf (%simple-fun-next res) (%code-entry-points code-obj))
29       (setf (%code-entry-points code-obj) res)
30       (setf (%simple-fun-name res) (entry-info-name entry-info))
31       (setf (%simple-fun-arglist res) (entry-info-arguments entry-info))
32       (setf (%simple-fun-type res) (entry-info-type entry-info))
33       (setf (%simple-fun-xrefs res) (entry-info-xref entry-info))
34
35       (note-fun entry-info res object))))
36
37 ;;; Dump a component to core. We pass in the assembler fixups, code
38 ;;; vector and node info.
39 (defun make-core-component (component segment length trace-table fixup-notes object)
40   (declare (type component component)
41            (type sb!assem:segment segment)
42            (type index length)
43            (list trace-table fixup-notes)
44            (type core-object object))
45   (without-gcing
46     (let* ((2comp (component-info component))
47            (constants (ir2-component-constants 2comp))
48            (trace-table (pack-trace-table trace-table))
49            (trace-table-len (length trace-table))
50            (trace-table-bits (* trace-table-len tt-bits-per-entry))
51            (total-length (+ length
52                             (ceiling trace-table-bits sb!vm:n-byte-bits)))
53            (box-num (- (length constants) sb!vm:code-trace-table-offset-slot))
54            (code-obj
55             (%primitive allocate-code-object box-num total-length))
56            (fill-ptr (code-instructions code-obj)))
57       (declare (type index box-num total-length))
58
59       (sb!assem:on-segment-contents-vectorly
60        segment
61        (lambda (v)
62          (declare (type (simple-array sb!assem:assembly-unit 1) v))
63          (copy-byte-vector-to-system-area v fill-ptr)
64          (setf fill-ptr (sap+ fill-ptr (length v)))))
65
66       (do-core-fixups code-obj fixup-notes)
67
68       (dolist (entry (ir2-component-entries 2comp))
69         (make-fun-entry entry code-obj object))
70
71       (sb!vm:sanctify-for-execution code-obj)
72
73       (let ((info (debug-info-for-component component)))
74         (push info (core-object-debug-info object))
75         (setf (%code-debug-info code-obj) info))
76
77       (setf (code-header-ref code-obj sb!vm:code-trace-table-offset-slot)
78             length)
79       ;; KLUDGE: the "old" COPY-TO-SYSTEM-AREA automagically worked if
80       ;; somebody changed the number of bytes in a trace table entry.
81       ;; This version is a bit more fragile; if only there were some way
82       ;; to insulate ourselves against changes like that...
83       ;;
84       ;; Then again, PACK-TRACE-TABLE in src/compiler/trace-table.lisp
85       ;; doesn't appear to do anything interesting, returning a 0-length
86       ;; array.  So it seemingly doesn't matter what we do here.  Is this
87       ;; stale code?
88       ;;   --njf, 2005-03-23
89       (copy-ub16-to-system-area trace-table 0 fill-ptr 0 trace-table-len)
90
91       (do ((index sb!vm:code-constants-offset (1+ index)))
92           ((>= index (length constants)))
93         (let ((const (aref constants index)))
94           (etypecase const
95             (null)
96             (constant
97              (setf (code-header-ref code-obj index)
98                    (constant-value const)))
99             (list
100              (ecase (car const)
101                (:entry
102                 (reference-core-fun code-obj index (cdr const) object))
103                (:fdefinition
104                 (setf (code-header-ref code-obj index)
105                       (fdefinition-object (cdr const) t))))))))))
106   (values))