Mercurial > emacs
annotate lisp/emacs-lisp/bytecomp.el @ 811:e694e0879463
*** empty log message ***
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Fri, 17 Jul 1992 08:15:29 +0000 |
parents | 6d993c174c62 |
children | 5bbabfcef929 |
rev | line source |
---|---|
757 | 1 ;;; -*- Mode: Emacs-Lisp -*- |
2 ;;; Compilation of Lisp code into byte code. | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
3 ;;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
757 | 4 |
5 ;; By Jamie Zawinski <jwz@lucid.com> and Hallvard Furuseth <hbf@ulrik.uio.no>. | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
6 ;; Subsequently modified by RMS. |
757 | 7 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
8 (defconst byte-compile-version "FSF 2.1") |
757 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 1, or (at your option) | |
15 ;; any later version. | |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | |
26 ;;; ======================================================================== | |
27 ;;; Entry points: | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
28 ;;; byte-recompile-directory, byte-compile-file, batch-byte-compile, |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
29 ;;; byte-compile, compile-defun |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
30 ;;; display-call-tree |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
31 ;;; (byte-compile-buffer and byte-compile-and-load-file were turned off |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
32 ;;; because they are not terribly useful and get in the way of completion.) |
757 | 33 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
34 ;;; This version of the byte compiler has the following improvements: |
757 | 35 ;;; + optimization of compiled code: |
36 ;;; - removal of unreachable code; | |
37 ;;; - removal of calls to side-effectless functions whose return-value | |
38 ;;; is unused; | |
39 ;;; - compile-time evaluation of safe constant forms, such as (consp nil) | |
40 ;;; and (ash 1 6); | |
41 ;;; - open-coding of literal lambdas; | |
42 ;;; - peephole optimization of emitted code; | |
43 ;;; - trivial functions are left uncompiled for speed. | |
44 ;;; + support for inline functions; | |
45 ;;; + compile-time evaluation of arbitrary expressions; | |
46 ;;; + compile-time warning messages for: | |
47 ;;; - functions being redefined with incompatible arglists; | |
48 ;;; - functions being redefined as macros, or vice-versa; | |
49 ;;; - functions or macros defined multiple times in the same file; | |
50 ;;; - functions being called with the incorrect number of arguments; | |
51 ;;; - functions being called which are not defined globally, in the | |
52 ;;; file, or as autoloads; | |
53 ;;; - assignment and reference of undeclared free variables; | |
54 ;;; - various syntax errors; | |
55 ;;; + correct compilation of nested defuns, defmacros, defvars and defsubsts; | |
56 ;;; + correct compilation of top-level uses of macros; | |
57 ;;; + the ability to generate a histogram of functions called. | |
58 | |
59 ;;; User customization variables: | |
60 ;;; | |
61 ;;; byte-compile-verbose Whether to report the function currently being | |
62 ;;; compiled in the minibuffer; | |
63 ;;; byte-optimize Whether to do optimizations; this may be | |
64 ;;; t, nil, 'source, or 'byte; | |
65 ;;; byte-optimize-log Whether to report (in excruciating detail) | |
66 ;;; exactly which optimizations have been made. | |
67 ;;; This may be t, nil, 'source, or 'byte; | |
68 ;;; byte-compile-error-on-warn Whether to stop compilation when a warning is | |
69 ;;; produced; | |
70 ;;; byte-compile-delete-errors Whether the optimizer may delete calls or | |
71 ;;; variable references that are side-effect-free | |
72 ;;; except that they may return an error. | |
73 ;;; byte-compile-generate-call-tree Whether to generate a histogram of | |
74 ;;; function calls. This can be useful for | |
75 ;;; finding unused functions, as well as simple | |
76 ;;; performance metering. | |
77 ;;; byte-compile-warnings List of warnings to issue, or t. May contain | |
78 ;;; 'free-vars (references to variables not in the | |
79 ;;; current lexical scope) | |
80 ;;; 'unresolved (calls to unknown functions) | |
81 ;;; 'callargs (lambda calls with args that don't | |
82 ;;; match the lambda's definition) | |
83 ;;; 'redefine (function cell redefined from | |
84 ;;; a macro to a lambda or vice versa, | |
85 ;;; or redefined to take other args) | |
86 ;;; This defaults to nil in -batch mode, which is | |
87 ;;; slightly faster. | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
88 ;;; byte-compile-compatibility Whether the compiler should |
757 | 89 ;;; generate .elc files which can be loaded into |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
90 ;;; generic emacs 18. |
757 | 91 ;;; byte-compile-single-version Normally the byte-compiler will consult the |
92 ;;; above two variables at runtime, but if this | |
93 ;;; variable is true when the compiler itself is | |
94 ;;; compiled, then the runtime checks will not be | |
95 ;;; made, and compilation will be slightly faster. | |
96 ;;; byte-compile-overwrite-file If nil, delete old .elc files before saving. | |
97 | |
98 ;;; New Features: | |
99 ;;; | |
100 ;;; o The form `defsubst' is just like `defun', except that the function | |
101 ;;; generated will be open-coded in compiled code which uses it. This | |
102 ;;; means that no function call will be generated, it will simply be | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
103 ;;; spliced in. Lisp functions calls are very slow, so this can be a |
757 | 104 ;;; big win. |
105 ;;; | |
106 ;;; You can generally accomplish the same thing with `defmacro', but in | |
107 ;;; that case, the defined procedure can't be used as an argument to | |
108 ;;; mapcar, etc. | |
109 ;;; | |
110 ;;; o You can also open-code one particular call to a function without | |
111 ;;; open-coding all calls. Use the 'inline' form to do this, like so: | |
112 ;;; | |
113 ;;; (inline (foo 1 2 3)) ;; `foo' will be open-coded | |
114 ;;; or... | |
115 ;;; (inline ;; `foo' and `baz' will be | |
116 ;;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not. | |
117 ;;; (baz 0)) | |
118 ;;; | |
119 ;;; o It is possible to open-code a function in the same file it is defined | |
120 ;;; in without having to load that file before compiling it. the | |
121 ;;; byte-compiler has been modified to remember function definitions in | |
122 ;;; the compilation environment in the same way that it remembers macro | |
123 ;;; definitions. | |
124 ;;; | |
125 ;;; o Forms like ((lambda ...) ...) are open-coded. | |
126 ;;; | |
127 ;;; o The form `eval-when-compile' is like progn, except that the body | |
128 ;;; is evaluated at compile-time. When it appears at top-level, this | |
129 ;;; is analagous to the Common Lisp idiom (eval-when (compile) ...). | |
130 ;;; When it does not appear at top-level, it is similar to the | |
131 ;;; Common Lisp #. reader macro (but not in interpreted code.) | |
132 ;;; | |
133 ;;; o The form `eval-and-compile' is similar to eval-when-compile, but | |
134 ;;; the whole form is evalled both at compile-time and at run-time. | |
135 ;;; | |
136 ;;; o The command Meta-X byte-compile-and-load-file does what you'd think. | |
137 ;;; | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
138 ;;; o The command compile-defun is analogous to eval-defun. |
757 | 139 ;;; |
140 ;;; o If you run byte-compile-file on a filename which is visited in a | |
141 ;;; buffer, and that buffer is modified, you are asked whether you want | |
142 ;;; to save the buffer before compiling. | |
143 | |
144 (or (fboundp 'defsubst) | |
145 ;; This really ought to be loaded already! | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
146 (load-library "byte-run")) |
757 | 147 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
148 ;;; The feature of compiling in a specific target Emacs version |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
149 ;;; has been turned off because compile time options are a bad idea. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
150 (defmacro byte-compile-single-version () nil) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
151 (defmacro byte-compile-version-cond (cond) cond) |
757 | 152 |
153 ;;; The crud you see scattered through this file of the form | |
154 ;;; (or (and (boundp 'epoch::version) epoch::version) | |
155 ;;; (string-lessp emacs-version "19")) | |
156 ;;; is because the Epoch folks couldn't be bothered to follow the | |
157 ;;; normal emacs version numbering convention. | |
158 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
159 ;; (if (byte-compile-version-cond |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
160 ;; (or (and (boundp 'epoch::version) epoch::version) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
161 ;; (string-lessp emacs-version "19"))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
162 ;; (progn |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
163 ;; ;; emacs-18 compatibility. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
164 ;; (defvar baud-rate (baud-rate)) ;Define baud-rate if it's undefined |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
165 ;; |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
166 ;; (if (byte-compile-single-version) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
167 ;; (defmacro compiled-function-p (x) "Emacs 18 doesn't have these." nil) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
168 ;; (defun compiled-function-p (x) "Emacs 18 doesn't have these." nil)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
169 ;; |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
170 ;; (or (and (fboundp 'member) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
171 ;; ;; avoid using someone else's possibly bogus definition of this. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
172 ;; (subrp (symbol-function 'member))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
173 ;; (defun member (elt list) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
174 ;; "like memq, but uses equal instead of eq. In v19, this is a subr." |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
175 ;; (while (and list (not (equal elt (car list)))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
176 ;; (setq list (cdr list))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
177 ;; list)))) |
757 | 178 |
179 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
180 (defvar emacs-lisp-file-regexp (if (eq system-type 'vax-vms) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
181 "\\.EL\\(;[0-9]+\\)?$" |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
182 "\\.el$") |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
183 "*Regexp which matches Emacs Lisp source files. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
184 You may want to redefine `byte-compile-dest-file' if you change this.") |
757 | 185 |
186 (or (fboundp 'byte-compile-dest-file) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
187 ;; The user may want to redefine this, |
757 | 188 ;; so only define it if it is undefined. |
189 (defun byte-compile-dest-file (filename) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
190 "Convert an Emacs Lisp source file name to a compiled file name." |
757 | 191 (setq filename (file-name-sans-versions filename)) |
192 (cond ((eq system-type 'vax-vms) | |
193 (concat (substring filename 0 (string-match ";" filename)) "c")) | |
194 (t (concat filename "c"))))) | |
195 | |
196 ;; This can be the 'byte-compile property of any symbol. | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
197 (autoload 'byte-compile-inline-expand "byte-opt") |
757 | 198 |
199 ;; This is the entrypoint to the lapcode optimizer pass1. | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
200 (autoload 'byte-optimize-form "byte-opt") |
757 | 201 ;; This is the entrypoint to the lapcode optimizer pass2. |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
202 (autoload 'byte-optimize-lapcode "byte-opt") |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
203 (autoload 'byte-compile-unfold-lambda "byte-opt") |
757 | 204 |
205 (defvar byte-compile-verbose | |
206 (and (not noninteractive) (> baud-rate search-slow-speed)) | |
207 "*Non-nil means print messages describing progress of byte-compiler.") | |
208 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
209 (defvar byte-compile-compatibility nil |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
210 "*Non-nil means generate output that can run in Emacs 18.") |
757 | 211 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
212 ;; (defvar byte-compile-generate-emacs19-bytecodes |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
213 ;; (not (or (and (boundp 'epoch::version) epoch::version) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
214 ;; (string-lessp emacs-version "19"))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
215 ;; "*If this is true, then the byte-compiler will generate bytecode which |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
216 ;; makes use of byte-ops which are present only in Emacs 19. Code generated |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
217 ;; this way can never be run in Emacs 18, and may even cause it to crash.") |
757 | 218 |
219 (defvar byte-optimize t | |
220 "*If nil, no compile-optimizations will be done. | |
221 Compilation will be faster, generated code will be slower and larger. | |
222 This may be nil, t, 'byte, or 'source. If it is 'byte, then only byte-level | |
223 optimizations will be done; if it is 'source, then only source-level | |
224 optimizations will be done.") | |
225 | |
226 (defvar byte-compile-delete-errors t | |
227 "*If non-nil, the optimizer may delete forms that may signal an error | |
228 (variable references and side-effect-free functions such as CAR).") | |
229 | |
230 (defvar byte-optimize-log nil | |
231 "*If true, the byte-compiler will log its optimizations into *Compile-Log*. | |
232 If this is 'source, then only source-level optimizations will be logged. | |
233 If it is 'byte, then only byte-level optimizations will be logged.") | |
234 | |
235 (defvar byte-compile-error-on-warn nil | |
236 "*If true, the byte-compiler will report warnings with `error' instead | |
237 of `message.'") | |
238 | |
239 (defconst byte-compile-warning-types '(redefine callargs free-vars unresolved)) | |
240 (defvar byte-compile-warnings (not noninteractive) | |
241 "*List of warnings that the byte-compiler should issue (t for all). | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
242 Valid elements of this list are `callargs', `redefine', `free-vars', |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
243 and `unresolved'.") |
757 | 244 |
245 (defvar byte-compile-generate-call-tree nil | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
246 "*Non-nil means collect call-graph information when compiling. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
247 This records functions were called and from where. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
248 If the value is t, compilation displays the call graph when it finishes. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
249 If the value is neither t nor nil, compilation asks you whether to display |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
250 the graph. |
757 | 251 |
252 The call tree only lists functions called, not macros used. Those functions | |
253 which the byte-code interpreter knows about directly (eq, cons, etc.) are | |
254 not reported. | |
255 | |
256 The call tree also lists those functions which are not known to be called | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
257 \(that is, to which no calls have been compiled.) Functions which can be |
757 | 258 invoked interactively are excluded from this list.") |
259 | |
260 (defconst byte-compile-call-tree nil "Alist of functions and their call tree. | |
261 Each element looks like | |
262 | |
263 \(FUNCTION CALLERS CALLS\) | |
264 | |
265 where CALLERS is a list of functions that call FUNCTION, and CALLS | |
266 is a list of functions for which calls were generated while compiling | |
267 FUNCTION.") | |
268 | |
269 (defvar byte-compile-call-tree-sort 'name | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
270 "*If non-nil, sort the call tree. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
271 The values `name', `callers', `calls', `calls+callers' |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
272 specify different fields to sort on.") |
757 | 273 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
274 ;; (defvar byte-compile-overwrite-file t |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
275 ;; "If nil, old .elc files are deleted before the new is saved, and .elc |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
276 ;; files will have the same modes as the corresponding .el file. Otherwise, |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
277 ;; existing .elc files will simply be overwritten, and the existing modes |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
278 ;; will not be changed. If this variable is nil, then an .elc file which |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
279 ;; is a symbolic link will be turned into a normal file, instead of the file |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
280 ;; which the link points to being overwritten.") |
757 | 281 |
282 (defvar byte-compile-constants nil | |
283 "list of all constants encountered during compilation of this form") | |
284 (defvar byte-compile-variables nil | |
285 "list of all variables encountered during compilation of this form") | |
286 (defvar byte-compile-bound-variables nil | |
287 "list of variables bound in the context of the current form; this list | |
288 lives partly on the stack.") | |
289 (defvar byte-compile-free-references) | |
290 (defvar byte-compile-free-assignments) | |
291 | |
292 (defconst byte-compile-initial-macro-environment | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
293 '( |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
294 ;; (byte-compiler-options . (lambda (&rest forms) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
295 ;; (apply 'byte-compiler-options-handler forms))) |
757 | 296 (eval-when-compile . (lambda (&rest body) |
297 (list 'quote (eval (byte-compile-top-level | |
298 (cons 'progn body)))))) | |
299 (eval-and-compile . (lambda (&rest body) | |
300 (eval (cons 'progn body)) | |
301 (cons 'progn body)))) | |
302 "The default macro-environment passed to macroexpand by the compiler. | |
303 Placing a macro here will cause a macro to have different semantics when | |
304 expanded by the compiler as when expanded by the interpreter.") | |
305 | |
306 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
307 "Alist of macros defined in the file being compiled. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
308 Each element looks like (MACRONAME . DEFINITION). It is |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
309 \(MACRONAME . nil) when a function is redefined as a function.") |
757 | 310 |
311 (defvar byte-compile-function-environment nil | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
312 "Alist of functions defined in the file being compiled. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
313 This is so we can inline them when necessary. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
314 Each element looks like (FUNCTIONNAME . DEFINITION). It is |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
315 \(FUNCTIONNAME . nil) when a function is redefined as a macro.") |
757 | 316 |
317 (defvar byte-compile-unresolved-functions nil | |
318 "Alist of undefined functions to which calls have been compiled (used for | |
319 warnings when the function is later defined with incorrect args).") | |
320 | |
321 (defvar byte-compile-tag-number 0) | |
322 (defvar byte-compile-output nil | |
323 "Alist describing contents to put in byte code string. | |
324 Each element is (INDEX . VALUE)") | |
325 (defvar byte-compile-depth 0 "Current depth of execution stack.") | |
326 (defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.") | |
327 | |
328 | |
329 ;;; The byte codes; this information is duplicated in bytecomp.c | |
330 | |
331 (defconst byte-code-vector nil | |
332 "An array containing byte-code names indexed by byte-code values.") | |
333 | |
334 (defconst byte-stack+-info nil | |
335 "An array with the stack adjustment for each byte-code.") | |
336 | |
337 (defmacro byte-defop (opcode stack-adjust opname &optional docstring) | |
338 ;; This is a speed-hack for building the byte-code-vector at compile-time. | |
339 ;; We fill in the vector at macroexpand-time, and then after the last call | |
340 ;; to byte-defop, we write the vector out as a constant instead of writing | |
341 ;; out a bunch of calls to aset. | |
342 ;; Actually, we don't fill in the vector itself, because that could make | |
343 ;; it problematic to compile big changes to this compiler; we store the | |
344 ;; values on its plist, and remove them later in -extrude. | |
345 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value) | |
346 (put 'byte-code-vector 'tmp-compile-time-value | |
347 (make-vector 256 nil)))) | |
348 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value) | |
349 (put 'byte-stack+-info 'tmp-compile-time-value | |
350 (make-vector 256 nil))))) | |
351 (aset v1 opcode opname) | |
352 (aset v2 opcode stack-adjust)) | |
353 (if docstring | |
354 (list 'defconst opname opcode (concat "Byte code opcode " docstring ".")) | |
355 (list 'defconst opname opcode))) | |
356 | |
357 (defmacro byte-extrude-byte-code-vectors () | |
358 (prog1 (list 'setq 'byte-code-vector | |
359 (get 'byte-code-vector 'tmp-compile-time-value) | |
360 'byte-stack+-info | |
361 (get 'byte-stack+-info 'tmp-compile-time-value)) | |
362 ;; emacs-18 has no REMPROP. | |
363 (put 'byte-code-vector 'tmp-compile-time-value nil) | |
364 (put 'byte-stack+-info 'tmp-compile-time-value nil))) | |
365 | |
366 | |
367 ;; unused: 0-7 | |
368 | |
369 ;; These opcodes are special in that they pack their argument into the | |
370 ;; opcode word. | |
371 ;; | |
372 (byte-defop 8 1 byte-varref "for variable reference") | |
373 (byte-defop 16 -1 byte-varset "for setting a variable") | |
374 (byte-defop 24 -1 byte-varbind "for binding a variable") | |
375 (byte-defop 32 0 byte-call "for calling a function") | |
376 (byte-defop 40 0 byte-unbind "for unbinding special bindings") | |
377 ;; codes 41-47 are consumed by the preceeding opcodes | |
378 | |
379 ;; unused: 48-55 | |
380 | |
381 (byte-defop 56 -1 byte-nth) | |
382 (byte-defop 57 0 byte-symbolp) | |
383 (byte-defop 58 0 byte-consp) | |
384 (byte-defop 59 0 byte-stringp) | |
385 (byte-defop 60 0 byte-listp) | |
386 (byte-defop 61 -1 byte-eq) | |
387 (byte-defop 62 -1 byte-memq) | |
388 (byte-defop 63 0 byte-not) | |
389 (byte-defop 64 0 byte-car) | |
390 (byte-defop 65 0 byte-cdr) | |
391 (byte-defop 66 -1 byte-cons) | |
392 (byte-defop 67 0 byte-list1) | |
393 (byte-defop 68 -1 byte-list2) | |
394 (byte-defop 69 -2 byte-list3) | |
395 (byte-defop 70 -3 byte-list4) | |
396 (byte-defop 71 0 byte-length) | |
397 (byte-defop 72 -1 byte-aref) | |
398 (byte-defop 73 -2 byte-aset) | |
399 (byte-defop 74 0 byte-symbol-value) | |
400 (byte-defop 75 0 byte-symbol-function) ; this was commented out | |
401 (byte-defop 76 -1 byte-set) | |
402 (byte-defop 77 -1 byte-fset) ; this was commented out | |
403 (byte-defop 78 -1 byte-get) | |
404 (byte-defop 79 -2 byte-substring) | |
405 (byte-defop 80 -1 byte-concat2) | |
406 (byte-defop 81 -2 byte-concat3) | |
407 (byte-defop 82 -3 byte-concat4) | |
408 (byte-defop 83 0 byte-sub1) | |
409 (byte-defop 84 0 byte-add1) | |
410 (byte-defop 85 -1 byte-eqlsign) | |
411 (byte-defop 86 -1 byte-gtr) | |
412 (byte-defop 87 -1 byte-lss) | |
413 (byte-defop 88 -1 byte-leq) | |
414 (byte-defop 89 -1 byte-geq) | |
415 (byte-defop 90 -1 byte-diff) | |
416 (byte-defop 91 0 byte-negate) | |
417 (byte-defop 92 -1 byte-plus) | |
418 (byte-defop 93 -1 byte-max) | |
419 (byte-defop 94 -1 byte-min) | |
420 (byte-defop 95 -1 byte-mult) ; v19 only | |
421 (byte-defop 96 1 byte-point) | |
422 (byte-defop 97 1 byte-mark-OBSOLETE) ; no longer generated as of v18 | |
423 (byte-defop 98 0 byte-goto-char) | |
424 (byte-defop 99 0 byte-insert) | |
425 (byte-defop 100 1 byte-point-max) | |
426 (byte-defop 101 1 byte-point-min) | |
427 (byte-defop 102 0 byte-char-after) | |
428 (byte-defop 103 1 byte-following-char) | |
429 (byte-defop 104 1 byte-preceding-char) | |
430 (byte-defop 105 1 byte-current-column) | |
431 (byte-defop 106 0 byte-indent-to) | |
432 (byte-defop 107 0 byte-scan-buffer-OBSOLETE) ; no longer generated as of v18 | |
433 (byte-defop 108 1 byte-eolp) | |
434 (byte-defop 109 1 byte-eobp) | |
435 (byte-defop 110 1 byte-bolp) | |
436 (byte-defop 111 1 byte-bobp) | |
437 (byte-defop 112 1 byte-current-buffer) | |
438 (byte-defop 113 0 byte-set-buffer) | |
439 (byte-defop 114 1 byte-read-char-OBSOLETE) | |
440 (byte-defop 115 0 byte-set-mark-OBSOLETE) | |
441 (byte-defop 116 1 byte-interactive-p) | |
442 | |
443 ;; These ops are new to v19 | |
444 (byte-defop 117 0 byte-forward-char) | |
445 (byte-defop 118 0 byte-forward-word) | |
446 (byte-defop 119 -1 byte-skip-chars-forward) | |
447 (byte-defop 120 -1 byte-skip-chars-backward) | |
448 (byte-defop 121 0 byte-forward-line) | |
449 (byte-defop 122 0 byte-char-syntax) | |
450 (byte-defop 123 -1 byte-buffer-substring) | |
451 (byte-defop 124 -1 byte-delete-region) | |
452 (byte-defop 125 -1 byte-narrow-to-region) | |
453 (byte-defop 126 1 byte-widen) | |
454 (byte-defop 127 0 byte-end-of-line) | |
455 | |
456 ;; unused: 128 | |
457 | |
458 ;; These store their argument in the next two bytes | |
459 (byte-defop 129 1 byte-constant2 | |
460 "for reference to a constant with vector index >= byte-constant-limit") | |
461 (byte-defop 130 0 byte-goto "for unconditional jump") | |
462 (byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil") | |
463 (byte-defop 132 -1 byte-goto-if-not-nil "to pop value and jump if it's not nil") | |
464 (byte-defop 133 -1 byte-goto-if-nil-else-pop | |
465 "to examine top-of-stack, jump and don't pop it if it's nil, | |
466 otherwise pop it") | |
467 (byte-defop 134 -1 byte-goto-if-not-nil-else-pop | |
468 "to examine top-of-stack, jump and don't pop it if it's non nil, | |
469 otherwise pop it") | |
470 | |
471 (byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'") | |
472 (byte-defop 136 -1 byte-discard "to discard one value from stack") | |
473 (byte-defop 137 1 byte-dup "to duplicate the top of the stack") | |
474 | |
475 (byte-defop 138 0 byte-save-excursion | |
476 "to make a binding to record the buffer, point and mark") | |
477 (byte-defop 139 0 byte-save-window-excursion | |
478 "to make a binding to record entire window configuration") | |
479 (byte-defop 140 0 byte-save-restriction | |
480 "to make a binding to record the current buffer clipping restrictions") | |
481 (byte-defop 141 -1 byte-catch | |
482 "for catch. Takes, on stack, the tag and an expression for the body") | |
483 (byte-defop 142 -1 byte-unwind-protect | |
484 "for unwind-protect. Takes, on stack, an expression for the unwind-action") | |
485 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
486 ;; For condition-case. Takes, on stack, the variable to bind, |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
487 ;; an expression for the body, and a list of clauses. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
488 (byte-defop 143 -2 byte-condition-case) |
757 | 489 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
490 ;; For entry to with-output-to-temp-buffer. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
491 ;; Takes, on stack, the buffer name. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
492 ;; Binds standard-output and does some other things. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
493 ;; Returns with temp buffer on the stack in place of buffer name. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
494 (byte-defop 144 0 byte-temp-output-buffer-setup) |
757 | 495 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
496 ;; For exit from with-output-to-temp-buffer. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
497 ;; Expects the temp buffer on the stack underneath value to return. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
498 ;; Pops them both, then pushes the value back on. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
499 ;; Unbinds standard-output and makes the temp buffer visible. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
500 (byte-defop 145 -1 byte-temp-output-buffer-show) |
757 | 501 |
502 ;; these ops are new to v19 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
503 |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
504 ;; To unbind back to the beginning of this frame. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
505 ;; Not used yet, but wil be needed for tail-recursion elimination. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
506 (byte-defop 146 0 byte-unbind-all) |
757 | 507 |
508 ;; these ops are new to v19 | |
509 (byte-defop 147 -2 byte-set-marker) | |
510 (byte-defop 148 0 byte-match-beginning) | |
511 (byte-defop 149 0 byte-match-end) | |
512 (byte-defop 150 0 byte-upcase) | |
513 (byte-defop 151 0 byte-downcase) | |
514 (byte-defop 152 -1 byte-string=) | |
515 (byte-defop 153 -1 byte-string<) | |
516 (byte-defop 154 -1 byte-equal) | |
517 (byte-defop 155 -1 byte-nthcdr) | |
518 (byte-defop 156 -1 byte-elt) | |
519 (byte-defop 157 -1 byte-member) | |
520 (byte-defop 158 -1 byte-assq) | |
521 (byte-defop 159 0 byte-nreverse) | |
522 (byte-defop 160 -1 byte-setcar) | |
523 (byte-defop 161 -1 byte-setcdr) | |
524 (byte-defop 162 0 byte-car-safe) | |
525 (byte-defop 163 0 byte-cdr-safe) | |
526 (byte-defop 164 -1 byte-nconc) | |
527 (byte-defop 165 -1 byte-quo) | |
528 (byte-defop 166 -1 byte-rem) | |
529 (byte-defop 167 0 byte-numberp) | |
530 (byte-defop 168 0 byte-integerp) | |
531 | |
532 ;; unused: 169 | |
533 | |
534 ;; New to v19. These store their arg in the next byte. | |
535 (byte-defop 170 0 byte-rel-goto) | |
536 (byte-defop 171 -1 byte-rel-goto-if-nil) | |
537 (byte-defop 172 -1 byte-rel-goto-if-not-nil) | |
538 (byte-defop 173 -1 byte-rel-goto-if-nil-else-pop) | |
539 (byte-defop 174 -1 byte-rel-goto-if-not-nil-else-pop) | |
540 | |
541 (byte-defop 175 nil byte-listN) | |
542 (byte-defop 176 nil byte-concatN) | |
543 (byte-defop 177 nil byte-insertN) | |
544 | |
545 ;; unused: 178-191 | |
546 | |
547 (byte-defop 192 1 byte-constant "for reference to a constant") | |
548 ;; codes 193-255 are consumed by byte-constant. | |
549 (defconst byte-constant-limit 64 | |
550 "Exclusive maximum index usable in the `byte-constant' opcode.") | |
551 | |
552 (defconst byte-goto-ops '(byte-goto byte-goto-if-nil byte-goto-if-not-nil | |
553 byte-goto-if-nil-else-pop | |
554 byte-goto-if-not-nil-else-pop) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
555 "List of byte-codes whose offset is a pc.") |
757 | 556 |
557 (defconst byte-goto-always-pop-ops '(byte-goto-if-nil byte-goto-if-not-nil)) | |
558 | |
559 (defconst byte-rel-goto-ops '(byte-rel-goto | |
560 byte-rel-goto-if-nil byte-rel-goto-if-not-nil | |
561 byte-rel-goto-if-nil-else-pop | |
562 byte-rel-goto-if-not-nil-else-pop) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
563 "List of byte-codes for relative jumps.") |
757 | 564 |
565 (byte-extrude-byte-code-vectors) | |
566 | |
567 ;;; lapcode generator | |
568 ;;; | |
569 ;;; the byte-compiler now does source -> lapcode -> bytecode instead of | |
570 ;;; source -> bytecode, because it's a lot easier to make optimizations | |
571 ;;; on lapcode than on bytecode. | |
572 ;;; | |
573 ;;; Elements of the lapcode list are of the form (<instruction> . <parameter>) | |
574 ;;; where instruction is a symbol naming a byte-code instruction, | |
575 ;;; and parameter is an argument to that instruction, if any. | |
576 ;;; | |
577 ;;; The instruction can be the pseudo-op TAG, which means that this position | |
578 ;;; in the instruction stream is a target of a goto. (car PARAMETER) will be | |
579 ;;; the PC for this location, and the whole instruction "(TAG pc)" will be the | |
580 ;;; parameter for some goto op. | |
581 ;;; | |
582 ;;; If the operation is varbind, varref, varset or push-constant, then the | |
583 ;;; parameter is (variable/constant . index_in_constant_vector). | |
584 ;;; | |
585 ;;; First, the source code is macroexpanded and optimized in various ways. | |
586 ;;; Then the resultant code is compiled into lapcode. Another set of | |
587 ;;; optimizations are then run over the lapcode. Then the variables and | |
588 ;;; constants referenced by the lapcode are collected and placed in the | |
589 ;;; constants-vector. (This happens now so that variables referenced by dead | |
590 ;;; code don't consume space.) And finally, the lapcode is transformed into | |
591 ;;; compacted byte-code. | |
592 ;;; | |
593 ;;; A distinction is made between variables and constants because the variable- | |
594 ;;; referencing instructions are more sensitive to the variables being near the | |
595 ;;; front of the constants-vector than the constant-referencing instructions. | |
596 ;;; Also, this lets us notice references to free variables. | |
597 | |
598 (defun byte-compile-lapcode (lap) | |
599 "Turns lapcode into bytecode. The lapcode is destroyed." | |
600 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC. | |
601 (let ((pc 0) ; Program counter | |
602 op off ; Operation & offset | |
603 (bytes '()) ; Put the output bytes here | |
604 (patchlist nil) ; List of tags and goto's to patch | |
605 rest rel tmp) | |
606 (while lap | |
607 (setq op (car (car lap)) | |
608 off (cdr (car lap))) | |
609 (cond ((not (symbolp op)) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
610 (error "Non-symbolic opcode `%s'" op)) |
757 | 611 ((eq op 'TAG) |
612 (setcar off pc) | |
613 (setq patchlist (cons off patchlist))) | |
614 ((memq op byte-goto-ops) | |
615 (setq pc (+ pc 3)) | |
616 (setq bytes (cons (cons pc (cdr off)) | |
617 (cons nil | |
618 (cons (symbol-value op) bytes)))) | |
619 (setq patchlist (cons bytes patchlist))) | |
620 (t | |
621 (setq bytes | |
622 (cond ((cond ((consp off) | |
623 ;; Variable or constant reference | |
624 (setq off (cdr off)) | |
625 (eq op 'byte-constant))) | |
626 (cond ((< off byte-constant-limit) | |
627 (setq pc (1+ pc)) | |
628 (cons (+ byte-constant off) bytes)) | |
629 (t | |
630 (setq pc (+ 3 pc)) | |
631 (cons (lsh off -8) | |
632 (cons (logand off 255) | |
633 (cons byte-constant2 bytes)))))) | |
634 ((<= byte-listN (symbol-value op)) | |
635 (setq pc (+ 2 pc)) | |
636 (cons off (cons (symbol-value op) bytes))) | |
637 ((< off 6) | |
638 (setq pc (1+ pc)) | |
639 (cons (+ (symbol-value op) off) bytes)) | |
640 ((< off 256) | |
641 (setq pc (+ 2 pc)) | |
642 (cons off (cons (+ (symbol-value op) 6) bytes))) | |
643 (t | |
644 (setq pc (+ 3 pc)) | |
645 (cons (lsh off -8) | |
646 (cons (logand off 255) | |
647 (cons (+ (symbol-value op) 7) | |
648 bytes)))))))) | |
649 (setq lap (cdr lap))) | |
650 ;;(if (not (= pc (length bytes))) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
651 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
652 (cond ((byte-compile-version-cond byte-compile-compatibility) |
757 | 653 ;; Make relative jumps |
654 (setq patchlist (nreverse patchlist)) | |
655 (while (progn | |
656 (setq off 0) ; PC change because of deleted bytes | |
657 (setq rest patchlist) | |
658 (while rest | |
659 (setq tmp (car rest)) | |
660 (and (consp (car tmp)) ; Jump | |
661 (prog1 (null (nth 1 tmp)) ; Absolute jump | |
662 (setq tmp (car tmp))) | |
663 (progn | |
664 (setq rel (- (car (cdr tmp)) (car tmp))) | |
665 (and (<= -129 rel) (< rel 128))) | |
666 (progn | |
667 ;; Convert to relative jump. | |
668 (setcdr (car rest) (cdr (cdr (car rest)))) | |
669 (setcar (cdr (car rest)) | |
670 (+ (car (cdr (car rest))) | |
671 (- byte-rel-goto byte-goto))) | |
672 (setq off (1- off)))) | |
673 (setcar tmp (+ (car tmp) off)) ; Adjust PC | |
674 (setq rest (cdr rest))) | |
675 ;; If optimizing, repeat until no change. | |
676 (and byte-optimize | |
677 (not (zerop off))))))) | |
678 ;; Patch PC into jumps | |
679 (let (bytes) | |
680 (while patchlist | |
681 (setq bytes (car patchlist)) | |
682 (cond ((atom (car bytes))) ; Tag | |
683 ((nth 1 bytes) ; Relative jump | |
684 (setcar bytes (+ (- (car (cdr (car bytes))) (car (car bytes))) | |
685 128))) | |
686 (t ; Absolute jump | |
687 (setq pc (car (cdr (car bytes)))) ; Pick PC from tag | |
688 (setcar (cdr bytes) (logand pc 255)) | |
689 (setcar bytes (lsh pc -8)))) | |
690 (setq patchlist (cdr patchlist)))) | |
691 (concat (nreverse bytes)))) | |
692 | |
693 | |
694 ;;; byte compiler messages | |
695 | |
696 (defconst byte-compile-current-form nil) | |
697 (defconst byte-compile-current-file nil) | |
698 | |
699 (defmacro byte-compile-log (format-string &rest args) | |
700 (list 'and | |
701 'byte-optimize | |
702 '(memq byte-optimize-log '(t source)) | |
703 (list 'let '((print-escape-newlines t) | |
704 (print-level 4) | |
705 (print-length 4)) | |
706 (list 'byte-compile-log-1 | |
707 (cons 'format | |
708 (cons format-string | |
709 (mapcar | |
710 '(lambda (x) | |
711 (if (symbolp x) (list 'prin1-to-string x) x)) | |
712 args))))))) | |
713 | |
714 (defconst byte-compile-last-warned-form nil) | |
715 | |
716 (defun byte-compile-log-1 (string) | |
717 (cond (noninteractive | |
718 (if (or byte-compile-current-file | |
719 (and byte-compile-last-warned-form | |
720 (not (eq byte-compile-current-form | |
721 byte-compile-last-warned-form)))) | |
722 (message (format "While compiling %s%s:" | |
723 (or byte-compile-current-form "toplevel forms") | |
724 (if byte-compile-current-file | |
725 (if (stringp byte-compile-current-file) | |
726 (concat " in file " byte-compile-current-file) | |
727 (concat " in buffer " | |
728 (buffer-name byte-compile-current-file))) | |
729 "")))) | |
730 (message " %s" string)) | |
731 (t | |
732 (save-excursion | |
733 (set-buffer (get-buffer-create "*Compile-Log*")) | |
734 (goto-char (point-max)) | |
735 (cond ((or byte-compile-current-file | |
736 (and byte-compile-last-warned-form | |
737 (not (eq byte-compile-current-form | |
738 byte-compile-last-warned-form)))) | |
739 (if byte-compile-current-file | |
740 (insert "\n\^L\n" (current-time-string) "\n")) | |
741 (insert "While compiling " | |
742 (if byte-compile-current-form | |
743 (format "%s" byte-compile-current-form) | |
744 "toplevel forms")) | |
745 (if byte-compile-current-file | |
746 (if (stringp byte-compile-current-file) | |
747 (insert " in file " byte-compile-current-file) | |
748 (insert " in buffer " | |
749 (buffer-name byte-compile-current-file)))) | |
750 (insert ":\n"))) | |
751 (insert " " string "\n")))) | |
752 (setq byte-compile-current-file nil | |
753 byte-compile-last-warned-form byte-compile-current-form)) | |
754 | |
755 (defun byte-compile-warn (format &rest args) | |
756 (setq format (apply 'format format args)) | |
757 (if byte-compile-error-on-warn | |
758 (error "%s" format) ; byte-compile-file catches and logs it | |
759 (byte-compile-log-1 (concat "** " format)) | |
760 (or noninteractive ; already written on stdout. | |
761 (message "Warning: %s" format)))) | |
762 | |
763 ;;; Used by make-obsolete. | |
764 (defun byte-compile-obsolete (form) | |
765 (let ((new (get (car form) 'byte-obsolete-info))) | |
766 (byte-compile-warn "%s is an obsolete function; %s" (car form) | |
767 (if (stringp (car new)) | |
768 (car new) | |
769 (format "use %s instead." (car new)))) | |
770 (funcall (or (cdr new) 'byte-compile-normal-call) form))) | |
771 | |
772 ;; Compiler options | |
773 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
774 ;; (defvar byte-compiler-valid-options |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
775 ;; '((optimize byte-optimize (t nil source byte) val) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
776 ;; (file-format byte-compile-compatibility (emacs18 emacs19) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
777 ;; (eq val 'emacs18)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
778 ;; ;; (new-bytecodes byte-compile-generate-emacs19-bytecodes (t nil) val) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
779 ;; (delete-errors byte-compile-delete-errors (t nil) val) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
780 ;; (verbose byte-compile-verbose (t nil) val) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
781 ;; (warnings byte-compile-warnings ((callargs redefine free-vars unresolved)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
782 ;; val))) |
757 | 783 |
784 ;; Inhibit v18/v19 selectors if the version is hardcoded. | |
785 ;; #### This should print a warning if the user tries to change something | |
786 ;; than can't be changed because the running compiler doesn't support it. | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
787 ;; (cond |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
788 ;; ((byte-compile-single-version) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
789 ;; (setcar (cdr (cdr (assq 'new-bytecodes byte-compiler-valid-options))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
790 ;; (list (byte-compile-version-cond |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
791 ;; byte-compile-generate-emacs19-bytecodes))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
792 ;; (setcar (cdr (cdr (assq 'file-format byte-compiler-valid-options))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
793 ;; (if (byte-compile-version-cond byte-compile-compatibility) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
794 ;; '(emacs18) '(emacs19))))) |
757 | 795 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
796 ;; (defun byte-compiler-options-handler (&rest args) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
797 ;; (let (key val desc choices) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
798 ;; (while args |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
799 ;; (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args)))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
800 ;; (error "Malformed byte-compiler option `%s'" (car args))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
801 ;; (setq key (car (car args)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
802 ;; val (car (cdr (car args))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
803 ;; desc (assq key byte-compiler-valid-options)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
804 ;; (or desc |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
805 ;; (error "Unknown byte-compiler option `%s'" key)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
806 ;; (setq choices (nth 2 desc)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
807 ;; (if (consp (car choices)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
808 ;; (let (this |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
809 ;; (handler 'cons) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
810 ;; (ret (and (memq (car val) '(+ -)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
811 ;; (copy-sequence (if (eq t (symbol-value (nth 1 desc))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
812 ;; choices |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
813 ;; (symbol-value (nth 1 desc))))))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
814 ;; (setq choices (car choices)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
815 ;; (while val |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
816 ;; (setq this (car val)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
817 ;; (cond ((memq this choices) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
818 ;; (setq ret (funcall handler this ret))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
819 ;; ((eq this '+) (setq handler 'cons)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
820 ;; ((eq this '-) (setq handler 'delq)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
821 ;; ((error "`%s' only accepts %s" key choices))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
822 ;; (setq val (cdr val))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
823 ;; (set (nth 1 desc) ret)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
824 ;; (or (memq val choices) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
825 ;; (error "`%s' must be one of `%s'" key choices)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
826 ;; (set (nth 1 desc) (eval (nth 3 desc)))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
827 ;; (setq args (cdr args))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
828 ;; nil)) |
757 | 829 |
830 ;;; sanity-checking arglists | |
831 | |
832 (defun byte-compile-fdefinition (name macro-p) | |
833 (let* ((list (if macro-p | |
834 byte-compile-macro-environment | |
835 byte-compile-function-environment)) | |
836 (env (cdr (assq name list)))) | |
837 (or env | |
838 (let ((fn name)) | |
839 (while (and (symbolp fn) | |
840 (fboundp fn) | |
841 (or (symbolp (symbol-function fn)) | |
842 (consp (symbol-function fn)) | |
843 (and (not macro-p) | |
844 (compiled-function-p (symbol-function fn))))) | |
845 (setq fn (symbol-function fn))) | |
846 (if (and (not macro-p) (compiled-function-p fn)) | |
847 fn | |
848 (and (consp fn) | |
849 (if (eq 'macro (car fn)) | |
850 (cdr fn) | |
851 (if macro-p | |
852 nil | |
853 (if (eq 'autoload (car fn)) | |
854 nil | |
855 fn))))))))) | |
856 | |
857 (defun byte-compile-arglist-signature (arglist) | |
858 (let ((args 0) | |
859 opts | |
860 restp) | |
861 (while arglist | |
862 (cond ((eq (car arglist) '&optional) | |
863 (or opts (setq opts 0))) | |
864 ((eq (car arglist) '&rest) | |
865 (if (cdr arglist) | |
866 (setq restp t | |
867 arglist nil))) | |
868 (t | |
869 (if opts | |
870 (setq opts (1+ opts)) | |
871 (setq args (1+ args))))) | |
872 (setq arglist (cdr arglist))) | |
873 (cons args (if restp nil (if opts (+ args opts) args))))) | |
874 | |
875 | |
876 (defun byte-compile-arglist-signatures-congruent-p (old new) | |
877 (not (or | |
878 (> (car new) (car old)) ; requires more args now | |
879 (and (null (cdr old)) ; tooks rest-args, doesn't any more | |
880 (cdr new)) | |
881 (and (cdr new) (cdr old) ; can't take as many args now | |
882 (< (cdr new) (cdr old))) | |
883 ))) | |
884 | |
885 (defun byte-compile-arglist-signature-string (signature) | |
886 (cond ((null (cdr signature)) | |
887 (format "%d+" (car signature))) | |
888 ((= (car signature) (cdr signature)) | |
889 (format "%d" (car signature))) | |
890 (t (format "%d-%d" (car signature) (cdr signature))))) | |
891 | |
892 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
893 ;; Warn if the form is calling a function with the wrong number of arguments. |
757 | 894 (defun byte-compile-callargs-warn (form) |
895 (let* ((def (or (byte-compile-fdefinition (car form) nil) | |
896 (byte-compile-fdefinition (car form) t))) | |
897 (sig (and def (byte-compile-arglist-signature | |
898 (if (eq 'lambda (car-safe def)) | |
899 (nth 1 def) | |
900 (aref def 0))))) | |
901 (ncall (length (cdr form)))) | |
902 (if sig | |
903 (if (or (< ncall (car sig)) | |
904 (and (cdr sig) (> ncall (cdr sig)))) | |
905 (byte-compile-warn | |
906 "%s called with %d argument%s, but %s %s" | |
907 (car form) ncall | |
908 (if (= 1 ncall) "" "s") | |
909 (if (< ncall (car sig)) | |
910 "requires" | |
911 "accepts only") | |
912 (byte-compile-arglist-signature-string sig))) | |
913 (or (fboundp (car form)) ; might be a subr or autoload. | |
914 (eq (car form) byte-compile-current-form) ; ## this doesn't work with recursion. | |
915 ;; It's a currently-undefined function. Remember number of args in call. | |
916 (let ((cons (assq (car form) byte-compile-unresolved-functions)) | |
917 (n (length (cdr form)))) | |
918 (if cons | |
919 (or (memq n (cdr cons)) | |
920 (setcdr cons (cons n (cdr cons)))) | |
921 (setq byte-compile-unresolved-functions | |
922 (cons (list (car form) n) | |
923 byte-compile-unresolved-functions)))))))) | |
924 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
925 ;; Warn if the function or macro is being redefined with a different |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
926 ;; number of arguments. |
757 | 927 (defun byte-compile-arglist-warn (form macrop) |
928 (let ((old (byte-compile-fdefinition (nth 1 form) macrop))) | |
929 (if old | |
930 (let ((sig1 (byte-compile-arglist-signature | |
931 (if (eq 'lambda (car-safe old)) | |
932 (nth 1 old) | |
933 (aref old 0)))) | |
934 (sig2 (byte-compile-arglist-signature (nth 2 form)))) | |
935 (or (byte-compile-arglist-signatures-congruent-p sig1 sig2) | |
936 (byte-compile-warn "%s %s used to take %s %s, now takes %s" | |
937 (if (eq (car form) 'defun) "function" "macro") | |
938 (nth 1 form) | |
939 (byte-compile-arglist-signature-string sig1) | |
940 (if (equal sig1 '(1 . 1)) "argument" "arguments") | |
941 (byte-compile-arglist-signature-string sig2)))) | |
942 ;; This is the first definition. See if previous calls are compatible. | |
943 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions)) | |
944 nums sig min max) | |
945 (if calls | |
946 (progn | |
947 (setq sig (byte-compile-arglist-signature (nth 2 form)) | |
948 nums (sort (copy-sequence (cdr calls)) (function <)) | |
949 min (car nums) | |
950 max (car (nreverse nums))) | |
951 (if (or (< min (car sig)) | |
952 (and (cdr sig) (> max (cdr sig)))) | |
953 (byte-compile-warn | |
954 "%s being defined to take %s%s, but was previously called with %s" | |
955 (nth 1 form) | |
956 (byte-compile-arglist-signature-string sig) | |
957 (if (equal sig '(1 . 1)) " arg" " args") | |
958 (byte-compile-arglist-signature-string (cons min max)))) | |
959 | |
960 (setq byte-compile-unresolved-functions | |
961 (delq calls byte-compile-unresolved-functions))))) | |
962 ))) | |
963 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
964 ;; If we have compiled any calls to functions which are not known to be |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
965 ;; defined, issue a warning enumerating them. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
966 ;; `unresolved' in the list `byte-compile-warnings' disables this. |
757 | 967 (defun byte-compile-warn-about-unresolved-functions () |
968 (if (memq 'unresolved byte-compile-warnings) | |
969 (let ((byte-compile-current-form "the end of the data")) | |
970 (if (cdr byte-compile-unresolved-functions) | |
971 (let* ((str "The following functions are not known to be defined: ") | |
972 (L (length str)) | |
973 (rest (reverse byte-compile-unresolved-functions)) | |
974 s) | |
975 (while rest | |
976 (setq s (symbol-name (car (car rest))) | |
977 L (+ L (length s) 2) | |
978 rest (cdr rest)) | |
979 (if (< L (1- fill-column)) | |
980 (setq str (concat str " " s (and rest ","))) | |
981 (setq str (concat str "\n " s (and rest ",")) | |
982 L (+ (length s) 4)))) | |
983 (byte-compile-warn "%s" str)) | |
984 (if byte-compile-unresolved-functions | |
985 (byte-compile-warn "the function %s is not known to be defined." | |
986 (car (car byte-compile-unresolved-functions))))))) | |
987 nil) | |
988 | |
989 | |
990 (defmacro byte-compile-constp (form) | |
991 ;; Returns non-nil if FORM is a constant. | |
992 (` (cond ((consp (, form)) (eq (car (, form)) 'quote)) | |
993 ((not (symbolp (, form)))) | |
994 ((memq (, form) '(nil t)))))) | |
995 | |
996 (defmacro byte-compile-close-variables (&rest body) | |
997 (cons 'let | |
998 (cons '(;; | |
999 ;; Close over these variables to encapsulate the | |
1000 ;; compilation state | |
1001 ;; | |
1002 (byte-compile-macro-environment | |
1003 ;; Copy it because the compiler may patch into the | |
1004 ;; macroenvironment. | |
1005 (copy-alist byte-compile-initial-macro-environment)) | |
1006 (byte-compile-function-environment nil) | |
1007 (byte-compile-bound-variables nil) | |
1008 (byte-compile-free-references nil) | |
1009 (byte-compile-free-assignments nil) | |
1010 ;; | |
1011 ;; Close over these variables so that `byte-compiler-options' | |
1012 ;; can change them on a per-file basis. | |
1013 ;; | |
1014 (byte-compile-verbose byte-compile-verbose) | |
1015 (byte-optimize byte-optimize) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1016 ;; (byte-compile-generate-emacs19-bytecodes |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1017 ;; byte-compile-generate-emacs19-bytecodes) |
757 | 1018 (byte-compile-warnings (if (eq byte-compile-warnings t) |
1019 byte-compile-warning-types | |
1020 byte-compile-warnings)) | |
1021 ) | |
1022 body))) | |
1023 | |
1024 (defvar byte-compile-warnings-point-max) | |
1025 (defmacro displaying-byte-compile-warnings (&rest body) | |
1026 (list 'let | |
1027 '((byte-compile-warnings-point-max | |
1028 (if (boundp 'byte-compile-warnings-point-max) | |
1029 byte-compile-warnings-point-max | |
1030 (save-excursion | |
1031 (set-buffer (get-buffer-create "*Compile-Log*")) | |
1032 (point-max))))) | |
1033 (list 'unwind-protect (cons 'progn body) | |
1034 '(save-excursion | |
1035 ;; If there were compilation warnings, display them. | |
1036 (set-buffer "*Compile-Log*") | |
1037 (if (= byte-compile-warnings-point-max (point-max)) | |
1038 nil | |
1039 (select-window | |
1040 (prog1 (selected-window) | |
1041 (select-window (display-buffer (current-buffer))) | |
1042 (goto-char byte-compile-warnings-point-max) | |
1043 (recenter 1)))))))) | |
1044 | |
1045 | |
1046 (defun byte-recompile-directory (directory &optional arg) | |
1047 "Recompile every `.el' file in DIRECTORY that needs recompilation. | |
1048 This is if a `.elc' file exists but is older than the `.el' file. | |
1049 | |
1050 If the `.elc' file does not exist, normally the `.el' file is *not* compiled. | |
1051 But a prefix argument (optional second arg) means ask user, | |
1052 for each such `.el' file, whether to compile it." | |
1053 (interactive "DByte recompile directory: \nP") | |
1054 (save-some-buffers) | |
1055 (set-buffer-modified-p (buffer-modified-p)) ;Update the mode line. | |
1056 (setq directory (expand-file-name directory)) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1057 (let ((files (directory-files directory nil emacs-lisp-file-regexp)) |
757 | 1058 (count 0) |
1059 source dest) | |
1060 (while files | |
1061 (if (and (not (auto-save-file-name-p (car files))) | |
1062 (setq source (expand-file-name (car files) directory)) | |
1063 (setq dest (byte-compile-dest-file source)) | |
1064 (if (file-exists-p dest) | |
1065 (file-newer-than-file-p source dest) | |
1066 (and arg (y-or-n-p (concat "Compile " source "? "))))) | |
1067 (progn (byte-compile-file source) | |
1068 (setq count (1+ count)))) | |
1069 (setq files (cdr files))) | |
1070 (message "Done (Total of %d file%s compiled)" | |
1071 count (if (= count 1) "" "s")))) | |
1072 | |
1073 (defun byte-compile-file (filename &optional load) | |
1074 "Compile a file of Lisp code named FILENAME into a file of byte code. | |
1075 The output file's name is made by appending `c' to the end of FILENAME. | |
1076 With prefix arg (noninteractively: 2nd arg), load the file after compiling." | |
1077 ;; (interactive "fByte compile file: \nP") | |
1078 (interactive | |
1079 (let ((file buffer-file-name) | |
1080 (file-name nil) | |
1081 (file-dir nil)) | |
1082 (and file | |
1083 (eq (cdr (assq 'major-mode (buffer-local-variables))) | |
1084 'emacs-lisp-mode) | |
1085 (setq file-name (file-name-nondirectory file) | |
1086 file-dir (file-name-directory file))) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1087 (list (read-file-name (if current-prefix-arg |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1088 "Byte compile and load file: " |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1089 "Byte compile file: ") |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1090 file-dir file-name nil)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1091 current-prefix-arg)) |
757 | 1092 ;; Expand now so we get the current buffer's defaults |
1093 (setq filename (expand-file-name filename)) | |
1094 | |
1095 ;; If we're compiling a file that's in a buffer and is modified, offer | |
1096 ;; to save it first. | |
1097 (or noninteractive | |
1098 (let ((b (get-file-buffer (expand-file-name filename)))) | |
1099 (if (and b (buffer-modified-p b) | |
1100 (y-or-n-p (format "save buffer %s first? " (buffer-name b)))) | |
1101 (save-excursion (set-buffer b) (save-buffer))))) | |
1102 | |
1103 (if byte-compile-verbose | |
1104 (message "Compiling %s..." filename)) | |
1105 (let ((byte-compile-current-file (file-name-nondirectory filename)) | |
1106 target-file) | |
1107 (save-excursion | |
1108 (set-buffer (get-buffer-create " *Compiler Input*")) | |
1109 (erase-buffer) | |
1110 (insert-file-contents filename) | |
1111 ;; Run hooks including the uncompression hook. | |
1112 ;; If they change the file name, then change it for the output also. | |
1113 (let ((buffer-file-name filename)) | |
1114 (set-auto-mode) | |
1115 (setq filename buffer-file-name)) | |
1116 (kill-buffer (prog1 (current-buffer) | |
1117 (set-buffer (byte-compile-from-buffer (current-buffer))))) | |
1118 (goto-char (point-max)) | |
1119 (insert "\n") ; aaah, unix. | |
1120 (let ((vms-stmlf-recfm t)) | |
1121 (setq target-file (byte-compile-dest-file filename)) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1122 ;; (or byte-compile-overwrite-file |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1123 ;; (condition-case () |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1124 ;; (delete-file target-file) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1125 ;; (error nil))) |
757 | 1126 (if (file-writable-p target-file) |
1127 (let ((kanji-flag nil)) ; for nemacs, from Nakagawa Takayuki | |
1128 (write-region 1 (point-max) target-file)) | |
1129 ;; This is just to give a better error message than write-region | |
1130 (signal 'file-error (list "Opening output file" | |
1131 (if (file-exists-p target-file) | |
1132 "cannot overwrite file" | |
1133 "directory not writable or nonexistent") | |
1134 target-file))) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1135 ;; (or byte-compile-overwrite-file |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1136 ;; (condition-case () |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1137 ;; (set-file-modes target-file (file-modes filename)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1138 ;; (error nil))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1139 ) |
757 | 1140 (kill-buffer (current-buffer))) |
1141 (if (and byte-compile-generate-call-tree | |
1142 (or (eq t byte-compile-generate-call-tree) | |
1143 (y-or-n-p (format "Report call tree for %s? " filename)))) | |
1144 (save-excursion | |
1145 (byte-compile-report-call-tree filename))) | |
1146 (if load | |
1147 (load target-file))) | |
1148 t) | |
1149 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1150 ;;(defun byte-compile-and-load-file (&optional filename) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1151 ;; "Compile a file of Lisp code named FILENAME into a file of byte code, |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1152 ;;and then load it. The output file's name is made by appending \"c\" to |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1153 ;;the end of FILENAME." |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1154 ;; (interactive) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1155 ;; (if filename ; I don't get it, (interactive-p) doesn't always work |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1156 ;; (byte-compile-file filename t) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1157 ;; (let ((current-prefix-arg '(4))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1158 ;; (call-interactively 'byte-compile-file)))) |
757 | 1159 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1160 ;;(defun byte-compile-buffer (&optional buffer) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1161 ;; "Byte-compile and evaluate contents of BUFFER (default: the current buffer)." |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1162 ;; (interactive "bByte compile buffer: ") |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1163 ;; (setq buffer (if buffer (get-buffer buffer) (current-buffer))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1164 ;; (message "Compiling %s..." (buffer-name buffer)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1165 ;; (let* ((filename (or (buffer-file-name buffer) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1166 ;; (concat "#<buffer " (buffer-name buffer) ">"))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1167 ;; (byte-compile-current-file buffer)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1168 ;; (byte-compile-from-buffer buffer t)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1169 ;; (message "Compiling %s...done" (buffer-name buffer)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1170 ;; t) |
757 | 1171 |
1172 ;;; compiling a single function | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1173 (defun compile-defun (&optional arg) |
757 | 1174 "Compile and evaluate the current top-level form. |
1175 Print the result in the minibuffer. | |
1176 With argument, insert value in current buffer after the form." | |
1177 (interactive "P") | |
1178 (save-excursion | |
1179 (end-of-defun) | |
1180 (beginning-of-defun) | |
1181 (let* ((byte-compile-current-file nil) | |
1182 (byte-compile-last-warned-form 'nothing) | |
1183 (value (eval (byte-compile-sexp (read (current-buffer)))))) | |
1184 (cond (arg | |
1185 (message "Compiling from buffer... done.") | |
1186 (prin1 value (current-buffer)) | |
1187 (insert "\n")) | |
1188 ((message "%s" (prin1-to-string value))))))) | |
1189 | |
1190 | |
1191 (defun byte-compile-from-buffer (inbuffer &optional eval) | |
1192 ;; buffer --> output-buffer, or buffer --> eval form, return nil | |
1193 (let (outbuffer) | |
1194 (let (;; Prevent truncation of flonums and lists as we read and print them | |
1195 (float-output-format "%20e") | |
1196 (case-fold-search nil) | |
1197 (print-length nil) | |
1198 ;; Simulate entry to byte-compile-top-level | |
1199 (byte-compile-constants nil) | |
1200 (byte-compile-variables nil) | |
1201 (byte-compile-tag-number 0) | |
1202 (byte-compile-depth 0) | |
1203 (byte-compile-maxdepth 0) | |
1204 (byte-compile-output nil) | |
1205 ;; #### This is bound in b-c-close-variables. | |
1206 ;;(byte-compile-warnings (if (eq byte-compile-warnings t) | |
1207 ;; byte-compile-warning-types | |
1208 ;; byte-compile-warnings)) | |
1209 ) | |
1210 (byte-compile-close-variables | |
1211 (save-excursion | |
1212 (setq outbuffer | |
1213 (set-buffer (get-buffer-create " *Compiler Output*"))) | |
1214 (erase-buffer) | |
1215 ;; (emacs-lisp-mode) | |
1216 (setq case-fold-search nil)) | |
1217 (displaying-byte-compile-warnings | |
1218 (save-excursion | |
1219 (set-buffer inbuffer) | |
1220 (goto-char 1) | |
1221 (while (progn | |
1222 (while (progn (skip-chars-forward " \t\n\^l") | |
1223 (looking-at ";")) | |
1224 (forward-line 1)) | |
1225 (not (eobp))) | |
1226 (byte-compile-file-form (read inbuffer))) | |
1227 ;; Compile pending forms at end of file. | |
1228 (byte-compile-flush-pending) | |
1229 (and (not eval) (byte-compile-insert-header)) | |
1230 (byte-compile-warn-about-unresolved-functions) | |
1231 ;; always do this? When calling multiple files, it would be useful | |
1232 ;; to delay this warning until all have been compiled. | |
1233 (setq byte-compile-unresolved-functions nil))) | |
1234 (save-excursion | |
1235 (set-buffer outbuffer) | |
1236 (goto-char (point-min))))) | |
1237 (if (not eval) | |
1238 outbuffer | |
1239 (while (condition-case nil | |
1240 (progn (setq form (read outbuffer)) | |
1241 t) | |
1242 (end-of-file nil)) | |
1243 (eval form)) | |
1244 (kill-buffer outbuffer) | |
1245 nil))) | |
1246 | |
1247 (defun byte-compile-insert-header () | |
1248 (save-excursion | |
1249 (set-buffer outbuffer) | |
1250 (goto-char 1) | |
1251 (insert ";;; compiled by " (user-login-name) "@" (system-name) " on " | |
1252 (current-time-string) "\n;;; from file " filename "\n") | |
1253 (insert ";;; emacs version " emacs-version ".\n") | |
1254 (insert ";;; bytecomp version " byte-compile-version "\n;;; " | |
1255 (cond | |
1256 ((eq byte-optimize 'source) "source-level optimization only") | |
1257 ((eq byte-optimize 'byte) "byte-level optimization only") | |
1258 (byte-optimize "optimization is on") | |
1259 (t "optimization is off")) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1260 (if (byte-compile-version-cond byte-compile-compatibility) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1261 "; compiled with Emacs 18 compatibility.\n" |
757 | 1262 ".\n")) |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1263 (if (byte-compile-version-cond byte-compile-compatibility) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1264 (insert ";;; this file uses opcodes which do not exist in Emacs 18.\n" |
757 | 1265 ;; Have to check if emacs-version is bound so that this works |
1266 ;; in files loaded early in loadup.el. | |
1267 "\n(if (and (boundp 'emacs-version)\n" | |
1268 "\t (or (and (boundp 'epoch::version) epoch::version)\n" | |
1269 "\t (string-lessp emacs-version \"19\")))\n" | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1270 " (error \"This file was compiled for Emacs 19\"))\n" |
757 | 1271 )) |
1272 )) | |
1273 | |
1274 | |
1275 (defun byte-compile-output-file-form (form) | |
1276 ;; writes the given form to the output buffer, being careful of docstrings | |
1277 ;; in defun, defmacro, defvar, defconst and autoload because make-docfile is | |
1278 ;; so amazingly stupid. | |
1279 ;; fset's are output directly by byte-compile-file-form-defmumble; it does | |
1280 ;; not pay to first build the fset in defmumble and then parse it here. | |
1281 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload)) | |
1282 (stringp (nth 3 form))) | |
1283 (byte-compile-output-docform '("\n(" 3 ")") form) | |
1284 (let ((print-escape-newlines t) | |
1285 (print-readably t)) | |
1286 (princ "\n" outbuffer) | |
1287 (prin1 form outbuffer) | |
1288 nil))) | |
1289 | |
1290 (defun byte-compile-output-docform (info form) | |
1291 ;; Print a form with a doc string. INFO is (prefix doc-index postfix). | |
1292 (set-buffer | |
1293 (prog1 (current-buffer) | |
1294 (set-buffer outbuffer) | |
1295 (insert (car info)) | |
1296 (let ((docl (nthcdr (nth 1 info) form)) | |
1297 (print-escape-newlines t) | |
1298 (print-readably t)) | |
1299 (prin1 (car form) outbuffer) | |
1300 (while (setq form (cdr form)) | |
1301 (insert " ") | |
1302 (if (eq form docl) | |
1303 (let ((print-escape-newlines nil)) | |
1304 (goto-char (prog1 (1+ (point)) | |
1305 (prin1 (car form) outbuffer))) | |
1306 (insert "\\\n") | |
1307 (goto-char (point-max))) | |
1308 (prin1 (car form) outbuffer)))) | |
1309 (insert (nth 2 info)))) | |
1310 nil) | |
1311 | |
1312 (defun byte-compile-keep-pending (form &optional handler) | |
1313 (if (memq byte-optimize '(t source)) | |
1314 (setq form (byte-optimize-form form t))) | |
1315 (if handler | |
1316 (let ((for-effect t)) | |
1317 ;; To avoid consing up monstrously large forms at load time, we split | |
1318 ;; the output regularly. | |
1319 (and (eq (car-safe form) 'fset) (nthcdr 300 byte-compile-output) | |
1320 (byte-compile-flush-pending)) | |
1321 (funcall handler form) | |
1322 (if for-effect | |
1323 (byte-compile-discard))) | |
1324 (byte-compile-form form t)) | |
1325 nil) | |
1326 | |
1327 (defun byte-compile-flush-pending () | |
1328 (if byte-compile-output | |
1329 (let ((form (byte-compile-out-toplevel t 'file))) | |
1330 (cond ((eq (car-safe form) 'progn) | |
1331 (mapcar 'byte-compile-output-file-form (cdr form))) | |
1332 (form | |
1333 (byte-compile-output-file-form form))) | |
1334 (setq byte-compile-constants nil | |
1335 byte-compile-variables nil | |
1336 byte-compile-depth 0 | |
1337 byte-compile-maxdepth 0 | |
1338 byte-compile-output nil)))) | |
1339 | |
1340 (defun byte-compile-file-form (form) | |
1341 (let ((byte-compile-current-form nil) ; close over this for warnings. | |
1342 handler) | |
1343 (cond | |
1344 ((not (consp form)) | |
1345 (byte-compile-keep-pending form)) | |
1346 ((and (symbolp (car form)) | |
1347 (setq handler (get (car form) 'byte-hunk-handler))) | |
1348 (cond ((setq form (funcall handler form)) | |
1349 (byte-compile-flush-pending) | |
1350 (byte-compile-output-file-form form)))) | |
1351 ((eq form (setq form (macroexpand form byte-compile-macro-environment))) | |
1352 (byte-compile-keep-pending form)) | |
1353 (t | |
1354 (byte-compile-file-form form))))) | |
1355 | |
1356 ;; Functions and variables with doc strings must be output separately, | |
1357 ;; so make-docfile can recognise them. Most other things can be output | |
1358 ;; as byte-code. | |
1359 | |
1360 (put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst) | |
1361 (defun byte-compile-file-form-defsubst (form) | |
1362 (cond ((assq (nth 1 form) byte-compile-unresolved-functions) | |
1363 (setq byte-compile-current-form (nth 1 form)) | |
1364 (byte-compile-warn "defsubst %s was used before it was defined" | |
1365 (nth 1 form)))) | |
1366 (byte-compile-file-form | |
1367 (macroexpand form byte-compile-macro-environment)) | |
1368 ;; Return nil so the form is not output twice. | |
1369 nil) | |
1370 | |
1371 (put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload) | |
1372 (defun byte-compile-file-form-autoload (form) | |
1373 (and (let ((form form)) | |
1374 (while (if (setq form (cdr form)) (byte-compile-constp (car form)))) | |
1375 (null form)) ;Constants only | |
1376 (eval (nth 5 form)) ;Macro | |
1377 (eval form)) ;Define the autoload. | |
1378 (if (stringp (nth 3 form)) | |
1379 form | |
1380 ;; No doc string, so we can compile this as a normal form. | |
1381 (byte-compile-keep-pending form 'byte-compile-normal-call))) | |
1382 | |
1383 (put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar) | |
1384 (put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar) | |
1385 (defun byte-compile-file-form-defvar (form) | |
1386 (if (null (nth 3 form)) | |
1387 ;; Since there is no doc string, we can compile this as a normal form, | |
1388 ;; and not do a file-boundary. | |
1389 (byte-compile-keep-pending form) | |
1390 (if (memq 'free-vars byte-compile-warnings) | |
1391 (setq byte-compile-bound-variables | |
1392 (cons (nth 1 form) byte-compile-bound-variables))) | |
1393 (cond ((consp (nth 2 form)) | |
1394 (setq form (copy-sequence form)) | |
1395 (setcar (cdr (cdr form)) | |
1396 (byte-compile-top-level (nth 2 form) nil 'file)))) | |
1397 form)) | |
1398 | |
1399 (put 'require 'byte-hunk-handler 'byte-compile-file-form-eval-boundary) | |
1400 (defun byte-compile-file-form-eval-boundary (form) | |
1401 (eval form) | |
1402 (byte-compile-keep-pending form 'byte-compile-normal-call)) | |
1403 | |
1404 (put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn) | |
1405 (put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn) | |
1406 (put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn) | |
1407 (defun byte-compile-file-form-progn (form) | |
1408 (mapcar 'byte-compile-file-form (cdr form)) | |
1409 ;; Return nil so the forms are not output twice. | |
1410 nil) | |
1411 | |
1412 ;; This handler is not necessary, but it makes the output from dont-compile | |
1413 ;; and similar macros cleaner. | |
1414 (put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval) | |
1415 (defun byte-compile-file-form-eval (form) | |
1416 (if (eq (car-safe (nth 1 form)) 'quote) | |
1417 (nth 1 (nth 1 form)) | |
1418 (byte-compile-keep-pending form))) | |
1419 | |
1420 (put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun) | |
1421 (defun byte-compile-file-form-defun (form) | |
1422 (byte-compile-file-form-defmumble form nil)) | |
1423 | |
1424 (put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro) | |
1425 (defun byte-compile-file-form-defmacro (form) | |
1426 (byte-compile-file-form-defmumble form t)) | |
1427 | |
1428 (defun byte-compile-file-form-defmumble (form macrop) | |
1429 (let* ((name (car (cdr form))) | |
1430 (this-kind (if macrop 'byte-compile-macro-environment | |
1431 'byte-compile-function-environment)) | |
1432 (that-kind (if macrop 'byte-compile-function-environment | |
1433 'byte-compile-macro-environment)) | |
1434 (this-one (assq name (symbol-value this-kind))) | |
1435 (that-one (assq name (symbol-value that-kind))) | |
1436 (byte-compile-free-references nil) | |
1437 (byte-compile-free-assignments nil)) | |
1438 | |
1439 ;; When a function or macro is defined, add it to the call tree so that | |
1440 ;; we can tell when functions are not used. | |
1441 (if byte-compile-generate-call-tree | |
1442 (or (assq name byte-compile-call-tree) | |
1443 (setq byte-compile-call-tree | |
1444 (cons (list name nil nil) byte-compile-call-tree)))) | |
1445 | |
1446 (setq byte-compile-current-form name) ; for warnings | |
1447 (if (memq 'redefine byte-compile-warnings) | |
1448 (byte-compile-arglist-warn form macrop)) | |
1449 (if byte-compile-verbose | |
1450 (message "Compiling %s (%s)..." (or filename "") (nth 1 form))) | |
1451 (cond (that-one | |
1452 (if (and (memq 'redefine byte-compile-warnings) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1453 ;; don't warn when compiling the stubs in byte-run... |
757 | 1454 (not (assq (nth 1 form) |
1455 byte-compile-initial-macro-environment))) | |
1456 (byte-compile-warn | |
1457 "%s defined multiple times, as both function and macro" | |
1458 (nth 1 form))) | |
1459 (setcdr that-one nil)) | |
1460 (this-one | |
1461 (if (and (memq 'redefine byte-compile-warnings) | |
1462 ;; hack: don't warn when compiling the magic internal | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1463 ;; byte-compiler macros in byte-run.el... |
757 | 1464 (not (assq (nth 1 form) |
1465 byte-compile-initial-macro-environment))) | |
1466 (byte-compile-warn "%s %s defined multiple times in this file" | |
1467 (if macrop "macro" "function") | |
1468 (nth 1 form)))) | |
1469 ((and (fboundp name) | |
1470 (eq (car-safe (symbol-function name)) | |
1471 (if macrop 'lambda 'macro))) | |
1472 (if (memq 'redefine byte-compile-warnings) | |
1473 (byte-compile-warn "%s %s being redefined as a %s" | |
1474 (if macrop "function" "macro") | |
1475 (nth 1 form) | |
1476 (if macrop "macro" "function"))) | |
1477 ;; shadow existing definition | |
1478 (set this-kind | |
1479 (cons (cons name nil) (symbol-value this-kind)))) | |
1480 ) | |
1481 (let ((body (nthcdr 3 form))) | |
1482 (if (and (stringp (car body)) | |
1483 (symbolp (car-safe (cdr-safe body))) | |
1484 (car-safe (cdr-safe body)) | |
1485 (stringp (car-safe (cdr-safe (cdr-safe body))))) | |
1486 (byte-compile-warn "Probable `\"' without `\\' in doc string of %s" | |
1487 (nth 1 form)))) | |
1488 (let* ((new-one (byte-compile-lambda (cons 'lambda (nthcdr 2 form)))) | |
1489 (code (byte-compile-byte-code-maker new-one))) | |
1490 (if this-one | |
1491 (setcdr this-one new-one) | |
1492 (set this-kind | |
1493 (cons (cons name new-one) (symbol-value this-kind)))) | |
1494 (if (and (stringp (nth 3 form)) | |
1495 (eq 'quote (car-safe code)) | |
1496 (eq 'lambda (car-safe (nth 1 code)))) | |
1497 (cons (car form) | |
1498 (cons name (cdr (nth 1 code)))) | |
1499 (if (not (stringp (nth 3 form))) | |
1500 ;; No doc string to make-docfile; insert form in normal code. | |
1501 (byte-compile-keep-pending | |
1502 (list 'fset (list 'quote name) | |
1503 (cond ((not macrop) | |
1504 code) | |
1505 ((eq 'make-byte-code (car-safe code)) | |
1506 (list 'cons ''macro code)) | |
1507 ((list 'quote (if macrop | |
1508 (cons 'macro new-one) | |
1509 new-one))))) | |
1510 'byte-compile-two-args) | |
1511 ;; Output the form by hand, that's much simpler than having | |
1512 ;; b-c-output-file-form analyze the fset. | |
1513 (byte-compile-flush-pending) | |
1514 (princ "\n(fset '" outbuffer) | |
1515 (prin1 name outbuffer) | |
1516 (byte-compile-output-docform | |
1517 (cond ((atom code) | |
1518 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]"))) | |
1519 ((eq (car code) 'quote) | |
1520 (setq code new-one) | |
1521 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")"))) | |
1522 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")")))) | |
1523 (append code nil)) | |
1524 (princ ")" outbuffer) | |
1525 nil))))) | |
1526 | |
1527 | |
1528 (defun byte-compile (form) | |
1529 "If FORM is a symbol, byte-compile its function definition. | |
1530 If FORM is a lambda or a macro, byte-compile it as a function." | |
1531 (displaying-byte-compile-warnings | |
1532 (byte-compile-close-variables | |
1533 (let* ((fun (if (symbolp form) | |
1534 (and (fboundp form) (symbol-function form)) | |
1535 form)) | |
1536 (macro (eq (car-safe fun) 'macro))) | |
1537 (if macro | |
1538 (setq fun (cdr fun))) | |
1539 (cond ((eq (car-safe fun) 'lambda) | |
1540 (setq fun (if macro | |
1541 (cons 'macro (byte-compile-lambda fun)) | |
1542 (byte-compile-lambda fun))) | |
1543 (if (symbolp form) | |
1544 (fset form fun) | |
1545 fun))))))) | |
1546 | |
1547 (defun byte-compile-sexp (sexp) | |
1548 "Compile and return SEXP." | |
1549 (displaying-byte-compile-warnings | |
1550 (byte-compile-close-variables | |
1551 (byte-compile-top-level sexp)))) | |
1552 | |
1553 ;; Given a function made by byte-compile-lambda, make a form which produces it. | |
1554 (defun byte-compile-byte-code-maker (fun) | |
1555 (cond | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1556 ((byte-compile-version-cond byte-compile-compatibility) |
757 | 1557 ;; Return (quote (lambda ...)). |
1558 (list 'quote (byte-compile-byte-code-unmake fun))) | |
1559 ;; ## atom is faster than compiled-func-p. | |
1560 ((atom fun) ; compiled function. | |
1561 ;; generate-emacs19-bytecodes must be on, otherwise byte-compile-lambda | |
1562 ;; would have produced a lambda. | |
1563 fun) | |
1564 ;; b-c-lambda didn't produce a compiled-function, so it's either a trivial | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1565 ;; function, or this is Emacs 18, or generate-emacs19-bytecodes is off. |
757 | 1566 ((let (tmp) |
1567 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun)))) | |
1568 (null (cdr (memq tmp fun)))) | |
1569 ;; Generate a make-byte-code call. | |
1570 (let* ((interactive (assq 'interactive (cdr (cdr fun))))) | |
1571 (nconc (list 'make-byte-code | |
1572 (list 'quote (nth 1 fun)) ;arglist | |
1573 (nth 1 tmp) ;bytes | |
1574 (nth 2 tmp) ;consts | |
1575 (nth 3 tmp)) ;depth | |
1576 (cond ((stringp (nth 2 fun)) | |
1577 (list (nth 2 fun))) ;doc | |
1578 (interactive | |
1579 (list nil))) | |
1580 (cond (interactive | |
1581 (list (if (or (null (nth 1 interactive)) | |
1582 (stringp (nth 1 interactive))) | |
1583 (nth 1 interactive) | |
1584 ;; Interactive spec is a list or a variable | |
1585 ;; (if it is correct). | |
1586 (list 'quote (nth 1 interactive)))))))) | |
1587 ;; a non-compiled function (probably trivial) | |
1588 (list 'quote fun)))))) | |
1589 | |
1590 ;; Turn a function into an ordinary lambda. Needed for v18 files. | |
1591 (defun byte-compile-byte-code-unmake (function) | |
1592 (if (consp function) | |
1593 function;;It already is a lambda. | |
1594 (setq function (append function nil)) ; turn it into a list | |
1595 (nconc (list 'lambda (nth 0 function)) | |
1596 (and (nth 4 function) (list (nth 4 function))) | |
1597 (if (nthcdr 5 function) | |
1598 (list (cons 'interactive (if (nth 5 function) | |
1599 (nthcdr 5 function))))) | |
1600 (list (list 'byte-code | |
1601 (nth 1 function) (nth 2 function) | |
1602 (nth 3 function)))))) | |
1603 | |
1604 | |
1605 ;; Byte-compile a lambda-expression and return a valid function. | |
1606 ;; The value is usually a compiled function but may be the original | |
1607 ;; lambda-expression. | |
1608 (defun byte-compile-lambda (fun) | |
1609 (let* ((arglist (nth 1 fun)) | |
1610 (byte-compile-bound-variables | |
1611 (nconc (and (memq 'free-vars byte-compile-warnings) | |
1612 (delq '&rest (delq '&optional (copy-sequence arglist)))) | |
1613 byte-compile-bound-variables)) | |
1614 (body (cdr (cdr fun))) | |
1615 (doc (if (stringp (car body)) | |
1616 (prog1 (car body) | |
1617 (setq body (cdr body))))) | |
1618 (int (assq 'interactive body))) | |
1619 (cond (int | |
1620 ;; Skip (interactive) if it is in front (the most usual location). | |
1621 (if (eq int (car body)) | |
1622 (setq body (cdr body))) | |
1623 (cond ((cdr int) | |
1624 (if (cdr (cdr int)) | |
1625 (byte-compile-warn "malformed interactive spec: %s" | |
1626 (prin1-to-string int))) | |
1627 (setq int (list 'interactive (byte-compile-top-level | |
1628 (nth 1 int)))))))) | |
1629 (let ((compiled (byte-compile-top-level (cons 'progn body) nil 'lambda))) | |
1630 (if (and (eq 'byte-code (car-safe compiled)) | |
1631 (byte-compile-version-cond | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1632 byte-compile-compatibility)) |
757 | 1633 (apply 'make-byte-code |
1634 (append (list arglist) | |
1635 ;; byte-string, constants-vector, stack depth | |
1636 (cdr compiled) | |
1637 ;; optionally, the doc string. | |
1638 (if (or doc int) | |
1639 (list doc)) | |
1640 ;; optionally, the interactive spec. | |
1641 (if int | |
1642 (list (nth 1 int))))) | |
1643 (setq compiled | |
1644 (nconc (if int (list int)) | |
1645 (cond ((eq (car-safe compiled) 'progn) (cdr compiled)) | |
1646 (compiled (list compiled))))) | |
1647 (nconc (list 'lambda arglist) | |
1648 (if (or doc (stringp (car compiled))) | |
1649 (cons doc (cond (compiled) | |
1650 (body (list nil)))) | |
1651 compiled)))))) | |
1652 | |
1653 (defun byte-compile-constants-vector () | |
1654 ;; Builds the constants-vector from the current variables and constants. | |
1655 ;; This modifies the constants from (const . nil) to (const . offset). | |
1656 ;; To keep the byte-codes to look up the vector as short as possible: | |
1657 ;; First 6 elements are vars, as there are one-byte varref codes for those. | |
1658 ;; Next up to byte-constant-limit are constants, still with one-byte codes. | |
1659 ;; Next variables again, to get 2-byte codes for variable lookup. | |
1660 ;; The rest of the constants and variables need 3-byte byte-codes. | |
1661 (let* ((i -1) | |
1662 (rest (nreverse byte-compile-variables)) ; nreverse because the first | |
1663 (other (nreverse byte-compile-constants)) ; vars often are used most. | |
1664 ret tmp | |
1665 (limits '(5 ; Use the 1-byte varref codes, | |
1666 63 ; 1-constlim ; 1-byte byte-constant codes, | |
1667 255 ; 2-byte varref codes, | |
1668 65535)) ; 3-byte codes for the rest. | |
1669 limit) | |
1670 (while (or rest other) | |
1671 (setq limit (car limits)) | |
1672 (while (and rest (not (eq i limit))) | |
1673 (if (setq tmp (assq (car (car rest)) ret)) | |
1674 (setcdr (car rest) (cdr tmp)) | |
1675 (setcdr (car rest) (setq i (1+ i))) | |
1676 (setq ret (cons (car rest) ret))) | |
1677 (setq rest (cdr rest))) | |
1678 (setq limits (cdr limits) | |
1679 rest (prog1 other | |
1680 (setq other rest)))) | |
1681 (apply 'vector (nreverse (mapcar 'car ret))))) | |
1682 | |
1683 ;; Given an expression FORM, compile it and return an equivalent byte-code | |
1684 ;; expression (a call to the function byte-code). | |
1685 (defun byte-compile-top-level (form &optional for-effect output-type) | |
1686 ;; OUTPUT-TYPE advises about how form is expected to be used: | |
1687 ;; 'eval or nil -> a single form, | |
1688 ;; 'progn or t -> a list of forms, | |
1689 ;; 'lambda -> body of a lambda, | |
1690 ;; 'file -> used at file-level. | |
1691 (let ((byte-compile-constants nil) | |
1692 (byte-compile-variables nil) | |
1693 (byte-compile-tag-number 0) | |
1694 (byte-compile-depth 0) | |
1695 (byte-compile-maxdepth 0) | |
1696 (byte-compile-output nil)) | |
1697 (if (memq byte-optimize '(t source)) | |
1698 (setq form (byte-optimize-form form for-effect))) | |
1699 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form)))) | |
1700 (setq form (nth 1 form))) | |
1701 (if (and (eq 'byte-code (car-safe form)) | |
1702 (not (memq byte-optimize '(t byte))) | |
1703 (stringp (nth 1 form)) (vectorp (nth 2 form)) | |
1704 (natnump (nth 3 form))) | |
1705 form | |
1706 (byte-compile-form form for-effect) | |
1707 (byte-compile-out-toplevel for-effect output-type)))) | |
1708 | |
1709 (defun byte-compile-out-toplevel (&optional for-effect output-type) | |
1710 (if for-effect | |
1711 ;; The stack is empty. Push a value to be returned from (byte-code ..). | |
1712 (if (eq (car (car byte-compile-output)) 'byte-discard) | |
1713 (setq byte-compile-output (cdr byte-compile-output)) | |
1714 (byte-compile-push-constant | |
1715 ;; Push any constant - preferably one which already is used, and | |
1716 ;; a number or symbol - ie not some big sequence. The return value | |
1717 ;; isn't returned, but it would be a shame if some textually large | |
1718 ;; constant was not optimized away because we chose to return it. | |
1719 (and (not (assq nil byte-compile-constants)) ; Nil is often there. | |
1720 (let ((tmp (reverse byte-compile-constants))) | |
1721 (while (and tmp (not (or (symbolp (car (car tmp))) | |
1722 (numberp (car (car tmp)))))) | |
1723 (setq tmp (cdr tmp))) | |
1724 (car (car tmp))))))) | |
1725 (byte-compile-out 'byte-return 0) | |
1726 (setq byte-compile-output (nreverse byte-compile-output)) | |
1727 (if (memq byte-optimize '(t byte)) | |
1728 (setq byte-compile-output | |
1729 (byte-optimize-lapcode byte-compile-output for-effect))) | |
1730 | |
1731 ;; Decompile trivial functions: | |
1732 ;; only constants and variables, or a single funcall except in lambdas. | |
1733 ;; Except for Lisp_Compiled objects, forms like (foo "hi") | |
1734 ;; are still quicker than (byte-code "..." [foo "hi"] 2). | |
1735 ;; Note that even (quote foo) must be parsed just as any subr by the | |
1736 ;; interpreter, so quote should be compiled into byte-code in some contexts. | |
1737 ;; What to leave uncompiled: | |
1738 ;; lambda -> a single atom. | |
1739 ;; eval -> atom, quote or (function atom atom atom) | |
1740 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom) | |
1741 ;; file -> as progn, but takes both quotes and atoms, and longer forms. | |
1742 (let (rest | |
1743 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall. | |
1744 tmp body) | |
1745 (cond | |
1746 ;; #### This should be split out into byte-compile-nontrivial-function-p. | |
1747 ((or (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output) | |
1748 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit. | |
1749 (not (setq tmp (assq 'byte-return byte-compile-output))) | |
1750 (progn | |
1751 (setq rest (nreverse | |
1752 (cdr (memq tmp (reverse byte-compile-output))))) | |
1753 (while (cond | |
1754 ((memq (car (car rest)) '(byte-varref byte-constant)) | |
1755 (setq tmp (car (cdr (car rest)))) | |
1756 (if (if (eq (car (car rest)) 'byte-constant) | |
1757 (or (consp tmp) | |
1758 (and (symbolp tmp) | |
1759 (not (memq tmp '(nil t)))))) | |
1760 (if maycall | |
1761 (setq body (cons (list 'quote tmp) body))) | |
1762 (setq body (cons tmp body)))) | |
1763 ((and maycall | |
1764 ;; Allow a funcall if at most one atom follows it. | |
1765 (null (nthcdr 3 rest)) | |
1766 (setq tmp (get (car (car rest)) 'byte-opcode-invert)) | |
1767 (or (null (cdr rest)) | |
1768 (and (memq output-type '(file progn t)) | |
1769 (cdr (cdr rest)) | |
1770 (eq (car (nth 1 rest)) 'byte-discard) | |
1771 (progn (setq rest (cdr rest)) t)))) | |
1772 (setq maycall nil) ; Only allow one real function call. | |
1773 (setq body (nreverse body)) | |
1774 (setq body (list | |
1775 (if (and (eq tmp 'funcall) | |
1776 (eq (car-safe (car body)) 'quote)) | |
1777 (cons (nth 1 (car body)) (cdr body)) | |
1778 (cons tmp body)))) | |
1779 (or (eq output-type 'file) | |
1780 (not (delq nil (mapcar 'consp (cdr (car body)))))))) | |
1781 (setq rest (cdr rest))) | |
1782 rest) | |
1783 (and (consp (car body)) (eq output-type 'lambda))) | |
1784 (let ((byte-compile-vector (byte-compile-constants-vector))) | |
1785 (list 'byte-code (byte-compile-lapcode byte-compile-output) | |
1786 byte-compile-vector byte-compile-maxdepth))) | |
1787 ;; it's a trivial function | |
1788 ((cdr body) (cons 'progn (nreverse body))) | |
1789 ((car body))))) | |
1790 | |
1791 ;; Given BODY, compile it and return a new body. | |
1792 (defun byte-compile-top-level-body (body &optional for-effect) | |
1793 (setq body (byte-compile-top-level (cons 'progn body) for-effect t)) | |
1794 (cond ((eq (car-safe body) 'progn) | |
1795 (cdr body)) | |
1796 (body | |
1797 (list body)))) | |
1798 | |
1799 ;; This is the recursive entry point for compiling each subform of an | |
1800 ;; expression. | |
1801 ;; If for-effect is non-nil, byte-compile-form will output a byte-discard | |
1802 ;; before terminating (ie no value will be left on the stack). | |
1803 ;; A byte-compile handler may, when for-effect is non-nil, choose output code | |
1804 ;; which does not leave a value on the stack, and then set for-effect to nil | |
1805 ;; (to prevent byte-compile-form from outputting the byte-discard). | |
1806 ;; If a handler wants to call another handler, it should do so via | |
1807 ;; byte-compile-form, or take extreme care to handle for-effect correctly. | |
1808 ;; (Use byte-compile-form-do-effect to reset the for-effect flag too.) | |
1809 ;; | |
1810 (defun byte-compile-form (form &optional for-effect) | |
1811 (setq form (macroexpand form byte-compile-macro-environment)) | |
1812 (cond ((not (consp form)) | |
1813 (cond ((or (not (symbolp form)) (memq form '(nil t))) | |
1814 (byte-compile-constant form)) | |
1815 ((and for-effect byte-compile-delete-errors) | |
1816 (setq for-effect nil)) | |
1817 (t (byte-compile-variable-ref 'byte-varref form)))) | |
1818 ((symbolp (car form)) | |
1819 (let* ((fn (car form)) | |
1820 (handler (get fn 'byte-compile))) | |
1821 (if (and handler | |
1822 (or (byte-compile-version-cond | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1823 byte-compile-compatibility) |
757 | 1824 (not (get (get fn 'byte-opcode) 'emacs19-opcode)))) |
1825 (funcall handler form) | |
1826 (if (memq 'callargs byte-compile-warnings) | |
1827 (byte-compile-callargs-warn form)) | |
1828 (byte-compile-normal-call form)))) | |
1829 ((and (or (compiled-function-p (car form)) | |
1830 (eq (car-safe (car form)) 'lambda)) | |
1831 ;; if the form comes out the same way it went in, that's | |
1832 ;; because it was malformed, and we couldn't unfold it. | |
1833 (not (eq form (setq form (byte-compile-unfold-lambda form))))) | |
1834 (byte-compile-form form for-effect) | |
1835 (setq for-effect nil)) | |
1836 ((byte-compile-normal-call form))) | |
1837 (if for-effect | |
1838 (byte-compile-discard))) | |
1839 | |
1840 (defun byte-compile-normal-call (form) | |
1841 (if byte-compile-generate-call-tree | |
1842 (byte-compile-annotate-call-tree form)) | |
1843 (byte-compile-push-constant (car form)) | |
1844 (mapcar 'byte-compile-form (cdr form)) ; wasteful, but faster. | |
1845 (byte-compile-out 'byte-call (length (cdr form)))) | |
1846 | |
1847 (defun byte-compile-variable-ref (base-op var) | |
1848 (if (or (not (symbolp var)) (memq var '(nil t))) | |
1849 (byte-compile-warn (if (eq base-op 'byte-varbind) | |
1850 "Attempt to let-bind %s %s" | |
1851 "Variable reference to %s %s") | |
1852 (if (symbolp var) "constant" "nonvariable") | |
1853 (prin1-to-string var)) | |
1854 (if (memq 'free-vars byte-compile-warnings) | |
1855 (if (eq base-op 'byte-varbind) | |
1856 (setq byte-compile-bound-variables | |
1857 (cons var byte-compile-bound-variables)) | |
1858 (or (boundp var) | |
1859 (memq var byte-compile-bound-variables) | |
1860 (if (eq base-op 'byte-varset) | |
1861 (or (memq var byte-compile-free-assignments) | |
1862 (progn | |
1863 (byte-compile-warn "assignment to free variable %s" var) | |
1864 (setq byte-compile-free-assignments | |
1865 (cons var byte-compile-free-assignments)))) | |
1866 (or (memq var byte-compile-free-references) | |
1867 (progn | |
1868 (byte-compile-warn "reference to free variable %s" var) | |
1869 (setq byte-compile-free-references | |
1870 (cons var byte-compile-free-references))))))))) | |
1871 (let ((tmp (assq var byte-compile-variables))) | |
1872 (or tmp | |
1873 (setq tmp (list var) | |
1874 byte-compile-variables (cons tmp byte-compile-variables))) | |
1875 (byte-compile-out base-op tmp))) | |
1876 | |
1877 (defmacro byte-compile-get-constant (const) | |
1878 (` (or (if (stringp (, const)) | |
1879 (assoc (, const) byte-compile-constants) | |
1880 (assq (, const) byte-compile-constants)) | |
1881 (car (setq byte-compile-constants | |
1882 (cons (list (, const)) byte-compile-constants)))))) | |
1883 | |
1884 ;; Use this when the value of a form is a constant. This obeys for-effect. | |
1885 (defun byte-compile-constant (const) | |
1886 (if for-effect | |
1887 (setq for-effect nil) | |
1888 (byte-compile-out 'byte-constant (byte-compile-get-constant const)))) | |
1889 | |
1890 ;; Use this for a constant that is not the value of its containing form. | |
1891 ;; This ignores for-effect. | |
1892 (defun byte-compile-push-constant (const) | |
1893 (let ((for-effect nil)) | |
1894 (inline (byte-compile-constant const)))) | |
1895 | |
1896 | |
1897 ;; Compile those primitive ordinary functions | |
1898 ;; which have special byte codes just for speed. | |
1899 | |
1900 (defmacro byte-defop-compiler (function &optional compile-handler) | |
1901 ;; add a compiler-form for FUNCTION. | |
1902 ;; If function is a symbol, then the variable "byte-SYMBOL" must name | |
1903 ;; the opcode to be used. If function is a list, the first element | |
1904 ;; is the function and the second element is the bytecode-symbol. | |
1905 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or | |
1906 ;; may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2. | |
1907 ;; If it is nil, then the handler is "byte-compile-SYMBOL." | |
1908 (let (opcode) | |
1909 (if (symbolp function) | |
1910 (setq opcode (intern (concat "byte-" (symbol-name function)))) | |
1911 (setq opcode (car (cdr function)) | |
1912 function (car function))) | |
1913 (let ((fnform | |
1914 (list 'put (list 'quote function) ''byte-compile | |
1915 (list 'quote | |
1916 (or (cdr (assq compile-handler | |
1917 '((0 . byte-compile-no-args) | |
1918 (1 . byte-compile-one-arg) | |
1919 (2 . byte-compile-two-args) | |
1920 (3 . byte-compile-three-args) | |
1921 (0-1 . byte-compile-zero-or-one-arg) | |
1922 (1-2 . byte-compile-one-or-two-args) | |
1923 (2-3 . byte-compile-two-or-three-args) | |
1924 ))) | |
1925 compile-handler | |
1926 (intern (concat "byte-compile-" | |
1927 (symbol-name function)))))))) | |
1928 (if opcode | |
1929 (list 'progn fnform | |
1930 (list 'put (list 'quote function) | |
1931 ''byte-opcode (list 'quote opcode)) | |
1932 (list 'put (list 'quote opcode) | |
1933 ''byte-opcode-invert (list 'quote function))) | |
1934 fnform)))) | |
1935 | |
1936 (defmacro byte-defop-compiler19 (function &optional compile-handler) | |
1937 ;; Just like byte-defop-compiler, but defines an opcode that will only | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1938 ;; be used when byte-compile-compatibility is true. |
757 | 1939 (if (and (byte-compile-single-version) |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
1940 (not byte-compile-compatibility)) |
757 | 1941 nil |
1942 (list 'progn | |
1943 (list 'put | |
1944 (list 'quote | |
1945 (or (car (cdr-safe function)) | |
1946 (intern (concat "byte-" | |
1947 (symbol-name (or (car-safe function) function)))))) | |
1948 ''emacs19-opcode t) | |
1949 (list 'byte-defop-compiler function compile-handler)))) | |
1950 | |
1951 (defmacro byte-defop-compiler-1 (function &optional compile-handler) | |
1952 (list 'byte-defop-compiler (list function nil) compile-handler)) | |
1953 | |
1954 | |
1955 (put 'byte-call 'byte-opcode-invert 'funcall) | |
1956 (put 'byte-list1 'byte-opcode-invert 'list) | |
1957 (put 'byte-list2 'byte-opcode-invert 'list) | |
1958 (put 'byte-list3 'byte-opcode-invert 'list) | |
1959 (put 'byte-list4 'byte-opcode-invert 'list) | |
1960 (put 'byte-listN 'byte-opcode-invert 'list) | |
1961 (put 'byte-concat2 'byte-opcode-invert 'concat) | |
1962 (put 'byte-concat3 'byte-opcode-invert 'concat) | |
1963 (put 'byte-concat4 'byte-opcode-invert 'concat) | |
1964 (put 'byte-concatN 'byte-opcode-invert 'concat) | |
1965 (put 'byte-insertN 'byte-opcode-invert 'insert) | |
1966 | |
1967 (byte-defop-compiler (dot byte-point) 0) | |
1968 (byte-defop-compiler (dot-max byte-point-max) 0) | |
1969 (byte-defop-compiler (dot-min byte-point-min) 0) | |
1970 (byte-defop-compiler point 0) | |
1971 ;;(byte-defop-compiler mark 0) ;; obsolete | |
1972 (byte-defop-compiler point-max 0) | |
1973 (byte-defop-compiler point-min 0) | |
1974 (byte-defop-compiler following-char 0) | |
1975 (byte-defop-compiler preceding-char 0) | |
1976 (byte-defop-compiler current-column 0) | |
1977 (byte-defop-compiler eolp 0) | |
1978 (byte-defop-compiler eobp 0) | |
1979 (byte-defop-compiler bolp 0) | |
1980 (byte-defop-compiler bobp 0) | |
1981 (byte-defop-compiler current-buffer 0) | |
1982 ;;(byte-defop-compiler read-char 0) ;; obsolete | |
1983 (byte-defop-compiler interactive-p 0) | |
1984 (byte-defop-compiler19 widen 0) | |
1985 (byte-defop-compiler19 end-of-line 0-1) | |
1986 (byte-defop-compiler19 forward-char 0-1) | |
1987 (byte-defop-compiler19 forward-line 0-1) | |
1988 (byte-defop-compiler symbolp 1) | |
1989 (byte-defop-compiler consp 1) | |
1990 (byte-defop-compiler stringp 1) | |
1991 (byte-defop-compiler listp 1) | |
1992 (byte-defop-compiler not 1) | |
1993 (byte-defop-compiler (null byte-not) 1) | |
1994 (byte-defop-compiler car 1) | |
1995 (byte-defop-compiler cdr 1) | |
1996 (byte-defop-compiler length 1) | |
1997 (byte-defop-compiler symbol-value 1) | |
1998 (byte-defop-compiler symbol-function 1) | |
1999 (byte-defop-compiler (1+ byte-add1) 1) | |
2000 (byte-defop-compiler (1- byte-sub1) 1) | |
2001 (byte-defop-compiler goto-char 1) | |
2002 (byte-defop-compiler char-after 1) | |
2003 (byte-defop-compiler set-buffer 1) | |
2004 ;;(byte-defop-compiler set-mark 1) ;; obsolete | |
2005 (byte-defop-compiler19 forward-word 1) | |
2006 (byte-defop-compiler19 char-syntax 1) | |
2007 (byte-defop-compiler19 nreverse 1) | |
2008 (byte-defop-compiler19 car-safe 1) | |
2009 (byte-defop-compiler19 cdr-safe 1) | |
2010 (byte-defop-compiler19 numberp 1) | |
2011 (byte-defop-compiler19 integerp 1) | |
2012 (byte-defop-compiler19 skip-chars-forward 1-2) | |
2013 (byte-defop-compiler19 skip-chars-backward 1-2) | |
2014 (byte-defop-compiler (eql byte-eq) 2) | |
2015 (byte-defop-compiler eq 2) | |
2016 (byte-defop-compiler memq 2) | |
2017 (byte-defop-compiler cons 2) | |
2018 (byte-defop-compiler aref 2) | |
2019 (byte-defop-compiler set 2) | |
2020 (byte-defop-compiler (= byte-eqlsign) 2) | |
2021 (byte-defop-compiler (< byte-lss) 2) | |
2022 (byte-defop-compiler (> byte-gtr) 2) | |
2023 (byte-defop-compiler (<= byte-leq) 2) | |
2024 (byte-defop-compiler (>= byte-geq) 2) | |
2025 (byte-defop-compiler get 2) | |
2026 (byte-defop-compiler nth 2) | |
2027 (byte-defop-compiler substring 2-3) | |
2028 (byte-defop-compiler (move-marker byte-set-marker) 2-3) | |
2029 (byte-defop-compiler19 set-marker 2-3) | |
2030 (byte-defop-compiler19 match-beginning 1) | |
2031 (byte-defop-compiler19 match-end 1) | |
2032 (byte-defop-compiler19 upcase 1) | |
2033 (byte-defop-compiler19 downcase 1) | |
2034 (byte-defop-compiler19 string= 2) | |
2035 (byte-defop-compiler19 string< 2) | |
2036 (byte-defop-compiler (string-equal byte-string=) 2) | |
2037 (byte-defop-compiler (string-lessp byte-string<) 2) | |
2038 (byte-defop-compiler19 equal 2) | |
2039 (byte-defop-compiler19 nthcdr 2) | |
2040 (byte-defop-compiler19 elt 2) | |
2041 (byte-defop-compiler19 member 2) | |
2042 (byte-defop-compiler19 assq 2) | |
2043 (byte-defop-compiler (rplaca byte-setcar) 2) | |
2044 (byte-defop-compiler (rplacd byte-setcdr) 2) | |
2045 (byte-defop-compiler19 setcar 2) | |
2046 (byte-defop-compiler19 setcdr 2) | |
2047 (byte-defop-compiler19 buffer-substring 2) | |
2048 (byte-defop-compiler19 delete-region 2) | |
2049 (byte-defop-compiler19 narrow-to-region 2) | |
2050 (byte-defop-compiler (mod byte-rem) 2) | |
2051 (byte-defop-compiler19 (% byte-rem) 2) | |
2052 (byte-defop-compiler aset 3) | |
2053 | |
2054 (byte-defop-compiler max byte-compile-associative) | |
2055 (byte-defop-compiler min byte-compile-associative) | |
2056 (byte-defop-compiler (+ byte-plus) byte-compile-associative) | |
2057 (byte-defop-compiler19 (* byte-mult) byte-compile-associative) | |
2058 | |
2059 ;;####(byte-defop-compiler19 move-to-column 1) | |
2060 (byte-defop-compiler-1 interactive byte-compile-noop) | |
2061 | |
2062 | |
2063 (defun byte-compile-subr-wrong-args (form n) | |
2064 (byte-compile-warn "%s called with %d arg%s, but requires %s" | |
2065 (car form) (length (cdr form)) | |
2066 (if (= 1 (length (cdr form))) "" "s") n) | |
2067 ;; get run-time wrong-number-of-args error. | |
2068 (byte-compile-normal-call form)) | |
2069 | |
2070 (defun byte-compile-no-args (form) | |
2071 (if (not (= (length form) 1)) | |
2072 (byte-compile-subr-wrong-args form "none") | |
2073 (byte-compile-out (get (car form) 'byte-opcode) 0))) | |
2074 | |
2075 (defun byte-compile-one-arg (form) | |
2076 (if (not (= (length form) 2)) | |
2077 (byte-compile-subr-wrong-args form 1) | |
2078 (byte-compile-form (car (cdr form))) ;; Push the argument | |
2079 (byte-compile-out (get (car form) 'byte-opcode) 0))) | |
2080 | |
2081 (defun byte-compile-two-args (form) | |
2082 (if (not (= (length form) 3)) | |
2083 (byte-compile-subr-wrong-args form 2) | |
2084 (byte-compile-form (car (cdr form))) ;; Push the arguments | |
2085 (byte-compile-form (nth 2 form)) | |
2086 (byte-compile-out (get (car form) 'byte-opcode) 0))) | |
2087 | |
2088 (defun byte-compile-three-args (form) | |
2089 (if (not (= (length form) 4)) | |
2090 (byte-compile-subr-wrong-args form 3) | |
2091 (byte-compile-form (car (cdr form))) ;; Push the arguments | |
2092 (byte-compile-form (nth 2 form)) | |
2093 (byte-compile-form (nth 3 form)) | |
2094 (byte-compile-out (get (car form) 'byte-opcode) 0))) | |
2095 | |
2096 (defun byte-compile-zero-or-one-arg (form) | |
2097 (let ((len (length form))) | |
2098 (cond ((= len 1) (byte-compile-one-arg (append form '(nil)))) | |
2099 ((= len 2) (byte-compile-one-arg form)) | |
2100 (t (byte-compile-subr-wrong-args form "0-1"))))) | |
2101 | |
2102 (defun byte-compile-one-or-two-args (form) | |
2103 (let ((len (length form))) | |
2104 (cond ((= len 2) (byte-compile-two-args (append form '(nil)))) | |
2105 ((= len 3) (byte-compile-two-args form)) | |
2106 (t (byte-compile-subr-wrong-args form "1-2"))))) | |
2107 | |
2108 (defun byte-compile-two-or-three-args (form) | |
2109 (let ((len (length form))) | |
2110 (cond ((= len 3) (byte-compile-three-args (append form '(nil)))) | |
2111 ((= len 4) (byte-compile-three-args form)) | |
2112 (t (byte-compile-subr-wrong-args form "2-3"))))) | |
2113 | |
2114 (defun byte-compile-noop (form) | |
2115 (byte-compile-constant nil)) | |
2116 | |
2117 (defun byte-compile-discard () | |
2118 (byte-compile-out 'byte-discard 0)) | |
2119 | |
2120 | |
2121 ;; Compile a function that accepts one or more args and is right-associative. | |
2122 (defun byte-compile-associative (form) | |
2123 (if (cdr form) | |
2124 (let ((opcode (get (car form) 'byte-opcode))) | |
2125 ;; To compile all the args first may enable some optimizaions. | |
2126 (mapcar 'byte-compile-form (setq form (cdr form))) | |
2127 (while (setq form (cdr form)) | |
2128 (byte-compile-out opcode 0))) | |
2129 (byte-compile-constant (eval form)))) | |
2130 | |
2131 | |
2132 ;; more complicated compiler macros | |
2133 | |
2134 (byte-defop-compiler list) | |
2135 (byte-defop-compiler concat) | |
2136 (byte-defop-compiler fset) | |
2137 (byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to) | |
2138 (byte-defop-compiler indent-to) | |
2139 (byte-defop-compiler insert) | |
2140 (byte-defop-compiler-1 function byte-compile-function-form) | |
2141 (byte-defop-compiler-1 - byte-compile-minus) | |
2142 (byte-defop-compiler19 (/ byte-quo) byte-compile-quo) | |
2143 (byte-defop-compiler19 nconc) | |
2144 (byte-defop-compiler-1 beginning-of-line) | |
2145 | |
2146 (defun byte-compile-list (form) | |
2147 (let ((count (length (cdr form)))) | |
2148 (cond ((= count 0) | |
2149 (byte-compile-constant nil)) | |
2150 ((< count 5) | |
2151 (mapcar 'byte-compile-form (cdr form)) | |
2152 (byte-compile-out | |
2153 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0)) | |
2154 ((and (< count 256) (byte-compile-version-cond | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2155 byte-compile-compatibility)) |
757 | 2156 (mapcar 'byte-compile-form (cdr form)) |
2157 (byte-compile-out 'byte-listN count)) | |
2158 (t (byte-compile-normal-call form))))) | |
2159 | |
2160 (defun byte-compile-concat (form) | |
2161 (let ((count (length (cdr form)))) | |
2162 (cond ((and (< 1 count) (< count 5)) | |
2163 (mapcar 'byte-compile-form (cdr form)) | |
2164 (byte-compile-out | |
2165 (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2)) | |
2166 0)) | |
2167 ;; Concat of one arg is not a no-op if arg is not a string. | |
2168 ((= count 0) | |
2169 (byte-compile-form "")) | |
2170 ((and (< count 256) (byte-compile-version-cond | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2171 byte-compile-compatibility)) |
757 | 2172 (mapcar 'byte-compile-form (cdr form)) |
2173 (byte-compile-out 'byte-concatN count)) | |
2174 ((byte-compile-normal-call form))))) | |
2175 | |
2176 (defun byte-compile-minus (form) | |
2177 (if (null (setq form (cdr form))) | |
2178 (byte-compile-constant 0) | |
2179 (byte-compile-form (car form)) | |
2180 (if (cdr form) | |
2181 (while (setq form (cdr form)) | |
2182 (byte-compile-form (car form)) | |
2183 (byte-compile-out 'byte-diff 0)) | |
2184 (byte-compile-out 'byte-negate 0)))) | |
2185 | |
2186 (defun byte-compile-quo (form) | |
2187 (let ((len (length form))) | |
2188 (cond ((<= len 2) | |
2189 (byte-compile-subr-wrong-args form "2 or more")) | |
2190 (t | |
2191 (byte-compile-form (car (setq form (cdr form)))) | |
2192 (while (setq form (cdr form)) | |
2193 (byte-compile-form (car form)) | |
2194 (byte-compile-out 'byte-quo 0)))))) | |
2195 | |
2196 (defun byte-compile-nconc (form) | |
2197 (let ((len (length form))) | |
2198 (cond ((= len 1) | |
2199 (byte-compile-constant nil)) | |
2200 ((= len 2) | |
2201 ;; nconc of one arg is a noop, even if that arg isn't a list. | |
2202 (byte-compile-form (nth 1 form))) | |
2203 (t | |
2204 (byte-compile-form (car (setq form (cdr form)))) | |
2205 (while (setq form (cdr form)) | |
2206 (byte-compile-form (car form)) | |
2207 (byte-compile-out 'byte-nconc 0)))))) | |
2208 | |
2209 (defun byte-compile-fset (form) | |
2210 ;; warn about forms like (fset 'foo '(lambda () ...)) | |
2211 ;; (where the lambda expression is non-trivial...) | |
2212 (let ((fn (nth 2 form)) | |
2213 body) | |
2214 (if (and (eq (car-safe fn) 'quote) | |
2215 (eq (car-safe (setq fn (nth 1 fn))) 'lambda)) | |
2216 (progn | |
2217 (setq body (cdr (cdr fn))) | |
2218 (if (stringp (car body)) (setq body (cdr body))) | |
2219 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body))) | |
2220 (if (and (consp (car body)) | |
2221 (not (eq 'byte-code (car (car body))))) | |
2222 (byte-compile-warn | |
2223 "A quoted lambda form is the second argument of fset. This is probably | |
2224 not what you want, as that lambda cannot be compiled. Consider using | |
2225 the syntax (function (lambda (...) ...)) instead."))))) | |
2226 (byte-compile-two-args form)) | |
2227 | |
2228 (defun byte-compile-funarg (form) | |
2229 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..) | |
2230 ;; for cases where it's guarenteed that first arg will be used as a lambda. | |
2231 (byte-compile-normal-call | |
2232 (let ((fn (nth 1 form))) | |
2233 (if (and (eq (car-safe fn) 'quote) | |
2234 (eq (car-safe (nth 1 fn)) 'lambda)) | |
2235 (cons (car form) | |
2236 (cons (cons 'function (cdr fn)) | |
2237 (cdr (cdr form)))) | |
2238 form)))) | |
2239 | |
2240 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo). | |
2241 ;; Otherwise it will be incompatible with the interpreter, | |
2242 ;; and (funcall (function foo)) will lose with autoloads. | |
2243 | |
2244 (defun byte-compile-function-form (form) | |
2245 (byte-compile-constant | |
2246 (cond ((symbolp (nth 1 form)) | |
2247 (nth 1 form)) | |
2248 ;; If we're not allowed to use #[] syntax, then output a form like | |
2249 ;; '(lambda (..) (byte-code ..)) instead of a call to make-byte-code. | |
2250 ;; In this situation, calling make-byte-code at run-time will usually | |
2251 ;; be less efficient than processing a call to byte-code. | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2252 ((byte-compile-version-cond byte-compile-compatibility) |
757 | 2253 (byte-compile-byte-code-unmake (byte-compile-lambda (nth 1 form)))) |
2254 ((byte-compile-lambda (nth 1 form)))))) | |
2255 | |
2256 (defun byte-compile-indent-to (form) | |
2257 (let ((len (length form))) | |
2258 (cond ((= len 2) | |
2259 (byte-compile-form (car (cdr form))) | |
2260 (byte-compile-out 'byte-indent-to 0)) | |
2261 ((= len 3) | |
2262 ;; no opcode for 2-arg case. | |
2263 (byte-compile-normal-call form)) | |
2264 (t | |
2265 (byte-compile-subr-wrong-args form "1-2"))))) | |
2266 | |
2267 (defun byte-compile-insert (form) | |
2268 (cond ((null (cdr form)) | |
2269 (byte-compile-constant nil)) | |
2270 ((and (byte-compile-version-cond | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2271 byte-compile-compatibility) |
757 | 2272 (<= (length form) 256)) |
2273 (mapcar 'byte-compile-form (cdr form)) | |
2274 (if (cdr (cdr form)) | |
2275 (byte-compile-out 'byte-insertN (length (cdr form))) | |
2276 (byte-compile-out 'byte-insert 0))) | |
2277 ((memq t (mapcar 'consp (cdr (cdr form)))) | |
2278 (byte-compile-normal-call form)) | |
2279 ;; We can split it; there is no function call after inserting 1st arg. | |
2280 (t | |
2281 (while (setq form (cdr form)) | |
2282 (byte-compile-form (car form)) | |
2283 (byte-compile-out 'byte-insert 0) | |
2284 (if (cdr form) | |
2285 (byte-compile-discard)))))) | |
2286 | |
2287 (defun byte-compile-beginning-of-line (form) | |
2288 (if (not (byte-compile-constp (nth 1 form))) | |
2289 (byte-compile-normal-call form) | |
2290 (byte-compile-form | |
2291 (list 'forward-line | |
2292 (if (integerp (setq form (or (eval (nth 1 form)) 1))) | |
2293 (1- form) | |
2294 (byte-compile-warn "Non-numeric arg to beginning-of-line: %s" | |
2295 form) | |
2296 (list '1- (list 'quote form)))) | |
2297 t) | |
2298 (byte-compile-constant nil))) | |
2299 | |
2300 | |
2301 (byte-defop-compiler-1 setq) | |
2302 (byte-defop-compiler-1 setq-default) | |
2303 (byte-defop-compiler-1 quote) | |
2304 (byte-defop-compiler-1 quote-form) | |
2305 | |
2306 (defun byte-compile-setq (form) | |
2307 (let ((args (cdr form))) | |
2308 (if args | |
2309 (while args | |
2310 (byte-compile-form (car (cdr args))) | |
2311 (or for-effect (cdr (cdr args)) | |
2312 (byte-compile-out 'byte-dup 0)) | |
2313 (byte-compile-variable-ref 'byte-varset (car args)) | |
2314 (setq args (cdr (cdr args)))) | |
2315 ;; (setq), with no arguments. | |
2316 (byte-compile-form nil for-effect)) | |
2317 (setq for-effect nil))) | |
2318 | |
2319 (defun byte-compile-setq-default (form) | |
2320 (byte-compile-form | |
2321 (cons 'set-default (cons (list 'quote (nth 1 form)) | |
2322 (nthcdr 2 form))))) | |
2323 | |
2324 (defun byte-compile-quote (form) | |
2325 (byte-compile-constant (car (cdr form)))) | |
2326 | |
2327 (defun byte-compile-quote-form (form) | |
2328 (byte-compile-constant (byte-compile-top-level (nth 1 form)))) | |
2329 | |
2330 | |
2331 ;;; control structures | |
2332 | |
2333 (defun byte-compile-body (body &optional for-effect) | |
2334 (while (cdr body) | |
2335 (byte-compile-form (car body) t) | |
2336 (setq body (cdr body))) | |
2337 (byte-compile-form (car body) for-effect)) | |
2338 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2339 (defsubst byte-compile-body-do-effect (body) |
757 | 2340 (byte-compile-body body for-effect) |
2341 (setq for-effect nil)) | |
2342 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2343 (defsubst byte-compile-form-do-effect (form) |
757 | 2344 (byte-compile-form form for-effect) |
2345 (setq for-effect nil)) | |
2346 | |
2347 (byte-defop-compiler-1 inline byte-compile-progn) | |
2348 (byte-defop-compiler-1 progn) | |
2349 (byte-defop-compiler-1 prog1) | |
2350 (byte-defop-compiler-1 prog2) | |
2351 (byte-defop-compiler-1 if) | |
2352 (byte-defop-compiler-1 cond) | |
2353 (byte-defop-compiler-1 and) | |
2354 (byte-defop-compiler-1 or) | |
2355 (byte-defop-compiler-1 while) | |
2356 (byte-defop-compiler-1 funcall) | |
2357 (byte-defop-compiler-1 apply byte-compile-funarg) | |
2358 (byte-defop-compiler-1 mapcar byte-compile-funarg) | |
2359 (byte-defop-compiler-1 mapatoms byte-compile-funarg) | |
2360 (byte-defop-compiler-1 mapconcat byte-compile-funarg) | |
2361 (byte-defop-compiler-1 let) | |
2362 (byte-defop-compiler-1 let*) | |
2363 | |
2364 (defun byte-compile-progn (form) | |
2365 (byte-compile-body-do-effect (cdr form))) | |
2366 | |
2367 (defun byte-compile-prog1 (form) | |
2368 (byte-compile-form-do-effect (car (cdr form))) | |
2369 (byte-compile-body (cdr (cdr form)) t)) | |
2370 | |
2371 (defun byte-compile-prog2 (form) | |
2372 (byte-compile-form (nth 1 form) t) | |
2373 (byte-compile-form-do-effect (nth 2 form)) | |
2374 (byte-compile-body (cdr (cdr (cdr form))) t)) | |
2375 | |
2376 (defmacro byte-compile-goto-if (cond discard tag) | |
2377 (` (byte-compile-goto | |
2378 (if (, cond) | |
2379 (if (, discard) 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop) | |
2380 (if (, discard) 'byte-goto-if-nil 'byte-goto-if-nil-else-pop)) | |
2381 (, tag)))) | |
2382 | |
2383 (defun byte-compile-if (form) | |
2384 (byte-compile-form (car (cdr form))) | |
2385 (if (null (nthcdr 3 form)) | |
2386 ;; No else-forms | |
2387 (let ((donetag (byte-compile-make-tag))) | |
2388 (byte-compile-goto-if nil for-effect donetag) | |
2389 (byte-compile-form (nth 2 form) for-effect) | |
2390 (byte-compile-out-tag donetag)) | |
2391 (let ((donetag (byte-compile-make-tag)) (elsetag (byte-compile-make-tag))) | |
2392 (byte-compile-goto 'byte-goto-if-nil elsetag) | |
2393 (byte-compile-form (nth 2 form) for-effect) | |
2394 (byte-compile-goto 'byte-goto donetag) | |
2395 (byte-compile-out-tag elsetag) | |
2396 (byte-compile-body (cdr (cdr (cdr form))) for-effect) | |
2397 (byte-compile-out-tag donetag))) | |
2398 (setq for-effect nil)) | |
2399 | |
2400 (defun byte-compile-cond (clauses) | |
2401 (let ((donetag (byte-compile-make-tag)) | |
2402 nexttag clause) | |
2403 (while (setq clauses (cdr clauses)) | |
2404 (setq clause (car clauses)) | |
2405 (cond ((or (eq (car clause) t) | |
2406 (and (eq (car-safe (car clause)) 'quote) | |
2407 (car-safe (cdr-safe (car clause))))) | |
2408 ;; Unconditional clause | |
2409 (setq clause (cons t clause) | |
2410 clauses nil)) | |
2411 ((cdr clauses) | |
2412 (byte-compile-form (car clause)) | |
2413 (if (null (cdr clause)) | |
2414 ;; First clause is a singleton. | |
2415 (byte-compile-goto-if t for-effect donetag) | |
2416 (setq nexttag (byte-compile-make-tag)) | |
2417 (byte-compile-goto 'byte-goto-if-nil nexttag) | |
2418 (byte-compile-body (cdr clause) for-effect) | |
2419 (byte-compile-goto 'byte-goto donetag) | |
2420 (byte-compile-out-tag nexttag))))) | |
2421 ;; Last clause | |
2422 (and (cdr clause) (not (eq (car clause) t)) | |
2423 (progn (byte-compile-form (car clause)) | |
2424 (byte-compile-goto-if nil for-effect donetag) | |
2425 (setq clause (cdr clause)))) | |
2426 (byte-compile-body-do-effect clause) | |
2427 (byte-compile-out-tag donetag))) | |
2428 | |
2429 (defun byte-compile-and (form) | |
2430 (let ((failtag (byte-compile-make-tag)) | |
2431 (args (cdr form))) | |
2432 (if (null args) | |
2433 (byte-compile-form-do-effect t) | |
2434 (while (cdr args) | |
2435 (byte-compile-form (car args)) | |
2436 (byte-compile-goto-if nil for-effect failtag) | |
2437 (setq args (cdr args))) | |
2438 (byte-compile-form-do-effect (car args)) | |
2439 (byte-compile-out-tag failtag)))) | |
2440 | |
2441 (defun byte-compile-or (form) | |
2442 (let ((wintag (byte-compile-make-tag)) | |
2443 (args (cdr form))) | |
2444 (if (null args) | |
2445 (byte-compile-form-do-effect nil) | |
2446 (while (cdr args) | |
2447 (byte-compile-form (car args)) | |
2448 (byte-compile-goto-if t for-effect wintag) | |
2449 (setq args (cdr args))) | |
2450 (byte-compile-form-do-effect (car args)) | |
2451 (byte-compile-out-tag wintag)))) | |
2452 | |
2453 (defun byte-compile-while (form) | |
2454 (let ((endtag (byte-compile-make-tag)) | |
2455 (looptag (byte-compile-make-tag))) | |
2456 (byte-compile-out-tag looptag) | |
2457 (byte-compile-form (car (cdr form))) | |
2458 (byte-compile-goto-if nil for-effect endtag) | |
2459 (byte-compile-body (cdr (cdr form)) t) | |
2460 (byte-compile-goto 'byte-goto looptag) | |
2461 (byte-compile-out-tag endtag) | |
2462 (setq for-effect nil))) | |
2463 | |
2464 (defun byte-compile-funcall (form) | |
2465 (mapcar 'byte-compile-form (cdr form)) | |
2466 (byte-compile-out 'byte-call (length (cdr (cdr form))))) | |
2467 | |
2468 | |
2469 (defun byte-compile-let (form) | |
2470 ;; First compute the binding values in the old scope. | |
2471 (let ((varlist (car (cdr form)))) | |
2472 (while varlist | |
2473 (if (consp (car varlist)) | |
2474 (byte-compile-form (car (cdr (car varlist)))) | |
2475 (byte-compile-push-constant nil)) | |
2476 (setq varlist (cdr varlist)))) | |
2477 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope | |
2478 (varlist (reverse (car (cdr form))))) | |
2479 (while varlist | |
2480 (byte-compile-variable-ref 'byte-varbind (if (consp (car varlist)) | |
2481 (car (car varlist)) | |
2482 (car varlist))) | |
2483 (setq varlist (cdr varlist))) | |
2484 (byte-compile-body-do-effect (cdr (cdr form))) | |
2485 (byte-compile-out 'byte-unbind (length (car (cdr form)))))) | |
2486 | |
2487 (defun byte-compile-let* (form) | |
2488 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope | |
2489 (varlist (copy-sequence (car (cdr form))))) | |
2490 (while varlist | |
2491 (if (atom (car varlist)) | |
2492 (byte-compile-push-constant nil) | |
2493 (byte-compile-form (car (cdr (car varlist)))) | |
2494 (setcar varlist (car (car varlist)))) | |
2495 (byte-compile-variable-ref 'byte-varbind (car varlist)) | |
2496 (setq varlist (cdr varlist))) | |
2497 (byte-compile-body-do-effect (cdr (cdr form))) | |
2498 (byte-compile-out 'byte-unbind (length (car (cdr form)))))) | |
2499 | |
2500 | |
2501 (byte-defop-compiler-1 /= byte-compile-negated) | |
2502 (byte-defop-compiler-1 atom byte-compile-negated) | |
2503 (byte-defop-compiler-1 nlistp byte-compile-negated) | |
2504 | |
2505 (put '/= 'byte-compile-negated-op '=) | |
2506 (put 'atom 'byte-compile-negated-op 'consp) | |
2507 (put 'nlistp 'byte-compile-negated-op 'listp) | |
2508 | |
2509 (defun byte-compile-negated (form) | |
2510 (byte-compile-form-do-effect (byte-compile-negation-optimizer form))) | |
2511 | |
2512 ;; Even when optimization is off, /= is optimized to (not (= ...)). | |
2513 (defun byte-compile-negation-optimizer (form) | |
2514 ;; an optimizer for forms where <form1> is less efficient than (not <form2>) | |
2515 (list 'not | |
2516 (cons (or (get (car form) 'byte-compile-negated-op) | |
2517 (error | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2518 "Compiler error: `%s' has no `byte-compile-negated-op' property" |
757 | 2519 (car form))) |
2520 (cdr form)))) | |
2521 | |
2522 ;;; other tricky macro-like special-forms | |
2523 | |
2524 (byte-defop-compiler-1 catch) | |
2525 (byte-defop-compiler-1 unwind-protect) | |
2526 (byte-defop-compiler-1 condition-case) | |
2527 (byte-defop-compiler-1 save-excursion) | |
2528 (byte-defop-compiler-1 save-restriction) | |
2529 (byte-defop-compiler-1 save-window-excursion) | |
2530 (byte-defop-compiler-1 with-output-to-temp-buffer) | |
2531 | |
2532 (defun byte-compile-catch (form) | |
2533 (byte-compile-form (car (cdr form))) | |
2534 (byte-compile-push-constant | |
2535 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect)) | |
2536 (byte-compile-out 'byte-catch 0)) | |
2537 | |
2538 (defun byte-compile-unwind-protect (form) | |
2539 (byte-compile-push-constant | |
2540 (byte-compile-top-level-body (cdr (cdr form)) t)) | |
2541 (byte-compile-out 'byte-unwind-protect 0) | |
2542 (byte-compile-form-do-effect (car (cdr form))) | |
2543 (byte-compile-out 'byte-unbind 1)) | |
2544 | |
2545 (defun byte-compile-condition-case (form) | |
2546 (let* ((var (nth 1 form)) | |
2547 (byte-compile-bound-variables | |
2548 (if var (cons var byte-compile-bound-variables) | |
2549 byte-compile-bound-variables))) | |
2550 (or (symbolp var) | |
2551 (byte-compile-warn | |
2552 "%s is not a variable-name or nil (in condition-case)" var)) | |
2553 (byte-compile-push-constant var) | |
2554 (byte-compile-push-constant (byte-compile-top-level | |
2555 (nth 2 form) for-effect)) | |
2556 (let ((clauses (cdr (cdr (cdr form)))) | |
2557 compiled-clauses) | |
2558 (while clauses | |
2559 (let ((clause (car clauses))) | |
2560 (setq compiled-clauses | |
2561 (cons (cons (car clause) | |
2562 (byte-compile-top-level-body | |
2563 (cdr clause) for-effect)) | |
2564 compiled-clauses))) | |
2565 (setq clauses (cdr clauses))) | |
2566 (byte-compile-push-constant (nreverse compiled-clauses))) | |
2567 (byte-compile-out 'byte-condition-case 0))) | |
2568 | |
2569 | |
2570 (defun byte-compile-save-excursion (form) | |
2571 (byte-compile-out 'byte-save-excursion 0) | |
2572 (byte-compile-body-do-effect (cdr form)) | |
2573 (byte-compile-out 'byte-unbind 1)) | |
2574 | |
2575 (defun byte-compile-save-restriction (form) | |
2576 (byte-compile-out 'byte-save-restriction 0) | |
2577 (byte-compile-body-do-effect (cdr form)) | |
2578 (byte-compile-out 'byte-unbind 1)) | |
2579 | |
2580 (defun byte-compile-save-window-excursion (form) | |
2581 (byte-compile-push-constant | |
2582 (byte-compile-top-level-body (cdr form) for-effect)) | |
2583 (byte-compile-out 'byte-save-window-excursion 0)) | |
2584 | |
2585 (defun byte-compile-with-output-to-temp-buffer (form) | |
2586 (byte-compile-form (car (cdr form))) | |
2587 (byte-compile-out 'byte-temp-output-buffer-setup 0) | |
2588 (byte-compile-body (cdr (cdr form))) | |
2589 (byte-compile-out 'byte-temp-output-buffer-show 0)) | |
2590 | |
2591 | |
2592 ;;; top-level forms elsewhere | |
2593 | |
2594 (byte-defop-compiler-1 defun) | |
2595 (byte-defop-compiler-1 defmacro) | |
2596 (byte-defop-compiler-1 defvar) | |
2597 (byte-defop-compiler-1 defconst byte-compile-defvar) | |
2598 (byte-defop-compiler-1 autoload) | |
2599 (byte-defop-compiler-1 lambda byte-compile-lambda-form) | |
2600 | |
2601 (defun byte-compile-defun (form) | |
2602 ;; This is not used for file-level defuns with doc strings. | |
2603 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning. | |
2604 (list 'fset (list 'quote (nth 1 form)) | |
2605 (byte-compile-byte-code-maker | |
2606 (byte-compile-lambda (cons 'lambda (cdr (cdr form))))))) | |
2607 (byte-compile-discard) | |
2608 (byte-compile-constant (nth 1 form))) | |
2609 | |
2610 (defun byte-compile-defmacro (form) | |
2611 ;; This is not used for file-level defmacros with doc strings. | |
2612 (byte-compile-body-do-effect | |
2613 (list (list 'fset (list 'quote (nth 1 form)) | |
2614 (let ((code (byte-compile-byte-code-maker | |
2615 (byte-compile-lambda | |
2616 (cons 'lambda (cdr (cdr form))))))) | |
2617 (if (eq (car-safe code) 'make-byte-code) | |
2618 (list 'cons ''macro code) | |
2619 (list 'quote (cons 'macro (eval code)))))) | |
2620 (list 'quote (nth 1 form))))) | |
2621 | |
2622 (defun byte-compile-defvar (form) | |
2623 ;; This is not used for file-level defvar/consts with doc strings. | |
2624 (let ((var (nth 1 form)) | |
2625 (value (nth 2 form)) | |
2626 (string (nth 3 form))) | |
2627 (if (memq 'free-vars byte-compile-warnings) | |
2628 (setq byte-compile-bound-variables | |
2629 (cons var byte-compile-bound-variables))) | |
2630 (byte-compile-body-do-effect | |
2631 (list (if (cdr (cdr form)) | |
2632 (if (eq (car form) 'defconst) | |
2633 (list 'setq var value) | |
2634 (list 'or (list 'boundp (list 'quote var)) | |
2635 (list 'setq var value)))) | |
2636 (if string | |
2637 (list 'put (list 'quote var) ''variable-documentation string)) | |
2638 (list 'quote var))))) | |
2639 | |
2640 (defun byte-compile-autoload (form) | |
2641 (and (byte-compile-constp (nth 1 form)) | |
2642 (byte-compile-constp (nth 5 form)) | |
2643 (eval (nth 5 form)) ; macro-p | |
2644 (not (fboundp (eval (nth 1 form)))) | |
2645 (byte-compile-warn | |
2646 "The compiler ignores `autoload' except at top level. You should | |
2647 probably put the autoload of the macro `%s' at top-level." | |
2648 (eval (nth 1 form)))) | |
2649 (byte-compile-normal-call form)) | |
2650 | |
2651 ;; Lambda's in valid places are handled as special cases by various code. | |
2652 ;; The ones that remain are errors. | |
2653 (defun byte-compile-lambda-form (form) | |
2654 (error "`lambda' used as function name is invalid")) | |
2655 | |
2656 | |
2657 ;;; tags | |
2658 | |
2659 ;; Note: Most operations will strip off the 'TAG, but it speeds up | |
2660 ;; optimization to have the 'TAG as a part of the tag. | |
2661 ;; Tags will be (TAG . (tag-number . stack-depth)). | |
2662 (defun byte-compile-make-tag () | |
2663 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number)))) | |
2664 | |
2665 | |
2666 (defun byte-compile-out-tag (tag) | |
2667 (setq byte-compile-output (cons tag byte-compile-output)) | |
2668 (if (cdr (cdr tag)) | |
2669 (progn | |
2670 ;; ## remove this someday | |
2671 (and byte-compile-depth | |
2672 (not (= (cdr (cdr tag)) byte-compile-depth)) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2673 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag)))) |
757 | 2674 (setq byte-compile-depth (cdr (cdr tag)))) |
2675 (setcdr (cdr tag) byte-compile-depth))) | |
2676 | |
2677 (defun byte-compile-goto (opcode tag) | |
2678 (setq byte-compile-output (cons (cons opcode tag) byte-compile-output)) | |
2679 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops) | |
2680 (1- byte-compile-depth) | |
2681 byte-compile-depth)) | |
2682 (setq byte-compile-depth (and (not (eq opcode 'byte-goto)) | |
2683 (1- byte-compile-depth)))) | |
2684 | |
2685 (defun byte-compile-out (opcode offset) | |
2686 (setq byte-compile-output (cons (cons opcode offset) byte-compile-output)) | |
2687 (cond ((eq opcode 'byte-call) | |
2688 (setq byte-compile-depth (- byte-compile-depth offset))) | |
2689 ((eq opcode 'byte-return) | |
2690 ;; This is actually an unnecessary case, because there should be | |
2691 ;; no more opcodes behind byte-return. | |
2692 (setq byte-compile-depth nil)) | |
2693 (t | |
2694 (setq byte-compile-depth (+ byte-compile-depth | |
2695 (or (aref byte-stack+-info | |
2696 (symbol-value opcode)) | |
2697 (- (1- offset)))) | |
2698 byte-compile-maxdepth (max byte-compile-depth | |
2699 byte-compile-maxdepth)))) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2700 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow")) |
757 | 2701 ) |
2702 | |
2703 | |
2704 ;;; call tree stuff | |
2705 | |
2706 (defun byte-compile-annotate-call-tree (form) | |
2707 (let (entry) | |
2708 ;; annotate the current call | |
2709 (if (setq entry (assq (car form) byte-compile-call-tree)) | |
2710 (or (memq byte-compile-current-form (nth 1 entry)) ;callers | |
2711 (setcar (cdr entry) | |
2712 (cons byte-compile-current-form (nth 1 entry)))) | |
2713 (setq byte-compile-call-tree | |
2714 (cons (list (car form) (list byte-compile-current-form) nil) | |
2715 byte-compile-call-tree))) | |
2716 ;; annotate the current function | |
2717 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree)) | |
2718 (or (memq (car form) (nth 2 entry)) ;called | |
2719 (setcar (cdr (cdr entry)) | |
2720 (cons (car form) (nth 2 entry)))) | |
2721 (setq byte-compile-call-tree | |
2722 (cons (list byte-compile-current-form nil (list (car form))) | |
2723 byte-compile-call-tree))) | |
2724 )) | |
2725 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2726 ;; Renamed from byte-compile-report-call-tree |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2727 ;; to avoid interfering with completion of byte-compile-file. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2728 (defun display-call-tree (&optional filename) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2729 "Display a call graph of a specified file. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2730 This lists which functions have been called, what functions called |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2731 them, and what functions they call. The list includes all functions |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2732 whose definitions have been compiled in this Emacs session, as well as |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2733 all functions called by those functions. |
757 | 2734 |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2735 The call graph does not include macros, inline functions, or |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2736 primitives that the byte-code interpreter knows about directly \(eq, |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2737 cons, etc.\). |
757 | 2738 |
2739 The call tree also lists those functions which are not known to be called | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2740 \(that is, to which no calls have been compiled\), and which cannot be |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2741 invoked interactively." |
757 | 2742 (interactive) |
2743 (message "Generating call tree...") | |
2744 (with-output-to-temp-buffer "*Call-Tree*" | |
2745 (set-buffer "*Call-Tree*") | |
2746 (erase-buffer) | |
2747 (message "Generating call tree (sorting on %s)..." | |
2748 byte-compile-call-tree-sort) | |
2749 (insert "Call tree for " | |
2750 (cond ((null byte-compile-current-file) (or filename "???")) | |
2751 ((stringp byte-compile-current-file) | |
2752 byte-compile-current-file) | |
2753 (t (buffer-name byte-compile-current-file))) | |
2754 " sorted on " | |
2755 (prin1-to-string byte-compile-call-tree-sort) | |
2756 ":\n\n") | |
2757 (if byte-compile-call-tree-sort | |
2758 (setq byte-compile-call-tree | |
2759 (sort byte-compile-call-tree | |
2760 (cond ((eq byte-compile-call-tree-sort 'callers) | |
2761 (function (lambda (x y) (< (length (nth 1 x)) | |
2762 (length (nth 1 y)))))) | |
2763 ((eq byte-compile-call-tree-sort 'calls) | |
2764 (function (lambda (x y) (< (length (nth 2 x)) | |
2765 (length (nth 2 y)))))) | |
2766 ((eq byte-compile-call-tree-sort 'calls+callers) | |
2767 (function (lambda (x y) (< (+ (length (nth 1 x)) | |
2768 (length (nth 2 x))) | |
2769 (+ (length (nth 1 y)) | |
2770 (length (nth 2 y))))))) | |
2771 ((eq byte-compile-call-tree-sort 'name) | |
2772 (function (lambda (x y) (string< (car x) | |
2773 (car y))))) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2774 (t (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode" |
757 | 2775 byte-compile-call-tree-sort)))))) |
2776 (message "Generating call tree...") | |
2777 (let ((rest byte-compile-call-tree) | |
2778 (b (current-buffer)) | |
2779 f p | |
2780 callers calls) | |
2781 (while rest | |
2782 (prin1 (car (car rest)) b) | |
2783 (setq callers (nth 1 (car rest)) | |
2784 calls (nth 2 (car rest))) | |
2785 (insert "\t" | |
2786 (cond ((not (fboundp (setq f (car (car rest))))) | |
2787 (if (null f) | |
2788 " <top level>";; shouldn't insert nil then, actually -sk | |
2789 " <not defined>")) | |
2790 ((subrp (setq f (symbol-function f))) | |
2791 " <subr>") | |
2792 ((symbolp f) | |
2793 (format " ==> %s" f)) | |
2794 ((compiled-function-p f) | |
2795 "<compiled function>") | |
2796 ((not (consp f)) | |
2797 "<malformed function>") | |
2798 ((eq 'macro (car f)) | |
2799 (if (or (compiled-function-p (cdr f)) | |
2800 (assq 'byte-code (cdr (cdr (cdr f))))) | |
2801 " <compiled macro>" | |
2802 " <macro>")) | |
2803 ((assq 'byte-code (cdr (cdr f))) | |
2804 "<compiled lambda>") | |
2805 ((eq 'lambda (car f)) | |
2806 "<function>") | |
2807 (t "???")) | |
2808 (format " (%d callers + %d calls = %d)" | |
2809 ;; Does the optimizer eliminate common subexpressions?-sk | |
2810 (length callers) | |
2811 (length calls) | |
2812 (+ (length callers) (length calls))) | |
2813 "\n") | |
2814 (if callers | |
2815 (progn | |
2816 (insert " called by:\n") | |
2817 (setq p (point)) | |
2818 (insert " " (if (car callers) | |
2819 (mapconcat 'symbol-name callers ", ") | |
2820 "<top level>")) | |
2821 (let ((fill-prefix " ")) | |
2822 (fill-region-as-paragraph p (point))))) | |
2823 (if calls | |
2824 (progn | |
2825 (insert " calls:\n") | |
2826 (setq p (point)) | |
2827 (insert " " (mapconcat 'symbol-name calls ", ")) | |
2828 (let ((fill-prefix " ")) | |
2829 (fill-region-as-paragraph p (point))))) | |
2830 (insert "\n") | |
2831 (setq rest (cdr rest))) | |
2832 | |
2833 (message "Generating call tree...(finding uncalled functions...)") | |
2834 (setq rest byte-compile-call-tree) | |
2835 (let ((uncalled nil)) | |
2836 (while rest | |
2837 (or (nth 1 (car rest)) | |
2838 (null (setq f (car (car rest)))) | |
2839 (byte-compile-fdefinition f t) | |
2840 (commandp (byte-compile-fdefinition f nil)) | |
2841 (setq uncalled (cons f uncalled))) | |
2842 (setq rest (cdr rest))) | |
2843 (if uncalled | |
2844 (let ((fill-prefix " ")) | |
2845 (insert "Noninteractive functions not known to be called:\n ") | |
2846 (setq p (point)) | |
2847 (insert (mapconcat 'symbol-name (nreverse uncalled) ", ")) | |
2848 (fill-region-as-paragraph p (point))))) | |
2849 ) | |
2850 (message "Generating call tree...done.") | |
2851 )) | |
2852 | |
2853 | |
2854 ;;; by crl@newton.purdue.edu | |
2855 ;;; Only works noninteractively. | |
2856 (defun batch-byte-compile () | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2857 "Run `byte-compile-file' on the files remaining on the command line. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2858 Use this from the command line, with `-batch'; |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2859 it won't work in an interactive Emacs. |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2860 Each file is processed even if an error occurred previously. |
757 | 2861 For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\"" |
2862 ;; command-line-args-left is what is left of the command line (from startup.el) | |
2863 (defvar command-line-args-left) ;Avoid 'free variable' warning | |
2864 (if (not noninteractive) | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2865 (error "`batch-byte-compile' is to be used only with -batch")) |
757 | 2866 (let ((error nil)) |
2867 (while command-line-args-left | |
2868 (if (file-directory-p (expand-file-name (car command-line-args-left))) | |
2869 (let ((files (directory-files (car command-line-args-left))) | |
2870 source dest) | |
2871 (while files | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2872 (if (and (string-match emacs-lisp-file-regexp (car files)) |
757 | 2873 (not (auto-save-file-name-p (car files))) |
2874 (setq source (expand-file-name (car files) | |
2875 (car command-line-args-left))) | |
2876 (setq dest (byte-compile-dest-file source)) | |
2877 (file-exists-p dest) | |
2878 (file-newer-than-file-p source dest)) | |
2879 (if (null (batch-byte-compile-file source)) | |
2880 (setq error t))) | |
2881 (setq files (cdr files)))) | |
2882 (if (null (batch-byte-compile-file (car command-line-args-left))) | |
2883 (setq error t))) | |
2884 (setq command-line-args-left (cdr command-line-args-left))) | |
2885 (message "Done") | |
2886 (kill-emacs (if error 1 0)))) | |
2887 | |
2888 (defun batch-byte-compile-file (file) | |
2889 (condition-case err | |
2890 (progn (byte-compile-file file) t) | |
2891 (error | |
2892 (message (if (cdr err) | |
2893 ">>Error occurred processing %s: %s (%s)" | |
2894 ">>Error occurred processing %s: %s") | |
2895 file | |
2896 (get (car err) 'error-message) | |
2897 (prin1-to-string (cdr err))) | |
2898 nil))) | |
2899 | |
2900 | |
2901 (make-obsolete 'mod '%) | |
2902 (make-obsolete 'dot 'point) | |
2903 (make-obsolete 'dot-max 'point-max) | |
2904 (make-obsolete 'dot-min 'point-min) | |
2905 (make-obsolete 'dot-marker 'point-marker) | |
2906 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2907 (make-obsolete 'buffer-flush-undo 'buffer-disable-undo) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2908 (make-obsolete 'baud-rate "use the baud-rate variable instead") |
757 | 2909 |
2910 (provide 'byte-compile) | |
2911 | |
2912 | |
2913 ;;; report metering (see the hacks in bytecode.c) | |
2914 | |
784
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2915 (defun byte-compile-report-ops () |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2916 (defvar byte-code-meter) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2917 (with-output-to-temp-buffer "*Meter*" |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2918 (set-buffer "*Meter*") |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2919 (let ((i 0) n op off) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2920 (while (< i 256) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2921 (setq n (aref (aref byte-code-meter 0) i) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2922 off nil) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2923 (if t ;(not (zerop n)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2924 (progn |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2925 (setq op i) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2926 (setq off nil) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2927 (cond ((< op byte-nth) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2928 (setq off (logand op 7)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2929 (setq op (logand op 248))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2930 ((>= op byte-constant) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2931 (setq off (- op byte-constant) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2932 op byte-constant))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2933 (setq op (aref byte-code-vector op)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2934 (insert (format "%-4d" i)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2935 (insert (symbol-name op)) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2936 (if off (insert " [" (int-to-string off) "]")) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2937 (indent-to 40) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2938 (insert (int-to-string n) "\n"))) |
6d993c174c62
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
757
diff
changeset
|
2939 (setq i (1+ i)))))) |
757 | 2940 |
2941 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles | |
2942 ;; itself, compile some of its most used recursive functions (at load time). | |
2943 ;; | |
2944 (eval-when-compile | |
2945 (or (compiled-function-p (symbol-function 'byte-compile-form)) | |
2946 (assq 'byte-code (symbol-function 'byte-compile-form)) | |
2947 (let ((byte-optimize nil) ; do it fast | |
2948 (byte-compile-warnings nil)) | |
2949 (mapcar '(lambda (x) | |
2950 (or noninteractive (message "compiling %s..." x)) | |
2951 (byte-compile x) | |
2952 (or noninteractive (message "compiling %s...done" x))) | |
2953 '(byte-compile-normal-call | |
2954 byte-compile-form | |
2955 byte-compile-body | |
2956 ;; Inserted some more than necessary, to speed it up. | |
2957 byte-compile-top-level | |
2958 byte-compile-out-toplevel | |
2959 byte-compile-constant | |
2960 byte-compile-variable-ref)))) | |
2961 nil) |