fddd4070987f4d0a94993be3e6ee3912c705afa1
[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 (declaim (ftype (function (fixnum fixnum) (values code-component &optional))
20                 allocate-code-object))
21 (defun allocate-code-object (boxed unboxed)
22   #!+gencgc
23   (without-gcing
24     (%make-lisp-obj
25      (alien-funcall (extern-alien "alloc_code_object" (function unsigned-long unsigned unsigned))
26                     boxed unboxed)))
27   #!-gencgc
28   (%primitive allocate-code-object boxed unboxed))
29
30 ;;; Make a function entry, filling in slots from the ENTRY-INFO.
31 (defun make-fun-entry (entry-info code-obj object)
32   (declare (type entry-info entry-info) (type core-object object))
33   (let ((offset (label-position (entry-info-offset entry-info))))
34     (declare (type index offset))
35     (unless (zerop (logand offset sb!vm:lowtag-mask))
36       (error "Unaligned function object, offset = #X~X." offset))
37     (let ((res (%primitive compute-fun code-obj offset)))
38       (setf (%simple-fun-self res) res)
39       (setf (%simple-fun-next res) (%code-entry-points code-obj))
40       (setf (%code-entry-points code-obj) res)
41       (setf (%simple-fun-name res) (entry-info-name entry-info))
42       (setf (%simple-fun-arglist res) (entry-info-arguments entry-info))
43       (setf (%simple-fun-type res) (entry-info-type entry-info))
44       (setf (%simple-fun-info res) (entry-info-info entry-info))
45
46       (note-fun entry-info res object))))
47
48 ;;; Dump a component to core. We pass in the assembler fixups, code
49 ;;; vector and node info.
50 (defun make-core-component (component segment length trace-table fixup-notes object)
51   (declare (type component component)
52            (type sb!assem:segment segment)
53            (type index length)
54            (list trace-table fixup-notes)
55            (type core-object object))
56   (without-gcing
57     (let* ((2comp (component-info component))
58            (constants (ir2-component-constants 2comp))
59            (trace-table (pack-trace-table trace-table))
60            (trace-table-len (length trace-table))
61            (trace-table-bits (* trace-table-len tt-bits-per-entry))
62            (total-length (+ length
63                             (ceiling trace-table-bits sb!vm:n-byte-bits)))
64            (box-num (- (length constants) sb!vm:code-trace-table-offset-slot))
65            (code-obj (allocate-code-object box-num total-length))
66            (fill-ptr (code-instructions code-obj)))
67       (declare (type index box-num total-length))
68
69       (sb!assem:on-segment-contents-vectorly
70        segment
71        (lambda (v)
72          (declare (type (simple-array sb!assem:assembly-unit 1) v))
73          (copy-byte-vector-to-system-area v fill-ptr)
74          (setf fill-ptr (sap+ fill-ptr (length v)))))
75
76       (do-core-fixups code-obj fixup-notes)
77
78       (dolist (entry (ir2-component-entries 2comp))
79         (make-fun-entry entry code-obj object))
80
81       (sb!vm:sanctify-for-execution code-obj)
82
83       (let ((info (debug-info-for-component component)))
84         (push info (core-object-debug-info object))
85         (setf (%code-debug-info code-obj) info))
86
87       (setf (code-header-ref code-obj sb!vm:code-trace-table-offset-slot)
88             length)
89       ;; KLUDGE: the "old" COPY-TO-SYSTEM-AREA automagically worked if
90       ;; somebody changed the number of bytes in a trace table entry.
91       ;; This version is a bit more fragile; if only there were some way
92       ;; to insulate ourselves against changes like that...
93       ;;
94       ;; Then again, PACK-TRACE-TABLE in src/compiler/trace-table.lisp
95       ;; doesn't appear to do anything interesting, returning a 0-length
96       ;; array.  So it seemingly doesn't matter what we do here.  Is this
97       ;; stale code?
98       ;;   --njf, 2005-03-23
99       (copy-ub16-to-system-area trace-table 0 fill-ptr 0 trace-table-len)
100
101       (do ((index sb!vm:code-constants-offset (1+ index)))
102           ((>= index (length constants)))
103         (let ((const (aref constants index)))
104           (etypecase const
105             (null)
106             (constant
107              (setf (code-header-ref code-obj index)
108                    (constant-value const)))
109             (list
110              (ecase (car const)
111                (:entry
112                 (reference-core-fun code-obj index (cdr const) object))
113                (:fdefinition
114                 (setf (code-header-ref code-obj index)
115                       (fdefinition-object (cdr const) t))))))))))
116   (values))