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 Free Software Foundation, Inc.
6 ;; Author: Jamie Zawinski <jwz@lucid.com>
7 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; The Emacs Lisp byte compiler. This crunches lisp source into a sort
31 ;; of p-code (`lapcode') which takes up less space and can be interpreted
32 ;; faster. [`LAP' == `Lisp Assembly Program'.]
33 ;; The user entry points are byte-compile-file and byte-recompile-directory.
37 ;; ========================================================================
39 ;; byte-recompile-directory, byte-compile-file,
40 ;; batch-byte-compile, batch-byte-recompile-directory,
41 ;; byte-compile, compile-defun,
43 ;; (byte-compile-buffer and byte-compile-and-load-file were turned off
44 ;; because they are not terribly useful and get in the way of completion.)
46 ;; This version of the byte compiler has the following improvements:
47 ;; + optimization of compiled code:
48 ;; - removal of unreachable code;
49 ;; - removal of calls to side-effectless functions whose return-value
51 ;; - compile-time evaluation of safe constant forms, such as (consp nil)
53 ;; - open-coding of literal lambdas;
54 ;; - peephole optimization of emitted code;
55 ;; - trivial functions are left uncompiled for speed.
56 ;; + support for inline functions;
57 ;; + compile-time evaluation of arbitrary expressions;
58 ;; + compile-time warning messages for:
59 ;; - functions being redefined with incompatible arglists;
60 ;; - functions being redefined as macros, or vice-versa;
61 ;; - functions or macros defined multiple times in the same file;
62 ;; - functions being called with the incorrect number of arguments;
63 ;; - functions being called which are not defined globally, in the
64 ;; file, or as autoloads;
65 ;; - assignment and reference of undeclared free variables;
66 ;; - various syntax errors;
67 ;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
68 ;; + correct compilation of top-level uses of macros;
69 ;; + the ability to generate a histogram of functions called.
71 ;; User customization variables:
73 ;; byte-compile-verbose Whether to report the function currently being
74 ;; compiled in the echo area;
75 ;; byte-optimize Whether to do optimizations; this may be
76 ;; t, nil, 'source, or 'byte;
77 ;; byte-optimize-log Whether to report (in excruciating detail)
78 ;; exactly which optimizations have been made.
79 ;; This may be t, nil, 'source, or 'byte;
80 ;; byte-compile-error-on-warn Whether to stop compilation when a warning is
82 ;; byte-compile-delete-errors Whether the optimizer may delete calls or
83 ;; variable references that are side-effect-free
84 ;; except that they may return an error.
85 ;; byte-compile-generate-call-tree Whether to generate a histogram of
86 ;; function calls. This can be useful for
87 ;; finding unused functions, as well as simple
88 ;; performance metering.
89 ;; byte-compile-warnings List of warnings to issue, or t. May contain
90 ;; `free-vars' (references to variables not in the
91 ;; current lexical scope)
92 ;; `unresolved' (calls to unknown functions)
93 ;; `callargs' (lambda calls with args that don't
94 ;; match the lambda's definition)
95 ;; `redefine' (function cell redefined from
96 ;; a macro to a lambda or vice versa,
97 ;; or redefined to take other args)
98 ;; `obsolete' (obsolete variables and functions)
99 ;; `noruntime' (calls to functions only defined
100 ;; within `eval-when-compile')
101 ;; `cl-warnings' (calls to CL functions)
102 ;; `interactive-only' (calls to commands that are
103 ;; not good to call from Lisp)
104 ;; byte-compile-compatibility Whether the compiler should
105 ;; generate .elc files which can be loaded into
107 ;; emacs-lisp-file-regexp Regexp for the extension of source-files;
108 ;; see also the function byte-compile-dest-file.
112 ;; o The form `defsubst' is just like `defun', except that the function
113 ;; generated will be open-coded in compiled code which uses it. This
114 ;; means that no function call will be generated, it will simply be
115 ;; spliced in. Lisp functions calls are very slow, so this can be a
118 ;; You can generally accomplish the same thing with `defmacro', but in
119 ;; that case, the defined procedure can't be used as an argument to
122 ;; o You can also open-code one particular call to a function without
123 ;; open-coding all calls. Use the 'inline' form to do this, like so:
125 ;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
127 ;; (inline ;; `foo' and `baz' will be
128 ;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
131 ;; o It is possible to open-code a function in the same file it is defined
132 ;; in without having to load that file before compiling it. The
133 ;; byte-compiler has been modified to remember function definitions in
134 ;; the compilation environment in the same way that it remembers macro
137 ;; o Forms like ((lambda ...) ...) are open-coded.
139 ;; o The form `eval-when-compile' is like progn, except that the body
140 ;; is evaluated at compile-time. When it appears at top-level, this
141 ;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
142 ;; When it does not appear at top-level, it is similar to the
143 ;; Common Lisp #. reader macro (but not in interpreted code).
145 ;; o The form `eval-and-compile' is similar to eval-when-compile, but
146 ;; the whole form is evalled both at compile-time and at run-time.
148 ;; o The command compile-defun is analogous to eval-defun.
150 ;; o If you run byte-compile-file on a filename which is visited in a
151 ;; buffer, and that buffer is modified, you are asked whether you want
152 ;; to save the buffer before compiling.
154 ;; o byte-compiled files now start with the string `;ELC'.
155 ;; Some versions of `file' can be customized to recognize that.
159 (or (fboundp 'defsubst)
160 ;; This really ought to be loaded already!
163 ;; The feature of compiling in a specific target Emacs version
164 ;; has been turned off because compile time options are a bad idea.
165 (defmacro byte-compile-single-version () nil)
166 (defmacro byte-compile-version-cond (cond) cond)
168 ;; The crud you see scattered through this file of the form
169 ;; (or (and (boundp 'epoch::version) epoch::version)
170 ;; (string-lessp emacs-version "19"))
171 ;; is because the Epoch folks couldn't be bothered to follow the
172 ;; normal emacs version numbering convention.
174 ;; (if (byte-compile-version-cond
175 ;; (or (and (boundp 'epoch::version) epoch::version)
176 ;; (string-lessp emacs-version "19")))
178 ;; ;; emacs-18 compatibility.
179 ;; (defvar baud-rate (baud-rate)) ;Define baud-rate if it's undefined
181 ;; (if (byte-compile-single-version)
182 ;; (defmacro byte-code-function-p (x) "Emacs 18 doesn't have these." nil)
183 ;; (defun byte-code-function-p (x) "Emacs 18 doesn't have these." nil))
185 ;; (or (and (fboundp 'member)
186 ;; ;; avoid using someone else's possibly bogus definition of this.
187 ;; (subrp (symbol-function 'member)))
188 ;; (defun member (elt list)
189 ;; "like memq, but uses equal instead of eq. In v19, this is a subr."
190 ;; (while (and list (not (equal elt (car list))))
191 ;; (setq list (cdr list)))
195 (defgroup bytecomp nil
196 "Emacs Lisp byte-compiler."
199 (defcustom emacs-lisp-file-regexp (if (eq system-type 'vax-vms)
200 "\\.EL\\(;[0-9]+\\)?$"
202 "*Regexp which matches Emacs Lisp source files.
203 You may want to redefine the function `byte-compile-dest-file'
204 if you change this variable."
208 ;; This enables file name handlers such as jka-compr
209 ;; to remove parts of the file name that should not be copied
210 ;; through to the output file name.
211 (defun byte-compiler-base-file-name (filename)
212 (let ((handler (find-file-name-handler filename
213 'byte-compiler-base-file-name)))
215 (funcall handler 'byte-compiler-base-file-name filename)
218 (or (fboundp 'byte-compile-dest-file)
219 ;; The user may want to redefine this along with emacs-lisp-file-regexp,
220 ;; so only define it if it is undefined.
221 (defun byte-compile-dest-file (filename)
222 "Convert an Emacs Lisp source file name to a compiled file name."
223 (setq filename (byte-compiler-base-file-name filename))
224 (setq filename (file-name-sans-versions filename))
225 (cond ((eq system-type 'vax-vms)
226 (concat (substring filename 0 (string-match ";" filename)) "c"))
227 ((string-match emacs-lisp-file-regexp filename)
228 (concat (substring filename 0 (match-beginning 0)) ".elc"))
229 (t (concat filename ".elc")))))
231 ;; This can be the 'byte-compile property of any symbol.
232 (autoload 'byte-compile-inline-expand "byte-opt")
234 ;; This is the entrypoint to the lapcode optimizer pass1.
235 (autoload 'byte-optimize-form "byte-opt")
236 ;; This is the entrypoint to the lapcode optimizer pass2.
237 (autoload 'byte-optimize-lapcode "byte-opt")
238 (autoload 'byte-compile-unfold-lambda "byte-opt")
240 ;; This is the entry point to the decompiler, which is used by the
241 ;; disassembler. The disassembler just requires 'byte-compile, but
242 ;; that doesn't define this function, so this seems to be a reasonable
244 (autoload 'byte-decompile-bytecode "byte-opt")
246 (defcustom byte-compile-verbose
247 (and (not noninteractive) (> baud-rate search-slow-speed))
248 "*Non-nil means print messages describing progress of byte-compiler."
252 (defcustom byte-compile-compatibility nil
253 "*Non-nil means generate output that can run in Emacs 18.
254 This only means that it can run in principle, if it doesn't require
255 facilities that have been added more recently."
259 ;; (defvar byte-compile-generate-emacs19-bytecodes
260 ;; (not (or (and (boundp 'epoch::version) epoch::version)
261 ;; (string-lessp emacs-version "19")))
262 ;; "*If this is true, then the byte-compiler will generate bytecode which
263 ;; makes use of byte-ops which are present only in Emacs 19. Code generated
264 ;; this way can never be run in Emacs 18, and may even cause it to crash.")
266 (defcustom byte-optimize t
267 "*Enable optimization in the byte compiler.
269 nil - no optimization
270 t - all optimizations
271 `source' - source-level optimizations only
272 `byte' - code-level optimizations only"
274 :type '(choice (const :tag "none" nil)
276 (const :tag "source-level" source)
277 (const :tag "byte-level" byte)))
279 (defcustom byte-compile-delete-errors nil
280 "*If non-nil, the optimizer may delete forms that may signal an error.
281 This includes variable references and calls to functions such as `car'."
285 (defvar byte-compile-dynamic nil
286 "If non-nil, compile function bodies so they load lazily.
287 They are hidden in comments in the compiled file,
288 and each one is brought into core when the
291 To enable this option, make it a file-local variable
292 in the source file you want it to apply to.
293 For example, add -*-byte-compile-dynamic: t;-*- on the first line.
295 When this option is true, if you load the compiled file and then move it,
296 the functions you loaded will not be able to run.")
297 ;;;###autoload(put 'byte-compile-dynamic 'safe-local-variable 'booleanp)
299 (defvar byte-compile-disable-print-circle nil
300 "If non-nil, disable `print-circle' on printing a byte-compiled code.")
301 ;;;###autoload(put 'byte-compile-disable-print-circle 'safe-local-variable 'booleanp)
303 (defcustom byte-compile-dynamic-docstrings t
304 "*If non-nil, compile doc strings for lazy access.
305 We bury the doc strings of functions and variables
306 inside comments in the file, and bring them into core only when they
309 When this option is true, if you load the compiled file and then move it,
310 you won't be able to find the documentation of anything in that file.
312 To disable this option for a certain file, make it a file-local variable
313 in the source file. For example, add this to the first line:
314 -*-byte-compile-dynamic-docstrings:nil;-*-
315 You can also set the variable globally.
317 This option is enabled by default because it reduces Emacs memory usage."
320 ;;;###autoload(put 'byte-compile-dynamic-docstrings 'safe-local-variable 'booleanp)
322 (defcustom byte-optimize-log nil
323 "*If true, the byte-compiler will log its optimizations into *Compile-Log*.
324 If this is 'source, then only source-level optimizations will be logged.
325 If it is 'byte, then only byte-level optimizations will be logged."
327 :type '(choice (const :tag "none" nil)
329 (const :tag "source-level" source)
330 (const :tag "byte-level" byte)))
332 (defcustom byte-compile-error-on-warn nil
333 "*If true, the byte-compiler reports warnings with `error'."
337 (defconst byte-compile-warning-types
338 '(redefine callargs free-vars unresolved
339 obsolete noruntime cl-functions interactive-only)
340 "The list of warning types used when `byte-compile-warnings' is t.")
341 (defcustom byte-compile-warnings t
342 "*List of warnings that the byte-compiler should issue (t for all).
344 Elements of the list may be:
346 free-vars references to variables not in the current lexical scope.
347 unresolved calls to unknown functions.
348 callargs function calls with args that don't match the definition.
349 redefine function name redefined from a macro to ordinary function or vice
350 versa, or redefined to take a different number of arguments.
351 obsolete obsolete variables and functions.
352 noruntime functions that may not be defined at runtime (typically
353 defined only under `eval-when-compile').
354 cl-functions calls to runtime functions from the CL package (as
355 distinguished from macros and aliases).
357 commands that normally shouldn't be called from Lisp code."
359 :type `(choice (const :tag "All" t)
360 (set :menu-tag "Some"
361 (const free-vars) (const unresolved)
362 (const callargs) (const redefine)
363 (const obsolete) (const noruntime)
364 (const cl-functions) (const interactive-only))))
365 (put 'byte-compile-warnings 'safe-local-variable 'byte-compile-warnings-safe-p)
367 (defun byte-compile-warnings-safe-p (x)
372 (when (memq e '(free-vars unresolved
375 cl-functions interactive-only))
380 (defvar byte-compile-interactive-only-functions
381 '(beginning-of-buffer end-of-buffer replace-string replace-regexp
382 insert-file insert-buffer insert-file-literally)
383 "List of commands that are not meant to be called from Lisp.")
385 (defvar byte-compile-not-obsolete-var nil
386 "If non-nil, this is a variable that shouldn't be reported as obsolete.")
388 (defcustom byte-compile-generate-call-tree nil
389 "*Non-nil means collect call-graph information when compiling.
390 This records which functions were called and from where.
391 If the value is t, compilation displays the call graph when it finishes.
392 If the value is neither t nor nil, compilation asks you whether to display
395 The call tree only lists functions called, not macros used. Those functions
396 which the byte-code interpreter knows about directly (eq, cons, etc.) are
399 The call tree also lists those functions which are not known to be called
400 \(that is, to which no calls have been compiled). Functions which can be
401 invoked interactively are excluded from this list."
403 :type '(choice (const :tag "Yes" t) (const :tag "No" nil)
404 (other :tag "Ask" lambda)))
406 (defvar byte-compile-call-tree nil "Alist of functions and their call tree.
407 Each element looks like
409 \(FUNCTION CALLERS CALLS\)
411 where CALLERS is a list of functions that call FUNCTION, and CALLS
412 is a list of functions for which calls were generated while compiling
415 (defcustom byte-compile-call-tree-sort 'name
416 "*If non-nil, sort the call tree.
417 The values `name', `callers', `calls', `calls+callers'
418 specify different fields to sort on."
420 :type '(choice (const name) (const callers) (const calls)
421 (const calls+callers) (const nil)))
423 (defvar byte-compile-debug nil)
425 ;; (defvar byte-compile-overwrite-file t
426 ;; "If nil, old .elc files are deleted before the new is saved, and .elc
427 ;; files will have the same modes as the corresponding .el file. Otherwise,
428 ;; existing .elc files will simply be overwritten, and the existing modes
429 ;; will not be changed. If this variable is nil, then an .elc file which
430 ;; is a symbolic link will be turned into a normal file, instead of the file
431 ;; which the link points to being overwritten.")
433 (defvar byte-compile-constants nil
434 "List of all constants encountered during compilation of this form.")
435 (defvar byte-compile-variables nil
436 "List of all variables encountered during compilation of this form.")
437 (defvar byte-compile-bound-variables nil
438 "List of variables bound in the context of the current form.
439 This list lives partly on the stack.")
440 (defvar byte-compile-const-variables nil
441 "List of variables declared as constants during compilation of this file.")
442 (defvar byte-compile-free-references)
443 (defvar byte-compile-free-assignments)
445 (defvar byte-compiler-error-flag)
447 (defconst byte-compile-initial-macro-environment
449 ;; (byte-compiler-options . (lambda (&rest forms)
450 ;; (apply 'byte-compiler-options-handler forms)))
451 (eval-when-compile . (lambda (&rest body)
453 (byte-compile-eval (byte-compile-top-level
454 (cons 'progn body))))))
455 (eval-and-compile . (lambda (&rest body)
456 (byte-compile-eval-before-compile (cons 'progn body))
457 (cons 'progn body))))
458 "The default macro-environment passed to macroexpand by the compiler.
459 Placing a macro here will cause a macro to have different semantics when
460 expanded by the compiler as when expanded by the interpreter.")
462 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment
463 "Alist of macros defined in the file being compiled.
464 Each element looks like (MACRONAME . DEFINITION). It is
465 \(MACRONAME . nil) when a macro is redefined as a function.")
467 (defvar byte-compile-function-environment nil
468 "Alist of functions defined in the file being compiled.
469 This is so we can inline them when necessary.
470 Each element looks like (FUNCTIONNAME . DEFINITION). It is
471 \(FUNCTIONNAME . nil) when a function is redefined as a macro.
472 It is \(FUNCTIONNAME . t) when all we know is that it was defined,
473 and we don't know the definition.")
475 (defvar byte-compile-unresolved-functions nil
476 "Alist of undefined functions to which calls have been compiled.
477 Used for warnings when the function is not known to be defined or is later
478 defined with incorrect args.")
480 (defvar byte-compile-noruntime-functions nil
481 "Alist of functions called that may not be defined when the compiled code is run.
482 Used for warnings about calling a function that is defined during compilation
483 but won't necessarily be defined when the compiled file is loaded.")
485 (defvar byte-compile-tag-number 0)
486 (defvar byte-compile-output nil
487 "Alist describing contents to put in byte code string.
488 Each element is (INDEX . VALUE)")
489 (defvar byte-compile-depth 0 "Current depth of execution stack.")
490 (defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
493 ;;; The byte codes; this information is duplicated in bytecomp.c
495 (defvar byte-code-vector nil
496 "An array containing byte-code names indexed by byte-code values.")
498 (defvar byte-stack+-info nil
499 "An array with the stack adjustment for each byte-code.")
501 (defmacro byte-defop (opcode stack-adjust opname &optional docstring)
502 ;; This is a speed-hack for building the byte-code-vector at compile-time.
503 ;; We fill in the vector at macroexpand-time, and then after the last call
504 ;; to byte-defop, we write the vector out as a constant instead of writing
505 ;; out a bunch of calls to aset.
506 ;; Actually, we don't fill in the vector itself, because that could make
507 ;; it problematic to compile big changes to this compiler; we store the
508 ;; values on its plist, and remove them later in -extrude.
509 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
510 (put 'byte-code-vector 'tmp-compile-time-value
511 (make-vector 256 nil))))
512 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
513 (put 'byte-stack+-info 'tmp-compile-time-value
514 (make-vector 256 nil)))))
515 (aset v1 opcode opname)
516 (aset v2 opcode stack-adjust))
518 (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
519 (list 'defconst opname opcode)))
521 (defmacro byte-extrude-byte-code-vectors ()
522 (prog1 (list 'setq 'byte-code-vector
523 (get 'byte-code-vector 'tmp-compile-time-value)
525 (get 'byte-stack+-info 'tmp-compile-time-value))
526 (put 'byte-code-vector 'tmp-compile-time-value nil)
527 (put 'byte-stack+-info 'tmp-compile-time-value nil)))
532 ;; These opcodes are special in that they pack their argument into the
535 (byte-defop 8 1 byte-varref "for variable reference")
536 (byte-defop 16 -1 byte-varset "for setting a variable")
537 (byte-defop 24 -1 byte-varbind "for binding a variable")
538 (byte-defop 32 0 byte-call "for calling a function")
539 (byte-defop 40 0 byte-unbind "for unbinding special bindings")
540 ;; codes 8-47 are consumed by the preceding opcodes
544 (byte-defop 56 -1 byte-nth)
545 (byte-defop 57 0 byte-symbolp)
546 (byte-defop 58 0 byte-consp)
547 (byte-defop 59 0 byte-stringp)
548 (byte-defop 60 0 byte-listp)
549 (byte-defop 61 -1 byte-eq)
550 (byte-defop 62 -1 byte-memq)
551 (byte-defop 63 0 byte-not)
552 (byte-defop 64 0 byte-car)
553 (byte-defop 65 0 byte-cdr)
554 (byte-defop 66 -1 byte-cons)
555 (byte-defop 67 0 byte-list1)
556 (byte-defop 68 -1 byte-list2)
557 (byte-defop 69 -2 byte-list3)
558 (byte-defop 70 -3 byte-list4)
559 (byte-defop 71 0 byte-length)
560 (byte-defop 72 -1 byte-aref)
561 (byte-defop 73 -2 byte-aset)
562 (byte-defop 74 0 byte-symbol-value)
563 (byte-defop 75 0 byte-symbol-function) ; this was commented out
564 (byte-defop 76 -1 byte-set)
565 (byte-defop 77 -1 byte-fset) ; this was commented out
566 (byte-defop 78 -1 byte-get)
567 (byte-defop 79 -2 byte-substring)
568 (byte-defop 80 -1 byte-concat2)
569 (byte-defop 81 -2 byte-concat3)
570 (byte-defop 82 -3 byte-concat4)
571 (byte-defop 83 0 byte-sub1)
572 (byte-defop 84 0 byte-add1)
573 (byte-defop 85 -1 byte-eqlsign)
574 (byte-defop 86 -1 byte-gtr)
575 (byte-defop 87 -1 byte-lss)
576 (byte-defop 88 -1 byte-leq)
577 (byte-defop 89 -1 byte-geq)
578 (byte-defop 90 -1 byte-diff)
579 (byte-defop 91 0 byte-negate)
580 (byte-defop 92 -1 byte-plus)
581 (byte-defop 93 -1 byte-max)
582 (byte-defop 94 -1 byte-min)
583 (byte-defop 95 -1 byte-mult) ; v19 only
584 (byte-defop 96 1 byte-point)
585 (byte-defop 98 0 byte-goto-char)
586 (byte-defop 99 0 byte-insert)
587 (byte-defop 100 1 byte-point-max)
588 (byte-defop 101 1 byte-point-min)
589 (byte-defop 102 0 byte-char-after)
590 (byte-defop 103 1 byte-following-char)
591 (byte-defop 104 1 byte-preceding-char)
592 (byte-defop 105 1 byte-current-column)
593 (byte-defop 106 0 byte-indent-to)
594 (byte-defop 107 0 byte-scan-buffer-OBSOLETE) ; no longer generated as of v18
595 (byte-defop 108 1 byte-eolp)
596 (byte-defop 109 1 byte-eobp)
597 (byte-defop 110 1 byte-bolp)
598 (byte-defop 111 1 byte-bobp)
599 (byte-defop 112 1 byte-current-buffer)
600 (byte-defop 113 0 byte-set-buffer)
601 (byte-defop 114 0 byte-save-current-buffer
602 "To make a binding to record the current buffer")
603 (byte-defop 115 0 byte-set-mark-OBSOLETE)
604 (byte-defop 116 1 byte-interactive-p)
606 ;; These ops are new to v19
607 (byte-defop 117 0 byte-forward-char)
608 (byte-defop 118 0 byte-forward-word)
609 (byte-defop 119 -1 byte-skip-chars-forward)
610 (byte-defop 120 -1 byte-skip-chars-backward)
611 (byte-defop 121 0 byte-forward-line)
612 (byte-defop 122 0 byte-char-syntax)
613 (byte-defop 123 -1 byte-buffer-substring)
614 (byte-defop 124 -1 byte-delete-region)
615 (byte-defop 125 -1 byte-narrow-to-region)
616 (byte-defop 126 1 byte-widen)
617 (byte-defop 127 0 byte-end-of-line)
621 ;; These store their argument in the next two bytes
622 (byte-defop 129 1 byte-constant2
623 "for reference to a constant with vector index >= byte-constant-limit")
624 (byte-defop 130 0 byte-goto "for unconditional jump")
625 (byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
626 (byte-defop 132 -1 byte-goto-if-not-nil "to pop value and jump if it's not nil")
627 (byte-defop 133 -1 byte-goto-if-nil-else-pop
628 "to examine top-of-stack, jump and don't pop it if it's nil,
630 (byte-defop 134 -1 byte-goto-if-not-nil-else-pop
631 "to examine top-of-stack, jump and don't pop it if it's non nil,
634 (byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
635 (byte-defop 136 -1 byte-discard "to discard one value from stack")
636 (byte-defop 137 1 byte-dup "to duplicate the top of the stack")
638 (byte-defop 138 0 byte-save-excursion
639 "to make a binding to record the buffer, point and mark")
640 (byte-defop 139 0 byte-save-window-excursion
641 "to make a binding to record entire window configuration")
642 (byte-defop 140 0 byte-save-restriction
643 "to make a binding to record the current buffer clipping restrictions")
644 (byte-defop 141 -1 byte-catch
645 "for catch. Takes, on stack, the tag and an expression for the body")
646 (byte-defop 142 -1 byte-unwind-protect
647 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
649 ;; For condition-case. Takes, on stack, the variable to bind,
650 ;; an expression for the body, and a list of clauses.
651 (byte-defop 143 -2 byte-condition-case)
653 ;; For entry to with-output-to-temp-buffer.
654 ;; Takes, on stack, the buffer name.
655 ;; Binds standard-output and does some other things.
656 ;; Returns with temp buffer on the stack in place of buffer name.
657 (byte-defop 144 0 byte-temp-output-buffer-setup)
659 ;; For exit from with-output-to-temp-buffer.
660 ;; Expects the temp buffer on the stack underneath value to return.
661 ;; Pops them both, then pushes the value back on.
662 ;; Unbinds standard-output and makes the temp buffer visible.
663 (byte-defop 145 -1 byte-temp-output-buffer-show)
665 ;; these ops are new to v19
667 ;; To unbind back to the beginning of this frame.
668 ;; Not used yet, but will be needed for tail-recursion elimination.
669 (byte-defop 146 0 byte-unbind-all)
671 ;; these ops are new to v19
672 (byte-defop 147 -2 byte-set-marker)
673 (byte-defop 148 0 byte-match-beginning)
674 (byte-defop 149 0 byte-match-end)
675 (byte-defop 150 0 byte-upcase)
676 (byte-defop 151 0 byte-downcase)
677 (byte-defop 152 -1 byte-string=)
678 (byte-defop 153 -1 byte-string<)
679 (byte-defop 154 -1 byte-equal)
680 (byte-defop 155 -1 byte-nthcdr)
681 (byte-defop 156 -1 byte-elt)
682 (byte-defop 157 -1 byte-member)
683 (byte-defop 158 -1 byte-assq)
684 (byte-defop 159 0 byte-nreverse)
685 (byte-defop 160 -1 byte-setcar)
686 (byte-defop 161 -1 byte-setcdr)
687 (byte-defop 162 0 byte-car-safe)
688 (byte-defop 163 0 byte-cdr-safe)
689 (byte-defop 164 -1 byte-nconc)
690 (byte-defop 165 -1 byte-quo)
691 (byte-defop 166 -1 byte-rem)
692 (byte-defop 167 0 byte-numberp)
693 (byte-defop 168 0 byte-integerp)
696 (byte-defop 175 nil byte-listN)
697 (byte-defop 176 nil byte-concatN)
698 (byte-defop 177 nil byte-insertN)
702 (byte-defop 192 1 byte-constant "for reference to a constant")
703 ;; codes 193-255 are consumed by byte-constant.
704 (defconst byte-constant-limit 64
705 "Exclusive maximum index usable in the `byte-constant' opcode.")
707 (defconst byte-goto-ops '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
708 byte-goto-if-nil-else-pop
709 byte-goto-if-not-nil-else-pop)
710 "List of byte-codes whose offset is a pc.")
712 (defconst byte-goto-always-pop-ops '(byte-goto-if-nil byte-goto-if-not-nil))
714 (byte-extrude-byte-code-vectors)
716 ;;; lapcode generator
718 ;; the byte-compiler now does source -> lapcode -> bytecode instead of
719 ;; source -> bytecode, because it's a lot easier to make optimizations
720 ;; on lapcode than on bytecode.
722 ;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
723 ;; where instruction is a symbol naming a byte-code instruction,
724 ;; and parameter is an argument to that instruction, if any.
726 ;; The instruction can be the pseudo-op TAG, which means that this position
727 ;; in the instruction stream is a target of a goto. (car PARAMETER) will be
728 ;; the PC for this location, and the whole instruction "(TAG pc)" will be the
729 ;; parameter for some goto op.
731 ;; If the operation is varbind, varref, varset or push-constant, then the
732 ;; parameter is (variable/constant . index_in_constant_vector).
734 ;; First, the source code is macroexpanded and optimized in various ways.
735 ;; Then the resultant code is compiled into lapcode. Another set of
736 ;; optimizations are then run over the lapcode. Then the variables and
737 ;; constants referenced by the lapcode are collected and placed in the
738 ;; constants-vector. (This happens now so that variables referenced by dead
739 ;; code don't consume space.) And finally, the lapcode is transformed into
740 ;; compacted byte-code.
742 ;; A distinction is made between variables and constants because the variable-
743 ;; referencing instructions are more sensitive to the variables being near the
744 ;; front of the constants-vector than the constant-referencing instructions.
745 ;; Also, this lets us notice references to free variables.
747 (defun byte-compile-lapcode (lap)
748 "Turns lapcode into bytecode. The lapcode is destroyed."
749 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
750 (let ((pc 0) ; Program counter
751 op off ; Operation & offset
752 (bytes '()) ; Put the output bytes here
753 (patchlist nil)) ; List of tags and goto's to patch
755 (setq op (car (car lap))
757 (cond ((not (symbolp op))
758 (error "Non-symbolic opcode `%s'" op))
761 (setq patchlist (cons off patchlist)))
762 ((memq op byte-goto-ops)
764 (setq bytes (cons (cons pc (cdr off))
766 (cons (symbol-value op) bytes))))
767 (setq patchlist (cons bytes patchlist)))
770 (cond ((cond ((consp off)
771 ;; Variable or constant reference
773 (eq op 'byte-constant)))
774 (cond ((< off byte-constant-limit)
776 (cons (+ byte-constant off) bytes))
780 (cons (logand off 255)
781 (cons byte-constant2 bytes))))))
782 ((<= byte-listN (symbol-value op))
784 (cons off (cons (symbol-value op) bytes)))
787 (cons (+ (symbol-value op) off) bytes))
790 (cons off (cons (+ (symbol-value op) 6) bytes)))
794 (cons (logand off 255)
795 (cons (+ (symbol-value op) 7)
797 (setq lap (cdr lap)))
798 ;;(if (not (= pc (length bytes)))
799 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
800 ;; Patch PC into jumps
803 (setq bytes (car patchlist))
804 (cond ((atom (car bytes))) ; Tag
806 (setq pc (car (cdr (car bytes)))) ; Pick PC from tag
807 (setcar (cdr bytes) (logand pc 255))
808 (setcar bytes (lsh pc -8))))
809 (setq patchlist (cdr patchlist))))
810 (concat (nreverse bytes))))
813 ;;; compile-time evaluation
815 (defun byte-compile-eval (form)
816 "Eval FORM and mark the functions defined therein.
817 Each function's symbol gets added to `byte-compile-noruntime-functions'."
818 (let ((hist-orig load-history)
819 (hist-nil-orig current-load-list))
821 (when (memq 'noruntime byte-compile-warnings)
822 (let ((hist-new load-history)
823 (hist-nil-new current-load-list))
824 ;; Go through load-history, look for newly loaded files
825 ;; and mark all the functions defined therein.
826 (while (and hist-new (not (eq hist-new hist-orig)))
827 (let ((xs (pop hist-new))
829 ;; Make sure the file was not already loaded before.
830 (unless (or (assoc (car xs) hist-orig)
831 (equal (car xs) "cl"))
835 (unless (memq s old-autoloads)
836 (push s byte-compile-noruntime-functions)))
837 ((and (consp s) (eq t (car s)))
838 (push (cdr s) old-autoloads))
839 ((and (consp s) (eq 'autoload (car s)))
840 (push (cdr s) byte-compile-noruntime-functions)))))))
841 ;; Go through current-load-list for the locally defined funs.
843 (while (and hist-nil-new (not (eq hist-nil-new hist-nil-orig)))
844 (let ((s (pop hist-nil-new)))
845 (when (and (symbolp s) (not (memq s old-autoloads)))
846 (push s byte-compile-noruntime-functions))
847 (when (and (consp s) (eq t (car s)))
848 (push (cdr s) old-autoloads)))))))
849 (when (memq 'cl-functions byte-compile-warnings)
850 (let ((hist-new load-history)
851 (hist-nil-new current-load-list))
852 ;; Go through load-history, look for newly loaded files
853 ;; and mark all the functions defined therein.
854 (while (and hist-new (not (eq hist-new hist-orig)))
855 (let ((xs (pop hist-new))
857 ;; Make sure the file was not already loaded before.
858 (when (and (equal (car xs) "cl") (not (assoc (car xs) hist-orig)))
859 (byte-compile-find-cl-functions)))))))))
861 (defun byte-compile-eval-before-compile (form)
862 "Evaluate FORM for `eval-and-compile'."
863 (let ((hist-nil-orig current-load-list))
865 ;; (eval-and-compile (require 'cl) turns off warnings for cl functions.
866 (let ((tem current-load-list))
867 (while (not (eq tem hist-nil-orig))
868 (when (equal (car tem) '(require . cl))
869 (setq byte-compile-warnings
870 (remq 'cl-functions byte-compile-warnings)))
871 (setq tem (cdr tem)))))))
873 ;;; byte compiler messages
875 (defvar byte-compile-current-form nil)
876 (defvar byte-compile-dest-file nil)
877 (defvar byte-compile-current-file nil)
878 (defvar byte-compile-current-buffer nil)
880 ;; Log something that isn't a warning.
881 (defmacro byte-compile-log (format-string &rest args)
884 (memq byte-optimize-log '(t source))
885 (let ((print-escape-newlines t)
892 (lambda (x) (if (symbolp x) (list 'prin1-to-string x) x))
895 ;; Log something that isn't a warning.
896 (defun byte-compile-log-1 (string)
897 (with-current-buffer "*Compile-Log*"
898 (let ((inhibit-read-only t))
899 (goto-char (point-max))
900 (byte-compile-warning-prefix nil nil)
901 (cond (noninteractive
902 (message " %s" string))
904 (insert (format "%s\n" string)))))))
906 (defvar byte-compile-read-position nil
907 "Character position we began the last `read' from.")
908 (defvar byte-compile-last-position nil
909 "Last known character position in the input.")
911 ;; copied from gnus-util.el
912 (defsubst byte-compile-delete-first (elt list)
913 (if (eq (car list) elt)
916 (while (and (cdr list)
917 (not (eq (cadr list) elt)))
918 (setq list (cdr list)))
920 (setcdr list (cddr list)))
923 ;; The purpose of this function is to iterate through the
924 ;; `read-symbol-positions-list'. Each time we process, say, a
925 ;; function definition (`defun') we remove `defun' from
926 ;; `read-symbol-positions-list', and set `byte-compile-last-position'
927 ;; to that symbol's character position. Similarly, if we encounter a
928 ;; variable reference, like in (1+ foo), we remove `foo' from the
929 ;; list. If our current position is after the symbol's position, we
930 ;; assume we've already passed that point, and look for the next
931 ;; occurrence of the symbol.
933 ;; This function should not be called twice for the same occurrence of
934 ;; a symbol, and it should not be called for symbols generated by the
935 ;; byte compiler itself; because rather than just fail looking up the
936 ;; symbol, we may find an occurrence of the symbol further ahead, and
937 ;; then `byte-compile-last-position' as advanced too far.
939 ;; So your're probably asking yourself: Isn't this function a
940 ;; gross hack? And the answer, of course, would be yes.
941 (defun byte-compile-set-symbol-position (sym &optional allow-previous)
942 (when byte-compile-read-position
945 (setq last byte-compile-last-position
946 entry (assq sym read-symbol-positions-list))
948 (setq byte-compile-last-position
949 (+ byte-compile-read-position (cdr entry))
950 read-symbol-positions-list
951 (byte-compile-delete-first
952 entry read-symbol-positions-list)))
953 (or (and allow-previous (not (= last byte-compile-last-position)))
954 (> last byte-compile-last-position)))))))
956 (defvar byte-compile-last-warned-form nil)
957 (defvar byte-compile-last-logged-file nil)
959 ;; This is used as warning-prefix for the compiler.
960 ;; It is always called with the warnings buffer current.
961 (defun byte-compile-warning-prefix (level entry)
962 (let* ((inhibit-read-only t)
963 (dir default-directory)
964 (file (cond ((stringp byte-compile-current-file)
965 (format "%s:" (file-relative-name byte-compile-current-file dir)))
966 ((bufferp byte-compile-current-file)
968 (buffer-name byte-compile-current-file)))
970 (pos (if (and byte-compile-current-file
971 (integerp byte-compile-read-position))
972 (with-current-buffer byte-compile-current-buffer
973 (format "%d:%d:" (count-lines (point-min)
974 byte-compile-last-position)
976 (goto-char byte-compile-last-position)
977 (1+ (current-column)))))
979 (form (if (eq byte-compile-current-form :end) "end of data"
980 (or byte-compile-current-form "toplevel form"))))
981 (when (or (and byte-compile-current-file
982 (not (equal byte-compile-current-file
983 byte-compile-last-logged-file)))
984 (and byte-compile-current-form
985 (not (eq byte-compile-current-form
986 byte-compile-last-warned-form))))
987 (insert (format "\nIn %s:\n" form)))
989 (insert (format "%s%s" file pos))))
990 (setq byte-compile-last-logged-file byte-compile-current-file
991 byte-compile-last-warned-form byte-compile-current-form)
994 ;; This no-op function is used as the value of warning-series
995 ;; to tell inner calls to displaying-byte-compile-warnings
996 ;; not to bind warning-series.
997 (defun byte-compile-warning-series (&rest ignore)
1000 ;; Log the start of a file in *Compile-Log*, and mark it as done.
1001 ;; Return the position of the start of the page in the log buffer.
1002 ;; But do nothing in batch mode.
1003 (defun byte-compile-log-file ()
1004 (and (not (equal byte-compile-current-file byte-compile-last-logged-file))
1005 (not noninteractive)
1007 (set-buffer (get-buffer-create "*Compile-Log*"))
1008 (goto-char (point-max))
1009 (let* ((inhibit-read-only t)
1010 (dir (and byte-compile-current-file
1011 (file-name-directory byte-compile-current-file)))
1012 (was-same (equal default-directory dir))
1016 (insert (format "Leaving directory `%s'\n" default-directory))))
1019 (setq pt (point-marker))
1020 (if byte-compile-current-file
1021 (insert "\f\nCompiling "
1022 (if (stringp byte-compile-current-file)
1023 (concat "file " byte-compile-current-file)
1024 (concat "buffer " (buffer-name byte-compile-current-file)))
1025 " at " (current-time-string) "\n")
1026 (insert "\f\nCompiling no file at " (current-time-string) "\n"))
1028 (setq default-directory dir)
1030 (insert (format "Entering directory `%s'\n" default-directory))))
1031 (setq byte-compile-last-logged-file byte-compile-current-file
1032 byte-compile-last-warned-form nil)
1033 ;; Do this after setting default-directory.
1034 (unless (eq major-mode 'compilation-mode)
1036 (compilation-forget-errors)
1039 ;; Log a message STRING in *Compile-Log*.
1040 ;; Also log the current function and file if not already done.
1041 (defun byte-compile-log-warning (string &optional fill level)
1042 (let ((warning-prefix-function 'byte-compile-warning-prefix)
1043 (warning-type-format "")
1044 (warning-fill-prefix (if fill " "))
1045 (inhibit-read-only t))
1046 (display-warning 'bytecomp string level "*Compile-Log*")))
1048 (defun byte-compile-warn (format &rest args)
1049 "Issue a byte compiler warning; use (format FORMAT ARGS...) for message."
1050 (setq format (apply 'format format args))
1051 (if byte-compile-error-on-warn
1052 (error "%s" format) ; byte-compile-file catches and logs it
1053 (byte-compile-log-warning format t :warning)))
1055 (defun byte-compile-report-error (error-info)
1056 "Report Lisp error in compilation. ERROR-INFO is the error data."
1057 (setq byte-compiler-error-flag t)
1058 (byte-compile-log-warning
1059 (error-message-string error-info)
1062 ;;; Used by make-obsolete.
1063 (defun byte-compile-obsolete (form)
1064 (let* ((new (get (car form) 'byte-obsolete-info))
1065 (handler (nth 1 new))
1067 (byte-compile-set-symbol-position (car form))
1068 (if (memq 'obsolete byte-compile-warnings)
1069 (byte-compile-warn "`%s' is an obsolete function%s; %s" (car form)
1070 (if when (concat " (as of Emacs " when ")") "")
1071 (if (stringp (car new))
1073 (format "use `%s' instead." (car new)))))
1074 (funcall (or handler 'byte-compile-normal-call) form)))
1078 ;; (defvar byte-compiler-valid-options
1079 ;; '((optimize byte-optimize (t nil source byte) val)
1080 ;; (file-format byte-compile-compatibility (emacs18 emacs19)
1081 ;; (eq val 'emacs18))
1082 ;; ;; (new-bytecodes byte-compile-generate-emacs19-bytecodes (t nil) val)
1083 ;; (delete-errors byte-compile-delete-errors (t nil) val)
1084 ;; (verbose byte-compile-verbose (t nil) val)
1085 ;; (warnings byte-compile-warnings ((callargs redefine free-vars unresolved))
1088 ;; Inhibit v18/v19 selectors if the version is hardcoded.
1089 ;; #### This should print a warning if the user tries to change something
1090 ;; than can't be changed because the running compiler doesn't support it.
1092 ;; ((byte-compile-single-version)
1093 ;; (setcar (cdr (cdr (assq 'new-bytecodes byte-compiler-valid-options)))
1094 ;; (list (byte-compile-version-cond
1095 ;; byte-compile-generate-emacs19-bytecodes)))
1096 ;; (setcar (cdr (cdr (assq 'file-format byte-compiler-valid-options)))
1097 ;; (if (byte-compile-version-cond byte-compile-compatibility)
1098 ;; '(emacs18) '(emacs19)))))
1100 ;; (defun byte-compiler-options-handler (&rest args)
1101 ;; (let (key val desc choices)
1103 ;; (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args))))
1104 ;; (error "Malformed byte-compiler option `%s'" (car args)))
1105 ;; (setq key (car (car args))
1106 ;; val (car (cdr (car args)))
1107 ;; desc (assq key byte-compiler-valid-options))
1109 ;; (error "Unknown byte-compiler option `%s'" key))
1110 ;; (setq choices (nth 2 desc))
1111 ;; (if (consp (car choices))
1114 ;; (ret (and (memq (car val) '(+ -))
1115 ;; (copy-sequence (if (eq t (symbol-value (nth 1 desc)))
1117 ;; (symbol-value (nth 1 desc)))))))
1118 ;; (setq choices (car choices))
1120 ;; (setq this (car val))
1121 ;; (cond ((memq this choices)
1122 ;; (setq ret (funcall handler this ret)))
1123 ;; ((eq this '+) (setq handler 'cons))
1124 ;; ((eq this '-) (setq handler 'delq))
1125 ;; ((error "`%s' only accepts %s" key choices)))
1126 ;; (setq val (cdr val)))
1127 ;; (set (nth 1 desc) ret))
1128 ;; (or (memq val choices)
1129 ;; (error "`%s' must be one of `%s'" key choices))
1130 ;; (set (nth 1 desc) (eval (nth 3 desc))))
1131 ;; (setq args (cdr args)))
1134 ;;; sanity-checking arglists
1136 ;; If a function has an entry saying (FUNCTION . t).
1137 ;; that means we know it is defined but we don't know how.
1138 ;; If a function has an entry saying (FUNCTION . nil),
1139 ;; that means treat it as not defined.
1140 (defun byte-compile-fdefinition (name macro-p)
1141 (let* ((list (if macro-p
1142 byte-compile-macro-environment
1143 byte-compile-function-environment))
1144 (env (cdr (assq name list))))
1147 (while (and (symbolp fn)
1149 (or (symbolp (symbol-function fn))
1150 (consp (symbol-function fn))
1152 (byte-code-function-p (symbol-function fn)))))
1153 (setq fn (symbol-function fn)))
1154 (if (and (not macro-p) (byte-code-function-p fn))
1157 (if (eq 'macro (car fn))
1161 (if (eq 'autoload (car fn))
1165 (defun byte-compile-arglist-signature (arglist)
1170 (cond ((eq (car arglist) '&optional)
1171 (or opts (setq opts 0)))
1172 ((eq (car arglist) '&rest)
1178 (setq opts (1+ opts))
1179 (setq args (1+ args)))))
1180 (setq arglist (cdr arglist)))
1181 (cons args (if restp nil (if opts (+ args opts) args)))))
1184 (defun byte-compile-arglist-signatures-congruent-p (old new)
1186 (> (car new) (car old)) ; requires more args now
1187 (and (null (cdr old)) ; took rest-args, doesn't any more
1189 (and (cdr new) (cdr old) ; can't take as many args now
1190 (< (cdr new) (cdr old)))
1193 (defun byte-compile-arglist-signature-string (signature)
1194 (cond ((null (cdr signature))
1195 (format "%d+" (car signature)))
1196 ((= (car signature) (cdr signature))
1197 (format "%d" (car signature)))
1198 (t (format "%d-%d" (car signature) (cdr signature)))))
1201 ;; Warn if the form is calling a function with the wrong number of arguments.
1202 (defun byte-compile-callargs-warn (form)
1203 (let* ((def (or (byte-compile-fdefinition (car form) nil)
1204 (byte-compile-fdefinition (car form) t)))
1205 (sig (if (and def (not (eq def t)))
1206 (byte-compile-arglist-signature
1207 (if (eq 'lambda (car-safe def))
1209 (if (byte-code-function-p def)
1212 (if (and (fboundp (car form))
1213 (subrp (symbol-function (car form))))
1214 (subr-arity (symbol-function (car form))))))
1215 (ncall (length (cdr form))))
1216 ;; Check many or unevalled from subr-arity.
1217 (if (and (cdr-safe sig)
1218 (not (numberp (cdr sig))))
1221 (when (or (< ncall (car sig))
1222 (and (cdr sig) (> ncall (cdr sig))))
1223 (byte-compile-set-symbol-position (car form))
1225 "%s called with %d argument%s, but %s %s"
1227 (if (= 1 ncall) "" "s")
1228 (if (< ncall (car sig))
1231 (byte-compile-arglist-signature-string sig))))
1232 (byte-compile-format-warn form)
1233 ;; Check to see if the function will be available at runtime
1234 ;; and/or remember its arity if it's unknown.
1235 (or (and (or def (fboundp (car form))) ; might be a subr or autoload.
1236 (not (memq (car form) byte-compile-noruntime-functions)))
1237 (eq (car form) byte-compile-current-form) ; ## this doesn't work
1239 ;; It's a currently-undefined function.
1240 ;; Remember number of args in call.
1241 (let ((cons (assq (car form) byte-compile-unresolved-functions))
1242 (n (length (cdr form))))
1244 (or (memq n (cdr cons))
1245 (setcdr cons (cons n (cdr cons))))
1246 (push (list (car form) n)
1247 byte-compile-unresolved-functions))))))
1249 (defun byte-compile-format-warn (form)
1250 "Warn if FORM is `format'-like with inconsistent args.
1251 Applies if head of FORM is a symbol with non-nil property
1252 `byte-compile-format-like' and first arg is a constant string.
1253 Then check the number of format fields matches the number of
1255 (when (and (symbolp (car form))
1256 (stringp (nth 1 form))
1257 (get (car form) 'byte-compile-format-like))
1258 (let ((nfields (with-temp-buffer
1259 (insert (nth 1 form))
1262 (while (re-search-forward "%." nil t)
1263 (unless (eq ?% (char-after (1+ (match-beginning 0))))
1266 (nargs (- (length form) 2)))
1267 (unless (= nargs nfields)
1269 "`%s' called with %d args to fill %d format field(s)" (car form)
1272 (dolist (elt '(format message error))
1273 (put elt 'byte-compile-format-like t))
1275 ;; Warn if a custom definition fails to specify :group.
1276 (defun byte-compile-nogroup-warn (form)
1277 (let ((keyword-args (cdr (cdr (cdr (cdr form)))))
1279 (or (not (eq (car-safe name) 'quote))
1280 (and (eq (car form) 'custom-declare-group)
1281 (equal name ''emacs))
1282 (plist-get keyword-args :group)
1283 (not (and (consp name) (eq (car name) 'quote)))
1285 "%s for `%s' fails to specify containing group"
1286 (cdr (assq (car form)
1287 '((custom-declare-group . defgroup)
1288 (custom-declare-face . defface)
1289 (custom-declare-variable . defcustom))))
1292 ;; Warn if the function or macro is being redefined with a different
1293 ;; number of arguments.
1294 (defun byte-compile-arglist-warn (form macrop)
1295 (let ((old (byte-compile-fdefinition (nth 1 form) macrop)))
1296 (if (and old (not (eq old t)))
1297 (let ((sig1 (byte-compile-arglist-signature
1298 (if (eq 'lambda (car-safe old))
1300 (if (byte-code-function-p old)
1303 (sig2 (byte-compile-arglist-signature (nth 2 form))))
1304 (unless (byte-compile-arglist-signatures-congruent-p sig1 sig2)
1305 (byte-compile-set-symbol-position (nth 1 form))
1307 "%s %s used to take %s %s, now takes %s"
1308 (if (eq (car form) 'defun) "function" "macro")
1310 (byte-compile-arglist-signature-string sig1)
1311 (if (equal sig1 '(1 . 1)) "argument" "arguments")
1312 (byte-compile-arglist-signature-string sig2))))
1313 ;; This is the first definition. See if previous calls are compatible.
1314 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions))
1318 (setq sig (byte-compile-arglist-signature (nth 2 form))
1319 nums (sort (copy-sequence (cdr calls)) (function <))
1321 max (car (nreverse nums)))
1322 (when (or (< min (car sig))
1323 (and (cdr sig) (> max (cdr sig))))
1324 (byte-compile-set-symbol-position (nth 1 form))
1326 "%s being defined to take %s%s, but was previously called with %s"
1328 (byte-compile-arglist-signature-string sig)
1329 (if (equal sig '(1 . 1)) " arg" " args")
1330 (byte-compile-arglist-signature-string (cons min max))))
1332 (setq byte-compile-unresolved-functions
1333 (delq calls byte-compile-unresolved-functions)))))
1336 (defvar byte-compile-cl-functions nil
1337 "List of functions defined in CL.")
1339 (defun byte-compile-find-cl-functions ()
1340 (unless byte-compile-cl-functions
1341 (dolist (elt load-history)
1342 (when (and (stringp (car elt))
1343 (string-match "^cl\\>" (car elt)))
1344 (setq byte-compile-cl-functions
1345 (append byte-compile-cl-functions
1347 (let ((tail byte-compile-cl-functions))
1349 (if (and (consp (car tail))
1350 (eq (car (car tail)) 'autoload))
1351 (setcar tail (cdr (car tail))))
1352 (setq tail (cdr tail))))))
1354 (defun byte-compile-cl-warn (form)
1355 "Warn if FORM is a call of a function from the CL package."
1356 (let ((func (car-safe form)))
1357 (if (and byte-compile-cl-functions
1358 (memq func byte-compile-cl-functions)
1359 ;; Aliases which won't have been expanded at this point.
1360 ;; These aren't all aliases of subrs, so not trivial to
1361 ;; avoid hardwiring the list.
1363 '(cl-block-wrapper cl-block-throw
1364 multiple-value-call nth-value
1365 copy-seq first second rest endp cl-member
1366 ;; These are included in generated code
1367 ;; that can't be called except at compile time
1368 ;; or unless cl is loaded anyway.
1369 cl-defsubst-expand cl-struct-setf-expander
1370 ;; These would sometimes be warned about
1371 ;; but such warnings are never useful,
1372 ;; so don't warn about them.
1373 macroexpand cl-macroexpand-all
1374 cl-compiling-file)))
1375 ;; Avoid warnings for things which are safe because they
1376 ;; have suitable compiler macros, but those aren't
1377 ;; expanded at this stage. There should probably be more
1378 ;; here than caaar and friends.
1379 (not (and (eq (get func 'byte-compile)
1380 'cl-byte-compile-compiler-macro)
1381 (string-match "\\`c[ad]+r\\'" (symbol-name func)))))
1382 (byte-compile-warn "Function `%s' from cl package called at runtime"
1386 (defun byte-compile-print-syms (str1 strn syms)
1388 (byte-compile-set-symbol-position (car syms) t))
1389 (cond ((and (cdr syms) (not noninteractive))
1394 (setq s (symbol-name (pop syms))
1395 L (+ L (length s) 2))
1396 (if (< L (1- fill-column))
1397 (setq str (concat str " " s (and syms ",")))
1398 (setq str (concat str "\n " s (and syms ","))
1399 L (+ (length s) 4))))
1400 (byte-compile-warn "%s" str)))
1402 (byte-compile-warn "%s %s"
1404 (mapconcat #'symbol-name syms ", ")))
1407 (byte-compile-warn str1 (car syms)))))
1409 ;; If we have compiled any calls to functions which are not known to be
1410 ;; defined, issue a warning enumerating them.
1411 ;; `unresolved' in the list `byte-compile-warnings' disables this.
1412 (defun byte-compile-warn-about-unresolved-functions ()
1413 (when (memq 'unresolved byte-compile-warnings)
1414 (let ((byte-compile-current-form :end)
1417 ;; Separate the functions that will not be available at runtime
1418 ;; from the truly unresolved ones.
1419 (dolist (f byte-compile-unresolved-functions)
1421 (if (fboundp f) (push f noruntime) (push f unresolved)))
1422 ;; Complain about the no-run-time functions
1423 (byte-compile-print-syms
1424 "the function `%s' might not be defined at runtime."
1425 "the following functions might not be defined at runtime:"
1427 ;; Complain about the unresolved functions
1428 (byte-compile-print-syms
1429 "the function `%s' is not known to be defined."
1430 "the following functions are not known to be defined:"
1435 (defsubst byte-compile-const-symbol-p (symbol &optional any-value)
1436 "Non-nil if SYMBOL is constant.
1437 If ANY-VALUE is nil, only return non-nil if the value of the symbol is the
1439 (or (memq symbol '(nil t))
1441 (if any-value (memq symbol byte-compile-const-variables))))
1443 (defmacro byte-compile-constp (form)
1444 "Return non-nil if FORM is a constant."
1445 `(cond ((consp ,form) (eq (car ,form) 'quote))
1446 ((not (symbolp ,form)))
1447 ((byte-compile-const-symbol-p ,form))))
1449 (defmacro byte-compile-close-variables (&rest body)
1452 ;; Close over these variables to encapsulate the
1453 ;; compilation state
1455 (byte-compile-macro-environment
1456 ;; Copy it because the compiler may patch into the
1457 ;; macroenvironment.
1458 (copy-alist byte-compile-initial-macro-environment))
1459 (byte-compile-function-environment nil)
1460 (byte-compile-bound-variables nil)
1461 (byte-compile-const-variables nil)
1462 (byte-compile-free-references nil)
1463 (byte-compile-free-assignments nil)
1465 ;; Close over these variables so that `byte-compiler-options'
1466 ;; can change them on a per-file basis.
1468 (byte-compile-verbose byte-compile-verbose)
1469 (byte-optimize byte-optimize)
1470 (byte-compile-compatibility byte-compile-compatibility)
1471 (byte-compile-dynamic byte-compile-dynamic)
1472 (byte-compile-dynamic-docstrings
1473 byte-compile-dynamic-docstrings)
1474 ;; (byte-compile-generate-emacs19-bytecodes
1475 ;; byte-compile-generate-emacs19-bytecodes)
1476 (byte-compile-warnings (if (eq byte-compile-warnings t)
1477 byte-compile-warning-types
1478 byte-compile-warnings))
1482 (defmacro displaying-byte-compile-warnings (&rest body)
1483 `(let* ((--displaying-byte-compile-warnings-fn (lambda () ,@body))
1484 (warning-series-started
1485 (and (markerp warning-series)
1486 (eq (marker-buffer warning-series)
1487 (get-buffer "*Compile-Log*")))))
1488 (byte-compile-find-cl-functions)
1489 (if (or (eq warning-series 'byte-compile-warning-series)
1490 warning-series-started)
1491 ;; warning-series does come from compilation,
1492 ;; so don't bind it, but maybe do set it.
1494 ;; Log the file name. Record position of that text.
1495 (setq tem (byte-compile-log-file))
1496 (unless warning-series-started
1497 (setq warning-series (or tem 'byte-compile-warning-series)))
1498 (if byte-compile-debug
1499 (funcall --displaying-byte-compile-warnings-fn)
1500 (condition-case error-info
1501 (funcall --displaying-byte-compile-warnings-fn)
1502 (error (byte-compile-report-error error-info)))))
1503 ;; warning-series does not come from compilation, so bind it.
1504 (let ((warning-series
1505 ;; Log the file name. Record position of that text.
1506 (or (byte-compile-log-file) 'byte-compile-warning-series)))
1507 (if byte-compile-debug
1508 (funcall --displaying-byte-compile-warnings-fn)
1509 (condition-case error-info
1510 (funcall --displaying-byte-compile-warnings-fn)
1511 (error (byte-compile-report-error error-info))))))))
1514 (defun byte-force-recompile (directory)
1515 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
1516 Files in subdirectories of DIRECTORY are processed also."
1517 (interactive "DByte force recompile (directory): ")
1518 (byte-recompile-directory directory nil t))
1521 (defun byte-recompile-directory (directory &optional arg force)
1522 "Recompile every `.el' file in DIRECTORY that needs recompilation.
1523 This is if a `.elc' file exists but is older than the `.el' file.
1524 Files in subdirectories of DIRECTORY are processed also.
1526 If the `.elc' file does not exist, normally this function *does not*
1527 compile the corresponding `.el' file. However,
1528 if ARG (the prefix argument) is 0, that means do compile all those files.
1529 A nonzero ARG means ask the user, for each such `.el' file,
1530 whether to compile it.
1532 A nonzero ARG also means ask about each subdirectory before scanning it.
1534 If the third argument FORCE is non-nil,
1535 recompile every `.el' file that already has a `.elc' file."
1536 (interactive "DByte recompile directory: \nP")
1538 (setq arg (prefix-numeric-value arg)))
1542 (force-mode-line-update))
1543 (save-current-buffer
1544 (set-buffer (get-buffer-create "*Compile-Log*"))
1545 (setq default-directory (expand-file-name directory))
1546 ;; compilation-mode copies value of default-directory.
1547 (unless (eq major-mode 'compilation-mode)
1549 (let ((directories (list (expand-file-name directory)))
1550 (default-directory default-directory)
1556 (displaying-byte-compile-warnings
1558 (setq directory (car directories))
1559 (message "Checking %s..." directory)
1560 (let ((files (directory-files directory))
1562 (dolist (file files)
1563 (setq source (expand-file-name file directory))
1564 (if (and (not (member file '("RCS" "CVS")))
1565 (not (eq ?\. (aref file 0)))
1566 (file-directory-p source)
1567 (not (file-symlink-p source)))
1568 ;; This file is a subdirectory. Handle them differently.
1569 (when (or (null arg)
1571 (y-or-n-p (concat "Check " source "? ")))
1573 (nconc directories (list source))))
1574 ;; It is an ordinary file. Decide whether to compile it.
1575 (if (and (string-match emacs-lisp-file-regexp source)
1576 (file-readable-p source)
1577 (not (auto-save-file-name-p source))
1578 (setq dest (byte-compile-dest-file source))
1579 (if (file-exists-p dest)
1580 ;; File was already compiled.
1581 (or force (file-newer-than-file-p source dest))
1582 ;; No compiled file exists yet.
1585 (y-or-n-p (concat "Compile " source "? "))))))
1586 (progn (if (and noninteractive (not byte-compile-verbose))
1587 (message "Compiling %s..." source))
1588 (let ((res (byte-compile-file source)))
1589 (cond ((eq res 'no-byte-compile)
1590 (setq skip-count (1+ skip-count)))
1592 (setq file-count (1+ file-count)))
1594 (setq fail-count (1+ fail-count)))))
1596 (message "Checking %s..." directory))
1597 (if (not (eq last-dir directory))
1598 (setq last-dir directory
1599 dir-count (1+ dir-count)))
1601 (setq directories (cdr directories))))
1602 (message "Done (Total of %d file%s compiled%s%s%s)"
1603 file-count (if (= file-count 1) "" "s")
1604 (if (> fail-count 0) (format ", %d failed" fail-count) "")
1605 (if (> skip-count 0) (format ", %d skipped" skip-count) "")
1606 (if (> dir-count 1) (format " in %d directories" dir-count) "")))))
1608 (defvar no-byte-compile nil
1609 "Non-nil to prevent byte-compiling of emacs-lisp code.
1610 This is normally set in local file variables at the end of the elisp file:
1612 ;; Local Variables:\n;; no-byte-compile: t\n;; End: ")
1613 ;;;###autoload(put 'no-byte-compile 'safe-local-variable 'booleanp)
1616 (defun byte-compile-file (filename &optional load)
1617 "Compile a file of Lisp code named FILENAME into a file of byte code.
1618 The output file's name is made by appending `c' to the end of FILENAME.
1619 With prefix arg (noninteractively: 2nd arg), LOAD the file after compiling.
1620 The value is non-nil if there were no errors, nil if errors."
1621 ;; (interactive "fByte compile file: \nP")
1623 (let ((file buffer-file-name)
1627 (eq (cdr (assq 'major-mode (buffer-local-variables)))
1629 (setq file-name (file-name-nondirectory file)
1630 file-dir (file-name-directory file)))
1631 (list (read-file-name (if current-prefix-arg
1632 "Byte compile and load file: "
1633 "Byte compile file: ")
1634 file-dir file-name nil)
1635 current-prefix-arg)))
1636 ;; Expand now so we get the current buffer's defaults
1637 (setq filename (expand-file-name filename))
1639 ;; If we're compiling a file that's in a buffer and is modified, offer
1640 ;; to save it first.
1642 (let ((b (get-file-buffer (expand-file-name filename))))
1643 (if (and b (buffer-modified-p b)
1644 (y-or-n-p (format "Save buffer %s first? " (buffer-name b))))
1645 (save-excursion (set-buffer b) (save-buffer)))))
1647 ;; Force logging of the file name for each file compiled.
1648 (setq byte-compile-last-logged-file nil)
1649 (let ((byte-compile-current-file filename)
1650 (set-auto-coding-for-load t)
1651 target-file input-buffer output-buffer
1652 byte-compile-dest-file)
1653 (setq target-file (byte-compile-dest-file filename))
1654 (setq byte-compile-dest-file target-file)
1656 (setq input-buffer (get-buffer-create " *Compiler Input*"))
1657 (set-buffer input-buffer)
1659 (setq buffer-file-coding-system nil)
1660 ;; Always compile an Emacs Lisp file as multibyte
1661 ;; unless the file itself forces unibyte with -*-coding: raw-text;-*-
1662 (set-buffer-multibyte t)
1663 (insert-file-contents filename)
1664 ;; Mimic the way after-insert-file-set-coding can make the
1665 ;; buffer unibyte when visiting this file.
1666 (when (or (eq last-coding-system-used 'no-conversion)
1667 (eq (coding-system-type last-coding-system-used) 5))
1668 ;; For coding systems no-conversion and raw-text...,
1669 ;; edit the buffer as unibyte.
1670 (set-buffer-multibyte nil))
1671 ;; Run hooks including the uncompression hook.
1672 ;; If they change the file name, then change it for the output also.
1673 (let ((buffer-file-name filename)
1674 (default-major-mode 'emacs-lisp-mode)
1675 ;; Ignore unsafe local variables.
1676 ;; We only care about a few of them for our purposes.
1677 (enable-local-variables :safe)
1678 (enable-local-eval nil))
1679 ;; Arg of t means don't alter enable-local-variables.
1681 (setq filename buffer-file-name))
1682 ;; Set the default directory, in case an eval-when-compile uses it.
1683 (setq default-directory (file-name-directory filename)))
1684 ;; Check if the file's local variables explicitly specify not to
1685 ;; compile this file.
1686 (if (with-current-buffer input-buffer no-byte-compile)
1688 ;; (message "%s not compiled because of `no-byte-compile: %s'"
1689 ;; (file-relative-name filename)
1690 ;; (with-current-buffer input-buffer no-byte-compile))
1691 (when (file-exists-p target-file)
1692 (message "%s deleted because of `no-byte-compile: %s'"
1693 (file-relative-name target-file)
1694 (buffer-local-value 'no-byte-compile input-buffer))
1695 (condition-case nil (delete-file target-file) (error nil)))
1696 ;; We successfully didn't compile this file.
1698 (when byte-compile-verbose
1699 (message "Compiling %s..." filename))
1700 (setq byte-compiler-error-flag nil)
1701 ;; It is important that input-buffer not be current at this call,
1702 ;; so that the value of point set in input-buffer
1703 ;; within byte-compile-from-buffer lingers in that buffer.
1705 (save-current-buffer
1706 (byte-compile-from-buffer input-buffer filename)))
1707 (if byte-compiler-error-flag
1709 (when byte-compile-verbose
1710 (message "Compiling %s...done" filename))
1711 (kill-buffer input-buffer)
1712 (with-current-buffer output-buffer
1713 (goto-char (point-max))
1714 (insert "\n") ; aaah, unix.
1715 (let ((vms-stmlf-recfm t))
1716 (if (file-writable-p target-file)
1717 ;; We must disable any code conversion here.
1718 (let ((coding-system-for-write 'no-conversion))
1719 (if (memq system-type '(ms-dos 'windows-nt))
1720 (setq buffer-file-type t))
1721 (when (file-exists-p target-file)
1722 ;; Remove the target before writing it, so that any
1723 ;; hard-links continue to point to the old file (this makes
1724 ;; it possible for installed files to share disk space with
1725 ;; the build tree, without causing problems when emacs-lisp
1726 ;; files in the build tree are recompiled).
1727 (delete-file target-file))
1728 (write-region (point-min) (point-max) target-file))
1729 ;; This is just to give a better error message than write-region
1731 (list "Opening output file"
1732 (if (file-exists-p target-file)
1733 "cannot overwrite file"
1734 "directory not writable or nonexistent")
1736 (kill-buffer (current-buffer)))
1737 (if (and byte-compile-generate-call-tree
1738 (or (eq t byte-compile-generate-call-tree)
1739 (y-or-n-p (format "Report call tree for %s? " filename))))
1741 (display-call-tree filename)))
1746 ;;(defun byte-compile-and-load-file (&optional filename)
1747 ;; "Compile a file of Lisp code named FILENAME into a file of byte code,
1748 ;;and then load it. The output file's name is made by appending \"c\" to
1749 ;;the end of FILENAME."
1751 ;; (if filename ; I don't get it, (interactive-p) doesn't always work
1752 ;; (byte-compile-file filename t)
1753 ;; (let ((current-prefix-arg '(4)))
1754 ;; (call-interactively 'byte-compile-file))))
1756 ;;(defun byte-compile-buffer (&optional buffer)
1757 ;; "Byte-compile and evaluate contents of BUFFER (default: the current buffer)."
1758 ;; (interactive "bByte compile buffer: ")
1759 ;; (setq buffer (if buffer (get-buffer buffer) (current-buffer)))
1760 ;; (message "Compiling %s..." (buffer-name buffer))
1761 ;; (let* ((filename (or (buffer-file-name buffer)
1762 ;; (concat "#<buffer " (buffer-name buffer) ">")))
1763 ;; (byte-compile-current-file buffer))
1764 ;; (byte-compile-from-buffer buffer nil))
1765 ;; (message "Compiling %s...done" (buffer-name buffer))
1768 ;;; compiling a single function
1770 (defun compile-defun (&optional arg)
1771 "Compile and evaluate the current top-level form.
1772 Print the result in the echo area.
1773 With argument, insert value in current buffer after the form."
1777 (beginning-of-defun)
1778 (let* ((byte-compile-current-file nil)
1779 (byte-compile-current-buffer (current-buffer))
1780 (byte-compile-read-position (point))
1781 (byte-compile-last-position byte-compile-read-position)
1782 (byte-compile-last-warned-form 'nothing)
1784 (let ((read-with-symbol-positions (current-buffer))
1785 (read-symbol-positions-list nil))
1786 (displaying-byte-compile-warnings
1787 (byte-compile-sexp (read (current-buffer))))))))
1789 (message "Compiling from buffer... done.")
1790 (prin1 value (current-buffer))
1792 ((message "%s" (prin1-to-string value)))))))
1795 (defun byte-compile-from-buffer (inbuffer &optional filename)
1796 ;; Filename is used for the loading-into-Emacs-18 error message.
1798 (byte-compile-current-buffer inbuffer)
1799 (byte-compile-read-position nil)
1800 (byte-compile-last-position nil)
1801 ;; Prevent truncation of flonums and lists as we read and print them
1802 (float-output-format nil)
1803 (case-fold-search nil)
1806 ;; Prevent edebug from interfering when we compile
1807 ;; and put the output into a file.
1808 ;; (edebug-all-defs nil)
1809 ;; (edebug-all-forms nil)
1810 ;; Simulate entry to byte-compile-top-level
1811 (byte-compile-constants nil)
1812 (byte-compile-variables nil)
1813 (byte-compile-tag-number 0)
1814 (byte-compile-depth 0)
1815 (byte-compile-maxdepth 0)
1816 (byte-compile-output nil)
1817 ;; This allows us to get the positions of symbols read; it's
1818 ;; new in Emacs 22.1.
1819 (read-with-symbol-positions inbuffer)
1820 (read-symbol-positions-list nil)
1821 ;; #### This is bound in b-c-close-variables.
1822 ;; (byte-compile-warnings (if (eq byte-compile-warnings t)
1823 ;; byte-compile-warning-types
1824 ;; byte-compile-warnings))
1826 (byte-compile-close-variables
1829 (set-buffer (get-buffer-create " *Compiler Output*")))
1830 (set-buffer-multibyte t)
1832 ;; (emacs-lisp-mode)
1833 (setq case-fold-search nil)
1834 ;; This is a kludge. Some operating systems (OS/2, DOS) need to
1835 ;; write files containing binary information specially.
1836 ;; Under most circumstances, such files will be in binary
1837 ;; overwrite mode, so those OS's use that flag to guess how
1838 ;; they should write their data. Advise them that .elc files
1839 ;; need to be written carefully.
1840 (setq overwrite-mode 'overwrite-mode-binary))
1841 (displaying-byte-compile-warnings
1842 (and filename (byte-compile-insert-header filename inbuffer outbuffer))
1844 (set-buffer inbuffer)
1847 ;; Compile the forms from the input buffer.
1849 (while (progn (skip-chars-forward " \t\n\^l")
1853 (setq byte-compile-read-position (point)
1854 byte-compile-last-position byte-compile-read-position)
1855 (let ((form (read inbuffer)))
1856 (byte-compile-file-form form)))
1857 ;; Compile pending forms at end of file.
1858 (byte-compile-flush-pending)
1859 ;; Make warnings about unresolved functions
1860 ;; give the end of the file as their position.
1861 (setq byte-compile-last-position (point-max))
1862 (byte-compile-warn-about-unresolved-functions)
1863 ;; Should we always do this? When calling multiple files, it
1864 ;; would be useful to delay this warning until all have
1866 (setq byte-compile-unresolved-functions nil))
1867 ;; Fix up the header at the front of the output
1868 ;; if the buffer contains multibyte characters.
1869 (and filename (byte-compile-fix-header filename inbuffer outbuffer))))
1872 (defun byte-compile-fix-header (filename inbuffer outbuffer)
1873 (with-current-buffer outbuffer
1874 ;; See if the buffer has any multibyte characters.
1875 (when (< (point-max) (position-bytes (point-max)))
1876 (when (byte-compile-version-cond byte-compile-compatibility)
1877 (error "Version-18 compatibility not valid with multibyte characters"))
1878 (goto-char (point-min))
1879 ;; Find the comment that describes the version test.
1880 (search-forward "\n;;; This file")
1882 (narrow-to-region (point) (point-max))
1883 ;; Find the line of ballast semicolons.
1884 (search-forward ";;;;;;;;;;")
1887 (narrow-to-region (point-min) (point))
1888 (let ((old-header-end (point))
1890 (goto-char (point-min))
1891 (delete-region (point) (progn (re-search-forward "^(")
1894 (insert ";;; This file contains multibyte non-ASCII characters\n"
1895 ";;; and therefore cannot be loaded into Emacs 19.\n")
1896 ;; Replace "19" or "19.29" with "20", twice.
1897 (re-search-forward "19\\(\\.[0-9]+\\)")
1898 (replace-match "20")
1899 (re-search-forward "19\\(\\.[0-9]+\\)")
1900 (replace-match "20")
1901 ;; Now compensate for the change in size,
1902 ;; to make sure all positions in the file remain valid.
1903 (setq delta (- (point-max) old-header-end))
1904 (goto-char (point-max))
1906 (delete-char delta)))))
1908 (defun byte-compile-insert-header (filename inbuffer outbuffer)
1909 (set-buffer inbuffer)
1910 (let ((dynamic-docstrings byte-compile-dynamic-docstrings)
1911 (dynamic byte-compile-dynamic))
1912 (set-buffer outbuffer)
1914 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After
1915 ;; that is the file-format version number (18, 19 or 20) as a
1916 ;; byte, followed by some nulls. The primary motivation for doing
1917 ;; this is to get some binary characters up in the first line of
1918 ;; the file so that `diff' will simply say "Binary files differ"
1919 ;; instead of actually doing a diff of two .elc files. An extra
1920 ;; benefit is that you can add this to /etc/magic:
1922 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
1923 ;; >4 byte x version %d
1927 (if (byte-compile-version-cond byte-compile-compatibility) 18 20)
1930 (insert ";;; Compiled by "
1931 (or (and (boundp 'user-mail-address) user-mail-address)
1932 (concat (user-login-name) "@" (system-name)))
1934 (current-time-string) "\n;;; from file " filename "\n")
1935 (insert ";;; in Emacs version " emacs-version "\n")
1938 ((eq byte-optimize 'source) "with source-level optimization only")
1939 ((eq byte-optimize 'byte) "with byte-level optimization only")
1940 (byte-optimize "with all optimizations")
1941 (t "without optimization"))
1942 (if (byte-compile-version-cond byte-compile-compatibility)
1943 "; compiled with Emacs 18 compatibility.\n"
1946 (insert ";;; Function definitions are lazy-loaded.\n"))
1947 (if (not (byte-compile-version-cond byte-compile-compatibility))
1948 (let (intro-string minimum-version)
1949 ;; Figure out which Emacs version to require,
1950 ;; and what comment to use to explain why.
1951 ;; Note that this fails to take account of whether
1952 ;; the buffer contains multibyte characters. We may have to
1953 ;; compensate at the end in byte-compile-fix-header.
1954 (if dynamic-docstrings
1956 ";;; This file uses dynamic docstrings, first added in Emacs 19.29.\n"
1957 minimum-version "19.29")
1959 ";;; This file uses opcodes which do not exist in Emacs 18.\n"
1960 minimum-version "19"))
1961 ;; Now insert the comment and the error check.
1965 ;; Have to check if emacs-version is bound so that this works
1966 ;; in files loaded early in loadup.el.
1967 "(if (and (boundp 'emacs-version)\n"
1968 ;; If there is a name at the end of emacs-version,
1969 ;; don't try to check the version number.
1970 "\t (< (aref emacs-version (1- (length emacs-version))) ?A)\n"
1971 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
1972 (format "\t (string-lessp emacs-version \"%s\")))\n"
1975 ;; prin1-to-string is used to quote backslashes.
1976 (substring (prin1-to-string (file-name-nondirectory filename))
1978 (format "' was compiled for Emacs %s or later\"))\n\n"
1980 ;; Insert semicolons as ballast, so that byte-compile-fix-header
1981 ;; can delete them so as to keep the buffer positions
1982 ;; constant for the actual compiled code.
1983 ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n"))
1984 ;; Here if we want Emacs 18 compatibility.
1985 (when dynamic-docstrings
1986 (error "Version-18 compatibility doesn't support dynamic doc strings"))
1987 (when byte-compile-dynamic
1988 (error "Version-18 compatibility doesn't support dynamic byte code"))
1989 (insert "(or (boundp 'current-load-list) (setq current-load-list nil))\n"
1992 (defun byte-compile-output-file-form (form)
1993 ;; writes the given form to the output buffer, being careful of docstrings
1994 ;; in defun, defmacro, defvar, defconst, autoload and
1995 ;; custom-declare-variable because make-docfile is so amazingly stupid.
1996 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
1997 ;; it does not pay to first build the defalias in defmumble and then parse
1999 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload
2000 custom-declare-variable))
2001 (stringp (nth 3 form)))
2002 (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil
2004 '(autoload custom-declare-variable)))
2005 (let ((print-escape-newlines t)
2010 (print-circle ; handle circular data structures
2011 (not byte-compile-disable-print-circle)))
2012 (princ "\n" outbuffer)
2013 (prin1 form outbuffer)
2016 (defvar print-gensym-alist) ;Used before print-circle existed.
2018 (defun byte-compile-output-docform (preface name info form specindex quoted)
2019 "Print a form with a doc string. INFO is (prefix doc-index postfix).
2020 If PREFACE and NAME are non-nil, print them too,
2021 before INFO and the FORM but after the doc string itself.
2022 If SPECINDEX is non-nil, it is the index in FORM
2023 of the function bytecode string. In that case,
2024 we output that argument and the following argument (the constants vector)
2025 together, for lazy loading.
2026 QUOTED says that we have to put a quote before the
2027 list that represents a doc string reference.
2028 `autoload' and `custom-declare-variable' need that."
2029 ;; We need to examine byte-compile-dynamic-docstrings
2030 ;; in the input buffer (now current), not in the output buffer.
2031 (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
2033 (prog1 (current-buffer)
2034 (set-buffer outbuffer)
2037 ;; Insert the doc string, and make it a comment with #@LENGTH.
2038 (and (>= (nth 1 info) 0)
2040 (not byte-compile-compatibility)
2042 ;; Make the doc string start at beginning of line
2043 ;; for make-docfile's sake.
2046 (byte-compile-output-as-comment
2047 (nth (nth 1 info) form) nil))
2048 (setq position (- (position-bytes position) (point-min) -1))
2049 ;; If the doc string starts with * (a user variable),
2051 (if (and (stringp (nth (nth 1 info) form))
2052 (> (length (nth (nth 1 info) form)) 0)
2053 (eq (aref (nth (nth 1 info) form) 0) ?*))
2054 (setq position (- position)))))
2059 (prin1 name outbuffer)))
2061 (let ((print-escape-newlines t)
2063 ;; For compatibility with code before print-circle,
2064 ;; use a cons cell to say that we want
2065 ;; print-gensym-alist not to be cleared
2066 ;; between calls to print functions.
2068 (print-circle ; handle circular data structures
2069 (not byte-compile-disable-print-circle))
2070 print-gensym-alist ; was used before print-circle existed.
2071 (print-continuous-numbering t)
2074 (prin1 (car form) outbuffer)
2075 (while (setq form (cdr form))
2076 (setq index (1+ index))
2078 (cond ((and (numberp specindex) (= index specindex)
2079 ;; Don't handle the definition dynamically
2080 ;; if it refers (or might refer)
2081 ;; to objects already output
2082 ;; (for instance, gensyms in the arg list).
2084 (dotimes (i (length print-number-table))
2085 (if (aref print-number-table i)
2088 ;; Output the byte code and constants specially
2089 ;; for lazy dynamic loading.
2091 (byte-compile-output-as-comment
2092 (cons (car form) (nth 1 form))
2094 (setq position (- (position-bytes position) (point-min) -1))
2095 (princ (format "(#$ . %d) nil" position) outbuffer)
2096 (setq form (cdr form))
2097 (setq index (1+ index))))
2098 ((= index (nth 1 info))
2100 (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)")
2103 (let ((print-escape-newlines nil))
2104 (goto-char (prog1 (1+ (point))
2105 (prin1 (car form) outbuffer)))
2107 (goto-char (point-max)))))
2109 (prin1 (car form) outbuffer)))))
2110 (insert (nth 2 info))))))
2113 (defun byte-compile-keep-pending (form &optional handler)
2114 (if (memq byte-optimize '(t source))
2115 (setq form (byte-optimize-form form t)))
2117 (let ((for-effect t))
2118 ;; To avoid consing up monstrously large forms at load time, we split
2119 ;; the output regularly.
2120 (and (memq (car-safe form) '(fset defalias))
2121 (nthcdr 300 byte-compile-output)
2122 (byte-compile-flush-pending))
2123 (funcall handler form)
2125 (byte-compile-discard)))
2126 (byte-compile-form form t))
2129 (defun byte-compile-flush-pending ()
2130 (if byte-compile-output
2131 (let ((form (byte-compile-out-toplevel t 'file)))
2132 (cond ((eq (car-safe form) 'progn)
2133 (mapc 'byte-compile-output-file-form (cdr form)))
2135 (byte-compile-output-file-form form)))
2136 (setq byte-compile-constants nil
2137 byte-compile-variables nil
2138 byte-compile-depth 0
2139 byte-compile-maxdepth 0
2140 byte-compile-output nil))))
2142 (defun byte-compile-file-form (form)
2143 (let ((byte-compile-current-form nil) ; close over this for warnings.
2147 (byte-compile-keep-pending form))
2148 ((and (symbolp (car form))
2149 (setq handler (get (car form) 'byte-hunk-handler)))
2150 (cond ((setq form (funcall handler form))
2151 (byte-compile-flush-pending)
2152 (byte-compile-output-file-form form))))
2153 ((eq form (setq form (macroexpand form byte-compile-macro-environment)))
2154 (byte-compile-keep-pending form))
2156 (byte-compile-file-form form)))))
2158 ;; Functions and variables with doc strings must be output separately,
2159 ;; so make-docfile can recognise them. Most other things can be output
2162 (put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst)
2163 (defun byte-compile-file-form-defsubst (form)
2164 (when (assq (nth 1 form) byte-compile-unresolved-functions)
2165 (setq byte-compile-current-form (nth 1 form))
2166 (byte-compile-warn "defsubst `%s' was used before it was defined"
2168 (byte-compile-file-form
2169 (macroexpand form byte-compile-macro-environment))
2170 ;; Return nil so the form is not output twice.
2173 (put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
2174 (defun byte-compile-file-form-autoload (form)
2175 (and (let ((form form))
2176 (while (if (setq form (cdr form)) (byte-compile-constp (car form))))
2177 (null form)) ;Constants only
2178 (eval (nth 5 form)) ;Macro
2179 (eval form)) ;Define the autoload.
2180 ;; Avoid undefined function warnings for the autoload.
2181 (if (and (consp (nth 1 form))
2182 (eq (car (nth 1 form)) 'quote)
2183 (consp (cdr (nth 1 form)))
2184 (symbolp (nth 1 (nth 1 form))))
2185 (push (cons (nth 1 (nth 1 form))
2186 (cons 'autoload (cdr (cdr form))))
2187 byte-compile-function-environment))
2188 (if (stringp (nth 3 form))
2190 ;; No doc string, so we can compile this as a normal form.
2191 (byte-compile-keep-pending form 'byte-compile-normal-call)))
2193 (put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar)
2194 (put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar)
2195 (defun byte-compile-file-form-defvar (form)
2196 (if (null (nth 3 form))
2197 ;; Since there is no doc string, we can compile this as a normal form,
2198 ;; and not do a file-boundary.
2199 (byte-compile-keep-pending form)
2200 (when (memq 'free-vars byte-compile-warnings)
2201 (push (nth 1 form) byte-compile-bound-variables)
2202 (if (eq (car form) 'defconst)
2203 (push (nth 1 form) byte-compile-const-variables)))
2204 (cond ((consp (nth 2 form))
2205 (setq form (copy-sequence form))
2206 (setcar (cdr (cdr form))
2207 (byte-compile-top-level (nth 2 form) nil 'file))))
2210 (put 'custom-declare-variable 'byte-hunk-handler
2211 'byte-compile-file-form-custom-declare-variable)
2212 (defun byte-compile-file-form-custom-declare-variable (form)
2213 (when (memq 'callargs byte-compile-warnings)
2214 (byte-compile-nogroup-warn form))
2215 (when (memq 'free-vars byte-compile-warnings)
2216 (push (nth 1 (nth 1 form)) byte-compile-bound-variables))
2217 (let ((tail (nthcdr 4 form)))
2219 ;; If there are any (function (lambda ...)) expressions, compile
2221 (if (and (consp (car tail))
2222 (eq (car (car tail)) 'function)
2223 (consp (nth 1 (car tail))))
2224 (setcar tail (byte-compile-lambda (nth 1 (car tail))))
2225 ;; Likewise for a bare lambda.
2226 (if (and (consp (car tail))
2227 (eq (car (car tail)) 'lambda))
2228 (setcar tail (byte-compile-lambda (car tail)))))
2229 (setq tail (cdr tail))))
2232 (put 'require 'byte-hunk-handler 'byte-compile-file-form-require)
2233 (defun byte-compile-file-form-require (form)
2234 (let ((old-load-list current-load-list)
2235 (args (mapcar 'eval (cdr form))))
2236 (apply 'require args)
2237 ;; Detect (require 'cl) in a way that works even if cl is already loaded.
2238 (if (member (car args) '("cl" cl))
2239 (setq byte-compile-warnings
2240 (remq 'cl-functions byte-compile-warnings))))
2241 (byte-compile-keep-pending form 'byte-compile-normal-call))
2243 (put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
2244 (put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
2245 (put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
2246 (defun byte-compile-file-form-progn (form)
2247 (mapc 'byte-compile-file-form (cdr form))
2248 ;; Return nil so the forms are not output twice.
2251 ;; This handler is not necessary, but it makes the output from dont-compile
2252 ;; and similar macros cleaner.
2253 (put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
2254 (defun byte-compile-file-form-eval (form)
2255 (if (eq (car-safe (nth 1 form)) 'quote)
2256 (nth 1 (nth 1 form))
2257 (byte-compile-keep-pending form)))
2259 (put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun)
2260 (defun byte-compile-file-form-defun (form)
2261 (byte-compile-file-form-defmumble form nil))
2263 (put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro)
2264 (defun byte-compile-file-form-defmacro (form)
2265 (byte-compile-file-form-defmumble form t))
2267 (defun byte-compile-file-form-defmumble (form macrop)
2268 (let* ((name (car (cdr form)))
2269 (this-kind (if macrop 'byte-compile-macro-environment
2270 'byte-compile-function-environment))
2271 (that-kind (if macrop 'byte-compile-function-environment
2272 'byte-compile-macro-environment))
2273 (this-one (assq name (symbol-value this-kind)))
2274 (that-one (assq name (symbol-value that-kind)))
2275 (byte-compile-free-references nil)
2276 (byte-compile-free-assignments nil))
2277 (byte-compile-set-symbol-position name)
2278 ;; When a function or macro is defined, add it to the call tree so that
2279 ;; we can tell when functions are not used.
2280 (if byte-compile-generate-call-tree
2281 (or (assq name byte-compile-call-tree)
2282 (setq byte-compile-call-tree
2283 (cons (list name nil nil) byte-compile-call-tree))))
2285 (setq byte-compile-current-form name) ; for warnings
2286 (if (memq 'redefine byte-compile-warnings)
2287 (byte-compile-arglist-warn form macrop))
2288 (if byte-compile-verbose
2289 (message "Compiling %s... (%s)" (or filename "") (nth 1 form)))
2291 (if (and (memq 'redefine byte-compile-warnings)
2292 ;; don't warn when compiling the stubs in byte-run...
2293 (not (assq (nth 1 form)
2294 byte-compile-initial-macro-environment)))
2296 "`%s' defined multiple times, as both function and macro"
2298 (setcdr that-one nil))
2300 (when (and (memq 'redefine byte-compile-warnings)
2301 ;; hack: don't warn when compiling the magic internal
2302 ;; byte-compiler macros in byte-run.el...
2303 (not (assq (nth 1 form)
2304 byte-compile-initial-macro-environment)))
2305 (byte-compile-warn "%s `%s' defined multiple times in this file"
2306 (if macrop "macro" "function")
2308 ((and (fboundp name)
2309 (eq (car-safe (symbol-function name))
2310 (if macrop 'lambda 'macro)))
2311 (when (memq 'redefine byte-compile-warnings)
2312 (byte-compile-warn "%s `%s' being redefined as a %s"
2313 (if macrop "function" "macro")
2315 (if macrop "macro" "function")))
2316 ;; shadow existing definition
2318 (cons (cons name nil) (symbol-value this-kind))))
2320 (let ((body (nthcdr 3 form)))
2321 (when (and (stringp (car body))
2322 (symbolp (car-safe (cdr-safe body)))
2323 (car-safe (cdr-safe body))
2324 (stringp (car-safe (cdr-safe (cdr-safe body)))))
2325 (byte-compile-set-symbol-position (nth 1 form))
2326 (byte-compile-warn "probable `\"' without `\\' in doc string of %s"
2329 ;; Generate code for declarations in macro definitions.
2330 ;; Remove declarations from the body of the macro definition.
2332 (let ((tail (nthcdr 2 form)))
2333 (when (stringp (car (cdr tail)))
2334 (setq tail (cdr tail)))
2335 (while (and (consp (car (cdr tail)))
2336 (eq (car (car (cdr tail))) 'declare))
2337 (let ((declaration (car (cdr tail))))
2338 (setcdr tail (cdr (cdr tail)))
2339 (princ `(if macro-declaration-function
2340 (funcall macro-declaration-function
2341 ',name ',declaration))
2344 (let* ((new-one (byte-compile-lambda (nthcdr 2 form) t))
2345 (code (byte-compile-byte-code-maker new-one)))
2347 (setcdr this-one new-one)
2349 (cons (cons name new-one) (symbol-value this-kind))))
2350 (if (and (stringp (nth 3 form))
2351 (eq 'quote (car-safe code))
2352 (eq 'lambda (car-safe (nth 1 code))))
2354 (cons name (cdr (nth 1 code))))
2355 (byte-compile-flush-pending)
2356 (if (not (stringp (nth 3 form)))
2357 ;; No doc string. Provide -1 as the "doc string index"
2358 ;; so that no element will be treated as a doc string.
2359 (byte-compile-output-docform
2360 (if (byte-compile-version-cond byte-compile-compatibility)
2361 "\n(fset '" "\n(defalias '")
2364 (if macrop '(" '(macro . #[" -1 "])") '(" #[" -1 "]")))
2365 ((eq (car code) 'quote)
2367 (if macrop '(" '(macro " -1 ")") '(" '(" -1 ")")))
2368 ((if macrop '(" (cons 'macro (" -1 "))") '(" (" -1 ")"))))
2370 (and (atom code) byte-compile-dynamic
2373 ;; Output the form by hand, that's much simpler than having
2374 ;; b-c-output-file-form analyze the defalias.
2375 (byte-compile-output-docform
2376 (if (byte-compile-version-cond byte-compile-compatibility)
2377 "\n(fset '" "\n(defalias '")
2380 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]")))
2381 ((eq (car code) 'quote)
2383 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")")))
2384 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")"))))
2386 (and (atom code) byte-compile-dynamic
2389 (princ ")" outbuffer)
2392 ;; Print Lisp object EXP in the output file, inside a comment,
2393 ;; and return the file position it will have.
2394 ;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting.
2395 (defun byte-compile-output-as-comment (exp quoted)
2396 (let ((position (point)))
2398 (prog1 (current-buffer)
2399 (set-buffer outbuffer)
2401 ;; Insert EXP, and make it a comment with #@LENGTH.
2404 (prin1 exp outbuffer)
2405 (princ exp outbuffer))
2406 (goto-char position)
2407 ;; Quote certain special characters as needed.
2408 ;; get_doc_string in doc.c does the unquoting.
2409 (while (search-forward "\^A" nil t)
2410 (replace-match "\^A\^A" t t))
2411 (goto-char position)
2412 (while (search-forward "\000" nil t)
2413 (replace-match "\^A0" t t))
2414 (goto-char position)
2415 (while (search-forward "\037" nil t)
2416 (replace-match "\^A_" t t))
2417 (goto-char (point-max))
2419 (goto-char position)
2420 (insert "#@" (format "%d" (- (position-bytes (point-max))
2421 (position-bytes position))))
2423 ;; Save the file position of the object.
2424 ;; Note we should add 1 to skip the space
2425 ;; that we inserted before the actual doc string,
2426 ;; and subtract 1 to convert from an 1-origin Emacs position
2427 ;; to a file position; they cancel.
2428 (setq position (point))
2429 (goto-char (point-max))))
2435 (defun byte-compile (form)
2436 "If FORM is a symbol, byte-compile its function definition.
2437 If FORM is a lambda or a macro, byte-compile it as a function."
2438 (displaying-byte-compile-warnings
2439 (byte-compile-close-variables
2440 (let* ((fun (if (symbolp form)
2441 (and (fboundp form) (symbol-function form))
2443 (macro (eq (car-safe fun) 'macro)))
2445 (setq fun (cdr fun)))
2446 (cond ((eq (car-safe fun) 'lambda)
2448 (cons 'macro (byte-compile-lambda fun))
2449 (byte-compile-lambda fun)))
2454 (defun byte-compile-sexp (sexp)
2455 "Compile and return SEXP."
2456 (displaying-byte-compile-warnings
2457 (byte-compile-close-variables
2458 (byte-compile-top-level sexp))))
2460 ;; Given a function made by byte-compile-lambda, make a form which produces it.
2461 (defun byte-compile-byte-code-maker (fun)
2463 ((byte-compile-version-cond byte-compile-compatibility)
2464 ;; Return (quote (lambda ...)).
2465 (list 'quote (byte-compile-byte-code-unmake fun)))
2466 ;; ## atom is faster than compiled-func-p.
2467 ((atom fun) ; compiled function.
2468 ;; generate-emacs19-bytecodes must be on, otherwise byte-compile-lambda
2469 ;; would have produced a lambda.
2471 ;; b-c-lambda didn't produce a compiled-function, so it's either a trivial
2472 ;; function, or this is Emacs 18, or generate-emacs19-bytecodes is off.
2474 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun))))
2475 (null (cdr (memq tmp fun))))
2476 ;; Generate a make-byte-code call.
2477 (let* ((interactive (assq 'interactive (cdr (cdr fun)))))
2478 (nconc (list 'make-byte-code
2479 (list 'quote (nth 1 fun)) ;arglist
2483 (cond ((stringp (nth 2 fun))
2484 (list (nth 2 fun))) ;doc
2488 (list (if (or (null (nth 1 interactive))
2489 (stringp (nth 1 interactive)))
2491 ;; Interactive spec is a list or a variable
2492 ;; (if it is correct).
2493 (list 'quote (nth 1 interactive))))))))
2494 ;; a non-compiled function (probably trivial)
2495 (list 'quote fun))))))
2497 ;; Turn a function into an ordinary lambda. Needed for v18 files.
2498 (defun byte-compile-byte-code-unmake (function)
2499 (if (consp function)
2500 function;;It already is a lambda.
2501 (setq function (append function nil)) ; turn it into a list
2502 (nconc (list 'lambda (nth 0 function))
2503 (and (nth 4 function) (list (nth 4 function)))
2504 (if (nthcdr 5 function)
2505 (list (cons 'interactive (if (nth 5 function)
2506 (nthcdr 5 function)))))
2507 (list (list 'byte-code
2508 (nth 1 function) (nth 2 function)
2509 (nth 3 function))))))
2512 (defun byte-compile-check-lambda-list (list)
2513 "Check lambda-list LIST for errors."
2516 (let ((arg (car list)))
2518 (byte-compile-set-symbol-position arg))
2519 (cond ((or (not (symbolp arg))
2520 (byte-compile-const-symbol-p arg t))
2521 (error "Invalid lambda variable %s" arg))
2524 (error "&rest without variable name"))
2526 (error "Garbage following &rest VAR in lambda-list")))
2527 ((eq arg '&optional)
2529 (error "Variable name missing after &optional")))
2531 (byte-compile-warn "repeated variable %s in lambda-list" arg))
2534 (setq list (cdr list)))))
2537 ;; Byte-compile a lambda-expression and return a valid function.
2538 ;; The value is usually a compiled function but may be the original
2539 ;; lambda-expression.
2540 ;; When ADD-LAMBDA is non-nil, the symbol `lambda' is added as head
2541 ;; of the list FUN and `byte-compile-set-symbol-position' is not called.
2542 ;; Use this feature to avoid calling `byte-compile-set-symbol-position'
2543 ;; for symbols generated by the byte compiler itself.
2544 (defun byte-compile-lambda (fun &optional add-lambda)
2546 (setq fun (cons 'lambda fun))
2547 (unless (eq 'lambda (car-safe fun))
2548 (error "Not a lambda list: %S" fun))
2549 (byte-compile-set-symbol-position 'lambda))
2550 (byte-compile-check-lambda-list (nth 1 fun))
2551 (let* ((arglist (nth 1 fun))
2552 (byte-compile-bound-variables
2553 (nconc (and (memq 'free-vars byte-compile-warnings)
2554 (delq '&rest (delq '&optional (copy-sequence arglist))))
2555 byte-compile-bound-variables))
2556 (body (cdr (cdr fun)))
2557 (doc (if (stringp (car body))
2559 ;; Discard the doc string
2560 ;; unless it is the last element of the body.
2562 (setq body (cdr body))))))
2563 (int (assq 'interactive body)))
2564 ;; Process the interactive spec.
2566 (byte-compile-set-symbol-position 'interactive)
2567 ;; Skip (interactive) if it is in front (the most usual location).
2568 (if (eq int (car body))
2569 (setq body (cdr body)))
2570 (cond ((consp (cdr int))
2572 (byte-compile-warn "malformed interactive spec: %s"
2573 (prin1-to-string int)))
2574 ;; If the interactive spec is a call to `list', don't
2575 ;; compile it, because `call-interactively' looks at the
2576 ;; args of `list'. Actually, compile it to get warnings,
2577 ;; but don't use the result.
2578 (let ((form (nth 1 int)))
2579 (while (memq (car-safe form) '(let let* progn save-excursion))
2580 (while (consp (cdr form))
2581 (setq form (cdr form)))
2582 (setq form (car form)))
2583 (if (eq (car-safe form) 'list)
2584 (byte-compile-top-level (nth 1 int))
2585 (setq int (list 'interactive
2586 (byte-compile-top-level (nth 1 int)))))))
2588 (byte-compile-warn "malformed interactive spec: %s"
2589 (prin1-to-string int)))))
2590 ;; Process the body.
2591 (let ((compiled (byte-compile-top-level (cons 'progn body) nil 'lambda)))
2592 ;; Build the actual byte-coded function.
2593 (if (and (eq 'byte-code (car-safe compiled))
2594 (not (byte-compile-version-cond
2595 byte-compile-compatibility)))
2596 (apply 'make-byte-code
2597 (append (list arglist)
2598 ;; byte-string, constants-vector, stack depth
2600 ;; optionally, the doc string.
2603 ;; optionally, the interactive spec.
2605 (list (nth 1 int)))))
2607 (nconc (if int (list int))
2608 (cond ((eq (car-safe compiled) 'progn) (cdr compiled))
2609 (compiled (list compiled)))))
2610 (nconc (list 'lambda arglist)
2611 (if (or doc (stringp (car compiled)))
2612 (cons doc (cond (compiled)
2616 (defun byte-compile-constants-vector ()
2617 ;; Builds the constants-vector from the current variables and constants.
2618 ;; This modifies the constants from (const . nil) to (const . offset).
2619 ;; To keep the byte-codes to look up the vector as short as possible:
2620 ;; First 6 elements are vars, as there are one-byte varref codes for those.
2621 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
2622 ;; Next variables again, to get 2-byte codes for variable lookup.
2623 ;; The rest of the constants and variables need 3-byte byte-codes.
2625 (rest (nreverse byte-compile-variables)) ; nreverse because the first
2626 (other (nreverse byte-compile-constants)) ; vars often are used most.
2628 (limits '(5 ; Use the 1-byte varref codes,
2629 63 ; 1-constlim ; 1-byte byte-constant codes,
2630 255 ; 2-byte varref codes,
2631 65535)) ; 3-byte codes for the rest.
2633 (while (or rest other)
2634 (setq limit (car limits))
2635 (while (and rest (not (eq i limit)))
2636 (if (setq tmp (assq (car (car rest)) ret))
2637 (setcdr (car rest) (cdr tmp))
2638 (setcdr (car rest) (setq i (1+ i)))
2639 (setq ret (cons (car rest) ret)))
2640 (setq rest (cdr rest)))
2641 (setq limits (cdr limits)
2643 (setq other rest))))
2644 (apply 'vector (nreverse (mapcar 'car ret)))))
2646 ;; Given an expression FORM, compile it and return an equivalent byte-code
2647 ;; expression (a call to the function byte-code).
2648 (defun byte-compile-top-level (form &optional for-effect output-type)
2649 ;; OUTPUT-TYPE advises about how form is expected to be used:
2650 ;; 'eval or nil -> a single form,
2651 ;; 'progn or t -> a list of forms,
2652 ;; 'lambda -> body of a lambda,
2653 ;; 'file -> used at file-level.
2654 (let ((byte-compile-constants nil)
2655 (byte-compile-variables nil)
2656 (byte-compile-tag-number 0)
2657 (byte-compile-depth 0)
2658 (byte-compile-maxdepth 0)
2659 (byte-compile-output nil))
2660 (if (memq byte-optimize '(t source))
2661 (setq form (byte-optimize-form form for-effect)))
2662 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
2663 (setq form (nth 1 form)))
2664 (if (and (eq 'byte-code (car-safe form))
2665 (not (memq byte-optimize '(t byte)))
2666 (stringp (nth 1 form)) (vectorp (nth 2 form))
2667 (natnump (nth 3 form)))
2669 (byte-compile-form form for-effect)
2670 (byte-compile-out-toplevel for-effect output-type))))
2672 (defun byte-compile-out-toplevel (&optional for-effect output-type)
2674 ;; The stack is empty. Push a value to be returned from (byte-code ..).
2675 (if (eq (car (car byte-compile-output)) 'byte-discard)
2676 (setq byte-compile-output (cdr byte-compile-output))
2677 (byte-compile-push-constant
2678 ;; Push any constant - preferably one which already is used, and
2679 ;; a number or symbol - ie not some big sequence. The return value
2680 ;; isn't returned, but it would be a shame if some textually large
2681 ;; constant was not optimized away because we chose to return it.
2682 (and (not (assq nil byte-compile-constants)) ; Nil is often there.
2683 (let ((tmp (reverse byte-compile-constants)))
2684 (while (and tmp (not (or (symbolp (caar tmp))
2685 (numberp (caar tmp)))))
2686 (setq tmp (cdr tmp)))
2688 (byte-compile-out 'byte-return 0)
2689 (setq byte-compile-output (nreverse byte-compile-output))
2690 (if (memq byte-optimize '(t byte))
2691 (setq byte-compile-output
2692 (byte-optimize-lapcode byte-compile-output for-effect)))
2694 ;; Decompile trivial functions:
2695 ;; only constants and variables, or a single funcall except in lambdas.
2696 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
2697 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
2698 ;; Note that even (quote foo) must be parsed just as any subr by the
2699 ;; interpreter, so quote should be compiled into byte-code in some contexts.
2700 ;; What to leave uncompiled:
2701 ;; lambda -> never. we used to leave it uncompiled if the body was
2702 ;; a single atom, but that causes confusion if the docstring
2703 ;; uses the (file . pos) syntax. Besides, now that we have
2704 ;; the Lisp_Compiled type, the compiled form is faster.
2705 ;; eval -> atom, quote or (function atom atom atom)
2706 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
2707 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
2709 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
2712 ;; #### This should be split out into byte-compile-nontrivial-function-p.
2713 ((or (eq output-type 'lambda)
2714 (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
2715 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
2716 (not (setq tmp (assq 'byte-return byte-compile-output)))
2718 (setq rest (nreverse
2719 (cdr (memq tmp (reverse byte-compile-output)))))
2721 ((memq (car (car rest)) '(byte-varref byte-constant))
2722 (setq tmp (car (cdr (car rest))))
2723 (if (if (eq (car (car rest)) 'byte-constant)
2726 (not (byte-compile-const-symbol-p tmp)))))
2728 (setq body (cons (list 'quote tmp) body)))
2729 (setq body (cons tmp body))))
2731 ;; Allow a funcall if at most one atom follows it.
2732 (null (nthcdr 3 rest))
2733 (setq tmp (get (car (car rest)) 'byte-opcode-invert))
2734 (or (null (cdr rest))
2735 (and (memq output-type '(file progn t))
2737 (eq (car (nth 1 rest)) 'byte-discard)
2738 (progn (setq rest (cdr rest)) t))))
2739 (setq maycall nil) ; Only allow one real function call.
2740 (setq body (nreverse body))
2742 (if (and (eq tmp 'funcall)
2743 (eq (car-safe (car body)) 'quote))
2744 (cons (nth 1 (car body)) (cdr body))
2746 (or (eq output-type 'file)
2747 (not (delq nil (mapcar 'consp (cdr (car body))))))))
2748 (setq rest (cdr rest)))
2750 (let ((byte-compile-vector (byte-compile-constants-vector)))
2751 (list 'byte-code (byte-compile-lapcode byte-compile-output)
2752 byte-compile-vector byte-compile-maxdepth)))
2753 ;; it's a trivial function
2754 ((cdr body) (cons 'progn (nreverse body)))
2757 ;; Given BODY, compile it and return a new body.
2758 (defun byte-compile-top-level-body (body &optional for-effect)
2759 (setq body (byte-compile-top-level (cons 'progn body) for-effect t))
2760 (cond ((eq (car-safe body) 'progn)
2765 ;; This is the recursive entry point for compiling each subform of an
2767 ;; If for-effect is non-nil, byte-compile-form will output a byte-discard
2768 ;; before terminating (ie no value will be left on the stack).
2769 ;; A byte-compile handler may, when for-effect is non-nil, choose output code
2770 ;; which does not leave a value on the stack, and then set for-effect to nil
2771 ;; (to prevent byte-compile-form from outputting the byte-discard).
2772 ;; If a handler wants to call another handler, it should do so via
2773 ;; byte-compile-form, or take extreme care to handle for-effect correctly.
2774 ;; (Use byte-compile-form-do-effect to reset the for-effect flag too.)
2776 (defun byte-compile-form (form &optional for-effect)
2777 (setq form (macroexpand form byte-compile-macro-environment))
2778 (cond ((not (consp form))
2779 (cond ((or (not (symbolp form)) (byte-compile-const-symbol-p form))
2780 (when (symbolp form)
2781 (byte-compile-set-symbol-position form))
2782 (byte-compile-constant form))
2783 ((and for-effect byte-compile-delete-errors)
2784 (when (symbolp form)
2785 (byte-compile-set-symbol-position form))
2786 (setq for-effect nil))
2787 (t (byte-compile-variable-ref 'byte-varref form))))
2788 ((symbolp (car form))
2789 (let* ((fn (car form))
2790 (handler (get fn 'byte-compile)))
2791 (when (byte-compile-const-symbol-p fn)
2792 (byte-compile-warn "`%s' called as a function" fn))
2793 (and (memq 'interactive-only byte-compile-warnings)
2794 (memq fn byte-compile-interactive-only-functions)
2795 (byte-compile-warn "`%s' used from Lisp code\n\
2796 That command is designed for interactive use only" fn))
2798 ;; Make sure that function exists. This is important
2799 ;; for CL compiler macros since the symbol may be
2800 ;; `cl-byte-compile-compiler-macro' but if CL isn't
2801 ;; loaded, this function doesn't exist.
2802 (or (not (memq handler '(cl-byte-compile-compiler-macro)))
2803 (functionp handler))
2804 (not (and (byte-compile-version-cond
2805 byte-compile-compatibility)
2806 (get (get fn 'byte-opcode) 'emacs19-opcode))))
2807 (funcall handler form)
2808 (when (memq 'callargs byte-compile-warnings)
2809 (if (memq fn '(custom-declare-group custom-declare-variable custom-declare-face))
2810 (byte-compile-nogroup-warn form))
2811 (byte-compile-callargs-warn form))
2812 (byte-compile-normal-call form))
2813 (if (memq 'cl-functions byte-compile-warnings)
2814 (byte-compile-cl-warn form))))
2815 ((and (or (byte-code-function-p (car form))
2816 (eq (car-safe (car form)) 'lambda))
2817 ;; if the form comes out the same way it went in, that's
2818 ;; because it was malformed, and we couldn't unfold it.
2819 (not (eq form (setq form (byte-compile-unfold-lambda form)))))
2820 (byte-compile-form form for-effect)
2821 (setq for-effect nil))
2822 ((byte-compile-normal-call form)))
2824 (byte-compile-discard)))
2826 (defun byte-compile-normal-call (form)
2827 (if byte-compile-generate-call-tree
2828 (byte-compile-annotate-call-tree form))
2829 (byte-compile-push-constant (car form))
2830 (mapc 'byte-compile-form (cdr form)) ; wasteful, but faster.
2831 (byte-compile-out 'byte-call (length (cdr form))))
2833 (defun byte-compile-variable-ref (base-op var)
2835 (byte-compile-set-symbol-position var))
2836 (if (or (not (symbolp var))
2837 (byte-compile-const-symbol-p var (not (eq base-op 'byte-varref))))
2839 (cond ((eq base-op 'byte-varbind) "attempt to let-bind %s `%s'")
2840 ((eq base-op 'byte-varset) "variable assignment to %s `%s'")
2841 (t "variable reference to %s `%s'"))
2842 (if (symbolp var) "constant" "nonvariable")
2843 (prin1-to-string var))
2844 (if (and (get var 'byte-obsolete-variable)
2845 (memq 'obsolete byte-compile-warnings)
2846 (not (eq var byte-compile-not-obsolete-var)))
2847 (let* ((ob (get var 'byte-obsolete-variable))
2849 (byte-compile-warn "`%s' is an obsolete variable%s; %s" var
2850 (if when (concat " (as of Emacs " when ")") "")
2851 (if (stringp (car ob))
2853 (format "use `%s' instead." (car ob))))))
2854 (if (memq 'free-vars byte-compile-warnings)
2855 (if (eq base-op 'byte-varbind)
2856 (push var byte-compile-bound-variables)
2858 (memq var byte-compile-bound-variables)
2859 (if (eq base-op 'byte-varset)
2860 (or (memq var byte-compile-free-assignments)
2862 (byte-compile-warn "assignment to free variable `%s'" var)
2863 (push var byte-compile-free-assignments)))
2864 (or (memq var byte-compile-free-references)
2866 (byte-compile-warn "reference to free variable `%s'" var)
2867 (push var byte-compile-free-references))))))))
2868 (let ((tmp (assq var byte-compile-variables)))
2870 (setq tmp (list var))
2871 (push tmp byte-compile-variables))
2872 (byte-compile-out base-op tmp)))
2874 (defmacro byte-compile-get-constant (const)
2875 `(or (if (stringp ,const)
2876 ;; In a string constant, treat properties as significant.
2878 (dolist (elt byte-compile-constants)
2879 (if (equal-including-properties (car elt) ,const)
2882 (assq ,const byte-compile-constants))
2883 (car (setq byte-compile-constants
2884 (cons (list ,const) byte-compile-constants)))))
2886 ;; Use this when the value of a form is a constant. This obeys for-effect.
2887 (defun byte-compile-constant (const)
2889 (setq for-effect nil)
2890 (when (symbolp const)
2891 (byte-compile-set-symbol-position const))
2892 (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
2894 ;; Use this for a constant that is not the value of its containing form.
2895 ;; This ignores for-effect.
2896 (defun byte-compile-push-constant (const)
2897 (let ((for-effect nil))
2898 (inline (byte-compile-constant const))))
2901 ;; Compile those primitive ordinary functions
2902 ;; which have special byte codes just for speed.
2904 (defmacro byte-defop-compiler (function &optional compile-handler)
2905 ;; add a compiler-form for FUNCTION.
2906 ;; If function is a symbol, then the variable "byte-SYMBOL" must name
2907 ;; the opcode to be used. If function is a list, the first element
2908 ;; is the function and the second element is the bytecode-symbol.
2909 ;; The second element may be nil, meaning there is no opcode.
2910 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or
2911 ;; may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
2912 ;; If it is nil, then the handler is "byte-compile-SYMBOL."
2914 (if (symbolp function)
2915 (setq opcode (intern (concat "byte-" (symbol-name function))))
2916 (setq opcode (car (cdr function))
2917 function (car function)))
2919 (list 'put (list 'quote function) ''byte-compile
2921 (or (cdr (assq compile-handler
2922 '((0 . byte-compile-no-args)
2923 (1 . byte-compile-one-arg)
2924 (2 . byte-compile-two-args)
2925 (3 . byte-compile-three-args)
2926 (0-1 . byte-compile-zero-or-one-arg)
2927 (1-2 . byte-compile-one-or-two-args)
2928 (2-3 . byte-compile-two-or-three-args)
2931 (intern (concat "byte-compile-"
2932 (symbol-name function))))))))
2935 (list 'put (list 'quote function)
2936 ''byte-opcode (list 'quote opcode))
2937 (list 'put (list 'quote opcode)
2938 ''byte-opcode-invert (list 'quote function)))
2941 (defmacro byte-defop-compiler19 (function &optional compile-handler)
2942 ;; Just like byte-defop-compiler, but defines an opcode that will only
2943 ;; be used when byte-compile-compatibility is false.
2944 (if (and (byte-compile-single-version)
2945 byte-compile-compatibility)
2946 ;; #### instead of doing nothing, this should do some remprops,
2947 ;; #### to protect against the case where a single-version compiler
2948 ;; #### is loaded into a world that has contained a multi-version one.
2953 (or (car (cdr-safe function))
2954 (intern (concat "byte-"
2955 (symbol-name (or (car-safe function) function))))))
2957 (list 'byte-defop-compiler function compile-handler))))
2959 (defmacro byte-defop-compiler-1 (function &optional compile-handler)
2960 (list 'byte-defop-compiler (list function nil) compile-handler))
2963 (put 'byte-call 'byte-opcode-invert 'funcall)
2964 (put 'byte-list1 'byte-opcode-invert 'list)
2965 (put 'byte-list2 'byte-opcode-invert 'list)
2966 (put 'byte-list3 'byte-opcode-invert 'list)
2967 (put 'byte-list4 'byte-opcode-invert 'list)
2968 (put 'byte-listN 'byte-opcode-invert 'list)
2969 (put 'byte-concat2 'byte-opcode-invert 'concat)
2970 (put 'byte-concat3 'byte-opcode-invert 'concat)
2971 (put 'byte-concat4 'byte-opcode-invert 'concat)
2972 (put 'byte-concatN 'byte-opcode-invert 'concat)
2973 (put 'byte-insertN 'byte-opcode-invert 'insert)
2975 (byte-defop-compiler point 0)
2976 ;;(byte-defop-compiler mark 0) ;; obsolete
2977 (byte-defop-compiler point-max 0)
2978 (byte-defop-compiler point-min 0)
2979 (byte-defop-compiler following-char 0)
2980 (byte-defop-compiler preceding-char 0)
2981 (byte-defop-compiler current-column 0)
2982 (byte-defop-compiler eolp 0)
2983 (byte-defop-compiler eobp 0)
2984 (byte-defop-compiler bolp 0)
2985 (byte-defop-compiler bobp 0)
2986 (byte-defop-compiler current-buffer 0)
2987 ;;(byte-defop-compiler read-char 0) ;; obsolete
2988 (byte-defop-compiler interactive-p 0)
2989 (byte-defop-compiler19 widen 0)
2990 (byte-defop-compiler19 end-of-line 0-1)
2991 (byte-defop-compiler19 forward-char 0-1)
2992 (byte-defop-compiler19 forward-line 0-1)
2993 (byte-defop-compiler symbolp 1)
2994 (byte-defop-compiler consp 1)
2995 (byte-defop-compiler stringp 1)
2996 (byte-defop-compiler listp 1)
2997 (byte-defop-compiler not 1)
2998 (byte-defop-compiler (null byte-not) 1)
2999 (byte-defop-compiler car 1)
3000 (byte-defop-compiler cdr 1)
3001 (byte-defop-compiler length 1)
3002 (byte-defop-compiler symbol-value 1)
3003 (byte-defop-compiler symbol-function 1)
3004 (byte-defop-compiler (1+ byte-add1) 1)
3005 (byte-defop-compiler (1- byte-sub1) 1)
3006 (byte-defop-compiler goto-char 1)
3007 (byte-defop-compiler char-after 0-1)
3008 (byte-defop-compiler set-buffer 1)
3009 ;;(byte-defop-compiler set-mark 1) ;; obsolete
3010 (byte-defop-compiler19 forward-word 0-1)
3011 (byte-defop-compiler19 char-syntax 1)
3012 (byte-defop-compiler19 nreverse 1)
3013 (byte-defop-compiler19 car-safe 1)
3014 (byte-defop-compiler19 cdr-safe 1)
3015 (byte-defop-compiler19 numberp 1)
3016 (byte-defop-compiler19 integerp 1)
3017 (byte-defop-compiler19 skip-chars-forward 1-2)
3018 (byte-defop-compiler19 skip-chars-backward 1-2)
3019 (byte-defop-compiler eq 2)
3020 (byte-defop-compiler memq 2)
3021 (byte-defop-compiler cons 2)
3022 (byte-defop-compiler aref 2)
3023 (byte-defop-compiler set 2)
3024 (byte-defop-compiler (= byte-eqlsign) 2)
3025 (byte-defop-compiler (< byte-lss) 2)
3026 (byte-defop-compiler (> byte-gtr) 2)
3027 (byte-defop-compiler (<= byte-leq) 2)
3028 (byte-defop-compiler (>= byte-geq) 2)
3029 (byte-defop-compiler get 2)
3030 (byte-defop-compiler nth 2)
3031 (byte-defop-compiler substring 2-3)
3032 (byte-defop-compiler19 (move-marker byte-set-marker) 2-3)
3033 (byte-defop-compiler19 set-marker 2-3)
3034 (byte-defop-compiler19 match-beginning 1)
3035 (byte-defop-compiler19 match-end 1)
3036 (byte-defop-compiler19 upcase 1)
3037 (byte-defop-compiler19 downcase 1)
3038 (byte-defop-compiler19 string= 2)
3039 (byte-defop-compiler19 string< 2)
3040 (byte-defop-compiler19 (string-equal byte-string=) 2)
3041 (byte-defop-compiler19 (string-lessp byte-string<) 2)
3042 (byte-defop-compiler19 equal 2)
3043 (byte-defop-compiler19 nthcdr 2)
3044 (byte-defop-compiler19 elt 2)
3045 (byte-defop-compiler19 member 2)
3046 (byte-defop-compiler19 assq 2)
3047 (byte-defop-compiler19 (rplaca byte-setcar) 2)
3048 (byte-defop-compiler19 (rplacd byte-setcdr) 2)
3049 (byte-defop-compiler19 setcar 2)
3050 (byte-defop-compiler19 setcdr 2)
3051 (byte-defop-compiler19 buffer-substring 2)
3052 (byte-defop-compiler19 delete-region 2)
3053 (byte-defop-compiler19 narrow-to-region 2)
3054 (byte-defop-compiler19 (% byte-rem) 2)
3055 (byte-defop-compiler aset 3)
3057 (byte-defop-compiler max byte-compile-associative)
3058 (byte-defop-compiler min byte-compile-associative)
3059 (byte-defop-compiler (+ byte-plus) byte-compile-associative)
3060 (byte-defop-compiler19 (* byte-mult) byte-compile-associative)
3062 ;;####(byte-defop-compiler19 move-to-column 1)
3063 (byte-defop-compiler-1 interactive byte-compile-noop)
3066 (defun byte-compile-subr-wrong-args (form n)
3067 (byte-compile-set-symbol-position (car form))
3068 (byte-compile-warn "`%s' called with %d arg%s, but requires %s"
3069 (car form) (length (cdr form))
3070 (if (= 1 (length (cdr form))) "" "s") n)
3071 ;; get run-time wrong-number-of-args error.
3072 (byte-compile-normal-call form))
3074 (defun byte-compile-no-args (form)
3075 (if (not (= (length form) 1))
3076 (byte-compile-subr-wrong-args form "none")
3077 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3079 (defun byte-compile-one-arg (form)
3080 (if (not (= (length form) 2))
3081 (byte-compile-subr-wrong-args form 1)
3082 (byte-compile-form (car (cdr form))) ;; Push the argument
3083 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3085 (defun byte-compile-two-args (form)
3086 (if (not (= (length form) 3))
3087 (byte-compile-subr-wrong-args form 2)
3088 (byte-compile-form (car (cdr form))) ;; Push the arguments
3089 (byte-compile-form (nth 2 form))
3090 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3092 (defun byte-compile-three-args (form)
3093 (if (not (= (length form) 4))
3094 (byte-compile-subr-wrong-args form 3)
3095 (byte-compile-form (car (cdr form))) ;; Push the arguments
3096 (byte-compile-form (nth 2 form))
3097 (byte-compile-form (nth 3 form))
3098 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3100 (defun byte-compile-zero-or-one-arg (form)
3101 (let ((len (length form)))
3102 (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
3103 ((= len 2) (byte-compile-one-arg form))
3104 (t (byte-compile-subr-wrong-args form "0-1")))))
3106 (defun byte-compile-one-or-two-args (form)
3107 (let ((len (length form)))
3108 (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
3109 ((= len 3) (byte-compile-two-args form))
3110 (t (byte-compile-subr-wrong-args form "1-2")))))
3112 (defun byte-compile-two-or-three-args (form)
3113 (let ((len (length form)))
3114 (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
3115 ((= len 4) (byte-compile-three-args form))
3116 (t (byte-compile-subr-wrong-args form "2-3")))))
3118 (defun byte-compile-noop (form)
3119 (byte-compile-constant nil))
3121 (defun byte-compile-discard ()
3122 (byte-compile-out 'byte-discard 0))
3125 ;; Compile a function that accepts one or more args and is right-associative.
3126 ;; We do it by left-associativity so that the operations
3127 ;; are done in the same order as in interpreted code.
3128 ;; We treat the one-arg case, as in (+ x), like (+ x 0).
3129 ;; in order to convert markers to numbers, and trigger expected errors.
3130 (defun byte-compile-associative (form)
3132 (let ((opcode (get (car form) 'byte-opcode))
3133 (args (copy-sequence (cdr form))))
3134 (byte-compile-form (car args))
3135 (setq args (cdr args))
3136 (or args (setq args '(0)
3137 opcode (get '+ 'byte-opcode)))
3139 (byte-compile-form arg)
3140 (byte-compile-out opcode 0)))
3141 (byte-compile-constant (eval form))))
3144 ;; more complicated compiler macros
3146 (byte-defop-compiler list)
3147 (byte-defop-compiler concat)
3148 (byte-defop-compiler fset)
3149 (byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to)
3150 (byte-defop-compiler indent-to)
3151 (byte-defop-compiler insert)
3152 (byte-defop-compiler-1 function byte-compile-function-form)
3153 (byte-defop-compiler-1 - byte-compile-minus)
3154 (byte-defop-compiler19 (/ byte-quo) byte-compile-quo)
3155 (byte-defop-compiler19 nconc)
3157 (defun byte-compile-list (form)
3158 (let ((count (length (cdr form))))
3160 (byte-compile-constant nil))
3162 (mapc 'byte-compile-form (cdr form))
3164 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0))
3165 ((and (< count 256) (not (byte-compile-version-cond
3166 byte-compile-compatibility)))
3167 (mapc 'byte-compile-form (cdr form))
3168 (byte-compile-out 'byte-listN count))
3169 (t (byte-compile-normal-call form)))))
3171 (defun byte-compile-concat (form)
3172 (let ((count (length (cdr form))))
3173 (cond ((and (< 1 count) (< count 5))
3174 (mapc 'byte-compile-form (cdr form))
3176 (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2))
3178 ;; Concat of one arg is not a no-op if arg is not a string.
3180 (byte-compile-form ""))
3181 ((and (< count 256) (not (byte-compile-version-cond
3182 byte-compile-compatibility)))
3183 (mapc 'byte-compile-form (cdr form))
3184 (byte-compile-out 'byte-concatN count))
3185 ((byte-compile-normal-call form)))))
3187 (defun byte-compile-minus (form)
3188 (if (null (setq form (cdr form)))
3189 (byte-compile-constant 0)
3190 (byte-compile-form (car form))
3192 (while (setq form (cdr form))
3193 (byte-compile-form (car form))
3194 (byte-compile-out 'byte-diff 0))
3195 (byte-compile-out 'byte-negate 0))))
3197 (defun byte-compile-quo (form)
3198 (let ((len (length form)))
3200 (byte-compile-subr-wrong-args form "2 or more"))
3202 (byte-compile-form (car (setq form (cdr form))))
3203 (while (setq form (cdr form))
3204 (byte-compile-form (car form))
3205 (byte-compile-out 'byte-quo 0))))))
3207 (defun byte-compile-nconc (form)
3208 (let ((len (length form)))
3210 (byte-compile-constant nil))
3212 ;; nconc of one arg is a noop, even if that arg isn't a list.
3213 (byte-compile-form (nth 1 form)))
3215 (byte-compile-form (car (setq form (cdr form))))
3216 (while (setq form (cdr form))
3217 (byte-compile-form (car form))
3218 (byte-compile-out 'byte-nconc 0))))))
3220 (defun byte-compile-fset (form)
3221 ;; warn about forms like (fset 'foo '(lambda () ...))
3222 ;; (where the lambda expression is non-trivial...)
3223 (let ((fn (nth 2 form))
3225 (if (and (eq (car-safe fn) 'quote)
3226 (eq (car-safe (setq fn (nth 1 fn))) 'lambda))
3228 (setq body (cdr (cdr fn)))
3229 (if (stringp (car body)) (setq body (cdr body)))
3230 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
3231 (if (and (consp (car body))
3232 (not (eq 'byte-code (car (car body)))))
3234 "A quoted lambda form is the second argument of `fset'. This is probably
3235 not what you want, as that lambda cannot be compiled. Consider using
3236 the syntax (function (lambda (...) ...)) instead.")))))
3237 (byte-compile-two-args form))
3239 (defun byte-compile-funarg (form)
3240 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
3241 ;; for cases where it's guaranteed that first arg will be used as a lambda.
3242 (byte-compile-normal-call
3243 (let ((fn (nth 1 form)))
3244 (if (and (eq (car-safe fn) 'quote)
3245 (eq (car-safe (nth 1 fn)) 'lambda))
3247 (cons (cons 'function (cdr fn))
3251 (defun byte-compile-funarg-2 (form)
3252 ;; (sort ... '(lambda (x) ..)) ==> (sort ... (function (lambda (x) ..)))
3253 ;; for cases where it's guaranteed that second arg will be used as a lambda.
3254 (byte-compile-normal-call
3255 (let ((fn (nth 2 form)))
3256 (if (and (eq (car-safe fn) 'quote)
3257 (eq (car-safe (nth 1 fn)) 'lambda))
3260 (cons (cons 'function (cdr fn))
3261 (cdr (cdr (cdr form))))))
3264 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
3265 ;; Otherwise it will be incompatible with the interpreter,
3266 ;; and (funcall (function foo)) will lose with autoloads.
3268 (defun byte-compile-function-form (form)
3269 (byte-compile-constant
3270 (cond ((symbolp (nth 1 form))
3272 ;; If we're not allowed to use #[] syntax, then output a form like
3273 ;; '(lambda (..) (byte-code ..)) instead of a call to make-byte-code.
3274 ;; In this situation, calling make-byte-code at run-time will usually
3275 ;; be less efficient than processing a call to byte-code.
3276 ((byte-compile-version-cond byte-compile-compatibility)
3277 (byte-compile-byte-code-unmake (byte-compile-lambda (nth 1 form))))
3278 ((byte-compile-lambda (nth 1 form))))))
3280 (defun byte-compile-indent-to (form)
3281 (let ((len (length form)))
3283 (byte-compile-form (car (cdr form)))
3284 (byte-compile-out 'byte-indent-to 0))
3286 ;; no opcode for 2-arg case.
3287 (byte-compile-normal-call form))
3289 (byte-compile-subr-wrong-args form "1-2")))))
3291 (defun byte-compile-insert (form)
3292 (cond ((null (cdr form))
3293 (byte-compile-constant nil))
3294 ((and (not (byte-compile-version-cond
3295 byte-compile-compatibility))
3296 (<= (length form) 256))
3297 (mapc 'byte-compile-form (cdr form))
3298 (if (cdr (cdr form))
3299 (byte-compile-out 'byte-insertN (length (cdr form)))
3300 (byte-compile-out 'byte-insert 0)))
3301 ((memq t (mapcar 'consp (cdr (cdr form))))
3302 (byte-compile-normal-call form))
3303 ;; We can split it; there is no function call after inserting 1st arg.
3305 (while (setq form (cdr form))
3306 (byte-compile-form (car form))
3307 (byte-compile-out 'byte-insert 0)
3309 (byte-compile-discard))))))
3312 (byte-defop-compiler-1 setq)
3313 (byte-defop-compiler-1 setq-default)
3314 (byte-defop-compiler-1 quote)
3315 (byte-defop-compiler-1 quote-form)
3317 (defun byte-compile-setq (form)
3318 (let ((args (cdr form)))
3321 (byte-compile-form (car (cdr args)))
3322 (or for-effect (cdr (cdr args))
3323 (byte-compile-out 'byte-dup 0))
3324 (byte-compile-variable-ref 'byte-varset (car args))
3325 (setq args (cdr (cdr args))))
3326 ;; (setq), with no arguments.
3327 (byte-compile-form nil for-effect))
3328 (setq for-effect nil)))
3330 (defun byte-compile-setq-default (form)
3331 (let ((args (cdr form))
3335 (cons (list 'set-default (list 'quote (car args)) (car (cdr args)))
3337 (setq args (cdr (cdr args))))
3338 (byte-compile-form (cons 'progn (nreverse setters)))))
3340 (defun byte-compile-quote (form)
3341 (byte-compile-constant (car (cdr form))))
3343 (defun byte-compile-quote-form (form)
3344 (byte-compile-constant (byte-compile-top-level (nth 1 form))))
3347 ;;; control structures
3349 (defun byte-compile-body (body &optional for-effect)
3351 (byte-compile-form (car body) t)
3352 (setq body (cdr body)))
3353 (byte-compile-form (car body) for-effect))
3355 (defsubst byte-compile-body-do-effect (body)
3356 (byte-compile-body body for-effect)
3357 (setq for-effect nil))
3359 (defsubst byte-compile-form-do-effect (form)
3360 (byte-compile-form form for-effect)
3361 (setq for-effect nil))
3363 (byte-defop-compiler-1 inline byte-compile-progn)
3364 (byte-defop-compiler-1 progn)
3365 (byte-defop-compiler-1 prog1)
3366 (byte-defop-compiler-1 prog2)
3367 (byte-defop-compiler-1 if)
3368 (byte-defop-compiler-1 cond)
3369 (byte-defop-compiler-1 and)
3370 (byte-defop-compiler-1 or)
3371 (byte-defop-compiler-1 while)
3372 (byte-defop-compiler-1 funcall)
3373 (byte-defop-compiler-1 apply byte-compile-funarg)
3374 (byte-defop-compiler-1 mapcar byte-compile-funarg)
3375 (byte-defop-compiler-1 mapatoms byte-compile-funarg)
3376 (byte-defop-compiler-1 mapconcat byte-compile-funarg)
3377 (byte-defop-compiler-1 mapc byte-compile-funarg)
3378 (byte-defop-compiler-1 maphash byte-compile-funarg)
3379 (byte-defop-compiler-1 map-char-table byte-compile-funarg)
3380 (byte-defop-compiler-1 sort byte-compile-funarg-2)
3381 (byte-defop-compiler-1 let)
3382 (byte-defop-compiler-1 let*)
3384 (defun byte-compile-progn (form)
3385 (byte-compile-body-do-effect (cdr form)))
3387 (defun byte-compile-prog1 (form)
3388 (byte-compile-form-do-effect (car (cdr form)))
3389 (byte-compile-body (cdr (cdr form)) t))
3391 (defun byte-compile-prog2 (form)
3392 (byte-compile-form (nth 1 form) t)
3393 (byte-compile-form-do-effect (nth 2 form))
3394 (byte-compile-body (cdr (cdr (cdr form))) t))
3396 (defmacro byte-compile-goto-if (cond discard tag)
3399 (if ,discard 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
3400 (if ,discard 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
3403 (defmacro byte-compile-maybe-guarded (condition &rest body)
3404 "Execute forms in BODY, potentially guarded by CONDITION.
3405 CONDITION is a variable whose value is a test in an `if' or `cond'.
3406 BODY is the code to compile first arm of the if or the body of the
3407 cond clause. If CONDITION's value is of the form (fboundp 'foo)
3408 or (boundp 'foo), the relevant warnings from BODY about foo's
3409 being undefined will be suppressed.
3411 If CONDITION's value is (not (featurep 'emacs)) or (featurep 'xemacs),
3412 that suppresses all warnings during execution of BODY."
3413 (declare (indent 1) (debug t))
3415 (if (eq 'fboundp (car-safe ,condition))
3416 (and (eq 'quote (car-safe (nth 1 ,condition)))
3417 ;; Ignore if the symbol is already on the
3419 (not (assq (nth 1 (nth 1 ,condition)) ; the relevant symbol
3420 byte-compile-unresolved-functions))
3421 (nth 1 (nth 1 ,condition)))))
3422 (bound (if (or (eq 'boundp (car-safe ,condition))
3423 (eq 'default-boundp (car-safe ,condition)))
3424 (and (eq 'quote (car-safe (nth 1 ,condition)))
3425 (nth 1 (nth 1 ,condition)))))
3426 ;; Maybe add to the bound list.
3427 (byte-compile-bound-variables
3429 (cons bound byte-compile-bound-variables)
3430 byte-compile-bound-variables))
3431 ;; Suppress all warnings, for code not used in Emacs.
3432 (byte-compile-warnings
3433 (if (member ,condition '((featurep 'xemacs)
3434 (not (featurep 'emacs))))
3435 nil byte-compile-warnings)))
3438 ;; Maybe remove the function symbol from the unresolved list.
3440 (setq byte-compile-unresolved-functions
3441 (delq (assq fbound byte-compile-unresolved-functions)
3442 byte-compile-unresolved-functions))))))
3444 (defun byte-compile-if (form)
3445 (byte-compile-form (car (cdr form)))
3446 ;; Check whether we have `(if (fboundp ...' or `(if (boundp ...'
3447 ;; and avoid warnings about the relevent symbols in the consequent.
3448 (let ((clause (nth 1 form))
3449 (donetag (byte-compile-make-tag)))
3450 (if (null (nthcdr 3 form))
3453 (byte-compile-goto-if nil for-effect donetag)
3454 (byte-compile-maybe-guarded clause
3455 (byte-compile-form (nth 2 form) for-effect))
3456 (byte-compile-out-tag donetag))
3457 (let ((elsetag (byte-compile-make-tag)))
3458 (byte-compile-goto 'byte-goto-if-nil elsetag)
3459 (byte-compile-maybe-guarded clause
3460 (byte-compile-form (nth 2 form) for-effect))
3461 (byte-compile-goto 'byte-goto donetag)
3462 (byte-compile-out-tag elsetag)
3463 (byte-compile-maybe-guarded (list 'not clause)
3464 (byte-compile-body (cdr (cdr (cdr form))) for-effect))
3465 (byte-compile-out-tag donetag))))
3466 (setq for-effect nil))
3468 (defun byte-compile-cond (clauses)
3469 (let ((donetag (byte-compile-make-tag))
3471 (while (setq clauses (cdr clauses))
3472 (setq clause (car clauses))
3473 (cond ((or (eq (car clause) t)
3474 (and (eq (car-safe (car clause)) 'quote)
3475 (car-safe (cdr-safe (car clause)))))
3476 ;; Unconditional clause
3477 (setq clause (cons t clause)
3480 (byte-compile-form (car clause))
3481 (if (null (cdr clause))
3482 ;; First clause is a singleton.
3483 (byte-compile-goto-if t for-effect donetag)
3484 (setq nexttag (byte-compile-make-tag))
3485 (byte-compile-goto 'byte-goto-if-nil nexttag)
3486 (byte-compile-maybe-guarded (car clause)
3487 (byte-compile-body (cdr clause) for-effect))
3488 (byte-compile-goto 'byte-goto donetag)
3489 (byte-compile-out-tag nexttag)))))
3491 (let ((guard (car clause)))
3492 (and (cdr clause) (not (eq guard t))
3493 (progn (byte-compile-form guard)
3494 (byte-compile-goto-if nil for-effect donetag)
3495 (setq clause (cdr clause))))
3496 (byte-compile-maybe-guarded guard
3497 (byte-compile-body-do-effect clause)))
3498 (byte-compile-out-tag donetag)))
3500 (defun byte-compile-and (form)
3501 (let ((failtag (byte-compile-make-tag))
3504 (byte-compile-form-do-effect t)
3505 (byte-compile-and-recursion args failtag))))
3507 ;; Handle compilation of a nontrivial `and' call.
3508 ;; We use tail recursion so we can use byte-compile-maybe-guarded.
3509 (defun byte-compile-and-recursion (rest failtag)
3512 (byte-compile-form (car rest))
3513 (byte-compile-goto-if nil for-effect failtag)
3514 (byte-compile-maybe-guarded (car rest)
3515 (byte-compile-and-recursion (cdr rest) failtag)))
3516 (byte-compile-form-do-effect (car rest))
3517 (byte-compile-out-tag failtag)))
3519 (defun byte-compile-or (form)
3520 (let ((wintag (byte-compile-make-tag))
3523 (byte-compile-form-do-effect nil)
3524 (byte-compile-or-recursion args wintag))))
3526 ;; Handle compilation of a nontrivial `or' call.
3527 ;; We use tail recursion so we can use byte-compile-maybe-guarded.
3528 (defun byte-compile-or-recursion (rest wintag)
3531 (byte-compile-form (car rest))
3532 (byte-compile-goto-if t for-effect wintag)
3533 (byte-compile-maybe-guarded (list 'not (car rest))
3534 (byte-compile-or-recursion (cdr rest) wintag)))
3535 (byte-compile-form-do-effect (car rest))
3536 (byte-compile-out-tag wintag)))
3538 (defun byte-compile-while (form)
3539 (let ((endtag (byte-compile-make-tag))
3540 (looptag (byte-compile-make-tag)))
3541 (byte-compile-out-tag looptag)
3542 (byte-compile-form (car (cdr form)))
3543 (byte-compile-goto-if nil for-effect endtag)
3544 (byte-compile-body (cdr (cdr form)) t)
3545 (byte-compile-goto 'byte-goto looptag)
3546 (byte-compile-out-tag endtag)
3547 (setq for-effect nil)))
3549 (defun byte-compile-funcall (form)
3550 (mapc 'byte-compile-form (cdr form))
3551 (byte-compile-out 'byte-call (length (cdr (cdr form)))))
3554 (defun byte-compile-let (form)
3555 ;; First compute the binding values in the old scope.
3556 (let ((varlist (car (cdr form))))
3557 (dolist (var varlist)
3559 (byte-compile-form (car (cdr var)))
3560 (byte-compile-push-constant nil))))
3561 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
3562 (varlist (reverse (car (cdr form)))))
3563 (dolist (var varlist)
3564 (byte-compile-variable-ref 'byte-varbind (if (consp var) (car var) var)))
3565 (byte-compile-body-do-effect (cdr (cdr form)))
3566 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
3568 (defun byte-compile-let* (form)
3569 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
3570 (varlist (copy-sequence (car (cdr form)))))
3571 (dolist (var varlist)
3573 (byte-compile-push-constant nil)
3574 (byte-compile-form (car (cdr var)))
3575 (setq var (car var)))
3576 (byte-compile-variable-ref 'byte-varbind var))
3577 (byte-compile-body-do-effect (cdr (cdr form)))
3578 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
3581 (byte-defop-compiler-1 /= byte-compile-negated)
3582 (byte-defop-compiler-1 atom byte-compile-negated)
3583 (byte-defop-compiler-1 nlistp byte-compile-negated)
3585 (put '/= 'byte-compile-negated-op '=)
3586 (put 'atom 'byte-compile-negated-op 'consp)
3587 (put 'nlistp 'byte-compile-negated-op 'listp)
3589 (defun byte-compile-negated (form)
3590 (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
3592 ;; Even when optimization is off, /= is optimized to (not (= ...)).
3593 (defun byte-compile-negation-optimizer (form)
3594 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
3595 (byte-compile-set-symbol-position (car form))
3597 (cons (or (get (car form) 'byte-compile-negated-op)
3599 "Compiler error: `%s' has no `byte-compile-negated-op' property"
3603 ;;; other tricky macro-like special-forms
3605 (byte-defop-compiler-1 catch)
3606 (byte-defop-compiler-1 unwind-protect)
3607 (byte-defop-compiler-1 condition-case)
3608 (byte-defop-compiler-1 save-excursion)
3609 (byte-defop-compiler-1 save-current-buffer)
3610 (byte-defop-compiler-1 save-restriction)
3611 (byte-defop-compiler-1 save-window-excursion)
3612 (byte-defop-compiler-1 with-output-to-temp-buffer)
3613 (byte-defop-compiler-1 track-mouse)
3615 (defun byte-compile-catch (form)
3616 (byte-compile-form (car (cdr form)))
3617 (byte-compile-push-constant
3618 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect))
3619 (byte-compile-out 'byte-catch 0))
3621 (defun byte-compile-unwind-protect (form)
3622 (byte-compile-push-constant
3623 (byte-compile-top-level-body (cdr (cdr form)) t))
3624 (byte-compile-out 'byte-unwind-protect 0)
3625 (byte-compile-form-do-effect (car (cdr form)))
3626 (byte-compile-out 'byte-unbind 1))
3628 (defun byte-compile-track-mouse (form)
3630 `(funcall '(lambda nil
3631 (track-mouse ,@(byte-compile-top-level-body (cdr form)))))))
3633 (defun byte-compile-condition-case (form)
3634 (let* ((var (nth 1 form))
3635 (byte-compile-bound-variables
3636 (if var (cons var byte-compile-bound-variables)
3637 byte-compile-bound-variables)))
3638 (byte-compile-set-symbol-position 'condition-case)
3639 (unless (symbolp var)
3641 "`%s' is not a variable-name or nil (in condition-case)" var))
3642 (byte-compile-push-constant var)
3643 (byte-compile-push-constant (byte-compile-top-level
3644 (nth 2 form) for-effect))
3645 (let ((clauses (cdr (cdr (cdr form))))
3648 (let* ((clause (car clauses))
3649 (condition (car clause)))
3650 (cond ((not (or (symbolp condition)
3651 (and (listp condition)
3652 (let ((syms condition) (ok t))
3654 (if (not (symbolp (car syms)))
3656 (setq syms (cdr syms)))
3659 "`%s' is not a condition name or list of such (in condition-case)"
3660 (prin1-to-string condition)))
3661 ;; ((not (or (eq condition 't)
3662 ;; (and (stringp (get condition 'error-message))
3663 ;; (consp (get condition 'error-conditions)))))
3664 ;; (byte-compile-warn
3665 ;; "`%s' is not a known condition name (in condition-case)"
3668 (setq compiled-clauses
3669 (cons (cons condition
3670 (byte-compile-top-level-body
3671 (cdr clause) for-effect))
3673 (setq clauses (cdr clauses)))
3674 (byte-compile-push-constant (nreverse compiled-clauses)))
3675 (byte-compile-out 'byte-condition-case 0)))
3678 (defun byte-compile-save-excursion (form)
3679 (byte-compile-out 'byte-save-excursion 0)
3680 (byte-compile-body-do-effect (cdr form))
3681 (byte-compile-out 'byte-unbind 1))
3683 (defun byte-compile-save-restriction (form)
3684 (byte-compile-out 'byte-save-restriction 0)
3685 (byte-compile-body-do-effect (cdr form))
3686 (byte-compile-out 'byte-unbind 1))
3688 (defun byte-compile-save-current-buffer (form)
3689 (byte-compile-out 'byte-save-current-buffer 0)
3690 (byte-compile-body-do-effect (cdr form))
3691 (byte-compile-out 'byte-unbind 1))
3693 (defun byte-compile-save-window-excursion (form)
3694 (byte-compile-push-constant
3695 (byte-compile-top-level-body (cdr form) for-effect))
3696 (byte-compile-out 'byte-save-window-excursion 0))
3698 (defun byte-compile-with-output-to-temp-buffer (form)
3699 (byte-compile-form (car (cdr form)))
3700 (byte-compile-out 'byte-temp-output-buffer-setup 0)
3701 (byte-compile-body (cdr (cdr form)))
3702 (byte-compile-out 'byte-temp-output-buffer-show 0))
3704 ;;; top-level forms elsewhere
3706 (byte-defop-compiler-1 defun)
3707 (byte-defop-compiler-1 defmacro)
3708 (byte-defop-compiler-1 defvar)
3709 (byte-defop-compiler-1 defconst byte-compile-defvar)
3710 (byte-defop-compiler-1 autoload)
3711 (byte-defop-compiler-1 lambda byte-compile-lambda-form)
3713 (defun byte-compile-defun (form)
3714 ;; This is not used for file-level defuns with doc strings.
3715 (if (symbolp (car form))
3716 (byte-compile-set-symbol-position (car form))
3717 (byte-compile-set-symbol-position 'defun)
3718 (error "defun name must be a symbol, not %s" (car form)))
3719 (if (byte-compile-version-cond byte-compile-compatibility)
3721 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning.
3723 (list 'quote (nth 1 form))
3724 (byte-compile-byte-code-maker
3725 (byte-compile-lambda (cdr (cdr form)) t))))
3726 (byte-compile-discard))
3727 ;; We prefer to generate a defalias form so it will record the function
3728 ;; definition just like interpreting a defun.
3731 (list 'quote (nth 1 form))
3732 (byte-compile-byte-code-maker
3733 (byte-compile-lambda (cdr (cdr form)) t)))
3735 (byte-compile-constant (nth 1 form)))
3737 (defun byte-compile-defmacro (form)
3738 ;; This is not used for file-level defmacros with doc strings.
3739 (byte-compile-body-do-effect
3740 (list (list 'fset (list 'quote (nth 1 form))
3741 (let ((code (byte-compile-byte-code-maker
3742 (byte-compile-lambda (cdr (cdr form)) t))))
3743 (if (eq (car-safe code) 'make-byte-code)
3744 (list 'cons ''macro code)
3745 (list 'quote (cons 'macro (eval code))))))
3746 (list 'quote (nth 1 form)))))
3748 (defun byte-compile-defvar (form)
3749 ;; This is not used for file-level defvar/consts with doc strings.
3750 (let ((fun (nth 0 form))
3752 (value (nth 2 form))
3753 (string (nth 3 form)))
3754 (byte-compile-set-symbol-position fun)
3755 (when (or (> (length form) 4)
3756 (and (eq fun 'defconst) (null (cddr form))))
3757 (let ((ncall (length (cdr form))))
3759 "`%s' called with %d argument%s, but %s %s"
3761 (if (= 1 ncall) "" "s")
3762 (if (< ncall 2) "requires" "accepts only")
3764 (when (memq 'free-vars byte-compile-warnings)
3765 (push var byte-compile-bound-variables)
3766 (if (eq fun 'defconst)
3767 (push var byte-compile-const-variables)))
3768 (byte-compile-body-do-effect
3770 ;; Put the defined variable in this library's load-history entry
3771 ;; just as a real defvar would, but only in top-level forms.
3772 (when (and (cddr form) (null byte-compile-current-form))
3773 `(push ',var current-load-list))
3774 (when (> (length form) 3)
3775 (when (and string (not (stringp string)))
3776 (byte-compile-warn "third arg to `%s %s' is not a string: %s"
3778 `(put ',var 'variable-documentation ,string))
3779 (if (cddr form) ; `value' provided
3780 (let ((byte-compile-not-obsolete-var var))
3781 (if (eq fun 'defconst)
3782 ;; `defconst' sets `var' unconditionally.
3783 (let ((tmp (make-symbol "defconst-tmp-var")))
3784 `(funcall '(lambda (,tmp) (defconst ,var ,tmp))
3786 ;; `defvar' sets `var' only when unbound.
3787 `(if (not (default-boundp ',var)) (setq-default ,var ,value))))
3788 (when (eq fun 'defconst)
3789 ;; This will signal an appropriate error at runtime.
3793 (defun byte-compile-autoload (form)
3794 (byte-compile-set-symbol-position 'autoload)
3795 (and (byte-compile-constp (nth 1 form))
3796 (byte-compile-constp (nth 5 form))
3797 (eval (nth 5 form)) ; macro-p
3798 (not (fboundp (eval (nth 1 form))))
3800 "The compiler ignores `autoload' except at top level. You should
3801 probably put the autoload of the macro `%s' at top-level."
3802 (eval (nth 1 form))))
3803 (byte-compile-normal-call form))
3805 ;; Lambdas in valid places are handled as special cases by various code.
3806 ;; The ones that remain are errors.
3807 (defun byte-compile-lambda-form (form)
3808 (byte-compile-set-symbol-position 'lambda)
3809 (error "`lambda' used as function name is invalid"))
3811 ;; Compile normally, but deal with warnings for the function being defined.
3812 (put 'defalias 'byte-hunk-handler 'byte-compile-file-form-defalias)
3813 (defun byte-compile-file-form-defalias (form)
3814 (if (and (consp (cdr form)) (consp (nth 1 form))
3815 (eq (car (nth 1 form)) 'quote)
3816 (consp (cdr (nth 1 form)))
3817 (symbolp (nth 1 (nth 1 form))))
3819 (and (consp (nthcdr 2 form))
3820 (consp (nth 2 form))
3821 (eq (car (nth 2 form)) 'quote)
3822 (consp (cdr (nth 2 form)))
3823 (symbolp (nth 1 (nth 2 form))))))
3824 (byte-compile-defalias-warn (nth 1 (nth 1 form)))
3825 (push (cons (nth 1 (nth 1 form))
3826 (if constant (nth 1 (nth 2 form)) t))
3827 byte-compile-function-environment)))
3828 ;; We used to jus do: (byte-compile-normal-call form)
3829 ;; But it turns out that this fails to optimize the code.
3830 ;; So instead we now do the same as what other byte-hunk-handlers do,
3831 ;; which is to call back byte-compile-file-form and then return nil.
3832 ;; Except that we can't just call byte-compile-file-form since it would
3833 ;; call us right back.
3834 (byte-compile-keep-pending form)
3835 ;; Return nil so the form is not output twice.
3838 ;; Turn off warnings about prior calls to the function being defalias'd.
3839 ;; This could be smarter and compare those calls with
3840 ;; the function it is being aliased to.
3841 (defun byte-compile-defalias-warn (new)
3842 (let ((calls (assq new byte-compile-unresolved-functions)))
3844 (setq byte-compile-unresolved-functions
3845 (delq calls byte-compile-unresolved-functions)))))
3847 (byte-defop-compiler-1 with-no-warnings byte-compile-no-warnings)
3848 (defun byte-compile-no-warnings (form)
3849 (let (byte-compile-warnings)
3850 (byte-compile-form (cons 'progn (cdr form)))))
3852 ;; Warn about misuses of make-variable-buffer-local.
3853 (byte-defop-compiler-1 make-variable-buffer-local byte-compile-make-variable-buffer-local)
3854 (defun byte-compile-make-variable-buffer-local (form)
3855 (if (eq (car-safe (car-safe (cdr-safe form))) 'quote)
3857 "`make-variable-buffer-local' should be called at toplevel"))
3858 (byte-compile-normal-call form))
3859 (put 'make-variable-buffer-local
3860 'byte-hunk-handler 'byte-compile-form-make-variable-buffer-local)
3861 (defun byte-compile-form-make-variable-buffer-local (form)
3862 (byte-compile-keep-pending form 'byte-compile-normal-call))
3867 ;; Note: Most operations will strip off the 'TAG, but it speeds up
3868 ;; optimization to have the 'TAG as a part of the tag.
3869 ;; Tags will be (TAG . (tag-number . stack-depth)).
3870 (defun byte-compile-make-tag ()
3871 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
3874 (defun byte-compile-out-tag (tag)
3875 (setq byte-compile-output (cons tag byte-compile-output))
3878 ;; ## remove this someday
3879 (and byte-compile-depth
3880 (not (= (cdr (cdr tag)) byte-compile-depth))
3881 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
3882 (setq byte-compile-depth (cdr (cdr tag))))
3883 (setcdr (cdr tag) byte-compile-depth)))
3885 (defun byte-compile-goto (opcode tag)
3886 (push (cons opcode tag) byte-compile-output)
3887 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
3888 (1- byte-compile-depth)
3889 byte-compile-depth))
3890 (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
3891 (1- byte-compile-depth))))
3893 (defun byte-compile-out (opcode offset)
3894 (push (cons opcode offset) byte-compile-output)
3895 (cond ((eq opcode 'byte-call)
3896 (setq byte-compile-depth (- byte-compile-depth offset)))
3897 ((eq opcode 'byte-return)
3898 ;; This is actually an unnecessary case, because there should be
3899 ;; no more opcodes behind byte-return.
3900 (setq byte-compile-depth nil))
3902 (setq byte-compile-depth (+ byte-compile-depth
3903 (or (aref byte-stack+-info
3904 (symbol-value opcode))
3906 byte-compile-maxdepth (max byte-compile-depth
3907 byte-compile-maxdepth))))
3908 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
3914 (defun byte-compile-annotate-call-tree (form)
3916 ;; annotate the current call
3917 (if (setq entry (assq (car form) byte-compile-call-tree))
3918 (or (memq byte-compile-current-form (nth 1 entry)) ;callers
3920 (cons byte-compile-current-form (nth 1 entry))))
3921 (setq byte-compile-call-tree
3922 (cons (list (car form) (list byte-compile-current-form) nil)
3923 byte-compile-call-tree)))
3924 ;; annotate the current function
3925 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
3926 (or (memq (car form) (nth 2 entry)) ;called
3927 (setcar (cdr (cdr entry))
3928 (cons (car form) (nth 2 entry))))
3929 (setq byte-compile-call-tree
3930 (cons (list byte-compile-current-form nil (list (car form)))
3931 byte-compile-call-tree)))
3934 ;; Renamed from byte-compile-report-call-tree
3935 ;; to avoid interfering with completion of byte-compile-file.
3937 (defun display-call-tree (&optional filename)
3938 "Display a call graph of a specified file.
3939 This lists which functions have been called, what functions called
3940 them, and what functions they call. The list includes all functions
3941 whose definitions have been compiled in this Emacs session, as well as
3942 all functions called by those functions.
3944 The call graph does not include macros, inline functions, or
3945 primitives that the byte-code interpreter knows about directly \(eq,
3948 The call tree also lists those functions which are not known to be called
3949 \(that is, to which no calls have been compiled\), and which cannot be
3950 invoked interactively."
3952 (message "Generating call tree...")
3953 (with-output-to-temp-buffer "*Call-Tree*"
3954 (set-buffer "*Call-Tree*")
3956 (message "Generating call tree... (sorting on %s)"
3957 byte-compile-call-tree-sort)
3958 (insert "Call tree for "
3959 (cond ((null byte-compile-current-file) (or filename "???"))
3960 ((stringp byte-compile-current-file)
3961 byte-compile-current-file)
3962 (t (buffer-name byte-compile-current-file)))
3964 (prin1-to-string byte-compile-call-tree-sort)
3966 (if byte-compile-call-tree-sort
3967 (setq byte-compile-call-tree
3968 (sort byte-compile-call-tree
3969 (cond ((eq byte-compile-call-tree-sort 'callers)
3970 (function (lambda (x y) (< (length (nth 1 x))
3971 (length (nth 1 y))))))
3972 ((eq byte-compile-call-tree-sort 'calls)
3973 (function (lambda (x y) (< (length (nth 2 x))
3974 (length (nth 2 y))))))
3975 ((eq byte-compile-call-tree-sort 'calls+callers)
3976 (function (lambda (x y) (< (+ (length (nth 1 x))
3978 (+ (length (nth 1 y))
3979 (length (nth 2 y)))))))
3980 ((eq byte-compile-call-tree-sort 'name)
3981 (function (lambda (x y) (string< (car x)
3983 (t (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
3984 byte-compile-call-tree-sort))))))
3985 (message "Generating call tree...")
3986 (let ((rest byte-compile-call-tree)
3987 (b (current-buffer))
3991 (prin1 (car (car rest)) b)
3992 (setq callers (nth 1 (car rest))
3993 calls (nth 2 (car rest)))
3995 (cond ((not (fboundp (setq f (car (car rest)))))
3997 " <top level>";; shouldn't insert nil then, actually -sk
3999 ((subrp (setq f (symbol-function f)))
4002 (format " ==> %s" f))
4003 ((byte-code-function-p f)
4004 "<compiled function>")
4006 "<malformed function>")
4007 ((eq 'macro (car f))
4008 (if (or (byte-code-function-p (cdr f))
4009 (assq 'byte-code (cdr (cdr (cdr f)))))
4012 ((assq 'byte-code (cdr (cdr f)))
4013 "<compiled lambda>")
4014 ((eq 'lambda (car f))
4017 (format " (%d callers + %d calls = %d)"
4018 ;; Does the optimizer eliminate common subexpressions?-sk
4021 (+ (length callers) (length calls)))
4025 (insert " called by:\n")
4027 (insert " " (if (car callers)
4028 (mapconcat 'symbol-name callers ", ")
4030 (let ((fill-prefix " "))
4031 (fill-region-as-paragraph p (point)))
4032 (unless (= 0 (current-column))
4036 (insert " calls:\n")
4038 (insert " " (mapconcat 'symbol-name calls ", "))
4039 (let ((fill-prefix " "))
4040 (fill-region-as-paragraph p (point)))
4041 (unless (= 0 (current-column))
4043 (setq rest (cdr rest)))
4045 (message "Generating call tree...(finding uncalled functions...)")
4046 (setq rest byte-compile-call-tree)
4047 (let ((uncalled nil))
4049 (or (nth 1 (car rest))
4050 (null (setq f (car (car rest))))
4051 (functionp (byte-compile-fdefinition f t))
4052 (commandp (byte-compile-fdefinition f nil))
4053 (setq uncalled (cons f uncalled)))
4054 (setq rest (cdr rest)))
4056 (let ((fill-prefix " "))
4057 (insert "Noninteractive functions not known to be called:\n ")
4059 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
4060 (fill-region-as-paragraph p (point)))))
4062 (message "Generating call tree...done.")
4067 (defun batch-byte-compile-if-not-done ()
4068 "Like `byte-compile-file' but doesn't recompile if already up to date.
4069 Use this from the command line, with `-batch';
4070 it won't work in an interactive Emacs."
4071 (batch-byte-compile t))
4073 ;;; by crl@newton.purdue.edu
4074 ;;; Only works noninteractively.
4076 (defun batch-byte-compile (&optional noforce)
4077 "Run `byte-compile-file' on the files remaining on the command line.
4078 Use this from the command line, with `-batch';
4079 it won't work in an interactive Emacs.
4080 Each file is processed even if an error occurred previously.
4081 For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\".
4082 If NOFORCE is non-nil, don't recompile a file that seems to be
4083 already up-to-date."
4084 ;; command-line-args-left is what is left of the command line (from startup.el)
4085 (defvar command-line-args-left) ;Avoid 'free variable' warning
4086 (if (not noninteractive)
4087 (error "`batch-byte-compile' is to be used only with -batch"))
4089 (while command-line-args-left
4090 (if (file-directory-p (expand-file-name (car command-line-args-left)))
4091 ;; Directory as argument.
4092 (let ((files (directory-files (car command-line-args-left)))
4094 (dolist (file files)
4095 (if (and (string-match emacs-lisp-file-regexp file)
4096 (not (auto-save-file-name-p file))
4097 (setq source (expand-file-name file
4098 (car command-line-args-left)))
4099 (setq dest (byte-compile-dest-file source))
4100 (file-exists-p dest)
4101 (file-newer-than-file-p source dest))
4102 (if (null (batch-byte-compile-file source))
4104 ;; Specific file argument
4105 (if (or (not noforce)
4106 (let* ((source (car command-line-args-left))
4107 (dest (byte-compile-dest-file source)))
4108 (or (not (file-exists-p dest))
4109 (file-newer-than-file-p source dest))))
4110 (if (null (batch-byte-compile-file (car command-line-args-left)))
4112 (setq command-line-args-left (cdr command-line-args-left)))
4113 (kill-emacs (if error 1 0))))
4115 (defun batch-byte-compile-file (file)
4117 (byte-compile-file file)
4119 (byte-compile-file file)
4121 (message (if (cdr err)
4122 ">>Error occurred processing %s: %s (%s)"
4123 ">>Error occurred processing %s: %s")
4125 (get (car err) 'error-message)
4126 (prin1-to-string (cdr err)))
4127 (let ((destfile (byte-compile-dest-file file)))
4128 (if (file-exists-p destfile)
4129 (delete-file destfile)))
4132 (message (if (cdr err)
4133 ">>Error occurred processing %s: %s (%s)"
4134 ">>Error occurred processing %s: %s")
4136 (get (car err) 'error-message)
4137 (prin1-to-string (cdr err)))
4141 (defun batch-byte-recompile-directory (&optional arg)
4142 "Run `byte-recompile-directory' on the dirs remaining on the command line.
4143 Must be used only with `-batch', and kills Emacs on completion.
4144 For example, invoke `emacs -batch -f batch-byte-recompile-directory .'.
4146 Optional argument ARG is passed as second argument ARG to
4147 `batch-recompile-directory'; see there for its possible values
4148 and corresponding effects."
4149 ;; command-line-args-left is what is left of the command line (startup.el)
4150 (defvar command-line-args-left) ;Avoid 'free variable' warning
4151 (if (not noninteractive)
4152 (error "batch-byte-recompile-directory is to be used only with -batch"))
4153 (or command-line-args-left
4154 (setq command-line-args-left '(".")))
4155 (while command-line-args-left
4156 (byte-recompile-directory (car command-line-args-left) arg)
4157 (setq command-line-args-left (cdr command-line-args-left)))
4160 (provide 'byte-compile)
4164 ;;; report metering (see the hacks in bytecode.c)
4166 (defvar byte-code-meter)
4167 (defun byte-compile-report-ops ()
4168 (with-output-to-temp-buffer "*Meter*"
4169 (set-buffer "*Meter*")
4170 (let ((i 0) n op off)
4172 (setq n (aref (aref byte-code-meter 0) i)
4174 (if t ;(not (zerop n))
4178 (cond ((< op byte-nth)
4179 (setq off (logand op 7))
4180 (setq op (logand op 248)))
4181 ((>= op byte-constant)
4182 (setq off (- op byte-constant)
4184 (setq op (aref byte-code-vector op))
4185 (insert (format "%-4d" i))
4186 (insert (symbol-name op))
4187 (if off (insert " [" (int-to-string off) "]"))
4189 (insert (int-to-string n) "\n")))
4192 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
4193 ;; itself, compile some of its most used recursive functions (at load time).
4196 (or (byte-code-function-p (symbol-function 'byte-compile-form))
4197 (assq 'byte-code (symbol-function 'byte-compile-form))
4198 (let ((byte-optimize nil) ; do it fast
4199 (byte-compile-warnings nil))
4201 (or noninteractive (message "compiling %s..." x))
4203 (or noninteractive (message "compiling %s...done" x)))
4204 '(byte-compile-normal-call
4207 ;; Inserted some more than necessary, to speed it up.
4208 byte-compile-top-level
4209 byte-compile-out-toplevel
4210 byte-compile-constant
4211 byte-compile-variable-ref))))
4214 (run-hooks 'bytecomp-load-hook)
4216 ;; arch-tag: 9c97b0f0-8745-4571-bfc3-8dceb677292a
4217 ;;; bytecomp.el ends here