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