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