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