Fix make-array transforms.
[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 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       (let ((v (sb!assem:segment-contents-as-vector segment)))
70         (declare (type (simple-array sb!assem:assembly-unit 1) v))
71         (copy-byte-vector-to-system-area v fill-ptr)
72         (setf fill-ptr (sap+ fill-ptr (length v))))
73
74       (do-core-fixups code-obj fixup-notes)
75
76       (dolist (entry (ir2-component-entries 2comp))
77         (make-fun-entry entry code-obj object))
78
79       (sb!vm:sanctify-for-execution code-obj)
80
81       (let ((info (debug-info-for-component component)))
82         (push info (core-object-debug-info object))
83         (setf (%code-debug-info code-obj) info))
84
85       (setf (code-header-ref code-obj sb!vm:code-trace-table-offset-slot)
86             length)
87       ;; KLUDGE: the "old" COPY-TO-SYSTEM-AREA automagically worked if
88       ;; somebody changed the number of bytes in a trace table entry.
89       ;; This version is a bit more fragile; if only there were some way
90       ;; to insulate ourselves against changes like that...
91       ;;
92       ;; Then again, PACK-TRACE-TABLE in src/compiler/trace-table.lisp
93       ;; doesn't appear to do anything interesting, returning a 0-length
94       ;; array.  So it seemingly doesn't matter what we do here.  Is this
95       ;; stale code?
96       ;;   --njf, 2005-03-23
97       (copy-ub16-to-system-area trace-table 0 fill-ptr 0 trace-table-len)
98
99       (do ((index sb!vm:code-constants-offset (1+ index)))
100           ((>= index (length constants)))
101         (let ((const (aref constants index)))
102           (etypecase const
103             (null)
104             (constant
105              (setf (code-header-ref code-obj index)
106                    (constant-value const)))
107             (list
108              (ecase (car const)
109                (:entry
110                 (reference-core-fun code-obj index (cdr const) object))
111                (:fdefinition
112                 (setf (code-header-ref code-obj index)
113                       (fdefinition-object (cdr const) t)))
114                (:known-fun
115                 (setf (code-header-ref code-obj index)
116                       (%coerce-name-to-fun (cdr const)))))))))))
117   (values))