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