248d83ed1cf1e9de314601a54aa0fcb0d2c9c9af
[sbcl.git] / src / compiler / entry.lisp
1 ;;;; Code in this file handles VM-independent details of run-time
2 ;;;; function representation that primarily concern IR2 conversion and
3 ;;;; the dumper/loader.
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!C")
15
16 ;;; This phase runs before IR2 conversion, initializing each XEP's
17 ;;; ENTRY-INFO structure. We call the VM-supplied
18 ;;; SELECT-COMPONENT-FORMAT function to make VM-dependent
19 ;;; initializations in the IR2-COMPONENT. This includes setting the
20 ;;; IR2-COMPONENT-KIND and allocating fixed implementation overhead in
21 ;;; the constant pool. If there was a forward reference to a function,
22 ;;; then the ENTRY-INFO will already exist, but will be uninitialized.
23 (defun entry-analyze (component)
24   (let ((2comp (component-info component)))
25     (dolist (fun (component-lambdas component))
26       (when (xep-p fun)
27         (let ((info (or (leaf-info fun)
28                         (setf (leaf-info fun) (make-entry-info)))))
29           (compute-entry-info fun info)
30           (push info (ir2-component-entries 2comp))))))
31   (select-component-format component)
32   (values))
33
34 ;;; Initialize INFO structure to correspond to the XEP LAMBDA FUN.
35 (defun compute-entry-info (fun info)
36   (declare (type clambda fun) (type entry-info info))
37   (let ((bind (lambda-bind fun))
38         (internal-fun (functional-entry-fun fun)))
39     (setf (entry-info-closure-p info)
40           (not (null (physenv-closure (lambda-physenv fun)))))
41     (setf (entry-info-offset info) (gen-label))
42     (setf (entry-info-name info)
43           (leaf-debug-name internal-fun))
44     (when (policy bind (>= debug 1))
45       (let ((args (functional-arg-documentation internal-fun)))
46         (aver (not (eq args :unspecified)))
47         (setf (entry-info-arguments info) args))
48       (setf (entry-info-type info) (type-specifier (leaf-type internal-fun)))))
49   (values))
50
51 ;;; Replace all references to COMPONENT's non-closure XEPs that appear
52 ;;; in top level or externally-referenced components, changing to
53 ;;; :TOPLEVEL-XEP FUNCTIONALs. If the cross-component ref is not in a
54 ;;; :TOPLEVEL/externally-referenced component, or is to a closure,
55 ;;; then substitution is suppressed.
56 ;;;
57 ;;; When a cross-component ref is not substituted, we return T to
58 ;;; indicate that early deletion of this component's IR1 should not be
59 ;;; done. We also return T if this component contains
60 ;;; :TOPLEVEL/externally-referenced lambdas (though it is not a
61 ;;; :TOPLEVEL component.)
62 ;;;
63 ;;; We deliberately don't use the normal reference deletion, since we
64 ;;; don't want to trigger deletion of the XEP (although it shouldn't
65 ;;; hurt, since this is called after COMPONENT is compiled.) Instead,
66 ;;; we just clobber the REF-LEAF.
67 (defun replace-toplevel-xeps (component)
68   (let ((res nil))
69     (dolist (lambda (component-lambdas component))
70       (case (functional-kind lambda)
71         (:external
72          (unless (lambda-has-external-references-p lambda)
73            (let* ((ef (functional-entry-fun lambda))
74                   (new (make-functional
75                         :kind :toplevel-xep
76                         :info (leaf-info lambda)
77                         :%source-name (functional-%source-name ef)
78                         :%debug-name (functional-%debug-name ef)
79                         :lexenv (make-null-lexenv)))
80                   (closure (physenv-closure
81                             (lambda-physenv (main-entry ef)))))
82              (dolist (ref (leaf-refs lambda))
83                (let ((ref-component (node-component ref)))
84                  (cond ((eq ref-component component))
85                        ((or (not (component-toplevelish-p ref-component))
86                             closure)
87                         (setq res t))
88                        (t
89                         (setf (ref-leaf ref) new)
90                         (push ref (leaf-refs new))
91                         (setf (leaf-refs lambda)
92                               (delq ref (leaf-refs lambda))))))))))
93         (:toplevel
94          (setq res t))))
95     res))