0.6.12.49:
[sbcl.git] / src / compiler / generic / core.lisp
1 ;;;; stuff that knows how to load compiled code directly into core
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!C")
13
14 ;;; A CORE-OBJECT structure holds the state needed to resolve cross-component
15 ;;; references during in-core compilation.
16 (defstruct (core-object
17             (:constructor make-core-object ())
18             #-no-ansi-print-object
19             (:print-object (lambda (x s)
20                              (print-unreadable-object (x s :type t))))
21             (:copier nil))
22   ;; A hashtable translating ENTRY-INFO structures to the corresponding actual
23   ;; FUNCTIONs for functions in this compilation.
24   (entry-table (make-hash-table :test 'eq) :type hash-table)
25   ;; A hashtable translating ENTRY-INFO structures to a list of pairs
26   ;; (<code object> . <offset>) describing the places that need to be
27   ;; backpatched to point to the function for ENTRY-INFO.
28   (patch-table (make-hash-table :test 'eq) :type hash-table)
29   ;; A list of all the DEBUG-INFO objects created, kept so that we can
30   ;; backpatch with the source info.
31   (debug-info () :type list))
32
33 ;;; Note the existence of FUNCTION.
34 (defun note-function (info function object)
35   (declare (type function function)
36            (type core-object object))
37   (let ((patch-table (core-object-patch-table object)))
38     (dolist (patch (gethash info patch-table))
39       (setf (code-header-ref (car patch) (the index (cdr patch))) function))
40     (remhash info patch-table))
41   (setf (gethash info (core-object-entry-table object)) function)
42   (values))
43
44 ;;; Do "load-time" fixups on the code vector.
45 (defun do-core-fixups (code fixups)
46   (declare (list fixups))
47   (dolist (info fixups)
48     (let* ((kind (first info))
49            (fixup (second info))
50            (name (fixup-name fixup))
51            (flavor (fixup-flavor fixup))
52            (offset (third info))
53            (value (ecase flavor
54                     (:assembly-routine
55                      (aver (symbolp name))
56                      (or (gethash name *assembler-routines*)
57                          (error "undefined assembler routine: ~S" name)))
58                     (:foreign
59                      (aver (stringp name))
60                      (or (foreign-symbol-address-as-integer name)
61                          (error "unknown foreign symbol: ~S")))
62                     #!+x86
63                     (:code-object
64                      (aver (null name))
65                      (values (get-lisp-obj-address code) t)))))
66       (sb!vm:fixup-code-object code offset value kind))))
67
68 ;;; Stick a reference to the function Fun in Code-Object at index I. If the
69 ;;; function hasn't been compiled yet, make a note in the Patch-Table.
70 (defun reference-core-function (code-obj i fun object)
71   (declare (type core-object object) (type functional fun)
72            (type index i))
73   (let* ((info (leaf-info fun))
74          (found (gethash info (core-object-entry-table object))))
75     (if found
76         (setf (code-header-ref code-obj i) found)
77         (push (cons code-obj i)
78               (gethash info (core-object-patch-table object)))))
79   (values))
80
81 ;;; Call the top-level lambda function dumped for Entry, returning the
82 ;;; values. Entry may be a :TOP-LEVEL-XEP functional.
83 (defun core-call-top-level-lambda (entry object)
84   (declare (type functional entry) (type core-object object))
85   (funcall (or (gethash (leaf-info entry)
86                         (core-object-entry-table object))
87                (error "Unresolved forward reference."))))
88
89 ;;; Backpatch all the DEBUG-INFOs dumped so far with the specified
90 ;;; SOURCE-INFO list. We also check that there are no outstanding forward
91 ;;; references to functions.
92 (defun fix-core-source-info (info object &optional source-info)
93   (declare (type source-info info) (type core-object object))
94   (aver (zerop (hash-table-count (core-object-patch-table object))))
95   (let ((res (debug-source-for-info info)))
96     (dolist (sinfo res)
97       (setf (debug-source-info sinfo) source-info))
98     (dolist (info (core-object-debug-info object))
99       (setf (compiled-debug-info-source info) res))
100     (setf (core-object-debug-info object) ()))
101   (values))