Mercurial > hg > xemacs-beta
annotate lisp/bytecomp.el @ 4671:e9b88e997479
Deal with arglist newlines better, #'function-arglist, #'function-documentation
lisp/ChangeLog addition:
2009-08-10 Aidan Kehoe <kehoea@parhasard.net>
* help.el (function-arglist, function-documentation):
Deal more gracefully with tabs in the function arglist; in
function-documentation, when stripping the arglist, pay attention
to the fact that they may span multiple lines.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Mon, 10 Aug 2009 16:22:09 +0100 |
parents | 7757334005ae |
children | 8f1ee2d15784 |
rev | line source |
---|---|
428 | 1 ;;; bytecomp.el --- compilation of Lisp code into byte code. |
2 | |
3 ;;; Copyright (C) 1985-1987, 1991-1994 Free Software Foundation, Inc. | |
4 ;;; Copyright (C) 1996 Ben Wing. | |
5 | |
442 | 6 ;; Authors: Jamie Zawinski <jwz@jwz.org> |
428 | 7 ;; Hallvard Furuseth <hbf@ulrik.uio.no> |
442 | 8 ;; Ben Wing <ben@xemacs.org> |
9 ;; Martin Buchholz <martin@xemacs.org> | |
10 ;; Richard Stallman <rms@gnu.org> | |
11 ;; Keywords: internal lisp | |
12 | |
444 | 13 (defconst byte-compile-version "2.27 XEmacs; 2000-09-12.") |
428 | 14 |
15 ;; This file is part of XEmacs. | |
16 | |
17 ;; XEmacs is free software; you can redistribute it and/or modify it | |
18 ;; under the terms of the GNU General Public License as published by | |
19 ;; the Free Software Foundation; either version 2, or (at your option) | |
20 ;; any later version. | |
21 | |
22 ;; XEmacs is distributed in the hope that it will be useful, but | |
23 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 ;; General Public License for more details. | |
26 | |
27 ;; You should have received a copy of the GNU General Public License | |
28 ;; along with XEmacs; see the file COPYING. If not, write to the | |
29 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
30 ;; Boston, MA 02111-1307, USA. | |
31 | |
32 ;;; Synched up with: FSF 19.30. | |
33 | |
34 ;;; Commentary: | |
35 | |
36 ;; The Emacs Lisp byte compiler. This crunches lisp source into a | |
442 | 37 ;; sort of p-code (`bytecode') which takes up less space and can be |
38 ;; interpreted faster. First, the source code forms are converted to | |
39 ;; an intermediate form, `lapcode' [`LAP' == `Lisp Assembly Program'] | |
40 ;; which is much easier to manipulate than bytecode. Then the lapcode | |
41 ;; is converted to bytecode, which can be considered to be actual | |
42 ;; machine language. Optimizations can occur at either the source | |
43 ;; level or the lapcode level. | |
44 | |
45 ;; The user entry points are byte-compile-file, | |
428 | 46 ;; byte-recompile-directory and byte-compile-buffer. |
47 | |
48 ;;; Code: | |
49 | |
50 ;;; ======================================================================== | |
51 ;;; Entry points: | |
52 ;;; byte-recompile-directory, byte-compile-file, | |
53 ;;; batch-byte-compile, batch-byte-recompile-directory, | |
54 ;;; byte-compile, compile-defun, | |
55 ;;; display-call-tree | |
56 ;;; RMS says: | |
57 ;;; (byte-compile-buffer and byte-compile-and-load-file were turned off | |
58 ;;; because they are not terribly useful and get in the way of completion.) | |
59 ;;; But I'm leaving them. --ben | |
60 | |
61 ;;; This version of the byte compiler has the following improvements: | |
62 ;;; + optimization of compiled code: | |
63 ;;; - removal of unreachable code; | |
64 ;;; - removal of calls to side-effectless functions whose return-value | |
65 ;;; is unused; | |
66 ;;; - compile-time evaluation of safe constant forms, such as (consp nil) | |
67 ;;; and (ash 1 6); | |
68 ;;; - open-coding of literal lambdas; | |
69 ;;; - peephole optimization of emitted code; | |
70 ;;; - trivial functions are left uncompiled for speed. | |
71 ;;; + support for inline functions; | |
72 ;;; + compile-time evaluation of arbitrary expressions; | |
73 ;;; + compile-time warning messages for: | |
74 ;;; - functions being redefined with incompatible arglists; | |
75 ;;; - functions being redefined as macros, or vice-versa; | |
76 ;;; - functions or macros defined multiple times in the same file; | |
77 ;;; - functions being called with the incorrect number of arguments; | |
78 ;;; - functions being called which are not defined globally, in the | |
79 ;;; file, or as autoloads; | |
80 ;;; - assignment and reference of undeclared free variables; | |
81 ;;; - various syntax errors; | |
82 ;;; + correct compilation of nested defuns, defmacros, defvars and defsubsts; | |
83 ;;; + correct compilation of top-level uses of macros; | |
84 ;;; + the ability to generate a histogram of functions called. | |
85 | |
86 ;;; User customization variables: | |
87 ;;; | |
88 ;;; byte-compile-verbose Whether to report the function currently being | |
89 ;;; compiled in the minibuffer; | |
90 ;;; byte-optimize Whether to do optimizations; this may be | |
91 ;;; t, nil, 'source, or 'byte; | |
92 ;;; byte-optimize-log Whether to report (in excruciating detail) | |
93 ;;; exactly which optimizations have been made. | |
94 ;;; This may be t, nil, 'source, or 'byte; | |
95 ;;; byte-compile-error-on-warn Whether to stop compilation when a warning is | |
96 ;;; produced; | |
97 ;;; byte-compile-delete-errors Whether the optimizer may delete calls or | |
98 ;;; variable references that are side-effect-free | |
99 ;;; except that they may return an error. | |
100 ;;; byte-compile-generate-call-tree Whether to generate a histogram of | |
101 ;;; function calls. This can be useful for | |
102 ;;; finding unused functions, as well as simple | |
103 ;;; performance metering. | |
104 ;;; byte-compile-warnings List of warnings to issue, or t. May contain | |
105 ;;; 'free-vars (references to variables not in the | |
106 ;;; current lexical scope) | |
107 ;;; 'unused-vars (non-global variables bound but | |
108 ;;; not referenced) | |
109 ;;; 'unresolved (calls to unknown functions) | |
110 ;;; 'callargs (lambda calls with args that don't | |
111 ;;; match the lambda's definition) | |
112 ;;; 'subr-callargs (calls to subrs with args that | |
113 ;;; don't match the subr's definition) | |
114 ;;; 'redefine (function cell redefined from | |
115 ;;; a macro to a lambda or vice versa, | |
116 ;;; or redefined to take other args) | |
117 ;;; 'obsolete (obsolete variables and functions) | |
118 ;;; 'pedantic (references to Emacs-compatible | |
119 ;;; symbols) | |
120 ;;; byte-compile-emacs19-compatibility Whether the compiler should | |
121 ;;; generate .elc files which can be loaded into | |
122 ;;; generic emacs 19. | |
123 ;;; emacs-lisp-file-regexp Regexp for the extension of source-files; | |
444 | 124 ;;; see also the function `byte-compile-dest-file'. |
428 | 125 ;;; byte-compile-overwrite-file If nil, delete old .elc files before saving. |
126 ;;; | |
127 ;;; Most of the above parameters can also be set on a file-by-file basis; see | |
128 ;;; the documentation of the `byte-compiler-options' macro. | |
129 | |
130 ;;; New Features: | |
131 ;;; | |
132 ;;; o The form `defsubst' is just like `defun', except that the function | |
133 ;;; generated will be open-coded in compiled code which uses it. This | |
134 ;;; means that no function call will be generated, it will simply be | |
135 ;;; spliced in. Lisp functions calls are very slow, so this can be a | |
136 ;;; big win. | |
137 ;;; | |
138 ;;; You can generally accomplish the same thing with `defmacro', but in | |
139 ;;; that case, the defined procedure can't be used as an argument to | |
140 ;;; mapcar, etc. | |
141 ;;; | |
142 ;;; o You can make a given function be inline even if it has already been | |
143 ;;; defined with `defun' by using the `proclaim-inline' form like so: | |
144 ;;; (proclaim-inline my-function) | |
145 ;;; This is, in fact, exactly what `defsubst' does. To make a function no | |
146 ;;; longer be inline, you must use `proclaim-notinline'. Beware that if | |
147 ;;; you define a function with `defsubst' and later redefine it with | |
444 | 148 ;;; `defun', it will still be open-coded until you use `proclaim-notinline'. |
428 | 149 ;;; |
150 ;;; o You can also open-code one particular call to a function without | |
151 ;;; open-coding all calls. Use the 'inline' form to do this, like so: | |
152 ;;; | |
153 ;;; (inline (foo 1 2 3)) ;; `foo' will be open-coded | |
154 ;;; or... | |
155 ;;; (inline ;; `foo' and `baz' will be | |
156 ;;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not. | |
157 ;;; (baz 0)) | |
158 ;;; | |
159 ;;; o It is possible to open-code a function in the same file it is defined | |
160 ;;; in without having to load that file before compiling it. the | |
161 ;;; byte-compiler has been modified to remember function definitions in | |
162 ;;; the compilation environment in the same way that it remembers macro | |
163 ;;; definitions. | |
164 ;;; | |
165 ;;; o Forms like ((lambda ...) ...) are open-coded. | |
166 ;;; | |
444 | 167 ;;; o The form `eval-when-compile' is like `progn', except that the body |
428 | 168 ;;; is evaluated at compile-time. When it appears at top-level, this |
169 ;;; is analogous to the Common Lisp idiom (eval-when (compile) ...). | |
170 ;;; When it does not appear at top-level, it is similar to the | |
171 ;;; Common Lisp #. reader macro (but not in interpreted code). | |
172 ;;; | |
444 | 173 ;;; o The form `eval-and-compile' is similar to `eval-when-compile', |
174 ;;; but the whole form is evalled both at compile-time and at run-time. | |
428 | 175 ;;; |
176 ;;; o The command M-x byte-compile-and-load-file does what you'd think. | |
177 ;;; | |
444 | 178 ;;; o The command `compile-defun' is analogous to `eval-defun'. |
428 | 179 ;;; |
444 | 180 ;;; o If you run `byte-compile-file' on a filename which is visited in a |
428 | 181 ;;; buffer, and that buffer is modified, you are asked whether you want |
182 ;;; to save the buffer before compiling. | |
183 ;;; | |
184 ;;; o You can add this to /etc/magic to make file(1) recognize the files | |
185 ;;; generated by this compiler: | |
186 ;;; | |
613 | 187 ;;; 0 string ;ELC XEmacs Lisp compiled file, |
428 | 188 ;;; >4 byte x version %d |
189 ;;; | |
190 ;;; TO DO: | |
191 ;;; | |
192 ;;; o Should implement declarations and proclamations, notably special, | |
193 ;;; unspecial, and ignore. Do this in such a way as to not break cl.el. | |
194 ;;; o The bound-but-not-used warnings are not issued for variables whose | |
195 ;;; bindings were established in the arglist, due to the lack of an | |
196 ;;; ignore declaration. Once ignore exists, this should be turned on. | |
197 ;;; o Warn about functions and variables defined but not used? | |
198 ;;; Maybe add some kind of `export' declaration for this? | |
199 ;;; (With interactive functions being automatically exported?) | |
200 ;;; o Any reference to a variable, even one which is a no-op, will cause | |
201 ;;; the warning not to be given. Possibly we could use the for-effect | |
202 ;;; flag to determine when this reference is useless; possibly more | |
203 ;;; complex flow analysis would be necessary. | |
204 ;;; o If the optimizer deletes a variable reference, we might be left with | |
205 ;;; a bound-but-not-referenced warning. Generally this is ok, but not if | |
206 ;;; it's a synergistic result of macroexpansion. Need some way to note | |
207 ;;; that a varref is being optimized away? Of course it would be nice to | |
208 ;;; optimize away the binding too, someday, but it's unsafe today. | |
209 ;;; o (See byte-optimize.el for the optimization TODO list.) | |
210 | |
211 (require 'backquote) | |
212 | |
213 (or (fboundp 'defsubst) | |
214 ;; This really ought to be loaded already! | |
215 (load-library "bytecomp-runtime")) | |
216 | |
217 (eval-when-compile | |
218 (defvar byte-compile-single-version nil | |
219 "If this is true, the choice of emacs version (v19 or v20) byte-codes will | |
220 be hard-coded into bytecomp when it compiles itself. If the compiler itself | |
221 is compiled with optimization, this causes a speedup.") | |
222 | |
223 (cond | |
224 (byte-compile-single-version | |
225 (defmacro byte-compile-single-version () t) | |
226 (defmacro byte-compile-version-cond (cond) (list 'quote (eval cond)))) | |
227 (t | |
228 (defmacro byte-compile-single-version () nil) | |
229 (defmacro byte-compile-version-cond (cond) cond))) | |
230 ) | |
231 | |
444 | 232 (defvar emacs-lisp-file-regexp "\\.el$" |
428 | 233 "*Regexp which matches Emacs Lisp source files. |
234 You may want to redefine `byte-compile-dest-file' if you change this.") | |
235 | |
236 ;; This enables file name handlers such as jka-compr | |
237 ;; to remove parts of the file name that should not be copied | |
238 ;; through to the output file name. | |
239 (defun byte-compiler-base-file-name (filename) | |
240 (let ((handler (find-file-name-handler filename | |
241 'byte-compiler-base-file-name))) | |
242 (if handler | |
243 (funcall handler 'byte-compiler-base-file-name filename) | |
244 filename))) | |
245 | |
246 (unless (fboundp 'byte-compile-dest-file) | |
247 ;; The user may want to redefine this along with emacs-lisp-file-regexp, | |
248 ;; so only define it if it is undefined. | |
249 (defun byte-compile-dest-file (filename) | |
250 "Convert an Emacs Lisp source file name to a compiled file name." | |
251 (setq filename (byte-compiler-base-file-name filename)) | |
252 (setq filename (file-name-sans-versions filename)) | |
253 (if (string-match emacs-lisp-file-regexp filename) | |
254 (concat (substring filename 0 (match-beginning 0)) ".elc") | |
255 (concat filename ".elc")))) | |
256 | |
257 ;; This can be the 'byte-compile property of any symbol. | |
258 (autoload 'byte-compile-inline-expand "byte-optimize") | |
259 | |
260 ;; This is the entrypoint to the lapcode optimizer pass1. | |
261 (autoload 'byte-optimize-form "byte-optimize") | |
262 ;; This is the entrypoint to the lapcode optimizer pass2. | |
263 (autoload 'byte-optimize-lapcode "byte-optimize") | |
264 (autoload 'byte-compile-unfold-lambda "byte-optimize") | |
265 | |
266 ;; This is the entry point to the decompiler, which is used by the | |
267 ;; disassembler. The disassembler just requires 'byte-compile, but | |
268 ;; that doesn't define this function, so this seems to be a reasonable | |
269 ;; thing to do. | |
270 (autoload 'byte-decompile-bytecode "byte-optimize") | |
271 | |
272 (defvar byte-compile-verbose | |
273 (and (not noninteractive) (> (device-baud-rate) search-slow-speed)) | |
274 "*Non-nil means print messages describing progress of byte-compiler.") | |
275 | |
276 (defvar byte-compile-emacs19-compatibility | |
277 (not (emacs-version>= 20)) | |
278 "*Non-nil means generate output that can run in Emacs 19.") | |
279 | |
280 (defvar byte-compile-print-gensym t | |
281 "*Non-nil means generate code that creates unique symbols at run-time. | |
282 This is achieved by printing uninterned symbols using the `#:SYMBOL' | |
283 notation, so that they will be read uninterned when run. | |
284 | |
285 With this feature, code that uses uninterned symbols in macros will | |
286 not be runnable under pre-21.0 XEmacsen. | |
287 | |
288 When `byte-compile-emacs19-compatibility' is non-nil, this variable is | |
289 ignored and considered to be nil.") | |
290 | |
291 (defvar byte-optimize t | |
292 "*Enables optimization in the byte compiler. | |
293 nil means don't do any optimization. | |
294 t means do all optimizations. | |
295 `source' means do source-level optimizations only. | |
296 `byte' means do code-level optimizations only.") | |
297 | |
298 (defvar byte-compile-delete-errors t | |
299 "*If non-nil, the optimizer may delete forms that may signal an error. | |
300 This includes variable references and calls to functions such as `car'.") | |
301 | |
302 ;; XEmacs addition | |
303 (defvar byte-compile-new-bytecodes nil | |
304 "This is completely ignored. It is only around for backwards | |
305 compatibility.") | |
306 | |
307 | |
308 ;; FSF enables byte-compile-dynamic-docstrings but not byte-compile-dynamic | |
309 ;; by default. This would be a reasonable conservative approach except | |
310 ;; for the fact that if you enable either of these, you get incompatible | |
311 ;; byte code that can't be read by XEmacs 19.13 or before or FSF 19.28 or | |
312 ;; before. | |
313 ;; | |
314 ;; Therefore, neither is enabled for 19.14. Both are enabled for 20.0 | |
315 ;; because we have no reason to be conservative about changing the | |
316 ;; way things work. (Ben) | |
317 | |
318 ;; However, I don't think that defaulting byte-compile-dynamic to nil | |
319 ;; is a compatibility issue - rather it is a performance issue. | |
320 ;; Therefore I am setting byte-compile-dynamic back to nil. (mrb) | |
321 | |
322 (defvar byte-compile-dynamic nil | |
323 "*If non-nil, compile function bodies so they load lazily. | |
324 They are hidden comments in the compiled file, and brought into core when the | |
325 function is called. | |
326 | |
327 To enable this option, make it a file-local variable | |
328 in the source file you want it to apply to. | |
329 For example, add -*-byte-compile-dynamic: t;-*- on the first line. | |
330 | |
331 When this option is true, if you load the compiled file and then move it, | |
332 the functions you loaded will not be able to run.") | |
333 | |
334 (defvar byte-compile-dynamic-docstrings (emacs-version>= 20) | |
335 "*If non-nil, compile doc strings for lazy access. | |
336 We bury the doc strings of functions and variables | |
337 inside comments in the file, and bring them into core only when they | |
338 are actually needed. | |
339 | |
340 When this option is true, if you load the compiled file and then move it, | |
341 you won't be able to find the documentation of anything in that file. | |
342 | |
343 To disable this option for a certain file, make it a file-local variable | |
344 in the source file. For example, add this to the first line: | |
345 -*-byte-compile-dynamic-docstrings:nil;-*- | |
346 You can also set the variable globally. | |
347 | |
348 This option is enabled by default because it reduces Emacs memory usage.") | |
349 | |
350 (defvar byte-optimize-log nil | |
351 "*If true, the byte-compiler will log its optimizations into *Compile-Log*. | |
352 If this is 'source, then only source-level optimizations will be logged. | |
353 If it is 'byte, then only byte-level optimizations will be logged.") | |
354 | |
355 (defvar byte-compile-error-on-warn nil | |
356 "*If true, the byte-compiler reports warnings with `error'.") | |
357 | |
358 ;; byte-compile-warning-types in FSF. | |
359 (defvar byte-compile-default-warnings | |
360 '(redefine callargs subr-callargs free-vars unresolved unused-vars obsolete) | |
361 "*The warnings used when byte-compile-warnings is t.") | |
362 | |
363 (defvar byte-compile-warnings t | |
364 "*List of warnings that the compiler should issue (t for the default set). | |
365 Elements of the list may be: | |
366 | |
367 free-vars references to variables not in the current lexical scope. | |
368 unused-vars references to non-global variables bound but not referenced. | |
369 unresolved calls to unknown functions. | |
370 callargs lambda calls with args that don't match the definition. | |
371 subr-callargs calls to subrs with args that don't match the definition. | |
372 redefine function cell redefined from a macro to a lambda or vice | |
373 versa, or redefined to take a different number of arguments. | |
374 obsolete use of an obsolete function or variable. | |
375 pedantic warn of use of compatible symbols. | |
376 | |
377 The default set is specified by `byte-compile-default-warnings' and | |
378 normally encompasses all possible warnings. | |
379 | |
380 See also the macro `byte-compiler-options'.") | |
381 | |
382 (defvar byte-compile-generate-call-tree nil | |
383 "*Non-nil means collect call-graph information when compiling. | |
384 This records functions that were called and from where. | |
385 If the value is t, compilation displays the call graph when it finishes. | |
386 If the value is neither t nor nil, compilation asks you whether to display | |
387 the graph. | |
388 | |
389 The call tree only lists functions called, not macros used. Those functions | |
390 which the byte-code interpreter knows about directly (eq, cons, etc.) are | |
391 not reported. | |
392 | |
393 The call tree also lists those functions which are not known to be called | |
394 \(that is, to which no calls have been compiled). Functions which can be | |
395 invoked interactively are excluded from this list.") | |
396 | |
397 (defconst byte-compile-call-tree nil "Alist of functions and their call tree. | |
398 Each element looks like | |
399 | |
400 \(FUNCTION CALLERS CALLS\) | |
401 | |
402 where CALLERS is a list of functions that call FUNCTION, and CALLS | |
403 is a list of functions for which calls were generated while compiling | |
404 FUNCTION.") | |
405 | |
406 (defvar byte-compile-call-tree-sort 'name | |
407 "*If non-nil, sort the call tree. | |
408 The values `name', `callers', `calls', `calls+callers' | |
409 specify different fields to sort on.") | |
410 | |
411 (defvar byte-compile-overwrite-file t | |
412 "If nil, old .elc files are deleted before the new is saved, and .elc | |
413 files will have the same modes as the corresponding .el file. Otherwise, | |
414 existing .elc files will simply be overwritten, and the existing modes | |
415 will not be changed. If this variable is nil, then an .elc file which | |
416 is a symbolic link will be turned into a normal file, instead of the file | |
417 which the link points to being overwritten.") | |
418 | |
419 (defvar byte-recompile-directory-ignore-errors-p nil | |
420 "If true, then `byte-recompile-directory' will continue compiling even | |
421 when an error occurs in a file. This is bound to t by | |
422 `batch-byte-recompile-directory'.") | |
423 | |
502 | 424 (defvar byte-recompile-ignore-uncompilable-mule-files t |
425 "If non-nil, `byte-recompile-*' ignores non-ASCII .el files in a non-Mule | |
426 XEmacs. This assumes that such files have a -*- coding: ??? -*- magic | |
427 cookie in their first line or a ;;;###coding system: magic cookie | |
428 early in the file.") | |
429 | |
428 | 430 (defvar byte-recompile-directory-recursively t |
431 "*If true, then `byte-recompile-directory' will recurse on subdirectories.") | |
432 | |
433 (defvar byte-compile-constants nil | |
434 "list of all constants encountered during compilation of this form") | |
435 (defvar byte-compile-variables nil | |
436 "list of all variables encountered during compilation of this form") | |
437 (defvar byte-compile-bound-variables nil | |
438 "Alist of variables bound in the context of the current form, | |
439 that is, the current lexical environment. This list lives partly | |
440 on the specbind stack. The cdr of each cell is an integer bitmask.") | |
441 | |
4623
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
442 (defvar byte-compile-force-escape-quoted nil |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
443 "If non-nil, `byte-compile-insert-header' always adds a coding cookie. |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
444 |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
445 This is for situations where the byte compiler output file needs to be |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
446 able to encode character values above ?\\xFF, but this cannot be |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
447 easily determined from the input file.") |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
448 |
428 | 449 (defconst byte-compile-referenced-bit 1) |
450 (defconst byte-compile-assigned-bit 2) | |
451 (defconst byte-compile-arglist-bit 4) | |
452 (defconst byte-compile-global-bit 8) | |
453 | |
454 (defvar byte-compile-free-references) | |
455 (defvar byte-compile-free-assignments) | |
456 | |
457 (defvar byte-compiler-error-flag) | |
458 | |
446 | 459 ;;; A form of eval that includes the currently defined macro definitions. |
460 ;;; This helps implement the promise made in the Lispref: | |
461 ;;; | |
462 ;;; "If a file being compiled contains a `defmacro' form, the macro is | |
463 ;;; defined temporarily for the rest of the compilation of that file." | |
464 (defun byte-compile-eval (form) | |
465 (let ((save-macro-environment nil)) | |
466 (unwind-protect | |
467 (loop for (sym . def) in byte-compile-macro-environment do | |
468 (push | |
469 (if (fboundp sym) (cons sym (symbol-function sym)) sym) | |
470 save-macro-environment) | |
471 (fset sym (cons 'macro def)) | |
472 finally return (eval form)) | |
473 (dolist (elt save-macro-environment) | |
474 (if (symbolp elt) | |
475 (fmakunbound elt) | |
476 (fset (car elt) (cdr elt))))))) | |
477 | |
428 | 478 (defconst byte-compile-initial-macro-environment |
444 | 479 '((byte-compiler-options . (lambda (&rest forms) |
480 (apply 'byte-compiler-options-handler forms))) | |
481 (eval-when-compile . (lambda (&rest body) | |
446 | 482 (list 'quote (byte-compile-eval (cons 'progn body))))) |
444 | 483 (eval-and-compile . (lambda (&rest body) |
446 | 484 (byte-compile-eval (cons 'progn body)) |
444 | 485 (cons 'progn body)))) |
428 | 486 "The default macro-environment passed to macroexpand by the compiler. |
487 Placing a macro here will cause a macro to have different semantics when | |
488 expanded by the compiler as when expanded by the interpreter.") | |
489 | |
490 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment | |
491 "Alist of macros defined in the file being compiled. | |
492 Each element looks like (MACRONAME . DEFINITION). It is | |
493 \(MACRONAME . nil) when a macro is redefined as a function.") | |
494 | |
495 (defvar byte-compile-function-environment nil | |
496 "Alist of functions defined in the file being compiled. | |
497 This is so we can inline them when necessary. | |
498 Each element looks like (FUNCTIONNAME . DEFINITION). It is | |
499 \(FUNCTIONNAME . nil) when a function is redefined as a macro.") | |
500 | |
501 (defvar byte-compile-autoload-environment nil | |
502 "Alist of functions and macros defined by autoload in the file being compiled. | |
503 This is so we can suppress warnings about calls to these functions, even though | |
504 they do not have `real' definitions. | |
505 Each element looks like (FUNCTIONNAME . CALL-TO-AUTOLOAD).") | |
506 | |
507 (defvar byte-compile-unresolved-functions nil | |
508 "Alist of undefined functions to which calls have been compiled (used for | |
509 warnings when the function is later defined with incorrect args).") | |
510 | |
511 (defvar byte-compile-file-domain) ; domain of file being compiled | |
512 | |
513 (defvar byte-compile-tag-number 0) | |
514 (defvar byte-compile-output nil | |
515 "Alist describing contents to put in byte code string. | |
516 Each element is (INDEX . VALUE)") | |
517 (defvar byte-compile-depth 0 "Current depth of execution stack.") | |
518 (defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.") | |
519 | |
520 | |
521 ;;; The byte codes; this information is duplicated in bytecode.c | |
522 | |
523 (defconst byte-code-vector nil | |
524 "An array containing byte-code names indexed by byte-code values.") | |
525 | |
526 (defconst byte-stack+-info nil | |
527 "An array with the stack adjustment for each byte-code.") | |
528 | |
529 (defmacro byte-defop (opcode stack-adjust opname &optional docstring) | |
530 ;; This is a speed-hack for building the byte-code-vector at compile-time. | |
531 ;; We fill in the vector at macroexpand-time, and then after the last call | |
532 ;; to byte-defop, we write the vector out as a constant instead of writing | |
533 ;; out a bunch of calls to aset. | |
534 ;; Actually, we don't fill in the vector itself, because that could make | |
535 ;; it problematic to compile big changes to this compiler; we store the | |
536 ;; values on its plist, and remove them later in -extrude. | |
537 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value) | |
538 (put 'byte-code-vector 'tmp-compile-time-value | |
539 (make-vector 256 nil)))) | |
540 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value) | |
541 (put 'byte-stack+-info 'tmp-compile-time-value | |
542 (make-vector 256 nil))))) | |
543 (aset v1 opcode opname) | |
544 (aset v2 opcode stack-adjust)) | |
545 (if docstring | |
546 (list 'defconst opname opcode (concat "Byte code opcode " docstring ".")) | |
547 (list 'defconst opname opcode))) | |
548 | |
549 (defmacro byte-extrude-byte-code-vectors () | |
550 (prog1 (list 'setq 'byte-code-vector | |
551 (get 'byte-code-vector 'tmp-compile-time-value) | |
552 'byte-stack+-info | |
553 (get 'byte-stack+-info 'tmp-compile-time-value)) | |
554 (remprop 'byte-code-vector 'tmp-compile-time-value) | |
555 (remprop 'byte-stack+-info 'tmp-compile-time-value))) | |
556 | |
557 | |
558 ;; unused: 0-7 | |
559 | |
560 ;; These opcodes are special in that they pack their argument into the | |
561 ;; opcode word. | |
562 ;; | |
563 (byte-defop 8 1 byte-varref "for variable reference") | |
564 (byte-defop 16 -1 byte-varset "for setting a variable") | |
565 (byte-defop 24 -1 byte-varbind "for binding a variable") | |
566 (byte-defop 32 0 byte-call "for calling a function") | |
567 (byte-defop 40 0 byte-unbind "for unbinding special bindings") | |
568 ;; codes 8-47 are consumed by the preceding opcodes | |
569 | |
570 ;; unused: 48-55 | |
571 | |
572 (byte-defop 56 -1 byte-nth) | |
573 (byte-defop 57 0 byte-symbolp) | |
574 (byte-defop 58 0 byte-consp) | |
575 (byte-defop 59 0 byte-stringp) | |
576 (byte-defop 60 0 byte-listp) | |
577 (byte-defop 61 -1 byte-old-eq) | |
578 (byte-defop 62 -1 byte-old-memq) | |
579 (byte-defop 63 0 byte-not) | |
580 (byte-defop 64 0 byte-car) | |
581 (byte-defop 65 0 byte-cdr) | |
582 (byte-defop 66 -1 byte-cons) | |
583 (byte-defop 67 0 byte-list1) | |
584 (byte-defop 68 -1 byte-list2) | |
585 (byte-defop 69 -2 byte-list3) | |
586 (byte-defop 70 -3 byte-list4) | |
587 (byte-defop 71 0 byte-length) | |
588 (byte-defop 72 -1 byte-aref) | |
589 (byte-defop 73 -2 byte-aset) | |
590 (byte-defop 74 0 byte-symbol-value) | |
591 (byte-defop 75 0 byte-symbol-function) ; this was commented out | |
592 (byte-defop 76 -1 byte-set) | |
593 (byte-defop 77 -1 byte-fset) ; this was commented out | |
594 (byte-defop 78 -1 byte-get) | |
595 (byte-defop 79 -2 byte-substring) | |
596 (byte-defop 80 -1 byte-concat2) | |
597 (byte-defop 81 -2 byte-concat3) | |
598 (byte-defop 82 -3 byte-concat4) | |
599 (byte-defop 83 0 byte-sub1) | |
600 (byte-defop 84 0 byte-add1) | |
601 (byte-defop 85 -1 byte-eqlsign) | |
602 (byte-defop 86 -1 byte-gtr) | |
603 (byte-defop 87 -1 byte-lss) | |
604 (byte-defop 88 -1 byte-leq) | |
605 (byte-defop 89 -1 byte-geq) | |
606 (byte-defop 90 -1 byte-diff) | |
607 (byte-defop 91 0 byte-negate) | |
608 (byte-defop 92 -1 byte-plus) | |
609 (byte-defop 93 -1 byte-max) | |
610 (byte-defop 94 -1 byte-min) | |
611 (byte-defop 95 -1 byte-mult) | |
612 (byte-defop 96 1 byte-point) | |
613 (byte-defop 97 -1 byte-eq) ; new as of v20 | |
614 (byte-defop 98 0 byte-goto-char) | |
615 (byte-defop 99 0 byte-insert) | |
616 (byte-defop 100 1 byte-point-max) | |
617 (byte-defop 101 1 byte-point-min) | |
618 (byte-defop 102 0 byte-char-after) | |
619 (byte-defop 103 1 byte-following-char) | |
620 (byte-defop 104 1 byte-preceding-char) | |
621 (byte-defop 105 1 byte-current-column) | |
622 (byte-defop 106 0 byte-indent-to) | |
623 (byte-defop 107 -1 byte-equal) ; new as of v20 | |
624 (byte-defop 108 1 byte-eolp) | |
625 (byte-defop 109 1 byte-eobp) | |
626 (byte-defop 110 1 byte-bolp) | |
627 (byte-defop 111 1 byte-bobp) | |
628 (byte-defop 112 1 byte-current-buffer) | |
629 (byte-defop 113 0 byte-set-buffer) | |
630 (byte-defop 114 0 byte-save-current-buffer | |
631 "To make a binding to record the current buffer.") | |
632 ;;(byte-defop 114 1 byte-read-char-OBSOLETE) ;obsolete as of v19 | |
633 (byte-defop 115 -1 byte-memq) ; new as of v20 | |
634 (byte-defop 116 1 byte-interactive-p) | |
635 | |
636 (byte-defop 117 0 byte-forward-char) | |
637 (byte-defop 118 0 byte-forward-word) | |
638 (byte-defop 119 -1 byte-skip-chars-forward) | |
639 (byte-defop 120 -1 byte-skip-chars-backward) | |
640 (byte-defop 121 0 byte-forward-line) | |
641 (byte-defop 122 0 byte-char-syntax) | |
642 (byte-defop 123 -1 byte-buffer-substring) | |
643 (byte-defop 124 -1 byte-delete-region) | |
644 (byte-defop 125 -1 byte-narrow-to-region) | |
645 (byte-defop 126 1 byte-widen) | |
646 (byte-defop 127 0 byte-end-of-line) | |
647 | |
648 ;; unused: 128 | |
649 | |
650 ;; These store their argument in the next two bytes | |
651 (byte-defop 129 1 byte-constant2 | |
652 "for reference to a constant with vector index >= byte-constant-limit") | |
653 (byte-defop 130 0 byte-goto "for unconditional jump") | |
654 (byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil") | |
655 (byte-defop 132 -1 byte-goto-if-not-nil | |
656 "to pop value and jump if it's not nil") | |
657 (byte-defop 133 -1 byte-goto-if-nil-else-pop | |
658 "to examine top-of-stack, jump and don't pop it if it's nil, | |
659 otherwise pop it") | |
660 (byte-defop 134 -1 byte-goto-if-not-nil-else-pop | |
661 "to examine top-of-stack, jump and don't pop it if it's non-nil, | |
662 otherwise pop it") | |
663 | |
664 (byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'") | |
665 (byte-defop 136 -1 byte-discard "to discard one value from stack") | |
666 (byte-defop 137 1 byte-dup "to duplicate the top of the stack") | |
667 | |
668 (byte-defop 138 0 byte-save-excursion | |
669 "to make a binding to record the buffer, point and mark") | |
670 (byte-defop 139 0 byte-save-window-excursion | |
671 "to make a binding to record entire window configuration") | |
672 (byte-defop 140 0 byte-save-restriction | |
673 "to make a binding to record the current buffer clipping restrictions") | |
674 (byte-defop 141 -1 byte-catch | |
675 "for catch. Takes, on stack, the tag and an expression for the body") | |
676 (byte-defop 142 -1 byte-unwind-protect | |
677 "for unwind-protect. Takes, on stack, an expression for the unwind-action") | |
678 | |
679 ;; For condition-case. Takes, on stack, the variable to bind, | |
680 ;; an expression for the body, and a list of clauses. | |
681 (byte-defop 143 -2 byte-condition-case) | |
682 | |
683 ;; For entry to with-output-to-temp-buffer. | |
684 ;; Takes, on stack, the buffer name. | |
685 ;; Binds standard-output and does some other things. | |
686 ;; Returns with temp buffer on the stack in place of buffer name. | |
687 (byte-defop 144 0 byte-temp-output-buffer-setup) | |
688 | |
689 ;; For exit from with-output-to-temp-buffer. | |
690 ;; Expects the temp buffer on the stack underneath value to return. | |
691 ;; Pops them both, then pushes the value back on. | |
692 ;; Unbinds standard-output and makes the temp buffer visible. | |
693 (byte-defop 145 -1 byte-temp-output-buffer-show) | |
694 | |
695 ;; To unbind back to the beginning of this frame. | |
696 ;; Not used yet, but will be needed for tail-recursion elimination. | |
697 (byte-defop 146 0 byte-unbind-all) | |
698 | |
699 (byte-defop 147 -2 byte-set-marker) | |
700 (byte-defop 148 0 byte-match-beginning) | |
701 (byte-defop 149 0 byte-match-end) | |
702 (byte-defop 150 0 byte-upcase) | |
703 (byte-defop 151 0 byte-downcase) | |
704 (byte-defop 152 -1 byte-string=) | |
705 (byte-defop 153 -1 byte-string<) | |
706 (byte-defop 154 -1 byte-old-equal) | |
707 (byte-defop 155 -1 byte-nthcdr) | |
708 (byte-defop 156 -1 byte-elt) | |
709 (byte-defop 157 -1 byte-old-member) | |
710 (byte-defop 158 -1 byte-old-assq) | |
711 (byte-defop 159 0 byte-nreverse) | |
712 (byte-defop 160 -1 byte-setcar) | |
713 (byte-defop 161 -1 byte-setcdr) | |
714 (byte-defop 162 0 byte-car-safe) | |
715 (byte-defop 163 0 byte-cdr-safe) | |
716 (byte-defop 164 -1 byte-nconc) | |
717 (byte-defop 165 -1 byte-quo) | |
718 (byte-defop 166 -1 byte-rem) | |
719 (byte-defop 167 0 byte-numberp) | |
720 (byte-defop 168 0 byte-integerp) | |
721 | |
722 ;; unused: 169 | |
723 | |
724 ;; These are not present in FSF. | |
725 ;; | |
726 (byte-defop 170 0 byte-rel-goto) | |
727 (byte-defop 171 -1 byte-rel-goto-if-nil) | |
728 (byte-defop 172 -1 byte-rel-goto-if-not-nil) | |
729 (byte-defop 173 -1 byte-rel-goto-if-nil-else-pop) | |
730 (byte-defop 174 -1 byte-rel-goto-if-not-nil-else-pop) | |
731 | |
732 (byte-defop 175 nil byte-listN) | |
733 (byte-defop 176 nil byte-concatN) | |
734 (byte-defop 177 nil byte-insertN) | |
735 | |
736 ;; unused: 178-181 | |
737 | |
738 ;; these ops are new to v20 | |
739 (byte-defop 182 -1 byte-member) | |
740 (byte-defop 183 -1 byte-assq) | |
741 | |
742 ;; unused: 184-191 | |
743 | |
744 (byte-defop 192 1 byte-constant "for reference to a constant") | |
745 ;; codes 193-255 are consumed by byte-constant. | |
746 (defconst byte-constant-limit 64 | |
747 "Exclusive maximum index usable in the `byte-constant' opcode.") | |
748 | |
444 | 749 (defconst byte-goto-ops |
750 '(byte-goto byte-goto-if-nil byte-goto-if-not-nil | |
751 byte-goto-if-nil-else-pop | |
752 byte-goto-if-not-nil-else-pop) | |
428 | 753 "List of byte-codes whose offset is a pc.") |
754 | |
755 (defconst byte-goto-always-pop-ops | |
444 | 756 '(byte-goto-if-nil byte-goto-if-not-nil)) |
428 | 757 |
758 (defconst byte-rel-goto-ops | |
444 | 759 '(byte-rel-goto byte-rel-goto-if-nil byte-rel-goto-if-not-nil |
760 byte-rel-goto-if-nil-else-pop byte-rel-goto-if-not-nil-else-pop) | |
428 | 761 "byte-codes for relative jumps.") |
762 | |
763 (byte-extrude-byte-code-vectors) | |
764 | |
765 ;;; lapcode generator | |
766 ;;; | |
767 ;;; the byte-compiler now does source -> lapcode -> bytecode instead of | |
768 ;;; source -> bytecode, because it's a lot easier to make optimizations | |
769 ;;; on lapcode than on bytecode. | |
770 ;;; | |
771 ;;; Elements of the lapcode list are of the form (<instruction> . <parameter>) | |
772 ;;; where instruction is a symbol naming a byte-code instruction, | |
773 ;;; and parameter is an argument to that instruction, if any. | |
774 ;;; | |
775 ;;; The instruction can be the pseudo-op TAG, which means that this position | |
776 ;;; in the instruction stream is a target of a goto. (car PARAMETER) will be | |
777 ;;; the PC for this location, and the whole instruction "(TAG pc)" will be the | |
778 ;;; parameter for some goto op. | |
779 ;;; | |
780 ;;; If the operation is varbind, varref, varset or push-constant, then the | |
781 ;;; parameter is (variable/constant . index_in_constant_vector). | |
782 ;;; | |
783 ;;; First, the source code is macroexpanded and optimized in various ways. | |
784 ;;; Then the resultant code is compiled into lapcode. Another set of | |
785 ;;; optimizations are then run over the lapcode. Then the variables and | |
786 ;;; constants referenced by the lapcode are collected and placed in the | |
787 ;;; constants-vector. (This happens now so that variables referenced by dead | |
788 ;;; code don't consume space.) And finally, the lapcode is transformed into | |
789 ;;; compacted byte-code. | |
790 ;;; | |
791 ;;; A distinction is made between variables and constants because the variable- | |
792 ;;; referencing instructions are more sensitive to the variables being near the | |
793 ;;; front of the constants-vector than the constant-referencing instructions. | |
794 ;;; Also, this lets us notice references to free variables. | |
795 | |
796 (defun byte-compile-lapcode (lap) | |
797 "Turns lapcode into bytecode. The lapcode is destroyed." | |
798 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC. | |
799 (let ((pc 0) ; Program counter | |
800 op off ; Operation & offset | |
801 (bytes '()) ; Put the output bytes here | |
802 (patchlist nil) ; List of tags and goto's to patch | |
803 rest rel tmp) | |
804 (while lap | |
805 (setq op (car (car lap)) | |
806 off (cdr (car lap))) | |
807 (cond ((not (symbolp op)) | |
808 (error "Non-symbolic opcode `%s'" op)) | |
809 ((eq op 'TAG) | |
810 (setcar off pc) | |
811 (push off patchlist)) | |
812 ((memq op byte-goto-ops) | |
813 (setq pc (+ pc 3)) | |
814 (setq bytes (cons (cons pc (cdr off)) | |
815 (cons nil | |
816 (cons (symbol-value op) bytes)))) | |
817 (push bytes patchlist)) | |
818 (t | |
819 (setq bytes | |
820 (cond ((cond ((consp off) | |
821 ;; Variable or constant reference | |
822 (setq off (cdr off)) | |
823 (eq op 'byte-constant))) | |
824 (cond ((< off byte-constant-limit) | |
825 (setq pc (1+ pc)) | |
826 (cons (+ byte-constant off) bytes)) | |
827 (t | |
828 (setq pc (+ 3 pc)) | |
829 (cons (lsh off -8) | |
830 (cons (logand off 255) | |
831 (cons byte-constant2 bytes)))))) | |
832 ((and (<= byte-listN (symbol-value op)) | |
833 (<= (symbol-value op) byte-insertN)) | |
834 (setq pc (+ 2 pc)) | |
835 (cons off (cons (symbol-value op) bytes))) | |
836 ((< off 6) | |
837 (setq pc (1+ pc)) | |
838 (cons (+ (symbol-value op) off) bytes)) | |
839 ((< off 256) | |
840 (setq pc (+ 2 pc)) | |
841 (cons off (cons (+ (symbol-value op) 6) bytes))) | |
842 (t | |
843 (setq pc (+ 3 pc)) | |
844 (cons (lsh off -8) | |
845 (cons (logand off 255) | |
846 (cons (+ (symbol-value op) 7) | |
847 bytes)))))))) | |
848 (setq lap (cdr lap))) | |
849 ;;(if (not (= pc (length bytes))) | |
850 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes))) | |
851 (cond (t ;; starting with Emacs 19. | |
852 ;; Make relative jumps | |
853 (setq patchlist (nreverse patchlist)) | |
854 (while (progn | |
855 (setq off 0) ; PC change because of deleted bytes | |
856 (setq rest patchlist) | |
857 (while rest | |
858 (setq tmp (car rest)) | |
859 (and (consp (car tmp)) ; Jump | |
860 (prog1 (null (nth 1 tmp)) ; Absolute jump | |
861 (setq tmp (car tmp))) | |
862 (progn | |
863 (setq rel (- (car (cdr tmp)) (car tmp))) | |
864 (and (<= -129 rel) (< rel 128))) | |
865 (progn | |
866 ;; Convert to relative jump. | |
867 (setcdr (car rest) (cdr (cdr (car rest)))) | |
868 (setcar (cdr (car rest)) | |
869 (+ (car (cdr (car rest))) | |
870 (- byte-rel-goto byte-goto))) | |
871 (setq off (1- off)))) | |
872 (setcar tmp (+ (car tmp) off)) ; Adjust PC | |
873 (setq rest (cdr rest))) | |
874 ;; If optimizing, repeat until no change. | |
875 (and byte-optimize | |
876 (not (zerop off))))))) | |
877 ;; Patch PC into jumps | |
878 (let (bytes) | |
879 (while patchlist | |
880 (setq bytes (car patchlist)) | |
881 (cond ((atom (car bytes))) ; Tag | |
882 ((nth 1 bytes) ; Relative jump | |
883 (setcar bytes (+ (- (car (cdr (car bytes))) (car (car bytes))) | |
884 128))) | |
885 (t ; Absolute jump | |
886 (setq pc (car (cdr (car bytes)))) ; Pick PC from tag | |
887 (setcar (cdr bytes) (logand pc 255)) | |
888 (setcar bytes (lsh pc -8)))) | |
889 (setq patchlist (cdr patchlist)))) | |
890 (concat (nreverse bytes)))) | |
891 | |
892 | |
893 ;;; byte compiler messages | |
894 | |
895 (defvar byte-compile-current-form nil) | |
896 (defvar byte-compile-current-file nil) | |
897 (defvar byte-compile-dest-file nil) | |
898 | |
899 (defmacro byte-compile-log (format-string &rest args) | |
900 `(when (and byte-optimize (memq byte-optimize-log '(t source))) | |
901 (let ((print-escape-newlines t) | |
902 (print-level 4) | |
903 (print-length 4)) | |
904 (byte-compile-log-1 (format ,format-string ,@args))))) | |
905 | |
906 (defconst byte-compile-last-warned-form 'nothing) | |
907 | |
908 ;; Log a message STRING in *Compile-Log*. | |
909 ;; Also log the current function and file if not already done. | |
910 (defun byte-compile-log-1 (string &optional fill) | |
911 (let* ((this-form (or byte-compile-current-form "toplevel forms")) | |
912 (while-compiling-msg | |
913 (when (or byte-compile-current-file | |
914 (not (eq this-form byte-compile-last-warned-form))) | |
915 (format | |
916 "While compiling %s%s:" | |
917 this-form | |
918 (cond | |
919 ((stringp byte-compile-current-file) | |
920 (concat " in file " byte-compile-current-file)) | |
921 ((bufferp byte-compile-current-file) | |
922 (concat " in buffer " | |
923 (buffer-name byte-compile-current-file))) | |
924 ("")))))) | |
925 (if noninteractive | |
926 (progn | |
927 (when while-compiling-msg (message "%s" while-compiling-msg)) | |
928 (message " %s" string)) | |
929 (with-current-buffer (get-buffer-create "*Compile-Log*") | |
930 (goto-char (point-max)) | |
931 (when byte-compile-current-file | |
932 (when (> (point-max) (point-min)) | |
933 (insert "\n\^L\n")) | |
934 (insert (current-time-string) "\n")) | |
935 (when while-compiling-msg (insert while-compiling-msg "\n")) | |
936 (insert " " string "\n") | |
937 (when (and fill (not (string-match "\n" string))) | |
938 (let ((fill-prefix " ") | |
939 (fill-column 78)) | |
940 (fill-paragraph nil))))) | |
941 (setq byte-compile-current-file nil) | |
942 (setq byte-compile-last-warned-form this-form))) | |
943 | |
944 ;; Log the start of a file in *Compile-Log*, and mark it as done. | |
945 ;; But do nothing in batch mode. | |
946 (defun byte-compile-log-file () | |
947 (when (and byte-compile-current-file (not noninteractive)) | |
948 (with-current-buffer (get-buffer-create "*Compile-Log*") | |
949 (when (> (point-max) (point-min)) | |
950 (goto-char (point-max)) | |
951 (insert "\n\^L\n")) | |
952 (insert "Compiling " | |
953 (if (stringp byte-compile-current-file) | |
954 (concat "file " byte-compile-current-file) | |
955 (concat "buffer " (buffer-name byte-compile-current-file))) | |
956 " at " (current-time-string) "\n") | |
957 (setq byte-compile-current-file nil)))) | |
958 | |
502 | 959 (defvar byte-compile-inbuffer) |
960 (defvar byte-compile-outbuffer) | |
961 | |
428 | 962 (defun byte-compile-warn (format &rest args) |
963 (setq format (apply 'format format args)) | |
964 (if byte-compile-error-on-warn | |
965 (error "%s" format) ; byte-compile-file catches and logs it | |
966 (byte-compile-log-1 (concat "** " format) t) | |
502 | 967 |
968 ;; This was a first attempt to add line numbers to the | |
969 ;; byte-compilation output. Unfortunately, it doesn't work | |
970 ;; perfectly: it reports the line number at the end of the form | |
971 ;; (which may be an entire function), rather than the line number | |
972 ;; of the actual problem. Doing this right is hard because we | |
973 ;; currently use the built-in Lisp parser to parse the entire form | |
974 ;; at once. What we basically need is a whole separate parser | |
975 ;; that annotates its output with line numbers. For example, we | |
976 ;; might modify the parser in lread.c so that, with the right | |
977 ;; option set, it replaces every Lisp object contained in the | |
978 ;; structure it returns with a cons of that object and the line | |
979 ;; number it was found on (determined by counting newlines, | |
980 ;; starting from some arbitrary point). You then have two | |
981 ;; options: (a) Modify the byte compiler so that everything that | |
982 ;; compiles a form deals with the new annotated form rather than | |
983 ;; the old one, or (b) The byte compiler saves this structure | |
984 ;; while converting it into a normal structure that's given to the | |
985 ;; various form handlers, which need no (or less) modification. | |
986 ;; In the former case, finding the line number is trivial because | |
987 ;; it's in the form. In the latter case, finding the line number | |
988 ;; depends on having a unique Lisp object that can be looked up in | |
989 ;; the annotated structure -- i.e. a list, vector, or string. | |
990 ;; You'd have to look at the various places where errors are spit | |
991 ;; out (not very many, really), and make sure that such a unique | |
992 ;; object is available. Then you do a depth-first search through | |
993 ;; the annotated structure to find the object. | |
994 ;; | |
995 ;; An alternative way of doing (b) that's probably much more | |
996 ;; efficient (and easier to implement) is simply to have the | |
997 ;; parser in lread.c annotate every unique object using a separate | |
998 ;; hash table. This also eliminates the need for a search to find | |
999 ;; the line number. In order to be fine-grained enough to get at | |
1000 ;; every symbol in a form -- e.g. if we want to pinpoint a | |
1001 ;; particular undefined variable in a function call -- we need to | |
1002 ;; annotate every cons, not just each list. We still have | |
1003 ;; (probably unimportant) problems with vectors, since all we have | |
1004 ;; is the start of the vector. If we cared about this, we could | |
1005 ;; store in the hash table a list of the line numbers for each | |
1006 ;; item in the vector, not just its start. | |
1007 ;; | |
1008 ;; --ben | |
1009 | |
1010 ; (byte-compile-log-1 (concat "** line: " | |
1011 ; (save-excursion | |
1012 ; (set-buffer byte-compile-inbuffer) | |
1013 ; (int-to-string (line-number))) | |
1014 ; " " | |
1015 ; format) t) | |
428 | 1016 ;;; RMS says: |
1017 ;;; It is useless to flash warnings too fast to be read. | |
1018 ;;; Besides, they will all be shown at the end. | |
1019 ;;; and comments out the next two lines. | |
1020 (or noninteractive ; already written on stdout. | |
1021 (message "Warning: %s" format)))) | |
1022 | |
1023 ;;; This function should be used to report errors that have halted | |
1024 ;;; compilation of the current file. | |
1025 (defun byte-compile-report-error (error-info) | |
1026 (setq byte-compiler-error-flag t) | |
1027 (byte-compile-log-1 | |
1028 (concat "!! " | |
1029 (format (if (cdr error-info) "%s (%s)" "%s") | |
1030 (get (car error-info) 'error-message) | |
442 | 1031 (prin1-to-string (cdr error-info))))) |
1032 (if stack-trace-on-error | |
1033 (backtrace nil t))) | |
428 | 1034 |
1035 ;;; Used by make-obsolete. | |
1036 (defun byte-compile-obsolete (form) | |
1037 (let ((new (get (car form) 'byte-obsolete-info))) | |
1038 (if (memq 'obsolete byte-compile-warnings) | |
1039 (byte-compile-warn "%s is an obsolete function; %s" (car form) | |
1040 (if (stringp (car new)) | |
1041 (car new) | |
1042 (format "use %s instead." (car new))))) | |
1043 (funcall (or (cdr new) 'byte-compile-normal-call) form))) | |
1044 | |
1045 ;;; Used by make-obsolete. | |
1046 (defun byte-compile-compatible (form) | |
1047 (let ((new (get (car form) 'byte-compatible-info))) | |
1048 (if (memq 'pedantic byte-compile-warnings) | |
1049 (byte-compile-warn "%s is provided for compatibility; %s" (car form) | |
1050 (if (stringp (car new)) | |
1051 (car new) | |
1052 (format "use %s instead." (car new))))) | |
1053 (funcall (or (cdr new) 'byte-compile-normal-call) form))) | |
1054 | |
1055 ;; Compiler options | |
1056 | |
1057 (defconst byte-compiler-legal-options | |
1058 '((optimize byte-optimize (t nil source byte) val) | |
1059 (file-format byte-compile-emacs19-compatibility (emacs19 emacs20) | |
1060 (eq val 'emacs19)) | |
1061 (delete-errors byte-compile-delete-errors (t nil) val) | |
1062 (verbose byte-compile-verbose (t nil) val) | |
1063 (new-bytecodes byte-compile-new-bytecodes (t nil) val) | |
1064 (warnings byte-compile-warnings | |
1065 ((callargs subr-callargs redefine free-vars unused-vars unresolved)) | |
1066 val))) | |
1067 | |
1068 ;; XEmacs addition | |
1069 (defconst byte-compiler-obsolete-options | |
1070 '((new-bytecodes t))) | |
1071 | |
1072 ;; Inhibit v19/v20 selectors if the version is hardcoded. | |
1073 ;; #### This should print a warning if the user tries to change something | |
1074 ;; than can't be changed because the running compiler doesn't support it. | |
1075 (cond | |
1076 ((byte-compile-single-version) | |
1077 (setcar (cdr (cdr (assq 'file-format byte-compiler-legal-options))) | |
1078 (if (byte-compile-version-cond byte-compile-emacs19-compatibility) | |
1079 '(emacs19) '(emacs20))))) | |
1080 | |
1081 ;; now we can copy it. | |
444 | 1082 (setq byte-compiler-legal-options byte-compiler-legal-options) |
428 | 1083 |
1084 (defun byte-compiler-options-handler (&rest args) | |
1085 (let (key val desc choices) | |
1086 (while args | |
1087 (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args)))) | |
1088 (error "malformed byte-compiler-option %s" (car args))) | |
1089 (setq key (car (car args)) | |
1090 val (car (cdr (car args))) | |
1091 desc (assq key byte-compiler-legal-options)) | |
1092 (or desc | |
1093 (error "unknown byte-compiler option %s" key)) | |
1094 (if (assq key byte-compiler-obsolete-options) | |
1095 (byte-compile-warn "%s is an obsolete byte-compiler option." key)) | |
1096 (setq choices (nth 2 desc)) | |
1097 (if (consp (car choices)) | |
1098 (let* (this | |
1099 (handler 'cons) | |
1100 (var (nth 1 desc)) | |
1101 (ret (and (memq (car val) '(+ -)) | |
1102 (copy-sequence (if (eq t (symbol-value var)) | |
1103 (car choices) | |
1104 (symbol-value var)))))) | |
1105 (setq choices (car choices)) | |
1106 (while val | |
1107 (setq this (car val)) | |
1108 (cond ((memq this choices) | |
1109 (setq ret (funcall handler this ret))) | |
1110 ((eq this '+) (setq handler 'cons)) | |
1111 ((eq this '-) (setq handler 'delq)) | |
1112 ((error "%s only accepts %s." key choices))) | |
1113 (setq val (cdr val))) | |
1114 (set (nth 1 desc) ret)) | |
1115 (or (memq val choices) | |
1116 (error "%s must be one of %s." key choices)) | |
1117 (set (nth 1 desc) (eval (nth 3 desc)))) | |
1118 (setq args (cdr args))) | |
1119 nil)) | |
1120 | |
1121 ;;; sanity-checking arglists | |
1122 | |
1123 (defun byte-compile-fdefinition (name macro-p) | |
1124 (let* ((list (if (memq macro-p '(nil subr)) | |
1125 byte-compile-function-environment | |
1126 byte-compile-macro-environment)) | |
1127 (env (cdr (assq name list)))) | |
1128 (or env | |
1129 (let ((fn name)) | |
1130 (while (and (symbolp fn) | |
1131 (fboundp fn) | |
1132 (or (symbolp (symbol-function fn)) | |
1133 (consp (symbol-function fn)) | |
1134 (and (not macro-p) | |
1135 (compiled-function-p (symbol-function fn))) | |
1136 (and (eq macro-p 'subr) (subrp fn)))) | |
1137 (setq fn (symbol-function fn))) | |
1138 (if (or (and (not macro-p) (compiled-function-p fn)) | |
1139 (and (eq macro-p 'subr) (subrp fn))) | |
1140 fn | |
1141 (and (consp fn) | |
1142 (not (eq macro-p 'subr)) | |
1143 (if (eq 'macro (car fn)) | |
1144 (cdr fn) | |
1145 (if macro-p | |
1146 nil | |
1147 (if (eq 'autoload (car fn)) | |
1148 nil | |
1149 fn))))))))) | |
1150 | |
1151 (defun byte-compile-arglist-signature (arglist) | |
1152 (let ((args 0) | |
1153 opts | |
1154 restp) | |
1155 (while arglist | |
1156 (cond ((eq (car arglist) '&optional) | |
1157 (or opts (setq opts 0))) | |
1158 ((eq (car arglist) '&rest) | |
1159 (if (cdr arglist) | |
1160 (setq restp t | |
1161 arglist nil))) | |
1162 (t | |
1163 (if opts | |
1164 (setq opts (1+ opts)) | |
1165 (setq args (1+ args))))) | |
1166 (setq arglist (cdr arglist))) | |
1167 (cons args (if restp nil (if opts (+ args opts) args))))) | |
1168 | |
1169 | |
1170 (defun byte-compile-arglist-signatures-congruent-p (old new) | |
1171 (not (or | |
1172 (> (car new) (car old)) ; requires more args now | |
1173 (and (null (cdr old)) ; tooks rest-args, doesn't any more | |
1174 (cdr new)) | |
1175 (and (cdr new) (cdr old) ; can't take as many args now | |
1176 (< (cdr new) (cdr old))) | |
1177 ))) | |
1178 | |
1179 (defun byte-compile-arglist-signature-string (signature) | |
1180 (cond ((null (cdr signature)) | |
1181 (format "%d+" (car signature))) | |
1182 ((= (car signature) (cdr signature)) | |
1183 (format "%d" (car signature))) | |
1184 (t (format "%d-%d" (car signature) (cdr signature))))) | |
1185 | |
1186 | |
1187 ;; Warn if the form is calling a function with the wrong number of arguments. | |
1188 (defun byte-compile-callargs-warn (form) | |
1189 (let* ((def (or (byte-compile-fdefinition (car form) nil) | |
1190 (byte-compile-fdefinition (car form) t))) | |
1191 (sig (and def (byte-compile-arglist-signature | |
1192 (if (eq 'lambda (car-safe def)) | |
1193 (nth 1 def) | |
1194 (if (compiled-function-p def) | |
1195 (compiled-function-arglist def) | |
1196 '(&rest def)))))) | |
1197 (ncall (length (cdr form)))) | |
1198 (if (and (null def) | |
1199 (fboundp 'subr-min-args) | |
1200 (setq def (byte-compile-fdefinition (car form) 'subr))) | |
1201 (setq sig (cons (subr-min-args def) (subr-max-args def)))) | |
1202 (if sig | |
1203 (if (or (< ncall (car sig)) | |
1204 (and (cdr sig) (> ncall (cdr sig)))) | |
1205 (byte-compile-warn | |
1206 "%s called with %d argument%s, but %s %s" | |
1207 (car form) ncall | |
1208 (if (= 1 ncall) "" "s") | |
1209 (if (< ncall (car sig)) | |
1210 "requires" | |
1211 "accepts only") | |
1212 (byte-compile-arglist-signature-string sig))) | |
1213 (or (fboundp (car form)) ; might be a subr or autoload. | |
1214 ;; ## this doesn't work with recursion. | |
1215 (eq (car form) byte-compile-current-form) | |
1216 ;; It's a currently-undefined function. | |
1217 ;; Remember number of args in call. | |
1218 (let ((cons (assq (car form) byte-compile-unresolved-functions)) | |
1219 (n (length (cdr form)))) | |
1220 (if cons | |
1221 (or (memq n (cdr cons)) | |
1222 (setcdr cons (cons n (cdr cons)))) | |
1223 (setq byte-compile-unresolved-functions | |
1224 (cons (list (car form) n) | |
1225 byte-compile-unresolved-functions)))))))) | |
1226 | |
1227 ;; Warn if the function or macro is being redefined with a different | |
1228 ;; number of arguments. | |
1229 (defun byte-compile-arglist-warn (form macrop) | |
1230 (let ((old (byte-compile-fdefinition (nth 1 form) macrop))) | |
1231 (if old | |
1232 (let ((sig1 (byte-compile-arglist-signature | |
1233 (if (eq 'lambda (car-safe old)) | |
1234 (nth 1 old) | |
1235 (if (compiled-function-p old) | |
1236 (compiled-function-arglist old) | |
1237 '(&rest def))))) | |
1238 (sig2 (byte-compile-arglist-signature (nth 2 form)))) | |
1239 (or (byte-compile-arglist-signatures-congruent-p sig1 sig2) | |
1240 (byte-compile-warn "%s %s used to take %s %s, now takes %s" | |
1241 (if (eq (car form) 'defun) "function" "macro") | |
1242 (nth 1 form) | |
1243 (byte-compile-arglist-signature-string sig1) | |
1244 (if (equal sig1 '(1 . 1)) "argument" "arguments") | |
1245 (byte-compile-arglist-signature-string sig2)))) | |
1246 ;; This is the first definition. See if previous calls are compatible. | |
1247 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions)) | |
1248 nums sig min max) | |
1249 (if calls | |
1250 (progn | |
1251 (setq sig (byte-compile-arglist-signature (nth 2 form)) | |
1252 nums (sort (copy-sequence (cdr calls)) (function <)) | |
1253 min (car nums) | |
1254 max (car (nreverse nums))) | |
1255 (if (or (< min (car sig)) | |
1256 (and (cdr sig) (> max (cdr sig)))) | |
1257 (byte-compile-warn | |
1258 "%s being defined to take %s%s, but was previously called with %s" | |
1259 (nth 1 form) | |
1260 (byte-compile-arglist-signature-string sig) | |
1261 (if (equal sig '(1 . 1)) " arg" " args") | |
1262 (byte-compile-arglist-signature-string (cons min max)))) | |
1263 | |
1264 (setq byte-compile-unresolved-functions | |
1265 (delq calls byte-compile-unresolved-functions))))) | |
1266 ))) | |
1267 | |
1268 ;; If we have compiled any calls to functions which are not known to be | |
1269 ;; defined, issue a warning enumerating them. | |
1270 ;; `unresolved' in the list `byte-compile-warnings' disables this. | |
1271 (defun byte-compile-warn-about-unresolved-functions (&optional msg) | |
1272 (if (memq 'unresolved byte-compile-warnings) | |
1273 (let ((byte-compile-current-form (or msg "the end of the data"))) | |
1274 ;; First delete the autoloads from the list. | |
1275 (if byte-compile-autoload-environment | |
1276 (let ((rest byte-compile-unresolved-functions)) | |
1277 (while rest | |
1278 (if (assq (car (car rest)) byte-compile-autoload-environment) | |
1279 (setq byte-compile-unresolved-functions | |
1280 (delq (car rest) byte-compile-unresolved-functions))) | |
1281 (setq rest (cdr rest))))) | |
1282 ;; Now warn. | |
1283 (if (cdr byte-compile-unresolved-functions) | |
1284 (let* ((str "The following functions are not known to be defined: ") | |
1285 (L (+ (length str) 5)) | |
1286 (rest (reverse byte-compile-unresolved-functions)) | |
1287 s) | |
1288 (while rest | |
1289 (setq s (symbol-name (car (car rest))) | |
1290 L (+ L (length s) 2) | |
1291 rest (cdr rest)) | |
1292 (if (<= L (1- fill-column)) | |
1293 (setq str (concat str " " s (and rest ","))) | |
1294 (setq str (concat str "\n " s (and rest ",")) | |
1295 L (+ (length s) 4)))) | |
1296 (byte-compile-warn "%s" str)) | |
1297 (if byte-compile-unresolved-functions | |
1298 (byte-compile-warn "the function %s is not known to be defined." | |
1299 (car (car byte-compile-unresolved-functions))))))) | |
1300 nil) | |
1301 | |
1302 (defun byte-compile-defvar-p (var) | |
1303 ;; Whether the byte compiler thinks that non-lexical references to this | |
1304 ;; variable are ok. | |
1305 (or (globally-boundp var) | |
1306 (let ((rest byte-compile-bound-variables)) | |
1307 (while (and rest var) | |
1308 (if (and (eq var (car-safe (car rest))) | |
1309 (not (= 0 (logand (cdr (car rest)) | |
1310 byte-compile-global-bit)))) | |
1311 (setq var nil)) | |
1312 (setq rest (cdr rest))) | |
1313 ;; if var is nil at this point, it's a defvar in this file. | |
444 | 1314 (not var)) |
1315 ;; Perhaps (eval-when-compile (defvar foo)) | |
1316 (and (boundp 'current-load-list) | |
1317 (memq var current-load-list)))) | |
428 | 1318 |
1319 | |
1320 ;;; If we have compiled bindings of variables which have no referents, warn. | |
1321 (defun byte-compile-warn-about-unused-variables () | |
1322 (let ((rest byte-compile-bound-variables) | |
1323 (unreferenced '()) | |
1324 cell) | |
1325 (while (and rest | |
1326 ;; only warn about variables whose lifetime is now ending, | |
1327 ;; that is, variables from the lexical scope that is now | |
1328 ;; terminating. (Think nested lets.) | |
1329 (not (eq (car rest) 'new-scope))) | |
1330 (setq cell (car rest)) | |
1331 (if (and (= 0 (logand byte-compile-referenced-bit (cdr cell))) | |
1332 ;; Don't warn about declared-but-unused arguments, | |
1333 ;; for two reasons: first, the arglist structure | |
1334 ;; might be imposed by external forces, and we don't | |
1335 ;; have (declare (ignore x)) yet; and second, inline | |
1336 ;; expansion produces forms like | |
1337 ;; ((lambda (arg) (byte-code "..." [arg])) x) | |
1338 ;; which we can't (ok, well, don't) recognize as | |
1339 ;; containing a reference to arg, so every inline | |
1340 ;; expansion would generate a warning. (If we had | |
1341 ;; `ignore' then inline expansion could emit an | |
1342 ;; ignore declaration.) | |
1343 (= 0 (logand byte-compile-arglist-bit (cdr cell))) | |
1344 ;; Don't warn about defvars because this is a | |
1345 ;; legitimate special binding. | |
1346 (not (byte-compile-defvar-p (car cell)))) | |
1347 (setq unreferenced (cons (car cell) unreferenced))) | |
1348 (setq rest (cdr rest))) | |
1349 (setq unreferenced (nreverse unreferenced)) | |
1350 (while unreferenced | |
1351 (byte-compile-warn | |
1352 (format "variable %s bound but not referenced" (car unreferenced))) | |
1353 (setq unreferenced (cdr unreferenced))))) | |
1354 | |
1355 | |
1356 (defmacro byte-compile-constant-symbol-p (symbol) | |
1357 `(or (keywordp ,symbol) (memq ,symbol '(nil t)))) | |
1358 | |
1359 (defmacro byte-compile-constp (form) | |
1360 ;; Returns non-nil if FORM is a constant. | |
1361 `(cond ((consp ,form) (eq (car ,form) 'quote)) | |
1362 ((symbolp ,form) (byte-compile-constant-symbol-p ,form)) | |
1363 (t))) | |
1364 | |
1365 (defmacro byte-compile-close-variables (&rest body) | |
1366 `(let | |
1367 (;; | |
1368 ;; Close over these variables to encapsulate the | |
1369 ;; compilation state | |
1370 ;; | |
1371 (byte-compile-macro-environment | |
1372 ;; Copy it because the compiler may patch into the | |
1373 ;; macroenvironment. | |
1374 (copy-alist byte-compile-initial-macro-environment)) | |
1375 (byte-compile-function-environment nil) | |
1376 (byte-compile-autoload-environment nil) | |
1377 (byte-compile-unresolved-functions nil) | |
1378 (byte-compile-bound-variables nil) | |
1379 (byte-compile-free-references nil) | |
1380 (byte-compile-free-assignments nil) | |
1381 ;; | |
1382 ;; Close over these variables so that `byte-compiler-options' | |
1383 ;; can change them on a per-file basis. | |
1384 ;; | |
1385 (byte-compile-verbose byte-compile-verbose) | |
1386 (byte-optimize byte-optimize) | |
1387 (byte-compile-emacs19-compatibility | |
1388 byte-compile-emacs19-compatibility) | |
1389 (byte-compile-dynamic byte-compile-dynamic) | |
1390 (byte-compile-dynamic-docstrings | |
1391 byte-compile-dynamic-docstrings) | |
1392 (byte-compile-warnings (if (eq byte-compile-warnings t) | |
1393 byte-compile-default-warnings | |
1394 byte-compile-warnings)) | |
1395 (byte-compile-file-domain nil)) | |
1396 (prog1 | |
1397 (progn ,@body) | |
1398 (if (memq 'unused-vars byte-compile-warnings) | |
1399 ;; done compiling in this scope, warn now. | |
1400 (byte-compile-warn-about-unused-variables))))) | |
1401 | |
1402 | |
1403 (defmacro displaying-byte-compile-warnings (&rest body) | |
1404 `(let* ((byte-compile-log-buffer (get-buffer-create "*Compile-Log*")) | |
1405 (byte-compile-point-max-prev (point-max byte-compile-log-buffer))) | |
1406 ;; Log the file name or buffer name. | |
1407 (byte-compile-log-file) | |
1408 ;; Record how much is logged now. | |
1409 ;; We will display the log buffer if anything more is logged | |
1410 ;; before the end of BODY. | |
1411 (defvar byte-compile-warnings-beginning) | |
1412 (let ((byte-compile-warnings-beginning | |
1413 (if (boundp 'byte-compile-warnings-beginning) | |
1414 byte-compile-warnings-beginning | |
1415 (point-max byte-compile-log-buffer)))) | |
1416 | |
1417 (unwind-protect | |
442 | 1418 (call-with-condition-handler |
1419 #'(lambda (error-info) | |
1420 (byte-compile-report-error error-info)) | |
1421 #'(lambda () | |
1422 (progn ,@body))) | |
428 | 1423 ;; Always set point in log to start of interesting output. |
1424 (with-current-buffer byte-compile-log-buffer | |
1425 (let ((show-begin | |
1426 (progn (goto-char byte-compile-point-max-prev) | |
1427 (skip-chars-forward "\^L\n") | |
1428 (point)))) | |
1429 ;; If there were compilation warnings, display them. | |
1430 (if temp-buffer-show-function | |
1431 (let ((show-buffer (get-buffer-create "*Compile-Log-Show*"))) | |
1432 ;; Always clean show-buffer, even when not displaying it, | |
1433 ;; so that misleading previous messages aren't left around. | |
1434 (with-current-buffer show-buffer | |
1435 (setq buffer-read-only nil) | |
1436 (erase-buffer)) | |
1437 (copy-to-buffer show-buffer show-begin (point-max)) | |
1438 (when (< byte-compile-warnings-beginning (point-max)) | |
1439 (funcall temp-buffer-show-function show-buffer))) | |
1440 (when (< byte-compile-warnings-beginning (point-max)) | |
1441 (select-window | |
1442 (prog1 (selected-window) | |
1443 (select-window (display-buffer (current-buffer))) | |
1444 (goto-char show-begin) | |
1445 (recenter 1))))))))))) | |
1446 | |
1447 | |
1448 ;;;###autoload | |
1449 (defun byte-force-recompile (directory) | |
1450 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file. | |
1451 Files in subdirectories of DIRECTORY are processed also." | |
1452 (interactive "DByte force recompile (directory): ") | |
442 | 1453 (byte-recompile-directory directory nil nil t)) |
428 | 1454 |
1455 ;;;###autoload | |
1456 (defun byte-recompile-directory (directory &optional arg norecursion force) | |
1457 "Recompile every `.el' file in DIRECTORY that needs recompilation. | |
1458 This is if a `.elc' file exists but is older than the `.el' file. | |
444 | 1459 Files in subdirectories of DIRECTORY are also processed unless |
1460 optional argument NORECURSION is non-nil. | |
428 | 1461 |
1462 If the `.elc' file does not exist, normally the `.el' file is *not* compiled. | |
1463 But a prefix argument (optional second arg) means ask user, | |
1464 for each such `.el' file, whether to compile it. Prefix argument 0 means | |
1465 don't ask and compile the file anyway. | |
1466 | |
1467 A nonzero prefix argument also means ask about each subdirectory. | |
1468 | |
444 | 1469 If the fourth optional argument FORCE is non-nil, |
428 | 1470 recompile every `.el' file that already has a `.elc' file." |
1471 (interactive "DByte recompile directory: \nP") | |
1472 (if arg | |
1473 (setq arg (prefix-numeric-value arg))) | |
1474 (if noninteractive | |
1475 nil | |
1476 (save-some-buffers) | |
1477 (redraw-modeline)) | |
1478 (let ((directories (list (expand-file-name directory))) | |
1479 (file-count 0) | |
1480 (dir-count 0) | |
1481 last-dir) | |
1482 (displaying-byte-compile-warnings | |
1483 (while directories | |
1484 (setq directory (file-name-as-directory (car directories))) | |
1485 (or noninteractive (message "Checking %s..." directory)) | |
1486 (let ((files (directory-files directory)) | |
1487 source dest) | |
1488 (while files | |
1489 (setq source (expand-file-name (car files) directory)) | |
1490 (if (and (not (member (car files) '("." ".." "RCS" "CVS" "SCCS"))) | |
1491 ;; Stay away from directory back-links, etc: | |
1492 (not (file-symlink-p source)) | |
1493 (file-directory-p source) | |
1494 byte-recompile-directory-recursively) | |
1495 ;; This file is a subdirectory. Handle them differently. | |
1496 (if (or (null arg) | |
1497 (eq arg 0) | |
1498 (y-or-n-p (concat "Check " source "? "))) | |
1499 (setq directories | |
1500 (nconc directories (list source)))) | |
1501 ;; It is an ordinary file. Decide whether to compile it. | |
1502 (if (and (string-match emacs-lisp-file-regexp source) | |
1503 (not (auto-save-file-name-p source)) | |
502 | 1504 ;; make sure not a mule file we can't handle. |
1505 (or (not byte-recompile-ignore-uncompilable-mule-files) | |
1506 (featurep 'mule) | |
1507 (not (find-coding-system-magic-cookie-in-file | |
1508 source))) | |
428 | 1509 (setq dest (byte-compile-dest-file source)) |
1510 (if (file-exists-p dest) | |
1511 ;; File was already compiled. | |
1512 (or force (file-newer-than-file-p source dest)) | |
1513 ;; No compiled file exists yet. | |
1514 (and arg | |
1515 (or (eq 0 arg) | |
1516 (y-or-n-p (concat "Compile " source "? ")))))) | |
1517 (progn ;(if (and noninteractive (not byte-compile-verbose)) | |
1518 ; (message "Compiling %s..." source)) | |
1519 ; we do this in byte-compile-file. | |
1520 (if byte-recompile-directory-ignore-errors-p | |
1521 (batch-byte-compile-1 source) | |
1522 (byte-compile-file source)) | |
1523 (or noninteractive | |
1524 (message "Checking %s..." directory)) | |
1525 (setq file-count (1+ file-count)) | |
1526 (if (not (eq last-dir directory)) | |
1527 (setq last-dir directory | |
1528 dir-count (1+ dir-count))) | |
1529 ))) | |
1530 (setq files (cdr files)))) | |
1531 (setq directories (cdr directories)))) | |
1532 (message "Done (Total of %d file%s compiled%s)" | |
1533 file-count (if (= file-count 1) "" "s") | |
1534 (if (> dir-count 1) (format " in %d directories" dir-count) "")))) | |
1535 | |
1536 ;;;###autoload | |
1537 (defun byte-recompile-file (filename &optional force) | |
1538 "Recompile a file of Lisp code named FILENAME if it needs recompilation. | |
1539 This is if the `.elc' file exists but is older than the `.el' file. | |
1540 | |
1541 If the `.elc' file does not exist, normally the `.el' file is *not* | |
1542 compiled. But a prefix argument (optional second arg) means ask user | |
1543 whether to compile it. Prefix argument 0 don't ask and recompile anyway." | |
1544 (interactive "fByte recompile file: \nP") | |
1545 (let ((dest)) | |
1546 (if (and (string-match emacs-lisp-file-regexp filename) | |
1547 (not (auto-save-file-name-p filename)) | |
1548 (setq dest (byte-compile-dest-file filename)) | |
1549 (if (file-exists-p dest) | |
1550 (file-newer-than-file-p filename dest) | |
1551 (and force | |
1552 (or (eq 0 force) | |
502 | 1553 (y-or-n-p (concat "Compile " filename "? "))))) |
1554 (or (not byte-recompile-ignore-uncompilable-mule-files) | |
1555 (featurep 'mule) | |
1556 (not (find-coding-system-magic-cookie-in-file filename)))) | |
428 | 1557 (byte-compile-file filename)))) |
1558 | |
1559 ;;;###autoload | |
1560 (defun byte-compile-file (filename &optional load) | |
1561 "Compile a file of Lisp code named FILENAME into a file of byte code. | |
1562 The output file's name is made by appending `c' to the end of FILENAME. | |
1563 With prefix arg (noninteractively: 2nd arg), load the file after compiling." | |
1564 ;; (interactive "fByte compile file: \nP") | |
1565 (interactive | |
1566 (let ((file buffer-file-name) | |
1567 (file-name nil) | |
1568 (file-dir nil)) | |
1569 (and file | |
1570 (eq (cdr (assq 'major-mode (buffer-local-variables))) | |
1571 'emacs-lisp-mode) | |
1572 (setq file-name (file-name-nondirectory file) | |
1573 file-dir (file-name-directory file))) | |
1574 (list (read-file-name (if current-prefix-arg | |
1575 "Byte compile and load file: " | |
1576 "Byte compile file: ") | |
1577 file-dir nil nil file-name) | |
1578 current-prefix-arg))) | |
1579 ;; Expand now so we get the current buffer's defaults | |
1580 (setq filename (expand-file-name filename)) | |
1581 | |
1582 ;; If we're compiling a file that's in a buffer and is modified, offer | |
1583 ;; to save it first. | |
1584 (or noninteractive | |
1585 (let ((b (get-file-buffer (expand-file-name filename)))) | |
1586 (if (and b (buffer-modified-p b) | |
1587 (y-or-n-p (format "save buffer %s first? " (buffer-name b)))) | |
1588 (save-excursion (set-buffer b) (save-buffer))))) | |
1589 | |
1590 (if (or noninteractive byte-compile-verbose) ; XEmacs change | |
1591 (message "Compiling %s..." filename)) | |
1592 (let (;;(byte-compile-current-file (file-name-nondirectory filename)) | |
1593 (byte-compile-current-file filename) | |
1594 target-file input-buffer output-buffer | |
1595 byte-compile-dest-file) | |
1596 (setq target-file (byte-compile-dest-file filename)) | |
1597 (setq byte-compile-dest-file target-file) | |
1598 (save-excursion | |
1599 (setq input-buffer (get-buffer-create " *Compiler Input*")) | |
1600 (set-buffer input-buffer) | |
1601 (erase-buffer) | |
1602 (insert-file-contents filename) | |
1603 ;; Run hooks including the uncompression hook. | |
1604 ;; If they change the file name, then change it for the output also. | |
1605 (let ((buffer-file-name filename) | |
1606 (default-major-mode 'emacs-lisp-mode) | |
1607 (enable-local-eval nil)) | |
1608 (normal-mode) | |
1609 (setq filename buffer-file-name))) | |
1610 (setq byte-compiler-error-flag nil) | |
1611 ;; It is important that input-buffer not be current at this call, | |
1612 ;; so that the value of point set in input-buffer | |
1613 ;; within byte-compile-from-buffer lingers in that buffer. | |
1614 (setq output-buffer (byte-compile-from-buffer input-buffer filename)) | |
1615 (if byte-compiler-error-flag | |
1616 nil | |
1617 (if byte-compile-verbose | |
1618 (message "Compiling %s...done" filename)) | |
1619 (kill-buffer input-buffer) | |
1620 (save-excursion | |
1621 (set-buffer output-buffer) | |
1622 (goto-char (point-max)) | |
1623 (insert "\n") ; aaah, unix. | |
1624 (setq target-file (byte-compile-dest-file filename)) | |
1625 (unless byte-compile-overwrite-file | |
1626 (ignore-file-errors (delete-file target-file))) | |
1627 (if (file-writable-p target-file) | |
4529
6f41fb7f3a65
Protect .elc encoding from latin-unity.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4392
diff
changeset
|
1628 ;; prevent generic hooks from changing our format, eg, |
6f41fb7f3a65
Protect .elc encoding from latin-unity.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4392
diff
changeset
|
1629 ;; latin-unity is known to change the coding system! |
6f41fb7f3a65
Protect .elc encoding from latin-unity.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4392
diff
changeset
|
1630 (let ((write-region-pre-hook nil)) |
6f41fb7f3a65
Protect .elc encoding from latin-unity.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4392
diff
changeset
|
1631 (write-region 1 (point-max) target-file)) |
428 | 1632 ;; This is just to give a better error message than write-region |
1633 (signal 'file-error | |
1634 (list "Opening output file" | |
1635 (if (file-exists-p target-file) | |
1636 "cannot overwrite file" | |
1637 "directory not writable or nonexistent") | |
1638 target-file))) | |
1639 (or byte-compile-overwrite-file | |
1640 (condition-case () | |
1641 (set-file-modes target-file (file-modes filename)) | |
1642 (error nil))) | |
1643 (kill-buffer (current-buffer))) | |
1644 (if (and byte-compile-generate-call-tree | |
1645 (or (eq t byte-compile-generate-call-tree) | |
1646 (y-or-n-p (format "Report call tree for %s? " filename)))) | |
1647 (save-excursion | |
1648 (display-call-tree filename))) | |
1649 (if load | |
1650 (load target-file)) | |
1651 t))) | |
1652 | |
1653 ;; RMS comments the next two out. | |
1654 | |
1655 ;;;###autoload | |
1656 (defun byte-compile-and-load-file (&optional filename) | |
1657 "Compile a file of Lisp code named FILENAME into a file of byte code, | |
1658 and then load it. The output file's name is made by appending \"c\" to | |
1659 the end of FILENAME." | |
1660 (interactive) | |
1661 (if filename ; I don't get it, (interactive-p) doesn't always work | |
1662 (byte-compile-file filename t) | |
1663 (let ((current-prefix-arg '(4))) | |
1664 (call-interactively 'byte-compile-file)))) | |
1665 | |
1666 ;;;###autoload | |
1667 (defun byte-compile-buffer (&optional buffer) | |
1668 "Byte-compile and evaluate contents of BUFFER (default: the current buffer)." | |
1669 (interactive "bByte compile buffer: ") | |
1670 (setq buffer (if buffer (get-buffer buffer) (current-buffer))) | |
1671 (message "Compiling %s..." buffer) | |
1672 (let* ((filename (or (buffer-file-name buffer) | |
1673 (prin1-to-string buffer))) | |
1674 (byte-compile-current-file buffer)) | |
1675 (byte-compile-from-buffer buffer filename t)) | |
1676 (message "Compiling %s...done" buffer) | |
1677 t) | |
1678 | |
1679 ;;; compiling a single function | |
1680 ;;;###autoload | |
1681 (defun compile-defun (&optional arg) | |
1682 "Compile and evaluate the current top-level form. | |
1683 Print the result in the minibuffer. | |
1684 With argument, insert value in current buffer after the form." | |
1685 (interactive "P") | |
1686 (save-excursion | |
1687 (end-of-defun) | |
1688 (beginning-of-defun) | |
1689 (let* ((byte-compile-current-file (buffer-file-name)) | |
1690 (load-file-name (buffer-file-name)) | |
1691 (byte-compile-last-warned-form 'nothing) | |
1692 (value (eval (displaying-byte-compile-warnings | |
1693 (byte-compile-sexp (read (current-buffer)) | |
1694 "toplevel forms"))))) | |
1695 (cond (arg | |
1696 (message "Compiling from buffer... done.") | |
1697 (prin1 value (current-buffer)) | |
1698 (insert "\n")) | |
1699 ((message "%s" (prin1-to-string value))))))) | |
1700 | |
1701 (defun byte-compile-from-buffer (byte-compile-inbuffer filename &optional eval) | |
1702 ;; buffer --> output-buffer, or buffer --> eval form, return nil | |
1703 (let (byte-compile-outbuffer | |
1704 ;; Prevent truncation of flonums and lists as we read and print them | |
1705 (float-output-format nil) | |
1706 (case-fold-search nil) | |
1707 (print-length nil) | |
1708 (print-level nil) | |
1709 ;; Simulate entry to byte-compile-top-level | |
1710 (byte-compile-constants nil) | |
1711 (byte-compile-variables nil) | |
1712 (byte-compile-tag-number 0) | |
1713 (byte-compile-depth 0) | |
1714 (byte-compile-maxdepth 0) | |
1715 (byte-compile-output nil) | |
1716 ;; #### This is bound in b-c-close-variables. | |
1717 ;; (byte-compile-warnings (if (eq byte-compile-warnings t) | |
1718 ;; byte-compile-warning-types | |
1719 ;; byte-compile-warnings)) | |
4623
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1720 (byte-compile-force-escape-quoted byte-compile-force-escape-quoted) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1721 (byte-compile-using-dynamic nil) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1722 (byte-compile-using-escape-quoted nil) |
428 | 1723 ) |
1724 (byte-compile-close-variables | |
1725 (save-excursion | |
1726 (setq byte-compile-outbuffer | |
1727 (set-buffer (get-buffer-create " *Compiler Output*"))) | |
1728 (erase-buffer) | |
1729 ;; (emacs-lisp-mode) | |
1730 (setq case-fold-search nil) | |
1731 (and filename | |
1732 (not eval) | |
1733 (byte-compile-insert-header filename | |
1734 byte-compile-inbuffer | |
1735 byte-compile-outbuffer)) | |
4623
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1736 (setq byte-compile-using-dynamic |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1737 (or (symbol-value-in-buffer 'byte-compile-dynamic |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1738 byte-compile-inbuffer) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1739 (symbol-value-in-buffer 'byte-compile-dynamic-docstrings |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1740 byte-compile-inbuffer))) |
428 | 1741 ;; This is a kludge. Some operating systems (OS/2, DOS) need to |
1742 ;; write files containing binary information specially. | |
1743 ;; Under most circumstances, such files will be in binary | |
1744 ;; overwrite mode, so those OS's use that flag to guess how | |
1745 ;; they should write their data. Advise them that .elc files | |
1746 ;; need to be written carefully. | |
1747 (setq overwrite-mode 'overwrite-mode-binary)) | |
1748 (displaying-byte-compile-warnings | |
1749 (save-excursion | |
4623
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1750 ;; All our save-excursions may have led to a less-than-useful |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1751 ;; value for point in the outbuffer: |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1752 (goto-char (point-max byte-compile-outbuffer) byte-compile-outbuffer) |
428 | 1753 (set-buffer byte-compile-inbuffer) |
1754 (goto-char 1) | |
1755 | |
1756 ;; Compile the forms from the input buffer. | |
1757 (while (progn | |
1758 (while (progn (skip-chars-forward " \t\n\^L") | |
1759 (looking-at ";")) | |
1760 (forward-line 1)) | |
1761 (not (eobp))) | |
1762 (byte-compile-file-form (read byte-compile-inbuffer))) | |
1763 | |
1764 ;; Compile pending forms at end of file. | |
1765 (byte-compile-flush-pending) | |
1766 (byte-compile-warn-about-unresolved-functions) | |
1767 ;; Should we always do this? When calling multiple files, it | |
1768 ;; would be useful to delay this warning until all have | |
1769 ;; been compiled. | |
1770 (setq byte-compile-unresolved-functions nil))) | |
1771 (save-excursion | |
1772 (set-buffer byte-compile-outbuffer) | |
4623
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1773 (goto-char (point-min)) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1774 (when (and (or byte-compile-using-dynamic |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1775 (eq buffer-file-coding-system 'raw-text-unix)) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1776 (re-search-forward "[^\x00-\xff]" nil t)) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1777 (when (or noninteractive byte-compile-verbose) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1778 (message |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1779 "%s: includes char above ?\\xFF, recompiling sans dynamic features." |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1780 filename)) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1781 (set-symbol-value-in-buffer 'byte-compile-dynamic nil |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1782 byte-compile-inbuffer) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1783 (set-symbol-value-in-buffer 'byte-compile-dynamic-docstrings nil |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1784 byte-compile-inbuffer) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1785 (setq byte-compile-force-escape-quoted t |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1786 byte-compile-outbuffer |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1787 (byte-compile-from-buffer byte-compile-inbuffer |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1788 filename eval))))) |
428 | 1789 (if (not eval) |
1790 byte-compile-outbuffer | |
1791 (let (form) | |
1792 (while (condition-case nil | |
1793 (progn (setq form (read byte-compile-outbuffer)) | |
1794 t) | |
1795 (end-of-file nil)) | |
1796 (eval form))) | |
1797 (kill-buffer byte-compile-outbuffer) | |
1798 nil))) | |
1799 | |
1800 (defun byte-compile-insert-header (filename byte-compile-inbuffer | |
1801 byte-compile-outbuffer) | |
1802 (set-buffer byte-compile-inbuffer) | |
1803 (let ((dynamic-docstrings byte-compile-dynamic-docstrings)) | |
1804 (set-buffer byte-compile-outbuffer) | |
1805 (goto-char 1) | |
1806 ;; | |
1807 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After that is | |
1808 ;; the file-format version number (19 or 20) as a byte, followed by some | |
1809 ;; nulls. The primary motivation for doing this is to get some binary | |
1810 ;; characters up in the first line of the file so that `diff' will simply | |
1811 ;; say "Binary files differ" instead of actually doing a diff of two .elc | |
1812 ;; files. An extra benefit is that you can add this to /etc/magic: | |
1813 ;; | |
613 | 1814 ;; 0 string ;ELC XEmacs Lisp compiled file, |
428 | 1815 ;; >4 byte x version %d |
1816 ;; | |
1817 (insert | |
1818 ";ELC" | |
1819 (if (byte-compile-version-cond byte-compile-emacs19-compatibility) 19 20) | |
1820 "\000\000\000\n" | |
1821 ) | |
1822 (insert ";;; compiled by " | |
1823 (or (and (boundp 'user-mail-address) user-mail-address) | |
1824 (concat (user-login-name) "@" (system-name))) | |
1825 " on " | |
1826 (current-time-string) "\n;;; from file " filename "\n") | |
1827 (insert ";;; emacs version " emacs-version ".\n") | |
1828 (insert ";;; bytecomp version " byte-compile-version "\n;;; " | |
1829 (cond | |
1830 ((eq byte-optimize 'source) "source-level optimization only") | |
1831 ((eq byte-optimize 'byte) "byte-level optimization only") | |
1832 (byte-optimize "optimization is on") | |
1833 (t "optimization is off")) | |
1834 (if (byte-compile-version-cond byte-compile-emacs19-compatibility) | |
1835 "; compiled with Emacs 19 compatibility.\n" | |
1836 ".\n")) | |
1837 (if (not (byte-compile-version-cond byte-compile-emacs19-compatibility)) | |
1838 (insert ";;; this file uses opcodes which do not exist in Emacs 19.\n" | |
1839 ;; Have to check if emacs-version is bound so that this works | |
1840 ;; in files loaded early in loadup.el. | |
1841 "\n(if (and (boundp 'emacs-version)\n" | |
1842 "\t (or (and (boundp 'epoch::version) epoch::version)\n" | |
1843 "\t (string-lessp emacs-version \"20\")))\n" | |
1844 " (error \"`" | |
1845 ;; prin1-to-string is used to quote backslashes. | |
1846 (substring (prin1-to-string (file-name-nondirectory filename)) | |
1847 1 -1) | |
1848 "' was compiled for Emacs 20\"))\n\n")) | |
1849 (insert "(or (boundp 'current-load-list) (setq current-load-list nil))\n" | |
1850 "\n") | |
1851 (if (and (byte-compile-version-cond byte-compile-emacs19-compatibility) | |
1852 dynamic-docstrings) | |
1853 (insert ";;; this file uses opcodes which do not exist prior to\n" | |
1854 ";;; XEmacs 19.14/GNU Emacs 19.29 or later." | |
1855 ;; Have to check if emacs-version is bound so that this works | |
1856 ;; in files loaded early in loadup.el. | |
1857 "\n(if (and (boundp 'emacs-version)\n" | |
1858 "\t (or (and (boundp 'epoch::version) epoch::version)\n" | |
1859 "\t (and (not (string-match \"XEmacs\" emacs-version))\n" | |
1860 "\t (string-lessp emacs-version \"19.29\"))\n" | |
1861 "\t (string-lessp emacs-version \"19.14\")))\n" | |
1862 " (error \"`" | |
1863 ;; prin1-to-string is used to quote backslashes. | |
1864 (substring (prin1-to-string (file-name-nondirectory filename)) | |
1865 1 -1) | |
1866 "' was compiled for XEmacs 19.14/Emacs 19.29 or later\"))\n\n" | |
1867 ) | |
1868 )) | |
1869 | |
1870 ;; back in the inbuffer; determine and set the coding system for the .elc | |
1871 ;; file if under Mule. If there are any extended characters in the | |
1872 ;; input file, use `escape-quoted' to make sure that both binary and | |
1873 ;; extended characters are output properly and distinguished properly. | |
1874 ;; Otherwise, use `raw-text' for maximum portability with non-Mule | |
1875 ;; Emacsen. | |
771 | 1876 (if (or (featurep '(not mule)) ;; Don't scan buffer if we are not muleized |
4623
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1877 (and |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1878 (not byte-compile-force-escape-quoted) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1879 (save-excursion |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1880 (set-buffer byte-compile-inbuffer) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1881 (goto-char (point-min)) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1882 ;; Look for any non-Latin-1 literals or Unicode character |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1883 ;; escapes. Any such occurrences in a @#COUNT comment will lead |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1884 ;; to an escape-quoted coding cookie being inserted, but this is |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1885 ;; not true of ordinary comments. |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1886 (let ((non-latin-1-re |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1887 (concat "[^\000-\377]" |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1888 #r"\|\\u[0-9a-fA-F]\{4,4\}\|\\U[0-9a-fA-F]\{8,8\}")) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1889 (case-fold-search nil)) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1890 (catch 'need-to-escape-quote |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1891 (while (re-search-forward non-latin-1-re nil t) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1892 (skip-chars-backward "^;" (point-at-bol)) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1893 (if (bolp) (throw 'need-to-escape-quote nil)) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1894 (forward-line 1)) |
a9f83990e6bf
Fix a byte compiler bug with characters above ?\xFF.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4539
diff
changeset
|
1895 t))))) |
771 | 1896 (setq buffer-file-coding-system 'raw-text-unix) |
1315 | 1897 (insert "(or (featurep 'mule) (error \"Loading this file requires Mule support\")) |
1898 ;;;###coding system: escape-quoted\n") | |
771 | 1899 (setq buffer-file-coding-system 'escape-quoted) |
1900 ;; #### Lazy loading not yet implemented for MULE files | |
1901 ;; mrb - Fix this someday. | |
1902 (save-excursion | |
1903 (set-buffer byte-compile-inbuffer) | |
1904 (setq byte-compile-dynamic nil | |
1905 byte-compile-dynamic-docstrings nil)) | |
1906 ;;(external-debugging-output (prin1-to-string (buffer-local-variables)))) | |
1907 ) | |
428 | 1908 ) |
1909 | |
1910 | |
1911 (defun byte-compile-output-file-form (form) | |
1912 ;; writes the given form to the output buffer, being careful of docstrings | |
1913 ;; in defun, defmacro, defvar, defconst and autoload because make-docfile is | |
1914 ;; so amazingly stupid. | |
1915 ;; defalias calls are output directly by byte-compile-file-form-defmumble; | |
1916 ;; it does not pay to first build the defalias in defmumble and then parse | |
1917 ;; it here. | |
4539
061e030e3270
Fix some bugs in load-history construction, built-in symbol file names.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4529
diff
changeset
|
1918 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload |
061e030e3270
Fix some bugs in load-history construction, built-in symbol file names.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4529
diff
changeset
|
1919 custom-declare-variable)) |
428 | 1920 (stringp (nth 3 form))) |
1921 (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil | |
4539
061e030e3270
Fix some bugs in load-history construction, built-in symbol file names.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4529
diff
changeset
|
1922 (memq (car form) |
061e030e3270
Fix some bugs in load-history construction, built-in symbol file names.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4529
diff
changeset
|
1923 '(autoload custom-declare-variable))) |
428 | 1924 (let ((print-escape-newlines t) |
1925 (print-length nil) | |
1926 (print-level nil) | |
1927 (print-readably t) ; print #[] for bytecode, 'x for (quote x) | |
1928 (print-gensym (if (and byte-compile-print-gensym | |
1929 (not byte-compile-emacs19-compatibility)) | |
4392
e8f448f997ac
bytecomp.el: bind print-gensym-alist to nil even with non-defvar, defun, [...] forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4304
diff
changeset
|
1930 '(t) nil)) |
e8f448f997ac
bytecomp.el: bind print-gensym-alist to nil even with non-defvar, defun, [...] forms.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4304
diff
changeset
|
1931 print-gensym-alist) |
428 | 1932 (princ "\n" byte-compile-outbuffer) |
1933 (prin1 form byte-compile-outbuffer) | |
1934 nil))) | |
1935 | |
1936 (defun byte-compile-output-docform (preface name info form specindex quoted) | |
1937 "Print a form with a doc string. INFO is (prefix doc-index postfix). | |
1938 If PREFACE and NAME are non-nil, print them too, | |
1939 before INFO and the FORM but after the doc string itself. | |
1940 If SPECINDEX is non-nil, it is the index in FORM | |
1941 of the function bytecode string. In that case, | |
1942 we output that argument and the following argument (the constants vector) | |
1943 together, for lazy loading. | |
1944 QUOTED says that we have to put a quote before the | |
1945 list that represents a doc string reference. | |
1946 `autoload' needs that." | |
1947 ;; We need to examine byte-compile-dynamic-docstrings | |
1948 ;; in the input buffer (now current), not in the output buffer. | |
1949 (let ((dynamic-docstrings byte-compile-dynamic-docstrings)) | |
1950 (set-buffer | |
1951 (prog1 (current-buffer) | |
1952 (set-buffer byte-compile-outbuffer) | |
1953 (let (position) | |
1954 | |
1955 ;; Insert the doc string, and make it a comment with #@LENGTH. | |
1956 (and (>= (nth 1 info) 0) | |
1957 dynamic-docstrings | |
1958 (progn | |
1959 ;; Make the doc string start at beginning of line | |
1960 ;; for make-docfile's sake. | |
1961 (insert "\n") | |
1962 (setq position | |
1963 (byte-compile-output-as-comment | |
1964 (nth (nth 1 info) form) nil)) | |
1965 ;; If the doc string starts with * (a user variable), | |
1966 ;; negate POSITION. | |
1967 (if (and (stringp (nth (nth 1 info) form)) | |
1968 (> (length (nth (nth 1 info) form)) 0) | |
1969 (char= (aref (nth (nth 1 info) form) 0) ?*)) | |
1970 (setq position (- position))))) | |
1971 | |
1972 (if preface | |
1973 (progn | |
1974 (insert preface) | |
1975 (prin1 name byte-compile-outbuffer))) | |
1976 (insert (car info)) | |
1977 (let ((print-escape-newlines t) | |
1978 (print-readably t) ; print #[] for bytecode, 'x for (quote x) | |
1979 ;; Use a cons cell to say that we want | |
1980 ;; print-gensym-alist not to be cleared between calls | |
1981 ;; to print functions. | |
1982 (print-gensym (if (and byte-compile-print-gensym | |
1983 (not byte-compile-emacs19-compatibility)) | |
1984 '(t) nil)) | |
1985 print-gensym-alist | |
1986 (index 0)) | |
1987 (prin1 (car form) byte-compile-outbuffer) | |
1988 (while (setq form (cdr form)) | |
1989 (setq index (1+ index)) | |
1990 (insert " ") | |
1991 (cond ((and (numberp specindex) (= index specindex)) | |
1992 (let ((position | |
1993 (byte-compile-output-as-comment | |
1994 (cons (car form) (nth 1 form)) | |
1995 t))) | |
1996 (princ (format "(#$ . %d) nil" position) | |
1997 byte-compile-outbuffer) | |
1998 (setq form (cdr form)) | |
1999 (setq index (1+ index)))) | |
2000 ((= index (nth 1 info)) | |
2001 (if position | |
2002 (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)") | |
2003 position) | |
2004 byte-compile-outbuffer) | |
2005 (let ((print-escape-newlines nil)) | |
2006 (goto-char (prog1 (1+ (point)) | |
2007 (prin1 (car form) | |
2008 byte-compile-outbuffer))) | |
2009 (insert "\\\n") | |
2010 (goto-char (point-max))))) | |
2011 (t | |
2012 (prin1 (car form) byte-compile-outbuffer))))) | |
2013 (insert (nth 2 info)))))) | |
2014 nil) | |
2015 | |
2016 (defvar for-effect) ; ## Kludge! This should be an arg, not a special. | |
2017 | |
2018 (defun byte-compile-keep-pending (form &optional handler) | |
2019 (if (memq byte-optimize '(t source)) | |
2020 (setq form (byte-optimize-form form t))) | |
2021 (if handler | |
2022 (let ((for-effect t)) | |
2023 ;; To avoid consing up monstrously large forms at load time, we split | |
2024 ;; the output regularly. | |
2025 (and (memq (car-safe form) '(fset defalias define-function)) | |
2026 (nthcdr 300 byte-compile-output) | |
2027 (byte-compile-flush-pending)) | |
2028 (funcall handler form) | |
2029 (when for-effect | |
2030 (byte-compile-discard))) | |
2031 (byte-compile-form form t)) | |
2032 nil) | |
2033 | |
2034 (defun byte-compile-flush-pending () | |
2035 (if byte-compile-output | |
2036 (let ((form (byte-compile-out-toplevel t 'file))) | |
2037 (cond ((eq (car-safe form) 'progn) | |
2038 (mapcar 'byte-compile-output-file-form (cdr form))) | |
2039 (form | |
2040 (byte-compile-output-file-form form))) | |
2041 (setq byte-compile-constants nil | |
2042 byte-compile-variables nil | |
2043 byte-compile-depth 0 | |
2044 byte-compile-maxdepth 0 | |
2045 byte-compile-output nil)))) | |
2046 | |
2047 (defun byte-compile-file-form (form) | |
2048 (let ((byte-compile-current-form nil) ; close over this for warnings. | |
2049 handler) | |
2050 (cond | |
2051 ((not (consp form)) | |
2052 (byte-compile-keep-pending form)) | |
2053 ((and (symbolp (car form)) | |
2054 (setq handler (get (car form) 'byte-hunk-handler))) | |
2055 (cond ((setq form (funcall handler form)) | |
2056 (byte-compile-flush-pending) | |
2057 (byte-compile-output-file-form form)))) | |
2058 ((eq form (setq form (macroexpand form byte-compile-macro-environment))) | |
2059 (byte-compile-keep-pending form)) | |
2060 (t | |
2061 (byte-compile-file-form form))))) | |
2062 | |
2063 ;; Functions and variables with doc strings must be output separately, | |
2064 ;; so make-docfile can recognize them. Most other things can be output | |
2065 ;; as byte-code. | |
2066 | |
2067 (put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst) | |
2068 (defun byte-compile-file-form-defsubst (form) | |
2069 (cond ((assq (nth 1 form) byte-compile-unresolved-functions) | |
2070 (setq byte-compile-current-form (nth 1 form)) | |
2071 (byte-compile-warn "defsubst %s was used before it was defined" | |
2072 (nth 1 form)))) | |
2073 (byte-compile-file-form | |
2074 (macroexpand form byte-compile-macro-environment)) | |
2075 ;; Return nil so the form is not output twice. | |
2076 nil) | |
2077 | |
2078 (put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload) | |
2079 (defun byte-compile-file-form-autoload (form) | |
2080 ;; | |
2081 ;; If this is an autoload of a macro, and all arguments are constants (that | |
2082 ;; is, there is no hairy computation going on here) then evaluate the form | |
2083 ;; at compile-time. This is so that we can make use of macros which we | |
2084 ;; have autoloaded from the file being compiled. Normal function autoloads | |
2085 ;; are not automatically evaluated at compile time, because there's not | |
2086 ;; much point to it (so why bother cluttering up the compile-time namespace.) | |
2087 ;; | |
2088 ;; If this is an autoload of a function, then record its definition in the | |
2089 ;; byte-compile-autoload-environment to suppress any `not known to be | |
2090 ;; defined' warnings at the end of this file (this only matters for | |
2091 ;; functions which are autoloaded and compiled in the same file, if the | |
2092 ;; autoload already exists in the compilation environment, we wouldn't have | |
2093 ;; warned anyway.) | |
2094 ;; | |
2095 (let* ((name (if (byte-compile-constp (nth 1 form)) | |
2096 (eval (nth 1 form)))) | |
2097 ;; In v19, the 5th arg to autoload can be t, nil, 'macro, or 'keymap. | |
2098 (macrop (and (byte-compile-constp (nth 5 form)) | |
2099 (memq (eval (nth 5 form)) '(t macro)))) | |
2100 ;; (functionp (and (byte-compile-constp (nth 5 form)) | |
2101 ;; (eq 'nil (eval (nth 5 form))))) | |
2102 ) | |
2103 (if (and macrop | |
2104 (let ((form form)) | |
2105 ;; all forms are constant | |
2106 (while (if (setq form (cdr form)) | |
2107 (byte-compile-constp (car form)))) | |
2108 (null form))) | |
440 | 2109 ;; eval the macro autoload into the compilation environment |
428 | 2110 (eval form)) |
2111 | |
2112 (if name | |
2113 (let ((old (assq name byte-compile-autoload-environment))) | |
2114 (cond (old | |
2115 (if (memq 'redefine byte-compile-warnings) | |
2116 (byte-compile-warn "multiple autoloads for %s" name)) | |
2117 (setcdr old form)) | |
2118 (t | |
2119 ;; We only use the names in the autoload environment, but | |
2120 ;; it might be useful to have the bodies some day. | |
2121 (setq byte-compile-autoload-environment | |
2122 (cons (cons name form) | |
2123 byte-compile-autoload-environment))))))) | |
2124 ;; | |
2125 ;; Now output the form. | |
2126 (if (stringp (nth 3 form)) | |
2127 form | |
2128 ;; No doc string, so we can compile this as a normal form. | |
2129 (byte-compile-keep-pending form 'byte-compile-normal-call))) | |
2130 | |
442 | 2131 (put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar-or-defconst) |
2132 (put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar-or-defconst) | |
2133 (defun byte-compile-file-form-defvar-or-defconst (form) | |
2134 ;; (defvar|defconst VAR [VALUE [DOCSTRING]]) | |
428 | 2135 (if (> (length form) 4) |
442 | 2136 (byte-compile-warn |
2137 "%s %s called with %d arguments, but accepts only %s" | |
2138 (car form) (nth 1 form) (length (cdr form)) 3)) | |
428 | 2139 (if (and (> (length form) 3) (not (stringp (nth 3 form)))) |
2140 (byte-compile-warn "Third arg to %s %s is not a string: %s" | |
2141 (car form) (nth 1 form) (nth 3 form))) | |
2142 (if (null (nth 3 form)) | |
2143 ;; Since there is no doc string, we can compile this as a normal form, | |
2144 ;; and not do a file-boundary. | |
2145 (byte-compile-keep-pending form) | |
2146 (if (memq 'free-vars byte-compile-warnings) | |
2147 (setq byte-compile-bound-variables | |
2148 (cons (cons (nth 1 form) byte-compile-global-bit) | |
2149 byte-compile-bound-variables))) | |
2150 (cond ((consp (nth 2 form)) | |
2151 (setq form (copy-sequence form)) | |
2152 (setcar (cdr (cdr form)) | |
2153 (byte-compile-top-level (nth 2 form) nil 'file)))) | |
2154 | |
2155 ;; The following turns out not to be necessary, since we emit a call to | |
2156 ;; defvar, which can hack Vfile_domain by itself! | |
2157 ;; | |
2158 ;; If a file domain has been set, emit (put 'VAR 'variable-domain ...) | |
2159 ;; after this defvar. | |
2160 ; (if byte-compile-file-domain | |
2161 ; (progn | |
2162 ; ;; Actually, this will emit the (put ...) before the (defvar ...) | |
2163 ; ;; but I don't think that can matter in this case. | |
2164 ; (byte-compile-keep-pending | |
2165 ; (list 'put (list 'quote (nth 1 form)) ''variable-domain | |
2166 ; (list 'quote byte-compile-file-domain))))) | |
2167 form)) | |
2168 | |
2169 (put 'require 'byte-hunk-handler 'byte-compile-file-form-eval-boundary) | |
2170 (defun byte-compile-file-form-eval-boundary (form) | |
2171 (eval form) | |
2172 (byte-compile-keep-pending form 'byte-compile-normal-call)) | |
2173 | |
2174 (put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn) | |
2175 (put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn) | |
2176 (put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn) | |
2177 (defun byte-compile-file-form-progn (form) | |
2178 (mapcar 'byte-compile-file-form (cdr form)) | |
2179 ;; Return nil so the forms are not output twice. | |
2180 nil) | |
2181 | |
2182 ;; This handler is not necessary, but it makes the output from dont-compile | |
2183 ;; and similar macros cleaner. | |
2184 (put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval) | |
2185 (defun byte-compile-file-form-eval (form) | |
2186 (if (eq (car-safe (nth 1 form)) 'quote) | |
2187 (nth 1 (nth 1 form)) | |
2188 (byte-compile-keep-pending form))) | |
2189 | |
2190 (put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun) | |
2191 (defun byte-compile-file-form-defun (form) | |
2192 (byte-compile-file-form-defmumble form nil)) | |
2193 | |
2194 (put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro) | |
2195 (defun byte-compile-file-form-defmacro (form) | |
2196 (byte-compile-file-form-defmumble form t)) | |
2197 | |
2198 (defun byte-compile-compiled-obj-to-list (obj) | |
2199 ;; #### this is fairly disgusting. Rewrite the code instead | |
2200 ;; so that it doesn't create compiled objects in the first place! | |
2201 ;; Much better than creating them and then "uncreating" them | |
2202 ;; like this. | |
2203 (read (concat "(" | |
2204 (substring (let ((print-readably t) | |
2205 (print-gensym | |
2206 (if (and byte-compile-print-gensym | |
2207 (not byte-compile-emacs19-compatibility)) | |
2208 '(t) nil)) | |
2209 (print-gensym-alist nil)) | |
2210 (prin1-to-string obj)) | |
2211 2 -1) | |
2212 ")"))) | |
2213 | |
2214 (defun byte-compile-file-form-defmumble (form macrop) | |
2215 (let* ((name (car (cdr form))) | |
2216 (this-kind (if macrop 'byte-compile-macro-environment | |
2217 'byte-compile-function-environment)) | |
2218 (that-kind (if macrop 'byte-compile-function-environment | |
2219 'byte-compile-macro-environment)) | |
2220 (this-one (assq name (symbol-value this-kind))) | |
2221 (that-one (assq name (symbol-value that-kind))) | |
2222 (byte-compile-free-references nil) | |
2223 (byte-compile-free-assignments nil)) | |
2224 | |
2225 ;; When a function or macro is defined, add it to the call tree so that | |
2226 ;; we can tell when functions are not used. | |
2227 (if byte-compile-generate-call-tree | |
2228 (or (assq name byte-compile-call-tree) | |
2229 (setq byte-compile-call-tree | |
2230 (cons (list name nil nil) byte-compile-call-tree)))) | |
2231 | |
2232 (setq byte-compile-current-form name) ; for warnings | |
2233 (when (memq 'redefine byte-compile-warnings) | |
2234 (byte-compile-arglist-warn form macrop)) | |
2235 (defvar filename) ; #### filename used free | |
2236 (when byte-compile-verbose | |
2237 (message "Compiling %s... (%s)" | |
2238 (if filename (file-name-nondirectory filename) "") | |
2239 (nth 1 form))) | |
2240 (cond (that-one | |
2241 (when (and (memq 'redefine byte-compile-warnings) | |
2242 ;; hack hack: don't warn when compiling the stubs in | |
2243 ;; bytecomp-runtime... | |
2244 (not (assq (nth 1 form) | |
2245 byte-compile-initial-macro-environment))) | |
2246 (byte-compile-warn | |
2247 "%s defined multiple times, as both function and macro" | |
2248 (nth 1 form))) | |
2249 (setcdr that-one nil)) | |
2250 (this-one | |
2251 (when (and (memq 'redefine byte-compile-warnings) | |
2252 ;; hack: don't warn when compiling the magic internal | |
2253 ;; byte-compiler macros in bytecomp-runtime.el... | |
2254 (not (assq (nth 1 form) | |
2255 byte-compile-initial-macro-environment))) | |
2256 (byte-compile-warn "%s %s defined multiple times in this file" | |
2257 (if macrop "macro" "function") | |
2258 (nth 1 form)))) | |
2259 ((and (fboundp name) | |
2260 (or (subrp (symbol-function name)) | |
2261 (eq (car-safe (symbol-function name)) | |
2262 (if macrop 'lambda 'macro)))) | |
2263 (if (memq 'redefine byte-compile-warnings) | |
2264 (byte-compile-warn "%s %s being redefined as a %s" | |
2265 (if (subrp (symbol-function name)) | |
2266 "subr" | |
2267 (if macrop "function" "macro")) | |
2268 (nth 1 form) | |
2269 (if macrop "macro" "function"))) | |
2270 ;; shadow existing definition | |
2271 (set this-kind | |
2272 (cons (cons name nil) (symbol-value this-kind))))) | |
2273 (let ((body (nthcdr 3 form))) | |
2274 (if (and (stringp (car body)) | |
2275 (symbolp (car-safe (cdr-safe body))) | |
2276 (car-safe (cdr-safe body)) | |
2277 (stringp (car-safe (cdr-safe (cdr-safe body))))) | |
2278 (byte-compile-warn "Probable `\"' without `\\' in doc string of %s" | |
2279 (nth 1 form)))) | |
2280 (let* ((new-one (byte-compile-lambda (cons 'lambda (nthcdr 2 form)))) | |
2281 (code (byte-compile-byte-code-maker new-one))) | |
2282 (if this-one | |
2283 (setcdr this-one new-one) | |
2284 (set this-kind | |
2285 (cons (cons name new-one) (symbol-value this-kind)))) | |
2286 (if (and (stringp (nth 3 form)) | |
2287 (eq 'quote (car-safe code)) | |
2288 (eq 'lambda (car-safe (nth 1 code)))) | |
2289 (cons (car form) | |
2290 (cons name (cdr (nth 1 code)))) | |
2291 (byte-compile-flush-pending) | |
2292 (if (not (stringp (nth 3 form))) | |
2293 ;; No doc string. Provide -1 as the "doc string index" | |
2294 ;; so that no element will be treated as a doc string. | |
2295 (byte-compile-output-docform | |
2296 "\n(defalias '" | |
2297 name | |
2298 (cond ((atom code) | |
2299 (if macrop '(" '(macro . #[" -1 "])") '(" #[" -1 "]"))) | |
2300 ((eq (car code) 'quote) | |
2301 (setq code new-one) | |
2302 (if macrop '(" '(macro " -1 ")") '(" '(" -1 ")"))) | |
2303 ((if macrop '(" (cons 'macro (" -1 "))") '(" (" -1 ")")))) | |
2304 ;; FSF just calls `(append code nil)' here but that relies | |
2305 ;; on horrible C kludges in concat() that accept byte- | |
2306 ;; compiled objects and pretend they're vectors. | |
2307 (if (compiled-function-p code) | |
2308 (byte-compile-compiled-obj-to-list code) | |
2309 (append code nil)) | |
2310 (and (atom code) byte-compile-dynamic | |
2311 1) | |
2312 nil) | |
2313 ;; Output the form by hand, that's much simpler than having | |
2314 ;; b-c-output-file-form analyze the defalias. | |
2315 (byte-compile-output-docform | |
2316 "\n(defalias '" | |
2317 name | |
2318 (cond ((atom code) ; compiled-function-p | |
2319 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]"))) | |
2320 ((eq (car code) 'quote) | |
2321 (setq code new-one) | |
2322 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")"))) | |
2323 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")")))) | |
2324 ;; The result of byte-compile-byte-code-maker is either a | |
2325 ;; compiled-function object, or a list of some kind. If it's | |
2326 ;; not a cons, we must coerce it into a list of the elements | |
2327 ;; to be printed to the file. | |
2328 (if (consp code) | |
2329 code | |
2330 (nconc (list | |
2331 (compiled-function-arglist code) | |
2332 (compiled-function-instructions code) | |
2333 (compiled-function-constants code) | |
2334 (compiled-function-stack-depth code)) | |
2335 (let ((doc (documentation code t))) | |
2336 (if doc (list doc))) | |
2337 (if (commandp code) | |
2338 (list (nth 1 (compiled-function-interactive code)))))) | |
2339 (and (atom code) byte-compile-dynamic | |
2340 1) | |
2341 nil)) | |
2342 (princ ")" byte-compile-outbuffer) | |
2343 nil)))) | |
2344 | |
2345 ;; Print Lisp object EXP in the output file, inside a comment, | |
2346 ;; and return the file position it will have. | |
2347 ;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting. | |
2348 (defun byte-compile-output-as-comment (exp quoted) | |
2349 (let ((position (point))) | |
2350 (set-buffer | |
2351 (prog1 (current-buffer) | |
2352 (set-buffer byte-compile-outbuffer) | |
2353 | |
2354 ;; Insert EXP, and make it a comment with #@LENGTH. | |
2355 (insert " ") | |
2356 (if quoted | |
2357 (prin1 exp byte-compile-outbuffer) | |
2358 (princ exp byte-compile-outbuffer)) | |
2359 (goto-char position) | |
2360 ;; Quote certain special characters as needed. | |
2361 ;; get_doc_string in doc.c does the unquoting. | |
2362 (while (search-forward "\^A" nil t) | |
2363 (replace-match "\^A\^A" t t)) | |
2364 (goto-char position) | |
2365 (while (search-forward "\000" nil t) | |
2366 (replace-match "\^A0" t t)) | |
2367 (goto-char position) | |
2368 (while (search-forward "\037" nil t) | |
2369 (replace-match "\^A_" t t)) | |
2370 (goto-char (point-max)) | |
2371 (insert "\037") | |
2372 (goto-char position) | |
2373 (insert "#@" (format "%d" (- (point-max) position))) | |
2374 | |
2375 ;; Save the file position of the object. | |
2376 ;; Note we should add 1 to skip the space | |
2377 ;; that we inserted before the actual doc string, | |
2378 ;; and subtract 1 to convert from an 1-origin Emacs position | |
2379 ;; to a file position; they cancel. | |
2380 (setq position (point)) | |
2381 (goto-char (point-max)))) | |
2382 position)) | |
2383 | |
2384 | |
2385 | |
2386 ;; The `domain' declaration. This is legal only at top-level in a file, and | |
2387 ;; should generally be the first form in the file. It is not legal inside | |
2388 ;; function bodies. | |
2389 | |
2390 (put 'domain 'byte-hunk-handler 'byte-compile-file-form-domain) | |
2391 (defun byte-compile-file-form-domain (form) | |
2392 (if (not (null (cdr (cdr form)))) | |
2393 (byte-compile-warn "domain used with too many arguments: %s" form)) | |
2394 (let ((domain (nth 1 form))) | |
2395 (or (null domain) | |
2396 (stringp domain) | |
2397 (progn | |
2398 (byte-compile-warn | |
2399 "argument to `domain' declaration must be a literal string: %s" | |
2400 form) | |
2401 (setq domain nil))) | |
2402 (setq byte-compile-file-domain domain)) | |
2403 (byte-compile-keep-pending form 'byte-compile-normal-call)) | |
2404 | |
2405 (defun byte-compile-domain (form) | |
2406 (byte-compile-warn "The `domain' declaration is legal only at top-level: %s" | |
2407 (let ((print-escape-newlines t) | |
2408 (print-level 4) | |
2409 (print-length 4)) | |
2410 (prin1-to-string form))) | |
2411 (byte-compile-normal-call | |
2412 (list 'signal ''error | |
2413 (list 'quote (list "`domain' used inside a function" form))))) | |
2414 | |
2415 ;; This is part of bytecomp.el in 19.35: | |
2416 (put 'custom-declare-variable 'byte-hunk-handler | |
2417 'byte-compile-file-form-custom-declare-variable) | |
2418 (defun byte-compile-file-form-custom-declare-variable (form) | |
4289 | 2419 ;; XEmacs change; our implementation byte compiles and gives warnings |
2420 ;; about the default value code, which GNU's doesn't. | |
2421 (let* ((quoted-default (car-safe (cdr-safe (cdr-safe form)))) | |
2422 (to-examine (car-safe (cdr-safe quoted-default)))) | |
2423 (if (memq 'free-vars byte-compile-warnings) | |
2424 (setq byte-compile-bound-variables | |
2425 (cons (cons (nth 1 (nth 1 form)) | |
2426 byte-compile-global-bit) | |
2427 byte-compile-bound-variables))) | |
2428 ;; Byte compile anything that smells like a lambda. I initially | |
2429 ;; considered limiting it to the :initialize, :set and :get args, but | |
2430 ;; that's not amazingly forward-compatible, and anyone expecting other | |
2431 ;; things to be stored as data, not code, is unrealistic. | |
2432 (loop | |
2433 for entry in-ref (nthcdr 4 form) | |
2434 do (cond ((and (eq 'function (car-safe entry)) | |
2435 (consp (car-safe (cdr-safe entry)))) | |
2436 (setf entry (copy-sequence entry)) | |
2437 (setcar (cdr entry) (byte-compile-lambda (car (cdr entry))))) | |
2438 ((and (eq 'lambda (car-safe entry))) | |
2439 (setf entry (byte-compile-lambda entry))))) | |
2440 ;; Byte compile the default value, as we do for defvar. | |
2441 (when (consp (cdr-safe to-examine)) | |
2442 (setq form (copy-sequence form)) | |
2443 (setcdr (third form) | |
2444 (list (byte-compile-top-level to-examine nil 'file))) | |
2445 ;; And save a value to be examined in the custom UI, if that differs | |
2446 ;; from the init value. | |
2447 (unless (equal to-examine (car-safe (cdr (third form)))) | |
4304 | 2448 (setcdr (third form) |
2449 (list (byte-compile-top-level | |
2450 ;; This is ugly. custom-declare-variable errors if | |
2451 ;; it's passed a keyword it doesn't know about, and | |
2452 ;; so to make this code run on 21.4, we add code to | |
2453 ;; modify the standard-value property to the | |
2454 ;; byte-compiled value for DEFAULT. | |
2455 `(prog2 (put ,(second form) 'standard-value | |
2456 '(,to-examine)) | |
2457 ,to-examine) | |
2458 nil 'file))))) | |
4289 | 2459 form)) |
428 | 2460 |
2461 ;;;###autoload | |
2462 (defun byte-compile (form) | |
2463 "If FORM is a symbol, byte-compile its function definition. | |
2464 If FORM is a lambda or a macro, byte-compile it as a function." | |
2465 (displaying-byte-compile-warnings | |
2466 (byte-compile-close-variables | |
2467 (let* ((fun (if (symbolp form) | |
2468 (and (fboundp form) (symbol-function form)) | |
2469 form)) | |
2470 (macro (eq (car-safe fun) 'macro))) | |
2471 (if macro | |
2472 (setq fun (cdr fun))) | |
2473 (cond ((eq (car-safe fun) 'lambda) | |
2474 (setq fun (if macro | |
2475 (cons 'macro (byte-compile-lambda fun)) | |
2476 (byte-compile-lambda fun))) | |
2477 (if (symbolp form) | |
2478 (defalias form fun) | |
2479 fun))))))) | |
2480 | |
2481 ;;;###autoload | |
2482 (defun byte-compile-sexp (sexp &optional msg) | |
2483 "Compile and return SEXP." | |
2484 (displaying-byte-compile-warnings | |
2485 (byte-compile-close-variables | |
2486 (prog1 | |
2487 (byte-compile-top-level sexp) | |
2488 (byte-compile-warn-about-unresolved-functions msg))))) | |
2489 | |
2490 ;; Given a function made by byte-compile-lambda, make a form which produces it. | |
2491 (defun byte-compile-byte-code-maker (fun) | |
2492 (cond | |
2493 ;; ## atom is faster than compiled-func-p. | |
2494 ((atom fun) ; compiled-function-p | |
2495 fun) | |
2496 ;; b-c-lambda didn't produce a compiled-function, so it must be a trivial | |
2497 ;; function. | |
2498 ((let (tmp) | |
2499 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun)))) | |
2500 (null (cdr (memq tmp fun)))) | |
2501 ;; Generate a make-byte-code call. | |
2502 (let* ((interactive (assq 'interactive (cdr (cdr fun))))) | |
2503 (nconc (list 'make-byte-code | |
2504 (list 'quote (nth 1 fun)) ;arglist | |
2505 (nth 1 tmp) ;instructions | |
2506 (nth 2 tmp) ;constants | |
2507 (nth 3 tmp)) ;stack-depth | |
2508 (cond ((stringp (nth 2 fun)) | |
2509 (list (nth 2 fun))) ;docstring | |
2510 (interactive | |
2511 (list nil))) | |
2512 (cond (interactive | |
2513 (list (if (or (null (nth 1 interactive)) | |
2514 (stringp (nth 1 interactive))) | |
2515 (nth 1 interactive) | |
2516 ;; Interactive spec is a list or a variable | |
2517 ;; (if it is correct). | |
2518 (list 'quote (nth 1 interactive)))))))) | |
2519 ;; a non-compiled function (probably trivial) | |
2520 (list 'quote fun)))))) | |
2521 | |
2522 ;; Byte-compile a lambda-expression and return a valid function. | |
2523 ;; The value is usually a compiled function but may be the original | |
2524 ;; lambda-expression. | |
2525 (defun byte-compile-lambda (fun) | |
2526 (or (eq 'lambda (car-safe fun)) | |
2527 (error "not a lambda -- %s" (prin1-to-string fun))) | |
2528 (let* ((arglist (nth 1 fun)) | |
2529 (byte-compile-bound-variables | |
2530 (let ((new-bindings | |
2531 (mapcar #'(lambda (x) (cons x byte-compile-arglist-bit)) | |
2532 (and (memq 'free-vars byte-compile-warnings) | |
2533 (delq '&rest (delq '&optional | |
2534 (copy-sequence arglist))))))) | |
2535 (nconc new-bindings | |
2536 (cons 'new-scope byte-compile-bound-variables)))) | |
2537 (body (cdr (cdr fun))) | |
2538 (doc (if (stringp (car body)) | |
2539 (prog1 (car body) | |
1548 | 2540 ;; Discard the doc string |
2541 ;; only if it is not the only element of the body. | |
2542 (if (cdr body) | |
2543 (setq body (cdr body)))))) | |
4639
7757334005ae
bytecomp.el: always check code in (interactive SEXP) for sanity
Aidan Kehoe <kehoea@parhasard.net>
parents:
4623
diff
changeset
|
2544 (int (assq 'interactive body)) compiled-int) |
428 | 2545 (dolist (arg arglist) |
2546 (cond ((not (symbolp arg)) | |
2547 (byte-compile-warn "non-symbol in arglist: %S" arg)) | |
2548 ((byte-compile-constant-symbol-p arg) | |
2549 (byte-compile-warn "constant symbol in arglist: %s" arg)) | |
2550 ((and (char= ?\& (aref (symbol-name arg) 0)) | |
2551 (not (eq arg '&optional)) | |
2552 (not (eq arg '&rest))) | |
2553 (byte-compile-warn "unrecognized `&' keyword in arglist: %s" | |
2554 arg)))) | |
2555 (cond (int | |
2556 ;; Skip (interactive) if it is in front (the most usual location). | |
2557 (if (eq int (car body)) | |
2558 (setq body (cdr body))) | |
2559 (cond ((consp (cdr int)) | |
2560 (if (cdr (cdr int)) | |
2561 (byte-compile-warn "malformed interactive spec: %s" | |
2562 (prin1-to-string int))) | |
4639
7757334005ae
bytecomp.el: always check code in (interactive SEXP) for sanity
Aidan Kehoe <kehoea@parhasard.net>
parents:
4623
diff
changeset
|
2563 ;; If the interactive spec is a call to `list', don't |
7757334005ae
bytecomp.el: always check code in (interactive SEXP) for sanity
Aidan Kehoe <kehoea@parhasard.net>
parents:
4623
diff
changeset
|
2564 ;; store the compiled form, because `call-interactively' |
7757334005ae
bytecomp.el: always check code in (interactive SEXP) for sanity
Aidan Kehoe <kehoea@parhasard.net>
parents:
4623
diff
changeset
|
2565 ;; looks at the args of `list' and treats certain |
7757334005ae
bytecomp.el: always check code in (interactive SEXP) for sanity
Aidan Kehoe <kehoea@parhasard.net>
parents:
4623
diff
changeset
|
2566 ;; functions specially. Compiling it is nonetheless |
7757334005ae
bytecomp.el: always check code in (interactive SEXP) for sanity
Aidan Kehoe <kehoea@parhasard.net>
parents:
4623
diff
changeset
|
2567 ;; useful for warnings. |
428 | 2568 (let ((form (nth 1 int))) |
2569 (while (or (eq (car-safe form) 'let) | |
2570 (eq (car-safe form) 'let*) | |
2571 (eq (car-safe form) 'save-excursion)) | |
2572 (while (consp (cdr form)) | |
2573 (setq form (cdr form))) | |
2574 (setq form (car form))) | |
4639
7757334005ae
bytecomp.el: always check code in (interactive SEXP) for sanity
Aidan Kehoe <kehoea@parhasard.net>
parents:
4623
diff
changeset
|
2575 (setq compiled-int |
7757334005ae
bytecomp.el: always check code in (interactive SEXP) for sanity
Aidan Kehoe <kehoea@parhasard.net>
parents:
4623
diff
changeset
|
2576 (byte-compile-top-level (nth 1 int))) |
428 | 2577 (or (eq (car-safe form) 'list) |
4639
7757334005ae
bytecomp.el: always check code in (interactive SEXP) for sanity
Aidan Kehoe <kehoea@parhasard.net>
parents:
4623
diff
changeset
|
2578 (setq int (list 'interactive compiled-int))))) |
428 | 2579 ((cdr int) |
2580 (byte-compile-warn "malformed interactive spec: %s" | |
2581 (prin1-to-string int)))))) | |
2582 (let ((compiled (byte-compile-top-level (cons 'progn body) nil 'lambda))) | |
2583 (if (memq 'unused-vars byte-compile-warnings) | |
2584 ;; done compiling in this scope, warn now. | |
2585 (byte-compile-warn-about-unused-variables)) | |
2586 (if (eq 'byte-code (car-safe compiled)) | |
2587 (apply 'make-byte-code | |
2588 (append (list arglist) | |
2589 ;; byte-string, constants-vector, stack depth | |
2590 (cdr compiled) | |
2591 ;; optionally, the doc string. | |
2592 (if (or doc int) | |
2593 (list doc)) | |
2594 ;; optionally, the interactive spec. | |
2595 (if int | |
2596 (list (nth 1 int))))) | |
2597 (setq compiled | |
2598 (nconc (if int (list int)) | |
2599 (cond ((eq (car-safe compiled) 'progn) (cdr compiled)) | |
2600 (compiled (list compiled))))) | |
2601 (nconc (list 'lambda arglist) | |
2602 (if (or doc (stringp (car compiled))) | |
2603 (cons doc (cond (compiled) | |
2604 (body (list nil)))) | |
2605 compiled)))))) | |
2606 | |
2607 (defun byte-compile-constants-vector () | |
2608 ;; Builds the constants-vector from the current variables and constants. | |
2609 ;; This modifies the constants from (const . nil) to (const . offset). | |
2610 ;; To keep the byte-codes to look up the vector as short as possible: | |
2611 ;; First 6 elements are vars, as there are one-byte varref codes for those. | |
2612 ;; Next up to byte-constant-limit are constants, still with one-byte codes. | |
2613 ;; Next variables again, to get 2-byte codes for variable lookup. | |
2614 ;; The rest of the constants and variables need 3-byte byte-codes. | |
2615 (let* ((i -1) | |
2616 (rest (nreverse byte-compile-variables)) ; nreverse because the first | |
2617 (other (nreverse byte-compile-constants)) ; vars often are used most. | |
2618 ret tmp | |
2619 (limits '(5 ; Use the 1-byte varref codes, | |
2620 63 ; 1-constlim ; 1-byte byte-constant codes, | |
2621 255 ; 2-byte varref codes, | |
2622 65535)) ; 3-byte codes for the rest. | |
2623 limit) | |
2624 (while (or rest other) | |
2625 (setq limit (car limits)) | |
2626 (while (and rest (not (eq i limit))) | |
2627 (if (setq tmp (assq (car (car rest)) ret)) | |
2628 (setcdr (car rest) (cdr tmp)) | |
2629 (setcdr (car rest) (setq i (1+ i))) | |
2630 (setq ret (cons (car rest) ret))) | |
2631 (setq rest (cdr rest))) | |
2632 (setq limits (cdr limits) | |
2633 rest (prog1 other | |
2634 (setq other rest)))) | |
2635 (apply 'vector (nreverse (mapcar 'car ret))))) | |
2636 | |
2637 ;; Given an expression FORM, compile it and return an equivalent byte-code | |
2638 ;; expression (a call to the function byte-code). | |
2639 (defun byte-compile-top-level (form &optional for-effect output-type) | |
2640 ;; OUTPUT-TYPE advises about how form is expected to be used: | |
2641 ;; 'eval or nil -> a single form, | |
2642 ;; 'progn or t -> a list of forms, | |
2643 ;; 'lambda -> body of a lambda, | |
2644 ;; 'file -> used at file-level. | |
2645 (let ((byte-compile-constants nil) | |
2646 (byte-compile-variables nil) | |
2647 (byte-compile-tag-number 0) | |
2648 (byte-compile-depth 0) | |
2649 (byte-compile-maxdepth 0) | |
2650 (byte-compile-output nil)) | |
2651 (if (memq byte-optimize '(t source)) | |
2652 (setq form (byte-optimize-form form for-effect))) | |
2653 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form)))) | |
2654 (setq form (nth 1 form))) | |
2655 (if (and (eq 'byte-code (car-safe form)) | |
2656 (not (memq byte-optimize '(t byte))) | |
2657 (stringp (nth 1 form)) | |
2658 (vectorp (nth 2 form)) | |
2659 (natnump (nth 3 form))) | |
2660 form | |
2661 (byte-compile-form form for-effect) | |
2662 (byte-compile-out-toplevel for-effect output-type)))) | |
2663 | |
2664 (defun byte-compile-out-toplevel (&optional for-effect output-type) | |
2665 (if for-effect | |
2666 ;; The stack is empty. Push a value to be returned from (byte-code ..). | |
2667 (if (eq (car (car byte-compile-output)) 'byte-discard) | |
2668 (setq byte-compile-output (cdr byte-compile-output)) | |
2669 (byte-compile-push-constant | |
2670 ;; Push any constant - preferably one which already is used, and | |
2671 ;; a number or symbol - ie not some big sequence. The return value | |
2672 ;; isn't returned, but it would be a shame if some textually large | |
2673 ;; constant was not optimized away because we chose to return it. | |
2674 (and (not (assq nil byte-compile-constants)) ; Nil is often there. | |
2675 (let ((tmp (reverse byte-compile-constants))) | |
2676 (while (and tmp (not (or (symbolp (car (car tmp))) | |
2677 (numberp (car (car tmp)))))) | |
2678 (setq tmp (cdr tmp))) | |
2679 (car (car tmp))))))) | |
2680 (byte-compile-out 'byte-return 0) | |
2681 (setq byte-compile-output (nreverse byte-compile-output)) | |
2682 (if (memq byte-optimize '(t byte)) | |
2683 (setq byte-compile-output | |
2684 (byte-optimize-lapcode byte-compile-output for-effect))) | |
2685 | |
2686 ;; Decompile trivial functions: | |
2687 ;; only constants and variables, or a single funcall except in lambdas. | |
2688 ;; Except for Lisp_Compiled objects, forms like (foo "hi") | |
2689 ;; are still quicker than (byte-code "..." [foo "hi"] 2). | |
2690 ;; Note that even (quote foo) must be parsed just as any subr by the | |
2691 ;; interpreter, so quote should be compiled into byte-code in some contexts. | |
2692 ;; What to leave uncompiled: | |
2693 ;; lambda -> never. we used to leave it uncompiled if the body was | |
2694 ;; a single atom, but that causes confusion if the docstring | |
2695 ;; uses the (file . pos) syntax. Besides, now that we have | |
2696 ;; the Lisp_Compiled type, the compiled form is faster. | |
2697 ;; eval -> atom, quote or (function atom atom atom) | |
2698 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom) | |
2699 ;; file -> as progn, but takes both quotes and atoms, and longer forms. | |
2700 (let (rest | |
2701 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall. | |
2702 tmp body) | |
2703 (cond | |
2704 ;; #### This should be split out into byte-compile-nontrivial-function-p. | |
2705 ((or (eq output-type 'lambda) | |
2706 (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output) | |
2707 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit. | |
2708 (not (setq tmp (assq 'byte-return byte-compile-output))) | |
2709 (progn | |
2710 (setq rest (nreverse | |
2711 (cdr (memq tmp (reverse byte-compile-output))))) | |
2712 (while (cond | |
2713 ((memq (car (car rest)) '(byte-varref byte-constant)) | |
2714 (setq tmp (car (cdr (car rest)))) | |
2715 (if (if (eq (car (car rest)) 'byte-constant) | |
2716 (or (consp tmp) | |
2717 (and (symbolp tmp) | |
2718 (not (byte-compile-constant-symbol-p tmp))))) | |
2719 (if maycall | |
2720 (setq body (cons (list 'quote tmp) body))) | |
2721 (setq body (cons tmp body)))) | |
2722 ((and maycall | |
2723 ;; Allow a funcall if at most one atom follows it. | |
2724 (null (nthcdr 3 rest)) | |
2725 (setq tmp | |
2726 ;; XEmacs change for rms funs | |
2727 (or (and | |
2728 (byte-compile-version-cond | |
2729 byte-compile-emacs19-compatibility) | |
2730 (get (car (car rest)) | |
2731 'byte-opcode19-invert)) | |
2732 (get (car (car rest)) | |
2733 'byte-opcode-invert))) | |
2734 (or (null (cdr rest)) | |
2735 (and (memq output-type '(file progn t)) | |
2736 (cdr (cdr rest)) | |
2737 (eq (car (nth 1 rest)) 'byte-discard) | |
2738 (progn (setq rest (cdr rest)) t)))) | |
2739 (setq maycall nil) ; Only allow one real function call. | |
2740 (setq body (nreverse body)) | |
2741 (setq body (list | |
2742 (if (and (eq tmp 'funcall) | |
2743 (eq (car-safe (car body)) 'quote)) | |
2744 (cons (nth 1 (car body)) (cdr body)) | |
2745 (cons tmp body)))) | |
2746 (or (eq output-type 'file) | |
2747 (not (delq nil (mapcar 'consp (cdr (car body)))))))) | |
2748 (setq rest (cdr rest))) | |
2749 rest)) | |
2750 (let ((byte-compile-vector (byte-compile-constants-vector))) | |
2751 (list 'byte-code (byte-compile-lapcode byte-compile-output) | |
2752 byte-compile-vector byte-compile-maxdepth))) | |
2753 ;; it's a trivial function | |
2754 ((cdr body) (cons 'progn (nreverse body))) | |
2755 ((car body))))) | |
2756 | |
2757 ;; Given BODY, compile it and return a new body. | |
2758 (defun byte-compile-top-level-body (body &optional for-effect) | |
2759 (setq body (byte-compile-top-level (cons 'progn body) for-effect t)) | |
2760 (cond ((eq (car-safe body) 'progn) | |
2761 (cdr body)) | |
2762 (body | |
2763 (list body)))) | |
2764 | |
2765 ;; This is the recursive entry point for compiling each subform of an | |
2766 ;; expression. | |
2767 ;; If for-effect is non-nil, byte-compile-form will output a byte-discard | |
2768 ;; before terminating (ie. no value will be left on the stack). | |
2769 ;; A byte-compile handler may, when for-effect is non-nil, choose output code | |
2770 ;; which does not leave a value on the stack, and then set for-effect to nil | |
2771 ;; (to prevent byte-compile-form from outputting the byte-discard). | |
2772 ;; If a handler wants to call another handler, it should do so via | |
2773 ;; byte-compile-form, or take extreme care to handle for-effect correctly. | |
2774 ;; (Use byte-compile-form-do-effect to reset the for-effect flag too.) | |
2775 ;; | |
2776 (defun byte-compile-form (form &optional for-effect) | |
2777 (setq form (macroexpand form byte-compile-macro-environment)) | |
2778 (cond ((not (consp form)) | |
2779 (cond ((or (not (symbolp form)) | |
2780 (byte-compile-constant-symbol-p form)) | |
2781 (byte-compile-constant form)) | |
2782 ((and for-effect byte-compile-delete-errors) | |
2783 (setq for-effect nil)) | |
2784 (t (byte-compile-variable-ref 'byte-varref form)))) | |
2785 ((symbolp (car form)) | |
2786 (let* ((fn (car form)) | |
2787 (handler (get fn 'byte-compile))) | |
2788 (if (memq fn '(t nil)) | |
2789 (byte-compile-warn "%s called as a function" fn)) | |
2790 (if (and handler | |
2791 (or (not (byte-compile-version-cond | |
2792 byte-compile-emacs19-compatibility)) | |
2793 (not (get (get fn 'byte-opcode) 'emacs20-opcode)))) | |
2794 (funcall handler form) | |
2795 (if (memq 'callargs byte-compile-warnings) | |
2796 (byte-compile-callargs-warn form)) | |
2797 (byte-compile-normal-call form)))) | |
2798 ((and (or (compiled-function-p (car form)) | |
2799 (eq (car-safe (car form)) 'lambda)) | |
2800 ;; if the form comes out the same way it went in, that's | |
2801 ;; because it was malformed, and we couldn't unfold it. | |
2802 (not (eq form (setq form (byte-compile-unfold-lambda form))))) | |
2803 (byte-compile-form form for-effect) | |
2804 (setq for-effect nil)) | |
2805 ((byte-compile-normal-call form))) | |
2806 (when for-effect | |
2807 (byte-compile-discard))) | |
2808 | |
2809 (defun byte-compile-normal-call (form) | |
2810 (if byte-compile-generate-call-tree | |
2811 (byte-compile-annotate-call-tree form)) | |
2812 (byte-compile-push-constant (car form)) | |
2813 (mapcar 'byte-compile-form (cdr form)) ; wasteful, but faster. | |
2814 (byte-compile-out 'byte-call (length (cdr form)))) | |
2815 | |
2816 ;; kludge added to XEmacs to work around the bogosities of a nonlexical lisp. | |
2817 (or (fboundp 'globally-boundp) (fset 'globally-boundp 'boundp)) | |
2818 | |
2819 (defun byte-compile-variable-ref (base-op var &optional varbind-flags) | |
2820 (if (or (not (symbolp var)) (byte-compile-constant-symbol-p var)) | |
2821 (byte-compile-warn | |
2822 (case base-op | |
2823 (byte-varref "Variable reference to %s %s") | |
2824 (byte-varset "Attempt to set %s %s") | |
2825 (byte-varbind "Attempt to let-bind %s %s")) | |
2826 (if (symbolp var) "constant symbol" "non-symbol") | |
2827 var) | |
2828 (if (and (get var 'byte-obsolete-variable) | |
2829 (memq 'obsolete byte-compile-warnings)) | |
2830 (let ((ob (get var 'byte-obsolete-variable))) | |
2831 (byte-compile-warn "%s is an obsolete variable; %s" var | |
2832 (if (stringp ob) | |
2833 ob | |
2834 (format "use %s instead." ob))))) | |
2835 (if (and (get var 'byte-compatible-variable) | |
2836 (memq 'pedantic byte-compile-warnings)) | |
2837 (let ((ob (get var 'byte-compatible-variable))) | |
2838 (byte-compile-warn "%s is provided for compatibility; %s" var | |
2839 (if (stringp ob) | |
2840 ob | |
2841 (format "use %s instead." ob))))) | |
2842 (if (memq 'free-vars byte-compile-warnings) | |
2843 (if (eq base-op 'byte-varbind) | |
2844 (setq byte-compile-bound-variables | |
2845 (cons (cons var (or varbind-flags 0)) | |
2846 byte-compile-bound-variables)) | |
2847 (or (globally-boundp var) | |
2848 (let ((cell (assq var byte-compile-bound-variables))) | |
2849 (if cell (setcdr cell | |
2850 (logior (cdr cell) | |
2851 (if (eq base-op 'byte-varset) | |
2852 byte-compile-assigned-bit | |
2853 byte-compile-referenced-bit))))) | |
444 | 2854 (and (boundp 'current-load-list) |
2855 (memq var current-load-list)) | |
428 | 2856 (if (eq base-op 'byte-varset) |
2857 (or (memq var byte-compile-free-assignments) | |
2858 (progn | |
2859 (byte-compile-warn "assignment to free variable %s" | |
2860 var) | |
2861 (setq byte-compile-free-assignments | |
2862 (cons var byte-compile-free-assignments)))) | |
2863 (or (memq var byte-compile-free-references) | |
2864 (progn | |
2865 (byte-compile-warn "reference to free variable %s" var) | |
2866 (setq byte-compile-free-references | |
2867 (cons var byte-compile-free-references))))))))) | |
2868 (let ((tmp (assq var byte-compile-variables))) | |
2869 (or tmp | |
2870 (setq tmp (list var) | |
2871 byte-compile-variables (cons tmp byte-compile-variables))) | |
2872 (byte-compile-out base-op tmp))) | |
2873 | |
2874 (defmacro byte-compile-get-constant (const) | |
2875 `(or (if (stringp ,const) | |
2876 (assoc ,const byte-compile-constants) | |
2877 (assq ,const byte-compile-constants)) | |
2878 (car (setq byte-compile-constants | |
2879 (cons (list ,const) byte-compile-constants))))) | |
2880 | |
2881 ;; Use this when the value of a form is a constant. This obeys for-effect. | |
2882 (defun byte-compile-constant (const) | |
2883 (if for-effect | |
2884 (setq for-effect nil) | |
2885 (byte-compile-out 'byte-constant (byte-compile-get-constant const)))) | |
2886 | |
2887 ;; Use this for a constant that is not the value of its containing form. | |
2888 ;; This ignores for-effect. | |
2889 (defun byte-compile-push-constant (const) | |
2890 (let ((for-effect nil)) | |
2891 (inline (byte-compile-constant const)))) | |
2892 | |
2893 | |
2894 ;; Compile those primitive ordinary functions | |
2895 ;; which have special byte codes just for speed. | |
2896 | |
2897 (defmacro byte-defop-compiler (function &optional compile-handler) | |
2898 ;; add a compiler-form for FUNCTION. | |
446 | 2899 ;; If FUNCTION is a symbol, then the variable "byte-SYMBOL" must name |
2900 ;; the opcode to be used. If is a list, the first element | |
428 | 2901 ;; is the function and the second element is the bytecode-symbol. |
2902 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or | |
2903 ;; may be the abbreviations 0, 1, 2, 3, 0-1, 1-2, 2-3, 0+1, 1+1, 2+1, | |
2904 ;; 0-1+1, 1-2+1, 2-3+1, 0+2, or 1+2. If it is nil, then the handler is | |
2905 ;; "byte-compile-SYMBOL." | |
2906 (let (opcode) | |
2907 (if (symbolp function) | |
2908 (setq opcode (intern (concat "byte-" (symbol-name function)))) | |
2909 (setq opcode (car (cdr function)) | |
2910 function (car function))) | |
2911 (let ((fnform | |
2912 (list 'put (list 'quote function) ''byte-compile | |
2913 (list 'quote | |
2914 (or (cdr (assq compile-handler | |
2915 '((0 . byte-compile-no-args) | |
2916 (1 . byte-compile-one-arg) | |
2917 (2 . byte-compile-two-args) | |
2918 (3 . byte-compile-three-args) | |
2919 (0-1 . byte-compile-zero-or-one-arg) | |
2920 (1-2 . byte-compile-one-or-two-args) | |
2921 (2-3 . byte-compile-two-or-three-args) | |
2922 (0+1 . byte-compile-no-args-with-one-extra) | |
2923 (1+1 . byte-compile-one-arg-with-one-extra) | |
2924 (2+1 . byte-compile-two-args-with-one-extra) | |
2925 (0-1+1 . byte-compile-zero-or-one-arg-with-one-extra) | |
2926 (1-2+1 . byte-compile-one-or-two-args-with-one-extra) | |
2927 (2-3+1 . byte-compile-two-or-three-args-with-one-extra) | |
2928 (0+2 . byte-compile-no-args-with-two-extra) | |
2929 (1+2 . byte-compile-one-arg-with-two-extra) | |
2930 | |
2931 ))) | |
2932 compile-handler | |
2933 (intern (concat "byte-compile-" | |
2934 (symbol-name function)))))))) | |
2935 (if opcode | |
2936 (list 'progn fnform | |
2937 (list 'put (list 'quote function) | |
2938 ''byte-opcode (list 'quote opcode)) | |
2939 (list 'put (list 'quote opcode) | |
2940 ''byte-opcode-invert (list 'quote function))) | |
2941 fnform)))) | |
2942 | |
2943 (defmacro byte-defop-compiler20 (function &optional compile-handler) | |
2944 ;; Just like byte-defop-compiler, but defines an opcode that will only | |
2945 ;; be used when byte-compile-emacs19-compatibility is false. | |
2946 (if (and (byte-compile-single-version) | |
2947 byte-compile-emacs19-compatibility) | |
2948 ;; #### instead of doing nothing, this should do some remprops, | |
2949 ;; #### to protect against the case where a single-version compiler | |
2950 ;; #### is loaded into a world that has contained a multi-version one. | |
2951 nil | |
2952 (list 'progn | |
2953 (list 'put | |
2954 (list 'quote | |
2955 (or (car (cdr-safe function)) | |
2956 (intern (concat "byte-" | |
2957 (symbol-name (or (car-safe function) function)))))) | |
2958 ''emacs20-opcode t) | |
2959 (list 'byte-defop-compiler function compile-handler)))) | |
2960 | |
2961 ;; XEmacs addition: | |
2962 (defmacro byte-defop-compiler-rmsfun (function &optional compile-handler) | |
2963 ;; for functions like `eq' that compile into different opcodes depending | |
2964 ;; on the Emacs version: byte-old-eq for v19, byte-eq for v20. | |
2965 (let ((opcode (intern (concat "byte-" (symbol-name function)))) | |
2966 (opcode19 (intern (concat "byte-old-" (symbol-name function)))) | |
2967 (fnform | |
2968 (list 'put (list 'quote function) ''byte-compile | |
2969 (list 'quote | |
2970 (or (cdr (assq compile-handler | |
2971 '((2 . byte-compile-two-args-19->20) | |
2972 ))) | |
2973 compile-handler | |
2974 (intern (concat "byte-compile-" | |
2975 (symbol-name function)))))))) | |
2976 (list 'progn fnform | |
2977 (list 'put (list 'quote function) | |
2978 ''byte-opcode (list 'quote opcode)) | |
2979 (list 'put (list 'quote function) | |
2980 ''byte-opcode19 (list 'quote opcode19)) | |
2981 (list 'put (list 'quote opcode) | |
2982 ''byte-opcode-invert (list 'quote function)) | |
2983 (list 'put (list 'quote opcode19) | |
2984 ''byte-opcode19-invert (list 'quote function))))) | |
2985 | |
2986 (defmacro byte-defop-compiler-1 (function &optional compile-handler) | |
2987 (list 'byte-defop-compiler (list function nil) compile-handler)) | |
2988 | |
2989 | |
2990 (put 'byte-call 'byte-opcode-invert 'funcall) | |
2991 (put 'byte-list1 'byte-opcode-invert 'list) | |
2992 (put 'byte-list2 'byte-opcode-invert 'list) | |
2993 (put 'byte-list3 'byte-opcode-invert 'list) | |
2994 (put 'byte-list4 'byte-opcode-invert 'list) | |
2995 (put 'byte-listN 'byte-opcode-invert 'list) | |
2996 (put 'byte-concat2 'byte-opcode-invert 'concat) | |
2997 (put 'byte-concat3 'byte-opcode-invert 'concat) | |
2998 (put 'byte-concat4 'byte-opcode-invert 'concat) | |
2999 (put 'byte-concatN 'byte-opcode-invert 'concat) | |
3000 (put 'byte-insertN 'byte-opcode-invert 'insert) | |
3001 | |
3002 ;; How old is this stuff? -slb | |
3003 ;(byte-defop-compiler (dot byte-point) 0+1) | |
3004 ;(byte-defop-compiler (dot-max byte-point-max) 0+1) | |
3005 ;(byte-defop-compiler (dot-min byte-point-min) 0+1) | |
3006 (byte-defop-compiler point 0+1) | |
3007 (byte-defop-compiler-rmsfun eq 2) | |
3008 (byte-defop-compiler point-max 0+1) | |
3009 (byte-defop-compiler point-min 0+1) | |
3010 (byte-defop-compiler following-char 0+1) | |
3011 (byte-defop-compiler preceding-char 0+1) | |
3012 (byte-defop-compiler current-column 0+1) | |
3013 ;; FSF has special function here; generalized here by the 1+2 stuff. | |
3014 (byte-defop-compiler (indent-to-column byte-indent-to) 1+2) | |
3015 (byte-defop-compiler indent-to 1+2) | |
3016 (byte-defop-compiler-rmsfun equal 2) | |
3017 (byte-defop-compiler eolp 0+1) | |
3018 (byte-defop-compiler eobp 0+1) | |
3019 (byte-defop-compiler bolp 0+1) | |
3020 (byte-defop-compiler bobp 0+1) | |
3021 (byte-defop-compiler current-buffer 0) | |
3022 ;;(byte-defop-compiler read-char 0) ;; obsolete | |
3023 (byte-defop-compiler-rmsfun memq 2) | |
3024 (byte-defop-compiler interactive-p 0) | |
3025 (byte-defop-compiler widen 0+1) | |
3026 (byte-defop-compiler end-of-line 0-1+1) | |
3027 (byte-defop-compiler forward-char 0-1+1) | |
3028 (byte-defop-compiler forward-line 0-1+1) | |
3029 (byte-defop-compiler symbolp 1) | |
3030 (byte-defop-compiler consp 1) | |
3031 (byte-defop-compiler stringp 1) | |
3032 (byte-defop-compiler listp 1) | |
3033 (byte-defop-compiler not 1) | |
3034 (byte-defop-compiler (null byte-not) 1) | |
3035 (byte-defop-compiler car 1) | |
3036 (byte-defop-compiler cdr 1) | |
3037 (byte-defop-compiler length 1) | |
3038 (byte-defop-compiler symbol-value 1) | |
3039 (byte-defop-compiler symbol-function 1) | |
3040 (byte-defop-compiler (1+ byte-add1) 1) | |
3041 (byte-defop-compiler (1- byte-sub1) 1) | |
3042 (byte-defop-compiler goto-char 1+1) | |
3043 (byte-defop-compiler char-after 0-1+1) | |
3044 (byte-defop-compiler set-buffer 1) | |
3045 ;;(byte-defop-compiler set-mark 1) ;; obsolete | |
2217 | 3046 (byte-defop-compiler forward-word 0-1+1) |
428 | 3047 (byte-defop-compiler char-syntax 1+1) |
3048 (byte-defop-compiler nreverse 1) | |
3049 (byte-defop-compiler car-safe 1) | |
3050 (byte-defop-compiler cdr-safe 1) | |
3051 (byte-defop-compiler numberp 1) | |
3052 (byte-defop-compiler integerp 1) | |
3053 (byte-defop-compiler skip-chars-forward 1-2+1) | |
3054 (byte-defop-compiler skip-chars-backward 1-2+1) | |
3055 (byte-defop-compiler (eql byte-eq) 2) | |
3056 (byte-defop-compiler20 old-eq 2) | |
3057 (byte-defop-compiler20 old-memq 2) | |
3058 (byte-defop-compiler cons 2) | |
3059 (byte-defop-compiler aref 2) | |
3060 (byte-defop-compiler get 2+1) | |
3061 (byte-defop-compiler nth 2) | |
3062 (byte-defop-compiler substring 2-3) | |
3063 (byte-defop-compiler (move-marker byte-set-marker) 2-3) | |
3064 (byte-defop-compiler set-marker 2-3) | |
3065 (byte-defop-compiler match-beginning 1) | |
3066 (byte-defop-compiler match-end 1) | |
3067 (byte-defop-compiler upcase 1+1) | |
3068 (byte-defop-compiler downcase 1+1) | |
3069 (byte-defop-compiler string= 2) | |
3070 (byte-defop-compiler string< 2) | |
3071 (byte-defop-compiler (string-equal byte-string=) 2) | |
3072 (byte-defop-compiler (string-lessp byte-string<) 2) | |
3073 (byte-defop-compiler20 old-equal 2) | |
3074 (byte-defop-compiler nthcdr 2) | |
3075 (byte-defop-compiler elt 2) | |
3076 (byte-defop-compiler20 old-member 2) | |
3077 (byte-defop-compiler20 old-assq 2) | |
3078 (byte-defop-compiler (rplaca byte-setcar) 2) | |
3079 (byte-defop-compiler (rplacd byte-setcdr) 2) | |
3080 (byte-defop-compiler setcar 2) | |
3081 (byte-defop-compiler setcdr 2) | |
3082 (byte-defop-compiler delete-region 2+1) | |
3083 (byte-defop-compiler narrow-to-region 2+1) | |
3084 (byte-defop-compiler (% byte-rem) 2) | |
3085 (byte-defop-compiler aset 3) | |
3086 | |
3087 (byte-defop-compiler-rmsfun member 2) | |
3088 (byte-defop-compiler-rmsfun assq 2) | |
3089 | |
3090 ;;####(byte-defop-compiler move-to-column 1) | |
3091 (byte-defop-compiler-1 interactive byte-compile-noop) | |
3092 (byte-defop-compiler-1 domain byte-compile-domain) | |
3093 | |
3094 ;; As of GNU Emacs 19.18 and Lucid Emacs 19.8, mod and % are different: `%' | |
3095 ;; means integral remainder and may have a negative result; `mod' is always | |
3096 ;; positive, and accepts floating point args. All code which uses `mod' and | |
3097 ;; requires the new interpretation must be compiled with bytecomp version 2.18 | |
3098 ;; or newer, or the emitted code will run the byte-code for `%' instead of an | |
3099 ;; actual call to `mod'. So be careful of compiling new code with an old | |
3100 ;; compiler. Note also that `%' is more efficient than `mod' because the | |
3101 ;; former is byte-coded and the latter is not. | |
3102 ;;(byte-defop-compiler (mod byte-rem) 2) | |
3103 | |
3104 | |
3105 (defun byte-compile-subr-wrong-args (form n) | |
3106 (when (memq 'subr-callargs byte-compile-warnings) | |
3107 (byte-compile-warn "%s called with %d arg%s, but requires %s" | |
3108 (car form) (length (cdr form)) | |
3109 (if (= 1 (length (cdr form))) "" "s") n)) | |
3110 ;; get run-time wrong-number-of-args error. | |
3111 (byte-compile-normal-call form)) | |
3112 | |
3113 (defun byte-compile-no-args (form) | |
3114 (case (length (cdr form)) | |
3115 (0 (byte-compile-out (get (car form) 'byte-opcode) 0)) | |
3116 (t (byte-compile-subr-wrong-args form "none")))) | |
3117 | |
3118 (defun byte-compile-one-arg (form) | |
3119 (case (length (cdr form)) | |
3120 (1 (byte-compile-form (car (cdr form))) ;; Push the argument | |
3121 (byte-compile-out (get (car form) 'byte-opcode) 0)) | |
3122 (t (byte-compile-subr-wrong-args form 1)))) | |
3123 | |
3124 (defun byte-compile-two-args (form) | |
3125 (case (length (cdr form)) | |
3126 (2 (byte-compile-form (nth 1 form)) ;; Push the arguments | |
3127 (byte-compile-form (nth 2 form)) | |
3128 (byte-compile-out (get (car form) 'byte-opcode) 0)) | |
3129 (t (byte-compile-subr-wrong-args form 2)))) | |
3130 | |
3131 (defun byte-compile-three-args (form) | |
3132 (case (length (cdr form)) | |
3133 (3 (byte-compile-form (nth 1 form)) ;; Push the arguments | |
3134 (byte-compile-form (nth 2 form)) | |
3135 (byte-compile-form (nth 3 form)) | |
3136 (byte-compile-out (get (car form) 'byte-opcode) 0)) | |
3137 (t (byte-compile-subr-wrong-args form 3)))) | |
3138 | |
3139 (defun byte-compile-zero-or-one-arg (form) | |
3140 (case (length (cdr form)) | |
3141 (0 (byte-compile-one-arg (append form '(nil)))) | |
3142 (1 (byte-compile-one-arg form)) | |
3143 (t (byte-compile-subr-wrong-args form "0-1")))) | |
3144 | |
3145 (defun byte-compile-one-or-two-args (form) | |
3146 (case (length (cdr form)) | |
3147 (1 (byte-compile-two-args (append form '(nil)))) | |
3148 (2 (byte-compile-two-args form)) | |
3149 (t (byte-compile-subr-wrong-args form "1-2")))) | |
3150 | |
3151 (defun byte-compile-two-or-three-args (form) | |
3152 (case (length (cdr form)) | |
3153 (2 (byte-compile-three-args (append form '(nil)))) | |
3154 (3 (byte-compile-three-args form)) | |
3155 (t (byte-compile-subr-wrong-args form "2-3")))) | |
3156 | |
3157 ;; from Ben Wing <ben@xemacs.org>: some inlined functions have extra | |
3158 ;; optional args added to them in XEmacs 19.12. Changing the byte | |
3159 ;; interpreter to deal with these args would be wrong and cause | |
3160 ;; incompatibility, so we generate non-inlined calls for those cases. | |
3161 ;; Without the following functions, spurious warnings will be generated; | |
3162 ;; however, they would still compile correctly because | |
3163 ;; `byte-compile-subr-wrong-args' also converts the call to non-inlined. | |
3164 | |
3165 (defun byte-compile-no-args-with-one-extra (form) | |
3166 (case (length (cdr form)) | |
3167 (0 (byte-compile-no-args form)) | |
446 | 3168 (1 (if (eq nil (nth 1 form)) |
3169 (byte-compile-no-args (butlast form)) | |
3170 (byte-compile-normal-call form))) | |
428 | 3171 (t (byte-compile-subr-wrong-args form "0-1")))) |
3172 | |
3173 (defun byte-compile-one-arg-with-one-extra (form) | |
3174 (case (length (cdr form)) | |
3175 (1 (byte-compile-one-arg form)) | |
446 | 3176 (2 (if (eq nil (nth 2 form)) |
3177 (byte-compile-one-arg (butlast form)) | |
3178 (byte-compile-normal-call form))) | |
428 | 3179 (t (byte-compile-subr-wrong-args form "1-2")))) |
3180 | |
3181 (defun byte-compile-two-args-with-one-extra (form) | |
3182 (case (length (cdr form)) | |
3183 (2 (byte-compile-two-args form)) | |
446 | 3184 (3 (if (eq nil (nth 3 form)) |
3185 (byte-compile-two-args (butlast form)) | |
3186 (byte-compile-normal-call form))) | |
428 | 3187 (t (byte-compile-subr-wrong-args form "2-3")))) |
3188 | |
3189 (defun byte-compile-zero-or-one-arg-with-one-extra (form) | |
3190 (case (length (cdr form)) | |
3191 (0 (byte-compile-one-arg (append form '(nil)))) | |
3192 (1 (byte-compile-one-arg form)) | |
446 | 3193 (2 (if (eq nil (nth 2 form)) |
3194 (byte-compile-one-arg (butlast form)) | |
3195 (byte-compile-normal-call form))) | |
428 | 3196 (t (byte-compile-subr-wrong-args form "0-2")))) |
3197 | |
3198 (defun byte-compile-one-or-two-args-with-one-extra (form) | |
3199 (case (length (cdr form)) | |
3200 (1 (byte-compile-two-args (append form '(nil)))) | |
3201 (2 (byte-compile-two-args form)) | |
446 | 3202 (3 (if (eq nil (nth 3 form)) |
3203 (byte-compile-two-args (butlast form)) | |
3204 (byte-compile-normal-call form))) | |
428 | 3205 (t (byte-compile-subr-wrong-args form "1-3")))) |
3206 | |
3207 (defun byte-compile-two-or-three-args-with-one-extra (form) | |
3208 (case (length (cdr form)) | |
3209 (2 (byte-compile-three-args (append form '(nil)))) | |
3210 (3 (byte-compile-three-args form)) | |
446 | 3211 (4 (if (eq nil (nth 4 form)) |
3212 (byte-compile-three-args (butlast form)) | |
3213 (byte-compile-normal-call form))) | |
428 | 3214 (t (byte-compile-subr-wrong-args form "2-4")))) |
3215 | |
3216 (defun byte-compile-no-args-with-two-extra (form) | |
3217 (case (length (cdr form)) | |
3218 (0 (byte-compile-no-args form)) | |
3219 ((1 2) (byte-compile-normal-call form)) | |
3220 (t (byte-compile-subr-wrong-args form "0-2")))) | |
3221 | |
3222 (defun byte-compile-one-arg-with-two-extra (form) | |
3223 (case (length (cdr form)) | |
3224 (1 (byte-compile-one-arg form)) | |
3225 ((2 3) (byte-compile-normal-call form)) | |
3226 (t (byte-compile-subr-wrong-args form "1-3")))) | |
3227 | |
3228 ;; XEmacs: used for functions that have a different opcode in v19 than v20. | |
3229 ;; this includes `eq', `equal', and other old-ified functions. | |
3230 (defun byte-compile-two-args-19->20 (form) | |
3231 (if (not (= (length form) 3)) | |
3232 (byte-compile-subr-wrong-args form 2) | |
3233 (byte-compile-form (car (cdr form))) ;; Push the arguments | |
3234 (byte-compile-form (nth 2 form)) | |
3235 (if (byte-compile-version-cond byte-compile-emacs19-compatibility) | |
3236 (byte-compile-out (get (car form) 'byte-opcode19) 0) | |
3237 (byte-compile-out (get (car form) 'byte-opcode) 0)))) | |
3238 | |
3239 (defun byte-compile-noop (form) | |
3240 (byte-compile-constant nil)) | |
3241 | |
3242 (defun byte-compile-discard () | |
3243 (byte-compile-out 'byte-discard 0)) | |
3244 | |
446 | 3245 (defun byte-compile-max (form) |
3246 (let ((args (cdr form))) | |
428 | 3247 (case (length args) |
446 | 3248 (0 (byte-compile-subr-wrong-args form "1 or more")) |
3249 (1 (byte-compile-form (car args)) | |
3250 (when (not byte-compile-delete-errors) | |
3251 (byte-compile-out 'byte-dup 0) | |
3252 (byte-compile-out 'byte-max 0))) | |
428 | 3253 (t (byte-compile-form (car args)) |
446 | 3254 (dolist (elt (cdr args)) |
3255 (byte-compile-form elt) | |
3256 (byte-compile-out 'byte-max 0)))))) | |
3257 | |
3258 (defun byte-compile-min (form) | |
3259 (let ((args (cdr form))) | |
3260 (case (length args) | |
3261 (0 (byte-compile-subr-wrong-args form "1 or more")) | |
3262 (1 (byte-compile-form (car args)) | |
3263 (when (not byte-compile-delete-errors) | |
3264 (byte-compile-out 'byte-dup 0) | |
3265 (byte-compile-out 'byte-min 0))) | |
3266 (t (byte-compile-form (car args)) | |
3267 (dolist (elt (cdr args)) | |
3268 (byte-compile-form elt) | |
3269 (byte-compile-out 'byte-min 0)))))) | |
428 | 3270 |
3271 | |
3272 ;; more complicated compiler macros | |
3273 | |
3274 (byte-defop-compiler list) | |
3275 (byte-defop-compiler concat) | |
3276 (byte-defop-compiler fset) | |
3277 (byte-defop-compiler insert) | |
3278 (byte-defop-compiler-1 function byte-compile-function-form) | |
446 | 3279 (byte-defop-compiler max) |
3280 (byte-defop-compiler min) | |
3281 (byte-defop-compiler (+ byte-plus) byte-compile-plus) | |
3282 (byte-defop-compiler-1 - byte-compile-minus) | |
3283 (byte-defop-compiler (* byte-mult) byte-compile-mult) | |
3284 (byte-defop-compiler (/ byte-quo) byte-compile-quo) | |
428 | 3285 (byte-defop-compiler nconc) |
3286 (byte-defop-compiler-1 beginning-of-line) | |
3287 | |
3288 (byte-defop-compiler (= byte-eqlsign) byte-compile-arithcompare) | |
3289 (byte-defop-compiler (< byte-lss) byte-compile-arithcompare) | |
3290 (byte-defop-compiler (> byte-gtr) byte-compile-arithcompare) | |
3291 (byte-defop-compiler (<= byte-leq) byte-compile-arithcompare) | |
3292 (byte-defop-compiler (>= byte-geq) byte-compile-arithcompare) | |
3293 | |
3294 (defun byte-compile-arithcompare (form) | |
3295 (case (length (cdr form)) | |
3296 (0 (byte-compile-subr-wrong-args form "1 or more")) | |
549 | 3297 (1 (if byte-compile-delete-errors |
3298 (byte-compile-constant t) | |
3299 (byte-compile-normal-call form))) | |
428 | 3300 (2 (byte-compile-two-args form)) |
3301 (t (byte-compile-normal-call form)))) | |
3302 | |
3303 (byte-defop-compiler /= byte-compile-/=) | |
3304 | |
3305 (defun byte-compile-/= (form) | |
3306 (case (length (cdr form)) | |
3307 (0 (byte-compile-subr-wrong-args form "1 or more")) | |
3308 (1 (byte-compile-constant t)) | |
3309 ;; optimize (/= X Y) to (not (= X Y)) | |
3310 (2 (byte-compile-form-do-effect `(not (= ,@(cdr form))))) | |
3311 (t (byte-compile-normal-call form)))) | |
3312 | |
3313 ;; buffer-substring now has its own function. This used to be | |
3314 ;; 2+1, but now all args are optional. | |
3315 (byte-defop-compiler buffer-substring) | |
3316 | |
3317 (defun byte-compile-buffer-substring (form) | |
3318 ;; buffer-substring used to take exactly two args, but now takes 0-3. | |
3319 ;; convert 0-2 to two args and use special bytecode operand. | |
3320 ;; convert 3 args to a normal call. | |
3321 (case (length (cdr form)) | |
3322 (0 (byte-compile-two-args (append form '(nil nil)))) | |
3323 (1 (byte-compile-two-args (append form '(nil)))) | |
3324 (2 (byte-compile-two-args form)) | |
3325 (3 (byte-compile-normal-call form)) | |
3326 (t (byte-compile-subr-wrong-args form "0-3")))) | |
3327 | |
3328 (defun byte-compile-list (form) | |
3329 (let* ((args (cdr form)) | |
3330 (nargs (length args))) | |
3331 (cond | |
3332 ((= nargs 0) | |
3333 (byte-compile-constant nil)) | |
3334 ((< nargs 5) | |
3335 (mapcar 'byte-compile-form args) | |
3336 (byte-compile-out | |
3337 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- nargs)) | |
3338 0)) | |
3339 ((< nargs 256) | |
3340 (mapcar 'byte-compile-form args) | |
3341 (byte-compile-out 'byte-listN nargs)) | |
3342 (t (byte-compile-normal-call form))))) | |
3343 | |
3344 (defun byte-compile-concat (form) | |
3345 (let* ((args (cdr form)) | |
3346 (nargs (length args))) | |
3347 ;; Concat of one arg is not a no-op if arg is not a string. | |
3348 (cond | |
3349 ((memq nargs '(2 3 4)) | |
3350 (mapcar 'byte-compile-form args) | |
3351 (byte-compile-out | |
3352 (aref [byte-concat2 byte-concat3 byte-concat4] (- nargs 2)) | |
3353 0)) | |
3354 ((eq nargs 0) | |
3355 (byte-compile-form "")) | |
3356 ((< nargs 256) | |
3357 (mapcar 'byte-compile-form args) | |
3358 (byte-compile-out 'byte-concatN nargs)) | |
3359 ((byte-compile-normal-call form))))) | |
3360 | |
446 | 3361 (defun byte-compile-plus (form) |
3362 (let ((args (cdr form))) | |
3363 (case (length args) | |
3364 (0 (byte-compile-constant 0)) | |
3365 (1 (byte-compile-plus (append form '(0)))) | |
3366 (t (byte-compile-form (car args)) | |
3367 (dolist (elt (cdr args)) | |
3368 (case elt | |
3369 (0 (when (not byte-compile-delete-errors) | |
3370 (byte-compile-constant 0) | |
3371 (byte-compile-out 'byte-plus 0))) | |
3372 (+1 (byte-compile-out 'byte-add1 0)) | |
3373 (-1 (byte-compile-out 'byte-sub1 0)) | |
3374 (t | |
3375 (byte-compile-form elt) | |
3376 (byte-compile-out 'byte-plus 0)))))))) | |
3377 | |
428 | 3378 (defun byte-compile-minus (form) |
3379 (let ((args (cdr form))) | |
3380 (case (length args) | |
3381 (0 (byte-compile-subr-wrong-args form "1 or more")) | |
3382 (1 (byte-compile-form (car args)) | |
3383 (byte-compile-out 'byte-negate 0)) | |
3384 (t (byte-compile-form (car args)) | |
3385 (dolist (elt (cdr args)) | |
446 | 3386 (case elt |
3387 (0 (when (not byte-compile-delete-errors) | |
3388 (byte-compile-constant 0) | |
3389 (byte-compile-out 'byte-diff 0))) | |
3390 (+1 (byte-compile-out 'byte-sub1 0)) | |
3391 (-1 (byte-compile-out 'byte-add1 0)) | |
3392 (t | |
3393 (byte-compile-form elt) | |
3394 (byte-compile-out 'byte-diff 0)))))))) | |
3395 | |
3396 (defun byte-compile-mult (form) | |
3397 (let ((args (cdr form))) | |
3398 (case (length args) | |
3399 (0 (byte-compile-constant 1)) | |
3400 (1 (byte-compile-mult (append form '(1)))) | |
3401 (t (byte-compile-form (car args)) | |
3402 (dolist (elt (cdr args)) | |
3403 (case elt | |
3404 (1 (when (not byte-compile-delete-errors) | |
3405 (byte-compile-constant 1) | |
3406 (byte-compile-out 'byte-mult 0))) | |
3407 (-1 (byte-compile-out 'byte-negate 0)) | |
3408 (2 (byte-compile-out 'byte-dup 0) | |
3409 (byte-compile-out 'byte-plus 0)) | |
3410 (t | |
3411 (byte-compile-form elt) | |
3412 (byte-compile-out 'byte-mult 0)))))))) | |
428 | 3413 |
3414 (defun byte-compile-quo (form) | |
3415 (let ((args (cdr form))) | |
3416 (case (length args) | |
3417 (0 (byte-compile-subr-wrong-args form "1 or more")) | |
3418 (1 (byte-compile-constant 1) | |
3419 (byte-compile-form (car args)) | |
3420 (byte-compile-out 'byte-quo 0)) | |
3421 (t (byte-compile-form (car args)) | |
3422 (dolist (elt (cdr args)) | |
446 | 3423 (case elt |
3424 (+1 (when (not byte-compile-delete-errors) | |
3425 (byte-compile-constant 1) | |
3426 (byte-compile-out 'byte-quo 0))) | |
3427 (-1 (byte-compile-out 'byte-negate 0)) | |
3428 (t | |
3429 (when (and (numberp elt) (= elt 0)) | |
3430 (byte-compile-warn "Attempt to divide by zero: %s" form)) | |
3431 (byte-compile-form elt) | |
3432 (byte-compile-out 'byte-quo 0)))))))) | |
428 | 3433 |
3434 (defun byte-compile-nconc (form) | |
3435 (let ((args (cdr form))) | |
3436 (case (length args) | |
3437 (0 (byte-compile-constant nil)) | |
3438 ;; nconc of one arg is a noop, even if that arg isn't a list. | |
3439 (1 (byte-compile-form (car args))) | |
3440 (t (byte-compile-form (car args)) | |
3441 (dolist (elt (cdr args)) | |
3442 (byte-compile-form elt) | |
3443 (byte-compile-out 'byte-nconc 0)))))) | |
3444 | |
3445 (defun byte-compile-fset (form) | |
3446 ;; warn about forms like (fset 'foo '(lambda () ...)) | |
3447 ;; (where the lambda expression is non-trivial...) | |
3448 ;; Except don't warn if the first argument is 'make-byte-code, because | |
3449 ;; I'm sick of getting mail asking me whether that warning is a problem. | |
3450 (let ((fn (nth 2 form)) | |
3451 body) | |
3452 (when (and (eq (car-safe fn) 'quote) | |
3453 (eq (car-safe (setq fn (nth 1 fn))) 'lambda) | |
3454 (not (eq (car-safe (cdr-safe (nth 1 form))) 'make-byte-code))) | |
3455 (setq body (cdr (cdr fn))) | |
3456 (if (stringp (car body)) (setq body (cdr body))) | |
3457 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body))) | |
3458 (if (and (consp (car body)) | |
3459 (not (eq 'byte-code (car (car body))))) | |
3460 (byte-compile-warn | |
3461 "A quoted lambda form is the second argument of fset. This is probably | |
3462 not what you want, as that lambda cannot be compiled. Consider using | |
3463 the syntax (function (lambda (...) ...)) instead.")))) | |
3464 (byte-compile-two-args form)) | |
3465 | |
3466 (defun byte-compile-funarg (form) | |
3467 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..) | |
3468 ;; for cases where it's guaranteed that first arg will be used as a lambda. | |
3469 (byte-compile-normal-call | |
3470 (let ((fn (nth 1 form))) | |
3471 (if (and (eq (car-safe fn) 'quote) | |
3472 (eq (car-safe (nth 1 fn)) 'lambda)) | |
3473 (cons (car form) | |
3474 (cons (cons 'function (cdr fn)) | |
3475 (cdr (cdr form)))) | |
3476 form)))) | |
3477 | |
3478 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo). | |
3479 ;; Otherwise it will be incompatible with the interpreter, | |
3480 ;; and (funcall (function foo)) will lose with autoloads. | |
3481 | |
3482 (defun byte-compile-function-form (form) | |
3483 (byte-compile-constant | |
3484 (cond ((symbolp (nth 1 form)) | |
3485 (nth 1 form)) | |
3486 ((byte-compile-lambda (nth 1 form)))))) | |
3487 | |
3488 (defun byte-compile-insert (form) | |
3489 (cond ((null (cdr form)) | |
3490 (byte-compile-constant nil)) | |
3491 ((<= (length form) 256) | |
3492 (mapcar 'byte-compile-form (cdr form)) | |
3493 (if (cdr (cdr form)) | |
3494 (byte-compile-out 'byte-insertN (length (cdr form))) | |
3495 (byte-compile-out 'byte-insert 0))) | |
3496 ((memq t (mapcar 'consp (cdr (cdr form)))) | |
3497 (byte-compile-normal-call form)) | |
3498 ;; We can split it; there is no function call after inserting 1st arg. | |
3499 (t | |
3500 (while (setq form (cdr form)) | |
3501 (byte-compile-form (car form)) | |
3502 (byte-compile-out 'byte-insert 0) | |
3503 (when (cdr form) | |
3504 (byte-compile-discard)))))) | |
3505 | |
3506 ;; alas, the old (pre-19.12, and all existing versions of FSFmacs 19) | |
3507 ;; byte compiler will generate incorrect code for | |
3508 ;; (beginning-of-line nil buffer) because it buggily doesn't | |
3509 ;; check the number of arguments passed to beginning-of-line. | |
3510 | |
3511 (defun byte-compile-beginning-of-line (form) | |
3512 (let ((len (length form))) | |
3513 (cond ((> len 3) | |
3514 (byte-compile-subr-wrong-args form "0-2")) | |
3515 ((or (= len 3) (not (byte-compile-constp (nth 1 form)))) | |
3516 (byte-compile-normal-call form)) | |
3517 (t | |
3518 (byte-compile-form | |
3519 (list 'forward-line | |
3520 (if (integerp (setq form (or (eval (nth 1 form)) 1))) | |
3521 (1- form) | |
3522 (byte-compile-warn | |
3523 "Non-numeric arg to beginning-of-line: %s" form) | |
3524 (list '1- (list 'quote form)))) | |
3525 t) | |
3526 (byte-compile-constant nil))))) | |
3527 | |
3528 | |
3529 (byte-defop-compiler set) | |
3530 (byte-defop-compiler-1 setq) | |
3531 (byte-defop-compiler-1 set-default) | |
3532 (byte-defop-compiler-1 setq-default) | |
3533 | |
3534 (byte-defop-compiler-1 quote) | |
3535 (byte-defop-compiler-1 quote-form) | |
3536 | |
3537 (defun byte-compile-setq (form) | |
3538 (let ((args (cdr form)) var val) | |
3539 (if (null args) | |
3540 ;; (setq), with no arguments. | |
3541 (byte-compile-form nil for-effect) | |
3542 (while args | |
3543 (setq var (pop args)) | |
3544 (if (null args) | |
3545 ;; Odd number of args? Let `set' get the error. | |
3546 (byte-compile-form `(set ',var) for-effect) | |
3547 (setq val (pop args)) | |
3548 (if (keywordp var) | |
3549 ;; (setq :foo ':foo) compatibility kludge | |
3550 (byte-compile-form `(set ',var ,val) (if args t for-effect)) | |
3551 (byte-compile-form val) | |
3552 (unless (or args for-effect) | |
3553 (byte-compile-out 'byte-dup 0)) | |
3554 (byte-compile-variable-ref 'byte-varset var)))))) | |
3555 (setq for-effect nil)) | |
3556 | |
3557 (defun byte-compile-set (form) | |
3558 ;; Compile (set 'foo x) as (setq foo x) for trivially better code and so | |
3559 ;; that we get applicable warnings. Compile everything else (including | |
3560 ;; malformed calls) like a normal 2-arg byte-coded function. | |
3561 (let ((symform (nth 1 form)) | |
3562 (valform (nth 2 form)) | |
3563 sym) | |
3564 (if (and (= (length form) 3) | |
3565 (= (safe-length symform) 2) | |
3566 (eq (car symform) 'quote) | |
3567 (symbolp (setq sym (car (cdr symform)))) | |
3568 (not (byte-compile-constant-symbol-p sym))) | |
3569 (byte-compile-setq `(setq ,sym ,valform)) | |
3570 (byte-compile-two-args form)))) | |
3571 | |
3572 (defun byte-compile-setq-default (form) | |
3573 (let ((args (cdr form))) | |
3574 (if (null args) | |
3575 ;; (setq-default), with no arguments. | |
3576 (byte-compile-form nil for-effect) | |
3577 ;; emit multiple calls to `set-default' if necessary | |
3578 (while args | |
3579 (byte-compile-form | |
3580 ;; Odd number of args? Let `set-default' get the error. | |
3581 `(set-default ',(pop args) ,@(if args (list (pop args)) nil)) | |
3582 (if args t for-effect))))) | |
3583 (setq for-effect nil)) | |
3584 | |
3585 | |
3586 (defun byte-compile-set-default (form) | |
3587 (let* ((args (cdr form)) | |
3588 (nargs (length args)) | |
3589 (var (car args))) | |
3590 (when (and (= (safe-length var) 2) | |
3591 (eq (car var) 'quote)) | |
3592 (let ((sym (nth 1 var))) | |
3593 (cond | |
3594 ((not (symbolp sym)) | |
3595 (byte-compile-warn "Attempt to set-globally non-symbol %s" sym)) | |
3596 ((byte-compile-constant-symbol-p sym) | |
3597 (byte-compile-warn "Attempt to set-globally constant symbol %s" sym)) | |
3598 ((let ((cell (assq sym byte-compile-bound-variables))) | |
3599 (and cell | |
3600 (setcdr cell (logior (cdr cell) byte-compile-assigned-bit)) | |
3601 t))) | |
3602 ;; notice calls to set-default/setq-default for variables which | |
3603 ;; have not been declared with defvar/defconst. | |
3604 ((globally-boundp sym)) ; OK | |
3605 ((not (memq 'free-vars byte-compile-warnings))) ; warnings suppressed? | |
3606 ((memq sym byte-compile-free-assignments)) ; already warned about sym | |
3607 (t | |
3608 (byte-compile-warn "assignment to free variable %s" sym) | |
3609 (push sym byte-compile-free-assignments))))) | |
3610 (if (= nargs 2) | |
3611 ;; now emit a normal call to set-default | |
3612 (byte-compile-normal-call form) | |
3613 (byte-compile-subr-wrong-args form 2)))) | |
3614 | |
3615 | |
3616 (defun byte-compile-quote (form) | |
3617 (byte-compile-constant (car (cdr form)))) | |
3618 | |
3619 (defun byte-compile-quote-form (form) | |
3620 (byte-compile-constant (byte-compile-top-level (nth 1 form)))) | |
3621 | |
3622 | |
3623 ;;; control structures | |
3624 | |
3625 (defun byte-compile-body (body &optional for-effect) | |
3626 (while (cdr body) | |
3627 (byte-compile-form (car body) t) | |
3628 (setq body (cdr body))) | |
3629 (byte-compile-form (car body) for-effect)) | |
3630 | |
3631 (proclaim-inline byte-compile-body-do-effect) | |
3632 (defun byte-compile-body-do-effect (body) | |
3633 (byte-compile-body body for-effect) | |
3634 (setq for-effect nil)) | |
3635 | |
3636 (proclaim-inline byte-compile-form-do-effect) | |
3637 (defun byte-compile-form-do-effect (form) | |
3638 (byte-compile-form form for-effect) | |
3639 (setq for-effect nil)) | |
3640 | |
3641 (byte-defop-compiler-1 inline byte-compile-progn) | |
3642 (byte-defop-compiler-1 progn) | |
3643 (byte-defop-compiler-1 prog1) | |
3644 (byte-defop-compiler-1 prog2) | |
3645 (byte-defop-compiler-1 if) | |
3646 (byte-defop-compiler-1 cond) | |
3647 (byte-defop-compiler-1 and) | |
3648 (byte-defop-compiler-1 or) | |
3649 (byte-defop-compiler-1 while) | |
3650 (byte-defop-compiler-1 funcall) | |
3651 (byte-defop-compiler-1 apply byte-compile-funarg) | |
3652 (byte-defop-compiler-1 mapcar byte-compile-funarg) | |
3653 (byte-defop-compiler-1 mapatoms byte-compile-funarg) | |
3654 (byte-defop-compiler-1 mapconcat byte-compile-funarg) | |
3655 (byte-defop-compiler-1 let) | |
3656 (byte-defop-compiler-1 let*) | |
3657 | |
3658 (defun byte-compile-progn (form) | |
3659 (byte-compile-body-do-effect (cdr form))) | |
3660 | |
3661 (defun byte-compile-prog1 (form) | |
3662 (setq form (cdr form)) | |
3663 (byte-compile-form-do-effect (pop form)) | |
3664 (byte-compile-body form t)) | |
3665 | |
3666 (defun byte-compile-prog2 (form) | |
3667 (setq form (cdr form)) | |
3668 (byte-compile-form (pop form) t) | |
3669 (byte-compile-form-do-effect (pop form)) | |
3670 (byte-compile-body form t)) | |
3671 | |
3672 (defmacro byte-compile-goto-if (cond discard tag) | |
3673 `(byte-compile-goto | |
3674 (if ,cond | |
3675 (if ,discard 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop) | |
3676 (if ,discard 'byte-goto-if-nil 'byte-goto-if-nil-else-pop)) | |
3677 ,tag)) | |
3678 | |
3679 (defun byte-compile-if (form) | |
3680 (byte-compile-form (car (cdr form))) | |
3681 (if (null (nthcdr 3 form)) | |
3682 ;; No else-forms | |
3683 (let ((donetag (byte-compile-make-tag))) | |
3684 (byte-compile-goto-if nil for-effect donetag) | |
3685 (byte-compile-form (nth 2 form) for-effect) | |
3686 (byte-compile-out-tag donetag)) | |
3687 (let ((donetag (byte-compile-make-tag)) (elsetag (byte-compile-make-tag))) | |
3688 (byte-compile-goto 'byte-goto-if-nil elsetag) | |
3689 (byte-compile-form (nth 2 form) for-effect) | |
3690 (byte-compile-goto 'byte-goto donetag) | |
3691 (byte-compile-out-tag elsetag) | |
3692 (byte-compile-body (cdr (cdr (cdr form))) for-effect) | |
3693 (byte-compile-out-tag donetag))) | |
3694 (setq for-effect nil)) | |
3695 | |
3696 (defun byte-compile-cond (clauses) | |
3697 (let ((donetag (byte-compile-make-tag)) | |
3698 nexttag clause) | |
3699 (while (setq clauses (cdr clauses)) | |
3700 (setq clause (car clauses)) | |
3701 (cond ((or (eq (car clause) t) | |
3702 (and (eq (car-safe (car clause)) 'quote) | |
3703 (car-safe (cdr-safe (car clause))))) | |
3704 ;; Unconditional clause | |
3705 (setq clause (cons t clause) | |
3706 clauses nil)) | |
3707 ((cdr clauses) | |
3708 (byte-compile-form (car clause)) | |
3709 (if (null (cdr clause)) | |
3710 ;; First clause is a singleton. | |
3711 (byte-compile-goto-if t for-effect donetag) | |
3712 (setq nexttag (byte-compile-make-tag)) | |
3713 (byte-compile-goto 'byte-goto-if-nil nexttag) | |
3714 (byte-compile-body (cdr clause) for-effect) | |
3715 (byte-compile-goto 'byte-goto donetag) | |
3716 (byte-compile-out-tag nexttag))))) | |
3717 ;; Last clause | |
3718 (and (cdr clause) (not (eq (car clause) t)) | |
3719 (progn (byte-compile-form (car clause)) | |
3720 (byte-compile-goto-if nil for-effect donetag) | |
3721 (setq clause (cdr clause)))) | |
3722 (byte-compile-body-do-effect clause) | |
3723 (byte-compile-out-tag donetag))) | |
3724 | |
3725 (defun byte-compile-and (form) | |
3726 (let ((failtag (byte-compile-make-tag)) | |
3727 (args (cdr form))) | |
3728 (if (null args) | |
3729 (byte-compile-form-do-effect t) | |
3730 (while (cdr args) | |
3731 (byte-compile-form (car args)) | |
3732 (byte-compile-goto-if nil for-effect failtag) | |
3733 (setq args (cdr args))) | |
3734 (byte-compile-form-do-effect (car args)) | |
3735 (byte-compile-out-tag failtag)))) | |
3736 | |
3737 (defun byte-compile-or (form) | |
3738 (let ((wintag (byte-compile-make-tag)) | |
3739 (args (cdr form))) | |
3740 (if (null args) | |
3741 (byte-compile-form-do-effect nil) | |
3742 (while (cdr args) | |
3743 (byte-compile-form (car args)) | |
3744 (byte-compile-goto-if t for-effect wintag) | |
3745 (setq args (cdr args))) | |
3746 (byte-compile-form-do-effect (car args)) | |
3747 (byte-compile-out-tag wintag)))) | |
3748 | |
3749 (defun byte-compile-while (form) | |
3750 (let ((endtag (byte-compile-make-tag)) | |
3751 (looptag (byte-compile-make-tag))) | |
3752 (byte-compile-out-tag looptag) | |
3753 (byte-compile-form (car (cdr form))) | |
3754 (byte-compile-goto-if nil for-effect endtag) | |
3755 (byte-compile-body (cdr (cdr form)) t) | |
3756 (byte-compile-goto 'byte-goto looptag) | |
3757 (byte-compile-out-tag endtag) | |
3758 (setq for-effect nil))) | |
3759 | |
3760 (defun byte-compile-funcall (form) | |
3761 (mapcar 'byte-compile-form (cdr form)) | |
3762 (byte-compile-out 'byte-call (length (cdr (cdr form))))) | |
3763 | |
3764 | |
3765 (defun byte-compile-let (form) | |
3766 ;; First compute the binding values in the old scope. | |
3767 (let ((varlist (car (cdr form)))) | |
3768 (while varlist | |
3769 (if (consp (car varlist)) | |
3770 (byte-compile-form (car (cdr (car varlist)))) | |
3771 (byte-compile-push-constant nil)) | |
3772 (setq varlist (cdr varlist)))) | |
3773 (let ((byte-compile-bound-variables | |
3774 (cons 'new-scope byte-compile-bound-variables)) | |
3775 (varlist (reverse (car (cdr form)))) | |
3776 (extra-flags | |
3777 ;; If this let is of the form (let (...) (byte-code ...)) | |
3778 ;; then assume that it is the result of a transformation of | |
3779 ;; ((lambda (...) (byte-code ... )) ...) and thus compile | |
3780 ;; the variable bindings as if they were arglist bindings | |
3781 ;; (which matters for what warnings.) | |
3782 (if (eq 'byte-code (car-safe (nth 2 form))) | |
3783 byte-compile-arglist-bit | |
3784 nil))) | |
3785 (while varlist | |
3786 (byte-compile-variable-ref 'byte-varbind | |
3787 (if (consp (car varlist)) | |
3788 (car (car varlist)) | |
3789 (car varlist)) | |
3790 extra-flags) | |
3791 (setq varlist (cdr varlist))) | |
3792 (byte-compile-body-do-effect (cdr (cdr form))) | |
3793 (if (memq 'unused-vars byte-compile-warnings) | |
3794 ;; done compiling in this scope, warn now. | |
3795 (byte-compile-warn-about-unused-variables)) | |
3796 (byte-compile-out 'byte-unbind (length (car (cdr form)))))) | |
3797 | |
3798 (defun byte-compile-let* (form) | |
3799 (let ((byte-compile-bound-variables | |
3800 (cons 'new-scope byte-compile-bound-variables)) | |
3801 (varlist (copy-sequence (car (cdr form))))) | |
3802 (while varlist | |
3803 (if (atom (car varlist)) | |
3804 (byte-compile-push-constant nil) | |
3805 (byte-compile-form (car (cdr (car varlist)))) | |
3806 (setcar varlist (car (car varlist)))) | |
3807 (byte-compile-variable-ref 'byte-varbind (car varlist)) | |
3808 (setq varlist (cdr varlist))) | |
3809 (byte-compile-body-do-effect (cdr (cdr form))) | |
3810 (if (memq 'unused-vars byte-compile-warnings) | |
3811 ;; done compiling in this scope, warn now. | |
3812 (byte-compile-warn-about-unused-variables)) | |
3813 (byte-compile-out 'byte-unbind (length (car (cdr form)))))) | |
3814 | |
3815 | |
3816 ;;(byte-defop-compiler-1 /= byte-compile-negated) | |
3817 (byte-defop-compiler-1 atom byte-compile-negated) | |
3818 (byte-defop-compiler-1 nlistp byte-compile-negated) | |
3819 | |
3820 ;;(put '/= 'byte-compile-negated-op '=) | |
3821 (put 'atom 'byte-compile-negated-op 'consp) | |
3822 (put 'nlistp 'byte-compile-negated-op 'listp) | |
3823 | |
3824 (defun byte-compile-negated (form) | |
3825 (byte-compile-form-do-effect (byte-compile-negation-optimizer form))) | |
3826 | |
3827 ;; Even when optimization is off, atom is optimized to (not (consp ...)). | |
3828 (defun byte-compile-negation-optimizer (form) | |
3829 ;; an optimizer for forms where <form1> is less efficient than (not <form2>) | |
3830 (list 'not | |
3831 (cons (or (get (car form) 'byte-compile-negated-op) | |
3832 (error | |
3833 "Compiler error: `%s' has no `byte-compile-negated-op' property" | |
3834 (car form))) | |
3835 (cdr form)))) | |
3836 | |
3837 ;;; other tricky macro-like special-forms | |
3838 | |
3839 (byte-defop-compiler-1 catch) | |
3840 (byte-defop-compiler-1 unwind-protect) | |
3841 (byte-defop-compiler-1 condition-case) | |
3842 (byte-defop-compiler-1 save-excursion) | |
3843 (byte-defop-compiler-1 save-current-buffer) | |
3844 (byte-defop-compiler-1 save-restriction) | |
3845 (byte-defop-compiler-1 save-window-excursion) | |
3846 (byte-defop-compiler-1 with-output-to-temp-buffer) | |
3847 ;; no track-mouse. | |
3848 | |
3849 (defun byte-compile-catch (form) | |
3850 (byte-compile-form (car (cdr form))) | |
3851 (byte-compile-push-constant | |
3852 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect)) | |
3853 (byte-compile-out 'byte-catch 0)) | |
3854 | |
3855 (defun byte-compile-unwind-protect (form) | |
3856 (byte-compile-push-constant | |
3857 (byte-compile-top-level-body (cdr (cdr form)) t)) | |
3858 (byte-compile-out 'byte-unwind-protect 0) | |
3859 (byte-compile-form-do-effect (car (cdr form))) | |
3860 (byte-compile-out 'byte-unbind 1)) | |
3861 | |
3862 ;;(defun byte-compile-track-mouse (form) | |
3863 ;; (byte-compile-form | |
3864 ;; (list | |
3865 ;; 'funcall | |
3866 ;; (list 'quote | |
3867 ;; (list 'lambda nil | |
3868 ;; (cons 'track-mouse | |
3869 ;; (byte-compile-top-level-body (cdr form)))))))) | |
3870 | |
3871 (defun byte-compile-condition-case (form) | |
3872 (let* ((var (nth 1 form)) | |
3873 (byte-compile-bound-variables | |
3874 (if var | |
3875 (cons (cons var 0) | |
3876 (cons 'new-scope byte-compile-bound-variables)) | |
3877 (cons 'new-scope byte-compile-bound-variables)))) | |
3878 (or (symbolp var) | |
3879 (byte-compile-warn | |
3880 "%s is not a variable-name or nil (in condition-case)" | |
3881 (prin1-to-string var))) | |
3882 (byte-compile-push-constant var) | |
3883 (byte-compile-push-constant (byte-compile-top-level | |
3884 (nth 2 form) for-effect)) | |
3885 (let ((clauses (cdr (cdr (cdr form)))) | |
3886 compiled-clauses) | |
3887 (while clauses | |
3888 (let* ((clause (car clauses)) | |
3889 (condition (car clause))) | |
3890 (cond ((not (or (symbolp condition) | |
3891 (and (listp condition) | |
3892 (let ((syms condition) (ok t)) | |
3893 (while syms | |
3894 (if (not (symbolp (car syms))) | |
3895 (setq ok nil)) | |
3896 (setq syms (cdr syms))) | |
3897 ok)))) | |
3898 (byte-compile-warn | |
3899 "%s is not a symbol naming a condition or a list of such (in condition-case)" | |
3900 (prin1-to-string condition))) | |
3901 ;; ((not (or (eq condition 't) | |
3902 ;; (and (stringp (get condition 'error-message)) | |
3903 ;; (consp (get condition 'error-conditions))))) | |
3904 ;; (byte-compile-warn | |
3905 ;; "%s is not a known condition name (in condition-case)" | |
3906 ;; condition)) | |
3907 ) | |
3908 (setq compiled-clauses | |
3909 (cons (cons condition | |
3910 (byte-compile-top-level-body | |
3911 (cdr clause) for-effect)) | |
3912 compiled-clauses))) | |
3913 (setq clauses (cdr clauses))) | |
3914 (byte-compile-push-constant (nreverse compiled-clauses))) | |
3915 (if (memq 'unused-vars byte-compile-warnings) | |
3916 ;; done compiling in this scope, warn now. | |
3917 (byte-compile-warn-about-unused-variables)) | |
3918 (byte-compile-out 'byte-condition-case 0))) | |
3919 | |
3920 | |
3921 (defun byte-compile-save-excursion (form) | |
3922 (byte-compile-out 'byte-save-excursion 0) | |
3923 (byte-compile-body-do-effect (cdr form)) | |
3924 (byte-compile-out 'byte-unbind 1)) | |
3925 | |
3926 (defun byte-compile-save-restriction (form) | |
3927 (byte-compile-out 'byte-save-restriction 0) | |
3928 (byte-compile-body-do-effect (cdr form)) | |
3929 (byte-compile-out 'byte-unbind 1)) | |
3930 | |
3931 (defun byte-compile-save-current-buffer (form) | |
3932 (if (byte-compile-version-cond byte-compile-emacs19-compatibility) | |
3933 ;; `save-current-buffer' special form is not available in XEmacs 19. | |
3934 (byte-compile-form | |
3935 `(let ((_byte_compiler_save_buffer_emulation_closure_ (current-buffer))) | |
3936 (unwind-protect | |
3937 (progn ,@(cdr form)) | |
3938 (and (buffer-live-p _byte_compiler_save_buffer_emulation_closure_) | |
3939 (set-buffer _byte_compiler_save_buffer_emulation_closure_))))) | |
3940 (byte-compile-out 'byte-save-current-buffer 0) | |
3941 (byte-compile-body-do-effect (cdr form)) | |
3942 (byte-compile-out 'byte-unbind 1))) | |
3943 | |
3944 (defun byte-compile-save-window-excursion (form) | |
3945 (byte-compile-push-constant | |
3946 (byte-compile-top-level-body (cdr form) for-effect)) | |
3947 (byte-compile-out 'byte-save-window-excursion 0)) | |
3948 | |
3949 (defun byte-compile-with-output-to-temp-buffer (form) | |
3950 (byte-compile-form (car (cdr form))) | |
3951 (byte-compile-out 'byte-temp-output-buffer-setup 0) | |
3952 (byte-compile-body (cdr (cdr form))) | |
3953 (byte-compile-out 'byte-temp-output-buffer-show 0)) | |
3954 | |
3955 | |
3956 ;;; top-level forms elsewhere | |
3957 | |
3958 (byte-defop-compiler-1 defun) | |
3959 (byte-defop-compiler-1 defmacro) | |
3960 (byte-defop-compiler-1 defvar) | |
442 | 3961 (byte-defop-compiler-1 defvar byte-compile-defvar-or-defconst) |
3962 (byte-defop-compiler-1 defconst byte-compile-defvar-or-defconst) | |
428 | 3963 (byte-defop-compiler-1 autoload) |
3964 ;; According to Mly this can go now that lambda is a macro | |
3965 ;(byte-defop-compiler-1 lambda byte-compile-lambda-form) | |
3966 (byte-defop-compiler-1 defalias) | |
3967 (byte-defop-compiler-1 define-function) | |
3968 | |
3969 (defun byte-compile-defun (form) | |
3970 ;; This is not used for file-level defuns with doc strings. | |
3971 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning. | |
3972 (list 'fset (list 'quote (nth 1 form)) | |
3973 (byte-compile-byte-code-maker | |
3974 (byte-compile-lambda (cons 'lambda (cdr (cdr form))))))) | |
3975 (byte-compile-discard) | |
3976 (byte-compile-constant (nth 1 form))) | |
3977 | |
3978 (defun byte-compile-defmacro (form) | |
3979 ;; This is not used for file-level defmacros with doc strings. | |
3980 (byte-compile-body-do-effect | |
3981 (list (list 'fset (list 'quote (nth 1 form)) | |
3982 (let ((code (byte-compile-byte-code-maker | |
3983 (byte-compile-lambda | |
3984 (cons 'lambda (cdr (cdr form))))))) | |
3985 (if (eq (car-safe code) 'make-byte-code) | |
3986 (list 'cons ''macro code) | |
3987 (list 'quote (cons 'macro (eval code)))))) | |
3988 (list 'quote (nth 1 form))))) | |
3989 | |
442 | 3990 (defun byte-compile-defvar-or-defconst (form) |
3991 ;; This is not used for file-level defvar/defconsts with doc strings: | |
3992 ;; byte-compile-file-form-defvar-or-defconst will be used in that case. | |
3993 ;; (defvar|defconst VAR [VALUE [DOCSTRING]]) | |
3994 (let ((fun (nth 0 form)) | |
3995 (var (nth 1 form)) | |
428 | 3996 (value (nth 2 form)) |
3997 (string (nth 3 form))) | |
442 | 3998 (when (> (length form) 4) |
3999 (byte-compile-warn | |
4000 "%s %s called with %d arguments, but accepts only %s" | |
4001 fun var (length (cdr form)) 3)) | |
4002 (when (memq 'free-vars byte-compile-warnings) | |
4003 (push (cons var byte-compile-global-bit) byte-compile-bound-variables)) | |
428 | 4004 (byte-compile-body-do-effect |
442 | 4005 (list |
4006 ;; Put the defined variable in this library's load-history entry | |
444 | 4007 ;; just as a real defvar would, but only in top-level forms with values. |
4008 (when (and (> (length form) 2) | |
4009 (null byte-compile-current-form)) | |
442 | 4010 `(push ',var current-load-list)) |
4011 (when (> (length form) 3) | |
4012 (when (and string (not (stringp string))) | |
4013 (byte-compile-warn "Third arg to %s %s is not a string: %s" | |
4014 fun var string)) | |
4015 `(put ',var 'variable-documentation ,string)) | |
4016 (if (cdr (cdr form)) ; `value' provided | |
4017 (if (eq fun 'defconst) | |
4018 ;; `defconst' sets `var' unconditionally. | |
4019 `(setq ,var ,value) | |
4020 ;; `defvar' sets `var' only when unbound. | |
1672 | 4021 `(if (not (default-boundp ',var)) (set-default ',var ,value)))) |
442 | 4022 `',var)))) |
428 | 4023 |
4024 (defun byte-compile-autoload (form) | |
4025 (and (byte-compile-constp (nth 1 form)) | |
4026 (byte-compile-constp (nth 5 form)) | |
4027 (memq (eval (nth 5 form)) '(t macro)) ; macro-p | |
4028 (not (fboundp (eval (nth 1 form)))) | |
4029 (byte-compile-warn | |
4030 "The compiler ignores `autoload' except at top level. You should | |
4031 probably put the autoload of the macro `%s' at top-level." | |
4032 (eval (nth 1 form)))) | |
4033 (byte-compile-normal-call form)) | |
4034 | |
4035 ;; Lambda's in valid places are handled as special cases by various code. | |
4036 ;; The ones that remain are errors. | |
4037 ;; According to Mly this can go now that lambda is a macro | |
4038 ;(defun byte-compile-lambda-form (form) | |
4039 ; (byte-compile-warn | |
4040 ; "`lambda' used in function position is invalid: probably you mean #'%s" | |
4041 ; (let ((print-escape-newlines t) | |
4042 ; (print-level 4) | |
4043 ; (print-length 4)) | |
4044 ; (prin1-to-string form))) | |
4045 ; (byte-compile-normal-call | |
4046 ; (list 'signal ''error | |
4047 ; (list 'quote (list "`lambda' used in function position" form))))) | |
4048 | |
4049 ;; Compile normally, but deal with warnings for the function being defined. | |
4050 (defun byte-compile-defalias (form) | |
4051 (if (and (consp (cdr form)) (consp (nth 1 form)) | |
4052 (eq (car (nth 1 form)) 'quote) | |
4053 (consp (cdr (nth 1 form))) | |
4054 (symbolp (nth 1 (nth 1 form))) | |
4055 (consp (nthcdr 2 form)) | |
4056 (consp (nth 2 form)) | |
4057 (eq (car (nth 2 form)) 'quote) | |
4058 (consp (cdr (nth 2 form))) | |
4059 (symbolp (nth 1 (nth 2 form)))) | |
4060 (progn | |
4061 (byte-compile-defalias-warn (nth 1 (nth 1 form)) | |
4062 (nth 1 (nth 2 form))) | |
4063 (setq byte-compile-function-environment | |
4064 (cons (cons (nth 1 (nth 1 form)) | |
4065 (nth 1 (nth 2 form))) | |
4066 byte-compile-function-environment)))) | |
4067 (byte-compile-normal-call form)) | |
4068 | |
4069 (defun byte-compile-define-function (form) | |
4070 (byte-compile-defalias form)) | |
4071 | |
4072 ;; Turn off warnings about prior calls to the function being defalias'd. | |
4073 ;; This could be smarter and compare those calls with | |
4074 ;; the function it is being aliased to. | |
4075 (defun byte-compile-defalias-warn (new alias) | |
4076 (let ((calls (assq new byte-compile-unresolved-functions))) | |
4077 (if calls | |
4078 (setq byte-compile-unresolved-functions | |
4079 (delq calls byte-compile-unresolved-functions))))) | |
4080 | |
4081 ;;; tags | |
4082 | |
4083 ;; Note: Most operations will strip off the 'TAG, but it speeds up | |
4084 ;; optimization to have the 'TAG as a part of the tag. | |
4085 ;; Tags will be (TAG . (tag-number . stack-depth)). | |
4086 (defun byte-compile-make-tag () | |
4087 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number)))) | |
4088 | |
4089 | |
4090 (defun byte-compile-out-tag (tag) | |
4091 (push tag byte-compile-output) | |
4092 (if (cdr (cdr tag)) | |
4093 (progn | |
4094 ;; ## remove this someday | |
4095 (and byte-compile-depth | |
4096 (not (= (cdr (cdr tag)) byte-compile-depth)) | |
4097 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag)))) | |
4098 (setq byte-compile-depth (cdr (cdr tag)))) | |
4099 (setcdr (cdr tag) byte-compile-depth))) | |
4100 | |
4101 (defun byte-compile-goto (opcode tag) | |
4102 (push (cons opcode tag) byte-compile-output) | |
4103 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops) | |
4104 (1- byte-compile-depth) | |
4105 byte-compile-depth)) | |
4106 (setq byte-compile-depth (and (not (eq opcode 'byte-goto)) | |
4107 (1- byte-compile-depth)))) | |
4108 | |
4109 (defun byte-compile-out (opcode offset) | |
4110 (push (cons opcode offset) byte-compile-output) | |
4111 (case opcode | |
4112 (byte-call | |
4113 (setq byte-compile-depth (- byte-compile-depth offset))) | |
4114 (byte-return | |
4115 ;; This is actually an unnecessary case, because there should be | |
4116 ;; no more opcodes behind byte-return. | |
4117 (setq byte-compile-depth nil)) | |
4118 (t | |
4119 (setq byte-compile-depth (+ byte-compile-depth | |
4120 (or (aref byte-stack+-info | |
4121 (symbol-value opcode)) | |
4122 (- (1- offset)))) | |
4123 byte-compile-maxdepth (max byte-compile-depth | |
4124 byte-compile-maxdepth)))) | |
4125 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow")) | |
4126 ) | |
4127 | |
4128 | |
4129 ;;; call tree stuff | |
4130 | |
4131 (defun byte-compile-annotate-call-tree (form) | |
4132 (let (entry) | |
4133 ;; annotate the current call | |
4134 (if (setq entry (assq (car form) byte-compile-call-tree)) | |
4135 (or (memq byte-compile-current-form (nth 1 entry)) ;callers | |
4136 (setcar (cdr entry) | |
4137 (cons byte-compile-current-form (nth 1 entry)))) | |
4138 (push (list (car form) (list byte-compile-current-form) nil) | |
4139 byte-compile-call-tree)) | |
4140 ;; annotate the current function | |
4141 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree)) | |
4142 (or (memq (car form) (nth 2 entry)) ;called | |
4143 (setcar (cdr (cdr entry)) | |
4144 (cons (car form) (nth 2 entry)))) | |
4145 (push (list byte-compile-current-form nil (list (car form))) | |
4146 byte-compile-call-tree)))) | |
4147 | |
4148 ;; Renamed from byte-compile-report-call-tree | |
4149 ;; to avoid interfering with completion of byte-compile-file. | |
4150 ;;;###autoload | |
4151 (defun display-call-tree (&optional filename) | |
4152 "Display a call graph of a specified file. | |
4153 This lists which functions have been called, what functions called | |
4154 them, and what functions they call. The list includes all functions | |
4155 whose definitions have been compiled in this Emacs session, as well as | |
4156 all functions called by those functions. | |
4157 | |
4158 The call graph does not include macros, inline functions, or | |
4159 primitives that the byte-code interpreter knows about directly \(eq, | |
4160 cons, etc.\). | |
4161 | |
4162 The call tree also lists those functions which are not known to be called | |
4163 \(that is, to which no calls have been compiled\), and which cannot be | |
4164 invoked interactively." | |
4165 (interactive) | |
4166 (message "Generating call tree...") | |
4167 (with-output-to-temp-buffer "*Call-Tree*" | |
4168 (set-buffer "*Call-Tree*") | |
4169 (erase-buffer) | |
4170 (message "Generating call tree... (sorting on %s)" | |
4171 byte-compile-call-tree-sort) | |
4172 (insert "Call tree for " | |
4173 (cond ((null byte-compile-current-file) (or filename "???")) | |
4174 ((stringp byte-compile-current-file) | |
4175 byte-compile-current-file) | |
4176 (t (buffer-name byte-compile-current-file))) | |
4177 " sorted on " | |
4178 (prin1-to-string byte-compile-call-tree-sort) | |
4179 ":\n\n") | |
4180 (if byte-compile-call-tree-sort | |
4181 (setq byte-compile-call-tree | |
4182 (sort byte-compile-call-tree | |
4183 (cond | |
4184 ((eq byte-compile-call-tree-sort 'callers) | |
4185 #'(lambda (x y) (< (length (nth 1 x)) | |
4186 (length (nth 1 y))))) | |
4187 ((eq byte-compile-call-tree-sort 'calls) | |
4188 #'(lambda (x y) (< (length (nth 2 x)) | |
4189 (length (nth 2 y))))) | |
4190 ((eq byte-compile-call-tree-sort 'calls+callers) | |
4191 #'(lambda (x y) (< (+ (length (nth 1 x)) | |
4192 (length (nth 2 x))) | |
4193 (+ (length (nth 1 y)) | |
4194 (length (nth 2 y)))))) | |
4195 ((eq byte-compile-call-tree-sort 'name) | |
4196 #'(lambda (x y) (string< (car x) | |
4197 (car y)))) | |
4198 (t (error | |
4199 "`byte-compile-call-tree-sort': `%s' - unknown sort mode" | |
4200 byte-compile-call-tree-sort)))))) | |
4201 (message "Generating call tree...") | |
4202 (let ((rest byte-compile-call-tree) | |
4203 (b (current-buffer)) | |
4204 f p | |
4205 callers calls) | |
4206 (while rest | |
4207 (prin1 (car (car rest)) b) | |
4208 (setq callers (nth 1 (car rest)) | |
4209 calls (nth 2 (car rest))) | |
4210 (insert "\t" | |
4211 (cond ((not (fboundp (setq f (car (car rest))))) | |
4212 (if (null f) | |
4213 " <top level>";; shouldn't insert nil then, actually -sk | |
4214 " <not defined>")) | |
4215 ((subrp (setq f (symbol-function f))) | |
4216 " <subr>") | |
4217 ((symbolp f) | |
4218 (format " ==> %s" f)) | |
4219 ((compiled-function-p f) | |
4220 "<compiled function>") | |
4221 ((not (consp f)) | |
4222 "<malformed function>") | |
4223 ((eq 'macro (car f)) | |
4224 (if (or (compiled-function-p (cdr f)) | |
4225 (assq 'byte-code (cdr (cdr (cdr f))))) | |
4226 " <compiled macro>" | |
4227 " <macro>")) | |
4228 ((assq 'byte-code (cdr (cdr f))) | |
4229 "<compiled lambda>") | |
4230 ((eq 'lambda (car f)) | |
4231 "<function>") | |
4232 (t "???")) | |
4233 (format " (%d callers + %d calls = %d)" | |
4234 ;; Does the optimizer eliminate common subexpressions?-sk | |
4235 (length callers) | |
4236 (length calls) | |
4237 (+ (length callers) (length calls))) | |
4238 "\n") | |
4239 (if callers | |
4240 (progn | |
4241 (insert " called by:\n") | |
4242 (setq p (point)) | |
4243 (insert " " (if (car callers) | |
4244 (mapconcat 'symbol-name callers ", ") | |
4245 "<top level>")) | |
4246 (let ((fill-prefix " ")) | |
4247 (fill-region-as-paragraph p (point))))) | |
4248 (if calls | |
4249 (progn | |
4250 (insert " calls:\n") | |
4251 (setq p (point)) | |
4252 (insert " " (mapconcat 'symbol-name calls ", ")) | |
4253 (let ((fill-prefix " ")) | |
4254 (fill-region-as-paragraph p (point))))) | |
4255 (insert "\n") | |
4256 (setq rest (cdr rest))) | |
4257 | |
4258 (message "Generating call tree...(finding uncalled functions...)") | |
4259 (setq rest byte-compile-call-tree) | |
4260 (let ((uncalled nil)) | |
4261 (while rest | |
4262 (or (nth 1 (car rest)) | |
4263 (null (setq f (car (car rest)))) | |
4264 (byte-compile-fdefinition f t) | |
4265 (commandp (byte-compile-fdefinition f nil)) | |
4266 (setq uncalled (cons f uncalled))) | |
4267 (setq rest (cdr rest))) | |
4268 (if uncalled | |
4269 (let ((fill-prefix " ")) | |
4270 (insert "Noninteractive functions not known to be called:\n ") | |
4271 (setq p (point)) | |
4272 (insert (mapconcat 'symbol-name (nreverse uncalled) ", ")) | |
4273 (fill-region-as-paragraph p (point))))) | |
4274 ) | |
4275 (message "Generating call tree...done.") | |
4276 )) | |
4277 | |
4278 | |
4279 ;;; by crl@newton.purdue.edu | |
4280 ;;; Only works noninteractively. | |
4281 ;;;###autoload | |
4282 (defun batch-byte-compile () | |
4283 "Run `byte-compile-file' on the files remaining on the command line. | |
4284 Use this from the command line, with `-batch'; | |
4285 it won't work in an interactive Emacs. | |
4286 Each file is processed even if an error occurred previously. | |
444 | 4287 For example, invoke \"xemacs -batch -f batch-byte-compile $emacs/ ~/*.el\"." |
428 | 4288 ;; command-line-args-left is what is left of the command line (from |
4289 ;; startup.el) | |
4290 (defvar command-line-args-left) ;Avoid 'free variable' warning | |
4291 (if (not noninteractive) | |
4292 (error "`batch-byte-compile' is to be used only with -batch")) | |
4293 (let ((error nil)) | |
4294 (while command-line-args-left | |
442 | 4295 (if (null (batch-byte-compile-one-file)) |
4296 (setq error t))) | |
428 | 4297 (message "Done") |
4298 (kill-emacs (if error 1 0)))) | |
4299 | |
442 | 4300 ;;;###autoload |
4301 (defun batch-byte-compile-one-file () | |
4302 "Run `byte-compile-file' on a single file remaining on the command line. | |
4303 Use this from the command line, with `-batch'; | |
4304 it won't work in an interactive Emacs." | |
4305 ;; command-line-args-left is what is left of the command line (from | |
4306 ;; startup.el) | |
4307 (defvar command-line-args-left) ;Avoid 'free variable' warning | |
4308 (if (not noninteractive) | |
4309 (error "`batch-byte-compile-one-file' is to be used only with -batch")) | |
4310 (let (error | |
4311 (file-to-process (car command-line-args-left))) | |
4312 (setq command-line-args-left (cdr command-line-args-left)) | |
4313 (if (file-directory-p (expand-file-name file-to-process)) | |
4314 (let ((files (directory-files file-to-process)) | |
4315 source dest) | |
4316 (while files | |
4317 (if (and (string-match emacs-lisp-file-regexp (car files)) | |
4318 (not (auto-save-file-name-p (car files))) | |
4319 (setq source (expand-file-name | |
4320 (car files) | |
4321 file-to-process)) | |
4322 (setq dest (byte-compile-dest-file source)) | |
4323 (file-exists-p dest) | |
4324 (file-newer-than-file-p source dest)) | |
4325 (if (null (batch-byte-compile-1 source)) | |
4326 (setq error t))) | |
4327 (setq files (cdr files))) | |
4328 (null error)) | |
4329 (batch-byte-compile-1 file-to-process)))) | |
4330 | |
428 | 4331 (defun batch-byte-compile-1 (file) |
4332 (condition-case err | |
4333 (progn (byte-compile-file file) t) | |
4334 (error | |
4335 (princ ">>Error occurred processing ") | |
4336 (princ file) | |
4337 (princ ": ") | |
4338 (if (fboundp 'display-error) ; XEmacs 19.8+ | |
4339 (display-error err nil) | |
4340 (princ (or (get (car err) 'error-message) (car err))) | |
4341 (mapcar #'(lambda (x) (princ " ") (prin1 x)) (cdr err))) | |
4342 (princ "\n") | |
4343 nil))) | |
4344 | |
4345 ;;;###autoload | |
4346 (defun batch-byte-recompile-directory-norecurse () | |
4347 "Same as `batch-byte-recompile-directory' but without recursion." | |
4348 (setq byte-recompile-directory-recursively nil) | |
4349 (batch-byte-recompile-directory)) | |
4350 | |
4351 ;;;###autoload | |
4352 (defun batch-byte-recompile-directory () | |
4353 "Runs `byte-recompile-directory' on the dirs remaining on the command line. | |
4354 Must be used only with `-batch', and kills Emacs on completion. | |
4355 For example, invoke `xemacs -batch -f batch-byte-recompile-directory .'." | |
4356 ;; command-line-args-left is what is left of the command line (startup.el) | |
4357 (defvar command-line-args-left) ;Avoid 'free variable' warning | |
4358 (if (not noninteractive) | |
4359 (error "batch-byte-recompile-directory is to be used only with -batch")) | |
4360 (or command-line-args-left | |
4361 (setq command-line-args-left '("."))) | |
4362 (let ((byte-recompile-directory-ignore-errors-p t)) | |
4363 (while command-line-args-left | |
4364 (byte-recompile-directory (car command-line-args-left)) | |
4365 (setq command-line-args-left (cdr command-line-args-left)))) | |
4366 (kill-emacs 0)) | |
4367 | |
4368 (make-obsolete 'elisp-compile-defun 'compile-defun) | |
4369 (make-obsolete 'byte-compile-report-call-tree 'display-call-tree) | |
4370 | |
4371 ;; other make-obsolete calls in obsolete.el. | |
4372 | |
4373 (provide 'byte-compile) | |
4374 (provide 'bytecomp) | |
4375 | |
4376 | |
4377 ;;; report metering (see the hacks in bytecode.c) | |
4378 | |
4379 (if (boundp 'byte-code-meter) | |
4380 (defun byte-compile-report-ops () | |
4381 (defvar byte-code-meter) | |
4382 (with-output-to-temp-buffer "*Meter*" | |
4383 (set-buffer "*Meter*") | |
4384 (let ((i 0) n op off) | |
4385 (while (< i 256) | |
4386 (setq n (aref (aref byte-code-meter 0) i) | |
4387 off nil) | |
4388 (if t ;(not (zerop n)) | |
4389 (progn | |
4390 (setq op i) | |
4391 (setq off nil) | |
4392 (cond ((< op byte-nth) | |
4393 (setq off (logand op 7)) | |
4394 (setq op (logand op 248))) | |
4395 ((>= op byte-constant) | |
4396 (setq off (- op byte-constant) | |
4397 op byte-constant))) | |
4398 (setq op (aref byte-code-vector op)) | |
4399 (insert (format "%-4d" i)) | |
4400 (insert (symbol-name op)) | |
4401 (if off (insert " [" (int-to-string off) "]")) | |
4402 (indent-to 40) | |
4403 (insert (int-to-string n) "\n"))) | |
4404 (setq i (1+ i))))))) | |
4405 | |
4406 | |
4407 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles | |
4408 ;; itself, compile some of its most used recursive functions (at load time). | |
4409 ;; | |
4410 (eval-when-compile | |
4411 (or (compiled-function-p (symbol-function 'byte-compile-form)) | |
4412 (assq 'byte-code (symbol-function 'byte-compile-form)) | |
4413 (let ((byte-optimize nil) ; do it fast | |
4414 (byte-compile-warnings nil)) | |
4415 (mapcar #'(lambda (x) | |
4416 (or noninteractive (message "compiling %s..." x)) | |
4417 (byte-compile x) | |
4418 (or noninteractive (message "compiling %s...done" x))) | |
4419 '(byte-compile-normal-call | |
4420 byte-compile-form | |
4421 byte-compile-body | |
4422 ;; Inserted some more than necessary, to speed it up. | |
4423 byte-compile-top-level | |
4424 byte-compile-out-toplevel | |
4425 byte-compile-constant | |
4426 byte-compile-variable-ref)))) | |
4427 nil) | |
4428 | |
4429 ;;; bytecomp.el ends here |