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 3, 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-functions' (calls to CL functions)
102 ;; `interactive-only' (calls to commands that are
103 ;; not good to call from Lisp)
104 ;; `make-local' (dubious calls to
105 ;; `make-variable-buffer-local')
106 ;; byte-compile-compatibility Whether the compiler should
107 ;; generate .elc files which can be loaded into
109 ;; emacs-lisp-file-regexp Regexp for the extension of source-files;
110 ;; see also the function byte-compile-dest-file.
114 ;; o The form `defsubst' is just like `defun', except that the function
115 ;; generated will be open-coded in compiled code which uses it. This
116 ;; means that no function call will be generated, it will simply be
117 ;; spliced in. Lisp functions calls are very slow, so this can be a
120 ;; You can generally accomplish the same thing with `defmacro', but in
121 ;; that case, the defined procedure can't be used as an argument to
124 ;; o You can also open-code one particular call to a function without
125 ;; open-coding all calls. Use the 'inline' form to do this, like so:
127 ;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
129 ;; (inline ;; `foo' and `baz' will be
130 ;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
133 ;; o It is possible to open-code a function in the same file it is defined
134 ;; in without having to load that file before compiling it. The
135 ;; byte-compiler has been modified to remember function definitions in
136 ;; the compilation environment in the same way that it remembers macro
139 ;; o Forms like ((lambda ...) ...) are open-coded.
141 ;; o The form `eval-when-compile' is like progn, except that the body
142 ;; is evaluated at compile-time. When it appears at top-level, this
143 ;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
144 ;; When it does not appear at top-level, it is similar to the
145 ;; Common Lisp #. reader macro (but not in interpreted code).
147 ;; o The form `eval-and-compile' is similar to eval-when-compile, but
148 ;; the whole form is evalled both at compile-time and at run-time.
150 ;; o The command compile-defun is analogous to eval-defun.
152 ;; o If you run byte-compile-file on a filename which is visited in a
153 ;; buffer, and that buffer is modified, you are asked whether you want
154 ;; to save the buffer before compiling.
156 ;; o byte-compiled files now start with the string `;ELC'.
157 ;; Some versions of `file' can be customized to recognize that.
161 (or (fboundp 'defsubst
)
162 ;; This really ought to be loaded already!
165 ;; The feature of compiling in a specific target Emacs version
166 ;; has been turned off because compile time options are a bad idea.
167 (defmacro byte-compile-single-version
() nil
)
168 (defmacro byte-compile-version-cond
(cond) cond
)
170 ;; The crud you see scattered through this file of the form
171 ;; (or (and (boundp 'epoch::version) epoch::version)
172 ;; (string-lessp emacs-version "19"))
173 ;; is because the Epoch folks couldn't be bothered to follow the
174 ;; normal emacs version numbering convention.
176 ;; (if (byte-compile-version-cond
177 ;; (or (and (boundp 'epoch::version) epoch::version)
178 ;; (string-lessp emacs-version "19")))
180 ;; ;; emacs-18 compatibility.
181 ;; (defvar baud-rate (baud-rate)) ;Define baud-rate if it's undefined
183 ;; (if (byte-compile-single-version)
184 ;; (defmacro byte-code-function-p (x) "Emacs 18 doesn't have these." nil)
185 ;; (defun byte-code-function-p (x) "Emacs 18 doesn't have these." nil))
187 ;; (or (and (fboundp 'member)
188 ;; ;; avoid using someone else's possibly bogus definition of this.
189 ;; (subrp (symbol-function 'member)))
190 ;; (defun member (elt list)
191 ;; "like memq, but uses equal instead of eq. In v19, this is a subr."
192 ;; (while (and list (not (equal elt (car list))))
193 ;; (setq list (cdr list)))
197 (defgroup bytecomp nil
198 "Emacs Lisp byte-compiler."
201 (defcustom emacs-lisp-file-regexp
(if (eq system-type
'vax-vms
)
202 "\\.EL\\(;[0-9]+\\)?$"
204 "*Regexp which matches Emacs Lisp source files.
205 You may want to redefine the function `byte-compile-dest-file'
206 if you change this variable."
210 ;; This enables file name handlers such as jka-compr
211 ;; to remove parts of the file name that should not be copied
212 ;; through to the output file name.
213 (defun byte-compiler-base-file-name (filename)
214 (let ((handler (find-file-name-handler filename
215 'byte-compiler-base-file-name
)))
217 (funcall handler
'byte-compiler-base-file-name filename
)
220 (or (fboundp 'byte-compile-dest-file
)
221 ;; The user may want to redefine this along with emacs-lisp-file-regexp,
222 ;; so only define it if it is undefined.
223 (defun byte-compile-dest-file (filename)
224 "Convert an Emacs Lisp source file name to a compiled file name.
225 If FILENAME matches `emacs-lisp-file-regexp' (by default, files
226 with the extension `.el'), add `c' to it; otherwise add `.elc'."
227 (setq filename
(byte-compiler-base-file-name filename
))
228 (setq filename
(file-name-sans-versions filename
))
229 (cond ((eq system-type
'vax-vms
)
230 (concat (substring filename
0 (string-match ";" filename
)) "c"))
231 ((string-match emacs-lisp-file-regexp filename
)
232 (concat (substring filename
0 (match-beginning 0)) ".elc"))
233 (t (concat filename
".elc")))))
235 ;; This can be the 'byte-compile property of any symbol.
236 (autoload 'byte-compile-inline-expand
"byte-opt")
238 ;; This is the entrypoint to the lapcode optimizer pass1.
239 (autoload 'byte-optimize-form
"byte-opt")
240 ;; This is the entrypoint to the lapcode optimizer pass2.
241 (autoload 'byte-optimize-lapcode
"byte-opt")
242 (autoload 'byte-compile-unfold-lambda
"byte-opt")
244 ;; This is the entry point to the decompiler, which is used by the
245 ;; disassembler. The disassembler just requires 'byte-compile, but
246 ;; that doesn't define this function, so this seems to be a reasonable
248 (autoload 'byte-decompile-bytecode
"byte-opt")
250 (defcustom byte-compile-verbose
251 (and (not noninteractive
) (> baud-rate search-slow-speed
))
252 "*Non-nil means print messages describing progress of byte-compiler."
256 (defcustom byte-compile-compatibility nil
257 "*Non-nil means generate output that can run in Emacs 18.
258 This only means that it can run in principle, if it doesn't require
259 facilities that have been added more recently."
263 ;; (defvar byte-compile-generate-emacs19-bytecodes
264 ;; (not (or (and (boundp 'epoch::version) epoch::version)
265 ;; (string-lessp emacs-version "19")))
266 ;; "*If this is true, then the byte-compiler will generate bytecode which
267 ;; makes use of byte-ops which are present only in Emacs 19. Code generated
268 ;; this way can never be run in Emacs 18, and may even cause it to crash.")
270 (defcustom byte-optimize t
271 "*Enable optimization in the byte compiler.
273 nil - no optimization
274 t - all optimizations
275 `source' - source-level optimizations only
276 `byte' - code-level optimizations only"
278 :type
'(choice (const :tag
"none" nil
)
280 (const :tag
"source-level" source
)
281 (const :tag
"byte-level" byte
)))
283 (defcustom byte-compile-delete-errors nil
284 "*If non-nil, the optimizer may delete forms that may signal an error.
285 This includes variable references and calls to functions such as `car'."
289 (defvar byte-compile-dynamic nil
290 "If non-nil, compile function bodies so they load lazily.
291 They are hidden in comments in the compiled file,
292 and each one is brought into core when the
295 To enable this option, make it a file-local variable
296 in the source file you want it to apply to.
297 For example, add -*-byte-compile-dynamic: t;-*- on the first line.
299 When this option is true, if you load the compiled file and then move it,
300 the functions you loaded will not be able to run.")
301 ;;;###autoload(put 'byte-compile-dynamic 'safe-local-variable 'booleanp)
303 (defvar byte-compile-disable-print-circle nil
304 "If non-nil, disable `print-circle' on printing a byte-compiled code.")
305 ;;;###autoload(put 'byte-compile-disable-print-circle 'safe-local-variable 'booleanp)
307 (defcustom byte-compile-dynamic-docstrings t
308 "*If non-nil, compile doc strings for lazy access.
309 We bury the doc strings of functions and variables
310 inside comments in the file, and bring them into core only when they
313 When this option is true, if you load the compiled file and then move it,
314 you won't be able to find the documentation of anything in that file.
316 To disable this option for a certain file, make it a file-local variable
317 in the source file. For example, add this to the first line:
318 -*-byte-compile-dynamic-docstrings:nil;-*-
319 You can also set the variable globally.
321 This option is enabled by default because it reduces Emacs memory usage."
324 ;;;###autoload(put 'byte-compile-dynamic-docstrings 'safe-local-variable 'booleanp)
326 (defcustom byte-optimize-log nil
327 "*If true, the byte-compiler will log its optimizations into *Compile-Log*.
328 If this is 'source, then only source-level optimizations will be logged.
329 If it is 'byte, then only byte-level optimizations will be logged."
331 :type
'(choice (const :tag
"none" nil
)
333 (const :tag
"source-level" source
)
334 (const :tag
"byte-level" byte
)))
336 (defcustom byte-compile-error-on-warn nil
337 "*If true, the byte-compiler reports warnings with `error'."
341 (defconst byte-compile-warning-types
342 '(redefine callargs free-vars unresolved
343 obsolete noruntime cl-functions interactive-only
)
344 "The list of warning types used when `byte-compile-warnings' is t.")
345 (defcustom byte-compile-warnings t
346 "*List of warnings that the byte-compiler should issue (t for all).
348 Elements of the list may be:
350 free-vars references to variables not in the current lexical scope.
351 unresolved calls to unknown functions.
352 callargs function calls with args that don't match the definition.
353 redefine function name redefined from a macro to ordinary function or vice
354 versa, or redefined to take a different number of arguments.
355 obsolete obsolete variables and functions.
356 noruntime functions that may not be defined at runtime (typically
357 defined only under `eval-when-compile').
358 cl-functions calls to runtime functions from the CL package (as
359 distinguished from macros and aliases).
361 commands that normally shouldn't be called from Lisp code.
362 make-local calls to make-variable-buffer-local that may be incorrect."
364 :type
`(choice (const :tag
"All" t
)
365 (set :menu-tag
"Some"
366 (const free-vars
) (const unresolved
)
367 (const callargs
) (const redefine
)
368 (const obsolete
) (const noruntime
)
369 (const cl-functions
) (const interactive-only
)
370 (const make-local
))))
371 (put 'byte-compile-warnings
'safe-local-variable
'byte-compile-warnings-safe-p
)
373 (defun byte-compile-warnings-safe-p (x)
378 (when (memq e
'(free-vars unresolved
381 cl-functions interactive-only make-local
))
386 (defvar byte-compile-interactive-only-functions
387 '(beginning-of-buffer end-of-buffer replace-string replace-regexp
388 insert-file insert-buffer insert-file-literally previous-line next-line
)
389 "List of commands that are not meant to be called from Lisp.")
391 (defvar byte-compile-not-obsolete-var nil
392 "If non-nil, this is a variable that shouldn't be reported as obsolete.")
394 (defcustom byte-compile-generate-call-tree nil
395 "*Non-nil means collect call-graph information when compiling.
396 This records which functions were called and from where.
397 If the value is t, compilation displays the call graph when it finishes.
398 If the value is neither t nor nil, compilation asks you whether to display
401 The call tree only lists functions called, not macros used. Those functions
402 which the byte-code interpreter knows about directly (eq, cons, etc.) are
405 The call tree also lists those functions which are not known to be called
406 \(that is, to which no calls have been compiled). Functions which can be
407 invoked interactively are excluded from this list."
409 :type
'(choice (const :tag
"Yes" t
) (const :tag
"No" nil
)
410 (other :tag
"Ask" lambda
)))
412 (defvar byte-compile-call-tree nil
"Alist of functions and their call tree.
413 Each element looks like
415 \(FUNCTION CALLERS CALLS\)
417 where CALLERS is a list of functions that call FUNCTION, and CALLS
418 is a list of functions for which calls were generated while compiling
421 (defcustom byte-compile-call-tree-sort
'name
422 "*If non-nil, sort the call tree.
423 The values `name', `callers', `calls', `calls+callers'
424 specify different fields to sort on."
426 :type
'(choice (const name
) (const callers
) (const calls
)
427 (const calls
+callers
) (const nil
)))
429 (defvar byte-compile-debug nil
)
431 ;; (defvar byte-compile-overwrite-file t
432 ;; "If nil, old .elc files are deleted before the new is saved, and .elc
433 ;; files will have the same modes as the corresponding .el file. Otherwise,
434 ;; existing .elc files will simply be overwritten, and the existing modes
435 ;; will not be changed. If this variable is nil, then an .elc file which
436 ;; is a symbolic link will be turned into a normal file, instead of the file
437 ;; which the link points to being overwritten.")
439 (defvar byte-compile-constants nil
440 "List of all constants encountered during compilation of this form.")
441 (defvar byte-compile-variables nil
442 "List of all variables encountered during compilation of this form.")
443 (defvar byte-compile-bound-variables nil
444 "List of variables bound in the context of the current form.
445 This list lives partly on the stack.")
446 (defvar byte-compile-const-variables nil
447 "List of variables declared as constants during compilation of this file.")
448 (defvar byte-compile-free-references
)
449 (defvar byte-compile-free-assignments
)
451 (defvar byte-compiler-error-flag
)
453 (defconst byte-compile-initial-macro-environment
455 ;; (byte-compiler-options . (lambda (&rest forms)
456 ;; (apply 'byte-compiler-options-handler forms)))
457 (eval-when-compile .
(lambda (&rest body
)
459 (byte-compile-eval (byte-compile-top-level
460 (cons 'progn body
))))))
461 (eval-and-compile .
(lambda (&rest body
)
462 (byte-compile-eval-before-compile (cons 'progn body
))
463 (cons 'progn body
))))
464 "The default macro-environment passed to macroexpand by the compiler.
465 Placing a macro here will cause a macro to have different semantics when
466 expanded by the compiler as when expanded by the interpreter.")
468 (defvar byte-compile-macro-environment byte-compile-initial-macro-environment
469 "Alist of macros defined in the file being compiled.
470 Each element looks like (MACRONAME . DEFINITION). It is
471 \(MACRONAME . nil) when a macro is redefined as a function.")
473 (defvar byte-compile-function-environment nil
474 "Alist of functions defined in the file being compiled.
475 This is so we can inline them when necessary.
476 Each element looks like (FUNCTIONNAME . DEFINITION). It is
477 \(FUNCTIONNAME . nil) when a function is redefined as a macro.
478 It is \(FUNCTIONNAME . t) when all we know is that it was defined,
479 and we don't know the definition.")
481 (defvar byte-compile-unresolved-functions nil
482 "Alist of undefined functions to which calls have been compiled.
483 This variable is only significant whilst compiling an entire buffer.
484 Used for warnings when a function is not known to be defined or is later
485 defined with incorrect args.")
487 (defvar byte-compile-noruntime-functions nil
488 "Alist of functions called that may not be defined when the compiled code is run.
489 Used for warnings about calling a function that is defined during compilation
490 but won't necessarily be defined when the compiled file is loaded.")
492 (defvar byte-compile-tag-number
0)
493 (defvar byte-compile-output nil
494 "Alist describing contents to put in byte code string.
495 Each element is (INDEX . VALUE)")
496 (defvar byte-compile-depth
0 "Current depth of execution stack.")
497 (defvar byte-compile-maxdepth
0 "Maximum depth of execution stack.")
500 ;;; The byte codes; this information is duplicated in bytecomp.c
502 (defvar byte-code-vector nil
503 "An array containing byte-code names indexed by byte-code values.")
505 (defvar byte-stack
+-info nil
506 "An array with the stack adjustment for each byte-code.")
508 (defmacro byte-defop
(opcode stack-adjust opname
&optional docstring
)
509 ;; This is a speed-hack for building the byte-code-vector at compile-time.
510 ;; We fill in the vector at macroexpand-time, and then after the last call
511 ;; to byte-defop, we write the vector out as a constant instead of writing
512 ;; out a bunch of calls to aset.
513 ;; Actually, we don't fill in the vector itself, because that could make
514 ;; it problematic to compile big changes to this compiler; we store the
515 ;; values on its plist, and remove them later in -extrude.
516 (let ((v1 (or (get 'byte-code-vector
'tmp-compile-time-value
)
517 (put 'byte-code-vector
'tmp-compile-time-value
518 (make-vector 256 nil
))))
519 (v2 (or (get 'byte-stack
+-info
'tmp-compile-time-value
)
520 (put 'byte-stack
+-info
'tmp-compile-time-value
521 (make-vector 256 nil
)))))
522 (aset v1 opcode opname
)
523 (aset v2 opcode stack-adjust
))
525 (list 'defconst opname opcode
(concat "Byte code opcode " docstring
"."))
526 (list 'defconst opname opcode
)))
528 (defmacro byte-extrude-byte-code-vectors
()
529 (prog1 (list 'setq
'byte-code-vector
530 (get 'byte-code-vector
'tmp-compile-time-value
)
532 (get 'byte-stack
+-info
'tmp-compile-time-value
))
533 (put 'byte-code-vector
'tmp-compile-time-value nil
)
534 (put 'byte-stack
+-info
'tmp-compile-time-value nil
)))
539 ;; These opcodes are special in that they pack their argument into the
542 (byte-defop 8 1 byte-varref
"for variable reference")
543 (byte-defop 16 -
1 byte-varset
"for setting a variable")
544 (byte-defop 24 -
1 byte-varbind
"for binding a variable")
545 (byte-defop 32 0 byte-call
"for calling a function")
546 (byte-defop 40 0 byte-unbind
"for unbinding special bindings")
547 ;; codes 8-47 are consumed by the preceding opcodes
551 (byte-defop 56 -
1 byte-nth
)
552 (byte-defop 57 0 byte-symbolp
)
553 (byte-defop 58 0 byte-consp
)
554 (byte-defop 59 0 byte-stringp
)
555 (byte-defop 60 0 byte-listp
)
556 (byte-defop 61 -
1 byte-eq
)
557 (byte-defop 62 -
1 byte-memq
)
558 (byte-defop 63 0 byte-not
)
559 (byte-defop 64 0 byte-car
)
560 (byte-defop 65 0 byte-cdr
)
561 (byte-defop 66 -
1 byte-cons
)
562 (byte-defop 67 0 byte-list1
)
563 (byte-defop 68 -
1 byte-list2
)
564 (byte-defop 69 -
2 byte-list3
)
565 (byte-defop 70 -
3 byte-list4
)
566 (byte-defop 71 0 byte-length
)
567 (byte-defop 72 -
1 byte-aref
)
568 (byte-defop 73 -
2 byte-aset
)
569 (byte-defop 74 0 byte-symbol-value
)
570 (byte-defop 75 0 byte-symbol-function
) ; this was commented out
571 (byte-defop 76 -
1 byte-set
)
572 (byte-defop 77 -
1 byte-fset
) ; this was commented out
573 (byte-defop 78 -
1 byte-get
)
574 (byte-defop 79 -
2 byte-substring
)
575 (byte-defop 80 -
1 byte-concat2
)
576 (byte-defop 81 -
2 byte-concat3
)
577 (byte-defop 82 -
3 byte-concat4
)
578 (byte-defop 83 0 byte-sub1
)
579 (byte-defop 84 0 byte-add1
)
580 (byte-defop 85 -
1 byte-eqlsign
)
581 (byte-defop 86 -
1 byte-gtr
)
582 (byte-defop 87 -
1 byte-lss
)
583 (byte-defop 88 -
1 byte-leq
)
584 (byte-defop 89 -
1 byte-geq
)
585 (byte-defop 90 -
1 byte-diff
)
586 (byte-defop 91 0 byte-negate
)
587 (byte-defop 92 -
1 byte-plus
)
588 (byte-defop 93 -
1 byte-max
)
589 (byte-defop 94 -
1 byte-min
)
590 (byte-defop 95 -
1 byte-mult
) ; v19 only
591 (byte-defop 96 1 byte-point
)
592 (byte-defop 98 0 byte-goto-char
)
593 (byte-defop 99 0 byte-insert
)
594 (byte-defop 100 1 byte-point-max
)
595 (byte-defop 101 1 byte-point-min
)
596 (byte-defop 102 0 byte-char-after
)
597 (byte-defop 103 1 byte-following-char
)
598 (byte-defop 104 1 byte-preceding-char
)
599 (byte-defop 105 1 byte-current-column
)
600 (byte-defop 106 0 byte-indent-to
)
601 (byte-defop 107 0 byte-scan-buffer-OBSOLETE
) ; no longer generated as of v18
602 (byte-defop 108 1 byte-eolp
)
603 (byte-defop 109 1 byte-eobp
)
604 (byte-defop 110 1 byte-bolp
)
605 (byte-defop 111 1 byte-bobp
)
606 (byte-defop 112 1 byte-current-buffer
)
607 (byte-defop 113 0 byte-set-buffer
)
608 (byte-defop 114 0 byte-save-current-buffer
609 "To make a binding to record the current buffer")
610 (byte-defop 115 0 byte-set-mark-OBSOLETE
)
611 (byte-defop 116 1 byte-interactive-p
)
613 ;; These ops are new to v19
614 (byte-defop 117 0 byte-forward-char
)
615 (byte-defop 118 0 byte-forward-word
)
616 (byte-defop 119 -
1 byte-skip-chars-forward
)
617 (byte-defop 120 -
1 byte-skip-chars-backward
)
618 (byte-defop 121 0 byte-forward-line
)
619 (byte-defop 122 0 byte-char-syntax
)
620 (byte-defop 123 -
1 byte-buffer-substring
)
621 (byte-defop 124 -
1 byte-delete-region
)
622 (byte-defop 125 -
1 byte-narrow-to-region
)
623 (byte-defop 126 1 byte-widen
)
624 (byte-defop 127 0 byte-end-of-line
)
628 ;; These store their argument in the next two bytes
629 (byte-defop 129 1 byte-constant2
630 "for reference to a constant with vector index >= byte-constant-limit")
631 (byte-defop 130 0 byte-goto
"for unconditional jump")
632 (byte-defop 131 -
1 byte-goto-if-nil
"to pop value and jump if it's nil")
633 (byte-defop 132 -
1 byte-goto-if-not-nil
"to pop value and jump if it's not nil")
634 (byte-defop 133 -
1 byte-goto-if-nil-else-pop
635 "to examine top-of-stack, jump and don't pop it if it's nil,
637 (byte-defop 134 -
1 byte-goto-if-not-nil-else-pop
638 "to examine top-of-stack, jump and don't pop it if it's non nil,
641 (byte-defop 135 -
1 byte-return
"to pop a value and return it from `byte-code'")
642 (byte-defop 136 -
1 byte-discard
"to discard one value from stack")
643 (byte-defop 137 1 byte-dup
"to duplicate the top of the stack")
645 (byte-defop 138 0 byte-save-excursion
646 "to make a binding to record the buffer, point and mark")
647 (byte-defop 139 0 byte-save-window-excursion
648 "to make a binding to record entire window configuration")
649 (byte-defop 140 0 byte-save-restriction
650 "to make a binding to record the current buffer clipping restrictions")
651 (byte-defop 141 -
1 byte-catch
652 "for catch. Takes, on stack, the tag and an expression for the body")
653 (byte-defop 142 -
1 byte-unwind-protect
654 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
656 ;; For condition-case. Takes, on stack, the variable to bind,
657 ;; an expression for the body, and a list of clauses.
658 (byte-defop 143 -
2 byte-condition-case
)
660 ;; For entry to with-output-to-temp-buffer.
661 ;; Takes, on stack, the buffer name.
662 ;; Binds standard-output and does some other things.
663 ;; Returns with temp buffer on the stack in place of buffer name.
664 (byte-defop 144 0 byte-temp-output-buffer-setup
)
666 ;; For exit from with-output-to-temp-buffer.
667 ;; Expects the temp buffer on the stack underneath value to return.
668 ;; Pops them both, then pushes the value back on.
669 ;; Unbinds standard-output and makes the temp buffer visible.
670 (byte-defop 145 -
1 byte-temp-output-buffer-show
)
672 ;; these ops are new to v19
674 ;; To unbind back to the beginning of this frame.
675 ;; Not used yet, but will be needed for tail-recursion elimination.
676 (byte-defop 146 0 byte-unbind-all
)
678 ;; these ops are new to v19
679 (byte-defop 147 -
2 byte-set-marker
)
680 (byte-defop 148 0 byte-match-beginning
)
681 (byte-defop 149 0 byte-match-end
)
682 (byte-defop 150 0 byte-upcase
)
683 (byte-defop 151 0 byte-downcase
)
684 (byte-defop 152 -
1 byte-string
=)
685 (byte-defop 153 -
1 byte-string
<)
686 (byte-defop 154 -
1 byte-equal
)
687 (byte-defop 155 -
1 byte-nthcdr
)
688 (byte-defop 156 -
1 byte-elt
)
689 (byte-defop 157 -
1 byte-member
)
690 (byte-defop 158 -
1 byte-assq
)
691 (byte-defop 159 0 byte-nreverse
)
692 (byte-defop 160 -
1 byte-setcar
)
693 (byte-defop 161 -
1 byte-setcdr
)
694 (byte-defop 162 0 byte-car-safe
)
695 (byte-defop 163 0 byte-cdr-safe
)
696 (byte-defop 164 -
1 byte-nconc
)
697 (byte-defop 165 -
1 byte-quo
)
698 (byte-defop 166 -
1 byte-rem
)
699 (byte-defop 167 0 byte-numberp
)
700 (byte-defop 168 0 byte-integerp
)
703 (byte-defop 175 nil byte-listN
)
704 (byte-defop 176 nil byte-concatN
)
705 (byte-defop 177 nil byte-insertN
)
709 (byte-defop 192 1 byte-constant
"for reference to a constant")
710 ;; codes 193-255 are consumed by byte-constant.
711 (defconst byte-constant-limit
64
712 "Exclusive maximum index usable in the `byte-constant' opcode.")
714 (defconst byte-goto-ops
'(byte-goto byte-goto-if-nil byte-goto-if-not-nil
715 byte-goto-if-nil-else-pop
716 byte-goto-if-not-nil-else-pop
)
717 "List of byte-codes whose offset is a pc.")
719 (defconst byte-goto-always-pop-ops
'(byte-goto-if-nil byte-goto-if-not-nil
))
721 (byte-extrude-byte-code-vectors)
723 ;;; lapcode generator
725 ;; the byte-compiler now does source -> lapcode -> bytecode instead of
726 ;; source -> bytecode, because it's a lot easier to make optimizations
727 ;; on lapcode than on bytecode.
729 ;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
730 ;; where instruction is a symbol naming a byte-code instruction,
731 ;; and parameter is an argument to that instruction, if any.
733 ;; The instruction can be the pseudo-op TAG, which means that this position
734 ;; in the instruction stream is a target of a goto. (car PARAMETER) will be
735 ;; the PC for this location, and the whole instruction "(TAG pc)" will be the
736 ;; parameter for some goto op.
738 ;; If the operation is varbind, varref, varset or push-constant, then the
739 ;; parameter is (variable/constant . index_in_constant_vector).
741 ;; First, the source code is macroexpanded and optimized in various ways.
742 ;; Then the resultant code is compiled into lapcode. Another set of
743 ;; optimizations are then run over the lapcode. Then the variables and
744 ;; constants referenced by the lapcode are collected and placed in the
745 ;; constants-vector. (This happens now so that variables referenced by dead
746 ;; code don't consume space.) And finally, the lapcode is transformed into
747 ;; compacted byte-code.
749 ;; A distinction is made between variables and constants because the variable-
750 ;; referencing instructions are more sensitive to the variables being near the
751 ;; front of the constants-vector than the constant-referencing instructions.
752 ;; Also, this lets us notice references to free variables.
754 (defun byte-compile-lapcode (lap)
755 "Turns lapcode into bytecode. The lapcode is destroyed."
756 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
757 (let ((pc 0) ; Program counter
758 op off
; Operation & offset
759 (bytes '()) ; Put the output bytes here
760 (patchlist nil
)) ; List of tags and goto's to patch
762 (setq op
(car (car lap
))
764 (cond ((not (symbolp op
))
765 (error "Non-symbolic opcode `%s'" op
))
768 (setq patchlist
(cons off patchlist
)))
769 ((memq op byte-goto-ops
)
771 (setq bytes
(cons (cons pc
(cdr off
))
773 (cons (symbol-value op
) bytes
))))
774 (setq patchlist
(cons bytes patchlist
)))
777 (cond ((cond ((consp off
)
778 ;; Variable or constant reference
780 (eq op
'byte-constant
)))
781 (cond ((< off byte-constant-limit
)
783 (cons (+ byte-constant off
) bytes
))
787 (cons (logand off
255)
788 (cons byte-constant2 bytes
))))))
789 ((<= byte-listN
(symbol-value op
))
791 (cons off
(cons (symbol-value op
) bytes
)))
794 (cons (+ (symbol-value op
) off
) bytes
))
797 (cons off
(cons (+ (symbol-value op
) 6) bytes
)))
801 (cons (logand off
255)
802 (cons (+ (symbol-value op
) 7)
804 (setq lap
(cdr lap
)))
805 ;;(if (not (= pc (length bytes)))
806 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
807 ;; Patch PC into jumps
810 (setq bytes
(car patchlist
))
811 (cond ((atom (car bytes
))) ; Tag
813 (setq pc
(car (cdr (car bytes
)))) ; Pick PC from tag
814 (setcar (cdr bytes
) (logand pc
255))
815 (setcar bytes
(lsh pc -
8))))
816 (setq patchlist
(cdr patchlist
))))
817 (concat (nreverse bytes
))))
820 ;;; compile-time evaluation
822 (defun byte-compile-eval (form)
823 "Eval FORM and mark the functions defined therein.
824 Each function's symbol gets added to `byte-compile-noruntime-functions'."
825 (let ((hist-orig load-history
)
826 (hist-nil-orig current-load-list
))
828 (when (memq 'noruntime byte-compile-warnings
)
829 (let ((hist-new load-history
)
830 (hist-nil-new current-load-list
))
831 ;; Go through load-history, look for newly loaded files
832 ;; and mark all the functions defined therein.
833 (while (and hist-new
(not (eq hist-new hist-orig
)))
834 (let ((xs (pop hist-new
))
836 ;; Make sure the file was not already loaded before.
837 (unless (or (assoc (car xs
) hist-orig
)
838 (equal (car xs
) "cl"))
842 (unless (memq s old-autoloads
)
843 (push s byte-compile-noruntime-functions
)))
844 ((and (consp s
) (eq t
(car s
)))
845 (push (cdr s
) old-autoloads
))
846 ((and (consp s
) (eq 'autoload
(car s
)))
847 (push (cdr s
) byte-compile-noruntime-functions
)))))))
848 ;; Go through current-load-list for the locally defined funs.
850 (while (and hist-nil-new
(not (eq hist-nil-new hist-nil-orig
)))
851 (let ((s (pop hist-nil-new
)))
852 (when (and (symbolp s
) (not (memq s old-autoloads
)))
853 (push s byte-compile-noruntime-functions
))
854 (when (and (consp s
) (eq t
(car s
)))
855 (push (cdr s
) old-autoloads
)))))))
856 (when (memq 'cl-functions byte-compile-warnings
)
857 (let ((hist-new load-history
))
858 ;; Go through load-history, look for newly loaded files
859 ;; and mark all the functions defined therein.
860 (while (and hist-new
(not (eq hist-new hist-orig
)))
861 (let ((xs (pop hist-new
)))
862 ;; Make sure the file was not already loaded before.
863 (when (and (equal (car xs
) "cl") (not (assoc (car xs
) hist-orig
)))
864 (byte-compile-find-cl-functions)))))))))
866 (defun byte-compile-eval-before-compile (form)
867 "Evaluate FORM for `eval-and-compile'."
868 (let ((hist-nil-orig current-load-list
))
870 ;; (eval-and-compile (require 'cl) turns off warnings for cl functions.
871 (let ((tem current-load-list
))
872 (while (not (eq tem hist-nil-orig
))
873 (when (equal (car tem
) '(require . cl
))
874 (setq byte-compile-warnings
875 (remq 'cl-functions byte-compile-warnings
)))
876 (setq tem
(cdr tem
)))))))
878 ;;; byte compiler messages
880 (defvar byte-compile-current-form nil
)
881 (defvar byte-compile-dest-file nil
)
882 (defvar byte-compile-current-file nil
)
883 (defvar byte-compile-current-buffer nil
)
885 ;; Log something that isn't a warning.
886 (defmacro byte-compile-log
(format-string &rest args
)
889 (memq byte-optimize-log
'(t source
))
890 (let ((print-escape-newlines t
)
897 (lambda (x) (if (symbolp x
) (list 'prin1-to-string x
) x
))
900 ;; Log something that isn't a warning.
901 (defun byte-compile-log-1 (string)
902 (with-current-buffer "*Compile-Log*"
903 (let ((inhibit-read-only t
))
904 (goto-char (point-max))
905 (byte-compile-warning-prefix nil nil
)
906 (cond (noninteractive
907 (message " %s" string
))
909 (insert (format "%s\n" string
)))))))
911 (defvar byte-compile-read-position nil
912 "Character position we began the last `read' from.")
913 (defvar byte-compile-last-position nil
914 "Last known character position in the input.")
916 ;; copied from gnus-util.el
917 (defsubst byte-compile-delete-first
(elt list
)
918 (if (eq (car list
) elt
)
921 (while (and (cdr list
)
922 (not (eq (cadr list
) elt
)))
923 (setq list
(cdr list
)))
925 (setcdr list
(cddr list
)))
928 ;; The purpose of this function is to iterate through the
929 ;; `read-symbol-positions-list'. Each time we process, say, a
930 ;; function definition (`defun') we remove `defun' from
931 ;; `read-symbol-positions-list', and set `byte-compile-last-position'
932 ;; to that symbol's character position. Similarly, if we encounter a
933 ;; variable reference, like in (1+ foo), we remove `foo' from the
934 ;; list. If our current position is after the symbol's position, we
935 ;; assume we've already passed that point, and look for the next
936 ;; occurrence of the symbol.
938 ;; This function should not be called twice for the same occurrence of
939 ;; a symbol, and it should not be called for symbols generated by the
940 ;; byte compiler itself; because rather than just fail looking up the
941 ;; symbol, we may find an occurrence of the symbol further ahead, and
942 ;; then `byte-compile-last-position' as advanced too far.
944 ;; So your're probably asking yourself: Isn't this function a
945 ;; gross hack? And the answer, of course, would be yes.
946 (defun byte-compile-set-symbol-position (sym &optional allow-previous
)
947 (when byte-compile-read-position
950 (setq last byte-compile-last-position
951 entry
(assq sym read-symbol-positions-list
))
953 (setq byte-compile-last-position
954 (+ byte-compile-read-position
(cdr entry
))
955 read-symbol-positions-list
956 (byte-compile-delete-first
957 entry read-symbol-positions-list
)))
958 (or (and allow-previous
(not (= last byte-compile-last-position
)))
959 (> last byte-compile-last-position
)))))))
961 (defvar byte-compile-last-warned-form nil
)
962 (defvar byte-compile-last-logged-file nil
)
964 ;; This is used as warning-prefix for the compiler.
965 ;; It is always called with the warnings buffer current.
966 (defun byte-compile-warning-prefix (level entry
)
967 (let* ((inhibit-read-only t
)
968 (dir default-directory
)
969 (file (cond ((stringp byte-compile-current-file
)
970 (format "%s:" (file-relative-name byte-compile-current-file dir
)))
971 ((bufferp byte-compile-current-file
)
973 (buffer-name byte-compile-current-file
)))
975 (pos (if (and byte-compile-current-file
976 (integerp byte-compile-read-position
))
977 (with-current-buffer byte-compile-current-buffer
980 (goto-char byte-compile-last-position
)
981 (1+ (count-lines (point-min) (point-at-bol))))
983 (goto-char byte-compile-last-position
)
984 (1+ (current-column)))))
986 (form (if (eq byte-compile-current-form
:end
) "end of data"
987 (or byte-compile-current-form
"toplevel form"))))
988 (when (or (and byte-compile-current-file
989 (not (equal byte-compile-current-file
990 byte-compile-last-logged-file
)))
991 (and byte-compile-current-form
992 (not (eq byte-compile-current-form
993 byte-compile-last-warned-form
))))
994 (insert (format "\nIn %s:\n" form
)))
996 (insert (format "%s%s" file pos
))))
997 (setq byte-compile-last-logged-file byte-compile-current-file
998 byte-compile-last-warned-form byte-compile-current-form
)
1001 ;; This no-op function is used as the value of warning-series
1002 ;; to tell inner calls to displaying-byte-compile-warnings
1003 ;; not to bind warning-series.
1004 (defun byte-compile-warning-series (&rest ignore
)
1007 ;; Log the start of a file in *Compile-Log*, and mark it as done.
1008 ;; Return the position of the start of the page in the log buffer.
1009 ;; But do nothing in batch mode.
1010 (defun byte-compile-log-file ()
1011 (and (not (equal byte-compile-current-file byte-compile-last-logged-file
))
1012 (not noninteractive
)
1014 (set-buffer (get-buffer-create "*Compile-Log*"))
1015 (goto-char (point-max))
1016 (let* ((inhibit-read-only t
)
1017 (dir (and byte-compile-current-file
1018 (file-name-directory byte-compile-current-file
)))
1019 (was-same (equal default-directory dir
))
1023 (insert (format "Leaving directory `%s'\n" default-directory
))))
1026 (setq pt
(point-marker))
1027 (if byte-compile-current-file
1028 (insert "\f\nCompiling "
1029 (if (stringp byte-compile-current-file
)
1030 (concat "file " byte-compile-current-file
)
1031 (concat "buffer " (buffer-name byte-compile-current-file
)))
1032 " at " (current-time-string) "\n")
1033 (insert "\f\nCompiling no file at " (current-time-string) "\n"))
1035 (setq default-directory dir
)
1037 (insert (format "Entering directory `%s'\n" default-directory
))))
1038 (setq byte-compile-last-logged-file byte-compile-current-file
1039 byte-compile-last-warned-form nil
)
1040 ;; Do this after setting default-directory.
1041 (unless (eq major-mode
'compilation-mode
)
1043 (compilation-forget-errors)
1046 ;; Log a message STRING in *Compile-Log*.
1047 ;; Also log the current function and file if not already done.
1048 (defun byte-compile-log-warning (string &optional fill level
)
1049 (let ((warning-prefix-function 'byte-compile-warning-prefix
)
1050 (warning-type-format "")
1051 (warning-fill-prefix (if fill
" "))
1052 (inhibit-read-only t
))
1053 (display-warning 'bytecomp string level
"*Compile-Log*")))
1055 (defun byte-compile-warn (format &rest args
)
1056 "Issue a byte compiler warning; use (format FORMAT ARGS...) for message."
1057 (setq format
(apply 'format format args
))
1058 (if byte-compile-error-on-warn
1059 (error "%s" format
) ; byte-compile-file catches and logs it
1060 (byte-compile-log-warning format t
:warning
)))
1062 (defun byte-compile-report-error (error-info)
1063 "Report Lisp error in compilation. ERROR-INFO is the error data."
1064 (setq byte-compiler-error-flag t
)
1065 (byte-compile-log-warning
1066 (error-message-string error-info
)
1069 ;;; Used by make-obsolete.
1070 (defun byte-compile-obsolete (form)
1071 (let* ((new (get (car form
) 'byte-obsolete-info
))
1072 (handler (nth 1 new
))
1074 (byte-compile-set-symbol-position (car form
))
1075 (if (memq 'obsolete byte-compile-warnings
)
1076 (byte-compile-warn "`%s' is an obsolete function%s; %s" (car form
)
1077 (if when
(concat " (as of Emacs " when
")") "")
1078 (if (stringp (car new
))
1080 (format "use `%s' instead." (car new
)))))
1081 (funcall (or handler
'byte-compile-normal-call
) form
)))
1085 ;; (defvar byte-compiler-valid-options
1086 ;; '((optimize byte-optimize (t nil source byte) val)
1087 ;; (file-format byte-compile-compatibility (emacs18 emacs19)
1088 ;; (eq val 'emacs18))
1089 ;; ;; (new-bytecodes byte-compile-generate-emacs19-bytecodes (t nil) val)
1090 ;; (delete-errors byte-compile-delete-errors (t nil) val)
1091 ;; (verbose byte-compile-verbose (t nil) val)
1092 ;; (warnings byte-compile-warnings ((callargs redefine free-vars unresolved))
1095 ;; Inhibit v18/v19 selectors if the version is hardcoded.
1096 ;; #### This should print a warning if the user tries to change something
1097 ;; than can't be changed because the running compiler doesn't support it.
1099 ;; ((byte-compile-single-version)
1100 ;; (setcar (cdr (cdr (assq 'new-bytecodes byte-compiler-valid-options)))
1101 ;; (list (byte-compile-version-cond
1102 ;; byte-compile-generate-emacs19-bytecodes)))
1103 ;; (setcar (cdr (cdr (assq 'file-format byte-compiler-valid-options)))
1104 ;; (if (byte-compile-version-cond byte-compile-compatibility)
1105 ;; '(emacs18) '(emacs19)))))
1107 ;; (defun byte-compiler-options-handler (&rest args)
1108 ;; (let (key val desc choices)
1110 ;; (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args))))
1111 ;; (error "Malformed byte-compiler option `%s'" (car args)))
1112 ;; (setq key (car (car args))
1113 ;; val (car (cdr (car args)))
1114 ;; desc (assq key byte-compiler-valid-options))
1116 ;; (error "Unknown byte-compiler option `%s'" key))
1117 ;; (setq choices (nth 2 desc))
1118 ;; (if (consp (car choices))
1121 ;; (ret (and (memq (car val) '(+ -))
1122 ;; (copy-sequence (if (eq t (symbol-value (nth 1 desc)))
1124 ;; (symbol-value (nth 1 desc)))))))
1125 ;; (setq choices (car choices))
1127 ;; (setq this (car val))
1128 ;; (cond ((memq this choices)
1129 ;; (setq ret (funcall handler this ret)))
1130 ;; ((eq this '+) (setq handler 'cons))
1131 ;; ((eq this '-) (setq handler 'delq))
1132 ;; ((error "`%s' only accepts %s" key choices)))
1133 ;; (setq val (cdr val)))
1134 ;; (set (nth 1 desc) ret))
1135 ;; (or (memq val choices)
1136 ;; (error "`%s' must be one of `%s'" key choices))
1137 ;; (set (nth 1 desc) (eval (nth 3 desc))))
1138 ;; (setq args (cdr args)))
1141 ;;; sanity-checking arglists
1143 ;; If a function has an entry saying (FUNCTION . t).
1144 ;; that means we know it is defined but we don't know how.
1145 ;; If a function has an entry saying (FUNCTION . nil),
1146 ;; that means treat it as not defined.
1147 (defun byte-compile-fdefinition (name macro-p
)
1148 (let* ((list (if macro-p
1149 byte-compile-macro-environment
1150 byte-compile-function-environment
))
1151 (env (cdr (assq name list
))))
1154 (while (and (symbolp fn
)
1156 (or (symbolp (symbol-function fn
))
1157 (consp (symbol-function fn
))
1159 (byte-code-function-p (symbol-function fn
)))))
1160 (setq fn
(symbol-function fn
)))
1161 (if (and (not macro-p
) (byte-code-function-p fn
))
1164 (if (eq 'macro
(car fn
))
1168 (if (eq 'autoload
(car fn
))
1172 (defun byte-compile-arglist-signature (arglist)
1177 (cond ((eq (car arglist
) '&optional
)
1178 (or opts
(setq opts
0)))
1179 ((eq (car arglist
) '&rest
)
1185 (setq opts
(1+ opts
))
1186 (setq args
(1+ args
)))))
1187 (setq arglist
(cdr arglist
)))
1188 (cons args
(if restp nil
(if opts
(+ args opts
) args
)))))
1191 (defun byte-compile-arglist-signatures-congruent-p (old new
)
1193 (> (car new
) (car old
)) ; requires more args now
1194 (and (null (cdr old
)) ; took rest-args, doesn't any more
1196 (and (cdr new
) (cdr old
) ; can't take as many args now
1197 (< (cdr new
) (cdr old
)))
1200 (defun byte-compile-arglist-signature-string (signature)
1201 (cond ((null (cdr signature
))
1202 (format "%d+" (car signature
)))
1203 ((= (car signature
) (cdr signature
))
1204 (format "%d" (car signature
)))
1205 (t (format "%d-%d" (car signature
) (cdr signature
)))))
1208 ;; Warn if the form is calling a function with the wrong number of arguments.
1209 (defun byte-compile-callargs-warn (form)
1210 (let* ((def (or (byte-compile-fdefinition (car form
) nil
)
1211 (byte-compile-fdefinition (car form
) t
)))
1212 (sig (if (and def
(not (eq def t
)))
1213 (byte-compile-arglist-signature
1214 (if (eq 'lambda
(car-safe def
))
1216 (if (byte-code-function-p def
)
1219 (if (and (fboundp (car form
))
1220 (subrp (symbol-function (car form
))))
1221 (subr-arity (symbol-function (car form
))))))
1222 (ncall (length (cdr form
))))
1223 ;; Check many or unevalled from subr-arity.
1224 (if (and (cdr-safe sig
)
1225 (not (numberp (cdr sig
))))
1228 (when (or (< ncall
(car sig
))
1229 (and (cdr sig
) (> ncall
(cdr sig
))))
1230 (byte-compile-set-symbol-position (car form
))
1232 "%s called with %d argument%s, but %s %s"
1234 (if (= 1 ncall
) "" "s")
1235 (if (< ncall
(car sig
))
1238 (byte-compile-arglist-signature-string sig
))))
1239 (byte-compile-format-warn form
)
1240 ;; Check to see if the function will be available at runtime
1241 ;; and/or remember its arity if it's unknown.
1242 (or (and (or def
(fboundp (car form
))) ; might be a subr or autoload.
1243 (not (memq (car form
) byte-compile-noruntime-functions
)))
1244 (eq (car form
) byte-compile-current-form
) ; ## this doesn't work
1246 ;; It's a currently-undefined function.
1247 ;; Remember number of args in call.
1248 (let ((cons (assq (car form
) byte-compile-unresolved-functions
))
1249 (n (length (cdr form
))))
1251 (or (memq n
(cdr cons
))
1252 (setcdr cons
(cons n
(cdr cons
))))
1253 (push (list (car form
) n
)
1254 byte-compile-unresolved-functions
))))))
1256 (defun byte-compile-format-warn (form)
1257 "Warn if FORM is `format'-like with inconsistent args.
1258 Applies if head of FORM is a symbol with non-nil property
1259 `byte-compile-format-like' and first arg is a constant string.
1260 Then check the number of format fields matches the number of
1262 (when (and (symbolp (car form
))
1263 (stringp (nth 1 form
))
1264 (get (car form
) 'byte-compile-format-like
))
1265 (let ((nfields (with-temp-buffer
1266 (insert (nth 1 form
))
1267 (goto-char (point-min))
1269 (while (re-search-forward "%." nil t
)
1270 (unless (eq ?%
(char-after (1+ (match-beginning 0))))
1273 (nargs (- (length form
) 2)))
1274 (unless (= nargs nfields
)
1276 "`%s' called with %d args to fill %d format field(s)" (car form
)
1279 (dolist (elt '(format message error
))
1280 (put elt
'byte-compile-format-like t
))
1282 ;; Warn if a custom definition fails to specify :group.
1283 (defun byte-compile-nogroup-warn (form)
1284 (let ((keyword-args (cdr (cdr (cdr (cdr form
)))))
1286 (or (not (eq (car-safe name
) 'quote
))
1287 (and (eq (car form
) 'custom-declare-group
)
1288 (equal name
''emacs
))
1289 (plist-get keyword-args
:group
)
1290 (not (and (consp name
) (eq (car name
) 'quote
)))
1292 "%s for `%s' fails to specify containing group"
1293 (cdr (assq (car form
)
1294 '((custom-declare-group . defgroup
)
1295 (custom-declare-face . defface
)
1296 (custom-declare-variable . defcustom
))))
1299 ;; Warn if the function or macro is being redefined with a different
1300 ;; number of arguments.
1301 (defun byte-compile-arglist-warn (form macrop
)
1302 (let ((old (byte-compile-fdefinition (nth 1 form
) macrop
)))
1303 (if (and old
(not (eq old t
)))
1304 (let ((sig1 (byte-compile-arglist-signature
1305 (if (eq 'lambda
(car-safe old
))
1307 (if (byte-code-function-p old
)
1310 (sig2 (byte-compile-arglist-signature (nth 2 form
))))
1311 (unless (byte-compile-arglist-signatures-congruent-p sig1 sig2
)
1312 (byte-compile-set-symbol-position (nth 1 form
))
1314 "%s %s used to take %s %s, now takes %s"
1315 (if (eq (car form
) 'defun
) "function" "macro")
1317 (byte-compile-arglist-signature-string sig1
)
1318 (if (equal sig1
'(1 .
1)) "argument" "arguments")
1319 (byte-compile-arglist-signature-string sig2
))))
1320 ;; This is the first definition. See if previous calls are compatible.
1321 (let ((calls (assq (nth 1 form
) byte-compile-unresolved-functions
))
1325 (setq sig
(byte-compile-arglist-signature (nth 2 form
))
1326 nums
(sort (copy-sequence (cdr calls
)) (function <))
1328 max
(car (nreverse nums
)))
1329 (when (or (< min
(car sig
))
1330 (and (cdr sig
) (> max
(cdr sig
))))
1331 (byte-compile-set-symbol-position (nth 1 form
))
1333 "%s being defined to take %s%s, but was previously called with %s"
1335 (byte-compile-arglist-signature-string sig
)
1336 (if (equal sig
'(1 .
1)) " arg" " args")
1337 (byte-compile-arglist-signature-string (cons min max
))))
1339 (setq byte-compile-unresolved-functions
1340 (delq calls byte-compile-unresolved-functions
)))))
1343 (defvar byte-compile-cl-functions nil
1344 "List of functions defined in CL.")
1346 (defun byte-compile-find-cl-functions ()
1347 (unless byte-compile-cl-functions
1348 (dolist (elt load-history
)
1349 (when (and (stringp (car elt
))
1351 "^cl\\>" (file-name-nondirectory (car elt
))))
1352 (setq byte-compile-cl-functions
1353 (append byte-compile-cl-functions
1355 (let ((tail byte-compile-cl-functions
))
1357 (if (and (consp (car tail
))
1358 (eq (car (car tail
)) 'autoload
))
1359 (setcar tail
(cdr (car tail
))))
1360 (setq tail
(cdr tail
))))))
1362 (defun byte-compile-cl-warn (form)
1363 "Warn if FORM is a call of a function from the CL package."
1364 (let ((func (car-safe form
)))
1365 (if (and byte-compile-cl-functions
1366 (memq func byte-compile-cl-functions
)
1367 ;; Aliases which won't have been expanded at this point.
1368 ;; These aren't all aliases of subrs, so not trivial to
1369 ;; avoid hardwiring the list.
1371 '(cl-block-wrapper cl-block-throw
1372 multiple-value-call nth-value
1373 copy-seq first second rest endp cl-member
1374 ;; These are included in generated code
1375 ;; that can't be called except at compile time
1376 ;; or unless cl is loaded anyway.
1377 cl-defsubst-expand cl-struct-setf-expander
1378 ;; These would sometimes be warned about
1379 ;; but such warnings are never useful,
1380 ;; so don't warn about them.
1381 macroexpand cl-macroexpand-all
1382 cl-compiling-file
)))
1383 ;; Avoid warnings for things which are safe because they
1384 ;; have suitable compiler macros, but those aren't
1385 ;; expanded at this stage. There should probably be more
1386 ;; here than caaar and friends.
1387 (not (and (eq (get func
'byte-compile
)
1388 'cl-byte-compile-compiler-macro
)
1389 (string-match "\\`c[ad]+r\\'" (symbol-name func
)))))
1390 (byte-compile-warn "Function `%s' from cl package called at runtime"
1394 (defun byte-compile-print-syms (str1 strn syms
)
1396 (byte-compile-set-symbol-position (car syms
) t
))
1397 (cond ((and (cdr syms
) (not noninteractive
))
1402 (setq s
(symbol-name (pop syms
))
1403 L
(+ L
(length s
) 2))
1404 (if (< L
(1- fill-column
))
1405 (setq str
(concat str
" " s
(and syms
",")))
1406 (setq str
(concat str
"\n " s
(and syms
","))
1407 L
(+ (length s
) 4))))
1408 (byte-compile-warn "%s" str
)))
1410 (byte-compile-warn "%s %s"
1412 (mapconcat #'symbol-name syms
", ")))
1415 (byte-compile-warn str1
(car syms
)))))
1417 ;; If we have compiled any calls to functions which are not known to be
1418 ;; defined, issue a warning enumerating them.
1419 ;; `unresolved' in the list `byte-compile-warnings' disables this.
1420 (defun byte-compile-warn-about-unresolved-functions ()
1421 (when (memq 'unresolved byte-compile-warnings
)
1422 (let ((byte-compile-current-form :end
)
1425 ;; Separate the functions that will not be available at runtime
1426 ;; from the truly unresolved ones.
1427 (dolist (f byte-compile-unresolved-functions
)
1429 (if (fboundp f
) (push f noruntime
) (push f unresolved
)))
1430 ;; Complain about the no-run-time functions
1431 (byte-compile-print-syms
1432 "the function `%s' might not be defined at runtime."
1433 "the following functions might not be defined at runtime:"
1435 ;; Complain about the unresolved functions
1436 (byte-compile-print-syms
1437 "the function `%s' is not known to be defined."
1438 "the following functions are not known to be defined:"
1443 (defsubst byte-compile-const-symbol-p
(symbol &optional any-value
)
1444 "Non-nil if SYMBOL is constant.
1445 If ANY-VALUE is nil, only return non-nil if the value of the symbol is the
1447 (or (memq symbol
'(nil t
))
1449 (if any-value
(memq symbol byte-compile-const-variables
))))
1451 (defmacro byte-compile-constp
(form)
1452 "Return non-nil if FORM is a constant."
1453 `(cond ((consp ,form
) (eq (car ,form
) 'quote
))
1454 ((not (symbolp ,form
)))
1455 ((byte-compile-const-symbol-p ,form
))))
1457 (defmacro byte-compile-close-variables
(&rest body
)
1460 ;; Close over these variables to encapsulate the
1461 ;; compilation state
1463 (byte-compile-macro-environment
1464 ;; Copy it because the compiler may patch into the
1465 ;; macroenvironment.
1466 (copy-alist byte-compile-initial-macro-environment
))
1467 (byte-compile-function-environment nil
)
1468 (byte-compile-bound-variables nil
)
1469 (byte-compile-const-variables nil
)
1470 (byte-compile-free-references nil
)
1471 (byte-compile-free-assignments nil
)
1473 ;; Close over these variables so that `byte-compiler-options'
1474 ;; can change them on a per-file basis.
1476 (byte-compile-verbose byte-compile-verbose
)
1477 (byte-optimize byte-optimize
)
1478 (byte-compile-compatibility byte-compile-compatibility
)
1479 (byte-compile-dynamic byte-compile-dynamic
)
1480 (byte-compile-dynamic-docstrings
1481 byte-compile-dynamic-docstrings
)
1482 ;; (byte-compile-generate-emacs19-bytecodes
1483 ;; byte-compile-generate-emacs19-bytecodes)
1484 (byte-compile-warnings (if (eq byte-compile-warnings t
)
1485 byte-compile-warning-types
1486 byte-compile-warnings
))
1490 (defmacro displaying-byte-compile-warnings
(&rest body
)
1491 `(let* ((--displaying-byte-compile-warnings-fn (lambda () ,@body
))
1492 (warning-series-started
1493 (and (markerp warning-series
)
1494 (eq (marker-buffer warning-series
)
1495 (get-buffer "*Compile-Log*")))))
1496 (byte-compile-find-cl-functions)
1497 (if (or (eq warning-series
'byte-compile-warning-series
)
1498 warning-series-started
)
1499 ;; warning-series does come from compilation,
1500 ;; so don't bind it, but maybe do set it.
1502 ;; Log the file name. Record position of that text.
1503 (setq tem
(byte-compile-log-file))
1504 (unless warning-series-started
1505 (setq warning-series
(or tem
'byte-compile-warning-series
)))
1506 (if byte-compile-debug
1507 (funcall --displaying-byte-compile-warnings-fn
)
1508 (condition-case error-info
1509 (funcall --displaying-byte-compile-warnings-fn
)
1510 (error (byte-compile-report-error error-info
)))))
1511 ;; warning-series does not come from compilation, so bind it.
1512 (let ((warning-series
1513 ;; Log the file name. Record position of that text.
1514 (or (byte-compile-log-file) 'byte-compile-warning-series
)))
1515 (if byte-compile-debug
1516 (funcall --displaying-byte-compile-warnings-fn
)
1517 (condition-case error-info
1518 (funcall --displaying-byte-compile-warnings-fn
)
1519 (error (byte-compile-report-error error-info
))))))))
1522 (defun byte-force-recompile (directory)
1523 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
1524 Files in subdirectories of DIRECTORY are processed also."
1525 (interactive "DByte force recompile (directory): ")
1526 (byte-recompile-directory directory nil t
))
1529 (defun byte-recompile-directory (directory &optional arg force
)
1530 "Recompile every `.el' file in DIRECTORY that needs recompilation.
1531 This is if a `.elc' file exists but is older than the `.el' file.
1532 Files in subdirectories of DIRECTORY are processed also.
1534 If the `.elc' file does not exist, normally this function *does not*
1535 compile the corresponding `.el' file. However,
1536 if ARG (the prefix argument) is 0, that means do compile all those files.
1537 A nonzero ARG means ask the user, for each such `.el' file,
1538 whether to compile it.
1540 A nonzero ARG also means ask about each subdirectory before scanning it.
1542 If the third argument FORCE is non-nil,
1543 recompile every `.el' file that already has a `.elc' file."
1544 (interactive "DByte recompile directory: \nP")
1546 (setq arg
(prefix-numeric-value arg
)))
1550 (force-mode-line-update))
1551 (save-current-buffer
1552 (set-buffer (get-buffer-create "*Compile-Log*"))
1553 (setq default-directory
(expand-file-name directory
))
1554 ;; compilation-mode copies value of default-directory.
1555 (unless (eq major-mode
'compilation-mode
)
1557 (let ((directories (list (expand-file-name directory
)))
1558 (default-directory default-directory
)
1564 (displaying-byte-compile-warnings
1566 (setq directory
(car directories
))
1567 (message "Checking %s..." directory
)
1568 (let ((files (directory-files directory
))
1570 (dolist (file files
)
1571 (setq source
(expand-file-name file directory
))
1572 (if (and (not (member file
'("RCS" "CVS")))
1573 (not (eq ?\.
(aref file
0)))
1574 (file-directory-p source
)
1575 (not (file-symlink-p source
)))
1576 ;; This file is a subdirectory. Handle them differently.
1577 (when (or (null arg
)
1579 (y-or-n-p (concat "Check " source
"? ")))
1581 (nconc directories
(list source
))))
1582 ;; It is an ordinary file. Decide whether to compile it.
1583 (if (and (string-match emacs-lisp-file-regexp source
)
1584 (file-readable-p source
)
1585 (not (auto-save-file-name-p source
))
1586 (setq dest
(byte-compile-dest-file source
))
1587 (if (file-exists-p dest
)
1588 ;; File was already compiled.
1589 (or force
(file-newer-than-file-p source dest
))
1590 ;; No compiled file exists yet.
1593 (y-or-n-p (concat "Compile " source
"? "))))))
1594 (progn (if (and noninteractive
(not byte-compile-verbose
))
1595 (message "Compiling %s..." source
))
1596 (let ((res (byte-compile-file source
)))
1597 (cond ((eq res
'no-byte-compile
)
1598 (setq skip-count
(1+ skip-count
)))
1600 (setq file-count
(1+ file-count
)))
1602 (setq fail-count
(1+ fail-count
)))))
1604 (message "Checking %s..." directory
))
1605 (if (not (eq last-dir directory
))
1606 (setq last-dir directory
1607 dir-count
(1+ dir-count
)))
1609 (setq directories
(cdr directories
))))
1610 (message "Done (Total of %d file%s compiled%s%s%s)"
1611 file-count
(if (= file-count
1) "" "s")
1612 (if (> fail-count
0) (format ", %d failed" fail-count
) "")
1613 (if (> skip-count
0) (format ", %d skipped" skip-count
) "")
1614 (if (> dir-count
1) (format " in %d directories" dir-count
) "")))))
1616 (defvar no-byte-compile nil
1617 "Non-nil to prevent byte-compiling of emacs-lisp code.
1618 This is normally set in local file variables at the end of the elisp file:
1620 ;; Local Variables:\n;; no-byte-compile: t\n;; End: ")
1621 ;;;###autoload(put 'no-byte-compile 'safe-local-variable 'booleanp)
1624 (defun byte-compile-file (filename &optional load
)
1625 "Compile a file of Lisp code named FILENAME into a file of byte code.
1626 The output file's name is generated by passing FILENAME to the
1627 `byte-compile-dest-file' function (which see).
1628 With prefix arg (noninteractively: 2nd arg), LOAD the file after compiling.
1629 The value is non-nil if there were no errors, nil if errors."
1630 ;; (interactive "fByte compile file: \nP")
1632 (let ((file buffer-file-name
)
1636 (eq (cdr (assq 'major-mode
(buffer-local-variables)))
1638 (setq file-name
(file-name-nondirectory file
)
1639 file-dir
(file-name-directory file
)))
1640 (list (read-file-name (if current-prefix-arg
1641 "Byte compile and load file: "
1642 "Byte compile file: ")
1643 file-dir file-name nil
)
1644 current-prefix-arg
)))
1645 ;; Expand now so we get the current buffer's defaults
1646 (setq filename
(expand-file-name filename
))
1648 ;; If we're compiling a file that's in a buffer and is modified, offer
1649 ;; to save it first.
1651 (let ((b (get-file-buffer (expand-file-name filename
))))
1652 (if (and b
(buffer-modified-p b
)
1653 (y-or-n-p (format "Save buffer %s first? " (buffer-name b
))))
1654 (save-excursion (set-buffer b
) (save-buffer)))))
1656 ;; Force logging of the file name for each file compiled.
1657 (setq byte-compile-last-logged-file nil
)
1658 (let ((byte-compile-current-file filename
)
1659 (set-auto-coding-for-load t
)
1660 target-file input-buffer output-buffer
1661 byte-compile-dest-file
)
1662 (setq target-file
(byte-compile-dest-file filename
))
1663 (setq byte-compile-dest-file target-file
)
1665 (setq input-buffer
(get-buffer-create " *Compiler Input*"))
1666 (set-buffer input-buffer
)
1668 (setq buffer-file-coding-system nil
)
1669 ;; Always compile an Emacs Lisp file as multibyte
1670 ;; unless the file itself forces unibyte with -*-coding: raw-text;-*-
1671 (set-buffer-multibyte t
)
1672 (insert-file-contents filename
)
1673 ;; Mimic the way after-insert-file-set-coding can make the
1674 ;; buffer unibyte when visiting this file.
1675 (when (or (eq last-coding-system-used
'no-conversion
)
1676 (eq (coding-system-type last-coding-system-used
) 5))
1677 ;; For coding systems no-conversion and raw-text...,
1678 ;; edit the buffer as unibyte.
1679 (set-buffer-multibyte nil
))
1680 ;; Run hooks including the uncompression hook.
1681 ;; If they change the file name, then change it for the output also.
1682 (let ((buffer-file-name filename
)
1683 (default-major-mode 'emacs-lisp-mode
)
1684 ;; Ignore unsafe local variables.
1685 ;; We only care about a few of them for our purposes.
1686 (enable-local-variables :safe
)
1687 (enable-local-eval nil
))
1688 ;; Arg of t means don't alter enable-local-variables.
1690 (setq filename buffer-file-name
))
1691 ;; Set the default directory, in case an eval-when-compile uses it.
1692 (setq default-directory
(file-name-directory filename
)))
1693 ;; Check if the file's local variables explicitly specify not to
1694 ;; compile this file.
1695 (if (with-current-buffer input-buffer no-byte-compile
)
1697 ;; (message "%s not compiled because of `no-byte-compile: %s'"
1698 ;; (file-relative-name filename)
1699 ;; (with-current-buffer input-buffer no-byte-compile))
1700 (when (file-exists-p target-file
)
1701 (message "%s deleted because of `no-byte-compile: %s'"
1702 (file-relative-name target-file
)
1703 (buffer-local-value 'no-byte-compile input-buffer
))
1704 (condition-case nil
(delete-file target-file
) (error nil
)))
1705 ;; We successfully didn't compile this file.
1707 (when byte-compile-verbose
1708 (message "Compiling %s..." filename
))
1709 (setq byte-compiler-error-flag nil
)
1710 ;; It is important that input-buffer not be current at this call,
1711 ;; so that the value of point set in input-buffer
1712 ;; within byte-compile-from-buffer lingers in that buffer.
1714 (save-current-buffer
1715 (byte-compile-from-buffer input-buffer filename
)))
1716 (if byte-compiler-error-flag
1718 (when byte-compile-verbose
1719 (message "Compiling %s...done" filename
))
1720 (kill-buffer input-buffer
)
1721 (with-current-buffer output-buffer
1722 (goto-char (point-max))
1723 (insert "\n") ; aaah, unix.
1724 (let ((vms-stmlf-recfm t
))
1725 (if (file-writable-p target-file
)
1726 ;; We must disable any code conversion here.
1727 (let ((coding-system-for-write 'no-conversion
))
1728 (if (memq system-type
'(ms-dos 'windows-nt
))
1729 (setq buffer-file-type t
))
1730 (when (file-exists-p target-file
)
1731 ;; Remove the target before writing it, so that any
1732 ;; hard-links continue to point to the old file (this makes
1733 ;; it possible for installed files to share disk space with
1734 ;; the build tree, without causing problems when emacs-lisp
1735 ;; files in the build tree are recompiled).
1736 (delete-file target-file
))
1737 (write-region (point-min) (point-max) target-file
))
1738 ;; This is just to give a better error message than write-region
1740 (list "Opening output file"
1741 (if (file-exists-p target-file
)
1742 "cannot overwrite file"
1743 "directory not writable or nonexistent")
1745 (kill-buffer (current-buffer)))
1746 (if (and byte-compile-generate-call-tree
1747 (or (eq t byte-compile-generate-call-tree
)
1748 (y-or-n-p (format "Report call tree for %s? " filename
))))
1750 (display-call-tree filename
)))
1755 ;;(defun byte-compile-and-load-file (&optional filename)
1756 ;; "Compile a file of Lisp code named FILENAME into a file of byte code,
1757 ;;and then load it. The output file's name is made by appending \"c\" to
1758 ;;the end of FILENAME."
1760 ;; (if filename ; I don't get it, (interactive-p) doesn't always work
1761 ;; (byte-compile-file filename t)
1762 ;; (let ((current-prefix-arg '(4)))
1763 ;; (call-interactively 'byte-compile-file))))
1765 ;;(defun byte-compile-buffer (&optional buffer)
1766 ;; "Byte-compile and evaluate contents of BUFFER (default: the current buffer)."
1767 ;; (interactive "bByte compile buffer: ")
1768 ;; (setq buffer (if buffer (get-buffer buffer) (current-buffer)))
1769 ;; (message "Compiling %s..." (buffer-name buffer))
1770 ;; (let* ((filename (or (buffer-file-name buffer)
1771 ;; (concat "#<buffer " (buffer-name buffer) ">")))
1772 ;; (byte-compile-current-file buffer))
1773 ;; (byte-compile-from-buffer buffer nil))
1774 ;; (message "Compiling %s...done" (buffer-name buffer))
1777 ;;; compiling a single function
1779 (defun compile-defun (&optional arg
)
1780 "Compile and evaluate the current top-level form.
1781 Print the result in the echo area.
1782 With argument, insert value in current buffer after the form."
1786 (beginning-of-defun)
1787 (let* ((byte-compile-current-file nil
)
1788 (byte-compile-current-buffer (current-buffer))
1789 (byte-compile-read-position (point))
1790 (byte-compile-last-position byte-compile-read-position
)
1791 (byte-compile-last-warned-form 'nothing
)
1793 (let ((read-with-symbol-positions (current-buffer))
1794 (read-symbol-positions-list nil
))
1795 (displaying-byte-compile-warnings
1796 (byte-compile-sexp (read (current-buffer))))))))
1798 (message "Compiling from buffer... done.")
1799 (prin1 value
(current-buffer))
1801 ((message "%s" (prin1-to-string value
)))))))
1804 (defun byte-compile-from-buffer (inbuffer &optional filename
)
1805 ;; Filename is used for the loading-into-Emacs-18 error message.
1807 (byte-compile-current-buffer inbuffer
)
1808 (byte-compile-read-position nil
)
1809 (byte-compile-last-position nil
)
1810 ;; Prevent truncation of flonums and lists as we read and print them
1811 (float-output-format nil
)
1812 (case-fold-search nil
)
1815 ;; Prevent edebug from interfering when we compile
1816 ;; and put the output into a file.
1817 ;; (edebug-all-defs nil)
1818 ;; (edebug-all-forms nil)
1819 ;; Simulate entry to byte-compile-top-level
1820 (byte-compile-constants nil
)
1821 (byte-compile-variables nil
)
1822 (byte-compile-tag-number 0)
1823 (byte-compile-depth 0)
1824 (byte-compile-maxdepth 0)
1825 (byte-compile-output nil
)
1826 ;; This allows us to get the positions of symbols read; it's
1827 ;; new in Emacs 22.1.
1828 (read-with-symbol-positions inbuffer
)
1829 (read-symbol-positions-list nil
)
1830 ;; #### This is bound in b-c-close-variables.
1831 ;; (byte-compile-warnings (if (eq byte-compile-warnings t)
1832 ;; byte-compile-warning-types
1833 ;; byte-compile-warnings))
1835 (byte-compile-close-variables
1836 (with-current-buffer
1837 (setq outbuffer
(get-buffer-create " *Compiler Output*"))
1838 (set-buffer-multibyte t
)
1840 ;; (emacs-lisp-mode)
1841 (setq case-fold-search nil
)
1842 ;; This is a kludge. Some operating systems (OS/2, DOS) need to
1843 ;; write files containing binary information specially.
1844 ;; Under most circumstances, such files will be in binary
1845 ;; overwrite mode, so those OS's use that flag to guess how
1846 ;; they should write their data. Advise them that .elc files
1847 ;; need to be written carefully.
1848 (setq overwrite-mode
'overwrite-mode-binary
))
1849 (displaying-byte-compile-warnings
1850 (and filename
(byte-compile-insert-header filename inbuffer outbuffer
))
1851 (with-current-buffer inbuffer
1852 (goto-char (point-min))
1853 ;; Should we always do this? When calling multiple files, it
1854 ;; would be useful to delay this warning until all have been
1855 ;; compiled. A: Yes! b-c-u-f might contain dross from a
1856 ;; previous byte-compile.
1857 (setq byte-compile-unresolved-functions nil
)
1859 ;; Compile the forms from the input buffer.
1861 (while (progn (skip-chars-forward " \t\n\^l")
1865 (setq byte-compile-read-position
(point)
1866 byte-compile-last-position byte-compile-read-position
)
1867 (let ((form (read inbuffer
)))
1868 (byte-compile-file-form form
)))
1869 ;; Compile pending forms at end of file.
1870 (byte-compile-flush-pending)
1871 ;; Make warnings about unresolved functions
1872 ;; give the end of the file as their position.
1873 (setq byte-compile-last-position
(point-max))
1874 (byte-compile-warn-about-unresolved-functions))
1875 ;; Fix up the header at the front of the output
1876 ;; if the buffer contains multibyte characters.
1877 (and filename
(byte-compile-fix-header filename inbuffer outbuffer
))))
1880 (defun byte-compile-fix-header (filename inbuffer outbuffer
)
1881 (with-current-buffer outbuffer
1882 ;; See if the buffer has any multibyte characters.
1883 (when (< (point-max) (position-bytes (point-max)))
1884 (when (byte-compile-version-cond byte-compile-compatibility
)
1885 (error "Version-18 compatibility not valid with multibyte characters"))
1886 (goto-char (point-min))
1887 ;; Find the comment that describes the version test.
1888 (search-forward "\n;;; This file")
1890 (narrow-to-region (point) (point-max))
1891 ;; Find the line of ballast semicolons.
1892 (search-forward ";;;;;;;;;;")
1895 (narrow-to-region (point-min) (point))
1896 (let ((old-header-end (point))
1898 (goto-char (point-min))
1899 (delete-region (point) (progn (re-search-forward "^(")
1902 (insert ";;; This file contains multibyte non-ASCII characters\n"
1903 ";;; and therefore cannot be loaded into Emacs 19.\n")
1904 ;; Replace "19" or "19.29" with "20", twice.
1905 (re-search-forward "19\\(\\.[0-9]+\\)")
1906 (replace-match "20")
1907 (re-search-forward "19\\(\\.[0-9]+\\)")
1908 (replace-match "20")
1909 ;; Now compensate for the change in size,
1910 ;; to make sure all positions in the file remain valid.
1911 (setq delta
(- (point-max) old-header-end
))
1912 (goto-char (point-max))
1914 (delete-char delta
)))))
1916 (defun byte-compile-insert-header (filename inbuffer outbuffer
)
1917 (set-buffer inbuffer
)
1918 (let ((dynamic-docstrings byte-compile-dynamic-docstrings
)
1919 (dynamic byte-compile-dynamic
))
1920 (set-buffer outbuffer
)
1921 (goto-char (point-min))
1922 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After
1923 ;; that is the file-format version number (18, 19 or 20) as a
1924 ;; byte, followed by some nulls. The primary motivation for doing
1925 ;; this is to get some binary characters up in the first line of
1926 ;; the file so that `diff' will simply say "Binary files differ"
1927 ;; instead of actually doing a diff of two .elc files. An extra
1928 ;; benefit is that you can add this to /etc/magic:
1930 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
1931 ;; >4 byte x version %d
1935 (if (byte-compile-version-cond byte-compile-compatibility
) 18 20)
1938 (insert ";;; Compiled by "
1939 (or (and (boundp 'user-mail-address
) user-mail-address
)
1940 (concat (user-login-name) "@" (system-name)))
1942 (current-time-string) "\n;;; from file " filename
"\n")
1943 (insert ";;; in Emacs version " emacs-version
"\n")
1946 ((eq byte-optimize
'source
) "with source-level optimization only")
1947 ((eq byte-optimize
'byte
) "with byte-level optimization only")
1948 (byte-optimize "with all optimizations")
1949 (t "without optimization"))
1950 (if (byte-compile-version-cond byte-compile-compatibility
)
1951 "; compiled with Emacs 18 compatibility.\n"
1954 (insert ";;; Function definitions are lazy-loaded.\n"))
1955 (if (not (byte-compile-version-cond byte-compile-compatibility
))
1956 (let (intro-string minimum-version
)
1957 ;; Figure out which Emacs version to require,
1958 ;; and what comment to use to explain why.
1959 ;; Note that this fails to take account of whether
1960 ;; the buffer contains multibyte characters. We may have to
1961 ;; compensate at the end in byte-compile-fix-header.
1962 (if dynamic-docstrings
1964 ";;; This file uses dynamic docstrings, first added in Emacs 19.29.\n"
1965 minimum-version
"19.29")
1967 ";;; This file uses opcodes which do not exist in Emacs 18.\n"
1968 minimum-version
"19"))
1969 ;; Now insert the comment and the error check.
1973 ;; Have to check if emacs-version is bound so that this works
1974 ;; in files loaded early in loadup.el.
1975 "(if (and (boundp 'emacs-version)\n"
1976 ;; If there is a name at the end of emacs-version,
1977 ;; don't try to check the version number.
1978 "\t (< (aref emacs-version (1- (length emacs-version))) ?A)\n"
1979 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
1980 (format "\t (string-lessp emacs-version \"%s\")))\n"
1983 ;; prin1-to-string is used to quote backslashes.
1984 (substring (prin1-to-string (file-name-nondirectory filename
))
1986 (format "' was compiled for Emacs %s or later\"))\n\n"
1988 ;; Insert semicolons as ballast, so that byte-compile-fix-header
1989 ;; can delete them so as to keep the buffer positions
1990 ;; constant for the actual compiled code.
1991 ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n"))
1992 ;; Here if we want Emacs 18 compatibility.
1993 (when dynamic-docstrings
1994 (error "Version-18 compatibility doesn't support dynamic doc strings"))
1995 (when byte-compile-dynamic
1996 (error "Version-18 compatibility doesn't support dynamic byte code"))
1997 (insert "(or (boundp 'current-load-list) (setq current-load-list nil))\n"
2000 (defun byte-compile-output-file-form (form)
2001 ;; writes the given form to the output buffer, being careful of docstrings
2002 ;; in defun, defmacro, defvar, defconst, autoload and
2003 ;; custom-declare-variable because make-docfile is so amazingly stupid.
2004 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
2005 ;; it does not pay to first build the defalias in defmumble and then parse
2007 (if (and (memq (car-safe form
) '(defun defmacro defvar defconst autoload
2008 custom-declare-variable
))
2009 (stringp (nth 3 form
)))
2010 (byte-compile-output-docform nil nil
'("\n(" 3 ")") form nil
2012 '(autoload custom-declare-variable
)))
2013 (let ((print-escape-newlines t
)
2018 (print-circle ; handle circular data structures
2019 (not byte-compile-disable-print-circle
)))
2020 (princ "\n" outbuffer
)
2021 (prin1 form outbuffer
)
2024 (defvar print-gensym-alist
) ;Used before print-circle existed.
2026 (defun byte-compile-output-docform (preface name info form specindex quoted
)
2027 "Print a form with a doc string. INFO is (prefix doc-index postfix).
2028 If PREFACE and NAME are non-nil, print them too,
2029 before INFO and the FORM but after the doc string itself.
2030 If SPECINDEX is non-nil, it is the index in FORM
2031 of the function bytecode string. In that case,
2032 we output that argument and the following argument (the constants vector)
2033 together, for lazy loading.
2034 QUOTED says that we have to put a quote before the
2035 list that represents a doc string reference.
2036 `autoload' and `custom-declare-variable' need that."
2037 ;; We need to examine byte-compile-dynamic-docstrings
2038 ;; in the input buffer (now current), not in the output buffer.
2039 (let ((dynamic-docstrings byte-compile-dynamic-docstrings
))
2041 (prog1 (current-buffer)
2042 (set-buffer outbuffer
)
2045 ;; Insert the doc string, and make it a comment with #@LENGTH.
2046 (and (>= (nth 1 info
) 0)
2048 (not byte-compile-compatibility
)
2050 ;; Make the doc string start at beginning of line
2051 ;; for make-docfile's sake.
2054 (byte-compile-output-as-comment
2055 (nth (nth 1 info
) form
) nil
))
2056 (setq position
(- (position-bytes position
) (point-min) -
1))
2057 ;; If the doc string starts with * (a user variable),
2059 (if (and (stringp (nth (nth 1 info
) form
))
2060 (> (length (nth (nth 1 info
) form
)) 0)
2061 (eq (aref (nth (nth 1 info
) form
) 0) ?
*))
2062 (setq position
(- position
)))))
2067 (prin1 name outbuffer
)))
2069 (let ((print-escape-newlines t
)
2071 ;; For compatibility with code before print-circle,
2072 ;; use a cons cell to say that we want
2073 ;; print-gensym-alist not to be cleared
2074 ;; between calls to print functions.
2076 (print-circle ; handle circular data structures
2077 (not byte-compile-disable-print-circle
))
2078 print-gensym-alist
; was used before print-circle existed.
2079 (print-continuous-numbering t
)
2082 (prin1 (car form
) outbuffer
)
2083 (while (setq form
(cdr form
))
2084 (setq index
(1+ index
))
2086 (cond ((and (numberp specindex
) (= index specindex
)
2087 ;; Don't handle the definition dynamically
2088 ;; if it refers (or might refer)
2089 ;; to objects already output
2090 ;; (for instance, gensyms in the arg list).
2092 (dotimes (i (length print-number-table
))
2093 (if (aref print-number-table i
)
2096 ;; Output the byte code and constants specially
2097 ;; for lazy dynamic loading.
2099 (byte-compile-output-as-comment
2100 (cons (car form
) (nth 1 form
))
2102 (setq position
(- (position-bytes position
) (point-min) -
1))
2103 (princ (format "(#$ . %d) nil" position
) outbuffer
)
2104 (setq form
(cdr form
))
2105 (setq index
(1+ index
))))
2106 ((= index
(nth 1 info
))
2108 (princ (format (if quoted
"'(#$ . %d)" "(#$ . %d)")
2111 (let ((print-escape-newlines nil
))
2112 (goto-char (prog1 (1+ (point))
2113 (prin1 (car form
) outbuffer
)))
2115 (goto-char (point-max)))))
2117 (prin1 (car form
) outbuffer
)))))
2118 (insert (nth 2 info
))))))
2121 (defun byte-compile-keep-pending (form &optional handler
)
2122 (if (memq byte-optimize
'(t source
))
2123 (setq form
(byte-optimize-form form t
)))
2125 (let ((for-effect t
))
2126 ;; To avoid consing up monstrously large forms at load time, we split
2127 ;; the output regularly.
2128 (and (memq (car-safe form
) '(fset defalias
))
2129 (nthcdr 300 byte-compile-output
)
2130 (byte-compile-flush-pending))
2131 (funcall handler form
)
2133 (byte-compile-discard)))
2134 (byte-compile-form form t
))
2137 (defun byte-compile-flush-pending ()
2138 (if byte-compile-output
2139 (let ((form (byte-compile-out-toplevel t
'file
)))
2140 (cond ((eq (car-safe form
) 'progn
)
2141 (mapc 'byte-compile-output-file-form
(cdr form
)))
2143 (byte-compile-output-file-form form
)))
2144 (setq byte-compile-constants nil
2145 byte-compile-variables nil
2146 byte-compile-depth
0
2147 byte-compile-maxdepth
0
2148 byte-compile-output nil
))))
2150 (defun byte-compile-file-form (form)
2151 (let ((byte-compile-current-form nil
) ; close over this for warnings.
2155 (byte-compile-keep-pending form
))
2156 ((and (symbolp (car form
))
2157 (setq handler
(get (car form
) 'byte-hunk-handler
)))
2158 (cond ((setq form
(funcall handler form
))
2159 (byte-compile-flush-pending)
2160 (byte-compile-output-file-form form
))))
2161 ((eq form
(setq form
(macroexpand form byte-compile-macro-environment
)))
2162 (byte-compile-keep-pending form
))
2164 (byte-compile-file-form form
)))))
2166 ;; Functions and variables with doc strings must be output separately,
2167 ;; so make-docfile can recognise them. Most other things can be output
2170 (put 'defsubst
'byte-hunk-handler
'byte-compile-file-form-defsubst
)
2171 (defun byte-compile-file-form-defsubst (form)
2172 (when (assq (nth 1 form
) byte-compile-unresolved-functions
)
2173 (setq byte-compile-current-form
(nth 1 form
))
2174 (byte-compile-warn "defsubst `%s' was used before it was defined"
2176 (byte-compile-file-form
2177 (macroexpand form byte-compile-macro-environment
))
2178 ;; Return nil so the form is not output twice.
2181 (put 'autoload
'byte-hunk-handler
'byte-compile-file-form-autoload
)
2182 (defun byte-compile-file-form-autoload (form)
2183 (and (let ((form form
))
2184 (while (if (setq form
(cdr form
)) (byte-compile-constp (car form
))))
2185 (null form
)) ;Constants only
2186 (eval (nth 5 form
)) ;Macro
2187 (eval form
)) ;Define the autoload.
2188 ;; Avoid undefined function warnings for the autoload.
2189 (if (and (consp (nth 1 form
))
2190 (eq (car (nth 1 form
)) 'quote
)
2191 (consp (cdr (nth 1 form
)))
2192 (symbolp (nth 1 (nth 1 form
))))
2193 (push (cons (nth 1 (nth 1 form
))
2194 (cons 'autoload
(cdr (cdr form
))))
2195 byte-compile-function-environment
))
2196 (if (stringp (nth 3 form
))
2198 ;; No doc string, so we can compile this as a normal form.
2199 (byte-compile-keep-pending form
'byte-compile-normal-call
)))
2201 (put 'defvar
'byte-hunk-handler
'byte-compile-file-form-defvar
)
2202 (put 'defconst
'byte-hunk-handler
'byte-compile-file-form-defvar
)
2203 (defun byte-compile-file-form-defvar (form)
2204 (if (null (nth 3 form
))
2205 ;; Since there is no doc string, we can compile this as a normal form,
2206 ;; and not do a file-boundary.
2207 (byte-compile-keep-pending form
)
2208 (when (memq 'free-vars byte-compile-warnings
)
2209 (push (nth 1 form
) byte-compile-bound-variables
)
2210 (if (eq (car form
) 'defconst
)
2211 (push (nth 1 form
) byte-compile-const-variables
)))
2212 (cond ((consp (nth 2 form
))
2213 (setq form
(copy-sequence form
))
2214 (setcar (cdr (cdr form
))
2215 (byte-compile-top-level (nth 2 form
) nil
'file
))))
2218 (put 'custom-declare-variable
'byte-hunk-handler
2219 'byte-compile-file-form-custom-declare-variable
)
2220 (defun byte-compile-file-form-custom-declare-variable (form)
2221 (when (memq 'callargs byte-compile-warnings
)
2222 (byte-compile-nogroup-warn form
))
2223 (when (memq 'free-vars byte-compile-warnings
)
2224 (push (nth 1 (nth 1 form
)) byte-compile-bound-variables
))
2225 (let ((tail (nthcdr 4 form
)))
2227 ;; If there are any (function (lambda ...)) expressions, compile
2229 (if (and (consp (car tail
))
2230 (eq (car (car tail
)) 'function
)
2231 (consp (nth 1 (car tail
))))
2232 (setcar tail
(byte-compile-lambda (nth 1 (car tail
))))
2233 ;; Likewise for a bare lambda.
2234 (if (and (consp (car tail
))
2235 (eq (car (car tail
)) 'lambda
))
2236 (setcar tail
(byte-compile-lambda (car tail
)))))
2237 (setq tail
(cdr tail
))))
2240 (put 'require
'byte-hunk-handler
'byte-compile-file-form-require
)
2241 (defun byte-compile-file-form-require (form)
2242 (let ((args (mapcar 'eval
(cdr form
))))
2243 (apply 'require args
)
2244 ;; Detect (require 'cl) in a way that works even if cl is already loaded.
2245 (if (member (car args
) '("cl" cl
))
2246 (setq byte-compile-warnings
2247 (remq 'cl-functions byte-compile-warnings
))))
2248 (byte-compile-keep-pending form
'byte-compile-normal-call
))
2250 (put 'progn
'byte-hunk-handler
'byte-compile-file-form-progn
)
2251 (put 'prog1
'byte-hunk-handler
'byte-compile-file-form-progn
)
2252 (put 'prog2
'byte-hunk-handler
'byte-compile-file-form-progn
)
2253 (defun byte-compile-file-form-progn (form)
2254 (mapc 'byte-compile-file-form
(cdr form
))
2255 ;; Return nil so the forms are not output twice.
2258 ;; This handler is not necessary, but it makes the output from dont-compile
2259 ;; and similar macros cleaner.
2260 (put 'eval
'byte-hunk-handler
'byte-compile-file-form-eval
)
2261 (defun byte-compile-file-form-eval (form)
2262 (if (eq (car-safe (nth 1 form
)) 'quote
)
2263 (nth 1 (nth 1 form
))
2264 (byte-compile-keep-pending form
)))
2266 (put 'defun
'byte-hunk-handler
'byte-compile-file-form-defun
)
2267 (defun byte-compile-file-form-defun (form)
2268 (byte-compile-file-form-defmumble form nil
))
2270 (put 'defmacro
'byte-hunk-handler
'byte-compile-file-form-defmacro
)
2271 (defun byte-compile-file-form-defmacro (form)
2272 (byte-compile-file-form-defmumble form t
))
2274 (defun byte-compile-file-form-defmumble (form macrop
)
2275 (let* ((name (car (cdr form
)))
2276 (this-kind (if macrop
'byte-compile-macro-environment
2277 'byte-compile-function-environment
))
2278 (that-kind (if macrop
'byte-compile-function-environment
2279 'byte-compile-macro-environment
))
2280 (this-one (assq name
(symbol-value this-kind
)))
2281 (that-one (assq name
(symbol-value that-kind
)))
2282 (byte-compile-free-references nil
)
2283 (byte-compile-free-assignments nil
))
2284 (byte-compile-set-symbol-position name
)
2285 ;; When a function or macro is defined, add it to the call tree so that
2286 ;; we can tell when functions are not used.
2287 (if byte-compile-generate-call-tree
2288 (or (assq name byte-compile-call-tree
)
2289 (setq byte-compile-call-tree
2290 (cons (list name nil nil
) byte-compile-call-tree
))))
2292 (setq byte-compile-current-form name
) ; for warnings
2293 (if (memq 'redefine byte-compile-warnings
)
2294 (byte-compile-arglist-warn form macrop
))
2295 (if byte-compile-verbose
2296 (message "Compiling %s... (%s)" (or filename
"") (nth 1 form
)))
2298 (if (and (memq 'redefine byte-compile-warnings
)
2299 ;; don't warn when compiling the stubs in byte-run...
2300 (not (assq (nth 1 form
)
2301 byte-compile-initial-macro-environment
)))
2303 "`%s' defined multiple times, as both function and macro"
2305 (setcdr that-one nil
))
2307 (when (and (memq 'redefine byte-compile-warnings
)
2308 ;; hack: don't warn when compiling the magic internal
2309 ;; byte-compiler macros in byte-run.el...
2310 (not (assq (nth 1 form
)
2311 byte-compile-initial-macro-environment
)))
2312 (byte-compile-warn "%s `%s' defined multiple times in this file"
2313 (if macrop
"macro" "function")
2315 ((and (fboundp name
)
2316 (eq (car-safe (symbol-function name
))
2317 (if macrop
'lambda
'macro
)))
2318 (when (memq 'redefine byte-compile-warnings
)
2319 (byte-compile-warn "%s `%s' being redefined as a %s"
2320 (if macrop
"function" "macro")
2322 (if macrop
"macro" "function")))
2323 ;; shadow existing definition
2325 (cons (cons name nil
) (symbol-value this-kind
))))
2327 (let ((body (nthcdr 3 form
)))
2328 (when (and (stringp (car body
))
2329 (symbolp (car-safe (cdr-safe body
)))
2330 (car-safe (cdr-safe body
))
2331 (stringp (car-safe (cdr-safe (cdr-safe body
)))))
2332 (byte-compile-set-symbol-position (nth 1 form
))
2333 (byte-compile-warn "probable `\"' without `\\' in doc string of %s"
2336 ;; Generate code for declarations in macro definitions.
2337 ;; Remove declarations from the body of the macro definition.
2339 (let ((tail (nthcdr 2 form
)))
2340 (when (stringp (car (cdr tail
)))
2341 (setq tail
(cdr tail
)))
2342 (while (and (consp (car (cdr tail
)))
2343 (eq (car (car (cdr tail
))) 'declare
))
2344 (let ((declaration (car (cdr tail
))))
2345 (setcdr tail
(cdr (cdr tail
)))
2346 (prin1 `(if macro-declaration-function
2347 (funcall macro-declaration-function
2348 ',name
',declaration
))
2351 (let* ((new-one (byte-compile-lambda (nthcdr 2 form
) t
))
2352 (code (byte-compile-byte-code-maker new-one
)))
2354 (setcdr this-one new-one
)
2356 (cons (cons name new-one
) (symbol-value this-kind
))))
2357 (if (and (stringp (nth 3 form
))
2358 (eq 'quote
(car-safe code
))
2359 (eq 'lambda
(car-safe (nth 1 code
))))
2361 (cons name
(cdr (nth 1 code
))))
2362 (byte-compile-flush-pending)
2363 (if (not (stringp (nth 3 form
)))
2364 ;; No doc string. Provide -1 as the "doc string index"
2365 ;; so that no element will be treated as a doc string.
2366 (byte-compile-output-docform
2367 (if (byte-compile-version-cond byte-compile-compatibility
)
2368 "\n(fset '" "\n(defalias '")
2371 (if macrop
'(" '(macro . #[" -
1 "])") '(" #[" -
1 "]")))
2372 ((eq (car code
) 'quote
)
2374 (if macrop
'(" '(macro " -
1 ")") '(" '(" -
1 ")")))
2375 ((if macrop
'(" (cons 'macro (" -
1 "))") '(" (" -
1 ")"))))
2377 (and (atom code
) byte-compile-dynamic
2380 ;; Output the form by hand, that's much simpler than having
2381 ;; b-c-output-file-form analyze the defalias.
2382 (byte-compile-output-docform
2383 (if (byte-compile-version-cond byte-compile-compatibility
)
2384 "\n(fset '" "\n(defalias '")
2387 (if macrop
'(" '(macro . #[" 4 "])") '(" #[" 4 "]")))
2388 ((eq (car code
) 'quote
)
2390 (if macrop
'(" '(macro " 2 ")") '(" '(" 2 ")")))
2391 ((if macrop
'(" (cons 'macro (" 5 "))") '(" (" 5 ")"))))
2393 (and (atom code
) byte-compile-dynamic
2396 (princ ")" outbuffer
)
2399 ;; Print Lisp object EXP in the output file, inside a comment,
2400 ;; and return the file position it will have.
2401 ;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting.
2402 (defun byte-compile-output-as-comment (exp quoted
)
2403 (let ((position (point)))
2405 (prog1 (current-buffer)
2406 (set-buffer outbuffer
)
2408 ;; Insert EXP, and make it a comment with #@LENGTH.
2411 (prin1 exp outbuffer
)
2412 (princ exp outbuffer
))
2413 (goto-char position
)
2414 ;; Quote certain special characters as needed.
2415 ;; get_doc_string in doc.c does the unquoting.
2416 (while (search-forward "\^A" nil t
)
2417 (replace-match "\^A\^A" t t
))
2418 (goto-char position
)
2419 (while (search-forward "\000" nil t
)
2420 (replace-match "\^A0" t t
))
2421 (goto-char position
)
2422 (while (search-forward "\037" nil t
)
2423 (replace-match "\^A_" t t
))
2424 (goto-char (point-max))
2426 (goto-char position
)
2427 (insert "#@" (format "%d" (- (position-bytes (point-max))
2428 (position-bytes position
))))
2430 ;; Save the file position of the object.
2431 ;; Note we should add 1 to skip the space
2432 ;; that we inserted before the actual doc string,
2433 ;; and subtract 1 to convert from an 1-origin Emacs position
2434 ;; to a file position; they cancel.
2435 (setq position
(point))
2436 (goto-char (point-max))))
2442 (defun byte-compile (form)
2443 "If FORM is a symbol, byte-compile its function definition.
2444 If FORM is a lambda or a macro, byte-compile it as a function."
2445 (displaying-byte-compile-warnings
2446 (byte-compile-close-variables
2447 (let* ((fun (if (symbolp form
)
2448 (and (fboundp form
) (symbol-function form
))
2450 (macro (eq (car-safe fun
) 'macro
)))
2452 (setq fun
(cdr fun
)))
2453 (cond ((eq (car-safe fun
) 'lambda
)
2455 (cons 'macro
(byte-compile-lambda fun
))
2456 (byte-compile-lambda fun
)))
2461 (defun byte-compile-sexp (sexp)
2462 "Compile and return SEXP."
2463 (displaying-byte-compile-warnings
2464 (byte-compile-close-variables
2465 (byte-compile-top-level sexp
))))
2467 ;; Given a function made by byte-compile-lambda, make a form which produces it.
2468 (defun byte-compile-byte-code-maker (fun)
2470 ((byte-compile-version-cond byte-compile-compatibility
)
2471 ;; Return (quote (lambda ...)).
2472 (list 'quote
(byte-compile-byte-code-unmake fun
)))
2473 ;; ## atom is faster than compiled-func-p.
2474 ((atom fun
) ; compiled function.
2475 ;; generate-emacs19-bytecodes must be on, otherwise byte-compile-lambda
2476 ;; would have produced a lambda.
2478 ;; b-c-lambda didn't produce a compiled-function, so it's either a trivial
2479 ;; function, or this is Emacs 18, or generate-emacs19-bytecodes is off.
2481 (if (and (setq tmp
(assq 'byte-code
(cdr-safe (cdr fun
))))
2482 (null (cdr (memq tmp fun
))))
2483 ;; Generate a make-byte-code call.
2484 (let* ((interactive (assq 'interactive
(cdr (cdr fun
)))))
2485 (nconc (list 'make-byte-code
2486 (list 'quote
(nth 1 fun
)) ;arglist
2490 (cond ((stringp (nth 2 fun
))
2491 (list (nth 2 fun
))) ;doc
2495 (list (if (or (null (nth 1 interactive
))
2496 (stringp (nth 1 interactive
)))
2498 ;; Interactive spec is a list or a variable
2499 ;; (if it is correct).
2500 (list 'quote
(nth 1 interactive
))))))))
2501 ;; a non-compiled function (probably trivial)
2502 (list 'quote fun
))))))
2504 ;; Turn a function into an ordinary lambda. Needed for v18 files.
2505 (defun byte-compile-byte-code-unmake (function)
2506 (if (consp function
)
2507 function
;;It already is a lambda.
2508 (setq function
(append function nil
)) ; turn it into a list
2509 (nconc (list 'lambda
(nth 0 function
))
2510 (and (nth 4 function
) (list (nth 4 function
)))
2511 (if (nthcdr 5 function
)
2512 (list (cons 'interactive
(if (nth 5 function
)
2513 (nthcdr 5 function
)))))
2514 (list (list 'byte-code
2515 (nth 1 function
) (nth 2 function
)
2516 (nth 3 function
))))))
2519 (defun byte-compile-check-lambda-list (list)
2520 "Check lambda-list LIST for errors."
2523 (let ((arg (car list
)))
2525 (byte-compile-set-symbol-position arg
))
2526 (cond ((or (not (symbolp arg
))
2527 (byte-compile-const-symbol-p arg t
))
2528 (error "Invalid lambda variable %s" arg
))
2531 (error "&rest without variable name"))
2533 (error "Garbage following &rest VAR in lambda-list")))
2534 ((eq arg
'&optional
)
2536 (error "Variable name missing after &optional")))
2538 (byte-compile-warn "repeated variable %s in lambda-list" arg
))
2541 (setq list
(cdr list
)))))
2544 ;; Byte-compile a lambda-expression and return a valid function.
2545 ;; The value is usually a compiled function but may be the original
2546 ;; lambda-expression.
2547 ;; When ADD-LAMBDA is non-nil, the symbol `lambda' is added as head
2548 ;; of the list FUN and `byte-compile-set-symbol-position' is not called.
2549 ;; Use this feature to avoid calling `byte-compile-set-symbol-position'
2550 ;; for symbols generated by the byte compiler itself.
2551 (defun byte-compile-lambda (fun &optional add-lambda
)
2553 (setq fun
(cons 'lambda fun
))
2554 (unless (eq 'lambda
(car-safe fun
))
2555 (error "Not a lambda list: %S" fun
))
2556 (byte-compile-set-symbol-position 'lambda
))
2557 (byte-compile-check-lambda-list (nth 1 fun
))
2558 (let* ((arglist (nth 1 fun
))
2559 (byte-compile-bound-variables
2560 (nconc (and (memq 'free-vars byte-compile-warnings
)
2561 (delq '&rest
(delq '&optional
(copy-sequence arglist
))))
2562 byte-compile-bound-variables
))
2563 (body (cdr (cdr fun
)))
2564 (doc (if (stringp (car body
))
2566 ;; Discard the doc string
2567 ;; unless it is the last element of the body.
2569 (setq body
(cdr body
))))))
2570 (int (assq 'interactive body
)))
2571 ;; Process the interactive spec.
2573 (byte-compile-set-symbol-position 'interactive
)
2574 ;; Skip (interactive) if it is in front (the most usual location).
2575 (if (eq int
(car body
))
2576 (setq body
(cdr body
)))
2577 (cond ((consp (cdr int
))
2579 (byte-compile-warn "malformed interactive spec: %s"
2580 (prin1-to-string int
)))
2581 ;; If the interactive spec is a call to `list', don't
2582 ;; compile it, because `call-interactively' looks at the
2583 ;; args of `list'. Actually, compile it to get warnings,
2584 ;; but don't use the result.
2585 (let ((form (nth 1 int
)))
2586 (while (memq (car-safe form
) '(let let
* progn save-excursion
))
2587 (while (consp (cdr form
))
2588 (setq form
(cdr form
)))
2589 (setq form
(car form
)))
2590 (if (eq (car-safe form
) 'list
)
2591 (byte-compile-top-level (nth 1 int
))
2592 (setq int
(list 'interactive
2593 (byte-compile-top-level (nth 1 int
)))))))
2595 (byte-compile-warn "malformed interactive spec: %s"
2596 (prin1-to-string int
)))))
2597 ;; Process the body.
2598 (let ((compiled (byte-compile-top-level (cons 'progn body
) nil
'lambda
)))
2599 ;; Build the actual byte-coded function.
2600 (if (and (eq 'byte-code
(car-safe compiled
))
2601 (not (byte-compile-version-cond
2602 byte-compile-compatibility
)))
2603 (apply 'make-byte-code
2604 (append (list arglist
)
2605 ;; byte-string, constants-vector, stack depth
2607 ;; optionally, the doc string.
2610 ;; optionally, the interactive spec.
2612 (list (nth 1 int
)))))
2614 (nconc (if int
(list int
))
2615 (cond ((eq (car-safe compiled
) 'progn
) (cdr compiled
))
2616 (compiled (list compiled
)))))
2617 (nconc (list 'lambda arglist
)
2618 (if (or doc
(stringp (car compiled
)))
2619 (cons doc
(cond (compiled)
2623 (defun byte-compile-constants-vector ()
2624 ;; Builds the constants-vector from the current variables and constants.
2625 ;; This modifies the constants from (const . nil) to (const . offset).
2626 ;; To keep the byte-codes to look up the vector as short as possible:
2627 ;; First 6 elements are vars, as there are one-byte varref codes for those.
2628 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
2629 ;; Next variables again, to get 2-byte codes for variable lookup.
2630 ;; The rest of the constants and variables need 3-byte byte-codes.
2632 (rest (nreverse byte-compile-variables
)) ; nreverse because the first
2633 (other (nreverse byte-compile-constants
)) ; vars often are used most.
2635 (limits '(5 ; Use the 1-byte varref codes,
2636 63 ; 1-constlim ; 1-byte byte-constant codes,
2637 255 ; 2-byte varref codes,
2638 65535)) ; 3-byte codes for the rest.
2640 (while (or rest other
)
2641 (setq limit
(car limits
))
2642 (while (and rest
(not (eq i limit
)))
2643 (if (setq tmp
(assq (car (car rest
)) ret
))
2644 (setcdr (car rest
) (cdr tmp
))
2645 (setcdr (car rest
) (setq i
(1+ i
)))
2646 (setq ret
(cons (car rest
) ret
)))
2647 (setq rest
(cdr rest
)))
2648 (setq limits
(cdr limits
)
2650 (setq other rest
))))
2651 (apply 'vector
(nreverse (mapcar 'car ret
)))))
2653 ;; Given an expression FORM, compile it and return an equivalent byte-code
2654 ;; expression (a call to the function byte-code).
2655 (defun byte-compile-top-level (form &optional for-effect output-type
)
2656 ;; OUTPUT-TYPE advises about how form is expected to be used:
2657 ;; 'eval or nil -> a single form,
2658 ;; 'progn or t -> a list of forms,
2659 ;; 'lambda -> body of a lambda,
2660 ;; 'file -> used at file-level.
2661 (let ((byte-compile-constants nil
)
2662 (byte-compile-variables nil
)
2663 (byte-compile-tag-number 0)
2664 (byte-compile-depth 0)
2665 (byte-compile-maxdepth 0)
2666 (byte-compile-output nil
))
2667 (if (memq byte-optimize
'(t source
))
2668 (setq form
(byte-optimize-form form for-effect
)))
2669 (while (and (eq (car-safe form
) 'progn
) (null (cdr (cdr form
))))
2670 (setq form
(nth 1 form
)))
2671 (if (and (eq 'byte-code
(car-safe form
))
2672 (not (memq byte-optimize
'(t byte
)))
2673 (stringp (nth 1 form
)) (vectorp (nth 2 form
))
2674 (natnump (nth 3 form
)))
2676 (byte-compile-form form for-effect
)
2677 (byte-compile-out-toplevel for-effect output-type
))))
2679 (defun byte-compile-out-toplevel (&optional for-effect output-type
)
2681 ;; The stack is empty. Push a value to be returned from (byte-code ..).
2682 (if (eq (car (car byte-compile-output
)) 'byte-discard
)
2683 (setq byte-compile-output
(cdr byte-compile-output
))
2684 (byte-compile-push-constant
2685 ;; Push any constant - preferably one which already is used, and
2686 ;; a number or symbol - ie not some big sequence. The return value
2687 ;; isn't returned, but it would be a shame if some textually large
2688 ;; constant was not optimized away because we chose to return it.
2689 (and (not (assq nil byte-compile-constants
)) ; Nil is often there.
2690 (let ((tmp (reverse byte-compile-constants
)))
2691 (while (and tmp
(not (or (symbolp (caar tmp
))
2692 (numberp (caar tmp
)))))
2693 (setq tmp
(cdr tmp
)))
2695 (byte-compile-out 'byte-return
0)
2696 (setq byte-compile-output
(nreverse byte-compile-output
))
2697 (if (memq byte-optimize
'(t byte
))
2698 (setq byte-compile-output
2699 (byte-optimize-lapcode byte-compile-output for-effect
)))
2701 ;; Decompile trivial functions:
2702 ;; only constants and variables, or a single funcall except in lambdas.
2703 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
2704 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
2705 ;; Note that even (quote foo) must be parsed just as any subr by the
2706 ;; interpreter, so quote should be compiled into byte-code in some contexts.
2707 ;; What to leave uncompiled:
2708 ;; lambda -> never. we used to leave it uncompiled if the body was
2709 ;; a single atom, but that causes confusion if the docstring
2710 ;; uses the (file . pos) syntax. Besides, now that we have
2711 ;; the Lisp_Compiled type, the compiled form is faster.
2712 ;; eval -> atom, quote or (function atom atom atom)
2713 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
2714 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
2716 (maycall (not (eq output-type
'lambda
))) ; t if we may make a funcall.
2719 ;; #### This should be split out into byte-compile-nontrivial-function-p.
2720 ((or (eq output-type
'lambda
)
2721 (nthcdr (if (eq output-type
'file
) 50 8) byte-compile-output
)
2722 (assq 'TAG byte-compile-output
) ; Not necessary, but speeds up a bit.
2723 (not (setq tmp
(assq 'byte-return byte-compile-output
)))
2725 (setq rest
(nreverse
2726 (cdr (memq tmp
(reverse byte-compile-output
)))))
2728 ((memq (car (car rest
)) '(byte-varref byte-constant
))
2729 (setq tmp
(car (cdr (car rest
))))
2730 (if (if (eq (car (car rest
)) 'byte-constant
)
2733 (not (byte-compile-const-symbol-p tmp
)))))
2735 (setq body
(cons (list 'quote tmp
) body
)))
2736 (setq body
(cons tmp body
))))
2738 ;; Allow a funcall if at most one atom follows it.
2739 (null (nthcdr 3 rest
))
2740 (setq tmp
(get (car (car rest
)) 'byte-opcode-invert
))
2741 (or (null (cdr rest
))
2742 (and (memq output-type
'(file progn t
))
2744 (eq (car (nth 1 rest
)) 'byte-discard
)
2745 (progn (setq rest
(cdr rest
)) t
))))
2746 (setq maycall nil
) ; Only allow one real function call.
2747 (setq body
(nreverse body
))
2749 (if (and (eq tmp
'funcall
)
2750 (eq (car-safe (car body
)) 'quote
))
2751 (cons (nth 1 (car body
)) (cdr body
))
2753 (or (eq output-type
'file
)
2754 (not (delq nil
(mapcar 'consp
(cdr (car body
))))))))
2755 (setq rest
(cdr rest
)))
2757 (let ((byte-compile-vector (byte-compile-constants-vector)))
2758 (list 'byte-code
(byte-compile-lapcode byte-compile-output
)
2759 byte-compile-vector byte-compile-maxdepth
)))
2760 ;; it's a trivial function
2761 ((cdr body
) (cons 'progn
(nreverse body
)))
2764 ;; Given BODY, compile it and return a new body.
2765 (defun byte-compile-top-level-body (body &optional for-effect
)
2766 (setq body
(byte-compile-top-level (cons 'progn body
) for-effect t
))
2767 (cond ((eq (car-safe body
) 'progn
)
2772 ;; This is the recursive entry point for compiling each subform of an
2774 ;; If for-effect is non-nil, byte-compile-form will output a byte-discard
2775 ;; before terminating (ie no value will be left on the stack).
2776 ;; A byte-compile handler may, when for-effect is non-nil, choose output code
2777 ;; which does not leave a value on the stack, and then set for-effect to nil
2778 ;; (to prevent byte-compile-form from outputting the byte-discard).
2779 ;; If a handler wants to call another handler, it should do so via
2780 ;; byte-compile-form, or take extreme care to handle for-effect correctly.
2781 ;; (Use byte-compile-form-do-effect to reset the for-effect flag too.)
2783 (defun byte-compile-form (form &optional for-effect
)
2784 (setq form
(macroexpand form byte-compile-macro-environment
))
2785 (cond ((not (consp form
))
2786 (cond ((or (not (symbolp form
)) (byte-compile-const-symbol-p form
))
2787 (when (symbolp form
)
2788 (byte-compile-set-symbol-position form
))
2789 (byte-compile-constant form
))
2790 ((and for-effect byte-compile-delete-errors
)
2791 (when (symbolp form
)
2792 (byte-compile-set-symbol-position form
))
2793 (setq for-effect nil
))
2794 (t (byte-compile-variable-ref 'byte-varref form
))))
2795 ((symbolp (car form
))
2796 (let* ((fn (car form
))
2797 (handler (get fn
'byte-compile
)))
2798 (when (byte-compile-const-symbol-p fn
)
2799 (byte-compile-warn "`%s' called as a function" fn
))
2800 (and (memq 'interactive-only byte-compile-warnings
)
2801 (memq fn byte-compile-interactive-only-functions
)
2802 (byte-compile-warn "`%s' used from Lisp code\n\
2803 That command is designed for interactive use only" fn
))
2805 ;; Make sure that function exists. This is important
2806 ;; for CL compiler macros since the symbol may be
2807 ;; `cl-byte-compile-compiler-macro' but if CL isn't
2808 ;; loaded, this function doesn't exist.
2809 (or (not (memq handler
'(cl-byte-compile-compiler-macro)))
2810 (functionp handler
))
2811 (not (and (byte-compile-version-cond
2812 byte-compile-compatibility
)
2813 (get (get fn
'byte-opcode
) 'emacs19-opcode
))))
2814 (funcall handler form
)
2815 (when (memq 'callargs byte-compile-warnings
)
2816 (if (memq fn
'(custom-declare-group custom-declare-variable custom-declare-face
))
2817 (byte-compile-nogroup-warn form
))
2818 (byte-compile-callargs-warn form
))
2819 (byte-compile-normal-call form
))
2820 (if (memq 'cl-functions byte-compile-warnings
)
2821 (byte-compile-cl-warn form
))))
2822 ((and (or (byte-code-function-p (car form
))
2823 (eq (car-safe (car form
)) 'lambda
))
2824 ;; if the form comes out the same way it went in, that's
2825 ;; because it was malformed, and we couldn't unfold it.
2826 (not (eq form
(setq form
(byte-compile-unfold-lambda form
)))))
2827 (byte-compile-form form for-effect
)
2828 (setq for-effect nil
))
2829 ((byte-compile-normal-call form
)))
2831 (byte-compile-discard)))
2833 (defun byte-compile-normal-call (form)
2834 (if byte-compile-generate-call-tree
2835 (byte-compile-annotate-call-tree form
))
2836 (byte-compile-push-constant (car form
))
2837 (mapc 'byte-compile-form
(cdr form
)) ; wasteful, but faster.
2838 (byte-compile-out 'byte-call
(length (cdr form
))))
2840 (defun byte-compile-variable-ref (base-op var
)
2842 (byte-compile-set-symbol-position var
))
2843 (if (or (not (symbolp var
))
2844 (byte-compile-const-symbol-p var
(not (eq base-op
'byte-varref
))))
2846 (cond ((eq base-op
'byte-varbind
) "attempt to let-bind %s `%s'")
2847 ((eq base-op
'byte-varset
) "variable assignment to %s `%s'")
2848 (t "variable reference to %s `%s'"))
2849 (if (symbolp var
) "constant" "nonvariable")
2850 (prin1-to-string var
))
2851 (if (and (get var
'byte-obsolete-variable
)
2852 (memq 'obsolete byte-compile-warnings
)
2853 (not (eq var byte-compile-not-obsolete-var
)))
2854 (let* ((ob (get var
'byte-obsolete-variable
))
2856 (byte-compile-warn "`%s' is an obsolete variable%s; %s" var
2857 (if when
(concat " (as of Emacs " when
")") "")
2858 (if (stringp (car ob
))
2860 (format "use `%s' instead." (car ob
))))))
2861 (if (memq 'free-vars byte-compile-warnings
)
2862 (if (eq base-op
'byte-varbind
)
2863 (push var byte-compile-bound-variables
)
2865 (memq var byte-compile-bound-variables
)
2866 (if (eq base-op
'byte-varset
)
2867 (or (memq var byte-compile-free-assignments
)
2869 (byte-compile-warn "assignment to free variable `%s'" var
)
2870 (push var byte-compile-free-assignments
)))
2871 (or (memq var byte-compile-free-references
)
2873 (byte-compile-warn "reference to free variable `%s'" var
)
2874 (push var byte-compile-free-references
))))))))
2875 (let ((tmp (assq var byte-compile-variables
)))
2877 (setq tmp
(list var
))
2878 (push tmp byte-compile-variables
))
2879 (byte-compile-out base-op tmp
)))
2881 (defmacro byte-compile-get-constant
(const)
2882 `(or (if (stringp ,const
)
2883 ;; In a string constant, treat properties as significant.
2885 (dolist (elt byte-compile-constants
)
2886 (if (equal-including-properties (car elt
) ,const
)
2889 (assq ,const byte-compile-constants
))
2890 (car (setq byte-compile-constants
2891 (cons (list ,const
) byte-compile-constants
)))))
2893 ;; Use this when the value of a form is a constant. This obeys for-effect.
2894 (defun byte-compile-constant (const)
2896 (setq for-effect nil
)
2897 (when (symbolp const
)
2898 (byte-compile-set-symbol-position const
))
2899 (byte-compile-out 'byte-constant
(byte-compile-get-constant const
))))
2901 ;; Use this for a constant that is not the value of its containing form.
2902 ;; This ignores for-effect.
2903 (defun byte-compile-push-constant (const)
2904 (let ((for-effect nil
))
2905 (inline (byte-compile-constant const
))))
2908 ;; Compile those primitive ordinary functions
2909 ;; which have special byte codes just for speed.
2911 (defmacro byte-defop-compiler
(function &optional compile-handler
)
2912 ;; add a compiler-form for FUNCTION.
2913 ;; If function is a symbol, then the variable "byte-SYMBOL" must name
2914 ;; the opcode to be used. If function is a list, the first element
2915 ;; is the function and the second element is the bytecode-symbol.
2916 ;; The second element may be nil, meaning there is no opcode.
2917 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or
2918 ;; may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
2919 ;; If it is nil, then the handler is "byte-compile-SYMBOL."
2921 (if (symbolp function
)
2922 (setq opcode
(intern (concat "byte-" (symbol-name function
))))
2923 (setq opcode
(car (cdr function
))
2924 function
(car function
)))
2926 (list 'put
(list 'quote function
) ''byte-compile
2928 (or (cdr (assq compile-handler
2929 '((0 . byte-compile-no-args
)
2930 (1 . byte-compile-one-arg
)
2931 (2 . byte-compile-two-args
)
2932 (3 . byte-compile-three-args
)
2933 (0-1 . byte-compile-zero-or-one-arg
)
2934 (1-2 . byte-compile-one-or-two-args
)
2935 (2-3 . byte-compile-two-or-three-args
)
2938 (intern (concat "byte-compile-"
2939 (symbol-name function
))))))))
2942 (list 'put
(list 'quote function
)
2943 ''byte-opcode
(list 'quote opcode
))
2944 (list 'put
(list 'quote opcode
)
2945 ''byte-opcode-invert
(list 'quote function
)))
2948 (defmacro byte-defop-compiler19
(function &optional compile-handler
)
2949 ;; Just like byte-defop-compiler, but defines an opcode that will only
2950 ;; be used when byte-compile-compatibility is false.
2951 (if (and (byte-compile-single-version)
2952 byte-compile-compatibility
)
2953 ;; #### instead of doing nothing, this should do some remprops,
2954 ;; #### to protect against the case where a single-version compiler
2955 ;; #### is loaded into a world that has contained a multi-version one.
2960 (or (car (cdr-safe function
))
2961 (intern (concat "byte-"
2962 (symbol-name (or (car-safe function
) function
))))))
2964 (list 'byte-defop-compiler function compile-handler
))))
2966 (defmacro byte-defop-compiler-1
(function &optional compile-handler
)
2967 (list 'byte-defop-compiler
(list function nil
) compile-handler
))
2970 (put 'byte-call
'byte-opcode-invert
'funcall
)
2971 (put 'byte-list1
'byte-opcode-invert
'list
)
2972 (put 'byte-list2
'byte-opcode-invert
'list
)
2973 (put 'byte-list3
'byte-opcode-invert
'list
)
2974 (put 'byte-list4
'byte-opcode-invert
'list
)
2975 (put 'byte-listN
'byte-opcode-invert
'list
)
2976 (put 'byte-concat2
'byte-opcode-invert
'concat
)
2977 (put 'byte-concat3
'byte-opcode-invert
'concat
)
2978 (put 'byte-concat4
'byte-opcode-invert
'concat
)
2979 (put 'byte-concatN
'byte-opcode-invert
'concat
)
2980 (put 'byte-insertN
'byte-opcode-invert
'insert
)
2982 (byte-defop-compiler point
0)
2983 ;;(byte-defop-compiler mark 0) ;; obsolete
2984 (byte-defop-compiler point-max
0)
2985 (byte-defop-compiler point-min
0)
2986 (byte-defop-compiler following-char
0)
2987 (byte-defop-compiler preceding-char
0)
2988 (byte-defop-compiler current-column
0)
2989 (byte-defop-compiler eolp
0)
2990 (byte-defop-compiler eobp
0)
2991 (byte-defop-compiler bolp
0)
2992 (byte-defop-compiler bobp
0)
2993 (byte-defop-compiler current-buffer
0)
2994 ;;(byte-defop-compiler read-char 0) ;; obsolete
2995 (byte-defop-compiler interactive-p
0)
2996 (byte-defop-compiler19 widen
0)
2997 (byte-defop-compiler19 end-of-line
0-
1)
2998 (byte-defop-compiler19 forward-char
0-
1)
2999 (byte-defop-compiler19 forward-line
0-
1)
3000 (byte-defop-compiler symbolp
1)
3001 (byte-defop-compiler consp
1)
3002 (byte-defop-compiler stringp
1)
3003 (byte-defop-compiler listp
1)
3004 (byte-defop-compiler not
1)
3005 (byte-defop-compiler (null byte-not
) 1)
3006 (byte-defop-compiler car
1)
3007 (byte-defop-compiler cdr
1)
3008 (byte-defop-compiler length
1)
3009 (byte-defop-compiler symbol-value
1)
3010 (byte-defop-compiler symbol-function
1)
3011 (byte-defop-compiler (1+ byte-add1
) 1)
3012 (byte-defop-compiler (1- byte-sub1
) 1)
3013 (byte-defop-compiler goto-char
1)
3014 (byte-defop-compiler char-after
0-
1)
3015 (byte-defop-compiler set-buffer
1)
3016 ;;(byte-defop-compiler set-mark 1) ;; obsolete
3017 (byte-defop-compiler19 forward-word
0-
1)
3018 (byte-defop-compiler19 char-syntax
1)
3019 (byte-defop-compiler19 nreverse
1)
3020 (byte-defop-compiler19 car-safe
1)
3021 (byte-defop-compiler19 cdr-safe
1)
3022 (byte-defop-compiler19 numberp
1)
3023 (byte-defop-compiler19 integerp
1)
3024 (byte-defop-compiler19 skip-chars-forward
1-
2)
3025 (byte-defop-compiler19 skip-chars-backward
1-
2)
3026 (byte-defop-compiler eq
2)
3027 (byte-defop-compiler memq
2)
3028 (byte-defop-compiler cons
2)
3029 (byte-defop-compiler aref
2)
3030 (byte-defop-compiler set
2)
3031 (byte-defop-compiler (= byte-eqlsign
) 2)
3032 (byte-defop-compiler (< byte-lss
) 2)
3033 (byte-defop-compiler (> byte-gtr
) 2)
3034 (byte-defop-compiler (<= byte-leq
) 2)
3035 (byte-defop-compiler (>= byte-geq
) 2)
3036 (byte-defop-compiler get
2)
3037 (byte-defop-compiler nth
2)
3038 (byte-defop-compiler substring
2-
3)
3039 (byte-defop-compiler19 (move-marker byte-set-marker
) 2-
3)
3040 (byte-defop-compiler19 set-marker
2-
3)
3041 (byte-defop-compiler19 match-beginning
1)
3042 (byte-defop-compiler19 match-end
1)
3043 (byte-defop-compiler19 upcase
1)
3044 (byte-defop-compiler19 downcase
1)
3045 (byte-defop-compiler19 string
= 2)
3046 (byte-defop-compiler19 string
< 2)
3047 (byte-defop-compiler19 (string-equal byte-string
=) 2)
3048 (byte-defop-compiler19 (string-lessp byte-string
<) 2)
3049 (byte-defop-compiler19 equal
2)
3050 (byte-defop-compiler19 nthcdr
2)
3051 (byte-defop-compiler19 elt
2)
3052 (byte-defop-compiler19 member
2)
3053 (byte-defop-compiler19 assq
2)
3054 (byte-defop-compiler19 (rplaca byte-setcar
) 2)
3055 (byte-defop-compiler19 (rplacd byte-setcdr
) 2)
3056 (byte-defop-compiler19 setcar
2)
3057 (byte-defop-compiler19 setcdr
2)
3058 (byte-defop-compiler19 buffer-substring
2)
3059 (byte-defop-compiler19 delete-region
2)
3060 (byte-defop-compiler19 narrow-to-region
2)
3061 (byte-defop-compiler19 (% byte-rem
) 2)
3062 (byte-defop-compiler aset
3)
3064 (byte-defop-compiler max byte-compile-associative
)
3065 (byte-defop-compiler min byte-compile-associative
)
3066 (byte-defop-compiler (+ byte-plus
) byte-compile-associative
)
3067 (byte-defop-compiler19 (* byte-mult
) byte-compile-associative
)
3069 ;;####(byte-defop-compiler19 move-to-column 1)
3070 (byte-defop-compiler-1 interactive byte-compile-noop
)
3073 (defun byte-compile-subr-wrong-args (form n
)
3074 (byte-compile-set-symbol-position (car form
))
3075 (byte-compile-warn "`%s' called with %d arg%s, but requires %s"
3076 (car form
) (length (cdr form
))
3077 (if (= 1 (length (cdr form
))) "" "s") n
)
3078 ;; get run-time wrong-number-of-args error.
3079 (byte-compile-normal-call form
))
3081 (defun byte-compile-no-args (form)
3082 (if (not (= (length form
) 1))
3083 (byte-compile-subr-wrong-args form
"none")
3084 (byte-compile-out (get (car form
) 'byte-opcode
) 0)))
3086 (defun byte-compile-one-arg (form)
3087 (if (not (= (length form
) 2))
3088 (byte-compile-subr-wrong-args form
1)
3089 (byte-compile-form (car (cdr form
))) ;; Push the argument
3090 (byte-compile-out (get (car form
) 'byte-opcode
) 0)))
3092 (defun byte-compile-two-args (form)
3093 (if (not (= (length form
) 3))
3094 (byte-compile-subr-wrong-args form
2)
3095 (byte-compile-form (car (cdr form
))) ;; Push the arguments
3096 (byte-compile-form (nth 2 form
))
3097 (byte-compile-out (get (car form
) 'byte-opcode
) 0)))
3099 (defun byte-compile-three-args (form)
3100 (if (not (= (length form
) 4))
3101 (byte-compile-subr-wrong-args form
3)
3102 (byte-compile-form (car (cdr form
))) ;; Push the arguments
3103 (byte-compile-form (nth 2 form
))
3104 (byte-compile-form (nth 3 form
))
3105 (byte-compile-out (get (car form
) 'byte-opcode
) 0)))
3107 (defun byte-compile-zero-or-one-arg (form)
3108 (let ((len (length form
)))
3109 (cond ((= len
1) (byte-compile-one-arg (append form
'(nil))))
3110 ((= len
2) (byte-compile-one-arg form
))
3111 (t (byte-compile-subr-wrong-args form
"0-1")))))
3113 (defun byte-compile-one-or-two-args (form)
3114 (let ((len (length form
)))
3115 (cond ((= len
2) (byte-compile-two-args (append form
'(nil))))
3116 ((= len
3) (byte-compile-two-args form
))
3117 (t (byte-compile-subr-wrong-args form
"1-2")))))
3119 (defun byte-compile-two-or-three-args (form)
3120 (let ((len (length form
)))
3121 (cond ((= len
3) (byte-compile-three-args (append form
'(nil))))
3122 ((= len
4) (byte-compile-three-args form
))
3123 (t (byte-compile-subr-wrong-args form
"2-3")))))
3125 (defun byte-compile-noop (form)
3126 (byte-compile-constant nil
))
3128 (defun byte-compile-discard ()
3129 (byte-compile-out 'byte-discard
0))
3132 ;; Compile a function that accepts one or more args and is right-associative.
3133 ;; We do it by left-associativity so that the operations
3134 ;; are done in the same order as in interpreted code.
3135 ;; We treat the one-arg case, as in (+ x), like (+ x 0).
3136 ;; in order to convert markers to numbers, and trigger expected errors.
3137 (defun byte-compile-associative (form)
3139 (let ((opcode (get (car form
) 'byte-opcode
))
3140 (args (copy-sequence (cdr form
))))
3141 (byte-compile-form (car args
))
3142 (setq args
(cdr args
))
3143 (or args
(setq args
'(0)
3144 opcode
(get '+ 'byte-opcode
)))
3146 (byte-compile-form arg
)
3147 (byte-compile-out opcode
0)))
3148 (byte-compile-constant (eval form
))))
3151 ;; more complicated compiler macros
3153 (byte-defop-compiler char-before
)
3154 (byte-defop-compiler backward-char
)
3155 (byte-defop-compiler backward-word
)
3156 (byte-defop-compiler list
)
3157 (byte-defop-compiler concat
)
3158 (byte-defop-compiler fset
)
3159 (byte-defop-compiler (indent-to-column byte-indent-to
) byte-compile-indent-to
)
3160 (byte-defop-compiler indent-to
)
3161 (byte-defop-compiler insert
)
3162 (byte-defop-compiler-1 function byte-compile-function-form
)
3163 (byte-defop-compiler-1 - byte-compile-minus
)
3164 (byte-defop-compiler19 (/ byte-quo
) byte-compile-quo
)
3165 (byte-defop-compiler19 nconc
)
3167 (defun byte-compile-char-before (form)
3168 (cond ((= 2 (length form
))
3169 (byte-compile-form (list 'char-after
(if (numberp (nth 1 form
))
3171 `(1- ,(nth 1 form
))))))
3172 ((= 1 (length form
))
3173 (byte-compile-form '(char-after (1- (point)))))
3174 (t (byte-compile-subr-wrong-args form
"0-1"))))
3176 ;; backward-... ==> forward-... with negated argument.
3177 (defun byte-compile-backward-char (form)
3178 (cond ((= 2 (length form
))
3179 (byte-compile-form (list 'forward-char
(if (numberp (nth 1 form
))
3181 `(- ,(nth 1 form
))))))
3182 ((= 1 (length form
))
3183 (byte-compile-form '(forward-char -
1)))
3184 (t (byte-compile-subr-wrong-args form
"0-1"))))
3186 (defun byte-compile-backward-word (form)
3187 (cond ((= 2 (length form
))
3188 (byte-compile-form (list 'forward-word
(if (numberp (nth 1 form
))
3190 `(- ,(nth 1 form
))))))
3191 ((= 1 (length form
))
3192 (byte-compile-form '(forward-word -
1)))
3193 (t (byte-compile-subr-wrong-args form
"0-1"))))
3195 (defun byte-compile-list (form)
3196 (let ((count (length (cdr form
))))
3198 (byte-compile-constant nil
))
3200 (mapc 'byte-compile-form
(cdr form
))
3202 (aref [byte-list1 byte-list2 byte-list3 byte-list4
] (1- count
)) 0))
3203 ((and (< count
256) (not (byte-compile-version-cond
3204 byte-compile-compatibility
)))
3205 (mapc 'byte-compile-form
(cdr form
))
3206 (byte-compile-out 'byte-listN count
))
3207 (t (byte-compile-normal-call form
)))))
3209 (defun byte-compile-concat (form)
3210 (let ((count (length (cdr form
))))
3211 (cond ((and (< 1 count
) (< count
5))
3212 (mapc 'byte-compile-form
(cdr form
))
3214 (aref [byte-concat2 byte-concat3 byte-concat4
] (- count
2))
3216 ;; Concat of one arg is not a no-op if arg is not a string.
3218 (byte-compile-form ""))
3219 ((and (< count
256) (not (byte-compile-version-cond
3220 byte-compile-compatibility
)))
3221 (mapc 'byte-compile-form
(cdr form
))
3222 (byte-compile-out 'byte-concatN count
))
3223 ((byte-compile-normal-call form
)))))
3225 (defun byte-compile-minus (form)
3226 (if (null (setq form
(cdr form
)))
3227 (byte-compile-constant 0)
3228 (byte-compile-form (car form
))
3230 (while (setq form
(cdr form
))
3231 (byte-compile-form (car form
))
3232 (byte-compile-out 'byte-diff
0))
3233 (byte-compile-out 'byte-negate
0))))
3235 (defun byte-compile-quo (form)
3236 (let ((len (length form
)))
3238 (byte-compile-subr-wrong-args form
"2 or more"))
3240 (byte-compile-form (car (setq form
(cdr form
))))
3241 (while (setq form
(cdr form
))
3242 (byte-compile-form (car form
))
3243 (byte-compile-out 'byte-quo
0))))))
3245 (defun byte-compile-nconc (form)
3246 (let ((len (length form
)))
3248 (byte-compile-constant nil
))
3250 ;; nconc of one arg is a noop, even if that arg isn't a list.
3251 (byte-compile-form (nth 1 form
)))
3253 (byte-compile-form (car (setq form
(cdr form
))))
3254 (while (setq form
(cdr form
))
3255 (byte-compile-form (car form
))
3256 (byte-compile-out 'byte-nconc
0))))))
3258 (defun byte-compile-fset (form)
3259 ;; warn about forms like (fset 'foo '(lambda () ...))
3260 ;; (where the lambda expression is non-trivial...)
3261 (let ((fn (nth 2 form
))
3263 (if (and (eq (car-safe fn
) 'quote
)
3264 (eq (car-safe (setq fn
(nth 1 fn
))) 'lambda
))
3266 (setq body
(cdr (cdr fn
)))
3267 (if (stringp (car body
)) (setq body
(cdr body
)))
3268 (if (eq 'interactive
(car-safe (car body
))) (setq body
(cdr body
)))
3269 (if (and (consp (car body
))
3270 (not (eq 'byte-code
(car (car body
)))))
3272 "A quoted lambda form is the second argument of `fset'. This is probably
3273 not what you want, as that lambda cannot be compiled. Consider using
3274 the syntax (function (lambda (...) ...)) instead.")))))
3275 (byte-compile-two-args form
))
3277 (defun byte-compile-funarg (form)
3278 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
3279 ;; for cases where it's guaranteed that first arg will be used as a lambda.
3280 (byte-compile-normal-call
3281 (let ((fn (nth 1 form
)))
3282 (if (and (eq (car-safe fn
) 'quote
)
3283 (eq (car-safe (nth 1 fn
)) 'lambda
))
3285 (cons (cons 'function
(cdr fn
))
3289 (defun byte-compile-funarg-2 (form)
3290 ;; (sort ... '(lambda (x) ..)) ==> (sort ... (function (lambda (x) ..)))
3291 ;; for cases where it's guaranteed that second arg will be used as a lambda.
3292 (byte-compile-normal-call
3293 (let ((fn (nth 2 form
)))
3294 (if (and (eq (car-safe fn
) 'quote
)
3295 (eq (car-safe (nth 1 fn
)) 'lambda
))
3298 (cons (cons 'function
(cdr fn
))
3299 (cdr (cdr (cdr form
))))))
3302 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
3303 ;; Otherwise it will be incompatible with the interpreter,
3304 ;; and (funcall (function foo)) will lose with autoloads.
3306 (defun byte-compile-function-form (form)
3307 (byte-compile-constant
3308 (cond ((symbolp (nth 1 form
))
3310 ;; If we're not allowed to use #[] syntax, then output a form like
3311 ;; '(lambda (..) (byte-code ..)) instead of a call to make-byte-code.
3312 ;; In this situation, calling make-byte-code at run-time will usually
3313 ;; be less efficient than processing a call to byte-code.
3314 ((byte-compile-version-cond byte-compile-compatibility
)
3315 (byte-compile-byte-code-unmake (byte-compile-lambda (nth 1 form
))))
3316 ((byte-compile-lambda (nth 1 form
))))))
3318 (defun byte-compile-indent-to (form)
3319 (let ((len (length form
)))
3321 (byte-compile-form (car (cdr form
)))
3322 (byte-compile-out 'byte-indent-to
0))
3324 ;; no opcode for 2-arg case.
3325 (byte-compile-normal-call form
))
3327 (byte-compile-subr-wrong-args form
"1-2")))))
3329 (defun byte-compile-insert (form)
3330 (cond ((null (cdr form
))
3331 (byte-compile-constant nil
))
3332 ((and (not (byte-compile-version-cond
3333 byte-compile-compatibility
))
3334 (<= (length form
) 256))
3335 (mapc 'byte-compile-form
(cdr form
))
3336 (if (cdr (cdr form
))
3337 (byte-compile-out 'byte-insertN
(length (cdr form
)))
3338 (byte-compile-out 'byte-insert
0)))
3339 ((memq t
(mapcar 'consp
(cdr (cdr form
))))
3340 (byte-compile-normal-call form
))
3341 ;; We can split it; there is no function call after inserting 1st arg.
3343 (while (setq form
(cdr form
))
3344 (byte-compile-form (car form
))
3345 (byte-compile-out 'byte-insert
0)
3347 (byte-compile-discard))))))
3350 (byte-defop-compiler-1 setq
)
3351 (byte-defop-compiler-1 setq-default
)
3352 (byte-defop-compiler-1 quote
)
3353 (byte-defop-compiler-1 quote-form
)
3355 (defun byte-compile-setq (form)
3356 (let ((args (cdr form
)))
3359 (byte-compile-form (car (cdr args
)))
3360 (or for-effect
(cdr (cdr args
))
3361 (byte-compile-out 'byte-dup
0))
3362 (byte-compile-variable-ref 'byte-varset
(car args
))
3363 (setq args
(cdr (cdr args
))))
3364 ;; (setq), with no arguments.
3365 (byte-compile-form nil for-effect
))
3366 (setq for-effect nil
)))
3368 (defun byte-compile-setq-default (form)
3369 (let ((args (cdr form
))
3373 (cons (list 'set-default
(list 'quote
(car args
)) (car (cdr args
)))
3375 (setq args
(cdr (cdr args
))))
3376 (byte-compile-form (cons 'progn
(nreverse setters
)))))
3378 (defun byte-compile-quote (form)
3379 (byte-compile-constant (car (cdr form
))))
3381 (defun byte-compile-quote-form (form)
3382 (byte-compile-constant (byte-compile-top-level (nth 1 form
))))
3385 ;;; control structures
3387 (defun byte-compile-body (body &optional for-effect
)
3389 (byte-compile-form (car body
) t
)
3390 (setq body
(cdr body
)))
3391 (byte-compile-form (car body
) for-effect
))
3393 (defsubst byte-compile-body-do-effect
(body)
3394 (byte-compile-body body for-effect
)
3395 (setq for-effect nil
))
3397 (defsubst byte-compile-form-do-effect
(form)
3398 (byte-compile-form form for-effect
)
3399 (setq for-effect nil
))
3401 (byte-defop-compiler-1 inline byte-compile-progn
)
3402 (byte-defop-compiler-1 progn
)
3403 (byte-defop-compiler-1 prog1
)
3404 (byte-defop-compiler-1 prog2
)
3405 (byte-defop-compiler-1 if
)
3406 (byte-defop-compiler-1 cond
)
3407 (byte-defop-compiler-1 and
)
3408 (byte-defop-compiler-1 or
)
3409 (byte-defop-compiler-1 while
)
3410 (byte-defop-compiler-1 funcall
)
3411 (byte-defop-compiler-1 apply byte-compile-funarg
)
3412 (byte-defop-compiler-1 mapcar byte-compile-funarg
)
3413 (byte-defop-compiler-1 mapatoms byte-compile-funarg
)
3414 (byte-defop-compiler-1 mapconcat byte-compile-funarg
)
3415 (byte-defop-compiler-1 mapc byte-compile-funarg
)
3416 (byte-defop-compiler-1 maphash byte-compile-funarg
)
3417 (byte-defop-compiler-1 map-char-table byte-compile-funarg
)
3418 (byte-defop-compiler-1 sort byte-compile-funarg-2
)
3419 (byte-defop-compiler-1 let
)
3420 (byte-defop-compiler-1 let
*)
3422 (defun byte-compile-progn (form)
3423 (byte-compile-body-do-effect (cdr form
)))
3425 (defun byte-compile-prog1 (form)
3426 (byte-compile-form-do-effect (car (cdr form
)))
3427 (byte-compile-body (cdr (cdr form
)) t
))
3429 (defun byte-compile-prog2 (form)
3430 (byte-compile-form (nth 1 form
) t
)
3431 (byte-compile-form-do-effect (nth 2 form
))
3432 (byte-compile-body (cdr (cdr (cdr form
))) t
))
3434 (defmacro byte-compile-goto-if
(cond discard tag
)
3437 (if ,discard
'byte-goto-if-not-nil
'byte-goto-if-not-nil-else-pop
)
3438 (if ,discard
'byte-goto-if-nil
'byte-goto-if-nil-else-pop
))
3441 (defmacro byte-compile-maybe-guarded
(condition &rest body
)
3442 "Execute forms in BODY, potentially guarded by CONDITION.
3443 CONDITION is a variable whose value is a test in an `if' or `cond'.
3444 BODY is the code to compile first arm of the if or the body of the
3445 cond clause. If CONDITION's value is of the form (fboundp 'foo)
3446 or (boundp 'foo), the relevant warnings from BODY about foo's
3447 being undefined will be suppressed.
3449 If CONDITION's value is (not (featurep 'emacs)) or (featurep 'xemacs),
3450 that suppresses all warnings during execution of BODY."
3451 (declare (indent 1) (debug t
))
3453 (if (eq 'fboundp
(car-safe ,condition
))
3454 (and (eq 'quote
(car-safe (nth 1 ,condition
)))
3455 ;; Ignore if the symbol is already on the
3457 (not (assq (nth 1 (nth 1 ,condition
)) ; the relevant symbol
3458 byte-compile-unresolved-functions
))
3459 (nth 1 (nth 1 ,condition
)))))
3460 (bound (if (or (eq 'boundp
(car-safe ,condition
))
3461 (eq 'default-boundp
(car-safe ,condition
)))
3462 (and (eq 'quote
(car-safe (nth 1 ,condition
)))
3463 (nth 1 (nth 1 ,condition
)))))
3464 ;; Maybe add to the bound list.
3465 (byte-compile-bound-variables
3467 (cons bound byte-compile-bound-variables
)
3468 byte-compile-bound-variables
))
3469 ;; Suppress all warnings, for code not used in Emacs.
3470 (byte-compile-warnings
3471 (if (member ,condition
'((featurep 'xemacs
)
3472 (not (featurep 'emacs
))))
3473 nil byte-compile-warnings
)))
3476 ;; Maybe remove the function symbol from the unresolved list.
3478 (setq byte-compile-unresolved-functions
3479 (delq (assq fbound byte-compile-unresolved-functions
)
3480 byte-compile-unresolved-functions
))))))
3482 (defun byte-compile-if (form)
3483 (byte-compile-form (car (cdr form
)))
3484 ;; Check whether we have `(if (fboundp ...' or `(if (boundp ...'
3485 ;; and avoid warnings about the relevent symbols in the consequent.
3486 (let ((clause (nth 1 form
))
3487 (donetag (byte-compile-make-tag)))
3488 (if (null (nthcdr 3 form
))
3491 (byte-compile-goto-if nil for-effect donetag
)
3492 (byte-compile-maybe-guarded clause
3493 (byte-compile-form (nth 2 form
) for-effect
))
3494 (byte-compile-out-tag donetag
))
3495 (let ((elsetag (byte-compile-make-tag)))
3496 (byte-compile-goto 'byte-goto-if-nil elsetag
)
3497 (byte-compile-maybe-guarded clause
3498 (byte-compile-form (nth 2 form
) for-effect
))
3499 (byte-compile-goto 'byte-goto donetag
)
3500 (byte-compile-out-tag elsetag
)
3501 (byte-compile-maybe-guarded (list 'not clause
)
3502 (byte-compile-body (cdr (cdr (cdr form
))) for-effect
))
3503 (byte-compile-out-tag donetag
))))
3504 (setq for-effect nil
))
3506 (defun byte-compile-cond (clauses)
3507 (let ((donetag (byte-compile-make-tag))
3509 (while (setq clauses
(cdr clauses
))
3510 (setq clause
(car clauses
))
3511 (cond ((or (eq (car clause
) t
)
3512 (and (eq (car-safe (car clause
)) 'quote
)
3513 (car-safe (cdr-safe (car clause
)))))
3514 ;; Unconditional clause
3515 (setq clause
(cons t clause
)
3518 (byte-compile-form (car clause
))
3519 (if (null (cdr clause
))
3520 ;; First clause is a singleton.
3521 (byte-compile-goto-if t for-effect donetag
)
3522 (setq nexttag
(byte-compile-make-tag))
3523 (byte-compile-goto 'byte-goto-if-nil nexttag
)
3524 (byte-compile-maybe-guarded (car clause
)
3525 (byte-compile-body (cdr clause
) for-effect
))
3526 (byte-compile-goto 'byte-goto donetag
)
3527 (byte-compile-out-tag nexttag
)))))
3529 (let ((guard (car clause
)))
3530 (and (cdr clause
) (not (eq guard t
))
3531 (progn (byte-compile-form guard
)
3532 (byte-compile-goto-if nil for-effect donetag
)
3533 (setq clause
(cdr clause
))))
3534 (byte-compile-maybe-guarded guard
3535 (byte-compile-body-do-effect clause
)))
3536 (byte-compile-out-tag donetag
)))
3538 (defun byte-compile-and (form)
3539 (let ((failtag (byte-compile-make-tag))
3542 (byte-compile-form-do-effect t
)
3543 (byte-compile-and-recursion args failtag
))))
3545 ;; Handle compilation of a nontrivial `and' call.
3546 ;; We use tail recursion so we can use byte-compile-maybe-guarded.
3547 (defun byte-compile-and-recursion (rest failtag
)
3550 (byte-compile-form (car rest
))
3551 (byte-compile-goto-if nil for-effect failtag
)
3552 (byte-compile-maybe-guarded (car rest
)
3553 (byte-compile-and-recursion (cdr rest
) failtag
)))
3554 (byte-compile-form-do-effect (car rest
))
3555 (byte-compile-out-tag failtag
)))
3557 (defun byte-compile-or (form)
3558 (let ((wintag (byte-compile-make-tag))
3561 (byte-compile-form-do-effect nil
)
3562 (byte-compile-or-recursion args wintag
))))
3564 ;; Handle compilation of a nontrivial `or' call.
3565 ;; We use tail recursion so we can use byte-compile-maybe-guarded.
3566 (defun byte-compile-or-recursion (rest wintag
)
3569 (byte-compile-form (car rest
))
3570 (byte-compile-goto-if t for-effect wintag
)
3571 (byte-compile-maybe-guarded (list 'not
(car rest
))
3572 (byte-compile-or-recursion (cdr rest
) wintag
)))
3573 (byte-compile-form-do-effect (car rest
))
3574 (byte-compile-out-tag wintag
)))
3576 (defun byte-compile-while (form)
3577 (let ((endtag (byte-compile-make-tag))
3578 (looptag (byte-compile-make-tag)))
3579 (byte-compile-out-tag looptag
)
3580 (byte-compile-form (car (cdr form
)))
3581 (byte-compile-goto-if nil for-effect endtag
)
3582 (byte-compile-body (cdr (cdr form
)) t
)
3583 (byte-compile-goto 'byte-goto looptag
)
3584 (byte-compile-out-tag endtag
)
3585 (setq for-effect nil
)))
3587 (defun byte-compile-funcall (form)
3588 (mapc 'byte-compile-form
(cdr form
))
3589 (byte-compile-out 'byte-call
(length (cdr (cdr form
)))))
3592 (defun byte-compile-let (form)
3593 ;; First compute the binding values in the old scope.
3594 (let ((varlist (car (cdr form
))))
3595 (dolist (var varlist
)
3597 (byte-compile-form (car (cdr var
)))
3598 (byte-compile-push-constant nil
))))
3599 (let ((byte-compile-bound-variables byte-compile-bound-variables
) ;new scope
3600 (varlist (reverse (car (cdr form
)))))
3601 (dolist (var varlist
)
3602 (byte-compile-variable-ref 'byte-varbind
(if (consp var
) (car var
) var
)))
3603 (byte-compile-body-do-effect (cdr (cdr form
)))
3604 (byte-compile-out 'byte-unbind
(length (car (cdr form
))))))
3606 (defun byte-compile-let* (form)
3607 (let ((byte-compile-bound-variables byte-compile-bound-variables
) ;new scope
3608 (varlist (copy-sequence (car (cdr form
)))))
3609 (dolist (var varlist
)
3611 (byte-compile-push-constant nil
)
3612 (byte-compile-form (car (cdr var
)))
3613 (setq var
(car var
)))
3614 (byte-compile-variable-ref 'byte-varbind var
))
3615 (byte-compile-body-do-effect (cdr (cdr form
)))
3616 (byte-compile-out 'byte-unbind
(length (car (cdr form
))))))
3619 (byte-defop-compiler-1 /= byte-compile-negated
)
3620 (byte-defop-compiler-1 atom byte-compile-negated
)
3621 (byte-defop-compiler-1 nlistp byte-compile-negated
)
3623 (put '/= 'byte-compile-negated-op
'=)
3624 (put 'atom
'byte-compile-negated-op
'consp
)
3625 (put 'nlistp
'byte-compile-negated-op
'listp
)
3627 (defun byte-compile-negated (form)
3628 (byte-compile-form-do-effect (byte-compile-negation-optimizer form
)))
3630 ;; Even when optimization is off, /= is optimized to (not (= ...)).
3631 (defun byte-compile-negation-optimizer (form)
3632 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
3633 (byte-compile-set-symbol-position (car form
))
3635 (cons (or (get (car form
) 'byte-compile-negated-op
)
3637 "Compiler error: `%s' has no `byte-compile-negated-op' property"
3641 ;;; other tricky macro-like special-forms
3643 (byte-defop-compiler-1 catch
)
3644 (byte-defop-compiler-1 unwind-protect
)
3645 (byte-defop-compiler-1 condition-case
)
3646 (byte-defop-compiler-1 save-excursion
)
3647 (byte-defop-compiler-1 save-current-buffer
)
3648 (byte-defop-compiler-1 save-restriction
)
3649 (byte-defop-compiler-1 save-window-excursion
)
3650 (byte-defop-compiler-1 with-output-to-temp-buffer
)
3651 (byte-defop-compiler-1 track-mouse
)
3653 (defun byte-compile-catch (form)
3654 (byte-compile-form (car (cdr form
)))
3655 (byte-compile-push-constant
3656 (byte-compile-top-level (cons 'progn
(cdr (cdr form
))) for-effect
))
3657 (byte-compile-out 'byte-catch
0))
3659 (defun byte-compile-unwind-protect (form)
3660 (byte-compile-push-constant
3661 (byte-compile-top-level-body (cdr (cdr form
)) t
))
3662 (byte-compile-out 'byte-unwind-protect
0)
3663 (byte-compile-form-do-effect (car (cdr form
)))
3664 (byte-compile-out 'byte-unbind
1))
3666 (defun byte-compile-track-mouse (form)
3668 `(funcall '(lambda nil
3669 (track-mouse ,@(byte-compile-top-level-body (cdr form
)))))))
3671 (defun byte-compile-condition-case (form)
3672 (let* ((var (nth 1 form
))
3673 (byte-compile-bound-variables
3674 (if var
(cons var byte-compile-bound-variables
)
3675 byte-compile-bound-variables
)))
3676 (byte-compile-set-symbol-position 'condition-case
)
3677 (unless (symbolp var
)
3679 "`%s' is not a variable-name or nil (in condition-case)" var
))
3680 (byte-compile-push-constant var
)
3681 (byte-compile-push-constant (byte-compile-top-level
3682 (nth 2 form
) for-effect
))
3683 (let ((clauses (cdr (cdr (cdr form
))))
3686 (let* ((clause (car clauses
))
3687 (condition (car clause
)))
3688 (cond ((not (or (symbolp condition
)
3689 (and (listp condition
)
3690 (let ((syms condition
) (ok t
))
3692 (if (not (symbolp (car syms
)))
3694 (setq syms
(cdr syms
)))
3697 "`%s' is not a condition name or list of such (in condition-case)"
3698 (prin1-to-string condition
)))
3699 ;; ((not (or (eq condition 't)
3700 ;; (and (stringp (get condition 'error-message))
3701 ;; (consp (get condition 'error-conditions)))))
3702 ;; (byte-compile-warn
3703 ;; "`%s' is not a known condition name (in condition-case)"
3706 (setq compiled-clauses
3707 (cons (cons condition
3708 (byte-compile-top-level-body
3709 (cdr clause
) for-effect
))
3711 (setq clauses
(cdr clauses
)))
3712 (byte-compile-push-constant (nreverse compiled-clauses
)))
3713 (byte-compile-out 'byte-condition-case
0)))
3716 (defun byte-compile-save-excursion (form)
3717 (byte-compile-out 'byte-save-excursion
0)
3718 (byte-compile-body-do-effect (cdr form
))
3719 (byte-compile-out 'byte-unbind
1))
3721 (defun byte-compile-save-restriction (form)
3722 (byte-compile-out 'byte-save-restriction
0)
3723 (byte-compile-body-do-effect (cdr form
))
3724 (byte-compile-out 'byte-unbind
1))
3726 (defun byte-compile-save-current-buffer (form)
3727 (byte-compile-out 'byte-save-current-buffer
0)
3728 (byte-compile-body-do-effect (cdr form
))
3729 (byte-compile-out 'byte-unbind
1))
3731 (defun byte-compile-save-window-excursion (form)
3732 (byte-compile-push-constant
3733 (byte-compile-top-level-body (cdr form
) for-effect
))
3734 (byte-compile-out 'byte-save-window-excursion
0))
3736 (defun byte-compile-with-output-to-temp-buffer (form)
3737 (byte-compile-form (car (cdr form
)))
3738 (byte-compile-out 'byte-temp-output-buffer-setup
0)
3739 (byte-compile-body (cdr (cdr form
)))
3740 (byte-compile-out 'byte-temp-output-buffer-show
0))
3742 ;;; top-level forms elsewhere
3744 (byte-defop-compiler-1 defun
)
3745 (byte-defop-compiler-1 defmacro
)
3746 (byte-defop-compiler-1 defvar
)
3747 (byte-defop-compiler-1 defconst byte-compile-defvar
)
3748 (byte-defop-compiler-1 autoload
)
3749 (byte-defop-compiler-1 lambda byte-compile-lambda-form
)
3751 (defun byte-compile-defun (form)
3752 ;; This is not used for file-level defuns with doc strings.
3753 (if (symbolp (car form
))
3754 (byte-compile-set-symbol-position (car form
))
3755 (byte-compile-set-symbol-position 'defun
)
3756 (error "defun name must be a symbol, not %s" (car form
)))
3757 (if (byte-compile-version-cond byte-compile-compatibility
)
3759 (byte-compile-two-args ; Use this to avoid byte-compile-fset's warning.
3761 (list 'quote
(nth 1 form
))
3762 (byte-compile-byte-code-maker
3763 (byte-compile-lambda (cdr (cdr form
)) t
))))
3764 (byte-compile-discard))
3765 ;; We prefer to generate a defalias form so it will record the function
3766 ;; definition just like interpreting a defun.
3769 (list 'quote
(nth 1 form
))
3770 (byte-compile-byte-code-maker
3771 (byte-compile-lambda (cdr (cdr form
)) t
)))
3773 (byte-compile-constant (nth 1 form
)))
3775 (defun byte-compile-defmacro (form)
3776 ;; This is not used for file-level defmacros with doc strings.
3777 (byte-compile-body-do-effect
3778 (list (list 'fset
(list 'quote
(nth 1 form
))
3779 (let ((code (byte-compile-byte-code-maker
3780 (byte-compile-lambda (cdr (cdr form
)) t
))))
3781 (if (eq (car-safe code
) 'make-byte-code
)
3782 (list 'cons
''macro code
)
3783 (list 'quote
(cons 'macro
(eval code
))))))
3784 (list 'quote
(nth 1 form
)))))
3786 (defun byte-compile-defvar (form)
3787 ;; This is not used for file-level defvar/consts with doc strings.
3788 (let ((fun (nth 0 form
))
3790 (value (nth 2 form
))
3791 (string (nth 3 form
)))
3792 (byte-compile-set-symbol-position fun
)
3793 (when (or (> (length form
) 4)
3794 (and (eq fun
'defconst
) (null (cddr form
))))
3795 (let ((ncall (length (cdr form
))))
3797 "`%s' called with %d argument%s, but %s %s"
3799 (if (= 1 ncall
) "" "s")
3800 (if (< ncall
2) "requires" "accepts only")
3802 (when (memq 'free-vars byte-compile-warnings
)
3803 (push var byte-compile-bound-variables
)
3804 (if (eq fun
'defconst
)
3805 (push var byte-compile-const-variables
)))
3806 (byte-compile-body-do-effect
3808 ;; Put the defined variable in this library's load-history entry
3809 ;; just as a real defvar would, but only in top-level forms.
3810 (when (and (cddr form
) (null byte-compile-current-form
))
3811 `(push ',var current-load-list
))
3812 (when (> (length form
) 3)
3813 (when (and string
(not (stringp string
)))
3814 (byte-compile-warn "third arg to `%s %s' is not a string: %s"
3816 `(put ',var
'variable-documentation
,string
))
3817 (if (cddr form
) ; `value' provided
3818 (let ((byte-compile-not-obsolete-var var
))
3819 (if (eq fun
'defconst
)
3820 ;; `defconst' sets `var' unconditionally.
3821 (let ((tmp (make-symbol "defconst-tmp-var")))
3822 `(funcall '(lambda (,tmp
) (defconst ,var
,tmp
))
3824 ;; `defvar' sets `var' only when unbound.
3825 `(if (not (default-boundp ',var
)) (setq-default ,var
,value
))))
3826 (when (eq fun
'defconst
)
3827 ;; This will signal an appropriate error at runtime.
3831 (defun byte-compile-autoload (form)
3832 (byte-compile-set-symbol-position 'autoload
)
3833 (and (byte-compile-constp (nth 1 form
))
3834 (byte-compile-constp (nth 5 form
))
3835 (eval (nth 5 form
)) ; macro-p
3836 (not (fboundp (eval (nth 1 form
))))
3838 "The compiler ignores `autoload' except at top level. You should
3839 probably put the autoload of the macro `%s' at top-level."
3840 (eval (nth 1 form
))))
3841 (byte-compile-normal-call form
))
3843 ;; Lambdas in valid places are handled as special cases by various code.
3844 ;; The ones that remain are errors.
3845 (defun byte-compile-lambda-form (form)
3846 (byte-compile-set-symbol-position 'lambda
)
3847 (error "`lambda' used as function name is invalid"))
3849 ;; Compile normally, but deal with warnings for the function being defined.
3850 (put 'defalias
'byte-hunk-handler
'byte-compile-file-form-defalias
)
3851 (defun byte-compile-file-form-defalias (form)
3852 (if (and (consp (cdr form
)) (consp (nth 1 form
))
3853 (eq (car (nth 1 form
)) 'quote
)
3854 (consp (cdr (nth 1 form
)))
3855 (symbolp (nth 1 (nth 1 form
))))
3857 (and (consp (nthcdr 2 form
))
3858 (consp (nth 2 form
))
3859 (eq (car (nth 2 form
)) 'quote
)
3860 (consp (cdr (nth 2 form
)))
3861 (symbolp (nth 1 (nth 2 form
))))))
3862 (byte-compile-defalias-warn (nth 1 (nth 1 form
)))
3863 (push (cons (nth 1 (nth 1 form
))
3864 (if constant
(nth 1 (nth 2 form
)) t
))
3865 byte-compile-function-environment
)))
3866 ;; We used to jus do: (byte-compile-normal-call form)
3867 ;; But it turns out that this fails to optimize the code.
3868 ;; So instead we now do the same as what other byte-hunk-handlers do,
3869 ;; which is to call back byte-compile-file-form and then return nil.
3870 ;; Except that we can't just call byte-compile-file-form since it would
3871 ;; call us right back.
3872 (byte-compile-keep-pending form
)
3873 ;; Return nil so the form is not output twice.
3876 ;; Turn off warnings about prior calls to the function being defalias'd.
3877 ;; This could be smarter and compare those calls with
3878 ;; the function it is being aliased to.
3879 (defun byte-compile-defalias-warn (new)
3880 (let ((calls (assq new byte-compile-unresolved-functions
)))
3882 (setq byte-compile-unresolved-functions
3883 (delq calls byte-compile-unresolved-functions
)))))
3885 (byte-defop-compiler-1 with-no-warnings byte-compile-no-warnings
)
3886 (defun byte-compile-no-warnings (form)
3887 (let (byte-compile-warnings)
3888 (byte-compile-form (cons 'progn
(cdr form
)))))
3890 ;; Warn about misuses of make-variable-buffer-local.
3891 (byte-defop-compiler-1 make-variable-buffer-local byte-compile-make-variable-buffer-local
)
3892 (defun byte-compile-make-variable-buffer-local (form)
3893 (if (and (eq (car-safe (car-safe (cdr-safe form
))) 'quote
)
3894 (memq 'make-local byte-compile-warnings
))
3896 "`make-variable-buffer-local' should be called at toplevel"))
3897 (byte-compile-normal-call form
))
3898 (put 'make-variable-buffer-local
3899 'byte-hunk-handler
'byte-compile-form-make-variable-buffer-local
)
3900 (defun byte-compile-form-make-variable-buffer-local (form)
3901 (byte-compile-keep-pending form
'byte-compile-normal-call
))
3906 ;; Note: Most operations will strip off the 'TAG, but it speeds up
3907 ;; optimization to have the 'TAG as a part of the tag.
3908 ;; Tags will be (TAG . (tag-number . stack-depth)).
3909 (defun byte-compile-make-tag ()
3910 (list 'TAG
(setq byte-compile-tag-number
(1+ byte-compile-tag-number
))))
3913 (defun byte-compile-out-tag (tag)
3914 (setq byte-compile-output
(cons tag byte-compile-output
))
3917 ;; ## remove this someday
3918 (and byte-compile-depth
3919 (not (= (cdr (cdr tag
)) byte-compile-depth
))
3920 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag
))))
3921 (setq byte-compile-depth
(cdr (cdr tag
))))
3922 (setcdr (cdr tag
) byte-compile-depth
)))
3924 (defun byte-compile-goto (opcode tag
)
3925 (push (cons opcode tag
) byte-compile-output
)
3926 (setcdr (cdr tag
) (if (memq opcode byte-goto-always-pop-ops
)
3927 (1- byte-compile-depth
)
3928 byte-compile-depth
))
3929 (setq byte-compile-depth
(and (not (eq opcode
'byte-goto
))
3930 (1- byte-compile-depth
))))
3932 (defun byte-compile-out (opcode offset
)
3933 (push (cons opcode offset
) byte-compile-output
)
3934 (cond ((eq opcode
'byte-call
)
3935 (setq byte-compile-depth
(- byte-compile-depth offset
)))
3936 ((eq opcode
'byte-return
)
3937 ;; This is actually an unnecessary case, because there should be
3938 ;; no more opcodes behind byte-return.
3939 (setq byte-compile-depth nil
))
3941 (setq byte-compile-depth
(+ byte-compile-depth
3942 (or (aref byte-stack
+-info
3943 (symbol-value opcode
))
3945 byte-compile-maxdepth
(max byte-compile-depth
3946 byte-compile-maxdepth
))))
3947 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
3953 (defun byte-compile-annotate-call-tree (form)
3955 ;; annotate the current call
3956 (if (setq entry
(assq (car form
) byte-compile-call-tree
))
3957 (or (memq byte-compile-current-form
(nth 1 entry
)) ;callers
3959 (cons byte-compile-current-form
(nth 1 entry
))))
3960 (setq byte-compile-call-tree
3961 (cons (list (car form
) (list byte-compile-current-form
) nil
)
3962 byte-compile-call-tree
)))
3963 ;; annotate the current function
3964 (if (setq entry
(assq byte-compile-current-form byte-compile-call-tree
))
3965 (or (memq (car form
) (nth 2 entry
)) ;called
3966 (setcar (cdr (cdr entry
))
3967 (cons (car form
) (nth 2 entry
))))
3968 (setq byte-compile-call-tree
3969 (cons (list byte-compile-current-form nil
(list (car form
)))
3970 byte-compile-call-tree
)))
3973 ;; Renamed from byte-compile-report-call-tree
3974 ;; to avoid interfering with completion of byte-compile-file.
3976 (defun display-call-tree (&optional filename
)
3977 "Display a call graph of a specified file.
3978 This lists which functions have been called, what functions called
3979 them, and what functions they call. The list includes all functions
3980 whose definitions have been compiled in this Emacs session, as well as
3981 all functions called by those functions.
3983 The call graph does not include macros, inline functions, or
3984 primitives that the byte-code interpreter knows about directly \(eq,
3987 The call tree also lists those functions which are not known to be called
3988 \(that is, to which no calls have been compiled\), and which cannot be
3989 invoked interactively."
3991 (message "Generating call tree...")
3992 (with-output-to-temp-buffer "*Call-Tree*"
3993 (set-buffer "*Call-Tree*")
3995 (message "Generating call tree... (sorting on %s)"
3996 byte-compile-call-tree-sort
)
3997 (insert "Call tree for "
3998 (cond ((null byte-compile-current-file
) (or filename
"???"))
3999 ((stringp byte-compile-current-file
)
4000 byte-compile-current-file
)
4001 (t (buffer-name byte-compile-current-file
)))
4003 (prin1-to-string byte-compile-call-tree-sort
)
4005 (if byte-compile-call-tree-sort
4006 (setq byte-compile-call-tree
4007 (sort byte-compile-call-tree
4008 (cond ((eq byte-compile-call-tree-sort
'callers
)
4009 (function (lambda (x y
) (< (length (nth 1 x
))
4010 (length (nth 1 y
))))))
4011 ((eq byte-compile-call-tree-sort
'calls
)
4012 (function (lambda (x y
) (< (length (nth 2 x
))
4013 (length (nth 2 y
))))))
4014 ((eq byte-compile-call-tree-sort
'calls
+callers
)
4015 (function (lambda (x y
) (< (+ (length (nth 1 x
))
4017 (+ (length (nth 1 y
))
4018 (length (nth 2 y
)))))))
4019 ((eq byte-compile-call-tree-sort
'name
)
4020 (function (lambda (x y
) (string< (car x
)
4022 (t (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
4023 byte-compile-call-tree-sort
))))))
4024 (message "Generating call tree...")
4025 (let ((rest byte-compile-call-tree
)
4026 (b (current-buffer))
4030 (prin1 (car (car rest
)) b
)
4031 (setq callers
(nth 1 (car rest
))
4032 calls
(nth 2 (car rest
)))
4034 (cond ((not (fboundp (setq f
(car (car rest
)))))
4036 " <top level>";; shouldn't insert nil then, actually -sk
4038 ((subrp (setq f
(symbol-function f
)))
4041 (format " ==> %s" f
))
4042 ((byte-code-function-p f
)
4043 "<compiled function>")
4045 "<malformed function>")
4046 ((eq 'macro
(car f
))
4047 (if (or (byte-code-function-p (cdr f
))
4048 (assq 'byte-code
(cdr (cdr (cdr f
)))))
4051 ((assq 'byte-code
(cdr (cdr f
)))
4052 "<compiled lambda>")
4053 ((eq 'lambda
(car f
))
4056 (format " (%d callers + %d calls = %d)"
4057 ;; Does the optimizer eliminate common subexpressions?-sk
4060 (+ (length callers
) (length calls
)))
4064 (insert " called by:\n")
4066 (insert " " (if (car callers
)
4067 (mapconcat 'symbol-name callers
", ")
4069 (let ((fill-prefix " "))
4070 (fill-region-as-paragraph p
(point)))
4071 (unless (= 0 (current-column))
4075 (insert " calls:\n")
4077 (insert " " (mapconcat 'symbol-name calls
", "))
4078 (let ((fill-prefix " "))
4079 (fill-region-as-paragraph p
(point)))
4080 (unless (= 0 (current-column))
4082 (setq rest
(cdr rest
)))
4084 (message "Generating call tree...(finding uncalled functions...)")
4085 (setq rest byte-compile-call-tree
)
4086 (let ((uncalled nil
))
4088 (or (nth 1 (car rest
))
4089 (null (setq f
(car (car rest
))))
4090 (functionp (byte-compile-fdefinition f t
))
4091 (commandp (byte-compile-fdefinition f nil
))
4092 (setq uncalled
(cons f uncalled
)))
4093 (setq rest
(cdr rest
)))
4095 (let ((fill-prefix " "))
4096 (insert "Noninteractive functions not known to be called:\n ")
4098 (insert (mapconcat 'symbol-name
(nreverse uncalled
) ", "))
4099 (fill-region-as-paragraph p
(point)))))
4101 (message "Generating call tree...done.")
4106 (defun batch-byte-compile-if-not-done ()
4107 "Like `byte-compile-file' but doesn't recompile if already up to date.
4108 Use this from the command line, with `-batch';
4109 it won't work in an interactive Emacs."
4110 (batch-byte-compile t
))
4112 ;;; by crl@newton.purdue.edu
4113 ;;; Only works noninteractively.
4115 (defun batch-byte-compile (&optional noforce
)
4116 "Run `byte-compile-file' on the files remaining on the command line.
4117 Use this from the command line, with `-batch';
4118 it won't work in an interactive Emacs.
4119 Each file is processed even if an error occurred previously.
4120 For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\".
4121 If NOFORCE is non-nil, don't recompile a file that seems to be
4122 already up-to-date."
4123 ;; command-line-args-left is what is left of the command line (from startup.el)
4124 (defvar command-line-args-left
) ;Avoid 'free variable' warning
4125 (if (not noninteractive
)
4126 (error "`batch-byte-compile' is to be used only with -batch"))
4128 (while command-line-args-left
4129 (if (file-directory-p (expand-file-name (car command-line-args-left
)))
4130 ;; Directory as argument.
4131 (let ((files (directory-files (car command-line-args-left
)))
4133 (dolist (file files
)
4134 (if (and (string-match emacs-lisp-file-regexp file
)
4135 (not (auto-save-file-name-p file
))
4136 (setq source
(expand-file-name file
4137 (car command-line-args-left
)))
4138 (setq dest
(byte-compile-dest-file source
))
4139 (file-exists-p dest
)
4140 (file-newer-than-file-p source dest
))
4141 (if (null (batch-byte-compile-file source
))
4143 ;; Specific file argument
4144 (if (or (not noforce
)
4145 (let* ((source (car command-line-args-left
))
4146 (dest (byte-compile-dest-file source
)))
4147 (or (not (file-exists-p dest
))
4148 (file-newer-than-file-p source dest
))))
4149 (if (null (batch-byte-compile-file (car command-line-args-left
)))
4151 (setq command-line-args-left
(cdr command-line-args-left
)))
4152 (kill-emacs (if error
1 0))))
4154 (defun batch-byte-compile-file (file)
4156 (byte-compile-file file
)
4158 (byte-compile-file file
)
4160 (message (if (cdr err
)
4161 ">>Error occurred processing %s: %s (%s)"
4162 ">>Error occurred processing %s: %s")
4164 (get (car err
) 'error-message
)
4165 (prin1-to-string (cdr err
)))
4166 (let ((destfile (byte-compile-dest-file file
)))
4167 (if (file-exists-p destfile
)
4168 (delete-file destfile
)))
4171 (message (if (cdr err
)
4172 ">>Error occurred processing %s: %s (%s)"
4173 ">>Error occurred processing %s: %s")
4175 (get (car err
) 'error-message
)
4176 (prin1-to-string (cdr err
)))
4180 (defun batch-byte-recompile-directory (&optional arg
)
4181 "Run `byte-recompile-directory' on the dirs remaining on the command line.
4182 Must be used only with `-batch', and kills Emacs on completion.
4183 For example, invoke `emacs -batch -f batch-byte-recompile-directory .'.
4185 Optional argument ARG is passed as second argument ARG to
4186 `batch-recompile-directory'; see there for its possible values
4187 and corresponding effects."
4188 ;; command-line-args-left is what is left of the command line (startup.el)
4189 (defvar command-line-args-left
) ;Avoid 'free variable' warning
4190 (if (not noninteractive
)
4191 (error "batch-byte-recompile-directory is to be used only with -batch"))
4192 (or command-line-args-left
4193 (setq command-line-args-left
'(".")))
4194 (while command-line-args-left
4195 (byte-recompile-directory (car command-line-args-left
) arg
)
4196 (setq command-line-args-left
(cdr command-line-args-left
)))
4199 (provide 'byte-compile
)
4203 ;;; report metering (see the hacks in bytecode.c)
4205 (defvar byte-code-meter
)
4206 (defun byte-compile-report-ops ()
4207 (with-output-to-temp-buffer "*Meter*"
4208 (set-buffer "*Meter*")
4209 (let ((i 0) n op off
)
4211 (setq n
(aref (aref byte-code-meter
0) i
)
4213 (if t
;(not (zerop n))
4217 (cond ((< op byte-nth
)
4218 (setq off
(logand op
7))
4219 (setq op
(logand op
248)))
4220 ((>= op byte-constant
)
4221 (setq off
(- op byte-constant
)
4223 (setq op
(aref byte-code-vector op
))
4224 (insert (format "%-4d" i
))
4225 (insert (symbol-name op
))
4226 (if off
(insert " [" (int-to-string off
) "]"))
4228 (insert (int-to-string n
) "\n")))
4231 ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
4232 ;; itself, compile some of its most used recursive functions (at load time).
4235 (or (byte-code-function-p (symbol-function 'byte-compile-form
))
4236 (assq 'byte-code
(symbol-function 'byte-compile-form
))
4237 (let ((byte-optimize nil
) ; do it fast
4238 (byte-compile-warnings nil
))
4240 (or noninteractive
(message "compiling %s..." x
))
4242 (or noninteractive
(message "compiling %s...done" x
)))
4243 '(byte-compile-normal-call
4246 ;; Inserted some more than necessary, to speed it up.
4247 byte-compile-top-level
4248 byte-compile-out-toplevel
4249 byte-compile-constant
4250 byte-compile-variable-ref
))))
4253 (run-hooks 'bytecomp-load-hook
)
4255 ;; arch-tag: 9c97b0f0-8745-4571-bfc3-8dceb677292a
4256 ;;; bytecomp.el ends here