6b76b1807e2ed6d3b5273e48c87b833074671b86
[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
34       (note-fun entry-info res object))))
35
36 ;;; Dump a component to core. We pass in the assembler fixups, code
37 ;;; vector and node info.
38 (defun make-core-component (component segment length trace-table fixup-notes object)
39   (declare (type component component)
40            (type sb!assem:segment segment)
41            (type index length)
42            (list trace-table fixup-notes)
43            (type core-object object))
44   (without-gcing
45     (let* ((2comp (component-info component))
46            (constants (ir2-component-constants 2comp))
47            (trace-table (pack-trace-table trace-table))
48            (trace-table-len (length trace-table))
49            (trace-table-bits (* trace-table-len tt-bits-per-entry))
50            (total-length (+ length
51                             (ceiling trace-table-bits sb!vm:n-byte-bits)))
52            (box-num (- (length constants) sb!vm:code-trace-table-offset-slot))
53            (code-obj
54             (%primitive allocate-code-object box-num total-length))
55            (fill-ptr (code-instructions code-obj)))
56       (declare (type index box-num total-length))
57
58       (sb!assem:on-segment-contents-vectorly
59        segment
60        (lambda (v)
61          (declare (type (simple-array sb!assem:assembly-unit 1) v))
62          (copy-byte-vector-to-system-area v fill-ptr)
63          (setf fill-ptr (sap+ fill-ptr (length v)))))
64
65       (do-core-fixups code-obj fixup-notes)
66
67       (dolist (entry (ir2-component-entries 2comp))
68         (make-fun-entry entry code-obj object))
69
70       (sb!vm:sanctify-for-execution code-obj)
71
72       (let ((info (debug-info-for-component component)))
73         (push info (core-object-debug-info object))
74         (setf (%code-debug-info code-obj) info))
75
76       (setf (code-header-ref code-obj sb!vm:code-trace-table-offset-slot)
77             length)
78       ;; KLUDGE: the "old" COPY-TO-SYSTEM-AREA automagically worked if
79       ;; somebody changed the number of bytes in a trace table entry.
80       ;; This version is a bit more fragile; if only there were some way
81       ;; to insulate ourselves against changes like that...
82       ;;
83       ;; Then again, PACK-TRACE-TABLE in src/compiler/trace-table.lisp
84       ;; doesn't appear to do anything interesting, returning a 0-length
85       ;; array.  So it seemingly doesn't matter what we do here.  Is this
86       ;; stale code?
87       ;;   --njf, 2005-03-23
88       (copy-ub16-to-system-area trace-table 0 fill-ptr 0 trace-table-len)
89
90       (do ((index sb!vm:code-constants-offset (1+ index)))
91           ((>= index (length constants)))
92         (let ((const (aref constants index)))
93           (etypecase const
94             (null)
95             (constant
96              (setf (code-header-ref code-obj index)
97                    (constant-value const)))
98             (list
99              (ecase (car const)
100                (:entry
101                 (reference-core-fun code-obj index (cdr const) object))
102                (:fdefinition
103                 (setf (code-header-ref code-obj index)
104                       (fdefinition-object (cdr const) t))))))))))
105   (values))