3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 (defvar *xref-kinds* '(:binds :calls :sets :references :macroexpands))
16 (defun record-component-xrefs (component)
17 (declare (type component component))
18 (when (policy *lexenv* (zerop store-xref-data))
19 (return-from record-component-xrefs))
20 (do ((block (block-next (component-head component)) (block-next block)))
21 ((null (block-next block)))
22 (let* ((this-cont (block-start block))
23 (last (block-last block)))
24 (flet ((handle-node (functional)
25 ;; Record xref information for all nodes in the block.
26 ;; Note that this code can get executed several times
27 ;; for the same block, if the functional is referenced
28 ;; from multiple XEPs.
29 (loop for node = (ctran-next this-cont) then (ctran-next (node-next node))
31 do (record-node-xrefs node functional))
32 ;; Properly record the deferred macroexpansion information
33 ;; that's been stored in the block.
34 (dolist (xref-data (block-macroexpands block))
35 (record-xref :macroexpands
37 ;; We use the debug-name of the functional
38 ;; as an identifier. This works quite nicely,
39 ;; except for (fast/slow)-methods with non-symbol,
40 ;; non-number eql specializers, for which
41 ;; the debug-name doesn't map exactly
42 ;; to the fdefinition of the method.
46 (call-with-block-external-functionals block #'handle-node)))))
48 (defun call-with-block-external-functionals (block fun)
49 (let* ((functional (block-home-lambda block))
51 (labels ((local-function-name-p (name)
54 '(flet labels lambda))))
55 (handle-functional (functional)
56 ;; If a functional looks like a global function (has a
57 ;; XEP, isn't a local function or a lambda) record xref
58 ;; information for it. Otherwise recurse on the
59 ;; home-lambdas of all references to the functional.
60 (when (eq (functional-kind functional) :external)
61 (let ((entry (functional-entry-fun functional)))
63 (let ((name (functional-debug-name entry)))
64 (unless (local-function-name-p name)
65 (return-from handle-functional
66 (funcall fun entry)))))))
67 ;; Recurse only if we haven't already seen the
69 (unless (member functional seen)
70 (push functional seen)
71 (dolist (ref (functional-refs functional))
72 (handle-functional (node-home-lambda ref))))))
73 (unless (or (eq :deleted (functional-kind functional))
74 ;; If the block came from an inlined global
75 ;; function, ignore it.
76 (and (functional-inlinep functional)
77 (symbolp (functional-debug-name functional))))
78 (handle-functional functional)))))
80 (defun record-node-xrefs (node context)
81 (declare (type node node))
83 ((or creturn cif entry combination mv-combination cast))
85 (let ((leaf (ref-leaf node)))
88 (let* ((name (leaf-debug-name leaf)))
89 (case (global-var-kind leaf)
92 (record-xref :references name context node nil))
95 (record-xref :calls name context node nil)))))
96 ;; Inlined global function
98 (when (functional-inlinep leaf)
99 (let ((name (leaf-debug-name leaf)))
100 ;; FIXME: we should store the original var into the
101 ;; functional when creating inlined-functionals, so that
102 ;; we could just check whether it was a global-var,
103 ;; rather then needing to guess based on the debug-name.
104 (when (or (symbolp name)
105 ;; Any non-SETF non-symbol names will
106 ;; currently be either non-functions or
109 (equal (car name) 'setf)))
110 ;; TODO: a WHO-INLINES xref-kind could be useful
111 (record-xref :calls name context node nil)))))
112 ;; Reading a constant
114 (let* ((name (constant-%source-name leaf)))
115 (record-xref :references name context node nil))))))
116 ;; Setting a special variable
118 (let* ((var (set-var node)))
119 (when (and (global-var-p var)
120 (eq :special (global-var-kind var)))
122 (leaf-debug-name var)
126 ;; Binding a special variable
128 (let ((vars (lambda-vars (bind-lambda node))))
130 (when (lambda-var-specvar var)
132 (lambda-var-%source-name var)
137 (defun internal-name-p (what)
138 ;; Don't store XREF information for internals. We define as internal
139 ;; anything named only by symbols from either implementation
140 ;; packages, COMMON-LISP or KEYWORD. The last one is useful for
141 ;; example when dealing with ctors.
144 (every #'internal-name-p what))
146 (member (symbol-package what)
147 (load-time-value (list* (find-package "COMMON-LISP")
148 (find-package "KEYWORD")
152 (package-name package))
154 (list-all-packages))))))
157 (defun record-xref (kind what context node path)
158 (unless (internal-name-p what)
160 (source-path-original-source
162 (node-source-path node))))))
163 (push (list what path)
164 (getf (functional-xref context) kind)))))
166 (defun record-macroexpansion (what block path)
167 (unless (internal-name-p what)
168 (push (cons what path) (block-macroexpands block))))
170 ;;; Pack the xref table that was stored for a functional into a more
171 ;;; space-efficient form, and return that packed form.
172 (defun pack-xref-data (xref-data)
174 (let ((array (make-array (length *xref-kinds*))))
175 (loop for key in *xref-kinds*
177 for values = (remove-duplicates (getf xref-data key)
179 for flattened = (reduce #'append values :from-end t)
180 collect (setf (aref array i)
182 (make-array (length flattened)
183 :initial-contents flattened))))