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