declare smobs in alloc.c
[bpt/emacs.git] / admin / find-gc.el
CommitLineData
9584e63a
GM
1;;; find-gc.el --- detect functions that call the garbage collector
2
ba318903 3;; Copyright (C) 1992, 2001-2014 Free Software Foundation, Inc.
9584e63a 4
34dc21db 5;; Maintainer: emacs-devel@gnu.org
9584e63a
GM
6
7;; This file is part of GNU Emacs.
8
d6cba7ae 9;; GNU Emacs is free software: you can redistribute it and/or modify
9584e63a 10;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
9584e63a
GM
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
d6cba7ae 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
9584e63a
GM
21
22;;; Commentary:
23
b63ecadb
RS
24;; Produce in find-gc-unsafe-list the set of all functions that may invoke GC.
25;; This expects the Emacs sources to live in find-gc-source-directory.
9584e63a
GM
26
27;;; Code:
28
b63ecadb
RS
29(defvar find-gc-unsafe-list nil
30 "The list of unsafe functions is placed here by `find-gc-unsafe'.")
31
0c4decae
GM
32(defvar find-gc-source-directory
33 (file-name-as-directory (expand-file-name "src" source-directory))
34 "Directory containing Emacs C sources.")
b63ecadb 35
dc9c856d
RS
36(defvar find-gc-subrs-callers nil
37 "Alist of users of subrs, from GC testing.
38Each entry has the form (FUNCTION . FUNCTIONS-THAT-CALL-IT).")
39
40(defvar find-gc-subrs-called nil
41 "Alist of subrs called, in GC testing.
42Each entry has the form (FUNCTION . FUNCTIONS-IT-CALLS).")
43
b63ecadb
RS
44
45;;; Functions on this list are safe, even if they appear to be able
46;;; to call the target.
47
48(defvar find-gc-noreturn-list '(Fsignal Fthrow wrong_type_argument))
49
50;;; This was originally generated directory-files, but there were
51;;; too many files there that were not actually compiled. The
52;;; list below was created for a HP-UX 7.0 system.
53
54(defvar find-gc-source-files
55 '("dispnew.c" "scroll.c" "xdisp.c" "window.c"
56 "term.c" "cm.c" "emacs.c" "keyboard.c" "macros.c"
57 "keymap.c" "sysdep.c" "buffer.c" "filelock.c"
58 "insdel.c" "marker.c" "minibuf.c" "fileio.c"
5fa4ac76 59 "dired.c" "cmds.c" "casefiddle.c"
b63ecadb
RS
60 "indent.c" "search.c" "regex.c" "undo.c"
61 "alloc.c" "data.c" "doc.c" "editfns.c"
62 "callint.c" "eval.c" "fns.c" "print.c" "lread.c"
0c4decae 63 "syntax.c" "unexcoff.c"
b63ecadb 64 "bytecode.c" "process.c" "callproc.c" "doprnt.c"
0c4decae 65 "xterm.c" "xfns.c"))
b63ecadb
RS
66
67
9584e63a 68(defun find-gc-unsafe ()
b63ecadb 69 "Return a list of unsafe functions--that is, which can call GC.
0c4decae 70Also store it in `find-gc-unsafe-list'."
9584e63a
GM
71 (trace-call-tree nil)
72 (trace-use-tree)
73 (find-unsafe-funcs 'Fgarbage_collect)
b63ecadb
RS
74 (setq find-gc-unsafe-list
75 (sort find-gc-unsafe-list
76 (function (lambda (x y)
c5d670f5 77 (string-lessp (car x) (car y)))))))
9584e63a 78
9584e63a
GM
79;;; This does a depth-first search to find all functions that can
80;;; ultimately call the function "target". The result is an a-list
b63ecadb 81;;; in find-gc-unsafe-list; the cars are the unsafe functions, and the cdrs
9584e63a
GM
82;;; are (one of) the unsafe functions that these functions directly
83;;; call.
84
85(defun find-unsafe-funcs (target)
b63ecadb 86 (setq find-gc-unsafe-list (list (list target)))
c5d670f5 87 (trace-unsafe target))
9584e63a
GM
88
89(defun trace-unsafe (func)
dc9c856d 90 (let ((used (assq func find-gc-subrs-callers)))
9584e63a 91 (or used
dc9c856d 92 (error "No find-gc-subrs-callers for %s" (car find-gc-unsafe-list)))
9584e63a 93 (while (setq used (cdr used))
b63ecadb
RS
94 (or (assq (car used) find-gc-unsafe-list)
95 (memq (car used) find-gc-noreturn-list)
9584e63a 96 (progn
b63ecadb 97 (push (cons (car used) func) find-gc-unsafe-list)
c5d670f5 98 (trace-unsafe (car used)))))))
9584e63a
GM
99
100
9584e63a 101
9584e63a 102
0c4decae 103(defun trace-call-tree (&optional ignored)
9584e63a 104 (message "Setting up directories...")
0c4decae
GM
105 (setq find-gc-subrs-called nil)
106 (let ((case-fold-search nil)
107 (default-directory find-gc-source-directory)
108 (files find-gc-source-files)
109 name entry rtlfile)
110 (dolist (file files)
111 (message "Compiling %s..." file)
112 (call-process "gcc" nil nil nil "-I" "." "-I" "../lib"
113 "-fdump-rtl-expand" "-o" null-device "-c" file)
114 (setq rtlfile
115 (file-expand-wildcards (format "%s.*.expand" file) t))
116 (if (/= 1 (length rtlfile))
117 (message "Error compiling `%s'?" file)
118 (with-temp-buffer
119 (insert-file-contents (setq rtlfile (car rtlfile)))
120 (delete-file rtlfile)
121 (while (re-search-forward ";; Function \\|(call_insn " nil t)
122 (if (= (char-after (- (point) 3)) ?o)
9584e63a 123 (progn
0c4decae
GM
124 (looking-at "[a-zA-Z0-9_]+")
125 (setq name (intern (match-string 0)))
126 (message "%s : %s" (car files) name)
127 (setq entry (list name)
128 find-gc-subrs-called
129 (cons entry find-gc-subrs-called)))
130 (if (looking-at ".*\n?.*\"\\([A-Za-z0-9_]+\\)\"")
131 (progn
132 (setq name (intern (match-string 1)))
133 (or (memq name (cdr entry))
134 (setcdr entry (cons name (cdr entry)))))))))))))
9584e63a 135
9584e63a 136(defun trace-use-tree ()
dc9c856d
RS
137 (setq find-gc-subrs-callers (mapcar 'list (mapcar 'car find-gc-subrs-called)))
138 (let ((ptr find-gc-subrs-called)
9584e63a
GM
139 p2 found)
140 (while ptr
141 (setq p2 (car ptr))
142 (while (setq p2 (cdr p2))
dc9c856d 143 (if (setq found (assq (car p2) find-gc-subrs-callers))
9584e63a 144 (setcdr found (cons (car (car ptr)) (cdr found)))))
c5d670f5 145 (setq ptr (cdr ptr)))))
9584e63a
GM
146
147(provide 'find-gc)
148
149;;; find-gc.el ends here