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