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