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