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