Add :IMMOBILE-CODE feature.
[sbcl.git] / src / compiler / generic / target-core.lisp
blob9a343a9e18d83d663b45a5703f3c331292a32b92
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.
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.
17 (in-package "SB!C")
19 (declaim (ftype (sfunction (#!+immobile-code boolean fixnum fixnum)
20 code-component) allocate-code-object))
21 (defun allocate-code-object (#!+immobile-code immobile-p boxed unboxed)
22 #!+gencgc
23 (without-gcing
24 (if (or #!+immobile-code immobile-p)
25 #!+immobile-code (sb!vm::allocate-immobile-code boxed unboxed)
26 #!-immobile-code nil
27 (%make-lisp-obj
28 (alien-funcall (extern-alien "alloc_code_object" (function unsigned unsigned unsigned))
29 boxed unboxed))))
30 #!-gencgc
31 (%primitive allocate-code-object boxed unboxed))
33 ;;; Make a function entry, filling in slots from the ENTRY-INFO.
34 (defun make-fun-entry (entry-info code-obj object)
35 (declare (type entry-info entry-info) (type core-object object))
36 (let ((offset (label-position (entry-info-offset entry-info))))
37 (declare (type index offset))
38 (unless (zerop (logand offset sb!vm:lowtag-mask))
39 (error "Unaligned function object, offset = #X~X." offset))
40 (let ((res (%primitive compute-fun code-obj offset)))
41 (setf (%simple-fun-self res) res)
42 (setf (%simple-fun-next res) (%code-entry-points code-obj))
43 (setf (%code-entry-points code-obj) res)
44 (setf (%simple-fun-name res) (entry-info-name entry-info))
45 (setf (%simple-fun-arglist res) (entry-info-arguments entry-info))
46 (setf (%simple-fun-type res) (entry-info-type entry-info))
47 (setf (%simple-fun-info res) (entry-info-info entry-info))
49 (note-fun entry-info res object))))
51 ;;; Dump a component to core. We pass in the assembler fixups, code
52 ;;; vector and node info.
53 (defun make-core-component (component segment length fixup-notes object)
54 (declare (type component component)
55 (type sb!assem:segment segment)
56 (type index length)
57 (list fixup-notes)
58 (type core-object object))
59 (let ((debug-info (debug-info-for-component component)))
60 ;; FIXME: use WITHOUT-GCING only for stuff that needs it.
61 (without-gcing
62 (let* ((2comp (component-info component))
63 (constants (ir2-component-constants 2comp))
64 (box-num (- (length constants) sb!vm:code-constants-offset))
65 ;; All compilation into memory favors the immobile space.
66 (code-obj (allocate-code-object #!+immobile-code t box-num length))
67 (fill-ptr (code-instructions code-obj)))
68 (declare (type index box-num length))
70 (let ((v (sb!assem:segment-contents-as-vector segment)))
71 (declare (type (simple-array sb!assem:assembly-unit 1) v))
72 (copy-byte-vector-to-system-area v fill-ptr)
73 (setf fill-ptr (sap+ fill-ptr (length v))))
75 (do-core-fixups code-obj fixup-notes)
77 (dolist (entry (ir2-component-entries 2comp))
78 (make-fun-entry entry code-obj object))
80 #!-(or x86 x86-64)
81 (sb!vm:sanctify-for-execution code-obj)
83 (push debug-info (core-object-debug-info object))
84 (setf (%code-debug-info code-obj) debug-info)
86 (do ((index sb!vm:code-constants-offset (1+ index)))
87 ((>= index (length constants)))
88 (let ((const (aref constants index)))
89 (etypecase const
90 (null)
91 (constant
92 (setf (code-header-ref code-obj index)
93 (constant-value const)))
94 (list
95 (ecase (car const)
96 (:entry
97 (reference-core-fun code-obj index (cdr const) object))
98 (:fdefinition
99 (setf (code-header-ref code-obj index)
100 (find-or-create-fdefn (cdr const))))
101 (:known-fun
102 (setf (code-header-ref code-obj index)
103 (%coerce-name-to-fun (cdr const))))))))))))
104 (values))