da4ac3c335844bc275059dfd0f592e0208f66e7f
[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-function-entry (entry code-obj object)
21   (declare (type entry-info entry) (type core-object object))
22   (let ((offset (label-position (entry-info-offset entry))))
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-function code-obj offset)))
27       (setf (%function-self res) res)
28       (setf (%function-next res) (%code-entry-points code-obj))
29       (setf (%code-entry-points code-obj) res)
30       (setf (%function-name res) (entry-info-name entry))
31       (setf (%function-arglist res) (entry-info-arguments entry))
32       (setf (%function-type res) (entry-info-type entry))
33
34       (note-function entry res object))))
35
36 ;;; Dump a component to core. We pass in the assembler fixups, code vector
37 ;;; and node info.
38 (defun make-core-component (component segment length trace-table fixups object)
39   (declare (type component component)
40            (type sb!assem:segment segment)
41            (type index length)
42            (list trace-table fixups)
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 (ceiling trace-table-bits sb!vm:byte-bits)))
51            (box-num (- (length constants) sb!vm:code-trace-table-offset-slot))
52            (code-obj
53             ;; FIXME: In CMU CL the X86 behavior here depended on
54             ;; *ENABLE-DYNAMIC-SPACE-CODE*, but in SBCL we always use
55             ;; dynamic space code, so we could make
56             ;; ALLOCATE-DYNAMIC-CODE-OBJECT more parallel with
57             ;; ALLOCATE-CODE-OBJECT and remove this confusing
58             ;; read-macro conditionalization.
59             #!+x86
60             (%primitive allocate-dynamic-code-object box-num total-length)
61             #!-x86
62             (%primitive allocate-code-object box-num total-length))
63            (fill-ptr (code-instructions code-obj)))
64       (declare (type index box-num total-length))
65
66       (sb!assem:on-segment-contents-vectorly
67        segment
68        (lambda (v)
69          (declare (type (simple-array sb!assem:assembly-unit 1) v))
70          (copy-byte-vector-to-system-area v fill-ptr)
71          (setf fill-ptr (sap+ fill-ptr (length v)))))
72
73       (do-core-fixups code-obj fixups)
74
75       (dolist (entry (ir2-component-entries 2comp))
76         (make-function-entry entry code-obj object))
77
78       (sb!vm:sanctify-for-execution code-obj)
79
80       (let ((info (debug-info-for-component component)))
81         (push info (core-object-debug-info object))
82         (setf (%code-debug-info code-obj) info))
83
84       (setf (code-header-ref code-obj sb!vm:code-trace-table-offset-slot) length)
85       (copy-to-system-area trace-table
86                            (* sb!vm:vector-data-offset sb!vm:word-bits)
87                            fill-ptr
88                            0
89                            trace-table-bits)
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-function code-obj index
103                                          (cdr const) object))
104                (:fdefinition
105                 (setf (code-header-ref code-obj index)
106                       (fdefinition-object (cdr const) t))))))))))
107   (values))
108
109 ;;; FIXME: byte compiler to go away completely
110 #|
111 (defun make-core-byte-component (segment length constants xeps object)
112   (declare (type sb!assem:segment segment)
113            (type index length)
114            (type vector constants)
115            (type list xeps)
116            (type core-object object))
117   (without-gcing
118     (let* ((num-constants (length constants))
119            ;; KLUDGE: On the X86, using ALLOCATE-CODE-OBJECT is
120            ;; supposed to make the result non-relocatable, which is
121            ;; probably not what we want. Could this be made into
122            ;; ALLOCATE-DYNAMIC-CODE-OBJECT? Is there some other fix?
123            ;; Am I just confused? -- WHN 19990916
124            (code-obj (%primitive allocate-code-object
125                                  (the index (1+ num-constants))
126                                  length))
127            (fill-ptr (code-instructions code-obj)))
128       (declare (type index length)
129                (type system-area-pointer fill-ptr))
130       (sb!assem:on-segment-contents-vectorly
131        segment
132        (lambda (v)
133          (declare (type (simple-array sb!assem:assembly-unit 1) v))
134          (copy-byte-vector-to-system-area v fill-ptr)
135          (setf fill-ptr (sap+ fill-ptr (length v)))))
136
137       (setf (code-header-ref code-obj sb!vm:code-trace-table-offset-slot)
138             nil)
139       (dolist (noise xeps)
140         (let ((xep (cdr noise)))
141           (setf (byte-function-component xep) code-obj)
142           (initialize-byte-compiled-function xep)
143           (note-function (lambda-info (car noise)) xep object)))
144
145       (dotimes (index num-constants)
146         (let ((const (aref constants index))
147               (code-obj-index (+ index sb!vm:code-constants-offset)))
148           (etypecase const
149             (null)
150             (constant
151              (setf (code-header-ref code-obj code-obj-index)
152                    (constant-value const)))
153             (list
154              (ecase (car const)
155                (:entry
156                 (reference-core-function code-obj code-obj-index (cdr const)
157                                          object))
158                (:fdefinition
159                 (setf (code-header-ref code-obj code-obj-index)
160                       (sb!impl::fdefinition-object (cdr const) t)))
161                (:type-predicate
162                 (let ((*unparse-function-type-simplify* t))
163                   (setf (code-header-ref code-obj code-obj-index)
164                         (load-type-predicate (type-specifier (cdr const))))))
165                (:xep
166                 (let ((xep (cdr (assoc (cdr const) xeps :test #'eq))))
167                   (aver xep)
168                   (setf (code-header-ref code-obj code-obj-index) xep))))))))))
169
170   (values))
171 |#