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