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