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